Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add savedBattlerAttacker and stack for saved target/attacker #4061

Merged
merged 8 commits into from
May 29, 2024

Conversation

ghoulslash
Copy link
Collaborator

Several effects have had issues overwriting gBattlerAttacker/gBattlerTarget in the past and present. We should move away from backing up battler IDs into other id slots by using savetarget/saveattacker. So I added a stack for redundancy. An example of one possible issue would be backing up a battler id to saved battler and then having a chaining effect that also saved a different battler id to saved battler. The top-most call stack would restore the incorrect battler id.

Copy link
Collaborator

@DizzyEggg DizzyEggg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good changes, should be merged soon.

include/battle.h Outdated Show resolved Hide resolved
src/battle_script_commands.c Outdated Show resolved Hide resolved
src/battle_script_commands.c Outdated Show resolved Hide resolved
@ghoulslash
Copy link
Collaborator Author

Added requisite safety checks. Also added debug warning statements as failsafe checks. Added a final safety check in MOVEEND_CLEAR_BITS to ensure all saved attackers/targets were restored within a given turn. Ready for re review

@mrgriffin
Copy link
Collaborator

Also added debug warning statements as failsafe checks.

Good idea! Would it be possible to turn them into errors in tests? I think something like:

#if TESTING
Test_ExitWithResult(TEST_RESULT_ERROR, "BS_RestoreTarget attempting to restore an empty target!");
#else
DebugPrintfLevel(MGBA_LOG_WARN, "BS_RestoreTarget attempting to restore an empty target!");
#endif

With a:

#if TESTING
#include "test/test.h"
#endif

Ought to work. Annoyingly Test_ExitWithResult only exists if TESTING is defined, so we can't avoid the preprocessor...

Maybe in the future we should use AGB_ASSERT for these sorts of things, and then make that responsible for calling Test_ExitWithResult if TESTING is set (and doing the current behavior of either aborting or doing nothing if not, depending on NDEBUG).

@ghoulslash
Copy link
Collaborator Author

Also added debug warning statements as failsafe checks.

Good idea! Would it be possible to turn them into errors in tests? I think something like:

#if TESTING
Test_ExitWithResult(TEST_RESULT_ERROR, "BS_RestoreTarget attempting to restore an empty target!");
#else
DebugPrintfLevel(MGBA_LOG_WARN, "BS_RestoreTarget attempting to restore an empty target!");
#endif

With a:

#if TESTING
#include "test/test.h"
#endif

Ought to work. Annoyingly Test_ExitWithResult only exists if TESTING is defined, so we can't avoid the preprocessor...

Maybe in the future we should use AGB_ASSERT for these sorts of things, and then make that responsible for calling Test_ExitWithResult if TESTING is set (and doing the current behavior of either aborting or doing nothing if not, depending on NDEBUG).

good suggestion. Just pushed these changes. Also added to the checks in MOVEEND_CLEAR_BITS. In my project I use a lot of asserts, but warnings may be better because assertion fails just shut down mgba. But discussion on that is probably better on discord.
Anyway, ready for review again

@ghoulslash
Copy link
Collaborator Author

This keeps failing with [Test_ExitWithResult](error: implicit declaration of function 'Test_ExitWithResult' [-Werror=implicit-function-declaration]) but builds locally

@AlexOn1ine
Copy link
Collaborator

This keeps failing with [Test_ExitWithResult](error: implicit declaration of function 'Test_ExitWithResult' [-Werror=implicit-function-declaration]) but builds locally

It doesn't build with make -jN check. I think you forgot to include the test runner in battle scripts commands

@ghoulslash
Copy link
Collaborator Author

This keeps failing with [Test_ExitWithResult](error: implicit declaration of function 'Test_ExitWithResult' [-Werror=implicit-function-declaration]) but builds locally

It doesn't build with make -jN check. I think you forgot to include the test runner in battle scripts commands

Even after adding that make check fails

@AlexOn1ine
Copy link
Collaborator

This keeps failing with [Test_ExitWithResult](error: implicit declaration of function 'Test_ExitWithResult' [-Werror=implicit-function-declaration]) but builds locally

It doesn't build with make -jN check. I think you forgot to include the test runner in battle scripts commands

Even after adding that make check fails

My bad, sorry.

@ghoulslash
Copy link
Collaborator Author

Fixed the error by including test/test.h in battle_script_commands.c but make check them bombs with illegal op codes after a while. I think I saw an error with the sticky web/mirror armor test?

@mrgriffin
Copy link
Collaborator

Illegal opcodes usually means stack corruption—something overwriting an lr on the stack, which then causes the CPU to branch into non-code memory, (hopefully!) quickly running into bytes which don't represent a legal opcode. If you have a DACS-capable mgba-rom-test (which unfortunately means building it yourself or explicitly specifying a BIOS atm) then as of #3761 you should get an immediate CRASH instead of waiting a minute for a TIMEOUT.

@ghoulslash
Copy link
Collaborator Author

ready for review. I put incorporating into tests on the backburner. Will open a reminder issue upon merging

@DizzyEggg DizzyEggg merged commit 7b1248b into rh-hideout:upcoming May 29, 2024
1 check passed
AlexOn1ine added a commit that referenced this pull request May 29, 2024
#4061 change `ABILITY_FRISK` to use `gBattleScripting.battler` but the script wasn't fully updated. BS_ATTACKER needed to be changed to `BS_SCRIPTING`
AlexOn1ine added a commit that referenced this pull request May 29, 2024
#4061 change `ABILITY_FRISK` to use `gBattleScripting.battler` but the script wasn't fully updated. BS_ATTACKER needed to be changed to `BS_SCRIPTING`
@ghoulslash ghoulslash deleted the be/saveattacker branch June 1, 2024 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants