Skip to content

Commit

Permalink
Properly port [modify_ai] to Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Dec 11, 2016
1 parent f763655 commit f00e2e1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 29 deletions.
33 changes: 29 additions & 4 deletions data/lua/wml-tags.lua
Expand Up @@ -688,11 +688,36 @@ function wml_actions.store_side(cfg)
end
end

-- This is the port of the old [modify_ai] into lua. It is different from wesnoth.modify_ai in that it uses a standard side filter.
-- I don't know why these functions were made to behave differently, but this seems to be the more powerful and useful one according
-- to mattsc's comments
function wml_actions.modify_ai(cfg)
wesnoth.modify_ai_wml(cfg)
local sides = utils.get_sides(cfg)
local component
if cfg.action == "add" or cfg.action == "change" then
local start = string.find(cfg.path, "[a-z]+%[[a-z0-9_*]+%]$")
local final = string.find(cfg.path, '[', start, true) - 1
local comp_type = string.sub(cfg.path, start, final)
component = helper.get_child(cfg, comp_type)
if component == nil then
helper.wml_error("Missing component definition in [modify_ai]")
end
component = helper.parsed(component)
end
for i = 1, #sides do
if cfg.action == "add" then
wesnoth.add_ai_component(sides[i].side, cfg.path, component)
elseif cfg.action == "delete" or cfg.action == "try_delete" then
wesnoth.delete_ai_component(sides[i].side, cfg.path)
elseif cfg.action == "change" then
local id_start = final + 2
local id_final = string.len(cfg.path) - 1
local id = string.sub(cfg.path, id_start, id_final)
if id == "*" then
helper.wml_error("[modify_ai] can only change one component at a time")
elseif ~component.id and ~id:match("[0-9]+") then
component.id = id
end
wesnoth.change_ai_component(sides[i].side, cfg.path, component)
end
end
end

function wml_actions.add_ai_behavior(cfg)
Expand Down
14 changes: 4 additions & 10 deletions src/ai/manager.cpp
Expand Up @@ -155,30 +155,24 @@ void holder::modify_ai(const config &cfg)
get_ai_ref();
}
const std::string &act = cfg["action"];
LOG_AI_MOD << "side "<< side_ << " [modify_ai] "<<act<<" \""<<cfg["path"]<<"\""<<std::endl;
LOG_AI_MOD << "side "<< side_ << " "<<act<<"_ai_component \""<<cfg["path"]<<"\""<<std::endl;
DBG_AI_MOD << std::endl << cfg << std::endl;
DBG_AI_MOD << "side "<< side_ << " before [modify_ai]"<<std::endl << to_config() << std::endl;
DBG_AI_MOD << "side "<< side_ << " before "<<act<<"_ai_component"<<std::endl << to_config() << std::endl;
bool res = false;
if (act == "add") {
res = component_manager::add_component(&*this->ai_,cfg["path"],cfg);
} else if (act == "change") {
res = component_manager::change_component(&*this->ai_,cfg["path"],cfg);
} else if (act == "delete") {
res = component_manager::delete_component(&*this->ai_,cfg["path"]);
} else if (act == "try_delete") {
res = component_manager::delete_component(&*this->ai_,cfg["path"]);
if (!res) {
LOG_AI_MOD << "[modify_ai] "<<act<<" failed, ignoring because it's a try_delete"<< std::endl;
res = true;
}
} else {
ERR_AI_MOD << "modify_ai tag has invalid 'action' attribute " << act << std::endl;
}
DBG_AI_MOD << "side "<< side_ << " after [modify_ai]"<<act<<std::endl << to_config() << std::endl;
if (!res) {
LOG_AI_MOD << "[modify_ai] "<<act<<" failed"<< std::endl;
LOG_AI_MOD << act << "_ai_component failed"<< std::endl;
} else {
LOG_AI_MOD << "[modify_ai] "<<act<<" success"<< std::endl;
LOG_AI_MOD << act << "_ai_component success"<< std::endl;
}

}
Expand Down
36 changes: 21 additions & 15 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -2817,16 +2817,20 @@ int game_lua_kernel::intf_set_side_id(lua_State *L)
return 0;
}

