The Battle for Wesnoth  1.15.0-dev
stage.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Yurii Chernyi <terraninfo@terraninfo.net>
3  Part of the Battle for Wesnoth Project https://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 namespace ai {
26 
27 class ai_composite;
28 
29 class stage : public virtual ai_context_proxy, public component {
30 public:
31 
32  /**
33  * Constructor
34  */
35  stage( ai_context &context, const config &cfg );
36 
37  /**
38  * Initialization
39  */
40  virtual void on_create();
41 
42 
43  /**
44  * Destructor
45  */
46  virtual ~stage();
47 
48  /**
49  * Play the turn - strategy
50  * @return true only if game state has changed. Returning false is always safe.
51  */
52  bool play_stage();
53 
54 
55  /**
56  * get the value of the recursion counter
57  */
58  int get_recursion_count() const;
59 
60 
61  /**
62  * serialize
63  */
64  virtual config to_config() const;
65 
66  virtual std::string get_id() const;
67  virtual std::string get_name() const;
68  virtual std::string get_engine() const;
69 
70 protected:
71  /**
72  * Play the turn - implementation
73  * @return true only if game state has changed. Returning false is always safe.
74  */
75  virtual bool do_play_stage() = 0;
76 
78 
80 
81 };
82 
83 
84 class idle_stage : public stage {
85 public:
86  idle_stage( ai_context &context, const config &cfg );
87 
88  ~idle_stage();
89 
90  virtual bool do_play_stage();
91 };
92 
93 
95  bool is_duplicate(const std::string &name);
96 public:
97  typedef std::shared_ptr< stage_factory > factory_ptr;
98  typedef std::map<std::string, factory_ptr> factory_map;
99  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
100 
101  static factory_map& get_list() {
102  static factory_map *stage_factories;
103  if (stage_factories==nullptr) {
104  stage_factories = new factory_map;
105  }
106  return *stage_factories;
107  }
108 
109  virtual stage_ptr get_new_instance( ai_context &context, const config &cfg ) = 0;
110 
111  stage_factory( const std::string &name )
112  {
113  if (is_duplicate(name)) {
114  return;
115  }
116  factory_ptr ptr_to_this(this);
117  get_list().emplace(name,ptr_to_this);
118  }
119 
120  virtual ~stage_factory() {}
121 };
122 
123 
124 template<class STAGE>
126 public:
127  register_stage_factory( const std::string &name )
128  : stage_factory( name )
129  {
130  }
131 
132  virtual stage_ptr get_new_instance( ai_context &context, const config &cfg ){
133  stage_ptr a = std::make_shared<STAGE>(context, cfg);
134  a->on_create();
135  return a;
136  }
137 };
138 
139 } //end of namespace ai
virtual std::string get_name() const
Definition: stage.cpp:85
std::shared_ptr< stage > stage_ptr
Definition: game_info.hpp:101
virtual std::string get_engine() const
Definition: stage.cpp:80
std::map< std::string, factory_ptr > factory_map
Definition: stage.hpp:98
stage_factory(const std::string &name)
Definition: stage.hpp:111
bool play_stage()
Play the turn - strategy.
Definition: stage.cpp:56
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: stage.hpp:99
recursion_counter recursion_counter_
Definition: stage.hpp:77
#define a
virtual std::string get_id() const
Definition: stage.cpp:75
virtual ~stage()
Destructor.
Definition: stage.cpp:52
A small explanation about what&#39;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:101
const t_string name
A component of the AI framework.
virtual void on_create()
Initialization.
Definition: stage.cpp:47
config cfg_
Definition: stage.hpp:79
stage(ai_context &context, const config &cfg)
Constructor.
Definition: stage.cpp:41
std::shared_ptr< stage_factory > factory_ptr
Definition: stage.hpp:97
virtual config to_config() const
serialize
Definition: stage.cpp:66
virtual bool do_play_stage()=0
Play the turn - implementation.
register_stage_factory(const std::string &name)
Definition: stage.hpp:127
virtual ~stage_factory()
Definition: stage.hpp:120
Composite AI contexts.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
int get_recursion_count() const
get the value of the recursion counter
Definition: stage.cpp:61
virtual stage_ptr get_new_instance(ai_context &context, const config &cfg)
Definition: stage.hpp:132