The Battle for Wesnoth  1.19.7+dev
Classes | Public Types | Static Public Member Functions | Static Private Attributes | List of all members
synced_context Class Reference

#include <synced_context.hpp>

Classes

class  server_choice
 

Public Types

enum  synced_state { UNSYNCED , SYNCED , LOCAL_CHOICE }
 

Static Public Member Functions

static bool run (const std::string &commandname, const config &data, action_spectator &spectator=get_default_spectator())
 Sets the context to 'synced', initialises random context, and calls the given function. More...
 
static bool run_and_store (const std::string &commandname, const config &data, action_spectator &spectator=get_default_spectator())
 
static bool run_and_throw (const std::string &commandname, const config &data, action_spectator &spectator=get_default_spectator())
 
static bool run_in_synced_context_if_not_already (const std::string &commandname, const config &data, action_spectator &spectator=get_default_spectator())
 Checks whether we are currently running in a synced context, and if not we enters it. More...
 
static synced_state get_synced_state ()
 
static bool is_synced ()
 
static bool is_unsynced ()
 
static void set_synced_state (synced_state newstate)
 Should only be called form set_scontext_synced, set_scontext_local_choice. More...
 
static std::string generate_random_seed ()
 Generates a new seed for a synced event, by asking the 'server'. More...
 
static void pull_remote_user_input ()
 called from get_user_choice while waiting for a remove user choice. More...
 
static void send_user_choice ()
 called from get_user_choice to send a recently made choice to the other clients. More...
 
static action_spectatorget_default_spectator ()
 An object to be passed to run_in_synced_context to assert false on error (the default). More...
 
static std::shared_ptr< randomness::rngget_rng_for_action ()
 
static void block_undo (bool do_block=true, bool clear_undo=true)
 set this to false to prevent clearing the undo stack, this is important when we cannot change the gamestate becasue with dsu clearing the undo stack changes the gamestate, which we for example don't want during formula evaluation, when it used the dice operator. More...
 
static void reset_block_undo ()
 
static bool undo_blocked ()
 
static void set_last_unit_id (int id)
 
static int get_unit_id_diff ()
 
static config ask_server_choice (const server_choice &)
 If we are in a mp game, ask the server, otherwise generate the answer ourselves. More...
 
static bool ignore_undo ()
 

Static Private Attributes

static synced_state state_ = synced_context::UNSYNCED
 Weather we are in a synced move, in a user_choice, or none of them. More...
 
static bool is_undo_blocked_ = false
 As soon as get_user_choice is used with side != current_side (for example in generate_random_seed) other sides execute the command simultaneously and is_simultaneously is set to true. More...
 
static int last_unit_id_ = 0
 Used to restore the unit id manager when undoing. More...
 

Detailed Description

Definition at line 29 of file synced_context.hpp.

Member Enumeration Documentation

◆ synced_state

Enumerator
UNSYNCED 
SYNCED 
LOCAL_CHOICE 

Definition at line 32 of file synced_context.hpp.

Member Function Documentation

◆ ask_server_choice()

config synced_context::ask_server_choice ( const server_choice sch)
static

◆ block_undo()

void synced_context::block_undo ( bool  do_block = true,
bool  clear_undo = true 
)
static

set this to false to prevent clearing the undo stack, this is important when we cannot change the gamestate becasue with dsu clearing the undo stack changes the gamestate, which we for example don't want during formula evaluation, when it used the dice operator.

TODO: consider removing this parameter when dsu is removed.

Definition at line 184 of file synced_context.cpp.

References actions::undo_list::clear(), resources::controller, is_undo_blocked_, play_controller::send_actions(), and resources::undo_stack.

Referenced by ask_server_choice(), play_controller::do_init_side(), play_controller::finish_side_turn_events(), get_rng_for_action(), actions::recall_unit(), actions::recruit_unit(), play_controller::start_game(), SYNCED_COMMAND_HANDLER_FUNCTION(), and user_choice_manager::user_choice_manager().

◆ generate_random_seed()

std::string synced_context::generate_random_seed ( )
static

Generates a new seed for a synced event, by asking the 'server'.

Definition at line 176 of file synced_context.cpp.

References ask_server_choice(), and config_attribute_value::str().

Referenced by get_rng_for_action().

◆ get_default_spectator()

action_spectator & synced_context::get_default_spectator ( )
static

An object to be passed to run_in_synced_context to assert false on error (the default).

Definition at line 137 of file synced_context.cpp.

References ERR_REPLAY.

◆ get_rng_for_action()

std::shared_ptr< randomness::rng > synced_context::get_rng_for_action ( )
static
Returns
A rng_deterministic if in determinsic mode otherwise a rng_synced.

Definition at line 232 of file synced_context.cpp.

References block_undo(), resources::classification, resources::gamedata, generate_random_seed(), randomness::mt_rng::get_next_random(), game_classification::random_mode, and game_data::rng().

◆ get_synced_state()

static synced_state synced_context::get_synced_state ( )
inlinestatic

◆ get_unit_id_diff()

int synced_context::get_unit_id_diff ( )
static

