-
-
Notifications
You must be signed in to change notification settings - Fork 990
/
game_board.cpp
389 lines (322 loc) · 10.9 KB
/
game_board.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
Copyright (C) 2014 by Chris Beck <render787@gmail.com>
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.
*/
#include "config.hpp"
#include "game_board.hpp"
#include "game_preferences.hpp"
#include "log.hpp"
#include "map.hpp"
#include "recall_list_manager.hpp"
#include "unit.hpp"
#include "utils/foreach.tpp"
#include <boost/foreach.hpp>
static lg::log_domain log_engine("enginerefac");
#define DBG_RG LOG_STREAM(debug, log_engine)
#define LOG_RG LOG_STREAM(info, log_engine)
#define WRN_RG LOG_STREAM(warn, log_engine)
#define ERR_RG LOG_STREAM(err, log_engine)
game_board::game_board(const config & game_config, const config & level) : teams_(), map_(new gamemap(game_config, level)), units_() {}
game_board::game_board(const game_board & other) : teams_(other.teams_), map_(new gamemap(*(other.map_))), units_(other.units_) {}
game_board::~game_board() {}
//TODO: Fix this so that we swap pointers to maps, and also fix replace_map to use scoped_ptr::reset.
// However, then anytime gameboard is overwritten, resources::gamemap must be updated. So might want to
// just get rid of resources::gamemap and replace with resources::gameboard->map() at that point.
void swap(game_board & one, game_board & other) {
std::swap(one.teams_, other.teams_);
std::swap(one.units_, other.units_);
one.map_.swap(other.map_);
}
game_board & game_board::operator= (game_board other)
{
swap(*this, other);
return(*this);
}
void game_board::new_turn(int player_num) {
BOOST_FOREACH (unit & i, units_) {
if (i.side() == player_num) {
i.new_turn();
}
}
}
void game_board::end_turn(int player_num) {
BOOST_FOREACH (unit & i, units_) {
if (i.side() == player_num) {
i.end_turn();
}
}
}
void game_board::set_all_units_user_end_turn() {
BOOST_FOREACH (unit & i, units_) {
i.set_user_end_turn(true);
}
}
void game_board::all_survivors_to_recall() {
for (unit_map::iterator it = units_.begin(); it != units_.end(); it++) {
unit_ptr un = it.get_shared_ptr();
if (teams_[un->side() - 1].persistent()) {
un->new_turn();
un->new_scenario();
#if 0
teams_[un->side() - 1].recall_list().push_back(un);
#endif
}
}
}
unit_map::iterator game_board::find_visible_unit(const map_location &loc,
const team& current_team, bool see_all)
{
if (!map_->on_board(loc)) return units_.end();
unit_map::iterator u = units_.find(loc);
if (!u.valid() || !u->is_visible_to_team(current_team, *map_, see_all))
return units_.end();
return u;
}
bool game_board::has_visible_unit(const map_location & loc, const team& current_team, bool see_all)
{
if (!map_->on_board(loc)) return false;
unit_map::iterator u = units_.find(loc);
if (!u.valid() || !u->is_visible_to_team(current_team, *map_, see_all))
return false;
return true;
}
unit* game_board::get_visible_unit(const map_location &loc,
const team ¤t_team, bool see_all)
{
unit_map::iterator ui = find_visible_unit(loc, current_team, see_all);
if (ui == units_.end()) return NULL;
return &*ui;
}
void game_board::side_drop_to(int side_num, team::CONTROLLER ctrl) {
team &tm = teams_[side_num-1];
tm.change_controller(ctrl);
tm.set_current_player(lexical_cast<std::string> (ctrl) + lexical_cast<std::string> (side_num));
unit_map::iterator leader = units_.find_leader(side_num);
if (leader.valid()) leader->rename(lexical_cast<std::string> (ctrl) + lexical_cast<std::string> (side_num));
}
void game_board::side_change_controller(int side_num, team::CONTROLLER ctrl, const std::string pname) {
team &tm = teams_[side_num-1];
tm.change_controller(ctrl);
if (pname.empty()) {
return ;
}
tm.set_current_player(pname);
unit_map::iterator leader = units_.find_leader(side_num);
if (leader.valid()) {
leader->rename(pname);
}
}
bool game_board::try_add_unit_to_recall_list(const map_location& loc, const unit_ptr u)
{
if(teams_[u->side()-1].persistent()) {
teams_[u->side()-1].recall_list().add(u);
return true;
} else {
ERR_RG << "unit with id " << u->id() << ": location (" << loc.x << "," << loc.y <<") is not on the map, and player "
<< u->side() << " has no recall list.\n";
return false;
}
}
boost::optional<std::string> game_board::replace_map(const gamemap & newmap) {
boost::optional<std::string> ret = boost::optional<std::string> ();
/* Remember the locations where a village is owned by a side. */
std::map<map_location, int> villages;
FOREACH(const AUTO& village, map_->villages()) {
const int owner = village_owner(village);
if(owner != -1) {
villages[village] = owner;
}
}
for (unit_map::iterator itor = units_.begin(); itor != units_.end(); ) {
if (!newmap.on_board(itor->get_location())) {
if (!try_add_unit_to_recall_list(itor->get_location(), itor.get_shared_ptr())) {
*ret = std::string("replace_map: Cannot add a unit that would become off-map to the recall list\n");
}
units_.erase(itor++);
} else {
++itor;
}
}
/* Disown villages that are no longer villages. */
FOREACH(const AUTO& village, villages) {
if(!newmap.is_village(village.first)) {
teams_[village.second].lose_village(village.first);
}
}
*map_ = newmap;
return ret;
}
void game_board::overlay_map(const gamemap & mask_map, const config & cfg, map_location loc, bool border) {
map_->overlay(mask_map, cfg, loc.x, loc.y, border);
}
bool game_board::change_terrain(const map_location &loc, const std::string &t_str,
const std::string & mode_str, bool replace_if_failed)
{
//Code internalized from the implementation in lua.cpp
t_translation::t_terrain terrain = t_translation::read_terrain_code(t_str);
if (terrain == t_translation::NONE_TERRAIN) return false;
gamemap::tmerge_mode mode = gamemap::BOTH;
if (mode_str == "base") mode = gamemap::BASE;
else if (mode_str == "overlay") mode = gamemap::OVERLAY;
/*
* When a hex changes from a village terrain to a non-village terrain, and
* a team owned that village it loses that village. When a hex changes from
* a non-village terrain to a village terrain and there is a unit on that
* hex it does not automatically capture the village. The reason for not
* capturing villages it that there are too many choices to make; should a
* unit loose its movement points, should capture events be fired. It is
* easier to do this as wanted by the author in WML.
*/
t_translation::t_terrain
old_t = map_->get_terrain(loc),
new_t = map_->merge_terrains(old_t, terrain, mode, replace_if_failed);
if (new_t == t_translation::NONE_TERRAIN) return false;
preferences::encountered_terrains().insert(new_t);
if (map_->is_village(old_t) && !map_->is_village(new_t)) {
int owner = village_owner(loc);
if (owner != -1)
teams_[owner].lose_village(loc);
}
map_->set_terrain(loc, new_t);
BOOST_FOREACH(const t_translation::t_terrain &ut, map_->underlying_union_terrain(loc)) {
preferences::encountered_terrains().insert(ut);
}
return true;
}
void game_board::write_config(config & cfg) const {
for(std::vector<team>::const_iterator t = teams_.begin(); t != teams_.end(); ++t) {
int side_num = t - teams_.begin() + 1;
config& side = cfg.add_child("side");
t->write(side);
side["no_leader"] = true;
side["side"] = str_cast(side_num);
//current units
{
BOOST_FOREACH(const unit & i, units_) {
if (i.side() == side_num) {
config& u = side.add_child("unit");
i.get_location().write(u);
i.write(u);
}
}
}
//recall list
{
BOOST_FOREACH(const unit_const_ptr & j, t->recall_list()) {
config& u = side.add_child("unit");
j->write(u);
}
}
}
//write the map
cfg["map_data"] = map_->write();
//Used by the carryover calculations.
cfg["map_villages_num"] = map_->villages().size();
}
temporary_unit_placer::temporary_unit_placer(unit_map& m, const map_location& loc, unit& u)
: m_(m), loc_(loc), temp_(m_.extract(loc))
{
u.clone();
m_.add(loc, u);
}
temporary_unit_placer::temporary_unit_placer(game_board& b, const map_location& loc, unit& u)
: m_(b.units_), loc_(loc), temp_(m_.extract(loc))
{
u.clone();
m_.add(loc, u);
}
temporary_unit_placer::~temporary_unit_placer()
{
try {
m_.erase(loc_);
if(temp_) {
m_.insert(temp_);
}
} catch (...) {}
}
temporary_unit_remover::temporary_unit_remover(unit_map& m, const map_location& loc)
: m_(m), loc_(loc), temp_(m_.extract(loc))
{
}
temporary_unit_remover::temporary_unit_remover(game_board& b, const map_location& loc)
: m_(b.units_), loc_(loc), temp_(m_.extract(loc))
{
}
temporary_unit_remover::~temporary_unit_remover()
{
try {
if(temp_) {
m_.insert(temp_);
}
} catch (...) {}
}
/**
* Constructor
* This version will change the unit's current movement to @a new_moves while
* the unit is moved (and restored to its previous value upon this object's
* destruction).
*/
temporary_unit_mover::temporary_unit_mover(unit_map& m, const map_location& src,
const map_location& dst, int new_moves)
: m_(m), src_(src), dst_(dst), old_moves_(-1),
temp_(src == dst ? unit_ptr() : m_.extract(dst))
{
std::pair<unit_map::iterator, bool> move_result = m_.move(src_, dst_);
// Set the movement.
if ( move_result.second )
{
old_moves_ = move_result.first->movement_left(true);
move_result.first->set_movement(new_moves);
}
}
temporary_unit_mover::temporary_unit_mover(game_board& b, const map_location& src,
const map_location& dst, int new_moves)
: m_(b.units_), src_(src), dst_(dst), old_moves_(-1),
temp_(src == dst ? unit_ptr() : m_.extract(dst))
{
std::pair<unit_map::iterator, bool> move_result = m_.move(src_, dst_);
// Set the movement.
if ( move_result.second )
{
old_moves_ = move_result.first->movement_left(true);
move_result.first->set_movement(new_moves);
}
}
/**
* Constructor
* This version does not change (nor restore) the unit's movement.
*/
temporary_unit_mover::temporary_unit_mover(unit_map& m, const map_location& src,
const map_location& dst)
: m_(m), src_(src), dst_(dst), old_moves_(-1),
temp_(src == dst ? unit_ptr() : m_.extract(dst))
{
m_.move(src_, dst_);
}
temporary_unit_mover::temporary_unit_mover(game_board& b, const map_location& src,
const map_location& dst)
: m_(b.units_), src_(src), dst_(dst), old_moves_(-1),
temp_(src == dst ? unit_ptr() : m_.extract(dst))
{
m_.move(src_, dst_);
}
temporary_unit_mover::~temporary_unit_mover()
{
try {
std::pair<unit_map::iterator, bool> move_result = m_.move(dst_, src_);
// Restore the movement?
if ( move_result.second && old_moves_ >= 0 )
move_result.first->set_movement(old_moves_);
// Restore the extracted unit?
if(temp_) {
m_.insert(temp_);
}
} catch (...) {}
}