The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
contexts.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2009 - 2025
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 * @file
18 * Default AI contexts
19 */
20
21#pragma once
22
23#include "ai/ai_target.hpp"
24#include "ai/contexts.hpp"
25
26//============================================================================
27namespace ai {
28
29struct target {
30 target(const map_location& pos, double val, ai_target::type target_type = ai_target::type::village) : loc(pos), value(val), type(target_type)
31 {}
33 double value;
34
35 ai_target::type type;
36};
37
39{
40public:
42 target(),
43 movements(),
44 target_value(0.0),
45 avg_losses(0.0),
46 chance_to_kill(0.0),
50 resources_used(0.0),
51 terrain_quality(0.0),
53 vulnerability(0.0),
54 support(0.0),
55 leader_threat(false),
56 uses_leader(false),
57 is_surrounded(false)
58 {
59 }
60
61 void analyze(const gamemap& map, unit_map& units,
62 const readonly_context& ai_obj,
63 const move_map& dstsrc, const move_map& srcdst,
64 const move_map& enemy_dstsrc, double aggression);
65
66 double rating(double aggression, const readonly_context& ai_obj) const;
67
68 bool attack_close(const map_location& loc) const;
69
71 std::vector<std::pair<map_location,map_location>> movements;
72
73 /** The value of the unit being targeted. */
75
76 /** The value on average, of units lost in the combat. */
77 double avg_losses;
78
79 /** Estimated % chance to kill the unit. */
81
82 /** The average hitpoints damage inflicted. */
84
86
87 /** The average hitpoints damage taken. */
89
90 /** The sum of the values of units used in the attack. */
92
93 /** The weighted average of the % chance to hit each attacking unit. */
95
96 /**
97 * The weighted average of the % defense of the best possible terrain
98 * that the attacking units could reach this turn, without attacking
99 * (good for comparison to see just how good/bad 'terrain_quality' is).
100 */
102
103 /**
104 * The vulnerability is the power projection of enemy units onto the hex
105 * we're standing on. support is the power projection of friendly units.
106 */
108
109 /** Is true if the unit is a threat to our leader. */
111
112 /** Is true if this attack sequence makes use of the leader. */
114
115 /** Is true if the units involved in this attack sequence are surrounded. */
117};
118
120public:
121
122 virtual int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes) = 0;
123
124 /** Constructor */
126
127 /** Destructor */
128 virtual ~default_ai_context();
129
130 virtual const std::vector<target>& additional_targets() const = 0;
131
132 virtual void add_target(const target& t) const = 0;
133
134 virtual void clear_additional_targets() const = 0;
135
137
138 virtual std::vector<target> find_targets(const move_map& enemy_dstsrc) = 0;
139
140 virtual int rate_terrain(const unit& u, const map_location& loc) const = 0;
141
143};
144
145// proxies
147public:
148
149 int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes)
150 {
151 return target_->count_free_hexes_in_castle(loc, checked_hexes);
152 }
153
155 : target_(nullptr)
156 {
157 }
158
160
161 virtual const std::vector<target>& additional_targets() const
162 {
163 return target_->additional_targets();
164 }
165
166 virtual void add_target(const target& t) const
167 {
169 }
170
171 virtual void clear_additional_targets() const
172 {
174 }
175
180
181 virtual std::vector<target> find_targets(const move_map& enemy_dstsrc)
182 {
183 return target_->find_targets(enemy_dstsrc);
184 }
185
187
188 virtual int rate_terrain(const unit& u, const map_location& loc) const
189 {
190 return target_->rate_terrain(u,loc);
191 }
192
197
198private:
200};
201
203public:
204
205 int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes);
206
212
213 virtual ~default_ai_context_impl();
214
216
217 virtual const std::vector<target>& additional_targets() const;
218
219 virtual void add_target(const target& t) const;
220
221 virtual void clear_additional_targets() const;
222
224 {
226 }
227
228 virtual std::vector<target> find_targets(const move_map& enemy_dstsrc);
229
230 virtual int rate_terrain(const unit& u, const map_location& loc) const;
231
232 virtual config to_default_ai_context_config() const;
233
234private:
236 mutable std::vector<target> additional_targets_;// TODO: refactor this (remove mutable)
237
238};
239
240} //end of namespace ai
map_location loc
Definition move.cpp:172
double t
std::vector< std::pair< map_location, map_location > > movements
Definition contexts.hpp:71
void analyze(const gamemap &map, unit_map &units, const readonly_context &ai_obj, const move_map &dstsrc, const move_map &srcdst, const move_map &enemy_dstsrc, double aggression)
Definition attack.cpp:42
bool uses_leader
Is true if this attack sequence makes use of the leader.
Definition contexts.hpp:113
map_location target
Definition contexts.hpp:70
double target_value
The value of the unit being targeted.
Definition contexts.hpp:74
double avg_damage_inflicted
The average hitpoints damage inflicted.
Definition contexts.hpp:83
double chance_to_kill
Estimated % chance to kill the unit.
Definition contexts.hpp:80
bool attack_close(const map_location &loc) const
Definition attack.cpp:255
double terrain_quality
The weighted average of the % chance to hit each attacking unit.
Definition contexts.hpp:94
double avg_damage_taken
The average hitpoints damage taken.
Definition contexts.hpp:88
double alternative_terrain_quality
The weighted average of the % defense of the best possible terrain that the attacking units could rea...
Definition contexts.hpp:101
bool leader_threat
Is true if the unit is a threat to our leader.
Definition contexts.hpp:110
double avg_losses
The value on average, of units lost in the combat.
Definition contexts.hpp:77
double vulnerability
The vulnerability is the power projection of enemy units onto the hex we're standing on.
Definition contexts.hpp:107
double resources_used
The sum of the values of units used in the attack.
Definition contexts.hpp:91
double rating(double aggression, const readonly_context &ai_obj) const
Definition attack.cpp:268
bool is_surrounded
Is true if the units involved in this attack sequence are surrounded.
Definition contexts.hpp:116
virtual default_ai_context & get_default_ai_context()
Definition contexts.cpp:90
virtual void clear_additional_targets() const
Definition contexts.cpp:277
recursion_counter recursion_counter_
Definition contexts.hpp:235
int get_recursion_count() const
Get the value of the recursion counter.
Definition contexts.hpp:223
virtual const std::vector< target > & additional_targets() const
Definition contexts.cpp:267
virtual int rate_terrain(const unit &u, const map_location &loc) const
Definition contexts.cpp:94
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)
Definition contexts.cpp:124
default_ai_context_impl(readwrite_context &context, const config &)
Definition contexts.hpp:207
virtual config to_default_ai_context_config() const
Definition contexts.cpp:282
int count_free_hexes_in_castle(const map_location &loc, std::set< map_location > &checked_hexes)
Definition contexts.cpp:67
virtual void add_target(const target &t) const
Definition contexts.cpp:272
std::vector< target > additional_targets_
Definition contexts.hpp:236
virtual int rate_terrain(const unit &u, const map_location &loc) const
Definition contexts.hpp:188
virtual void clear_additional_targets() const
Definition contexts.hpp:171
int count_free_hexes_in_castle(const map_location &loc, std::set< map_location > &checked_hexes)
Definition contexts.hpp:149
virtual const std::vector< target > & additional_targets() const
Definition contexts.hpp:161
default_ai_context * target_
Definition contexts.hpp:199
virtual default_ai_context & get_default_ai_context()
Definition contexts.hpp:176
virtual config to_default_ai_context_config() const
Definition contexts.hpp:193
virtual void add_target(const target &t) const
Definition contexts.hpp:166
void init_default_ai_context_proxy(default_ai_context &target)
Definition contexts.cpp:57
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)
Definition contexts.hpp:181
default_ai_context()
Constructor.
Definition contexts.cpp:43
virtual const std::vector< target > & additional_targets() const =0
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)=0
virtual default_ai_context & get_default_ai_context()=0
virtual int count_free_hexes_in_castle(const map_location &loc, std::set< map_location > &checked_hexes)=0
virtual ~default_ai_context()
Destructor.
Definition contexts.cpp:47
virtual void clear_additional_targets() const =0
virtual int rate_terrain(const unit &u, const map_location &loc) const =0
virtual void add_target(const target &t) const =0
virtual config to_default_ai_context_config() const =0
void init_readwrite_context_proxy(readwrite_context &target)
Definition contexts.hpp:874
int get_count() const
Get the current value of the recursion counter.
Definition contexts.hpp:65
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
Encapsulates the map of the game.
Definition map.hpp:176
Container associating units to locations.
Definition map.hpp:98
This class represents a single unit of a specific type.
Definition unit.hpp:39
Helper functions for the object which operates in the context of AI for specific side this is part of...
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
map_location loc
Definition contexts.hpp:32
target(const map_location &pos, double val, ai_target::type target_type=ai_target::type::village)
Definition contexts.hpp:30
ai_target::type type
Definition contexts.hpp:35
double value
Definition contexts.hpp:33
Encapsulates the map of the game.
Definition location.hpp:46