Skip to content

Commit

Permalink
give global fcn's (find/get)_visible_unit a home in game_board
Browse files Browse the repository at this point in the history
Unit tested and playtested after moving the functions decalarations,
and definitions, adding links to play_controller::game_board from
resources.?pp, and executing find and replace commands:

git grep -lz 'find_visible_unit(' | xargs -0 perl -i'' -pE "s/find_visible_unit\(/resources::gameboard->find_visible_unit\(/g"
git grep -lz 'get_visible_unit(' | xargs -0 perl -i'' -pE "s/get_visible_unit\(/resources::gameboard->get_visible_unit\(/g"
  • Loading branch information
cbeck88 committed Jun 1, 2014
1 parent d557ea5 commit df74d89
Show file tree
Hide file tree
Showing 21 changed files with 103 additions and 93 deletions.
3 changes: 2 additions & 1 deletion src/actions/create.cpp
Expand Up @@ -25,6 +25,7 @@

#include "../config.hpp"
#include "../config_assign.hpp"
#include "../game_board.hpp"
#include "../game_display.hpp"
#include "../game_events/pump.hpp"
#include "../game_preferences.hpp"
Expand Down Expand Up @@ -315,7 +316,7 @@ bool can_recruit_on(const map_location& leader_loc, const map_location& recruit_
if ( view_team.shrouded(recruit_loc) )
return false;

if ( get_visible_unit(recruit_loc, view_team) != NULL )
if ( resources::gameboard->get_visible_unit(recruit_loc, view_team) != NULL )
return false;

castle_cost_calculator calc(map, view_team);
Expand Down
2 changes: 1 addition & 1 deletion src/actions/move.cpp
Expand Up @@ -768,7 +768,7 @@ namespace { // Private helpers for move_unit()
if ( !is_replay_ ) {
// Avoiding stopping on a (known) unit.
route_iterator min_end = start == begin_ ? start : start + 1;
while ( end != min_end && get_visible_unit(*(end-1), *current_team_) )
while ( end != min_end && resources::gameboard->get_visible_unit(*(end-1), *current_team_) )
// Backtrack.
--end;
}
Expand Down
2 changes: 1 addition & 1 deletion src/actions/vision.cpp
Expand Up @@ -284,7 +284,7 @@ bool shroud_clearer::clear_loc(team &tm, const map_location &loc,
// Check for units?
if ( result && check_units && loc != event_non_loc ) {
// Uncovered a unit?
unit_map::const_iterator sight_it = find_visible_unit(loc, tm);
unit_map::const_iterator sight_it = resources::gameboard->find_visible_unit(loc, tm);
if ( sight_it.valid() ) {
record_sighting(*sight_it, loc, viewer_id, view_loc);

Expand Down
3 changes: 2 additions & 1 deletion src/ai/contexts.cpp
Expand Up @@ -32,6 +32,7 @@
#include "../formula.hpp"
#include "../formula_function.hpp"
#include "../formula_fwd.hpp"
#include "../game_board.hpp"
#include "../game_display.hpp"
#include "../log.hpp"
#include "../map.hpp"
Expand Down Expand Up @@ -430,7 +431,7 @@ void readonly_context_impl::calculate_moves(const unit_map& units, std::map<map_
continue;
}

if(src != dst && (find_visible_unit(dst, current_team()) == resources::units->end()) ) {
if(src != dst && (resources::gameboard->find_visible_unit(dst, current_team()) == resources::units->end()) ) {
srcdst.insert(std::pair<map_location,map_location>(src,dst));
dstsrc.insert(std::pair<map_location,map_location>(dst,src));
}
Expand Down
5 changes: 3 additions & 2 deletions src/ai/testing/ca.cpp
Expand Up @@ -23,6 +23,7 @@
#include "../composite/engine.hpp"
#include "../composite/rca.hpp"
#include "../composite/stage.hpp"
#include "../../game_board.hpp"
#include "../../gamestatus.hpp"
#include "../../log.hpp"
#include "../../map.hpp"
Expand Down Expand Up @@ -935,7 +936,7 @@ void get_villages_phase::execute()
if(leader != units_.end() && leader->get_location() == i->second) {
leader_move = *i;
} else {
if (find_visible_unit(i->first, current_team()) == units_.end()) {
if (resources::gameboard->find_visible_unit(i->first, current_team()) == units_.end()) {
move_result_ptr move_res = execute_move_action(i->second,i->first,true);
if (!move_res->is_ok()) {
return;
Expand All @@ -961,7 +962,7 @@ void get_villages_phase::execute()
}

if(leader_move.second.valid()) {
if((find_visible_unit(leader_move.first , current_team()) == units_.end())
if((resources::gameboard->find_visible_unit(leader_move.first , current_team()) == units_.end())
&& resources::game_map->is_village(leader_move.first)) {
move_result_ptr move_res = execute_move_action(leader_move.second,leader_move.first,true);
if (!move_res->is_ok()) {
Expand Down
18 changes: 18 additions & 0 deletions src/game_board.cpp
Expand Up @@ -41,6 +41,24 @@ void game_board::set_all_units_user_end_turn() {
}
}

unit_map::iterator game_board::find_visible_unit(const map_location &loc,
const team& current_team, bool see_all)
{
if (!map_.on_board(loc)) return units_.end();
unit_map::iterator u = units_.find(loc);
if (!u.valid() || !u->is_visible_to_team(current_team, see_all))
return units_.end();
return u;
}

unit* game_board::get_visible_unit(const map_location &loc,
const team &current_team, bool see_all)
{
unit_map::iterator ui = find_visible_unit(loc, current_team, see_all);
if (ui == units_.end()) return NULL;
return &*ui;
}

void game_board::write_config(config & cfg) const {
for(std::vector<team>::const_iterator t = teams_.begin(); t != teams_.end(); ++t) {
int side_num = t - teams_.begin() + 1;
Expand Down
4 changes: 4 additions & 0 deletions src/game_board.hpp
Expand Up @@ -58,6 +58,10 @@ class game_board {
void end_turn(int pnum);
void set_all_units_user_end_turn();

// Global accessor from unit.hpp

unit_map::iterator find_visible_unit(const map_location &loc, const team& current_team, bool see_all = false);
unit* get_visible_unit(const map_location &loc, const team &current_team, bool see_all = false); //TODO: can this not return a pointer?
};

#endif
13 changes: 7 additions & 6 deletions src/game_display.cpp
Expand Up @@ -19,6 +19,7 @@

#include "global.hpp"

#include "game_board.hpp"
#include "game_display.hpp"
#include "gettext.hpp"
#include "wesconfig.h"
Expand Down Expand Up @@ -163,12 +164,12 @@ void game_display::highlight_hex(map_location hex)
{
wb::future_map future; /**< Lasts for whole method. */

const unit *u = get_visible_unit(hex, (*teams_)[viewing_team()], !viewpoint_);
const unit *u = resources::gameboard->get_visible_unit(hex, (*teams_)[viewing_team()], !viewpoint_);
if (u) {
displayedUnitHex_ = hex;
invalidate_unit();
} else {
u = get_visible_unit(mouseoverHex_, (*teams_)[viewing_team()], !viewpoint_);
u = resources::gameboard->get_visible_unit(mouseoverHex_, (*teams_)[viewing_team()], !viewpoint_);
if (u) {
// mouse moved from unit hex to non-unit hex
if (units_->count(selectedHex_)) {
Expand All @@ -190,7 +191,7 @@ void game_display::display_unit_hex(map_location hex)

wb::future_map future; /**< Lasts for whole method. */

const unit *u = get_visible_unit(hex, (*teams_)[viewing_team()], !viewpoint_);
const unit *u = resources::gameboard->get_visible_unit(hex, (*teams_)[viewing_team()], !viewpoint_);
if (u) {
displayedUnitHex_ = hex;
invalidate_unit();
Expand Down Expand Up @@ -277,7 +278,7 @@ void game_display::draw_hex(const map_location& loc)

if(on_map && loc == mouseoverHex_) {
tdrawing_layer hex_top_layer = LAYER_MOUSEOVER_BOTTOM;
const unit *u = get_visible_unit(loc, (*teams_)[viewing_team()] );
const unit *u = resources::gameboard->get_visible_unit(loc, (*teams_)[viewing_team()] );
if( u != NULL ) {
hex_top_layer = LAYER_MOUSEOVER_TOP;
}
Expand Down Expand Up @@ -459,8 +460,8 @@ void game_display::draw_movement_info(const map_location& loc)
// When out-of-turn, it's still interesting to check out the terrain defs of the selected unit
else if (selectedHex_.valid() && loc == mouseoverHex_)
{
const unit_map::const_iterator selectedUnit = find_visible_unit(selectedHex_,resources::teams->at(currentTeam_));
const unit_map::const_iterator mouseoveredUnit = find_visible_unit(mouseoverHex_,resources::teams->at(currentTeam_));
const unit_map::const_iterator selectedUnit = resources::gameboard->find_visible_unit(selectedHex_,resources::teams->at(currentTeam_));
const unit_map::const_iterator mouseoveredUnit = resources::gameboard->find_visible_unit(mouseoverHex_,resources::teams->at(currentTeam_));
if(selectedUnit != units_->end() && mouseoveredUnit == units_->end()) {
// Display the def% of this terrain
int def = 100 - selectedUnit->defense_modifier(get_map().get_terrain(loc));
Expand Down
4 changes: 2 additions & 2 deletions src/menu_events.cpp
Expand Up @@ -930,12 +930,12 @@ unit_map::iterator menu_handler::current_unit()
{
const mouse_handler& mousehandler = resources::controller->get_mouse_handler_base();

unit_map::iterator res = find_visible_unit(mousehandler.get_last_hex(),
unit_map::iterator res = resources::gameboard->find_visible_unit(mousehandler.get_last_hex(),
teams_[gui_->viewing_team()]);
if(res != units_.end()) {
return res;
} else {
return find_visible_unit(mousehandler.get_selected_hex(),
return resources::gameboard->find_visible_unit(mousehandler.get_selected_hex(),
teams_[gui_->viewing_team()]);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/mouse_events.cpp
Expand Up @@ -325,7 +325,7 @@ unit_map::iterator mouse_handler::selected_unit()

unit_map::iterator mouse_handler::find_unit(const map_location& hex)
{
unit_map::iterator it = find_visible_unit(hex, viewing_team());
unit_map::iterator it = resources::gameboard->find_visible_unit(hex, viewing_team());
if (it.valid())
return it;
else
Expand All @@ -334,7 +334,7 @@ unit_map::iterator mouse_handler::find_unit(const map_location& hex)

unit_map::const_iterator mouse_handler::find_unit(const map_location& hex) const
{
return find_visible_unit(hex, viewing_team());
return resources::gameboard->find_visible_unit(hex, viewing_team());
}

map_location mouse_handler::current_unit_attacks_from(const map_location& loc) const
Expand Down
7 changes: 4 additions & 3 deletions src/pathfind/pathfind.cpp
Expand Up @@ -23,6 +23,7 @@
#include "pathfind/pathfind.hpp"
#include "pathfind/teleport.hpp"

#include "game_board.hpp"
#include "game_display.hpp"
#include "gettext.hpp"
#include "log.hpp"
Expand Down Expand Up @@ -140,7 +141,7 @@ bool enemy_zoc(team const &current_team, map_location const &loc,
get_adjacent_tiles(loc,locs);
for (int i = 0; i != 6; ++i)
{
const unit *u = get_visible_unit(locs[i], viewing_team, see_all);
const unit *u = resources::gameboard->get_visible_unit(locs[i], viewing_team, see_all);
if ( u && current_team.is_enemy(u->side()) && u->emits_zoc() )
return true;
}
Expand Down Expand Up @@ -388,7 +389,7 @@ static void find_routes(

if ( current_team ) {
// Account for enemy units.
const unit *v = get_visible_unit(next_hex, *viewing_team, see_all);
const unit *v = resources::gameboard->get_visible_unit(next_hex, *viewing_team, see_all);
if ( v && current_team->is_enemy(v->side()) ) {
// Cannot enter enemy hexes.
if ( edges != NULL )
Expand Down Expand Up @@ -719,7 +720,7 @@ double shortest_path_calculator::cost(const map_location& loc, const double so_f
int other_unit_subcost = 0;
if (!ignore_unit_) {
const unit *other_unit =
get_visible_unit(loc, viewing_team_, see_all_);
resources::gameboard->get_visible_unit(loc, viewing_team_, see_all_);

// We can't traverse visible enemy and we also prefer empty hexes
// (less blocking in multi-turn moves and better when exploring fog,
Expand Down
2 changes: 2 additions & 0 deletions src/play_controller.cpp
Expand Up @@ -72,6 +72,7 @@ static lg::log_domain log_enginerefac("enginerefac");
static void clear_resources()
{
resources::controller = NULL;
resources::gameboard = NULL;
resources::gamedata = NULL;
resources::game_map = NULL;
resources::persist = NULL;
Expand Down Expand Up @@ -135,6 +136,7 @@ play_controller::play_controller(const config& level, game_state& state_of_game,
scope_()
{
resources::controller = this;
resources::gameboard = &gameboard_;
resources::gamedata = &gamedata_;
resources::game_map = &gameboard_.map_;
resources::persist = &persist_;
Expand Down

0 comments on commit df74d89

Please sign in to comment.