diff --git a/Engine/ac/game.cpp b/Engine/ac/game.cpp index b66344530e..e5056474f5 100644 --- a/Engine/ac/game.cpp +++ b/Engine/ac/game.cpp @@ -822,9 +822,7 @@ const char* Game_InputBox(const char *msg) { } const char* Game_GetLocationName(int x, int y) { - char buffer[STD_BUFFER_SIZE]; - GetLocationName(x, y, buffer); // fills up to MAX_MAXSTRLEN - return CreateNewScriptString(buffer); + return CreateNewScriptString(GetLocationName(x, y)); } const char* Game_GetGlobalMessages(int index) { diff --git a/Engine/ac/global_api.cpp b/Engine/ac/global_api.cpp index 9522d9e02b..66a3c53169 100644 --- a/Engine/ac/global_api.cpp +++ b/Engine/ac/global_api.cpp @@ -645,9 +645,9 @@ RuntimeScriptValue Sc_GetInvPropertyText(const RuntimeScriptValue *params, int32 } // void (int xxx,int yyy,char*tempo) -RuntimeScriptValue Sc_GetLocationName(const RuntimeScriptValue *params, int32_t param_count) +RuntimeScriptValue Sc_GetLocationNameInBuf(const RuntimeScriptValue *params, int32_t param_count) { - API_SCALL_VOID_PINT2_POBJ(GetLocationName, char); + API_SCALL_VOID_PINT2_POBJ(GetLocationNameInBuf, char); } // int (int xxx,int yyy) @@ -2444,7 +2444,7 @@ void RegisterGlobalAPI(ScriptAPIVersion base_api, ScriptAPIVersion /*compat_api* { "GetInvName", API_FN_PAIR(GetInvName) }, { "GetInvProperty", API_FN_PAIR(GetInvProperty) }, { "GetInvPropertyText", API_FN_PAIR(GetInvPropertyText) }, - { "GetLocationName", API_FN_PAIR(GetLocationName) }, + { "GetLocationName", API_FN_PAIR(GetLocationNameInBuf) }, { "GetLocationType", API_FN_PAIR(GetLocationType) }, { "GetMessageText", API_FN_PAIR(GetMessageText) }, { "GetMIDIPosition", API_FN_PAIR(GetMIDIPosition) }, diff --git a/Engine/ac/global_game.cpp b/Engine/ac/global_game.cpp index 9dc9764d05..8b2fe22e3f 100644 --- a/Engine/ac/global_game.cpp +++ b/Engine/ac/global_game.cpp @@ -713,11 +713,11 @@ int GetLocationType(int xxx,int yyy) { } void SaveCursorForLocationChange() { - // update the current location name - char tempo[100]; - GetLocationName(game_to_data_coord(mousex), game_to_data_coord(mousey), tempo); + // update the current location name (ignore return value) + GetLocationName(game_to_data_coord(mousex), game_to_data_coord(mousey)); - if (play.get_loc_name_save_cursor != play.get_loc_name_last_time) { + if (play.get_loc_name_save_cursor != play.get_loc_name_last_time) + { play.get_loc_name_save_cursor = play.get_loc_name_last_time; play.restore_cursor_mode_to = GetCursorMode(); play.restore_cursor_image_to = GetMouseCursor(); @@ -725,78 +725,96 @@ void SaveCursorForLocationChange() { } } -void GetLocationName(int xxx,int yyy,char*tempo) +const char *GetLocationName(int x, int y) { - VALIDATE_STRING(tempo); - tempo[0] = 0; - if (displayed_room < 0) - return; // no room loaded yet + return nullptr; // no room loaded yet - if (GetGUIAt(xxx, yyy) >= 0) { - int mover = GetInvAt (xxx, yyy); - if (mover > 0) { + const char *out_name = nullptr; + if (GetGUIAt(x, y) >= 0) + { + int mover = GetInvAt(x, y); + if (mover > 0) + { if (play.get_loc_name_last_time != 1000 + mover) GUIE::MarkSpecialLabelsForUpdate(kLabelMacro_Overhotspot); play.get_loc_name_last_time = 1000 + mover; - snprintf(tempo, MAX_MAXSTRLEN, "%s", get_translation(game.invinfo[mover].name.GetCStr())); + out_name = get_translation(game.invinfo[mover].name.GetCStr()); } else if ((play.get_loc_name_last_time > 1000) && (play.get_loc_name_last_time < 1000 + MAX_INV)) { // no longer selecting an item GUIE::MarkSpecialLabelsForUpdate(kLabelMacro_Overhotspot); play.get_loc_name_last_time = -1; } - return; + return out_name; } - int loctype = GetLocationType(xxx, yyy); // GetLocationType takes screen coords - VpPoint vpt = play.ScreenToRoomDivDown(xxx, yyy); + int loctype = GetLocationType(x, y); // GetLocationType takes screen coords + VpPoint vpt = play.ScreenToRoomDivDown(x, y); if (vpt.second < 0) - return; - xxx = vpt.first.X; - yyy = vpt.first.Y; - if ((xxx>=thisroom.Width) | (xxx<0) | (yyy<0) | (yyy>=thisroom.Height)) - return; + return nullptr; + x = vpt.first.X; + y = vpt.first.Y; + if ((x >= thisroom.Width) || (x < 0) || (y < 0) || (y >= thisroom.Height)) + return nullptr; int onhs,aa; - if (loctype == 0) { - if (play.get_loc_name_last_time != 0) { + if (loctype == 0) + { + if (play.get_loc_name_last_time != 0) + { play.get_loc_name_last_time = 0; GUIE::MarkSpecialLabelsForUpdate(kLabelMacro_Overhotspot); } - return; + return nullptr; } // on character - if (loctype == LOCTYPE_CHAR) { + if (loctype == LOCTYPE_CHAR) + { onhs = getloctype_index; - snprintf(tempo, MAX_MAXSTRLEN, "%s", get_translation(game.chars2[onhs].name_new.GetCStr())); + out_name = get_translation(game.chars2[onhs].name_new.GetCStr()); if (play.get_loc_name_last_time != 2000+onhs) GUIE::MarkSpecialLabelsForUpdate(kLabelMacro_Overhotspot); play.get_loc_name_last_time = 2000+onhs; - return; + return out_name; } // on object - if (loctype == LOCTYPE_OBJ) { + if (loctype == LOCTYPE_OBJ) + { aa = getloctype_index; - snprintf(tempo, MAX_MAXSTRLEN, "%s", get_translation(croom->obj[aa].name.GetCStr())); + out_name = get_translation(croom->obj[aa].name.GetCStr()); // Compatibility: < 3.1.1 games returned space for nameless object // (presumably was a bug, but fixing it affected certain games behavior) - if (loaded_game_file_version < kGameVersion_311 && tempo[0] == 0) { - tempo[0] = ' '; - tempo[1] = 0; + if (loaded_game_file_version < kGameVersion_311 && out_name[0] == 0) + { + out_name = " "; } if (play.get_loc_name_last_time != 3000+aa) GUIE::MarkSpecialLabelsForUpdate(kLabelMacro_Overhotspot); play.get_loc_name_last_time = 3000+aa; - return; + return out_name; } onhs = getloctype_index; if (onhs>0) - snprintf(tempo, MAX_MAXSTRLEN, "%s", get_translation(croom->hotspot[onhs].Name.GetCStr())); + out_name = get_translation(croom->hotspot[onhs].Name.GetCStr()); if (play.get_loc_name_last_time != onhs) GUIE::MarkSpecialLabelsForUpdate(kLabelMacro_Overhotspot); play.get_loc_name_last_time = onhs; + return out_name; +} + +// GetLocationNameInBuf assumes a string buffer of MAX_MAXSTRLEN +void GetLocationNameInBuf(int x, int y, char *buf) +{ + VALIDATE_STRING(buf); + buf[0] = 0; + + const char *name = GetLocationName(x, y); + if (!name) + return; + + snprintf(buf, MAX_MAXSTRLEN, "%s", name); } int IsKeyPressed (int keycode) { diff --git a/Engine/ac/global_game.h b/Engine/ac/global_game.h index fa5a5329c3..8a0890ccde 100644 --- a/Engine/ac/global_game.h +++ b/Engine/ac/global_game.h @@ -117,8 +117,9 @@ void ShowInputBoxImpl(const char *msg, char *bufr, size_t buf_len); int GetLocationType(int xxx,int yyy); void SaveCursorForLocationChange(); -// GetLocationName assumes a string buffer of MAX_MAXSTRLEN -void GetLocationName(int xxx,int yyy, char *buf); +const char *GetLocationName(int xxx, int yyy); +// GetLocationNameInBuf assumes a string buffer of MAX_MAXSTRLEN +void GetLocationNameInBuf(int xxx,int yyy, char *buf); int IsKeyPressed (int keycode); diff --git a/Engine/ac/gui.cpp b/Engine/ac/gui.cpp index 4faae3af11..f00ec8ec07 100644 --- a/Engine/ac/gui.cpp +++ b/Engine/ac/gui.cpp @@ -528,7 +528,7 @@ void replace_macro_tokens(const char *text, String &fixed_text) { if (!IsInterfaceEnabled()) tempo[0] = 0; else - GetLocationName(game_to_data_coord(mousex), game_to_data_coord(mousey), tempo); + GetLocationNameInBuf(game_to_data_coord(mousex), game_to_data_coord(mousey), tempo); } else { // not a macro, there's just a @ in the message curptr = curptrWasAt + 1; diff --git a/Engine/main/game_run.cpp b/Engine/main/game_run.cpp index 02f3a6faa0..7c266976a8 100644 --- a/Engine/main/game_run.cpp +++ b/Engine/main/game_run.cpp @@ -1062,8 +1062,7 @@ static void UpdateMouseOverLocation() { // Call GetLocationName - it will internally force a GUI refresh // if the result it returns has changed from last time - char tempo[STD_BUFFER_SIZE]; - GetLocationName(game_to_data_coord(mousex), game_to_data_coord(mousey), tempo); + GetLocationName(game_to_data_coord(mousex), game_to_data_coord(mousey)); if ((play.get_loc_name_save_cursor >= 0) && (play.get_loc_name_save_cursor != play.get_loc_name_last_time) &&