/
stage_unit_formulas.cpp
158 lines (132 loc) · 4.81 KB
/
stage_unit_formulas.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
/*
Copyright (C) 2009 - 2018 by Yurii Chernyi <terraninfo@terraninfo.net>
Part of the Battle for Wesnoth Project https://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
* Defines formula ai unit formulas stage
* */
#include "ai/formula/stage_unit_formulas.hpp"
#include "ai/formula/ai.hpp"
#include "formula/formula.hpp"
#include "formula/function.hpp"
#include "game_board.hpp"
#include "log.hpp"
#include "resources.hpp"
#include "units/unit.hpp"
#include "units/formula_manager.hpp"
static lg::log_domain log_formula_ai("ai/stage/unit_formulas");
#define LOG_AI LOG_STREAM(info, log_formula_ai)
#define WRN_AI LOG_STREAM(warn, log_formula_ai)
#define ERR_AI LOG_STREAM(err, log_formula_ai)
namespace ai {
stage_unit_formulas::stage_unit_formulas(
ai_context &context
, const config &cfg
, formula_ai &fai)
: stage(context,cfg), fai_(fai)
{
}
stage_unit_formulas::~stage_unit_formulas()
{
}
bool stage_unit_formulas::do_play_stage()
{
//execute units formulas first
wfl::unit_formula_set units_with_formulas;
unit_map &units_ = resources::gameboard->units();
for(unit_map::unit_iterator i = units_.begin() ; i != units_.end() ; ++i)
{
if (i->side() == get_side()) {
if (i->formula_manager().has_formula() || i->formula_manager().has_loop_formula()) {
int priority = 0;
if (i->formula_manager().has_priority_formula()) {
try {
wfl::const_formula_ptr priority_formula(fai_.create_optional_formula(i->formula_manager().get_priority_formula()));
if (priority_formula) {
wfl::map_formula_callable callable(fai_);
callable.add("me", wfl::variant(std::make_shared<wfl::unit_callable>(*i)));
priority = (wfl::formula::evaluate(priority_formula, callable)).as_int();
} else {
WRN_AI << "priority formula skipped, maybe it's empty or incorrect"<< std::endl;
}
} catch(wfl::formula_error& e) {
if(e.filename == "formula")
e.line = 0;
fai_.handle_exception( e, "Unit priority formula error for unit: '" + i->type_id() + "' standing at (" + std::to_string(i->get_location().wml_x()) + "," + std::to_string(i->get_location().wml_y()) + ")");
priority = 0;
} catch(const wfl::type_error& e) {
priority = 0;
ERR_AI << "formula type error while evaluating unit priority formula " << e.message << std::endl;
}
}
units_with_formulas.insert( wfl::unit_formula_pair( i, priority ) );
}
}
}
for(wfl::unit_formula_set::iterator pair_it = units_with_formulas.begin() ; pair_it != units_with_formulas.end() ; ++pair_it)
{
unit_map::iterator i = pair_it->first;
if( i.valid() ) {
if (i->formula_manager().has_formula()) {
try {
wfl::const_formula_ptr formula(fai_.create_optional_formula(i->formula_manager().get_formula()));
if (formula) {
wfl::map_formula_callable callable(fai_);
callable.add("me", wfl::variant(std::make_shared<wfl::unit_callable>(*i)));
fai_.make_action(formula, callable);
} else {
WRN_AI << "unit formula skipped, maybe it's empty or incorrect" << std::endl;
}
}
catch(wfl::formula_error& e) {
if(e.filename == "formula") {
e.line = 0;
}
fai_.handle_exception( e, "Unit formula error for unit: '" + i->type_id() + "' standing at (" + std::to_string(i->get_location().wml_x()) + "," + std::to_string(i->get_location().wml_y()) + ")");
}
}
}
if( i.valid() ) {
if (i->formula_manager().has_loop_formula())
{
try {
wfl::const_formula_ptr loop_formula(fai_.create_optional_formula(i->formula_manager().get_loop_formula()));
if (loop_formula) {
wfl::map_formula_callable callable(fai_);
callable.add("me", wfl::variant(std::make_shared<wfl::unit_callable>(*i)));
while ( !fai_.make_action(loop_formula, callable).is_empty() && i.valid() )
{
}
} else {
WRN_AI << "Loop formula skipped, maybe it's empty or incorrect" << std::endl;
}
} catch(wfl::formula_error& e) {
if (e.filename == "formula") {
e.line = 0;
}
fai_.handle_exception( e, "Unit loop formula error for unit: '" + i->type_id() + "' standing at (" + std::to_string(i->get_location().wml_x()) + "," + std::to_string(i->get_location().wml_y()) + ")");
}
}
}
}
return false;
}
void stage_unit_formulas::on_create()
{
//we have no state on our own
}
config stage_unit_formulas::to_config() const
{
config cfg = stage::to_config();
//we have no state on our own
return cfg;
}
} // end of namespace ai