44 #define DBG_AI_LUA LOG_STREAM(debug, log_ai_engine_lua) 45 #define LOG_AI_LUA LOG_STREAM(info, log_ai_engine_lua) 46 #define WRN_AI_LUA LOG_STREAM(warn, log_ai_engine_lua) 47 #define ERR_AI_LUA LOG_STREAM(err, log_ai_engine_lua) 52 #pragma warning(disable:4250) 68 auto l_obj = std::make_shared<lua_object<double>>();
76 std::shared_ptr<double> result = l_obj->get();
78 return result ? *result : 0.0;
117 cfg[
"evaluation"] = evaluation_;
118 cfg[
"execution"] = execution_;
135 exec_parms_ = cfg[
"exec_parms"].str();
136 eval_parms_ = cfg[
"eval_parms"].str();
138 std::string eval_code;
139 std::string exec_code;
140 generate_code(eval_code, exec_code);
151 cfg[
"location"] = location_;
153 cfg[
"eval_parms"] = eval_parms_;
154 cfg[
"exec_parms"] = exec_parms_;
166 std::string preamble =
"local self, params, data, filter_own = ...\n";
167 std::string
load =
"wesnoth.require(\"" + location_ +
"\")";
169 eval = preamble +
"return " + load +
":evaluation(ai, {" + eval_parms_ +
"}, {data = data})";
170 exec = preamble + load +
":execution(ai, {" + exec_parms_ +
"}, {data = data})";
172 eval = preamble +
"return " + load +
".evaluation(self, params, data, filter_own)";
173 exec = preamble + load +
".execution(self, params, data, filter_own)";
210 cfg[
"sticky"] =
true;
211 cfg[
"unit_x"] = bound_unit_->get_location().wml_x();
212 cfg[
"unit_y"] = bound_unit_->get_location().wml_y();
225 action_handler_.reset(
resources::lua_kernel->create_lua_ai_action_handler(code_.c_str(),lua_ai_ctx));
236 if (action_handler_) {
264 , code_(get_engine_code(cfg))
266 get_engine_code(cfg).c_str(), this))
282 return cfg[
"code"].str();
285 std::string code =
"wesnoth.require(\"ai/lua/dummy_engine_lua.lua\")";
316 if (!cfg[
"sticky"].to_bool())
353 const std::string aspect_factory_key =
id+
"*lua_aspect";
358 DBG_AI_LUA <<
"config snippet contains: " << std::endl << cfg;
363 ERR_AI_LUA <<
"side "<<
ai_.
get_side()<<
" : UNABLE TO CREATE aspect, key=["<<aspect_factory_key<<
"]";
364 DBG_AI_LUA <<
"config snippet contains: " << std::endl << cfg;
375 DBG_AI_LUA <<
"config snippet contains: " << std::endl << cfg;
378 goal_ptr new_goal = f->second->get_new_instance(
ai_,cfg);
380 if (!new_goal || !new_goal->ok()) {
382 DBG_AI_LUA <<
"config snippet contains: " << std::endl << cfg;
405 cfg[
"code"] = this->
code_;
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
virtual void execute()
Execute the candidate action.
std::shared_ptr< candidate_action > candidate_action_ptr
std::shared_ptr< lua_ai_context > lua_ai_context_
std::shared_ptr< stage > stage_ptr
lua_stage_wrapper(ai_context &context, const config &cfg, lua_ai_context &lua_ai_ctx)
virtual ~lua_candidate_action_wrapper_external()
virtual const unit_map & units() const override
Interfaces for manipulating version numbers of engine, add-ons, etc.
std::string code_
Method to inject AI context into the engine.
static factory_map & get_list()
bool is_gamestate_changed()
Check if the gamestate has changed since last reset reset is done once on construction, and can be done by hand via reset() method.
virtual ~lua_candidate_action_wrapper_base()
std::vector< game_tip > load(const config &cfg)
Loads the tips from a config.
virtual config to_config() const
serialize
bool has_attribute(config_key_type key) const
virtual config to_config() const
serialize
wfl::candidate_action_ptr ca_ptr
static factory_map & get_list()
virtual config to_config() const
Serialize to config.
std::string get_engine_code(const config &) const
void generate_code(std::string &eval, std::string &exec)
virtual ~lua_stage_wrapper()
virtual std::string get_id() const
std::shared_ptr< unit > unit_ptr
A helper class to observe the game state.
Lua object(value) wrapper implementation.
engine_lua(readonly_context &context, const config &cfg)
Note that initially we get access only to readonly context (engine is created rather early...
config serialized_filterown_
config serialized_evaluation_state_
std::shared_ptr< aspect > aspect_ptr
std::shared_ptr< lua_ai_action_handler > execution_action_handler_
std::shared_ptr< lua_ai_action_handler > action_handler_
A small explanation about what's going on here: Each action has access to two game_info objects First...
std::shared_ptr< goal > goal_ptr
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Proxy table for the AI context.
virtual void do_parse_goal_from_config(const config &cfg, std::back_insert_iterator< std::vector< goal_ptr > > b)
Composite AI with turn sequence which is a vector of stages.
virtual void execute()
Execute the candidate action.
config serialized_evaluation_state_
virtual void do_parse_aspect_from_config(const config &cfg, const std::string &id, std::back_insert_iterator< std::vector< aspect_ptr > > b)
Taka a config (with engine=lua in it) and parse several (usually, 1) aspects out of it...
virtual void set_to_be_removed()
virtual config to_config() const
serialize
void apply_micro_ai(const config &cfg)
static lg::log_domain log_ai_engine_lua("ai/engine/lua")
virtual void do_parse_stage_from_config(ai_context &context, const config &cfg, std::back_insert_iterator< std::vector< stage_ptr > > b)
Taka a config (with engine=lua in it) and parse several (usually, 1) stages out of it...
Encapsulates the map of the game.
unit_iterator find(std::size_t id)
virtual bool do_play_stage()
Play the turn - implementation.
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
virtual config to_config() const
serialize
virtual void push_ai_table()
Method that pushes the AI table of the lua_context on the stack for debugging purposes.
virtual config to_config() const
serialize
virtual config to_config() const
serialize
std::shared_ptr< lua_ai_action_handler > evaluation_action_handler_
lua_candidate_action_wrapper(rca_context &context, const config &cfg, lua_ai_context &lua_ai_ctx)
virtual ~lua_candidate_action_wrapper()
virtual config to_config() const
serialize
config & add_child(config_key_type key)
void disable()
Disable the candidate action.
lua_sticky_candidate_action_wrapper(rca_context &context, const config &cfg, lua_ai_context &lua_ai_ctx)
Standard logging facilities (interface).
virtual side_number get_side() const =0
Get the side number.
static const double BAD_SCORE
game_lua_kernel * lua_kernel
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) candidate actions out of it...
lua_candidate_action_wrapper_base(rca_context &context, const config &cfg)
const config & child_or_empty(config_key_type key) const
Returns the first child with the given key, or an empty config if there is none.
virtual std::string evaluate(const std::string &str)
A config object defines a single node in a WML file, with access to child nodes.
LUA AI Support engine - creating specific ai components from config.
lua_candidate_action_wrapper_external(rca_context &context, const config &cfg, lua_ai_context &lua_ai_ctx)
std::string::const_iterator iterator
candidate action framework
std::shared_ptr< lua_object_base > lua_object_ptr
virtual config to_config() const
serialize