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