-
-
Notifications
You must be signed in to change notification settings - Fork 994
/
stage.hpp
162 lines (120 loc) · 3.21 KB
/
stage.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Copyright (C) 2009 - 2015 by Yurii Chernyi <terraninfo@terraninfo.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Composite AI stages
*/
#ifndef AI_COMPOSITE_STAGE_HPP_INCLUDED
#define AI_COMPOSITE_STAGE_HPP_INCLUDED
#include "component.hpp"
#include "contexts.hpp"
#ifdef _MSC_VER
#pragma warning(push)
//silence "inherits via dominance" warnings
#pragma warning(disable:4250)
#endif
namespace ai {
class ai_composite;
class stage : public virtual ai_context_proxy, public component {
public:
/**
* Constructor
*/
stage( ai_context &context, const config &cfg );
/**
* Initialization
*/
virtual void on_create();
/**
* Destructor
*/
virtual ~stage();
/**
* Play the turn - strategy
* @return true only if game state has changed. Really only needed for ministages. Returning false is always safe.
*/
bool play_stage();
/**
* get the value of the recursion counter
*/
int get_recursion_count() const;
/**
* serialize
*/
virtual config to_config() const;
virtual std::string get_id() const;
virtual std::string get_name() const;
virtual std::string get_engine() const;
protected:
/**
* Play the turn - implementation
* @return true only if game state has changed. Really only needed for ministages. Returning false is always safe.
*/
virtual bool do_play_stage() = 0;
recursion_counter recursion_counter_;
config cfg_;
};
class idle_stage : public stage {
public:
idle_stage( ai_context &context, const config &cfg );
~idle_stage();
virtual bool do_play_stage();
};
class stage_factory{
public:
typedef boost::shared_ptr< stage_factory > factory_ptr;
typedef std::map<std::string, factory_ptr> factory_map;
typedef std::pair<const std::string, factory_ptr> factory_map_pair;
static factory_map& get_list() {
static factory_map *stage_factories;
if (stage_factories==NULL) {
stage_factories = new factory_map;
}
return *stage_factories;
}
virtual stage_ptr get_new_instance( ai_context &context, const config &cfg ) = 0;
stage_factory( const std::string &name )
{
factory_ptr ptr_to_this(this);
get_list().insert(make_pair(name,ptr_to_this));
}
virtual ~stage_factory() {}
};
template<class STAGE>
class register_stage_factory : public stage_factory {
public:
register_stage_factory( const std::string &name )
: stage_factory( name )
{
}
virtual stage_ptr get_new_instance( ai_context &context, const config &cfg ){
stage_ptr a(new STAGE(context,cfg));
a->on_create();
return a;
}
};
/** this class is a lazily-initializing proxy for a stage **/
class ministage {
public:
ministage(const config &cfg);
virtual ~ministage();
stage_ptr get_stage_ptr(ai_context &context);
config to_config() const;
private:
config cfg_;
stage_ptr stage_;
};
} //end of namespace ai
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif