The Battle for Wesnoth  1.19.5+dev
ca.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  * 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 
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 
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  // The list of moves we want to make
137 
138  /** Dispatches all units to their best location. */
139  void dispatch(treachmap& reachmap, tmoves& moves);
140 
141  /**
142  * Dispatches all units who can reach one village.
143  * Returns true if it modified reachmap isn't empty.
144  */
145  bool dispatch_unit_simple(treachmap& reachmap, tmoves& moves);
146 
147  /*
148  * Dispatches units to villages which can only be reached by one unit.
149  * Returns true if modified reachmap and reachmap isn't empty.
150  */
152  treachmap& reachmap, tmoves& moves, std::size_t& village_count);
153 
154  /** Removes a village for all units, returns true if anything is deleted. */
155  bool remove_village(
156  treachmap& reachmap, tmoves& moves, const map_location& village);
157 
158  /** Removes a unit which can't reach any village anymore. */
160  treachmap& reachmap, tmoves& moves, treachmap::iterator unit);
161 
162  /** Dispatches the units to a village after the simple dispatching failed. */
163  void dispatch_complex(
164  treachmap& reachmap, tmoves& moves, const std::size_t village_count);
165 
166  /** Dispatches all units to a village, every unit can reach every village. */
167  void full_dispatch(treachmap& reachmap, tmoves& moves);
168 
169  /** Shows which villages every unit can reach (debug function). */
170  void dump_reachmap(treachmap& reachmap);
171 
172  void get_villages(
173  const move_map &dstsrc, const move_map &enemy_dstsrc,
174  unit_map::const_iterator &leader);
175 
176  void find_villages(
177  treachmap& reachmap,
178  tmoves& moves,
179  const std::multimap<map_location,map_location>& dstsrc,
180  const std::multimap<map_location,map_location>& enemy_dstsrc);
181 
182 };
183 
184 //============================================================================
185 
187 public:
188 
189  get_healing_phase( rca_context &context, const config& cfg );
190 
191  virtual ~get_healing_phase();
192 
193  virtual double evaluate();
194 
195  virtual void execute();
196 private:
197 
199 };
200 
201 //============================================================================
202 
204 public:
205 
206  retreat_phase( rca_context &context, const config &cfg );
207 
208  virtual ~retreat_phase();
209 
210  virtual double evaluate();
211 
212  virtual void execute();
213 private:
214 
215  bool should_retreat(const map_location& loc, const unit_map::const_iterator& un, const move_map &srcdst, const move_map &dstsrc, double caution);
216 
218 
219 };
220 
221 //============================================================================
222 
224 public:
225 
226  leader_control_phase( rca_context &context, const config &cfg );
227 
228  virtual ~leader_control_phase();
229 
230  virtual double evaluate();
231 
232  virtual void execute();
233 };
234 
235 //============================================================================
237 public:
238 
239  leader_shares_keep_phase( rca_context &context, const config &cfg );
240 
241  virtual ~leader_shares_keep_phase();
242 
243  virtual double evaluate();
244 
245  virtual void execute();
246 };
247 
248 //============================================================================
249 
250 } // end of namespace testing_ai_default
251 
252 } // end of namespace ai
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:236
attack_analysis best_analysis_
Definition: ca.hpp:60
combat_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:152
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:161
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:1377
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:1321
get_healing_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:1312
void dump_reachmap(treachmap &reachmap)
Shows which villages every unit can reach (debug function).
Definition: ca.cpp:1285
bool dispatch_village_simple(treachmap &reachmap, tmoves &moves, std::size_t &village_count)
Definition: ca.cpp:878
get_villages_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:533
map_location keep_loc_
Location of the keep the closest to our leader.
Definition: ca.hpp:118
treachmap::iterator remove_unit(treachmap &reachmap, tmoves &moves, treachmap::iterator unit)
Removes a unit which can't reach any village anymore.
Definition: ca.cpp:947
map_location leader_loc_
Locaton of our leader.
Definition: ca.hpp:121
bool dispatch_unit_simple(treachmap &reachmap, tmoves &moves)
Dispatches all units who can reach one village.
Definition: ca.cpp:834
std::map< map_location, std::vector< map_location > > treachmap
Definition: ca.hpp:130
void dispatch(treachmap &reachmap, tmoves &moves)
Dispatches all units to their best location.
Definition: ca.cpp:774
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:547
void dispatch_complex(treachmap &reachmap, tmoves &moves, const std::size_t village_count)
Dispatches the units to a village after the simple dispatching failed.
Definition: ca.cpp:963
bool debug_
debug log level for AI enabled?
Definition: ca.hpp:127
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:930
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:662
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:558
void full_dispatch(treachmap &reachmap, tmoves &moves)
Dispatches all units to a village, every unit can reach every village.
Definition: ca.cpp:1275
void get_villages(const move_map &dstsrc, const move_map &enemy_dstsrc, unit_map::const_iterator &leader)
Definition: ca.cpp:603
std::vector< std::pair< map_location, map_location > > tmoves
Definition: ca.hpp:133
map_location best_leader_loc_
The best possible location for our leader if it can't reach a village.
Definition: ca.hpp:124
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:61
move_result_ptr move_
Definition: ca.hpp:44
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:129
goto_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:51
leader_control_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:1543
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:1558
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:1552
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:1574
leader_shares_keep_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:1565
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:1604
move_leader_to_goals_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:265
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:362
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:274
void remove_goal(const std::string &id)
Definition: ca.cpp:376
move_leader_to_keep_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:387
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:398
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:523
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:1519
retreat_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:1388
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:1397
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:1511
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
This class represents a single unit of a specific type.
Definition: unit.hpp:133
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
std::multimap< map_location, map_location > move_map
The standard way in which a map of possible moves is recorded.
Definition: game_info.hpp:43
std::shared_ptr< move_result > move_result_ptr
Definition: game_info.hpp:85
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
candidate action framework
Encapsulates the map of the game.
Definition: location.hpp:45