-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
game_state.cpp
399 lines (333 loc) · 11.5 KB
/
game_state.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
389
390
391
392
393
394
395
396
397
398
399
/*
Copyright (C) 2014 - 2016 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 "game_state.hpp"
#include "actions/undo.hpp"
#include "game_board.hpp"
#include "game_data.hpp"
#include "game_events/manager.hpp"
#include "log.hpp"
#include "map/map.hpp"
#include "pathfind/pathfind.hpp"
#include "pathfind/teleport.hpp"
#include "play_controller.hpp"
#include "game_preferences.hpp"
#include "random_new_deterministic.hpp"
#include "reports.hpp"
#include "scripting/game_lua_kernel.hpp"
#include "teambuilder.hpp"
#include "units/unit.hpp"
#include "whiteboard/manager.hpp"
#include "gui/dialogs/loadscreen.hpp"
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <SDL_timer.h>
#include <algorithm>
#include <set>
static lg::log_domain log_engine("engine");
#define LOG_NG LOG_STREAM(info, log_engine)
#define DBG_NG LOG_STREAM(debug, log_engine)
game_state::game_state(const config & level, play_controller &, const tdata_cache & tdata) :
gamedata_(level),
board_(tdata, level),
tod_manager_(level),
pathfind_manager_(),
reports_(new reports()),
lua_kernel_(),
events_manager_(new game_events::manager()),
undo_stack_(new actions::undo_list(level.child("undo_stack"))),
player_number_(level["playing_team"].to_int() + 1),
init_side_done_(level["init_side_done"].to_bool(false)),
start_event_fired_(!level["playing_team"].empty()),
server_request_number_(level["server_request_number"].to_int()),
first_human_team_(-1)
{
if(const config& endlevel_cfg = level.child("end_level_data")) {
end_level_data el_data;
el_data.read(endlevel_cfg);
el_data.transient.carryover_report = false;
end_level_data_ = el_data;
}
}
game_state::game_state(const config & level, play_controller & pc, game_board& board) :
gamedata_(level),
board_(board),
tod_manager_(level),
pathfind_manager_(new pathfind::manager(level)),
reports_(new reports()),
lua_kernel_(new game_lua_kernel(NULL, *this, pc, *reports_)),
events_manager_(new game_events::manager()),
player_number_(level["playing_team"].to_int() + 1),
end_level_data_(),
init_side_done_(level["init_side_done"].to_bool(false)),
first_human_team_(-1)
{
events_manager_->read_scenario(level);
if(const config& endlevel_cfg = level.child("end_level_data")) {
end_level_data el_data;
el_data.read(endlevel_cfg);
el_data.transient.carryover_report = false;
end_level_data_ = el_data;
}
}
game_state::~game_state() {}
static int placing_score(const config& side, const gamemap& map, const map_location& pos)
{
int positions = 0, liked = 0;
const t_translation::t_list terrain = t_translation::read_list(side["terrain_liked"]);
for(int i = pos.x-8; i != pos.x+8; ++i) {
for(int j = pos.y-8; j != pos.y+8; ++j) {
const map_location pos(i,j);
if(map.on_board(pos)) {
++positions;
if(std::count(terrain.begin(),terrain.end(),map[pos])) {
++liked;
}
}
}
}
return (100*liked)/positions;
}
struct placing_info {
placing_info() :
side(0),
score(0),
pos()
{
}
int side, score;
map_location pos;
};
static bool operator<(const placing_info& a, const placing_info& b) { return a.score > b.score; }
void game_state::place_sides_in_preferred_locations(const config& level)
{
std::vector<placing_info> placings;
int num_pos = board_.map().num_valid_starting_positions();
int side_num = 1;
BOOST_FOREACH(const config &side, level.child_range("side"))
{
for(int p = 1; p <= num_pos; ++p) {
const map_location& pos = board_.map().starting_position(p);
int score = placing_score(side, board_.map(), pos);
placing_info obj;
obj.side = side_num;
obj.score = score;
obj.pos = pos;
placings.push_back(obj);
}
++side_num;
}
std::stable_sort(placings.begin(),placings.end());
std::set<int> placed;
std::set<map_location> positions_taken;
for (std::vector<placing_info>::const_iterator i = placings.begin(); i != placings.end() && int(placed.size()) != side_num - 1; ++i) {
if(placed.count(i->side) == 0 && positions_taken.count(i->pos) == 0) {
placed.insert(i->side);
positions_taken.insert(i->pos);
board_.map_->set_starting_position(i->side,i->pos);
LOG_NG << "placing side " << i->side << " at " << i->pos << std::endl;
}
}
}
void game_state::init(const config& level, play_controller & pc)
{
events_manager_->read_scenario(level);
gui2::tloadscreen::progress("init teams");
if (level["modify_placing"].to_bool()) {
LOG_NG << "modifying placing..." << std::endl;
place_sides_in_preferred_locations(level);
}
LOG_NG << "initialized time of day regions... " << (SDL_GetTicks() - pc.ticks()) << std::endl;
BOOST_FOREACH(const config &t, level.child_range("time_area")) {
tod_manager_.add_time_area(board_.map(),t);
}
LOG_NG << "initialized teams... " << (SDL_GetTicks() - pc.ticks()) << std::endl;
board_.teams_.resize(level.child_count("side"));
std::vector<team_builder_ptr> team_builders;
int team_num = 0;
BOOST_FOREACH(const config &side, level.child_range("side"))
{
if (first_human_team_ == -1) {
const std::string &controller = side["controller"];
if (controller == "human") {
first_human_team_ = team_num;
}
}
team_builder_ptr tb_ptr = create_team_builder(side,
board_.teams_, level, board_);
++team_num;
build_team_stage_one(tb_ptr);
team_builders.push_back(tb_ptr);
}
{
//sync traits of start units and the random start time.
random_new::set_random_determinstic deterministic(gamedata_.rng());
tod_manager_.resolve_random(*random_new::generator);
BOOST_FOREACH(team_builder_ptr tb_ptr, team_builders)
{
build_team_stage_two(tb_ptr);
}
for(size_t i = 0; i < board_.teams_.size(); i++) {
// Labels from players in your ignore list default to hidden
if(preferences::is_ignored(board_.teams_[i].current_player())) {
std::string label_cat = "side:" + str_cast(i + 1);
board_.hidden_label_categories_ref().push_back(label_cat);
}
}
}
pathfind_manager_.reset(new pathfind::manager(level));
lua_kernel_.reset(new game_lua_kernel(NULL, *this, pc, *reports_));
}
void game_state::bind(wb::manager *, game_display * gd)
{
set_game_display(gd);
}
void game_state::set_game_display(game_display * gd)
{
lua_kernel_->set_game_display(gd);
}
void game_state::write(config& cfg) const
{
cfg["init_side_done"] = init_side_done_;
if(gamedata_.phase() == game_data::PLAY) {
cfg["playing_team"] = player_number_ - 1;
}
cfg["server_request_number"] = server_request_number_;
//Call the lua save_game functions
lua_kernel_->save_game(cfg);
//Write the game events.
events_manager_->write_events(cfg);
//Write the map, unit_map, and teams info
board_.write_config(cfg);
//Write the tod manager, and time areas
cfg.merge_with(tod_manager_.to_config());
//write out the current state of the map
cfg.merge_with(pathfind_manager_->to_config());
//Write the game data, including wml vars
gamedata_.write_snapshot(cfg);
// Preserve the undo stack so that fog/shroud clearing is kept accurate.
undo_stack_->write(cfg.add_child("undo_stack"));
if(end_level_data_.get_ptr() != NULL) {
end_level_data_->write(cfg.add_child("end_level_data"));
}
}
namespace {
struct castle_cost_calculator : pathfind::cost_calculator
{
castle_cost_calculator(const gamemap& map, const team & view_team) :
map_(map),
viewer_(view_team),
use_shroud_(view_team.uses_shroud())
{}
virtual double cost(const map_location& loc, const double) const
{
if(!map_.is_castle(loc))
return 10000;
if ( use_shroud_ && viewer_.shrouded(loc) )
return 10000;
return 1;
}
private:
const gamemap& map_;
const team& viewer_;
const bool use_shroud_; // Allows faster checks when shroud is disabled.
};
}//anonymous namespace
/**
* Checks to see if a leader at @a leader_loc could recruit somewhere.
* This takes into account terrain, shroud (for side @a side), and the presence
* of visible units.
* The behavior for an invalid @a side is subject to change for future needs.
*/
bool game_state::can_recruit_from(const map_location& leader_loc, int side) const
{
const gamemap& map = board_.map();
if( !map.is_keep(leader_loc) )
return false;
if ( side < 1 || board_.teams().size() < static_cast<size_t>(side) ) {
// Invalid side specified.
// Currently this cannot happen, but it could conceivably be used in
// the future to request that shroud and visibility be ignored. Until
// that comes to pass, just return.
return false;
}
return pathfind::find_vacant_tile(leader_loc, pathfind::VACANT_CASTLE, NULL,
&(board_.teams())[side-1])
!= map_location::null_location();
}
bool game_state::can_recruit_from(const unit& leader) const
{
return can_recruit_from(leader.get_location(), leader.side());
}
/**
* Checks to see if a leader at @a leader_loc could recruit on @a recruit_loc.
* This takes into account terrain, shroud (for side @a side), and whether or
* not there is already a visible unit at recruit_loc.
* The behavior for an invalid @a side is subject to change for future needs.
*/
bool game_state::can_recruit_on(const map_location& leader_loc, const map_location& recruit_loc, int side) const
{
const gamemap& map = board_.map();
if( !map.is_castle(recruit_loc) )
return false;
if( !map.is_keep(leader_loc) )
return false;
if ( side < 1 || board_.teams().size() < static_cast<size_t>(side) ) {
// Invalid side specified.
// Currently this cannot happen, but it could conceivably be used in
// the future to request that shroud and visibility be ignored. Until
// that comes to pass, just return.
return false;
}
const team & view_team = board_.teams()[side-1];
if ( view_team.shrouded(recruit_loc) )
return false;
if ( board_.has_visible_unit(recruit_loc, view_team) )
return false;
castle_cost_calculator calc(map, view_team);
// The limit computed in the third argument is more than enough for
// any convex castle on the map. Strictly speaking it could be
// reduced to sqrt(map.w()**2 + map.h()**2).
pathfind::plain_route rt =
pathfind::a_star_search(leader_loc, recruit_loc, map.w()+map.h(), &calc,
map.w(), map.h());
return !rt.steps.empty();
}
bool game_state::can_recruit_on(const unit& leader, const map_location& recruit_loc) const
{
return can_recruit_on(leader.get_location(), recruit_loc, leader.side());
}
bool game_state::side_can_recruit_on(int side, map_location hex) const
{
unit_map::const_iterator leader = board_.units().find(hex);
if ( leader != board_.units().end() ) {
return leader->can_recruit() && leader->side() == side && can_recruit_from(*leader);
} else {
// Look for a leader who can recruit on last_hex.
for ( leader = board_.units().begin(); leader != board_.units().end(); ++leader) {
if ( leader->can_recruit() && leader->side() == side && can_recruit_on(*leader, hex) ) {
return true;
}
}
}
// No leader found who can recruit at last_hex.
return false;
}
game_events::wmi_container& game_state::get_wml_menu_items()
{
return this->events_manager_->wml_menu_items_;
}
const game_events::wmi_container& game_state::get_wml_menu_items() const
{
return this->events_manager_->wml_menu_items_;
}