| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name replay.cpp - Replay game. */ |
| 11 |
// |
| 12 |
// (c) Copyright 2000-2008 by Lutz Sammer, Andreas Arens, and Jimmy Salmon. |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
// |
| 28 |
|
| 29 |
//@{ |
| 30 |
|
| 31 |
//---------------------------------------------------------------------------- |
| 32 |
// Includes |
| 33 |
//---------------------------------------------------------------------------- |
| 34 |
|
| 35 |
#include <time.h> |
| 36 |
|
| 37 |
#include "stratagus.h" |
| 38 |
#include "replay.h" |
| 39 |
#include "version.h" |
| 40 |
#include "iolib.h" |
| 41 |
#include "iocompat.h" |
| 42 |
#include "script.h" |
| 43 |
#include "unittype.h" |
| 44 |
#include "unit_manager.h" |
| 45 |
#include "settings.h" |
| 46 |
#include "commands.h" |
| 47 |
#include "player.h" |
| 48 |
#include "map.h" |
| 49 |
#include "netconnect.h" |
| 50 |
#include "network.h" |
| 51 |
#include "interface.h" |
| 52 |
#include "actions.h" |
| 53 |
|
| 54 |
//---------------------------------------------------------------------------- |
| 55 |
// Structures |
| 56 |
//---------------------------------------------------------------------------- |
| 57 |
|
| 58 |
/** |
| 59 |
** LogEntry structure. |
| 60 |
*/ |
| 61 |
class LogEntry { |
| 62 |
public: |
| 63 |
LogEntry() : GameCycle(0), Flush(0), PosX(0), PosY(0), DestUnitNumber(0), |
| 64 |
Num(0), SyncRandSeed(0), Next(NULL) |
| 65 |
{ |
| 66 |
UnitNumber = 0; |
| 67 |
} |
| 68 |
|
| 69 |
unsigned long GameCycle; |
| 70 |
int UnitNumber; |
| 71 |
std::string UnitIdent; |
| 72 |
std::string Action; |
| 73 |
int Flush; |
| 74 |
int PosX; |
| 75 |
int PosY; |
| 76 |
int DestUnitNumber; |
| 77 |
std::string Value; |
| 78 |
int Num; |
| 79 |
unsigned SyncRandSeed; |
| 80 |
LogEntry *Next; |
| 81 |
}; |
| 82 |
|
| 83 |
/** |
| 84 |
** Multiplayer Player definition |
| 85 |
*/ |
| 86 |
class MPPlayer { |
| 87 |
public: |
| 88 |
MPPlayer() : Team(0), Type((PlayerTypes)0) {} |
| 89 |
|
| 90 |
std::string Name; |
| 91 |
int Team; |
| 92 |
PlayerTypes Type; |
| 93 |
}; |
| 94 |
|
| 95 |
/** |
| 96 |
** Full replay structure (definition + logs) |
| 97 |
*/ |
| 98 |
class FullReplay { |
| 99 |
public: |
| 100 |
FullReplay() : |
| 101 |
MapId(0), Type(0), LocalPlayer(0), |
| 102 |
Resource(0), NumUnits(0), Difficulty(0), NoFow(false), RevealMap(0), |
| 103 |
MapRichness(0), GameType(0), Opponents(0), Commands(NULL) |
| 104 |
{ |
| 105 |
memset(Engine, 0, sizeof(Engine)); |
| 106 |
memset(Network, 0, sizeof(Network)); |
| 107 |
} |
| 108 |
std::string Comment1; |
| 109 |
std::string Comment2; |
| 110 |
std::string Comment3; |
| 111 |
std::string Date; |
| 112 |
std::string Map; |
| 113 |
std::string MapPath; |
| 114 |
unsigned MapId; |
| 115 |
|
| 116 |
int Type; |
| 117 |
int LocalPlayer; |
| 118 |
MPPlayer Players[PlayerMax]; |
| 119 |
|
| 120 |
int Resource; |
| 121 |
int NumUnits; |
| 122 |
int Difficulty; |
| 123 |
bool NoFow; |
| 124 |
int RevealMap; |
| 125 |
int MapRichness; |
| 126 |
int GameType; |
| 127 |
int Opponents; |
| 128 |
int Engine[3]; |
| 129 |
int Network[3]; |
| 130 |
LogEntry *Commands; |
| 131 |
}; |
| 132 |
|
| 133 |
//---------------------------------------------------------------------------- |
| 134 |
// Constants |
| 135 |
//---------------------------------------------------------------------------- |
| 136 |
|
| 137 |
|
| 138 |
//---------------------------------------------------------------------------- |
| 139 |
// Variables |
| 140 |
//---------------------------------------------------------------------------- |
| 141 |
|
| 142 |
bool CommandLogDisabled; /// True if command log is off |
| 143 |
ReplayType ReplayGameType; /// Replay game type |
| 144 |
static bool DisabledLog; /// Disabled log for replay |
| 145 |
static CFile *LogFile; /// Replay log file |
| 146 |
static unsigned long NextLogCycle; /// Next log cycle number |
| 147 |
static int InitReplay; /// Initialize replay |
| 148 |
static FullReplay *CurrentReplay; |
| 149 |
static LogEntry *ReplayStep; |
| 150 |
|
| 151 |
static void AppendLog(LogEntry *log, CFile *dest); |
| 152 |
bool IsReplayGame(); |
| 153 |
|
| 154 |
//---------------------------------------------------------------------------- |
| 155 |
// Log commands |
| 156 |
//---------------------------------------------------------------------------- |
| 157 |
|
| 158 |
/** |
| 159 |
** Allocate & fill a new FullReplay structure, from GameSettings. |
| 160 |
** |
| 161 |
** @return A new FullReplay structure |
| 162 |
*/ |
| 163 |
static FullReplay *StartReplay(void) |
| 164 |
{ |
| 165 |
FullReplay *replay; |
| 166 |
char *s; |
| 167 |
time_t now; |
| 168 |
char *s1; |
| 169 |
|
| 170 |
replay = new FullReplay; |
| 171 |
|
| 172 |
time(&now); |
| 173 |
s = ctime(&now); |
| 174 |
if ((s1 = strchr(s, '\n'))) { |
| 175 |
*s1 = '\0'; |
| 176 |
} |
| 177 |
|
| 178 |
replay->Comment1 = "Generated by Bos Wars Version " VERSION ""; |
| 179 |
replay->Comment2 = "Visit http://www.boswars.org for more information"; |
| 180 |
replay->Comment3 = "$Id$"; |
| 181 |
|
| 182 |
if (GameSettings.NetGameType == SettingsSinglePlayerGame) { |
| 183 |
replay->Type = ReplaySinglePlayer; |
| 184 |
} else { |
| 185 |
replay->Type = ReplayMultiPlayer; |
| 186 |
} |
| 187 |
|
| 188 |
for (int i = 0; i < PlayerMax; ++i) { |
| 189 |
replay->Players[i].Name = Players[i].Name; |
| 190 |
replay->Players[i].Team = GameSettings.Presets[i].Team; |
| 191 |
replay->Players[i].Type = GameSettings.Presets[i].Type; |
| 192 |
} |
| 193 |
|
| 194 |
replay->LocalPlayer = ThisPlayer->Index; |
| 195 |
|
| 196 |
replay->Date = s; |
| 197 |
replay->Map = Map.Info.Description; |
| 198 |
replay->MapId = (signed int)Map.Info.MapUID; |
| 199 |
replay->MapPath = CurrentMapPath; |
| 200 |
replay->Resource = GameSettings.Resources; |
| 201 |
replay->NumUnits = GameSettings.NumUnits; |
| 202 |
replay->Difficulty = GameSettings.Difficulty; |
| 203 |
replay->NoFow = GameSettings.NoFogOfWar; |
| 204 |
replay->GameType = GameSettings.GameType; |
| 205 |
replay->RevealMap = GameSettings.RevealMap; |
| 206 |
replay->MapRichness = GameSettings.MapRichness; |
| 207 |
replay->Opponents = GameSettings.Opponents; |
| 208 |
|
| 209 |
replay->Engine[0] = StratagusMajorVersion; |
| 210 |
replay->Engine[1] = StratagusMinorVersion; |
| 211 |
replay->Engine[2] = StratagusPatchLevel; |
| 212 |
|
| 213 |
replay->Network[0] = NetworkProtocolMajorVersion; |
| 214 |
replay->Network[1] = NetworkProtocolMinorVersion; |
| 215 |
replay->Network[2] = NetworkProtocolPatchLevel; |
| 216 |
return replay; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
** Applies settings the game used at the start of the replay |
| 221 |
*/ |
| 222 |
static void ApplyReplaySettings(void) |
| 223 |
{ |
| 224 |
if (CurrentReplay->Type == ReplayMultiPlayer) { |
| 225 |
ExitNetwork1(); |
| 226 |
NetPlayers = 2; |
| 227 |
GameSettings.NetGameType = SettingsMultiPlayerGame; |
| 228 |
|
| 229 |
ReplayGameType = ReplayMultiPlayer; |
| 230 |
NetLocalPlayerNumber = CurrentReplay->LocalPlayer; |
| 231 |
} else { |
| 232 |
GameSettings.NetGameType = SettingsSinglePlayerGame; |
| 233 |
ReplayGameType = ReplaySinglePlayer; |
| 234 |
} |
| 235 |
|
| 236 |
for (int i = 0; i < PlayerMax; ++i) { |
| 237 |
GameSettings.Presets[i].Team = CurrentReplay->Players[i].Team; |
| 238 |
GameSettings.Presets[i].Type = CurrentReplay->Players[i].Type; |
| 239 |
} |
| 240 |
|
| 241 |
if (strcpy_s(CurrentMapPath, sizeof(CurrentMapPath), CurrentReplay->MapPath.c_str()) != 0) { |
| 242 |
fprintf(stderr, "Replay map path is too long\n"); |
| 243 |
// FIXME: need to handle errors better |
| 244 |
Exit(1); |
| 245 |
} |
| 246 |
GameSettings.Resources = CurrentReplay->Resource; |
| 247 |
GameSettings.NumUnits = CurrentReplay->NumUnits; |
| 248 |
GameSettings.Difficulty = CurrentReplay->Difficulty; |
| 249 |
Map.NoFogOfWar = GameSettings.NoFogOfWar = CurrentReplay->NoFow; |
| 250 |
GameSettings.GameType = CurrentReplay->GameType; |
| 251 |
FlagRevealMap = GameSettings.RevealMap = CurrentReplay->RevealMap; |
| 252 |
GameSettings.MapRichness = CurrentReplay->MapRichness; |
| 253 |
GameSettings.Opponents = CurrentReplay->Opponents; |
| 254 |
|
| 255 |
// FIXME : check engine version |
| 256 |
// FIXME : FIXME: check network version |
| 257 |
// FIXME : check mapid |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
** Free a replay from memory |
| 262 |
** |
| 263 |
** @param replay Pointer to the replay to be freed |
| 264 |
*/ |
| 265 |
static void DeleteReplay(FullReplay *replay) |
| 266 |
{ |
| 267 |
LogEntry *log; |
| 268 |
LogEntry *next; |
| 269 |
|
| 270 |
log = replay->Commands; |
| 271 |
while (log) { |
| 272 |
next = log->Next; |
| 273 |
delete log; |
| 274 |
log = next; |
| 275 |
} |
| 276 |
|
| 277 |
delete replay; |
| 278 |
} |
| 279 |
|
| 280 |
static void PrintLogCommand(LogEntry *log, CFile *dest) |
| 281 |
{ |
| 282 |
dest->printf("Log( { "); |
| 283 |
dest->printf("GameCycle = %lu, ", log->GameCycle); |
| 284 |
if (log->UnitNumber != -1) { |
| 285 |
dest->printf("UnitNumber = %d, ", log->UnitNumber); |
| 286 |
} |
| 287 |
if (!log->UnitIdent.empty()) { |
| 288 |
dest->printf("UnitIdent = \"%s\", ", log->UnitIdent.c_str()); |
| 289 |
} |
| 290 |
dest->printf("Action = \"%s\", ", log->Action.c_str()); |
| 291 |
dest->printf("Flush = %d, ", log->Flush); |
| 292 |
if (log->PosX != -1 || log->PosY != -1) { |
| 293 |
dest->printf("PosX = %d, PosY = %d, ", log->PosX, log->PosY); |
| 294 |
} |
| 295 |
if (log->DestUnitNumber != -1) { |
| 296 |
dest->printf("DestUnitNumber = %d, ", log->DestUnitNumber); |
| 297 |
} |
| 298 |
if (!log->Value.empty()) { |
| 299 |
dest->printf("Value = [[%s]], ", log->Value.c_str()); |
| 300 |
} |
| 301 |
if (log->Num != -1) { |
| 302 |
dest->printf("Num = %d, ", log->Num); |
| 303 |
} |
| 304 |
dest->printf("SyncRandSeed = %d } )\n", (signed)log->SyncRandSeed); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
** Output the FullReplay list to dest file |
| 309 |
** |
| 310 |
** @param dest The file to output to |
| 311 |
*/ |
| 312 |
static void SaveFullLog(CFile *dest) |
| 313 |
{ |
| 314 |
dest->printf("ReplayLog( {\n"); |
| 315 |
dest->printf(" Comment1 = \"%s\",\n", CurrentReplay->Comment1.c_str()); |
| 316 |
dest->printf(" Comment2 = \"%s\",\n", CurrentReplay->Comment2.c_str()); |
| 317 |
dest->printf(" Comment3 = \"%s\",\n", CurrentReplay->Comment3.c_str()); |
| 318 |
dest->printf(" Date = \"%s\",\n", CurrentReplay->Date.c_str()); |
| 319 |
dest->printf(" Map = \"%s\",\n", CurrentReplay->Map.c_str()); |
| 320 |
dest->printf(" MapPath = \"%s\",\n", CurrentReplay->MapPath.c_str()); |
| 321 |
dest->printf(" MapId = %u,\n", CurrentReplay->MapId); |
| 322 |
dest->printf(" Type = %d,\n", CurrentReplay->Type); |
| 323 |
dest->printf(" LocalPlayer = %d,\n", CurrentReplay->LocalPlayer); |
| 324 |
dest->printf(" Players = {\n"); |
| 325 |
for (int i = 0; i < PlayerMax; ++i) { |
| 326 |
if (!CurrentReplay->Players[i].Name.empty()) { |
| 327 |
dest->printf("\t{ Name = \"%s\",", CurrentReplay->Players[i].Name.c_str()); |
| 328 |
} else { |
| 329 |
dest->printf("\t{"); |
| 330 |
} |
| 331 |
dest->printf(" Team = %d,", CurrentReplay->Players[i].Team); |
| 332 |
dest->printf(" Type = %d }%s", CurrentReplay->Players[i].Type, |
| 333 |
i != PlayerMax - 1 ? ",\n" : "\n"); |
| 334 |
} |
| 335 |
dest->printf(" },\n"); |
| 336 |
dest->printf(" Resource = %d,\n", CurrentReplay->Resource); |
| 337 |
dest->printf(" NumUnits = %d,\n", CurrentReplay->NumUnits); |
| 338 |
dest->printf(" Difficulty = %d,\n", CurrentReplay->Difficulty); |
| 339 |
dest->printf(" NoFow = %s,\n", CurrentReplay->NoFow ? "true" : "false"); |
| 340 |
dest->printf(" RevealMap = %d,\n", CurrentReplay->RevealMap); |
| 341 |
dest->printf(" GameType = %d,\n", CurrentReplay->GameType); |
| 342 |
dest->printf(" Opponents = %d,\n", CurrentReplay->Opponents); |
| 343 |
dest->printf(" MapRichness = %d,\n", CurrentReplay->MapRichness); |
| 344 |
dest->printf(" Engine = { %d, %d, %d },\n", |
| 345 |
CurrentReplay->Engine[0], CurrentReplay->Engine[1], CurrentReplay->Engine[2]); |
| 346 |
dest->printf(" Network = { %d, %d, %d }\n", |
| 347 |
CurrentReplay->Network[0], CurrentReplay->Network[1], CurrentReplay->Network[2]); |
| 348 |
dest->printf("} )\n"); |
| 349 |
|
| 350 |
LogEntry *log = CurrentReplay->Commands; |
| 351 |
while (log) { |
| 352 |
PrintLogCommand(log, dest); |
| 353 |
log = log->Next; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
** Append the LogEntry structure at the end of currentLog, and to LogFile |
| 359 |
** |
| 360 |
** @param log Pointer the replay log entry to be added |
| 361 |
** @param dest The file to output to |
| 362 |
*/ |
| 363 |
static void AppendLog(LogEntry *log, CFile *dest) |
| 364 |
{ |
| 365 |
LogEntry **last; |
| 366 |
|
| 367 |
// Append to linked list |
| 368 |
last = &CurrentReplay->Commands; |
| 369 |
while (*last) { |
| 370 |
last = &(*last)->Next; |
| 371 |
} |
| 372 |
|
| 373 |
*last = log; |
| 374 |
log->Next = 0; |
| 375 |
|
| 376 |
// Append to file |
| 377 |
if (!dest) { |
| 378 |
return; |
| 379 |
} |
| 380 |
|
| 381 |
PrintLogCommand(log, dest); |
| 382 |
dest->flush(); |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
** Log commands into file. |
| 387 |
** |
| 388 |
** This could later be used to recover, crashed games. |
| 389 |
** |
| 390 |
** @param action Command name (move,attack,...). |
| 391 |
** @param unit Unit that receive the command. |
| 392 |
** @param flush Append command or flush old commands. |
| 393 |
** @param x optional X map position. |
| 394 |
** @param y optional y map position. |
| 395 |
** @param dest optional destination unit. |
| 396 |
** @param value optional command argument (unit-type,...). |
| 397 |
** @param num optional number argument |
| 398 |
*/ |
| 399 |
void CommandLog(const char *action, const CUnit *unit, int flush, |
| 400 |
int x, int y, const CUnit *dest, const char *value, int num) |
| 401 |
{ |
| 402 |
LogEntry *log; |
| 403 |
|
| 404 |
if (CommandLogDisabled) { // No log wanted |
| 405 |
return; |
| 406 |
} |
| 407 |
|
| 408 |
// |
| 409 |
// Create and write header of log file. The player number is added |
| 410 |
// to the save file name, to test more than one player on one computer. |
| 411 |
// |
| 412 |
if (!LogFile) { |
| 413 |
char buf[PATH_MAX]; |
| 414 |
|
| 415 |
sprintf_s(buf, sizeof(buf), "%slogs/log_of_stratagus_%d.log", UserDirectory.c_str(), ThisPlayer->Index); |
| 416 |
LogFile = new CFile; |
| 417 |
if (LogFile->open(buf, CL_OPEN_WRITE) == -1) { |
| 418 |
// don't retry for each command |
| 419 |
CommandLogDisabled = false; |
| 420 |
delete LogFile; |
| 421 |
LogFile = NULL; |
| 422 |
return; |
| 423 |
} |
| 424 |
|
| 425 |
if (CurrentReplay) { |
| 426 |
SaveFullLog(LogFile); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
if (!CurrentReplay) { |
| 431 |
CurrentReplay = StartReplay(); |
| 432 |
SaveFullLog(LogFile); |
| 433 |
} |
| 434 |
|
| 435 |
if (!action) { |
| 436 |
return; |
| 437 |
} |
| 438 |
|
| 439 |
log = new LogEntry; |
| 440 |
|
| 441 |
// |
| 442 |
// Frame, unit, (type-ident only to be better readable). |
| 443 |
// |
| 444 |
log->GameCycle = GameCycle; |
| 445 |
|
| 446 |
log->UnitNumber = (unit ? UnitNumber(unit) : -1); |
| 447 |
log->UnitIdent = (unit ? unit->Type->Ident.c_str() : ""); |
| 448 |
|
| 449 |
log->Action = action; |
| 450 |
log->Flush = flush; |
| 451 |
|
| 452 |
// |
| 453 |
// Coordinates given. |
| 454 |
// |
| 455 |
log->PosX = x; |
| 456 |
log->PosY = y; |
| 457 |
|
| 458 |
// |
| 459 |
// Destination given. |
| 460 |
// |
| 461 |
log->DestUnitNumber = (dest ? UnitNumber(dest) : -1); |
| 462 |
|
| 463 |
// |
| 464 |
// Value given. |
| 465 |
// |
| 466 |
log->Value = (value ? value : ""); |
| 467 |
|
| 468 |
// |
| 469 |
// Number given. |
| 470 |
// |
| 471 |
log->Num = num; |
| 472 |
|
| 473 |
log->SyncRandSeed = SyncRandSeed; |
| 474 |
|
| 475 |
// Append it to ReplayLog list |
| 476 |
AppendLog(log, LogFile); |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
** Parse log |
| 481 |
*/ |
| 482 |
static int CclLog(lua_State *l) |
| 483 |
{ |
| 484 |
LogEntry *log; |
| 485 |
LogEntry **last; |
| 486 |
const char *value; |
| 487 |
|
| 488 |
LuaCheckArgs(l, 1); |
| 489 |
LuaCheckTable(l, 1); |
| 490 |
|
| 491 |
Assert(CurrentReplay); |
| 492 |
|
| 493 |
log = new LogEntry; |
| 494 |
log->UnitNumber = -1; |
| 495 |
log->PosX = -1; |
| 496 |
log->PosY = -1; |
| 497 |
log->DestUnitNumber = -1; |
| 498 |
log->Num = -1; |
| 499 |
|
| 500 |
lua_pushnil(l); |
| 501 |
while (lua_next(l, 1)) { |
| 502 |
value = LuaToString(l, -2); |
| 503 |
if (!strcmp(value, "GameCycle")) { |
| 504 |
log->GameCycle = LuaToNumber(l, -1); |
| 505 |
} else if (!strcmp(value, "UnitNumber")) { |
| 506 |
log->UnitNumber = LuaToNumber(l, -1); |
| 507 |
} else if (!strcmp(value, "UnitIdent")) { |
| 508 |
log->UnitIdent = LuaToString(l, -1); |
| 509 |
} else if (!strcmp(value, "Action")) { |
| 510 |
log->Action = LuaToString(l, -1); |
| 511 |
} else if (!strcmp(value, "Flush")) { |
| 512 |
log->Flush = LuaToNumber(l, -1); |
| 513 |
} else if (!strcmp(value, "PosX")) { |
| 514 |
log->PosX = LuaToNumber(l, -1); |
| 515 |
} else if (!strcmp(value, "PosY")) { |
| 516 |
log->PosY = LuaToNumber(l, -1); |
| 517 |
} else if (!strcmp(value, "DestUnitNumber")) { |
| 518 |
log->DestUnitNumber = LuaToNumber(l, -1); |
| 519 |
} else if (!strcmp(value, "Value")) { |
| 520 |
log->Value = LuaToString(l, -1); |
| 521 |
} else if (!strcmp(value, "Num")) { |
| 522 |
log->Num = LuaToNumber(l, -1); |
| 523 |
} else if (!strcmp(value, "SyncRandSeed")) { |
| 524 |
log->SyncRandSeed = (unsigned)LuaToNumber(l, -1); |
| 525 |
} else { |
| 526 |
LuaError(l, "Unsupported key: %s" _C_ value); |
| 527 |
} |
| 528 |
lua_pop(l, 1); |
| 529 |
} |
| 530 |
|
| 531 |
// Append to linked list |
| 532 |
last = &CurrentReplay->Commands; |
| 533 |
while (*last) { |
| 534 |
last = &(*last)->Next; |
| 535 |
} |
| 536 |
|
| 537 |
*last = log; |
| 538 |
|
| 539 |
return 0; |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
** Parse replay-log |
| 544 |
*/ |
| 545 |
static int CclReplayLog(lua_State *l) |
| 546 |
{ |
| 547 |
if (CommandLogDisabled && !IsReplayGame()) { |
| 548 |
return 0; |
| 549 |
} |
| 550 |
|
| 551 |
FullReplay *replay; |
| 552 |
const char *value; |
| 553 |
int j; |
| 554 |
|
| 555 |
LuaCheckArgs(l, 1); |
| 556 |
LuaCheckTable(l, 1); |
| 557 |
|
| 558 |
Assert(CurrentReplay == NULL); |
| 559 |
|
| 560 |
replay = new FullReplay; |
| 561 |
|
| 562 |
lua_pushnil(l); |
| 563 |
while (lua_next(l, 1) != 0) { |
| 564 |
value = LuaToString(l, -2); |
| 565 |
if (!strcmp(value, "Comment1")) { |
| 566 |
replay->Comment1 = LuaToString(l, -1); |
| 567 |
} else if (!strcmp(value, "Comment2")) { |
| 568 |
replay->Comment2 = LuaToString(l, -1); |
| 569 |
} else if (!strcmp(value, "Comment3")) { |
| 570 |
replay->Comment3 = LuaToString(l, -1); |
| 571 |
} else if (!strcmp(value, "Date")) { |
| 572 |
replay->Date = LuaToString(l, -1); |
| 573 |
} else if (!strcmp(value, "Map")) { |
| 574 |
replay->Map = LuaToString(l, -1); |
| 575 |
} else if (!strcmp(value, "MapPath")) { |
| 576 |
replay->MapPath = LuaToString(l, -1); |
| 577 |
} else if (!strcmp(value, "MapId")) { |
| 578 |
replay->MapId = LuaToNumber(l, -1); |
| 579 |
} else if (!strcmp(value, "Type")) { |
| 580 |
replay->Type = LuaToNumber(l, -1); |
| 581 |
} else if (!strcmp(value, "LocalPlayer")) { |
| 582 |
replay->LocalPlayer = LuaToNumber(l, -1); |
| 583 |
} else if (!strcmp(value, "Players")) { |
| 584 |
LuaCheckTableSize(l, -1, PlayerMax); |
| 585 |
for (j = 0; j < PlayerMax; ++j) { |
| 586 |
int top; |
| 587 |
|
| 588 |
lua_rawgeti(l, -1, j + 1); |
| 589 |
LuaCheckTable(l, -1); |
| 590 |
top = lua_gettop(l); |
| 591 |
lua_pushnil(l); |
| 592 |
while (lua_next(l, top) != 0) { |
| 593 |
value = LuaToString(l, -2); |
| 594 |
if (!strcmp(value, "Name")) { |
| 595 |
replay->Players[j].Name = LuaToString(l, -1); |
| 596 |
} else if (!strcmp(value, "Team")) { |
| 597 |
replay->Players[j].Team = LuaToNumber(l, -1); |
| 598 |
} else if (!strcmp(value, "Type")) { |
| 599 |
replay->Players[j].Type = (PlayerTypes)LuaToNumber(l, -1); |
| 600 |
} else { |
| 601 |
LuaError(l, "Unsupported key: %s" _C_ value); |
| 602 |
} |
| 603 |
lua_pop(l, 1); |
| 604 |
} |
| 605 |
lua_pop(l, 1); |
| 606 |
} |
| 607 |
} else if (!strcmp(value, "Resource")) { |
| 608 |
replay->Resource = LuaToNumber(l, -1); |
| 609 |
} else if (!strcmp(value, "NumUnits")) { |
| 610 |
replay->NumUnits = LuaToNumber(l, -1); |
| 611 |
} else if (!strcmp(value, "Difficulty")) { |
| 612 |
replay->Difficulty = LuaToNumber(l, -1); |
| 613 |
} else if (!strcmp(value, "NoFow")) { |
| 614 |
replay->NoFow = LuaToBoolean(l, -1); |
| 615 |
} else if (!strcmp(value, "RevealMap")) { |
| 616 |
replay->RevealMap = LuaToNumber(l, -1); |
| 617 |
} else if (!strcmp(value, "GameType")) { |
| 618 |
replay->GameType = LuaToNumber(l, -1); |
| 619 |
} else if (!strcmp(value, "Opponents")) { |
| 620 |
replay->Opponents = LuaToNumber(l, -1); |
| 621 |
} else if (!strcmp(value, "MapRichness")) { |
| 622 |
replay->MapRichness = LuaToNumber(l, -1); |
| 623 |
} else if (!strcmp(value, "Engine")) { |
| 624 |
LuaCheckTableSize(l, -1, 3); |
| 625 |
replay->Engine[0] = LuaToNumber(l, -1, 1); |
| 626 |
replay->Engine[1] = LuaToNumber(l, -1, 2); |
| 627 |
replay->Engine[2] = LuaToNumber(l, -1, 3); |
| 628 |
} else if (!strcmp(value, "Network")) { |
| 629 |
LuaCheckTableSize(l, -1, 3); |
| 630 |
replay->Network[0] = LuaToNumber(l, -1, 1); |
| 631 |
replay->Network[1] = LuaToNumber(l, -1, 2); |
| 632 |
replay->Network[2] = LuaToNumber(l, -1, 3); |
| 633 |
} else { |
| 634 |
LuaError(l, "Unsupported key: %s" _C_ value); |
| 635 |
} |
| 636 |
lua_pop(l, 1); |
| 637 |
} |
| 638 |
|
| 639 |
CurrentReplay = replay; |
| 640 |
|
| 641 |
// Apply CurrentReplay settings. |
| 642 |
if (!SaveGameLoading) { |
| 643 |
ApplyReplaySettings(); |
| 644 |
} |
| 645 |
|
| 646 |
return 0; |
| 647 |
} |
| 648 |
|
| 649 |
/** |
| 650 |
** Check if we're replaying a game |
| 651 |
*/ |
| 652 |
bool IsReplayGame() |
| 653 |
{ |
| 654 |
return ReplayGameType != ReplayNone; |
| 655 |
} |
| 656 |
|
| 657 |
/** |
| 658 |
** Save generated replay |
| 659 |
** |
| 660 |
** @param file file to save to. |
| 661 |
*/ |
| 662 |
void SaveReplayList(CFile *file) |
| 663 |
{ |
| 664 |
if (CurrentReplay) { |
| 665 |
SaveFullLog(file); |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
/** |
| 670 |
** Load a log file to replay a game |
| 671 |
** |
| 672 |
** @param name name of file to load. |
| 673 |
*/ |
| 674 |
int LoadReplay(const std::string &name) |
| 675 |
{ |
| 676 |
CleanReplayLog(); |
| 677 |
ReplayGameType = ReplaySinglePlayer; |
| 678 |
|
| 679 |
LuaLoadFile(name); |
| 680 |
|
| 681 |
NextLogCycle = ~0UL; |
| 682 |
if (!CommandLogDisabled) { |
| 683 |
CommandLogDisabled = true; |
| 684 |
DisabledLog = true; |
| 685 |
} |
| 686 |
GameObserve = true; |
| 687 |
InitReplay = 1; |
| 688 |
|
| 689 |
return 0; |
| 690 |
} |
| 691 |
|
| 692 |
/** |
| 693 |
** End logging |
| 694 |
*/ |
| 695 |
void EndReplayLog(void) |
| 696 |
{ |
| 697 |
if (LogFile) { |
| 698 |
LogFile->close(); |
| 699 |
delete LogFile; |
| 700 |
LogFile = NULL; |
| 701 |
} |
| 702 |
if (CurrentReplay) { |
| 703 |
DeleteReplay(CurrentReplay); |
| 704 |
CurrentReplay = NULL; |
| 705 |
} |
| 706 |
ReplayStep = NULL; |
| 707 |
} |
| 708 |
|
| 709 |
/** |
| 710 |
** Clean replay log |
| 711 |
*/ |
| 712 |
void CleanReplayLog(void) |
| 713 |
{ |
| 714 |
if (CurrentReplay) { |
| 715 |
DeleteReplay(CurrentReplay); |
| 716 |
CurrentReplay = 0; |
| 717 |
} |
| 718 |
ReplayStep = NULL; |
| 719 |
|
| 720 |
if (DisabledLog) { |
| 721 |
CommandLogDisabled = false; |
| 722 |
DisabledLog = false; |
| 723 |
} |
| 724 |
GameObserve = false; |
| 725 |
NetPlayers = 0; |
| 726 |
ReplayGameType = ReplayNone; |
| 727 |
} |
| 728 |
|
| 729 |
/** |
| 730 |
** Do next replay |
| 731 |
*/ |
| 732 |
static void DoNextReplay(void) |
| 733 |
{ |
| 734 |
int unit; |
| 735 |
const char *action; |
| 736 |
int flags; |
| 737 |
int posx; |
| 738 |
int posy; |
| 739 |
const char *val; |
| 740 |
int num; |
| 741 |
CUnit *dunit; |
| 742 |
|
| 743 |
Assert(ReplayStep != 0); |
| 744 |
|
| 745 |
NextLogCycle = ReplayStep->GameCycle; |
| 746 |
|
| 747 |
if (NextLogCycle != GameCycle) { |
| 748 |
return; |
| 749 |
} |
| 750 |
|
| 751 |
unit = ReplayStep->UnitNumber; |
| 752 |
action = ReplayStep->Action.c_str(); |
| 753 |
flags = ReplayStep->Flush; |
| 754 |
posx = ReplayStep->PosX; |
| 755 |
posy = ReplayStep->PosY; |
| 756 |
dunit = (ReplayStep->DestUnitNumber != -1 ? UnitSlots[ReplayStep->DestUnitNumber] : NoUnitP); |
| 757 |
val = ReplayStep->Value.c_str(); |
| 758 |
num = ReplayStep->Num; |
| 759 |
|
| 760 |
Assert(unit == -1 || ReplayStep->UnitIdent == UnitSlots[unit]->Type->Ident); |
| 761 |
|
| 762 |
if (SyncRandSeed != ReplayStep->SyncRandSeed) { |
| 763 |
#ifdef DEBUG |
| 764 |
if (!ReplayStep->SyncRandSeed) { |
| 765 |
// Replay without the 'sync info |
| 766 |
ThisPlayer->Notify(NotifyYellow, -1, -1, "%s", _("No sync info for this replay !")); |
| 767 |
} else { |
| 768 |
ThisPlayer->Notify(NotifyYellow, -1, -1, _("Replay got out of sync (%lu) !"), GameCycle); |
| 769 |
DebugPrint("OUT OF SYNC %u != %u\n" _C_ SyncRandSeed _C_ ReplayStep->SyncRandSeed); |
| 770 |
DebugPrint("OUT OF SYNC GameCycle %lu \n" _C_ GameCycle); |
| 771 |
Assert(0); |
| 772 |
// ReplayStep = 0; |
| 773 |
// NextLogCycle = ~0UL; |
| 774 |
// return; |
| 775 |
} |
| 776 |
#else |
| 777 |
ThisPlayer->Notify(NotifyYellow, -1, -1, "%s", _("Replay got out of sync !")); |
| 778 |
ReplayStep = 0; |
| 779 |
NextLogCycle = ~0UL; |
| 780 |
return; |
| 781 |
#endif |
| 782 |
} |
| 783 |
|
| 784 |
if (!strcmp(action, "stop")) { |
| 785 |
SendCommandStopUnit(UnitSlots[unit]); |
| 786 |
} else if (!strcmp(action, "stand-ground")) { |
| 787 |
SendCommandStandGround(UnitSlots[unit], flags); |
| 788 |
} else if (!strcmp(action, "follow")) { |
| 789 |
SendCommandFollow(UnitSlots[unit], dunit, flags); |
| 790 |
} else if (!strcmp(action, "move")) { |
| 791 |
SendCommandMove(UnitSlots[unit], posx, posy, flags); |
| 792 |
} else if (!strcmp(action, "repair")) { |
| 793 |
SendCommandRepair(UnitSlots[unit], posx, posy, dunit, flags); |
| 794 |
} else if (!strcmp(action, "auto-repair")) { |
| 795 |
SendCommandAutoRepair(UnitSlots[unit], posx); |
| 796 |
} else if (!strcmp(action, "attack")) { |
| 797 |
SendCommandAttack(UnitSlots[unit], posx, posy, dunit, flags); |
| 798 |
} else if (!strcmp(action, "attack-ground")) { |
| 799 |
SendCommandAttackGround(UnitSlots[unit], posx, posy, flags); |
| 800 |
} else if (!strcmp(action, "patrol")) { |
| 801 |
SendCommandPatrol(UnitSlots[unit], posx, posy, flags); |
| 802 |
} else if (!strcmp(action, "board")) { |
| 803 |
SendCommandBoard(UnitSlots[unit], posx, posy, dunit, flags); |
| 804 |
} else if (!strcmp(action, "unload")) { |
| 805 |
SendCommandUnload(UnitSlots[unit], posx, posy, dunit, flags); |
| 806 |
} else if (!strcmp(action, "build")) { |
| 807 |
SendCommandBuildBuilding(UnitSlots[unit], posx, posy, UnitTypeByIdent(val), flags); |
| 808 |
} else if (!strcmp(action, "dismiss")) { |
| 809 |
SendCommandDismiss(UnitSlots[unit]); |
| 810 |
} else if (!strcmp(action, "resource")) { |
| 811 |
SendCommandResource(UnitSlots[unit], dunit, flags); |
| 812 |
} else if (!strcmp(action, "train")) { |
| 813 |
SendCommandTrainUnit(UnitSlots[unit], UnitTypeByIdent(val), flags); |
| 814 |
} else if (!strcmp(action, "cancel-train")) { |
| 815 |
SendCommandCancelTraining(UnitSlots[unit], num, (val && *val) ? UnitTypeByIdent(val) : NULL); |
| 816 |
} else if (!strcmp(action, "spell-cast")) { |
| 817 |
SendCommandSpellCast(UnitSlots[unit], posx, posy, dunit, num, flags); |
| 818 |
} else if (!strcmp(action, "auto-spell-cast")) { |
| 819 |
SendCommandAutoSpellCast(UnitSlots[unit], num, posx); |
| 820 |
} else if (!strcmp(action, "diplomacy")) { |
| 821 |
int state; |
| 822 |
if (!strcmp(val, "neutral")) { |
| 823 |
state = DiplomacyNeutral; |
| 824 |
} else if (!strcmp(val, "allied")) { |
| 825 |
state = DiplomacyAllied; |
| 826 |
} else if (!strcmp(val, "enemy")) { |
| 827 |
state = DiplomacyEnemy; |
| 828 |
} else if (!strcmp(val, "crazy")) { |
| 829 |
state = DiplomacyCrazy; |
| 830 |
} else { |
| 831 |
DebugPrint("Invalid diplomacy command: %s" _C_ val); |
| 832 |
state = -1; |
| 833 |
} |
| 834 |
SendCommandDiplomacy(posx, state, posy); |
| 835 |
} else if (!strcmp(action, "shared-vision")) { |
| 836 |
bool state; |
| 837 |
state = atoi(val) ? true : false; |
| 838 |
SendCommandSharedVision(posx, state, posy); |
| 839 |
} else if (!strcmp(action, "input")) { |
| 840 |
if (val[0] == '-') { |
| 841 |
CclCommand(val + 1, false); |
| 842 |
} else { |
| 843 |
HandleCheats(val); |
| 844 |
} |
| 845 |
} else if (!strcmp(action, "quit")) { |
| 846 |
CommandQuit(posx); |
| 847 |
} else { |
| 848 |
DebugPrint("Invalid action: %s" _C_ action); |
| 849 |
} |
| 850 |
|
| 851 |
ReplayStep = ReplayStep->Next; |
| 852 |
NextLogCycle = ReplayStep ? (unsigned)ReplayStep->GameCycle : ~0UL; |
| 853 |
} |
| 854 |
|
| 855 |
/** |
| 856 |
** Replay user commands from log each cycle |
| 857 |
*/ |
| 858 |
static void ReplayEachCycle(void) |
| 859 |
{ |
| 860 |
if (!CurrentReplay) { |
| 861 |
return; |
| 862 |
} |
| 863 |
if (InitReplay) { |
| 864 |
for (int i = 0; i < PlayerMax; ++i) { |
| 865 |
if (!CurrentReplay->Players[i].Name.empty()) { |
| 866 |
Players[i].SetName(CurrentReplay->Players[i].Name); |
| 867 |
} |
| 868 |
} |
| 869 |
ReplayStep = CurrentReplay->Commands; |
| 870 |
NextLogCycle = (ReplayStep ? (unsigned)ReplayStep->GameCycle : ~0UL); |
| 871 |
InitReplay = 0; |
| 872 |
} |
| 873 |
|
| 874 |
if (!ReplayStep) { |
| 875 |
SetMessage("%s", _("End of replay")); |
| 876 |
GameObserve = false; |
| 877 |
return; |
| 878 |
} |
| 879 |
|
| 880 |
if (NextLogCycle != ~0UL && NextLogCycle != GameCycle) { |
| 881 |
return; |
| 882 |
} |
| 883 |
|
| 884 |
do { |
| 885 |
DoNextReplay(); |
| 886 |
} while (ReplayStep && (NextLogCycle == ~0UL || NextLogCycle == GameCycle)); |
| 887 |
|
| 888 |
if (!ReplayStep) { |
| 889 |
SetMessage("%s", _("End of replay")); |
| 890 |
GameObserve = false; |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
/** |
| 895 |
** Replay user commands from log each cycle, single player games |
| 896 |
*/ |
| 897 |
void SinglePlayerReplayEachCycle() |
| 898 |
{ |
| 899 |
if (ReplayGameType == ReplaySinglePlayer) { |
| 900 |
ReplayEachCycle(); |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
/** |
| 905 |
** Replay user commands from log each cycle, multiplayer games |
| 906 |
*/ |
| 907 |
void MultiPlayerReplayEachCycle() |
| 908 |
{ |
| 909 |
if (ReplayGameType == ReplayMultiPlayer) { |
| 910 |
ReplayEachCycle(); |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
/** |
| 915 |
** Register Ccl functions with lua |
| 916 |
*/ |
| 917 |
void ReplayCclRegister(void) |
| 918 |
{ |
| 919 |
lua_register(Lua, "Log", CclLog); |
| 920 |
lua_register(Lua, "ReplayLog", CclReplayLog); |
| 921 |
} |
| 922 |
|
| 923 |
//@} |