The Battle for Wesnoth  1.17.17+dev
replay_controller.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2015 - 2023
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  }
80 }
82 {
85  }
88 }
90 {
91  const config& theme_cfg = theme::get_theme_config(controller_.theme());
92  if (const auto res = theme_cfg.optional_child("resolution"))
93  {
94  if (const auto replay_theme_cfg = res->optional_child("replay")) {
95  controller_.get_display().get_theme().modify(replay_theme_cfg.value());
96  }
97  }
98 }
99 
101 {
104 }
105 
107 {
108  stop_condition_.reset(new replay_play_turn(controller_.gamestate().tod_manager_.turn()));
110 }
111 
113 {
114  stop_condition_.reset(new replay_play_side());
116 }
117 
119 {
120  stop_condition_.reset(new replay_play_moves(1));
122 }
123 
124 //move all sides till stop/end
126 {
127  stop_condition_.reset(new replay_play_nostop());
129 }
130 
132 {
134 }
135 
137 {
139 }
140 
141 void replay_controller::handle_generic_event(const std::string& name)
142 {
143  // this is only attached to one event - the theme_reset_event
144  if(name == "theme_reset") {
146  }
147  if(std::shared_ptr<gui::button> skip_animation_button = controller_.get_display().find_action_button("skip-animation")) {
148  skip_animation_button->set_check(controller_.is_skipping_replay());
149  }
150 }
151 
153 {
154  return resources::recorder->at_end();
155 }
156 
158 {
160  while(!return_to_play_side_ && !static_cast<playsingle_controller&>(controller_).get_player_type_changed())
161  {
162  if(!stop_condition_->should_stop())
163  {
164  if(resources::recorder->at_end()) {
165  //Gather more replay data
166  on_end_replay_();
167  }
168  else {
169  REPLAY_RETURN res = do_replay(true);
170  if(res == REPLAY_FOUND_END_MOVE) {
171  stop_condition_->move_done();
172  }
174  return;
175  }
176  if(res == REPLAY_FOUND_END_TURN) {
177  return;
178  }
179  // TODO: how can this be the case when we just checked for "resources::recorder->at_end()" above?
180  if(res == REPLAY_RETURN_AT_END) {
181  stop_replay();
182  }
183  if(res == REPLAY_FOUND_INIT_TURN)
184  {
186  }
187  }
188  controller_.play_slice(false);
189 
190  // Update the buttons once, on the transition from not-stopped to stopped.
191  if(stop_condition_->should_stop()) {
193  }
194  }
195  else
196  {
197  // Don't move the update_enabled_buttons() call here. This play_slice() should block
198  // until the next event occurs, but on X11/Linux update_enabled_buttons() seems to put
199  // an event in the queue, turning this into a busy loop.
200  controller_.play_slice(true);
201  }
202  }
203  return;
204 }
206 {
207  switch(cmd.hotkey_command) {
209  return true;
213  return is_controlling_view();
214  //commands we only can do before the end of the replay
216  return !recorder_at_end();
221  //we have one events_disabler when starting the replay_controller and a second when entering the synced context.
222  return should_stop() && (events::commands_disabled <= 1 ) && !recorder_at_end();
225  default:
226  assert(false);
227  return false;
228  }
229 }
230 
232 {
233  vision_ = SHOW_ALL;
234  update_teams();
235 }
236 
238 {
240  update_teams();
241 }
242 
244 {
246  update_teams();
247 }
248 
250 {
253  update_gui();
254 }
255 
257 {
258  assert(vision_);
259  int viewing_side_num = vision_ == HUMAN_TEAM ? controller_.find_viewing_side() : controller_.current_side();
260  if(viewing_side_num != 0) {
261  controller_.update_gui_to_player(viewing_side_num - 1, *vision_ == SHOW_ALL);
262  }
263 }
264 
266 {
267  return vision_ == SHOW_ALL;
268 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Euivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition: config.cpp:389
virtual void play_slice(bool is_delay_enabled=true)
std::shared_ptr< gui::button > find_action_button(const std::string &id)
Retrieves a pointer to a theme UI button.
Definition: display.cpp:806
void invalidate_all()
Function to invalidate all tiles.
Definition: display.cpp:3087
theme & get_theme()
Definition: display.hpp:386
void queue_rerender()
Marks everything for rendering including all tiles and sidebar.
Definition: display.cpp:2275
virtual bool attach_handler(observer *obs)
virtual bool detach_handler(observer *obs)
tod_manager tod_manager_
Definition: game_state.hpp:48
int find_viewing_side() const
returns 0 if no such team was found.
bool is_regular_game_end() const
game_state & gamestate()
game_display & get_display() override
Get a reference to a display member a derived class uses.
std::string theme() const
int current_side() const
Returns the number of the side whose turn it is.
bool is_skipping_replay() const
void update_gui_to_player(const int team_index, const bool observe=false)
Changes the UI for this client to the passed side index.
replay_controller(play_controller &controller, bool control_view, const std::shared_ptr< config > &reset_state, const std::function< void()> &on_end_replay=nop)
bool recorder_at_end() const
std::optional< REPLAY_VISION > vision_
play_controller & controller_
void handle_generic_event(const std::string &name) override
bool is_controlling_view() const
std::function< void()> on_end_replay_
Called when there are no more moves in the [replay] to process.
std::unique_ptr< replay_stop_condition > stop_condition_
bool can_execute_command(const hotkey::ui_command &cmd) const
bool should_stop() const
bool allow_reset_replay() const
bool return_to_play_side_
Used by unit tests.
void update_enabled_buttons()
Refresh the states of the replay-control buttons, this will cause the hotkey framework to query can_e...
bool at_end() const
Definition: replay.cpp:631
static const config & get_theme_config(const std::string &id)
Returns the saved config for the theme with the given ID.
Definition: theme.cpp:1004
events::generic_event & theme_reset_event()
Definition: theme.hpp:284
void modify(const config &cfg)
Definition: theme.cpp:820
int turn() const
Standard logging facilities (interface).
@ HOTKEY_REPLAY_PLAY
@ HOTKEY_REPLAY_STOP
@ HOTKEY_REPLAY_NEXT_TURN
@ HOTKEY_REPLAY_SHOW_EVERYTHING
@ HOTKEY_REPLAY_SHOW_TEAM1
@ HOTKEY_REPLAY_NEXT_SIDE
@ HOTKEY_REPLAY_NEXT_MOVE
@ HOTKEY_REPLAY_RESET
@ HOTKEY_REPLAY_SKIP_ANIMATION
@ HOTKEY_REPLAY_SHOW_EACH
replay * recorder
Definition: resources.cpp:29
REPLAY_RETURN do_replay(bool one_move)
Definition: replay.cpp:682
Replay control code.
REPLAY_RETURN
Definition: replay.hpp:157
@ REPLAY_FOUND_INIT_TURN
Definition: replay.hpp:161
@ REPLAY_RETURN_AT_END
Definition: replay.hpp:158
@ REPLAY_FOUND_END_MOVE
Definition: replay.hpp:162
@ REPLAY_FOUND_END_TURN
Definition: replay.hpp:160
static lg::log_domain log_engine("engine")
static lg::log_domain log_replay("replay")
Used as the main paramneter for can_execute_command/do_execute_command These functions are used to ex...
hotkey::HOTKEY_COMMAND hotkey_command
The hotkey::HOTKEY_COMMAND associated with this action, HOTKEY_NULL for actions that don't allow hotk...