The Battle for Wesnoth  1.13.10+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 - 2017 by Yurii Chernyi <terraninfo@terraninfo.net>
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 #pragma once
21 
24 
25 #ifdef _MSC_VER
26 #pragma warning(push)
27 //silence "inherits via dominance" warnings
28 #pragma warning(disable:4250)
29 #endif
30 
31 namespace ai {
32 
33 class ai_composite;
34 
35 class stage : public virtual ai_context_proxy, public component {
36 public:
37 
38  /**
39  * Constructor
40  */
41  stage( ai_context &context, const config &cfg );
42 
43  /**
44  * Initialization
45  */
46  virtual void on_create();
47 
48 
49  /**
50  * Destructor
51  */
52  virtual ~stage();
53 
54  /**
55  * Play the turn - strategy
56  * @return true only if game state has changed. Returning false is always safe.
57  */
58  bool play_stage();
59 
60 
61  /**
62  * get the value of the recursion counter
63  */
64  int get_recursion_count() const;
65 
66 
67  /**
68  * serialize
69  */
70  virtual config to_config() const;
71 
72  virtual std::string get_id() const;
73  virtual std::string get_name() const;
74  virtual std::string get_engine() const;
75 
76 protected:
77  /**
78  * Play the turn - implementation
79  * @return true only if game state has changed. Returning false is always safe.
80  */
81  virtual bool do_play_stage() = 0;
82 
84 
86 
87 };
88 
89 
90 class idle_stage : public stage {
91 public:
92  idle_stage( ai_context &context, const config &cfg );
93 
94  ~idle_stage();
95 
96  virtual bool do_play_stage();
97 };
98 
99 
101  bool is_duplicate(const std::string &name);
102 public:
103  typedef std::shared_ptr< stage_factory > factory_ptr;
104  typedef std::map<std::string, factory_ptr> factory_map;
105  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
106 
107  static factory_map& get_list() {
108  static factory_map *stage_factories;
109  if (stage_factories==nullptr) {
110  stage_factories = new factory_map;
111  }
112  return *stage_factories;
113  }
114 
115  virtual stage_ptr get_new_instance( ai_context &context, const config &cfg ) = 0;
116 
117  stage_factory( const std::string &name )
118  {
119  if (is_duplicate(name)) {
120  return;
121  }
122  factory_ptr ptr_to_this(this);
123  get_list().emplace(name,ptr_to_this);
124  }
125 
126  virtual ~stage_factory() {}
127 };
128 
129 
130 template<class STAGE>
132 public:
134  : stage_factory( name )
135  {
136  }
137 
138  virtual stage_ptr get_new_instance( ai_context &context, const config &cfg ){
139  stage_ptr a(new STAGE(context,cfg));
140  a->on_create();
141  return a;
142  }
143 };
144 
145 } //end of namespace ai
146 
147 #ifdef _MSC_VER
148 #pragma warning(pop)
149 #endif
std::vector< char_t > string
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:104
stage_factory(const std::string &name)
Definition: stage.hpp:117
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:105
recursion_counter recursion_counter_
Definition: stage.hpp:83
#define a
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:58
static factory_map & get_list()
Definition: stage.hpp:107
virtual config to_config() const
serialize
Definition: stage.cpp:66
std::shared_ptr< stage > stage_ptr
Definition: game_info.hpp:110
bool is_duplicate(const std::string &name)
Definition: stage.cpp:114
A component of the AI framework.
virtual void on_create()
Initialization.
Definition: stage.cpp:47
config cfg_
Definition: stage.hpp:85
stage(ai_context &context, const config &cfg)
Constructor.
Definition: stage.cpp:41
std::shared_ptr< stage_factory > factory_ptr
Definition: stage.hpp:103
virtual bool do_play_stage()=0
Play the turn - implementation.
register_stage_factory(const std::string &name)
Definition: stage.hpp:133
virtual stage_ptr get_new_instance(ai_context &context, const config &cfg)=0
virtual ~stage_factory()
Definition: stage.hpp:126
Composite AI contexts.
virtual std::string get_engine() const
Definition: stage.cpp:80
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
virtual stage_ptr get_new_instance(ai_context &context, const config &cfg)
Definition: stage.hpp:138