1 
2 /*
3    Copyright (C) 2009 - 2018 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:
44 	move_result_ptr move_;
45 };
46 
47 //============================================================================
48 
49 class combat_phase : public candidate_action {
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:
60 	attack_analysis best_analysis_;
61 	double choice_rating_;
62 
63 };
64 
65 //============================================================================
66 
67 class move_leader_to_goals_phase : public candidate_action {
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 
81 	bool auto_remove_;
82 	map_location dst_;
83 	std::string id_;
84 	move_result_ptr move_;
85 };
86 
87 //============================================================================
88 
89 class move_leader_to_keep_phase : public candidate_action {
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:
101 	move_result_ptr move_;
102 };
103 
104 //============================================================================
105 
106 class get_villages_phase : public candidate_action {
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. */
118 	map_location keep_loc_;
119 
120 	/** Locaton of our leader. */
121 	map_location leader_loc_;
122 
123 	/** The best possible location for our leader if it can't reach a village. */
124 	map_location best_leader_loc_;
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, 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 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 
197 class get_healing_phase : public candidate_action {
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 
209 	move_result_ptr move_;
210 };
211 
212 //============================================================================
213 
214 class retreat_phase : public candidate_action {
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 
228 	move_result_ptr move_;
229 
230 };
231 
232 
233 //============================================================================
234 
235 class leader_control_phase : public candidate_action {
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 //============================================================================
249 class leader_shares_keep_phase : public candidate_action {
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
267