Skip to content

Commit

Permalink
Engine: separate GetLocationName() and GetLocationNameInBuf()
Browse files Browse the repository at this point in the history
Also fix SaveCursorForLocationChange() was providing buffer of insufficient size to GetLocationName().
  • Loading branch information
ivan-mogilko committed Feb 4, 2025
1 parent 53c6319 commit 494ced3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 45 deletions.
4 changes: 1 addition & 3 deletions Engine/ac/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions Engine/ac/global_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) },
Expand Down
86 changes: 52 additions & 34 deletions Engine/ac/global_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,90 +713,108 @@ 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();
debug_script_log("Saving mouse: mode %d cursor %d", play.restore_cursor_mode_to, play.restore_cursor_image_to);
}
}

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) {
Expand Down
5 changes: 3 additions & 2 deletions Engine/ac/global_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Engine/ac/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions Engine/main/game_run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand Down

0 comments on commit 494ced3

Please sign in to comment.