-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
tod_manager.cpp
421 lines (360 loc) · 12.2 KB
/
tod_manager.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
Copyright (C) 2009 - 2014 by Eugen Jiresch
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 "tod_manager.hpp"
#include "wml_exception.hpp"
#include "gettext.hpp"
#include "formula_string_utils.hpp"
#include "gamestatus.hpp"
#include "log.hpp"
#include "map.hpp"
#include "play_controller.hpp"
#include "resources.hpp"
#include "unit.hpp"
#include "unit_abilities.hpp"
#include <boost/foreach.hpp>
static lg::log_domain log_engine("engine");
#define LOG_NG LOG_STREAM(info, log_engine)
tod_manager::tod_manager(const config& scenario_cfg):
savegame_config(),
currentTime_(0),
times_(),
areas_(),
turn_(scenario_cfg["turn_at"].to_int(1)),
num_turns_(scenario_cfg["turns"].to_int(-1))
{
time_of_day::parse_times(scenario_cfg,times_);
currentTime_ = get_start_ToD(scenario_cfg);
//TODO:
//Very bad, since we're pretending to not modify the cfg. Needed to transfer the result
//to the network clients in a mp game, otherwise we have OOS.
config& non_const_config = const_cast<config&>(scenario_cfg);
non_const_config["current_time"] = currentTime_;
}
tod_manager& tod_manager::operator=(const tod_manager& manager)
{
if(this == &manager) {
return *this;
}
currentTime_ = manager.currentTime_;
times_ = manager.times_;
areas_ = manager.areas_;
turn_ = manager.turn_;
num_turns_ = manager.num_turns_;
return *this;
}
config tod_manager::to_config() const
{
config cfg;
cfg["turn_at"] = turn_;
cfg["turns"] = num_turns_;
cfg["current_time"] = currentTime_;
std::vector<time_of_day>::const_iterator t;
for(t = times_.begin(); t != times_.end(); ++t) {
t->write(cfg.add_child("time"));
}
for(std::vector<area_time_of_day>::const_iterator i = areas_.begin(); i != areas_.end(); ++i) {
config& area = cfg.add_child("time_area");
// if no ranges, then use hexes to generate ranges
if(i->xsrc.empty() && i->ysrc.empty()) {
write_location_range(i->hexes, area);
} else {
area["x"] = i->xsrc;
area["y"] = i->ysrc;
}
for(t = i->times.begin(); t != i->times.end(); ++t) {
t->write(area.add_child("time"));
}
area["current_time"] = i->currentTime;
if (!i->id.empty())
area["id"] = i->id;
}
return cfg;
}
const time_of_day& tod_manager::get_previous_time_of_day() const
{
return get_time_of_day_turn(times_, turn_ - 1, currentTime_);
}
int tod_manager::get_current_area_time(int index) const {
assert(index < static_cast<int>(areas_.size()) );
return areas_[index].currentTime;
}
int tod_manager::get_current_time(const map_location& loc) const
{
if ( loc != map_location::null_location() ) {
for ( std::vector<area_time_of_day>::const_reverse_iterator
i = areas_.rbegin(), i_end = areas_.rend(); i != i_end; ++i )
{
if (i->hexes.find(loc) != i->hexes.end())
return i->currentTime;
}
}
return currentTime_;
}
const std::vector<time_of_day>& tod_manager::times(const map_location& loc) const
{
if ( loc != map_location::null_location() ) {
for ( std::vector<area_time_of_day>::const_reverse_iterator
i = areas_.rbegin(), i_end = areas_.rend(); i != i_end; ++i )
{
if (i->hexes.find(loc) != i->hexes.end())
return i->times;
}
}
return times_;
}
const time_of_day& tod_manager::get_time_of_day(const map_location& loc, int n_turn) const
{
if(n_turn == 0)
n_turn = turn_;
if ( loc != map_location::null_location() )
{
for ( std::vector<area_time_of_day>::const_reverse_iterator
i = areas_.rbegin(), i_end = areas_.rend(); i != i_end; ++i )
{
if (i->hexes.find(loc) != i->hexes.end())
return get_time_of_day_turn(i->times, n_turn, i->currentTime);
}
}
return get_time_of_day_turn(times_, n_turn, currentTime_);
}
const time_of_day tod_manager::get_illuminated_time_of_day(const map_location& loc, int for_turn) const
{
const gamemap& map = *resources::game_map;
const unit_map& units = *resources::units;
// get ToD ignoring illumination
time_of_day tod = get_time_of_day(loc, for_turn);
if ( map.on_board_with_border(loc) )
{
// Now add terrain illumination.
const int terrain_light = map.get_terrain_info(loc).light_bonus(tod.lawful_bonus);
std::vector<int> mod_list;
std::vector<int> max_list;
std::vector<int> min_list;
int most_add = 0;
int most_sub = 0;
// Find the "illuminates" effects from units that can affect loc.
map_location locs[7];
locs[0] = loc;
get_adjacent_tiles(loc,locs+1);
for ( size_t i = 0; i != 7; ++i ) {
const unit_map::const_iterator itor = units.find(locs[i]);
if (itor != units.end() &&
itor->get_ability_bool("illuminates") &&
!itor->incapacitated())
{
unit_ability_list illum = itor->get_abilities("illuminates");
unit_abilities::effect illum_effect(illum, terrain_light, false);
const int unit_mod = illum_effect.get_composite_value();
// Record this value.
mod_list.push_back(unit_mod);
max_list.push_back(illum.highest("max_value").first);
min_list.push_back(illum.lowest("min_value").first);
if ( unit_mod > most_add )
most_add = unit_mod;
else if ( unit_mod < most_sub )
most_sub = unit_mod;
}
}
const bool net_darker = most_add < -most_sub;
// Apply each unit's effect, tracking the best result.
int best_result = terrain_light;
const int base_light = terrain_light + (net_darker ? most_add : most_sub);
for ( size_t i = 0; i != mod_list.size(); ++i ) {
int result =
bounded_add(base_light, mod_list[i], max_list[i], min_list[i]);
if ( net_darker && result < best_result )
best_result = result;
else if ( !net_darker && result > best_result )
best_result = result;
}
// Update the object we will return.
tod.bonus_modified = best_result - tod.lawful_bonus;
tod.lawful_bonus = best_result;
}
return tod;
}
bool tod_manager::is_start_ToD(const std::string& random_start_time)
{
return !random_start_time.empty()
&& utils::string_bool(random_start_time, true);
}
void tod_manager::replace_schedule(const config& time_cfg)
{
times_.clear();
time_of_day::parse_times(time_cfg,times_);
currentTime_ = time_cfg["current_time"].to_int(0);
}
void tod_manager::replace_schedule(const std::vector<time_of_day>& schedule)
{
times_ = schedule;
currentTime_ = 0;
}
void tod_manager::replace_area_locations(int area_index, const std::set<map_location>& locs) {
assert(area_index < static_cast<int>(areas_.size()));
areas_[area_index].hexes = locs;
}
void tod_manager::replace_local_schedule(const std::vector<time_of_day>& schedule, int area_index)
{
assert(area_index < static_cast<int>(areas_.size()));
areas_[area_index].times = schedule;
areas_[area_index].currentTime = 0;
}
void tod_manager::set_area_id(int area_index, const std::string& id) {
assert(area_index < static_cast<int>(areas_.size()));
areas_[area_index].id = id;
}
std::vector<std::string> tod_manager::get_area_ids() const
{
std::vector<std::string> areas;
BOOST_FOREACH(const area_time_of_day& area, areas_) {
areas.push_back(area.id);
}
return areas;
}
const std::set<map_location>& tod_manager::get_area_by_id(const std::string& id) const
{
BOOST_FOREACH(const area_time_of_day& area, areas_) {
if (area.id == id)
return area.hexes;
}
return areas_[0].hexes;
}
const std::set<map_location>& tod_manager::get_area_by_index(int index) const
{
return areas_[index].hexes;
}
void tod_manager::add_time_area(const config& cfg)
{
areas_.push_back(area_time_of_day());
area_time_of_day &area = areas_.back();
area.id = cfg["id"].str();
area.xsrc = cfg["x"].str();
area.ysrc = cfg["y"].str();
area.currentTime = cfg["current_time"].to_int(0);
std::vector<map_location> const& locs (parse_location_range(area.xsrc, area.ysrc, true));
area.hexes.insert(locs.begin(), locs.end());
time_of_day::parse_times(cfg, area.times);
}
void tod_manager::add_time_area(const std::string& id, const std::set<map_location>& locs,
const config& time_cfg)
{
areas_.push_back(area_time_of_day());
area_time_of_day& area = areas_.back();
area.id = id;
area.hexes = locs;
area.currentTime = time_cfg["current_time"].to_int(0);
time_of_day::parse_times(time_cfg, area.times);
}
void tod_manager::remove_time_area(const std::string& area_id)
{
if(area_id.empty()) {
areas_.clear();
} else {
// search for all time areas that match the id.
std::vector<area_time_of_day>::iterator i = areas_.begin();
while(i != areas_.end()) {
if((*i).id == area_id) {
i = areas_.erase(i);
} else {
++i;
}
}
}
}
void tod_manager::remove_time_area(int area_index)
{
assert(area_index < static_cast<int>(areas_.size()));
areas_.erase(areas_.begin() + area_index);
}
int tod_manager::get_start_ToD(const config &level) const
{
const config::attribute_value& current_time = level["current_time"];
if (!current_time.blank())
{
return calculate_current_time(times_.size(), turn_, current_time.to_int(0), true);
}
const int default_result = calculate_current_time(times_.size(), turn_, currentTime_);
const config::attribute_value& cfg_random_start_time = level["random_start_time"];
if(!cfg_random_start_time.blank()) {
const std::string& random_start_time = cfg_random_start_time.str();
//TODO:
//Here there is danger of OOS (bug #15948)
//But this randomization is needed on the other hand to make the "random start time" option
//in the mp game selection screen work.
//process the random_start_time string, which can be boolean yes/no true/false or a
//comma-separated string of integers >= 1 referring to the times_ array indices
const std::vector<std::string>& random_start_time_strings = utils::split(random_start_time);
const int random_index = calculate_current_time(random_start_time_strings.size(), turn_, rand(), true);
const int given_current_time = lexical_cast_default<int, std::string>(random_start_time_strings[random_index], 0) - 1;
if(given_current_time >= 0) return calculate_current_time(times_.size(), turn_, given_current_time, true);
if(cfg_random_start_time.to_bool(false)) return calculate_current_time(times_.size(), turn_, rand(), true);
}
return default_result;
}
const time_of_day& tod_manager::get_time_of_day_turn(const std::vector<time_of_day>& times, int nturn, const int current_time) const
{
const int time = calculate_current_time(times.size(), nturn, current_time);
return times[time];
}
void tod_manager::modify_turns(const std::string& mod)
{
num_turns_ = std::max<int>(utils::apply_modifier(num_turns_,mod,0),-1);
}
void tod_manager::set_number_of_turns(int num)
{
num_turns_ = std::max<int>(num, -1);
}
void tod_manager::set_turn(const int num, const bool increase_limit_if_needed)
{
const int new_turn = std::max<int>(num, 1);
LOG_NG << "changing current turn number from " << turn_ << " to " << new_turn << '\n';
// Correct ToD
set_new_current_times(new_turn);
if(increase_limit_if_needed && (new_turn > num_turns_) && num_turns_ != -1) {
set_number_of_turns(new_turn);
}
turn_ = new_turn;
if (resources::gamedata)
resources::gamedata->get_variable("turn_number") = new_turn;
}
void tod_manager::set_new_current_times(const int new_current_turn_number)
{
currentTime_ = calculate_current_time(times_.size(), new_current_turn_number, currentTime_);
BOOST_FOREACH(area_time_of_day& area, areas_) {
area.currentTime = calculate_current_time(
area.times.size(),
new_current_turn_number,
area.currentTime);
}
}
int tod_manager::calculate_current_time(
const int number_of_times,
const int for_turn_number,
const int current_time,
const bool only_to_allowed_range) const
{
if (number_of_times == 0) return 0;
int new_current_time = 0;
if(only_to_allowed_range) new_current_time = current_time % number_of_times;
else new_current_time = (current_time + for_turn_number - turn_) % number_of_times;
while(new_current_time < 0) { new_current_time += number_of_times; }
return new_current_time;
}
bool tod_manager::next_turn()
{
set_turn(turn_ + 1, false);
return is_time_left();
}
bool tod_manager::is_time_left()
{
return num_turns_ == -1 || turn_ <= num_turns_;
}