lua AI support engine, interface code and AI integration

This commit is contained in:
Iurii Chernyi 2010-01-18 21:32:38 +00:00
parent a114771fab
commit 62b629e179
9 changed files with 252 additions and 0 deletions

View file

@ -63,6 +63,8 @@
<Unit filename="..\..\src\ai\composite\engine_default.hpp" />
<Unit filename="..\..\src\ai\composite\engine_fai.cpp" />
<Unit filename="..\..\src\ai\composite\engine_fai.hpp" />
<Unit filename="..\..\src\ai\composite\engine_lua.cpp" />
<Unit filename="..\..\src\ai\composite\engine_lua.hpp" />
<Unit filename="..\..\src\ai\composite\goal.cpp" />
<Unit filename="..\..\src\ai\composite\goal.hpp" />
<Unit filename="..\..\src\ai\composite\property_handler.hpp" />

View file

@ -92,6 +92,8 @@
<Unit filename="..\..\src\ai\composite\engine_default.hpp" />
<Unit filename="..\..\src\ai\composite\engine_fai.cpp" />
<Unit filename="..\..\src\ai\composite\engine_fai.hpp" />
<Unit filename="..\..\src\ai\composite\engine_lua.cpp" />
<Unit filename="..\..\src\ai\composite\engine_lua.hpp" />
<Unit filename="..\..\src\ai\composite\goal.cpp" />
<Unit filename="..\..\src\ai\composite\goal.hpp" />
<Unit filename="..\..\src\ai\composite\property_handler.hpp" />

View file

@ -4459,6 +4459,34 @@
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\src\ai\composite\engine_lua.cpp"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)\ai\composite\"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)\ai\composite\"
/>
</FileConfiguration>
<FileConfiguration
Name="Debug (fast)|Win32"
>
<Tool
Name="VCCLCompilerTool"
ObjectFile="$(IntDir)\ai\composite\"
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\src\ai\composite\goal.cpp"
>
@ -6246,6 +6274,10 @@
RelativePath="..\..\src\ai\composite\engine_fai.hpp"
>
</File>
<File
RelativePath="..\..\src\ai\composite\engine_lua.hpp"
>
</File>
<File
RelativePath="..\..\src\ai\composite\goal.hpp"
>

View file

