Properly port [modify_ai] to Lua
This commit is contained in:
parent
e0d07e854e
commit
37368c3654
3 changed files with 55 additions and 30 deletions
|
@ -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, final
|
||||
if cfg.action == "add" or cfg.action == "change" then
|
||||
local start = string.find(cfg.path, "[a-z_]+%[[a-z0-9_*]*%]$")
|
||||
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 not component.id and not 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)
|
||||
|
|
|
@ -155,9 +155,9 @@ 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);
|
||||
|
@ -165,20 +165,14 @@ void holder::modify_ai(const config &cfg)
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2820,16 +2820,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);
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -3345,11 +3349,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;
|
||||
}
|
||||
|
@ -3967,7 +3972,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 },
|
||||
|
@ -4033,7 +4038,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 > },
|
||||
|
@ -4074,15 +4078,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);
|
||||
|
||||
if(play_controller_.get_classification().campaign_type == game_classification::CAMPAIGN_TYPE::TEST) {
|
||||
static luaL_Reg const test_callbacks[] = {
|
||||
|
@ -4091,8 +4098,7 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
|
|||
};
|
||||
luaL_setfuncs(L, test_callbacks , 0);
|
||||
}
|
||||
|
||||
//lua_cpp::set_functions(L, cpp_callbacks);
|
||||
|
||||
lua_setglobal(L, "wesnoth");
|
||||
|
||||
// Create the getside metatable.
|
||||
|
|
Loading…
Add table
Reference in a new issue