Mercurial > parpg-source
annotate components/container.py @ 68:8915ff6183b0
Added equip to the usable components of the World class.
author | KarstenBock@gmx.net |
---|---|
date | Wed, 21 Sep 2011 16:48:01 +0200 |
parents | 58d58bf2e567 |
children | 180cbd2b5da8 |
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 |
65
e856b604b650
Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents:
62
diff
changeset
|
14 from parpg.bGrease.component import Component |
12
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
15 |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
16 class Container(Component): |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
17 """ |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
18 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
|
19 """ |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
20 |
9c7a96c6fe41
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
21 def __init__(self): |
21 | 22 Component.__init__(self, children=list, max_bulk=int) |
22
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
23 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
24 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
25 class BulkLimitError(Exception): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
26 """Error that gets raised when the item would exceed the |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
27 bulk limit of the container.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
28 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
29 def __init__(self, bulk, max_bulk): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
30 self.bulk = bulk |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
31 self.max_bulk = max_bulk |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
32 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
33 def __str__(self): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
34 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
|
35 |
eab8af30dfc7
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
22
diff
changeset
|
36 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
|
37 """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
|
38 |
eab8af30dfc7
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
22
diff
changeset
|
39 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
|
40 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
|
41 |
22
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
42 def get_free_slot(container): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
43 """Returns the first slot of the container that is not occupied.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
44 index = 0 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
45 for child in container.children: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
46 if not child: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
47 return index |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
48 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
|
49 raise NoFreeSlotError |
22
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
50 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
51 def get_total_bulk(container): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
52 """Returns the bulk of all items in the container.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
53 total_bulk = 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 child: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
56 total_bulk += child.bulk |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
57 return total_bulk |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
58 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
59 def get_total_weight(container): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
60 """Returns the weight of all items in the container.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
61 total_weight = 0 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
62 for child in container.children: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
63 if child: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
64 total_weight += child.weight |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
65 return total_weight |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
66 |
62
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
67 def get_item(container, slot_or_type): |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
68 """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
|
69 if type(slot_or_type) == int: |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
70 if len(container.children) >= (slot_or_type + 1): |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
71 return container.children[slot_or_type] |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
72 else: |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
73 for item in container.children: |
66 | 74 if child and child.type == slot_or_type: |
62
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
75 return child |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
76 |
22
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
77 return None |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
78 |
62
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
79 def remove_item(container, slot_or_type): |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
80 """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
|
81 if type(slot_or_type) == int: |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
82 item = get_item(container, slot_or_type) |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
83 if item: |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
84 container.children[slot_or_type] = None |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
85 item.container = None |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
86 item.slot = -1 |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
87 else: |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
88 for item in container.children: |
66 | 89 if child and child.type == slot_or_type: |
62
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
90 container.children[child.slot] = None |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
91 child.container = None |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
92 child.slot = -1 |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
93 |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
94 def take_item(container, slot_or_type): |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
95 """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
|
96 out of the container and returns it.""" |
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
97 item = get_item(container, slot_or_type) |
22
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
98 if item: |
62
bf63da4b27f5
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
99 remove_item(container, slot_or_type) |
22
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
100 return item |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
101 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
102 def put_item(container, item, slot=-1): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
103 """Puts the item at the given slot in the container. |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
104 Returns the item previously at the slot.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
105 if slot == -1: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
106 slot = get_free_slot(container) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
107 total_bulk = get_total_bulk(container) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
108 total_bulk += item.bulk |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
109 old_item = get_item(container, slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
110 if old_item: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
111 total_bulk -= old_item.bulk |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
112 if total_bulk > container.max_bulk: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
113 raise BulkLimitError(total_bulk, container.max_bulk) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
114 remove_item(container, slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
115 container.children[slot] = item |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
116 if item.container: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
117 remove_item(item.container, item.slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
118 item.container = container |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
119 item.slot = slot |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
120 return old_item |