From a2dbc88d969a0b7746706d1c82ff4328cd07732b Mon Sep 17 00:00:00 2001 From: alice <58637860+alicealys@users.noreply.github.com> Date: Mon, 2 Sep 2024 16:05:28 +0200 Subject: [PATCH] Add g_soldierPronouns dvar --- data/cdata/scripts/names.gsc | 69 ++++++++++++++++--- data/cdata/ui_scripts/settings/settings.lua | 65 ++++++++++------- data/zonetool/localizedstrings/english.json | 2 + src/client/component/gsc/script_extension.cpp | 11 +++ src/client/component/patches.cpp | 2 + 5 files changed, 116 insertions(+), 33 deletions(-) diff --git a/data/cdata/scripts/names.gsc b/data/cdata/scripts/names.gsc index b5d86616..3dc2f115 100644 --- a/data/cdata/scripts/names.gsc +++ b/data/cdata/scripts/names.gsc @@ -3,6 +3,14 @@ main() level.__pronouns__ = spawnstruct(); level.__pronouns__.pronouns_first = []; level.__pronouns__.pronouns_second = []; + level.__pronouns__.rand_iter = 1; + level.__pronouns__.enabled = getdvarint("g_soldierPronouns"); + + rand_iter_dvar = getdvarint("g_soldierPronounsRandIter"); + if (rand_iter_dvar != 0) + { + level.__pronouns__.rand_iter = rand_iter_dvar; + } add_first_pronouns("she"); add_first_pronouns("they"); @@ -27,11 +35,42 @@ add_second_pronouns(text) level.__pronouns__.pronouns_second[level.__pronouns__.pronouns_second.size] = text; } -get_random_pronouns() +random_value(rand, min, max) { - combined = randomintrange(0, 2) == 1; + return rand % ((max - 1) + 1 - min) + min; +} - index_first = randomintrange(0, level.__pronouns__.pronouns_first.size); +absolute_value(val) +{ + if (val < 0) + { + return val * -1; + } + + return val; +} + +next_rand(rand) +{ + return absolute_value(214013 * rand + 2531011); +} + +get_random_pronouns(soldier_name) +{ + hash = hashstring(soldier_name); + rand = absolute_value(hash); + + for (i = 0; i < level.__pronouns__.rand_iter; i++) + { + rand = next_rand(rand); + } + + rand_1 = next_rand(rand); + rand_2 = next_rand(rand_1); + rand_3 = next_rand(rand_2); + + combined = random_value(rand_1, 0, 2) == 1; + index_first = random_value(rand_2, 0, level.__pronouns__.pronouns_first.size); pronouns_first = level.__pronouns__.pronouns_first[index_first]; pronouns_second = level.__pronouns__.pronouns_second[index_first]; @@ -39,9 +78,11 @@ get_random_pronouns() if (combined) { index_second = index_first; - while (index_second == index_first) + tries = 0; + while (index_second == index_first && tries < 25) { - index_second = randomintrange(0, level.__pronouns__.pronouns_first.size); + index_second = random_value(rand_3, 0, level.__pronouns__.pronouns_first.size); + ++tries; } pronouns_second = level.__pronouns__.pronouns_first[index_second]; @@ -50,10 +91,18 @@ get_random_pronouns() return pronouns_first + "/" + pronouns_second; } -get_name(var_0) +format_name(soldier_name) { - pronouns = " (" + get_random_pronouns() + ")"; + if (!level.__pronouns__.enabled) + { + return soldier_name; + } + + return soldier_name + " (" + get_random_pronouns(soldier_name) + ")"; +} +get_name(var_0) +{ if ( isdefined( self.team ) && self.team == "neutral" ) return; @@ -68,13 +117,13 @@ get_name(var_0) if ( self.script_friendname == "none" ) return; - self.name = self.script_friendname + pronouns; - maps\_names::getrankfromname( self.name ); + self.name = format_name(self.script_friendname); + maps\_names::getrankfromname(); self notify( "set name and rank" ); return; } maps\_names::get_name_for_nationality( self.voice ); - self.name = self.name + pronouns; + self.name = format_name(self.name); self notify( "set name and rank" ); } diff --git a/data/cdata/ui_scripts/settings/settings.lua b/data/cdata/ui_scripts/settings/settings.lua index 845f2e44..014c16b3 100644 --- a/data/cdata/ui_scripts/settings/settings.lua +++ b/data/cdata/ui_scripts/settings/settings.lua @@ -1,21 +1,21 @@ function createdivider(menu, text) - local element = LUI.UIElement.new({ - leftAnchor = true, - rightAnchor = true, - left = 0, - right = 0, - topAnchor = true, - bottomAnchor = false, - top = 0, - bottom = 33.33 - }) - - element.scrollingToNext = true - element:addElement(LUI.MenuBuilder.BuildRegisteredType("h1_option_menu_titlebar", { - title_bar_text = Engine.ToUpperCase(Engine.Localize(text)) - })) - - menu.list:addElement(element) + local element = LUI.UIElement.new({ + leftAnchor = true, + rightAnchor = true, + left = 0, + right = 0, + topAnchor = true, + bottomAnchor = false, + top = 0, + bottom = 33.33 + }) + + element.scrollingToNext = true + element:addElement(LUI.MenuBuilder.BuildRegisteredType("h1_option_menu_titlebar", { + title_bar_text = Engine.ToUpperCase(Engine.Localize(text)) + })) + + menu.list:addElement(element) end LUI.addmenubutton("pc_controls", { @@ -29,10 +29,10 @@ LUI.addmenubutton("pc_controls", { LUI.MenuBuilder.m_types_build["settings_menu"] = function(a1) local menu = LUI.MenuTemplate.new(a1, { - menu_title = "@MENU_GENERAL", - menu_list_divider_top_offset = -(LUI.H1MenuTab.tabChangeHoldingElementHeight + H1MenuDims.spacing), - menu_width = GenericMenuDims.OptionMenuWidth - }) + menu_title = "@MENU_GENERAL", + menu_list_divider_top_offset = -(LUI.H1MenuTab.tabChangeHoldingElementHeight + H1MenuDims.spacing), + menu_width = GenericMenuDims.OptionMenuWidth + }) createdivider(menu, "@LUA_MENU_UPDATES") @@ -208,8 +208,27 @@ LUI.MenuBuilder.m_types_build["settings_menu"] = function(a1) } ) - LUI.Options.InitScrollingList(menu.list, nil) - LUI.Options.AddOptionTextInfo(menu) + createdivider(menu, "@PLATFORM_UI_MISCELLANEOUS_OPTIONS") + + LUI.Options.CreateOptionButton( + menu, + "g_soldierPronouns", + "@LUA_MENU_SOLDIER_PRONOUNS", + "@LUA_MENU_SOLDIER_PRONOUNS_DESC", + { + { + text = "@LUA_MENU_DISABLED", + value = false + }, + { + text = "@LUA_MENU_ENABLED", + value = true + }, + } + ) + + LUI.Options.InitScrollingList(menu.list, nil) + LUI.Options.AddOptionTextInfo(menu) menu:AddBackButton() diff --git a/data/zonetool/localizedstrings/english.json b/data/zonetool/localizedstrings/english.json index 6c8399f0..7add452a 100644 --- a/data/zonetool/localizedstrings/english.json +++ b/data/zonetool/localizedstrings/english.json @@ -68,6 +68,8 @@ "LUA_MENU_SWITCH_TO_DEVELOP": "Switch to the ^3Develop^7 branch", "LUA_MENU_SWITCH_TO_MAIN": "Switch to the ^3Main^7 branch", "LUA_MENU_SWITCH_BRANCH_DESC": "Switch the h2-mod branch (develop: latest changes, main: latest stable release).", + "LUA_MENU_SOLDIER_PRONOUNS": "Soldier pronouns", + "LUA_MENU_SOLDIER_PRONOUNS_DESC": "Show random soldier pronouns.", "MENU_MUSIC_VOLUME": "Music Volume", "MENU_MUSIC_VOLUME_DESC": "Move the slider to adjust the volume of the music.", diff --git a/src/client/component/gsc/script_extension.cpp b/src/client/component/gsc/script_extension.cpp index 2c38edb3..04acb8dc 100644 --- a/src/client/component/gsc/script_extension.cpp +++ b/src/client/component/gsc/script_extension.cpp @@ -616,6 +616,17 @@ namespace gsc !game::DB_IsXAssetDefault(type_index, name); game::Scr_AddInt(result); }); + + add_function("hashstring", []() + { + if (game::Scr_GetNumParam() < 1) + { + return game::Scr_AddInt(0); + } + + const auto hash = game::generateHashValue(game::Scr_GetString(0)); + game::Scr_AddInt(hash); + }); } }; } diff --git a/src/client/component/patches.cpp b/src/client/component/patches.cpp index 2d89ede2..45d1a537 100644 --- a/src/client/component/patches.cpp +++ b/src/client/component/patches.cpp @@ -112,6 +112,8 @@ namespace patches utils::hook::set(0x140948F48, 1.f); utils::hook::nop(0x14037B8AE, 7); + + dvars::register_bool("g_soldierPronouns", false, game::DVAR_FLAG_SAVED, "Show soldier pronouns"); } }; }