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

Get rid of the gActiveBattler variable #3262

Merged

Conversation

DizzyEggg
Copy link
Collaborator

@DizzyEggg DizzyEggg commented Aug 29, 2023

gActiveBattler is one of the worst global variables used in the battle's code. It's caused many problems before, because there are functions which expect that value to be correctly set when called. I laid the groundwork in the controllers clean-up PR #3202, and this will be a follow-up.

Goals:

  • completely remove gActiveBattler from the codebase
  • use local variables/function arguments in place of gActiveBattler(also in places like controller emitters)
  • simplify/clean-up some code if needed

Additional things:

  • I moved sBattleBuffersTransferData to gBattleResources, so it's allocated dynamically which frees 256 bytes in EWRAM.

  • I renamed instances of battlerId to battler for consistency

  • It doesn't add/remove any features, so all the compatibility should be preserved

  • gActiveBattler was used in some strings, so I created an additional gSelectionBattler variable in its place, so that the messages display correct battler. I could change it to sth else if anyone has a better idea.

@AsparagusEduardo
Copy link
Collaborator

Now this we truly have to make sure that doesn't break linking between versions ^^;

@DizzyEggg DizzyEggg marked this pull request as ready for review August 30, 2023 11:39
@DizzyEggg
Copy link
Collaborator Author

I think this is ready for testing and review. I did some initial plays and everything seemed to be working correctly 👀

src/battle_main.c Show resolved Hide resolved
shouldSwitch = ShouldSwitch();
battlerToSwitch = *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler);
gActiveBattler = backupBattler;
shouldSwitch = ShouldSwitch(battlerAtk);
Copy link
Collaborator

Choose a reason for hiding this comment

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

somewhat unrelated to this PR, but is it a problem to call ShouldSwitch and run through the EmitReturnValues inside an AI calculation? Couldnt that potentially mess up other data transfers?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hm, you may be right. This should be investigated and fixed if it's an issue.

@@ -240,21 +240,21 @@ static bool8 FindMonThatAbsorbsOpponentsMove(void)
if (absorbingTypeAbilities[j] == monAbility && Random() & 1)
{
// we found a mon.
*(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = i;
Copy link
Collaborator

Choose a reason for hiding this comment

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

not necessarily in this PR, but we should change *(gBattleStruct->AI_monToSwitchIntoId + battler); to gBattleStruct->AI_monToSwitchIntoId[battler]; and other similar lines

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I agree, these should be changed.

Copy link
Collaborator

@ghoulslash ghoulslash left a comment

Choose a reason for hiding this comment

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

Code changes look solid. Link compatability was confirmed to work - just need to fix conflicts and its ready to go

@ghoulslash ghoulslash merged commit b907029 into rh-hideout:upcoming Sep 4, 2023
@DizzyEggg DizzyEggg deleted the active_battler_controllers branch September 4, 2023 13:06
Copy link
Collaborator

@AsparagusEduardo AsparagusEduardo left a comment

Choose a reason for hiding this comment

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

Post-mortem review.

Comment on lines +412 to +413
@ FD 3C - preiously gActiveBattler
@ FD 3D - preiously gActiveBattler without Illusion Check
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
@ FD 3C - preiously gActiveBattler
@ FD 3D - preiously gActiveBattler without Illusion Check
@ FD 3C - preiously B_ACTIVE_NAME (gActiveBattler)
@ FD 3D - preiously B_ACTIVE_NAME2 (gActiveBattler without Illusion Check)

@@ -367,7 +367,7 @@ B_ATK_PARTNER_NAME = FD 0E
B_ATK_NAME_WITH_PREFIX = FD 0F
B_DEF_NAME_WITH_PREFIX = FD 10
B_EFF_NAME_WITH_PREFIX = FD 11 @ EFF = short for gEffectBattler
B_ACTIVE_NAME_WITH_PREFIX = FD 12
@ FD 12 - preiously gActiveBattler with prefix
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
@ FD 12 - preiously gActiveBattler with prefix
@ FD 12 - preiously B_ACTIVE_NAME_WITH_PREFIX (gActiveBattler with prefix)

@@ -112,6 +112,7 @@ BattleScript_ItemSetFocusEnergy::
setfocusenergy
playmoveanimation BS_ATTACKER, MOVE_FOCUS_ENERGY
waitanimation
copybyte sBATTLER, gBattlerAttacker
Copy link
Collaborator

Choose a reason for hiding this comment

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

Spaces instead of tab.

@AsparagusEduardo AsparagusEduardo mentioned this pull request Sep 27, 2023
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.

3 participants