The Battle for Wesnoth  1.19.5+dev
stage.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
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 
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 bool do_play_stage()
Play the turn - implementation.
Definition: stage.cpp:102
idle_stage(ai_context &context, const config &cfg)
Definition: stage.cpp:93
virtual stage_ptr get_new_instance(ai_context &context, const config &cfg)
Definition: stage.hpp:126
register_stage_factory(const std::string &name)
Definition: stage.hpp:121
virtual stage_ptr get_new_instance(ai_context &context, const config &cfg)=0
std::map< std::string, factory_ptr > factory_map
Definition: stage.hpp:93
bool is_duplicate(const std::string &name)
Definition: stage.cpp:108
stage_factory(const std::string &name)
Definition: stage.hpp:106
static factory_map & get_list()
Definition: stage.hpp:96
std::shared_ptr< stage_factory > factory_ptr
Definition: stage.hpp:92
virtual ~stage_factory()
Definition: stage.hpp:115
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: stage.hpp:94
int get_recursion_count() const
get the value of the recursion counter
Definition: stage.cpp:60
recursion_counter recursion_counter_
Definition: stage.hpp:74
virtual std::string get_engine() const
Definition: stage.cpp:79
virtual std::string get_id() const
Definition: stage.cpp:74
virtual ~stage()
Destructor.
Definition: stage.cpp:51
config cfg_
Definition: stage.hpp:76
bool play_stage()
Play the turn - strategy.
Definition: stage.cpp:55
stage(ai_context &context, const config &cfg)
Constructor.
Definition: stage.cpp:40
virtual bool do_play_stage()=0
Play the turn - implementation.
virtual config to_config() const
serialize
Definition: stage.cpp:65
virtual void on_create()
Initialization.
Definition: stage.cpp:46
virtual std::string get_name() const
Definition: stage.cpp:84
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
A component of the AI framework.
Composite AI contexts.
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
std::shared_ptr< stage > stage_ptr
Definition: game_info.hpp:102