mirror of
https://github.com/GearsProgress/Poke_Transporter_GB.git
synced 2026-07-17 09:08:39 -05:00
91 lines
5.4 KiB
C++
91 lines
5.4 KiB
C++
#include <tonc.h>
|
|
#include "script.h"
|
|
#include "flash_mem.h"
|
|
#include "gba_flash.h"
|
|
#include "script_builder.h"
|
|
|
|
static u32 wonder_card[0x14E] = {
|
|
0xFC, 0x85, 0x00, 0x00, 0xE8, 0x03, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBB, 0xCF, 0xCC, 0xC9, 0xCC, 0xBB, 0xCE, 0xC3, 0xBD, 0xC5, 0xBF, 0xCE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBF, 0xEC, 0xD7, 0xDC, 0xD5, 0xE2, 0xDB, 0xD9, 0x00, 0xBD, 0xD5, 0xE6, 0xD8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC1, 0xE3, 0x00, 0xE8, 0xE3, 0x00, 0xE8, 0xDC, 0xD9, 0x00, 0xE7, 0xD9, 0xD7, 0xE3, 0xE2, 0xD8, 0x00, 0xDA, 0xE0, 0xE3, 0xE3, 0xE6, 0x00, 0xE3, 0xDA, 0x00, 0xE8, 0xDC, 0xD9, 0x00, 0xCA, 0xC9, 0xC5, 0x1B, 0xC7, 0xC9, 0xC8, 0x00, 0x00, 0x00, 0xBD, 0xBF, 0xC8, 0xCE, 0xBF, 0xCC, 0x00, 0xD5, 0xE2, 0xD8, 0x00, 0xE1, 0xD9, 0xD9, 0xE8, 0x00, 0xE8, 0xDC, 0xD9, 0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|
|
|
|
// CRC16 checksum
|
|
void inject_mystery(Pokemon incoming_party_aray[])
|
|
{
|
|
flash_read(memory_section_array[4], &global_memory_buffer[0], 0x1000);
|
|
for (int i = 0; i < 0x14E; i++)
|
|
{
|
|
global_memory_buffer[INTER_WONDER_CARD_OFFSET_EMER + i] = wonder_card[i];
|
|
}
|
|
|
|
mystery_gift_script script;
|
|
script.build_script(incoming_party_aray);
|
|
u16 checksum = script.calc_checksum();
|
|
|
|
// Set checksum and padding
|
|
global_memory_buffer[EVENT_SCRIPT_DATA_OFFSET_EMER] = checksum >> 0;
|
|
global_memory_buffer[EVENT_SCRIPT_DATA_OFFSET_EMER + 1] = checksum >> 8;
|
|
global_memory_buffer[EVENT_SCRIPT_DATA_OFFSET_EMER + 2] = 0x00;
|
|
global_memory_buffer[EVENT_SCRIPT_DATA_OFFSET_EMER + 3] = 0x00;
|
|
|
|
// Add in Mystery Script data
|
|
for (int i = 0; i < MG_SCRIPT_SIZE; i++)
|
|
{
|
|
global_memory_buffer[EVENT_SCRIPT_DATA_OFFSET_EMER + 4 + i] = script.get_script_value_at(i);
|
|
}
|
|
update_memory_buffer_checksum();
|
|
flash_write(memory_section_array[4], &global_memory_buffer[0], 0x1000);
|
|
|
|
//Set flags
|
|
flash_read(memory_section_array[2], &global_memory_buffer[0], 0x1000);
|
|
global_memory_buffer[0x2F0] |= 0b00111111; //Set "to obtain" flags to 1
|
|
global_memory_buffer[0x2F0] &= ~0b01000000; //Set "collected all" flag to 0
|
|
update_memory_buffer_checksum();
|
|
flash_write(memory_section_array[2], &global_memory_buffer[0], 0x1000);
|
|
|
|
}
|
|
|
|
/*
|
|
000168: The main function
|
|
push {lr} save the load register to the stack
|
|
ldr r3, [pc, <000188>] set r3 to variable to 0x020375E8 (the pokemon offset, variable 0x8008's pointer)
|
|
ldr r3, [r3, #0] set r3 to the value in memory r3 points to
|
|
add r0, pc, #28 set r0 to a pointer 28 bytes ahead, which is the start of the Pokemon struct.
|
|
add r0, r0, r3 add r3 to r0, giving it the correct offset for the current index
|
|
ldr r1, [pc, <000180>] set r1 to 0x0806B491, which is the location of "SendMonToPC" plus one, since it is thumb code
|
|
mov r2, r15 move r15 (the program counter) to r2
|
|
add r2, r2, #7 add 7 to r2 to compensate for the six following bytes, plus to tell the system to read as thumb code
|
|
mov r14, r2 move r2 into r14 (the load register)
|
|
bx r1 jump to the pointer stored in r1 (SendMonToPC)
|
|
00017A: The second part of the function
|
|
ldr r2, [pc, <000184>] load 0x020375E4 (variable 0x8006's pointer) into r2
|
|
str r0, [r2, #0] put the value of r0 into the memory location pointed at by r2, plus 0
|
|
pop {r0} remove r0 from the stack and put it into r0
|
|
bx r0 jump to r0 (return to where the function was called)
|
|
000180:
|
|
.word 0x0806B491 the location of "SendMonToPC", plus one (so it is interpreted as thumb code)
|
|
000184:
|
|
.word 0x020375E4 the location of variable "0x8006" (the return value)
|
|
000188:
|
|
.word 0x020375E8 the location of variable "0x8008" (the pokemon offset)
|
|
|
|
- 00 B5
|
|
- 08 4B
|
|
- 1B 68
|
|
- 08 A0
|
|
- C0 18
|
|
- 04 49
|
|
- 7A 46
|
|
- 07 32
|
|
- 96 46
|
|
- 08 47
|
|
- 02 4A
|
|
- 10 60
|
|
01 BC
|
|
00 47
|
|
91 B4
|
|
06 08
|
|
E4 75
|
|
03 02
|
|
E8 75
|
|
03 02
|
|
|
|
*/ |