annotate inventory.py @ 189:4381be70e1bb tip

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