annotate src/parpg/components/container.py @ 66:96af64cf3b81

Renamed grease to bGrease (Basic Grease) to get rid of conflicts with an already installed grease.
author KarstenBock@gmx.net
date Mon, 05 Sep 2011 15:00:34 +0200
parents 3bacef35b252
children b5619032c521
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
66
96af64cf3b81 Renamed grease to bGrease (Basic Grease) to get rid of conflicts with an already installed grease.
KarstenBock@gmx.net
parents: 46
diff changeset
14 from bGrease.component import Component
34
5ac50245e42c Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
15
5ac50245e42c Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
16 class Container(Component):
5ac50245e42c Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
17 """
5ac50245e42c 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.
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
5ac50245e42c Refactored components and began defining basic Entities and Systems.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
21 def __init__(self):
43
1cc037593517 Fixed bug in components
KarstenBock@gmx.net
parents: 41
diff changeset
22 Component.__init__(self, children=list, max_bulk=int)
44
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
23
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
24
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
25 class BulkLimitError(Exception):
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
26 """Error that gets raised when the item would exceed the
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
27 bulk limit of the container."""
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
28
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
29 def __init__(self, bulk, max_bulk):
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
30 self.bulk = bulk
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
31 self.max_bulk = max_bulk
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
32
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
33 def __str__(self):
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
34 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
35
3bacef35b252 get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents: 44
diff changeset
36 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
37 """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
38
3bacef35b252 get_free_slot no raises an exception instead of appending a slot to the container.
KarstenBock@gmx.net
parents: 44
diff changeset
39 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
40 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
41
44
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
42 def get_free_slot(container):
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
43 """Returns the first slot of the container that is not occupied."""
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
44 index = 0
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
45 for child in container.children:
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
46 if not child:
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
47 return index
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
48 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
49 raise NoFreeSlotError
44
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
50
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
51 def get_total_bulk(container):
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
52 """Returns the bulk of all items in the container."""
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
53 total_bulk = 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 child:
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
56 total_bulk += child.bulk
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
57 return total_bulk
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
58
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
59 def get_total_weight(container):
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
60 """Returns the weight of all items in the container."""
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
61 total_weight = 0
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
62 for child in container.children:
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
63 if child:
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
64 total_weight += child.weight
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
65 return total_weight
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
66
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
67 def get_item(container, slot):
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
68 """Returns the item that is in the slot."""
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
69 if len(container.children) >= (slot + 1):
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
70 return container.children[slot]
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
71 return None
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
72
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
73 def remove_item(container, slot):
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
74 """Removes the item at the given slot."""
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
75 item = get_item(container, slot)
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
76 if item:
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
77 container.children[slot] = None
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
78 item.container = None
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
79 item.slot = -1
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
80
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
81 def take_item(container, slot):
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
82 """Moves the item at the given slot out of the container and returns it."""
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
83 item = get_item(container, slot)
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
84 if item:
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
85 remove_item(container, slot)
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
86 return item
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
87
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
88 def put_item(container, item, slot=-1):
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
89 """Puts the item at the given slot in the container.
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
90 Returns the item previously at the slot."""
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
91 if slot == -1:
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
92 slot = get_free_slot(container)
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
93 total_bulk = get_total_bulk(container)
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
94 total_bulk += item.bulk
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
95 old_item = get_item(container, slot)
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
96 if old_item:
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
97 total_bulk -= old_item.bulk
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
98 if total_bulk > container.max_bulk:
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
99 raise BulkLimitError(total_bulk, container.max_bulk)
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
100 remove_item(container, slot)
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
101 container.children[slot] = item
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
102 if item.container:
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
103 remove_item(item.container, item.slot)
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
104 item.container = container
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
105 item.slot = slot
9f631144124f Added functions for container components
KarstenBock@gmx.net
parents: 43
diff changeset
106 return old_item