int game_lua_kernel::intf_modify_ai_wml(lua_State *L)
static int intf_modify_ai(lua_State *L, const char* action)
{
vconfig cfg(luaW_checkvconfig(L, 1));

side_filter ssf(cfg, &game_state_);
std::vector<int> sides = ssf.get_teams();
for (const int &side_num : sides)
{
ai::manager::modify_active_ai_for_side(side_num,cfg.get_parsed_config());
int side_num = luaL_checkinteger(L, 1) - 1;
std::string path = luaL_checkstring(L, 2);
config cfg = config_of("action", action)("path", path);
if(strcmp(action, "delete") == 0) {
ai::manager::modify_active_ai_for_side(side_num, cfg);
return 0;
}
config component = luaW_checkconfig(L, 3);
size_t open_brak = path.find_last_of('[');
size_t dot = path.find_last_of('.');
cfg.add_child(path.substr(dot + 1, open_brak - dot - 1), component);
ai::manager::modify_active_ai_for_side(side_num, cfg);
return 0;
}

Expand Down Expand Up @@ -3342,11 +3346,12 @@ static int intf_get_time_stamp(lua_State *L)
* Lua frontend to the modify_ai functionality
* - Arg 1: config.
*/
static int intf_modify_ai(lua_State *L)
static int intf_modify_ai_old(lua_State *L)
{
config cfg;
luaW_toconfig(L, 1, cfg);
int side = cfg["side"];
WRN_LUA << "wesnoth.modify_ai is deprecated\n";
ai::manager::modify_active_ai_for_side(side, cfg);
return 0;
}
Expand Down Expand Up @@ -3964,7 +3969,7 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "get_time_stamp", &intf_get_time_stamp },
{ "get_traits", &intf_get_traits },
{ "get_viewing_side", &intf_get_viewing_side },
{ "modify_ai", &intf_modify_ai },
{ "modify_ai", &intf_modify_ai_old },
{ "remove_modifications", &intf_remove_modifications },
{ "set_music", &intf_set_music },
{ "transform_unit", &intf_transform_unit },
Expand Down Expand Up @@ -4031,7 +4036,6 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "match_side", &dispatch<&game_lua_kernel::intf_match_side > },
{ "match_unit", &dispatch<&game_lua_kernel::intf_match_unit > },
{ "message", &dispatch<&game_lua_kernel::intf_message > },
{ "modify_ai_wml", &dispatch<&game_lua_kernel::intf_modify_ai_wml > },
{ "open_help", &dispatch<&game_lua_kernel::intf_open_help > },
{ "play_sound", &dispatch<&game_lua_kernel::intf_play_sound > },
{ "print", &dispatch<&game_lua_kernel::intf_print > },
Expand Down Expand Up @@ -4072,16 +4076,18 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "remove_shroud", &dispatch2<&game_lua_kernel::intf_shroud_op, false > },
{ nullptr, nullptr }
};
/*
lua_cpp::Reg const cpp_callbacks[] = {
std::vector<lua_cpp::Reg> const cpp_callbacks = {
{"add_ai_component", std::bind(intf_modify_ai, _1, "add")},
{"delete_ai_component", std::bind(intf_modify_ai, _1, "delete")},
{"change_ai_component", std::bind(intf_modify_ai, _1, "change")},
{nullptr, nullptr}
};
*/
lua_getglobal(L, "wesnoth");
if (!lua_istable(L,-1)) {
lua_newtable(L);
}
luaL_setfuncs(L, callbacks, 0);
//lua_cpp::set_functions(L, cpp_callbacks);
lua_cpp::set_functions(L, cpp_callbacks);
lua_setglobal(L, "wesnoth");

// Create the getside metatable.
Expand Down

0 comments on commit f00e2e1

Please sign in to comment.