Planeshift
pscharinventory.h
Go to the documentation of this file.
1 /*
2  * pscharinventory.h by Keith Fulton <keith@paqrat.com>
3  *
4  * Copyright (C) 2007 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation (version 2 of the License)
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 
20 #ifndef __PSCHARINV_H__
21 #define __PSCHARINV_H__
22 
23 //=============================================================================
24 // Crystal Space Includes
25 //=============================================================================
26 #include <csutil/sysfunc.h>
27 #include <csutil/weakref.h>
28 
29 //=============================================================================
30 // Project Includes
31 //=============================================================================
32 #include "util/poolallocator.h"
33 #include "util/psconst.h"
34 
35 #include "../icachedobject.h"
36 
37 //=============================================================================
38 // Local Includes
39 //=============================================================================
40 #include "psitemstats.h"
41 #include "psstdint.h"
42 #include "psitem.h"
43 #include "psinventorycachesvr.h"
44 #include "psspell.h"
45 
46 struct iDocumentNode;
47 
48 class MsgEntry;
49 class psItemStats;
50 class psItem;
51 class gemContainer;
52 struct psRaceInfo;
53 class gemActor;
54 
59 #define PSCHARACTER_EQUIPMENTFLAG_AUTOATTACK 0x00000001
61 #define PSCHARACTER_EQUIPMENTFLAG_SINGLEATTACK 0x00000002
63 #define PSCHARACTER_EQUIPMENTFLAG_ATTACKIFEMPTY 0x00000004
65 
66 
67 //-----------------------------------------------------------------------------
68 
71 {
74 };
75 
76 
77 //-----------------------------------------------------------------------------
78 
82 {
83 public:
86 
88  {
89  friend class psCharacterInventory;
90  psItem* item;
91  public:
93  {
94  item = it;
95  exchangeOfferSlot = -1;
96  exchangeStackCount = 0;
97  }
101  {
102  return item;
103  }
104  };
105 
106 protected:
107 
109  csArray<psCharacterInventoryItem> inventory;
110 
112  csArray<psItem*> storageInventory;
113 
114  struct psEquipInfo
115  {
116  unsigned int EquipmentFlags;
117  int eventId;
120  };
121 
123  psEquipInfo equipment[PSCHARACTER_SLOT_BULK1]; // bulk1 is the first slot above equipment
124 
127 
129  bool loaded;
130 
132  float maxWeight;
133 
135  float maxSize;
136 
139 
141  uint32 version;
142 
143 public:
144 
147 
149  bool Load();
150 
160  bool Load(PID id);
161 
165  bool QuickLoad(PID id);
166 
173  bool HasEnoughUnusedSpace(float desiredSpace);
174 
181  int GetCurrentTotalSpace();
182 
188  int GetCurrentMaxSpace();
189 
196  void AddStorageItem(psItem* &item);
197 
204  bool HasEnoughUnusedWeight(float requiredWeight);
205  float GetCurrentTotalWeight();
206 
213  size_t HowManyCanFit(psItem* item);
214 
217 
221  float MaxWeight()
222  {
223  return maxWeight;
224  }
225 
228 
230  // float Weight() { return 0; }
231 
235  float GetTotalSizeOfItemsInContainer(psItem* container=NULL);
236 
240  uint32 GetInventoryVersion() const
241  {
242  return version;
243  }
244 
249  {
250  version++;
251  }
252 
268  bool Add(psItem* &item, bool test = false, bool stack = true,
269  INVENTORY_SLOT_NUMBER slot = PSCHARACTER_SLOT_NONE, gemContainer* container = NULL, bool precise = true, bool skip_requirements = false);
270 
278  psItem* AddStacked(psItem* &item, int &added);
279 
283  bool AddOrDrop(psItem* &item, bool stack = true);
284 
292  psItem* GetItem(psItem* container, INVENTORY_SLOT_NUMBER slot);
293 
306  bool hasItemsWithName(csString &itemname, bool includeEquipment, bool includeBulk, int amountMin = 1, int amountMax = -1, float qualityMin = 0, float qualityMax = 0);
307 
314  psItem* GetInventoryItem(INVENTORY_SLOT_NUMBER slot);
315 
321  void RestoreAllInventoryQuality();
322 
326  psItem* GetItemHeld();
327 
328  psCharacterInventoryItem* GetCharInventoryItem(INVENTORY_SLOT_NUMBER slot);
329 
336  INVENTORY_SLOT_NUMBER FindSlotHoldingItem(psItem* item);
337 
341  size_t FindItemStatIndex(psItemStats* itemstats,size_t startAt=1);
342 
351  psItem* RemoveItem(psItem* container, INVENTORY_SLOT_NUMBER bulkslot, int count = -1);
352 
361  psItem* RemoveItemID(uint32 itemID,int count = -1, bool storage = false);
362 
370  psItem* RemoveInventoryItem(INVENTORY_SLOT_NUMBER slot, int count = -1);
371 
372 
379  unsigned int TotalStackOfItem(psItemStats* item);
380 
388  psItem* FindItemID(uint32 itemID, bool storage = false);
389 
390  void SetExchangeOfferSlot(psItem* Container,INVENTORY_SLOT_NUMBER slot,int toSlot,int stackCount);
391  int GetOfferedStackCount(psItem* item);
392  psCharacterInventoryItem* FindExchangeSlotOffered(int slotID);
393  void PurgeOffered();
394 
401  bool HaveKeyForLock(uint32 lock);
402 
409  bool CanItemAttack(INVENTORY_SLOT_NUMBER slot);
410 
417  bool IsItemAutoAttack(INVENTORY_SLOT_NUMBER slot);
418 
426  psEquipInfo &GetEquipmentObject(INVENTORY_SLOT_NUMBER idx);
427 
437  psItem* GetEffectiveWeaponInSlot(INVENTORY_SLOT_NUMBER slot, bool includeShield = false);
438 
445  psItem* GetEffectiveArmorInSlot(INVENTORY_SLOT_NUMBER slot);
446 
456  bool hasItemCategory(psItemCategory* category, bool includeEquipment, bool includeBulk, bool includeStorage = false);
457 
471  bool hasItemCategory(csString &categoryname, bool includeEquipment, bool includeBulk, bool includeStorage = false, int amountMin = 1, int amountMax = -1 , float qualityMin = 0, float qualityMax = 0);
472 
481  csArray<psItem*> GetItemsInCategory(psItemCategory* category, bool storage = false);
482 
491  psItem* StackNumberItems(const csString &itemname, int count, bool container);
492 
493 
494  size_t GetContainedItemCount(psItem* container);
495  float GetContainedWeight(psItem* container);
496  float GetContainedSize(psItem* container);
497 
499  void CalculateLimits();
500 
502  void RunEquipScripts();
503 
505  csString lastError;
506 
508  void SetDoRestrictions(bool v);
509 
512  {
513  return doRestrictions;
514  }
515 
518  {
519  return &inventoryCacheServer;
520  }
521 
523  bool HasPurifiedGlyphs(const glyphList_t glyphsToCheck);
524 
526  void CreateGlyphList(csArray <glyphSlotInfo> &slots);
527 
536  bool AddLoadedItem(uint32 parentID, INVENTORY_SLOT_NUMBER slot, psItem* item);
537 
538  void Equip(psItem* item);
539  void Unequip(psItem* item);
540  bool CheckSlotRequirements(psItem* item, INVENTORY_SLOT_NUMBER proposedSlot, unsigned short stackCount = 0);
541 
549  bool EquipItem(psItem* newItem, INVENTORY_SLOT_NUMBER slotID);
550 
554  INVENTORY_SLOT_NUMBER FindFreeEquipSlot(psItem* itemToPlace);
555 
562  {
563  return inventory.GetSize();
564  }
565 
572  {
573  return inventory[which].item;
574  }
575 
578  {
579  return &inventory[index];
580  }
581 
583  void WriteAllInventory(iDocumentNode* npcRoot);
584 
594  psItem* RemoveItemIndex(size_t index,int count=-1, bool storage = false);
595 
597  bool BeginExchange();
598 
600  void CommitExchange();
601 
603  void RollbackExchange();
604 
606  void UpdateEncumbrance();
607 
614  void SetBasicArmor(psRaceInfo* race = NULL);
615 
622  void SetBasicWeapon(psRaceInfo* race = NULL);
623 
627  static psItem* GetItemFactory(psItemStats* stats);
628 
629 private:
630  void WriteItem(csRef<iDocumentNode> equipmentNode, psItem* item, int bulk, INVENTORY_SLOT_NUMBER slot);
631 
632  size_t GetItemIndex(psItem* item);
633 
638  size_t FindSlotIndex(psItem* container,INVENTORY_SLOT_NUMBER slot);
639 
643  int FindFirstOpenSlot(psItem* container);
644 
649  size_t FindCompatibleStackedItem(psItem* item, bool checkStackCount = true);
650 
660  csArray<size_t> FindCompatibleStackedItems(psItem* item, bool checkStackCount = true, bool precise = true);
661 
662  psInventoryCacheServer inventoryCacheServer;
663 
664 };
665 
667 {
668 public:
670  {
671  myInv = what;
672  committed = false;
673  myInv->BeginExchange();
674  }
676  {
677  if(!committed && myInv)
678  myInv->RollbackExchange();
679  }
680  void Commit()
681  {
682  myInv->CommitExchange();
683  committed = true;
684  }
685  void Cancel()
686  {
687  myInv = NULL; // character deleted or somesuch
688  }
689 
690 protected:
692  bool committed;
693 };
694 
697 #endif
698 
psCharacterInventory * myInv
This class handles the details behind a character&#39;s inventory system.
psInventoryCacheServer * GetInventoryCacheServer(void)
Get the inventory cache handler.
bool GetDoRestrictions()
Get the status of the weight/capacity restrictions.
float maxSize
Max capacity in inventory.
uint32 GetInventoryVersion() const
void IncreaseInventoryVersion()
Updates (increases) the current inventory version.
bool BeginExchange()
Copies current inventory to backup inventory.
size_t GetInventoryIndexCount()
Return a count of items in the base inventory array.
csArray< psItem * > storageInventory
Array with all the items stored by the character.
INVENTORY_SLOT_NUMBER
Holds a list of the possible socket identifiers that items can be attached to.
Definition: slots.h:32
The structure of 1 queue entry (pointer to a message)
Definition: message.h:143
psItemStats * glyphType
csString lastError
Contain error messge for last function failing.
csArray< psItemStats * > glyphList_t
Definition: psspell.h:66
Defines characteristics of a race associated to an NPC, player or pet.
Definition: psraceinfo.h:58
bool loaded
Has this inventory been fully loaded yet?
Any semi-autonomous object, either a player or an NPC.
Definition: gem.h:1170
gemContainers are the public containers in the world for crafting, like forges or ovens...
Definition: gem.h:935
The psInventoryCacheServer class implements the inventory cache on the server.
used by psCharacter::CreateGlyphList()
InventoryTransaction(psCharacterInventory *what)
int exchangeOfferSlot
Slot in exchange offer window on client.
psCharacter * owner
Current owner of the inventory.
uint32 version
Inventory version.
bool inExchangeMode
Exchange mode flag.
psCharacterInventoryItem * GetIndexCharInventoryItem(size_t index)
Returns the exchange info and the item in the index specified.
This huge class stores all the properties of any object a player can have in the game.
Definition: psitemstats.h:463
psItem * GetInventoryIndexItem(size_t which)
Return the psItem at the inventory index specified.
float maxWeight
Max weight the inventory can hold.
int purifyStatus
possible values are: 0=not purified 1=purifying 2=purified
bool doRestrictions
Do weight/capacity restrictions?
csArray< psCharacterInventoryItem > inventory
Flat array of all items owned in this inventory.
int exchangeStackCount
count of items being offered, subset of stack in *item
This class embodies item instances in the game.
Definition: psitem.h:238