Mercurial > parpg-core
annotate src/parpg/components/container.py @ 140:e1ca6a99eb85
Added gender, picture, age, origin and traits to the CharacterStatistics component and splitted statistics into primary_stats and secondary_stats.
author | KarstenBock@gmx.net |
---|---|
date | Sat, 01 Oct 2011 14:07:40 +0200 |
parents | 90b49fda0340 |
children |
rev | line source |
---|---|
34
5ac50245e42c
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 |
5ac50245e42c
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 |
5ac50245e42c
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 |
5ac50245e42c
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. |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
5 # |
5ac50245e42c
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, |
5ac50245e42c
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 |
5ac50245e42c
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 |
5ac50245e42c
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. |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
10 # |
5ac50245e42c
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 |
5ac50245e42c
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/>. |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
13 |
125 | 14 from copy import deepcopy |
15 | |
119
3564a46544bc
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:
107
diff
changeset
|
16 from base import Base |
34
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
17 |
119
3564a46544bc
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:
107
diff
changeset
|
18 class Container(Base): |
34
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
19 """ |
5ac50245e42c
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. |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
21 """ |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
22 |
5ac50245e42c
Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
23 def __init__(self): |
119
3564a46544bc
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:
107
diff
changeset
|
24 Base.__init__(self, children=list, max_bulk=int) |
44
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
25 |
125 | 26 @property |
27 def saveable_fields(self): | |
127
90b49fda0340
saveable_fields property of components is now a List.
KarstenBock@gmx.net
parents:
125
diff
changeset
|
28 fields = self.fields.keys() |
90b49fda0340
saveable_fields property of components is now a List.
KarstenBock@gmx.net
parents:
125
diff
changeset
|
29 fields.remove("children") |
125 | 30 return fields |
31 | |
32 | |
44
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
33 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
34 class BulkLimitError(Exception): |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
35 """Error that gets raised when the item would exceed the |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
36 bulk limit of the container.""" |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
37 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
38 def __init__(self, bulk, max_bulk): |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
39 self.bulk = bulk |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
40 self.max_bulk = max_bulk |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
41 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
42 def __str__(self): |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
43 return "Item would exceed the bulk limit of the container." |
46
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
44 |
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
45 class NoFreeSlotError(Exception): |
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
46 """Error that gets raised when the container has no free slots.""" |
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
47 |
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
48 def __str__(self): |
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
49 return "Container can't hold any more items." |
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
50 |
44
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
51 def get_free_slot(container): |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
52 """Returns the first slot of the container that is not occupied.""" |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
53 index = 0 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
54 for child in container.children: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
55 if not child: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
56 return index |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
57 index += 1 |
46
3bacef35b252
get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents:
44
diff
changeset
|
58 raise NoFreeSlotError |
44
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
59 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
60 def get_total_bulk(container): |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
61 """Returns the bulk of all items in the container.""" |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
62 total_bulk = 0 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
63 for child in container.children: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
64 if child: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
65 total_bulk += child.bulk |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
66 return total_bulk |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
67 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
68 def get_total_weight(container): |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
69 """Returns the weight of all items in the container.""" |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
70 total_weight = 0 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
71 for child in container.children: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
72 if child: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
73 total_weight += child.weight |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
74 return total_weight |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
75 |
89
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
76 def get_item(container, slot_or_type): |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
77 """Returns the item that is in the slot, or has the given type.""" |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
78 if type(slot_or_type) == int: |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
79 if len(container.children) >= (slot_or_type + 1): |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
80 return container.children[slot_or_type] |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
81 else: |
107 | 82 for child in container.children: |
83 if child and child.item_type == slot_or_type: | |
89
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
84 return child |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
85 |
44
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
86 return None |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
87 |
89
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
88 def remove_item(container, slot_or_type): |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
89 """Removes the item at the given slot, or with the given type.""" |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
90 if type(slot_or_type) == int: |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
91 item = get_item(container, slot_or_type) |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
92 if item: |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
93 container.children[slot_or_type] = None |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
94 item.container = None |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
95 item.slot = -1 |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
96 else: |
107 | 97 for child in container.children: |
98 if child and child.item_type == slot_or_type: | |
89
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
99 container.children[child.slot] = None |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
100 child.container = None |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
101 child.slot = -1 |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
102 |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
103 def take_item(container, slot_or_type): |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
104 """Moves the item at the given slot, or with the given type, |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
105 out of the container and returns it.""" |
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
106 item = get_item(container, slot_or_type) |
44
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
107 if item: |
89
b5619032c521
Added item_type attribute to the containable component.
KarstenBock@gmx.net
parents:
66
diff
changeset
|
108 remove_item(container, slot_or_type) |
44
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
109 return item |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
110 |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
111 def put_item(container, item, slot=-1): |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
112 """Puts the item at the given slot in the container. |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
113 Returns the item previously at the slot.""" |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
114 if slot == -1: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
115 slot = get_free_slot(container) |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
116 total_bulk = get_total_bulk(container) |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
117 total_bulk += item.bulk |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
118 old_item = get_item(container, slot) |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
119 if old_item: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
120 total_bulk -= old_item.bulk |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
121 if total_bulk > container.max_bulk: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
122 raise BulkLimitError(total_bulk, container.max_bulk) |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
123 remove_item(container, slot) |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
124 container.children[slot] = item |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
125 if item.container: |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
126 remove_item(item.container, item.slot) |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
127 item.container = container |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
128 item.slot = slot |
9f631144124f
Added functions for container components
KarstenBock@gmx.net
parents:
43
diff
changeset
|
129 return old_item |