summaryrefslogtreecommitdiff
path: root/src/game-server
diff options
context:
space:
mode:
Diffstat (limited to 'src/game-server')
-rw-r--r--src/game-server/inventory.cpp55
-rw-r--r--src/game-server/inventory.hpp30
2 files changed, 74 insertions, 11 deletions
diff --git a/src/game-server/inventory.cpp b/src/game-server/inventory.cpp
index b7e5e881..d2615f66 100644
--- a/src/game-server/inventory.cpp
+++ b/src/game-server/inventory.cpp
@@ -164,6 +164,22 @@ int Inventory::insert(int itemId, int amount)
return amount > 0 ? fillFreeSlot(itemId, amount, maxPerSlot) : 0;
}
+int Inventory::count(int itemId)
+{
+ int nb = 0;
+
+ for (std::vector< InventoryItem >::iterator i = poss.inventory.begin(),
+ i_end = poss.inventory.end(); i != i_end; ++i)
+ {
+ if (i->itemId == itemId)
+ {
+ nb += i->amount;
+ }
+ }
+
+ return nb;
+}
+
void Inventory::freeIndex(int i)
{
InventoryItem &it = poss.inventory[i];
@@ -223,6 +239,33 @@ int Inventory::remove(int itemId, int amount)
return amount;
}
+int Inventory::removeFromSlot(int slot, int amount)
+{
+ int i = getIndex(slot);
+
+ if (i < 0)
+ {
+ return amount;
+ }
+
+ InventoryItem &it = poss.inventory[i];
+ int nb = std::min((int)it.amount, amount);
+ it.amount -= nb;
+ amount -= nb;
+
+ msg.writeByte(slot + EQUIP_CLIENT_INVENTORY);
+ msg.writeShort(it.itemId);
+ msg.writeByte(nb);
+
+ // If the slot is empty, compress the inventory.
+ if (it.amount == 0)
+ {
+ freeIndex(i);
+ }
+
+ return amount;
+}
+
bool Inventory::equip(int slot)
{
int itemId = getItem(slot);
@@ -255,11 +298,9 @@ bool Inventory::equip(int slot)
msg.writeShort(itemId);
msg.writeByte(EQUIP_FIGHT2_SLOT);
msg.writeShort(0);
- msg.writeByte(slot + EQUIP_CLIENT_INVENTORY);
- msg.writeShort(0);
poss.equipment[EQUIP_FIGHT1_SLOT] = itemId;
poss.equipment[EQUIP_FIGHT2_SLOT] = 0;
- freeIndex(getIndex(slot));
+ removeFromSlot(slot, 1);
return true;
}
@@ -323,10 +364,8 @@ bool Inventory::equip(int slot)
// The first slot is full and the second slot is empty.
msg.writeByte(secondSlot);
msg.writeShort(itemId);
- msg.writeByte(slot + EQUIP_CLIENT_INVENTORY);
- msg.writeShort(0);
poss.equipment[secondSlot] = itemId;
- freeIndex(getIndex(slot));
+ removeFromSlot(slot, 1);
return true;
}
// no break!
@@ -338,10 +377,8 @@ bool Inventory::equip(int slot)
}
msg.writeByte(firstSlot);
msg.writeShort(itemId);
- msg.writeByte(slot + EQUIP_CLIENT_INVENTORY);
- msg.writeShort(0);
poss.equipment[firstSlot] = itemId;
- freeIndex(getIndex(slot));
+ removeFromSlot(slot, 1);
return true;
default:
diff --git a/src/game-server/inventory.hpp b/src/game-server/inventory.hpp
index 3640abc3..950dc013 100644
--- a/src/game-server/inventory.hpp
+++ b/src/game-server/inventory.hpp
@@ -94,16 +94,42 @@ class Inventory
*/
int remove(int itemId, int amount);
- int countItem(int itemId);
+ /**
+ * Removes some items from inventory.
+ * @return number of items not removed.
+ */
+ int removeFromSlot(int slot, int amount);
+
+ /**
+ * Counts number of items with given ID.
+ */
+ int count(int itemId);
+
+ /**
+ * Gets the ID of the items in a given slot.
+ */
+ int getItem(int slot);
+
private:
/**
* Fills some slots with items.
* @return number of items not inserted.
*/
int fillFreeSlot(int itemId, int amount, int MaxPerSlot);
+
+ /**
+ * Frees an inventory slot given by its real index.
+ */
void freeIndex(int index);
- int getItem(int slot);
+
+ /**
+ * Gets the real index associated to a slot.
+ */
int getIndex(int slot);
+
+ /**
+ * Gets the slot number of an inventory index.
+ */
int getSlot(int index);
};