The Battle for Wesnoth  1.15.0-dev
ca.hpp
Go to the documentation of this file.
1 
2 /*
3  Copyright (C) 2009 - 2018 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 namespace ai {
28 
29 namespace ai_default_rca {
30 
31 //============================================================================
32 
33 class goto_phase : public candidate_action {
34 public:
35 
36  goto_phase( rca_context &context, const config &cfg );
37 
38  virtual ~goto_phase();
39 
40  virtual double evaluate();
41 
42  virtual void execute();
43 private:
45 };
46 
47 //============================================================================
48 
50 public:
51 
52  combat_phase( rca_context &context, const config &cfg );
53 
54  virtual ~combat_phase();
55 
56  virtual double evaluate();
57 
58  virtual void execute();
59 private:
62 
63 };
64 
65 //============================================================================
66 
68 public:
69 
70  move_leader_to_goals_phase( rca_context &context, const config &cfg );
71 
72  virtual ~move_leader_to_goals_phase();
73 
74  virtual double evaluate();
75 
76  virtual void execute();
77 private:
78 
79  void remove_goal(const std::string &id);
80 
83  std::string id_;
85 };
86 
87 //============================================================================
88 
90 public:
91 
92  move_leader_to_keep_phase( rca_context &context, const config &cfg );
93 
94  virtual ~move_leader_to_keep_phase();
95 
96  virtual double evaluate();
97 
98  virtual void execute();
99 
100 private:
102 };
103 
104 //============================================================================
105 
107 public:
108 
109  get_villages_phase( rca_context &context, const config& cfg );
110 
111  virtual ~get_villages_phase();
112 
113  virtual double evaluate();
114 
115  virtual void execute();
116 private:
117  /** Location of the keep the closest to our leader. */
119 
120  /** Locaton of our leader. */
122 
123  /** The best possible location for our leader if it can't reach a village. */
125 
126  /** debug log level for AI enabled? */
127  bool debug_;
128 
129  typedef std::map<map_location /* unit location */,
130  std::vector<map_location /* villages we can reach */>> treachmap;
131 
132  typedef std::vector<std::pair<map_location /* destination */,
133  map_location /* start */ >> tmoves;
134 
135 
136  // The list of moves we want to make
137  tmoves moves_;
138 
139 
140  /** Dispatches all units to their best location. */
141  void dispatch(treachmap& reachmap, tmoves& moves);
142 
143 
144  /**
145  * Dispatches all units who can reach one village.
146  * Returns true if it modified reachmap isn't empty.
147  */
148  bool dispatch_unit_simple(treachmap& reachmap, tmoves& moves);
149 
150 
151  /*
152  * Dispatches units to villages which can only be reached by one unit.
153  * Returns true if modified reachmap and reachmap isn't empty.
154  */
155  bool dispatch_village_simple(
156  treachmap& reachmap, tmoves& moves, std::size_t& village_count);
157 
158 
159  /** Removes a village for all units, returns true if anything is deleted. */
160  bool remove_village(
161  treachmap& reachmap, tmoves& moves, const map_location& village);
162 
163 
164  /** Removes a unit which can't reach any village anymore. */
165  treachmap::iterator remove_unit(
166  treachmap& reachmap, tmoves& moves, treachmap::iterator unit);
167 
168 
169  /** Dispatches the units to a village after the simple dispatching failed. */
170  void dispatch_complex(
171  treachmap& reachmap, tmoves& moves, const std::size_t village_count);
172 
173 
174  /** Dispatches all units to a village, every unit can reach every village. */
175  void full_dispatch(treachmap& reachmap, tmoves& moves);
176 
177 
178  /** Shows which villages every unit can reach (debug function). */
179  void dump_reachmap(treachmap& reachmap);
180 
181 
182  void get_villages(
183  const move_map &dstsrc, const move_map &enemy_dstsrc,
184  unit_map::const_iterator &leader);
185 
186 
187  void find_villages(
188  treachmap& reachmap,
189  tmoves& moves,
190  const std::multimap<map_location,map_location>& dstsrc,
191  const std::multimap<map_location,map_location>& enemy_dstsrc);
192 
193 };
194 
195 //============================================================================
196 
198 public:
199 
200  get_healing_phase( rca_context &context, const config& cfg );
201 
202  virtual ~get_healing_phase();
203 
204  virtual double evaluate();
205 
206  virtual void execute();
207 private:
208 
210 };
211 
212 //============================================================================
213 
215 public:
216 
217  retreat_phase( rca_context &context, const config &cfg );
218 
219  virtual ~retreat_phase();
220 
221  virtual double evaluate();
222 
223  virtual void execute();
224 private:
225 
226  bool should_retreat(const map_location& loc, const unit_map::const_iterator& un, const move_map &srcdst, const move_map &dstsrc, double caution);
227 
229 
230 };
231 
232 
233 //============================================================================
234 
236 public:
237 
238  leader_control_phase( rca_context &context, const config &cfg );
239 
240  virtual ~leader_control_phase();
241 
242  virtual double evaluate();
243 
244  virtual void execute();
245 };
246 
247 
248 //============================================================================
250 public:
251 
252  leader_shares_keep_phase( rca_context &context, const config &cfg );
253 
254  virtual ~leader_shares_keep_phase();
255 
256  virtual double evaluate();
257 
258  virtual void execute();
259 };
260 
261 
262 //============================================================================
263 
264 } // end of namespace testing_ai_default
265 
266 } // end of namespace ai
int dispatch(lua_State *L)
move_result_ptr move_
Definition: ca.hpp:44
std::vector< std::pair< map_location, map_location > > tmoves
Definition: ca.hpp:133
This class represents a single unit of a specific type.
Definition: unit.hpp:99
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&#39;t reach a village.
Definition: ca.hpp:124
map_location keep_loc_
Location of the keep the closest to our leader.
Definition: ca.hpp:118
std::multimap< map_location, map_location > move_map
The standard way in which a map of possible moves is recorded.
Definition: game_info.hpp:42
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
Encapsulates the map of the game.
Definition: location.hpp:42
map_location leader_loc_
Locaton of our leader.
Definition: ca.hpp:121
bool debug_
debug log level for AI enabled?
Definition: ca.hpp:127
std::map< map_location, std::vector< map_location > > treachmap
Definition: ca.hpp:130
std::unique_ptr< move_result > move_result_ptr
Definition: game_info.hpp:84
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:65
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:129
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
candidate action framework
attack_analysis best_analysis_
Definition: ca.hpp:60