-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
replay_controller.cpp
382 lines (331 loc) · 10.4 KB
/
replay_controller.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
/*
Copyright (C) 2015 - 2017 by the Battle for Wesnoth Project
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 "replay_controller.hpp"
#include "game_config_manager.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "replay.hpp"
#include "resources.hpp"
#include "playsingle_controller.hpp"
static lg::log_domain log_engine("engine");
#define DBG_NG LOG_STREAM(debug, log_engine)
static lg::log_domain log_replay("replay");
#define DBG_REPLAY LOG_STREAM(debug, log_replay)
#define LOG_REPLAY LOG_STREAM(info, log_replay)
#define ERR_REPLAY LOG_STREAM(err, log_replay)
namespace
{
struct replay_play_nostop : public replay_controller::replay_stop_condition
{
replay_play_nostop() {}
virtual bool should_stop() { return false; }
};
struct replay_play_moves : public replay_controller::replay_stop_condition
{
int moves_todo_;
replay_play_moves(int moves_todo) : moves_todo_(moves_todo) {}
virtual void move_done() { --moves_todo_; }
virtual bool should_stop() { return moves_todo_ == 0; }
};
struct replay_play_turn : public replay_controller::replay_stop_condition
{
int turn_begin_;
int turn_current_;
replay_play_turn(int turn_begin) : turn_begin_(turn_begin), turn_current_(turn_begin) {}
virtual void new_side_turn(int , int turn) { turn_current_ = turn; }
virtual bool should_stop() { return turn_begin_ != turn_current_; }
};
struct replay_play_side : public replay_controller::replay_stop_condition
{
bool next_side_;
replay_play_side() : next_side_(false) {}
virtual void new_side_turn(int , int) { next_side_ = true; }
virtual bool should_stop() { return next_side_; }
};
}
replay_controller::replay_controller(play_controller& controller, bool control_view, const std::shared_ptr<config>& reset_state, const std::function<void()>& on_end_replay)
: controller_(controller)
, stop_condition_(new replay_stop_condition())
, disabler_()
, vision_()
, reset_state_(reset_state)
, on_end_replay_(on_end_replay)
, return_to_play_side_(false)
{
if(control_view) {
vision_ = HUMAN_TEAM;
}
controller_.get_display().get_theme().theme_reset_event().attach_handler(this);
controller_.get_display().create_buttons();
controller_.get_display().redraw_everything();
}
replay_controller::~replay_controller()
{
if(controller_.is_skipping_replay()) {
controller_.toggle_skipping_replay();
}
controller_.get_display().get_theme().theme_reset_event().detach_handler(this);
controller_.get_display().create_buttons();
controller_.get_display().redraw_everything();
controller_.get_display().create_buttons();
}
void replay_controller::add_replay_theme()
{
const config &theme_cfg = controller_.get_theme(game_config_manager::get()->game_config(), controller_.theme());
if (const config &res = theme_cfg.child("resolution"))
{
if (const config &replay_theme_cfg = res.child("replay")) {
controller_.get_display().get_theme().modify(replay_theme_cfg);
}
//Make sure we get notified if the theme is redrawn completely. That way we have
//a chance to restore the replay controls of the theme as well.
controller_.get_display().invalidate_theme();
}
}
void replay_controller::rebuild_replay_theme()
{
const config &theme_cfg = controller_.get_theme(game_config_manager::get()->game_config(), controller_.theme());
if (const config &res = theme_cfg.child("resolution"))
{
if (const config &replay_theme_cfg = res.child("replay")) {
controller_.get_display().get_theme().modify(replay_theme_cfg);
}
controller_.get_display().get_theme().modify_label("time-icon", _ ("current local time"));
//Make sure we get notified if the theme is redrawn completely. That way we have
//a chance to restore the replay controls of the theme as well.
controller_.get_display().invalidate_theme();
}
}
std::shared_ptr<gui::button> replay_controller::play_button()
{
return controller_.get_display().find_action_button("button-playreplay");
}
std::shared_ptr<gui::button> replay_controller::stop_button()
{
return controller_.get_display().find_action_button("button-stopreplay");
}
std::shared_ptr<gui::button> replay_controller::reset_button()
{
return controller_.get_display().find_action_button("button-resetreplay");
}
std::shared_ptr<gui::button> replay_controller::play_turn_button()
{
return controller_.get_display().find_action_button("button-nextturn");
}
std::shared_ptr<gui::button> replay_controller::play_side_button()
{
return controller_.get_display().find_action_button("button-nextside");
}
std::shared_ptr<gui::button> replay_controller::play_move_button()
{
return controller_.get_display().find_action_button("button-nextmove");
}
void replay_controller::update_replay_ui()
{
//check if we have all buttons - if someone messed with theme then some buttons may be missing
//if any of the buttons is missing, we just disable every one
if(!replay_ui_has_all_buttons()) {
std::shared_ptr<gui::button> play_b = play_button(), stop_b = stop_button(),
reset_b = reset_button(), play_turn_b = play_turn_button(),
play_side_b = play_side_button(), play_move_b = play_move_button();
if(play_b) {
play_b->enable(false);
}
if(stop_b) {
stop_b->enable(false);
}
if(reset_b) {
reset_b->enable(false);
}
if(play_turn_b) {
play_turn_b->enable(false);
}
if(play_side_b) {
play_side_b->enable(false);
}
if (play_move_b) {
play_move_b->enable(false);
}
}
}
void replay_controller::replay_ui_playback_should_start()
{
if(!replay_ui_has_all_buttons())
return;
play_button()->enable(false);
reset_button()->enable(false);
play_turn_button()->enable(false);
play_side_button()->enable(false);
play_move_button()->enable(false);
}
void replay_controller::replay_ui_playback_should_stop()
{
if(!replay_ui_has_all_buttons())
return;
if(!resources::recorder->at_end()) {
play_button()->enable(true);
reset_button()->enable(true);
play_turn_button()->enable(true);
play_side_button()->enable(true);
play_move_button()->enable(true);
play_button()->release();
play_turn_button()->release();
play_side_button()->release();
play_move_button()->release();
} else {
reset_button()->enable(true);
stop_button()->enable(false);
}
if(stop_condition_->should_stop()) {
//user interrupted
stop_button()->release();
}
}
void replay_controller::reset_replay_ui()
{
if(!replay_ui_has_all_buttons())
return;
play_button()->enable(true);
stop_button()->enable(true);
reset_button()->enable(true);
play_turn_button()->enable(true);
play_side_button()->enable(true);
}
void replay_controller::stop_replay()
{
stop_condition_.reset(new replay_stop_condition());
}
void replay_controller::replay_next_turn()
{
stop_condition_.reset(new replay_play_turn(controller_.gamestate().tod_manager_.turn()));
}
void replay_controller::replay_next_side()
{
stop_condition_.reset(new replay_play_side());
}
void replay_controller::replay_next_move()
{
stop_condition_.reset(new replay_play_moves(1));
}
//move all sides till stop/end
void replay_controller::play_replay()
{
stop_condition_.reset(new replay_play_nostop());
}
void replay_controller::update_gui()
{
controller_.get_display().recalculate_minimap();
controller_.get_display().redraw_minimap();
controller_.get_display().invalidate_all();
}
void replay_controller::handle_generic_event(const std::string& name)
{
if( name == "completely_redrawn" ) {
update_replay_ui();
} else {
add_replay_theme();
}
if(std::shared_ptr<gui::button> skip_animation_button = controller_.get_display().find_action_button("skip-animation")) {
skip_animation_button->set_check(controller_.is_skipping_replay());
}
}
bool replay_controller::recorder_at_end() const
{
return resources::recorder->at_end();
}
REPLAY_RETURN replay_controller::play_side_impl()
{
while(!return_to_play_side_ && !static_cast<playsingle_controller&>(controller_).get_player_type_changed())
{
if(!stop_condition_->should_stop())
{
if(resources::recorder->at_end()) {
//Gather more replay data
on_end_replay_();
}
else {
REPLAY_RETURN res = do_replay(true);
if(res == REPLAY_FOUND_END_MOVE) {
stop_condition_->move_done();
}
if(res == REPLAY_FOUND_END_TURN) {
return res;
}
if(res == REPLAY_RETURN_AT_END) {
stop_replay();
}
if(res == REPLAY_FOUND_INIT_TURN)
{
stop_condition_->new_side_turn(controller_.current_side(), controller_.gamestate().tod_manager_.turn());
}
}
controller_.play_slice(false);
}
else
{
controller_.play_slice(true);
replay_ui_playback_should_stop();
}
}
return REPLAY_FOUND_END_MOVE;
}
bool replay_controller::can_execute_command(const hotkey::hotkey_command& cmd, int) const
{
hotkey::HOTKEY_COMMAND command = cmd.id;
switch(command) {
case hotkey::HOTKEY_REPLAY_SKIP_ANIMATION:
return true;
case hotkey::HOTKEY_REPLAY_SHOW_EVERYTHING:
case hotkey::HOTKEY_REPLAY_SHOW_EACH:
case hotkey::HOTKEY_REPLAY_SHOW_TEAM1:
return is_controlling_view();
//commands we only can do before the end of the replay
case hotkey::HOTKEY_REPLAY_STOP:
return !recorder_at_end();
case hotkey::HOTKEY_REPLAY_PLAY:
case hotkey::HOTKEY_REPLAY_NEXT_TURN:
case hotkey::HOTKEY_REPLAY_NEXT_SIDE:
case hotkey::HOTKEY_REPLAY_NEXT_MOVE:
//we have one events_disabler when starting the replay_controller and a second when entering the synced context.
return should_stop() && (events::commands_disabled <= 1 ) && !recorder_at_end();
case hotkey::HOTKEY_REPLAY_RESET:
return allow_reset_replay() && events::commands_disabled <= 1;
default:
assert(false);
return false;
}
}
void replay_controller::replay_show_everything()
{
vision_ = SHOW_ALL;
update_teams();
}
void replay_controller::replay_show_each()
{
vision_ = CURRENT_TEAM;
update_teams();
}
void replay_controller::replay_show_team1()
{
vision_ = HUMAN_TEAM;
update_teams();
}
void replay_controller::update_teams()
{
update_viewing_player();
controller_.get_display().invalidate_all();
update_gui();
}
void replay_controller::update_viewing_player()
{
assert(vision_);
controller_.update_gui_to_player(vision_ == HUMAN_TEAM ? controller_.gamestate().first_human_team_ : controller_.current_side() - 1, *vision_ == SHOW_ALL);
}