Adds Add_Tutorial_Text and Remove_Tutorial_Text Lua functions #1

Merged
drew merged 5 commits from tutorial-text into main 2026-03-09 22:52:01 -05:00
Showing only changes of commit 14f02697be - Show all commits

View File

@@ -3,79 +3,55 @@
#include <windows.h> #include <windows.h>
#include <string> #include <string>
// Maximum number of lines held in the feed at once. // Add_Tutorial_Text(key, text [, duration [, r, g, b, a]])
static constexpr int MAX_LINES = 8; // 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
// Keys used as the RawText identifier for each slot, enabling Remove_Tutorial_Text lookups. // before its own null check, so we always pass a valid buffer.
static constexpr float PERPETUAL = -1.0f; static int L_Add_Tutorial_Text(lua_State* L)
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()
{ {
g_head = 0; const char* key = pfn_tostring(L, 1);
g_count = 0; const char* text = pfn_tostring(L, 2);
for (int i = 0; i < MAX_LINES; i++) if (!key || !text)
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)
return 0; return 0;
float duration = PERPETUAL; float duration = -1.0f;
float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
if (pfn_tonumber) if (pfn_tonumber)
{ {
double arg2 = pfn_tonumber(L, 2); double arg3 = pfn_tonumber(L, 3);
if (arg2 != 0.0) if (arg3 != 0.0) duration = (float)arg3;
duration = (float)arg2;
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<wchar_t> parameter.
int wlen = MultiByteToWideChar(CP_ACP, 0, text, -1, nullptr, 0); int wlen = MultiByteToWideChar(CP_ACP, 0, text, -1, nullptr, 0);
std::wstring wtext(wlen - 1, L'\0'); std::wstring wtext(wlen - 1, L'\0');
MultiByteToWideChar(CP_ACP, 0, text, -1, &wtext[0], wlen); MultiByteToWideChar(CP_ACP, 0, text, -1, &wtext[0], wlen);
// If the feed is full, evict the oldest entry. pfn_add_tutorial_text(g_command_bar, &wtext, (char*)key, duration, 0, false, color);
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);
return 0; return 0;
} }
// Clear_Screen_Text() // Remove_Tutorial_Text(key)
// Removes all active FoCAPI-managed screen text entries. static int L_Remove_Tutorial_Text(lua_State* L)
static int L_Clear_Screen_Text(lua_State* L)
{ {
if (!pfn_remove_tutorial_text || !g_command_bar) const char* key = pfn_tostring(L, 1);
if (!key)
return 0; return 0;
for (int i = 0; i < g_count; i++) pfn_remove_tutorial_text(g_command_bar, (char*)key);
{
int slot = (g_head + i) % MAX_LINES;
pfn_remove_tutorial_text(g_command_bar, g_keys[slot]);
}
g_head = 0;
g_count = 0;
return 0; return 0;
} }
@@ -85,8 +61,6 @@ void Register_ScreenText(lua_State* L)
if (!pfn_add_tutorial_text || !pfn_remove_tutorial_text || !g_command_bar) if (!pfn_add_tutorial_text || !pfn_remove_tutorial_text || !g_command_bar)
return; return;
reset_feed(); register_global(L, "Add_Tutorial_Text", L_Add_Tutorial_Text);
register_global(L, "Remove_Tutorial_Text", L_Remove_Tutorial_Text);
register_global(L, "Print_Screen_Text", L_Print_Screen_Text);
register_global(L, "Clear_Screen_Text", L_Clear_Screen_Text);
} }