summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-01-03 20:54:03 +0000
committerGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-01-03 20:54:03 +0000
commit282ca19e33b79f4468ce6402406ddb1397f2d115 (patch)
tree8e9e3845353183c9838f7cce9eefcdbf566a9bd1
parenta372dc5340a1d5653ef5a60c65c5a1501a333c6b (diff)
downloadmanaserv-282ca19e33b79f4468ce6402406ddb1397f2d115.tar.gz
manaserv-282ca19e33b79f4468ce6402406ddb1397f2d115.tar.bz2
manaserv-282ca19e33b79f4468ce6402406ddb1397f2d115.tar.xz
manaserv-282ca19e33b79f4468ce6402406ddb1397f2d115.zip
Starting to work on adding items to the map. Nothing concrete for now, just a few simplifications. Fixed file end-of-lines along the way.
-rw-r--r--ChangeLog11
-rw-r--r--src/Makefile.am4
-rw-r--r--src/game-server/being.cpp20
-rw-r--r--src/game-server/being.hpp11
-rw-r--r--src/game-server/gamehandler.cpp8
-rw-r--r--src/game-server/inventory.cpp198
-rw-r--r--src/game-server/inventory.hpp103
-rw-r--r--src/game-server/item.cpp (renamed from src/item.cpp)10
-rw-r--r--src/game-server/item.hpp (renamed from src/item.h)138
-rw-r--r--src/game-server/itemmanager.cpp16
-rw-r--r--src/game-server/itemmanager.hpp43
-rw-r--r--src/game-server/mapcomposite.cpp2
-rw-r--r--src/game-server/mapcomposite.hpp6
-rw-r--r--src/game-server/player.cpp19
-rw-r--r--src/game-server/player.hpp23
15 files changed, 273 insertions, 339 deletions
diff --git a/ChangeLog b/ChangeLog
index d38516eb..45f40698 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -31,6 +31,17 @@
* src/defines.h: Fixed enumerations.
* src/games-server/gamehandler.cpp, src/game-server/player.cpp:
Commented in inventory code back.
+ * src/item.h, src/item.cpp: Moved to src/game-server directory and
+ changed header extension to hpp.
+ * src/game-server/itemmanager.hpp, src/game-server/itemmanager.cpp,
+ src/game-server/inventory.hpp, src/game-server/inventory.cpp,
+ src/game-server/player.hpp, src/game-server/gamehandler.cpp,
+ src/game-server/player.cpp: Simplified Inventory interface a bit.
+ Renamed Item to ItemClass to reflect its role. Added a Item class for
+ Objects lying on the world map.
+ * src/game-server/being.hpp: Removed counted pointers.
+ * src/game-server/mapcomposite.hpp, src/game-server/mapcomposite.cpp,
+ src/game-server/being.cpp: Fixed end of lines.
2007-01-02 Guillaume Melquiond <guillaume.melquiond@gmail.com>
diff --git a/src/Makefile.am b/src/Makefile.am
index 255b25e7..8f6f2c56 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -68,8 +68,6 @@ tmwserv_game_SOURCES = \
controller.h \
controller.cpp \
defines.h \
- item.h \
- item.cpp \
playerdata.hpp \
point.h \
resourcemanager.h \
@@ -84,6 +82,8 @@ tmwserv_game_SOURCES = \
game-server/gamehandler.cpp \
game-server/inventory.hpp \
game-server/inventory.cpp \
+ game-server/item.hpp \
+ game-server/item.cpp \
game-server/itemmanager.hpp \
game-server/itemmanager.cpp \
game-server/map.hpp \
diff --git a/src/game-server/being.cpp b/src/game-server/being.cpp
index 64986c26..da8f1f2c 100644
--- a/src/game-server/being.cpp
+++ b/src/game-server/being.cpp
@@ -24,16 +24,16 @@
#include "game-server/mapcomposite.hpp"
#include "utils/logger.h"
-void Being::damage(Damage damage)
-{
- int HPloss;
-
- HPloss = damage; // TODO: Implement complex damage calculation here
-
- mHitpoints -= HPloss;
- mHitsTaken.push_back(HPloss);
- LOG_DEBUG("Being " << getPublicID() << " got hit", 0);
-}
+void Being::damage(Damage damage)
+{
+ int HPloss;
+
+ HPloss = damage; // TODO: Implement complex damage calculation here
+
+ mHitpoints -= HPloss;
+ mHitsTaken.push_back(HPloss);
+ LOG_DEBUG("Being " << getPublicID() << " got hit", 0);
+}
void Being::performAttack(MapComposite *map)
{
diff --git a/src/game-server/being.hpp b/src/game-server/being.hpp
index 32b30b1e..327783ce 100644
--- a/src/game-server/being.hpp
+++ b/src/game-server/being.hpp
@@ -29,7 +29,6 @@
#include "defines.h"
#include "game-server/object.hpp"
-#include "utils/countedptr.h"
class MapComposite;
@@ -201,14 +200,4 @@ class Being : public MovingObject
Hits mHitsTaken; /**< List of punches taken since last update */
};
-/**
- * Type definition for a smart pointer to Being.
- */
-typedef utils::CountedPtr<Being> BeingPtr;
-
-/**
- * Type definition for a list of Beings.
- */
-typedef std::vector<BeingPtr> Beings;
-
#endif // _TMWSERV_BEING_H_
diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp
index f7a3905a..08e043f1 100644
--- a/src/game-server/gamehandler.cpp
+++ b/src/game-server/gamehandler.cpp
@@ -234,19 +234,19 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message)
case PGMSG_PICKUP:
{
// add item to inventory (this is too simplistic atm)
- unsigned int itemId = message.readLong();
+ int itemId = message.readLong();
// remove the item from world map
// send feedback
- computer.character->addItem(itemId);
+ computer.character->insertItem(itemId, 1);
result.writeShort(GPMSG_PICKUP_RESPONSE);
result.writeByte(ERRMSG_OK);
} break;
case PGMSG_USE_ITEM:
{
- unsigned int itemId = message.readLong();
+ int itemId = message.readLong();
result.writeShort(GPMSG_USE_RESPONSE);
@@ -273,7 +273,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message)
case PGMSG_EQUIP:
{
message.readLong(); // ItemId: Not useful, the inventory knows it
- char slot = message.readByte();
+ int slot = message.readByte();
result.writeShort(GPMSG_EQUIP_RESPONSE);
result.writeByte(computer.character->equip(slot) ?
diff --git a/src/game-server/inventory.cpp b/src/game-server/inventory.cpp
index b3b8a254..c5913e15 100644
--- a/src/game-server/inventory.cpp
+++ b/src/game-server/inventory.cpp
@@ -21,38 +21,37 @@
* $Id$
*/
+#include <cassert>
+
#include "game-server/inventory.hpp"
#include "game-server/itemmanager.hpp"
// ---------
// Items
// ---------
-unsigned char
-Inventory::getInventoryFreeSlot()
+int Inventory::getInventoryFreeSlot()
{
for (int a = 0; a < MAX_ITEMS_IN_INVENTORY; a++)
{
- if (itemList.at(a).amount == 0)
+ if (itemList[a].amount == 0)
return a;
}
return INVENTORY_FULL;
}
-unsigned char
-Inventory::getSlotFromId(const unsigned int itemId)
+int Inventory::getSlotFromId(int itemId)
{
for (int a = 0; a < MAX_ITEMS_IN_INVENTORY; a++)
{
- if (itemList.at(a).itemId == itemId)
+ if (itemList[a].itemId == itemId)
return a;
}
return INVENTORY_FULL;
}
-bool
-Inventory::hasItem(unsigned int itemId,
- bool searchInInventory,
- bool searchInEquipment)
+bool Inventory::hasItem(int itemId,
+ bool searchInInventory,
+ bool searchInEquipment)
{
bool hasItem = false;
// Search in inventory
@@ -82,44 +81,51 @@ Inventory::hasItem(unsigned int itemId,
return hasItem;
}
-short
-Inventory::addItem(unsigned int itemId, unsigned char amount)
+int Inventory::insertItem(int itemId, int amount)
{
+ if (amount <= 0)
+ {
+ return 0;
+ }
+
// We get the max number of item we can have in the same slot
// for the given item.
- unsigned char maxPerSlot = itemManager->getMaxPerSlot(itemId);
+ int maxPerSlot = itemManager->getItem(itemId)->getMaxPerSlot();
// We'll add items in slots with the item type and in free slots
// until it's all done or until the inventory will be all parsed.
// Searching for items with the same Id in the inventory, before
// seeking a free slot.
- unsigned char amountToAdd = amount;
+ int amountToAdd = amount;
// Parsing inventory
- unsigned char currentAmountInSlot = 0;
- std::vector<StoredItem>::iterator iter = itemList.begin();
- for (iter = itemList.begin(); iter != itemList.end(); ++iter)
+ for (std::vector< StoredItem >::iterator iter = itemList.begin(),
+ iter_end = itemList.end(); iter != iter_end; ++iter)
{
- currentAmountInSlot = iter->amount;
+ int currentAmountInSlot = iter->amount;
// If a slot has got place for some more of such an item.
if (iter->itemId == itemId && currentAmountInSlot < maxPerSlot)
{
// If there isn't enough space to put every item in the slot.
// We add the difference.
- if ((maxPerSlot - currentAmountInSlot) < amountToAdd)
+ int possibleAmount = maxPerSlot - currentAmountInSlot;
+ assert(possibleAmount > 0);
+ if (possibleAmount < amountToAdd)
{
- iter->amount += (maxPerSlot - currentAmountInSlot);
- amountToAdd -= (maxPerSlot - currentAmountInSlot);
+ iter->amount += possibleAmount;
+ amountToAdd -= possibleAmount;
}
else // there is enough to add everything.
{
iter->amount += amountToAdd;
amountToAdd = 0; // Ok!
+ break;
}
}
// Or if there is an empty slot.
else if (iter->amount == 0)
{
// We add the item in the new slot (setting also the Id.)
+ iter->itemId = itemId;
if (maxPerSlot < amountToAdd)
{
iter->amount = maxPerSlot;
@@ -129,28 +135,31 @@ Inventory::addItem(unsigned int itemId, unsigned char amount)
{
iter->amount += amountToAdd;
amountToAdd = 0; // Ok!
+ break;
}
- iter->itemId = itemId;
}
}
- return (short)(amount - amountToAdd);
+ return amount - amountToAdd;
}
-short
-Inventory::removeItem(unsigned int itemId, unsigned char amount)
+int Inventory::removeItemById(int itemId, int amount)
{
+ if (amount <= 0)
+ {
+ return 0;
+ }
+
// We'll remove items in slots with the item type
// until it's all done or until the inventory will be all parsed.
// Searching for items with the same Id in the inventory
- unsigned char amountToRemove = amount;
+ int amountToRemove = amount;
// Parsing inventory
- unsigned char currentAmountInSlot = 0;
- std::vector<StoredItem>::iterator iter = itemList.begin();
- for (iter = itemList.begin(); iter != itemList.end(); ++iter)
+ for (std::vector< StoredItem >::iterator iter = itemList.begin(),
+ iter_end = itemList.end(); iter != iter_end; ++iter)
{
- currentAmountInSlot = iter->amount;
+ int currentAmountInSlot = iter->amount;
// If a slot has got such an item, remove it
if (iter->itemId == itemId && currentAmountInSlot > 0)
{
@@ -166,76 +175,68 @@ Inventory::removeItem(unsigned int itemId, unsigned char amount)
{
iter->amount -= amountToRemove;
amountToRemove = 0; // Ok!
+ break;
}
}
}
- return (short)(amount - amountToRemove);
+ return amount - amountToRemove;
}
-short
-Inventory::removeItem(unsigned char slot, unsigned char amount)
+int Inventory::removeItemBySlot(int slot, int amount)
{
- if (itemList.at(slot).amount < amount)
+ if (amount <= 0)
{
- unsigned char value = itemList.at(slot).amount;
- itemList.at(slot).amount = 0;
- return (short)value;
+ return 0;
+ }
+
+ if (itemList[slot].amount < amount)
+ {
+ int value = itemList[slot].amount;
+ itemList[slot].amount = 0;
+ return value;
}
else
{
- itemList.at(slot).amount -= amount;
+ itemList[slot].amount -= amount;
return amount;
}
}
-bool
-Inventory::use(unsigned char slot, BeingPtr itemUser)
-{
- return false; // TODO
-}
-
-bool
-Inventory::use(unsigned int itemId, BeingPtr itemUser)
-{
- return false; // TODO
-}
-
// ---------
// Equipment
// ---------
-unsigned char
-Inventory::equipItem(unsigned int itemId)
+int Inventory::equipItem(int itemId)
{
// First, we look for the item in the player's inventory.
if (!hasItem(itemId, true, false))
return false;
- unsigned char availableSlots = 0, firstSlot = 0, secondSlot = 0;
+ int availableSlots = 0, firstSlot = 0, secondSlot = 0;
- unsigned short itemType = itemManager->getItemType(itemId);
+ int itemType = itemManager->getItem(itemId)->getType();
switch (itemType)
{
case ITEM_EQUIPMENT_TWO_HANDS_WEAPON:
// Special case 1, the two one-handed weapons are to be placed back
// in the inventory, if there are any.
- if (equippedItemList.at(EQUIP_FIGHT1_SLOT).itemId > 0)
+ if (equippedItemList[EQUIP_FIGHT1_SLOT].itemId > 0)
{ // Slot 1 full
// old two-handed weapon case:
- if (equippedItemList.at(EQUIP_FIGHT1_SLOT).itemType
+ if (equippedItemList[EQUIP_FIGHT1_SLOT].itemType
== ITEM_EQUIPMENT_TWO_HANDS_WEAPON)
{
- equippedItemList.at(EQUIP_FIGHT2_SLOT).itemId = 0;
- equippedItemList.at(EQUIP_FIGHT2_SLOT).itemType = 0;
+ equippedItemList[EQUIP_FIGHT2_SLOT].itemId = 0;
+ equippedItemList[EQUIP_FIGHT2_SLOT].itemType = 0;
}
- if (unequipItem_(equippedItemList.at(EQUIP_FIGHT1_SLOT).itemId,
- EQUIP_FIGHT1_SLOT) == INVENTORY_FULL)
+ if (unequipItem_(equippedItemList[EQUIP_FIGHT1_SLOT].itemId,
+ EQUIP_FIGHT1_SLOT) == INVENTORY_FULL)
return INVENTORY_FULL;
}
- if (equippedItemList.at(EQUIP_FIGHT2_SLOT).itemId > 0)
+ if (equippedItemList[EQUIP_FIGHT2_SLOT].itemId > 0)
{ // Slot 2 full
- if (unequipItem_(equippedItemList.at(EQUIP_FIGHT2_SLOT).itemId,
- EQUIP_FIGHT2_SLOT) == INVENTORY_FULL)
+ if (unequipItem_(equippedItemList[EQUIP_FIGHT2_SLOT].itemId,
+ EQUIP_FIGHT2_SLOT) == INVENTORY_FULL)
return INVENTORY_FULL;
}
// Only the slot 1 needs to be updated.
@@ -248,19 +249,18 @@ Inventory::equipItem(unsigned int itemId)
// Case 1: Reloading
if (equippedProjectiles.itemId == itemId)
{
- equippedProjectiles.amount += removeItem(itemId,
- (255 - equippedProjectiles.amount));
+ equippedProjectiles.amount +=
+ removeItemById(itemId, 255 - equippedProjectiles.amount);
return EQUIP_PROJECTILES_SLOT;
}
else // Case 2: Changing projectiles.
{
- short added;
- added = addItem(equippedProjectiles.itemId,
- equippedProjectiles.amount);
+ int added = insertItem(equippedProjectiles.itemId,
+ equippedProjectiles.amount);
if (added == equippedProjectiles.amount)
{ // Ok, we can equip
equippedProjectiles.itemId = itemId;
- equippedProjectiles.amount = removeItem(itemId, 255);
+ equippedProjectiles.amount = removeItemById(itemId, 255);
return EQUIP_PROJECTILES_SLOT;
}
else // Some were unequipped.
@@ -316,10 +316,10 @@ Inventory::equipItem(unsigned int itemId)
switch (availableSlots)
{
case 1:
- if (equippedItemList.at(firstSlot).itemId > 0)
+ if (equippedItemList[firstSlot].itemId > 0)
{
- if (unequipItem_(equippedItemList.at(firstSlot).itemId,
- firstSlot) != INVENTORY_FULL)
+ if (unequipItem_(equippedItemList[firstSlot].itemId,
+ firstSlot) != INVENTORY_FULL)
return equipItem_(itemId, itemType, firstSlot);
else
return INVENTORY_FULL;
@@ -331,25 +331,25 @@ Inventory::equipItem(unsigned int itemId)
break;
case 2:
- if (equippedItemList.at(firstSlot).itemId > 0)
+ if (equippedItemList[firstSlot].itemId > 0)
{
// If old weapon is two-handed one, we can unequip
// the first slot only, and clean the second.
- if (equippedItemList.at(firstSlot).itemId ==
+ if (equippedItemList[firstSlot].itemId ==
ITEM_EQUIPMENT_TWO_HANDS_WEAPON)
{
- if (unequipItem_(equippedItemList.at(firstSlot).itemId,
+ if (unequipItem_(equippedItemList[firstSlot].itemId,
firstSlot) != INVENTORY_FULL)
return equipItem_(itemId, itemType, firstSlot);
else
return INVENTORY_FULL;
}
- if (equippedItemList.at(secondSlot).itemId > 0)
+ if (equippedItemList[secondSlot].itemId > 0)
{ // Both slots are full,
// we remove the first one to equip
- if (unequipItem_(equippedItemList.at(firstSlot).itemId,
- firstSlot) != INVENTORY_FULL)
+ if (unequipItem_(equippedItemList[firstSlot].itemId,
+ firstSlot) != INVENTORY_FULL)
return equipItem_(itemId, itemType, firstSlot);
else
return INVENTORY_FULL;
@@ -370,48 +370,26 @@ Inventory::equipItem(unsigned int itemId)
}
}
-bool
-Inventory::equipItem(unsigned char inventorySlot, unsigned char equipmentSlot)
-{
- return false; // TODO
-}
-
-bool
-Inventory::unequipItem(unsigned int itemId)
-{
- return false; // TODO
-}
-
-bool
-Inventory::unequipItem(unsigned char inventorySlot,
- unsigned char equipmentSlot)
-{
- return false; // TODO
-}
-
-unsigned char
-Inventory::equipItem_(unsigned int itemId,
- unsigned int itemType,
- unsigned char equipmentSlot)
+int Inventory::equipItem_(int itemId,
+ int itemType,
+ int equipmentSlot)
{
- if (removeItem(itemId, 1) == 1)
+ if (removeItemById(itemId, 1) == 1)
{
- equippedItemList.at(equipmentSlot).itemId = itemId;
- equippedItemList.at(equipmentSlot).itemType = itemType;
+ equippedItemList[equipmentSlot].itemId = itemId;
+ equippedItemList[equipmentSlot].itemType = itemType;
return equipmentSlot;
}
else
return NO_ITEM_TO_EQUIP;
}
-unsigned char
-Inventory::unequipItem_(unsigned int itemId,
- unsigned char equipmentSlot)
+int Inventory::unequipItem_(int itemId, int equipmentSlot)
{
- if (addItem(itemId, 1) == 1)
+ if (insertItem(itemId, 1) == 1)
{
- equippedItemList.at(equipmentSlot).itemId = 0;
- equippedItemList.at(equipmentSlot).itemType = 0;
+ equippedItemList[equipmentSlot].itemId = 0;
+ equippedItemList[equipmentSlot].itemType = 0;
return equipmentSlot;
}
else
diff --git a/src/game-server/inventory.hpp b/src/game-server/inventory.hpp
index 61bc5818..1fa66373 100644
--- a/src/game-server/inventory.hpp
+++ b/src/game-server/inventory.hpp
@@ -70,7 +70,7 @@ enum
*/
struct StoredItem
{
- unsigned int itemId;
+ int itemId;
unsigned char amount;
};
@@ -79,7 +79,7 @@ struct StoredItem
*/
struct EquippedItem
{
- unsigned int itemId;
+ int itemId;
short itemType;
};
@@ -95,61 +95,50 @@ class Inventory
* Convenience function to get slot from ItemId.
* If more than one occurence is found, the first is given.
*/
- unsigned char
- getSlotFromId(unsigned int itemId);
+ int getSlotFromId(int itemId);
/**
- * Return StoredItem
+ * Returns item.
*/
- StoredItem
- getStoredItemAt(unsigned char slot) const { return itemList[slot]; };
+ StoredItem const &getStoredItemAt(int slot) const
+ { return itemList[slot]; };
/**
- * Search in inventory and equipment if an item is present.
+ * Looks in inventory and equipment whether an item is present or not.
*/
- bool
- hasItem(unsigned int itemId,
- bool searchInInventory = true,
- bool searchInEquipment = true);
+ bool hasItem(int itemId,
+ bool searchInInventory = true,
+ bool searchInEquipment = true);
/**
* Tells an item's amount
*/
- unsigned short
- getItemAmount(unsigned char slot) const { return itemList[slot].amount; };
+ int getItemAmount(int slot) const
+ { return itemList[slot].amount; };
/**
- * Return Item reference Id
+ * Returns item reference ID.
*/
- unsigned int
- getItemId(unsigned char slot) const { return itemList[slot].itemId; };
+ int getItemId(int slot) const
+ { return itemList[slot].itemId; };
/**
- * add an item with amount
- * (don't create it if amount was 0)
- * @return short value: Indicates the number of items added.
+ * Adds a given amount of items.
+ * @return Number of items really added.
*/
- short
- addItem(unsigned int itemId, unsigned char amount = 1);
+ int insertItem(int itemId, int amount = 1);
/**
- * Remove an item searched by ItemId.
- * Delete if amount = 0.
- * @return short value: Indicates the number of items removed.
- * This function removes the given amount using every slots
- * if necessary.
+ * Removes an item given by ID.
+ * @return Number of items really removed.
*/
- short
- removeItem(unsigned int itemId, unsigned char amount = 0);
+ int removeItemById(int itemId, int amount);
/**
- * Remove an item searched by slot index.
- * Delete if amount = 0.
- * @return short value: Indicates the number of items removed.
- * Removes only in the given slot.
+ * Removes an item given by slot.
+ * @return Number of items really removed.
*/
- short
- removeItem(unsigned char slot, unsigned char amount = 0);
+ int removeItemBySlot(int slot, int amount);
/**
* Equip an item searched by its id.
@@ -157,48 +146,30 @@ class Inventory
* @return unsigned char value: Returns the slot if successful
* or the error code if not.
*/
- unsigned char
- equipItem(unsigned int itemId);
+ int equipItem(int itemId);
/**
* Unequip an item searched by its id.
* Can unequip more than one item at a time.
*/
- bool
- unequipItem(unsigned int itemId);
+ bool unequipItem(int itemId);
/**
- * Equip an item searched by its slot index.
+ * Equips an item searched by its slot index.
*/
- bool
- equipItem(unsigned char inventorySlot, unsigned char equipmentSlot);
+ bool equipItem(int inventorySlot, int equipmentSlot);
/**
- * Unequip an equipped item searched by its slot index.
+ * Unequips an equipped item searched by its slot index.
*/
- bool
- unequipItem(unsigned char inventorySlot, unsigned char equipmentSlot);
-
- /**
- * The function called to use an item applying
- * only the modifiers
- */
- bool
- use(unsigned char slot, BeingPtr itemUser);
-
- /**
- * The function called to use an item applying
- * only the modifiers
- */
- bool
- use(unsigned int itemId, BeingPtr itemUser);
+ bool unequipItem(int inventorySlot, int equipmentSlot);
private:
/**
- * Give the first free slot number in itemList.
+ * Gives the first free slot number in itemList.
*/
- unsigned char getInventoryFreeSlot();
+ int getInventoryFreeSlot();
/**
* Quick equip an equipment with a given equipSlot,
@@ -206,9 +177,9 @@ class Inventory
* @return the equipment slot if successful,
* the error code, if not.
*/
- unsigned char equipItem_(unsigned int itemId,
- unsigned int itemType,
- unsigned char equipmentSlot);
+ int equipItem_(int itemId,
+ int itemType,
+ int equipmentSlot);
/**
* Quick unequip an equipment with a given equipSlot,
@@ -216,8 +187,8 @@ class Inventory
* @return the Equipment slot if successful,
* the error code, if not.
*/
- unsigned char unequipItem_(unsigned int itemId,
- unsigned char equipmentSlot);
+ int unequipItem_(int itemId,
+ int equipmentSlot);
// Stored items in inventory and equipment
diff --git a/src/item.cpp b/src/game-server/item.cpp
index 013c2c69..7eb9afb6 100644
--- a/src/item.cpp
+++ b/src/game-server/item.cpp
@@ -21,18 +21,18 @@
* $Id$
*/
-#include "item.h"
+#include "game-server/item.hpp"
-bool Item::use(BeingPtr itemUser)
+bool ItemClass::use(Being *itemUser)
{
bool usedSuccessfully = true;
// Applying Modifiers for a given lifetime
// TODO
// Calling a script if scriptName != ""
- if (mScriptName != "")
+ if (!mScriptName.empty())
{
- if(runScript(itemUser) && usedSuccessfully)
+ if (runScript(itemUser) && usedSuccessfully)
return true;
else
return false;
@@ -41,7 +41,7 @@ bool Item::use(BeingPtr itemUser)
return usedSuccessfully;
}
-bool Item::runScript(BeingPtr itemUser)
+bool ItemClass::runScript(Being *itemUser)
{
//TODO
return true;
diff --git a/src/item.h b/src/game-server/item.hpp
index 3ac9c68d..283e685c 100644
--- a/src/item.h
+++ b/src/game-server/item.hpp
@@ -21,16 +21,16 @@
* $Id$
*/
-#ifndef ITEM_H
-#define ITEM_H
+#ifndef _TMWSERV_ITEM
+#define _TMWSERV_ITEM
-#include "playerdata.hpp"
-#include "game-server/being.hpp"
+#include "game-server/player.hpp"
/**
* Enumeration of available Item types.
*/
-typedef enum ItemType {
+enum
+{
ITEM_UNUSABLE = 0,
ITEM_USABLE, // 1
ITEM_EQUIPMENT_ONE_HAND_WEAPON, // 2
@@ -49,7 +49,8 @@ typedef enum ItemType {
/**
* Enumeration of available weapon's types.
*/
-typedef enum WeaponType {
+enum
+{
WPNTYPE_NONE = 0,
WPNTYPE_KNIFE, // 1
WPNTYPE_SWORD, // 2
@@ -73,7 +74,8 @@ typedef enum WeaponType {
* States attribute effects to beings, and actors.
* States can be multiple for the same being.
*/
-typedef enum BeingStateEffect {
+enum
+{
STATE_NORMAL = 0,
STATE_POISONED,
STATE_STONED,
@@ -105,102 +107,126 @@ struct Modifiers
{
// General
unsigned char element; /**< Item Element */
- BeingStateEffect beingStateEffect; /**< Being State (dis)alteration */
+ unsigned char beingStateEffect; /**< Being State (dis)alteration */
unsigned short lifetime; /**< Modifiers lifetime in seconds. */
// Caracteristics Modifiers
short rawStats[NB_RSTAT]; /**< Raw Stats modifiers */
short computedStats[NB_CSTAT]; /**< Computed Stats modifiers */
- int hp; /**< HP modifier */
- int mp; /**< MP Modifier */
+ short hp; /**< HP modifier */
+ short mp; /**< MP Modifier */
// Weapon
unsigned short range; /**< Weapon Item Range */
- WeaponType weaponType; /**< Weapon Type enum */
+ unsigned char weaponType; /**< Weapon Type enum */
};
/**
* Class for simple reference to item information.
- * See WorldItem to get full featured Item Objects.
*/
-class Item
+class ItemClass
{
public:
-
- Item(Modifiers modifiers,
- unsigned short itemType = 0,
- unsigned int weight = 0,
- unsigned int value = 0,
- std::string scriptName = "",
- unsigned short maxPerSlot = 0):
- mItemType(itemType),
- mWeight(weight),
- mValue(value),
- mScriptName(scriptName),
- mMaxPerSlot(maxPerSlot),
- mModifiers(modifiers) {}
-
- virtual ~Item() throw() { }
+ ItemClass(int type)
+ : mType(type)
+ {}
/**
* The function called to use an item applying
* only the modifiers (for simple items...)
*/
- bool use(BeingPtr itemUser);
+ bool use(Being *itemUser);
/**
- * Return item Type
+ * Gets item type.
*/
- unsigned short getItemType() const { return mItemType; };
+ int getType() const
+ { return mType; }
/**
- * Return Weight of item
+ * Gets item weight.
*/
- unsigned int getWeight() const { return mWeight; };
+ int getWeight() const
+ { return mWeight; }
/**
- * Return gold value of item
+ * Sets item weight.
*/
- unsigned int getGoldValue() const { return mValue; };
+ void setWeight(int weight)
+ { mWeight = weight; }
/**
- * Return max item per slot
+ * Gets unit cost of these items.
*/
- unsigned short getMaxPerSlot() const { return mMaxPerSlot; };
+ int getCost() const
+ { return mCost; }
/**
- * Return item's modifiers
+ * Sets unit cost of these items.
*/
- Modifiers
- getItemModifiers() const { return mModifiers; };
+ void setCost(int cost)
+ { mCost = cost; }
+
+ /**
+ * Gets max item per slot.
+ */
+ int getMaxPerSlot() const
+ { return mMaxPerSlot; }
+
+ /**
+ * Sets max item per slot.
+ */
+ void setMaxPerSlot(int perSlot)
+ { mMaxPerSlot = perSlot; }
+
+ /**
+ * Gets item modifiers.
+ */
+ Modifiers const &getModifiers() const
+ { return mModifiers; }
+
+ /**
+ * Sets item modifiers.
+ */
+ void setModifiers(Modifiers const &modifiers)
+ { mModifiers = modifiers; }
+
+ /**
+ * Sets associated script name.
+ */
+ void setScriptName(std::string const &name)
+ { mScriptName = name; }
private:
/**
* Runs the associated script when using the item, if any.
*/
- bool runScript(BeingPtr itemUser);
+ bool runScript(Being *itemUser);
// Item reference information
- unsigned short mItemType; /**< ItemType: Usable, equipment */
- unsigned int mWeight; /**< Weight of the item */
- unsigned int mValue; /**< Gold value of the item */
- std::string mScriptName; /**< item's script. None if =="" */
- unsigned short mMaxPerSlot; /**< Max item amount per slot in inventory */
-
- Modifiers mModifiers; /**< Item's Modifiers */
+ unsigned char mType; /**< Type: usable, equipment. */
+ unsigned short mWeight; /**< Weight of the item. */
+ unsigned short mCost; /**< Unit cost the item. */
+ unsigned short mMaxPerSlot; /**< Max item amount per slot in inventory. */
+ std::string mScriptName; /**< Item script. */
+ Modifiers mModifiers; /**< Item modifiers. */
};
-/**
- * Type definition for a smart pointer to Item.
- */
-typedef utils::CountedPtr<Item> ItemPtr;
+class Item: public Object
+{
+ public:
+ Item(ItemClass *type)
+ : Object(OBJECT_ITEM), mType(type)
+ {}
-/**
- * Type definition for a list of Items.
- */
-typedef std::vector<ItemPtr> Items;
+ ItemClass *getItemClass() const
+ { return mType; }
+
+ private:
+ ItemClass *mType;
+};
#endif
diff --git a/src/game-server/itemmanager.cpp b/src/game-server/itemmanager.cpp
index 01b957a6..f4a5b751 100644
--- a/src/game-server/itemmanager.cpp
+++ b/src/game-server/itemmanager.cpp
@@ -98,13 +98,17 @@ ItemManager::ItemManager(std::string const &itemReferenceFile)
modifiers.hp = XML::getProperty(node, "hp", 0);
modifiers.mp = XML::getProperty(node, "mp", 0);
modifiers.range = XML::getProperty(node, "range", 0);
- modifiers.weaponType = (WeaponType)XML::getProperty(node, "weapon_type", 0);
- modifiers.beingStateEffect = (BeingStateEffect)XML::getProperty(node, "status_effect", 0);
-
- ItemPtr item(new Item(modifiers, itemType, weight,
- value, scriptName, maxPerSlot));
+ modifiers.weaponType = XML::getProperty(node, "weapon_type", 0);
+ modifiers.beingStateEffect = XML::getProperty(node, "status_effect", 0);
+
+ ItemClass *item = new ItemClass(itemType);
+ item->setWeight(weight);
+ item->setCost(value);
+ item->setMaxPerSlot(maxPerSlot);
+ item->setScriptName(scriptName);
+ item->setModifiers(modifiers);
mItemReference[id] = item;
- nbItems++;
+ ++nbItems;
if (maxPerSlot == 0)
{
diff --git a/src/game-server/itemmanager.hpp b/src/game-server/itemmanager.hpp
index a5ebc8f3..64bf5ae2 100644
--- a/src/game-server/itemmanager.hpp
+++ b/src/game-server/itemmanager.hpp
@@ -26,12 +26,11 @@
#include <map>
-#include "item.h"
+#include "game-server/item.hpp"
/**
* The Item Manager loads the item reference database
* and also offers an API to items information, and more.
- * For item objects, see the WorldItem class.
*/
class ItemManager
{
@@ -39,50 +38,16 @@ class ItemManager
/**
* Constructor (loads item reference file)
*/
- ItemManager(const std::string &itemReferenceFile);
+ ItemManager(std::string const &itemReferenceFile);
/**
* Gives an Item having the demanded information.
*/
- ItemPtr getItem(const unsigned int itemId)
+ ItemClass *getItem(int itemId)
{ return mItemReference[itemId]; };
- bool use(BeingPtr beingPtr, const unsigned int itemId)
- { return mItemReference[itemId].get()->use(beingPtr); };
-
- /**
- * Return item Type
- */
- unsigned short getItemType(const unsigned int itemId)
- { return mItemReference[itemId].get()->getItemType(); };
-
- /**
- * Return Weight of item
- */
- unsigned int getWeight(const unsigned int itemId)
- { return mItemReference[itemId].get()->getWeight(); };
-
- /**
- * Return gold value of item
- */
- unsigned int getGoldValue(const unsigned int itemId)
- { return mItemReference[itemId].get()->getGoldValue(); };
-
- /**
- * Return max item per slot
- */
- unsigned short getMaxPerSlot(const unsigned int itemId)
- { return mItemReference[itemId].get()->getMaxPerSlot(); };
-
- /**
- * Return item's modifiers
- */
- Modifiers
- getItemModifiers(const unsigned int itemId)
- { return mItemReference[itemId].get()->getItemModifiers(); };
-
private:
- std::map<unsigned int, ItemPtr> mItemReference; /**< Item reference */
+ std::map< int, ItemClass * > mItemReference; /**< Item reference */
};
extern ItemManager *itemManager;
diff --git a/src/game-server/mapcomposite.cpp b/src/game-server/mapcomposite.cpp
index 1b824447..21648bc1 100644
--- a/src/game-server/mapcomposite.cpp
+++ b/src/game-server/mapcomposite.cpp
@@ -418,7 +418,7 @@ ZoneIterator MapComposite::getAroundPlayerIterator(MovingObject *obj, int radius
}
fillRegion(r2, obj->getPosition(), radius);
return ZoneIterator(r2, this);
-}
+}
bool MapComposite::insert(Thing *ptr)
{
diff --git a/src/game-server/mapcomposite.hpp b/src/game-server/mapcomposite.hpp
index 526b36e2..1996950e 100644
--- a/src/game-server/mapcomposite.hpp
+++ b/src/game-server/mapcomposite.hpp
@@ -23,7 +23,7 @@
#ifndef _TMW_SERVER_MAPCOMPOSITE_
#define _TMW_SERVER_MAPCOMPOSITE_
-
+
#include <vector>
class Map;
@@ -183,12 +183,12 @@ class MapComposite {
/**
* Gets an iterator on the objects inside a given rectangle.
*/
- ZoneIterator getInsideRectangleIterator(Rectangle const &) const;
+ ZoneIterator getInsideRectangleIterator(Rectangle const &) const;
/**
* Gets an iterator on the objects around a given object.
*/
- ZoneIterator getAroundObjectIterator(Object *, int radius) const;
+ ZoneIterator getAroundObjectIterator(Object *, int radius) const;
/**
* Gets an iterator on the objects around the old and new positions of
diff --git a/src/game-server/player.cpp b/src/game-server/player.cpp
index 294239f9..99884661 100644
--- a/src/game-server/player.cpp
+++ b/src/game-server/player.cpp
@@ -56,32 +56,27 @@ void Player::update()
}
}
-void Player::setInventory(const Inventory &inven)
+bool Player::insertItem(int itemId, int amount)
{
- inventory = inven;
+ return inventory.insertItem(itemId, amount);
}
-bool Player::addItem(unsigned int itemId, unsigned char amount)
+bool Player::removeItem(int itemId, int amount)
{
- return inventory.addItem(itemId, amount);
+ return inventory.removeItemById(itemId, amount);
}
-bool Player::removeItem(unsigned int itemId, unsigned char amount)
-{
- return inventory.removeItem(itemId, amount);
-}
-
-bool Player::hasItem(unsigned int itemId)
+bool Player::hasItem(int itemId)
{
return inventory.hasItem(itemId);
}
-bool Player::equip(unsigned char slot)
+bool Player::equip(int slot)
{
return false; // TODO
}
-bool Player::unequip(unsigned char slot)
+bool Player::unequip(int slot)
{
return false; // TODO
}
diff --git a/src/game-server/player.hpp b/src/game-server/player.hpp
index 4ecef616..eaf4d9e9 100644
--- a/src/game-server/player.hpp
+++ b/src/game-server/player.hpp
@@ -46,53 +46,48 @@ class Player : public Being, public PlayerData
/**
* Updates the internal status.
*/
- void update();
-
+ void update();
+
/**
* Sets inventory.
*/
- void
- setInventory(const Inventory &inven);
+ void setInventory(Inventory const &inven)
+ { inventory = inven; }
/**
* Adds item with ID to inventory.
*
* @return Item add success/failure
*/
- bool
- addItem(unsigned int itemId, unsigned char amount = 1);
+ bool insertItem(int itemId, int amount);
/**
* Removes item with ID from inventory.
*
* @return Item delete success/failure
*/
- bool
- removeItem(unsigned int itemId, unsigned char amount = 0);
+ bool removeItem(int itemId, int amount);
/**
* Checks if character has an item.
*
* @return true if being has item, false otherwise
*/
- bool
- hasItem(unsigned int itemId);
+ bool hasItem(int itemId);
/**
* Equips item with ID in equipment slot.
*
* @return Equip success/failure
*/
- bool
- equip(unsigned char slot);
+ bool equip(int slot);
/**
* Un-equips item.
*
* @return Un-equip success/failure
*/
- bool
- unequip(unsigned char slot);
+ bool unequip(int slot);
/**
* Set attacking state