The Battle for Wesnoth  1.19.7+dev
engine.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
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  * AI Support engine - creating specific ai components from config
18  * @file
19  */
20 
21 #pragma once
22 
24 #include "ai/contexts.hpp"
25 
26 #include <iterator>
27 
28 //============================================================================
29 
30 namespace ai {
31 
32 class rca_context;
33 
34 class engine : public component {
35 public:
36  engine( readonly_context &context, const config &cfg );
37 
38  virtual ~engine();
39 
40  virtual bool is_ok() const;
41 
42  static void parse_aspect_from_config( readonly_context &context, const config &cfg, const std::string &id, std::back_insert_iterator<std::vector< aspect_ptr >> b );
43 
44  static void parse_goal_from_config( readonly_context &context, const config &cfg, std::back_insert_iterator<std::vector< goal_ptr >> b );
45 
46  static void parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr >> b );
47 
48  static void parse_engine_from_config( readonly_context &context, const config &cfg, std::back_insert_iterator<std::vector< engine_ptr >> b );
49 
50  static void parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr >> b );
51 
52  //do not override that method in subclasses which cannot create aspects
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  //do not override that method in subclasses which cannot create candidate_actions
56  virtual void do_parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr >> b );
57 
58  //do not override that method in subclasses which cannot create goals
59  virtual void do_parse_goal_from_config( const config &cfg, std::back_insert_iterator<std::vector< goal_ptr >> b );
60 
61  //do not override that method in subclasses which cannot create engines
62  virtual void do_parse_engine_from_config( const config &cfg, std::back_insert_iterator<std::vector< engine_ptr >> b );
63 
64  //do not override that method in subclasses which cannot create stages
65  virtual void do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr >> b );
66 
67  //do not override that method in subclasses which cannot evaluate formulas
68  virtual std::string evaluate(const std::string& str);
69 
71 
72  /**
73  * set ai context (which is not available during early initialization)
74  */
75  virtual void set_ai_context(ai_context_ptr context);
76 
78  /**
79  * serialize
80  */
81  virtual config to_config() const;
82 
83  virtual std::string get_id() const
84  { return id_; }
85 
86  virtual std::string get_engine() const
87  { return engine_; }
88 
89  virtual std::string get_name() const
90  { return name_; }
91 
92 protected:
95 
96  /** name of the engine which has created this engine*/
97  std::string engine_;
98  std::string id_;
99  std::string name_;
100 };
101 
102 
104  bool is_duplicate(const std::string &name);
105 public:
106  typedef std::shared_ptr< engine_factory > factory_ptr;
107  typedef std::map<std::string, factory_ptr> factory_map;
108  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
109 
110  static factory_map& get_list() {
111  static factory_map *engine_factories;
112  if (engine_factories==nullptr) {
113  engine_factories = new factory_map;
114  }
115  return *engine_factories;
116  }
117 
118  virtual engine_ptr get_new_instance( readonly_context &ai, const config &cfg ) = 0;
119  virtual engine_ptr get_new_instance( readonly_context &ai, const std::string& name ) = 0;
120 
121  engine_factory( const std::string &name )
122  {
123  if (is_duplicate(name)) {
124  return;
125  }
126  factory_ptr ptr_to_this(this);
127  get_list().emplace(name,ptr_to_this);
128  }
129 
130  virtual ~engine_factory() {}
131 };
132 
133 template<class ENGINE>
135 public:
136  register_engine_factory( const std::string &name )
137  : engine_factory( name )
138  {
139  }
140 
142  engine_ptr e = std::make_shared<ENGINE>(ai, cfg);
143  if (!e->is_ok()) {
144  return engine_ptr();
145  }
146  return e;
147  }
148 
149  virtual engine_ptr get_new_instance( readonly_context &ai, const std::string& name ){
150  config cfg;
151  cfg["name"] = name;
152  cfg["engine"] = "cpp"; // @Crab: what is the purpose of this line(neph)
153  return std::make_shared<ENGINE>(ai, cfg);
154  }
155 };
156 
157 } //end of namespace ai
virtual engine_ptr get_new_instance(readonly_context &ai, const std::string &name)=0
virtual engine_ptr get_new_instance(readonly_context &ai, const config &cfg)=0
engine_factory(const std::string &name)
Definition: engine.hpp:121
static factory_map & get_list()
Definition: engine.hpp:110
virtual ~engine_factory()
Definition: engine.hpp:130
std::map< std::string, factory_ptr > factory_map
Definition: engine.hpp:107
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: engine.hpp:108
std::shared_ptr< engine_factory > factory_ptr
Definition: engine.hpp:106
bool is_duplicate(const std::string &name)
Definition: engine.cpp:148
std::string engine_
name of the engine which has created this engine
Definition: engine.hpp:97
virtual ai_context_ptr get_ai_context()
Definition: engine.cpp:129
engine(readonly_context &context, const config &cfg)
Definition: engine.cpp:33
std::string name_
Definition: engine.hpp:99
static void parse_goal_from_config(readonly_context &context, const config &cfg, std::back_insert_iterator< std::vector< goal_ptr >> b)
Definition: engine.cpp:79
virtual std::string get_id() const
Definition: engine.hpp:83
virtual void set_ai_context(ai_context_ptr context)
set ai context (which is not available during early initialization)
Definition: engine.cpp:124
virtual ~engine()
Definition: engine.cpp:43
virtual config to_config() const
serialize
Definition: engine.cpp:134
virtual std::string get_name() const
Definition: engine.hpp:89
virtual std::string evaluate(const std::string &str)
Definition: engine.cpp:119
static void parse_aspect_from_config(readonly_context &context, const config &cfg, const std::string &id, std::back_insert_iterator< std::vector< aspect_ptr >> b)
Definition: engine.cpp:52
ai_context_ptr ai_context_
Definition: engine.hpp:94
virtual void do_parse_aspect_from_config(const config &cfg, const std::string &id, std::back_insert_iterator< std::vector< aspect_ptr >> b)
Definition: engine.cpp:97
virtual void do_parse_engine_from_config(const config &cfg, std::back_insert_iterator< std::vector< engine_ptr >> b)
Definition: engine.cpp:106
virtual void do_parse_candidate_action_from_config(rca_context &context, const config &cfg, std::back_insert_iterator< std::vector< candidate_action_ptr >> b)
Definition: engine.cpp:102
static void parse_candidate_action_from_config(rca_context &context, const config &cfg, std::back_insert_iterator< std::vector< candidate_action_ptr >> b)
Definition: engine.cpp:61
virtual void do_parse_stage_from_config(ai_context &context, const config &cfg, std::back_insert_iterator< std::vector< stage_ptr >> b)
Definition: engine.cpp:114
static void parse_stage_from_config(ai_context &context, const config &cfg, std::back_insert_iterator< std::vector< stage_ptr >> b)
Definition: engine.cpp:88
readonly_context & ai_
Definition: engine.hpp:93
static void parse_engine_from_config(readonly_context &context, const config &cfg, std::back_insert_iterator< std::vector< engine_ptr >> b)
Definition: engine.cpp:70
virtual bool is_ok() const
Definition: engine.cpp:47
readonly_context & get_readonly_context()
Definition: engine.cpp:142
virtual std::string get_engine() const
Definition: engine.hpp:86
virtual void do_parse_goal_from_config(const config &cfg, std::back_insert_iterator< std::vector< goal_ptr >> b)
Definition: engine.cpp:110
std::string id_
Definition: engine.hpp:98
virtual engine_ptr get_new_instance(readonly_context &ai, const std::string &name)
Definition: engine.hpp:149
register_engine_factory(const std::string &name)
Definition: engine.hpp:136
virtual engine_ptr get_new_instance(readonly_context &ai, const config &cfg)
Definition: engine.hpp:141
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
A component of the AI framework.
Helper functions for the object which operates in the context of AI for specific side this is part of...
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
std::shared_ptr< engine > engine_ptr
Definition: game_info.hpp:99
#define e
#define b