Skip to content

Commit

Permalink
Cleanup of fef953a
Browse files Browse the repository at this point in the history
- Fix the rabbit AI
- Enable invoke_synced_command to also call (some) built-in commands
  and give an error message in the case of an unknown command
- Remove some unnecessary implementation details
  • Loading branch information
CelticMinstrel committed Mar 17, 2018
1 parent 81d965c commit a9c757b
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 43 deletions.
1 change: 0 additions & 1 deletion data/_main.cfg
Expand Up @@ -60,7 +60,6 @@
{scenario-story.cfg}
{ai/scenarios/}
{ai/micro_ais/scenarios/}
{ai/utils/custom_command.cfg}
#define DONT_RELOAD_CORE
#enddef

Expand Down
30 changes: 30 additions & 0 deletions data/ai/micro_ais/mai-defs/animals.lua
Expand Up @@ -82,6 +82,9 @@ function wesnoth.micro_ais.herding(cfg)
return required_keys, optional_keys, CA_parms
end

local rabbit_registry_counter = 0;
local save_rabbit_spawn, save_rabbit_despawn

function wesnoth.micro_ais.forest_animals(cfg)
local optional_keys = { "rabbit_type", "rabbit_number", "rabbit_enemy_distance", "rabbit_hole_img",
"tusker_type", "tusklet_type", "deer_type", "[filter_location]"
Expand All @@ -94,6 +97,33 @@ function wesnoth.micro_ais.forest_animals(cfg)
{ ca_id = "move", location = 'ca_forest_animals_move.lua', score = score - 2 },
{ ca_id = "tusklet_move", location = 'ca_forest_animals_tusklet_move.lua', score = score - 3 }
}

-- Register custom synced commands for the rabbit AI
if cfg.action == "delete" then
rabbit_registry_counter = rabbit_registry_counter - 1
if rabbit_registry_counter == 0 then
wesnoth.custom_synced_commands.rabbit_spawn = save_rabbit_spawn
wesnoth.custom_synced_commands.rabbit_despawn = save_rabbit_despawn
end
else
if rabbit_registry_counter == 0 then
save_rabbit_spawn = wesnoth.custom_synced_commands.rabbit_spawn
save_rabbit_despawn = wesnoth.custom_synced_commands.rabbit_despawn
end

rabbit_registry_counter = rabbit_registry_counter + 1

function wesnoth.custom_synced_commands.rabbit_despawn(cfg)
--TODO: maybe we only want to allow erasing of unit of certain types/sides/locations?
wesnoth.erase_unit(cfg.x, cfg.y)
end

function wesnoth.custom_synced_commands.rabbit_spawn(cfg)
--TODO: maybe we only want to allow creation of unit of certain types/sides/locations?
wesnoth.put_unit({ side = wesnoth.current.side, type = cfg.rabbit_type}, cfg.x, cfg.y)
end
end

return {}, optional_keys, CA_parms
end

Expand Down
18 changes: 0 additions & 18 deletions data/ai/utils/custom_command.cfg

This file was deleted.

1 change: 0 additions & 1 deletion data/core/_main.cfg
Expand Up @@ -8,7 +8,6 @@
wesnoth.dofile 'lua/backwards-compatibility.lua'
wesnoth.dofile 'lua/wml-tags.lua'
wesnoth.dofile 'lua/feeding.lua'
wesnoth.dofile 'lua/custom_command.lua'
>>
[/lua]

Expand Down
19 changes: 0 additions & 19 deletions data/lua/custom_command.lua

This file was deleted.

41 changes: 39 additions & 2 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -80,6 +80,7 @@
#include "scripting/lua_team.hpp"
#include "scripting/lua_unit_type.hpp"
#include "scripting/push_check.hpp"
#include "synced_commands.hpp"
#include "color.hpp" // for surface
#include "sdl/surface.hpp" // for surface
#include "side_filter.hpp" // for side_filter
Expand Down Expand Up @@ -3946,6 +3947,33 @@ int game_lua_kernel::intf_toggle_fog(lua_State *L, const bool clear)
return 0;
}

