The Battle for Wesnoth  1.19.0-dev
playsingle_controller.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2024
3  by Joerg Hinrichs <joerg.hinrichs@alice-dsl.de>
4  Copyright (C) 2003 by David White <dave@whitevine.net>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 #pragma once
18 
19 #include "play_controller.hpp"
20 
21 #include "cursor.hpp"
24 #include "playturn.hpp"
25 #include "replay.hpp"
26 
27 #include <exception>
28 
29 class replay_controller;
30 class saved_game;
31 
32 struct reset_gamestate_exception final : public lua_jailbreak_exception, public std::exception
33 {
34  reset_gamestate_exception(std::shared_ptr<config> l, std::shared_ptr<config> stats, bool s = true) : level(l), stats_(stats), start_replay(s)
35  {
36  this->store();
37  }
38  std::shared_ptr<config> level;
39  std::shared_ptr<config> stats_;
41  const char * what() const noexcept { return "reset_gamestate_exception"; }
42 private:
43 
45 };
46 
48 {
49 public:
50  playsingle_controller(const config& level, saved_game& state_of_game, bool skip_replay);
51 
54  void play_scenario_init(const config& level);
55 
56  struct ses_result { int side_num; bool wrapped; };
57  /// Calculates the current side, starting at @a side_num that is non-empty
58  /// @return side_num: the new side that is non-empty,
59  /// @a side_num, if no such side was found.
60  /// wrapped: whether we wrapped around (usually indicates a new tod turn.)
61  /// also true if no non-empty side was found.
62  ses_result skip_empty_sides(int side_num);
63  void play_some();
64  void finish_side_turn();
65  void do_end_level();
67 
68  virtual void handle_generic_event(const std::string& name) override;
69 
70  virtual void check_objectives() override;
71  virtual void on_not_observer() override {}
72  virtual bool is_host() const { return true; }
73  virtual void maybe_linger();
74 
75  void end_turn();
76  void force_end_turn() override;
77  void require_end_turn();
78 
79  class hotkey_handler;
80  std::string describe_result() const;
81 
84  virtual bool should_return_to_play_side() const override;
85  replay_controller * get_replay_controller() const override { return replay_controller_.get(); }
86  void enable_replay(bool is_unit_test = false);
87  void on_replay_end(bool is_unit_test);
88 protected:
89  virtual void play_side_impl() override;
90  void before_human_turn();
91  void show_turn_dialog();
92  void execute_gotos();
93  virtual void play_human_turn();
94  virtual void after_human_turn();
95  void end_turn_enable(bool enable);
96  void play_ai_turn();
97  virtual void play_idle_loop();
98  virtual void do_idle_notification();
99  virtual void play_network_turn();
100  virtual void init_gui() override;
101 
103 
104  /// Helper to send our actions to the server
105  /// Used by turn_data_
107  /// Used by turn_data_
109  /// Helper to read and execute (in particular replay data/ user actions ) messsages from the server
111  /// true iff the user has pressed the end turn button this turn.
112  /// (or wants to end linger mode, which is implemented via the same button)
114  /// true when the current side is actually an ai side but was taken over by a human (usually for debugging purposes),
115  /// we need this variable to remember to give the ai control back next turn.
117  /// non-null when replay mode in active, is used in singleplayer and for the "back to turn" feature in multiplayer.
118  std::unique_ptr<replay_controller> replay_controller_;
119  void linger();
120  void update_gui_linger();
121  void sync_end_turn() override;
122  void update_viewing_player() override;
123  void reset_replay();
124 };
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Base class for exceptions that want to be thrown 'through' lua.
void store() const noexcept
Stores a copy the current exception to be rethrown.
bool player_type_changed_
true when the controller of the currently playing side has changed.
void end_turn_enable(bool enable)
playsingle_controller(const config &level, saved_game &state_of_game, bool skip_replay)
virtual void play_network_turn()
Will handle networked turns in descendent classes.
virtual void init_gui() override
void on_replay_end(bool is_unit_test)
std::string describe_result() const
std::unique_ptr< replay_controller > replay_controller_
non-null when replay mode in active, is used in singleplayer and for the "back to turn" feature in mu...
bool ai_fallback_
true when the current side is actually an ai side but was taken over by a human (usually for debuggin...
virtual void check_objectives() override
ses_result skip_empty_sides(int side_num)
Calculates the current side, starting at side_num that is non-empty.
replay_controller * get_replay_controller() const override
level_result::type play_scenario(const config &level)
virtual bool is_host() const
playturn_network_adapter network_reader_
Used by turn_data_.
virtual bool should_return_to_play_side() const override
virtual void play_side_impl() override
~playsingle_controller()
Defined here to reduce file includes.
replay_network_sender replay_sender_
Helper to send our actions to the server Used by turn_data_.
turn_info turn_data_
Helper to read and execute (in particular replay data/ user actions ) messsages from the server.
const cursor::setter cursor_setter_
void update_viewing_player() override
virtual void handle_generic_event(const std::string &name) override
virtual void do_idle_notification()
Will handle sending a networked notification in descendent classes.
bool end_turn_requested_
true iff the user has pressed the end turn button this turn.
virtual void on_not_observer() override
void play_scenario_init(const config &level)
void enable_replay(bool is_unit_test=false)
TODO: rename this class since it isn't that much related to turns.
Definition: playturn.hpp:29
#define IMPLEMENT_LUA_JAILBREAK_EXCEPTION(type)
Helper macro for classes deriving from lua_jailbreak_exception.
Replay control code.
std::shared_ptr< config > stats_
const char * what() const noexcept
std::shared_ptr< config > level
reset_gamestate_exception(std::shared_ptr< config > l, std::shared_ptr< config > stats, bool s=true)
static map_location::DIRECTION s