diff --git a/functions/screen_text.cpp b/functions/screen_text.cpp index 459840a..0e75de7 100644 --- a/functions/screen_text.cpp +++ b/functions/screen_text.cpp @@ -3,79 +3,55 @@ #include #include -// Maximum number of lines held in the feed at once. -static constexpr int MAX_LINES = 8; - -// Keys used as the RawText identifier for each slot, enabling Remove_Tutorial_Text lookups. -static constexpr float PERPETUAL = -1.0f; - -static char g_keys[MAX_LINES][16]; // "FOCA_0" .. "FOCA_7" -static int g_head = 0; // index of oldest active slot -static int g_count = 0; // number of active slots - -static void reset_feed() +// Add_Tutorial_Text(key, text [, duration [, r, g, b, a]]) +// duration defaults to -1 (perpetual). r/g/b/a default to 1.0 (white, opaque). +// The color pointer must be non-null: the game copies 16 bytes from it unconditionally +// before its own null check, so we always pass a valid buffer. +static int L_Add_Tutorial_Text(lua_State* L) { - g_head = 0; - g_count = 0; - for (int i = 0; i < MAX_LINES; i++) - wsprintfA(g_keys[i], "FOCA_%d", i); -} - -// Print_Screen_Text(text [, duration]) -// Appends a line to the bottom of the tutorial text feed. -// duration defaults to -1 (perpetual). Pass a positive number of seconds to auto-expire. -static int L_Print_Screen_Text(lua_State* L) -{ - const char* text = pfn_tostring(L, 1); - if (!text || !pfn_add_tutorial_text || !g_command_bar) + const char* key = pfn_tostring(L, 1); + const char* text = pfn_tostring(L, 2); + if (!key || !text) return 0; - float duration = PERPETUAL; + float duration = -1.0f; + float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; + if (pfn_tonumber) { - double arg2 = pfn_tonumber(L, 2); - if (arg2 != 0.0) - duration = (float)arg2; + double arg3 = pfn_tonumber(L, 3); + if (arg3 != 0.0) duration = (float)arg3; + + double r = pfn_tonumber(L, 4); + double g = pfn_tonumber(L, 5); + double b = pfn_tonumber(L, 6); + double a = pfn_tonumber(L, 7); + if (r != 0.0 || g != 0.0 || b != 0.0 || a != 0.0) + { + color[0] = (float)r; + color[1] = (float)g; + color[2] = (float)b; + color[3] = (float)a; + } } - // Widen to wchar_t for the game's basic_string parameter. int wlen = MultiByteToWideChar(CP_ACP, 0, text, -1, nullptr, 0); std::wstring wtext(wlen - 1, L'\0'); MultiByteToWideChar(CP_ACP, 0, text, -1, &wtext[0], wlen); - // If the feed is full, evict the oldest entry. - if (g_count >= MAX_LINES) - { - pfn_remove_tutorial_text(g_command_bar, g_keys[g_head]); - g_head = (g_head + 1) % MAX_LINES; - g_count--; - } - - int slot = (g_head + g_count) % MAX_LINES; - g_count++; - - // Inner overload: (this, wstring*, key, duration, index, teletype, color) - // index=0 so StartTime is set immediately; teletype=false for instant display; color=nullptr for default. - pfn_add_tutorial_text(g_command_bar, &wtext, g_keys[slot], duration, 0, false, nullptr); + pfn_add_tutorial_text(g_command_bar, &wtext, (char*)key, duration, 0, false, color); return 0; } -// Clear_Screen_Text() -// Removes all active FoCAPI-managed screen text entries. -static int L_Clear_Screen_Text(lua_State* L) +// Remove_Tutorial_Text(key) +static int L_Remove_Tutorial_Text(lua_State* L) { - if (!pfn_remove_tutorial_text || !g_command_bar) + const char* key = pfn_tostring(L, 1); + if (!key) return 0; - for (int i = 0; i < g_count; i++) - { - int slot = (g_head + i) % MAX_LINES; - pfn_remove_tutorial_text(g_command_bar, g_keys[slot]); - } - - g_head = 0; - g_count = 0; + pfn_remove_tutorial_text(g_command_bar, (char*)key); return 0; } @@ -85,8 +61,6 @@ void Register_ScreenText(lua_State* L) if (!pfn_add_tutorial_text || !pfn_remove_tutorial_text || !g_command_bar) return; - reset_feed(); - - register_global(L, "Print_Screen_Text", L_Print_Screen_Text); - register_global(L, "Clear_Screen_Text", L_Clear_Screen_Text); + register_global(L, "Add_Tutorial_Text", L_Add_Tutorial_Text); + register_global(L, "Remove_Tutorial_Text", L_Remove_Tutorial_Text); }