Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from cbeck88/add_game_board_struct_alternate
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
Showing
37 changed files
with
461 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ¤t_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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ¤t_team, bool see_all = false); //TODO: can this not return a pointer? | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.