More aspect exposure.

@Crab_, please, leave feedback on this, I am unsure about several
parts of this patch
This commit is contained in:
Dmitry Kovalenko 2011-06-10 15:17:17 +00:00
parent b892668de8
commit c1a2084259
6 changed files with 106 additions and 30 deletions

View file

@ -247,9 +247,19 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
[aspect]
id=recruitment_pattern
engine=lua
value="'fighter','fighter'"
value=<< {'fighter','scout'} >>
[/aspect]
[aspect]
id=leader_goal
engine=lua
value=<< {x=14, y=10} >>
[/aspect]
[aspect]
id=avoid
engine=lua
value=<< { x = '2-4', y = '23-25'} >>
[/aspect]
version=10710
[engine]
name="lua"
@ -288,21 +298,22 @@ end
function my_ai:do_moves()
wesnoth.message('Passive leader: ', tostring(ai.get_passive_leader()))
wesnoth.message('Passive leader shares keep: ', tostring(ai.get_passive_leader_shares_keep()))
wesnoth.message('Simple targeting: ', tostring(ai.get_simple_targeting()))
wesnoth.message('Scout village targ.: ' , ai.get_scout_village_targeting())
wesnoth.message('Recruit. ignore bc: ' , tostring(ai.get_recruitment_ignore_bad_combat()))
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())
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)
--wesnoth.message('Passive leader: ', tostring(ai.get_passive_leader()))
--wesnoth.message('Passive leader shares keep: ', tostring(ai.get_passive_leader_shares_keep()))
--wesnoth.message('Simple targeting: ', tostring(ai.get_simple_targeting()))
--wesnoth.message('Scout village targ.: ' , ai.get_scout_village_targeting())
--wesnoth.message('Recruit. ignore bc: ' , tostring(ai.get_recruitment_ignore_bad_combat()))
--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())
--local rp = ai.get_recruitment_pattern() -- rp is used like a simple array
--wesnoth.message('pattern'.. rp[1] .. rp[2])
--wesnoth.message('TOOD: ' .. wesnoth.get_time_of_day().id)
wesnoth.message('Leader goal' .. ai.get_leader_goal().x .. ' ' .. ai.get_leader_goal().y) -- fixed
wesnoth.message('Avoid ex. ' .. #ai.get_avoid() .. ' ' ..ai.get_avoid()[1]["x"] .. " " .. ai.get_avoid()[1]["y"]) -- TOFIX
my_leader = wesnoth.get_units({canrecruit = true, side = ai.side})[1]
x,y = ai.suitable_keep(my_leader)

View file

@ -43,9 +43,11 @@
#include "../../play_controller.hpp"
#include "../../resources.hpp"
#include "../../terrain_translation.hpp"
#include "../../terrain_filter.hpp"
#include "../../unit.hpp"
#include "../actions.hpp"
#include "../composite/engine_lua.hpp"
#include <lua/llimits.h>
static lg::log_domain log_ai_engine_lua("ai/engine/lua");
#define LOG_LUA LOG_STREAM(info, log_ai_engine_lua)
@ -263,10 +265,34 @@ static int cfun_ai_get_attack_depth(lua_State *L)
return 1;
}
//static int cfun_ai_get_avoid(lua_State *L)
//{
// return 1;
//}
static int cfun_ai_get_avoid(lua_State *L)
{
std::set<map_location> locs;
terrain_filter avoid = get_readonly_context(L).get_avoid();
avoid.get_locations(locs, true); // is it true here?
int sz = locs.size();
lua_createtable(L, sz, 0); // create a table that we'll use as an array
std::set<map_location>::iterator it = locs.begin();
for (int i = 0; it != locs.end(); ++it, ++i)
{
lua_pushinteger(L, i + 1); // Index for the map location
lua_createtable(L, 2, 0); // Table for a single map location
lua_pushstring(L, "x");
lua_pushinteger(L, it->x);
lua_settable(L, -3);
lua_pushstring(L, "y");
lua_pushinteger(L, it->y);
lua_settable(L, -3);
lua_settable(L, -3);
}
return 1;
}
static int cfun_ai_get_caution(lua_State *L)
{
@ -289,6 +315,13 @@ static int cfun_ai_get_leader_aggression(lua_State *L)
return 1;
}
static int cfun_ai_get_leader_goal(lua_State *L)
{
config goal = get_readonly_context(L).get_leader_goal();
luaW_pushconfig(L, goal);
return 1;
}
static int cfun_ai_get_leader_value(lua_State *L)
{
double leader_value = get_readonly_context(L).get_leader_value();
@ -402,10 +435,12 @@ lua_ai_context* lua_ai_context::create(lua_State *L, char const *code, ai::engin
{ "attack", &cfun_ai_execute_attack },
// Aspects
{ "get_aggression", &cfun_ai_get_aggression },
{ "get_avoid", &cfun_ai_get_avoid },
{ "get_attack_depth", &cfun_ai_get_attack_depth }, // { "get_", &cfun_ai_get_}, little template # TODELETE
{ "get_caution", &cfun_ai_get_caution },
{ "get_grouping", &cfun_ai_get_grouping },
{ "get_leader_aggression", &cfun_ai_get_leader_aggression },
{ "get_leader_goal", &cfun_ai_get_leader_goal },
{ "get_leader_value", &cfun_ai_get_leader_value },
{ "get_number_of_possible_recruits_to_force_recruit", &cfun_ai_get_number_of_possible_recruits_to_force_recruit},
{ "get_passive_leader", &cfun_ai_get_passive_leader },

View file

@ -27,6 +27,10 @@
#include <boost/shared_ptr.hpp>
#include "lua/lualib.h"
#include "../../scripting/lua_api.hpp"
#include "config.hpp"
#include "terrain_filter.hpp"
#include "resources.hpp"
namespace ai {
@ -99,20 +103,38 @@ 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))
int l = lua_objlen(L, n);
for (int i = 1; i < l + 1; ++i)
{
--top; // Don't take nils from the top of the stack
lua_pushinteger(L, i);
lua_gettable(L, n);
std::string s = lua_tostring(L, -1);
lua_settop(L, n);
v->push_back(s);
}
for (int i = n; i <= top; ++i)
{
v->push_back(lua_tostring(L, i));
}
return v;
}
template <>
inline boost::shared_ptr<config> lua_object<config>::to_type(lua_State *L, int n)
{
boost::shared_ptr<config> cfg = boost::shared_ptr<config>(new config());
luaW_toconfig(L, n, *cfg);
return cfg;
}
template <>
inline boost::shared_ptr<terrain_filter> lua_object<terrain_filter>::to_type(lua_State *L, int n)
{
// To Crab_: Is this part ok? I tested it, works fine
boost::shared_ptr<config> cfg = boost::shared_ptr<config>(new config());
boost::shared_ptr<vconfig> vcfg = boost::shared_ptr<vconfig>(new vconfig(*cfg));
luaW_tovconfig(L, n, *vcfg);
boost::shared_ptr<terrain_filter> tf = boost::shared_ptr<terrain_filter>(new terrain_filter(*vcfg, *resources::units));
return tf;
}
} // end of namespace ai

