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  /**
61  * Method that pushes the AI table of the lua_context on the stack
62  * for debugging purposes
63  */
64 
65  virtual void push_ai_table();
66 
67  /**
68  * Serialize to config
69  */
70  virtual config to_config() const;
71 
72 // /**
73 // * Method to inject AI context into the engine.
74 // * The context includes all that in necessary for the AI -
75 // * , like access to game state and movement/attack routines.
76 // */
77 // virtual void set_ai_context(ai_context *context);
78 
79 private:
80 
81  /**
82  * The underlying lua code
83  */
84  std::string code_;
85 
86  //There is one lua engine per AI. So, it can hold state
87  std::shared_ptr<lua_ai_context> lua_ai_context_;
88 
89  std::string get_engine_code(const config&) const;
90 
91 };
92 
93 } //end of namespace ai
std::shared_ptr< lua_ai_context > lua_ai_context_
Definition: engine_lua.hpp:87
std::string code_
Method to inject AI context into the engine.
Definition: engine_lua.hpp:84
virtual config to_config() const
Serialize to config.
Definition: engine_lua.cpp:385
virtual ~engine_lua()
Definition: engine_lua.cpp:279
std::string get_engine_code(const config &) const
Definition: engine_lua.cpp:269
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:252
#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:360
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:341
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:324
bool is_ok() const
Definition: engine_lua.cpp:283
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:288
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:296
virtual std::string evaluate(const std::string &str)
Definition: engine_lua.cpp:378
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61