From 5a1e38e99b322145cd5d4fff79eef30d40bee65a Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Sat, 19 Jun 2021 15:39:10 -0400 Subject: [PATCH] Add wesnoth.sync module for the synchronization-related commands (#5862) --- .../micro_ais/cas/ca_forest_animals_move.lua | 2 +- .../cas/ca_forest_animals_new_rabbit.lua | 2 +- data/ai/micro_ais/micro_ai_unit_variables.lua | 2 +- .../ai/ca_transport_S6.lua | 2 +- .../lua/game_mechanics/invest/invest.lua | 2 +- .../pickup_confirmation_dialog.lua | 2 +- data/lua/core/_initial.lua | 5 ++++ data/lua/core/wml.lua | 4 ++-- data/lua/wml-tags.lua | 4 ++-- data/lua/wml/message.lua | 2 +- data/modifications/pick_advance/main.lua | 2 +- data/scenario-test.cfg | 2 +- .../break_replay_with_lua_random.cfg | 2 +- data/test/scenarios/recruit_facing.cfg | 2 +- src/scripting/game_lua_kernel.cpp | 23 +++++++++++++++---- 15 files changed, 38 insertions(+), 20 deletions(-) diff --git a/data/ai/micro_ais/cas/ca_forest_animals_move.lua b/data/ai/micro_ais/cas/ca_forest_animals_move.lua index 5b240e1fce28..c63afa276a77 100644 --- a/data/ai/micro_ais/cas/ca_forest_animals_move.lua +++ b/data/ai/micro_ais/cas/ca_forest_animals_move.lua @@ -150,7 +150,7 @@ function ca_forest_animals_move:execution(cfg) -- If this is a rabbit ending on a hole -> disappears if (unit.type == rabbit_type) and hole_map:get(farthest_hex[1], farthest_hex[2]) then - wesnoth.invoke_synced_command("rabbit_despawn", { x = farthest_hex[1], y = farthest_hex[2]}) + wesnoth.sync.invoke_command("rabbit_despawn", { x = farthest_hex[1], y = farthest_hex[2]}) end end diff --git a/data/ai/micro_ais/cas/ca_forest_animals_new_rabbit.lua b/data/ai/micro_ais/cas/ca_forest_animals_new_rabbit.lua index 73337fb62c08..a0465fb39bc5 100644 --- a/data/ai/micro_ais/cas/ca_forest_animals_new_rabbit.lua +++ b/data/ai/micro_ais/cas/ca_forest_animals_new_rabbit.lua @@ -58,7 +58,7 @@ function ca_forest_animals_new_rabbit:execution(cfg) x, y = wesnoth.find_vacant_tile(holes[i].x, holes[i].y) end - wesnoth.invoke_synced_command("rabbit_spawn", { rabbit_type = cfg.rabbit_type, x = x, y = y}) + wesnoth.sync.invoke_command("rabbit_spawn", { rabbit_type = cfg.rabbit_type, x = x, y = y}) end if wesnoth.sides[wesnoth.current.side].shroud then diff --git a/data/ai/micro_ais/micro_ai_unit_variables.lua b/data/ai/micro_ais/micro_ai_unit_variables.lua index aa2021ce20fe..c854351ccb88 100644 --- a/data/ai/micro_ais/micro_ai_unit_variables.lua +++ b/data/ai/micro_ais/micro_ai_unit_variables.lua @@ -26,7 +26,7 @@ function MAI_set_unit_variable(unit, ai_id, value) ai_id = ai_id } if value then table.insert(cfg, { "value", value }) end - wesnoth.invoke_synced_command("MAI_set_unit_variable", cfg) + wesnoth.sync.invoke_command("MAI_set_unit_variable", cfg) end local micro_ai_unit_variables = {} diff --git a/data/campaigns/Son_Of_The_Black_Eye/ai/ca_transport_S6.lua b/data/campaigns/Son_Of_The_Black_Eye/ai/ca_transport_S6.lua index fabc18d68921..7cf08d06937f 100644 --- a/data/campaigns/Son_Of_The_Black_Eye/ai/ca_transport_S6.lua +++ b/data/campaigns/Son_Of_The_Black_Eye/ai/ca_transport_S6.lua @@ -115,7 +115,7 @@ function ca_transport:execution() table.insert(command_data, T.dst { x = best_adj_tiles[i][1], y = best_adj_tiles[i][2]} ) end - wesnoth.invoke_synced_command("ship_unload", command_data) + wesnoth.sync.invoke_command("ship_unload", command_data) return end diff --git a/data/campaigns/World_Conquest/lua/game_mechanics/invest/invest.lua b/data/campaigns/World_Conquest/lua/game_mechanics/invest/invest.lua index 4deccb1d5cbd..69e222315652 100644 --- a/data/campaigns/World_Conquest/lua/game_mechanics/invest/invest.lua +++ b/data/campaigns/World_Conquest/lua/game_mechanics/invest/invest.lua @@ -139,7 +139,7 @@ function wc2_invest.invest() local gold_available = true for i = 1,2 do local is_local = false - local res = wesnoth.synchronize_choice(_"WC2 Invest", function() + local res = wesnoth.sync.evaluate_single(_"WC2 Invest", function() is_local = true return wc2_show_invest_dialog { items_available = items_available, diff --git a/data/campaigns/World_Conquest/lua/game_mechanics/pickup_confirmation_dialog.lua b/data/campaigns/World_Conquest/lua/game_mechanics/pickup_confirmation_dialog.lua index 662251124d04..1f5b64127924 100644 --- a/data/campaigns/World_Conquest/lua/game_mechanics/pickup_confirmation_dialog.lua +++ b/data/campaigns/World_Conquest/lua/game_mechanics/pickup_confirmation_dialog.lua @@ -21,7 +21,7 @@ end -- returns true when the item should be picked up. function pickup_confirmation_dialog.promt_synced(unit, item_image) - local res = wesnoth.synchronize_choice("Item Pickup Choice", function() + local res = wesnoth.sync.evaluate_single("Item Pickup Choice", function() return { take_item = show_dialog(unit, item_image) } end) return res.take_item diff --git a/data/lua/core/_initial.lua b/data/lua/core/_initial.lua index 6cf3ea346664..2c0de46edec6 100644 --- a/data/lua/core/_initial.lua +++ b/data/lua/core/_initial.lua @@ -162,4 +162,9 @@ if wesnoth.kernel_type() == 'Game Lua Kernel' then wesnoth.get_traits = wesnoth.deprecate_api('wesnoth.get_traits', 'wesnoth.game_config.global_traits', 1, nil, function() return wesnoth.game_config.global_traits end) wesnoth.end_level = wesnoth.deprecate_api('wesnoth.end_level', 'wesnoth.scenario.end_level_data assignment', 1, nil, function(cfg) wesnoth.scenario.end_level_data = cfg end) wesnoth.get_end_level_data = wesnoth.deprecate_api('wesnoth.get_end_level_data', 'wesnoth.scenario.end_level_data', 1, nil, function() return wesnoth.scenario.end_level_data end) + + wesnoth.invoke_synced_command = wesnoth.deprecate_api('wesnoth.invoke_synced_command', 'wesnoth.sync.invoke_command', 1, nil, wesnoth.sync.invoke_command) + wesnoth.unsynced = wesnoth.deprecate_api('wesnoth.unsynced', 'wesnoth.sync.run_unsynced', 1, nil, wesnoth.sync.run_unsynced) + wesnoth.synchronize_choice = wesnoth.deprecate_api('wesnoth.synchronize_choice', 'wesnoth.sync.evaluate_single', 1, nil, wesnoth.sync.evaluate_single) + wesnoth.synchronize_choices = wesnoth.deprecate_api('wesnoth.synchronize_choices', 'wesnoth.sync.evaluate_multiple', 1, nil, wesnoth.sync.evaluate_multiple) end diff --git a/data/lua/core/wml.lua b/data/lua/core/wml.lua index f628dbbfad50..38a52c8016ac 100644 --- a/data/lua/core/wml.lua +++ b/data/lua/core/wml.lua @@ -385,7 +385,7 @@ if wesnoth.kernel_type() ~= "Application Lua Kernel" then function global_vars_ns.__index(self, name) local U = wesnoth.require "wml-utils" local var = U.scoped_var(global_temp) - wesnoth.unsynced(function() + wesnoth.sync.run_unsynced(function() wesnoth.wml_actions.get_global_variable { namespace = self[ns_key], to_local = global_temp, @@ -404,7 +404,7 @@ if wesnoth.kernel_type() ~= "Application Lua Kernel" then local U = wesnoth.require "wml-utils" local var = U.scoped_var(global_temp) var:set(val) - wesnoth.unsynced(function() + wesnoth.sync.run_unsynced(function() wesnoth.wml_actions.set_global_variable { namespace = self[ns_key], from_local = global_temp, diff --git a/data/lua/wml-tags.lua b/data/lua/wml-tags.lua index 205b5b4559af..dbbc13d0b29e 100644 --- a/data/lua/wml-tags.lua +++ b/data/lua/wml-tags.lua @@ -20,7 +20,7 @@ That means before loading the WML tags via wesnoth.require "wml". function wml_actions.sync_variable(cfg) local names = cfg.name or wml.error "[sync_variable] missing required name= attribute." - local result = wesnoth.synchronize_choice( + local result = wesnoth.sync.evaluate_single( function() local res = {} for _,name_raw in ipairs(names:split()) do @@ -772,7 +772,7 @@ function wml_actions.print(cfg) end function wml_actions.unsynced(cfg) - wesnoth.unsynced(function () + wesnoth.sync.run_unsynced(function () wml_actions.command(cfg) end) end diff --git a/data/lua/wml/message.lua b/data/lua/wml/message.lua index 4d1e6a4ae2b1..abf7ef84cd44 100644 --- a/data/lua/wml/message.lua +++ b/data/lua/wml/message.lua @@ -434,7 +434,7 @@ function wesnoth.wml_actions.message(cfg) -- 0 means currently playing side. sides_for = 0 end - local choice = wesnoth.synchronize_choice(wait_description, msg_dlg, sides_for) + local choice = wesnoth.sync.evaluate_single(wait_description, msg_dlg, sides_for) option_chosen = tonumber(choice.value) diff --git a/data/modifications/pick_advance/main.lua b/data/modifications/pick_advance/main.lua index b9a0c3e925d7..0b8395f80cdb 100644 --- a/data/modifications/pick_advance/main.lua +++ b/data/modifications/pick_advance/main.lua @@ -125,7 +125,7 @@ function pickadvance.pick_advance(unit) unit = unit or wesnoth.units.get(wml.variables.x1, wml.variables.y1) initialize_unit(unit) local _, orig_options_sanitized = original_advances(unit) - local dialog_result = wesnoth.synchronize_choice(function() + local dialog_result = wesnoth.sync.evaluate_single(function() local local_result = pickadvance.show_dialog_unsynchronized(get_advance_info(unit), unit) return local_result end, function() return { is_ai = true } end) diff --git a/data/scenario-test.cfg b/data/scenario-test.cfg index c980a8fefc48..4434000235a5 100644 --- a/data/scenario-test.cfg +++ b/data/scenario-test.cfg @@ -1328,7 +1328,7 @@ Adjacent own units of lower level will do more damage in battle. When a unit adj local types = { "Ancient Lich", "Ancient Wose", "Elvish Avenger" } local dialog = ... -local result = wesnoth.synchronize_choice(function() +local result = wesnoth.sync.evaluate_single(function() -- Human choice local function preshow(dialog) local function select() diff --git a/data/test/scenarios/break_replay_with_lua_random.cfg b/data/test/scenarios/break_replay_with_lua_random.cfg index 75821a186d16..66718d9fa14c 100644 --- a/data/test/scenarios/break_replay_with_lua_random.cfg +++ b/data/test/scenarios/break_replay_with_lua_random.cfg @@ -78,7 +78,7 @@ {TEST_BREAK_REPLAY "fixed_lua_random_replay_with_sync_choice" ( [lua] code =<< - local result = wesnoth.synchronize_choice( + local result = wesnoth.sync.evaluate_single( function() return { value = math.random(200) } end) diff --git a/data/test/scenarios/recruit_facing.cfg b/data/test/scenarios/recruit_facing.cfg index e795984086a1..b60df1c11045 100644 --- a/data/test/scenarios/recruit_facing.cfg +++ b/data/test/scenarios/recruit_facing.cfg @@ -4,7 +4,7 @@ [lua] code =<< local temp = wml.variables["unit"] - local result = wesnoth.synchronize_choice( + local result = wesnoth.sync.evaluate_single( function() return { value = temp.facing } end) diff --git a/src/scripting/game_lua_kernel.cpp b/src/scripting/game_lua_kernel.cpp index 2683e734b468..6987d84f911d 100644 --- a/src/scripting/game_lua_kernel.cpp +++ b/src/scripting/game_lua_kernel.cpp @@ -2891,7 +2891,9 @@ namespace lua_pushnumber(L, side); if (luaW_pcall(L, 1, 1, false)) { if(!luaW_toconfig(L, -1, cfg)) { - lua_kernel_base::get_lua_kernel(L).log_error("function returned to wesnoth.synchronize_choice a table which was partially invalid"); + static const char* msg = "function returned to wesnoth.sync.[multi_]evaluate a table which was partially invalid"; + lua_kernel_base::get_lua_kernel(L).log_error(msg); + lua_warning(L, msg, false); } } } @@ -4162,9 +4164,7 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports { "add_known_unit", &intf_add_known_unit }, { "get_era", &intf_get_era }, { "get_resource", &intf_get_resource }, - { "invoke_synced_command", &intf_invoke_synced_command }, { "modify_ai", &intf_modify_ai_old }, - { "unsynced", &intf_do_unsynced }, { "add_event_handler", &dispatch<&game_lua_kernel::intf_add_event > }, { "allow_undo", &dispatch<&game_lua_kernel::intf_allow_undo > }, { "cancel_action", &dispatch<&game_lua_kernel::intf_cancel_action > }, @@ -4181,8 +4181,6 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports { "redraw", &dispatch<&game_lua_kernel::intf_redraw > }, { "remove_event_handler", &dispatch<&game_lua_kernel::intf_remove_event > }, { "simulate_combat", &dispatch<&game_lua_kernel::intf_simulate_combat > }, - { "synchronize_choice", &intf_synchronize_choice }, - { "synchronize_choices", &intf_synchronize_choices }, { nullptr, nullptr } };lua_getglobal(L, "wesnoth"); if (!lua_istable(L,-1)) { @@ -4431,6 +4429,21 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports lua_setfield(L, -2, "audio"); lua_pop(L, 1); + // Create the sync module + cmd_log_ << "Adding sync module...\n"; + static luaL_Reg const sync_callbacks[] { + { "invoke_command", &intf_invoke_synced_command }, + { "run_unsynced", &intf_do_unsynced }, + { "evaluate_single", &intf_synchronize_choice }, + { "evaluate_multiple", &intf_synchronize_choices }, + { nullptr, nullptr } + }; + lua_getglobal(L, "wesnoth"); + lua_newtable(L); + luaL_setfuncs(L, sync_callbacks, 0); + lua_setfield(L, -2, "sync"); + lua_pop(L, 1); + // Create the schedule module cmd_log_ << "Adding schedule module...\n"; static luaL_Reg const schedule_callbacks[] {