The Battle for Wesnoth  1.15.12+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  * Destructor
44  */
45  virtual ~stage();
46 
47  /**
48  * Play the turn - strategy
49  * @return true only if game state has changed. Returning false is always safe.
50  */
51  bool play_stage();
52 
53  /**
54  * get the value of the recursion counter
55  */
56  int get_recursion_count() const;
57 
58  /**
59  * serialize
60  */
61  virtual config to_config() const;
62 
63  virtual std::string get_id() const;
64  virtual std::string get_name() const;
65  virtual std::string get_engine() const;
66 
67 protected:
68  /**
69  * Play the turn - implementation
70  * @return true only if game state has changed. Returning false is always safe.
71  */
72  virtual bool do_play_stage() = 0;
73 
75 
77 
78 };
79 
80 class idle_stage : public stage {
81 public:
82  idle_stage( ai_context &context, const config &cfg );
83 
84  ~idle_stage();
85 
86  virtual bool do_play_stage();
87 };
88 
90  bool is_duplicate(const std::string &name);
91 public:
92  typedef std::shared_ptr< stage_factory > factory_ptr;
93  typedef std::map<std::string, factory_ptr> factory_map;
94  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
95 
96  static factory_map& get_list() {
97  static factory_map *stage_factories;
98  if (stage_factories==nullptr) {
99  stage_factories = new factory_map;
100  }
101  return *stage_factories;
102  }
103 
104  virtual stage_ptr get_new_instance( ai_context &context, const config &cfg ) = 0;
105 
106  stage_factory( const std::string &name )
107  {
108  if (is_duplicate(name)) {
109  return;
110  }
111  factory_ptr ptr_to_this(this);
112  get_list().emplace(name,ptr_to_this);
113  }
114 
115  virtual ~stage_factory() {}
116 };
117 
118 template<class STAGE>
120 public:
121  register_stage_factory( const std::string &name )
122  : stage_factory( name )
123  {
124  }
125 
126  virtual stage_ptr get_new_instance( ai_context &context, const config &cfg ){
127  stage_ptr a = std::make_shared<STAGE>(context, cfg);
128  a->on_create();
129  return a;
130  }
131 };
132 
133 } //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:93
stage_factory(const std::string &name)
Definition: stage.hpp:106
bool play_stage()
Play the turn - strategy.
Definition: stage.cpp:56
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: stage.hpp:94
recursion_counter recursion_counter_
Definition: stage.hpp:74
#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:59
static factory_map & get_list()
Definition: stage.hpp:96
A component of the AI framework.
virtual void on_create()
Initialization.
Definition: stage.cpp:47
config cfg_
Definition: stage.hpp:76
stage(ai_context &context, const config &cfg)
Constructor.
Definition: stage.cpp:41
std::shared_ptr< stage_factory > factory_ptr
Definition: stage.hpp:92
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:121
virtual ~stage_factory()
Definition: stage.hpp:115
Composite AI contexts.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
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:126