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