From 4406cdfae0788533b31b4bf0667cf532d85f2060 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Thu, 12 Jun 2014 11:52:52 -0400 Subject: [PATCH 1/9] add accessors for selected, moused-over hex to class display --- src/display.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/display.hpp b/src/display.hpp index dcb08e8c8a9c..e4fc7929a6a2 100644 --- a/src/display.hpp +++ b/src/display.hpp @@ -383,6 +383,9 @@ class display void refresh_report(std::string const &report_name, const config * new_cfg=NULL); + map_location get_selected_hex() const { return selectedHex_; } + map_location get_highlight_hex() const { return mouseoverHex_; } + void draw_minimap_units(); /** Function to invalidate all tiles. */ From 5145c9422c2dd26efd9f532f88f99730801c6019 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Thu, 12 Jun 2014 12:44:51 -0400 Subject: [PATCH 2/9] display takes a pointer to whiteboard manager It uses a shared_ptr, to prevent crashes during play_controller initialization. But it only holds a weak_ptr, to preserve previous behavior of skipping whiteboard ops when the ptr is NULL. It requires also for us to construct the whiteboard before constructing the gui, so that we have a valid pointer to pass. Most likely all of the managers will have to be moved forward, in this commit we move forward the whiteboard and pathfinding module, anticipating future commits. --- src/display.cpp | 7 +++--- src/display.hpp | 8 +++++- src/editor/editor_display.cpp | 8 +++++- src/game_display.cpp | 42 +++++++++++++++++--------------- src/game_display.hpp | 1 + src/play_controller.cpp | 12 +++++---- src/play_controller.hpp | 2 +- src/tests/utils/fake_display.cpp | 6 ++++- 8 files changed, 55 insertions(+), 31 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index 6616edf5af91..9538b6ab6122 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -136,8 +136,9 @@ void display::remove_single_overlay(const map_location& loc, const std::string& -display::display(const display_context * dc, CVideo& video, const config& theme_cfg, const config& level) : +display::display(const display_context * dc, CVideo& video, boost::weak_ptr wb, const config& theme_cfg, const config& level) : dc_(dc), + wb_(wb), exclusive_unit_draw_requests_(), screen_(video), currentTeam_(0), @@ -364,8 +365,8 @@ void display::set_team(size_t teamindex, bool show_everything) viewpoint_ = NULL; } labels().recalculate_labels(); - if(resources::whiteboard) - resources::whiteboard->on_viewer_change(teamindex); + if(boost::shared_ptr w = wb_.lock()) + w->on_viewer_change(teamindex); } void display::set_playing_team(size_t teamindex) diff --git a/src/display.hpp b/src/display.hpp index e4fc7929a6a2..af89f9789e76 100644 --- a/src/display.hpp +++ b/src/display.hpp @@ -39,6 +39,10 @@ struct time_of_day; class map_labels; class arrow; +namespace wb { + class manager; +} + #include "display_context.hpp" #include "font.hpp" #include "key.hpp" @@ -55,6 +59,7 @@ class arrow; #include #include +#include #include #include @@ -63,7 +68,7 @@ class gamemap; class display { public: - display(const display_context * dc, CVideo& video, + display(const display_context * dc, CVideo& video, boost::weak_ptr wb, const config& theme_cfg, const config& level); virtual ~display(); static display* get_singleton() { return singleton_ ;} @@ -630,6 +635,7 @@ class display protected: //TODO sort const display_context * dc_; + boost::weak_ptr wb_; typedef std::map exclusive_unit_draw_requests_t; /// map of hexes where only one unit should be drawn, the one identified by the associated id string diff --git a/src/editor/editor_display.cpp b/src/editor/editor_display.cpp index ca72cb8f3d59..eaa2169da492 100644 --- a/src/editor/editor_display.cpp +++ b/src/editor/editor_display.cpp @@ -17,6 +17,12 @@ #include "reports.hpp" #include "terrain_builder.hpp" +#include + +namespace wb { + class manager; +} + namespace editor { // Define dummy display context; @@ -47,7 +53,7 @@ const display_context * get_dummy_display_context() { editor_display::editor_display(const display_context * dc, CVideo& video, const config& theme_cfg, const config& level) - : display(dc, video, theme_cfg, level) + : display(dc, video, boost::shared_ptr(), theme_cfg, level) , brush_locations_() , palette_report_() { diff --git a/src/game_display.cpp b/src/game_display.cpp index 3e2614dc45da..ea5be1ac64a4 100644 --- a/src/game_display.cpp +++ b/src/game_display.cpp @@ -61,10 +61,10 @@ static lg::log_domain log_engine("engine"); std::map game_display::debugHighlights_; -game_display::game_display(game_board& board, CVideo& video, +game_display::game_display(game_board& board, CVideo& video, boost::weak_ptr wb, const tod_manager& tod, const config& theme_cfg, const config& level) : - display(&board, video, theme_cfg, level), + display(&board, video, wb, theme_cfg, level), overlay_map_(), fake_units_(), attack_indicator_src_(), @@ -90,7 +90,7 @@ game_display* game_display::create_dummy_display(CVideo& video) static config dummy_cfg2; static game_board dummy_board(dummy_cfg, dummy_cfg2); static tod_manager dummy_tod(dummy_cfg); - return new game_display(dummy_board, video, dummy_tod, + return new game_display(dummy_board, video, boost::shared_ptr(), dummy_tod, dummy_cfg, dummy_cfg); } @@ -222,8 +222,8 @@ void game_display::scroll_to_leader(int side, SCROLL_TYPE scroll_type,bool force } void game_display::pre_draw() { - if (resources::whiteboard) { - resources::whiteboard->pre_draw(); + if (boost::shared_ptr w = wb_.lock()) { + w->pre_draw(); } process_reachmap_changes(); /** @@ -235,8 +235,8 @@ void game_display::pre_draw() { void game_display::post_draw() { - if (resources::whiteboard) { - resources::whiteboard->post_draw(); + if (boost::shared_ptr w = wb_.lock()) { + w->post_draw(); } } @@ -318,14 +318,16 @@ void game_display::draw_hex(const map_location& loc) image::get_image(unreachable,image::SCALED_TO_HEX)); } - resources::whiteboard->draw_hex(loc); + if (boost::shared_ptr w = wb_.lock()) { + w->draw_hex(loc); - if (!(resources::whiteboard->is_active() && resources::whiteboard->has_temp_move())) - { - // Footsteps indicating a movement path - const std::vector& footstepImages = footsteps_images(loc); - if (!footstepImages.empty()) { - drawing_buffer_add(LAYER_FOOTSTEPS, loc, xpos, ypos, footstepImages); + if (!(w->is_active() && w->has_temp_move())) + { + // Footsteps indicating a movement path + const std::vector& footstepImages = footsteps_images(loc); + if (!footstepImages.empty()) { + drawing_buffer_add(LAYER_FOOTSTEPS, loc, xpos, ypos, footstepImages); + } } } // Draw the attack direction indicator @@ -411,12 +413,14 @@ void game_display::draw_movement_info(const map_location& loc) // Search if there is a mark here pathfind::marked_route::mark_map::iterator w = route_.marks.find(loc); + boost::shared_ptr wb = wb_.lock(); + // Don't use empty route or the first step (the unit will be there) if(w != route_.marks.end() && !route_.steps.empty() && route_.steps.front() != loc) { const unit_map::const_iterator un = - resources::whiteboard->get_temp_move_unit().valid() ? - resources::whiteboard->get_temp_move_unit() : dc_->units().find(route_.steps.front()); + (wb && wb->get_temp_move_unit().valid()) ? + wb->get_temp_move_unit() : dc_->units().find(route_.steps.front()); if(un != dc_->units().end()) { // Display the def% of this terrain int def = 100 - un->defense_modifier(get_map().get_terrain(loc)); @@ -432,7 +436,7 @@ void game_display::draw_movement_info(const map_location& loc) int xpos = get_location_x(loc); int ypos = get_location_y(loc); - if (w->second.invisible) { + if (w->second.invisible) { drawing_buffer_add(LAYER_MOVE_INFO, loc, xpos, ypos, image::get_image("misc/hidden.png", image::SCALED_TO_HEX)); } @@ -1002,8 +1006,8 @@ void game_display::set_team(size_t teamindex, bool show_everything) viewpoint_ = NULL; } labels().recalculate_labels(); - if(resources::whiteboard) - resources::whiteboard->on_viewer_change(teamindex); + if(boost::shared_ptr w = wb_.lock()) + w->on_viewer_change(teamindex); } void game_display::set_playing_team(size_t teamindex) diff --git a/src/game_display.hpp b/src/game_display.hpp index df427a1d6e38..bc8a5b4d386e 100644 --- a/src/game_display.hpp +++ b/src/game_display.hpp @@ -38,6 +38,7 @@ class game_display : public display { public: game_display(game_board& board, CVideo& video, + boost::weak_ptr wb, const tod_manager& tod_manager, const config& theme_cfg, const config& level); diff --git a/src/play_controller.cpp b/src/play_controller.cpp index b3b084e7b06c..303597ee88e4 100644 --- a/src/play_controller.cpp +++ b/src/play_controller.cpp @@ -255,9 +255,15 @@ void play_controller::init(CVideo& video){ loadscreen::start_stage("init theme"); const config &theme_cfg = get_theme(game_config_, level_["theme"]); + LOG_NG << "initializing pathfinding and whiteboard..." << (SDL_GetTicks() - ticks_) << std::endl; + pathfind_manager_.reset(new pathfind::manager(level_)); + whiteboard_manager_.reset(new wb::manager()); + resources::tunnels = pathfind_manager_.get(); + resources::whiteboard = whiteboard_manager_.get(); + LOG_NG << "building terrain rules... " << (SDL_GetTicks() - ticks_) << std::endl; loadscreen::start_stage("build terrain"); - gui_.reset(new game_display(gameboard_, video, tod_manager_, theme_cfg, level_)); + gui_.reset(new game_display(gameboard_, video, whiteboard_manager_, tod_manager_, theme_cfg, level_)); if (!gui_->video().faked()) { if (gamestate_.mp_settings().mp_countdown) gui_->get_theme().modify_label("time-icon", _ ("time left for current turn")); @@ -316,12 +322,8 @@ void play_controller::init_managers(){ prefs_disp_manager_.reset(new preferences::display_manager(gui_.get())); tooltips_manager_.reset(new tooltips::manager(gui_->video())); soundsources_manager_.reset(new soundsource::manager(*gui_)); - pathfind_manager_.reset(new pathfind::manager(level_)); - whiteboard_manager_.reset(new wb::manager()); resources::soundsources = soundsources_manager_.get(); - resources::tunnels = pathfind_manager_.get(); - resources::whiteboard = whiteboard_manager_.get(); halo_manager_.reset(new halo::manager(*gui_)); LOG_NG << "done initializing managers... " << (SDL_GetTicks() - ticks_) << std::endl; diff --git a/src/play_controller.hpp b/src/play_controller.hpp index 7fa223df5977..aa6aa0e9b2d1 100644 --- a/src/play_controller.hpp +++ b/src/play_controller.hpp @@ -247,7 +247,7 @@ class play_controller : public controller_base, public events::observer, public boost::scoped_ptr undo_stack_; //whiteboard manager - boost::scoped_ptr whiteboard_manager_; + boost::shared_ptr whiteboard_manager_; const unit_type::experience_accelerator xp_mod_; //if a team is specified whose turn it is, it means we're loading a game diff --git a/src/tests/utils/fake_display.cpp b/src/tests/utils/fake_display.cpp index fd273cb71326..c36b8979c28a 100644 --- a/src/tests/utils/fake_display.cpp +++ b/src/tests/utils/fake_display.cpp @@ -20,6 +20,10 @@ #include "game_display.hpp" #include "tod_manager.hpp" +namespace wb { + class manager; +} + namespace test_utils { class fake_display_manager { @@ -61,7 +65,7 @@ namespace test_utils { dummy_board_(dummy_cfg_, dummy_cfg2_), dummy_tod_(dummy_cfg_), main_event_context_(), - disp_(dummy_board_, video_, dummy_tod_, + disp_(dummy_board_, video_, boost::shared_ptr (), dummy_tod_, dummy_cfg_, dummy_cfg_) { } From 7078dd34e61b18287338e9545575f9cf2dad5b93 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Thu, 12 Jun 2014 12:58:10 -0400 Subject: [PATCH 3/9] purge a useless "report()" function in reports.cpp --- src/reports.cpp | 77 +++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 41 deletions(-) diff --git a/src/reports.cpp b/src/reports.cpp index c3b00abf8f79..a6dc7604cc82 100644 --- a/src/reports.cpp +++ b/src/reports.cpp @@ -60,11 +60,6 @@ static void add_image(config &report, const std::string &image, if (!help.empty()) element["help"] = help; } -static config report() -{ - return config(); -} - static config text_report(const std::string &text, const std::string &tooltip = "", const std::string &help = "") { @@ -151,7 +146,7 @@ static config gray_inactive(const std::string &str) static config unit_name(const unit *u) { if (!u) { - return report(); + return config(); } /* @@ -178,7 +173,7 @@ REPORT_GENERATOR(selected_unit_name) static config unit_type(const unit* u) { - if (!u) return report(); + if (!u) return config(); std::string has_variations_prefix = (!u->type().variations().empty() ? ".." : ""); std::ostringstream str, tooltip; str << u->type_name(); @@ -199,7 +194,7 @@ REPORT_GENERATOR(selected_unit_type) static config unit_race(const unit* u) { - if (!u) return report(); + if (!u) return config(); std::ostringstream str, tooltip; str << u->race()->name(u->gender()); tooltip << _("Race: ") << "" << u->race()->name(u->gender()) << ""; @@ -218,7 +213,7 @@ REPORT_GENERATOR(selected_unit_race) static config unit_side(const unit* u) { - if (!u) return report(); + if (!u) return config(); config report; const team &u_team = resources::disp_context->teams()[u->side() - 1]; @@ -249,7 +244,7 @@ REPORT_GENERATOR(selected_unit_side) static config unit_level(const unit* u) { - if (!u) return report(); + if (!u) return config(); std::ostringstream str, tooltip; str << u->level(); tooltip << _("Level: ") << "" << u->level() << "\n"; @@ -275,7 +270,7 @@ REPORT_GENERATOR(selected_unit_level) REPORT_GENERATOR(unit_amla) { const unit *u = reports::get_visible_unit(); - if (!u) return report(); + if (!u) return config(); config res; typedef std::pair pair_string; BOOST_FOREACH(const pair_string &ps, u->amla_icons()) { @@ -286,7 +281,7 @@ REPORT_GENERATOR(unit_amla) static config unit_traits(const unit* u) { - if (!u) return report(); + if (!u) return config(); config res; const std::vector &traits = u->trait_names(); const std::vector &descriptions = u->trait_descriptions(); @@ -316,7 +311,7 @@ REPORT_GENERATOR(selected_unit_traits) static config unit_status(const unit* u) { - if (!u) return report(); + if (!u) return config(); config res; map_location displayed_unit_hex = resources::screen->displayed_unit_hex(); if (resources::disp_context->map().on_board(displayed_unit_hex) && u->invisible(displayed_unit_hex)) { @@ -350,7 +345,7 @@ REPORT_GENERATOR(selected_unit_status) static config unit_alignment(const unit* u) { - if (!u) return report(); + if (!u) return config(); std::ostringstream str, tooltip; const std::string align = unit_type::alignment_description(u->alignment(), u->gender()); const std::string align_id = lexical_cast(u->alignment()); @@ -383,7 +378,7 @@ REPORT_GENERATOR(selected_unit_alignment) static config unit_abilities(const unit* u) { - if (!u) return report(); + if (!u) return config(); config res; std::vector active; @@ -428,7 +423,7 @@ REPORT_GENERATOR(selected_unit_abilities) static config unit_hp(const unit* u) { - if (!u) return report(); + if (!u) return config(); std::ostringstream str, tooltip; str << span_color(u->hp_color()) << u->hitpoints() << '/' << u->max_hitpoints() << naps; @@ -481,7 +476,7 @@ REPORT_GENERATOR(selected_unit_hp) static config unit_xp(const unit* u) { - if (!u) return report(); + if (!u) return config(); std::ostringstream str, tooltip; str << span_color(u->xp_color()) << u->experience() << '/' << u->max_experience() << naps; @@ -503,7 +498,7 @@ REPORT_GENERATOR(selected_unit_xp) static config unit_advancement_options(const unit* u) { - if (!u) return report(); + if (!u) return config(); config res; typedef std::pair pair_string; BOOST_FOREACH(const pair_string &ps, u->advancement_icons()) { @@ -525,13 +520,13 @@ REPORT_GENERATOR(selected_unit_advancement_options) static config unit_defense(const unit* u, const map_location& displayed_unit_hex) { if(!u) { - return report(); + return config(); } std::ostringstream str, tooltip; const gamemap &map = resources::disp_context->map(); if(!resources::disp_context->map().on_board(displayed_unit_hex)) { - return report(); + return config(); } const t_translation::t_terrain &terrain = map[displayed_unit_hex]; @@ -578,7 +573,7 @@ REPORT_GENERATOR(selected_unit_defense) static config unit_vision(const unit* u) { - if (!u) return report(); + if (!u) return config(); // TODO std::ostringstream str; @@ -599,7 +594,7 @@ REPORT_GENERATOR(selected_unit_vision) static config unit_moves(const unit* u) { - if (!u) return report(); + if (!u) return config(); std::ostringstream str, tooltip; double movement_frac = 1.0; if (u->side() == resources::screen->playing_side()) { @@ -871,7 +866,7 @@ static void format_hp(char str_buf[10], int hp) static config unit_weapons(const unit *attacker, const map_location &attacker_pos, const unit *defender, bool show_attacker) { - if (!attacker || !defender) return report(); + if (!attacker || !defender) return config(); const unit* u = show_attacker ? attacker : defender; const map_location unit_loc = show_attacker ? attacker_pos : defender->get_location(); @@ -997,7 +992,7 @@ static config unit_weapons(const unit *attacker, const map_location &attacker_po static config unit_weapons(const unit *u) { - if (!u || u->attacks().empty()) return report(); + if (!u || u->attacks().empty()) return config(); map_location displayed_unit_hex = resources::screen->displayed_unit_hex(); config res; @@ -1059,26 +1054,26 @@ REPORT_GENERATOR(selected_unit_weapons) REPORT_GENERATOR(unit_image) { const unit *u = reports::get_visible_unit(); - if (!u) return report(); + if (!u) return config(); return image_report(u->absolute_image() + u->image_mods()); } REPORT_GENERATOR(selected_unit_image) { const unit *u = reports::get_selected_unit(); - if (!u) return report(); + if (!u) return config(); return image_report(u->absolute_image() + u->image_mods()); } REPORT_GENERATOR(selected_unit_profile) { const unit *u = reports::get_selected_unit(); - if (!u) return report(); + if (!u) return config(); return image_report(u->small_profile()); } REPORT_GENERATOR(unit_profile) { const unit *u = reports::get_visible_unit(); - if (!u) return report(); + if (!u) return config(); return image_report(u->small_profile()); } @@ -1204,7 +1199,7 @@ static config unit_box_at(const map_location& mouseover_hex) t_translation::t_terrain terrain = map.get_terrain(mouseover_hex); //if (terrain == t_translation::OFF_MAP_USER) - // return report(); + // return config(); //if (map.is_keep(mouseover_hex)) { // add_image(cfg, "icons/terrain/terrain_type_keep.png", ""); @@ -1359,11 +1354,11 @@ REPORT_GENERATOR(terrain_info) mouseover_hex = display::get_singleton()->selected_hex(); if (!map.on_board(mouseover_hex)) - return report(); + return config(); t_translation::t_terrain terrain = map.get_terrain(mouseover_hex); if (terrain == t_translation::OFF_MAP_USER) - return report(); + return config(); std::ostringstream str; config cfg; @@ -1403,11 +1398,11 @@ REPORT_GENERATOR(terrain) const team &viewing_team = resources::disp_context->teams()[viewing_side - 1]; map_location mouseover_hex = resources::screen->mouseover_hex(); if (!map.on_board(mouseover_hex) || viewing_team.shrouded(mouseover_hex)) - return report(); + return config(); t_translation::t_terrain terrain = map.get_terrain(mouseover_hex); if (terrain == t_translation::OFF_MAP_USER) - return report(); + return config(); std::ostringstream str; if (map.is_village(mouseover_hex)) @@ -1454,7 +1449,7 @@ REPORT_GENERATOR(position) if (!map.on_board(mouseover_hex)) { if (!map.on_board(selected_hex)) - return report(); + return config(); else { mouseover_hex = selected_hex; } @@ -1462,7 +1457,7 @@ REPORT_GENERATOR(position) t_translation::t_terrain terrain = map[mouseover_hex]; if (terrain == t_translation::OFF_MAP_USER) - return report(); + return config(); std::ostringstream str; str << mouseover_hex; @@ -1503,7 +1498,7 @@ REPORT_GENERATOR(observers) { const std::set &observers = resources::screen->observers(); if (observers.empty()) - return report(); + return config(); std::ostringstream str; str << _("Observers:") << '\n'; @@ -1518,7 +1513,7 @@ REPORT_GENERATOR(selected_terrain) { const std::string selected_terrain = editor::get_selected_terrain(); if (selected_terrain.empty()) - return report(); + return config(); else return text_report(selected_terrain); } @@ -1529,7 +1524,7 @@ REPORT_GENERATOR(edit_left_button_function) { const std::string left_button_function = editor::get_left_button_function(); if (left_button_function.empty()) - return report(); + return config(); else return text_report(left_button_function); } @@ -1539,12 +1534,12 @@ REPORT_GENERATOR(report_clock) { time_t t = std::time(NULL); struct tm *lt = std::localtime(&t); - if (!lt) return report(); + if (!lt) return config(); char temp[15]; size_t s = util::strftime(temp, 15, (preferences::use_twelve_hour_clock_format() ? _("%I:%M %p") : _("%H:%M")), lt); - return s ? text_report(temp) : report(); + return s ? text_report(temp) : config(); } @@ -1605,7 +1600,7 @@ config reports::generate_report(const std::string &name, bool only_static) static_report_generators::const_iterator j = static_generators.find(name); if (j != static_generators.end()) return j->second(); - return report(); + return config(); } const std::set &reports::report_list() From 8faa4bf059f75128e922696905f151d1ba7f7d48 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Thu, 12 Jun 2014 14:38:45 -0400 Subject: [PATCH 4/9] refactor reports to take display_context as an argument --- src/display.cpp | 2 +- src/reports.cpp | 337 +++++++++++++++++++++--------------------- src/reports.hpp | 5 +- src/scripting/lua.cpp | 6 +- 4 files changed, 175 insertions(+), 175 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index 9538b6ab6122..610a0a021f9f 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -2734,7 +2734,7 @@ void display::refresh_report(std::string const &report_name, const config * new_ } // Now we will need the config. Generate one if needed. - const config generated_cfg = new_cfg ? config() : reports::generate_report(report_name); + const config generated_cfg = new_cfg ? config() : reports::generate_report(report_name, dc_); if ( new_cfg == NULL ) new_cfg = &generated_cfg; diff --git a/src/reports.cpp b/src/reports.cpp index a6dc7604cc82..6514b68b2013 100644 --- a/src/reports.cpp +++ b/src/reports.cpp @@ -16,7 +16,6 @@ #include "actions/attack.hpp" #include "attack_prediction.hpp" -#include "display_context.hpp" //#include "editor/editor_controller.hpp" //#include "editor/palette/terrain_palettes.hpp" #include "font.hpp" @@ -93,7 +92,7 @@ static std::string flush(std::ostringstream &s) return r; } -typedef config (*generator_function)(); +typedef config (*generator_function)(const display_context *); typedef std::map static_report_generators; typedef std::map dynamic_report_generators; @@ -108,26 +107,26 @@ struct report_generator_helper } }; -#define REPORT_GENERATOR(n) \ - static config report_##n(); \ +#define REPORT_GENERATOR(n, dcn) \ + static config report_##n(const display_context * dcn); \ static report_generator_helper reg_gen_##n(#n, &report_##n); \ - static config report_##n() + static config report_##n(const display_context * dcn) static char const *naps = ""; namespace reports { -static const unit *get_visible_unit() +static const unit *get_visible_unit(const display_context * dc) { - return resources::disp_context->get_visible_unit(resources::screen->displayed_unit_hex(), - resources::disp_context->teams()[resources::screen->viewing_team()], + return dc->get_visible_unit(resources::screen->displayed_unit_hex(), + dc->teams()[resources::screen->viewing_team()], resources::screen->show_everything()); } -static const unit *get_selected_unit() +static const unit *get_selected_unit(const display_context * dc) { - return resources::disp_context->get_visible_unit(resources::screen->selected_hex(), - resources::disp_context->teams()[resources::screen->viewing_team()], + return dc->get_visible_unit(resources::screen->selected_hex(), + dc->teams()[resources::screen->viewing_team()], resources::screen->show_everything()); } @@ -160,14 +159,14 @@ static config unit_name(const unit *u) return text_report(str.str(), tooltip.str()); } -REPORT_GENERATOR(unit_name) +REPORT_GENERATOR(unit_name, dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); return unit_name(u); } -REPORT_GENERATOR(selected_unit_name) +REPORT_GENERATOR(selected_unit_name, dc) { - const unit *u = reports::get_selected_unit(); + const unit *u = reports::get_selected_unit(dc); return unit_name(u); } @@ -181,14 +180,14 @@ static config unit_type(const unit* u) << u->unit_description(); return text_report(str.str(), tooltip.str(), has_variations_prefix + "unit_" + u->type_id()); } -REPORT_GENERATOR(unit_type) +REPORT_GENERATOR(unit_type, dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); return unit_type(u); } -REPORT_GENERATOR(selected_unit_type) +REPORT_GENERATOR(selected_unit_type, dc) { - const unit *u = reports::get_selected_unit(); + const unit *u = reports::get_selected_unit(dc); return unit_type(u); } @@ -200,23 +199,23 @@ static config unit_race(const unit* u) tooltip << _("Race: ") << "" << u->race()->name(u->gender()) << ""; return text_report(str.str(), tooltip.str(), "..race_" + u->race()->id()); } -REPORT_GENERATOR(unit_race) +REPORT_GENERATOR(unit_race, dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); return unit_race(u); } -REPORT_GENERATOR(selected_unit_race) +REPORT_GENERATOR(selected_unit_race, dc) { - const unit *u = reports::get_selected_unit(); + const unit *u = reports::get_selected_unit(dc); return unit_race(u); } -static config unit_side(const unit* u) +static config unit_side(const display_context * dc, const unit* u) { if (!u) return config(); config report; - const team &u_team = resources::disp_context->teams()[u->side() - 1]; + const team &u_team = dc->teams()[u->side() - 1]; std::string flag_icon = u_team.flag_icon(); std::string old_rgb = game_config::flag_rgb; std::string new_rgb = team::get_side_color_index(u->side()); @@ -231,15 +230,15 @@ static config unit_side(const unit* u) add_text(report, text.str(), "", ""); return report; } -REPORT_GENERATOR(unit_side) +REPORT_GENERATOR(unit_side, dc) { - const unit *u = reports::get_visible_unit(); - return unit_side(u); + const unit *u = reports::get_visible_unit(dc); + return unit_side(dc,u); } -REPORT_GENERATOR(selected_unit_side) +REPORT_GENERATOR(selected_unit_side, dc) { - const unit *u = reports::get_selected_unit(); - return unit_side(u); + const unit *u = reports::get_selected_unit(dc); + return unit_side(dc, u); } static config unit_level(const unit* u) @@ -256,20 +255,20 @@ static config unit_level(const unit* u) << utils::join(adv_to, "\n\t") << ""; return text_report(str.str(), tooltip.str()); } -REPORT_GENERATOR(unit_level) +REPORT_GENERATOR(unit_level, dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); return unit_level(u); } -REPORT_GENERATOR(selected_unit_level) +REPORT_GENERATOR(selected_unit_level, dc) { - const unit *u = reports::get_selected_unit(); + const unit *u = reports::get_selected_unit(dc); return unit_level(u); } -REPORT_GENERATOR(unit_amla) +REPORT_GENERATOR(unit_amla, dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); if (!u) return config(); config res; typedef std::pair pair_string; @@ -298,23 +297,23 @@ static config unit_traits(const unit* u) } return res; } -REPORT_GENERATOR(unit_traits) +REPORT_GENERATOR(unit_traits, dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); return unit_traits(u); } -REPORT_GENERATOR(selected_unit_traits) +REPORT_GENERATOR(selected_unit_traits, dc) { - const unit *u = reports::get_selected_unit(); + const unit *u = reports::get_selected_unit(dc); return unit_traits(u); } -static config unit_status(const unit* u) +static config unit_status(const display_context * dc, const unit* u) { if (!u) return config(); config res; map_location displayed_unit_hex = resources::screen->displayed_unit_hex(); - if (resources::disp_context->map().on_board(displayed_unit_hex) && u->invisible(displayed_unit_hex)) { + if (dc->map().on_board(displayed_unit_hex) && u->invisible(displayed_unit_hex)) { add_status(res, "misc/invisible.png", N_("invisible: "), N_("This unit is invisible. It cannot be seen or attacked by enemy units.")); } @@ -332,15 +331,15 @@ static config unit_status(const unit* u) } return res; } -REPORT_GENERATOR(unit_status) +REPORT_GENERATOR(unit_status,dc) { - const unit *u = reports::get_visible_unit(); - return unit_status(u); + const unit *u = reports::get_visible_unit(dc); + return unit_status(dc,u); } -REPORT_GENERATOR(selected_unit_status) +REPORT_GENERATOR(selected_unit_status, dc) { - const unit *u = reports::get_selected_unit(); - return unit_status(u); + const unit *u = reports::get_selected_unit(dc); + return unit_status(dc, u); } static config unit_alignment(const unit* u) @@ -364,14 +363,14 @@ static config unit_alignment(const unit* u) return text_report(str.str(), tooltip.str(), "time_of_day"); } -REPORT_GENERATOR(unit_alignment) +REPORT_GENERATOR(unit_alignment, dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); return unit_alignment(u); } -REPORT_GENERATOR(selected_unit_alignment) +REPORT_GENERATOR(selected_unit_alignment, dc) { - const unit *u = reports::get_selected_unit(); + const unit *u = reports::get_selected_unit(dc); return unit_alignment(u); } @@ -409,14 +408,14 @@ static config unit_abilities(const unit* u) } return res; } -REPORT_GENERATOR(unit_abilities) +REPORT_GENERATOR(unit_abilities, dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); return unit_abilities(u); } -REPORT_GENERATOR(selected_unit_abilities) +REPORT_GENERATOR(selected_unit_abilities, dc) { - const unit *u = reports::get_selected_unit(); + const unit *u = reports::get_selected_unit(dc); return unit_abilities(u); } @@ -463,14 +462,14 @@ static config unit_hp(const unit* u) } return text_report(str.str(), tooltip.str()); } -REPORT_GENERATOR(unit_hp) +REPORT_GENERATOR(unit_hp, dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); return unit_hp(u); } -REPORT_GENERATOR(selected_unit_hp) +REPORT_GENERATOR(selected_unit_hp, dc) { - const unit *u = reports::get_selected_unit(); + const unit *u = reports::get_selected_unit(dc); return unit_hp(u); } @@ -485,14 +484,14 @@ static config unit_xp(const unit* u) tooltip << _("Experience Modifier: ") << exp_mod << '%'; return text_report(str.str(), tooltip.str()); } -REPORT_GENERATOR(unit_xp) +REPORT_GENERATOR(unit_xp, dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); return unit_xp(u); } -REPORT_GENERATOR(selected_unit_xp) +REPORT_GENERATOR(selected_unit_xp, dc) { - const unit *u = reports::get_selected_unit(); + const unit *u = reports::get_selected_unit(dc); return unit_xp(u); } @@ -506,26 +505,26 @@ static config unit_advancement_options(const unit* u) } return res; } -REPORT_GENERATOR(unit_advancement_options) +REPORT_GENERATOR(unit_advancement_options, dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); return unit_advancement_options(u); } -REPORT_GENERATOR(selected_unit_advancement_options) +REPORT_GENERATOR(selected_unit_advancement_options, dc) { - const unit *u = reports::get_selected_unit(); + const unit *u = reports::get_selected_unit(dc); return unit_advancement_options(u); } -static config unit_defense(const unit* u, const map_location& displayed_unit_hex) +static config unit_defense(const display_context * dc, const unit* u, const map_location& displayed_unit_hex) { if(!u) { return config(); } std::ostringstream str, tooltip; - const gamemap &map = resources::disp_context->map(); - if(!resources::disp_context->map().on_board(displayed_unit_hex)) { + const gamemap &map = dc->map(); + if(!dc->map().on_board(displayed_unit_hex)) { return config(); } @@ -558,17 +557,17 @@ static config unit_defense(const unit* u, const map_location& displayed_unit_hex tooltip << "" << _("Defense: ") << span_color(color) << def << '%' << naps << ""; return text_report(str.str(), tooltip.str()); } -REPORT_GENERATOR(unit_defense) +REPORT_GENERATOR(unit_defense,dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); const map_location& displayed_unit_hex = resources::screen->displayed_unit_hex(); - return unit_defense(u, displayed_unit_hex); + return unit_defense(dc, u, displayed_unit_hex); } -REPORT_GENERATOR(selected_unit_defense) +REPORT_GENERATOR(selected_unit_defense, dc) { - const unit *u = reports::get_selected_unit(); + const unit *u = reports::get_selected_unit(dc); const map_location& selected_hex = resources::screen->selected_hex(); - return unit_defense(u, selected_hex); + return unit_defense(dc, u, selected_hex); } static config unit_vision(const unit* u) @@ -581,18 +580,18 @@ static config unit_vision(const unit* u) str << _("vision: ") << u->vision(); } return text_report(str.str()); } -REPORT_GENERATOR(unit_vision) +REPORT_GENERATOR(unit_vision, dc) { - const unit* u = reports::get_visible_unit(); + const unit* u = reports::get_visible_unit(dc); return unit_vision(u); } -REPORT_GENERATOR(selected_unit_vision) +REPORT_GENERATOR(selected_unit_vision, dc) { - const unit* u = reports::get_selected_unit(); + const unit* u = reports::get_selected_unit(dc); return unit_vision(u); } -static config unit_moves(const unit* u) +static config unit_moves(const display_context * dc, const unit* u) { if (!u) return config(); std::ostringstream str, tooltip; @@ -613,7 +612,7 @@ static config unit_moves(const unit* u) if (terrain == t_translation::FOGGED || terrain == t_translation::VOID_TERRAIN || terrain == t_translation::OFF_MAP_USER) continue; - const terrain_type& info = resources::disp_context->map().get_terrain_info(terrain); + const terrain_type& info = dc->map().get_terrain_info(terrain); if (info.union_type().size() == 1 && info.union_type()[0] == info.number() && info.is_nonnull()) { @@ -648,18 +647,18 @@ static config unit_moves(const unit* u) str << span_color(c) << u->movement_left() << '/' << u->total_movement() << naps; return text_report(str.str(), tooltip.str()); } -REPORT_GENERATOR(unit_moves) +REPORT_GENERATOR(unit_moves, dc) { - const unit *u = reports::get_visible_unit(); - return unit_moves(u); + const unit *u = reports::get_visible_unit(dc); + return unit_moves(dc, u); } -REPORT_GENERATOR(selected_unit_moves) +REPORT_GENERATOR(selected_unit_moves, dc) { - const unit *u = reports::get_selected_unit(); - return unit_moves(u); + const unit *u = reports::get_selected_unit(dc); + return unit_moves(dc, u); } -static int attack_info(const attack_type &at, config &res, const unit &u, const map_location &displayed_unit_hex) +static int attack_info(const display_context * dc, const attack_type &at, config &res, const unit &u, const map_location &displayed_unit_hex) { std::ostringstream str, tooltip; @@ -670,7 +669,7 @@ static int attack_info(const attack_type &at, config &res, const unit &u, const int tod_bonus = combat_modifier(displayed_unit_hex, u.alignment(), u.is_fearless()); damage_multiplier += tod_bonus; int leader_bonus = 0; - if (under_leadership(resources::disp_context->units(), displayed_unit_hex, &leader_bonus).valid()) + if (under_leadership(dc->units(), displayed_unit_hex, &leader_bonus).valid()) damage_multiplier += leader_bonus; bool slowed = u.get_state(unit::STATE_SLOWED); @@ -767,9 +766,9 @@ static int attack_info(const attack_type &at, config &res, const unit &u, const // We want weak resistances (= good damage) first. std::map, std::greater > resistances; std::set seen_types; - const team &unit_team = resources::disp_context->teams()[u.side() - 1]; - const team &viewing_team = resources::disp_context->teams()[resources::screen->viewing_team()]; - BOOST_FOREACH(const unit &enemy, resources::disp_context->units()) + const team &unit_team = dc->teams()[u.side() - 1]; + const team &viewing_team = dc->teams()[resources::screen->viewing_team()]; + BOOST_FOREACH(const unit &enemy, dc->units()) { if (enemy.incapacitated()) //we can't attack statues so don't display them in this tooltip continue; @@ -864,7 +863,7 @@ static void format_hp(char str_buf[10], int hp) str_buf[9] = '\0'; //prevents _snprintf error } -static config unit_weapons(const unit *attacker, const map_location &attacker_pos, const unit *defender, bool show_attacker) +static config unit_weapons(const display_context * dc, const unit *attacker, const map_location &attacker_pos, const unit *defender, bool show_attacker) { if (!attacker || !defender) return config(); @@ -878,7 +877,7 @@ static config unit_weapons(const unit *attacker, const map_location &attacker_po for (unsigned int i = 0; i < attacker->attacks().size(); i++) { // skip weapons with attack_weight=0 if (attacker->attacks()[i].attack_weight() > 0) { - battle_context weapon(resources::disp_context->units(), attacker_pos, defender->get_location(), i, -1, 0.0, NULL, attacker); + battle_context weapon(dc->units(), attacker_pos, defender->get_location(), i, -1, 0.0, NULL, attacker); weapons.push_back(weapon); } } @@ -901,7 +900,7 @@ static config unit_weapons(const unit *attacker, const map_location &attacker_po SDL_Color dmg_color = font::weapon_color; if (context_unit_stats.weapon) { - base_damage = attack_info(*context_unit_stats.weapon, res, *u, unit_loc); + base_damage = attack_info(dc, *context_unit_stats.weapon, res, *u, unit_loc); total_damage = context_unit_stats.damage; num_blows = context_unit_stats.num_blows; chance_to_hit = context_unit_stats.chance_to_hit; @@ -990,7 +989,7 @@ static config unit_weapons(const unit *attacker, const map_location &attacker_po return res; } -static config unit_weapons(const unit *u) +static config unit_weapons(const display_context * dc, const unit *u) { if (!u || u->attacks().empty()) return config(); map_location displayed_unit_hex = resources::screen->displayed_unit_hex(); @@ -1005,79 +1004,79 @@ static config unit_weapons(const unit *u) BOOST_FOREACH(const attack_type &at, u->attacks()) { - attack_info(at, res, *u, displayed_unit_hex); + attack_info(dc, at, res, *u, displayed_unit_hex); } return res; } -REPORT_GENERATOR(unit_weapons) +REPORT_GENERATOR(unit_weapons, dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); if (!u) return config(); - return unit_weapons(u); + return unit_weapons(dc, u); } -REPORT_GENERATOR(highlighted_unit_weapons) +REPORT_GENERATOR(highlighted_unit_weapons, dc) { - const unit *u = reports::get_selected_unit(); - const unit *sec_u = reports::get_visible_unit(); + const unit *u = reports::get_selected_unit(dc); + const unit *sec_u = reports::get_visible_unit(dc); if (!u) return config(); - if (!sec_u || u == sec_u) return unit_weapons(sec_u); + if (!sec_u || u == sec_u) return unit_weapons(dc, sec_u); map_location highlighted_hex = resources::screen->displayed_unit_hex(); map_location attack_loc = resources::controller->get_mouse_handler_base().current_unit_attacks_from(highlighted_hex); if (!attack_loc.valid()) - return unit_weapons(sec_u); + return unit_weapons(dc, sec_u); - return unit_weapons(u, attack_loc, sec_u, false); + return unit_weapons(dc, u, attack_loc, sec_u, false); } -REPORT_GENERATOR(selected_unit_weapons) +REPORT_GENERATOR(selected_unit_weapons, dc) { - const unit *u = reports::get_selected_unit(); - const unit *sec_u = reports::get_visible_unit(); + const unit *u = reports::get_selected_unit(dc); + const unit *sec_u = reports::get_visible_unit(dc); if (!u) return config(); - if (!sec_u || u == sec_u) return unit_weapons(u); + if (!sec_u || u == sec_u) return unit_weapons(dc, u); map_location highlighted_hex = resources::screen->displayed_unit_hex(); map_location attack_loc = resources::controller->get_mouse_handler_base().current_unit_attacks_from(highlighted_hex); if (!attack_loc.valid()) - return unit_weapons(u); + return unit_weapons(dc, u); - return unit_weapons(u, attack_loc, sec_u, true); + return unit_weapons(dc, u, attack_loc, sec_u, true); } -REPORT_GENERATOR(unit_image) +REPORT_GENERATOR(unit_image,dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); if (!u) return config(); return image_report(u->absolute_image() + u->image_mods()); } -REPORT_GENERATOR(selected_unit_image) +REPORT_GENERATOR(selected_unit_image, dc) { - const unit *u = reports::get_selected_unit(); + const unit *u = reports::get_selected_unit(dc); if (!u) return config(); return image_report(u->absolute_image() + u->image_mods()); } -REPORT_GENERATOR(selected_unit_profile) +REPORT_GENERATOR(selected_unit_profile, dc) { - const unit *u = reports::get_selected_unit(); + const unit *u = reports::get_selected_unit(dc); if (!u) return config(); return image_report(u->small_profile()); } -REPORT_GENERATOR(unit_profile) +REPORT_GENERATOR(unit_profile, dc) { - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); if (!u) return config(); return image_report(u->small_profile()); } -REPORT_GENERATOR(tod_stats) +REPORT_GENERATOR(tod_stats, /*dc*/) { std::ostringstream tooltip; std::ostringstream text; @@ -1104,11 +1103,11 @@ REPORT_GENERATOR(tod_stats) return text_report(text.str(), tooltip.str()); } -static config time_of_day_at(const map_location& mouseover_hex) +static config time_of_day_at(const display_context * dc, const map_location& mouseover_hex) { std::ostringstream tooltip; time_of_day tod; - const team &viewing_team = resources::disp_context->teams()[resources::screen->viewing_team()]; + const team &viewing_team = dc->teams()[resources::screen->viewing_team()]; if (viewing_team.shrouded(mouseover_hex)) { // Don't show time on shrouded tiles. tod = resources::tod_manager->get_time_of_day(); @@ -1146,19 +1145,19 @@ static config time_of_day_at(const map_location& mouseover_hex) return image_report(tod_image, tooltip.str(), "time_of_day"); } -REPORT_GENERATOR(time_of_day) +REPORT_GENERATOR(time_of_day, dc) { map_location mouseover_hex = resources::screen->mouseover_hex(); - if (mouseover_hex.valid()) return time_of_day_at(mouseover_hex); - return time_of_day_at(resources::screen->selected_hex()); + if (mouseover_hex.valid()) return time_of_day_at(dc, mouseover_hex); + return time_of_day_at(dc, resources::screen->selected_hex()); } -static config unit_box_at(const map_location& mouseover_hex) +static config unit_box_at(const display_context * dc, const map_location& mouseover_hex) { std::ostringstream tooltip; time_of_day local_tod; time_of_day global_tod = resources::tod_manager->get_time_of_day(); - const team &viewing_team = resources::disp_context->teams()[resources::screen->viewing_team()]; + const team &viewing_team = dc->teams()[resources::screen->viewing_team()]; if (viewing_team.shrouded(mouseover_hex)) { // Don't show time on shrouded tiles. local_tod = global_tod; @@ -1195,7 +1194,7 @@ static config unit_box_at(const map_location& mouseover_hex) else if (local_tod.bonus_modified < 0) local_tod_image += "~DARKEN()"; if (preferences::flip_time()) local_tod_image += "~FL(horiz)"; - const gamemap &map = resources::disp_context->map(); + const gamemap &map = dc->map(); t_translation::t_terrain terrain = map.get_terrain(mouseover_hex); //if (terrain == t_translation::OFF_MAP_USER) @@ -1219,7 +1218,7 @@ static config unit_box_at(const map_location& mouseover_hex) bg_terrain_image = bg_terrain_image + "~CS(" + color.str() + ")"; - const unit *u = reports::get_visible_unit(); + const unit *u = reports::get_visible_unit(dc); std::string unit_image; if (u) unit_image = "~BLIT(" + u->absolute_image() + u->image_mods() + ",35,22)"; @@ -1228,14 +1227,14 @@ static config unit_box_at(const map_location& mouseover_hex) return image_report(tod_image + bg_terrain_image + unit_image, tooltip.str(), "time_of_day"); } -REPORT_GENERATOR(unit_box) +REPORT_GENERATOR(unit_box, dc) { map_location mouseover_hex = resources::screen->mouseover_hex(); - return unit_box_at(mouseover_hex); + return unit_box_at(dc, mouseover_hex); } -REPORT_GENERATOR(turn) +REPORT_GENERATOR(turn, /*dc*/) { std::ostringstream str; str << resources::tod_manager->turn(); @@ -1244,12 +1243,12 @@ REPORT_GENERATOR(turn) return text_report(str.str()); } -REPORT_GENERATOR(gold) +REPORT_GENERATOR(gold, dc) { std::ostringstream str; int viewing_side = resources::screen->viewing_side(); // Suppose the full unit map is applied. - int fake_gold = resources::disp_context->teams()[viewing_side - 1].gold() - + int fake_gold = dc->teams()[viewing_side - 1].gold() - resources::whiteboard->get_spent_gold_for(viewing_side); char const *end = naps; if (viewing_side != resources::screen->playing_side()) { @@ -1265,54 +1264,54 @@ REPORT_GENERATOR(gold) return text_report(str.str()); } -REPORT_GENERATOR(villages) +REPORT_GENERATOR(villages, dc) { std::ostringstream str; int viewing_side = display::get_singleton()->viewing_side(); - const team &viewing_team = resources::disp_context->teams()[viewing_side - 1]; + const team &viewing_team = dc->teams()[viewing_side - 1]; team_data td = calculate_team_data(viewing_team, viewing_side); str << td.villages << '/'; if (viewing_team.uses_shroud()) { int unshrouded_villages = 0; - BOOST_FOREACH(const map_location &loc, resources::disp_context->map().villages()) { + BOOST_FOREACH(const map_location &loc, dc->map().villages()) { if (!viewing_team.shrouded(loc)) ++unshrouded_villages; } str << unshrouded_villages; } else { - str << resources::disp_context->map().villages().size(); + str << dc->map().villages().size(); } return gray_inactive(str.str()); } -REPORT_GENERATOR(num_units) +REPORT_GENERATOR(num_units, /*dc*/) { return gray_inactive(str_cast(side_units(display::get_singleton()->viewing_side()))); } -REPORT_GENERATOR(upkeep) +REPORT_GENERATOR(upkeep, dc) { std::ostringstream str; int viewing_side = resources::screen->viewing_side(); - const team &viewing_team = resources::disp_context->teams()[viewing_side - 1]; + const team &viewing_team = dc->teams()[viewing_side - 1]; team_data td = calculate_team_data(viewing_team, viewing_side); str << td.expenses << " (" << td.upkeep << ")"; return gray_inactive(str.str()); } -REPORT_GENERATOR(expenses) +REPORT_GENERATOR(expenses, dc) { int viewing_side = resources::screen->viewing_side(); - const team &viewing_team = resources::disp_context->teams()[viewing_side - 1]; + const team &viewing_team = dc->teams()[viewing_side - 1]; team_data td = calculate_team_data(viewing_team, resources::screen->viewing_side()); return gray_inactive(str_cast(td.expenses)); } -REPORT_GENERATOR(income) +REPORT_GENERATOR(income, dc) { std::ostringstream str; int viewing_side = resources::screen->viewing_side(); - const team &viewing_team = resources::disp_context->teams()[viewing_side - 1]; + const team &viewing_team = dc->teams()[viewing_side - 1]; team_data td = calculate_team_data(viewing_team, viewing_side); char const *end = naps; if (viewing_side != resources::screen->playing_side()) { @@ -1345,9 +1344,9 @@ void blit_tced_icon(config &cfg, const std::string &terrain_id, const std::strin } } -REPORT_GENERATOR(terrain_info) +REPORT_GENERATOR(terrain_info, dc) { - const gamemap &map = resources::disp_context->map(); + const gamemap &map = dc->map(); map_location mouseover_hex = display::get_singleton()->mouseover_hex(); if (!map.on_board(mouseover_hex)) @@ -1391,11 +1390,11 @@ REPORT_GENERATOR(terrain_info) return cfg; } -REPORT_GENERATOR(terrain) +REPORT_GENERATOR(terrain, dc) { - const gamemap &map = resources::disp_context->map(); + const gamemap &map = dc->map(); int viewing_side = resources::screen->viewing_side(); - const team &viewing_team = resources::disp_context->teams()[viewing_side - 1]; + const team &viewing_team = dc->teams()[viewing_side - 1]; map_location mouseover_hex = resources::screen->mouseover_hex(); if (!map.on_board(mouseover_hex) || viewing_team.shrouded(mouseover_hex)) return config(); @@ -1429,7 +1428,7 @@ REPORT_GENERATOR(terrain) return text_report(str.str()); } -REPORT_GENERATOR(zoom_level) +REPORT_GENERATOR(zoom_level, /*dc*/) { std::ostringstream text; std::ostringstream tooltip; @@ -1440,9 +1439,9 @@ REPORT_GENERATOR(zoom_level) return text_report(text.str(), tooltip.str(), help.str()); } -REPORT_GENERATOR(position) +REPORT_GENERATOR(position, dc) { - const gamemap &map = resources::disp_context->map(); + const gamemap &map = dc->map(); map_location mouseover_hex = resources::screen->mouseover_hex(), displayed_unit_hex = resources::screen->displayed_unit_hex(), selected_hex = resources::screen->selected_hex(); @@ -1462,8 +1461,8 @@ REPORT_GENERATOR(position) std::ostringstream str; str << mouseover_hex; - const unit *u = reports::get_visible_unit(); - const team &viewing_team = resources::disp_context->teams()[resources::screen->viewing_team()]; + const unit *u = reports::get_visible_unit(dc); + const team &viewing_team = dc->teams()[resources::screen->viewing_team()]; if (!u || (displayed_unit_hex != mouseover_hex && displayed_unit_hex != resources::screen->selected_hex()) || @@ -1482,9 +1481,9 @@ REPORT_GENERATOR(position) return text_report(str.str()); } -REPORT_GENERATOR(side_playing) +REPORT_GENERATOR(side_playing, dc) { - const team &active_team = resources::disp_context->teams()[resources::screen->playing_team()]; + const team &active_team = dc->teams()[resources::screen->playing_team()]; std::string flag_icon = active_team.flag_icon(); std::string old_rgb = game_config::flag_rgb; std::string new_rgb = team::get_side_color_index(resources::screen->playing_side()); @@ -1494,7 +1493,7 @@ REPORT_GENERATOR(side_playing) return image_report(flag_icon + mods, active_team.current_player()); } -REPORT_GENERATOR(observers) +REPORT_GENERATOR(observers, /*dc*/) { const std::set &observers = resources::screen->observers(); if (observers.empty()) @@ -1530,7 +1529,7 @@ REPORT_GENERATOR(edit_left_button_function) } */ -REPORT_GENERATOR(report_clock) +REPORT_GENERATOR(report_clock, /*dc*/) { time_t t = std::time(NULL); struct tm *lt = std::localtime(&t); @@ -1543,13 +1542,13 @@ REPORT_GENERATOR(report_clock) } -REPORT_GENERATOR(report_countdown) +REPORT_GENERATOR(report_countdown, dc) { int viewing_side = resources::screen->viewing_side(); - const team &viewing_team = resources::disp_context->teams()[viewing_side - 1]; + const team &viewing_team = dc->teams()[viewing_side - 1]; int min, sec; if (viewing_team.countdown_time() == 0) - return report_report_clock(); + return report_report_clock(dc); std::ostringstream str; sec = viewing_team.countdown_time() / 1000; char const *end = naps; @@ -1590,16 +1589,16 @@ void reports::register_generator(const std::string &name, reports::generator *g) } } -config reports::generate_report(const std::string &name, bool only_static) +config reports::generate_report(const std::string &name, const display_context * dc, bool only_static) { if (!only_static) { dynamic_report_generators::const_iterator i = dynamic_generators.find(name); if (i != dynamic_generators.end()) - return i->second->generate(); + return i->second->generate(dc); } static_report_generators::const_iterator j = static_generators.find(name); if (j != static_generators.end()) - return j->second(); + return j->second(dc); return config(); } diff --git a/src/reports.hpp b/src/reports.hpp index 154b2c19adcc..02facce43c01 100644 --- a/src/reports.hpp +++ b/src/reports.hpp @@ -16,6 +16,7 @@ #define REPORTS_HPP_INCLUDED #include "map_location.hpp" +#include "display_context.hpp" //this module is responsible for outputting textual reports of //various game and unit statistics @@ -23,14 +24,14 @@ namespace reports { struct generator { - virtual config generate() = 0; + virtual config generate(const display_context *) = 0; virtual ~generator() {} }; void reset_generators(); void register_generator(const std::string &name, generator *); -config generate_report(const std::string &name, bool only_static = false); +config generate_report(const std::string &name, const display_context * dc, bool only_static = false); const std::set &report_list(); } diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 5311846b9316..1ec7ab0c7c5f 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -3575,10 +3575,10 @@ namespace { std::string name; lua_report_generator(lua_State *L, const std::string &n) : mState(L), name(n) {} - virtual config generate(); + virtual config generate(const display_context * dc); }; - config lua_report_generator::generate() + config lua_report_generator::generate(const display_context * /*dc*/) { lua_State *L = mState; config cfg; @@ -3597,7 +3597,7 @@ namespace { static int cfun_theme_item(lua_State *L) { const char *m = lua_tostring(L, lua_upvalueindex(1)); - luaW_pushconfig(L, reports::generate_report(m, true)); + luaW_pushconfig(L, reports::generate_report(m, resources::disp_context, true)); return 1; } From f4754496d3ae0eaca9a73b86ff849ba793564ffe Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Thu, 12 Jun 2014 17:20:38 -0400 Subject: [PATCH 5/9] remove unnecessary (duplicated) accessors --- src/display.hpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/display.hpp b/src/display.hpp index af89f9789e76..d468086231cb 100644 --- a/src/display.hpp +++ b/src/display.hpp @@ -388,9 +388,6 @@ class display void refresh_report(std::string const &report_name, const config * new_cfg=NULL); - map_location get_selected_hex() const { return selectedHex_; } - map_location get_highlight_hex() const { return mouseoverHex_; } - void draw_minimap_units(); /** Function to invalidate all tiles. */ From 2a2a50a1788ba3ced56e2f51a230d0683a21c1f0 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Thu, 12 Jun 2014 17:23:08 -0400 Subject: [PATCH 6/9] make whiteboard a shared_pointer in resources This makes subsequent commit easier, because lua wants to be able to make reports, and therefore needs a report_context --- src/play_controller.cpp | 4 ++-- src/resources.cpp | 2 +- src/resources.hpp | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/play_controller.cpp b/src/play_controller.cpp index 303597ee88e4..4a138868bec3 100644 --- a/src/play_controller.cpp +++ b/src/play_controller.cpp @@ -84,7 +84,7 @@ static void clear_resources() resources::tunnels = NULL; resources::undo_stack = NULL; resources::units = NULL; - resources::whiteboard = NULL; + resources::whiteboard.reset(); resources::classification = NULL; @@ -259,7 +259,7 @@ void play_controller::init(CVideo& video){ pathfind_manager_.reset(new pathfind::manager(level_)); whiteboard_manager_.reset(new wb::manager()); resources::tunnels = pathfind_manager_.get(); - resources::whiteboard = whiteboard_manager_.get(); + resources::whiteboard = whiteboard_manager_; LOG_NG << "building terrain rules... " << (SDL_GetTicks() - ticks_) << std::endl; loadscreen::start_stage("build terrain"); diff --git a/src/resources.cpp b/src/resources.cpp index eb1feea81c38..25c1cb8d3e96 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -31,7 +31,7 @@ namespace resources pathfind::manager *tunnels = NULL; actions::undo_list *undo_stack = NULL; unit_map *units = NULL; - wb::manager *whiteboard = NULL; + boost::shared_ptr whiteboard = boost::shared_ptr(); game_classification *classification = NULL; const mp_game_settings *mp_settings = NULL; } diff --git a/src/resources.hpp b/src/resources.hpp index 848e54a0a1d3..efdae3d68322 100644 --- a/src/resources.hpp +++ b/src/resources.hpp @@ -16,6 +16,7 @@ #define RESOURCES_H_ #include +#include class game_board; class game_config_manager; @@ -57,7 +58,7 @@ namespace resources extern pathfind::manager *tunnels; extern actions::undo_list *undo_stack; extern unit_map *units; - extern wb::manager *whiteboard; + extern boost::shared_ptr whiteboard; } #endif From 816be79aa2c48849186edb6c6fff3252b6bdf813 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Thu, 12 Jun 2014 19:52:09 -0400 Subject: [PATCH 7/9] add reports::context structure, and use it for reports --- src/display.cpp | 12 +- src/reports.cpp | 473 ++++++++++++++++++++++-------------------- src/reports.hpp | 44 +++- src/scripting/lua.cpp | 7 +- 4 files changed, 307 insertions(+), 229 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index 610a0a021f9f..b500b37371b3 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -28,6 +28,7 @@ #include "map.hpp" #include "map_label.hpp" #include "minimap.hpp" +#include "play_controller.hpp" //note: this can probably be refactored out #include "reports.hpp" #include "terrain_builder.hpp" #include "text.hpp" @@ -2734,7 +2735,16 @@ void display::refresh_report(std::string const &report_name, const config * new_ } // Now we will need the config. Generate one if needed. - const config generated_cfg = new_cfg ? config() : reports::generate_report(report_name, dc_); + + boost::optional mhb = boost::none; + + if (resources::controller) { + mhb = resources::controller->get_mouse_handler_base(); + } + + reports::context temp_context = reports::context(*dc_, *this, wb_.lock(), mhb); + + const config generated_cfg = new_cfg ? config() : reports::generate_report(report_name, temp_context); if ( new_cfg == NULL ) new_cfg = &generated_cfg; diff --git a/src/reports.cpp b/src/reports.cpp index 6514b68b2013..7469f5dd3853 100644 --- a/src/reports.cpp +++ b/src/reports.cpp @@ -92,7 +92,7 @@ static std::string flush(std::ostringstream &s) return r; } -typedef config (*generator_function)(const display_context *); +typedef config (*generator_function)(reports::context & ); typedef std::map static_report_generators; typedef std::map dynamic_report_generators; @@ -107,36 +107,59 @@ struct report_generator_helper } }; -#define REPORT_GENERATOR(n, dcn) \ - static config report_##n(const display_context * dcn); \ +#define REPORT_GENERATOR(n, cn) \ + static config report_##n(reports::context & cn); \ static report_generator_helper reg_gen_##n(#n, &report_##n); \ - static config report_##n(const display_context * dcn) + static config report_##n(reports::context & cn) static char const *naps = ""; namespace reports { -static const unit *get_visible_unit(const display_context * dc) +static map_location get_displayed_unit_hex(reports::context & rc) { + return dynamic_cast(rc.screen()).displayed_unit_hex(); +} + +static int get_playing_side(reports::context & rc) { + if (rc.wb()) { + return dynamic_cast(rc.screen()).playing_side(); + } + return -100; //this is a hacky run_time fix, the dynamic cast is not safe for the editor. I think it was only working before because resources::screen is null. +} + +static const std::set get_observers (reports::context & rc) { + if (rc.wb()) { + return dynamic_cast(rc.screen()).observers(); + } + return std::set (); +} + +static double get_get_zoom_factor (reports::context & rc) { + if (rc.wb()) { + return dynamic_cast(rc.screen()).get_zoom_factor(); + } + return -100; +} + +static const unit *get_visible_unit(reports::context & rc) { - return dc->get_visible_unit(resources::screen->displayed_unit_hex(), - dc->teams()[resources::screen->viewing_team()], - resources::screen->show_everything()); + return rc.dc().get_visible_unit(get_displayed_unit_hex(rc), + rc.teams()[rc.screen().viewing_team()], + rc.screen().show_everything()); } -static const unit *get_selected_unit(const display_context * dc) +static const unit *get_selected_unit(reports::context & rc) { - return dc->get_visible_unit(resources::screen->selected_hex(), - dc->teams()[resources::screen->viewing_team()], - resources::screen->show_everything()); + return rc.dc().get_visible_unit(rc.screen().selected_hex(), + rc.teams()[rc.screen().viewing_team()], + rc.screen().show_everything()); } } -static config gray_inactive(const std::string &str) +static config gray_inactive(reports::context & rc, const std::string &str) { - if ( (resources::screen && - (resources::screen->viewing_side() == resources::screen->playing_side()) ) - || !resources::screen ) + if ( rc.screen().viewing_side() == get_playing_side(rc) ) return text_report(str); return text_report(span_color(font::GRAY_COLOR) + str + naps); @@ -159,14 +182,14 @@ static config unit_name(const unit *u) return text_report(str.str(), tooltip.str()); } -REPORT_GENERATOR(unit_name, dc) +REPORT_GENERATOR(unit_name, rc) { - const unit *u = reports::get_visible_unit(dc); + const unit *u = reports::get_visible_unit(rc); return unit_name(u); } -REPORT_GENERATOR(selected_unit_name, dc) +REPORT_GENERATOR(selected_unit_name, rc) { - const unit *u = reports::get_selected_unit(dc); + const unit *u = reports::get_selected_unit(rc); return unit_name(u); } @@ -180,14 +203,14 @@ static config unit_type(const unit* u) << u->unit_description(); return text_report(str.str(), tooltip.str(), has_variations_prefix + "unit_" + u->type_id()); } -REPORT_GENERATOR(unit_type, dc) +REPORT_GENERATOR(unit_type, rc) { - const unit *u = reports::get_visible_unit(dc); + const unit *u = reports::get_visible_unit(rc); return unit_type(u); } -REPORT_GENERATOR(selected_unit_type, dc) +REPORT_GENERATOR(selected_unit_type, rc) { - const unit *u = reports::get_selected_unit(dc); + const unit *u = reports::get_selected_unit(rc); return unit_type(u); } @@ -199,23 +222,23 @@ static config unit_race(const unit* u) tooltip << _("Race: ") << "" << u->race()->name(u->gender()) << ""; return text_report(str.str(), tooltip.str(), "..race_" + u->race()->id()); } -REPORT_GENERATOR(unit_race, dc) +REPORT_GENERATOR(unit_race, rc) { - const unit *u = reports::get_visible_unit(dc); + const unit *u = reports::get_visible_unit(rc); return unit_race(u); } -REPORT_GENERATOR(selected_unit_race, dc) +REPORT_GENERATOR(selected_unit_race, rc) { - const unit *u = reports::get_selected_unit(dc); + const unit *u = reports::get_selected_unit(rc); return unit_race(u); } -static config unit_side(const display_context * dc, const unit* u) +static config unit_side(reports::context & rc, const unit* u) { if (!u) return config(); config report; - const team &u_team = dc->teams()[u->side() - 1]; + const team &u_team = rc.teams()[u->side() - 1]; std::string flag_icon = u_team.flag_icon(); std::string old_rgb = game_config::flag_rgb; std::string new_rgb = team::get_side_color_index(u->side()); @@ -230,15 +253,15 @@ static config unit_side(const display_context * dc, const unit* u) add_text(report, text.str(), "", ""); return report; } -REPORT_GENERATOR(unit_side, dc) +REPORT_GENERATOR(unit_side, rc) { - const unit *u = reports::get_visible_unit(dc); - return unit_side(dc,u); + const unit *u = reports::get_visible_unit(rc); + return unit_side(rc,u); } -REPORT_GENERATOR(selected_unit_side, dc) +REPORT_GENERATOR(selected_unit_side, rc) { - const unit *u = reports::get_selected_unit(dc); - return unit_side(dc, u); + const unit *u = reports::get_selected_unit(rc); + return unit_side(rc, u); } static config unit_level(const unit* u) @@ -255,20 +278,20 @@ static config unit_level(const unit* u) << utils::join(adv_to, "\n\t") << ""; return text_report(str.str(), tooltip.str()); } -REPORT_GENERATOR(unit_level, dc) +REPORT_GENERATOR(unit_level, rc) { - const unit *u = reports::get_visible_unit(dc); + const unit *u = reports::get_visible_unit(rc); return unit_level(u); } -REPORT_GENERATOR(selected_unit_level, dc) +REPORT_GENERATOR(selected_unit_level, rc) { - const unit *u = reports::get_selected_unit(dc); + const unit *u = reports::get_selected_unit(rc); return unit_level(u); } -REPORT_GENERATOR(unit_amla, dc) +REPORT_GENERATOR(unit_amla, rc) { - const unit *u = reports::get_visible_unit(dc); + const unit *u = reports::get_visible_unit(rc); if (!u) return config(); config res; typedef std::pair pair_string; @@ -297,23 +320,23 @@ static config unit_traits(const unit* u) } return res; } -REPORT_GENERATOR(unit_traits, dc) +REPORT_GENERATOR(unit_traits, rc) { - const unit *u = reports::get_visible_unit(dc); + const unit *u = reports::get_visible_unit(rc); return unit_traits(u); } -REPORT_GENERATOR(selected_unit_traits, dc) +REPORT_GENERATOR(selected_unit_traits, rc) { - const unit *u = reports::get_selected_unit(dc); + const unit *u = reports::get_selected_unit(rc); return unit_traits(u); } -static config unit_status(const display_context * dc, const unit* u) +static config unit_status(reports::context & rc, const unit* u) { if (!u) return config(); config res; - map_location displayed_unit_hex = resources::screen->displayed_unit_hex(); - if (dc->map().on_board(displayed_unit_hex) && u->invisible(displayed_unit_hex)) { + map_location displayed_unit_hex = get_displayed_unit_hex(rc); + if (rc.map().on_board(displayed_unit_hex) && u->invisible(displayed_unit_hex)) { add_status(res, "misc/invisible.png", N_("invisible: "), N_("This unit is invisible. It cannot be seen or attacked by enemy units.")); } @@ -331,24 +354,24 @@ static config unit_status(const display_context * dc, const unit* u) } return res; } -REPORT_GENERATOR(unit_status,dc) +REPORT_GENERATOR(unit_status,rc) { - const unit *u = reports::get_visible_unit(dc); - return unit_status(dc,u); + const unit *u = reports::get_visible_unit(rc); + return unit_status(rc,u); } -REPORT_GENERATOR(selected_unit_status, dc) +REPORT_GENERATOR(selected_unit_status, rc) { - const unit *u = reports::get_selected_unit(dc); - return unit_status(dc, u); + const unit *u = reports::get_selected_unit(rc); + return unit_status(rc, u); } -static config unit_alignment(const unit* u) +static config unit_alignment(reports::context & rc, const unit* u) { if (!u) return config(); std::ostringstream str, tooltip; const std::string align = unit_type::alignment_description(u->alignment(), u->gender()); const std::string align_id = lexical_cast(u->alignment()); - int cm = combat_modifier(resources::screen->displayed_unit_hex(), u->alignment(), + int cm = combat_modifier(get_displayed_unit_hex(rc), u->alignment(), u->is_fearless()); SDL_Color color = font::weapon_color; @@ -363,15 +386,15 @@ static config unit_alignment(const unit* u) return text_report(str.str(), tooltip.str(), "time_of_day"); } -REPORT_GENERATOR(unit_alignment, dc) +REPORT_GENERATOR(unit_alignment, rc) { - const unit *u = reports::get_visible_unit(dc); - return unit_alignment(u); + const unit *u = reports::get_visible_unit(rc); + return unit_alignment(rc, u); } -REPORT_GENERATOR(selected_unit_alignment, dc) +REPORT_GENERATOR(selected_unit_alignment, rc) { - const unit *u = reports::get_selected_unit(dc); - return unit_alignment(u); + const unit *u = reports::get_selected_unit(rc); + return unit_alignment(rc, u); } @@ -408,19 +431,19 @@ static config unit_abilities(const unit* u) } return res; } -REPORT_GENERATOR(unit_abilities, dc) +REPORT_GENERATOR(unit_abilities, rc) { - const unit *u = reports::get_visible_unit(dc); + const unit *u = reports::get_visible_unit(rc); return unit_abilities(u); } -REPORT_GENERATOR(selected_unit_abilities, dc) +REPORT_GENERATOR(selected_unit_abilities, rc) { - const unit *u = reports::get_selected_unit(dc); + const unit *u = reports::get_selected_unit(rc); return unit_abilities(u); } -static config unit_hp(const unit* u) +static config unit_hp(reports::context& rc, const unit* u) { if (!u) return config(); std::ostringstream str, tooltip; @@ -430,7 +453,7 @@ static config unit_hp(const unit* u) std::set resistances_table; bool att_def_diff = false; - map_location displayed_unit_hex = resources::screen->displayed_unit_hex(); + map_location displayed_unit_hex = get_displayed_unit_hex(rc); BOOST_FOREACH(const utils::string_map::value_type &resist, u->get_base_resistances()) { std::ostringstream line; @@ -462,15 +485,15 @@ static config unit_hp(const unit* u) } return text_report(str.str(), tooltip.str()); } -REPORT_GENERATOR(unit_hp, dc) +REPORT_GENERATOR(unit_hp, rc) { - const unit *u = reports::get_visible_unit(dc); - return unit_hp(u); + const unit *u = reports::get_visible_unit(rc); + return unit_hp(rc, u); } -REPORT_GENERATOR(selected_unit_hp, dc) +REPORT_GENERATOR(selected_unit_hp, rc) { - const unit *u = reports::get_selected_unit(dc); - return unit_hp(u); + const unit *u = reports::get_selected_unit(rc); + return unit_hp(rc, u); } static config unit_xp(const unit* u) @@ -484,14 +507,14 @@ static config unit_xp(const unit* u) tooltip << _("Experience Modifier: ") << exp_mod << '%'; return text_report(str.str(), tooltip.str()); } -REPORT_GENERATOR(unit_xp, dc) +REPORT_GENERATOR(unit_xp, rc) { - const unit *u = reports::get_visible_unit(dc); + const unit *u = reports::get_visible_unit(rc); return unit_xp(u); } -REPORT_GENERATOR(selected_unit_xp, dc) +REPORT_GENERATOR(selected_unit_xp, rc) { - const unit *u = reports::get_selected_unit(dc); + const unit *u = reports::get_selected_unit(rc); return unit_xp(u); } @@ -505,26 +528,26 @@ static config unit_advancement_options(const unit* u) } return res; } -REPORT_GENERATOR(unit_advancement_options, dc) +REPORT_GENERATOR(unit_advancement_options, rc) { - const unit *u = reports::get_visible_unit(dc); + const unit *u = reports::get_visible_unit(rc); return unit_advancement_options(u); } -REPORT_GENERATOR(selected_unit_advancement_options, dc) +REPORT_GENERATOR(selected_unit_advancement_options, rc) { - const unit *u = reports::get_selected_unit(dc); + const unit *u = reports::get_selected_unit(rc); return unit_advancement_options(u); } -static config unit_defense(const display_context * dc, const unit* u, const map_location& displayed_unit_hex) +static config unit_defense(reports::context & rc, const unit* u, const map_location& displayed_unit_hex) { if(!u) { return config(); } std::ostringstream str, tooltip; - const gamemap &map = dc->map(); - if(!dc->map().on_board(displayed_unit_hex)) { + const gamemap &map = rc.map(); + if(!rc.map().on_board(displayed_unit_hex)) { return config(); } @@ -557,17 +580,17 @@ static config unit_defense(const display_context * dc, const unit* u, const map_ tooltip << "" << _("Defense: ") << span_color(color) << def << '%' << naps << ""; return text_report(str.str(), tooltip.str()); } -REPORT_GENERATOR(unit_defense,dc) +REPORT_GENERATOR(unit_defense,rc) { - const unit *u = reports::get_visible_unit(dc); - const map_location& displayed_unit_hex = resources::screen->displayed_unit_hex(); - return unit_defense(dc, u, displayed_unit_hex); + const unit *u = reports::get_visible_unit(rc); + const map_location& displayed_unit_hex = get_displayed_unit_hex(rc); + return unit_defense(rc, u, displayed_unit_hex); } -REPORT_GENERATOR(selected_unit_defense, dc) +REPORT_GENERATOR(selected_unit_defense, rc) { - const unit *u = reports::get_selected_unit(dc); - const map_location& selected_hex = resources::screen->selected_hex(); - return unit_defense(dc, u, selected_hex); + const unit *u = reports::get_selected_unit(rc); + const map_location& selected_hex = rc.screen().selected_hex(); + return unit_defense(rc, u, selected_hex); } static config unit_vision(const unit* u) @@ -580,23 +603,23 @@ static config unit_vision(const unit* u) str << _("vision: ") << u->vision(); } return text_report(str.str()); } -REPORT_GENERATOR(unit_vision, dc) +REPORT_GENERATOR(unit_vision, rc) { - const unit* u = reports::get_visible_unit(dc); + const unit* u = reports::get_visible_unit(rc); return unit_vision(u); } -REPORT_GENERATOR(selected_unit_vision, dc) +REPORT_GENERATOR(selected_unit_vision, rc) { - const unit* u = reports::get_selected_unit(dc); + const unit* u = reports::get_selected_unit(rc); return unit_vision(u); } -static config unit_moves(const display_context * dc, const unit* u) +static config unit_moves(reports::context & rc, const unit* u) { if (!u) return config(); std::ostringstream str, tooltip; double movement_frac = 1.0; - if (u->side() == resources::screen->playing_side()) { + if (u->side() == get_playing_side(rc)) { movement_frac = double(u->movement_left()) / std::max(1, u->total_movement()); if (movement_frac > 1.0) movement_frac = 1.0; @@ -612,7 +635,7 @@ static config unit_moves(const display_context * dc, const unit* u) if (terrain == t_translation::FOGGED || terrain == t_translation::VOID_TERRAIN || terrain == t_translation::OFF_MAP_USER) continue; - const terrain_type& info = dc->map().get_terrain_info(terrain); + const terrain_type& info = rc.map().get_terrain_info(terrain); if (info.union_type().size() == 1 && info.union_type()[0] == info.number() && info.is_nonnull()) { @@ -647,29 +670,29 @@ static config unit_moves(const display_context * dc, const unit* u) str << span_color(c) << u->movement_left() << '/' << u->total_movement() << naps; return text_report(str.str(), tooltip.str()); } -REPORT_GENERATOR(unit_moves, dc) +REPORT_GENERATOR(unit_moves, rc) { - const unit *u = reports::get_visible_unit(dc); - return unit_moves(dc, u); + const unit *u = reports::get_visible_unit(rc); + return unit_moves(rc, u); } -REPORT_GENERATOR(selected_unit_moves, dc) +REPORT_GENERATOR(selected_unit_moves, rc) { - const unit *u = reports::get_selected_unit(dc); - return unit_moves(dc, u); + const unit *u = reports::get_selected_unit(rc); + return unit_moves(rc, u); } -static int attack_info(const display_context * dc, const attack_type &at, config &res, const unit &u, const map_location &displayed_unit_hex) +static int attack_info(reports::context & rc, const attack_type &at, config &res, const unit &u, const map_location &displayed_unit_hex) { std::ostringstream str, tooltip; - at.set_specials_context(displayed_unit_hex, u.side() == resources::screen->playing_side()); + at.set_specials_context(displayed_unit_hex, u.side() == get_playing_side(rc)); int base_damage = at.damage(); int specials_damage = at.modified_damage(false); int damage_multiplier = 100; int tod_bonus = combat_modifier(displayed_unit_hex, u.alignment(), u.is_fearless()); damage_multiplier += tod_bonus; int leader_bonus = 0; - if (under_leadership(dc->units(), displayed_unit_hex, &leader_bonus).valid()) + if (under_leadership(rc.units(), displayed_unit_hex, &leader_bonus).valid()) damage_multiplier += leader_bonus; bool slowed = u.get_state(unit::STATE_SLOWED); @@ -766,9 +789,9 @@ static int attack_info(const display_context * dc, const attack_type &at, config // We want weak resistances (= good damage) first. std::map, std::greater > resistances; std::set seen_types; - const team &unit_team = dc->teams()[u.side() - 1]; - const team &viewing_team = dc->teams()[resources::screen->viewing_team()]; - BOOST_FOREACH(const unit &enemy, dc->units()) + const team &unit_team = rc.teams()[u.side() - 1]; + const team &viewing_team = rc.teams()[rc.screen().viewing_team()]; + BOOST_FOREACH(const unit &enemy, rc.units()) { if (enemy.incapacitated()) //we can't attack statues so don't display them in this tooltip continue; @@ -863,7 +886,7 @@ static void format_hp(char str_buf[10], int hp) str_buf[9] = '\0'; //prevents _snprintf error } -static config unit_weapons(const display_context * dc, const unit *attacker, const map_location &attacker_pos, const unit *defender, bool show_attacker) +static config unit_weapons(reports::context & rc, const unit *attacker, const map_location &attacker_pos, const unit *defender, bool show_attacker) { if (!attacker || !defender) return config(); @@ -877,7 +900,7 @@ static config unit_weapons(const display_context * dc, const unit *attacker, con for (unsigned int i = 0; i < attacker->attacks().size(); i++) { // skip weapons with attack_weight=0 if (attacker->attacks()[i].attack_weight() > 0) { - battle_context weapon(dc->units(), attacker_pos, defender->get_location(), i, -1, 0.0, NULL, attacker); + battle_context weapon(rc.units(), attacker_pos, defender->get_location(), i, -1, 0.0, NULL, attacker); weapons.push_back(weapon); } } @@ -900,7 +923,7 @@ static config unit_weapons(const display_context * dc, const unit *attacker, con SDL_Color dmg_color = font::weapon_color; if (context_unit_stats.weapon) { - base_damage = attack_info(dc, *context_unit_stats.weapon, res, *u, unit_loc); + base_damage = attack_info(rc, *context_unit_stats.weapon, res, *u, unit_loc); total_damage = context_unit_stats.damage; num_blows = context_unit_stats.num_blows; chance_to_hit = context_unit_stats.chance_to_hit; @@ -989,10 +1012,10 @@ static config unit_weapons(const display_context * dc, const unit *attacker, con return res; } -static config unit_weapons(const display_context * dc, const unit *u) +static config unit_weapons(reports::context & rc, const unit *u) { if (!u || u->attacks().empty()) return config(); - map_location displayed_unit_hex = resources::screen->displayed_unit_hex(); + map_location displayed_unit_hex = get_displayed_unit_hex(rc); config res; //TODO enable after the string frezze is lifted @@ -1004,85 +1027,87 @@ static config unit_weapons(const display_context * dc, const unit *u) BOOST_FOREACH(const attack_type &at, u->attacks()) { - attack_info(dc, at, res, *u, displayed_unit_hex); + attack_info(rc, at, res, *u, displayed_unit_hex); } return res; } -REPORT_GENERATOR(unit_weapons, dc) +REPORT_GENERATOR(unit_weapons, rc) { - const unit *u = reports::get_visible_unit(dc); + const unit *u = reports::get_visible_unit(rc); if (!u) return config(); - return unit_weapons(dc, u); + return unit_weapons(rc, u); } -REPORT_GENERATOR(highlighted_unit_weapons, dc) +REPORT_GENERATOR(highlighted_unit_weapons, rc) { - const unit *u = reports::get_selected_unit(dc); - const unit *sec_u = reports::get_visible_unit(dc); + const unit *u = reports::get_selected_unit(rc); + const unit *sec_u = reports::get_visible_unit(rc); if (!u) return config(); - if (!sec_u || u == sec_u) return unit_weapons(dc, sec_u); + if (!sec_u || u == sec_u) return unit_weapons(rc, sec_u); - map_location highlighted_hex = resources::screen->displayed_unit_hex(); - map_location attack_loc = - resources::controller->get_mouse_handler_base().current_unit_attacks_from(highlighted_hex); + map_location highlighted_hex = get_displayed_unit_hex(rc); + map_location attack_loc; + if (rc.mhb()) + attack_loc = rc.mhb()->current_unit_attacks_from(highlighted_hex); if (!attack_loc.valid()) - return unit_weapons(dc, sec_u); + return unit_weapons(rc, sec_u); - return unit_weapons(dc, u, attack_loc, sec_u, false); + return unit_weapons(rc, u, attack_loc, sec_u, false); } -REPORT_GENERATOR(selected_unit_weapons, dc) +REPORT_GENERATOR(selected_unit_weapons, rc) { - const unit *u = reports::get_selected_unit(dc); - const unit *sec_u = reports::get_visible_unit(dc); + const unit *u = reports::get_selected_unit(rc); + const unit *sec_u = reports::get_visible_unit(rc); if (!u) return config(); - if (!sec_u || u == sec_u) return unit_weapons(dc, u); + if (!sec_u || u == sec_u) return unit_weapons(rc, u); - map_location highlighted_hex = resources::screen->displayed_unit_hex(); - map_location attack_loc = - resources::controller->get_mouse_handler_base().current_unit_attacks_from(highlighted_hex); + map_location highlighted_hex = get_displayed_unit_hex(rc); + map_location attack_loc; + if (rc.mhb()) + attack_loc = rc.mhb()->current_unit_attacks_from(highlighted_hex); if (!attack_loc.valid()) - return unit_weapons(dc, u); + return unit_weapons(rc, u); - return unit_weapons(dc, u, attack_loc, sec_u, true); + return unit_weapons(rc, u, attack_loc, sec_u, true); } -REPORT_GENERATOR(unit_image,dc) +REPORT_GENERATOR(unit_image,rc) { - const unit *u = reports::get_visible_unit(dc); + const unit *u = reports::get_visible_unit(rc); if (!u) return config(); return image_report(u->absolute_image() + u->image_mods()); } -REPORT_GENERATOR(selected_unit_image, dc) +REPORT_GENERATOR(selected_unit_image, rc) { - const unit *u = reports::get_selected_unit(dc); + const unit *u = reports::get_selected_unit(rc); if (!u) return config(); return image_report(u->absolute_image() + u->image_mods()); } -REPORT_GENERATOR(selected_unit_profile, dc) +REPORT_GENERATOR(selected_unit_profile, rc) { - const unit *u = reports::get_selected_unit(dc); + const unit *u = reports::get_selected_unit(rc); if (!u) return config(); return image_report(u->small_profile()); } -REPORT_GENERATOR(unit_profile, dc) +REPORT_GENERATOR(unit_profile, rc) { - const unit *u = reports::get_visible_unit(dc); + const unit *u = reports::get_visible_unit(rc); if (!u) return config(); return image_report(u->small_profile()); } -REPORT_GENERATOR(tod_stats, /*dc*/) +REPORT_GENERATOR(tod_stats, rc) { std::ostringstream tooltip; std::ostringstream text; - const map_location& selected_hex = resources::screen->selected_hex(); - const map_location& mouseover_hex = resources::screen->mouseover_hex(); + const map_location& selected_hex = rc.screen().selected_hex(); + const map_location& mouseover_hex = rc.screen().mouseover_hex(); const map_location& hex = mouseover_hex.valid() ? mouseover_hex : selected_hex; @@ -1103,11 +1128,11 @@ REPORT_GENERATOR(tod_stats, /*dc*/) return text_report(text.str(), tooltip.str()); } -static config time_of_day_at(const display_context * dc, const map_location& mouseover_hex) +static config time_of_day_at(reports::context & rc, const map_location& mouseover_hex) { std::ostringstream tooltip; time_of_day tod; - const team &viewing_team = dc->teams()[resources::screen->viewing_team()]; + const team &viewing_team = rc.teams()[rc.screen().viewing_team()]; if (viewing_team.shrouded(mouseover_hex)) { // Don't show time on shrouded tiles. tod = resources::tod_manager->get_time_of_day(); @@ -1145,19 +1170,19 @@ static config time_of_day_at(const display_context * dc, const map_location& mou return image_report(tod_image, tooltip.str(), "time_of_day"); } -REPORT_GENERATOR(time_of_day, dc) +REPORT_GENERATOR(time_of_day, rc) { - map_location mouseover_hex = resources::screen->mouseover_hex(); - if (mouseover_hex.valid()) return time_of_day_at(dc, mouseover_hex); - return time_of_day_at(dc, resources::screen->selected_hex()); + map_location mouseover_hex = rc.screen().mouseover_hex(); + if (mouseover_hex.valid()) return time_of_day_at(rc, mouseover_hex); + return time_of_day_at(rc, rc.screen().selected_hex()); } -static config unit_box_at(const display_context * dc, const map_location& mouseover_hex) +static config unit_box_at(reports::context & rc, const map_location& mouseover_hex) { std::ostringstream tooltip; time_of_day local_tod; time_of_day global_tod = resources::tod_manager->get_time_of_day(); - const team &viewing_team = dc->teams()[resources::screen->viewing_team()]; + const team &viewing_team = rc.teams()[rc.screen().viewing_team()]; if (viewing_team.shrouded(mouseover_hex)) { // Don't show time on shrouded tiles. local_tod = global_tod; @@ -1194,7 +1219,7 @@ static config unit_box_at(const display_context * dc, const map_location& mouseo else if (local_tod.bonus_modified < 0) local_tod_image += "~DARKEN()"; if (preferences::flip_time()) local_tod_image += "~FL(horiz)"; - const gamemap &map = dc->map(); + const gamemap &map = rc.map(); t_translation::t_terrain terrain = map.get_terrain(mouseover_hex); //if (terrain == t_translation::OFF_MAP_USER) @@ -1218,7 +1243,7 @@ static config unit_box_at(const display_context * dc, const map_location& mouseo bg_terrain_image = bg_terrain_image + "~CS(" + color.str() + ")"; - const unit *u = reports::get_visible_unit(dc); + const unit *u = reports::get_visible_unit(rc); std::string unit_image; if (u) unit_image = "~BLIT(" + u->absolute_image() + u->image_mods() + ",35,22)"; @@ -1227,10 +1252,10 @@ static config unit_box_at(const display_context * dc, const map_location& mouseo return image_report(tod_image + bg_terrain_image + unit_image, tooltip.str(), "time_of_day"); } -REPORT_GENERATOR(unit_box, dc) +REPORT_GENERATOR(unit_box, rc) { - map_location mouseover_hex = resources::screen->mouseover_hex(); - return unit_box_at(dc, mouseover_hex); + map_location mouseover_hex = rc.screen().mouseover_hex(); + return unit_box_at(rc, mouseover_hex); } @@ -1243,15 +1268,17 @@ REPORT_GENERATOR(turn, /*dc*/) return text_report(str.str()); } -REPORT_GENERATOR(gold, dc) +REPORT_GENERATOR(gold, rc) { std::ostringstream str; - int viewing_side = resources::screen->viewing_side(); + int viewing_side = rc.screen().viewing_side(); // Suppose the full unit map is applied. - int fake_gold = dc->teams()[viewing_side - 1].gold() - - resources::whiteboard->get_spent_gold_for(viewing_side); + int fake_gold = rc.teams()[viewing_side - 1].gold(); + + if (rc.wb()) + fake_gold -= rc.wb()->get_spent_gold_for(viewing_side); char const *end = naps; - if (viewing_side != resources::screen->playing_side()) { + if (viewing_side != get_playing_side(rc)) { str << span_color(font::GRAY_COLOR); } else if (fake_gold < 0) { @@ -1264,57 +1291,57 @@ REPORT_GENERATOR(gold, dc) return text_report(str.str()); } -REPORT_GENERATOR(villages, dc) +REPORT_GENERATOR(villages, rc) { std::ostringstream str; - int viewing_side = display::get_singleton()->viewing_side(); - const team &viewing_team = dc->teams()[viewing_side - 1]; + int viewing_side = rc.screen().viewing_side(); + const team &viewing_team = rc.teams()[viewing_side - 1]; team_data td = calculate_team_data(viewing_team, viewing_side); str << td.villages << '/'; if (viewing_team.uses_shroud()) { int unshrouded_villages = 0; - BOOST_FOREACH(const map_location &loc, dc->map().villages()) { + BOOST_FOREACH(const map_location &loc, rc.map().villages()) { if (!viewing_team.shrouded(loc)) ++unshrouded_villages; } str << unshrouded_villages; } else { - str << dc->map().villages().size(); + str << rc.map().villages().size(); } - return gray_inactive(str.str()); + return gray_inactive(rc,str.str()); } -REPORT_GENERATOR(num_units, /*dc*/) +REPORT_GENERATOR(num_units, rc) { - return gray_inactive(str_cast(side_units(display::get_singleton()->viewing_side()))); + return gray_inactive(rc, str_cast(side_units(rc.screen().viewing_side()))); } -REPORT_GENERATOR(upkeep, dc) +REPORT_GENERATOR(upkeep, rc) { std::ostringstream str; - int viewing_side = resources::screen->viewing_side(); - const team &viewing_team = dc->teams()[viewing_side - 1]; + int viewing_side = rc.screen().viewing_side(); + const team &viewing_team = rc.teams()[viewing_side - 1]; team_data td = calculate_team_data(viewing_team, viewing_side); str << td.expenses << " (" << td.upkeep << ")"; - return gray_inactive(str.str()); + return gray_inactive(rc,str.str()); } -REPORT_GENERATOR(expenses, dc) +REPORT_GENERATOR(expenses, rc) { - int viewing_side = resources::screen->viewing_side(); - const team &viewing_team = dc->teams()[viewing_side - 1]; - team_data td = calculate_team_data(viewing_team, resources::screen->viewing_side()); - return gray_inactive(str_cast(td.expenses)); + int viewing_side = rc.screen().viewing_side(); + const team &viewing_team = rc.teams()[viewing_side - 1]; + team_data td = calculate_team_data(viewing_team, rc.screen().viewing_side()); + return gray_inactive(rc,str_cast(td.expenses)); } -REPORT_GENERATOR(income, dc) +REPORT_GENERATOR(income, rc) { std::ostringstream str; - int viewing_side = resources::screen->viewing_side(); - const team &viewing_team = dc->teams()[viewing_side - 1]; + int viewing_side = rc.screen().viewing_side(); + const team &viewing_team = rc.teams()[viewing_side - 1]; team_data td = calculate_team_data(viewing_team, viewing_side); char const *end = naps; - if (viewing_side != resources::screen->playing_side()) { + if (viewing_side != get_playing_side(rc)) { if (td.net_income < 0) { td.net_income = - td.net_income; str << span_color(font::GRAY_COLOR); @@ -1344,13 +1371,13 @@ void blit_tced_icon(config &cfg, const std::string &terrain_id, const std::strin } } -REPORT_GENERATOR(terrain_info, dc) +REPORT_GENERATOR(terrain_info, rc) { - const gamemap &map = dc->map(); - map_location mouseover_hex = display::get_singleton()->mouseover_hex(); + const gamemap &map = rc.map(); + map_location mouseover_hex = rc.screen().mouseover_hex(); if (!map.on_board(mouseover_hex)) - mouseover_hex = display::get_singleton()->selected_hex(); + mouseover_hex = rc.screen().selected_hex(); if (!map.on_board(mouseover_hex)) return config(); @@ -1390,12 +1417,12 @@ REPORT_GENERATOR(terrain_info, dc) return cfg; } -REPORT_GENERATOR(terrain, dc) +REPORT_GENERATOR(terrain, rc) { - const gamemap &map = dc->map(); - int viewing_side = resources::screen->viewing_side(); - const team &viewing_team = dc->teams()[viewing_side - 1]; - map_location mouseover_hex = resources::screen->mouseover_hex(); + const gamemap &map = rc.map(); + int viewing_side = rc.screen().viewing_side(); + const team &viewing_team = rc.teams()[viewing_side - 1]; + map_location mouseover_hex = rc.screen().mouseover_hex(); if (!map.on_board(mouseover_hex) || viewing_team.shrouded(mouseover_hex)) return config(); @@ -1428,23 +1455,23 @@ REPORT_GENERATOR(terrain, dc) return text_report(str.str()); } -REPORT_GENERATOR(zoom_level, /*dc*/) +REPORT_GENERATOR(zoom_level, rc) { std::ostringstream text; std::ostringstream tooltip; std::ostringstream help; - text << static_cast(display::get_singleton()->get_zoom_factor() * 100) << "%"; + text << static_cast(get_get_zoom_factor(rc) * 100) << "%"; return text_report(text.str(), tooltip.str(), help.str()); } -REPORT_GENERATOR(position, dc) +REPORT_GENERATOR(position, rc) { - const gamemap &map = dc->map(); - map_location mouseover_hex = resources::screen->mouseover_hex(), - displayed_unit_hex = resources::screen->displayed_unit_hex(), - selected_hex = resources::screen->selected_hex(); + const gamemap &map = rc.map(); + map_location mouseover_hex = rc.screen().mouseover_hex(), + displayed_unit_hex = get_displayed_unit_hex(rc), + selected_hex = rc.screen().selected_hex(); if (!map.on_board(mouseover_hex)) { if (!map.on_board(selected_hex)) @@ -1461,11 +1488,11 @@ REPORT_GENERATOR(position, dc) std::ostringstream str; str << mouseover_hex; - const unit *u = reports::get_visible_unit(dc); - const team &viewing_team = dc->teams()[resources::screen->viewing_team()]; + const unit *u = reports::get_visible_unit(rc); + const team &viewing_team = rc.teams()[rc.screen().viewing_team()]; if (!u || (displayed_unit_hex != mouseover_hex && - displayed_unit_hex != resources::screen->selected_hex()) || + displayed_unit_hex != rc.screen().selected_hex()) || viewing_team.shrouded(mouseover_hex)) return text_report(str.str()); @@ -1481,21 +1508,21 @@ REPORT_GENERATOR(position, dc) return text_report(str.str()); } -REPORT_GENERATOR(side_playing, dc) +REPORT_GENERATOR(side_playing, rc) { - const team &active_team = dc->teams()[resources::screen->playing_team()]; + const team &active_team = rc.teams()[rc.screen().playing_team()]; std::string flag_icon = active_team.flag_icon(); std::string old_rgb = game_config::flag_rgb; - std::string new_rgb = team::get_side_color_index(resources::screen->playing_side()); + std::string new_rgb = team::get_side_color_index(get_playing_side(rc)); std::string mods = "~RC(" + old_rgb + ">" + new_rgb + ")"; if (flag_icon.empty()) flag_icon = game_config::images::flag_icon; return image_report(flag_icon + mods, active_team.current_player()); } -REPORT_GENERATOR(observers, /*dc*/) +REPORT_GENERATOR(observers, rc) { - const std::set &observers = resources::screen->observers(); + const std::set &observers = get_observers(rc); if (observers.empty()) return config(); @@ -1529,7 +1556,7 @@ REPORT_GENERATOR(edit_left_button_function) } */ -REPORT_GENERATOR(report_clock, /*dc*/) +REPORT_GENERATOR(report_clock, /*rc*/) { time_t t = std::time(NULL); struct tm *lt = std::localtime(&t); @@ -1542,17 +1569,17 @@ REPORT_GENERATOR(report_clock, /*dc*/) } -REPORT_GENERATOR(report_countdown, dc) +REPORT_GENERATOR(report_countdown, rc) { - int viewing_side = resources::screen->viewing_side(); - const team &viewing_team = dc->teams()[viewing_side - 1]; + int viewing_side = rc.screen().viewing_side(); + const team &viewing_team = rc.teams()[viewing_side - 1]; int min, sec; if (viewing_team.countdown_time() == 0) - return report_report_clock(dc); + return report_report_clock(rc); std::ostringstream str; sec = viewing_team.countdown_time() / 1000; char const *end = naps; - if (viewing_side != resources::screen->playing_side()) + if (viewing_side != get_playing_side(rc)) str << span_color(font::GRAY_COLOR); else if (sec < 60) str << ""; @@ -1589,16 +1616,16 @@ void reports::register_generator(const std::string &name, reports::generator *g) } } -config reports::generate_report(const std::string &name, const display_context * dc, bool only_static) +config reports::generate_report(const std::string &name, reports::context & rc, bool only_static) { if (!only_static) { dynamic_report_generators::const_iterator i = dynamic_generators.find(name); if (i != dynamic_generators.end()) - return i->second->generate(dc); + return i->second->generate(rc); } static_report_generators::const_iterator j = static_generators.find(name); if (j != static_generators.end()) - return j->second(dc); + return j->second(rc); return config(); } diff --git a/src/reports.hpp b/src/reports.hpp index 02facce43c01..c19575d4097e 100644 --- a/src/reports.hpp +++ b/src/reports.hpp @@ -17,21 +17,61 @@ #include "map_location.hpp" #include "display_context.hpp" +#include + +#include +#include //this module is responsible for outputting textual reports of //various game and unit statistics + +class gamemap; +class team; +class unit_map; + +class display; + +namespace wb { + class manager; +} + +namespace events { + class mouse_handler; +} + namespace reports { +class context +{ +public: + context(const display_context & dc, display & disp, boost::shared_ptr wb, boost::optional mhb) : dc_(dc), disp_(disp), wb_(wb), mhb_(mhb) {} + + const std::vector & teams() { return dc_.teams(); } + const unit_map & units() { return dc_.units(); } + const gamemap & map() { return dc_.map(); } + + const display_context & dc() { return dc_; } + display & screen() { return disp_; } + boost::shared_ptr wb() { return wb_; } + boost::optional mhb() { return mhb_; } + +private: + const display_context & dc_; + display & disp_; + boost::shared_ptr wb_; + boost::optional mhb_; +}; + struct generator { - virtual config generate(const display_context *) = 0; + virtual config generate(context & ct) = 0; virtual ~generator() {} }; void reset_generators(); void register_generator(const std::string &name, generator *); -config generate_report(const std::string &name, const display_context * dc, bool only_static = false); +config generate_report(const std::string &name, context & ct, bool only_static = false); const std::set &report_list(); } diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 1ec7ab0c7c5f..fd3dc6a51093 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -3575,10 +3575,10 @@ namespace { std::string name; lua_report_generator(lua_State *L, const std::string &n) : mState(L), name(n) {} - virtual config generate(const display_context * dc); + virtual config generate(reports::context & rc); }; - config lua_report_generator::generate(const display_context * /*dc*/) + config lua_report_generator::generate(reports::context & /*rc*/) { lua_State *L = mState; config cfg; @@ -3597,7 +3597,8 @@ namespace { static int cfun_theme_item(lua_State *L) { const char *m = lua_tostring(L, lua_upvalueindex(1)); - luaW_pushconfig(L, reports::generate_report(m, resources::disp_context, true)); + reports::context temp_context = reports::context(*resources::disp_context, *resources::screen, resources::whiteboard, resources::controller->get_mouse_handler_base()); + luaW_pushconfig(L, reports::generate_report(m, temp_context , true)); return 1; } From dd8e0159f90a115654de4f0612a6917f0ed00c0f Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Thu, 12 Jun 2014 20:09:05 -0400 Subject: [PATCH 8/9] add tod manager to report::context This allows to remove all remaining resources:: links from the reports generator code, and to finish removing several expensive dependencies from that compilation unit. --- src/display.cpp | 2 +- src/reports.cpp | 25 ++++++++++++------------- src/reports.hpp | 8 ++++++-- src/scripting/lua.cpp | 2 +- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index b500b37371b3..c8f4660ca81e 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -2742,7 +2742,7 @@ void display::refresh_report(std::string const &report_name, const config * new_ mhb = resources::controller->get_mouse_handler_base(); } - reports::context temp_context = reports::context(*dc_, *this, wb_.lock(), mhb); + reports::context temp_context = reports::context(*dc_, *this, *resources::tod_manager, wb_.lock(), mhb); const config generated_cfg = new_cfg ? config() : reports::generate_report(report_name, temp_context); if ( new_cfg == NULL ) diff --git a/src/reports.cpp b/src/reports.cpp index 7469f5dd3853..94de5d4cccb4 100644 --- a/src/reports.cpp +++ b/src/reports.cpp @@ -25,9 +25,8 @@ #include "language.hpp" #include "map.hpp" #include "marked-up_text.hpp" -#include "play_controller.hpp" +#include "mouse_events.hpp" #include "reports.hpp" -#include "resources.hpp" #include "strftime.hpp" #include "team.hpp" #include "text.hpp" @@ -1111,9 +1110,9 @@ REPORT_GENERATOR(tod_stats, rc) const map_location& hex = mouseover_hex.valid() ? mouseover_hex : selected_hex; - const std::vector& schedule = resources::tod_manager->times(hex); + const std::vector& schedule = rc.tod().times(hex); - int current = resources::tod_manager->get_current_time(hex); + int current = rc.tod().get_current_time(hex); int i = 0; BOOST_FOREACH(const time_of_day& tod, schedule) { if (i == current) tooltip << ""; @@ -1135,12 +1134,12 @@ static config time_of_day_at(reports::context & rc, const map_location& mouseove const team &viewing_team = rc.teams()[rc.screen().viewing_team()]; if (viewing_team.shrouded(mouseover_hex)) { // Don't show time on shrouded tiles. - tod = resources::tod_manager->get_time_of_day(); + tod = rc.tod().get_time_of_day(); } else if (viewing_team.fogged(mouseover_hex)) { // Don't show illuminated time on fogged tiles. - tod = resources::tod_manager->get_time_of_day(mouseover_hex); + tod = rc.tod().get_time_of_day(mouseover_hex); } else { - tod = resources::tod_manager->get_illuminated_time_of_day(mouseover_hex); + tod = rc.tod().get_illuminated_time_of_day(mouseover_hex); } int b = tod.lawful_bonus; @@ -1181,16 +1180,16 @@ static config unit_box_at(reports::context & rc, const map_location& mouseover_h { std::ostringstream tooltip; time_of_day local_tod; - time_of_day global_tod = resources::tod_manager->get_time_of_day(); + time_of_day global_tod = rc.tod().get_time_of_day(); const team &viewing_team = rc.teams()[rc.screen().viewing_team()]; if (viewing_team.shrouded(mouseover_hex)) { // Don't show time on shrouded tiles. local_tod = global_tod; } else if (viewing_team.fogged(mouseover_hex)) { // Don't show illuminated time on fogged tiles. - local_tod = resources::tod_manager->get_time_of_day(mouseover_hex); + local_tod = rc.tod().get_time_of_day(mouseover_hex); } else { - local_tod = resources::tod_manager->get_illuminated_time_of_day(mouseover_hex); + local_tod = rc.tod().get_illuminated_time_of_day(mouseover_hex); } int bonus = local_tod.lawful_bonus; @@ -1259,11 +1258,11 @@ REPORT_GENERATOR(unit_box, rc) } -REPORT_GENERATOR(turn, /*dc*/) +REPORT_GENERATOR(turn, rc) { std::ostringstream str; - str << resources::tod_manager->turn(); - int nb = resources::tod_manager->number_of_turns(); + str << rc.tod().turn(); + int nb = rc.tod().number_of_turns(); if (nb != -1) str << '/' << nb; return text_report(str.str()); } diff --git a/src/reports.hpp b/src/reports.hpp index c19575d4097e..597dfb7a2238 100644 --- a/src/reports.hpp +++ b/src/reports.hpp @@ -15,8 +15,10 @@ #ifndef REPORTS_HPP_INCLUDED #define REPORTS_HPP_INCLUDED -#include "map_location.hpp" #include "display_context.hpp" +#include "map_location.hpp" +#include "tod_manager.hpp" + #include #include @@ -44,7 +46,7 @@ namespace reports { class context { public: - context(const display_context & dc, display & disp, boost::shared_ptr wb, boost::optional mhb) : dc_(dc), disp_(disp), wb_(wb), mhb_(mhb) {} + context(const display_context & dc, display & disp, const tod_manager & tod, boost::shared_ptr wb, boost::optional mhb) : dc_(dc), disp_(disp), tod_(tod), wb_(wb), mhb_(mhb) {} const std::vector & teams() { return dc_.teams(); } const unit_map & units() { return dc_.units(); } @@ -52,12 +54,14 @@ class context const display_context & dc() { return dc_; } display & screen() { return disp_; } + const tod_manager & tod() { return tod_; } boost::shared_ptr wb() { return wb_; } boost::optional mhb() { return mhb_; } private: const display_context & dc_; display & disp_; + const tod_manager & tod_; boost::shared_ptr wb_; boost::optional mhb_; }; diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index fd3dc6a51093..c2499bdf557f 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -3597,7 +3597,7 @@ namespace { static int cfun_theme_item(lua_State *L) { const char *m = lua_tostring(L, lua_upvalueindex(1)); - reports::context temp_context = reports::context(*resources::disp_context, *resources::screen, resources::whiteboard, resources::controller->get_mouse_handler_base()); + reports::context temp_context = reports::context(*resources::disp_context, *resources::screen, *resources::tod_manager, resources::whiteboard, resources::controller->get_mouse_handler_base()); luaW_pushconfig(L, reports::generate_report(m, temp_context , true)); return 1; } From 89c22a827982b65d2ccd6c73d758c09d43892730 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Thu, 12 Jun 2014 20:37:18 -0400 Subject: [PATCH 9/9] add virtual fcns to display, remove refs to game_display in reports This commit adds virtual functions covering "observers", "displayed_unit_hex", and "playing_side", in display:: object. This allows to avoid dynamic casts in the reports.cpp file, and to eliminate a header. It avoids the possibility of segfault when using the editor if the reports context isn't set up correctly. --- src/display.hpp | 5 ++++ src/reports.cpp | 63 +++++++++++++++---------------------------------- 2 files changed, 24 insertions(+), 44 deletions(-) diff --git a/src/display.hpp b/src/display.hpp index d468086231cb..3c95d7df816a 100644 --- a/src/display.hpp +++ b/src/display.hpp @@ -196,6 +196,11 @@ class display virtual bool in_game() const { return false; } virtual bool in_editor() const { return false; } + /** Virtual functions shadowed in game_display. These are needed to generate reports easily, without dynamic casting. Hope to factor out eventually. */ + virtual const map_location & displayed_unit_hex() const { return map_location::null_location(); } + virtual int playing_side() const { return -100; } //In this case give an obviously wrong answer to fail fast, since this could actually cause a big bug. */ + virtual const std::set& observers() const { static const std::set fake_obs = std::set (); return fake_obs; } + /** * the dimensions of the display. x and y are width/height. * mapx is the width of the portion of the display which shows the game area. diff --git a/src/reports.cpp b/src/reports.cpp index 94de5d4cccb4..106dc73e17ef 100644 --- a/src/reports.cpp +++ b/src/reports.cpp @@ -18,8 +18,8 @@ #include "attack_prediction.hpp" //#include "editor/editor_controller.hpp" //#include "editor/palette/terrain_palettes.hpp" +#include "display.hpp" #include "font.hpp" -#include "game_display.hpp" #include "game_preferences.hpp" #include "gettext.hpp" #include "language.hpp" @@ -115,34 +115,9 @@ static char const *naps = ""; namespace reports { -static map_location get_displayed_unit_hex(reports::context & rc) { - return dynamic_cast(rc.screen()).displayed_unit_hex(); -} - -static int get_playing_side(reports::context & rc) { - if (rc.wb()) { - return dynamic_cast(rc.screen()).playing_side(); - } - return -100; //this is a hacky run_time fix, the dynamic cast is not safe for the editor. I think it was only working before because resources::screen is null. -} - -static const std::set get_observers (reports::context & rc) { - if (rc.wb()) { - return dynamic_cast(rc.screen()).observers(); - } - return std::set (); -} - -static double get_get_zoom_factor (reports::context & rc) { - if (rc.wb()) { - return dynamic_cast(rc.screen()).get_zoom_factor(); - } - return -100; -} - static const unit *get_visible_unit(reports::context & rc) { - return rc.dc().get_visible_unit(get_displayed_unit_hex(rc), + return rc.dc().get_visible_unit(rc.screen().displayed_unit_hex(), rc.teams()[rc.screen().viewing_team()], rc.screen().show_everything()); } @@ -158,7 +133,7 @@ static const unit *get_selected_unit(reports::context & rc) static config gray_inactive(reports::context & rc, const std::string &str) { - if ( rc.screen().viewing_side() == get_playing_side(rc) ) + if ( rc.screen().viewing_side() == rc.screen().playing_side() ) return text_report(str); return text_report(span_color(font::GRAY_COLOR) + str + naps); @@ -334,7 +309,7 @@ static config unit_status(reports::context & rc, const unit* u) { if (!u) return config(); config res; - map_location displayed_unit_hex = get_displayed_unit_hex(rc); + map_location displayed_unit_hex = rc.screen().displayed_unit_hex(); if (rc.map().on_board(displayed_unit_hex) && u->invisible(displayed_unit_hex)) { add_status(res, "misc/invisible.png", N_("invisible: "), N_("This unit is invisible. It cannot be seen or attacked by enemy units.")); @@ -370,7 +345,7 @@ static config unit_alignment(reports::context & rc, const unit* u) std::ostringstream str, tooltip; const std::string align = unit_type::alignment_description(u->alignment(), u->gender()); const std::string align_id = lexical_cast(u->alignment()); - int cm = combat_modifier(get_displayed_unit_hex(rc), u->alignment(), + int cm = combat_modifier(rc.screen().displayed_unit_hex(), u->alignment(), u->is_fearless()); SDL_Color color = font::weapon_color; @@ -452,7 +427,7 @@ static config unit_hp(reports::context& rc, const unit* u) std::set resistances_table; bool att_def_diff = false; - map_location displayed_unit_hex = get_displayed_unit_hex(rc); + map_location displayed_unit_hex = rc.screen().displayed_unit_hex(); BOOST_FOREACH(const utils::string_map::value_type &resist, u->get_base_resistances()) { std::ostringstream line; @@ -582,7 +557,7 @@ static config unit_defense(reports::context & rc, const unit* u, const map_locat REPORT_GENERATOR(unit_defense,rc) { const unit *u = reports::get_visible_unit(rc); - const map_location& displayed_unit_hex = get_displayed_unit_hex(rc); + const map_location& displayed_unit_hex = rc.screen().displayed_unit_hex(); return unit_defense(rc, u, displayed_unit_hex); } REPORT_GENERATOR(selected_unit_defense, rc) @@ -618,7 +593,7 @@ static config unit_moves(reports::context & rc, const unit* u) if (!u) return config(); std::ostringstream str, tooltip; double movement_frac = 1.0; - if (u->side() == get_playing_side(rc)) { + if (u->side() == rc.screen().playing_side()) { movement_frac = double(u->movement_left()) / std::max(1, u->total_movement()); if (movement_frac > 1.0) movement_frac = 1.0; @@ -684,7 +659,7 @@ static int attack_info(reports::context & rc, const attack_type &at, config &res { std::ostringstream str, tooltip; - at.set_specials_context(displayed_unit_hex, u.side() == get_playing_side(rc)); + at.set_specials_context(displayed_unit_hex, u.side() == rc.screen().playing_side()); int base_damage = at.damage(); int specials_damage = at.modified_damage(false); int damage_multiplier = 100; @@ -1014,7 +989,7 @@ static config unit_weapons(reports::context & rc, const unit *attacker, const ma static config unit_weapons(reports::context & rc, const unit *u) { if (!u || u->attacks().empty()) return config(); - map_location displayed_unit_hex = get_displayed_unit_hex(rc); + map_location displayed_unit_hex = rc.screen().displayed_unit_hex(); config res; //TODO enable after the string frezze is lifted @@ -1045,7 +1020,7 @@ REPORT_GENERATOR(highlighted_unit_weapons, rc) if (!u) return config(); if (!sec_u || u == sec_u) return unit_weapons(rc, sec_u); - map_location highlighted_hex = get_displayed_unit_hex(rc); + map_location highlighted_hex = rc.screen().displayed_unit_hex(); map_location attack_loc; if (rc.mhb()) attack_loc = rc.mhb()->current_unit_attacks_from(highlighted_hex); @@ -1063,7 +1038,7 @@ REPORT_GENERATOR(selected_unit_weapons, rc) if (!u) return config(); if (!sec_u || u == sec_u) return unit_weapons(rc, u); - map_location highlighted_hex = get_displayed_unit_hex(rc); + map_location highlighted_hex = rc.screen().displayed_unit_hex(); map_location attack_loc; if (rc.mhb()) attack_loc = rc.mhb()->current_unit_attacks_from(highlighted_hex); @@ -1277,7 +1252,7 @@ REPORT_GENERATOR(gold, rc) if (rc.wb()) fake_gold -= rc.wb()->get_spent_gold_for(viewing_side); char const *end = naps; - if (viewing_side != get_playing_side(rc)) { + if (viewing_side != rc.screen().playing_side()) { str << span_color(font::GRAY_COLOR); } else if (fake_gold < 0) { @@ -1340,7 +1315,7 @@ REPORT_GENERATOR(income, rc) const team &viewing_team = rc.teams()[viewing_side - 1]; team_data td = calculate_team_data(viewing_team, viewing_side); char const *end = naps; - if (viewing_side != get_playing_side(rc)) { + if (viewing_side != rc.screen().playing_side()) { if (td.net_income < 0) { td.net_income = - td.net_income; str << span_color(font::GRAY_COLOR); @@ -1460,7 +1435,7 @@ REPORT_GENERATOR(zoom_level, rc) std::ostringstream tooltip; std::ostringstream help; - text << static_cast(get_get_zoom_factor(rc) * 100) << "%"; + text << static_cast(rc.screen().get_zoom_factor() * 100) << "%"; return text_report(text.str(), tooltip.str(), help.str()); } @@ -1469,7 +1444,7 @@ REPORT_GENERATOR(position, rc) { const gamemap &map = rc.map(); map_location mouseover_hex = rc.screen().mouseover_hex(), - displayed_unit_hex = get_displayed_unit_hex(rc), + displayed_unit_hex = rc.screen().displayed_unit_hex(), selected_hex = rc.screen().selected_hex(); if (!map.on_board(mouseover_hex)) { @@ -1512,7 +1487,7 @@ REPORT_GENERATOR(side_playing, rc) const team &active_team = rc.teams()[rc.screen().playing_team()]; std::string flag_icon = active_team.flag_icon(); std::string old_rgb = game_config::flag_rgb; - std::string new_rgb = team::get_side_color_index(get_playing_side(rc)); + std::string new_rgb = team::get_side_color_index(rc.screen().playing_side()); std::string mods = "~RC(" + old_rgb + ">" + new_rgb + ")"; if (flag_icon.empty()) flag_icon = game_config::images::flag_icon; @@ -1521,7 +1496,7 @@ REPORT_GENERATOR(side_playing, rc) REPORT_GENERATOR(observers, rc) { - const std::set &observers = get_observers(rc); + const std::set &observers = rc.screen().observers(); if (observers.empty()) return config(); @@ -1578,7 +1553,7 @@ REPORT_GENERATOR(report_countdown, rc) std::ostringstream str; sec = viewing_team.countdown_time() / 1000; char const *end = naps; - if (viewing_side != get_playing_side(rc)) + if (viewing_side != rc.screen().playing_side()) str << span_color(font::GRAY_COLOR); else if (sec < 60) str << "";