Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 59 additions & 11 deletions lib/Inventory/inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "inventory.hpp"
#include "recipe.hpp"
#include <lib/proxyWeapon.hpp>
#include <lib/vehSupply.hpp>
#include <lib/world.hpp>
#include <lib/gameStateExt.hpp>
#include <lib/drawText.hpp>
Expand Down Expand Up @@ -377,7 +378,7 @@ Texture *InventoryItemType::GetAttachmentIcon(int i) const
}

InventoryItem::InventoryItem(const EntityAIType *type, bool fullCreate)
: base(type, fullCreate), _ui(false), _opened(true), _animationsEnabled(true), _isWorldAttached(false)
: base(type, fullCreate), _ui(false), _opened(true), _inventoryRotated(false), _animationsEnabled(true), _isWorldAttached(false)
{
int n = Type()->_attachments.Size();
_attachments.Realloc(n); _attachments.Resize(n);
Expand All @@ -400,6 +401,48 @@ int InventoryItem::GetObjSpecial(int shapeSpecial) const
return _ui ? shapeSpecial | DisableSun : shapeSpecial;
}

int InventoryItem::GetInventoryWidth() const
{
const InventoryItemType *type = Type();
if (!type) return 0;
return _inventoryRotated ? type->GetItemHeight() : type->GetItemWidth();
}

int InventoryItem::GetInventoryHeight() const
{
const InventoryItemType *type = Type();
if (!type) return 0;
return _inventoryRotated ? type->GetItemWidth() : type->GetItemHeight();
}

bool InventoryItem::RotateInInventory()
{
const InventoryItemType *type = Type();
if (!type || type->GetItemWidth() == type->GetItemHeight()) return false;

EntityAIFull *owner = dyn_cast<EntityAIFull>(GetHierarchyParent());
ResourceSupply *supply = owner ? owner->GetSupply() : NULL;
if (!supply) return false;

InventoryStorage &cargo = supply->GetItemsCargo();
const RefArray<InventoryItem> &items = cargo.GetItems();
int index = -1;
for (int i=0; i<items.Size(); i++)
{
if (items[i] == this) {index = i; break;}
}
if (index < 0) return false;

_inventoryRotated = !_inventoryRotated;
// the item has to fit into the free space it already occupies
if (cargo.CanAddItem(this, index) != index)
{
_inventoryRotated = !_inventoryRotated;
return false;
}
return true;
}

bool InventoryItem::CheckAttachmentCondition(const InventoryItem *attachment) const
{
const InventoryItemType *type = attachment->Type();
Expand Down Expand Up @@ -2178,6 +2221,7 @@ LSError InventoryItem::Serialize(ParamArchive &ar)
{
CHECK(base::Serialize(ar));
CHECK(ar.SerializeRefs("Attachments", _attachments, 1));
CHECK(ar.Serialize("InventoryRotated", _inventoryRotated, 1, false));
return LSOK;
}

Expand Down Expand Up @@ -2230,6 +2274,7 @@ TMError InventoryItem::TransferMsg(NetworkMessageContext &ctx)
TRANSF_EX(inLandscape, inLandscape);
bool isOn = IsOn();
TRANSF_EX(isOn, isOn);
TRANSF_EX(inventoryRotated, _inventoryRotated);
}
else
{
Expand All @@ -2251,6 +2296,8 @@ TMError InventoryItem::TransferMsg(NetworkMessageContext &ctx)
InventoryItem *item = _attachments[i];
if (item) item->ItemMoveOut(this);
}

TRANSF_EX(inventoryRotated, _inventoryRotated);
}
}

Expand Down Expand Up @@ -2283,6 +2330,7 @@ float InventoryItem::CalculateError(NetworkMessageContextWithError &ctx)
error += ERR_COEF_MODE * dif;

if ( message->_isOn!=IsOn() ) error += ERR_COEF_STRUCTURE;
if ( message->_inventoryRotated != _inventoryRotated ) error += ERR_COEF_MODE;
}
return error;
}
Expand Down Expand Up @@ -2424,8 +2472,8 @@ int InventoryStorage::ItemOn(int row, int col) const
const InventoryItemType *type = item->Type();
if (!type) continue;