View file

@ -364,6 +364,9 @@ static register_lua_aspect_factory< lua_aspect<double> >
static register_lua_aspect_factory< lua_aspect<int> >
attack_depth__lua_aspect_factory("attack_depth*lua_aspect");
static register_lua_aspect_factory< lua_aspect<terrain_filter> >
avoid__lua_aspect_factory("avoid*lua_aspect");
static register_lua_aspect_factory< lua_aspect<double> >
caution__lua_aspect_factory("caution*lua_aspect");
@ -373,6 +376,9 @@ static register_lua_aspect_factory< lua_aspect<std::string> >
static register_lua_aspect_factory< lua_aspect<double> >
leader_aggression__lua_aspect_factory("leader_aggression*lua_aspect");
static register_lua_aspect_factory< lua_aspect<config> >
leader_goal__lua_aspect_factory("leader_goal*lua_aspect");
static register_lua_aspect_factory< lua_aspect<double> >
leader_value__lua_aspect_factory("leader_value*lua_aspect");

View file

@ -377,7 +377,7 @@ static config luaW_checkconfig(lua_State *L, int index)
* Gets an optional vconfig from either a table or a userdata.
* @return false in case of failure.
*/
static bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg)
bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg)
{
switch (lua_type(L, index))
{

View file

@ -20,12 +20,14 @@
struct lua_State;
class config;
class vconfig;
class unit;
bool luaW_pcall(lua_State *L , int nArgs, int nRets, bool allow_wml_error = false);
unit *luaW_tounit(lua_State *L, int index, bool only_on_map = false);
void luaW_pushconfig(lua_State *L, config const &cfg);
bool luaW_toconfig(lua_State *L, int index, config &cfg, int tstring_meta = 0);
bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg);
/**
* Storage for a unit, either owned by the Lua code (#ptr != 0), on a