The Battle for Wesnoth  1.17.0-dev
replay_controller.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2015 - 2021
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "replay_controller.hpp"
16 
17 #include "game_config_manager.hpp"
18 #include "gettext.hpp"
19 #include "log.hpp"
20 #include "replay.hpp"
21 #include "resources.hpp"
23 
24 static lg::log_domain log_engine("engine");
25 #define DBG_NG LOG_STREAM(debug, log_engine)
26 
27 static lg::log_domain log_replay("replay");
28 #define DBG_REPLAY LOG_STREAM(debug, log_replay)
29 #define LOG_REPLAY LOG_STREAM(info, log_replay)
30 #define ERR_REPLAY LOG_STREAM(err, log_replay)
31 
32 namespace
33 {
34 struct replay_play_nostop : public replay_controller::replay_stop_condition
35 {
36  replay_play_nostop() {}
37  virtual bool should_stop() { return false; }
38 };
39 
40 struct replay_play_moves : public replay_controller::replay_stop_condition
41 {
42  int moves_todo_;
43  replay_play_moves(int moves_todo) : moves_todo_(moves_todo) {}
44  virtual void move_done() { --moves_todo_; }
45  virtual bool should_stop() { return moves_todo_ == 0; }
46 };
47 
48 struct replay_play_turn : public replay_controller::replay_stop_condition
49 {
50  int turn_begin_;
51  int turn_current_;
52  replay_play_turn(int turn_begin) : turn_begin_(turn_begin), turn_current_(turn_begin) {}
53  virtual void new_side_turn(int , int turn) { turn_current_ = turn; }
54  virtual bool should_stop() { return turn_begin_ != turn_current_; }
55 };
56 
57 struct replay_play_side : public replay_controller::replay_stop_condition
58 {
59  bool next_side_;
60  replay_play_side() : next_side_(false) {}
61  virtual void new_side_turn(int , int) { next_side_ = true; }
62  virtual bool should_stop() { return next_side_; }
63 };
64 }
65 
66 replay_controller::replay_controller(play_controller& controller, bool control_view, const std::shared_ptr<config>& reset_state, const std::function<void()>& on_end_replay)
67  : controller_(controller)
68  , stop_condition_(new replay_stop_condition())
69  , disabler_()
70  , vision_()
71  , reset_state_(reset_state)
72  , on_end_replay_(on_end_replay)
73  , return_to_play_side_(false)
74 {
75  if(control_view) {
77  }
81 }
83 {
86  }
91 }
93 {
95  if (const auto res = theme_cfg.optional_child("resolution"))
96  {
97  if (const auto replay_theme_cfg = res->optional_child("replay")) {
98  controller_.get_display().get_theme().modify(replay_theme_cfg.value());
99  }
100  //Make sure we get notified if the theme is redrawn completely. That way we have
101  //a chance to restore the replay controls of the theme as well.
103  }
104 }
105 
107 {
110 }
111 
113 {
114  stop_condition_.reset(new replay_play_turn(controller_.gamestate().tod_manager_.turn()));
116 }
117 
119 {
120  stop_condition_.reset(new replay_play_side());
122 }
123 
125 {
126  stop_condition_.reset(new replay_play_moves(1));
128 }
129 
130 //move all sides till stop/end
132 {
133  stop_condition_.reset(new replay_play_nostop());
135 }
136 
138 {
143 }
144 
146 {
149 }
150 
151 void replay_controller::handle_generic_event(const std::string& name)
152 {
153  // this is only attached to one event - the theme_reset_event
154  if(name == "theme_reset") {
156  }
157  if(std::shared_ptr<gui::button> skip_animation_button = controller_.get_display().find_action_button("skip-animation")) {
158  skip_animation_button->set_check(controller_.is_skipping_replay());
159  }
160 }
161 
163 {
164  return resources::recorder->at_end();
165 }
166 
168 {
170  while(!return_to_play_side_ && !static_cast<playsingle_controller&>(controller_).get_player_type_changed())
171  {
172  if(!stop_condition_->should_stop())
173  {
174  if(resources::recorder->at_end()) {
175  //Gather more replay data
176  on_end_replay_();
177  }
178  else {
179  REPLAY_RETURN res = do_replay(true);
180  if(res == REPLAY_FOUND_END_MOVE) {
181  stop_condition_->move_done();
182  }
183  if(res == REPLAY_FOUND_END_TURN) {
184  return res;
185  }
186  if(res == REPLAY_RETURN_AT_END) {
187  stop_replay();
188  }
189  if(res == REPLAY_FOUND_INIT_TURN)
190  {
192  }
193  }
194  controller_.play_slice(false);
195 
196  // Update the buttons once, on the transition from not-stopped to stopped.
197  if(stop_condition_->should_stop()) {
199  }
200  }
201  else
202  {
203  // Don't move the update_enabled_buttons() call here. This play_slice() should block
204  // until the next event occurs, but on X11/Linux update_enabled_buttons() seems to put
205  // an event in the queue, turning this into a busy loop.
206  controller_.play_slice(true);
207  }
208  }
209  return REPLAY_FOUND_END_MOVE;
210 }
212 {
213  hotkey::HOTKEY_COMMAND command = cmd.id;
214 
215  switch(command) {
217  return true;
221  return is_controlling_view();
222  //commands we only can do before the end of the replay
224  return !recorder_at_end();
229  //we have one events_disabler when starting the replay_controller and a second when entering the synced context.
230  return should_stop() && (events::commands_disabled <= 1 ) && !recorder_at_end();
233  default:
234  assert(false);
235  return false;
236  }
237 }
238 
240 {
241  vision_ = SHOW_ALL;
242  update_teams();
243 }
244 
246 {
248  update_teams();
249 }
250 
252 {
254  update_teams();
255 }
256 
258 {
261  update_gui();
262 }
263 
265 {
266  assert(vision_);
268 }
269 
271 {
272  return vision_ == SHOW_ALL;
273 }
std::shared_ptr< gui::button > find_action_button(const std::string &id)
Retrieves a pointer to a theme UI button.
Definition: display.cpp:832
std::function< void()> on_end_replay_
HOTKEY_COMMAND id
the names are strange: the "hotkey::HOTKEY_COMMAND" is named id, and the string to identify the objec...
theme & get_theme()
Definition: display.hpp:384
replay_controller(play_controller &controller, bool control_view, const std::shared_ptr< config > &reset_state, const std::function< void()> &on_end_replay=nop)
bool is_skipping_replay() const
static lg::log_domain log_engine("engine")
game_display & get_display() override
Get a reference to a display member a derived class uses.
bool at_end() const
Definition: replay.cpp:633
int first_human_team_
Definition: game_state.hpp:75
void redraw_everything()
Invalidates entire screen, including all tiles and sidebar.
Definition: display.cpp:2419
static const config & get_theme(const game_config_view &game_config, std::string theme_name)
Stores all information related to functions that can be bound to hotkeys.
virtual void play_slice(bool is_delay_enabled=true)
Replay control code.
REPLAY_RETURN do_replay(bool one_move)
Definition: replay.cpp:684
bool recorder_at_end() const
void redraw_minimap()
Schedule the minimap to be redrawn.
Definition: display.hpp:625
void handle_generic_event(const std::string &name) override
REPLAY_RETURN
Definition: replay.hpp:156
std::unique_ptr< replay_stop_condition > stop_condition_
static game_config_manager * get()
std::optional< REPLAY_VISION > vision_
void recalculate_minimap()
Schedule the minimap for recalculation.
Definition: display.hpp:619
events::generic_event & theme_reset_event()
Definition: theme.hpp:286
void update_enabled_buttons()
Refresh the states of the replay-control buttons, this will cause the hotkey framework to query can_e...
void modify(const config &cfg)
Definition: theme.cpp:819
tod_manager tod_manager_
Definition: game_state.hpp:48
replay * recorder
Definition: resources.cpp:29
std::string theme() const
utils::optional_reference< config > optional_child(config_key_type key, int n=0)
Euivalent to child, but returns an empty optional if the nth child was not found. ...
Definition: config.cpp:445
void create_buttons()
Definition: display.cpp:879
REPLAY_RETURN play_side_impl()
void invalidate_all()
Function to invalidate all tiles.
Definition: display.cpp:3019
void update_gui_to_player(const int team_index, const bool observe=false)
Changes the UI for this client to the passed side index.
bool allow_reset_replay() const
virtual bool attach_handler(observer *obs)
bool can_execute_command(const hotkey::hotkey_command &cmd, int index) const
static lg::log_domain log_replay("replay")
Game configuration data as global variables.
Definition: build_info.cpp:59
game_state & gamestate()
play_controller & controller_
bool is_controlling_view() const
Standard logging facilities (interface).
int current_side() const
Returns the number of the side whose turn it is.
virtual bool detach_handler(observer *obs)
int turn() const
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
void invalidate_theme()
Definition: display.hpp:407
bool should_stop() const