The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
stage.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Yurii Chernyi <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Composite AI stages
18  */
19 
20 #ifndef AI_COMPOSITE_STAGE_HPP_INCLUDED
21 #define AI_COMPOSITE_STAGE_HPP_INCLUDED
22 
25 
26 #ifdef _MSC_VER
27 #pragma warning(push)
28 //silence "inherits via dominance" warnings
29 #pragma warning(disable:4250)
30 #endif
31 
32 namespace ai {
33 
34 class ai_composite;
35 
36 class stage : public virtual ai_context_proxy, public component {
37 public:
38 
39  /**
40  * Constructor
41  */
42  stage( ai_context &context, const config &cfg );
43 
44  /**
45  * Initialization
46  */
47  virtual void on_create();
48 
49 
50  /**
51  * Destructor
52  */
53  virtual ~stage();
54 
55  /**
56  * Play the turn - strategy
57  * @return true only if game state has changed. Returning false is always safe.
58  */
59  bool play_stage();
60 
61 
62  /**
63  * get the value of the recursion counter
64  */
65  int get_recursion_count() const;
66 
67 
68  /**
69  * serialize
70  */
71  virtual config to_config() const;
72 
73  virtual std::string get_id() const;
74  virtual std::string get_name() const;
75  virtual std::string get_engine() const;
76 
77 protected:
78  /**
79  * Play the turn - implementation
80  * @return true only if game state has changed. Returning false is always safe.
81  */
82  virtual bool do_play_stage() = 0;
83 
85 
87 
88 };
89 
90 
91 class idle_stage : public stage {
92 public:
93  idle_stage( ai_context &context, const config &cfg );
94 
95  ~idle_stage();
96 
97  virtual bool do_play_stage();
98 };
99 
100 
102  bool is_duplicate(const std::string &name);
103 public:
105  typedef std::map<std::string, factory_ptr> factory_map;
106  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
107 
108  static factory_map& get_list() {
109  static factory_map *stage_factories;
110  if (stage_factories==nullptr) {
111  stage_factories = new factory_map;
112  }
113  return *stage_factories;
114  }
115 
116  virtual stage_ptr get_new_instance( ai_context &context, const config &cfg ) = 0;
117 
118  stage_factory( const std::string &name )
119  {
120  if (is_duplicate(name)) {
121  return;
122  }
123  factory_ptr ptr_to_this(this);
124  get_list().insert(make_pair(name,ptr_to_this));
125  }
126 
127  virtual ~stage_factory() {}
128 };
129 
130 
131 template<class STAGE>
133 public:
135  : stage_factory( name )
136  {
137  }
138 
139  virtual stage_ptr get_new_instance( ai_context &context, const config &cfg ){
140  stage_ptr a(new STAGE(context,cfg));
141  a->on_create();
142  return a;
143  }
144 };
145 
146 } //end of namespace ai
147 
148 #ifdef _MSC_VER
149 #pragma warning(pop)
150 #endif
151 
152 #endif
virtual bool do_play_stage()
Play the turn - implementation.
Definition: stage.cpp:107
idle_stage(ai_context &context, const config &cfg)
Definition: stage.cpp:98
std::map< std::string, factory_ptr > factory_map
Definition: stage.hpp:105
stage_factory(const std::string &name)
Definition: stage.hpp:118
int get_recursion_count() const
get the value of the recursion counter
Definition: stage.cpp:61
bool play_stage()
Play the turn - strategy.
Definition: stage.cpp:56
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: stage.hpp:106
recursion_counter recursion_counter_
Definition: stage.hpp:84
virtual std::string get_id() const
Definition: stage.cpp:75
virtual std::string get_name() const
Definition: stage.cpp:85
virtual ~stage()
Destructor.
Definition: stage.cpp:52
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:57
static factory_map & get_list()
Definition: stage.hpp:108
virtual config to_config() const
serialize
Definition: stage.cpp:66
bool is_duplicate(const std::string &name)
Definition: stage.cpp:114
A component of the AI framework.
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
virtual void on_create()
Initialization.
Definition: stage.cpp:47
config cfg_
Definition: stage.hpp:86
stage(ai_context &context, const config &cfg)
Constructor.
Definition: stage.cpp:41
virtual bool do_play_stage()=0
Play the turn - implementation.
boost::shared_ptr< stage_factory > factory_ptr
Definition: stage.hpp:104
GLuint const GLchar * name
Definition: glew.h:1782
register_stage_factory(const std::string &name)
Definition: stage.hpp:134
virtual stage_ptr get_new_instance(ai_context &context, const config &cfg)=0
virtual ~stage_factory()
Definition: stage.hpp:127
Composite AI contexts.
virtual std::string get_engine() const
Definition: stage.cpp:80
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
virtual stage_ptr get_new_instance(ai_context &context, const config &cfg)
Definition: stage.hpp:139