From acdac2717a18261d77439d683a0260718cbb0872 Mon Sep 17 00:00:00 2001 From: Lorenzo Carletti Date: Wed, 3 Nov 2021 18:34:33 +0100 Subject: [PATCH] Fix issue with butterflies in Onett Decided to go for an "issue-specific fix" for this one. If many more of these are discovered in the future, a way to fix this for good is making it so m2_malloc also automatically clears the buffer (although that would be a very drastic decision, which would limit what you can do with a writing buffer) --- src/c/custom_codes.c | 5 +++++ src/c/custom_codes.h | 1 + src/c/ext.c | 1 + src/c/vwf.c | 12 ++++++++++++ src/c/vwf.h | 4 +++- src/m2-hack.asm | 1 + working/m12-strings-english.txt | 2 +- 7 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/c/custom_codes.c b/src/c/custom_codes.c index bb8bea9..167fe09 100644 --- a/src/c/custom_codes.c +++ b/src/c/custom_codes.c @@ -185,6 +185,11 @@ int custom_codes_parse_generic(int code, char* parserAddress, WINDOW* window, by inv_window->pixel_x = 0xFF; addedSize = 2; break; + + case RESET_WRITE_BUFFER: + // 58 FF: Reset the writing buffer to avoid crashes + free_overworld_buffer(); + break; default: break; diff --git a/src/c/custom_codes.h b/src/c/custom_codes.h index 247e91a..4850059 100644 --- a/src/c/custom_codes.h +++ b/src/c/custom_codes.h @@ -25,6 +25,7 @@ #define BATTLE_USER_GENDER 4 #define BATTLE_TARGET_GENDER 5 +#define RESET_WRITE_BUFFER 0x58 #define RESET_STORED_GOODS 0x59 #define RESTORE_DIALOGUE 0x5A #define PRINT_MAIN_WINDOW 0x5B diff --git a/src/c/ext.c b/src/c/ext.c index 9cbde83..782ae8b 100644 --- a/src/c/ext.c +++ b/src/c/ext.c @@ -28,5 +28,6 @@ void __attribute__((naked)) generic_reprinting_first_menu_talk_to_highlight() {} void __attribute__((naked)) m12_dim_palette(short* palette, int total, int dimmingFactor) {} int __attribute__((naked)) m2_jump_to_offset(byte* character) {} byte* __attribute__((naked)) m2_malloc(int size) {} +void __attribute__((naked)) m2_free(int* address) {} void __attribute__((naked)) m2_title_teardown() {} void __attribute__((naked)) vblank() {} diff --git a/src/c/vwf.c b/src/c/vwf.c index 6585d6b..12235e3 100644 --- a/src/c/vwf.c +++ b/src/c/vwf.c @@ -2051,6 +2051,18 @@ int check_overworld_buffer() return address; } +void free_overworld_buffer() +{ + int address = *((int*)(OVERWORLD_BUFFER_POINTER)); + if(address != 0) + { + int tmp_counter = m2_buffer_counter; + m2_free((int*)address); + *((int*)(OVERWORLD_BUFFER_POINTER)) = 0; + m2_buffer_counter = tmp_counter; + } +} + void load_pixels_overworld_buffer() { int tile = *tile_offset; diff --git a/src/c/vwf.h b/src/c/vwf.h index 91fb454..4209e22 100644 --- a/src/c/vwf.h +++ b/src/c/vwf.h @@ -129,6 +129,7 @@ void highlight_talk_to(); unsigned short printstr_hlight_buffer(WINDOW* window, byte* str, unsigned short x, unsigned short y, bool highlight); unsigned short printstr_hlight_pixels_buffer(WINDOW* window, byte* str, unsigned short x, unsigned short y, bool highlight); int check_overworld_buffer(); +void free_overworld_buffer(); void load_pixels_overworld_buffer(); void store_pixels_overworld_buffer(int totalYs); void store_pixels_overworld_buffer_totalTiles(int totalTiles); @@ -178,4 +179,5 @@ extern void m2_sub_d6844(); extern int m2_setupwindow(WINDOW* window, short window_x, short window_y, short window_width, short window_height); extern void m2_setupbattlename(short value); extern void store_pixels_overworld(); -extern byte* m2_malloc(); +extern byte* m2_malloc(int size); +extern void m2_free(int* address); diff --git a/src/m2-hack.asm b/src/m2-hack.asm index 846c2a6..84f29b7 100644 --- a/src/m2-hack.asm +++ b/src/m2-hack.asm @@ -2362,6 +2362,7 @@ disclaimer_map: .definelabel m2_copy_name_temp_mem ,0x8004E34 .definelabel m2_insert_default_name ,0x8005708 .definelabel m2_malloc ,0x8005B9C +.definelabel m2_free ,0x8005BD4 .definelabel m2_get_hall_address ,0x800D7BC .definelabel m2_title_quick_setup ,0x8011BFC .definelabel m12_dim_palette ,0x80137DC diff --git a/working/m12-strings-english.txt b/working/m12-strings-english.txt index bb32dc5..5659cc3 100644 --- a/working/m12-strings-english.txt +++ b/working/m12-strings-english.txt @@ -2146,7 +2146,7 @@ ^L2127^@Escargo Express.[02 FF]@...[02 FF]@I'm here to deliver[01 FF] the [90 FF A9][1A FF 02 00].[02 FF][91 FF FF][82 FF _L4025_][93 FF 00 A9][D4 FF 76 00]@Here you are.[02 FF]@Thank you very much![1D FF][C6 FF][09 FF B7 02][09 FF B5 02][86 FF _L4023_][86 FF _L4024_][00 FF] ^L2128^[09 FF B6 02][09 FF F2 02][00 FF] ^L2129^[09 FF B7 02][09 FF F2 02][00 FF] -^L2130^[A0 FF F1 03 2C 02 01 00][9E FF]@The Magic butterfly made[01 FF] [86 FF _L11000_] relax.[1D FF][C6 FF][A6 FF F1 03 2D 02][00 FF] +^L2130^[58 FF][A0 FF F1 03 2C 02 01 00][9E FF]@The Magic butterfly made[01 FF] [86 FF _L11000_] relax.[1D FF][C6 FF][A6 FF F1 03 2D 02][00 FF] ^L2131^[08 FF D5 01][08 FF 0B 00][CB FF FF 00 06 00][A0 FF D0 07 85 00 01 00][86 FF _L4040_][14 FF 0D 00][A0 FF D0 07 86 00 01 00][86 FF _L4040_][14 FF 0E 00][A0 FF D0 07 87 00 01 00][86 FF _L4040_][08 FF DD 01][08 FF 14 02][08 FF ED 02][14 FF 0F 00][A3 FF 1A 00][1B FF 78 00][1B FF 78 00][1B FF 78 00][1B FF 78 00][1B FF 78 00][1B FF 3C 00][A1 FF 1A 00 B3 00][A0 FF D0 07 89 00 01 00][09 FF 14 02][08 FF 15 02][C5 FF][9E FF][85 FF 1A 00 06 00][CC FF FF 00 01 00][A6 FF D0 07 8A 00][C9 FF D0 07][EB FF FF 00][9E FF][CA FF][08 FF DC 01][09 FF DD 01][09 FF D5 01][7F FF][00 FF] ^L2132^@While delivering pizza, this[01 FF] weird guy asked me to[01 FF] help him out...[02 FF]@He wanted me to deliver this to[01 FF] someone named [0D FF].[02 FF]@...who is wandering around[01 FF] Threed.[02 FF]@No one else knows about this,[01 FF] right?[02 FF]@Let's just pretend that you're[01 FF] [0D FF],[1B FF 0F 00] and I'll give this[01 FF] to you.[02 FF]@Oh![1B FF 0F 00] Hello, [0D FF]![02 FF]@I made the decision that you're[01 FF] [0D FF],[1B FF 0F 00] no matter[01 FF] what...[02 FF][91 FF FF][82 FF _L4227_][01 FF]^L4232^[93 FF 00 A6][86 FF _L2238_][02 FF]@That's right, [0D FF]...[01 FF] *wink, wink*[02 FF]@I've done my duty and given[01 FF] you Apple Kid's thingamajig...[02 FF]@Well, goodbye!...[02 FF][C6 FF][09 FF A3 02][08 FF 28 02][86 FF _L4023_][09 FF F2 02][00 FF] ^L2133^[CF FF 08 00][00 FF]