Remove debugging prints. Add get_data method for saving
[erdslangetjie.git] / erdslangetjie / level.py
1 # The level object
2
3 import os
4 from data import load_image, load, filepath
5
6 WALL = '.'
7 FLOOR = ' '
8 ENTRY = 'E'
9 EXIT = 'X'
10 GATE = 'G'
11 BUTTON = 'B'
12
13
14 class Level(object):
15
16     def __init__(self, levelfile):
17         self._data = []
18         self.exit_pos = []
19         self.enter_pos = None
20         self._tiles = []
21         self._changed = []
22         self._gates = {}
23         self._buttons = {}
24         # Because of how kivy's coordinate system works,
25         # we reverse the lines so things match up between
26         # the file and the display (top of file == top of display)
27         for line in reversed(levelfile.readlines()):
28             self._data.append(list(line.strip('\n')))
29
30     def load_tiles(self):
31         """Load the list of tiles for the level"""
32         self._tiles = []
33         self._gates = {}
34         self._buttons = {}
35         self.exit_pos = []
36         self._changed = []
37         self.enter_pos = None
38         for j, line in enumerate(self._data):
39             tile_line = []
40             for i, c in enumerate(line):
41                 tile_image = self._get_tile_image((i, j), c)
42                 tile_line.append(tile_image)
43             self._tiles.append(tile_line)
44
45     def _get_tile_image(self, pos, c):
46         if pos in self._gates:
47             del self._gates[pos]
48         if pos in self._buttons:
49             del self._buttons[pos]
50         image = None
51         if c == FLOOR:
52             image = load_image('tiles/floor.png')
53         elif c == WALL:
54             image = self._get_wall_tile(pos)
55         elif c == ENTRY:
56             self.enter_pos = pos
57             image = load_image('tiles/entry.png')
58         elif c == EXIT:
59             self.exit_pos.append(pos)
60             image = load_image('tiles/door.png')
61         elif c == GATE:
62             image = load_image('tiles/gate_down.png')
63             self._gates[pos] = -1  # down
64         elif c == BUTTON:
65             image = load_image('tiles/button.png')
66             self._buttons[pos] = 'active'
67         if image is None:
68             raise RuntimeError('Unknown tile type %s at %s' % (c, pos))
69         return image
70
71     def validate(self):
72         entry_points = 0
73         exit_points = 0
74         for line in self._data:
75             if ENTRY in line:
76                 entry_points += 1
77             if EXIT in line:
78                 exit_points += 1
79         if entry_points == 0:
80             raise RuntimeError('No entry point')
81         if entry_points > 1:
82             raise RuntimeError('Multiple entry points')
83         if exit_points == 0:
84             raise RuntimeError('No exit')
85
86     def get_tiles(self):
87         return self._tiles
88
89     def get_single_tile(self, pos):
90         return self._tiles[pos[1]][pos[0]]
91
92     def get_tile_type(self, pos):
93         return self._data[pos[1]][pos[0]]
94
95     def set_tile_type(self, pos, new_type):
96         self._data[pos[1]][pos[0]] = new_type
97         new_tile = self._get_tile_image(pos, new_type)
98         self._tiles[pos[1]][pos[0]] = new_tile
99         self._changed.append((pos, new_tile))
100         # Also update neighbourhood for wall types, etc.
101         for new_pos in [(pos[0] - 1, pos[1]), (pos[0] + 1, pos[1]),
102                 (pos[0] - 1, pos[1] - 1), (pos[0] + 1, pos[1] + 1),
103                 (pos[0], pos[1] - 1), (pos[0], pos[1] + 1),
104                 (pos[0] - 1, pos[1] + 1), (pos[0] + 1, pos[1] - 1)]:
105             if not self._in_limits(new_pos):
106                 continue
107             tile = self._data[new_pos[1]][new_pos[0]]
108             new_tile = self._get_tile_image(new_pos, tile)
109             self._tiles[new_pos[1]][new_pos[0]] = new_tile
110             self._changed.append((new_pos, new_tile))
111
112     def get_size(self):
113         return len(self._tiles[0]), len(self._tiles)
114
115     def at_exit(self, pos):
116         return pos in self.exit_pos
117
118     def get_level_data(self):
119         return '\n'.join(reversed([''.join(x) for x in self._data]))
120
121     def _get_wall_tile(self, pos):
122         # Is the neighbour in this direction also a wall?
123         x, y = pos
124         left = right = top = bottom = False
125         if x == 0:
126             left = True
127         elif self._data[y][x - 1] == WALL:
128             left = True
129         if x == len(self._data[0]) - 1:
130             right = True
131         elif self._data[y][x + 1] == WALL:
132             right = True
133         if y == 0:
134             top = True
135         elif self._data[y - 1][x] == WALL:
136             top = True
137         if y == len(self._data) - 1:
138             bottom = True
139         elif self._data[y + 1][x] == WALL:
140             bottom = True
141         if top and bottom and left and right:
142             return load_image('tiles/cwall.png')
143         elif bottom and left and right:
144             return load_image('tiles/bottom_wall.png')
145         elif top and left and right:
146             return load_image('tiles/top_wall.png')
147         elif top and bottom and right:
148             return load_image('tiles/left_wall.png')
149         elif top and bottom and left:
150             return load_image('tiles/right_wall.png')
151         elif top and bottom:
152             return load_image('tiles/vert_wall.png')
153         elif left and right:
154             return load_image('tiles/horiz_wall.png')
155         elif left and top:
156             return load_image('tiles/corner_lt.png')
157         elif left and bottom:
158             return load_image('tiles/corner_lb.png')
159         elif right and top:
160             return load_image('tiles/corner_rt.png')
161         elif right and bottom:
162             return load_image('tiles/corner_rb.png')
163         elif top:
164             return load_image('tiles/end_top.png')
165         elif bottom:
166             return load_image('tiles/end_bottom.png')
167         elif left:
168             return load_image('tiles/end_right.png')
169         elif right:
170             return load_image('tiles/end_left.png')
171         return load_image('tiles/pillar.png')
172
173     def _in_limits(self, pos):
174         if pos[0] < 0:
175             return False
176         if pos[1] < 0:
177             return False
178         try:
179             self._data[pos[1]][pos[0]]
180         except IndexError:
181             return False
182         print pos, self._data[pos[1]][pos[0]]
183         return True
184
185     def blocked(self, pos):
186         if pos[0] < 0:
187             return True
188         if pos[1] < 0:
189             return True
190         try:
191             tile = self._data[pos[1]][pos[0]]
192         except IndexError:
193             return True
194         if tile == WALL or tile == ENTRY:
195             return True
196         if tile == GATE:
197             if self._gates[pos] != 'down':
198                 return True
199         return False
200
201     def is_gate(self, pos):
202         return self._data[pos[1]][pos[0]] == GATE
203
204     def is_button(self, pos):
205         return self._data[pos[1]][pos[0]] == BUTTON
206
207     def trigger_button(self, pos):
208         if not self.is_button(pos):
209             return False
210         if not self._buttons[pos] == 'active':
211             return False
212         # Find the closest gate down gate and trigger it
213         gate_pos = pos
214
215         self._changed.append((pos, self.get_single_tile(pos)))
216         self._changed.append((gate_pos, self.get_single_tile(pos)))
217
218     def damage_gate(self, pos):
219         if not self.is_gate(pos):
220             return False
221         if self._gates[pos] == -1 or self._gates[pos] == 0:
222             return False
223         self._gates[pos] = self._gates[pos] - 1
224         self._fix_gate_tile(pos)
225         self._changed.append((pos, self.get_single_tile(pos)))
226         return True
227
228     def get_changed_tiles(self):
229         ret = self._changed[:]
230         self._changed = []
231         return ret
232
233
234 class LevelList(object):
235
236     LEVELS = 'level_list'
237
238     def __init__(self):
239         self.levels = []
240         level_list = load(self.LEVELS)
241         for line in level_list:
242             line = line.strip()
243             if os.path.exists(filepath(line)):
244                 level_file = load(line)
245                 self.levels.append(Level(level_file))
246                 level_file.close()
247             else:
248                 print 'Level list includes non-existant level %s' % line
249         level_list.close()
250         self._cur_level = 0
251
252     def get_current_level(self):
253         if self._cur_level < len(self.levels):
254             return self.levels[self._cur_level]
255         else:
256             return None
257
258     def advance_to_next_level(self):
259         self._cur_level += 1
260         return self.get_current_level()
261
262     def reset(self):
263         self._cur_level = 0