Skip to content

Commit

Permalink
Add wesnoth.sync module for the synchronization-related commands (#5862)
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Jun 19, 2021
1 parent 8ff364f commit 5a1e38e
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_forest_animals_move.lua
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_forest_animals_new_rabbit.lua
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/micro_ai_unit_variables.lua
Expand Up @@ -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 = {}
Expand Down
2 changes: 1 addition & 1 deletion data/campaigns/Son_Of_The_Black_Eye/ai/ca_transport_S6.lua
Expand Up @@ -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
Expand Down
Expand Up @@ -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,
Expand Down
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions data/lua/core/_initial.lua
Expand Up @@ -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
4 changes: 2 additions & 2 deletions data/lua/core/wml.lua
Expand Up @@ -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 <close> = 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,
Expand All @@ -404,7 +404,7 @@ if wesnoth.kernel_type() ~= "Application Lua Kernel" then
local U = wesnoth.require "wml-utils"
local var <close> = 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,
Expand Down
4 changes: 2 additions & 2 deletions data/lua/wml-tags.lua
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion data/lua/wml/message.lua
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion data/modifications/pick_advance/main.lua
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion data/scenario-test.cfg
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion data/test/scenarios/break_replay_with_lua_random.cfg
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion data/test/scenarios/recruit_facing.cfg
Expand Up @@ -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)
Expand Down
23 changes: 18 additions & 5 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -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<game_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<game_lua_kernel>(L).log_error(msg);
lua_warning(L, msg, false);
}
}
}
Expand Down Expand Up @@ -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 > },
Expand All @@ -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)) {
Expand Down Expand Up @@ -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[] {
Expand Down

0 comments on commit 5a1e38e

Please sign in to comment.