Exposed recruitment pattern aspect to Lua

This commit is contained in:
Dmitry Kovalenko 2011-06-08 10:09:29 +00:00
parent 0b2150e400
commit c1ea8ac520
5 changed files with 53 additions and 8 deletions

View file

@ -244,6 +244,12 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
engine=lua
value="'defensive'"
[/aspect]
[aspect]
id=recruitment_pattern
engine=lua
value="'fighter','fighter'"
[/aspect]
version=10710
[engine]
name="lua"
@ -291,10 +297,12 @@ function my_ai:do_moves()
wesnoth.message('Recruit. ignore bm: ' , tostring(ai.get_recruitment_ignore_bad_movement()))
wesnoth.message('nofprtfr: ' , ai.get_number_of_possible_recruits_to_force_recruit())
wesnoth.message('Grouping: ' .. ai.get_grouping())
wesnoth.message('Support villages: ' .. tostring(ai.get_support_villages()))
wesnoth.message('Village value: ' .. ai.get_village_value())
wesnoth.message('Villages per scout: ' .. ai.get_villages_per_scout())
wesnoth.message('TOOD: ' .. wesnoth.get_time_of_day().id)
wesnoth.message('Support villages: ' .. tostring(ai.get_support_villages()))
wesnoth.message('Village value: ' .. ai.get_village_value())
wesnoth.message('Villages per scout: ' .. ai.get_villages_per_scout())
local rp = ai.get_recruitment_pattern() -- rp is used like a simple array
wesnoth.message('pattern'.. #rp)
wesnoth.message('TOOD: ' .. wesnoth.get_time_of_day().id)
my_leader = wesnoth.get_units({canrecruit = true, side = ai.side})[1]
x,y = ai.suitable_keep(my_leader)

View file

@ -410,7 +410,7 @@ public:
{
value = "true";
}
value = "return (" + value + ")";
value = "return " + value;
}
else if (cfg.has_attribute("code"))
{

View file

@ -331,6 +331,20 @@ static int cfun_ai_get_recruitment_ignore_bad_movement(lua_State *L)
return 1;
}
static int cfun_ai_get_recruitment_pattern(lua_State *L)
{
std::vector<std::string> recruiting = get_readonly_context(L).get_recruitment_pattern();
int size = recruiting.size();
lua_createtable(L, size, 0); // create an exmpty table with predefined size
for (int i = 0; i < size; ++i)
{
lua_pushinteger(L, i + 1); // Indexing in Lua starts from 1
lua_pushstring(L, recruiting[i].c_str());
lua_settable(L, -3);
}
return 1;
}
static int cfun_ai_get_scout_village_targeting(lua_State *L)
{
double scout_village_targeting = get_readonly_context(L).get_scout_village_targeting();
@ -398,6 +412,7 @@ lua_ai_context* lua_ai_context::create(lua_State *L, char const *code, ai::engin
{ "get_passive_leader_shares_keep", &cfun_ai_get_passive_leader_shares_keep},
{ "get_recruitment_ignore_bad_combat", &cfun_ai_get_recruitment_ignore_bad_combat},
{ "get_recruitment_ignore_bad_movement", &cfun_ai_get_recruitment_ignore_bad_movement},
{ "get_recruitment_pattern", &cfun_ai_get_recruitment_pattern },
{ "get_scout_village_targeting", &cfun_ai_get_scout_village_targeting },
{ "get_simple_targeting", &cfun_ai_get_simple_targeting },
{ "get_support_villages", &cfun_ai_get_support_villages },
@ -498,9 +513,9 @@ void lua_ai_action_handler::handle(config &cfg, bool configOut, lua_object_ptr l
luaW_pushconfig(L, cfg);
luaW_pcall(L, 2, 0, true);
}
else if (luaW_pcall(L, 1, 2, true))
{
l_obj->store(L, initial_top + 1);
else if (luaW_pcall(L, 1, 5, true)) // @note for Crab: how much nrets should we actually have here
{ // there were 2 initially, but aspects like recruitment pattern
l_obj->store(L, initial_top + 1); // return a lot of results
}
lua_settop(L, initial_top);//empty stack

View file

@ -23,6 +23,7 @@
#define LUA_OBJECT_HPP_INCLUDED
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include "lua/lualib.h"
@ -94,6 +95,24 @@ inline boost::shared_ptr<int> lua_object<int>::to_type(lua_State *L, int n)
return boost::shared_ptr<int>(new int(lua_tonumber(L, n)));
}
template <>
inline boost::shared_ptr< std::vector<std::string> > lua_object< std::vector<std::string> >::to_type(lua_State *L, int n)
{
boost::shared_ptr< std::vector<std::string> > v = boost::shared_ptr< std::vector<std::string> >(new std::vector<std::string>());
int top = lua_gettop(L);
while (lua_isnil(L, top))
{
--top; // Don't take nils from the top of the stack
}
for (int i = n; i <= top; ++i)
{
v->push_back(lua_tostring(L, i));
}
return v;
}
} // end of namespace ai

View file

@ -406,6 +406,9 @@ static register_lua_aspect_factory< lua_aspect<double> >
static register_lua_aspect_factory< lua_aspect<int> >
villages_per_scout__lua_aspect_factory("villages_per_scout*lua_aspect");
static register_lua_aspect_factory< lua_aspect< std::vector<std::string> > >
recruitment_pattern__lua_aspect_factory("recruitment_pattern*lua_aspect");
void registry::init()
{