Skip to content

Commit

Permalink
fixup segfaulting editor, favoring display_context over game_board
Browse files Browse the repository at this point in the history
Currently, the editor does not generate game_board objects, and
leaves the pointer resources::gameboard null. This commit makes
map_location, reports, and tod_manager, three things used in
editor mode, use the display_context pointer instead of the
game_board pointer. We also move a function to display_context,
which finds a const unit * to a visible unit at a hex.
  • Loading branch information
cbeck88 committed Jun 11, 2014
1 parent 126358f commit da552fc
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 15 deletions.
1 change: 1 addition & 0 deletions projectfiles/CodeBlocks-SCons/wesnoth.cbp
Expand Up @@ -191,6 +191,7 @@
<Unit filename="../../src/dialogs.hpp" />
<Unit filename="../../src/display.cpp" />
<Unit filename="../../src/display.hpp" />
<Unit filename="../../src/display_context.cpp" />
<Unit filename="../../src/display_context.hpp" />
<Unit filename="../../src/editor/action/action_base.hpp" />
<Unit filename="../../src/editor/action/action.cpp" />
Expand Down
1 change: 1 addition & 0 deletions projectfiles/CodeBlocks/wesnoth.cbp
Expand Up @@ -229,6 +229,7 @@
<Unit filename="..\..\src\dialogs.hpp" />
<Unit filename="..\..\src\display.cpp" />
<Unit filename="..\..\src\display.hpp" />
<Unit filename="..\..\src\display_context.cpp" />
<Unit filename="..\..\src\display_context.hpp" />
<Unit filename="..\..\src\editor\action\action.cpp" />
<Unit filename="..\..\src\editor\action\action.hpp" />
Expand Down
4 changes: 4 additions & 0 deletions projectfiles/VC9/wesnoth.vcproj
Expand Up @@ -20052,6 +20052,10 @@
RelativePath="..\..\src\display.hpp"
>
</File>
<File
RelativePath="..\..\src\display_context.cpp"
>
</File>
<File
RelativePath="..\..\src\display_context.hpp"
>
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -916,6 +916,7 @@ set(libwesnoth-game_STAT_SRC
construct_dialog.cpp
cursor.cpp
display.cpp
display_context.cpp
events.cpp
font.cpp
format_time_summary.cpp
Expand Down
1 change: 1 addition & 0 deletions src/SConscript
Expand Up @@ -75,6 +75,7 @@ libwesnoth_sources = Split("""
construct_dialog.cpp
cursor.cpp
display.cpp
display_context.cpp
events.cpp
format_time_summary.cpp
generic_event.cpp
Expand Down
31 changes: 31 additions & 0 deletions src/display_context.cpp
@@ -0,0 +1,31 @@
/*
Copyright (C) 2014 by Chris Beck <render787@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#include "display_context.hpp"

#include "map.hpp"
#include "map_location.hpp"
#include "team.hpp"
#include "unit.hpp"
#include "unit_map.hpp"

const unit * display_context::get_visible_unit(const map_location & loc, const team &current_team, bool see_all) const
{
if (!map().on_board(loc)) return NULL;
const unit_map::const_iterator u = units().find(loc);
if (!u.valid() || !u->is_visible_to_team(current_team, map(), see_all)) {
return NULL;
}
return &*u;
}
5 changes: 5 additions & 0 deletions src/display_context.hpp
Expand Up @@ -28,12 +28,17 @@ class team;
class gamemap;
class unit_map;

class unit;
struct map_location;

class display_context {
public:
virtual const std::vector<team> & teams() const = 0;
virtual const gamemap & map() const = 0;
virtual const unit_map & units() const = 0;

const unit * get_visible_unit(const map_location &loc, const team &current_team, bool see_all = false) const;

virtual ~display_context() {}
};

Expand Down
4 changes: 2 additions & 2 deletions src/map_location.cpp
Expand Up @@ -24,8 +24,8 @@
#include "map_location.hpp"

#include "config.hpp"
#include "display_context.hpp"
#include "formula_string_utils.hpp"
#include "game_board.hpp"
#include "map.hpp"
#include "resources.hpp"
#include "gettext.hpp"
Expand Down Expand Up @@ -318,7 +318,7 @@ std::vector<map_location> parse_location_range(const std::string &x, const std::
std::vector<map_location> res;
const std::vector<std::string> xvals = utils::split(x);
const std::vector<std::string> yvals = utils::split(y);
const gamemap *map = & resources::gameboard->map();
const gamemap *map = & resources::disp_context->map();
assert(map);
int xmin = 1, xmax = map->w(), ymin = 1, ymax = map->h();
if (with_border) {
Expand Down
25 changes: 13 additions & 12 deletions src/reports.cpp
Expand Up @@ -16,6 +16,7 @@

#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"
Expand Down Expand Up @@ -123,14 +124,14 @@ namespace reports {

static const unit *get_visible_unit()
{
return resources::gameboard->get_visible_unit(resources::screen->displayed_unit_hex(),
return resources::disp_context->get_visible_unit(resources::screen->displayed_unit_hex(),
(*resources::teams)[resources::screen->viewing_team()],
resources::screen->show_everything());
}

static const unit *get_selected_unit()
{
return resources::gameboard->get_visible_unit(resources::screen->selected_hex(),
return resources::disp_context->get_visible_unit(resources::screen->selected_hex(),
(*resources::teams)[resources::screen->viewing_team()],
resources::screen->show_everything());
}
Expand Down Expand Up @@ -318,7 +319,7 @@ static config unit_status(const unit* u)
if (!u) return report();
config res;
map_location displayed_unit_hex = resources::screen->displayed_unit_hex();
if (resources::gameboard->map().on_board(displayed_unit_hex) && u->invisible(displayed_unit_hex)) {
if (resources::disp_context->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."));
}
Expand Down Expand Up @@ -528,8 +529,8 @@ static config unit_defense(const unit* u, const map_location& displayed_unit_hex
}

std::ostringstream str, tooltip;
const gamemap &map = resources::gameboard->map();
if(!resources::gameboard->map().on_board(displayed_unit_hex)) {
const gamemap &map = resources::disp_context->map();
if(!resources::disp_context->map().on_board(displayed_unit_hex)) {
return report();
}

Expand Down Expand Up @@ -617,7 +618,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::gameboard->map().get_terrain_info(terrain);
const terrain_type& info = resources::disp_context->map().get_terrain_info(terrain);

if (info.union_type().size() == 1 && info.union_type()[0] == info.number() && info.is_nonnull()) {

Expand Down Expand Up @@ -1199,7 +1200,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::gameboard->map();
const gamemap &map = resources::disp_context->map();
t_translation::t_terrain terrain = map.get_terrain(mouseover_hex);

//if (terrain == t_translation::OFF_MAP_USER)
Expand Down Expand Up @@ -1278,13 +1279,13 @@ REPORT_GENERATOR(villages)
str << td.villages << '/';
if (viewing_team.uses_shroud()) {
int unshrouded_villages = 0;
BOOST_FOREACH(const map_location &loc, resources::gameboard->map().villages()) {
BOOST_FOREACH(const map_location &loc, resources::disp_context->map().villages()) {
if (!viewing_team.shrouded(loc))
++unshrouded_villages;
}
str << unshrouded_villages;
} else {
str << resources::gameboard->map().villages().size();
str << resources::disp_context->map().villages().size();
}
return gray_inactive(str.str());
}
Expand Down Expand Up @@ -1351,7 +1352,7 @@ void blit_tced_icon(config &cfg, const std::string &terrain_id, const std::strin

REPORT_GENERATOR(terrain_info)
{
const gamemap &map = resources::gameboard->map();
const gamemap &map = resources::disp_context->map();
map_location mouseover_hex = display::get_singleton()->mouseover_hex();

if (!map.on_board(mouseover_hex))
Expand Down Expand Up @@ -1397,7 +1398,7 @@ REPORT_GENERATOR(terrain_info)

REPORT_GENERATOR(terrain)
{
const gamemap &map = resources::gameboard->map();
const gamemap &map = resources::disp_context->map();
int viewing_side = resources::screen->viewing_side();
const team &viewing_team = (*resources::teams)[viewing_side - 1];
map_location mouseover_hex = resources::screen->mouseover_hex();
Expand Down Expand Up @@ -1446,7 +1447,7 @@ REPORT_GENERATOR(zoom_level)

REPORT_GENERATOR(position)
{
const gamemap &map = resources::gameboard->map();
const gamemap &map = resources::disp_context->map();
map_location mouseover_hex = resources::screen->mouseover_hex(),
displayed_unit_hex = resources::screen->displayed_unit_hex(),
selected_hex = resources::screen->selected_hex();
Expand Down
3 changes: 2 additions & 1 deletion src/tod_manager.cpp
Expand Up @@ -15,6 +15,7 @@
#include "tod_manager.hpp"
#include "wml_exception.hpp"
#include "gettext.hpp"
#include "display_context.hpp"
#include "formula_string_utils.hpp"
#include "gamestatus.hpp"
#include "log.hpp"
Expand Down Expand Up @@ -194,7 +195,7 @@ const time_of_day& tod_manager::get_time_of_day(const map_location& loc, int n_t

const time_of_day tod_manager::get_illuminated_time_of_day(const map_location& loc, int for_turn) const
{
const gamemap& map = resources::gameboard->map();
const gamemap& map = resources::disp_context->map();
const unit_map& units = *resources::units;

// get ToD ignoring illumination
Expand Down

0 comments on commit da552fc

Please sign in to comment.