◆ ignore_undo()

bool synced_context::ignore_undo ( )
static

◆ is_synced()

static bool synced_context::is_synced ( )
inlinestatic

◆ is_unsynced()

static bool synced_context::is_unsynced ( )
inlinestatic
Returns
Whether we are not currently executing a synced action like recruit, start, recall, disband, movement, attack, init_side, end_turn, fire_event, lua_ai, auto_shroud or similar. and not doing a local choice.

Definition at line 84 of file synced_context.hpp.

References get_synced_state(), and UNSYNCED.

Referenced by wb::manager::can_modify_game_state(), playmp_controller::send_actions(), and undo_blocked().

◆ pull_remote_user_input()

void synced_context::pull_remote_user_input ( )
static

called from get_user_choice while waiting for a remove user choice.

Definition at line 220 of file synced_context.cpp.

References resources::controller, and play_controller::receive_actions().

Referenced by ask_server_choice(), and user_choice_manager::pull().

◆ reset_block_undo()

static void synced_context::reset_block_undo ( )
inlinestatic

Definition at line 119 of file synced_context.hpp.

References is_undo_blocked_.

Referenced by set_scontext_synced_base::set_scontext_synced_base().

◆ run()

bool synced_context::run ( const std::string &  commandname,
const config data,
action_spectator spectator = get_default_spectator() 
)
static

Sets the context to 'synced', initialises random context, and calls the given function.

The plan is that in replays and in real game the same function is called. However, if you cannot call this function you can also use set_scontext_synced directly (use it like it's used in this method).

Parameters
commandnameThe command to run.
dataThe data to use with the command.
spectatorAn error handler for the case that data contains invalid data.
Returns
True if the action was successful.

Definition at line 47 of file synced_context.cpp.

References play_controller::check_victory(), resources::controller, data, DBG_REPLAY, set_scontext_synced::do_final_checkup(), action_spectator::error(), utils::find(), actions::undo_list::finish_action(), actions::undo_list::init_action(), synced_command::registry(), send_user_choice(), undo_blocked(), and resources::undo_stack.

Referenced by do_replay_handle(), actions::undo_list::redo(), and run_and_store().

◆ run_and_store()

bool synced_context::run_and_store ( const std::string &  commandname,
const config data,
action_spectator spectator = get_default_spectator() 
)
static

◆ run_and_throw()

bool synced_context::run_and_throw ( const std::string &  commandname,
const config data,
action_spectator spectator = get_default_spectator() 
)
static

◆ run_in_synced_context_if_not_already()

bool synced_context::run_in_synced_context_if_not_already ( const std::string &  commandname,
const config data,
action_spectator spectator = get_default_spectator() 
)
static

Checks whether we are currently running in a synced context, and if not we enters it.

This is never called from so_replay_handle.

Definition at line 109 of file synced_context.cpp.

References data, ERR_REPLAY, action_spectator::error(), get_synced_state(), LOCAL_CHOICE, synced_command::registry(), run_and_throw(), SYNCED, and UNSYNCED.

Referenced by ai::attack_result::do_execute(), ai::recall_result::do_execute(), ai::recruit_result::do_execute(), game_events::wml_menu_item::fire_event(), and game_events::WML_HANDLER_FUNCTION().

◆ send_user_choice()

void synced_context::send_user_choice ( )
static

called from get_user_choice to send a recently made choice to the other clients.

Does not receive any data from the network any sends data.

Definition at line 226 of file synced_context.cpp.

References resources::controller, play_controller::send_actions(), and undo_blocked().

Referenced by user_choice_manager::ask_local_choice(), and run().

◆ set_last_unit_id()

static void synced_context::set_last_unit_id ( int  id)
inlinestatic

Definition at line 127 of file synced_context.hpp.

References id, and last_unit_id_.

Referenced by set_scontext_synced_base::set_scontext_synced_base().

◆ set_synced_state()

static void synced_context::set_synced_state ( synced_state  newstate)
inlinestatic

◆ undo_blocked()

bool synced_context::undo_blocked ( )
static

Member Data Documentation

◆ is_undo_blocked_

bool synced_context::is_undo_blocked_ = false
inlinestaticprivate

As soon as get_user_choice is used with side != current_side (for example in generate_random_seed) other sides execute the command simultaneously and is_simultaneously is set to true.

It's impossible to undo data that has been sent over the network.

false = we are on a local turn and haven't sent anything yet.

Definition at line 168 of file synced_context.hpp.

Referenced by block_undo(), reset_block_undo(), and undo_blocked().

◆ last_unit_id_

int synced_context::last_unit_id_ = 0
inlinestaticprivate

Used to restore the unit id manager when undoing.

Definition at line 171 of file synced_context.hpp.

Referenced by get_unit_id_diff(), and set_last_unit_id().

◆ state_

synced_state synced_context::state_ = synced_context::UNSYNCED
inlinestaticprivate

Weather we are in a synced move, in a user_choice, or none of them.

Definition at line 159 of file synced_context.hpp.

Referenced by get_synced_state(), and set_synced_state().


The documentation for this class was generated from the following files: