| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name game.cpp - The game set-up and creation. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2010 by Lutz Sammer, Andreas Arens, and |
| 13 |
// Jimmy Salmon |
| 14 |
// |
| 15 |
// This program is free software; you can redistribute it and/or modify |
| 16 |
// it under the terms of the GNU General Public License as published by |
| 17 |
// the Free Software Foundation; only version 2 of the License. |
| 18 |
// |
| 19 |
// This program is distributed in the hope that it will be useful, |
| 20 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
// GNU General Public License for more details. |
| 23 |
// |
| 24 |
// You should have received a copy of the GNU General Public License |
| 25 |
// along with this program; if not, write to the Free Software |
| 26 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 27 |
// 02111-1307, USA. |
| 28 |
// |
| 29 |
|
| 30 |
//@{ |
| 31 |
|
| 32 |
/*---------------------------------------------------------------------------- |
| 33 |
-- Includes |
| 34 |
----------------------------------------------------------------------------*/ |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
#include "stratagus.h" |
| 41 |
#include "map.h" |
| 42 |
#include "minimap.h" |
| 43 |
#include "player.h" |
| 44 |
#include "unit.h" |
| 45 |
#include "unittype.h" |
| 46 |
#include "upgrade.h" |
| 47 |
#include "pathfinder.h" |
| 48 |
#include "ui.h" |
| 49 |
#include "font.h" |
| 50 |
#include "sound.h" |
| 51 |
#include "sound_server.h" |
| 52 |
#include "interface.h" |
| 53 |
#include "cursor.h" |
| 54 |
#include "spells.h" |
| 55 |
#include "construct.h" |
| 56 |
#include "actions.h" |
| 57 |
#include "network.h" |
| 58 |
#include "netconnect.h" |
| 59 |
#include "missile.h" |
| 60 |
#include "settings.h" |
| 61 |
#include "results.h" |
| 62 |
#include "trigger.h" |
| 63 |
#include "replay.h" |
| 64 |
#include "iolib.h" |
| 65 |
#include "iocompat.h" |
| 66 |
|
| 67 |
#include "script.h" |
| 68 |
|
| 69 |
/*---------------------------------------------------------------------------- |
| 70 |
-- Variables |
| 71 |
----------------------------------------------------------------------------*/ |
| 72 |
|
| 73 |
Settings GameSettings; /// Game Settings |
| 74 |
static int LcmPreventRecurse; /// prevent recursion through LoadGameMap |
| 75 |
GameResults GameResult; /// Outcome of the game |
| 76 |
|
| 77 |
/*---------------------------------------------------------------------------- |
| 78 |
-- Functions |
| 79 |
----------------------------------------------------------------------------*/ |
| 80 |
|
| 81 |
static void WriteCreateUnits(FileWriter *f); |
| 82 |
static void WriteCreateUnit(FileWriter *f, const CUnit *unit); |
| 83 |
|
| 84 |
/*---------------------------------------------------------------------------- |
| 85 |
-- Map loading/saving |
| 86 |
----------------------------------------------------------------------------*/ |
| 87 |
|
| 88 |
/** |
| 89 |
** Load a Stratagus map. |
| 90 |
** |
| 91 |
** @param smpname smp filename |
| 92 |
** @param mapname map filename |
| 93 |
** @param map map loaded |
| 94 |
*/ |
| 95 |
static void LoadStratagusMap(const std::string &smpname, const std::string &mapname, CMap *map) |
| 96 |
{ |
| 97 |
char mapfull[PATH_MAX]; |
| 98 |
CFile file; |
| 99 |
|
| 100 |
// Try the same directory as the smp file first |
| 101 |
strcpy_s(mapfull, sizeof(mapfull), smpname.c_str()); |
| 102 |
char *p = strrchr(mapfull, '/'); |
| 103 |
if (!p) { |
| 104 |
p = mapfull; |
| 105 |
} else { |
| 106 |
++p; |
| 107 |
} |
| 108 |
strcpy_s(p, sizeof(mapfull) - (p - mapfull), mapname.c_str()); |
| 109 |
|
| 110 |
if (file.open(mapfull, CL_OPEN_READ) == -1) { |
| 111 |
// Not found, try StratagusLibPath and the smp's dir |
| 112 |
strcpy_s(mapfull, sizeof(mapfull), StratagusLibPath.c_str()); |
| 113 |
strcat_s(mapfull, sizeof(mapfull), "/"); |
| 114 |
strcat_s(mapfull, sizeof(mapfull), smpname.c_str()); |
| 115 |
p = strrchr(mapfull, '/'); |
| 116 |
if (!p) { |
| 117 |
p = mapfull; |
| 118 |
} else { |
| 119 |
++p; |
| 120 |
} |
| 121 |
strcpy_s(p, sizeof(mapfull) - (p - mapfull), mapname.c_str()); |
| 122 |
if (file.open(mapfull, CL_OPEN_READ) == -1) { |
| 123 |
// Not found again, try StratagusLibPath |
| 124 |
strcpy_s(mapfull, sizeof(mapfull), StratagusLibPath.c_str()); |
| 125 |
strcat_s(mapfull, sizeof(mapfull), "/"); |
| 126 |
strcat_s(mapfull, sizeof(mapfull), mapname.c_str()); |
| 127 |
if (file.open(mapfull, CL_OPEN_READ) == -1) { |
| 128 |
// Not found, try mapname by itself as a last resort |
| 129 |
strcpy_s(mapfull, sizeof(mapfull), mapname.c_str()); |
| 130 |
} else { |
| 131 |
file.close(); |
| 132 |
} |
| 133 |
} else { |
| 134 |
file.close(); |
| 135 |
} |
| 136 |
} else { |
| 137 |
file.close(); |
| 138 |
} |
| 139 |
|
| 140 |
if (LcmPreventRecurse) { |
| 141 |
fprintf(stderr, "recursive use of load map!\n"); |
| 142 |
ExitFatal(-1); |
| 143 |
} |
| 144 |
InitPlayers(); |
| 145 |
LcmPreventRecurse = 1; |
| 146 |
if (LuaLoadFile(mapfull) == -1) { |
| 147 |
fprintf(stderr, "Can't load lua file: %s\n", mapfull); |
| 148 |
ExitFatal(-1); |
| 149 |
} |
| 150 |
LcmPreventRecurse = 0; |
| 151 |
|
| 152 |
#if 0 |
| 153 |
// Not true if multiplayer levels! |
| 154 |
if (!ThisPlayer) { /// ARI: bomb if nothing was loaded! |
| 155 |
fprintf(stderr, "%s: invalid map\n", mapname.c_str()); |
| 156 |
ExitFatal(-1); |
| 157 |
} |
| 158 |
#endif |
| 159 |
if (!Map.Info.MapWidth || !Map.Info.MapHeight) { |
| 160 |
fprintf(stderr, "%s: invalid map\n", mapname.c_str()); |
| 161 |
ExitFatal(-1); |
| 162 |
} |
| 163 |
Map.Info.Filename = mapname; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
// Write the map presentation file |
| 168 |
static int WriteMapPresentation(const std::string &mapname, CMap *map) |
| 169 |
{ |
| 170 |
FileWriter *f = NULL; |
| 171 |
int i; |
| 172 |
int topplayer; |
| 173 |
int numplayers; |
| 174 |
const char *type[] = {"", "", "neutral", "nobody", |
| 175 |
"computer", "person", "rescue-passive", "rescue-active"}; |
| 176 |
|
| 177 |
numplayers = 0; |
| 178 |
topplayer = PlayerMax - 2; |
| 179 |
|
| 180 |
try { |
| 181 |
f = CreateFileWriter(mapname); |
| 182 |
f->printf("-- Stratagus Map Presentation\n"); |
| 183 |
f->printf("-- File generated by the Bos Wars builtin editor.\n"); |
| 184 |
// MAPTODO Copyright notice in generated file |
| 185 |
f->printf("-- File licensed under the GNU GPL version 2 or later.\n\n"); |
| 186 |
|
| 187 |
f->printf("DefinePlayerTypes("); |
| 188 |
while (topplayer > 0 && map->Info.PlayerType[topplayer] == PlayerNobody) { |
| 189 |
--topplayer; |
| 190 |
} |
| 191 |
for (i = 0; i <= topplayer; ++i) { |
| 192 |
f->printf("%s\"%s\"", (i ? ", " : ""), type[map->Info.PlayerType[i]]); |
| 193 |
if (map->Info.PlayerType[i] == PlayerPerson) { |
| 194 |
++numplayers; |
| 195 |
} |
| 196 |
} |
| 197 |
f->printf(")\n"); |
| 198 |
|
| 199 |
f->printf("PresentMap(\"%s\", %d, %d, %d, %d)\n", |
| 200 |
map->Info.Description.c_str(), numplayers, map->Info.MapWidth, map->Info.MapHeight, |
| 201 |
map->Info.MapUID + 1); |
| 202 |
|
| 203 |
f->printf("DefineMapSetup(GetCurrentLuaPath()..\"/setup.sms\")\n"); |
| 204 |
} catch (const FileException &) { |
| 205 |
fprintf(stderr, "ERROR: cannot write the map presentation\n"); |
| 206 |
delete f; |
| 207 |
return -1; |
| 208 |
} |
| 209 |
delete f; |
| 210 |
|
| 211 |
return 1; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
/** |
| 216 |
** Write the map setup file. |
| 217 |
** |
| 218 |
** @param mapSetup map filename |
| 219 |
** @param map map to save |
| 220 |
*/ |
| 221 |
int WriteMapSetup(const char *mapSetup, CMap *map) |
| 222 |
{ |
| 223 |
FileWriter *f = NULL; |
| 224 |
int i; |
| 225 |
|
| 226 |
try { |
| 227 |
f = CreateFileWriter(mapSetup); |
| 228 |
|
| 229 |
f->printf("-- Stratagus Map Setup\n"); |
| 230 |
f->printf("-- File generated by the Bos Wars builtin editor.\n"); |
| 231 |
// MAPTODO Copyright notice in generated file |
| 232 |
f->printf("-- File licensed under the GNU GPL version 2 or later.\n\n"); |
| 233 |
|
| 234 |
f->printf("-- player configuration\n"); |
| 235 |
for (i = 0; i < PlayerMax - 1; ++i) { |
| 236 |
f->printf("Players[%d]:SetStartView(%d, %d)\n", i, Players[i].StartX, Players[i].StartY); |
| 237 |
f->printf("Players[%d].EnergyStored = %d\n", i, Players[i].GetEnergyStored()); |
| 238 |
f->printf("Players[%d].MagmaStored = %d\n", i, Players[i].GetMagmaStored()); |
| 239 |
f->printf("Players[%d].AiName = \"%s\"\n", i, Players[i].AiName.c_str()); |
| 240 |
} |
| 241 |
f->printf("\n"); |
| 242 |
|
| 243 |
f->printf("Load(GetCurrentLuaPath() .. \"/terrain.lua\")\n"); |
| 244 |
f->printf("\n"); |
| 245 |
|
| 246 |
f->printf("-- place units\n"); |
| 247 |
WriteCreateUnits(f); |
| 248 |
f->printf("\n\n"); |
| 249 |
} catch (const FileException &) { |
| 250 |
fprintf(stderr,"Can't save map setup: `%s' \n", mapSetup); |
| 251 |
delete f; |
| 252 |
return -1; |
| 253 |
} |
| 254 |
delete f; |
| 255 |
|
| 256 |
return 1; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
** Write Lua CreateUnit calls for all existing units to the map setup |
| 261 |
** file. When the map is later loaded, these calls will recreate the |
| 262 |
** units. |
| 263 |
** |
| 264 |
** @param f file writer for the map setup file |
| 265 |
*/ |
| 266 |
static void WriteCreateUnits(FileWriter *f) |
| 267 |
{ |
| 268 |
int i; |
| 269 |
|
| 270 |
// Hot-spot units must be created before the magma-pump units |
| 271 |
// that are built on top of them. Otherwise, the map would |
| 272 |
// not be correctly loaded. |
| 273 |
// |
| 274 |
// When the user first adds a hot spot and a magma pump in the |
| 275 |
// editor, they end up in the correct order in the Units |
| 276 |
// array. However, if the user then deletes some other unit, |
| 277 |
// CUnit::Release moves the last unit in the array to the slot |
| 278 |
// of the deleted unit, and this can move the magma pump in |
| 279 |
// front of the hot spot. If the units are saved in that order, |
| 280 |
// and the map is then loaded, CclCreateUnit will refuse to |
| 281 |
// place the hot spot on top of the magma pump, and will instead |
| 282 |
// place it somewhere nearby. This behavior of CclCreateUnit |
| 283 |
// is necessary because it can be called from triggers too. |
| 284 |
// |
| 285 |
// Currently, magma pumps are the only unit type with any |
| 286 |
// building rules, so we just check that. This code may have |
| 287 |
// to be revised if building rules are added to other unit |
| 288 |
// types, especially if the rules are chained (X on top of Y |
| 289 |
// on top of Z). |
| 290 |
for (i = 0; i < NumUnits; ++i) { |
| 291 |
if (Units[i]->Type->BuildingRules.empty()) { |
| 292 |
WriteCreateUnit(f, Units[i]); |
| 293 |
} |
| 294 |
} |
| 295 |
for (i = 0; i < NumUnits; ++i) { |
| 296 |
if (!Units[i]->Type->BuildingRules.empty()) { |
| 297 |
WriteCreateUnit(f, Units[i]); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
** Write a Lua CreateUnit call for the specified unit to the map |
| 304 |
** setup file. When the map is later loaded, this call will recreate |
| 305 |
** the unit. |
| 306 |
** |
| 307 |
** @param f file writer for the map setup file |
| 308 |
** @param unit unit to be created when the map is loaded |
| 309 |
*/ |
| 310 |
static void WriteCreateUnit(FileWriter *f, const CUnit *unit) |
| 311 |
{ |
| 312 |
f->printf("CreateUnit(\"%s\", ", unit->Type->Ident.c_str()); |
| 313 |
if (unit->Player->Index == PlayerMax - 1) { |
| 314 |
f->printf("PlayerNumNeutral"); |
| 315 |
} else { |
| 316 |
f->printf("%d", unit->Player->Index); |
| 317 |
} |
| 318 |
f->printf(", {%d, %d})\n", unit->X, unit->Y); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
** Write a terrain into the given file. |
| 323 |
** |
| 324 |
** @param path pathname of the file to write the terrain into |
| 325 |
** @param map whose terrain must be saved |
| 326 |
*/ |
| 327 |
static void WriteTerrain(std::string &path, const CMap *map) |
| 328 |
{ |
| 329 |
FileWriter *f = NULL; |
| 330 |
|
| 331 |
try |
| 332 |
{ |
| 333 |
f = CreateFileWriter(path); |
| 334 |
f->printf("-- terrain\n"); |
| 335 |
f->printf("%s", Map.PatchManager.savePatches(true).c_str()); |
| 336 |
f->printf("\n"); |
| 337 |
} |
| 338 |
catch (const FileException &) |
| 339 |
{ |
| 340 |
fprintf(stderr,"Can't save map terrain: `%s' \n", path.c_str()); |
| 341 |
} |
| 342 |
|
| 343 |
delete f; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
/** |
| 348 |
** Save a Stratagus map. |
| 349 |
** |
| 350 |
** @param mapName map filename |
| 351 |
** @param map map to save |
| 352 |
*/ |
| 353 |
int SaveStratagusMap(const std::string &mapName, CMap *map) |
| 354 |
{ |
| 355 |
std::string mapPresentation; |
| 356 |
std::string mapSetup; |
| 357 |
|
| 358 |
if (!map->Info.MapWidth || !map->Info.MapHeight) { |
| 359 |
fprintf(stderr, "%s: invalid Bos Wars map\n", mapName.c_str()); |
| 360 |
ExitFatal(-1); |
| 361 |
} |
| 362 |
|
| 363 |
if (mapName.find(".map") == std::string::npos) { |
| 364 |
fprintf(stderr, "%s: invalid Bos Wars map filename\n", mapName.c_str()); |
| 365 |
} |
| 366 |
|
| 367 |
makedir(mapName.c_str(), 0777); |
| 368 |
mapPresentation = mapName + std::string("/presentation.smp"); |
| 369 |
mapSetup = mapName + std::string("/setup.sms"); |
| 370 |
if (WriteMapPresentation(mapPresentation, map) == -1) { |
| 371 |
return -1; |
| 372 |
} |
| 373 |
|
| 374 |
std::string terrainName = mapName + std::string("/terrain.lua"); |
| 375 |
WriteTerrain(terrainName, map); |
| 376 |
|
| 377 |
return WriteMapSetup(mapSetup.c_str(), map); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
/** |
| 382 |
** Load any map. |
| 383 |
** |
| 384 |
** @param filename map filename |
| 385 |
** @param map map loaded |
| 386 |
*/ |
| 387 |
static void LoadMap(const std::string &filename, CMap *map) |
| 388 |
{ |
| 389 |
const char *tmp; |
| 390 |
const char *name = filename.c_str(); |
| 391 |
|
| 392 |
tmp = strrchr(name, '.'); |
| 393 |
if (tmp) { |
| 394 |
if (!strcmp(tmp, ".gz")) { |
| 395 |
while (tmp - 1 > name && *--tmp != '.') { |
| 396 |
} |
| 397 |
} |
| 398 |
if (!strcmp(tmp, ".smp") || !strcmp(tmp, ".smp.gz")) { |
| 399 |
if (map->Info.Filename.empty()) { |
| 400 |
// The map info hasn't been loaded yet => do it now |
| 401 |
LoadStratagusMapInfo(filename); |
| 402 |
} |
| 403 |
Assert(!map->Info.Filename.empty()); |
| 404 |
map->Create(); |
| 405 |
LoadStratagusMap(filename, map->Info.Filename.c_str(), map); |
| 406 |
return; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
fprintf(stderr, "Unrecognized map format\n"); |
| 411 |
ExitFatal(-1); |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
** Set the game paused or unpaused |
| 416 |
** |
| 417 |
** @param paused True to pause game, false to unpause. |
| 418 |
*/ |
| 419 |
void SetGamePaused(bool paused) |
| 420 |
{ |
| 421 |
GamePaused = paused; |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
** Get the game paused or unpaused |
| 426 |
** |
| 427 |
** @return True if the game is paused, false otherwise |
| 428 |
*/ |
| 429 |
bool GetGamePaused() |
| 430 |
{ |
| 431 |
return GamePaused; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
** Set the game speed |
| 436 |
** |
| 437 |
** @param speed New game speed. |
| 438 |
*/ |
| 439 |
void SetGameSpeed(int speed) |
| 440 |
{ |
| 441 |
if (GameCycle == 0 || FastForwardCycle < GameCycle) { |
| 442 |
VideoSyncSpeed = speed * 100 / CYCLES_PER_SECOND; |
| 443 |
SetVideoSync(); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
/** |
| 448 |
** Get the game speed |
| 449 |
** |
| 450 |
** @return Game speed |
| 451 |
*/ |
| 452 |
int GetGameSpeed() |
| 453 |
{ |
| 454 |
return CYCLES_PER_SECOND * VideoSyncSpeed / 100; |
| 455 |
} |
| 456 |
|
| 457 |
/*---------------------------------------------------------------------------- |
| 458 |
-- Game types |
| 459 |
----------------------------------------------------------------------------*/ |
| 460 |
|
| 461 |
/** |
| 462 |
** Free for all |
| 463 |
*/ |
| 464 |
static void GameTypeFreeForAll(void) |
| 465 |
{ |
| 466 |
int i; |
| 467 |
int j; |
| 468 |
|
| 469 |
for (i = 0; i < PlayerMax - 1; ++i) { |
| 470 |
for (j = 0; j < PlayerMax - 1; ++j) { |
| 471 |
if (i != j) { |
| 472 |
CommandDiplomacy(i, DiplomacyEnemy, j); |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
/** |
| 479 |
** Top vs Bottom |
| 480 |
*/ |
| 481 |
static void GameTypeTopVsBottom(void) |
| 482 |
{ |
| 483 |
int i; |
| 484 |
int j; |
| 485 |
int top; |
| 486 |
int middle; |
| 487 |
|
| 488 |
middle = Map.Info.MapHeight / 2; |
| 489 |
for (i = 0; i < PlayerMax - 1; ++i) { |
| 490 |
top = Players[i].StartY <= middle; |
| 491 |
for (j = 0; j < PlayerMax - 1; ++j) { |
| 492 |
if (i != j) { |
| 493 |
if ((top && Players[j].StartY <= middle) || |
| 494 |
(!top && Players[j].StartY > middle)) { |
| 495 |
CommandDiplomacy(i, DiplomacyAllied, j); |
| 496 |
Players[i].SharedVision |= (1 << j); |
| 497 |
} else { |
| 498 |
CommandDiplomacy(i, DiplomacyEnemy, j); |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
/** |
| 506 |
** Left vs Right |
| 507 |
*/ |
| 508 |
static void GameTypeLeftVsRight(void) |
| 509 |
{ |
| 510 |
int i; |
| 511 |
int j; |
| 512 |
int left; |
| 513 |
int middle; |
| 514 |
|
| 515 |
middle = Map.Info.MapWidth / 2; |
| 516 |
for (i = 0; i < PlayerMax - 1; ++i) { |
| 517 |
left = Players[i].StartX <= middle; |
| 518 |
for (j = 0; j < PlayerMax - 1; ++j) { |
| 519 |
if (i != j) { |
| 520 |
if ((left && Players[j].StartX <= middle) || |
| 521 |
(!left && Players[j].StartX > middle)) { |
| 522 |
CommandDiplomacy(i, DiplomacyAllied, j); |
| 523 |
Players[i].SharedVision |= (1 << j); |
| 524 |
} else { |
| 525 |
CommandDiplomacy(i, DiplomacyEnemy, j); |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
/** |
| 533 |
** Man vs Machine |
| 534 |
*/ |
| 535 |
static void GameTypeManVsMachine(void) |
| 536 |
{ |
| 537 |
int i; |
| 538 |
int j; |
| 539 |
|
| 540 |
for (i = 0; i < PlayerMax - 1; ++i) { |
| 541 |
if (Players[i].Type != PlayerPerson && Players[i].Type != PlayerComputer) { |
| 542 |
continue; |
| 543 |
} |
| 544 |
for (j = 0; j < PlayerMax - 1; ++j) { |
| 545 |
if (i != j) { |
| 546 |
if (Players[i].Type == Players[j].Type) { |
| 547 |
CommandDiplomacy(i, DiplomacyAllied, j); |
| 548 |
Players[i].SharedVision |= (1 << j); |
| 549 |
} else { |
| 550 |
CommandDiplomacy(i, DiplomacyEnemy, j); |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
** Man vs Machine whith Humans on a Team |
| 559 |
*/ |
| 560 |
static void GameTypeManTeamVsMachine(void) |
| 561 |
{ |
| 562 |
int i; |
| 563 |
int j; |
| 564 |
|
| 565 |
for (i = 0; i < PlayerMax - 1; ++i) { |
| 566 |
if (Players[i].Type != PlayerPerson && Players[i].Type != PlayerComputer) { |
| 567 |
continue; |
| 568 |
} |
| 569 |
for (j = 0; j < PlayerMax - 1; ++j) { |
| 570 |
if (i != j) { |
| 571 |
if (Players[i].Type == Players[j].Type) { |
| 572 |
CommandDiplomacy(i, DiplomacyAllied, j); |
| 573 |
Players[i].SharedVision |= (1 << j); |
| 574 |
} else { |
| 575 |
CommandDiplomacy(i, DiplomacyEnemy, j); |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
if (Players[i].Type == PlayerPerson) { |
| 580 |
Players[i].Team = 2; |
| 581 |
} else { |
| 582 |
Players[i].Team = 1; |
| 583 |
} |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
/*---------------------------------------------------------------------------- |
| 588 |
-- Game creation |
| 589 |
----------------------------------------------------------------------------*/ |
| 590 |
|
| 591 |
/** |
| 592 |
** CreateGame. |
| 593 |
** |
| 594 |
** Load map, graphics, sounds, etc |
| 595 |
** |
| 596 |
** @param filename map filename |
| 597 |
** @param map map loaded |
| 598 |
** |
| 599 |
** @todo FIXME: use in this function InitModules / LoadModules!!! |
| 600 |
*/ |
| 601 |
void CreateGame(const std::string &filename, CMap *map) |
| 602 |
{ |
| 603 |
int i; |
| 604 |
|
| 605 |
if (SaveGameLoading) { |
| 606 |
SaveGameLoading = false; |
| 607 |
// Load game, already created game with Init/LoadModules |
| 608 |
CommandLog(NULL, NoUnitP, FlushCommands, -1, -1, NoUnitP, NULL, -1); |
| 609 |
return; |
| 610 |
} |
| 611 |
|
| 612 |
InitVisionTable(); // build vision table for fog of war |
| 613 |
InitPlayers(); |
| 614 |
|
| 615 |
if (Map.Info.Filename.empty() && !filename.empty()) { |
| 616 |
char path[PATH_MAX]; |
| 617 |
|
| 618 |
LibraryFileName(filename.c_str(), path, sizeof(path)); |
| 619 |
if (strcasestr(filename.c_str(), ".smp")) { |
| 620 |
LuaLoadFile(path); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
for (i = 0; i < PlayerMax; ++i) { |
| 625 |
PlayerTypes playertype = (PlayerTypes)Map.Info.PlayerType[i]; |
| 626 |
// Network games only: |
| 627 |
if (GameSettings.Presets[i].Type != SettingsPresetMapDefault) { |
| 628 |
playertype = GameSettings.Presets[i].Type; |
| 629 |
} |
| 630 |
CreatePlayer(playertype); |
| 631 |
} |
| 632 |
|
| 633 |
if (!filename.empty()) { |
| 634 |
if (CurrentMapPath != filename) { |
| 635 |
strcpy_s(CurrentMapPath, sizeof(CurrentMapPath), filename.c_str()); |
| 636 |
} |
| 637 |
|
| 638 |
// |
| 639 |
// Load the map. |
| 640 |
// |
| 641 |
InitUnitTypes(1); |
| 642 |
LoadMap(filename, map); |
| 643 |
} |
| 644 |
|
| 645 |
GameCycle = 0; |
| 646 |
SyncHash = 0; |
| 647 |
InitSyncRand(); |
| 648 |
|
| 649 |
if (IsNetworkGame()) { // Prepare network play |
| 650 |
DebugPrint("Client setup: Calling InitNetwork2\n"); |
| 651 |
InitNetwork2(); |
| 652 |
} else { |
| 653 |
if (!LocalPlayerName.empty() && LocalPlayerName != "Anonymous") { |
| 654 |
ThisPlayer->SetName(LocalPlayerName); |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
CallbackMusicOn(); |
| 659 |
|
| 660 |
#if 0 |
| 661 |
GamePaused = true; |
| 662 |
#endif |
| 663 |
|
| 664 |
if (FlagRevealMap) { |
| 665 |
Map.Reveal(); |
| 666 |
} |
| 667 |
|
| 668 |
// |
| 669 |
// Setup game types |
| 670 |
// |
| 671 |
if (GameSettings.GameType != SettingsGameTypeMapDefault) { |
| 672 |
switch (GameSettings.GameType) { |
| 673 |
case SettingsGameTypeMelee: |
| 674 |
break; |
| 675 |
case SettingsGameTypeFreeForAll: |
| 676 |
GameTypeFreeForAll(); |
| 677 |
break; |
| 678 |
case SettingsGameTypeTopVsBottom: |
| 679 |
GameTypeTopVsBottom(); |
| 680 |
break; |
| 681 |
case SettingsGameTypeLeftVsRight: |
| 682 |
GameTypeLeftVsRight(); |
| 683 |
break; |
| 684 |
case SettingsGameTypeManVsMachine: |
| 685 |
GameTypeManVsMachine(); |
| 686 |
break; |
| 687 |
case SettingsGameTypeManTeamVsMachine: |
| 688 |
GameTypeManTeamVsMachine(); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
// |
| 693 |
// Graphic part |
| 694 |
// |
| 695 |
SetPlayersPalette(); |
| 696 |
InitIcons(); |
| 697 |
LoadIcons(); |
| 698 |
|
| 699 |
LoadCursors(); |
| 700 |
UnitUnderCursor = NoUnitP; |
| 701 |
|
| 702 |
InitMissileTypes(); |
| 703 |
LoadMissileSprites(); |
| 704 |
InitConstructions(); |
| 705 |
LoadConstructions(); |
| 706 |
LoadUnitTypes(); |
| 707 |
LoadDecorations(); |
| 708 |
|
| 709 |
InitSelections(); |
| 710 |
|
| 711 |
InitUserInterface(); |
| 712 |
UI.Load(); |
| 713 |
|
| 714 |
Map.Init(); |
| 715 |
UI.Minimap.Create(); |
| 716 |
|
| 717 |
// |
| 718 |
// Sound part |
| 719 |
// |
| 720 |
LoadUnitSounds(); |
| 721 |
MapUnitSounds(); |
| 722 |
if (SoundEnabled()) { |
| 723 |
InitSoundClient(); |
| 724 |
} |
| 725 |
|
| 726 |
// |
| 727 |
// Spells |
| 728 |
// |
| 729 |
InitSpells(); |
| 730 |
|
| 731 |
// |
| 732 |
// Init units' groups |
| 733 |
// |
| 734 |
InitGroups(); |
| 735 |
|
| 736 |
// |
| 737 |
// Init players? |
| 738 |
// |
| 739 |
DebugPlayers(); |
| 740 |
PlayersInitAi(); |
| 741 |
|
| 742 |
// |
| 743 |
// Buttons (botpanel) |
| 744 |
// |
| 745 |
InitButtons(); |
| 746 |
|
| 747 |
// |
| 748 |
// Triggers |
| 749 |
// |
| 750 |
InitTriggers(); |
| 751 |
|
| 752 |
SetDefaultTextColors(UI.NormalFontColor, UI.ReverseFontColor); |
| 753 |
|
| 754 |
#if 0 |
| 755 |
if (!UI.SelectedViewport) { |
| 756 |
UI.SelectedViewport = UI.Viewports; |
| 757 |
} |
| 758 |
#endif |
| 759 |
UI.SelectedViewport->Center( |
| 760 |
ThisPlayer->StartX, ThisPlayer->StartY, TileSizeX / 2, TileSizeY / 2); |
| 761 |
|
| 762 |
// |
| 763 |
// Various hacks wich must be done after the map is loaded. |
| 764 |
// |
| 765 |
// FIXME: must be done after map is loaded |
| 766 |
InitPathfinder(); |
| 767 |
// |
| 768 |
// FIXME: The palette is loaded after the units are created. |
| 769 |
// FIXME: This loops fixes the colors of the units. |
| 770 |
// |
| 771 |
for (i = 0; i < NumUnits; ++i) { |
| 772 |
// I don't really think that there can be any rescued |
| 773 |
// units at this point. |
| 774 |
if (Units[i]->RescuedFrom) { |
| 775 |
Units[i]->Colors = &Units[i]->RescuedFrom->UnitColors; |
| 776 |
} else { |
| 777 |
Units[i]->Colors = &Units[i]->Player->UnitColors; |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
GameResult = GameNoResult; |
| 782 |
|
| 783 |
CommandLog(NULL, NoUnitP, FlushCommands, -1, -1, NoUnitP, NULL, -1); |
| 784 |
Video.ClearScreen(); |
| 785 |
} |
| 786 |
|
| 787 |
/** |
| 788 |
** Init Game Setting to default values |
| 789 |
** |
| 790 |
** @todo FIXME: this should not be executed for restart levels! |
| 791 |
*/ |
| 792 |
void InitSettings(void) |
| 793 |
{ |
| 794 |
for (int i = 0; i < PlayerMax; ++i) { |
| 795 |
GameSettings.Presets[i].Team = SettingsPresetMapDefault; |
| 796 |
GameSettings.Presets[i].Type = (PlayerTypes)SettingsPresetMapDefault; |
| 797 |
} |
| 798 |
GameSettings.Resources = SettingsPresetMapDefault; |
| 799 |
GameSettings.NumUnits = SettingsPresetMapDefault; |
| 800 |
GameSettings.Opponents = SettingsPresetMapDefault; |
| 801 |
GameSettings.Difficulty = SettingsPresetMapDefault; |
| 802 |
GameSettings.GameType = SettingsPresetMapDefault; |
| 803 |
GameSettings.MapRichness = SettingsPresetMapDefault; |
| 804 |
GameSettings.NetGameType = SettingsSinglePlayerGame; |
| 805 |
} |
| 806 |
|
| 807 |
//@} |