Permalink
Browse files

Clean up BattleItem saves

  • Loading branch information...
SupSuper committed Oct 19, 2018
1 parent a3aadb4 commit 0308485175c56fd89120de41e57a792da5a7eac8
Showing with 35 additions and 62 deletions.
  1. +2 −1 src/Mod/AlienDeployment.cpp
  2. +26 −40 src/Savegame/BattleItem.cpp
  3. +2 −1 src/Savegame/BattleItem.h
  4. +5 −20 src/Savegame/SavedBattleGame.cpp
@@ -128,7 +128,8 @@ AlienDeployment::~AlienDeployment()
/**
* Loads the Deployment from a YAML file.
* @param node YAML node.
* @param node YAML node.
* @param mod Mod for the deployment.
*/
void AlienDeployment::load(const YAML::Node &node, Mod *mod)
{
@@ -19,6 +19,7 @@
#include "BattleItem.h"
#include "BattleUnit.h"
#include "Tile.h"
#include "../Mod/Mod.h"
#include "../Mod/RuleItem.h"
#include "../Mod/RuleInventory.h"
@@ -61,9 +62,23 @@ BattleItem::~BattleItem()
/**
* Loads the item from a YAML file.
* @param node YAML node.
* @param mod Mod for the item.
*/
void BattleItem::load(const YAML::Node &node)
void BattleItem::load(const YAML::Node &node, Mod *mod)
{
std::string slot = node["inventoryslot"].as<std::string>("NULL");
if (slot != "NULL")
{
if (mod->getInventory(slot))
{
_inventorySlot = mod->getInventory(slot);
}
else
{
_inventorySlot = mod->getInventory("STR_GROUND");
}
}
_inventoryX = node["inventoryX"].as<int>(_inventoryX);
_inventoryY = node["inventoryY"].as<int>(_inventoryY);
_ammoQuantity = node["ammoqty"].as<int>(_ammoQuantity);
@@ -85,65 +100,36 @@ YAML::Node BattleItem::save() const
node["id"] = _id;
node["type"] = _rules->getType();
if (_owner)
{
node["owner"] = _owner->getId();
}
else
{
node["owner"] = -1;
}
if (_previousOwner)
{
node["previousOwner"] = _previousOwner->getId();
}
if (_unit)
{
node["unit"] = _unit->getId();
}
else
{
node["unit"] = -1;
}
if (_inventorySlot)
{
node["inventoryslot"] = _inventorySlot->getId();
}
else
{
node["inventoryslot"] = "NULL";
}
node["inventoryX"] = _inventoryX;
node["inventoryY"] = _inventoryY;
if (_tile)
{
node["position"] = _tile->getPosition();
}
else
{
node["position"] = Position(-1, -1, -1);
}
node["ammoqty"] = _ammoQuantity;
if (_ammoQuantity)
node["ammoqty"] = _ammoQuantity;
if (_ammoItem)
{
node["ammoItem"] = _ammoItem->getId();
}
else
{
node["ammoItem"] = -1;
}
node["painKiller"] = _painKiller;
node["heal"] = _heal;
node["stimulant"] = _stimulant;
node["fuseTimer"] = _fuseTimer;
if (_painKiller)
node["painKiller"] = _painKiller;
if (_heal)
node["heal"] = _heal;
if (_stimulant)
node["stimulant"] = _stimulant;
if (_fuseTimer != -1)
node["fuseTimer"] = _fuseTimer;
if (_droppedOnAlienTurn)
node["droppedOnAlienTurn"] = _droppedOnAlienTurn;
if (_XCOMProperty)
{
node["XCOMProperty"] = _XCOMProperty;
}
return node;
}
@@ -26,6 +26,7 @@ class RuleItem;
class RuleInventory;
class BattleUnit;
class Tile;
class Mod;
/**
* Represents a single item in the battlescape.
@@ -53,7 +54,7 @@ class BattleItem
/// Cleans up the item.
~BattleItem();
/// Loads the item from YAML.
void load(const YAML::Node& node);
void load(const YAML::Node& node, Mod *mod);
/// Saves the item to YAML.
YAML::Node save() const;
/// Gets the item's ruleset.
@@ -253,23 +253,11 @@ void SavedBattleGame::load(const YAML::Node &node, Mod *mod, SavedGame* savedGam
int id = (*i)["id"].as<int>();
_itemId = std::max(_itemId, id);
BattleItem *item = new BattleItem(mod->getItem(type), &id);
item->load(*i);
type = (*i)["inventoryslot"].as<std::string>();
if (type != "NULL")
{
if (mod->getInventory(type))
{
item->setSlot(mod->getInventory(type));
}
else
{
item->setSlot(mod->getInventory("STR_GROUND"));
}
}
int owner = (*i)["owner"].as<int>();
item->load(*i, mod);
int owner = (*i)["owner"].as<int>(-1);
int prevOwner = (*i)["previousOwner"].as<int>(-1);
int unit = (*i)["unit"].as<int>();
int unit = (*i)["unit"].as<int>(-1);
// match up items and units
for (std::vector<BattleUnit*>::iterator bu = _units.begin(); bu != _units.end(); ++bu)
@@ -282,9 +270,6 @@ void SavedBattleGame::load(const YAML::Node &node, Mod *mod, SavedGame* savedGam
{
item->setUnit(*bu);
}
}
for (std::vector<BattleUnit*>::iterator bu = _units.begin(); bu != _units.end(); ++bu)

This comment has been minimized.

@Yankes

Yankes Oct 19, 2018

Contributor

This is needed, moveToOwner will overwrite setPreviousOwner if called after.

This comment has been minimized.

@SupSuper

SupSuper Oct 19, 2018

Owner

Yeah I noticed and fixed it in e4084fa

{
if ((*bu)->getId() == prevOwner)
{
item->setPreviousOwner(*bu);
@@ -294,7 +279,7 @@ void SavedBattleGame::load(const YAML::Node &node, Mod *mod, SavedGame* savedGam
// match up items and tiles
if (item->getSlot() && item->getSlot()->getType() == INV_GROUND)
{
Position pos = (*i)["position"].as<Position>();
Position pos = (*i)["position"].as<Position>(Position(-1, -1, -1));
if (pos.x != -1)
getTile(pos)->addItem(item, mod->getInventory("STR_GROUND", true));
}

0 comments on commit 0308485

Please sign in to comment.