Skip to content

Commit

Permalink
Merge pull request #201 from gfgtdf/gamestate_refactor
Browse files Browse the repository at this point in the history
change savefile format & wesnothd protocoll

The main intentions of this patch are:
1) To remove redundant information in savefiles
2) To fix mp/sp bugs: "modification [event]s wont be reaplied if one advances from a mp game (also if it was reloaded from sp that was reloaded in sp)"
3) To fix "start-of-scenario mp saves are broken"
4) Clear up playcampaign.cpp s code

This patch changes the wesnothd server protocoll causing incompability with oder version regarding networked mutiplayer. We now use the savefile fomat when sending new games to the server.

Also between some commits in this pr some mp related things will not work.
  • Loading branch information
gfgtdf committed Jun 15, 2014
2 parents 9645fc4 + 976d990 commit fba6cb5
Show file tree
Hide file tree
Showing 36 changed files with 967 additions and 598 deletions.
52 changes: 50 additions & 2 deletions src/carryover.cpp
Expand Up @@ -20,6 +20,7 @@
#include "team.hpp"
#include "gamestatus.hpp"
#include <boost/foreach.hpp>
#include <cassert>

carryover::carryover(const config& side)
: add_(side["add"].to_bool())
Expand All @@ -33,6 +34,12 @@ carryover::carryover(const config& side)
{
BOOST_FOREACH(const config& u, side.child_range("unit")){
recall_list_.push_back(u);
config& u_back = recall_list_.back();
u_back.remove_attribute("side");
u_back.remove_attribute("goto_x");
u_back.remove_attribute("goto_y");
u_back.remove_attribute("x");
u_back.remove_attribute("y");
}
}

Expand Down Expand Up @@ -122,6 +129,10 @@ const std::string carryover::to_string(){
return side;
}

void carryover::set_gold(int gold){
gold_ = gold;
}

