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