-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
goal.cpp
370 lines (299 loc) · 8.84 KB
/
goal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
Copyright (C) 2009 - 2016 by Yurii Chernyi <terraninfo@terraninfo.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
*/
#include "goal.hpp"
#include "global.hpp"
#include "ai/default/contexts.hpp"
#include "ai/lua/core.hpp"
#include "ai/lua/lua_object.hpp"
#include "ai/manager.hpp"
#include "filter_context.hpp"
#include "game_board.hpp"
#include "log.hpp"
#include "map/location.hpp"
#include "resources.hpp"
#include "scripting/game_lua_kernel.hpp"
#include "terrain/filter.hpp"
#include "units/unit.hpp"
#include "units/map.hpp"
#include "units/filter.hpp"
#include "wml_exception.hpp"
#include <boost/lexical_cast.hpp>
#include <boost/foreach.hpp>
#include <set>
#include <sstream>
namespace ai {
static lg::log_domain log_ai_goal("ai/goal");
#define DBG_AI_GOAL LOG_STREAM(debug, log_ai_goal)
#define LOG_AI_GOAL LOG_STREAM(info, log_ai_goal)
#define ERR_AI_GOAL LOG_STREAM(err, log_ai_goal)
goal::goal(readonly_context &context, const config &cfg)
: readonly_context_proxy(), cfg_(cfg), ok_(true)
{
init_readonly_context_proxy(context);
}
void goal::on_create()
{
LOG_AI_GOAL << "side " << get_side() << " : " << " created goal with name=[" << cfg_["name"] << "]" << std::endl;
}
void goal::on_create(boost::shared_ptr<ai::lua_ai_context>)
{
unrecognized();
}
void goal::unrecognized()
{
ERR_AI_GOAL << "side " << get_side() << " : " << " tried to create goal with name=[" << cfg_["name"] << "], but the [" << cfg_["engine"] << "] engine did not recognize that type of goal. " << std::endl;
ok_ = false;
}
goal::~goal()
{
}
void goal::add_targets(std::back_insert_iterator< std::vector< target > > /*target_list*/)
{
}
config goal::to_config() const
{
return cfg_;
}
std::string goal::get_id() const
{
return cfg_["id"];
}
std::string goal::get_name() const
{
return cfg_["id"];
}
std::string goal::get_engine() const
{
return cfg_["engine"];
}
bool goal::redeploy(const config &cfg)
{
cfg_ = cfg;
on_create();
return true;
}
bool goal::ok() const
{
return ok_;
}
bool goal::active() const
{
return is_active(cfg_["time_of_day"],cfg_["turns"]);
}
void target_unit_goal::on_create()
{
goal::on_create();
if (cfg_["engine"] != "cpp") {
unrecognized();
value_ = 0;
return;
}
if (const config::attribute_value *v = cfg_.get("value")) {
try {
value_ = boost::lexical_cast<double>(*v);
} catch (boost::bad_lexical_cast){
ERR_AI_GOAL << "bad value of goal"<<std::endl;
value_ = 0;
}
}
}
void target_unit_goal::add_targets(std::back_insert_iterator< std::vector< target > > target_list)
{
if (!(this)->active()) {
return;
}
const config &criteria = cfg_.child("criteria");
if (!criteria) return;
//find the enemy leaders and explicit targets
const unit_filter ufilt(vconfig(criteria), resources::filter_con);
BOOST_FOREACH(const unit &u, *resources::units) {
if (ufilt( u )) {
LOG_AI_GOAL << "found explicit target unit at ... " << u.get_location() << " with value: " << value() << "\n";
*target_list = target(u.get_location(), value(), target::TYPE::EXPLICIT);
}
}
}
target_unit_goal::target_unit_goal(readonly_context &context, const config &cfg)
: goal(context,cfg)
, value_(0.0)
{
}
void target_location_goal::on_create()
{
goal::on_create();
if (cfg_["engine"] != "cpp") {
unrecognized();
value_ = 0;
return;
}
if (cfg_.has_attribute("value")) {
try {
value_ = boost::lexical_cast<double>(cfg_["value"]);
} catch (boost::bad_lexical_cast){
ERR_AI_GOAL << "bad value of goal"<<std::endl;
value_ = 0;
}
}
const config &criteria = cfg_.child("criteria");
if (criteria) {
filter_ptr_ = boost::shared_ptr<terrain_filter>(new terrain_filter(vconfig(criteria),resources::filter_con));
}
}
void target_location_goal::add_targets(std::back_insert_iterator< std::vector< target > > target_list)
{
if (!(this)->active()) {
return;
}
if (!filter_ptr_) return;
std::set<map_location> items;
filter_ptr_->get_locations(items);
BOOST_FOREACH(const map_location &loc, items)
{
LOG_AI_GOAL << "found explicit target location ... " << loc << " with value: " << value() << std::endl;
*target_list = target(loc, value(), target::TYPE::EXPLICIT);
}
}
target_location_goal::target_location_goal(readonly_context &context, const config &cfg)
: goal(context,cfg)
, filter_ptr_()
, value_(0.0)
{
}
void protect_goal::on_create()
{
goal::on_create();
if (cfg_["engine"] != "cpp") {
unrecognized();
value_ = 0;
return;
}
if (const config::attribute_value *v = cfg_.get("value")) {
try {
value_ = boost::lexical_cast<double>(*v);
} catch (boost::bad_lexical_cast){
ERR_AI_GOAL << "bad value of protect_goal"<<std::endl;
value_ = 0;
}
}
if (const config::attribute_value *v = cfg_.get("protect_radius")) {
try {
radius_ = boost::lexical_cast<int>(*v);
} catch (boost::bad_lexical_cast){
ERR_AI_GOAL << "bad protection radius of protect_goal"<<std::endl;
radius_ = 1;
}
}
if (radius_<1) {
radius_=20;
}
const config &criteria = cfg_.child("criteria");
if (criteria) {
filter_ptr_ = boost::shared_ptr<terrain_filter>(new terrain_filter(vconfig(criteria),resources::filter_con));
}
}
void protect_goal::add_targets(std::back_insert_iterator< std::vector< target > > target_list)
{
std::string goal_type;
if (protect_unit_) {
goal_type = "protect_unit";
} else {
goal_type ="protect_location";
}
if (!(this)->active()) {
LOG_AI_GOAL << "skipping " << goal_type << " goal - not active" << std::endl;
return;
}
const config &criteria = cfg_.child("criteria");
if (!criteria) {
LOG_AI_GOAL << "skipping " << goal_type << " goal - no criteria given" << std::endl;
return;
} else {
DBG_AI_GOAL << "side " << get_side() << ": "<< goal_type << " goal with criteria" << std::endl << cfg_.child("criteria") << std::endl;
}
unit_map &units = *resources::units;
std::set<map_location> items;
if (protect_unit_) {
const unit_filter ufilt(vconfig(criteria), resources::filter_con);
BOOST_FOREACH(const unit &u, units)
{
//TODO: we will protect hidden units, by not testing for invisibility to current side
if (ufilt(u)) {
DBG_AI_GOAL << "side " << get_side() << ": in " << goal_type << ": " << u.get_location() << " should be protected\n";
items.insert(u.get_location());
}
}
} else {
filter_ptr_->get_locations(items);
}
DBG_AI_GOAL << "side " << get_side() << ": seaching for threats in "+goal_type+" goal" << std::endl;
// Look for directions to protect a specific location or specific unit.
BOOST_FOREACH(const map_location &loc, items)
{
BOOST_FOREACH(const unit &u, units)
{
int distance = distance_between(u.get_location(), loc);
if (current_team().is_enemy(u.side()) && distance < radius_ &&
!u.invisible(u.get_location()))
{
DBG_AI_GOAL << "side " << get_side() << ": in " << goal_type << ": found threat target. " << u.get_location() << " is a threat to "<< loc << '\n';
*target_list = target(u.get_location(),
value_ * double(radius_ - distance) /
radius_, target::TYPE::THREAT);
}
}
}
}
protect_goal::protect_goal(readonly_context &context, const config &cfg, bool protect_unit)
: goal(context,cfg)
, filter_ptr_()
, protect_unit_(protect_unit)
, radius_(20) //this default radius is taken from old code
, value_(1.0) //this default value taken from old code
{
}
lua_goal::lua_goal(readonly_context &context, const config &cfg)
: goal(context, cfg)
, code_()
, handler_()
{
if (cfg.has_attribute("code")) {
code_ = cfg["code"].str();
}
else
{
ERR_AI_GOAL << "side " << get_side() << " : Error creating Lua goal (missing code= key)" << std::endl;
}
}
void lua_goal::on_create(boost::shared_ptr<ai::lua_ai_context> l_ctx)
{
handler_ = boost::shared_ptr<lua_ai_action_handler>(resources::lua_kernel->create_lua_ai_action_handler(code_.c_str(), *l_ctx));
}
void lua_goal::add_targets(std::back_insert_iterator< std::vector< target > > target_list)
{
boost::shared_ptr< lua_object< std::vector < target > > > l_obj
= boost::shared_ptr< lua_object< std::vector < target > > >(new lua_object< std::vector < target > >());
config c(cfg_.child_or_empty("args"));
handler_->handle(c, true, l_obj);
try {
std::vector < target > targets = *(l_obj->get());
BOOST_FOREACH(target tg, targets)
{
*target_list = tg;
}
} catch(bad_enum_cast& e) {
ERR_AI_GOAL << "A Lua goal returned a target of an unknown type (\"" << e.value() << "\"; unfortunately, the engine cannot recover from this error. As a result, all targets returned by the goal have been lost.\n";
}
}
} //end of namespace ai