From 28718db8c8b1c1899cd4fbccb79621b9105c90e4 Mon Sep 17 00:00:00 2001 From: ZombieFreak115 Date: Wed, 4 Nov 2026 17:45:58 +0100 Subject: [PATCH] Remove the occupied static modifier from being used again Prevent lua state from not being initialized properly if it's in MP or linux Call close_lua on state destruct --- src/gamestate/modifiers.cpp | 3 -- src/gamestate/system_state.cpp | 64 +++++++++++++++++-------------- src/gamestate/system_state.hpp | 4 +- src/gui/gui_modifier_tooltips.cpp | 4 -- 4 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/gamestate/modifiers.cpp b/src/gamestate/modifiers.cpp index d3f629282..e928edfdf 100644 --- a/src/gamestate/modifiers.cpp +++ b/src/gamestate/modifiers.cpp @@ -874,9 +874,6 @@ void recreate_single_land_province_modifiers(sys::state& state, prov_type provin if(state.national_definitions.has_siege) { ve_apply_modifier(state.national_definitions.has_siege, prov_valid_mask && military::province_is_under_siege(state, province)); } - if(state.national_definitions.occupied) { - ve_apply_modifier(state.national_definitions.occupied, prov_valid_mask && province::is_occupied(state, province)); - } if(state.national_definitions.blockaded) { ve_apply_modifier(state.national_definitions.blockaded, prov_valid_mask && military::province_is_blockaded(state, province)); } diff --git a/src/gamestate/system_state.cpp b/src/gamestate/system_state.cpp index cfb8d44fc..daf4760d9 100644 --- a/src/gamestate/system_state.cpp +++ b/src/gamestate/system_state.cpp @@ -54,6 +54,13 @@ state::state() : untrans_key_to_text_sequence(0, text::vector_backed_ci_hash(key state::~state() { quit_signaled.store(true, std::memory_order::release); + // Usually not needed if the game is quit, but if multiple scenarios are being created in the same session, the state destructor might run multiple times. Also just good practice + if(lua_ui_environment) { + lua_close(lua_ui_environment); + } + if(lua_game_loop_environment) { + lua_close(lua_game_loop_environment); + } logger_thread.join(); // wait for logger thread to quit after signalling } @@ -4275,6 +4282,34 @@ void state::on_scenario_load() { ui_state.rebel_flags.resize(world.ideology_size(), 0); + // Lua init. Save lua ui environment for later when UI is available + + lua_alice_api::set_state(this); + lua_alice_api::setup_gameloop_environment(*this); + + + + // populate the table with scripted functions from lua_combined_script (read during scenario generation). Do ui script later when UI is available + { + int status; + status = luaL_dostring(lua_game_loop_environment, lua_combined_script.c_str()); + if(status) { +#ifdef _WIN32 + OutputDebugStringA(lua_tostring(lua_game_loop_environment, -1)); +#endif + lua_settop(lua_game_loop_environment, 0); + std::abort(); + } + status = luaL_dostring(lua_game_loop_environment, lua_game_loop_script.c_str()); + if(status) { +#ifdef _WIN32 + OutputDebugStringA(lua_tostring(lua_game_loop_environment, -1)); +#endif + lua_settop(lua_game_loop_environment, 0); + std::abort(); + } + } + if(network_mode != network_mode_type::single_player) return; @@ -4571,35 +4606,6 @@ void state::on_scenario_load() { dispatch.detach(); - // Lua init. Save lua ui environment for later when UI is available - - lua_alice_api::set_state(this); - lua_alice_api::setup_gameloop_environment(*this); - - - - // populate the table with scripted functions from lua_combined_script (read during scenario generation). Do ui script later when UI is available - { - int status; - status = luaL_dostring(lua_game_loop_environment, lua_combined_script.c_str()); - if(status) { -#ifdef _WIN32 - OutputDebugStringA(lua_tostring(lua_game_loop_environment, -1)); -#endif - lua_settop(lua_game_loop_environment, 0); - std::abort(); - } - status = luaL_dostring(lua_game_loop_environment, lua_game_loop_script.c_str()); - if(status) { -#ifdef _WIN32 - OutputDebugStringA(lua_tostring(lua_game_loop_environment, -1)); -#endif - lua_settop(lua_game_loop_environment, 0); - std::abort(); - } - } - - #endif } diff --git a/src/gamestate/system_state.hpp b/src/gamestate/system_state.hpp index 3d984b465..03a6d55d3 100644 --- a/src/gamestate/system_state.hpp +++ b/src/gamestate/system_state.hpp @@ -795,8 +795,8 @@ struct alignas(64) state { // Lua scripting // - lua_State* lua_ui_environment; - lua_State* lua_game_loop_environment; + lua_State* lua_ui_environment = nullptr; + lua_State* lua_game_loop_environment = nullptr; std::vector lua_on_daily_tick; std::vector lua_on_battle_end; std::vector lua_on_battle_tick; diff --git a/src/gui/gui_modifier_tooltips.cpp b/src/gui/gui_modifier_tooltips.cpp index 56ae5370e..db753ff9d 100644 --- a/src/gui/gui_modifier_tooltips.cpp +++ b/src/gui/gui_modifier_tooltips.cpp @@ -330,10 +330,6 @@ void acting_modifiers_description_province(sys::state& state, text::layout_base& active_single_modifier_description(state, layout, state.national_definitions.has_siege, identation, header, nmid, military::province_is_under_siege(state, p) ? 1.f : 0.f); } - if(state.national_definitions.occupied) { - active_single_modifier_description(state, layout, state.national_definitions.occupied, identation, header, nmid, - province::is_occupied(state, p) ? 1.f : 0.f); - } if(state.national_definitions.blockaded) { active_single_modifier_description(state, layout, state.national_definitions.blockaded, identation, header, nmid, military::province_is_blockaded(state, p) ? 1.f : 0.f);