// Invokes a synced command
int intf_invoke_synced_command(lua_State* L)
{
const std::string name = luaL_checkstring(L, 1);
auto it = synced_command::registry().find(name);
config cmd;
if(it == synced_command::registry().end()) {
// Custom command
if(!luaW_getglobal(L, "wesnoth", "custom_synced_commands", name)) {
return luaL_argerror(L, 1, "Unknown synced command");
}
config& cmd_tag = cmd.child_or_add("custom_command");
cmd_tag["name"] = name;
if(!lua_isnoneornil(L, 2)) {
cmd_tag.add_child("data", luaW_checkconfig(L, 2));
}
} else {
// Built-in command
cmd.add_child(name, luaW_checkconfig(L, 2));
}
// Now just forward to the WML action.
luaW_getglobal(L, "wesnoth", "wml_actions", "do_command");
luaW_pushconfig(L, cmd);
luaW_pcall(L, 1, 0);
return 0;
}

// END CALLBACK IMPLEMENTATION

game_board & game_lua_kernel::board() {
Expand Down Expand Up @@ -4007,6 +4035,7 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "get_era", &intf_get_era },
{ "get_traits", &intf_get_traits },
{ "get_viewing_side", &intf_get_viewing_side },
{ "invoke_synced_command", &intf_invoke_synced_command },
{ "modify_ai", &intf_modify_ai_old },
{ "remove_modifications", &intf_remove_modifications },
{ "set_music", &intf_set_music },
Expand Down Expand Up @@ -4207,6 +4236,14 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
lua_setfield(L, -2, "effects");
lua_pop(L, 1);

// Create the custom_synced_commands table.
cmd_log_ << "Adding custom_synced_commands table...\n";

lua_getglobal(L, "wesnoth");
lua_newtable(L);
lua_setfield(L, -2, "custom_synced_commands");
lua_pop(L, 1);

// Create the game_events table.
cmd_log_ << "Adding game_events table...\n";

Expand Down Expand Up @@ -4395,11 +4432,11 @@ bool game_lua_kernel::run_event(const game_events::queued_event& ev)
return true;
}

void game_lua_kernel::custom_command(const config& cfg)
void game_lua_kernel::custom_command(const std::string& name, const config& cfg)
{
lua_State *L = mState;

if (!luaW_getglobal(L, "wesnoth", "game_events", "on_synced_command")) {
if (!luaW_getglobal(L, "wesnoth", "custom_synced_commands", name)) {
return;
}
luaW_pushconfig(L, cfg);
Expand Down
2 changes: 1 addition & 1 deletion src/scripting/game_lua_kernel.hpp
Expand Up @@ -200,7 +200,7 @@ class game_lua_kernel : public lua_kernel_base
void save_game(config & level);
void load_game(const config& level);
bool run_event(const game_events::queued_event&);
void custom_command(const config&);
void custom_command(const std::string&, const config&);
void push_builtin_effect();
void set_wml_action(const std::string&, game_events::wml_action::handler);
void set_wml_condition(const std::string&, bool(*)(const vconfig&));
Expand Down
2 changes: 1 addition & 1 deletion src/synced_commands.cpp
Expand Up @@ -347,7 +347,7 @@ SYNCED_COMMAND_HANDLER_FUNCTION(fire_event, child, use_undo, /*show*/, /*error_
SYNCED_COMMAND_HANDLER_FUNCTION(custom_command, child, /*use_undo*/, /*show*/, /*error_handler*/)
{
assert(resources::lua_kernel);
resources::lua_kernel->custom_command(child);
resources::lua_kernel->custom_command(child["name"], child.child_or_empty("data"));
return true;
}

Expand Down

5 comments on commit a9c757b

@CelticMinstrel
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gfgtdf - Any thoughts on this?

@gfgtdf
Copy link
Contributor

@gfgtdf gfgtdf commented on a9c757b Mar 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well i don't really see the advantage of moving the handling of custom_synced_commands from lua to the c++ side, but i also don't have problems with it.

For the change in animals.lua: I don't know exactly when that function is called but you if know that it is called for all clients even those that don't control that ai side (including observers and replays) then it's a good change.

@CelticMinstrel
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That function is called when the [micro_ai] tag is executed to enable or disable that AI.

@gfgtdf
Copy link
Contributor

@gfgtdf gfgtdf commented on a9c757b Mar 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and when the game is saved and reloaded it is then called in the initilisation of that side in all clients?

@CelticMinstrel
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that's a good point. I'll check if the rabbit AI is broken in that event.

Please sign in to comment.