@ -170,6 +170,7 @@ set(wesnoth-main_SRC
ai/composite/engine.cpp
ai/composite/engine_default.cpp
ai/composite/engine_fai.cpp
ai/composite/engine_lua.cpp
ai/composite/goal.cpp
ai/composite/rca.cpp
ai/composite/stage.cpp

View file

@ -50,6 +50,7 @@ wesnoth_source = \
ai/composite/engine.cpp \
ai/composite/engine_default.cpp \
ai/composite/engine_fai.cpp \
ai/composite/engine_lua.cpp \
ai/composite/goal.cpp \
ai/composite/rca.cpp \
ai/composite/stage.cpp \

View file

@ -155,6 +155,7 @@ wesnoth_sources = Split("""
ai/composite/engine.cpp
ai/composite/engine_default.cpp
ai/composite/engine_fai.cpp
ai/composite/engine_lua.cpp
ai/composite/goal.cpp
ai/composite/rca.cpp
ai/composite/stage.cpp

View file

@ -0,0 +1,141 @@
/* $Id$ */
/*
Copyright (C) 2009 - 2010 by Yurii Chernyi <terraninfo@terraninfo.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* LUA AI Support engine - creating specific ai components from config
* @file ai/composite/engine_lua.cpp
*/
#include "ai.hpp"
#include "engine_lua.hpp"
#include "rca.hpp"
#include "../../log.hpp"
namespace ai {
static lg::log_domain log_ai_engine_lua("ai/engine/lua");
#define DBG_AI_LUA LOG_STREAM(debug, log_ai_engine_lua)
#define LOG_AI_LUA LOG_STREAM(info, log_ai_engine_lua)
#define WRN_AI_LUA LOG_STREAM(warn, log_ai_engine_lua)
#define ERR_AI_LUA LOG_STREAM(err, log_ai_engine_lua)
#ifdef _MSC_VER
#pragma warning(push)
//silence "inherits via dominance" warnings
#pragma warning(disable:4250)
#endif
class lua_candidate_action_wrapper : public candidate_action {
public:
lua_candidate_action_wrapper( rca_context &context, const config &cfg )
: candidate_action(context,cfg),cfg_(cfg)
{
}
virtual ~lua_candidate_action_wrapper() {}
virtual double evaluate()
{
//@todo: lua must evaluate the score and return it
return 0;
}
virtual bool execute()
{
//@todo: lua must do the actions. the return value is supposed to be 'true if gamestate has changed' but it'll be refactored away soon.
return false;
}
virtual config to_config() const
{
return cfg_;//or we can serialize our current state to config instead of using original cfg
}
private:
const config cfg_;
};
/**
* Note that initially we get access only to readonly context (engine is created rather early, when there's no way to move/attack.
* We inject full ai_context later.
*/
engine_lua::engine_lua( readonly_context &context, const config &cfg )
: engine(context,cfg)
{
name_ = "lua";
}
engine_lua::~engine_lua()
{
}
void engine_lua::do_parse_candidate_action_from_config( rca_context & /*context*/, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr > > b ){
if (!cfg) {
return;
}
candidate_action_ptr ca_ptr;
//@todo : need to code an adapter class which implements the candidate action interface
//ca_ptr = candidate_action_ptr(new lua_candidate_action_wrapper(context,cfg));
if (ca_ptr) {
*b = ca_ptr;
}
}
void engine_lua::do_parse_stage_from_config( ai_context & /*context*/, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr > > b )
{
if (!cfg) {
return;
}
stage_ptr st_ptr;
//@todo : need to code an adapter class which implements the stage interface
//it's simple - the main part is do_play_stage() method
//st_ptr = stage_ptr(new lua_stage_wrapper(context,cfg));
if (st_ptr) {
st_ptr->on_create();
*b = st_ptr;
}
}
std::string engine_lua::evaluate(const std::string &str)
{
//@todo: this is not mandatory, but if we want to allow lua to evaluate
// something 'in context' of this ai, this will be useful
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();
//we can modify the cfg here
return cfg;
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
} //end of namespace ai

View file

@ -0,0 +1,68 @@
/* $Id$ */
/*
Copyright (C) 2009 - 2010 by Yurii Chernyi <terraninfo@terraninfo.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* LUA AI Support engine - creating specific ai components from config
* @file ai/composite/engine_lua.hpp
*/
#ifndef AI_COMPOSITE_ENGINE_LUA_HPP_INCLUDED
#define AI_COMPOSITE_ENGINE_LUA_HPP_INCLUDED
#include "../../global.hpp"
#include "engine.hpp"
#include "../contexts.hpp"
//============================================================================
namespace ai {
class engine_lua : public engine {
public:
engine_lua( readonly_context &context, const config &cfg );
virtual ~engine_lua();
/**
* Taka a config (with engine=lua in it)
* and parse several (usually, 1) candidate actions out of it
*/
virtual void do_parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr > > b );
/**
* Taka a config (with engine=lua in it)
* and parse several (usually, 1) stages out of it
*/
virtual void do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr > > b );
virtual std::string evaluate(const std::string &str);
/**
* Serialize to config
*/
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);
private:
//There is one lua engine per AI. So, it can hold state
};
} //end of namespace ai
#endif

View file

@ -22,6 +22,7 @@
#include "composite/aspect.hpp"
#include "composite/engine_default.hpp"
#include "composite/engine_fai.hpp"
#include "composite/engine_lua.hpp"
#include "composite/goal.hpp"
#include "default/ai.hpp"
#include "formula/ai.hpp"
@ -56,6 +57,9 @@ static register_engine_factory<engine_cpp>
static register_engine_factory<engine_fai>
composite_ai_factory_fai("fai");
static register_engine_factory<engine_lua>
composite_ai_factory_lua("lua");
// =======================================================================
// Stages
// =======================================================================