Mercurial > parpg-source
annotate components/container.py @ 43:8f9e9f47c363
Added dialogue value to Character entity
author | KarstenBock@gmx.net |
---|---|
date | Tue, 06 Sep 2011 15:21:36 +0200 |
parents | ff3e395abf91 |
children | bf63da4b27f5 |
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 |
41
ff3e395abf91
Renamed grease to bGrease (Basic Grease) to get rid of conflicts with an already installed grease.
KarstenBock@gmx.net
parents:
24
diff
changeset
|
14 from 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 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
67 def get_item(container, slot): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
68 """Returns the item that is in the slot.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
69 if len(container.children) >= (slot + 1): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
70 return container.children[slot] |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
71 return None |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
72 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
73 def remove_item(container, slot): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
74 """Removes the item at the given slot.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
75 item = get_item(container, slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
76 if item: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
77 container.children[slot] = None |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
78 item.container = None |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
79 item.slot = -1 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
80 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
81 def take_item(container, slot): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
82 """Moves the item at the given slot out of the container and returns it.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
83 item = get_item(container, slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
84 if item: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
85 remove_item(container, slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
86 return item |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
87 |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
88 def put_item(container, item, slot=-1): |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
89 """Puts the item at the given slot in the container. |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
90 Returns the item previously at the slot.""" |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
91 if slot == -1: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
92 slot = get_free_slot(container) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
93 total_bulk = get_total_bulk(container) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
94 total_bulk += item.bulk |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
95 old_item = get_item(container, slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
96 if old_item: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
97 total_bulk -= old_item.bulk |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
98 if total_bulk > container.max_bulk: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
99 raise BulkLimitError(total_bulk, container.max_bulk) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
100 remove_item(container, slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
101 container.children[slot] = item |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
102 if item.container: |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
103 remove_item(item.container, item.slot) |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
104 item.container = container |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
105 item.slot = slot |
d8c17766895b
Added functions for container components
KarstenBock@gmx.net
parents:
21
diff
changeset
|
106 return old_item |