annotate src/parpg/inventory.py @ 105:7829eb185d6f

(Re)added setting of inventory and equipment in the object files.
author KarstenBock@gmx.net
date Thu, 22 Sep 2011 16:18:07 +0200
parents 1fd2201f5c36
children 7b21d460fc31
rev   line source
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
1 # This file is part of PARPG.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
2 #
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
3 # PARPG is free software: you can redistribute it and/or modify
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
6 # (at your option) any later version.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
7 #
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
8 # PARPG is distributed in the hope that it will be useful,
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
11 # GNU General Public License for more details.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
12 #
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
15
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
16 from parpg.objects.base import Container
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
17 from parpg.objects.composed import SingleItemContainer as Slot
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
18
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
19 import copy
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
20
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
21 # TODO: many missing function definitions in this code
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
22
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
23 class Inventory(Container):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
24 """The class to represent inventory 'model': allow operations with
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
25 inventory contents, perform weight/bulk calculations, etc"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
26 def __init__(self, **kwargs):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
27 """Initialise instance"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
28 Container.__init__(self, **kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
29 self.items = {"head": Slot(), "neck": Slot(),
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
30 "shoulders": Slot(), "chest": Slot(),
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
31 "abdomen": Slot(), "left_arm": Slot(),
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
32 "right_arm": Slot(),"groin": Slot(),
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
33 "hips": Slot(), "left_leg": Slot(),
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
34 "right_leg": Slot(), "left_hand": Slot(),
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
35 "right_hand": Slot(), "ready": Container(),
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
36 "backpack": Container()}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
37 for key, item in self.items.iteritems():
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
38 item.name = key
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
39 kwargs = {}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
40 kwargs["container"] = item
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
41 item.setScript("onPlaceItem", self.onChildPlaceItem, kwargs = kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
42 self.item_lookup = {}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
43
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
44 def onChildPlaceItem(self, container):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
45 for item in container.items.itervalues():
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
46 self.item_lookup[item.ID] = container.name
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
47
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
48 def placeItem(self, item, index=None):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
49 self.items["backpack"].placeItem(item, index)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
50 #self.item_lookup[item.ID] = "backpack"
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
51
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
52 def takeItem(self, item):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
53 if not item.ID in self.item_lookup:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
54 raise ValueError ('I do not contain this item: %s' % item)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
55 self.items[self.item_lookup[item.ID]].takeItem(item)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
56 del self.item_lookup[item.ID]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
57
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
58 def removeItem(self, item):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
59 if not item.ID in self.item_lookup:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
60 raise ValueError ('I do not contain this item: %s' % item)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
61 self.items[self.item_lookup[item.ID]].removeItem(item)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
62 del self.item_lookup[item.ID]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
63
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
64 def replaceItem(self, old_item, new_item):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
65 """Replaces the old item with the new one
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
66 @param old_item: Old item which is removed
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
67 @type old_item: Carryable
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
68 @param new_item: New item which is added
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
69 @type new_item: Carryable
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
70 """
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
71 if not old_item.ID in self.item_lookup:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
72 raise ValueError ('I do not contain this item: %s' % old_item)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
73 self.items[self.item_lookup[old_item.ID]]\
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
74 .replaceItem(old_item, new_item)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
75
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
76 def getWeight(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
77 """Total weight of all items in container + container's own weight"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
78 return sum((item.weight for item in self.items.values()))
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
79
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
80 def setWeightDummy(self, weight):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
81 pass
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
82
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
83 weight = property(getWeight, setWeightDummy, "Total weight of container")
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
84
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
85
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
86 def count(self, item_type = ""):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
87 return sum(item.count(item_type) for item in self.items.values())
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
88
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
89 def takeOff(self, item):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
90 return self.moveItemToSlot(item, "backpack")
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
91
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
92 def moveItemToSlot(self,item,slot,index=None):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
93 if not slot in self.items:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
94 raise(ValueError("%s: No such slot" % slot))
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
95
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
96 if item.ID in self.item_lookup:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
97 self.items[self.item_lookup[item.ID]].takeItem(item)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
98 try:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
99 self.items[slot].placeItem(item, index)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
100 except Container.SlotBusy:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
101 if index == None :
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
102 offending_item = self.items[slot].items[0]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
103 else :
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
104 offending_item = self.items[slot].items[index]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
105 self.items[slot].takeItem(offending_item)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
106 self.items[slot].placeItem(item, index)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
107 self.placeItem(offending_item)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
108 self.item_lookup[item.ID] = slot
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
109
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
110 def getItemsInSlot(self, slot, index=None):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
111 if not slot in self.items:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
112 raise(ValueError("%s: No such slot" % slot))
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
113 if index != None:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
114 return self.items[slot].items.get(index)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
115 else:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
116 return copy.copy(self.items[slot].items)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
117
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
118 def isSlotEmpty(self, slot, index=None):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
119 if not slot in self.items:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
120 raise(ValueError("%s: No such slot" % slot))
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
121 if index == None:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
122 return self.items[slot].count() == 0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
123 else:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
124 return not index in self.items[slot].items
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
125
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
126
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
127 def has(self, item_ID):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
128 return item_ID in self.item_lookup
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
129
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
130 def findItemByID(self, ID):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
131 if ID not in self.item_lookup:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
132 return None
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
133 return self.items[self.item_lookup[ID]].findItemByID(ID)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
134
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
135 def findItem(self, **kwargs):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
136 """Find an item in inventory by various attributes. All parameters
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
137 are optional.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
138 @type name: String
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
139 @param name: Object name. If the name is non-unique,
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
140 first matching object is returned
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
141 @type kind: String
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
142 @param kind: One of the possible object kinds like "openable" or
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
143 "weapon" (see base.py)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
144 @return: The item matching criteria or None if none was found"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
145 for slot in self.items:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
146 item_found = self.items[slot].findItem(**kwargs)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
147 if item_found != None:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
148 return item_found
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
149 return None
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
150
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
151 def __repr__(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
152 return "[Inventory contents: " + \
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
153 reduce((lambda a,b: str(a)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
154 + ', ' + str(b)),
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
155 self.items.values()) + " ]"
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
156
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
157 def serializeInventory(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
158 """Returns the inventory items as a list"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
159 inventory = []
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
160 inventory.extend(self.items["backpack"].serializeItems())
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
161 for key, slot in self.items.iteritems():
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
162 if key == "ready" or key == "backpack":
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
163 continue
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
164 elif len(slot.items) > 0:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
165 item = slot.items[0]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
166 item_dict = item.getStateForSaving()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
167 item_dict["slot"] = key
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
168 item_dict["type"] = type(item).__name__
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
169 inventory.append(item_dict)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
170 return inventory
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
171
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
172 def getStateForSaving(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
173 """Returns state for saving
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
174 """
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
175 state = {}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
176 state["Inventory"] = self.serializeInventory()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
177 return state