/
unit_creator.cpp
235 lines (189 loc) · 6.22 KB
/
unit_creator.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
/*
Copyright (C) 2003 - 2016 by David White <dave@whitevine.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
* Recruiting, recalling.
*/
#include "unit_creator.hpp"
#include "move.hpp" //for actions::get_village
#include "config.hpp"
#include "filter_context.hpp"
#include "game_board.hpp"
#include "game_events/manager.hpp"
#include "game_events/pump.hpp"
#include "game_preferences.hpp"
#include "game_data.hpp" // for resources::gamedata conversion variable_set
#include "gettext.hpp"
#include "log.hpp"
#include "map/map.hpp"
#include "pathfind/pathfind.hpp"
#include "resources.hpp" // for resources::screen, resources::gamedata
#include "team.hpp" //for team
#include "units/unit.hpp" // for unit
#include "units/udisplay.hpp" // for unit_display
#include "variable.hpp" // for vconfig
#include "game_display.hpp" // for resources::screen
static lg::log_domain log_engine("engine");
#define DBG_NG LOG_STREAM(debug, log_engine)
#define LOG_NG LOG_STREAM(info, log_engine)
#define ERR_NG LOG_STREAM(err, log_engine)
unit_creator::unit_creator(team &tm, const map_location &start_pos, game_board* board)
: add_to_recall_(false)
, discover_(false)
, get_village_(false)
, invalidate_(false)
, rename_side_(false)
, show_(false)
, start_pos_(start_pos)
, team_(tm)
, board_(board ? board : resources::gameboard)
{
}
unit_creator& unit_creator::allow_show(bool b)
{
show_ = b;
return *this;
}
unit_creator& unit_creator::allow_get_village(bool b)
{
get_village_ = b;
return *this;
}
unit_creator& unit_creator::allow_rename_side(bool b)
{
rename_side_ = b;
return *this;
}
unit_creator& unit_creator::allow_invalidate(bool b)
{
invalidate_ = b;
return *this;
}
unit_creator& unit_creator::allow_discover(bool b)
{
discover_ = b;
return *this;
}
unit_creator& unit_creator::allow_add_to_recall(bool b)
{
add_to_recall_ = b;
return *this;
}
map_location unit_creator::find_location(const config &cfg, const unit* pass_check)
{
DBG_NG << "finding location for unit with id=["<<cfg["id"]<<"] placement=["<<cfg["placement"]<<"] x=["<<cfg["x"]<<"] y=["<<cfg["y"]<<"] for side " << team_.side() << "\n";
std::vector<std::string> placements = utils::split(cfg["placement"]);
placements.push_back("map");
placements.push_back("recall");
for (const std::string& place : placements)
{
map_location loc;
if ( place == "recall" ) {
return map_location::null_location();
}
else if ( place == "leader" || place == "leader_passable" ) {
unit_map::const_iterator leader = board_->units().find_leader(team_.side());
//todo: take 'leader in recall list' possibility into account
if (leader.valid()) {
loc = leader->get_location();
} else {
loc = start_pos_;
}
}
// "map", "map_passable", and "map_overwrite".
else if ( place == "map" || place.compare(0, 4, "map_") == 0 ) {
loc = map_location(cfg, resources::gamedata);
}
else {
loc = board_->map().special_location(place);
}
if(loc.valid() && board_->map().on_board(loc)) {
const bool pass((place == "leader_passable") || (place == "map_passable"));
if ( place != "map_overwrite" ) {
loc = find_vacant_tile(loc, pathfind::VACANT_ANY,
pass ? pass_check : nullptr, nullptr, board_);
}
if(loc.valid() && board_->map().on_board(loc)) {
return loc;
}
}
}
return map_location::null_location();
}
void unit_creator::add_unit(const config &cfg, const vconfig* vcfg)
{
config temp_cfg(cfg);
temp_cfg["side"] = team_.side();
const std::string& id =(cfg)["id"];
bool animate = temp_cfg["animate"].to_bool();
temp_cfg.remove_attribute("animate");
unit_ptr recall_list_element = team_.recall_list().find_if_matches_id(id);
if ( !recall_list_element ) {
//make the new unit
unit_ptr new_unit(new unit(temp_cfg, true, vcfg));
map_location loc = find_location(temp_cfg, new_unit.get());
if ( loc.valid() ) {
//add the new unit to map
board_->units().replace(loc, *new_unit);
LOG_NG << "inserting unit for side " << new_unit->side() << "\n";
post_create(loc,*(board_->units().find(loc)),animate);
}
else if ( add_to_recall_ ) {
//add to recall list
team_.recall_list().add(new_unit);
DBG_NG << "inserting unit with id=["<<id<<"] on recall list for side " << new_unit->side() << "\n";
preferences::encountered_units().insert(new_unit->type_id());
}
} else {
//get unit from recall list
map_location loc = find_location(temp_cfg, recall_list_element.get());
if ( loc.valid() ) {
board_->units().replace(loc, *recall_list_element);
LOG_NG << "inserting unit from recall list for side " << recall_list_element->side()<< " with id="<< id << "\n";
post_create(loc,*(board_->units().find(loc)),animate);
//if id is not empty, delete units with this ID from recall list
team_.recall_list().erase_if_matches_id( id);
}
else if ( add_to_recall_ ) {
LOG_NG << "wanted to insert unit on recall list, but recall list for side " << (cfg)["side"] << "already contains id=" <<id<<"\n";
return;
}
}
}
void unit_creator::post_create(const map_location &loc, const unit &new_unit, bool anim)
{
if (discover_) {
preferences::encountered_units().insert(new_unit.type_id());
}
bool show = show_ && (resources::screen !=nullptr) && !resources::screen->fogged(loc);
bool animate = show && anim;
if (get_village_) {
assert(resources::gameboard);
if (board_->map().is_village(loc)) {
actions::get_village(loc, new_unit.side());
}
}
// Only fire the events if it's safe; it's not if we're in the middle of play_controller::reset_gamestate()
if (resources::lua_kernel != nullptr) {
resources::game_events->pump().fire("unit_placed", loc);
}
if (resources::screen!=nullptr) {
if (invalidate_ ) {
resources::screen->invalidate(loc);
}
if (animate) {
unit_display::unit_recruited(loc);
} else if (show) {
resources::screen->draw();
}
}
}