The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ca.hpp
Go to the documentation of this file.
1 
2 /*
3  Copyright (C) 2009 - 2017 by Yurii Chernyi <terraninfo@terraninfo.net>
4  Part of the Battle for Wesnoth Project http://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  * Default AI (Testing)
18  * @file
19  */
20 
21 #pragma once
22 
23 #include "units/map.hpp"
24 
25 #include "ai/composite/rca.hpp"
26 
27 
28 #ifdef _MSC_VER
29 #pragma warning(push)
30 //silence "inherits via dominance" warnings
31 #pragma warning(disable:4250)
32 #endif
33 
34 namespace ai {
35 
36 namespace ai_default_rca {
37 
38 //============================================================================
39 
40 class goto_phase : public candidate_action {
41 public:
42 
43  goto_phase( rca_context &context, const config &cfg );
44 
45  virtual ~goto_phase();
46 
47  virtual double evaluate();
48 
49  virtual void execute();
50 private:
52 };
53 
54 //============================================================================
55 
57 public:
58 
59  combat_phase( rca_context &context, const config &cfg );
60 
61  virtual ~combat_phase();
62 
63  virtual double evaluate();
64 
65  virtual void execute();
66 private:
69 
70 };
71 
72 //============================================================================
73 
75 public:
76 
77  move_leader_to_goals_phase( rca_context &context, const config &cfg );
78 
80 
81  virtual double evaluate();
82 
83  virtual void execute();
84 private:
85 
86  void remove_goal(const std::string &id);
87 
92 };
93 
94 //============================================================================
95 
97 public:
98 
99  move_leader_to_keep_phase( rca_context &context, const config &cfg );
100 
101  virtual ~move_leader_to_keep_phase();
102 
103  virtual double evaluate();
104 
105  virtual void execute();
106 
107 private:
109 };
110 
111 //============================================================================
112 
114 public:
115 
116  get_villages_phase( rca_context &context, const config& cfg );
117 
118  virtual ~get_villages_phase();
119 
120  virtual double evaluate();
121 
122  virtual void execute();
123 private:
124  /** Location of the keep the closest to our leader. */
126 
127  /** Locaton of our leader. */
129 
130  /** The best possible location for our leader if it can't reach a village. */
132 
133  /** debug log level for AI enabled? */
134  bool debug_;
135 
136  typedef std::map<map_location /* unit location */,
137  std::vector<map_location /* villages we can reach */> > treachmap;
138 
139  typedef std::vector<std::pair<map_location /* destination */,
140  map_location /* start */ > > tmoves;
141 
142 
143  // The list of moves we want to make
144  tmoves moves_;
145 
146 
147  /** Dispatches all units to their best location. */
148  void dispatch(treachmap& reachmap, tmoves& moves);
149 
150 
151  /**
152  * Dispatches all units who can reach one village.
153  * Returns true if it modified reachmap isn't empty.
154  */
155  bool dispatch_unit_simple(treachmap& reachmap, tmoves& moves);
156 
157 
158  /*
159  * Dispatches units to villages which can only be reached by one unit.
160  * Returns true if modified reachmap and reachmap isn't empty.
161  */
163  treachmap& reachmap, tmoves& moves, size_t& village_count);
164 
165 
166  /** Removes a village for all units, returns true if anything is deleted. */
167  bool remove_village(
168  treachmap& reachmap, tmoves& moves, const map_location& village);
169 
170 
171  /** Removes a unit which can't reach any village anymore. */
173  treachmap& reachmap, tmoves& moves, treachmap::iterator unit);
174 
175 
176  /** Dispatches the units to a village after the simple dispatching failed. */
177  void dispatch_complex(
178  treachmap& reachmap, tmoves& moves, const size_t village_count);
179 
180 
181  /** Dispatches all units to a village, every unit can reach every village. */
182  void full_dispatch(treachmap& reachmap, tmoves& moves);
183 
184 
185  /** Shows which villages every unit can reach (debug function). */
186  void dump_reachmap(treachmap& reachmap);
187 
188 
189  void get_villages(
190  const move_map &dstsrc, const move_map &enemy_dstsrc,
191  unit_map::const_iterator &leader);
192 
193 
194  void find_villages(
195  treachmap& reachmap,
196  tmoves& moves,
197  const std::multimap<map_location,map_location>& dstsrc,
198  const std::multimap<map_location,map_location>& enemy_dstsrc);
199 
200 };
201 
202 //============================================================================
203 
205 public:
206 
207  get_healing_phase( rca_context &context, const config& cfg );
208 
209  virtual ~get_healing_phase();
210 
211  virtual double evaluate();
212 
213  virtual void execute();
214 private:
215 
217 };
218 
219 //============================================================================
220 
222 public:
223 
224  retreat_phase( rca_context &context, const config &cfg );
225 
226  virtual ~retreat_phase();
227 
228  virtual double evaluate();
229 
230  virtual void execute();
231 private:
232 
233  bool should_retreat(const map_location& loc, const unit_map::const_iterator& un, const move_map &srcdst, const move_map &dstsrc, double caution);
234 
236 
237 };
238 
239 
240 //============================================================================
241 
243 public:
244 
245  leader_control_phase( rca_context &context, const config &cfg );
246 
247  virtual ~leader_control_phase();
248 
249  virtual double evaluate();
250 
251  virtual void execute();
252 };
253 
254 
255 //============================================================================
257 public:
258 
259  leader_shares_keep_phase( rca_context &context, const config &cfg );
260 
261  virtual ~leader_shares_keep_phase();
262 
263  virtual double evaluate();
264 
265  virtual void execute();
266 };
267 
268 
269 //============================================================================
270 
271 } // end of namespace testing_ai_default
272 
273 } // end of namespace ai
274 
275 #ifdef _MSC_VER
276 #pragma warning(pop)
277 #endif
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:221
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:337
std::vector< char_t > string
move_result_ptr move_
Definition: ca.hpp:51
std::vector< std::pair< map_location, map_location > > tmoves
Definition: ca.hpp:140
This class represents a single unit of a specific type.
Definition: unit.hpp:101
void dispatch(treachmap &reachmap, tmoves &moves)
Dispatches all units to their best location.
Definition: ca.cpp:769
goto_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:55
map_location best_leader_loc_
The best possible location for our leader if it can't reach a village.
Definition: ca.hpp:131
void dump_reachmap(treachmap &reachmap)
Shows which villages every unit can reach (debug function).
Definition: ca.cpp:1280
leader_shares_keep_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:1576
map_location keep_loc_
Location of the keep the closest to our leader.
Definition: ca.hpp:125
bool dispatch_village_simple(treachmap &reachmap, tmoves &moves, size_t &village_count)
Definition: ca.cpp:873
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:162
std::shared_ptr< move_result > move_result_ptr
Definition: game_info.hpp:100
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:1317
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:373
move_leader_to_keep_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:362
retreat_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:1384
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:527
bool remove_village(treachmap &reachmap, tmoves &moves, const map_location &village)
Removes a village for all units, returns true if anything is deleted.
Definition: ca.cpp:925
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:1606
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:1393
std::multimap< map_location, map_location > move_map
The standard way in which a map of possible moves is recorded.
Definition: game_info.hpp:45
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:259
bool should_retreat(const map_location &loc, const unit_map::const_iterator &un, const move_map &srcdst, const move_map &dstsrc, double caution)
Definition: ca.cpp:1526
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:503
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:1561
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:1585
leader_control_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:1551
Encapsulates the map of the game.
Definition: location.hpp:40
map_location leader_loc_
Locaton of our leader.
Definition: ca.hpp:128
void dispatch_complex(treachmap &reachmap, tmoves &moves, const size_t village_count)
Dispatches the units to a village after the simple dispatching failed.
Definition: ca.cpp:958
get_healing_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:1308
bool debug_
debug log level for AI enabled?
Definition: ca.hpp:134
bool dispatch_unit_simple(treachmap &reachmap, tmoves &moves)
Dispatches all units who can reach one village.
Definition: ca.cpp:829
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:539
std::map< map_location, std::vector< map_location > > treachmap
Definition: ca.hpp:137
void get_villages(const move_map &dstsrc, const move_map &enemy_dstsrc, unit_map::const_iterator &leader)
Definition: ca.cpp:590
void full_dispatch(treachmap &reachmap, tmoves &moves)
Dispatches all units to a village, every unit can reach every village.
Definition: ca.cpp:1270
treachmap::iterator remove_unit(treachmap &reachmap, tmoves &moves, treachmap::iterator unit)
Removes a unit which can't reach any village anymore.
Definition: ca.cpp:942
get_villages_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:513
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:1569
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:65
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:1373
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
combat_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:153
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:129
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:1516
void remove_goal(const std::string &id)
Definition: ca.cpp:351
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
void find_villages(treachmap &reachmap, tmoves &moves, const std::multimap< map_location, map_location > &dstsrc, const std::multimap< map_location, map_location > &enemy_dstsrc)
Definition: ca.cpp:650
move_leader_to_goals_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:250
candidate action framework
attack_analysis best_analysis_
Definition: ca.hpp:67