Skip to content

Commit

Permalink
Merge pull request #180 from cbeck88/add_game_board_struct_alternate
Browse files Browse the repository at this point in the history
This is a refactor to introduce an object encapsulating the unit map, the game map, and the list of teams. Introducing this object permits us to move a substantial amount of code out of the play_controller object, and also to give a home to some helper functions in unit.?pp that previously sat in the global namespace. It also allows us to simplify the construction of some of the clients of play_controller.

This refactor is ongoing WIP. The goals are

(1) better organize the architecture of the engine, to make saving and reloading easier.
(2) Facilitate the introduction of an improved pathfinding mechanism, which will need to sit between most of the other engine modules and the unit map / game map.
(3) Refactoring clarifies what the existing code is doing, therefore it may help us to find bugs in the current system, which may be fixed independently of the refactor in 1.12.
  • Loading branch information
cbeck88 committed Jun 1, 2014
2 parents 9ddce31 + 2d2385b commit 096cb97
Show file tree
Hide file tree
Showing 37 changed files with 461 additions and 327 deletions.
1 change: 1 addition & 0 deletions projectfiles/CodeBlocks-SCons/wesnoth.cbp
Expand Up @@ -285,6 +285,7 @@
<Unit filename="../../src/formula_tokenizer.cpp" />
<Unit filename="../../src/formula_tokenizer.hpp" />
<Unit filename="../../src/game.cpp" />
<Unit filename="../../src/game_board.cpp" />
<Unit filename="../../src/game_config.cpp" />
<Unit filename="../../src/game_config.hpp" />
<Unit filename="../../src/game_config_manager.cpp" />
Expand Down
1 change: 1 addition & 0 deletions projectfiles/CodeBlocks/wesnoth.cbp
Expand Up @@ -322,6 +322,7 @@
<Unit filename="..\..\src\formula_tokenizer.cpp" />
<Unit filename="..\..\src\formula_tokenizer.hpp" />
<Unit filename="..\..\src\game.cpp" />
<Unit filename="..\..\src\game_board.cpp" />
<Unit filename="..\..\src\game_config.cpp" />
<Unit filename="..\..\src\game_config.hpp" />
<Unit filename="..\..\src\game_config_manager.cpp" />
Expand Down
2 changes: 2 additions & 0 deletions projectfiles/CodeLite/wesnoth.project
Expand Up @@ -4285,6 +4285,8 @@
<File Name="../../src/tooltips.cpp"/>
<File Name="../../src/race.hpp"/>
<File Name="../../src/halo.cpp"/>
<File Name="../../src/game_board.cpp"/>
<File Name="../../src/game_board.hpp"/>
<File Name="../../src/play_controller.cpp"/>
<File Name="../../src/multiplayer_connect.cpp"/>
<File Name="../../src/game_preferences_display.cpp"/>
Expand Down
8 changes: 8 additions & 0 deletions projectfiles/VC9/wesnoth.vcproj
Expand Up @@ -20060,6 +20060,14 @@
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\src\game_board.cpp"
>
</File>
<File
RelativePath="..\..\src\game_board.hpp"
>
</File>
<File
RelativePath="..\..\src\game_config.hpp"
>
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -737,6 +737,7 @@ set(wesnoth-main_SRC
formula_function.cpp
formula_string_utils.cpp
formula_tokenizer.cpp
game_board.cpp
game_config_manager.cpp
game_controller.cpp
game_display.cpp
Expand Down
1 change: 1 addition & 0 deletions src/SConscript
Expand Up @@ -269,6 +269,7 @@ wesnoth_sources = Split("""
formula_function.cpp
formula_string_utils.cpp
formula_tokenizer.cpp
game_board.cpp
game_config_manager.cpp
game_controller.cpp
game_display.cpp
Expand Down
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
92 changes: 92 additions & 0 deletions src/game_board.cpp
@@ -0,0 +1,92 @@
/*
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 "config.hpp"
#include "game_board.hpp"
#include "unit.hpp"

#include <boost/foreach.hpp>


void game_board::new_turn(int player_num) {
BOOST_FOREACH (unit & i, units_) {
if (i.side() == player_num) {
i.new_turn();
}
}
}

void game_board::end_turn(int player_num) {
BOOST_FOREACH (unit & i, units_) {
if (i.side() == player_num) {
i.end_turn();
}
}
}

void game_board::set_all_units_user_end_turn() {
BOOST_FOREACH (unit & i, units_) {
i.set_user_end_turn(true);
}
}

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;

config& side = cfg.add_child("side");
t->write(side);
side["no_leader"] = true;
side["side"] = str_cast(side_num);

//current units
{
BOOST_FOREACH(const unit & i, units_) {
if (i.side() == side_num) {
config& u = side.add_child("unit");
i.get_location().write(u);
i.write(u);
}
}
}
//recall list
{
BOOST_FOREACH(const unit & j, t->recall_list()) {
config& u = side.add_child("unit");
j.write(u);
}
}
}

//write the map
cfg["map_data"] = map_.write();
}
72 changes: 72 additions & 0 deletions src/game_board.hpp
@@ -0,0 +1,72 @@
/*
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.
*/

#ifndef GAME_BOARD_HPP_INCLUDED
#define GAME_BOARD_HPP_INCLUDED

#include "global.hpp"

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

#include <vector>

class config;

namespace events {
class mouse_handler;
}

class game_board {

std::vector<team> teams_;

gamemap map_;
unit_map units_;

//TODO: Remove these when we have refactored enough to make it possible.
friend class play_controller;
friend class replay_controller;
friend class playsingle_controller;
friend class playmp_controller;
friend class events::mouse_handler;

public:

// Constructors and const accessors

game_board(const config & game_config, const config & level) : teams_(), map_(game_config, level), units_() {}

const std::vector<team> & teams() const { return teams_; }
const gamemap & map() const { return map_; }
const unit_map & units() const { return units_; }

// Saving

void write_config(config & cfg) const;

// Manipulators from play_controller

void new_turn(int pnum);
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

0 comments on commit 096cb97

Please sign in to comment.