The Battle for Wesnoth  1.15.12+dev
engine_lua.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Yurii Chernyi <terraninfo@terraninfo.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * LUA AI Support engine - creating specific ai components from config
17  * @file
18  */
19 
20 #pragma once
21 
22 #include "ai/composite/engine.hpp"
23 
24 //============================================================================
25 namespace ai {
26 
27 class lua_ai_context;
28 
29 class engine_lua : public engine {
30 public:
31  engine_lua( readonly_context &context, const config &cfg );
32 
33  virtual ~engine_lua();
34 
35  bool is_ok() const;
36 
37  /**
38  * Taka a config (with engine=lua in it)
39  * and parse several (usually, 1) candidate actions out of it
40  */
41  virtual void do_parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr > > b );
42 
43  /**
44  * Taka a config (with engine=lua in it)
45  * and parse several (usually, 1) stages out of it
46  */
47  virtual void do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr > > b );
48 
49  /**
50  * Taka a config (with engine=lua in it)
51  * and parse several (usually, 1) aspects out of it
52  */
53  virtual void do_parse_aspect_from_config( const config &cfg, const std::string &id, std::back_insert_iterator<std::vector< aspect_ptr > > b );
54 
55  virtual void do_parse_goal_from_config(const config &cfg, std::back_insert_iterator<std::vector< goal_ptr > > b );
56 
57  virtual std::string evaluate(const std::string &str);
58 
59  /**
60  * Method that pushes the AI table of the lua_context on the stack
61  * for debugging purposes
62  */
63 
64  virtual void push_ai_table();
65 
66  /**
67  * Serialize to config
68  */
69  virtual config to_config() const;
70 
71 // /**
72 // * Method to inject AI context into the engine.
73 // * The context includes all that in necessary for the AI -
74 // * , like access to game state and movement/attack routines.
75 // */
76 // virtual void set_ai_context(ai_context *context);
77 
78 private:
79 
80  /**
81  * The underlying lua code
82  */
83  std::string code_;
84 
85  //There is one lua engine per AI. So, it can hold state
86  std::shared_ptr<lua_ai_context> lua_ai_context_;
87 
88  std::string get_engine_code(const config&) const;
89 
90 };
91 
92 } //end of namespace ai
std::shared_ptr< lua_ai_context > lua_ai_context_
Definition: engine_lua.hpp:86
std::string code_
Method to inject AI context into the engine.
Definition: engine_lua.hpp:83
virtual config to_config() const
Serialize to config.
Definition: engine_lua.cpp:384
virtual ~engine_lua()
Definition: engine_lua.cpp:278
std::string get_engine_code(const config &) const
Definition: engine_lua.cpp:268
AI Support engine - creating specific ai components from config.
engine_lua(readonly_context &context, const config &cfg)
Note that initially we get access only to readonly context (engine is created rather early...
Definition: engine_lua.cpp:251
#define b
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
virtual void do_parse_goal_from_config(const config &cfg, std::back_insert_iterator< std::vector< goal_ptr > > b)
Definition: engine_lua.cpp:359
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...
Definition: engine_lua.cpp:340
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...
Definition: engine_lua.cpp:323
bool is_ok() const
Definition: engine_lua.cpp:282
virtual void push_ai_table()
Method that pushes the AI table of the lua_context on the stack for debugging purposes.
Definition: engine_lua.cpp:287
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...
Definition: engine_lua.cpp:295
virtual std::string evaluate(const std::string &str)
Definition: engine_lua.cpp:377
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59