Code Mega Evolutions and Primal Reversion Page 10

Started by Touched April 29th, 2015 1:49 PM
  • 196486 views
  • 535 replies
Male
Seen February 1st, 2018
Posted October 19th, 2017
625 posts
8.9 Years
Hello, I have a question about this, does seeing an enemy Mega or using one yourself do anything to the Dex flags for the Mega if the dex numbers for the Mega and non-Mega don't match? I'm curious if the Dex flags are triggered.
I don't think it does in all cases. Here is what I guess what will happen: if you just see a Mega (trigger mega evolution or AI does it), you probably won't get the flags set. However, if the Mega subsequently switches out and back in, it probably will set the seen flag. I don't think the caught flag will ever get set, since the engine doesn't expect the species to switch mid-battle.

I've never tested this though, because I didn't think people would want duplicate entries in the dex and would thus set the numbers to be equal.

A Pokemon that is discriminated!
Support squirtle and make it everyone's favourite.
Age 25
Male
Seen 2 Weeks Ago
Posted September 2nd, 2020
534 posts
10.7 Years
Hey guys uh... I just wanna ask, how can I change the graphics that the codes insert? Well, I mean I did it, edited it in PS and all but when I did the thing (I wanna say compile and insert but this ain't ASM :P), it got a white background and glitched up. I don't know what bad I did, I just wanted to (excuse the word if I'm offending anyone) improve the graphics with better looking logo with a more obvious palette. (imo) T^T

Here's my image.
Spoiler:



Oh and P.S. I really don't mean any offense. The Curse animation after a Pokemon changes doesn't really fit imo. Sorry for ranting guys! :)v

MrDollSteak

Formerly known as 11bayerf1

Age 28
Male
Hong Kong
Seen March 16th, 2023
Posted September 26th, 2022
859 posts
14.4 Years
Hey guys uh... I just wanna ask, how can I change the graphics that the codes insert? Well, I mean I did it, edited it in PS and all but when I did the thing (I wanna say compile and insert but this ain't ASM :P), it got a white background and glitched up. I don't know what bad I did, I just wanted to (excuse the word if I'm offending anyone) improve the graphics with better looking logo with a more obvious palette. (imo) T^T

Here's my image.
Spoiler:



Oh and P.S. I really don't mean any offense. The Curse animation after a Pokemon changes doesn't really fit imo. Sorry for ranting guys! :)v
You're supposed to replace the Curse particle with the ones that I posted.

As for replacing the images you can just replace it in the directory I believe.
Age 25
Male
Seen 2 Weeks Ago
Posted September 2nd, 2020
534 posts
10.7 Years
You're supposed to replace the Curse particle with the ones that I posted.

As for replacing the images you can just replace it in the directory I believe.
Ohh~! Replacing the particle wouldn't change the animation for the move Curse would it? :D Oh and as I said in my post, I actually did get it inserted in-game but in the actual game (when I play it) colors are messed up. Oh well.
Male
Seen February 1st, 2018
Posted October 19th, 2017
625 posts
8.9 Years
Ohh~! Replacing the particle wouldn't change the animation for the move Curse would it? :D Oh and as I said in my post, I actually did get it inserted in-game but in the actual game (when I play it) colors are messed up. Oh well.
You need to manually expand the particle table and insert your new particles, then change the particles used by the animation manually in the code or by hex editing after the fact. The script is defined here, so just change the defines at the top (XX, YY, ZZ correspond the particles in MrDollSteak's post).

As for the colours being messed up, I assume you mean the big circle icon. After replacing the files in the images, you need to alter these lines in the config file such that they refer to the colours of the border in your sprite. You can have more or less than 4, just change the number above the list. These colours are the ones that are ignored by the routine that makes the orb brighter or greyscale (disabled), so you have to get them right to make that work properly.
If the colours are still messed up, you might want to select the transparent colour in the makefile. Please consult the grit command line documentation for more information on how to do that.

I will probably change this in the future to be simpler, I just can't really think of how to do that right now.

A Pokemon that is discriminated!
Support squirtle and make it everyone's favourite.
Male
Seen November 12th, 2021
Posted April 30th, 2020
218 posts
9.8 Years

Mega Evolution, Wish and Primal support for Emerald:



Little demonstration :


Implementation of the hack :
Spoiler:

You have to read the red bold comments.

You'll have to use the setword command, the the loader string, and the callasm command. I assume you have these.
I also assume you know how to assemble/insert ASM routine.
Lastly, apply the changes of this post.


Animation (thanks to MrDS and Touched, I just had to modify a bit their code):
Spoiler:

Spoiler:

@ You compile it as an ASM routine.
.global mega_animation_script_data

	.equ ANIM_XX, 0x27a3	@@ Change accordingly to your particles
	.equ ANIM_YY, 0x281b	@@ Those I used are in the video
	.equ ANIM_ZZ, 0x27d8

	.equ TheValue, 0x40
	.equ SizeRom, 0x8000000
	
@Buffer Particles:

.byte 0x00 
.hword ANIM_XX
.byte 0x00 
.hword ANIM_YY
.byte 0x00 
.hword ANIM_ZZ

@Charge rainbow particle

.byte 0x0A, 0x02 , 0x0C , 0x0C , 0x08 , 0x1C , 0x8C , 0x00 , 0xC0 , 0x0D , 0x03 
.byte 0x03 , 0x05 , 0x5A , 0x11 , 0x08 , 0x02 , 0x06 , 0x02 , 0x00 , 0x00 , 0x00 
.byte 0x06 , 0x00 , 0x00 , 0x00 , 0x0B , 0x00 , 0xFF , 0x2F 
.byte 0x0E 
.word anim_rainbow_x+SizeRom
.byte 0x0E 
.word anim_rainbow_x+SizeRom
.byte 0x0E 
.word anim_rainbow_x+SizeRom
.byte 0x05 , 0x0B

@Load mega stone :

.byte 0x02 , 0x0D , 0x04 , 0x00 , 0x17 , 0x0A , 0x00 , 0x0C , 0x0C , 0x08 , 0x19 , 0xC9 , 0x00 , 0xC0 
.byte 0x02 
.word anim_mega_stone_x+SizeRom
.byte 0x29 , 0x04 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 

@Whiteout screen:

.byte 0x04 , 0x14 , 0x03 , 0x65 , 0x66 , 0x11 , 0x08 , 0x05 , 0x05 , 0x05 , 0x00 , 0x02 , 0x00 , 0x00 , 0x00 
.byte 0x10 , 0x00 , 0xFF , 0xFF , 0x03 , 0x49 , 0x68 , 0x11 , 0x08 , 0x05 , 0x05 
.hword ANIM_YY
.byte 0x02 , 0x00 , 0x00 , 0x00 , 0x10 , 0x00 , 0xFF , 0xFF , 0x05 , 0x03 , 0x49 , 0x68 , 0x11 , 0x08 , 0x05

@Swap_spprite:

.byte 0x00 , 0x03 
.word swap_sprite_routine+SizeRom+1 @@@@@@@@@@ Your routine swap sprite
.byte 0x02 , 0x00 

@Return screen:

.byte 0x00 , 0x2D , 0x27 , 0x03 , 0x65 , 0x66 , 0x11 , 0x08, 0x05 , 0x05 , 0x05 , 0x00 , 0x02 , 0x00 
.byte 0x10 , 0x00 , 0x00 , 0x00 , 0xFF , 0xFF , 0x03 , 0xDD , 0x52 , 0x11 , 0x08 , 0x05 , 0x03 , 0x01 
.byte 0x00 , 0x05 , 0x00 , 0x0E , 0x00 , 0x05 , 0x0B 

@ Show Mega evolution symbol:

.byte 0x02 , 0x04 , 0x01 , 0x0A , 0x03 , 0x19 , 0x79 , 0x00 , 0x3F 
.byte 0x02 
.word anim_mega_symbol_x+SizeRom
.byte 0x82 , 0x00 , 0x05 , 0x0B , 0x0D , 0x08 , 0x08 


.align 2
anim_rainbow_x:
.byte 0x02
.word anim_rainbow_y+SizeRom
.byte 0x02 , 0x03 , 0x03 , 0x28 , 0x00 , 0xF6 , 0x0D , 0x00 , 0x04 , 0x03 
.byte 0x02 
.word anim_rainbow_y+SizeRom
.byte 0x02 , 0x03 , 0xDD , 0xFF , 0xF6 , 0xFF , 0x0D , 0x00 , 0x04 , 0x03 
.byte 0x02 
.word anim_rainbow_y+SizeRom
.byte 0x02 , 0x03 , 0x0F , 0x00 , 0xD8 , 0xFF , 0x0D , 0x00 , 0x04 , 0x03 
.byte 0x02 
.word anim_rainbow_y+SizeRom
.byte 0x02 , 0x03 , 0xF6 , 0xFF , 0xE0 , 0xFF , 0x0D , 0x00 , 0x04 , 0x03 
.byte 0x02 
.word anim_rainbow_y+SizeRom
.byte 0x02 , 0x03 , 0x19 , 0x00 , 0xEC , 0xFF , 0x0D , 0x00 , 0x04 , 0x03 
.byte 0x02 
.word anim_rainbow_y+SizeRom
.byte 0x02 , 0x03 , 0xD8 , 0xFF , 0xEC , 0xFF , 0x0D , 0x00 , 0x04 , 0x03 
.byte 0x02 
.word anim_rainbow_y+SizeRom
.byte 0x02 , 0x03 , 0x05 , 0x00 , 0xD8 , 0xFF , 0x0D , 0x00 , 0x04 , 0x03 , 0x0F 


.align 2
anim_rainbow_y:
.hword ANIM_XX
.hword ANIM_XX
.byte 0x8C , 0x4A , 0x52 , 0x08 , 0xC8 , 0x21 , 0x59 , 0x08 , 0x00 , 0x00 , 0x00 , 0x00 
.byte 0xDC , 0x21 , 0x59 , 0x08 , 0xE1 , 0xE8 , 0x0F , 0x08

.align 2
anim_mega_stone_x:
.hword ANIM_YY
.hword ANIM_YY
.byte 0xFC , 0x4A , 0x52 , 0x08 , 0x9C , 0xC6 , 0x2E , 0x08 , 0x00 , 0x00 , 0x00 , 0x00 , 0x94
.byte 0x68 , 0x59 , 0x08 , 0xC9 , 0x77 , 0x0A , 0x08 


.align 2
anim_mega_symbol_x:
.hword ANIM_ZZ
.hword ANIM_ZZ
.byte 0x34 , 0x4A , 0x52 , 0x08 , 0x9C , 0xC6 , 0x2E , 0x08 , 0x00 , 0x00 , 0x00 , 0x00
.byte 0xA8 , 0xC6 , 0x2E , 0x08 , 0x79 , 0x2B , 0x11 , 0x08

.align 2
swap_sprite_routine:
push {r4-r7, lr}
ldr r1, Attackeranim
ldr r2, battle_side_objid
ldrb r3, [r1]
ldrb r2, [r2, r3]
ldr r3, tasks
mov r12, r3
lsl r4, r0, #2
add r4, r4, r0
lsl r4, r4, #3
add r4, r12
mov r7, r4
ldrh r3, [r4, #0x1C]
cmp r3, #2
beq AddTaskMaybe
cmp r3, #0
beq Hidingsprite
cmp r3, #1
beq WeRefreshGraphics
cmp r3, #3
beq TaskRunningSomething
cmp r3, #4
bne End
lsl r3, r2, #4
add r2, r3, r2
ldr r3, somethingnotsureeither
mov r12, r3
lsl r2, r2, #2
ldrh r3, [r4, #0x1E]
add r2, r12
strh r3, [r2]
push {r0-r2}
ldr r0, Attackeranim
ldrb r0, [r0]
mov r1, #0x58
mul r1, r0
ldr r2, BattleData
add r1, r2
ldrh r1, [r1]
ldr r3, shadupdate
bl Jump
pop {r0-r2}
ldr r3, move_anim_task_del
bl Jump

End:
pop {r4-r7, pc}


WeRefreshGraphics:
ldr r3, Refreshgraphics
ldrb r0, [r1]
mov r1, #1
bl Jump

AddEnd:
ldrh r3, [r4, #0x1C]
add r3, #1
strh r3, [r4, #0x1C]
b End

Hidingsprite:
ldr r0, somethingnotsureeither
mov r12, r0
lsl r3, r2, #4
add r3, r3, r2
lsl r3, r3, #2
add r3, r12
mov r5, r3
ldrh r0, [r3]
strh r0, [r4, #0x1E]
mov r0, #TheValue
neg r0, r0
strh r0, [r3]
ldrh r3, [r4, #0x1C]
add r3, #1
strh r3, [r4, #0x1C]
b End


TaskRunningSomething:
ldr r3, task_find_id_by_funcptr
ldr r0, SomeRoutine
bl Jump
ldr r3, task_is_running
bl Jump
cmp r0, #0
bne End
ldrh r3, [r4, #0x1C]
add r3, #1
strh r3, [r4, #0x1C]
b End

AddTaskMaybe:
lsl r3, r2, #4
add r2, r3, r2
ldr r3, somethingnotsure
mov r12, r3
mov r1, #4
lsl r2, r2, #2
add r2, r12
ldrb  r3, [r2]
bic r3, r1
strb r3, [r2]
ldr r0, SomeRoutine
ldr r3, taskadd
mov r1, #5
ldr r6, tasks
bl Jump
lsl r3, r0, #2
add r0, r3, r0
mov r3, #0
lsl r0, r0, #3
add r0, r6, r0
strh r3, [r0, #8]
ldr r3, Attackeranim
ldrb r3, [r3]
strh r3, [r0, #0xC]
ldrh r3, [r4, #0x1C]
add r3, #1
strh r3, [r4, #0x1C]
b End

Jump:
bx r3

.align 2
shadupdate: .word 0x805EE54+1
BattleData: .word 0x02024084
Attackeranim: .word 0x2038436
battle_side_objid: .word 0x20241E4
tasks: .word 0x3005E00
somethingnotsure: .word 0x202066E @@@ Not sure this is the exact location, have to test/ask
somethingnotsureeither: .word 0x2020650 @@@ Not sure this is the exact location, have to test/ask
SomeRoutine: .word 0x80A4980+1
taskadd: .word 0x80A8FB0+1
Refreshgraphics: .word 0x805E8F4+1
task_find_id_by_funcptr: .word 0x80A921C+1
task_is_running: .word 0x80A91E4+1
move_anim_task_del: .word 0x80A3C1C+1


Expand the playanimation table :
Spoiler:

At 0x5DA3C there's a pointer. Change this pointer to your new table which will be :
9E 7E 2D 08 D4 7E 2D 08 DD 7E 2D 08 1A 7F 2D 08 22 7F 2D 08 6A 7F 2D 08 75 7F 2D 08 
08 81 2D 08 7C 81 2D 08 58 82 2D 08 A5 82 2D 08 EE 82 2D 08 F3 82 2D 08 F8 82 2D 08 
FD 82 2D 08 0B 83 2D 08 3C 83 2D 08 61 83 2D 08 BF 83 2D 08 1E 84 2D 08 E3 84 2D 08 
23 85 2D 08 66 85 2D 08 E3 85 2D 08 04 86 2D 08 15 86 2D 08 26 86 2D 08 7A 86 2D 08 
93 86 2D 08 9D 86 2D 08
We can see the last vanilla animation in bold, its index is 0x1D. Keep that in mind : the next animation will be the index 0x1E, then 0x1F, and so on.
So, put the pointer to the animation of the mega evolution here, keep its index in mind in case you added other animation.

Main routine:
Spoiler:

Before you panic because the routine is long, here are the explanations :

-The first thing you have to do is to change the values of the red lines if needed.
-Then, you take the whole routine, you don't even have to remove the comments, and you assemble it.
-The output will be a big file, like 10 Mo, (depending on where you want to insert it). Basically, it'll be filled of FFs bytes.
-Do a copy of the out put, open it with an hex editor, do ctrl+a then right click, "fill with" and fill with FFs bytes.
-Now open lunar ips (or NUPS if you have an expanded ROM), and create a patch, the unmodified will be the copy, filled of FF, the modified is
the original output.
-You can directly patch your ROM with the patch created!
Spoiler:

.thumb
.thumb_func
.global megahack



@@@@ What you have to read !

@ 0x1300 (4900) bytes of freespace needed.


.equ Myloc, 0x______	@ Change to whatever you want, you'll need 7000 (0x1B58) bytes of freespace to be sure.
						@ That address musts end with 0
						

.equ DoubleWordMega, 0x0203E3E0     @ Double word (8 bytes) of free ram, used for Mega evolution, must ends with 0, it's important.
.equ YourWordLocation, 0x203E320    @ Word used by your setword command
.equ ImposterLocation, 0x203E3FC    @ byte used by imposter
.equ WeathersMegaPrimal, 0x0203E330 @ One byte of free ram used for mega/primal weathers
.equ AegiFreeRam, 0x203E33C	        @ One byte of free ram used for Aegislash transformation


.equ YourMegaAnimationIndex, 0x1E   @ Index of the animation, you remember the expanded table ? Put Delta stream and Mega animation here.
.equ YourDeltaStreamAnimIndex, 0x1F

.equ SetWordCommand, 0xFA @ As I said, these commands have to be implemented, so put their index here.
.equ CallAsmCommand, 0xF9

@@@ If you don't want the following abilities to work, just give them an unused index (like 0xF5) and don't give it to your pokémons.

.equ DeltaStreamAbility, 0xC0	@ Abilities indexes
.equ PrimordialSeaAbility, 0xBE
.equ DesolateLandAbility, 0xBF
.equ ImposterAbility, 0x97
.equ MoxieAbility, 0x9A

.equ NumberEvoPerPokemon, 0x5	@ Number of evolution you have per pokemon
.equ SoundWeWantToPlay, 0x1		@ Sound we want to play when triggering the mega evolution. 0x1 is not bad.

@@ If you don't want aegislash, just give it an index unused like 0x800

.equ AegislashShield, 0x142 @ Index number of Aegislash Shield
.equ AegislashBlade, 0x113  @ Index number of Aegislash Blade

.equ PokemonDataPointer, 0x803A270       @ I don't see why it would change since it's a pointer for the pokemon data, but just in case, I put it here
.equ EvolutionPointerLocation, 0x806D140 @ I don't see why it would change since it's a pointer for the evolution data, but just in case, I put it here


@@@@From now on, no need to read, everything is automated

.equ MegaEvolutionIndex, 0xFE     @ 0xFE = Mega evolution. I choose to do as Touched here.
.equ WishMegaEvolutionIndex, 0xFC @ 0xFC = Wish
.equ PrimalReversionIndex, 0xFD   @ 0xFD = Primal

@@ Charac table.

.equ Space, 0x0
.equ JumpLine, 0xFE
.equ BuffCharac, 0xFD
.equ Apos, 0xB4
.equ Dot, 0xAD
.equ Exclam, 0xAB
.equ Termin, 0xFF
.equ Dash, 0xAE

.equ A_, 0xBB
.equ B_, 0xBC
.equ C_, 0xBD
.equ D_, 0xBE
.equ E_, 0xBF
.equ F_, 0xC0
.equ G_, 0xC1
.equ H_, 0xC2
.equ I_, 0xC3
.equ J_, 0xC4
.equ K_, 0xC5
.equ L_, 0xC6
.equ M_, 0xC7
.equ N_, 0xC8
.equ O_, 0xC9
.equ P_, 0xCA
.equ Q_, 0xCB
.equ R_, 0xCC
.equ S_, 0xCD
.equ T_, 0xCE
.equ U_, 0xCF
.equ V_, 0xD0
.equ W_, 0xD1
.equ X_, 0xD2
.equ Y_, 0xD3
.equ Z_, 0xD4

.equ a_, 0xD5
.equ b_, 0xD6
.equ c_, 0xD7
.equ d_, 0xD8
.equ e_, 0xD9
.equ f_, 0xDA
.equ g_, 0xDB
.equ h_, 0xDC
.equ i_, 0xDD
.equ j_, 0xDE
.equ k_, 0xDF
.equ l_, 0xE0
.equ m_, 0xE1
.equ n_, 0xE2
.equ o_, 0xE3
.equ p_, 0xE4
.equ q_, 0xE5
.equ r_, 0xE6
.equ s_, 0xE7
.equ t_, 0xE8
.equ u_, 0xE9
.equ v_, 0xEA
.equ w_, 0xEB
.equ x_, 0xEC
.equ y_, 0xED
.equ z_, 0xEE

@@Locations BS
.equ MegaBattleScriptForPlayer, MegaBSPlayer+SizeRom

.equ MegaBattleScriptForAI, MegaBSForAI+SizeRom

.equ WishBattleScriptForPlayer, BattleScriptWishPlayer+SizeRom

.equ WishBattleScriptForAI, BattleScriptWishAI+SizeRom

.equ MegaBattleScriptForPlayer2, MegaBSPlayer2+SizeRom

.equ MegaBattleScriptForAI2, MegaBSForAI2+SizeRom

.equ WishBattleScriptForPlayer2, BattleScriptWishPlayer2+SizeRom

.equ WishBattleScriptForAI2, BattleScriptWishAI2+SizeRom

.equ BattleScriptForImposter, ImposterBattleScript+SizeRom

.equ PrimalBattleScript, BattleScriptPrimal+SizeRom

.equ BattleScriptDeltaStream, DeltaStreamBattleScript+SizeRom

.equ BattleScriptMoxieClear, ScriptMoxieClear+SizeRom

.equ BattleScriptClearWeather, ClearWeatherBattleScript+SizeRom


.equ SizeRom, 0x8000000


@@ Stuffs BS
.equ LocationStringZZAI2, StringZZAI2+SizeRom
.equ LocationStringLLAI, StringLLAI+SizeRom
.equ PlayCry, 1+PlayCryRoutine+SizeRom
.equ HelpSwitchIn, 1+HelpSwitchInRoutine+SizeRom
.equ LocationStringPrim, StringPrimalNull+SizeRom
.equ HelpSwitchInPrimal, 1+PrimalSwitchInRoutine+SizeRom
.equ LocationStringPrimal, StringPrimal+SizeRom
.equ LocationStringUUAI2, StringUUAI2+SizeRom
.equ LocationStringZZAI, StringZZAI+SizeRom
.equ LocationStringUUAI, StringUUAI+SizeRom
.equ LocationStringZZ2, StringZZ2+SizeRom
.equ LocationStringLL, StringLL+SizeRom
.equ LocationStringUU2, StringUU2+SizeRom
.equ LocationStringZZ, StringZZ+SizeRom
.equ LocationStringUU, StringUU+SizeRom

.equ SecondRoutineTransform, 1+RoutineTransformFirst+SizeRom
.equ FirstRoutineTransform,	1+RoutineTransformSecond+SizeRom



/*The idea is to check whether we are in the menu attack or not, we also check the touched pressed
(because that routine runs multiple times by frame). Depending on where we are and what touch are
pressed, we set a specific ram location. Then we'll have to hook at the very start of the attack
canceler to transform the pokemons.
We also need to check whether the pokemon can mega evolve or not, and if it has the good item/attack.
*/

/*
First word :
original turn order

				Flag Trigger
Bank			transformed
				Banks:
				0   1  2  3		
OO OO OO OO 	XX XX XX XX


Flags : 1 = Mega, 2 = Wish, 4 = Transformed.


			
*/



@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@         HERE ARE THE HOOKS + a little routine


@@ Trigger player

.org 0x3BE80, 0xFF

ldr r0, Hook
mov pc, r0

.align 2

Hook: .word SizeRom+1+Myloc

@@ Clear ram end battle

.org 0x3DDE4, 0xFF

ldr r0, Hook6
mov pc, r0

.align 2

Hook6: .word 1+ClearRamEndBattle+SizeRom

@@@ Switch in check + primal

.org 0x42A20, 0xFF

ldr r3, Hook3
mov pc, r3

.align 2
Hook3: .word 1+SwitchInCheckPrimal+SizeRom

@@ Transformation

.org 0x45FFC, 0xFF

ldr r0, Hook2
mov pc, r0

.align 2
Hook2: .word 1+TransformationFromAttackCanceler+SizeRom

@@ Faintpokemon command

.org 0x49BE8, 0xFF

ldr r0, Hook4
mov pc, r0

.align 2
Hook4: .word 1+FaintPokemonCommand1+SizeRom

@@ Faintpokemon command

.org 0x49C08, 0xFF

ldr r1, Myloc5
mov pc, r1

.align 2
Myloc5: .word 1+FaintPokemonCommand2+SizeRom

@@ Handle Multi

.org 0x4DA14, 0xFF	@ when 0x60 is added to the pointer in 0x0202449C, we can see the order of the party player/opponent
ldrb r0, [r7]		@ At the start of a normal battle, the order is 01 23 45, 0 = first poke, 1 = second, etc. It's always updated when switch/faint etc
bl 0xA6A30			@ In multi, the order is 03 12 45, because there are really two part of the team (even if it's in the same area). The problem is the engine
cmp r0, #0			@ doesn't update the order for the opponent when we're in double. It's a problem when it comes to load the data of the opponent's pokemon.
bne pc+20			@ So that little routine corrects that.

@@@ Trigger AI


.org 0x63614, 0xFF	

ldr r0, Hook1
mov pc, r0

.align 2
Hook1: .word 1+TriggeringTheAIMega+SizeRom

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@              HERE START THE MAIN ROUTINES


@@@@@@@@@@@@@@@@@@@@@@@@ TRIGGER MEGA EVOLUTION FOR PLAYER


@@@@@@ CHECK MENU PART


.org Myloc, 0xFF

ldr r0, SomeLoc0
mov r1, #0
strb r1, [r0, #4]
ldr r0, b_active_side0		@ Until here it's the engine operations.
push {r0-r7}
mov r7, r10
mov r6, r9
mov r5, r8
push {r5-r7}
ldr r0, FlagBattle0
ldr r0, [r0]
ldr r2, FlagsMultiBattle0
and r0, r2
cmp r0, #0
beq CheckAllFlag0
mov r2, #1
mov r6, #7		@ 7 is 0x1(trigger mega) + 0x2(trigger wish) + 0x4(transformed), they are the flags for the first player.
b CheckingFlag0

CheckAllFlag0:
ldr r6, FlagsPlayer0		@ Or we check whether a mega evolution already happened or not, with flags of the first/second pokemon player, if not in multi
mov r2, #0

CheckingFlag0:
mov r8, r1
ldr r0, FirstMenu0	@ Used later, to know whether we're in the menu of the first or second pokemon
ldrb r1, [r0]
ldr r4, Pad0			@ This part loads the touch pressed
ldr r5, [r4]		@ Same
lsl r5, r5, #0x18	@ Same
lsr r5, r5, #0x18	@ Same
cmp r1, #2			@ Comparison to know whether we're in first menu or second
bls ScdMenu0
ldr r0, InOutMenu0	@ We're in the first menu, so we're checking wether we're selecting an attack or not
ldrb r0, [r0]
cmp r0, #0x3F		@ 0x4X we're out, 0x3X, we're in
bls InMenuFirst0

ClearTriggerFlagPlayerSide0:
mov r1, #0		@ If we are in the first menu, and we don't select an attack, we remove the triggers flag for player 1.
b Removing0

Return00:
b Return0

InMenuFirst0:
mov r0, r5			@ Those four lines check if B or select has been pressed
mov r4, #6
and r0, r4
cmp r0, #6
beq CheckStartPressedPlayer1
mov r1, #0
b Removing0			@ Remove the flags accordingly if pressed

CheckStartPressedPlayer1:
ldr r0, MegaRam0
ldr r0, [r0, #4]
and r0, r6		@ before checking start, we check if a flag hasn't be set already
cmp r0, #0
bne Return00
mov r0, r5
mov r4, #8		@ Check start
and r0, r4
cmp r0, #0
beq ActiveFirstMega0
b Return0

ActiveFirstMega0:
mov r5, #0
b CheckEvoMega0		@ Goto another part that checks mega evo. 

FirstOk0:			@ If the first can mega evolve, the loop back here. Otherwise it returns.
mov r1, #0
b Store0

ScdMenu0:
ldr r0, InOutMenu0
ldrb r0, [r0]
cmp r0, #0x3F		@ Same than before, checking whether we're selecting an attack or not
bls InMenuScd0
b Return0

InMenuScd0:
mov r0, r5			@ Check B or select
mov r4, #6
and r0, r4
cmp r0, #6
beq CheckStartPressedPlayer2
mov r1, #2
b Removing0

CheckStartPressedPlayer2:
ldr r0, MegaRam0
ldr r0, [r0, #4]
and r0, r6		@ before checking start, we check if a flag hasn't be set already
cmp r0, #0
bne Return00
mov r0, r5
mov r4, #8
and r0, r4
cmp r0, #0
beq ActiveSecondMega0	@ Same than before but for the second poke
b Return0

ActiveSecondMega0:	@Same thing than for the first
mov r5, #2
b CheckEvoMega0

SecondOk0:
mov r1, #2
b Store0

Removing0:
ldr r0, MegaRam0
add r0, #4
mov r2, #3
add r0, r1
ldrb r1, [r0]
bic r1, r2
strb r1, [r0]
b Return0

JumpForSound0:
bx r3

Store0:
ldr r0, MegaRam0
add r0, #4
add r0, r1
strb r4, [r0]
mov r0, #SoundWeWantToPlay
ldr r3, PlaySoundRoutine0
bl JumpForSound0

Return0:
pop {r5-r7}
mov r8, r5
mov r9, r6
mov r10, r7
pop {r0-r7}
ldr r5, Back0
mov pc, r5

@@@@@@@ CHECK EVOLUTION TABLE PART

CheckEvoMega0:		@ r5 is the parameter, the bank 0 or 2
push {r5}
ldr r6, PlayerParty0

StartingCheck0:
ldr r0, MemLoc0
ldr r0, [r0]
add r0, #0x60
ldrb r0, [r0]	@ loading the order of our party

HandleDouble0:
mov r1, #2
and r1, r5
cmp r1, #0				
beq First0		@ We load a byte, like 01, and we just want the 0 or the 1, depending on what bank we have in r5
mov r1, #0xF
and r0, r1
b Continue0

First0:
mov r1, #0xF0
and r0, r1
lsr r0, r0, #4

Continue0:
mov r2, #0x64		@ Size of data party pokemon
mul r0, r2			@ Size x Index party number
add r0, r6			@ + party data
mov r4, r0
ldrh r0, [r0, #0x20]	@ Start of its data + 0x20 = Index number after applying doesnt's hack (the link I gave you at the start)
mov r6, r0			@ I want to keep the index safe
ldr r0, EvoPointer0	@ I load the evolution from a pointer, that way if you move them the routine is still ok
ldr r0, [r0]
mov r1, #NumberEvoPerPokemon
mov r2, #8		@ Size of each evolution, doesn't change.
mul r1, r2
mul r1, r6		@ Index number poke x total size of evolution per poke
add r0, r1		@ + evo data
mov r2, #MegaEvolutionIndex	@ initialise the loop
mov r7, #0		@ r7 = counter
mov r1, r0		@ keep r0 safe
mov r4, r5		@ keeps bank safe

Loop0:
cmp r7, #NumberEvoPerPokemon
beq CheckWish0
ldrh r3, [r0]
cmp r3, r2
beq CheckStone0

BuffLoopStone0:
add r0, #8
add r7, #1
b Loop0

CheckWish0:
mov r2, #WishMegaEvolutionIndex
mov r7, #0
mov r0, r1

LoopWish0:
cmp r7, #NumberEvoPerPokemon
beq NoTransformation0
ldrh r3, [r0]
cmp r3, r2
beq CheckAttack0

BuffLoopWish0:
add r0, #8
add r7, #1
b LoopWish0

CheckAttack0:
mov r5, r4
mov r1, #0x58
mul r5, r1
ldr r1, BattData0
add r5, r1
ldrh r6, [r0, #2]
cmp r6, #0
beq BuffLoopWish0
mov r3, r5
add r3, #0xC		@ Battle data + 0xC = attacks of the poke
ldrh r1, [r3]
cmp r6, r1
beq AmorceWish0
ldrh r1, [r3, #2]
cmp r1, r6
beq AmorceWish0
ldrh r1, [r3, #4]
cmp r1, r6
beq AmorceWish0
ldrh r1, [r3, #6]
cmp r1, r6
beq AmorceWish0
b BuffLoopWish0

AmorceWish0:
mov r4, #2
b Amorce0

CheckStone0:
mov r5, r4
mov r1, #0x58
mul r5, r1
ldr r1, BattData0
add r5, r1
mov r3, r5			
add r3, #0x2E		@ Battle data + 0x2E = item held
ldrh r3, [r3]
ldrh r6, [r0, #2]
cmp r3, #0
beq BuffLoopStone0
cmp r6, #0
beq BuffLoopStone0
cmp r6, r3
bne BuffLoopStone0

AmorceMega0:
mov r4, #1

Amorce0:
pop {r5}
cmp r5, #0
beq NumberOne0
b SecondOk0

NumberOne0:
b FirstOk0

NoTransformation0:
pop {r5}
b Return0

.align 2
BattData0: .word 0x2024084
FlagsPlayer0: .word 0x70007
FlagBattle0: .word 0x02022FEC
FlagsMultiBattle0: .word 0x408040
PlaySoundRoutine0: .word 0x80A37A4+1
MegaRam0: .word DoubleWordMega
EvoPointer0: .word EvolutionPointerLocation
MemLoc0: .word 0x0202449C
PlayerParty0: .word 0x020244EC
b_active_side0: .word 0x2024064
SomeLoc0: .word 0x2024332
Back0: .word 0x803BE88+1
FirstMenu0: .word 0x200558C
SecondMenu0: .word 0x20055A4
InOutMenu0: .word 0x202018C
Pad0: .word 0x4000130


/*
First word :
original turn order

Order			Flag Trigger 	Banks	
Bank			transformed
				Banks:
				0   1  2  3		
OO OO OO OO 	XX XX XX XX  	UU TT 
*/

/*For that routine I assumed we applied the changes of this post : http://www.pokecommunity.com/showpost.php?p=8963359&postcount=773*/


@@@@@@@@@@@@@@@@@@@ TRIGGER MEGA EVOLUTION FOR AI
.align 2

TriggeringTheAIMega:

Start11:
push {r4-r7, lr}
mov r7, r10
mov r6, r9
mov r5, r8
push {r5-r7}
ldr r0, b_active_side1
ldrb r5, [r0]				@ Load the current bank the AI is handling
mov r8, r5		@keeping the bank safe
mov r2, #0					@ That part check whether we're in multi battle or not		
ldr r0, FlagsMultiBattle111
ldr r1, FlagBattle111
ldr r1, [r1]
and r1, r0
cmp r1, #0
beq NotMultiBattle1
mov r2, #1			@ r2 = 0 if not multi, 1 if multi

NotMultiBattle1:
cmp r5, #1
beq FirstPokeAI11		@ Depending on the bank we're checking, different way to handle it
cmp r5, #2
beq AIPartnerPoke111
cmp r5, #3
beq  AISecondPoke111
b GoOn11

FirstPokeAI11:
cmp r2, #1
bne CheckBothFlag1
ldr r1, FlagFirstPokeAI1
b CheckThePoke1

CheckBothFlag1:
ldr r1, FlagsBothPokeAI1

CheckThePoke1:
ldr r0, MegaRam11
ldr r0, [r0, #4]
and r0, r1
cmp r0, #0
beq FlagOkTransform1
mov r1, #0
b EndFlagsTriggerTransform1

FlagOkTransform1:
mov r1, #1
EndFlagsTriggerTransform1:
cmp r1, #1
beq DataPokemonToTrigger1
b GoOn11

AIPartnerPoke111:
ldr r1, FlagsPartnerAI1
b CheckThePoke1

AISecondPoke111:
cmp r2, #1
bne CheckBothFlag1
ldr r1, FlagSecondPokeAI1
b CheckThePoke1

DataPokemonToTrigger1:
mov r6, #0x58
mul r6, r5
ldr r0, BattleData11
add r0, r6
add r0, #0x28
ldrh r0, [r0]
cmp r0, #0		@ The AI check whether its pokemon is fainted or not (because of double battle)
bne Fine11
b GoOn11

Fine11:
cmp r5, #2
beq CheckAIPartner1
ldr r6, AIParty11
mov r1, #3			@ Remember the order party ? 3 bytes for the player, 3 bytes for the AI (there's also 6 bytes next to that, but we don't need them)
b CheckingTheParty1	@ So, depending on the side of the AI, we're loading the player darty data or the AI party data, because the partner of the poke
					@ has the data of its poke stored in the party player
CheckAIPartner1:
ldr r6, PlayerParty11
mov r1, #0

CheckingTheParty1:
ldr r0, MemLoc11
ldr r0, [r0]
add r0, #0x60
ldrb r0, [r0, r1]	@ Loading the order of the party

HandleDble11:
mov r1, r8
mov r2, #2
and r1, r2
cmp r1, #0			
beq First111
mov r1, #0xF		@ See my explanation in the Faintpokemon hook
and r0, r1
b Continue111

First111:
mov r1, #0xF0
and r0, r1
lsr r0, r0, #4

Continue111:
mov r2, #0x64
mul r0, r2
add r0, r6			@ r6 holds the party to load
mov r4, r0
ldrh r0, [r4, #0x20]	@Now we're loading the pokemon
mov r6, r0
ldr r0, EvoPointer11
ldr r0, [r0]
mov r1, #NumberEvoPerPokemon
mov r2, #8
mul r1, r2
mul r1, r6
add r0, r1
mov r2, #MegaEvolutionIndex
mov r7, #0
mov r1, r0

Looop11:
cmp r7, #NumberEvoPerPokemon
beq CheckWish111
ldrh r3, [r0]
cmp r3, r2
beq CheckOrb111

BuffLoopOrb111:
add r0, #8
add r7, #1
b Looop11

CheckWish111:
mov r2, #WishMegaEvolutionIndex
mov r7, #0
mov r0, r1

LoopWish111:
cmp r7, #NumberEvoPerPokemon
beq GoOn11
ldrh r3, [r0]
cmp r3, r2
beq CheckAttack111

BuffLoopWish111:
add r0, #8
add r7, #1
b LoopWish111

CheckAttack111:
mov r1, #0x58
mul r5, r1		@ r5 still holds the bank
ldr r1, BattleData11
add r5, r1
ldrh r6, [r0, #2]
cmp r6, #0
beq BuffLoopWish111
mov r3, r5
add r3, #0xC
ldrh r1, [r3]
cmp r6, r1
beq Amorce111Wish
ldrh r1, [r3, #2]
cmp r1, r6
beq Amorce111Wish
ldrh r1, [r3, #4]
cmp r1, r6
beq Amorce111Wish
ldrh r1, [r3, #6]
cmp r1, r6
beq Amorce111Wish
b BuffLoopWish111

Amorce111Wish:
mov r7, #2
b Amorce111

CheckOrb111:
mov r5, r4
mov r1, #0x58
mul r5, r1
ldr r1, BattleData11
add r5, r1
mov r3, r5
add r3, #0x2E
ldrh r3, [r3]
ldrh r6, [r0, #2]
cmp r3, #0
beq BuffLoopOrb111
cmp r6, #0
beq BuffLoopOrb111
cmp r6, r3
bne BuffLoopOrb111

MegaAmorce1:
mov r7, #1

Amorce111:
ldr r2, MegaRam11
add r2, #4
mov r3, r8
strb r7, [r2, r3]	@ We're storing the type of mega for the wanted bank in the Free Ram we use
mov r0, #0
b MegaActivated1

GoOn11:
MegaActivated1:
ldr r3, Back111
mov pc, r3

.align 2
b_active_side1: .word 0x02024064
FlagsPartnerAI1: .word 0x70000
FlagFirstPokeAI1: .word 0x700
FlagSecondPokeAI1: .word 0x7000000
FlagsBothPokeAI1: .word 0x7000700
FlagBattle111: .word 0x02022FEC
FlagsMultiBattle111: .word 0x408040
Back111: .word 0x806361E+1
NPokeField11: .word 0x202406C
MegaRam11: .word DoubleWordMega
EvoPointer11: .word EvolutionPointerLocation
BattleData11: .word 0x02024084
MemLoc11: .word 0x0202449C
PlayerParty11: .word 0x020244EC
AIParty11: .word 0x02024744

/*
First word :
original turn order

Order			Flag Trigger 	Banks	
Bank			transformed
				Banks:
				0   1  2  3		
OO OO OO OO 	XX XX XX XX  	UU TT 
*/

/*For that routine I assumed we applied the changes of this post : http://www.pokecommunity.com/showpost.php?p=8963359&postcount=773*/





@@@@@@@@@@@@@@@@@@@@@@@@ TRANSFORMATION PART

.align 2

TransformationFromAttackCanceler:

push {r4-r7, lr}
mov r7, r9
mov r6, r8
push {r6,r7}
sub sp, sp, #4
ldr r0, MoveOrderTurn2

StartToCheck2:
ldr r2, RamMega2
add r2, #4
mov r3, #0

LoopCheck2:			@ That loops loads the order of the turn, check one by one if the poke can mega evolve
cmp r3, #4
beq ByeBabe2

LoopCheckContinue12:
ldrb r1, [r0, r3]
ldrb r4, [r2, r1]
mov r5, #3
and r5, r4
cmp r5, #0
beq BuffLoop2
mov r8, r1			@ r8 holds the bank
mov r7, r5			@ r7 the type of mega
b LoadTransformation2

BuffLoop2:
add r3, #1
b LoopCheck2

LoadTransformation2:
mov r3, #0x58
mul r3, r1
ldr r5, BattleData2
add r5, r3
add r5, #0x28
ldrh r5, [r5]
cmp r5, #0
bne ItsOk2
mov r4, #0
strb r4, [r2, r1]
ByeBabe2:
b Return22

ItsOk2:
mov r4, #4
strb r4, [r2, r1]

Next2:
ldr r4, MemLoc2		@ Loads order party
ldr r4, [r4]
add r4, #0x60
mov r3, #1
and r3, r1		@ AI or player
cmp r3, #0
bne AIPicking2
ldrb r4, [r4]
ldr r5, PlayerParty2 
b NextAgain2

AIPicking2:
ldrb r4, [r4, #3]
ldr r5, AIParty2

NextAgain2:
mov r3, #2
and r3, r1		@ First or second poke
cmp r3, #0
beq First2
mov r3, #0xF
and r4, r3
b Continue22

First2:
mov r3, #0xF0
and r4, r3
lsr r4, r4, #4

Continue22:
mov r3, #0x64
mul r4, r3
add r4, r5
ldrh r3, [r4, #0x20]	@ Now we're loading the pokemon we want to check
ldr r0, EvoPointer2
ldr r0, [r0]
mov r1, #NumberEvoPerPokemon
mov r2, #8
mul r1, r2
mul r1, r3
add r0, r1		@ Loading its evolutions location
mov r5, #0
cmp r7, #1
bne CheckWish2
mov r2, #MegaEvolutionIndex

Loop2:	
cmp r5, #NumberEvoPerPokemon	@ We compare to number max of evolution, but it's not even necessary since we're sure we can mega evolve.
beq Returning2
ldrh r3, [r0]
cmp r3, r2
beq CheckStone2

BuffLoopStone2:
add r0, #8
add r5, #1
b Loop2

Returning2:
b Return22

CheckWish2:
mov r2, #WishMegaEvolutionIndex
LoopWish2:
cmp r5, #NumberEvoPerPokemon
beq Returning2
ldrh r3, [r0]
cmp r3, r2
beq CheckAttack2

BuffLoopWish2:
add r0, #8
add r5, #1
b LoopWish2

CheckAttack2:
mov r5, r8
mov r1, #0x58
mul r5, r1
ldr r1, BattleData2
add r5, r1
ldrh r6, [r0, #2]
cmp r6, #0
beq BuffLoopWish2
mov r3, r5
add r3, #0xC
ldrh r1, [r3]
cmp r6, r1
beq ChangeStats2
ldrh r1, [r3, #2]
cmp r1, r6
beq ChangeStats2
ldrh r1, [r3, #4]
cmp r1, r6
beq ChangeStats2
ldrh r1, [r3, #6]
cmp r1, r6
beq ChangeStats2
b BuffLoopWish2

CheckStone2:
mov r5, r4
mov r1, #0x58
mul r5, r1
ldr r1, BattleData2
add r5, r1
mov r3, r5
add r3, #0x2E
ldrh r3, [r3]
ldrh r6, [r0, #2]
cmp r3, #0
beq BuffLoopStone2
cmp r6, #0
beq BuffLoopStone2
cmp r6, r3
bne BuffLoopStone2

ChangeStats2:
ldrh r6, [r0, #4]
strh r6, [r5]
strh r6, [r4, #0x20]
mov r0, r4
ldr r3, calcstats2
bl Jump2
mov r0, r4
add r0, #0x5A
ldrh r3, [r0]
strh r3, [r5, #2]	@ This part updates attack.
ldrh r3, [r0, #2]	@ 
strh r3, [r5, #4]	@ defense
ldrh r3, [r0, #0x4]	@ 
strh r3, [r5, #0x6]	@ speed
ldrh r3, [r0, #0x6]	@ 
strh r3, [r5, #0x8]	@ att special
ldrh r3, [r0, #0x8]	@ 
strh r3, [r5, #0xA]	@ def special. Ends here.

UpdateTypeAndAbility2:
add r5, #0x20			@ I assumed Mega evolutions can only have one ability
ldr r2, PokePointer2
ldr r2, [r2]
mov r3, #28
mul r3, r6
add r3, r2
add r3, #6
ldrb r2, [r3]
strb r2, [r5, #1]
ldrb r2, [r3, #1]
strb r2, [r5, #2]
add r3, #16
ldrb r2, [r3]
strb r2, [r5]		@ Stats completly updated here.
ldr r3, ScriptPusher2	@ Keeps the current BS safe.
bl Jump2
ldr r1, CurBS2
mov r0, r8
mov r2, #1
and r2, r0
cmp r2, #0
bne AITransformation2
cmp r7, #1				@ r7 = 1 for mega, 2 for wish
bne WishPlayer2
cmp r0, #2
beq AIPartnerMega2
ldr r0, MegaBS2
b Transformation2

AIPartnerMega2:
ldr r0, MegaBS22
b Transformation2

WishPlayer2:
cmp r0, #2
beq AIPartnerWish2
ldr r0, MegaWishBS2
b Transformation2

AIPartnerWish2:
ldr r0, MegaWishBS22
b Transformation2

AITransformation2:
cmp r7, #1
bne WishAI2
cmp r0, #3
beq AITransformMega2
ldr r0, MegaBSAI2
b Transformation2

AITransformMega2:
ldr r0, MegaBSAI22
b Transformation2

AITransformWish2:
ldr r0, MegaWishAIBS22
b Transformation2

WishAI2:
cmp r0, #3
beq AITransformWish2
ldr r0, MegaWishAIBS2

Transformation2:
str r0, [r1]
DealWithTargetOrder2:
ldr r0, RamMega2
ldr r2, Bank2
ldrb r3, [r0]
mov r4, #0x10
and r4, r3
cmp r4, #0x10
beq DirectEnd2
ldrb r3, [r2]
mov r4, #0x10
orr r3, r4
strb r3, [r0]

DirectEnd2:
mov r3, r8
strb r3, [r2]
ldr r0, EndCanceler2
mov pc, r0

Return22:
ldr r0, RamMega2		@ Here we keep the banks safe.
ldr r2, Bank2	@ In our RAM location.
ldrb r3, [r0]
mov r4, #0x10
and r4, r3
cmp r4, #0
beq RealBack2
bic r3, r4
strb r3, [r2]
strb r3, [r0]

RealBack2:
ldr r0, Back2
mov pc, r0

Jump2:
mov pc, r3

.align 2
MoveOrderTurn2: .word 0x0202407E
EndCanceler2: .word 0x8046398+1
MegaBS2: .word MegaBattleScriptForPlayer
MegaBS22: .word MegaBattleScriptForPlayer2
MegaBSAI2: .word MegaBattleScriptForAI
MegaBSAI22: .word MegaBattleScriptForAI2
MegaWishBS2: .word WishBattleScriptForPlayer
MegaWishBS22: .word WishBattleScriptForPlayer2
MegaWishAIBS2: .word WishBattleScriptForAI
MegaWishAIBS22: .word WishBattleScriptForAI2
ScriptPusher2: .word 0x0803FAFC+1
CurBS2: .word 0x02024214
PokePointer2: .word PokemonDataPointer
calcstats2: .word 0x08068D0C+1
Back2: .word 0x8046006+1
RamMega2: .word DoubleWordMega
BankTarget2: .word 0x0202420C
Bank2: .word 0x0202420B
EvoPointer2: .word EvolutionPointerLocation
BattleData2: .word 0x02024084
MemLoc2: .word 0x0202449C
PlayerParty2: .word 0x020244EC
AIParty2: .word 0x02024744



@@@@@@@@@@@@@@@@@@@@@@@@ SWITCH IN + PRIMAL
.align 2

SwitchInCheckPrimal:
push {r0-r7}
ldr r0, FreeAegi3
mov r1, #0
strb r1, [r0]
mov r0, r10
mov r5, r10
mov r1, #1
and r1, r0
cmp r1, #0
beq Player3

AI3:
ldr r6, AIParty3
mov r1, #3
b CheckAegi

Player3:
ldr r6, PlayerParty3
mov r1, #0

CheckAegi:
ldr r0, MemLoc3
ldr r0, [r0]
add r0, #0x60
ldrb r0, [r0, r1]	@ loading the order of our party

HandleDouble3:
mov r1, #2
and r1, r5
cmp r1, #0				
beq First13
mov r1, #0xF
and r0, r1
b Continue223

First13:
mov r1, #0xF0
and r0, r1
lsr r0, r0, #4

Continue223:
mov r1, r6
mov r2, #0x64
mul r0, r2
add r0, r1
mov r4, r0
mov r1, #0xB
ldr r3, getattr3
bl Jump3
mov r6, r0
ldr r0, EvoPointer3
ldr r0, [r0]
mov r1, #NumberEvoPerPokemon
mov r2, #8
mul r1, r2
mul r1, r6
add r0, r1
mov r2, #PrimalReversionIndex
mov r7, #0

Loop3:
cmp r7, #NumberEvoPerPokemon
beq NoPrimal3
ldrh r3, [r0]
cmp r3, r2
beq CheckOrb3
add r0, #8
add r7, #1
b Loop3

CheckOrb3:
ldrh r7, [r0, #4]

bl BData
mov r3, r5
add r3, #0x2E
ldrh r3, [r3]
ldrh r2, [r0, #2]
cmp r3, #0
beq NoPrimal3
cmp r2, #0
beq NoPrimal3
cmp r2, r3
bne NoPrimal3
strh r7, [r5]
ldr r2, var8004
strh r7, [r2]

ChangePokeParty3:
mov r0, r4
mov r1, #0xB
ldr r3, setattr
bl Jump3
mov r0, r4
ldr r3, calcstats3
bl Jump3

ChangeBattleStats3:
mov r1, #0x54
mov r0, r4
ldr r3, getattr3
bl Jump3
strh r0, [r5, #2]
mov r1, #0x55
mov r0, r4
ldr r3, getattr3
bl Jump3
strh r0, [r5, #4]
mov r1, #0x56
mov r0, r4
ldr r3, getattr3
bl Jump3
strh r0, [r5, #6]
mov r1, #0x57
mov r0, r4
ldr r3, getattr3
bl Jump3
strh r0, [r5, #8]
mov r1, #0x58
mov r0, r4
ldr r3, getattr3
bl Jump3
strh r0, [r5, #0xA]

LoadAbilityBit3:
mov r1, #0x2E
mov r0, r4
ldr r3, getattr3
bl Jump3
add r5, #0x20
lsl r0, #0x1F
lsr r0, #0x1F

LoadAbilityTypeAndStore3:
ldr r2, PokePointer3
ldr r2, [r2]
mov r3, #28
mul r3, r7
add r3, r2
add r3, #6
ldrb r2, [r3]
strb r2, [r5, #1]
ldrb r2, [r3, #1]
strb r2, [r5, #2]
add r3, #16
ldrb r2, [r3]
strb r2, [r5]
ldr r0, Bank3
sub r0, #1
strb r2, [r0]
ldr r0, Bank3
mov r1, r10
strb r1, [r0]
pop {r0-r7}
ldr r0, PrimalBS
ldr r1, ExecScript
bl Jumpr1
b BackAfterScript

BData:
push {lr}
mov r1, #0x58
mul r5, r1
ldr r1, BattleData3
add r5, r1
pop {pc}

Jump3:
mov pc, r3

Jumpr1:
mov pc, r1

NoPrimal3:
pop {r0-r7}
StartCheckingSwitchInAbilities:
ldrb r3, [r5]
cmp r1, r3
blo CheckIfSwitchInAbility
mov r0, r10
strb r0, [r2]
 
CheckIfSwitchInAbility:
ldrb r0, [r7]
cmp r0, #0x2
bne CheckCloudNine

Drizzle:
ldr r2, FreeRam3
ldrb r1, [r2]
cmp r1, #0x00
bne NoSwitchInAbility
ldr r1, Drizzleback
bx r1

CheckCloudNine:
cmp r0, #0x0D
bne CheckIntimidate
b AirLockCloudNine

CheckIntimidate:
cmp r0, #0x16
bne CheckTrace

Intimidate:
ldr r1, Intimidateback
bx r1

CheckTrace:
cmp r0, #0x24
bne CheckSandStream

Trace:
ldr r1, Traceback
bx r1

CheckSandStream:
cmp r0, #0x2D
bne CheckForeCast

SandStream:
ldr r2, FreeRam3
ldrb r1, [r2]
cmp r1, #0x00
bne NoSwitchInAbility
ldr r1, Sandstreamback
bx r1

CheckForeCast:
cmp r0, #0x3B
bne CheckDrought

ForeCast:
ldr r1, Forecastback
bx r1

CheckDrought:
cmp r0, #0x46
bne CheckAirLock

Drought:
ldr r2, FreeRam3
ldrb r1, [r2]
cmp r1, #0x00
bne NoSwitchInAbility
ldr r1, Droughtback
bx r1

CheckAirLock:
cmp r0, #0x4D
bne CheckDelta

AirLockCloudNine:
ldr r2, FreeRam3
ldrb r1, [r2]
cmp r1, #0x00
bne NoSwitchInAbility
ldr r1, Airlockback
bx r1

CheckDelta:
cmp r0, #DeltaStreamAbility
bne CheckDeso

UpdateStrongWind:
ldr r0, CurrentWeather
mov r1, #0
strb r1, [r0]
ldr r0, FreeRam3
ldrb r1, [r0]
cmp r1, #DeltaStreamAbility
beq NoSwitchInAbility
mov r1, #DeltaStreamAbility
strb r1, [r0]
ldr r0, BattleScript3
bl ExecuteScript
BackAfterScript:
ldr r1, BackingAfterScript
bx r1

CheckDeso:
cmp r0, #DesolateLandAbility
bne CheckPrimo

UpdateDesolate:
ldr r2, FreeRam3
mov r0, #DesolateLandAbility
strb r0, [r2]
ldr r2, Droughtback
bx r2

CheckPrimo:
cmp r0, #PrimordialSeaAbility
bne CheckImposter

UpdatePrimordial:
ldr r2, FreeRam3
mov r0, #PrimordialSeaAbility
strb r0, [r2]
ldr r2, Drizzleback
bx r2

CheckImposter:
cmp r0, #ImposterAbility
beq Imposter

NoSwitchInAbility:
ldr r1, ReturnNoSwitch
bx r1

ExecuteScript:
ldr r1, Executescriptback
bx r1

Imposter:
mov r0, r10
ldr r1, Bank3
b CheckFlagAndStore

Store3:
ldr r0, BankTarget3
strb r4, [r0]
pop {r0-r4}
mov r0, r10
strb r0, [r1]

Next3:
ldr r0, BattleScriptImposter
bl ExecuteScript
b BackAfterScript

PopNoSwitch:
pop {r0-r4}
b NoSwitchInAbility

CheckFlagAndStore:
push {r0, r1}
ldr r0, NPokeOnTheField3
ldrb r1, [r0]
cmp r1, #2
beq Simple
pop {r0, r1}
cmp r0, #0
beq ThreeTarget
cmp r0, #2
beq OneTarget
cmp r0, #1
beq TwoTarget
mov r0, #0
b CheckTarget

DoubleBattle3:
ThreeTarget:
mov r0, #3
b CheckTarget

TwoTarget:
mov r0, #2
b CheckTarget

OneTarget:
mov r0, #1
b CheckTarget

Simple:
pop {r0, r1}
cmp r0, #0
beq OneTarget
mov r0, #0

CheckTarget:
push {r0-r4}
ldr r1, BattleData3
mov r2, #0x58
mov r4, r0
mul r0, r2
add r0, r1
add r0, #0x28
ldrh r3, [r0]
cmp r3, #0
beq PopNoSwitch
add r0, #0x28
ldr r3, [r0]
ldr r2, TransformSubStatut
and r2, r3
cmp r2, #0
bne PopNoSwitch
ldr r2, InvulnStatut
mov r0, r4
ldr r3, CheckInvulnerableStatut
lsl r0, #2
add r0, r3
ldr r3, [r0]
and r3, r2
cmp r3, #0
bne PopNoSwitch
b Store3

.align 2
Drizzleback: .word 0x8042B78+1
Intimidateback: .word 0x08042C08+1
Traceback: .word 0x08042C68+1
Sandstreamback: .word 0x08042BA8+1
Forecastback: .word 0x08042C3C+1
Droughtback: .word 0x08042BD8+1
Airlockback: .word 0x08042CA4+1
Executescriptback: .word 0x08042BEE+1
FreeAegi3: .word AegiFreeRam
BattleScriptImposter: .word BattleScriptForImposter
CheckInvulnerableStatut: .word 0x020242AC
PrimalBS: .word PrimalBattleScript
BattleScript3: .word BattleScriptDeltaStream
CurrentMove: .word 0x020241EA
CurrentWeather: .word 0x020243CC
FreeRam3: .word WeathersMegaPrimal
BankTarget3: .word 0x0202420C
Bank3: .word 0x0202420B
InvulnStatut: .word 0x400C0
TransformSubStatut: .word 0x1200000
NPokeOnTheField3: .word 0x0202406C
ReturnNoSwitch: .word 0x08042A7E+1
BackingAfterScript: .word 0x08042A72+1
ExecScript: .word 0x08042BEE+1
ScriptPusher3: .word 0x0803FAFC+1
CurBS3: .word 0x02024214
EvoPointer3: .word EvolutionPointerLocation
PokePointer3: .word PokemonDataPointer
BattleData3: .word 0x02024084
calcstats3: .word 0x08068D0C+1
setattr: .word 0x0806ACAC+1
var8004: .word 0x020275D8 + (0x8004*2)
MemLoc3: .word 0x0202449C
getattr3: .word 0x0806A518+1
PlayerParty3: .word 0x020244EC
AIParty3: .word 0x02024744



@@@@@@@@@@@@@@@@@@@@@@ FAINTPOKEMON (USER) HIJACK
.align 2

FaintPokemonCommand1:

Start4:
ldr r0, BankUser4
ldrb r0, [r0]
strb r0, [r1]
ldr r4, FreeRam4
ldrb r4, [r4]
cmp r4, #0
beq Return4
push {r0-r3}
ldr r1, BattleData4
mov r2, #0x58
mul r0, r2
add r0, r1
add r0, #0x20
ldrb r3, [r0]
ldrb r2, [r0, #8]
cmp r2, #0
bne PopAndReturn4
cmp r3, r4
beq Clear4
pop {r0-r3}
b Return4

Clear4:
pop {r0-r3}
ldr r4, FreeRam4
mov r3, #0
strb r3, [r4]
ldr r4, CurrentWeather4
strb r3, [r4]
bl StartingCheckMegaAegi4
ldr r0, BankTarget4
ldrb r0, [r0]
mov r9, r0
ldr r4, NormalWeatherScript4
ldr r0, SomethingBack4
bx r0

PopAndReturn4:
pop {r0-r3}

Return4:
bl StartingCheckMegaAegi4
ldr r0, BankTarget4
ldrb r0, [r0]
mov r9, r0
ldr r0, Back4
bx r0

StartingCheckMegaAegi4:
push {r0-r7,lr}
mov r5, r8
push {r5}
ldr r0, BankUser4
ldrb r2, [r0]
mov r1, #0x58
mul r2, r1
ldr r1, BattleData4
add r2, r1
add r2, #0x28
ldrh r2, [r2]
cmp r2, #0
bne ComeBack4
ldrb r0, [r0]
ldr r1, MemLoc4
ldr r1, [r1]
add r1, #0x60
mov r3, #0
mov r2, #1
and r2, r0
cmp r2, #0
beq Straight4
mov r3, #3

Straight4:
ldrb r5, [r1, r3]
mov r2, #2
and r2, r0
cmp r2, #0
beq First4
mov r1, #0xF
and r5, r1
b Continue14

First4:
mov r1, #0xF0
and r5, r1
lsr r5, r5, #4

Continue14:
mov r2, #0x64
mul r5, r2
cmp r3, #0
beq PlayerPartyLoading4

AIPartyLoading4:
ldr r4, AIParty4
b Continue4

PlayerPartyLoading4:
ldr r4, PlayerParty4

Continue4:
add r5, r4
ldrh r0, [r5, #0x20]
mov r7, r5
ldr r6, EvoPointer4
ldr r6, [r6]

Loop4:
ldr r1, AegiBlade4
cmp r0, r1
beq RevertAegi4

PrimalCheck4:
mov r3, #NumberEvoPerPokemon
mov r2, #8
mul r3, r2
mul r0, r3
add r0, r6
mov r2, #PrimalReversionIndex
mov r1, #0

Loop24:
cmp r1, #NumberEvoPerPokemon
beq ComeBack4
ldrh r3, [r0]
cmp r3, r2
bne CheckWish4

ContinueChecking4:
ldrh r3, [r0, #2]
cmp r3, #0
bne AddLoop24
ldrh r1, [r0, #4]
b Revert4

CheckWish4:
cmp r3, #WishMegaEvolutionIndex
bne CheckMega4
b ContinueChecking4

CheckMega4:
cmp r3, #MegaEvolutionIndex
bne AddLoop24
b ContinueChecking4

AddLoop24:
add r1, #1
b Loop24

RevertAegi4:
ldr r1, AegiShield4

Revert4:
strh r1, [r5, #0x20]
mov r0, r5
ldr r3, calcstats4
bl Jump4

ComeBack4:
pop {r5}
mov r8, r5
pop {r0-r7, pc}

Jump4:
mov pc, r3

.align 2
SomethingBack4: .word 0x08049C18+1
AegiBlade4: .word AegislashBlade
AegiShield4: .word AegislashShield
EvoPointer4: .word EvolutionPointerLocation
MemLoc4: .word 0x0202449C
CurrentWeather4: .word 0x020243CC
FreeRam4: .word WeathersMegaPrimal
BankTarget4: .word 0x0202420C
BankUser4: .word 0x0202420B
BattleData4: .word 0x02024084
MoxieandClear4: .word BattleScriptMoxieClear
Back4: .word 0x08049BF4+1
PokePointer4: .word PokemonDataPointer
calcstats4: .word 0x08068D0C+1
AIParty4: .word 0x02024744
PlayerParty4: .word 0x020244EC
NormalWeatherScript4: .word BattleScriptClearWeather


/*For that routine I assumed we applied the changes of this post : http://www.pokecommunity.com/showpost.php?p=8963359&postcount=773*/

@@@@@@@@@@@@@@@@@@@@@@@@ FAINTPOKEMON (TARGET) HIJACK
.align 2


FaintPokemonCommand2:

Start5:
ldr r1, b_active_side5
ldr r0, BankTarget5
ldrb r0, [r0]

StartCheckWeather5:
strb r0, [r1]
ldr r4, FreeRam5		@ We're checking if a mega or primal weather is active
ldrb r4, [r4]
cmp r4, #0
beq Return5
push {r0-r3}
ldr r1, BattleData5	@ We're loading the data of the pokemon
mov r2, #0x58
mul r0, r2
add r0, r1
add r0, #0x20
ldrb r3, [r0]
ldrb r2, [r0, #8]
cmp r2, #0			@ We're checking if still has HP
bne PopAndReturn5	@ If so, return
cmp r3, r4			@ Otherwise, we check if it is responsible of the actual mega or primal weather. If so, we remove it and play
beq Clear5			@ the apprioriated BS
pop {r0-r3}
b Return5

Clear5:
pop {r0-r3}
ldr r4, FreeRam5
mov r3, #0
strb r3, [r4]			@ So we clear the ram
ldr r4, CurrentWeather5
strb r3, [r4]			@ And the weather
bl StartingCheckMegaAegi5	@ Now we check if the poke has to be reverted, i.e is it a mega/primal or Aegi
ldr r0, BankUser5			@ Could be use for any other revert btw, if they exist
ldrb r0, [r0]
mov r9, r0
ldr r4, MoxieandClear5
ldr r0, ReturnSomething5
bx r0

PopAndReturn5:
pop {r0-r3}

Return5:
bl StartingCheckMegaAegi5	@ Here we also have to check if the poke need to be reverted
ldr r0, BankUser5
ldrb r0, [r0]
mov r9, r0
ldr r0, Back5
bx r0

StartingCheckMegaAegi5:
push {r0-r7,lr}
mov r5, r8
push {r5}
ldr r0, BankTarget5
ldrb r2, [r0]
mov r1, #0x58
mul r2, r1
ldr r1, BattleData5
add r2, r1
add r2, #0x28
ldrh r2, [r2]
cmp r2, #0		@ Check if the pokemon is indeed fainted
bne ComeBack5
ldrb r0, [r0]
ldr r1, MemLoc5
ldr r1, [r1]
add r1, #0x60	@ The pointer in MemLoc + 0x60 contains the order of your party and the party of the ennemy
mov r3, #0
mov r2, #1
and r2, r0		@ We "and" the Bank target with 1 to know whether we have to load the ennemy party or the player party
cmp r2, #0
beq Straight5
mov r3, #3		@ Used to load ennemy party
				@ Otherwise r3 stays 0
Straight5:		@ Our party takes the 3 first bytes, no matter how many poke you have, the 3 following are the order of the ennemy party
ldrb r5, [r1, r3]	@ So we load it accordingly thanks to r3
mov r2, #2
and r2, r0		@ Now we're "anding" again, but with 2, to know whether the pokemon is the first or the second (in double)
cmp r2, #0		@ Don't worry it works also in simple
beq First5
mov r1, #0xF	@ This party needs an explanation I think.
and r5, r1		@ So, assuming you have six pokemon, and you didn't switch/faint, the order in the RAM will be "01 23 45", 0 = first poke, 1 = second, and so on
b Continue15		@ It is the same for the ennemy, but 3 bytes further.
				@ So, if it is the first poke, by "anding" wih 0xF0, we save the "index" or whatever you call that of the first poke. by
First5:			@ "anding" with 0xF, we save the second poke. Then we have to divide by 16 (because hex) if it was the first poke (lsr r5, r5, #4)
mov r1, #0xF0	@ because if the order of your party was 23 45 01 for instance, anding with 0xF0 would give us 0x20, with the divide we have 0x2.
and r5, r1
lsr r5, r5, #4

Continue15:
mov r2, #0x64	@ Size of the data of a pokemon in the party (100 bytes or 0x64 bytes)
mul r5, r2		@ IndexOfOurPoke x Size of the data
cmp r3, #0		@ We're using r3 again to know whether we have to load the party of the player or the ennemy
beq PlayerPartyLoading5

AIPartyLoading5:
ldr r4, AIParty5
b Continue5

PlayerPartyLoading5:
ldr r4, PlayerParty5

Continue5:
add r5, r4
ldrh r0, [r5, #0x20]	@ Assuming you correctly applied the change to prevent the decrypt poke, the index number of your pokemon
mov r7, r5				@ is the halfword at the 0x20th byte in its battle data.
ldr r6, EvoPointer5
ldr r6, [r6]			@ Here I load the evolutions of the poke from a pointer, that way if you repointed you tables/expanded your evolution
						@ the routine still works.
Loop5:				@ Here the loop starts. It checks if your pokemon is aegi/primal/wish or mega evo, if so => Revert.
ldr r1, AegiBlade5
cmp r0, r1
beq RevertAegi5

PrimalCheck5:
mov r3, #NumberEvoPerPokemon
mov r2, #8
mul r3, r2
mul r0, r3
add r0, r6
mov r2, #PrimalReversionIndex
mov r1, #0

Loop25:
cmp r1, #NumberEvoPerPokemon
beq ComeBack5
ldrh r3, [r0]
cmp r3, r2
bne CheckWish5

ContinueChecking5:
ldrh r3, [r0, #2]
cmp r3, #0
bne AddLoop25
ldrh r1, [r0, #4]
b Revert5

CheckWish5:
cmp r3, #WishMegaEvolutionIndex
bne CheckMega5
b ContinueChecking5

CheckMega5:
cmp r3, #MegaEvolutionIndex
bne AddLoop25
b ContinueChecking5

AddLoop25:
add r1, #1
b Loop25

RevertAegi5:
ldr r1, AegiShield5

Revert5:
strh r1, [r5, #0x20]
mov r0, r5
ldr r3, calcstats5
bl Jump5

ComeBack5:
pop {r5}
mov r8, r5
pop {r0-r7, pc}

Jump5:
mov pc, r3

.align 2
b_active_side5: .word 0x02024064
ReturnSomething5: .word 0x08049C18+1
AegiBlade5: .word AegislashBlade
AegiShield5: .word AegislashShield
EvoPointer5: .word EvolutionPointerLocation
MemLoc5: .word 0x0202449C
CurrentWeather5: .word 0x020243CC
FreeRam5: .word WeathersMegaPrimal
BankTarget5: .word 0x0202420C
BankUser5: .word 0x0202420B
BattleData5: .word 0x02024084
MoxieandClear5: .word BattleScriptMoxieClear
Back5: .word 0x08049C16+1
PokePointer5: .word PokemonDataPointer
calcstats5: .word 0x08068D0C+1
AIParty5: .word 0x02024744
PlayerParty5: .word 0x020244EC


@@@@@@@@@@@@@@@@@@@@ CLEAR EVERYTHING END OF BATTLE
.align 2

ClearRamEndBattle:

CheckAegi6:
push {r0-r7}
mov r5, #0
ldr r7, PlayerParty6
ldr r6, EvoPointer6
ldr r6, [r6]

Loop6:
cmp r5, #6
beq BackEnd6
mov r4, r5
mov r2, #0x64
mul r4, r2
add r4, r7
mov r1, #0xB
mov r0, r4
ldr r3, getattr6
bl Jump6
cmp r0, #00
beq Loopplusone6
ldr r1, ABlade6		@ Check Aegi Blade
cmp r0, r1
beq RevertAegi6

PrimalCheck6:
mov r3, #NumberEvoPerPokemon
mov r2, #8
mul r3, r2
mul r0, r3
add r0, r6
mov r2, #PrimalReversionIndex
mov r1, #0

Loop26:
cmp r1, #NumberEvoPerPokemon
beq AddLoop6
ldrh r3, [r0]
cmp r3, r2
bne CheckWish6

ContinueChecking6:
ldrh r3, [r0, #2]
cmp r3, #0
bne AddLoop26
ldrh r1, [r0, #4]
b Revert6

CheckWish6:
cmp r3, #WishMegaEvolutionIndex
bne CheckMega6
b ContinueChecking6

CheckMega6:
cmp r3, #MegaEvolutionIndex
bne AddLoop26
b ContinueChecking6

AddLoop26:
add r1, #1
b Loop26

AddLoop6:
add r5, #1
b Loop6

Loopplusone6:
add r5, #1
b Loop6

BackEnd6:
pop {r0-r7}
b ClearRam6

RevertAegi6:
ldr r1, AShield6
b Revert6

Revert6:
add r5, #1
ldr r2, var80046
strh r1, [r2]
mov r0, r4
mov r1, #0xB
ldr r3, setattr6
bl Jump6
mov r0, r4
ldr r3, calcstats6
bl Jump6
b Loop6

ClearRam6:
ldr r0, Someotherstuff6
str r0, [r1]
ldr r0, FreeRam6
mov r1, #0
strb r1, [r0]
ldr r0, FreeRam26
strb r1, [r0]
ldr r0, MegaRam6
str r1, [r0]
str r1, [r0, #4]
strh r1, [r0, #8]
ldr r1, LocSomething6
ldr r0, LocStuff6
str r0, [r1]
ldr r0, Locstuffthing6
mov pc, r0

Jump6:
mov pc, r3

.align 2
Someotherstuff6: .word 0x803DE41
LocSomething6: .word 0x30061E8
LocStuff6: .word 0x8038421
Locstuffthing6: .word 0x803DE2A
MegaRam6: .word DoubleWordMega
FreeRam6: .word WeathersMegaPrimal
FreeRam26: .word AegiFreeRam
ABlade6: .word AegislashBlade
AShield6: .word AegislashShield
MemLoc6: .word 0x0202449C
getattr6: .word 0x0806A518+1
Bank6: .word 0x0202420B
PlayerParty6: .word 0x020244EC
calcstats6: .word 0x08068D0C+1
setattr6: .word 0x0806ACAC+1
var80046: .word 0x020275D8 + (0x8004*2)
EvoPointer6: .word EvolutionPointerLocation





@@@@@@@@@@@@ Battle script player/partner
.align 2

MegaBSPlayer:   @ MegaBattleScriptForPlayer

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringUU
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLL
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return

.align 2

BattleScriptWishPlayer: @ WishBattleScriptForPlayer

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringZZ
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLL
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return

.align 2

MegaBSPlayer2: @ MegaBattleScriptForPlayer2

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringUU2
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLL
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return

.align 2

BattleScriptWishPlayer2:	@ WishBattleScriptForPlayer2

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringZZ2
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLL
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return


@@@@@@@@@@@@@@@@@ Battle script AI first/second poke
.align 2

MegaBSForAI:	@ MegaBattleScriptForAI

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringUUAI
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLLAI
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return

.align 2

BattleScriptWishAI:	@ WishBattleScriptForAI

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringZZAI
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLLAI
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return

.align 2

MegaBSForAI2:	@ MegaBattleScriptForAI2

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringUUAI2
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLLAI
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return

.align 2

BattleScriptWishAI2:	@ WishBattleScriptForAI2

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringZZAI2
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLLAI
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return



@@@@@@@@@@@@@@@@@ Strings Player/Partner

.align 2

StringUU:	@ String UU

.byte BuffCharac, 0xF, Space, i_, s_, Space, r_, e_, a_, c_, t_, i_, n_, g_, Space, t_
.byte o_, Space, BuffCharac, 0x23, JumpLine, M_, e_, g_, a_, Space, B_, r_, a_, c_, e_, l_
.byte e_, t_, Exclam, Termin

.align 2

StringZZ:	@ String ZZ

.byte BuffCharac, 0x23, Apos, s_, Space, f_, e_, r_, v_, e_, n_, t_, Space, w_, i_, s_
.byte h_, JumpLine, r_, e_, a_, c_, h_, e_, d_, Space, BuffCharac, 0x0F, Exclam, Termin

.align 2

StringLL:		@ String LL

.byte BuffCharac, 0xF, Space, h_, a_, s_, Space, M_, e_, g_, a_, Space, E_, v_, o_, l_
.byte v_, e_, d_, Space, i_, n_, t_, o_, JumpLine, M_, e_, g_, a_, Space, BuffCharac, 0xF
.byte Exclam, Termin

.align 2

StringUU2:		@ String UU2

.byte BuffCharac, 0xF, Space, i_, s_, Space, r_, e_, a_, c_, t_, i_, n_, g_, Space, t_
.byte o_, Space, BuffCharac, 0x33, Apos, s_, JumpLine, M_, e_, g_, a_, Space, B_, r_, a_, c_, e_, l_
.byte e_, t_, Exclam, Termin

.align 2

StringZZ2:  	@ String ZZ2

.byte BuffCharac, 0x33, Apos, s_, Space, f_, e_, r_, v_, e_, n_, t_, Space, W_, i_, s_
.byte h_, JumpLine, r_, e_, a_, c_, h_, e_, d_, Space, BuffCharac, 0xF, Exclam, Termin


@@@@@@@@@@@@@@@@@@ Strings AI/Second poke AI

.align 2

StringUUAI:		@ String UUAI

.byte BuffCharac, 0xF, Space, i_, s_, Space, r_, e_, a_, c_, t_, i_, n_, g_, Space, t_
.byte o_, Space, BuffCharac, 0x1D, JumpLine, M_, e_, g_, a_, Space, B_, r_, a_, c_, e_, l_
.byte e_, t_, Exclam, Termin

.align 2

StringZZAI:		@ String ZZAI

.byte BuffCharac, 0x1D, Apos, s_, Space, f_, e_, r_, v_, e_, n_, t_, Space, W_, i_, s_
.byte h_, JumpLine, r_, e_, a_, c_, h_, e_, d_, Space, BuffCharac, 0xF, Exclam, Termin

.align 2

StringLLAI:		@ String LLAI

.byte BuffCharac, 0xF, Space, h_, a_, s_, Space, M_, e_, g_, a_, Space, E_, v_, o_, l_
.byte v_, e_, d_, Space, i_, n_, t_, o_, JumpLine, M_, e_, g_, a_, Space, BuffCharac, 0x8
.byte Exclam, Termin

.align 2

StringUUAI2: 	@ String UUAI2

.byte BuffCharac, 0xF, Space, i_, s_, Space, r_, e_, a_, c_, t_, i_, n_, g_, Space, t_
.byte o_, Space, BuffCharac, 0x2F, JumpLine, M_, e_, g_, a_, Space, B_, r_, a_, c_, e_, l_
.byte e_, t_, Exclam, Termin

.align 2

StringZZAI2:	@ String ZZAI2

.byte BuffCharac, 0x2F, Apos, s_, Space, f_, e_, r_, v_, e_, n_, t_, Space, W_, i_, s_
.byte h_, JumpLine, r_, e_, a_, c_, h_, e_, d_, Space, BuffCharac, 0xF, Exclam, Termin


@@@@@@@@@@@ End Strings.

.align 2

PlayCryRoutine:		@@ Play cry routine

push {lr}
ldr r0, BattleData8
ldr r3, Bank8
ldrb r3, [r3]
mov r2, #0x58
mul r3, r2
add r3, r0
ldrh r0, [r0]
mov r1, #0x19
neg r1, r1
mov r2, #0x5
ldr r3, play_cry_maybe8
bl Jump8
pop {pc}

Jump8:
mov pc, r3

.align 2
Bank8: .word 0x202420B
BattleData8: .word 0x2024084
play_cry_maybe8: .word 0x80A32E4+1

.align 2

HelpSwitchInRoutine:	@@ Help switch-in abilities for mega

Start9:
push {lr}
ldr r1, bank9
ldrb r1, [r1]
mov r0, #0
mov r2, #0
mov r3, #0
ldr r4, ability_something9
bl Jump9
cmp r0, #0
beq Return9
ldr r0, CurrentBS9
ldr r1, [r0]
sub r1, #5
str r1, [r0]
pop {pc}

Return9:
pop {pc}

Jump9:
mov pc, r4

.align 2
bank9: .word 0x0202420B
ability_something9: .word 0x08042828+1
CurrentBS9: .word 0x02024214

.align 2

BattleScriptPrimal:		@@ PrimalBattleScript

.byte SetWordCommand			
.word LocationStringPrim
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringPrimal
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchInPrimal
.byte 0x41	@ Call command
.word 0x82DB48D
.byte 0x3F	@ End3 command

.align 2

StringPrimalNull:
.byte Space, Termin

.align 2

StringPrimal:	@ String primal

.byte BuffCharac, 0x13, Apos, s_, Space, P_, r_, i_, m_, a_, l_, Space, R_, e_, v_, e_
.byte r_, s_, i_, o_, n_, Exclam, JumpLine, i_, t_, Space, r_, e_, v_, e_, r_, t_
.byte e_, d_, Space, t_, o_, Space, i_, t_, s_, Space, p_, r_, i_, m_, a_, l_
.byte Space, f_, o_, r_, m_, Exclam, Termin

.align 2

PrimalSwitchInRoutine:		@ Help switch-in primal.

Start10:
push {lr}
ldr r1, bank10
ldrb r1, [r1]
cmp r1, #0
beq Return
mov r0, #0
mov r2, #0
mov r3, #0
ldr r4, ability_something10
bl Jump10
cmp r0, #0
beq Return10
ldr r0, CurrentBS10
ldr r1, [r0]
sub r1, #5
str r1, [r0]

Return10:
pop {pc}

Jump10:
mov pc, r4

.align 2
bank10: .word 0x0202420B
ability_something10: .word 0x08042828+1
CurrentBS10: .word 0x02024214

.align 2

ImposterBattleScript: @ Battlescript imposter

.byte CallAsmCommand
.word FirstRoutineTransform
.byte 0x9B	@ Transformdataexecution
.byte 0x9	@ Attackanimation
.byte 0xA	@ Waitanimation
.byte 0x13	@ Printfromtable
.word 0x85CC8D4
.byte 0x12, 0x40, 0x0
.byte CallAsmCommand
.word SecondRoutineTransform
.byte 0x3F	@ End3

.align 2

RoutineTransformSecond: @ FirstRoutineTransform

push {lr}
ldr r0, CurrentMove11
ldrh r1, [r0]
ldr r2, ImposterLocation11
strh r1, [r2]
mov r1, #0x90	@ Transform Move
strh r1, [r0]
pop {pc}

.align 2
ImposterLocation11: .word ImposterLocation
CurrentMove11: .word 0x20241EA

.align 2

RoutineTransformFirst: @ SecondRoutineTransform

push {lr}
ldr r0, CurrentMove12
ldr r2, ImposterLocation12
ldrh r1, [r2]
strh r1, [r0]
pop {pc}

.align 2
ImposterLocation12: .word ImposterLocation
CurrentMove12: .word 0x20241EA

.align 2

ScriptMoxieClear: @ BattleScriptMoxieClear

.byte 0x56, 0x0, 0x39, 0x40, 0x0, 0x1A, 0x0, 0x1B, 0x0, 0x10, 0x1D, 0x0, 0x10, 0x30, 0x1, 0x39
.byte 0x40, 0x0
.byte SetWordCommand
.word YourWordLocation
.word StringWeatherFade+SizeRom
.byte 0x10, 0x84, 0x1, 0x12, 0x40, 0x0, 0x1E, 0x1
.byte MoxieAbility
.word JumpIfMoxie+SizeRom
.byte 0x3C

JumpIfMoxie:
.byte 0xE3, 0x1
.word JumpIfUserFainted+SizeRom
.byte 0x20, 0x1, 0x0, 0x1, 0xC
.word JumpIfUserFainted+SizeRom
.byte 0x39, 0x20, 0x0, 0x2E
.word 0x202448F
.byte 0x0, 0x48, 0x1, 0x2, 0x0, 0x2E
.word 0x202448E
.byte 0x11, 0x89, 0x41
.word JumpIfUserFainted+SizeRom
.byte SetWordCommand
.word YourWordLocation
.word StringMoxie+SizeRom
.byte 0x10, 0x84, 0x1, 0x12, 0x40, 0x0

JumpIfUserFainted:
.byte 0x3C


.align 2
StringWeatherFade:
.byte T_, h_, e_, Space, w_, e_, a_, t_, h_, e_, r_, Space, h_, a_, s_, Space
.byte r_, e_, t_, u_, r_, n_, e_, d_, JumpLine, t_, o_, Space, n_, o_, r_, m_
.byte a_, l_, Exclam, Termin

.align 2
StringMoxie:
.byte BuffCharac, 0xF, Apos, s_, Space, M_, o_, x_, i_, e_, JumpLine, r_, a_, i_, s_, e_
.byte d_, Space, i_, t_, s_, Space, A_, t_, t_, a_, c_, k_, Exclam, Termin

.align 2

DeltaStreamBattleScript:	@@ BattleScriptDeltaStream

.byte SetWordCommand
.word YourWordLocation
.word StringDeltaStream+SizeRom
.byte 0x10, 0x84, 0x1, 0x12, 0x40, 0x0, 0x45
.byte 0x0
.byte YourDeltaStreamAnimIndex
.word 0x0
.byte 0xA, 0x41
.word 0x82DB48D
.byte 0x3F
.byte 0xFF
.byte 0xFF

.align 2
StringDeltaStream:
.byte A_, Space, m_, y_, s_, t_, e_, r_, i_, o_, u_, s_, Space, a_, i_, r_
.byte Space, c_, u_, r_, r_, e_, n_, t_, Space, i_, s_, JumpLine, p_, r_, o_, t_
.byte e_, c_, t_, i_, n_, g_, Space, F_, l_, y_, i_, n_, g_, Dash, t_, y_
.byte p_, e_, Space, P_, o_, k_, e_, m_, o_, n_, Exclam, Termin

.align 2

ClearWeatherBattleScript:	@ BattleScriptClearWeather

.byte SetWordCommand
.word YourWordLocation
.word StringWeatherFade+SizeRom
.byte 0x10, 0x84, 0x1, 0x12, 0x40, Space, 0x56, 0x1, 0x39, 0x40, 0x0, 0x1A, 0x1, 0x1B, 0x1, 0x10
.byte 0x1C, Space, 0x3C, 0xFF


Explanations:
Spoiler:

-I use the same principle than Touched. That is to say, to set a mega evolution, you have to set the evolution of you pokemon to 0xFC or 0xFE, the first
being Wish mega evo, taking the attack as argument, the second being the classic mega evolution, taking the index number of your Mega Stone as argument.
The target will be your Mega/Primal form.
-Primal reversion is the 0xFD evolution and also takes the item to hold as argument.
-To make your mega/primal form revert, you have to give your Primal/Mega the 0xFC, 0xFD or 0xFE evolution with no argument (0), but this time the target will be
the reverted species.

-To active the mega evolution, when selecting an attack, press start, you will hear a sound if it has been activated. Though there are no graphic. Press B or select to desactivate it.

-This should support expanded evolution and expanded pokedex if you change the values accordingly in the routines. Indeed I load the evolution table/pokemon data from a
pointer rather than directly.

-Multi, Tag and double battles are supported.

-To make trick and such ineffective against mega poke, it's inside the multitype routine in my signature.

-Give your pokemon and their mega evolution the same xp ratio, HP, Name, EggGroup, IndexNumber etc
-What you can change is, as in the officials games : att/def/speed/attspe/defspe ability (only one), type.

To do list:
Spoiler:

-The sentence will always be with the item "Mega Bracelet" (I don't know if it's the right word), I didn't added support for custom item name yet.

-I also plan to add a check for a flag or something, this way you can allow/prevent the player from mega evolving.

-There are no icon yet, I don't know if I'll do this one day.

-It's the cry of the normal species that is played when your mega faint, I know why, I'll see what I can do for that.

Bugs:
Spoiler:

-This version might be buggy, I'll upload a bug-free one when I have time.

Lance32497

LanceKoijer of Pokemon_Addicts

Male
Criscanto town-Ginoa Region xD
Seen August 13th, 2017
Posted January 1st, 2017
792 posts
8.7 Years
I had successfully installed your mega evolution hack, but what it does is it restarts the game whenever the trainerbattle ends, here's the vid

EDIT: It works on a clean rom, but on edited rom, it's not
This signature has been disabled.
Scrollbar appears
Please review and fix the issues by reading the signature rules.

You must edit it to meet the limits set by the rules before you may remove the [sig-reason] code from your signature. Removing this tag will re-enable it.

Do not remove the tag until you fix the issues in your signature. You may be infracted for removing this tag if you do not fix the specified issues. Do not use this tag for decoration purposes.
Male
Seen February 1st, 2018
Posted October 19th, 2017
625 posts
8.9 Years
I had successfully installed your mega evolution hack, but what it does is it restarts the game whenever the trainerbattle ends, here's the vid

EDIT: It works on a clean rom, but on edited rom, it's not
What exactly have you changed in this ROM? There might be clashing hooks that cause the crash. Is it with all battles or just the first Gary battle?

A Pokemon that is discriminated!
Support squirtle and make it everyone's favourite.

Lance32497

LanceKoijer of Pokemon_Addicts

Male
Criscanto town-Ginoa Region xD
Seen August 13th, 2017
Posted January 1st, 2017
792 posts
8.7 Years
What exactly have you changed in this ROM? There might be clashing hooks that cause the crash. Is it with all battles or just the first Gary battle?
All battles, even the wild battle

EDIT: I tried it in clean rom but some weird things happened, I set Var 4001 to 162 then the enemy's HP bar shows the mega symbol, and, even I press start button, nothing happens, it automatically mega evolves the pokemon
This signature has been disabled.
Scrollbar appears
Please review and fix the issues by reading the signature rules.

You must edit it to meet the limits set by the rules before you may remove the [sig-reason] code from your signature. Removing this tag will re-enable it.

Do not remove the tag until you fix the issues in your signature. You may be infracted for removing this tag if you do not fix the specified issues. Do not use this tag for decoration purposes.

Christos

Male
Seen April 16th, 2020
Posted December 16th, 2019
6,355 posts
17.2 Years
Disculpe no me sale la verdad he estado intentando varias veces pero no queda, no se si tengo algún error.
This is an English speaking forum. Please post in English, or include an English translation along with your posts.

A Pokemon that is discriminated!
Support squirtle and make it everyone's favourite.

Lance32497

LanceKoijer of Pokemon_Addicts

Male
Criscanto town-Ginoa Region xD
Seen August 13th, 2017
Posted January 1st, 2017
792 posts
8.7 Years
I hav 1 question though, could it be possible to make a 2 Mega Evolutions in 2v2 Battle?
This signature has been disabled.
Scrollbar appears
Please review and fix the issues by reading the signature rules.

You must edit it to meet the limits set by the rules before you may remove the [sig-reason] code from your signature. Removing this tag will re-enable it.

Do not remove the tag until you fix the issues in your signature. You may be infracted for removing this tag if you do not fix the specified issues. Do not use this tag for decoration purposes.
Male
Seen 3 Weeks Ago
Posted June 12th, 2020
48 posts
8.7 Years

Mega Evolution, Wish and Primal support for Emerald:



Little demonstration :


Spoiler:

You have to read the red bold comments.

You'll have to use the setword command, the the loader string, and the callasm command. I assume you have these.
I also assume you know how to compile/insert ASM routine.
Lastly, apply the changes of this post : http://www.pokecommunity.com/showpost.php?p=8963359&postcount=773


Task Swap sprite (HUGE thanks to Touched and his explanations. I just had to modify his code a bit to make it work):
Spoiler:

Spoiler:

@@@@@@@@@@@@@        Trying task swap sprite

.thumb
.thumb_func

.equ TheValue, 0x40

start:
push {r4-r7, lr}
ldr r1, Attackeranim
ldr r2, battle_side_objid
ldrb r3, [r1]
ldrb r2, [r2, r3]
ldr r3, tasks
mov r12, r3
lsl r4, r0, #2
add r4, r4, r0
lsl r4, r4, #3
add r4, r12
mov r7, r4
ldrh r3, [r4, #0x1C]
cmp r3, #2
beq AddTaskMaybe
cmp r3, #0
beq Hidingsprite
cmp r3, #1
beq WeRefreshGraphics
cmp r3, #3
beq TaskRunningSomething
cmp r3, #4
bne End
lsl r3, r2, #4
add r2, r3, r2
ldr r3, somethingnotsureeither
mov r12, r3
lsl r2, r2, #2
ldrh r3, [r4, #0x1E]
add r2, r12
strh r3, [r2]
ldr r3, move_anim_task_del
bl Jump

End:
pop {r4-r7, pc}


WeRefreshGraphics:
ldr r3, Refreshgraphics
ldrb r0, [r1]
mov r1, #1
bl Jump

AddEnd:
ldrh r3, [r4, #0x1C]
add r3, #1
strh r3, [r4, #0x1C]
b End

Hidingsprite:
ldr r0, somethingnotsureeither
mov r12, r0
lsl r3, r2, #4
add r3, r3, r2
lsl r3, r3, #2
add r3, r12
mov r5, r3
ldrh r0, [r3]
strh r0, [r4, #0x1E]
mov r0, #TheValue
neg r0, r0
strh r0, [r3]
ldrh r3, [r4, #0x1C]
add r3, #1
strh r3, [r4, #0x1C]
b End


TaskRunningSomething:
ldr r3, task_find_id_by_funcptr
ldr r0, SomeRoutine
bl Jump
ldr r3, task_is_running
bl Jump
cmp r0, #0
bne End
ldrh r3, [r4, #0x1C]
add r3, #1
strh r3, [r4, #0x1C]
b End

AddTaskMaybe:
lsl r3, r2, #4
add r2, r3, r2
ldr r3, somethingnotsure
mov r12, r3
mov r1, #4
lsl r2, r2, #2
add r2, r12
ldrb  r3, [r2]
bic r3, r1
strb r3, [r2]
ldr r0, SomeRoutine
ldr r3, taskadd
mov r1, #5
ldr r6, tasks
bl Jump
lsl r3, r0, #2
add r0, r3, r0
mov r3, #0
lsl r0, r0, #3
add r0, r6, r0
strh r3, [r0, #8]
ldr r3, Attackeranim
ldrb r3, [r3]
strh r3, [r0, #0xC]
ldrh r3, [r4, #0x1C]
add r3, #1
strh r3, [r4, #0x1C]
b End




Jump:
bx r3

.align 2
Attackeranim: .word 0x2038436
battle_side_objid: .word 0x20241E4
tasks: .word 0x3005E00
somethingnotsure: .word 0x202066E @@@ Not sure this is the exact location, have to test/ask
somethingnotsureeither: .word 0x2020650 @@@ Not sure this is the exact location, have to test/ask
SomeRoutine: .word 0x80A4980+1
taskadd: .word 0x80A8FB0+1
Refreshgraphics: .word 0x805E8F4+1
task_find_id_by_funcptr: .word 0x80A921C+1
task_is_running: .word 0x80A91E4+1
move_anim_task_del: .word 0x80A3C1C+1

Note the location of this routine, we'll use it in the next step.

The awesome animation (HUGE thanks to MrDS and his explanations !):
Spoiler:

@ Just compile it and insert it as an ASM routine

.global mega_animation_script_data

	.equ ANIM_XX, 0x27a3 	@@ Change accordingly to your particles
	.equ ANIM_YY, 0x281b 	@@ Those I used are in the video
	.equ ANIM_ZZ, 0x27d8

	.equ SizeRom, 0x8000000
	
	.equ TaskSwapSprite, SizeRom+1+0x______ @ Location of your swap sprite routine something like 0xA00000, without the 0x8 prefix
	
	
.org 0x______	@ Change to wherever you want to insert your animation, again, without the 0x8 prefix


@Buffer Particles:

.byte 0x00 
.hword ANIM_XX
.byte 0x00 
.hword ANIM_YY
.byte 0x00 
.hword ANIM_ZZ

@Charge rainbow particle

.byte 0x0A, 0x02 , 0x0C , 0x0C , 0x08 , 0x1C , 0x8C , 0x00 , 0xC0 , 0x0D , 0x03 
.byte 0x03 , 0x05 , 0x5A , 0x11 , 0x08 , 0x02 , 0x06 , 0x02 , 0x00 , 0x00 , 0x00 
.byte 0x06 , 0x00 , 0x00 , 0x00 , 0x0B , 0x00 , 0xFF , 0x2F 
.byte 0x0E 
.word anim_rainbow_x+SizeRom
.byte 0x0E 
.word anim_rainbow_x+SizeRom
.byte 0x0E 
.word anim_rainbow_x+SizeRom
.byte 0x05 , 0x0B

@Load mega stone :

.byte 0x02 , 0x0D , 0x04 , 0x00 , 0x17 , 0x0A , 0x00 , 0x0C , 0x0C , 0x08 , 0x19 , 0xC9 , 0x00 , 0xC0 
.byte 0x02 
.word anim_mega_stone_x+SizeRom
.byte 0x29 , 0x04 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 

@Whiteout screen:

.byte 0x04 , 0x14 , 0x03 , 0x65 , 0x66 , 0x11 , 0x08 , 0x05 , 0x05 , 0x05 , 0x00 , 0x02 , 0x00 , 0x00 , 0x00 
.byte 0x10 , 0x00 , 0xFF , 0xFF , 0x03 , 0x49 , 0x68 , 0x11 , 0x08 , 0x05 , 0x05 
.hword ANIM_YY
.byte 0x02 , 0x00 , 0x00 , 0x00 , 0x10 , 0x00 , 0xFF , 0xFF , 0x05 , 0x03 , 0x49 , 0x68 , 0x11 , 0x08 , 0x05

@Swap_spprite:

.byte 0x00 , 0x03 
.word 0x8DE7910+1 @@@@@@@@@@ Your routine swap sprite
.byte 0x02 , 0x00 

@Return screen:

.byte 0x00 , 0x2D , 0x27 , 0x03 , 0x65 , 0x66 , 0x11 , 0x08, 0x05 , 0x05 , 0x05 , 0x00 , 0x02 , 0x00 
.byte 0x10 , 0x00 , 0x00 , 0x00 , 0xFF , 0xFF , 0x03 , 0xDD , 0x52 , 0x11 , 0x08 , 0x05 , 0x03 , 0x01 
.byte 0x00 , 0x05 , 0x00 , 0x0E , 0x00 , 0x05 , 0x0B 

@ Show Mega evolution symbol:

.byte 0x02 , 0x04 , 0x01 , 0x0A , 0x03 , 0x19 , 0x79 , 0x00 , 0x3F 
.byte 0x02 
.word anim_mega_symbol_x+SizeRom
.byte 0x82 , 0x00 , 0x05 , 0x0B , 0x0D , 0x08 , 0x08 


.align 2
anim_rainbow_x:
.byte 0x02
.word anim_rainbow_y+SizeRom
.byte 0x02 , 0x03 , 0x03 , 0x28 , 0x00 , 0xF6 , 0x0D , 0x00 , 0x04 , 0x03 
.byte 0x02 
.word anim_rainbow_y+SizeRom
.byte 0x02 , 0x03 , 0xDD , 0xFF , 0xF6 , 0xFF , 0x0D , 0x00 , 0x04 , 0x03 
.byte 0x02 
.word anim_rainbow_y+SizeRom
.byte 0x02 , 0x03 , 0x0F , 0x00 , 0xD8 , 0xFF , 0x0D , 0x00 , 0x04 , 0x03 
.byte 0x02 
.word anim_rainbow_y+SizeRom
.byte 0x02 , 0x03 , 0xF6 , 0xFF , 0xE0 , 0xFF , 0x0D , 0x00 , 0x04 , 0x03 
.byte 0x02 
.word anim_rainbow_y+SizeRom
.byte 0x02 , 0x03 , 0x19 , 0x00 , 0xEC , 0xFF , 0x0D , 0x00 , 0x04 , 0x03 
.byte 0x02 
.word anim_rainbow_y+SizeRom
.byte 0x02 , 0x03 , 0xD8 , 0xFF , 0xEC , 0xFF , 0x0D , 0x00 , 0x04 , 0x03 
.byte 0x02 
.word anim_rainbow_y+SizeRom
.byte 0x02 , 0x03 , 0x05 , 0x00 , 0xD8 , 0xFF , 0x0D , 0x00 , 0x04 , 0x03 , 0x0F 


.align 2
anim_rainbow_y:
.hword ANIM_XX
.hword ANIM_XX
.byte 0x8C , 0x4A , 0x52 , 0x08 , 0xC8 , 0x21 , 0x59 , 0x08 , 0x00 , 0x00 , 0x00 , 0x00 
.byte 0xDC , 0x21 , 0x59 , 0x08 , 0xE1 , 0xE8 , 0x0F , 0x08

.align 2
anim_mega_stone_x:
.hword ANIM_YY
.hword ANIM_YY
.byte 0xFC , 0x4A , 0x52 , 0x08 , 0x9C , 0xC6 , 0x2E , 0x08 , 0x00 , 0x00 , 0x00 , 0x00 , 0x94
.byte 0x68 , 0x59 , 0x08 , 0xC9 , 0x77 , 0x0A , 0x08 


.align 2
anim_mega_symbol_x:
.hword ANIM_ZZ
.hword ANIM_ZZ
.byte 0x34 , 0x4A , 0x52 , 0x08 , 0x9C , 0xC6 , 0x2E , 0x08 , 0x00 , 0x00 , 0x00 , 0x00
.byte 0xA8 , 0xC6 , 0x2E , 0x08 , 0x79 , 0x2B , 0x11 , 0x08

.align 2
swap_sprite_routine: .word TaskSwapSprite

Expand the playanimation table :
Spoiler:

At 0x5DA3C there's a pointer. Change this pointer to your new table which will be :
9E 7E 2D 08 D4 7E 2D 08 DD 7E 2D 08 1A 7F 2D 08 22 7F 2D 08 6A 7F 2D 08 75 7F 2D 08 
08 81 2D 08 7C 81 2D 08 58 82 2D 08 A5 82 2D 08 EE 82 2D 08 F3 82 2D 08 F8 82 2D 08 
FD 82 2D 08 0B 83 2D 08 3C 83 2D 08 61 83 2D 08 BF 83 2D 08 1E 84 2D 08 E3 84 2D 08 
23 85 2D 08 66 85 2D 08 E3 85 2D 08 04 86 2D 08 15 86 2D 08 26 86 2D 08 7A 86 2D 08 
93 86 2D 08 9D 86 2D 08
We can see the last vanilla animation in bold, its index is 0x1D. Keep that in mind : the next animation will be the index 0x1E, then 0x1F, and so on.
So, put the animation of the mega evolution here, keep its index in mind in case you added other animation.

Main routines :
Spoiler:

Before you panic because the routine is long, here are the explanations :

-The first thing you have to do is to change the values of the red lines if needed.
-Then, you take the whole routine, you don't even have to remove the comments, and you compile it. Keep the code with you.
-The output will be a big file, like 10 Mo, (depending on where you want to insert it). Basically, it'll be filled of zeroes.
-What you have to do is go to the hook location, scroll down a bit in the routine and you'll see them, and copy/paste what you find
from your compiled routine to your ROM, at the same addresses. It'll be like 8 - 10 bytes (you have to take the leading 0, so it'll be like 00 49 08 47 81 90 AA 08).
-Then go to the location "Myloc", and select from "Myloc" until the end of the output file, and again, copy/paste from your output to your ROM.
-You've finished ! Read the explanation to set up the mega evolutions/primal reversion.
Spoiler:

.thumb
.thumb_func
.global megahack



@@@@ What you have to read !

/*For that routine I assumed we applied the changes of this post : http://www.pokecommunity.com/showpost.php?p=8963359&postcount=773*/

@ 0x1300 (4900) bytes of freespace needed.


.equ Myloc, 0x______	@ This will be the location where the whole code will be inserted, make sure it has 0x1300 (4900) bytes of freespace.
							@ That address musts end with 0
						

.equ DoubleWordMega, 0x0203E3E0	@ Double word (8 bytes) of free ram, used for Mega evolution, must ends with 0, it's important.
.equ YourWordLocation, 0x203E320 	@ Word used by your setword command
.equ ImposterLocation, 0x203E3FC	@ byte used by imposter
.equ WeathersMegaPrimal, 0x0203E330	@ One byte of free ram used for mega/primal weathers
.equ AegiFreeRam, 0x203E33C			@ One byte of free ram used for Aegislash transformation


.equ YourMegaAnimationIndex, 0x1E	@ Index of the animation, you remember the expanded table ? Put Delta stream and Mega animation here.
.equ YourDeltaStreamAnimIndex, 0x1F

.equ SetWordCommand, 0xFA	@ As I said, these commands have to be implemented, so put their index here.
.equ CallAsmCommand, 0xF9

@@@ If you don't want the following abilities to work, just give them an unused index (like 0xF5) and don't give it to your pokémons.

.equ DeltaStreamAbility, 0xC0	@ Abilities indexes
.equ PrimordialSeaAbility, 0xBE
.equ DesolateLandAbility, 0xBF
.equ ImposterAbility, 0x97
.equ MoxieAbility, 0x9A

.equ NumberEvoPerPokemon, 0x5	@ Number of evolution you have per pokemon
.equ SoundWeWantToPlay, 0x1		@ Sound we want to play when triggering the mega evolution.

.equ MegaEvolutionIndex, 0xFE	@ I choose to do as Touched here. 0xFE = Mega evolution
.equ WishMegaEvolutionIndex, 0xFC	@ 0xFC = Wish
.equ PrimalReversionIndex, 0xFD	@ 0xFD = Primal

@@ If you don't want aegislash, just give it an index unused like 0x800

.equ AegislashShield, 0x142	@ Index number of Aegislash Shield
.equ AegislashBlade, 0x113	@ Index number of Aegislash Blade

.equ PokemonDataPointer, 0x803A270	@ I don't see why it would change since it's a pointer for the pokemon data, but just in case, I put it here
.equ EvolutionPointerLocation, 0x806D140 @ I don't see why it would change since it's a pointer for the evolution data, but just in case, I put it here


@@@@From now on, no need to read, everything is automated



@@Locations BS
.equ MegaBattleScriptForPlayer, MegaBSPlayer+SizeRom

.equ MegaBattleScriptForAI, MegaBSForAI+SizeRom

.equ WishBattleScriptForPlayer, BattleScriptWishPlayer+SizeRom

.equ WishBattleScriptForAI, BattleScriptWishAI+SizeRom

.equ MegaBattleScriptForPlayer2, MegaBSPlayer2+SizeRom

.equ MegaBattleScriptForAI2, MegaBSForAI2+SizeRom

.equ WishBattleScriptForPlayer2, BattleScriptWishPlayer2+SizeRom

.equ WishBattleScriptForAI2, BattleScriptWishAI2+SizeRom

.equ BattleScriptForImposter, ImposterBattleScript+SizeRom

.equ PrimalBattleScript, BattleScriptPrimal+SizeRom

.equ BattleScriptDeltaStream, DeltaStreamBattleScript+SizeRom

.equ BattleScriptMoxieClear, ScriptMoxieClear+SizeRom

.equ BattleScriptClearWeather, ClearWeatherBattleScript+SizeRom


.equ SizeRom, 0x8000000


@@ Stuffs BS
.equ LocationStringZZAI2, StringZZAI2+SizeRom
.equ LocationStringLLAI, StringLLAI+SizeRom
.equ PlayCry, 1+PlayCryRoutine+SizeRom
.equ HelpSwitchIn, 1+HelpSwitchInRoutine+SizeRom
.equ LocationStringPrim, StringPrimalNull+SizeRom
.equ HelpSwitchInPrimal, 1+PrimalSwitchInRoutine+SizeRom
.equ LocationStringPrimal, StringPrimal+SizeRom
.equ LocationStringUUAI2, StringUUAI2+SizeRom
.equ LocationStringZZAI, StringZZAI+SizeRom
.equ LocationStringUUAI, StringUUAI+SizeRom
.equ LocationStringZZ2, StringZZ2+SizeRom
.equ LocationStringLL, StringLL+SizeRom
.equ LocationStringUU2, StringUU2+SizeRom
.equ LocationStringZZ, StringZZ+SizeRom
.equ LocationStringUU, StringUU+SizeRom

.equ SecondRoutineTransform, 1+RoutineTransformFirst+SizeRom
.equ FirstRoutineTransform,	1+RoutineTransformSecond+SizeRom


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@         HERE ARE THE HOOKS + a little routine


@@ Trigger player

.org 0x3BE80

ldr r0, Hook
mov pc, r0

.align 2

Hook: .word SizeRom+1+Myloc

@@ Clear ram end battle

.org 0x3DDE4

ldr r0, Hook6
mov pc, r0

.align 2

Hook6: .word 1+ClearRamEndBattle+SizeRom

@@@ Switch in check + primal

.org 0x42A20

ldr r3, Hook3
mov pc, r3

.align 2
Hook3: .word 1+SwitchInCheckPrimal+SizeRom

@@ Transformation

.org 0x45FFC

ldr r0, Hook2
mov pc, r0

.align 2
Hook2: .word 1+TransformationFromAttackCanceler+SizeRom

@@ Faintpokemon command

.org 0x49BE8

ldr r0, Hook4
mov pc, r0

.align 2
Hook4: .word 1+FaintPokemonCommand1+SizeRom

@@ Faintpokemon command

.org 0x49C08

ldr r1, Myloc5
mov pc, r1

.align 2
Myloc5: .word 1+FaintPokemonCommand2+SizeRom

@@ Handle Multi

.org 0x4DA14

ldrb r0, [r7]
bl 0xA6A30
cmp r0, #0
bne pc+20

@@@ Trigger AI

.org 0x63614		

ldr r0, Hook1
mov pc, r0

.align 2
Hook1: .word 1+TriggeringTheAIMega+SizeRom




/*The idea is to check whether we are in the menu attack or not, we also check the touched pressed
(because that routine runs multiple times by frame). Depending on where we are and what touch are
pressed, we set a specific ram location. Then we'll have to hook at the very start of the attack
canceler to transform the pokemons.
We also need to check whether the pokemon can mega evolve or not, and if it has the good item/attack.
*/

/*
First word :
original turn order

				Flag Trigger
Bank			transformed
				Banks:
				0   1  2  3		
OO OO OO OO 	XX XX XX XX


Flags : 1 = Mega, 2 = Wish, 4 = Transformed.


			
*/



@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@              HERE START THE MAIN ROUTINES


@@@@@@@@@@@@@@@@@@@@@@@@ TRIGGER MEGA EVOLUTION FOR PLAYER


@@@@@@ CHECK MENU PART


.org Myloc

ldr r0, SomeLoc0
mov r1, #0
strb r1, [r0, #4]
ldr r0, b_active_side0
push {r0-r7}
mov r7, r10
mov r6, r9
mov r5, r8
push {r5-r7}
ldr r0, FlagBattle0
ldr r0, [r0]
ldr r2, FlagsMultiBattle0
and r0, r2
cmp r0, #0
beq CheckAllFlag0
mov r2, #1
mov r6, #7
b CheckingFlag0

CheckAllFlag0:
ldr r6, FlagsPlayer0		@ Here we check whether a mega evolution already happened or not, bitfield : 0x4 = First poke, 0x8 = second (use later for multiple battle, not only double)
mov r2, #0

CheckingFlag0:
mov r8, r1
ldr r0, FirstMenu0	@ Used later, to know whether we're in the menu of the first or second pokemon
ldrb r1, [r0]
ldr r4, Pad0			@ This part loads the touch pressed
ldr r5, [r4]		@ Same
lsl r5, r5, #0x18	@ Same
lsr r5, r5, #0x18	@ Same
cmp r1, #2			@ Comparison to know whether we're in first menu or second
bls ScdMenu0
ldr r0, InOutMenu0	@ We're in the first menu, so we're checking wether we're selecting an attack or not
ldrb r0, [r0]
cmp r0, #0x3F		@ 0x4X we're out, 0x3X, we're in
bls InMenuFirst0

ClearTriggerFlagPlayerSide0:
mov r1, #0
b Removing0

Return00:
b Return0

InMenuFirst0:
mov r0, r5			@ Those four lines check if B or select has been pressed
mov r4, #6
and r0, r4
cmp r0, #6
beq CheckStartPressedPlayer1
mov r1, #0
b Removing0

CheckStartPressedPlayer1:
ldr r0, MegaRam0
ldr r0, [r0, #4]
and r0, r6
cmp r0, #0
bne Return00
mov r0, r5
mov r4, #8
and r0, r4
cmp r0, #0
beq ActiveFirstMega0
b Return0

ActiveFirstMega0:
mov r5, #0
b CheckEvoMega0		@ Goto another part that checks mega evo. 

FirstOk0:			@ If the first can mega evolve, the loop back here. Otherwise it returns.
mov r1, #0
b Store0

ScdMenu0:
ldr r0, InOutMenu0
ldrb r0, [r0]
cmp r0, #0x3F		@ Same than before, checking whether we're selecting an attack or not
bls InMenuScd0
b Return0

InMenuScd0:
mov r0, r5			@ Check B or select
mov r4, #6
and r0, r4
cmp r0, #6
beq CheckStartPressedPlayer2
mov r1, #2
b Removing0

CheckStartPressedPlayer2:
ldr r0, MegaRam0
ldr r0, [r0, #4]
and r0, r6
cmp r0, #0
bne Return00
mov r0, r5
mov r4, #8
and r0, r4
cmp r0, #0
beq ActiveSecondMega0	@ Same than before but for the second poke
b Return0

ActiveSecondMega0:
mov r5, #2
b CheckEvoMega0

SecondOk0:
mov r1, #2
b Store0

Removing0:
ldr r0, MegaRam0
add r0, #4
mov r2, #0
add r0, r1
strb r2, [r0]
b Return0

JumpForSound0:
bx r3

Store0:
ldr r0, MegaRam0
add r0, #4
add r0, r1
strb r4, [r0]
mov r0, #SoundWeWantToPlay
ldr r3, PlaySoundRoutine0
bl JumpForSound0

Return0:
pop {r5-r7}
mov r8, r5
mov r9, r6
mov r10, r7
pop {r0-r7}
ldr r5, Back0
mov pc, r5

@@@@@@@ CHECK EVOLUTION TABLE PART

CheckEvoMega0:
push {r5}
ldr r6, PlayerParty0

StartingCheck0:
ldr r0, MemLoc0
ldr r0, [r0]
add r0, #0x60
ldrb r0, [r0]	@ loading the order of our party

HandleDouble0:
mov r1, #2
and r1, r5
cmp r1, #0				
beq First0
mov r1, #0xF
and r0, r1
b Continue0

First0:
mov r1, #0xF0
and r0, r1
lsr r0, r0, #4

Continue0:
mov r2, #0x64
mul r0, r2
add r0, r6
mov r4, r0
ldrh r0, [r0, #0x20]
mov r6, r0
ldr r0, EvoPointer0
ldr r0, [r0]
mov r1, #NumberEvoPerPokemon
mov r2, #8		@ Size of each evolution, doesn't change.
mul r1, r2
mul r1, r6
add r0, r1
mov r2, #MegaEvolutionIndex
mov r7, #0
mov r1, r0
mov r4, r5

Loop0:
cmp r7, #NumberEvoPerPokemon
beq CheckWish0
ldrh r3, [r0]
cmp r3, r2
beq CheckStone0

BuffLoopStone0:
add r0, #8
add r7, #1
b Loop0

CheckWish0:
mov r2, #WishMegaEvolutionIndex
mov r7, #0
mov r0, r1

LoopWish0:
cmp r7, #NumberEvoPerPokemon
beq NoTransformation0
ldrh r3, [r0]
cmp r3, r2
beq CheckAttack0

BuffLoopWish0:
add r0, #8
add r7, #1
b LoopWish0

CheckAttack0:
mov r5, r4
mov r1, #0x58
mul r5, r1
ldr r1, BattData0
add r5, r1
ldrh r6, [r0, #2]
cmp r6, #0
beq BuffLoopWish0
mov r3, r5
add r3, #0xC
ldrh r1, [r3]
cmp r6, r1
beq AmorceWish0
ldrh r1, [r3, #2]
cmp r1, r6
beq AmorceWish0
ldrh r1, [r3, #4]
cmp r1, r6
beq AmorceWish0
ldrh r1, [r3, #6]
cmp r1, r6
beq AmorceWish0
b BuffLoopWish0

AmorceWish0:
mov r4, #2
b Amorce0

CheckStone0:
mov r5, r4
mov r1, #0x58
mul r5, r1
ldr r1, BattData0
add r5, r1
mov r3, r5
add r3, #0x2E
ldrh r3, [r3]
ldrh r6, [r0, #2]
cmp r3, #0
beq BuffLoopStone0
cmp r6, #0
beq BuffLoopStone0
cmp r6, r3
bne BuffLoopStone0

AmorceMega0:
mov r4, #1

Amorce0:
pop {r5}
cmp r5, #0
beq NumberOne0
b SecondOk0

NumberOne0:
b FirstOk0

NoTransformation0:
pop {r5}
b Return0

.align 2
BattData0: .word 0x2024084
FlagsPlayer0: .word 0x70007
FlagBattle0: .word 0x02022FEC
FlagsMultiBattle0: .word 0x408040
PlaySoundRoutine0: .word 0x80A37A4+1
MegaRam0: .word DoubleWordMega
EvoPointer0: .word EvolutionPointerLocation
MemLoc0: .word 0x0202449C
PlayerParty0: .word 0x020244EC
b_active_side0: .word 0x2024064
SomeLoc0: .word 0x2024332
Back0: .word 0x803BE88+1
FirstMenu0: .word 0x200558C
SecondMenu0: .word 0x20055A4
InOutMenu0: .word 0x202018C
Pad0: .word 0x4000130


/*
First word :
original turn order

Order			Flag Trigger 	Banks	
Bank			transformed
				Banks:
				0   1  2  3		
OO OO OO OO 	XX XX XX XX  	UU TT 
*/

/*For that routine I assumed we applied the changes of this post : http://www.pokecommunity.com/showpost.php?p=8963359&postcount=773*/


@@@@@@@@@@@@@@@@@@@ TRIGGER MEGA EVOLUTION FOR AI
.align 2

TriggeringTheAIMega:

Start11:
push {r4-r7, lr}
mov r7, r10
mov r6, r9
mov r5, r8
push {r5-r7}
ldr r0, b_active_side1
ldrb r5, [r0]
mov r8, r5
mov r2, #0					@ That part check whether we're in multi battle or not		
ldr r0, FlagsMultiBattle111
ldr r1, FlagBattle111
ldr r1, [r1]
and r1, r0
cmp r1, #0
beq NotMultiBattle1
mov r2, #1

NotMultiBattle1:
cmp r5, #1
beq FirstPokeAI11
cmp r5, #2
beq AIPartnerPoke111
cmp r5, #3
beq  AISecondPoke111
b GoOn11

FirstPokeAI11:
cmp r2, #1
bne CheckBothFlag1
ldr r1, FlagFirstPokeAI1
b CheckThePoke1

CheckBothFlag1:
ldr r1, FlagsBothPokeAI1

CheckThePoke1:
ldr r0, MegaRam11
ldr r0, [r0, #4]
and r0, r1
cmp r0, #0
beq FlagOkTransform1
mov r1, #0
b EndFlagsTriggerTransform1

FlagOkTransform1:
mov r1, #1
EndFlagsTriggerTransform1:
cmp r1, #1
beq DataPokemonToTrigger1
b GoOn11

AIPartnerPoke111:
ldr r1, FlagsPartnerAI1
b CheckThePoke1

AISecondPoke111:
cmp r2, #1
bne CheckBothFlag1
ldr r1, FlagSecondPokeAI1
b CheckThePoke1

DataPokemonToTrigger1:
mov r6, #0x58
mul r6, r5
ldr r0, BattleData11
add r0, r6
add r0, #0x28
ldrh r0, [r0]
cmp r0, #0		@ The AI check whether its pokemon is fainted or not (because of double battle)
bne Fine11
b GoOn11

Fine11:
cmp r5, #2
beq CheckAIPartner1
ldr r6, AIParty11
mov r1, #3
b CheckingTheParty1

CheckAIPartner1:
ldr r6, PlayerParty11
mov r1, #0

CheckingTheParty1:
ldr r0, MemLoc11
ldr r0, [r0]
add r0, #0x60
ldrb r0, [r0, r1]	@ Loading the order of the party

HandleDble11:
mov r1, r8
mov r2, #2
and r1, r2
cmp r1, #0			
beq First111
mov r1, #0xF		@ See my explanation in the Faintpokemon hook
and r0, r1
b Continue111

First111:
mov r1, #0xF0
and r0, r1
lsr r0, r0, #4

Continue111:
mov r2, #0x64
mul r0, r2
add r0, r6			@ r6 holds the party to load
mov r4, r0
ldrh r0, [r4, #0x20]	@Now we're loading the pokemon
mov r6, r0
ldr r0, EvoPointer11
ldr r0, [r0]
mov r1, #NumberEvoPerPokemon
mov r2, #8
mul r1, r2
mul r1, r6
add r0, r1
mov r2, #MegaEvolutionIndex
mov r7, #0
mov r1, r0

Looop11:
cmp r7, #NumberEvoPerPokemon
beq CheckWish111
ldrh r3, [r0]
cmp r3, r2
beq CheckOrb111

BuffLoopOrb111:
add r0, #8
add r7, #1
b Looop11

CheckWish111:
mov r2, #WishMegaEvolutionIndex
mov r7, #0
mov r0, r1

LoopWish111:
cmp r7, #NumberEvoPerPokemon
beq GoOn11
ldrh r3, [r0]
cmp r3, r2
beq CheckAttack111

BuffLoopWish111:
add r0, #8
add r7, #1
b LoopWish111

CheckAttack111:
mov r1, #0x58
mul r5, r1		@ r5 still holds the bank
ldr r1, BattleData11
add r5, r1
ldrh r6, [r0, #2]
cmp r6, #0
beq BuffLoopWish111
mov r3, r5
add r3, #0xC
ldrh r1, [r3]
cmp r6, r1
beq Amorce111Wish
ldrh r1, [r3, #2]
cmp r1, r6
beq Amorce111Wish
ldrh r1, [r3, #4]
cmp r1, r6
beq Amorce111Wish
ldrh r1, [r3, #6]
cmp r1, r6
beq Amorce111Wish
b BuffLoopWish111

Amorce111Wish:
mov r7, #2
b Amorce111

CheckOrb111:
mov r5, r4
mov r1, #0x58
mul r5, r1
ldr r1, BattleData11
add r5, r1
mov r3, r5
add r3, #0x2E
ldrh r3, [r3]
ldrh r6, [r0, #2]
cmp r3, #0
beq BuffLoopOrb111
cmp r6, #0
beq BuffLoopOrb111
cmp r6, r3
bne BuffLoopOrb111

MegaAmorce1:
mov r7, #1

Amorce111:
ldr r2, MegaRam11
add r2, #4
mov r3, r8
strb r7, [r2, r3]	@ We're storing the type of mega for the wanted bank in the Free Ram we use
mov r0, #0
b MegaActivated1

GoOn11:
MegaActivated1:
ldr r3, Back111
mov pc, r3

.align 2
b_active_side1: .word 0x02024064
FlagsPartnerAI1: .word 0x70000
FlagFirstPokeAI1: .word 0x700
FlagSecondPokeAI1: .word 0x7000000
FlagsBothPokeAI1: .word 0x7000700
FlagBattle111: .word 0x02022FEC
FlagsMultiBattle111: .word 0x408040
Back111: .word 0x806361E+1
NPokeField11: .word 0x202406C
MegaRam11: .word DoubleWordMega
EvoPointer11: .word EvolutionPointerLocation
BattleData11: .word 0x02024084
MemLoc11: .word 0x0202449C
PlayerParty11: .word 0x020244EC
AIParty11: .word 0x02024744

/*
First word :
original turn order

Order			Flag Trigger 	Banks	
Bank			transformed
				Banks:
				0   1  2  3		
OO OO OO OO 	XX XX XX XX  	UU TT 
*/

/*For that routine I assumed we applied the changes of this post : http://www.pokecommunity.com/showpost.php?p=8963359&postcount=773*/





@@@@@@@@@@@@@@@@@@@@@@@@ TRANSFORMATION PART

.align 2

TransformationFromAttackCanceler:

push {r4-r7, lr}
mov r7, r9
mov r6, r8
push {r6,r7}
sub sp, sp, #4
ldr r0, MoveOrderTurn2

StartToCheck2:
ldr r2, RamMega2
add r2, #4
mov r3, #0

LoopCheck2:			@ That loops loads the order of the turn, check one by one if the poke can mega evolve
cmp r3, #4
beq ByeBabe2

LoopCheckContinue12:
ldrb r1, [r0, r3]
ldrb r4, [r2, r1]
mov r5, #3
and r5, r4
cmp r5, #0
beq BuffLoop2
mov r8, r1			@ r8 holds the bank
mov r7, r5			@ r7 the type of mega
b LoadTransformation2

BuffLoop2:
add r3, #1
b LoopCheck2

LoadTransformation2:
mov r3, #0x58
mul r3, r1
ldr r5, BattleData2
add r5, r3
add r5, #0x28
ldrh r5, [r5]
cmp r5, #0
bne ItsOk2
mov r4, #0
strb r4, [r2, r1]
ByeBabe2:
b Return22

ItsOk2:
mov r4, #4
strb r4, [r2, r1]

Next2:
ldr r4, MemLoc2		@ Loads order party
ldr r4, [r4]
add r4, #0x60
mov r3, #1
and r3, r1		@ AI or player
cmp r3, #0
bne AIPicking2
ldrb r4, [r4]
ldr r5, PlayerParty2 
b NextAgain2

AIPicking2:
ldrb r4, [r4, #3]
ldr r5, AIParty2

NextAgain2:
mov r3, #2
and r3, r1		@ First or second poke
cmp r3, #0
beq First2
mov r3, #0xF
and r4, r3
b Continue22

First2:
mov r3, #0xF0
and r4, r3
lsr r4, r4, #4

Continue22:
mov r3, #0x64
mul r4, r3
add r4, r5
ldrh r3, [r4, #0x20]	@ Now we're loading the pokemon we want to check
ldr r0, EvoPointer2
ldr r0, [r0]
mov r1, #NumberEvoPerPokemon
mov r2, #8
mul r1, r2
mul r1, r3
add r0, r1		@ Loading its evolutions location
mov r5, #0
cmp r7, #1
bne CheckWish2
mov r2, #MegaEvolutionIndex

Loop2:	
cmp r5, #NumberEvoPerPokemon	@ We compare to number max of evolution, but it's not even necessary since we're sure we can mega evolve.
beq Returning2
ldrh r3, [r0]
cmp r3, r2
beq CheckStone2

BuffLoopStone2:
add r0, #8
add r5, #1
b Loop2

Returning2:
b Return22

CheckWish2:
mov r2, #WishMegaEvolutionIndex
LoopWish2:
cmp r5, #NumberEvoPerPokemon
beq Returning2
ldrh r3, [r0]
cmp r3, r2
beq CheckAttack2

BuffLoopWish2:
add r0, #8
add r5, #1
b LoopWish2

CheckAttack2:
mov r5, r8
mov r1, #0x58
mul r5, r1
ldr r1, BattleData2
add r5, r1
ldrh r6, [r0, #2]
cmp r6, #0
beq BuffLoopWish2
mov r3, r5
add r3, #0xC
ldrh r1, [r3]
cmp r6, r1
beq ChangeStats2
ldrh r1, [r3, #2]
cmp r1, r6
beq ChangeStats2
ldrh r1, [r3, #4]
cmp r1, r6
beq ChangeStats2
ldrh r1, [r3, #6]
cmp r1, r6
beq ChangeStats2
b BuffLoopWish2

CheckStone2:
mov r5, r4
mov r1, #0x58
mul r5, r1
ldr r1, BattleData2
add r5, r1
mov r3, r5
add r3, #0x2E
ldrh r3, [r3]
ldrh r6, [r0, #2]
cmp r3, #0
beq BuffLoopStone2
cmp r6, #0
beq BuffLoopStone2
cmp r6, r3
bne BuffLoopStone2

ChangeStats2:
ldrh r6, [r0, #4]
strh r6, [r5]
strh r6, [r4, #0x20]
mov r0, r4
ldr r3, calcstats2
bl Jump2
mov r0, r4
add r0, #0x5A
ldrh r3, [r0]
strh r3, [r5, #2]	@ This part updates attack.
ldrh r3, [r0, #2]	@ 
strh r3, [r5, #4]	@ defense
ldrh r3, [r0, #0x4]	@ 
strh r3, [r5, #0x6]	@ speed
ldrh r3, [r0, #0x6]	@ 
strh r3, [r5, #0x8]	@ att special
ldrh r3, [r0, #0x8]	@ 
strh r3, [r5, #0xA]	@ def special. Ends here.

UpdateTypeAndAbility2:
add r5, #0x20			@ I assumed Mega evolutions can only have one ability
ldr r2, PokePointer2
ldr r2, [r2]
mov r3, #28
mul r3, r6
add r3, r2
add r3, #6
ldrb r2, [r3]
strb r2, [r5, #1]
ldrb r2, [r3, #1]
strb r2, [r5, #2]
add r3, #16
ldrb r2, [r3]
strb r2, [r5]		@ Stats completly updated here.
ldr r3, ScriptPusher2	@ Keeps the current BS safe.
bl Jump2
ldr r1, CurBS2
mov r0, r8
mov r2, #1
and r2, r0
cmp r2, #0
bne AITransformation2
cmp r7, #1				@ r7 = 1 for mega, 2 for wish
bne WishPlayer2
cmp r0, #2
beq AIPartnerMega2
ldr r0, MegaBS2
b Transformation2

AIPartnerMega2:
ldr r0, MegaBS22
b Transformation2

WishPlayer2:
cmp r0, #2
beq AIPartnerWish2
ldr r0, MegaWishBS2
b Transformation2

AIPartnerWish2:
ldr r0, MegaWishBS22
b Transformation2

AITransformation2:
cmp r7, #1
bne WishAI2
cmp r0, #3
beq AITransformMega2
ldr r0, MegaBSAI2
b Transformation2

AITransformMega2:
ldr r0, MegaBSAI22
b Transformation2

AITransformWish2:
ldr r0, MegaWishAIBS22
b Transformation2

WishAI2:
cmp r0, #3
beq AITransformWish2
ldr r0, MegaWishAIBS2

Transformation2:
str r0, [r1]
DealWithTargetOrder2:
ldr r0, RamMega2
ldr r2, Bank2
ldrb r3, [r0]
mov r4, #0x10
and r4, r3
cmp r4, #0x10
beq DirectEnd2
ldrb r3, [r2]
mov r4, #0x10
orr r3, r4
strb r3, [r0]

DirectEnd2:
mov r3, r8
strb r3, [r2]
ldr r0, EndCanceler2
mov pc, r0

Return22:
ldr r0, RamMega2		@ Here we keep the banks safe.
ldr r2, Bank2	@ In our RAM location.
ldrb r3, [r0]
mov r4, #0x10
and r4, r3
cmp r4, #0
beq RealBack2
bic r3, r4
strb r3, [r2]
strb r3, [r0]

RealBack2:
ldr r0, Back2
mov pc, r0

Jump2:
mov pc, r3

.align 2
MoveOrderTurn2: .word 0x0202407E
EndCanceler2: .word 0x8046398+1
MegaBS2: .word MegaBattleScriptForPlayer
MegaBS22: .word MegaBattleScriptForPlayer2
MegaBSAI2: .word MegaBattleScriptForAI
MegaBSAI22: .word MegaBattleScriptForAI2
MegaWishBS2: .word WishBattleScriptForPlayer
MegaWishBS22: .word WishBattleScriptForPlayer2
MegaWishAIBS2: .word WishBattleScriptForAI
MegaWishAIBS22: .word WishBattleScriptForAI2
ScriptPusher2: .word 0x0803FAFC+1
CurBS2: .word 0x02024214
PokePointer2: .word PokemonDataPointer
calcstats2: .word 0x08068D0C+1
Back2: .word 0x8046006+1
RamMega2: .word DoubleWordMega
BankTarget2: .word 0x0202420C
Bank2: .word 0x0202420B
EvoPointer2: .word EvolutionPointerLocation
BattleData2: .word 0x02024084
MemLoc2: .word 0x0202449C
PlayerParty2: .word 0x020244EC
AIParty2: .word 0x02024744



@@@@@@@@@@@@@@@@@@@@@@@@ SWITCH IN + PRIMAL
.align 2

SwitchInCheckPrimal:
push {r0-r7}
ldr r0, FreeAegi3
mov r1, #0
strb r1, [r0]
mov r0, r10
mov r5, r10
mov r1, #1
and r1, r0
cmp r1, #0
beq Player3

AI3:
ldr r6, AIParty3
mov r1, #3
b CheckAegi

Player3:
ldr r6, PlayerParty3
mov r1, #0

CheckAegi:
ldr r0, MemLoc3
ldr r0, [r0]
add r0, #0x60
ldrb r0, [r0, r1]	@ loading the order of our party

HandleDouble3:
mov r1, #2
and r1, r5
cmp r1, #0				
beq First13
mov r1, #0xF
and r0, r1
b Continue223

First13:
mov r1, #0xF0
and r0, r1
lsr r0, r0, #4

Continue223:
mov r1, r6
mov r2, #0x64
mul r0, r2
add r0, r1
mov r4, r0
mov r1, #0xB
ldr r3, getattr3
bl Jump3
mov r6, r0
ldr r0, EvoPointer3
ldr r0, [r0]
mov r1, #NumberEvoPerPokemon
mov r2, #8
mul r1, r2
mul r1, r6
add r0, r1
mov r2, #PrimalReversionIndex
mov r7, #0

Loop3:
cmp r7, #NumberEvoPerPokemon
beq NoPrimal3
ldrh r3, [r0]
cmp r3, r2
beq CheckOrb3
add r0, #8
add r7, #1
b Loop3

CheckOrb3:
ldrh r7, [r0, #4]

bl BData
mov r3, r5
add r3, #0x2E
ldrh r3, [r3]
ldrh r2, [r0, #2]
cmp r3, #0
beq NoPrimal3
cmp r2, #0
beq NoPrimal3
cmp r2, r3
bne NoPrimal3
strh r7, [r5]
ldr r2, var8004
strh r7, [r2]

ChangePokeParty3:
mov r0, r4
mov r1, #0xB
ldr r3, setattr
bl Jump3
mov r0, r4
ldr r3, calcstats3
bl Jump3

ChangeBattleStats3:
mov r1, #0x54
mov r0, r4
ldr r3, getattr3
bl Jump3
strh r0, [r5, #2]
mov r1, #0x55
mov r0, r4
ldr r3, getattr3
bl Jump3
strh r0, [r5, #4]
mov r1, #0x56
mov r0, r4
ldr r3, getattr3
bl Jump3
strh r0, [r5, #6]
mov r1, #0x57
mov r0, r4
ldr r3, getattr3
bl Jump3
strh r0, [r5, #8]
mov r1, #0x58
mov r0, r4
ldr r3, getattr3
bl Jump3
strh r0, [r5, #0xA]

LoadAbilityBit3:
mov r1, #0x2E
mov r0, r4
ldr r3, getattr3
bl Jump3
add r5, #0x20
lsl r0, #0x1F
lsr r0, #0x1F

LoadAbilityTypeAndStore3:
ldr r2, PokePointer3
ldr r2, [r2]
mov r3, #28
mul r3, r7
add r3, r2
add r3, #6
ldrb r2, [r3]
strb r2, [r5, #1]
ldrb r2, [r3, #1]
strb r2, [r5, #2]
add r3, #16
ldrb r2, [r3]
strb r2, [r5]
ldr r0, Bank3
sub r0, #1
strb r2, [r0]
ldr r0, Bank3
mov r1, r10
strb r1, [r0]
pop {r0-r7}
ldr r0, PrimalBS
ldr r1, ExecScript
bl Jumpr1
b BackAfterScript

BData:
push {lr}
mov r1, #0x58
mul r5, r1
ldr r1, BattleData3
add r5, r1
pop {pc}

Jump3:
mov pc, r3

Jumpr1:
mov pc, r1

NoPrimal3:
pop {r0-r7}
StartCheckingSwitchInAbilities:
ldrb r3, [r5]
cmp r1, r3
blo CheckIfSwitchInAbility
mov r0, r10
strb r0, [r2]
 
CheckIfSwitchInAbility:
ldrb r0, [r7]
cmp r0, #0x2
bne CheckCloudNine

Drizzle:
ldr r2, FreeRam3
ldrb r1, [r2]
cmp r1, #0x00
bne NoSwitchInAbility
ldr r1, Drizzleback
bx r1

CheckCloudNine:
cmp r0, #0x0D
bne CheckIntimidate
b AirLockCloudNine

CheckIntimidate:
cmp r0, #0x16
bne CheckTrace

Intimidate:
ldr r1, Intimidateback
bx r1

CheckTrace:
cmp r0, #0x24
bne CheckSandStream

Trace:
ldr r1, Traceback
bx r1

CheckSandStream:
cmp r0, #0x2D
bne CheckForeCast

SandStream:
ldr r2, FreeRam3
ldrb r1, [r2]
cmp r1, #0x00
bne NoSwitchInAbility
ldr r1, Sandstreamback
bx r1

CheckForeCast:
cmp r0, #0x3B
bne CheckDrought

ForeCast:
ldr r1, Forecastback
bx r1

CheckDrought:
cmp r0, #0x46
bne CheckAirLock

Drought:
ldr r2, FreeRam3
ldrb r1, [r2]
cmp r1, #0x00
bne NoSwitchInAbility
ldr r1, Droughtback
bx r1

CheckAirLock:
cmp r0, #0x4D
bne CheckDelta

AirLockCloudNine:
ldr r2, FreeRam3
ldrb r1, [r2]
cmp r1, #0x00
bne NoSwitchInAbility
ldr r1, Airlockback
bx r1

CheckDelta:
cmp r0, #DeltaStreamAbility
bne CheckDeso

UpdateStrongWind:
ldr r0, CurrentWeather
mov r1, #0
strb r1, [r0]
ldr r0, FreeRam3
ldrb r1, [r0]
cmp r1, #DeltaStreamAbility
beq NoSwitchInAbility
mov r1, #DeltaStreamAbility
strb r1, [r0]
ldr r0, BattleScript3
bl ExecuteScript
BackAfterScript:
ldr r1, BackingAfterScript
bx r1

CheckDeso:
cmp r0, #DesolateLandAbility
bne CheckPrimo

UpdateDesolate:
ldr r2, FreeRam3
mov r0, #DesolateLandAbility
strb r0, [r2]
ldr r2, Droughtback
bx r2

CheckPrimo:
cmp r0, #PrimordialSeaAbility
bne CheckImposter

UpdatePrimordial:
ldr r2, FreeRam3
mov r0, #PrimordialSeaAbility
strb r0, [r2]
ldr r2, Drizzleback
bx r2

CheckImposter:
cmp r0, #ImposterAbility
beq Imposter

NoSwitchInAbility:
ldr r1, ReturnNoSwitch
bx r1

ExecuteScript:
ldr r1, Executescriptback
bx r1

Imposter:
mov r0, r10
ldr r1, Bank3
b CheckFlagAndStore

Store3:
ldr r0, BankTarget3
strb r4, [r0]
pop {r0-r4}
mov r0, r10
strb r0, [r1]

Next3:
ldr r0, BattleScriptImposter
bl ExecuteScript
b BackAfterScript

PopNoSwitch:
pop {r0-r4}
b NoSwitchInAbility

CheckFlagAndStore:
push {r0, r1}
ldr r0, NPokeOnTheField3
ldrb r1, [r0]
cmp r1, #2
beq Simple
pop {r0, r1}
cmp r0, #0
beq ThreeTarget
cmp r0, #2
beq OneTarget
cmp r0, #1
beq TwoTarget
mov r0, #0
b CheckTarget

DoubleBattle3:
ThreeTarget:
mov r0, #3
b CheckTarget

TwoTarget:
mov r0, #2
b CheckTarget

OneTarget:
mov r0, #1
b CheckTarget

Simple:
pop {r0, r1}
cmp r0, #0
beq OneTarget
mov r0, #0

CheckTarget:
push {r0-r4}
ldr r1, BattleData3
mov r2, #0x58
mov r4, r0
mul r0, r2
add r0, r1
add r0, #0x28
ldrh r3, [r0]
cmp r3, #0
beq PopNoSwitch
add r0, #0x28
ldr r3, [r0]
ldr r2, TransformSubStatut
and r2, r3
cmp r2, #0
bne PopNoSwitch
ldr r2, InvulnStatut
mov r0, r4
ldr r3, CheckInvulnerableStatut
lsl r0, #2
add r0, r3
ldr r3, [r0]
and r3, r2
cmp r3, #0
bne PopNoSwitch
b Store3

.align 2
Drizzleback: .word 0x8042B78+1
Intimidateback: .word 0x08042C08+1
Traceback: .word 0x08042C68+1
Sandstreamback: .word 0x08042BA8+1
Forecastback: .word 0x08042C3C+1
Droughtback: .word 0x08042BD8+1
Airlockback: .word 0x08042CA4+1
Executescriptback: .word 0x08042BEE+1
FreeAegi3: .word AegiFreeRam
BattleScriptImposter: .word BattleScriptForImposter
CheckInvulnerableStatut: .word 0x020242AC
PrimalBS: .word PrimalBattleScript
BattleScript3: .word BattleScriptDeltaStream
CurrentMove: .word 0x020241EA
CurrentWeather: .word 0x020243CC
FreeRam3: .word WeathersMegaPrimal
BankTarget3: .word 0x0202420C
Bank3: .word 0x0202420B
InvulnStatut: .word 0x400C0
TransformSubStatut: .word 0x1200000
NPokeOnTheField3: .word 0x0202406C
ReturnNoSwitch: .word 0x08042A7E+1
BackingAfterScript: .word 0x08042A72+1
ExecScript: .word 0x08042BEE+1
ScriptPusher3: .word 0x0803FAFC+1
CurBS3: .word 0x02024214
EvoPointer3: .word EvolutionPointerLocation
PokePointer3: .word PokemonDataPointer
BattleData3: .word 0x02024084
calcstats3: .word 0x08068D0C+1
setattr: .word 0x0806ACAC+1
var8004: .word 0x020275D8 + (0x8004*2)
MemLoc3: .word 0x0202449C
getattr3: .word 0x0806A518+1
PlayerParty3: .word 0x020244EC
AIParty3: .word 0x02024744



@@@@@@@@@@@@@@@@@@@@@@ FAINTPOKEMON (USER) HIJACK
.align 2

FaintPokemonCommand1:

Start4:
ldr r0, BankUser4
ldrb r0, [r0]
strb r0, [r1]
ldr r4, FreeRam4
ldrb r4, [r4]
cmp r4, #0
beq Return4
push {r0-r3}
ldr r1, BattleData4
mov r2, #0x58
mul r0, r2
add r0, r1
add r0, #0x20
ldrb r3, [r0]
ldrb r2, [r0, #8]
cmp r2, #0
bne PopAndReturn4
cmp r3, r4
beq Clear4
pop {r0-r3}
b Return4

Clear4:
pop {r0-r3}
ldr r4, FreeRam4
mov r3, #0
strb r3, [r4]
ldr r4, CurrentWeather4
strb r3, [r4]
bl StartingCheckMegaAegi4
ldr r0, BankTarget4
ldrb r0, [r0]
mov r9, r0
ldr r4, NormalWeatherScript4
ldr r0, SomethingBack4
bx r0

PopAndReturn4:
pop {r0-r3}

Return4:
bl StartingCheckMegaAegi4
ldr r0, BankTarget4
ldrb r0, [r0]
mov r9, r0
ldr r0, Back4
bx r0

StartingCheckMegaAegi4:
push {r0-r7,lr}
mov r5, r8
push {r5}
ldr r0, BankUser4
ldrb r2, [r0]
mov r1, #0x58
mul r2, r1
ldr r1, BattleData4
add r2, r1
add r2, #0x28
ldrh r2, [r2]
cmp r2, #0
bne ComeBack4
ldrb r0, [r0]
ldr r1, MemLoc4
ldr r1, [r1]
add r1, #0x60
mov r3, #0
mov r2, #1
and r2, r0
cmp r2, #0
beq Straight4
mov r3, #3

Straight4:
ldrb r5, [r1, r3]
mov r2, #2
and r2, r0
cmp r2, #0
beq First4
mov r1, #0xF
and r5, r1
b Continue14

First4:
mov r1, #0xF0
and r5, r1
lsr r5, r5, #4

Continue14:
mov r2, #0x64
mul r5, r2
cmp r3, #0
beq PlayerPartyLoading4

AIPartyLoading4:
ldr r4, AIParty4
b Continue4

PlayerPartyLoading4:
ldr r4, PlayerParty4

Continue4:
add r5, r4
ldrh r0, [r5, #0x20]
mov r7, r5
ldr r6, EvoPointer4
ldr r6, [r6]

Loop4:
ldr r1, AegiBlade4
cmp r0, r1
beq RevertAegi4

PrimalCheck4:
mov r3, #NumberEvoPerPokemon
mov r2, #8
mul r3, r2
mul r0, r3
add r0, r6
mov r2, #PrimalReversionIndex
mov r1, #0

Loop24:
cmp r1, #NumberEvoPerPokemon
beq ComeBack4
ldrh r3, [r0]
cmp r3, r2
bne CheckWish4

ContinueChecking4:
ldrh r3, [r0, #2]
cmp r3, #0
bne AddLoop24
ldrh r1, [r0, #4]
b Revert4

CheckWish4:
cmp r3, #WishMegaEvolutionIndex
bne CheckMega4
b ContinueChecking4

CheckMega4:
cmp r3, #MegaEvolutionIndex
bne AddLoop24
b ContinueChecking4

AddLoop24:
add r1, #1
b Loop24

RevertAegi4:
ldr r1, AegiShield4

Revert4:
strh r1, [r5, #0x20]
mov r0, r5
ldr r3, calcstats4
bl Jump4

ComeBack4:
pop {r5}
mov r8, r5
pop {r0-r7, pc}

Jump4:
mov pc, r3

.align 2
SomethingBack4: .word 0x08049C18+1
AegiBlade4: .word AegislashBlade
AegiShield4: .word AegislashShield
EvoPointer4: .word EvolutionPointerLocation
MemLoc4: .word 0x0202449C
CurrentWeather4: .word 0x020243CC
FreeRam4: .word WeathersMegaPrimal
BankTarget4: .word 0x0202420C
BankUser4: .word 0x0202420B
BattleData4: .word 0x02024084
MoxieandClear4: .word BattleScriptMoxieClear
Back4: .word 0x08049BF4+1
PokePointer4: .word PokemonDataPointer
calcstats4: .word 0x08068D0C+1
AIParty4: .word 0x02024744
PlayerParty4: .word 0x020244EC
NormalWeatherScript4: .word BattleScriptClearWeather


/*For that routine I assumed we applied the changes of this post : http://www.pokecommunity.com/showpost.php?p=8963359&postcount=773*/

@@@@@@@@@@@@@@@@@@@@@@@@ FAINTPOKEMON (TARGET) HIJACK
.align 2


FaintPokemonCommand2:

Start5:
ldr r1, b_active_side5
ldr r0, BankTarget5
ldrb r0, [r0]

StartCheckWeather5:
strb r0, [r1]
ldr r4, FreeRam5		@ We're checking if a mega or primal weather is active
ldrb r4, [r4]
cmp r4, #0
beq Return5
push {r0-r3}
ldr r1, BattleData5	@ We're loading the data of the pokemon
mov r2, #0x58
mul r0, r2
add r0, r1
add r0, #0x20
ldrb r3, [r0]
ldrb r2, [r0, #8]
cmp r2, #0			@ We're checking if still has HP
bne PopAndReturn5	@ If so, return
cmp r3, r4			@ Otherwise, we check if it is responsible of the actual mega or primal weather. If so, we remove it and play
beq Clear5			@ the apprioriated BS
pop {r0-r3}
b Return5

Clear5:
pop {r0-r3}
ldr r4, FreeRam5
mov r3, #0
strb r3, [r4]			@ So we clear the ram
ldr r4, CurrentWeather5
strb r3, [r4]			@ And the weather
bl StartingCheckMegaAegi5	@ Now we check if the poke has to be reverted, i.e is it a mega/primal or Aegi
ldr r0, BankUser5			@ Could be use for any other revert btw, if they exist
ldrb r0, [r0]
mov r9, r0
ldr r4, MoxieandClear5
ldr r0, ReturnSomething5
bx r0

PopAndReturn5:
pop {r0-r3}

Return5:
bl StartingCheckMegaAegi5	@ Here we also have to check if the poke need to be reverted
ldr r0, BankUser5
ldrb r0, [r0]
mov r9, r0
ldr r0, Back5
bx r0

StartingCheckMegaAegi5:
push {r0-r7,lr}
mov r5, r8
push {r5}
ldr r0, BankTarget5
ldrb r2, [r0]
mov r1, #0x58
mul r2, r1
ldr r1, BattleData5
add r2, r1
add r2, #0x28
ldrh r2, [r2]
cmp r2, #0		@ Check if the pokemon is indeed fainted
bne ComeBack5
ldrb r0, [r0]
ldr r1, MemLoc5
ldr r1, [r1]
add r1, #0x60	@ The pointer in MemLoc + 0x60 contains the order of your party and the party of the ennemy
mov r3, #0
mov r2, #1
and r2, r0		@ We "and" the Bank target with 1 to know whether we have to load the ennemy party or the player party
cmp r2, #0
beq Straight5
mov r3, #3		@ Used to load ennemy party
				@ Otherwise r3 stays 0
Straight5:		@ Our party takes the 3 first bytes, no matter how many poke you have, the 3 following are the order of the ennemy party
ldrb r5, [r1, r3]	@ So we load it accordingly thanks to r3
mov r2, #2
and r2, r0		@ Now we're "anding" again, but with 2, to know whether the pokemon is the first or the second (in double)
cmp r2, #0		@ Don't worry it works also in simple
beq First5
mov r1, #0xF	@ This party needs an explanation I think.
and r5, r1		@ So, assuming you have six pokemon, and you didn't switch/faint, the order in the RAM will be "01 23 45", 0 = first poke, 1 = second, and so on
b Continue15		@ It is the same for the ennemy, but 3 bytes further.
				@ So, if it is the first poke, by "anding" wih 0xF0, we save the "index" or whatever you call that of the first poke. by
First5:			@ "anding" with 0xF, we save the second poke. Then we have to divide by 16 (because hex) if it was the first poke (lsr r5, r5, #4)
mov r1, #0xF0	@ because if the order of your party was 23 45 01 for instance, anding with 0xF0 would give us 0x20, with the divide we have 0x2.
and r5, r1
lsr r5, r5, #4

Continue15:
mov r2, #0x64	@ Size of the data of a pokemon in the party (100 bytes or 0x64 bytes)
mul r5, r2		@ IndexOfOurPoke x Size of the data
cmp r3, #0		@ We're using r3 again to know whether we have to load the party of the player or the ennemy
beq PlayerPartyLoading5

AIPartyLoading5:
ldr r4, AIParty5
b Continue5

PlayerPartyLoading5:
ldr r4, PlayerParty5

Continue5:
add r5, r4
ldrh r0, [r5, #0x20]	@ Assuming you correctly applied the change to prevent the decrypt poke, the index number of your pokemon
mov r7, r5				@ is the halfword at the 0x20th byte in its battle data.
ldr r6, EvoPointer5
ldr r6, [r6]			@ Here I load the evolutions of the poke from a pointer, that way if you repointed you tables/expanded your evolution
						@ the routine still works.
Loop5:				@ Here the loop starts. It checks if your pokemon is aegi/primal/wish or mega evo, if so => Revert.
ldr r1, AegiBlade5
cmp r0, r1
beq RevertAegi5

PrimalCheck5:
mov r3, #NumberEvoPerPokemon
mov r2, #8
mul r3, r2
mul r0, r3
add r0, r6
mov r2, #PrimalReversionIndex
mov r1, #0

Loop25:
cmp r1, #NumberEvoPerPokemon
beq ComeBack5
ldrh r3, [r0]
cmp r3, r2
bne CheckWish5

ContinueChecking5:
ldrh r3, [r0, #2]
cmp r3, #0
bne AddLoop25
ldrh r1, [r0, #4]
b Revert5

CheckWish5:
cmp r3, #WishMegaEvolutionIndex
bne CheckMega5
b ContinueChecking5

CheckMega5:
cmp r3, #MegaEvolutionIndex
bne AddLoop25
b ContinueChecking5

AddLoop25:
add r1, #1
b Loop25

RevertAegi5:
ldr r1, AegiShield5

Revert5:
strh r1, [r5, #0x20]
mov r0, r5
ldr r3, calcstats5
bl Jump5

ComeBack5:
pop {r5}
mov r8, r5
pop {r0-r7, pc}

Jump5:
mov pc, r3

.align 2
b_active_side5: .word 0x02024064
ReturnSomething5: .word 0x08049C18+1
AegiBlade5: .word AegislashBlade
AegiShield5: .word AegislashShield
EvoPointer5: .word EvolutionPointerLocation
MemLoc5: .word 0x0202449C
CurrentWeather5: .word 0x020243CC
FreeRam5: .word WeathersMegaPrimal
BankTarget5: .word 0x0202420C
BankUser5: .word 0x0202420B
BattleData5: .word 0x02024084
MoxieandClear5: .word BattleScriptMoxieClear
Back5: .word 0x08049C16+1
PokePointer5: .word PokemonDataPointer
calcstats5: .word 0x08068D0C+1
AIParty5: .word 0x02024744
PlayerParty5: .word 0x020244EC


@@@@@@@@@@@@@@@@@@@@ CLEAR EVERYTHING END OF BATTLE
.align 2

ClearRamEndBattle:

CheckAegi6:
push {r0-r7}
mov r5, #0
ldr r7, PlayerParty6
ldr r6, EvoPointer6
ldr r6, [r6]

Loop6:
cmp r5, #6
beq BackEnd6
mov r4, r5
mov r2, #0x64
mul r4, r2
add r4, r7
mov r1, #0xB
mov r0, r4
ldr r3, getattr6
bl Jump6
cmp r0, #00
beq Loopplusone6
ldr r1, ABlade6		@ Check Aegi Blade
cmp r0, r1
beq RevertAegi6

PrimalCheck6:
mov r3, #NumberEvoPerPokemon
mov r2, #8
mul r3, r2
mul r0, r3
add r0, r6
mov r2, #PrimalReversionIndex
mov r1, #0

Loop26:
cmp r1, #NumberEvoPerPokemon
beq AddLoop6
ldrh r3, [r0]
cmp r3, r2
bne CheckWish6

ContinueChecking6:
ldrh r3, [r0, #2]
cmp r3, #0
bne AddLoop26
ldrh r1, [r0, #4]
b Revert6

CheckWish6:
cmp r3, #WishMegaEvolutionIndex
bne CheckMega6
b ContinueChecking6

CheckMega6:
cmp r3, #MegaEvolutionIndex
bne AddLoop26
b ContinueChecking6

AddLoop26:
add r1, #1
b Loop26

AddLoop6:
add r5, #1
b Loop6

Loopplusone6:
add r5, #1
b Loop6

BackEnd6:
pop {r0-r7}
b ClearRam6

RevertAegi6:
ldr r1, AShield6
b Revert6

Revert6:
add r5, #1
ldr r2, var80046
strh r1, [r2]
mov r0, r4
mov r1, #0xB
ldr r3, setattr6
bl Jump6
mov r0, r4
ldr r3, calcstats6
bl Jump6
b Loop6

ClearRam6:
ldr r0, Someotherstuff6
str r0, [r1]
ldr r0, FreeRam6
mov r1, #0
strb r1, [r0]
ldr r0, FreeRam26
strb r1, [r0]
ldr r0, MegaRam6
str r1, [r0]
str r1, [r0, #4]
strh r1, [r0, #8]
ldr r1, LocSomething6
ldr r0, LocStuff6
str r0, [r1]
ldr r0, Locstuffthing6
mov pc, r0

Jump6:
mov pc, r3

.align 2
Someotherstuff6: .word 0x803DE41
LocSomething6: .word 0x30061E8
LocStuff6: .word 0x8038421
Locstuffthing6: .word 0x803DE2A
MegaRam6: .word DoubleWordMega
FreeRam6: .word WeathersMegaPrimal
FreeRam26: .word AegiFreeRam
ABlade6: .word AegislashBlade
AShield6: .word AegislashShield
MemLoc6: .word 0x0202449C
getattr6: .word 0x0806A518+1
Bank6: .word 0x0202420B
PlayerParty6: .word 0x020244EC
calcstats6: .word 0x08068D0C+1
setattr6: .word 0x0806ACAC+1
var80046: .word 0x020275D8 + (0x8004*2)
EvoPointer6: .word EvolutionPointerLocation





@@@@@@@@@@@@ Battle script player/partner
.align 2

MegaBSPlayer:   @ MegaBattleScriptForPlayer

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringUU
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLL
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return

.align 2

BattleScriptWishPlayer: @ WishBattleScriptForPlayer

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringZZ
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLL
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return

.align 2

MegaBSPlayer2: @ MegaBattleScriptForPlayer2

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringUU2
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLL
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return

.align 2

BattleScriptWishPlayer2:	@ WishBattleScriptForPlayer2

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringZZ2
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLL
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return


@@@@@@@@@@@@@@@@@ Battle script AI first/second poke
.align 2

MegaBSForAI:	@ MegaBattleScriptForAI

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringUUAI
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLLAI
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return

.align 2

BattleScriptWishAI:	@ WishBattleScriptForAI

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringZZAI
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLLAI
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return

.align 2

MegaBSForAI2:	@ MegaBattleScriptForAI2

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringUUAI2
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLLAI
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return

.align 2

BattleScriptWishAI2:	@ WishBattleScriptForAI2

.byte SetWordCommand			
.word YourWordLocation
.word LocationStringZZAI2
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringLLAI
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchIn
.byte 0x3C		@ return



@@@@@@@@@@@@@@@@@ Strings Player/Partner

.align 2

StringUU:	@ String UU

.byte 0xFD, 0xF, 0x0, 0xDD, 0xE7, 0x0, 0xE6, 0xD9, 0xD5, 0xD7, 0xE8, 0xDD, 0xE2, 0xDB, 0x0, 0xE8
.byte 0xE3, 0x0, 0xFD, 0x23, 0xFE, 0xC7, 0xD9, 0xDB, 0xD5, 0x0, 0xBC, 0xE6, 0xD5, 0xD7, 0xD9, 0xE0
.byte 0xD9, 0xE8, 0xAB, 0xFF

.align 2

StringZZ:	@ String ZZ

.byte 0xFD, 0x23, 0xB4, 0xE7, 0x0, 0xDA, 0xD9, 0xE6, 0xEA, 0xD9, 0xE2, 0xE8, 0x0, 0xD1, 0xDD, 0xE7
.byte 0xDC, 0xFE, 0xE6, 0xD9, 0xD5, 0xD7, 0xDC, 0xD9, 0xD8, 0x0, 0xFD, 0x0F, 0xAB, 0xFF

.align 2

StringLL:		@ String LL

.byte 0xFD, 0xF, 0x0, 0xDC, 0xD5, 0xE7, 0x0, 0xC7, 0xD9, 0xDB, 0xD5, 0x0, 0xBF, 0xEA, 0xE3, 0xE0
.byte 0xEA, 0xD9, 0xD8, 0x0, 0xDD, 0xE2, 0xE8, 0xE3, 0xFE, 0xC7, 0xD9, 0xDB, 0xD5, 0x0, 0xFD, 0xF
.byte 0xAB, 0xFF

.align 2

StringUU2:		@ String UU2

.byte 0xFD, 0xF, 0x0, 0xDD, 0xE7, 0x0, 0xE6, 0xD9, 0xD5, 0xD7, 0xE8, 0xDD, 0xE2, 0xDB, 0x0, 0xE8
.byte 0xE3, 0x0, 0xFD, 0x33, 0xFE, 0xC7, 0xD9, 0xDB, 0xD5, 0x0, 0xBC, 0xE6, 0xD5, 0xD7, 0xD9, 0xE0
.byte 0xD9, 0xE8, 0xAB, 0xFF

.align 2

StringZZ2:  	@ String ZZ2

.byte 0xFD, 0x33, 0xB4, 0xE7, 0x0, 0xDA, 0xD9, 0xE6, 0xEA, 0xD9, 0xE2, 0xE8, 0x0, 0xD1, 0xDD, 0xE7
.byte 0xDC, 0xFE, 0xE6, 0xD9, 0xD5, 0xD7, 0xDC, 0xD9, 0xD8, 0x0, 0xFD, 0xF, 0xAB, 0xFF


@@@@@@@@@@@@@@@@@@ Strings AI/Second poke AI

.align 2

StringUUAI:		@ String UUAI

.byte 0xFD, 0xF, 0x0, 0xDD, 0xE7, 0x0, 0xE6, 0xD9, 0xD5, 0xD7, 0xE8, 0xDD, 0xE2, 0xDB, 0x0, 0xE8
.byte 0xE3, 0x0, 0xFD, 0x1D, 0xFE, 0xC7, 0xD9, 0xDB, 0xD5, 0x0, 0xBC, 0xE6, 0xD5, 0xD7, 0xD9, 0xE0
.byte 0xD9, 0xE8, 0xAB, 0xFF

.align 2

StringZZAI:		@ String ZZAI

.byte 0xFD, 0x1D, 0xB4, 0xE7, 0x0, 0xDA, 0xD9, 0xE6, 0xEA, 0xD9, 0xE2, 0xE8, 0x0, 0xD1, 0xDD, 0xE7
.byte 0xDC, 0xFE, E6, 0xD9, 0xD5, 0xD7, 0xDC, 0xD9, 0xD8, 0x0, 0xFD, 0xF, 0xAB, 0xFF

.align 2

StringLLAI:		@ String LLAI

.byte 0xFD, 0xF, 0x0, 0xDC, 0xD5, 0xE7, 0x0, 0xC7, 0xD9, 0xDB, 0xD5, 0x0, 0xBF, 0xEA, 0xE3, 0xE0
.byte 0xEA, 0xD9, 0xD8, 0x0, 0xDD, 0xE2, 0xE8, 0xE3, 0xFE, 0xC7, 0xD9, 0xDB, 0xD5, 0x0, 0xFD, 0x8
.byte 0xAB, 0xFF

.align 2

StringUUAI2: 	@ String UUAI2

.byte 0xFD, 0xF, 0x0, 0xDD, 0xE7, 0x0, 0xE6, 0xD9, 0xD5, 0xD7, 0xE8, 0xDD, 0xE2, 0xDB, 0x0, 0xE8
.byte 0xE3, 0x0, 0xFD, 0x2F, 0xFE, 0xC7, 0xD9, 0xDB, 0xD5, 0x0, 0xBC, 0xE6, 0xD5, 0xD7, 0xD9, 0xE0
.byte 0xD9, 0xE8, 0xAB, 0xFF

.align 2

StringZZAI2:	@ String ZZAI2

.byte 0xFD, 0x2F, 0xB4, 0xE7, 0x0, 0xDA, 0xD9, 0xE6, 0xEA, 0xD9, 0xE2, 0xE8, 0x0, 0xD1, 0xDD, 0xE7
.byte 0xDC, 0xFE, 0xE6, 0xD9, 0xD5, 0xD7, 0xDC, 0xD9, 0xD8, 0x0, 0xFD, 0xF, 0xAB, 0xFF


@@@@@@@@@@@ End Strings.

.align 2

PlayCryRoutine:		@@ Play cry routine

push {lr}
ldr r0, BattleData8
ldr r3, Bank8
ldrb r3, [r3]
mov r2, #0x58
mul r3, r2
add r3, r0
ldrh r0, [r0]
mov r1, #0x19
neg r1, r1
mov r2, #0x5
ldr r3, play_cry_maybe8
bl Jump8
pop {pc}

Jump8:
mov pc, r3

.align 2
Bank8: .word 0x202420B
BattleData8: .word 0x2024084
play_cry_maybe8: .word 0x80A32E4+1

.align 2

HelpSwitchInRoutine:	@@ Help switch-in abilities for mega

Start9:
push {lr}
ldr r1, bank9
ldrb r1, [r1]
mov r0, #0
mov r2, #0
mov r3, #0
ldr r4, ability_something9
bl Jump9
cmp r0, #0
beq Return9
ldr r0, CurrentBS9
ldr r1, [r0]
sub r1, #5
str r1, [r0]
pop {pc}

Return9:
pop {pc}

Jump9:
mov pc, r4

.align 2
bank9: .word 0x0202420B
ability_something9: .word 0x08042828+1
CurrentBS9: .word 0x02024214

.align 2

BattleScriptPrimal:		@@ PrimalBattleScript

.byte SetWordCommand			
.word LocationStringPrim
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x20, 0x0	@ waitmessage
.byte 0x45	@ playanimation
.byte 0x1	@ Bankuser
.byte YourMegaAnimationIndex
.word 0x0
.byte 0xA	@ waitanimation
.byte CallAsmCommand
.word PlayCry
.byte SetWordCommand			
.word YourWordLocation
.word LocationStringPrimal
.byte 0x10, 0x84, 0x1	@ printstring 0x184
.byte 0x12, 0x40, 0x0	@ waitmessage
.byte CallAsmCommand
.word HelpSwitchInPrimal
.byte 0x41	@ Call command
.word 0x82DB48D
.byte 0x3F	@ End3 command

.align 2

StringPrimalNull:
.byte 0x0, 0xFF

.align 2

StringPrimal:	@ String primal

.byte 0xFD, 0x13, 0xB4, 0xE7, 0x0, 0xCA, 0xE6, 0xDD, 0xE1, 0xD5, 0xE0, 0x0, 0xCC, 0xD9, 0xEA, 0xD9
.byte 0xE6, 0xE7, 0xDD, 0xE3, 0xE2, 0xAB, 0xFE, 0xC3, 0xE8, 0x0, 0xE6, 0xD9, 0xEA, 0xD9, 0xE6, 0xE8
.byte 0xD9, 0xD8, 0x0, 0xE8, 0xE3, 0x0, 0xDD, 0xE8, 0xE7, 0x0, 0xE4, 0xE6, 0xDD, 0xE1, 0xD5, 0xE0
.byte 0x0, 0xDA, 0xE3, 0xE6, 0xE1, 0xAB, 0xFF

.align 2

PrimalSwitchInRoutine:		@ Help switch-in primal.

Start10:
push {lr}
ldr r1, bank10
ldrb r1, [r1]
cmp r1, #0
beq Return
mov r0, #0
mov r2, #0
mov r3, #0
ldr r4, ability_something10
bl Jump10
cmp r0, #0
beq Return10
ldr r0, CurrentBS10
ldr r1, [r0]
sub r1, #5
str r1, [r0]

Return10:
pop {pc}

Jump10:
mov pc, r4

.align 2
bank10: .word 0x0202420B
ability_something10: .word 0x08042828+1
CurrentBS10: .word 0x02024214

.align 2

ImposterBattleScript: @ Battlescript imposter

.byte CallAsmCommand
.word FirstRoutineTransform
.byte 0x9B	@ Transformdataexecution
.byte 0x9	@ Attackanimation
.byte 0xA	@ Waitanimation
.byte 0x13	@ Printfromtable
.word 0x85CC8D4
.byte 0x12, 0x40, 0x0
.byte CallAsmCommand
.word SecondRoutineTransform
.byte 0x3F	@ End3

.align 2

RoutineTransformSecond: @ FirstRoutineTransform

push {lr}
ldr r0, CurrentMove11
ldrh r1, [r0]
ldr r2, ImposterLocation11
strh r1, [r2]
mov r1, #0x90	@ Transform Move
strh r1, [r0]
pop {pc}

.align 2
ImposterLocation11: .word ImposterLocation
CurrentMove11: .word 0x20241EA

.align 2

RoutineTransformFirst: @ SecondRoutineTransform

push {lr}
ldr r0, CurrentMove12
ldr r2, ImposterLocation12
ldrh r1, [r2]
strh r1, [r0]
pop {pc}

.align 2
ImposterLocation12: .word ImposterLocation
CurrentMove12: .word 0x20241EA

.align 2

ScriptMoxieClear: @ BattleScriptMoxieClear

.byte 0x56, 0x0, 0x39, 0x40, 0x0, 0x1A, 0x0, 0x1B, 0x0, 0x10, 0x1D, 0x0, 0x10, 0x30, 0x1, 0x39
.byte 0x40, 0x0
.byte SetWordCommand
.word YourWordLocation
.word StringWeatherFade+SizeRom
.byte 0x10, 0x84, 0x1, 0x12, 0x40, 0x0, 0x1E, 0x1
.byte MoxieAbility
.word JumpIfMoxie+SizeRom
.byte 0x3C

JumpIfMoxie:
.byte 0xE3, 0x1
.word JumpIfUserFainted+SizeRom
.byte 0x20, 0x1, 0x0, 0x1, 0xC
.word JumpIfUserFainted+SizeRom
.byte 0x39, 0x20, 0x0, 0x2E
.word 0x202448F
.byte 0x0, 0x48, 0x1, 0x2, 0x0, 0x2E
.word 0x202448E
.byte 0x11, 0x89, 0x41
.word JumpIfUserFainted+SizeRom
.byte SetWordCommand
.word YourWordLocation
.word StringMoxie+SizeRom
.byte 0x10, 0x84, 0x1, 0x12, 0x40, 0x0

JumpIfUserFainted:
.byte 0x3C


.align 2
StringWeatherFade:
.byte 0xCE, 0xDC, 0xD9, 0x0, 0xEB, 0xD9, 0xD5, 0xE8, 0xDC, 0xD9, 0xE6, 0x0, 0xDC, 0xD5, 0xE7, 0x0
.byte 0xE6, 0xD9, 0xE8, 0xE9, 0xE6, 0xE2, 0xD9, 0xD8, 0xFE, 0xE8, 0xE3, 0x0, 0xE2, 0xE3, 0xE6, 0xE1
.byte 0xD5, 0xE0, 0xAB, 0xFF

.align 2
StringMoxie:
.byte 0xFD, 0xF, 0xB4, 0xE7, 0x0, 0xC7, 0xE3, 0xEC, 0xDD, 0xD9, 0xFE, 0xE6, 0xD5, 0xDD, 0xE7, 0xD9
.byte 0xD8, 0x0, 0xDD, 0xE8, 0xE7, 0x0, 0xBB, 0xE8, 0xE8, 0xD5, 0xD7, 0xDF, 0xAB, 0xFF

.align 2

DeltaStreamBattleScript:	@@ BattleScriptDeltaStream

.byte SetWordCommand
.word YourWordLocation
.word StringDeltaStream+SizeRom
.byte 0x10, 0x84, 0x1, 0x12, 0x40, 0x0, 0x45
.byte 0x0
.byte YourDeltaStreamAnimIndex
.word 0x0
.byte 0xA, 0x41
.word 0x82DB48D
.byte 0x3F
.byte 0xFF
.byte 0xFF

.align 2
StringDeltaStream:
.byte 0xBB, 0x0, 0xE1, 0xED, 0xE7, 0xE8, 0xD9, 0xE6, 0xDD, 0xE3, 0xE9, 0xE7, 0x0, 0xD5, 0xDD, 0xE6
.byte 0x0, 0xD7, 0xE9, 0xE6, 0xE6, 0xD9, 0xE2, 0xE8, 0x0, 0xDD, 0xE7, 0xFE, 0xE4, 0xE6, 0xE3, 0xE8
.byte 0xD9, 0xD7, 0xE8, 0xDD, 0xE2, 0xDB, 0x0, 0xC0, 0xE0, 0xED, 0xDD, 0xE2, 0xDB, 0xAE, 0xE8, 0xED
.byte 0xE4, 0xD9, 0x0, 0xCA, 0xE3, 0xDF, 0xD9, 0xE1, 0xE3, 0xE2, 0xAB, 0xFF

.align 2

ClearWeatherBattleScript:	@ BattleScriptClearWeather

.byte SetWordCommand
.word YourWordLocation
.word StringWeatherFadeAgain+SizeRom
.byte 0x10, 0x84, 0x1, 0x12, 0x40, 0x0, 0x56, 0x1, 0x39, 0x40, 0x0, 0x1A, 0x1, 0x1B, 0x1, 0x10
.byte 0x1C, 0x0, 0x3C

.align 2
StringWeatherFadeAgain:
.byte 0xCE, 0xDC, 0xD9, 0x0, 0xEB, 0xD9, 0xD5, 0xE8, 0xDC, 0xD9, 0xE6, 0x0, 0xDC, 0xD5, 0xE7, 0x0
.byte 0xE6, 0xD9, 0xE8, 0xE9, 0xE6, 0xE2, 0xD9, 0xD8, 0xFE, 0xE8, 0xE3, 0x0, 0xE2, 0xE3, 0xE6, 0xE1
.byte 0xD5, 0xE0, 0xAB, 0xFF


Explanations:
Spoiler:

-I use the same principle than Touched. That is to say, to set a mega evolution, you have to set the evolution of you pokemon to 0xFC or 0xFE, the first
being Wish mega evo, taking the attack as argument, the second being the classic mega evolution, taking the index number of your Mega Stone as argument.
-Primal reversion is the 0xFD evolution and also takes the item to hold as argument.

-To active the mega evolution, when selecting an attack, press start, you will hear a sound if it has been activated. Though there are no graphic. Press B or select to desactivate it.

-This should support expanded evolution and expanded pokedex if you change the values accordingly in the routines. Indeed I load the evolution table/pokemon data from a
pointer rather than directly.

-Multi, Tag and double battles are supported.

-To make trick and such ineffective against mega poke, it's inside the multitype routine in my signature.

-Give your pokemon and their mega evolution the same xp ratio, HP, Name, EggGroup, IndexNumber etc
-What you can change is, as in the officials games : att/def/speed/attspe/defspe ability (only one), type.

To do list:
Spoiler:

-The sentence will always be with the item "Mega Bracelet" (I don't know if it's the right word), I didn't added support for custom item name yet.

-I also plan to add a check for a flag or something, this way you can allow/prevent the player from mega evolving.

-There are no icon yet, I don't know if I'll do this one day.

Bug(s):
Spoiler:

-It's the cry of the normal species that is played when your mega faint, I know why, I'll see what I can do for that.
-The shadow doesn't update for now.

good work!and if you can make a IPS patch it will great !

Lance32497

LanceKoijer of Pokemon_Addicts

Male
Criscanto town-Ginoa Region xD
Seen August 13th, 2017
Posted January 1st, 2017
792 posts
8.7 Years
Guys, I can't still make it work, I know now how to change the offset, but everytime I enter the battle, the screen turns to black and it crashes the game
This signature has been disabled.
Scrollbar appears
Please review and fix the issues by reading the signature rules.

You must edit it to meet the limits set by the rules before you may remove the [sig-reason] code from your signature. Removing this tag will re-enable it.

Do not remove the tag until you fix the issues in your signature. You may be infracted for removing this tag if you do not fix the specified issues. Do not use this tag for decoration purposes.
Female
Seen February 5th, 2021
Posted April 5th, 2016
23 posts
8.5 Years
Hi everyone, so i applied this one to a clean rom,and a test.gba came out , i dont know if its properly implemented because after i run make on cmd,a bunch of codes came out then typed insert.py,it pauses for a while like it's doing something but no bunch of codes came out(according to PokeMew's tuts which i followed,there should be) but when i checked 0x800000 using g3hs's hex editor,it's not a bunch of FF's ...Is that 'Ok'?

Anyways, tried to make a mega-evolving pokemon and guess what, i happened to make one. Will the problem(if it is) above will have side-effects? And sorry if this is already asked,what are the do's and dont's? Thanks for making this happen!

Exodrake

The Manliest Chick that Ever Manlied

Age 29
Female
Seen November 30th, 2016
Posted July 24th, 2016
163 posts
10 Years
Hi everyone, so i applied this one to a clean rom,and a test.gba came out , i dont know if its properly implemented because after i run make on cmd,a bunch of codes came out then typed insert.py,it pauses for a while like it's doing something but no bunch of codes came out(according to PokeMew's tuts which i followed,there should be) but when i checked 0x800000 using g3hs's hex editor,it's not a bunch of FF's ...Is that 'Ok'?

Without knowing if it is properly implemented, i tried using it,following Criminon's tuts and using g3hs(added 1 new mon(same stats/moves with bulbasaur) using the 'expand pokemon' without changing total dex entries,,added mega evolution(no item) on bulbasaur to evolve to the new mon,,added mega evo(no item) on the new mon to evolve back to bulbasaur)...gave myself the gold teeth...and whatnot,there is a mega symbol next to the pokemon's level but when i click 'fight' and while selecting a move ,no mega symbol pop's out unlike on the sample video...BUT on the next battle, the pokemon shown is the evolved one,so i think it is because of the 'no item' on the main pokemom(it's evolving to it's other evolution every after battle)..So i changed the required item on bulbasaur to 'master ball' and given the master ball item,but the mega symbol next to the pokemon level is not showing itself.i even have the golden teeth on my bag, what did i do wrong? Any help is appreciated.
Try disabling the key stone check, as per the instructions here: http://www.pokecommunity.com/showpost.php?p=8878639&postcount=22

I'M A WITCH THAT'S HOW
Female
Seen February 5th, 2021
Posted April 5th, 2016
23 posts
8.5 Years
Try disabling the key stone check, as per the instructions here: http://www.pokecommunity.com/showpost.php?p=8878639&postcount=22
Yep, forgot to mention on my edit that i did disable the key check and yeah it works. i dont know if it is just me but mega evolve doesn't work on tutorial battle(first rival battle) because that's where i am checking if mega evolving is implemented until i tried it on wild. anyways, trying to change the texts now,and i read msgbox_len 35, is that the max amount of characters per msgbox?
Male
Seen February 1st, 2018
Posted October 19th, 2017
625 posts
8.9 Years
Guys, I can't still make it work, I know now how to change the offset, but everytime I enter the battle, the screen turns to black and it crashes the game
You have to change the offset in two places. Did you change them in both the insert.py script (by running insert.py --offset 0x800000) and in the linker script (linker.lsc). The offset also has to be properly aligned (i.e. it must end in 0, 4, 8 or C).

Yep, forgot to mention on my edit that i did disable the key check and yeah it works. i dont know if it is just me but mega evolve doesn't work on tutorial battle(first rival battle) because that's where i am checking if mega evolving is implemented until i tried it on wild. anyways, trying to change the texts now,and i read msgbox_len 35, is that the max amount of characters per msgbox?
insert.py no longer spits out addresses of the individual functions by default (you have to run "insert.py --debug" to see that), as that information is irrelevant to most users. If the script creates test.gba and uninstall.ips, then it worked properly.

Mega evolutions won't work on the tutorial battle, because that would be a waste of time to implement (you shouldn't have Megas that early, if you're using that irritating battle at all)

I think MSGBOX_LEN is the value that XSE's text adjuster uses to determine when to insert newlines. It's not used anywhere in the code, so you can ignore it.

A Pokemon that is discriminated!
Support squirtle and make it everyone's favourite.
Male
Seen February 1st, 2018
Posted October 19th, 2017
625 posts
8.9 Years
Oh, i see. Thanks!

But what if i edited some files in the /src folder,and want to generate new patched rom.do i need to just run the 'make' and 'insert.py' again? Or should i start from the beginning of the tutorial(skipping the installations)?
If you want to reinstall after modifications, all you have to do is rerun make and insert.py. That's pretty much the reason inserting this is "complicated" - it eases the development process. I actually just run the debug bash script when developing because it doesn't create the IPS patch (which takes like a full 2 seconds). If you're on windows and want to replicate (sort of) that script, just run

make
insert.py --no-patch --debug
This will prevent the patch from being created, which results in the insertion process being almost instantaneous.

A Pokemon that is discriminated!
Support squirtle and make it everyone's favourite.