void carryover::to_config(config& cfg){
config& side = cfg.add_child("side");
side["save_id"] = save_id_;
Expand All @@ -135,7 +146,7 @@ void carryover::to_config(config& cfg){
side.add_child("unit", u_cfg);
}

carryover_info::carryover_info(const config& cfg)
carryover_info::carryover_info(const config& cfg, bool from_snpashot)
: carryover_sides_()
, end_level_()
, variables_(cfg.child_or_empty("variables"))
Expand All @@ -144,9 +155,27 @@ carryover_info::carryover_info(const config& cfg)
, next_scenario_(cfg["next_scenario"])
, next_underlying_unit_id_(cfg["next_underlying_unit_id"].to_int(0))
{
int turns_left = cfg["turns"].to_int() - cfg["turn_at"].to_int();
end_level_.read(cfg.child_or_empty("end_level_data"));
BOOST_FOREACH(const config& side, cfg.child_range("side")){
BOOST_FOREACH(const config& side, cfg.child_range("side"))
{
if(side["lost"].to_bool(false) || !side["persistent"].to_bool(true))
{
//this shouldnt happen outside a snpshot.
assert(from_snpashot);
continue;
}
this->carryover_sides_.push_back(carryover(side));
if(from_snpashot)
{
//adjust gold
int finishing_bonus_per_turn = cfg["map_villages_num"] * side["village_gold"] + side["income"];
int finishing_bonus = std::max(0, finishing_bonus_per_turn * turns_left);
if(end_level_.gold_bonus)
{
carryover_sides_.back().set_gold(div100rounded((finishing_bonus + side["gold"]) * end_level_.carryover_percentage));
}
}
}

wml_menu_items_.set_menu_items(cfg);
Expand Down Expand Up @@ -296,3 +325,22 @@ carryover* carryover_info::get_side(std::string save_id){
}
return NULL;
}


void carryover_info::merge_old_carryover(const carryover_info& old_carryover)
{
BOOST_FOREACH(const carryover & old_side, old_carryover.carryover_sides_)
{
std::vector<carryover>::iterator iside = std::find_if(
carryover_sides_.begin(),
carryover_sides_.end(),
save_id_equals(old_side.get_save_id())
);
//add the side if don't already have it.
if(iside == carryover_sides_.end())
{
this->carryover_sides_.push_back(old_side);
}
}
}

8 changes: 6 additions & 2 deletions src/carryover.hpp
Expand Up @@ -38,6 +38,7 @@ class carryover{
void initialize_team(config& side_cfg);
const std::string to_string();
void to_config(config& cfg);
void set_gold(int gold);
private:
bool add_;
std::string color_;
Expand Down Expand Up @@ -65,8 +66,9 @@ class carryover_info{
, next_scenario_()
, next_underlying_unit_id_()
{}
// Turns config from a loaded savegame into carryover_info
explicit carryover_info(const config& cfg);
/// Turns config from a loaded savegame into carryover_info
/// @param from_snapshot true if cfg is a [snapshot], false if cfg is [carryover_sides(_start)]
explicit carryover_info(const config& cfg, bool from_snapshot = false);

carryover* get_side(std::string save_id);
std::vector<carryover>& get_all_sides();
Expand Down Expand Up @@ -94,6 +96,8 @@ class carryover_info{
const std::string& next_scenario() const { return next_scenario_; }

const config to_config();

void merge_old_carryover(const carryover_info& old_carryover);
private:
std::vector<carryover> carryover_sides_;
end_level_data end_level_;
Expand Down
5 changes: 5 additions & 0 deletions src/game_board.cpp
Expand Up @@ -78,7 +78,9 @@ void game_board::all_survivors_to_recall() {
if (teams_[un.side() - 1].persistent()) {
un.new_turn();
un.new_scenario();
#if 0
teams_[un.side() - 1].recall_list().push_back(un);
#endif
}
}
}
Expand Down Expand Up @@ -263,6 +265,9 @@ void game_board::write_config(config & cfg) const {

//write the map
cfg["map_data"] = map_->write();

//Used by the carryover calculations.
cfg["map_villages_num"] = map_->villages().size();
}

temporary_unit_placer::temporary_unit_placer(unit_map& m, const map_location& loc, unit& u)
Expand Down
11 changes: 5 additions & 6 deletions src/game_controller.cpp
Expand Up @@ -589,10 +589,10 @@ bool game_controller::load_game()
recorder.start_replay();
recorder.set_skip(false);

LOG_CONFIG << "has snapshot: " << (state_.snapshot.child("side") ? "yes" : "no") << "\n";
LOG_CONFIG << "has is middle game savefile: " << (state_.is_mid_game_save() ? "yes" : "no") << "\n";

if (!state_.snapshot.child("side")) {
// No snapshot; this is a start-of-scenario
if (!state_.is_mid_game_save()) {
//this is a start-of-scenario
if (load.show_replay()) {
// There won't be any turns to replay, but the
// user gets to watch the intro sequence again ...
Expand All @@ -616,7 +616,7 @@ bool game_controller::load_game()
}

if(state_.classification().campaign_type == game_classification::MULTIPLAYER) {
BOOST_FOREACH(config &side, state_.snapshot.child_range("side"))
BOOST_FOREACH(config &side, state_.get_starting_pos().child_range("side"))
{
if (side["controller"] == "network")
side["controller"] = "human";
Expand All @@ -627,7 +627,7 @@ bool game_controller::load_game()
}

if (load.cancel_orders()) {
BOOST_FOREACH(config &side, state_.snapshot.child_range("side"))
BOOST_FOREACH(config &side, state_.get_starting_pos().child_range("side"))
{
if (side["controller"] != "human") continue;
BOOST_FOREACH(config &unit, side.child_range("unit"))
Expand Down Expand Up @@ -790,7 +790,6 @@ bool game_controller::new_campaign()
}
}

state_.carryover_sides_start["difficulty"] = difficulties[difficulty];
state_.classification().difficulty = difficulties[difficulty];
}

Expand Down
7 changes: 7 additions & 0 deletions src/game_end_exceptions.hpp
Expand Up @@ -228,6 +228,13 @@ struct end_level_data
void write(config& cfg) const;

void read(const config& cfg);

config to_config() const
{
config r;
write(r);
return r;
}
};

#endif /* ! GAME_END_EXCEPTIONS_HPP_INCLUDED */
11 changes: 11 additions & 0 deletions src/gamestatus.cpp
Expand Up @@ -700,6 +700,17 @@ void convert_old_saves(config& cfg){
carryover_sides_start["next_underlying_unit_id"] = cfg["next_underlying_unit_id"];
}
}

if(config& snapshot = cfg.child("snapshot"))
{
//make [end_level] -> [end_level_data] since its alo called [end_level_data] in the carryover.
if(config& end_level = cfg.child("end_level") )
{
snapshot.add_child("end_level_data", end_level);
snapshot.remove_child("end_level",0);
}
}

//1.12-1.13 end
LOG_RG<<"cfg after conversion "<<cfg<<"\n";
}
9 changes: 2 additions & 7 deletions src/mp_game_settings.cpp
Expand Up @@ -51,8 +51,7 @@ mp_game_settings::mp_game_settings() :
share_view(false),
share_maps(false),
saved_game(false),
options(),
scenario_data()
options()

{ reset(); }

Expand Down Expand Up @@ -86,8 +85,7 @@ mp_game_settings::mp_game_settings(const config& cfg) :
share_view(false),
share_maps(false),
saved_game(false),
options(),
scenario_data()
options()
{
set_from_config(cfg);
}
Expand Down Expand Up @@ -123,7 +121,6 @@ mp_game_settings::mp_game_settings(const mp_game_settings& settings)
, share_maps(settings.share_maps)
, saved_game(settings.saved_game)
, options(settings.options)
, scenario_data(settings.scenario_data)
{
}

Expand Down Expand Up @@ -184,8 +181,6 @@ void mp_game_settings::reset()
mp_countdown=false;
use_map_settings = random_start_time = fog_game = shroud_game = allow_observers = shuffle_sides = share_view = share_maps = false;
options.clear();

scenario_data.clear();
}

config mp_game_settings::to_config() const
Expand Down
6 changes: 0 additions & 6 deletions src/mp_game_settings.hpp
Expand Up @@ -65,12 +65,6 @@ struct mp_game_settings : public savegame::savegame_config
bool saved_game;

config options;

/**
* If the game is to be randomly generated, the map generator
* will create the scenario data in this variable
*/
config scenario_data;
};

#endif

0 comments on commit fba6cb5

Please sign in to comment.