summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--src/equipment.cpp20
-rw-r--r--src/equipment.h17
-rw-r--r--src/gui/equipmentwindow.cpp20
-rw-r--r--src/gui/equipmentwindow.h2
-rw-r--r--src/localplayer.cpp2
-rw-r--r--src/net/equipmenthandler.cpp8
7 files changed, 41 insertions, 34 deletions
diff --git a/ChangeLog b/ChangeLog
index 31895fba..c95e33ca 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,12 @@
* src/game.cpp: Do not activate shortcuts if tradewindow is visible.
* src/equipment.cpp: Remove an extra include and arrange headers like
the trunk client for easier merging.
+ * src/localplayer.cpp, src/gui/equipmentwindow.cpp,
+ src/gui/equipmentwindow.h, src/equipment.h,
+ src/net/equipmenthandler.cpp, src/equipment.cpp: Have equipment tied
+ into player inventory instead of keeping its own list of pointers to
+ items. This ensures the ressource is up-to-date and avoids crashes on
+ stale item pointers.
2008-11-04 Bjørn Lindeijer <bjorn@lindeijer.nl>
diff --git a/src/equipment.cpp b/src/equipment.cpp
index b54988a7..a8910d14 100644
--- a/src/equipment.cpp
+++ b/src/equipment.cpp
@@ -25,24 +25,20 @@
#include "equipment.h"
#include "item.h"
+#include "inventory.h"
+#include "localplayer.h"
Equipment::Equipment():
mArrows(0)
{
- std::fill_n(mEquipment, EQUIPMENT_SIZE, (Item*) 0);
+ std::fill_n(mEquipment, EQUIPMENT_SIZE, 0);
}
void
-Equipment::removeEquipment(Item *item)
+Equipment::setEquipment(int index, int inventoryIndex)
{
- Item **i = std::find(mEquipment, mEquipment + EQUIPMENT_SIZE, item);
- if (i != mEquipment + EQUIPMENT_SIZE) {
- *i = 0;
- }
-}
-
-void Equipment::setEquipment(int index, Item *item)
-{
- mEquipment[index] = item;
- item->setEquipped(true);
+ mEquipment[index] = inventoryIndex;
+ Item* item = player_node->getInventory()->getItem(inventoryIndex);
+ if (item)
+ item->setEquipped(true);
}
diff --git a/src/equipment.h b/src/equipment.h
index 80a2ae49..e3b10514 100644
--- a/src/equipment.h
+++ b/src/equipment.h
@@ -39,14 +39,14 @@ class Equipment
/**
* Get equipment at the given slot.
*/
- Item* getEquipment(int index) const
+ int getEquipment(int index) const
{ return mEquipment[index]; }
/**
* Set equipment at the given slot.
*/
void
- setEquipment(int index, Item *item);
+ setEquipment(int index, int inventoryIndex);
/**
* Remove equipment from the given slot.
@@ -55,25 +55,20 @@ class Equipment
removeEquipment(int index) { mEquipment[index] = 0; }
/**
- * Remove the given item from equipment.
- */
- void removeEquipment(Item *item);
-
- /**
* Get the item used in the arrow slot.
*/
- Item*
+ int
getArrows() { return mArrows; }
/**
* Set the item used in the arrow slot.
*/
void
- setArrows(Item *arrows) { mArrows = arrows; }
+ setArrows(int arrows) { mArrows = arrows; }
private:
- Item *mEquipment[EQUIPMENT_SIZE];
- Item *mArrows;
+ int mEquipment[EQUIPMENT_SIZE];
+ int mArrows;
};
#endif
diff --git a/src/gui/equipmentwindow.cpp b/src/gui/equipmentwindow.cpp
index 93b9ea37..3ea2fcbc 100644
--- a/src/gui/equipmentwindow.cpp
+++ b/src/gui/equipmentwindow.cpp
@@ -24,6 +24,8 @@
#include "equipmentwindow.h"
#include "../equipment.h"
+#include "../inventory.h"
+#include "../localplayer.h"
#include "../graphics.h"
#include "../item.h"
#include "../log.h"
@@ -40,6 +42,7 @@ EquipmentWindow::EquipmentWindow(Equipment *equipment):
setCloseButton(true);
setDefaultSize(5, 230, 200, 120);
loadWindowState();
+ inventory = player_node->getInventory();
}
EquipmentWindow::~EquipmentWindow()
@@ -61,22 +64,27 @@ void EquipmentWindow::draw(gcn::Graphics *graphics)
graphics->drawRectangle(gcn::Rectangle(10 + 36 * (i % 4),
36 * (i / 4) + 25, 32, 32));
- if (!(item = mEquipment->getEquipment(i)))
+ if (!(item = inventory->getItem(mEquipment->getEquipment(i))))
continue;
image = item->getImage();
- static_cast<Graphics*>(graphics)->drawImage(
- image, 36 * (i % 4) + 10, 36 * (i / 4) + 25);
+ if (image)
+ {
+ static_cast<Graphics*>(graphics)->drawImage(
+ image, 36 * (i % 4) + 10, 36 * (i / 4) + 25);
+ }
}
graphics->drawRectangle(gcn::Rectangle(160, 25, 32, 32));
- if (!(item = mEquipment->getArrows()))
+ if (!(item = inventory->getItem(mEquipment->getArrows())))
return;
image = item->getImage();
-
- static_cast<Graphics*>(graphics)->drawImage(image, 160, 25);
+ if (image)
+ {
+ static_cast<Graphics*>(graphics)->drawImage(image, 160, 25);
+ }
graphics->drawText(toString(item->getQuantity()), 170, 62,
gcn::Graphics::CENTER);
}
diff --git a/src/gui/equipmentwindow.h b/src/gui/equipmentwindow.h
index 99a3cc60..c853a039 100644
--- a/src/gui/equipmentwindow.h
+++ b/src/gui/equipmentwindow.h
@@ -25,6 +25,7 @@
#define _TMW_EQUIPMENT_H
#include "window.h"
+#include "../inventory.h"
class Equipment;
@@ -53,6 +54,7 @@ class EquipmentWindow : public Window
private:
Equipment *mEquipment;
+ Inventory *inventory;
};
extern EquipmentWindow *equipmentWindow;
diff --git a/src/localplayer.cpp b/src/localplayer.cpp
index 3caa5ead..bb91711c 100644
--- a/src/localplayer.cpp
+++ b/src/localplayer.cpp
@@ -144,7 +144,7 @@ void LocalPlayer::unequipItem(Item *item)
outMsg.writeInt16(item->getInvIndex());
// Tidy equipment directly to avoid weapon still shown bug, for instance
- mEquipment->removeEquipment(item);
+ mEquipment->removeEquipment(item->getInvIndex());
}
void LocalPlayer::useItem(Item *item)
diff --git a/src/net/equipmenthandler.cpp b/src/net/equipmenthandler.cpp
index 85790b42..eddc0ea0 100644
--- a/src/net/equipmenthandler.cpp
+++ b/src/net/equipmenthandler.cpp
@@ -87,7 +87,7 @@ void EquipmentHandler::handleMessage(MessageIn *msg)
position++;
}
item = inventory->getItem(index);
- player_node->mEquipment->setEquipment(position, item);
+ player_node->mEquipment->setEquipment(position, index);
}
}
break;
@@ -117,13 +117,13 @@ void EquipmentHandler::handleMessage(MessageIn *msg)
position++;
}
logger->log("Position %i", position);
- item = player_node->mEquipment->getEquipment(position);
+ item = player_node->getInventory()->getItem(player_node->mEquipment->getEquipment(position));
if (item) {
item->setEquipped(false);
}
item = inventory->getItem(index);
- player_node->mEquipment->setEquipment(position, item);
+ player_node->mEquipment->setEquipment(position, index);
break;
case SMSG_PLAYER_UNEQUIP:
@@ -192,7 +192,7 @@ void EquipmentHandler::handleMessage(MessageIn *msg)
break;
item->setEquipped(true);
- player_node->mEquipment->setArrows(item);
+ player_node->mEquipment->setArrows(index);
logger->log("Arrows equipped: %i", index);
break;
}