Exposed the target list to Lua // minor fixes

This commit is contained in:
Dmitry Kovalenko 2011-08-05 13:25:35 +00:00
parent 8593049f5d
commit 8dc1551d30
8 changed files with 88 additions and 26 deletions

View file

@ -282,11 +282,19 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
[ai]
version=10710
[aspect]
id=aggression
engine=lua
code = << return 0.23 >>
[/aspect]
[goal]
name=protect_location
value=5
protect_radius=16
[criteria] #NOTE: this is a SLF, because we're protecting a location
x,y=42,20
[/criteria]
[/goal]
[aspect]
id=aggression
engine=lua
code = << return 0.23 >>
[/aspect]
[engine]
name="lua"
code= <<
@ -304,7 +312,9 @@ ai_stdlib.init(ai)
function my_ai:stage_hello()
wesnoth.message('hello from stage!')
wesnoth.message('PERSISTANCE: ' .. tostring(self.data["pers"]) .. self.data["stringg"])
--wesnoth.message('PERSISTANCE: ' .. tostring(self.data["pers"]) .. self.data["stringg"])
local tg = ai.get_targets()
wesnoth.message(tg[3].type .. " " .. tg[3].value .. " " .. tg[3].loc.x .. " " ..tg[3].loc.y)
end
function my_ai:candidate_action_evaluation_hello()

View file

@ -119,9 +119,14 @@ std::string engine::evaluate(const std::string& /*str*/)
return "evaluate command is not implemented by this engine";
}
void engine::set_ai_context(ai_context * /*context*/)
void engine::set_ai_context(ai_context_ptr ai_context)
{
//do nothing
ai_context_ = ai_context;
}
ai_context_ptr engine::get_ai_context()
{
return ai_context_;
}
config engine::to_config() const

View file

@ -84,9 +84,9 @@ public:
/**
* set ai context (which is not available during early initialization)
*/
virtual void set_ai_context(ai_context *context);
virtual void set_ai_context(ai_context_ptr context);
virtual ai_context_ptr get_ai_context();
/**
* serialize
*/
@ -104,6 +104,7 @@ public:
protected:
readonly_context &ai_;
ai_context_ptr ai_context_;
/** name of the engine which has created this engine*/
std::string engine_;

View file

@ -266,12 +266,6 @@ std::string engine_lua::evaluate(const std::string &/*str*/)
return "";
}
void engine_lua::set_ai_context(ai_context * /*context*/)
{
//this function is called when the ai is fully initialized
}
config engine_lua::to_config() const
{
config cfg = engine::to_config();

View file

@ -59,12 +59,12 @@ public:
*/
virtual config to_config() const;
/**
* Method to inject AI context into the engine.
* The context includes all that in necessary for the AI -
* , like access to game state and movement/attack routines.
*/
virtual void set_ai_context(ai_context *context);
// /**
// * Method to inject AI context into the engine.
// * The context includes all that in necessary for the AI -
// * , like access to game state and movement/attack routines.
// */
// virtual void set_ai_context(ai_context *context);
private:
@ -72,7 +72,7 @@ private:
* The underlying lua code
*/
std::string code_;
//There is one lua engine per AI. So, it can hold state
boost::shared_ptr<lua_ai_context> lua_ai_context_;

View file

@ -50,6 +50,9 @@ namespace ai {
class interface;
class ai_context;
typedef ai_context* ai_context_ptr;
// recursion counter
class recursion_counter {
public:

View file

@ -47,6 +47,7 @@
#include "../../unit.hpp"
#include "../actions.hpp"
#include "../composite/engine_lua.hpp"
#include "../composite/contexts.hpp"
#include <lua/llimits.h>
static lg::log_domain log_ai_engine_lua("ai/engine/lua");
@ -57,6 +58,8 @@ static char const aisKey = 0;
namespace ai {
static void push_map_location(lua_State *L, const map_location& ml);
void lua_ai_context::init(lua_State *L)
{
// Create the ai elements table.
@ -277,6 +280,42 @@ static int cfun_ai_execute_recall(lua_State *L)
return transform_ai_action(L,recall_result);
}
// Goals and targets
static int cfun_ai_get_targets(lua_State *L)
{
move_map enemy_dst_src = get_readonly_context(L).get_enemy_dstsrc();
std::vector<target> targets = get_engine(L).get_ai_context()->find_targets(enemy_dst_src);
int i = 1;
lua_createtable(L, 0, 0);
for (std::vector<target>::iterator it = targets.begin(); it != targets.end(); it++)
{
lua_pushinteger(L, i);
//to factor out
lua_createtable(L, 3, 0);
lua_pushstring(L, "type");
lua_pushnumber(L, it->type);
lua_rawset(L, -3);
lua_pushstring(L, "loc");
push_map_location(L, it->loc);
lua_rawset(L, -3);
lua_pushstring(L, "value");
lua_pushnumber(L, it->value);
lua_rawset(L, -3);
lua_rawset(L, -3);
++i;
}
return 1;
}
// Aspect section
static int cfun_ai_get_aggression(lua_State *L)
{
@ -446,11 +485,11 @@ static void push_map_location(lua_State *L, const map_location& ml)
lua_createtable(L, 2, 0);
lua_pushstring(L, "x");
lua_pushinteger(L, ml.x);
lua_pushinteger(L, ml.x + 1);
lua_rawset(L, -3);
lua_pushstring(L, "y");
lua_pushinteger(L, ml.y);
lua_pushinteger(L, ml.y + 1);
lua_rawset(L, -3);
}
@ -538,6 +577,9 @@ lua_ai_context* lua_ai_context::create(lua_State *L, char const *code, ai::engin
{ "get_enemy_dstsrc", &cfun_ai_get_enemy_dstsrc },
{ "get_enemy_srcdst", &cfun_ai_get_enemy_srcdst },
// End of move maps
// Goals and targets
{ "get_targets", &cfun_ai_get_targets },
// End of G & T
// Aspects
{ "get_aggression", &cfun_ai_get_aggression },
{ "get_avoid", &cfun_ai_get_avoid },

View file

@ -37,6 +37,7 @@
#include <map>
#include <stack>
#include <vector>
#include "composite/engine.hpp"
namespace ai {
@ -95,7 +96,13 @@ void holder::init( side_number side )
modify_ai(mod_ai);
}
cfg_.clear_children("modify_ai");
std::vector<engine_ptr> engines = ai_->get_engines();
for (std::vector<engine_ptr>::iterator it = engines.begin(); it != engines.end(); ++it)
{
(*it)->set_ai_context(&(ai_->get_ai_context()));
}
} else {
ERR_AI_MANAGER << describe_ai()<<"AI lazy initialization error!" << std::endl;
}