/
playcampaign.cpp
442 lines (371 loc) · 14.2 KB
/
playcampaign.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
Copyright (C) 2003-2005 by David White <dave@whitevine.net>
Copyright (C) 2005 - 2014 by Philippe Plantier <ayin@anathas.org>
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.
*/
/**
* @file
* Controls setup, play, (auto)save and replay of campaigns.
*/
#include "global.hpp"
#include "playcampaign.hpp"
#include "carryover.hpp"
#include "game_errors.hpp"
#include "game_preferences.hpp"
#include "generators/map_create.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "gui/widgets/window.hpp"
#include "persist_manager.hpp"
#include "playmp_controller.hpp"
#include "replay_controller.hpp"
#include "log.hpp"
#include "map_exception.hpp"
#include "mp_game_utils.hpp"
#include "multiplayer.hpp"
#include "connect_engine.hpp"
#include "dialogs.hpp"
#include "gettext.hpp"
#include "resources.hpp"
#include "savegame.hpp"
#include "saved_game.hpp"
#include "sound.hpp"
#include "wml_exception.hpp"
#include "formula_string_utils.hpp"
#include <boost/foreach.hpp>
#define LOG_G LOG_STREAM(info, lg::general)
static lg::log_domain log_engine("engine");
#define LOG_NG LOG_STREAM(info, log_engine)
#define ERR_NG LOG_STREAM(err, log_engine)
static lg::log_domain log_enginerefac("enginerefac");
#define LOG_RG LOG_STREAM(info, log_enginerefac)
static void show_carryover_message(saved_game& gamestate, playsingle_controller& playcontroller, display& disp, const end_level_data& end_level, const LEVEL_RESULT res){
bool has_next_scenario = !resources::gamedata->next_scenario().empty() &&
resources::gamedata->next_scenario() != "null";
//maybe this can be the case for scenario that only contain a story and end during the prestart event ?
if(resources::teams->size() < 1){
return;
}
std::ostringstream report;
std::string title;
bool obs = playcontroller.is_observer();
if (obs) {
title = _("Scenario Report");
} else if (res == VICTORY) {
title = _("Victory");
report << "<b>" << _("You have emerged victorious!") << "</b>\n\n";
} else {
title = _("Defeat");
report << _("You have been defeated!") << "\n";
}
std::vector<team> teams = playcontroller.get_teams_const();
int persistent_teams = 0;
BOOST_FOREACH(const team &t, teams) {
if (t.persistent()){
++persistent_teams;
}
}
if (persistent_teams > 0 && ((has_next_scenario && end_level.proceed_to_next_level)||
gamestate.classification().campaign_type == game_classification::TEST))
{
gamemap map = playcontroller.get_map_const();
// NOTE: this function uses game_config::village_income/game_config::base_income which is teh same for all teams
// the function that actualy does the carryover (carryover.cpp) uses team.base_income() / team.village_gold() since 1.13
// which can be different for every team
int finishing_bonus_per_turn =
map.villages().size() * game_config::village_income +
game_config::base_income;
tod_manager tod = playcontroller.get_tod_manager_const();
int turns_left = std::max<int>(0, tod.number_of_turns() - tod.turn());
int finishing_bonus = (end_level.gold_bonus && turns_left > -1) ?
finishing_bonus_per_turn * turns_left : 0;
BOOST_FOREACH(const team &t, teams)
{
if (!t.persistent() || t.lost() || !t.is_human())
{
continue;
}
int carryover_gold = div100rounded((t.gold() + finishing_bonus) * end_level.carryover_percentage);
if (persistent_teams > 1) {
report << "\n<b>" << t.current_player() << "</b>\n";
}
playcontroller.report_victory(report, carryover_gold, t.gold(), finishing_bonus_per_turn, turns_left, finishing_bonus);
}
}
if (end_level.transient.carryover_report) {
gui2::show_transient_message(disp.video(), title, report.str(), "", true);
}
}
LEVEL_RESULT play_replay(display& disp, saved_game& gamestate, const config& game_config,
CVideo& video, bool is_unit_test)
{
// 'starting_pos' will contain the position we start the game from.
// this call also might expand [scenario] in case thatt there is no replay_start
const config& starting_pos = gamestate.get_replay_starting_pos();
try {
// Preserve old label eg. replay
if (gamestate.classification().label.empty())
gamestate.classification().label = starting_pos["name"].str();
//if (gamestate.abbrev.empty())
// gamestate.abbrev = (*scenario)["abbrev"];
LEVEL_RESULT res = play_replay_level(game_config, video, gamestate, is_unit_test);
recorder.clear();
gamestate.replay_data.clear();
return res;
} catch(game::load_game_failed& e) {
if (is_unit_test) {
std::cerr << std::string(_("The game could not be loaded: ")) + " (game::load_game_failed) " + e.message << std::endl;
return DEFEAT;
} else {
gui2::show_error_message(disp.video(), _("The game could not be loaded: ") + e.message);
}
} catch(game::game_error& e) {
if (is_unit_test) {
std::cerr << std::string(_("Error while playing the game: ")) + " (game::game_error) " + e.message << std::endl;
return DEFEAT;
} else {
gui2::show_error_message(disp.video(), std::string(_("Error while playing the game: ")) + e.message);
}
} catch(incorrect_map_format_error& e) {
if (is_unit_test) {
std::cerr << std::string(_("The game map could not be loaded: ")) + " (incorrect_map_format_error) " + e.message << std::endl;
return DEFEAT;
} else {
gui2::show_error_message(disp.video(), std::string(_("The game map could not be loaded: ")) + e.message);
}
} catch(twml_exception& e) {
if (is_unit_test) {
std::cerr << std::string("WML Exception: ") + e.user_message << std::endl;
std::cerr << std::string("Dev Message: ") + e.dev_message << std::endl;
return DEFEAT;
} else {
e.show(disp);
}
}
return NONE;
}
static LEVEL_RESULT playsingle_scenario(const config& game_config,
display& disp, saved_game& state_of_game,
const config::const_child_itors &story,
bool skip_replay, end_level_data &end_level)
{
const int ticks = SDL_GetTicks();
LOG_NG << "creating objects... " << (SDL_GetTicks() - ticks) << "\n";
playsingle_controller playcontroller(state_of_game.get_starting_pos(), state_of_game, ticks, game_config, disp.video(), skip_replay);
LOG_NG << "created objects... " << (SDL_GetTicks() - playcontroller.get_ticks()) << "\n";
LEVEL_RESULT res = playcontroller.play_scenario(story, skip_replay);
end_level = playcontroller.get_end_level_data_const();
if (res != QUIT)
{
//if we are loading from linger mode then we already did this.
if(res != SKIP_TO_LINGER)
{
show_carryover_message(state_of_game, playcontroller, disp, end_level, res);
}
if(!disp.video().faked())
{
try {
playcontroller.maybe_linger();
} catch(end_level_exception& e) {
if (e.result == QUIT) {
return QUIT;
}
}
}
}
state_of_game.set_snapshot(playcontroller.to_config());
return res;
}
static LEVEL_RESULT playmp_scenario(const config& game_config,
display& disp, saved_game& state_of_game,
const config::const_child_itors &story, bool skip_replay,
bool blindfold_replay, io_type_t& io_type, end_level_data &end_level)
{
const int ticks = SDL_GetTicks();
playmp_controller playcontroller(state_of_game.get_starting_pos(), state_of_game, ticks,
game_config, disp.video(), skip_replay, blindfold_replay, io_type == IO_SERVER);
LEVEL_RESULT res = playcontroller.play_scenario(story, skip_replay);
end_level = playcontroller.get_end_level_data_const();
//Check if the player started as mp client and changed to host
if (io_type == IO_CLIENT && playcontroller.is_host())
io_type = IO_SERVER;
if (res != QUIT)
{
if(res != OBSERVER_END && res != SKIP_TO_LINGER)
{
//We need to call this before linger because it prints the defeated/victory message.
//(we want to see that message before entering the linger mode)
show_carryover_message(state_of_game, playcontroller, disp, end_level, res);
}
if(!disp.video().faked())
{
try {
playcontroller.maybe_linger();
} catch(end_level_exception& e) {
if (e.result == QUIT) {
return QUIT;
}
}
}
}
state_of_game.set_snapshot(playcontroller.to_config());
return res;
}
LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,
const config& game_config, io_type_t io_type, bool skip_replay,
bool network_game, bool blindfold_replay, bool is_unit_test)
{
gamestate.expand_scenario();
game_classification::CAMPAIGN_TYPE game_type = gamestate.classification().campaign_type;
while(gamestate.valid())
{
LEVEL_RESULT res = VICTORY;
end_level_data end_level;
try {
gamestate.expand_random_scenario();
//In case this an mp scenario reloaded by sp this was not already done yet.
//We don't need to expand [option]s because [variables] are persitent
gamestate.expand_mp_events();
sound::empty_playlist();
gamestate.expand_carryover();
config::const_child_itors story = gamestate.get_starting_pos().child_range("story");
#if !defined(ALWAYS_USE_MP_CONTROLLER)
if (game_type != game_classification::MULTIPLAYER) {
res = playsingle_scenario(game_config, disp, gamestate, story, skip_replay, end_level);
} else
#endif
{
res = playmp_scenario(game_config, disp, gamestate, story, skip_replay, blindfold_replay, io_type, end_level);
}
} catch(game::load_game_failed& e) {
gui2::show_error_message(disp.video(), _("The game could not be loaded: ") + e.message);
return QUIT;
} catch(game::game_error& e) {
gui2::show_error_message(disp.video(), _("Error while playing the game: ") + e.message);
return QUIT;
} catch(incorrect_map_format_error& e) {
gui2::show_error_message(disp.video(), std::string(_("The game map could not be loaded: ")) + e.message);
return QUIT;
} catch(config::error& e) {
std::cerr << "caught config::error...\n";
gui2::show_error_message(disp.video(), _("Error while reading the WML: ") + e.message);
return QUIT;
} catch(twml_exception& e) {
e.show(disp);
return QUIT;
}
if (is_unit_test) {
return res;
}
//in this case we might have skipped state.set_snapshot which means we cannot do gamestate.convert_to_start_save();
if(res == QUIT)
{
return res;
}
// Save-management options fire on game end.
// This means: (a) we have a victory, or
// (b) we're multiplayer live, in which
// case defeat is also game end. Someday,
// if MP campaigns ever work again, we might
// need to change this test.
if (res == VICTORY || (game_type == game_classification::MULTIPLAYER && res == DEFEAT)) {
if (preferences::delete_saves())
savegame::clean_saves(gamestate.classification().label);
if (preferences::save_replays() && end_level.replay_save) {
savegame::replay_savegame save(gamestate, preferences::save_compression_format());
save.save_game_automatic(disp.video(), true);
}
}
gamestate.convert_to_start_save();
recorder.clear();
// On DEFEAT, QUIT, or OBSERVER_END, we're done now
//If there is no next scenario we're done now.
if(!end_level.proceed_to_next_level || gamestate.get_scenario_id().empty())
{
return res;
}
else if(res == OBSERVER_END)
{
const int dlg_res = gui2::show_message(disp.video(), _("Game Over"),
_("This scenario has ended. Do you want to continue the campaign?"),
gui2::tmessage::yes_no_buttons);
if(dlg_res == gui2::twindow::CANCEL) {
return res;
}
}
if (io_type == IO_CLIENT) {
// Opens mp::connect dialog to get a new gamestate.
mp::ui::result wait_res = mp::goto_mp_wait(gamestate, disp,
game_config, res == OBSERVER_END);
if (wait_res == mp::ui::QUIT) {
return QUIT;
}
//The host should send the complete savegame now that also contains the carryvoer sides start.
} else {
// Retrieve next scenario data.
gamestate.expand_scenario();
if (gamestate.valid()) {
//note that although starting_pos is const it might be changed by gamestate.some_non_const_operation() .
const config& starting_pos = gamestate.get_starting_pos();
gamestate.mp_settings().num_turns = starting_pos["turns"].to_int(-1);
gamestate.mp_settings().saved_game = false;
gamestate.mp_settings().use_map_settings
= starting_pos["force_lock_settings"].to_bool(game_type != game_classification::MULTIPLAYER);
ng::connect_engine_ptr
connect_engine(new ng::connect_engine(gamestate,
!network_game, false));
if (starting_pos["allow_new_game"].to_bool(gamestate.mp_settings().show_connect)
|| (game_config::debug && network::nconnections() == 0 && game_type == game_classification::MULTIPLAYER)) {
// Opens mp::connect dialog to allow users to
// make an adjustments for scenario.
// TODO: Fix this so that it works when network::nconnections() > 0 as well.
mp::ui::result connect_res = mp::goto_mp_connect(disp,
*connect_engine, game_config, gamestate.mp_settings().name);
if (connect_res == mp::ui::QUIT) {
return QUIT;
}
} else {
// Start the next scenario immediately.
connect_engine->
start_game(ng::connect_engine::FORCE_IMPORT_USERS);
}
}
}
if(gamestate.valid()) {
// Update the label
gamestate.update_label();
// If this isn't the last scenario, then save the game
if(end_level.prescenario_save) {
// For multiplayer, we want the save
// to contain the starting position.
// For campaigns however, this is the
// start-of-scenario save and the
// starting position needs to be empty,
// to force a reload of the scenario config.
savegame::scenariostart_savegame save(gamestate, preferences::save_compression_format());
save.save_game_automatic(disp.video());
}
}
}
if (!gamestate.get_scenario_id().empty()) {
std::string message = _("Unknown scenario: '$scenario|'");
utils::string_map symbols;
symbols["scenario"] = gamestate.get_scenario_id();
message = utils::interpolate_variables_into_string(message, &symbols);
gui2::show_error_message(disp.video(), message);
return QUIT;
}
if (game_type == game_classification::SCENARIO){
if (preferences::delete_saves()) {
savegame::clean_saves(gamestate.classification().label);
}
}
return VICTORY;
}