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