annotate gamelib/buildings.py @ 394:ad77b3b71b08

Fix crash when multiple foxes finish digging at the same time
author Neil Muller <drnlmuller@gmail.com>
date Thu, 12 Nov 2009 15:19:12 +0000
parents 71f5897ac5ef
children 2d0ff46118e2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
1 """Classes for various buildings in the game."""
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
2
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
3 from pgu.vid import Sprite
204
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
4 from pygame.locals import SRCALPHA
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
5 import pygame
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
6
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
7 import imagecache
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
8 import tiles
204
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
9 import constants
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
10
231
857040b211d5 Note why we're disabling a warning.
Simon Cross <hodgestar@gmail.com>
parents: 224
diff changeset
11 # ignore os.popen3 warning generated by pygame.font.SysFont
204
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
12 import warnings
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
13 warnings.filterwarnings("ignore", "os.popen3 is deprecated.")
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
14
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
15 class Place(object):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
16 """Space within a building that can be occupied."""
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
17
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
18 def __init__(self, building, offset):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
19 self.occupant = None
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
20 self.building = building
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
21 self.offset = offset
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
22
204
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
23 def set_occupant(self, occupant, _update=True):
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
24 self.clear_occupant(_update=False)
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
25 self.occupant = occupant
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
26 self.occupant.abode = self
204
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
27 if _update:
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
28 self.building.update_occupant_count()
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
29
204
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
30 def clear_occupant(self, _update=True):
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
31 if self.occupant is not None:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
32 self.occupant.abode = None
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
33 self.occupant = None
204
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
34 if _update:
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
35 self.building.update_occupant_count()
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
36
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
37 def get_pos(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
38 bpos = self.building.pos
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
39 return (bpos[0] + self.offset[0], bpos[1] + self.offset[1])
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
40
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
41 class Floor(object):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
42 """A set of places within a building. Places on a
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
43 floor are organised into rows and columns.
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
44 """
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
45
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
46 def __init__(self, title, places):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
47 self.title = title # str
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
48 self.places = places # list of lists of places
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
49
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
50 def rows(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
51 for row in self.places:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
52 yield row
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
53
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
54 def width(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
55 return max(len(row) for row in self.places)
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
56
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
57 class BuildingFullError(Exception):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
58 pass
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
59
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
60 class Building(Sprite):
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
61 """Base class for buildings"""
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
62
109
48019afde338 Equipment purchasing and some toolbar tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 108
diff changeset
63 IS_BUILDING = True
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
64 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland']
198
355eaae40b1f Buildings now affect weapon range and accuracy.
Jeremy Thurgood <firxen@gmail.com>
parents: 177
diff changeset
65 MODIFY_KNIFE_RANGE = lambda s, x: 0
355eaae40b1f Buildings now affect weapon range and accuracy.
Jeremy Thurgood <firxen@gmail.com>
parents: 177
diff changeset
66 MODIFY_GUN_RANGE = lambda s, x: -1
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
67 BREAKABLE = False
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
68 ABODE = False
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
69
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
70 def __init__(self, pos):
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
71 """Initial image, tile vid position, size and tile number for building."""
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
72 self._set_images()
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
73 self.pos = pos
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
74 self.size = self.SIZE
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
75 self.tile_no = self.TILE_NO
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
76 self._buy_price = self.BUY_PRICE
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
77 self._sell_price = self.SELL_PRICE
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
78 self._repair_price = getattr(self, 'REPAIR_PRICE', None)
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
79 self._sun_on = True
204
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
80 self._font = pygame.font.SysFont('Vera', 30, bold=True)
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
81 self._font_image = pygame.Surface(self.images['fixed']['day'].get_size(), flags=SRCALPHA)
204
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
82 self._font_image.fill((0, 0, 0, 0))
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
83 self._broken = False
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
84
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
85 self._floors = []
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
86 if self.FLOORS:
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
87 for f in range(self.FLOORS):
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
88 places = []
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
89 for j in range(self.size[1]):
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
90 row = []
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
91 for i in range(self.size[0]):
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
92 row.append(Place(self, (i, j)))
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
93 places.append(row)
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
94 floor = Floor("Floor %s" % (f+1,), places)
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
95 self._floors.append(floor)
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
96
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
97 # 0: the main image
204
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
98 # 1: above, -1: below
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
99 self.draw_stack = {"main": (0, self.images['fixed']['day'])}
204
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
100
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
101 # Create the building somewhere far off screen
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
102 Sprite.__init__(self, self.images['fixed']['day'], (-1000, -1000))
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
103
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
104 def _set_images(self):
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
105 self.images = {'fixed': {
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
106 'day': imagecache.load_image(self.IMAGE),
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
107 'night': imagecache.load_image(self.IMAGE, ('night',)),
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
108 'selected': imagecache.load_image(self.SELECTED_IMAGE),
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
109 }}
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
110 if self.BREAKABLE:
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
111 self.images['broken'] = {
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
112 'day': imagecache.load_image(self.IMAGE_BROKEN),
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
113 'night': imagecache.load_image(self.IMAGE_BROKEN, ('night',)),
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
114 'selected': imagecache.load_image(self.SELECTED_IMAGE_BROKEN),
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
115 }
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
116
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
117 def _set_main_image(self):
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
118 image_set = self.images[{True: 'broken',False: 'fixed'}[self._broken]]
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
119 self._replace_main(image_set[{True: 'day', False: 'night'}[self._sun_on]])
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
120
204
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
121 def _redraw(self):
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
122 items = self.draw_stack.values()
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
123 items.sort(key=lambda x: x[0])
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
124 image = items.pop(0)[1].copy()
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
125 for _lvl, overlay in items:
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
126 image.blit(overlay, (0, 0))
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
127 self.setimage(image)
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
128
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
129 def _replace_main(self, new_main):
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
130 self.draw_stack["main"] = (0, new_main)
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
131 self._redraw()
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
132
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
133 def tile_positions(self):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
134 """Return pairs of (x, y) tile positions for each of the tile positions
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
135 occupied by the building.
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
136 """
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
137 xpos, ypos = self.pos
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
138 xsize, ysize = self.size
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
139
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
140 for dx in xrange(xsize):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
141 for dy in xrange(ysize):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
142 yield (xpos + dx, ypos + dy)
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
143
224
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
144 def adjacent_tiles(self):
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
145 """Return pairs of (x, y) tile positions for each of the tiles
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
146 adjacent to the building.
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
147 """
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
148 xpos, ypos = self.pos
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
149 xsize, ysize = self.size
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
150
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
151 for dx in xrange(xsize): # top and bottom
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
152 yield (xpos + dx, ypos - 1)
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
153 yield (xpos + dx, ypos + ysize)
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
154
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
155 for dy in xrange(ysize): # left and right
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
156 yield (xpos - 1, ypos + dy)
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
157 yield (xpos + xsize, ypos + dy)
c279ad59b8e2 Chicks fill hen house, then adjacent spaces, then die.
Simon Cross <hodgestar@gmail.com>
parents: 204
diff changeset
158
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
159 def loop(self, tv, _sprite):
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
160 ppos = tv.tile_to_view(self.pos)
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
161 self.rect.x = ppos[0]
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
162 self.rect.y = ppos[1]
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
163
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
164 def move(self, state):
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
165 """Given the game state, return a new position for the object"""
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
166 # Default is not to move
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
167 return self.pos
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
168
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
169 def place(self, tv):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
170 """Check that the building can be placed at its current position
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
171 and place it if possible.
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
172 """
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
173 # check that all spaces under the structure are grassland
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
174 for tile_pos in self.tile_positions():
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
175 if not tv.get(tile_pos) == self.GRASSLAND:
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
176 return False
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
177
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
178 # place tile
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
179 for tile_pos in self.tile_positions():
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
180 tv.set(tile_pos, self.tile_no)
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
181
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
182 return True
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
183
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
184 def covers(self, tile_pos):
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
185 """Return True if build covers tile_pos, False otherwise."""
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
186 xpos, ypos = self.pos
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
187 xsize, ysize = self.size
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
188 return (xpos <= tile_pos[0] < xpos + xsize) and \
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
189 (ypos <= tile_pos[1] < ypos + ysize)
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
190
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
191 def broken(self):
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
192 return self._broken
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
193
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
194 def damage(self, tv):
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
195 if not self.BREAKABLE:
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
196 return False
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
197 self._broken = True
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
198 self._sell_price = self.SELL_PRICE_BROKEN
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
199 self.tile_no = self.TILE_NO_BROKEN
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
200 for tile_pos in self.tile_positions():
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
201 tv.set(tile_pos, self.tile_no)
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
202 self._set_main_image()
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
203
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
204 def repair(self, tv):
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
205 self._broken = False
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
206 self._sell_price = self.SELL_PRICE
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
207 self.tile_no = self.TILE_NO
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
208 for tile_pos in self.tile_positions():
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
209 tv.set(tile_pos, self.tile_no)
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
210 self._set_main_image()
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
211
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
212 def remove(self, tv):
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
213 """Remove the building from its current position."""
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
214 # remove tile
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
215 for tile_pos in self.tile_positions():
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
216 tv.set(tile_pos, self.GRASSLAND)
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
217
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
218 def buy_price(self):
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
219 return self._buy_price
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
220
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
221 def sell_price(self):
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
222 return self._sell_price
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
223
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
224 def repair_price(self):
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
225 return self._repair_price
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
226
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
227 def selected(self, selected):
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
228 if selected:
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
229 self._replace_main(self.images[{True: 'broken',False: 'fixed'}[self._broken]]['selected'])
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
230 else:
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
231 self._set_main_image()
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
232
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
233 def sun(self, sun_on):
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
234 self._sun_on = sun_on
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
235 self._set_main_image()
204
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
236
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
237 def update_occupant_count(self):
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
238 count = len(list(self.occupants()))
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
239 if count == 0:
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
240 if "count" in self.draw_stack:
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
241 del self.draw_stack["count"]
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
242 else:
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
243 image = self._font_image.copy()
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
244 text = self._font.render(str(count), True, constants.FG_COLOR)
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
245 w, h = image.get_size()
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
246 x, y = text.get_size()
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
247 image.blit(text, (w - x, h - y))
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
248 self.draw_stack["count"] = (1, image)
636c3fafa32d Add counts to buildings.
Simon Cross <hodgestar@gmail.com>
parents: 199
diff changeset
249 self._redraw()
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
250
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
251 def floors(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
252 return self._floors
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
253
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
254 def places(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
255 for floor in self._floors:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
256 for row in floor.rows():
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
257 for place in row:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
258 yield place
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
259
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
260 def max_floor_width(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
261 return max(floor.width() for floor in self._floors)
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
262
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
263 def first_empty_place(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
264 for place in self.places():
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
265 if place.occupant is None:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
266 return place
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
267 raise BuildingFullError()
108
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
268
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
269 def add_occupant(self, occupant):
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
270 place = self.first_empty_place()
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
271 place.set_occupant(occupant)
108
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
272
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
273 def occupants(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
274 for place in self.places():
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
275 if place.occupant is not None:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
276 yield place.occupant
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
277
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
278 class Abode(Building):
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
279 ABODE = True
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
280
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
281 class HenHouse(Abode):
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
282 """A HenHouse."""
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
283
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
284 TILE_NO = tiles.REVERSE_TILE_MAP['henhouse']
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
285 BUY_PRICE = 100
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
286 SELL_PRICE = 90
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
287 SIZE = (3, 2)
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
288 IMAGE = 'sprites/henhouse.png'
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
289 SELECTED_IMAGE = 'sprites/select_henhouse.png'
177
b400991ccce1 pedantic building renaming
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 170
diff changeset
290 NAME = 'Henhouse'
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
291 FLOORS = 1
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
292
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
293 class DoubleStoryHenHouse(HenHouse):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
294 """A double story hen house."""
198
355eaae40b1f Buildings now affect weapon range and accuracy.
Jeremy Thurgood <firxen@gmail.com>
parents: 177
diff changeset
295
133
b7dc405a28be Add Hendominium image support.
Simon Cross <hodgestar@gmail.com>
parents: 125
diff changeset
296 TILE_NO = tiles.REVERSE_TILE_MAP['hendominium']
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
297 BUY_PRICE = 300
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
298 SELL_PRICE = 150
133
b7dc405a28be Add Hendominium image support.
Simon Cross <hodgestar@gmail.com>
parents: 125
diff changeset
299 SIZE = (2, 3)
b7dc405a28be Add Hendominium image support.
Simon Cross <hodgestar@gmail.com>
parents: 125
diff changeset
300 IMAGE = 'sprites/hendominium.png'
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
301 SELECTED_IMAGE = 'sprites/select_hendominium.png'
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
302 NAME = 'Hendominium'
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
303 FLOORS = 2
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
304
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
305 class GuardTower(Abode):
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
306 """A GuardTower."""
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
307
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
308 TILE_NO = tiles.REVERSE_TILE_MAP['guardtower']
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
309 BUY_PRICE = 200
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
310 SELL_PRICE = 150
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
311 SIZE = (2, 2)
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
312 IMAGE = 'sprites/watchtower.png'
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
313 SELECTED_IMAGE = 'sprites/select_watchtower.png'
177
b400991ccce1 pedantic building renaming
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 170
diff changeset
314 NAME = 'Watchtower'
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
315 FLOORS = 1
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
316
198
355eaae40b1f Buildings now affect weapon range and accuracy.
Jeremy Thurgood <firxen@gmail.com>
parents: 177
diff changeset
317 MODIFY_GUN_RANGE = lambda s, x: (3*x)/2
355eaae40b1f Buildings now affect weapon range and accuracy.
Jeremy Thurgood <firxen@gmail.com>
parents: 177
diff changeset
318 MODIFY_GUN_BASE_HIT = lambda s, x: x-5
355eaae40b1f Buildings now affect weapon range and accuracy.
Jeremy Thurgood <firxen@gmail.com>
parents: 177
diff changeset
319 MODIFY_GUN_RANGE_PENALTY = lambda s, x: x-1
199
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 198
diff changeset
320 MODIFY_VISION_BONUS = lambda s, x: x+10
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 198
diff changeset
321 MODIFY_VISION_RANGE_PENALTY = lambda s, x: x-2
198
355eaae40b1f Buildings now affect weapon range and accuracy.
Jeremy Thurgood <firxen@gmail.com>
parents: 177
diff changeset
322
378
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
323 class Fence(Building):
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
324 """A fence."""
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
325
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
326 TILE_NO = tiles.REVERSE_TILE_MAP['fence']
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
327 TILE_NO_BROKEN = tiles.REVERSE_TILE_MAP['broken fence']
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
328 BREAKABLE = True
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
329 BUY_PRICE = 50
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
330 SELL_PRICE = 25
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
331 REPAIR_PRICE = 25
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
332 SELL_PRICE_BROKEN = 5
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
333 SIZE = (1, 1)
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
334 IMAGE = 'tiles/fence.png'
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
335 SELECTED_IMAGE = 'tiles/fence.png'
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
336 IMAGE_BROKEN = 'tiles/broken_fence.png'
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
337 SELECTED_IMAGE_BROKEN = 'tiles/broken_fence.png'
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
338 NAME = 'Fence'
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
339 FLOORS = 0
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
340
71f5897ac5ef Fences are now buildings, with appropriate (but ugly) UI changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 268
diff changeset
341
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
342 def is_building(obj):
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
343 """Return true if obj is a build class."""
109
48019afde338 Equipment purchasing and some toolbar tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 108
diff changeset
344 return getattr(obj, "IS_BUILDING", False) and hasattr(obj, "NAME")
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
345
144
a9b800b4175e Add define for henhouses & egg laying.
Neil Muller <drnlmuller@gmail.com>
parents: 136
diff changeset
346 # Building hens can lay eggs in
a9b800b4175e Add define for henhouses & egg laying.
Neil Muller <drnlmuller@gmail.com>
parents: 136
diff changeset
347 HENHOUSES = [HenHouse.NAME, DoubleStoryHenHouse.NAME]
a9b800b4175e Add define for henhouses & egg laying.
Neil Muller <drnlmuller@gmail.com>
parents: 136
diff changeset
348
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
349 BUILDINGS = []
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
350 for name in dir():
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
351 obj = eval(name)
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
352 try:
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
353 if is_building(obj):
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
354 BUILDINGS.append(obj)
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
355 except TypeError:
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 63
diff changeset
356 pass