Skip to content

Commit

Permalink
Clean up BattleItem saves
Browse files Browse the repository at this point in the history
  • Loading branch information
SupSuper committed Oct 19, 2018
1 parent a3aadb4 commit 0308485
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 62 deletions.
3 changes: 2 additions & 1 deletion src/Mod/AlienDeployment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
66 changes: 26 additions & 40 deletions src/Savegame/BattleItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Savegame/BattleItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class RuleItem;
class RuleInventory;
class BattleUnit;
class Tile;
class Mod;

/**
* Represents a single item in the battlescape.
Expand Down Expand Up @@ -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.
Expand Down
25 changes: 5 additions & 20 deletions src/Savegame/SavedBattleGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.

Copy link
@Yankes

Yankes Oct 19, 2018

Contributor

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

This comment has been minimized.

Copy link
@SupSuper

SupSuper Oct 19, 2018

Author Member

Yeah I noticed and fixed it in e4084fa

{
if ((*bu)->getId() == prevOwner)
{
item->setPreviousOwner(*bu);
Expand All @@ -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));
}
Expand Down

0 comments on commit 0308485

Please sign in to comment.