Skip to content

Commit

Permalink
DEMO: partially working SP campaigns in MP
Browse files Browse the repository at this point in the history
  • Loading branch information
andrius-sil committed May 10, 2013
1 parent 8c864cd commit ab739ab
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/game_controller.cpp
Expand Up @@ -798,7 +798,7 @@ bool game_controller::play_multiplayer()

const mp::controller cntr = mp::CNTR_LOCAL;

mp::start_local_game(disp(), game_config(), cntr);
mp::start_local_game(cntr, this);

} else if((res >= 0 && res <= 2) || res == 4) {
std::string host;
Expand All @@ -810,7 +810,7 @@ bool game_controller::play_multiplayer()
host = multiplayer_server_;
multiplayer_server_ = "";
}
mp::start_client(disp(), game_config(), host);
mp::start_client(host, this);
}

} catch(game::mp_server_error& e) {
Expand Down Expand Up @@ -901,7 +901,7 @@ void game_controller::show_preferences()

void game_controller::set_unit_data()
{
loadscreen::start_stage("load unit types");
//loadscreen::start_stage("load unit types");
if (config &units = game_config_.child("units")) {
unit_types.set_config(units);
}
Expand All @@ -919,7 +919,7 @@ void game_controller::load_game_cfg(const bool force)
&& old_defines_map_ == cache_.get_preproc_map())
return; // game_config already holds requested config in memory
old_defines_map_ = cache_.get_preproc_map();
loadscreen::global_loadscreen_manager loadscreen_manager(disp().video());
//loadscreen::global_loadscreen_manager loadscreen_manager(disp().video());
cursor::setter cur(cursor::WAIT);
// The loadscreen will erase the titlescreen
// NOTE: even without loadscreen, needed after MP lobby
Expand All @@ -930,9 +930,9 @@ void game_controller::load_game_cfg(const bool force)
* Then handle terrains so that they are last loaded from data/
* 2nd everything in userdata
**/
loadscreen::start_stage("verify cache");
//loadscreen::start_stage("verify cache");
data_tree_checksum();
loadscreen::start_stage("create cache");
//loadscreen::start_stage("create cache");

// start transaction so macros are shared
game_config::config_cache_transaction main_transaction;
Expand Down
3 changes: 2 additions & 1 deletion src/game_controller.hpp
Expand Up @@ -71,6 +71,8 @@ class game_controller : public game_controller_abstract

void show_preferences();

void load_game_cfg(const bool force=false);

void launch_game(RELOAD_GAME_DATA reload=RELOAD_DATA);
void play_replay();

Expand All @@ -84,7 +86,6 @@ class game_controller : public game_controller_abstract
void operator=(const game_controller&);

bool init_config(const bool force);
void load_game_cfg(const bool force=false);
void set_unit_data();

void mark_completed_campaigns(std::vector<config>& campaigns);
Expand Down
20 changes: 11 additions & 9 deletions src/multiplayer.cpp
Expand Up @@ -517,7 +517,7 @@ static bool enter_connect_mode(game_display& disp, const config& game_config,
return true;
}

static void enter_create_mode(game_display& disp, const config& game_config, mp::chat& chat, config& gamelist, mp::controller default_controller, bool local_players_only)
static void enter_create_mode(game_display& disp, const config& game_config, mp::chat& chat, config& gamelist, mp::controller default_controller, bool local_players_only)
{
DBG_MP << "entering create mode" << std::endl;

Expand Down Expand Up @@ -686,16 +686,18 @@ static void enter_lobby_mode(game_display& disp, const config& game_config, mp::

namespace mp {

void start_local_game(game_display& disp, const config& game_config,
mp::controller default_controller)
game_controller* gcontr = NULL;

void start_local_game(mp::controller default_controller, game_controller* gcontroller)
{
DBG_MP << "starting local game" << std::endl;
gcontr = gcontroller;
const rand_rng::set_random_generator generator_setter(&recorder);
mp::chat chat;
config gamelist;
playmp_controller::set_replay_last_turn(0);
preferences::set_message_private(false);
enter_create_mode(disp, game_config, chat, gamelist, default_controller, true);
enter_create_mode(gcontr->disp(), gcontr->game_config(), chat, gamelist, default_controller, true);
}

void start_local_game_commandline(game_display& disp, const config& game_config,
Expand Down Expand Up @@ -823,25 +825,25 @@ void start_local_game_commandline(game_display& disp, const config& game_config,
recorder.clear();
}

void start_client(game_display& disp, const config& game_config,
const std::string& host)
void start_client(const std::string& host, game_controller* gcontroller)
{
DBG_MP << "starting client" << std::endl;
gcontr = gcontroller;
const rand_rng::set_random_generator generator_setter(&recorder);
const network::manager net_manager(1,1);

mp::chat chat;
config gamelist;
server_type type = open_connection(disp, host);
server_type type = open_connection(gcontr->disp(), host);

switch(type) {
case WESNOTHD_SERVER:
enter_lobby_mode(disp, game_config, chat, gamelist);
enter_lobby_mode(gcontr->disp(), gcontr->game_config(), chat, gamelist);
break;
case SIMPLE_SERVER:
playmp_controller::set_replay_last_turn(0);
preferences::set_message_private(false);
enter_wait_mode(disp, game_config, chat, gamelist, false);
enter_wait_mode(gcontr->disp(), gcontr->game_config(), chat, gamelist, false);
break;
case ABORT_SERVER:
break;
Expand Down
10 changes: 6 additions & 4 deletions src/multiplayer.hpp
Expand Up @@ -15,6 +15,8 @@
#define MULTIPLAYER_HPP_INCLUDED

#include "multiplayer_ui.hpp"

#include "game_controller.hpp"
#include "commandline_options.hpp"

class config;
Expand All @@ -25,6 +27,8 @@ namespace mp {
// max. length of a player name
const size_t max_login_size = 20;

extern game_controller* gcontr;

/*
* This is the main entry points of multiplayer mode.
*/
Expand All @@ -35,8 +39,7 @@ const size_t max_login_size = 20;
* @param game_config The global, top-level WML configuration for the game
* @param default_controller The default controller type
*/
void start_local_game(game_display& disp, const config& game_config,
mp::controller default_controller);
void start_local_game(mp::controller default_controller, game_controller* gcontroller);

/** Starts a multiplayer game in single-user mode.
*
Expand All @@ -52,8 +55,7 @@ void start_local_game_commandline(game_display& disp, const config& game_config,
* @param game_config The global, top-level WML configuration for the game
* @param host The host to connect to.
*/
void start_client(game_display& disp, const config& game_config,
const std::string& host);
void start_client(const std::string& host, game_controller* gcontroller);


}
Expand Down
72 changes: 70 additions & 2 deletions src/multiplayer_create.cpp
Expand Up @@ -27,6 +27,9 @@
#include "map.hpp"
#include "map_exception.hpp"
#include "map_create.hpp"
#include "multiplayer.hpp"
#include "gui/dialogs/campaign_difficulty.hpp"
#include "gui/dialogs/campaign_selection.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/dialogs/mp_create_game_choose_mods.hpp"
#include "gui/dialogs/mp_create_game_set_password.hpp"
Expand Down Expand Up @@ -103,6 +106,7 @@ create::create(game_display& disp, const config &cfg, chat& c, config& gamelist,
generator_settings_(disp.video(), _("Settings...")),
password_button_(disp.video(), _("Set Password...")),
choose_mods_(disp.video(), _("Modifications...")),
choose_campaign_(disp.video(), _("Campaign")),
era_combo_(disp, std::vector<std::string>()),
vision_combo_(disp, std::vector<std::string>()),
name_entry_(disp.video(), 32),
Expand Down Expand Up @@ -449,6 +453,59 @@ void create::process_event()
}
}

config campaign;
if(choose_campaign_.pressed()) {
// select a campaign
const config::const_child_itors &ci = game_config().child_range("campaign");
std::vector<config> campaigns(ci.first, ci.second);
if(campaigns.begin() == campaigns.end()) {
gui2::show_error_message(disp().video(), _("No campaigns are available.\n"));
return;
}
int campaign_num = -1;
gui2::tcampaign_selection dlg(campaigns);
try {
dlg.show(disp().video());
} catch(twml_exception& e) {
e.show(disp());
return;
}
if(dlg.get_retval() != gui2::twindow::OK) {
return;
}
campaign_num = dlg.get_choice();
campaign.append(campaigns[campaign_num]);

// select difficulty level
const std::string difficulty_descriptions = campaign["difficulty_descriptions"];
std::vector<std::string> difficulty_options = utils::split(difficulty_descriptions, ';');
const std::vector<std::string> difficulties = utils::split(campaign["difficulties"]);

std::string dif_def;
if(difficulties.empty() == false) {
int difficulty = 0;
if(difficulty_options.size() != difficulties.size()) {
difficulty_options.resize(difficulties.size());
std::copy(difficulties.begin(),difficulties.end(),difficulty_options.begin());
}

gui2::tcampaign_difficulty dlg(difficulty_options);
dlg.show(disp().video());

if(dlg.selected_index() == -1) {
return;
}
difficulty = dlg.selected_index();
dif_def = difficulties[difficulty];
}

// reload configs
game_config::scoped_preproc_define difficulty_define(dif_def);
game_config::scoped_preproc_define campaign_define(campaign["define"].str());
game_config::scoped_preproc_define multiplayer("MULTIPLAYER");
mp::gcontr->load_game_cfg();
}

// Turns per game
const int cur_turns = turns_slider_.value();

Expand Down Expand Up @@ -530,7 +587,7 @@ void create::process_event()
synchronize_selections();
}

bool map_changed = map_selection_ != maps_menu_.selection();
bool map_changed = (map_selection_ != maps_menu_.selection()) || !campaign.empty();
map_selection_ = maps_menu_.selection();

if (map_changed) {
Expand Down Expand Up @@ -567,7 +624,14 @@ void create::process_event()

if (levels.first != levels.second)
{
const config &level = *levels.first;
config level;
if(!campaign.empty()) { // should be in different place
std::string first_scenario = campaign["first_scenario"];
level.append(game_config().find_child("scenario", "id", first_scenario));
}
else {
level.append(*levels.first);
}
parameters_.scenario_data = level;
std::string map_data = level["map_data"];

Expand Down Expand Up @@ -774,6 +838,7 @@ void create::hide_children(bool hide)

era_combo_.hide(hide);
choose_mods_.hide(hide);
choose_campaign_.hide(hide);
password_button_.hide(hide);
vision_combo_.hide(hide);
name_entry_.hide(hide);
Expand Down Expand Up @@ -857,6 +922,9 @@ void create::layout_children(const SDL_Rect& rect)
ypos += era_combo_.height() + border_size;
choose_mods_.set_location(xpos, ypos);
ypos += choose_mods_.height() + border_size;
ypos += 20;
choose_campaign_.set_location(xpos, ypos);
ypos += choose_campaign_.height() + border_size;
if(!local_players_only_) {
password_button_.set_location(xpos, ypos);
ypos += password_button_.height() + border_size;
Expand Down
1 change: 1 addition & 0 deletions src/multiplayer_create.hpp
Expand Up @@ -103,6 +103,7 @@ class create : public mp::ui
gui::button generator_settings_;
gui::button password_button_;
gui::button choose_mods_;
gui::button choose_campaign_;

gui::combo era_combo_;
gui::combo vision_combo_;
Expand Down
2 changes: 1 addition & 1 deletion src/multiplayer_ui.cpp
Expand Up @@ -382,7 +382,7 @@ SDL_Rect ui::client_area() const

const config& ui::game_config() const
{
return game_config_;
return mp::gcontr->game_config();
}

void ui::draw_contents()
Expand Down

0 comments on commit ab739ab

Please sign in to comment.