| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name unit_save.cpp - Save unit. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2008 by Lutz Sammer 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 <sstream> |
| 36 |
#include <iomanip> |
| 37 |
|
| 38 |
#include "stratagus.h" |
| 39 |
#include "unit.h" |
| 40 |
#include "unittype.h" |
| 41 |
#include "unit_manager.h" |
| 42 |
#include "player.h" |
| 43 |
#include "animation.h" |
| 44 |
#include "spells.h" |
| 45 |
#include "construct.h" |
| 46 |
#include "iolib.h" |
| 47 |
|
| 48 |
/*---------------------------------------------------------------------------- |
| 49 |
-- Functions |
| 50 |
----------------------------------------------------------------------------*/ |
| 51 |
|
| 52 |
/** |
| 53 |
** Generate a unit reference, a printable unique string for unit. |
| 54 |
*/ |
| 55 |
std::string UnitReference(const CUnit *unit) |
| 56 |
{ |
| 57 |
std::ostringstream ss; |
| 58 |
ss << "U" << std::setfill('0') << std::setw(4) << std::uppercase << |
| 59 |
std::hex << UnitNumber(unit); |
| 60 |
return ss.str(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
** Save an order. |
| 65 |
** |
| 66 |
** @param order Order who should be saved. |
| 67 |
** @param file Output file. |
| 68 |
*/ |
| 69 |
void SaveOrder(const COrder *order, CFile *file) |
| 70 |
{ |
| 71 |
file->printf("{"); |
| 72 |
switch (order->Action) { |
| 73 |
case UnitActionNone: |
| 74 |
file->printf("\"action-none\","); |
| 75 |
break; |
| 76 |
|
| 77 |
case UnitActionStill: |
| 78 |
file->printf("\"action-still\","); |
| 79 |
break; |
| 80 |
case UnitActionStandGround: |
| 81 |
file->printf("\"action-stand-ground\","); |
| 82 |
break; |
| 83 |
case UnitActionFollow: |
| 84 |
file->printf("\"action-follow\","); |
| 85 |
break; |
| 86 |
case UnitActionMove: |
| 87 |
file->printf("\"action-move\","); |
| 88 |
break; |
| 89 |
case UnitActionAttack: |
| 90 |
file->printf("\"action-attack\","); |
| 91 |
break; |
| 92 |
case UnitActionAttackGround: |
| 93 |
file->printf("\"action-attack-ground\","); |
| 94 |
break; |
| 95 |
case UnitActionDie: |
| 96 |
file->printf("\"action-die\","); |
| 97 |
break; |
| 98 |
|
| 99 |
case UnitActionSpellCast: |
| 100 |
file->printf("\"action-spell-cast\","); |
| 101 |
break; |
| 102 |
|
| 103 |
case UnitActionTrain: |
| 104 |
file->printf("\"action-train\","); |
| 105 |
break; |
| 106 |
case UnitActionBuilt: |
| 107 |
file->printf("\"action-built\","); |
| 108 |
break; |
| 109 |
|
| 110 |
case UnitActionBoard: |
| 111 |
file->printf("\"action-board\","); |
| 112 |
break; |
| 113 |
case UnitActionUnload: |
| 114 |
file->printf("\"action-unload\","); |
| 115 |
break; |
| 116 |
case UnitActionPatrol: |
| 117 |
file->printf("\"action-patrol\","); |
| 118 |
break; |
| 119 |
case UnitActionBuild: |
| 120 |
file->printf("\"action-build\","); |
| 121 |
break; |
| 122 |
|
| 123 |
case UnitActionRepair: |
| 124 |
file->printf("\"action-repair\","); |
| 125 |
break; |
| 126 |
case UnitActionResource: |
| 127 |
file->printf("\"action-resource\","); |
| 128 |
break; |
| 129 |
default: |
| 130 |
DebugPrint("Unknown action in order\n"); |
| 131 |
} |
| 132 |
|
| 133 |
file->printf(" \"range\", %d,", order->Range); |
| 134 |
file->printf(" \"width\", %d,", order->Width); |
| 135 |
file->printf(" \"height\", %d,", order->Height); |
| 136 |
file->printf(" \"min-range\", %d,", order->MinRange); |
| 137 |
if (order->Goal) { |
| 138 |
if (order->Goal->Destroyed) { |
| 139 |
/* this unit is destroyed so it's not in the global unit |
| 140 |
* array - this means it won't be saved!!! */ |
| 141 |
printf ("FIXME: storing destroyed Goal - loading will fail.\n"); |
| 142 |
} |
| 143 |
file->printf(" \"goal\", \"%s\",", UnitReference(order->Goal).c_str()); |
| 144 |
} |
| 145 |
file->printf(" \"tile\", {%d, %d},", order->X, order->Y); |
| 146 |
if (order->Type) { |
| 147 |
file->printf(" \"type\", \"%s\",", order->Type->Ident.c_str()); |
| 148 |
} |
| 149 |
|
| 150 |
// Extra arg. |
| 151 |
switch (order->Action) { |
| 152 |
case UnitActionPatrol: |
| 153 |
file->printf(" \"patrol\", {%d, %d},", |
| 154 |
order->Arg1.Patrol.X, order->Arg1.Patrol.Y); |
| 155 |
break; |
| 156 |
case UnitActionSpellCast: |
| 157 |
if (order->Arg1.Spell) { |
| 158 |
file->printf(" \"spell\", \"%s\",", order->Arg1.Spell->Ident.c_str()); |
| 159 |
} |
| 160 |
break; |
| 161 |
default: |
| 162 |
break; |
| 163 |
} |
| 164 |
|
| 165 |
file->printf("}"); |
| 166 |
} |
| 167 |
|
| 168 |
/** Save the progress of the current order to a file. |
| 169 |
** |
| 170 |
** @param unit Unit pointer to be saved. |
| 171 |
** @param file Output file. |
| 172 |
** |
| 173 |
** Each unit must always have a current order, which is kept in |
| 174 |
** unit->Orders[0], and then possibly some other orders that it will |
| 175 |
** execute after the current order completes. When the unit begins |
| 176 |
** executing an order, it can save additional order-specific data to |
| 177 |
** some member of the unit->Data union. For example, the various |
| 178 |
** movement orders save a precalculated path in unit->Data.Move. |
| 179 |
** While the unit is executing the order, it can then read and update |
| 180 |
** this data. |
| 181 |
** |
| 182 |
** This function saves the appropriate member of unit->Data to the |
| 183 |
** specified file. unit->Orders[0]->Action controls which member that |
| 184 |
** is. |
| 185 |
*/ |
| 186 |
static void SaveOrderData(const CUnit *unit, CFile *file) |
| 187 |
{ |
| 188 |
int i; |
| 189 |
|
| 190 |
// If the unit has not yet begun executing this order, then |
| 191 |
// unit->Data can still contain data from the previous order. |
| 192 |
// Do not attempt to save the data in that case, because |
| 193 |
// integers in it may be out of range, and pointers in it can |
| 194 |
// point to invalid addresses. |
| 195 |
// |
| 196 |
// HOWEVER... UnitActionBuilt apparently keeps SubAction == 0 |
| 197 |
// at all times, and StartBuilding which sets UnitActionBuilt |
| 198 |
// also initializes unit->Data. So there we must allow 0 too. |
| 199 |
if (unit->SubAction == 0 && unit->Orders[0]->Action != UnitActionBuilt) |
| 200 |
return; |
| 201 |
|
| 202 |
switch (unit->Orders[0]->Action) { |
| 203 |
case UnitActionNone: |
| 204 |
case UnitActionStill: |
| 205 |
case UnitActionStandGround: |
| 206 |
case UnitActionDie: |
| 207 |
break; |
| 208 |
case UnitActionBuilt: |
| 209 |
{ |
| 210 |
CConstructionFrame *cframe; |
| 211 |
int frame; |
| 212 |
|
| 213 |
cframe = unit->Type->Construction->Frames; |
| 214 |
frame = 0; |
| 215 |
while (cframe != unit->Data.Built.Frame) { |
| 216 |
cframe = cframe->Next; |
| 217 |
++frame; |
| 218 |
} |
| 219 |
file->printf(",\n \"data-built\", {"); |
| 220 |
|
| 221 |
file->printf("\"progress\", %d, \"frame\", %d,", |
| 222 |
unit->Data.Built.Progress, frame); |
| 223 |
if (unit->Data.Built.Cancel) { |
| 224 |
file->printf(" \"cancel\","); |
| 225 |
} |
| 226 |
file->printf("}"); |
| 227 |
break; |
| 228 |
} |
| 229 |
case UnitActionTrain: |
| 230 |
file->printf(",\n \"data-train\", {"); |
| 231 |
file->printf("\"ticks\", %d, ", unit->Data.Train.Ticks); |
| 232 |
file->printf("}"); |
| 233 |
break; |
| 234 |
case UnitActionResource: |
| 235 |
/// @bug Should save unit->Data.Move instead, |
| 236 |
/// if unit->SubAction == SUB_MOVE_TO_RESOURCE. |
| 237 |
/// However, that macro is not visible here. |
| 238 |
file->printf(",\n \"data-harvest\", {"); |
| 239 |
file->printf("\"current-production\", {"); |
| 240 |
for (i = 0; i < MaxCosts; ++i) { |
| 241 |
file->printf("%s%d", (i ? ", " : ""), unit->Data.Harvest.CurrentProduction[i]); |
| 242 |
} |
| 243 |
file->printf("}}"); |
| 244 |
break; |
| 245 |
case UnitActionFollow: |
| 246 |
case UnitActionMove: |
| 247 |
case UnitActionAttack: |
| 248 |
case UnitActionAttackGround: |
| 249 |
case UnitActionSpellCast: |
| 250 |
case UnitActionBoard: |
| 251 |
case UnitActionUnload: |
| 252 |
case UnitActionPatrol: |
| 253 |
case UnitActionBuild: |
| 254 |
case UnitActionRepair: |
| 255 |
file->printf(",\n \"data-move\", {"); |
| 256 |
if (unit->Data.Move.Fast) { |
| 257 |
file->printf("\"fast\", "); |
| 258 |
} |
| 259 |
if (unit->Data.Move.Length > 0) { |
| 260 |
Assert(unit->Data.Move.Length <= MAX_PATH_LENGTH); |
| 261 |
|
| 262 |
file->printf("\"path\", {"); |
| 263 |
for (i = 0; i < unit->Data.Move.Length; ++i) { |
| 264 |
file->printf("%d, ", unit->Data.Move.Path[i]); |
| 265 |
} |
| 266 |
file->printf("},"); |
| 267 |
} |
| 268 |
file->printf("}"); |
| 269 |
break; |
| 270 |
default: |
| 271 |
DebugPrint("Unknown action in order\n"); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
** Save the state of a unit to file. |
| 277 |
** |
| 278 |
** @param unit Unit pointer to be saved. |
| 279 |
** @param file Output file. |
| 280 |
*/ |
| 281 |
void SaveUnit(const CUnit *unit, CFile *file) |
| 282 |
{ |
| 283 |
CUnit *uins; |
| 284 |
int i; |
| 285 |
|
| 286 |
file->printf("\nUnit(%d, ", UnitNumber(unit)); |
| 287 |
|
| 288 |
// 'type and 'player must be first, needed to create the unit slot |
| 289 |
file->printf("\"type\", \"%s\", ", unit->Type->Ident.c_str()); |
| 290 |
if (unit->Seen.Type) { |
| 291 |
file->printf("\"seen-type\", \"%s\", ", unit->Seen.Type->Ident.c_str()); |
| 292 |
} |
| 293 |
|
| 294 |
file->printf("\"player\", %d,\n ", unit->Player->Index); |
| 295 |
|
| 296 |
if (unit->Next) { |
| 297 |
file->printf("\"next\", %d, ", UnitNumber(unit->Next)); |
| 298 |
} |
| 299 |
|
| 300 |
file->printf("\"tile\", {%d, %d}, ", unit->X, unit->Y); |
| 301 |
file->printf("\"seen-tile\", {%d, %d}, ", unit->Seen.X, unit->Seen.Y); |
| 302 |
file->printf("\"refs\", %lu, ", unit->Refs); |
| 303 |
#if 0 |
| 304 |
// latimerius: why is this so complex? |
| 305 |
// JOHNS: An unit can be owned by a new player and have still the old stats |
| 306 |
for (i = 0; i < PlayerMax; ++i) { |
| 307 |
if (&unit->Type->Stats[i] == unit->Stats) { |
| 308 |
file->printf("\"stats\", %d,\n ", i); |
| 309 |
break; |
| 310 |
} |
| 311 |
} |
| 312 |
// latimerius: what's the point of storing a pointer value anyway? |
| 313 |
if (i == PlayerMax) { |
| 314 |
file->printf("\"stats\", \"S%08X\",\n ", (int)unit->Stats); |
| 315 |
} |
| 316 |
#else |
| 317 |
file->printf("\"stats\", %d,\n ", unit->Player->Index); |
| 318 |
#endif |
| 319 |
file->printf("\"pixel\", {%d, %d}, ", unit->IX, unit->IY); |
| 320 |
file->printf("\"seen-pixel\", {%d, %d}, ", unit->Seen.IX, unit->Seen.IY); |
| 321 |
file->printf("\"frame\", %d, ", unit->Frame); |
| 322 |
if (unit->Seen.Frame != UnitNotSeen) { |
| 323 |
file->printf("\"seen\", %d, ", unit->Seen.Frame); |
| 324 |
} else { |
| 325 |
file->printf("\"not-seen\", "); |
| 326 |
} |
| 327 |
file->printf("\"direction\", %d,\n ", unit->Direction); |
| 328 |
file->printf("\"attacked\", %lu,\n ", unit->Attacked); |
| 329 |
file->printf(" \"current-sight-range\", %d,", unit->CurrentSightRange); |
| 330 |
if (unit->Burning) { |
| 331 |
file->printf(" \"burning\","); |
| 332 |
} |
| 333 |
if (unit->Destroyed) { |
| 334 |
file->printf(" \"destroyed\","); |
| 335 |
} |
| 336 |
if (unit->Removed) { |
| 337 |
file->printf(" \"removed\","); |
| 338 |
} |
| 339 |
if (unit->Selected) { |
| 340 |
file->printf(" \"selected\","); |
| 341 |
} |
| 342 |
if (unit->RescuedFrom) { |
| 343 |
file->printf(" \"rescued-from\", %d,", unit->RescuedFrom->Index); |
| 344 |
} |
| 345 |
// n0b0dy: How is this useful? |
| 346 |
// mr-russ: You can't always load units in order, it saved the information |
| 347 |
// so you can load a unit whose Container hasn't been loaded yet. |
| 348 |
// SEE unit loading code. |
| 349 |
if (unit->Container && unit->Removed) { |
| 350 |
file->printf(" \"host-info\", {%d, %d, %d, %d}, ", |
| 351 |
unit->Container->X, unit->Container->Y, |
| 352 |
unit->Container->Type->TileWidth, |
| 353 |
unit->Container->Type->TileHeight); |
| 354 |
} |
| 355 |
file->printf(" \"seen-by-player\", \""); |
| 356 |
for (i = 0; i < PlayerMax; ++i) { |
| 357 |
file->printf("%c", (unit->Seen.ByPlayer & (1 << i)) ? 'X' : '_'); |
| 358 |
} |
| 359 |
file->printf("\",\n "); |
| 360 |
file->printf(" \"seen-destroyed\", \""); |
| 361 |
for (i = 0; i < PlayerMax; ++i) { |
| 362 |
file->printf("%c", (unit->Seen.Destroyed & (1 << i)) ? 'X' : '_'); |
| 363 |
} |
| 364 |
file->printf("\",\n "); |
| 365 |
if (unit->Constructed) { |
| 366 |
file->printf(" \"constructed\","); |
| 367 |
} |
| 368 |
if (unit->Seen.Constructed) { |
| 369 |
file->printf(" \"seen-constructed\","); |
| 370 |
} |
| 371 |
|
| 372 |
if (unit->Seen.CFrame) { |
| 373 |
CConstructionFrame *cframe = unit->Seen.Type->Construction->Frames; |
| 374 |
int frame = 0; |
| 375 |
while (cframe != unit->Seen.CFrame) { |
| 376 |
cframe = cframe->Next; |
| 377 |
++frame; |
| 378 |
} |
| 379 |
file->printf(" \"seen-cframe\", %d,", frame); |
| 380 |
} |
| 381 |
|
| 382 |
file->printf(" \"seen-state\", %d, ", unit->Seen.State); |
| 383 |
file->printf("\"ttl\", %lu, ", unit->TTL); |
| 384 |
|
| 385 |
for (i = 0; i < UnitTypeVar.NumberVariable; i++) { |
| 386 |
file->printf("\"%s\", {Value = %d, Max = %d, Increase = %d, Enable = %s},\n ", |
| 387 |
UnitTypeVar.VariableName[i], unit->Variable[i].Value, unit->Variable[i].Max, |
| 388 |
unit->Variable[i].Increase, unit->Variable[i].Enable ? "true" : "false"); |
| 389 |
} |
| 390 |
|
| 391 |
file->printf("\"group-id\", %d,\n ", unit->GroupId); |
| 392 |
file->printf("\"last-group\", %d,\n ", unit->LastGroup); |
| 393 |
|
| 394 |
file->printf("\"resources-held\", {"); |
| 395 |
for (i = 0; i < MaxCosts; ++i) { |
| 396 |
file->printf("%s%d", (i ? ", " : ""), unit->ResourcesHeld[i]); |
| 397 |
} |
| 398 |
file->printf("},\n "); |
| 399 |
|
| 400 |
file->printf("\"production-efficiency\", %d, ", unit->ProductionEfficiency); |
| 401 |
|
| 402 |
file->printf("\"sub-action\", %d, ", unit->SubAction); |
| 403 |
file->printf("\"wait\", %d, ", unit->Wait); |
| 404 |
file->printf("\"state\", %d,", unit->State); |
| 405 |
file->printf("\"anim-wait\", %d,", unit->Anim.Wait); |
| 406 |
for (i = 0; i < NumAnimations; ++i) { |
| 407 |
if (AnimationsArray[i] == unit->Anim.CurrAnim) { |
| 408 |
file->printf("\"curr-anim\", %d,", i); |
| 409 |
// The cast from ptrdiff_t to int will not |
| 410 |
// overflow because the array of animation |
| 411 |
// frames was converted from a Lua list |
| 412 |
// and lua_objlen returns int. |
| 413 |
file->printf("\"anim\", %d,", |
| 414 |
static_cast<int>(unit->Anim.Anim - unit->Anim.CurrAnim)); |
| 415 |
break; |
| 416 |
} |
| 417 |
} |
| 418 |
if (unit->Anim.Unbreakable) { |
| 419 |
file->printf(" \"unbreakable\","); |
| 420 |
} |
| 421 |
file->printf("\n \"blink\", %d,", unit->Blink); |
| 422 |
if (unit->Moving) { |
| 423 |
file->printf(" \"moving\","); |
| 424 |
} |
| 425 |
if (unit->ReCast) { |
| 426 |
file->printf(" \"re-cast\","); |
| 427 |
} |
| 428 |
if (unit->Boarded) { |
| 429 |
file->printf(" \"boarded\","); |
| 430 |
} |
| 431 |
if (unit->AutoRepair) { |
| 432 |
file->printf(" \"auto-repair\","); |
| 433 |
} |
| 434 |
|
| 435 |
file->printf(" \"units-boarded-count\", %d,", unit->BoardCount); |
| 436 |
|
| 437 |
if (unit->UnitInside) { |
| 438 |
file->printf("\n \"units-contained\", {"); |
| 439 |
uins = unit->UnitInside->PrevContained; |
| 440 |
for (i = unit->InsideCount; i; --i, uins = uins->PrevContained) { |
| 441 |
file->printf("\"%s\"", UnitReference(uins).c_str()); |
| 442 |
if (i > 1) { |
| 443 |
file->printf(", "); |
| 444 |
} |
| 445 |
} |
| 446 |
file->printf("},\n "); |
| 447 |
} |
| 448 |
Assert((unsigned int)unit->OrderCount == unit->Orders.size()); |
| 449 |
file->printf("\"order-count\", %d,\n ", unit->OrderCount); |
| 450 |
file->printf("\"order-flush\", %d,\n ", unit->OrderFlush); |
| 451 |
file->printf("\"orders\", {"); |
| 452 |
for (i = 0; i < unit->OrderCount; ++i) { |
| 453 |
file->printf("\n "); |
| 454 |
SaveOrder(unit->Orders[i], file); |
| 455 |
file->printf(","); |
| 456 |
} |
| 457 |
file->printf("},\n \"saved-order\", "); |
| 458 |
SaveOrder(&unit->SavedOrder, file); |
| 459 |
file->printf(",\n \"new-order\", "); |
| 460 |
SaveOrder(&unit->NewOrder, file); |
| 461 |
|
| 462 |
// |
| 463 |
// Order data part |
| 464 |
// |
| 465 |
SaveOrderData(unit, file); |
| 466 |
|
| 467 |
if (unit->Goal) { |
| 468 |
file->printf(",\n \"goal\", %d", UnitNumber(unit->Goal)); |
| 469 |
} |
| 470 |
if (unit->AutoCastSpell) { |
| 471 |
for (i = 0; (unsigned int) i < SpellTypeTable.size(); ++i) { |
| 472 |
if (unit->AutoCastSpell[i]) { |
| 473 |
file->printf(",\n \"auto-cast\", \"%s\"", SpellTypeTable[i]->Ident.c_str()); |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
file->printf(")\n"); |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
** Save state of units to file. |
| 483 |
** |
| 484 |
** @param file Output file. |
| 485 |
*/ |
| 486 |
void SaveUnits(CFile *file) |
| 487 |
{ |
| 488 |
UnitManager.Save(file); |
| 489 |
|
| 490 |
for (CUnit **table = Units; table < &Units[NumUnits]; ++table) { |
| 491 |
SaveUnit(*table, file); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
//@} |