75 #define LOG_AIT LOG_STREAM(info, log_aitesting)
79 #define LOG_NG LOG_STREAM(info, log_engine)
80 #define DBG_NG LOG_STREAM(debug, log_engine)
81 #define ERR_NG LOG_STREAM(err, log_engine)
84 #define ERR_DP LOG_STREAM(err, log_display)
87 #define LOG_RG LOG_STREAM(info, log_enginerefac)
90 #define DBG_EE LOG_STREAM(debug, log_engine_enemies)
97 static const std::set<std::string> attrs {
100 "victory_when_enemies_defeated",
101 "remove_from_carryover_on_defeat",
103 "experience_modifier",
108 static const std::set<std::string> tags {
114 for(
const std::string& attr : attrs) {
115 dst[attr] = src[attr];
118 for(
const std::string& tag : tags) {
145 , ticks_(SDL_GetTicks())
148 , saved_game_(state_of_game)
149 , tooltips_manager_()
150 , whiteboard_manager_()
152 , labels_manager_(new
font::floating_label_context())
153 , help_manager_(&game_config_)
154 , mouse_handler_(nullptr, *this)
155 , menu_handler_(nullptr, *this)
157 , soundsources_manager_()
161 , statistics_context_(new
statistics_t(state_of_game.statistics()))
162 , replay_(new
replay(state_of_game.get_replay()))
163 , skip_replay_(skip_replay)
164 , skip_story_(state_of_game.skip_story())
165 , did_autosave_this_turn_(true)
166 , did_tod_sound_this_turn_(false)
168 , start_faded_(start_faded)
169 , victory_when_enemies_defeated_(
level[
"victory_when_enemies_defeated"].to_bool(true))
170 , remove_from_carryover_on_defeat_(
level[
"remove_from_carryover_on_defeat"].to_bool(true))
174 , ignore_replay_errors_(false)
175 , player_type_changed_(false)
224 LOG_NG <<
"initializing game_state..." << (SDL_GetTicks() -
ticks());
239 LOG_NG <<
"initializing whiteboard..." << (SDL_GetTicks() -
ticks());
244 LOG_NG <<
"loading units..." << (SDL_GetTicks() -
ticks());
248 LOG_NG <<
"initializing theme... " << (SDL_GetTicks() -
ticks());
251 LOG_NG <<
"building terrain rules... " << (SDL_GetTicks() -
ticks());
257 gui_->set_fade({0,0,0,255});
258 gui_->set_prevent_draw(
true);
266 gui_->get_theme().modify_label(
"time-icon",
_(
"time left for current turn"));
268 gui_->get_theme().modify_label(
"time-icon",
_(
"current local time"));
276 LOG_NG <<
"done initializing display... " << (SDL_GetTicks() -
ticks());
278 LOG_NG <<
"building gamestate to gui and whiteboard... " << (SDL_GetTicks() -
ticks());
312 gui_->labels().set_team(
nullptr);
343 LOG_NG <<
"initializing managers... " << (SDL_GetTicks() -
ticks());
347 LOG_NG <<
"done initializing managers... " << (SDL_GetTicks() -
ticks());
378 const config cfg(
"side",
gui_->viewing_side());
399 tm.set_start_gold(tm.gold());
467 const std::string turn_num = std::to_string(
turn());
468 const std::string side_num = std::to_string(
current_side());
473 if(!
gamestate().tod_manager_.has_turn_event_fired()) {
480 pump().
fire(
"side_" + side_num +
"_turn");
481 pump().
fire(
"side_turn_" + turn_num);
482 pump().
fire(
"side_" + side_num +
"_turn_" + turn_num);
512 patient.set_resting(
true);
520 pump().
fire(
"side_" + side_num +
"_turn_refresh");
521 pump().
fire(
"turn_" + turn_num +
"_refresh");
522 pump().
fire(
"side_" + side_num +
"_turn_" + turn_num +
"_refresh");
561 gui_->labels().write(cfg);
564 if(cfg[
"replay_pos"].to_int(0) > 0 && cfg[
"playing_team"].empty()) {
579 const std::string turn_num = std::to_string(
turn());
580 const std::string side_num = std::to_string(
current_side());
586 pump().
fire(
"side_" + side_num +
"_turn_end");
587 pump().
fire(
"side_turn_" + turn_num +
"_end");
588 pump().
fire(
"side_" + side_num +
"_turn_" + turn_num +
"_end");
600 const std::string turn_num = std::to_string(
turn());
602 pump().
fire(
"turn_" + turn_num +
"_end");
655 ERR_DP <<
"unknown textbox mode";
674 auto prev = std::find(command_history.begin(), command_history.end(), str);
676 if (
prev != command_history.end())
679 if(
prev != command_history.begin()) {
683 if(++
prev != command_history.end()) {
690 if(command_history.size() > 0) {
703 std::set<std::string> dictionary;
708 if(!
gui_->fogged(loc) && !(
get_teams()[
gui_->viewing_team()].is_enemy(u.side()) && u.invisible(loc)))
709 dictionary.insert(u.name());
716 dictionary.insert(commands.begin(), commands.end());
722 dictionary.insert(
t.current_player());
726 for(
const std::string& o :
gui_->observers()) {
727 dictionary.insert(o);
731 for(
const std::string&
w :
gui_->get_chat_manager().whisperers()) {
732 dictionary.insert(
w);
738 for(std::map<std::string, std::string>::const_iterator iter = friends.begin(); iter != friends.end(); ++iter) {
739 dictionary.insert((*iter).first);
749 ERR_DP <<
"unknown textbox mode";
779 return !
t.get_disallow_observers() && !
t.is_empty();
781 return t.is_local_human() && !
t.is_idle();
787 const int num_teams =
get_teams().size();
790 for(
int i = 0;
i < num_teams;
i++) {
832 if(event.key.keysym.sym == SDLK_ESCAPE) {
834 }
else if(event.key.keysym.sym == SDLK_TAB) {
836 }
else if(event.key.keysym.sym == SDLK_UP) {
838 }
else if(event.key.keysym.sym == SDLK_DOWN) {
840 }
else if(event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER) {
847 if(event.key.keysym.sym == SDLK_TAB) {
856 if(event.key.keysym.sym >=
'1' && event.key.keysym.sym <=
'9') {
857 const int new_path_turns = (
event.type == SDL_KEYDOWN) ? event.key.keysym.sym -
'1' : 0;
876 }
else if(event.key.keysym.sym == SDLK_TAB) {
878 if(!keys[SDLK_TAB]) {
894 assert(!
gamestate().events_manager_->is_event_running());
928 load.load_game_ingame();
955 const std::vector<std::string>& music_list = victory
956 ? (
gamestate_->get_game_data()->get_victory_music().empty()
958 :
gamestate_->get_game_data()->get_victory_music())
959 : (
gamestate_->get_game_data()->get_defeat_music().empty()
961 :
gamestate_->get_game_data()->get_defeat_music());
963 if(music_list.empty()) {
965 static const std::string empty_str =
"";
982 bool continue_level, found_player, found_network_player,
invalidate_all;
983 std::set<unsigned> not_defeated;
988 found_network_player,
995 gui_->invalidate_all();
1002 if(found_player || found_network_player) {
1010 DBG_EE <<
"found_player: " << found_player;
1011 DBG_EE <<
"found_network_player: " << found_network_player;
1020 for(
unsigned l : not_defeated) {
1024 LOG_AIT << l <<
" (using " <<
ai <<
") ";
1031 DBG_EE <<
"throwing end level exception...";
1049 std::stringstream message;
1050 message <<
_(
"The game is out of sync. It might not make much sense to continue. Do you want to save your game?");
1051 message <<
"\n\n" <<
_(
"Error details:") <<
"\n\n" <<
msg;
1060 gui_->set_team(team_index, observe);
1061 gui_->recalculate_minimap();
1062 gui_->invalidate_all();
1117 return !
t.is_local_human() || !
t.is_proxy_human();
1160 gui_->recalculate_minimap();
1171 gui_->recalculate_minimap();
1181 for(
const auto& endlevel : parent.
child_range(
"endlevel")) {
1182 if(endlevel.has_attribute(
"next_scenario")) {
1183 result.insert(endlevel[
"next_scenario"]);
1193 std::set<std::string> possible_next_scenarios;
1194 possible_next_scenarios.insert(
gamestate().gamedata_.next_scenario());
1205 bool possible_this_is_the_last_scenario =
false;
1206 std::vector<std::string> known;
1207 std::vector<std::string> unknown;
1208 for(
const auto& x : possible_next_scenarios) {
1209 if(x.empty() || x ==
"null") {
1210 possible_this_is_the_last_scenario =
true;
1211 LOG_NG <<
"This can be the last scenario";
1215 LOG_NG <<
"Variable value for next scenario '" << x <<
"'";
1218 LOG_NG <<
"Known next scenario '" << x <<
"'";
1220 unknown.push_back(x);
1221 ERR_NG <<
"Unknown next scenario '" << x <<
"'";
1225 if(unknown.empty()) {
1230 std::string title =
_(
"Warning: broken campaign branches");
1231 std::stringstream message;
1236 "The next scenario is missing, you will not be able to finish this campaign.",
1239 "Some of the possible next scenarios are missing, you might not be able to finish this campaign.",
1240 unknown.size() + known.size() + (possible_this_is_the_last_scenario ? 1 : 0));
1243 "Please report the following missing scenario to the campaign’s author:\n$unknown_list|",
1244 "Please report the following missing scenarios to the campaign’s author:\n$unknown_list|",
1247 message <<
_(
"Once this is fixed, you will need to restart this scenario.");
1249 std::stringstream unknown_list;
1250 for(
const auto& x : unknown) {
1254 symbols[
"unknown_list"] = unknown_list.str();
1269 std::set<std::string> res =
gui_->observers();
1272 res.insert(
t.current_player());
1315 LOG_NG <<
"firing time over event...";
1318 LOG_NG <<
"done firing time over event...";
1321 if(
gamestate().tod_manager_.is_time_left()) {
1326 LOG_AIT <<
"time over (draw)";
1336 e.proceed_to_next_level =
false;
1337 e.is_victory =
false;
1350 controller_.saved_game_.remove_snapshot();
1356 static const std::string no_objectives(
_(
"No objectives available"));
1359 t.reset_objectives_changed();
1366 if(skip_animation_button) {
Various functions that implement advancements of units.
Managing the AIs lifecycle - headers TODO: Refactor history handling and internal commands.
Class that keeps track of all the keys on the keyboard.
void new_side_turn(int side)
Performs some initializations and error checks when starting a new side-turn.
bool can_undo() const
True if there are actions that can be undone.
void redo()
Redoes the top action on the redo stack.
void clear()
Clears the stack of undoable (and redoable) actions.
bool can_redo() const
True if there are actions that can be redone.
void undo()
Undoes the top action on the undo stack.
std::string get_active_ai_identifier_for_side(side_number side)
Gets AI algorithm identifier for active AI of the given side.
static manager & get_singleton()
static void log_victory(std::set< unsigned int > teams)
A config object defines a single node in a WML file, with access to child nodes.
const_all_children_itors all_children_range() const
In-order iteration over all children.
child_itors child_range(config_key_type key)
void append_children(const config &cfg)
Adds children from cfg.
config & add_child(config_key_type key)
virtual void play_slice(bool is_delay_enabled=true)
const game_config_view & game_config_
int side_upkeep(int side_num) const
std::shared_ptr< gui::button > find_action_button(const std::string &id)
Retrieves a pointer to a theme UI button.
void do_ai_formula(const std::string &str, int side_num, mouse_handler &mousehandler)
gui::floating_textbox & get_textbox()
void do_command(const std::string &str)
void do_search(const std::string &new_search)
void set_gui(game_display *gui)
std::vector< std::string > get_commands_list()
void set_gui(game_display *gui)
const pathfind::paths & current_paths() const
void set_current_paths(const pathfind::paths &new_paths)
unit_map::iterator selected_unit()
map_location get_selected_hex() const
int get_path_turns() const
void select_hex(const map_location &hex, const bool browse, const bool highlight=true, const bool fire_event=true)
void set_side(int side_number)
void set_path_turns(const int path_turns)
void check_victory(bool &, bool &, bool &, bool &, std::set< unsigned > &, bool)
std::string get_tagname() const
optional_const_config find_child(config_key_type key, const std::string &name, const std::string &value) const
static game_config_view wrap(const config &cfg)
void set_phase(PHASE phase)
@ PRELOAD
the preload [event] is fired next phase: PRESTART (normal game), TURN_STARTING_WAITING (reloaded game...
@ GAME_ENDED
The game has ended and the user is observing the final state "lingering" The game can be saved here.
@ PRESTART
the prestart [event] is fired next phase: START (default), GAME_ENDING
@ TURN_PLAYING
The User is controlling the game and invoking actions The game can be saved here.
@ TURN_STARTING_WAITING
we are waiting for the turn to start.
@ START
the start [event] is fired next phase: TURN_STARTING_WAITING (default), GAME_ENDING
@ TURN_STARTING
the turn, side turn etc.
map_location last_selected
the last location where a select event fired.
config::attribute_value & get_variable(const std::string &varname)
throws invalid_variablename_exception if varname is no valid variable name.
pump_result_t fire(const std::string &event, const entity_location &loc1=entity_location::null_entity, const entity_location &loc2=entity_location::null_entity, const config &data=config())
Function to fire an event.
void write(config &cfg) const
bool in_phase(game_data::PHASE phase) const
void set_game_display(game_display *)
std::unique_ptr< game_lua_kernel > lua_kernel_
std::unique_ptr< pathfind::manager > pathfind_manager_
const std::unique_ptr< game_events::manager > events_manager_
static void progress(loading_stage stage=loading_stage::none)
Report what is being loaded to the loading screen.
static void raise()
Raise the loading screen to the top of the draw stack.
static void display(std::function< void()> f)
@ close_button
Shows a close button.
void memorize_command(const std::string &command)
const std::unique_ptr< gui::textbox > & box() const
TEXTBOX_MODE mode() const
void tab(const std::set< std::string > &dictionary)
const std::vector< std::string > & command_history() const
game_classification & get_classification()
const std::string & select_music(bool victory) const
void process_keydown_event(const SDL_Event &event) override
Process keydown (always).
bool ignore_replay_errors_
void maybe_throw_return_to_play_side() const
config to_config() const
Builds the snapshot config from members and their respective configs.
void init(const config &level)
std::vector< team > & get_teams()
std::unique_ptr< hotkey_handler > hotkey_handler_
bool have_keyboard_focus() override
Derived classes should override this to return false when arrow keys should not scroll the map,...
std::unique_ptr< game_state > gamestate_
void show_objectives() const
events::menu_handler menu_handler_
int find_viewing_side() const
returns 0 if no such team was found.
void fire_preload()
preload events cannot be synced
void check_victory()
Checks to see if a side has won.
bool is_linger_mode() const
bool is_team_visible(int team_num, bool observer) const
actions::undo_list & undo_stack()
statistics_t & statistics()
virtual soundsource::manager * get_soundsource_man() override
Get (optionally) a soundsources manager a derived class uses.
void check_next_scenario_is_known()
This shows a warning dialog if either [scenario]next_scenario or any [endlevel]next_scenario would le...
const unit_map & get_units() const
void set_end_level_data(const end_level_data &data)
events::mouse_handler & get_mouse_handler_base() override
Get a reference to a mouse handler member a derived class uses.
void update_savegame_snapshot() const
void set_do_healing(bool do_healing)
bool is_during_turn() const
bool can_use_synced_wml_menu() const
void reset_gamestate(const config &level, int replay_pos)
void do_consolesave(const std::string &filename)
void save_game_auto(const std::string &filename)
void textbox_move_vertically(bool up)
bool is_regular_game_end() const
play_controller(const config &level, saved_game &state_of_game, bool skip_replay, bool start_faded=false)
virtual void check_objectives()=0
bool remove_from_carryover_on_defeat_
void save_replay_auto(const std::string &filename)
hotkey::command_executor * get_hotkey_command_executor() override
Optionally get a command executor to handle context menu events.
bool enemies_visible() const
std::unique_ptr< replay > replay_
const mp_game_settings & get_mp_settings()
game_display & get_display() override
Get a reference to a display member a derived class uses.
virtual void update_viewing_player()=0
virtual void sync_end_turn()
bool victory_when_enemies_defeated_
std::unique_ptr< game_display > gui_
void toggle_skipping_replay()
void maybe_do_init_side()
Called by turn_info::process_network_data() or init_side() to call do_init_side() if necessary.
virtual void process_oos(const std::string &msg) const
Asks the user whether to continue on an OOS error.
bool is_browsing() const override
virtual void play_side_impl()
std::unique_ptr< soundsource::manager > soundsources_manager_
void do_init_side()
Called by replay handler or init_side() to do actual work for turn change.
std::string theme() const
int current_side() const
Returns the number of the side whose turn it is.
bool is_skipping_replay() const
bool did_tod_sound_this_turn_
std::shared_ptr< wb::manager > get_whiteboard() const
bool did_autosave_this_turn_
Whether we did init sides in this session (false = we did init sides before we reloaded the game).
bool player_type_changed_
true when the controller of the currently playing side has changed.
virtual bool should_return_to_play_side() const
std::set< std::string > all_players() 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.
events::mouse_handler mouse_handler_
std::unique_ptr< plugins_context > plugins_context_
virtual plugins_context * get_plugins_context() override
Get (optionally) a plugins context a derived class uses.
virtual ~play_controller()
void process_focus_keydown_event(const SDL_Event &event) override
Process keydown (only when the general map display does not have focus).
void refresh_objectives() const
Reevaluate [show_if] conditions and build a new objectives string.
game_events::wml_event_pump & pump()
void process_keyup_event(const SDL_Event &event) override
Process keyup (always).
void finish_side_turn_events()
std::shared_ptr< wb::manager > whiteboard_manager_
virtual void check_time_over()
t_string get_scenario_name() const
Implements a quit confirmation dialog.
static rng & default_instance()
int get_random_int(int min, int max)
bool add_start_if_not_there_yet()
config * get_next_action()
Exception used to escape form the ai or ui code to playsingle_controller::play_side.
game_classification & classification()
mp_game_settings & mp_settings()
Multiplayer parameters for this game.
replay_recorder_base & get_replay()
config & set_snapshot(config snapshot)
void autosave(const bool disable_autosave, const int autosave_max, const int infinite_autosaves)
Class for "normal" midgame saves.
The class for loading a savefile.
Class for replay saves (either manually or automatically).
static std::shared_ptr< save_index_class > default_saves_dir()
Returns an instance for managing saves in filesystem::get_saves_dir()
bool save_game_automatic(bool ask_for_overwrite=false, const std::string &filename="")
Saves a game without user interaction, unless the file exists and it should be asked to overwrite it.
bool save_game_interactive(const std::string &message, DIALOG_TYPE dialog_type)
Save a game interactively through the savegame dialog.
A RAII object to enter the synced context, cannot be called if we are already in a synced context.
void do_final_checkup(bool dont_throw=false)
void reset_turn_stats(const std::string &save_id)
This class stores all the data for a single 'side' (in game nomenclature).
int support() const
Calculate total support capacity, based on support_per_village.
bool is_local_human() const
void spend_gold(const int amount)
const time_of_day & get_time_of_day(int for_turn=0) const
Returns global time of day for the passed turn.
bool next_turn(game_data *vars)
Function to move to the next turn.
void remove_scenario_fixes()
void apply_scenario_fix(const config &cfg)
This class represents a single unit of a specific type.
A variable-expanding proxy for the config class.
This class is the frontend of the whiteboard framework for the rest of the Wesnoth code.
Various functions related to the creation of units (recruits, recalls, and placed units).
void throw_quit_game_exception()
static std::string _n(const char *str1, const char *str2, int n)
static std::string _(const char *str)
void calculate_healing(int side, bool update_display)
Calculates healing for all units for the given side.
Various functions that implement healing of units (when a side turn starts).
This file implements all the hotkey handling and menu details for play controller.
Standard logging facilities (interface).
#define log_scope(description)
constexpr T modulo(T num, int mod, T min=0)
bool clear_shroud(int side, bool reset_fog, bool fire_events)
Function that will clear shroud (and fog) based on current unit positions.
void recalculate_fog(int side)
Function that recalculates the fog of war.
A small explanation about what's going on here: Each action has access to two game_info objects First...
void invalidate_all()
Mark the entire screen as requiring redraw.
Handling of system events.
Collection of helper functions relating to Pango formatting.
const std::string unicode_bullet
std::vector< std::string > default_defeat_music
bool ignore_replay_errors
static void add_color_info(const game_config_view &v, bool build_defaults)
std::vector< std::string > default_victory_music
std::vector< game_tip > load(const config &cfg)
Loads the tips from a config.
void show_transient_message(const std::string &title, const std::string &message, const std::string &image, const bool message_use_markup, const bool title_use_markup)
Shows a transient message to the user.
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
void show_message(const std::string &title, const std::string &msg, const std::string &button_caption, const bool auto_close, const bool message_use_markup, const bool title_use_markup)
Shows a message to the user.
Keyboard shortcuts for game actions.
constexpr uint32_t scope_game
compression::format save_compression_format()
void encounter_all_content(const game_board &gameboard_)
std::map< std::string, std::string > get_acquaintances_nice(const std::string &filter)
const int INFINITE_AUTO_SAVES
::tod_manager * tod_manager
persist_manager * persist
game_events::manager * game_events
actions::undo_list * undo_stack
soundsource::manager * soundsources
game_lua_kernel * lua_kernel
game_classification * classification
pathfind::manager * tunnels
play_controller * controller
filter_context * filter_con
std::shared_ptr< wb::manager > whiteboard
void write_music_play_list(config &snapshot)
void play_sound(const std::string &files, channel_group group, unsigned int repeats)
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
std::string interpolate_variables_into_string(const std::string &str, const string_map *const symbols)
Function which will interpolate variables, starting with '$' in the string 'str' with the equivalent ...
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
std::string get_unknown_exception_type()
Utility function for finding the type of thing caught with catch(...).
std::map< std::string, t_string > string_map
bool headless()
The game is running headless.
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
static void clear_resources()
static lg::log_domain log_engine("engine")
static lg::log_domain log_aitesting("ai/testing")
static void find_next_scenarios(const config &parent, std::set< std::string > &result)
Find all [endlevel]next_scenario= attributes, and add them to result.
static void copy_persistent(const config &src, config &dst)
Copies [scenario] attributes/tags that are not otherwise stored in C++ structs/clases.
static lg::log_domain log_enginerefac("enginerefac")
static lg::log_domain log_display("display")
static lg::log_domain log_engine_enemies("engine/enemies")
Define the game's event mechanism.
Additional information on the game outcome which can be provided by WML.
bool proceed_to_next_level
whether to proceed to the next scenario, equals is_victory in sp.
Error used for any general game error, e.g.
Encapsulates the map of the game.
static const map_location & null_location()
int mp_countdown_init_time
Object which contains all the possible locations a unit can move to, with associated best routes to t...
~scoped_savegame_snapshot()
scoped_savegame_snapshot(const play_controller &controller)
const play_controller & controller_
Object which defines a time of day with associated bonuses, image, sounds etc.
std::string sounds
List of "ambient" sounds associated with this time_of_day, Played at the beginning of turn.
Object which temporarily resets a unit's movement.
Gather statistics important for AI testing and output them.
unit_type_data unit_types
Various functions that implement the undoing (and redoing) of in-game commands.
Various functions implementing vision (through fog of war and shroud).