From 1c18f80f075f60ed94abec20624388f09bcdb39b Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Tue, 27 Jan 2015 02:05:36 +0000 Subject: [PATCH] implement game_command_set_ride_name --- src/game.c | 4 +- src/localisation/localisation.h | 1 + src/localisation/user.c | 16 +++++++ src/ride/ride.c | 79 ++++++++++++++++++++++++++++++++- src/ride/ride.h | 3 ++ src/windows/banner.c | 23 ++++------ src/windows/ride.c | 5 +-- src/windows/sign.c | 8 ++-- 8 files changed, 112 insertions(+), 27 deletions(-) diff --git a/src/game.c b/src/game.c index d1847d1351..bc3a8fbb23 100644 --- a/src/game.c +++ b/src/game.c @@ -869,7 +869,7 @@ static uint32 game_do_command_table[58] = { 0x006B49D9, 0x006B4EA6, 0x006B52D4, - 0x006B578B, // 10 + 0, // 10 0x006B5559, 0x006660A8, 0x0066640B, @@ -932,7 +932,7 @@ static GAME_COMMAND_POINTER* new_game_command_table[58] = { game_command_emptysub, game_command_emptysub, game_command_emptysub, - game_command_emptysub, // 10 + game_command_set_ride_name, // 10 game_command_emptysub, game_command_emptysub, game_command_emptysub, diff --git a/src/localisation/localisation.h b/src/localisation/localisation.h index 97068d7eed..7d7680156d 100644 --- a/src/localisation/localisation.h +++ b/src/localisation/localisation.h @@ -32,6 +32,7 @@ void error_string_quit(int error, rct_string_id format); int get_string_length(char* buffer); void user_string_clear_all(); +rct_string_id user_string_allocate(int base, const char *text); void user_string_free(rct_string_id id); #define MAX_USER_STRINGS 1024 diff --git a/src/localisation/user.c b/src/localisation/user.c index a832775f15..54a856ef02 100644 --- a/src/localisation/user.c +++ b/src/localisation/user.c @@ -18,6 +18,7 @@ * along with this program. If not, see . *****************************************************************************/ +#include "../addresses.h" #include "localisation.h" char *gUserStrings = (char*)0x0135A8F4; @@ -31,6 +32,21 @@ void user_string_clear_all() memset(gUserStrings, 0, MAX_USER_STRINGS * USER_STRING_MAX_LENGTH); } +/** + * + * rct2: 0x006C421D + */ +rct_string_id user_string_allocate(int base, const char *text) +{ + int eax, ebx, ecx, edx, esi, edi, ebp; + + ecx = base; + edi = (int)text; + RCT2_CALLFUNC_X(0x006C421D, &eax, &ebx, &ecx, &edx, &esi, &edi, &ebp); + + return eax & 0xFFFF; +} + /** * * rct2: 0x006C42AC diff --git a/src/ride/ride.c b/src/ride/ride.c index a447ef92ed..d927dbae48 100644 --- a/src/ride/ride.c +++ b/src/ride/ride.c @@ -2679,4 +2679,81 @@ void ride_music_update_final() } } -#pragma endregion \ No newline at end of file +#pragma endregion + +void ride_set_name(int rideIndex, const char *name) +{ + RCT2_GLOBAL(RCT2_ADDRESS_GAME_COMMAND_ERROR_TITLE, uint16) = STR_CANT_RENAME_RIDE_ATTRACTION; + game_do_command(1, (rideIndex << 8) | 1, 0, *((int*)(name + 0)), GAME_COMMAND_SET_RIDE_NAME, *((int*)(name + 8)), *((int*)(name + 4))); + game_do_command(2, (rideIndex << 8) | 1, 0, *((int*)(name + 12)), GAME_COMMAND_SET_RIDE_NAME, *((int*)(name + 20)), *((int*)(name + 16))); + game_do_command(0, (rideIndex << 8) | 1, 0, *((int*)(name + 24)), GAME_COMMAND_SET_RIDE_NAME, *((int*)(name + 32)), *((int*)(name + 28))); +} + +/** + * + * rct2: 0x006B578B + */ +void game_command_set_ride_name(int *eax, int *ebx, int *ecx, int *edx, int *esi, int *edi, int *ebp) +{ + rct_window *w; + rct_ride *ride; + rct_string_id newUserStringId; + char oldName[128]; + static char newName[128]; + + int rideIndex = (*ebx >> 8) & 0xFF; + int nameChunkIndex = *eax & 0xFFFF; + + RCT2_GLOBAL(0x0141F56C, uint8) = 4; + if (*ebx & GAME_COMMAND_FLAG_APPLY) { + int nameChunkOffset = nameChunkIndex - 1; + if (nameChunkOffset < 0) + nameChunkOffset = 2; + nameChunkOffset *= 12; + RCT2_GLOBAL(newName + nameChunkOffset + 0, uint32) = *edx; + RCT2_GLOBAL(newName + nameChunkOffset + 4, uint32) = *ebp; + RCT2_GLOBAL(newName + nameChunkOffset + 8, uint32) = *edi; + } + + if (nameChunkIndex != 0) { + *ebx = 0; + return; + } + + ride = GET_RIDE(rideIndex); + format_string(oldName, ride->name, &ride->name_arguments); + if (strcmp(oldName, newName) == 0) { + *ebx = 0; + return; + } + + if (newName[0] == 0) { + RCT2_GLOBAL(RCT2_ADDRESS_GAME_COMMAND_ERROR_TEXT, uint16) = STR_INVALID_RIDE_ATTRACTION_NAME; + *ebx = MONEY32_UNDEFINED; + return; + } + + newUserStringId = user_string_allocate(4, newName); + if (newUserStringId == 0) { + *ebx = MONEY32_UNDEFINED; + return; + } + + if (*ebx & GAME_COMMAND_FLAG_APPLY) { + // Free the old ride name + user_string_free(ride->name); + + ride->name = newUserStringId; + + gfx_invalidate_screen(); + + // Force ride list window refresh + w = window_find_by_class(WC_RIDE_LIST); + if (w != NULL) + w->no_list_items = 0; + } else { + user_string_free(newUserStringId); + } + + *ebx = 0; +} \ No newline at end of file diff --git a/src/ride/ride.h b/src/ride/ride.h index 5fc7a53d73..577509c285 100644 --- a/src/ride/ride.h +++ b/src/ride/ride.h @@ -637,4 +637,7 @@ void ride_set_map_tooltip(rct_map_element *mapElement); int ride_music_params_update(sint16 x, sint16 y, sint16 z, uint8 rideIndex, uint16 sampleRate, uint32 position, uint8 *tuneId); void ride_music_update_final(); +void ride_set_name(int rideIndex, const char *name); +void game_command_set_ride_name(int *eax, int *ebx, int *ecx, int *edx, int *esi, int *edi, int *ebp); + #endif diff --git a/src/windows/banner.c b/src/windows/banner.c index d2facef113..8f0ac1605f 100644 --- a/src/windows/banner.c +++ b/src/windows/banner.c @@ -287,17 +287,13 @@ static void window_banner_dropdown() text_buffer[0] = banner->text_colour + FORMAT_COLOUR_CODE_START; - int string_id = 0, ebx = 0, ecx = 128, edx = 0, ebp = 0, esi = 0; - // Allocate text_buffer to a new string_id? - RCT2_CALLFUNC_X(0x6C421D, &string_id, &ebx, &ecx, &edx, &esi, (int*)&text_buffer, &ebp); - - if (string_id){ + rct_string_id stringId = user_string_allocate(128, text_buffer); + if (stringId != 0) { rct_string_id prev_string_id = banner->string_idx; - banner->string_idx = string_id; + banner->string_idx = stringId; user_string_free(prev_string_id); window_invalidate(w); - } - else{ + } else { window_error_open(2984, RCT2_GLOBAL(RCT2_ADDRESS_GAME_COMMAND_ERROR_TEXT, rct_string_id)); } break; @@ -324,16 +320,13 @@ static void window_banner_textinput() text_buffer[0] = banner->text_colour + FORMAT_COLOUR_CODE_START; strncpy(text_buffer + 1, text, 32); - int string_id = 0, ebx = 0, ecx = 128, edx = 0, ebp = 0, esi = 0; - RCT2_CALLFUNC_X(0x6C421D, &string_id, &ebx, &ecx, &edx, &esi, (int*)&text_buffer, &ebp); - - if (string_id){ + rct_string_id stringId = user_string_allocate(128, text_buffer); + if (stringId) { rct_string_id prev_string_id = banner->string_idx; - banner->string_idx = string_id; + banner->string_idx = stringId; user_string_free(prev_string_id); window_invalidate(w); - } - else{ + } else { window_error_open(2984, RCT2_GLOBAL(RCT2_ADDRESS_GAME_COMMAND_ERROR_TEXT, rct_string_id)); } } diff --git a/src/windows/ride.c b/src/windows/ride.c index 94b2b57c04..c5fd0f9447 100644 --- a/src/windows/ride.c +++ b/src/windows/ride.c @@ -1899,10 +1899,7 @@ static void window_ride_main_textinput() if (widgetIndex != WIDX_RENAME || !result) return; - RCT2_GLOBAL(RCT2_ADDRESS_GAME_COMMAND_ERROR_TITLE, uint16) = STR_CANT_RENAME_RIDE_ATTRACTION; - game_do_command(1, (w->number << 8) | 1, 0, *((int*)(text + 0)), GAME_COMMAND_SET_RIDE_NAME, *((int*)(text + 8)), *((int*)(text + 4))); - game_do_command(2, (w->number << 8) | 1, 0, *((int*)(text + 12)), GAME_COMMAND_SET_RIDE_NAME, *((int*)(text + 20)), *((int*)(text + 16))); - game_do_command(0, (w->number << 8) | 1, 0, *((int*)(text + 24)), GAME_COMMAND_SET_RIDE_NAME, *((int*)(text + 32)), *((int*)(text + 28))); + ride_set_name(w->number, text); } /** diff --git a/src/windows/sign.c b/src/windows/sign.c index 8f1d4bb90e..8bed79575f 100644 --- a/src/windows/sign.c +++ b/src/windows/sign.c @@ -360,17 +360,15 @@ static void window_sign_textinput() if (widgetIndex == WIDX_SIGN_TEXT && result) { if (*text != 0){ - int string_id = 0, ebx = 0, ecx = 128, edx = 0, ebp = 0, esi = 0; - RCT2_CALLFUNC_X(0x6C421D, &string_id, &ebx, &ecx, &edx, &esi, (int*)&text, &ebp); - if (string_id){ + rct_string_id string_id = user_string_allocate(128, text); + if (string_id != 0) { rct_string_id prev_string_id = banner->string_idx; banner->string_idx = string_id; user_string_free(prev_string_id); banner->flags &= ~(BANNER_FLAG_2); gfx_invalidate_screen(); - } - else{ + } else { window_error_open(2984, RCT2_GLOBAL(RCT2_ADDRESS_GAME_COMMAND_ERROR_TEXT, rct_string_id)); } }