annotate components/container.py @ 99:6e4daff93e7d

If Items in containers or being equipped have no type the game will now try to get the object using the ID.
author KarstenBock@gmx.net
date Wed, 28 Sep 2011 12:58:18 +0200
parents f94d4577ca5e
children
rev   line source
12
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
1 # This program is free software: you can redistribute it and/or modify
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
2 # it under the terms of the GNU General Public License as published by
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
3 # the Free Software Foundation, either version 3 of the License, or
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
4 # (at your option) any later version.
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
5 #
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
6 # This program is distributed in the hope that it will be useful,
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
9 # GNU General Public License for more details.
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
10 #
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
11 # You should have received a copy of the GNU General Public License
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
13
95
d67e8288e9bf Added saveable_fields to Container.
KarstenBock@gmx.net
parents: 89
diff changeset
14 from copy import deepcopy
d67e8288e9bf Added saveable_fields to Container.
KarstenBock@gmx.net
parents: 89
diff changeset
15
89
939984cff702 Added Base component, which has a saveable_fields property. It is supposed to be derived from, thus it is not in the components list.
KarstenBock@gmx.net
parents: 77
diff changeset
16 from base import Base
12
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
17
89
939984cff702 Added Base component, which has a saveable_fields property. It is supposed to be derived from, thus it is not in the components list.
KarstenBock@gmx.net
parents: 77
diff changeset
18 class Container(Base):
12
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
19 """
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
20 Component that allows an entity to contain one or more child entities.
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
21 """
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
22
9c7a96c6fe41 Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
23 def __init__(self):
89
939984cff702 Added Base component, which has a saveable_fields property. It is supposed to be derived from, thus it is not in the components list.
KarstenBock@gmx.net
parents: 77
diff changeset
24 Base.__init__(self, children=list, max_bulk=int)
22
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
25
95
d67e8288e9bf Added saveable_fields to Container.
KarstenBock@gmx.net
parents: 89
diff changeset
26 @property
d67e8288e9bf Added saveable_fields to Container.
KarstenBock@gmx.net
parents: 89
diff changeset
27 def saveable_fields(self):
97
f94d4577ca5e saveable_fields property of components is now a List.
KarstenBock@gmx.net
parents: 95
diff changeset
28 fields = self.fields.keys()
f94d4577ca5e saveable_fields property of components is now a List.
KarstenBock@gmx.net
parents: 95
diff changeset
29 fields.remove("children")
95
d67e8288e9bf Added saveable_fields to Container.
KarstenBock@gmx.net
parents: 89
diff changeset
30 return fields
d67e8288e9bf Added saveable_fields to Container.
KarstenBock@gmx.net
parents: 89
diff changeset
31
d67e8288e9bf Added saveable_fields to Container.
KarstenBock@gmx.net
parents: 89
diff changeset
32
22
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
33
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
34 class BulkLimitError(Exception):
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
35 """Error that gets raised when the item would exceed the
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
36 bulk limit of the container."""
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
37
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
38 def __init__(self, bulk, max_bulk):
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
39 self.bulk = bulk
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
40 self.max_bulk = max_bulk
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
41
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
42 def __str__(self):
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
43 return "Item would exceed the bulk limit of the container."
24
eab8af30dfc7 get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents: 22
diff changeset
44
eab8af30dfc7 get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents: 22
diff changeset
45 class NoFreeSlotError(Exception):
eab8af30dfc7 get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents: 22
diff changeset
46 """Error that gets raised when the container has no free slots."""
eab8af30dfc7 get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents: 22
diff changeset
47
eab8af30dfc7 get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents: 22
diff changeset
48 def __str__(self):
eab8af30dfc7 get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents: 22
diff changeset
49 return "Container can't hold any more items."
eab8af30dfc7 get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents: 22
diff changeset
50
22
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
51 def get_free_slot(container):
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
52 """Returns the first slot of the container that is not occupied."""
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
53 index = 0
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
54 for child in container.children:
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
55 if not child:
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
56 return index
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
57 index += 1
24
eab8af30dfc7 get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents: 22
diff changeset
58 raise NoFreeSlotError
22
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
59
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
60 def get_total_bulk(container):
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
61 """Returns the bulk of all items in the container."""
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
62 total_bulk = 0
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
63 for child in container.children:
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
64 if child:
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
65 total_bulk += child.bulk
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
66 return total_bulk
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
67
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
68 def get_total_weight(container):
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
69 """Returns the weight of all items in the container."""
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
70 total_weight = 0
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
71 for child in container.children:
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
72 if child:
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
73 total_weight += child.weight
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
74 return total_weight
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
75
62
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
76 def get_item(container, slot_or_type):
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
77 """Returns the item that is in the slot, or has the given type."""
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
78 if type(slot_or_type) == int:
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
79 if len(container.children) >= (slot_or_type + 1):
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
80 return container.children[slot_or_type]
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
81 else:
77
180cbd2b5da8 Fixed bugs in the container functions.
KarstenBock@gmx.net
parents: 66
diff changeset
82 for child in container.children:
180cbd2b5da8 Fixed bugs in the container functions.
KarstenBock@gmx.net
parents: 66
diff changeset
83 if child and child.item_type == slot_or_type:
62
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
84 return child
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
85
22
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
86 return None
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
87
62
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
88 def remove_item(container, slot_or_type):
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
89 """Removes the item at the given slot, or with the given type."""
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
90 if type(slot_or_type) == int:
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
91 item = get_item(container, slot_or_type)
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
92 if item:
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
93 container.children[slot_or_type] = None
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
94 item.container = None
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
95 item.slot = -1
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
96 else:
77
180cbd2b5da8 Fixed bugs in the container functions.
KarstenBock@gmx.net
parents: 66
diff changeset
97 for child in container.children:
180cbd2b5da8 Fixed bugs in the container functions.
KarstenBock@gmx.net
parents: 66
diff changeset
98 if child and child.item_type == slot_or_type:
62
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
99 container.children[child.slot] = None
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
100 child.container = None
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
101 child.slot = -1
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
102
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
103 def take_item(container, slot_or_type):
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
104 """Moves the item at the given slot, or with the given type,
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
105 out of the container and returns it."""
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
106 item = get_item(container, slot_or_type)
22
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
107 if item:
62
bf63da4b27f5 Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents: 41
diff changeset
108 remove_item(container, slot_or_type)
22
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
109 return item
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
110
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
111 def put_item(container, item, slot=-1):
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
112 """Puts the item at the given slot in the container.
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
113 Returns the item previously at the slot."""
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
114 if slot == -1:
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
115 slot = get_free_slot(container)
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
116 total_bulk = get_total_bulk(container)
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
117 total_bulk += item.bulk
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
118 old_item = get_item(container, slot)
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
119 if old_item:
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
120 total_bulk -= old_item.bulk
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
121 if total_bulk > container.max_bulk:
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
122 raise BulkLimitError(total_bulk, container.max_bulk)
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
123 remove_item(container, slot)
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
124 container.children[slot] = item
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
125 if item.container:
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
126 remove_item(item.container, item.slot)
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
127 item.container = container
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
128 item.slot = slot
d8c17766895b Added functions for container components
KarstenBock@gmx.net
parents: 21
diff changeset
129 return old_item