int w = type->GetItemWidth();
int h = type->GetItemHeight();
int w = item->GetInventoryWidth();
int h = item->GetInventoryHeight();
DoAssert(c + w <= _width);
DoAssert(r + h <= _height);
if (c + w > col && r + h > row) return i - 1; // i was already incremented
Expand All @@ -2452,8 +2500,8 @@ void InventoryStorage::Render(StaticArrayAuto<bool> &image, const InventoryItem
const InventoryItemType *type = item->Type();
if (!type) continue;

int w = type->GetItemWidth();
int h = type->GetItemHeight();
int w = item->GetInventoryWidth();
int h = item->GetInventoryHeight();
DoAssert(col + w <= _width);
DoAssert(row + h <= _height);
// render the item
Expand All @@ -2472,10 +2520,10 @@ void InventoryStorage::Render(StaticArrayAuto<bool> &image, const InventoryItem
}
}

int InventoryStorage::FitsInto(const Array<bool> &image, const InventoryItemType *type, int index) const
int InventoryStorage::FitsInto(const Array<bool> &image, const InventoryItem *item, int index) const
{
int h = type->GetItemHeight();
int w = type->GetItemWidth();
int h = item->GetInventoryHeight();
int w = item->GetInventoryWidth();
if (index >= 0)
{
// check the given space
Expand Down Expand Up @@ -2543,7 +2591,7 @@ int InventoryStorage::CanAddItem(const InventoryItem *item, int index) const
AUTO_STATIC_ARRAY(bool, image, 256);
Render(image, item);

return FitsInto(image, type, index);
return FitsInto(image, item, index);
}

int InventoryStorage::CanReplaceItem(const InventoryItem *oldItem, const InventoryItem *newItem) const
Expand All @@ -2559,10 +2607,10 @@ int InventoryStorage::CanReplaceItem(const InventoryItem *oldItem, const Invento
Render(image, oldItem);

// check if the newItem fits on the same place
if (FitsInto(image, type, index) == index) return index;
if (FitsInto(image, newItem, index) == index) return index;

// try to find different place in the container
return FitsInto(image, type, -1);
return FitsInto(image, newItem, -1);
}

bool InventoryStorage::AddItem(EntityAI *owner, InventoryItem *item, int index)
Expand Down
16 changes: 15 additions & 1 deletion lib/Inventory/inventory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ TypeIsSimple(InventorySlotId)
XX(MessageName, bool, isOn, NDTBool, bool, NCTNone, DEFVALUE(bool, true), DOC_MSG("Device switched on"), TRANSF, ET_NOT_EQUAL, ERR_COEF_STRUCTURE) \
XX(MessageName, RefArray<InventoryItem>, attachments, NDTRefArray, AutoArray<NetworkId>, NCTNone, DEFVALUEREFARRAY, DOC_MSG("Attachments"), TRANSF_REFS, ET_NOT_EQUAL_COUNT, ERR_COEF_MODE) \
XX(MessageName, bool, inLandscape, NDTBool, bool, NCTNone, DEFVALUE(bool, false), DOC_MSG("Item is in landscape"), TRANSF, ET_NOT_EQUAL, ERR_COEF_IMMEDIATE) \
XX(MessageName, bool, inventoryRotated, NDTBool, bool, NCTNone, DEFVALUE(bool, false), DOC_MSG("Item is rotated in the inventory grid"), TRANSF, ET_NOT_EQUAL, ERR_COEF_MODE) \

DECLARE_NET_INDICES_EX_ERR(UpdateInventoryItem, UpdateVehicleAI, UPDATE_INVENTORY_ITEM_MSG)

Expand All @@ -177,6 +178,9 @@ class InventoryItem : public PoweredItem
// content of the container is accessible from the Inventory UI
bool _opened;

// item is rotated 90 degrees inside the inventory grid
bool _inventoryRotated;

Ref<Texture> _forcedTexture;

protected:
Expand Down Expand Up @@ -217,6 +221,16 @@ class InventoryItem : public PoweredItem
bool IsOpened() const {return _opened;}
void Open(bool open) {_opened = open;}

//! item rotation inside the inventory grid
bool IsInventoryRotated() const {return _inventoryRotated;}
void SetInventoryRotated(bool rotated) {_inventoryRotated = rotated;}
//! size of the item in the inventory grid, swapped when rotated
int GetInventoryWidth() const;
int GetInventoryHeight() const;
//! toggle the inventory rotation, keeping the item inside its container;
//! returns false when the rotation has no effect or the item would not fit
bool RotateInInventory();

//! Get parameters of action as structured texts
virtual bool GetActionParams(UIActionParams &params, const Action *action, AIBrain *unit) const override;
//! Perform action
Expand Down Expand Up @@ -432,7 +446,7 @@ class InventoryStorage

private:
// check the space for the item
int FitsInto(const Array<bool> &image, const InventoryItemType *type, int index) const;
int FitsInto(const Array<bool> &image, const InventoryItem *item, int index) const;
};

// Watch classes
Expand Down
Loading
Loading