-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.py
403 lines (343 loc) · 14.9 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import math
import pathlib
from importlib import resources
from typing import List, Optional, Sequence, Tuple, Union
import numpy as np
import pygame
from PIL import Image
from ..enums import BurnStatus, GameStatus
from ..utils.log import create_logger
from ..utils.units import mph_to_ftpm
from .image import load_image
from .sprites import Agent, Fire, FireLine, Terrain
log = create_logger(__name__)
class Game:
"""
Class that controls the game. This class will initalize the game and allow for
terrain, fire, and other sprites to be rendered and interact.
"""
def __init__(
self,
screen_size: Tuple[int, int],
rescale_factor: Optional[int] = None,
headless: bool = False,
record: bool = False,
show_wind_magnitude: bool = False,
show_wind_direction: bool = False,
mw_speed_min: float = 0.0,
mw_speed_max: float = mph_to_ftpm(150.0),
dw_deg_min: float = 0.0,
dw_deg_max: float = 360.0,
) -> None:
"""
Initalize the class by creating the game display and background.
Arguments:
screen_size: The (n,n) size of the game screen/display in pixels.
rescale_size: The (s, s) factor to rescale display to
headless: Flag to run in a headless state.
record: Flag to save a recording of the simulation game screen
"""
if record and headless:
raise ValueError(
"The game cannot be recorded with headless=True. "
"Got record=True and headless=True"
)
self.screen_size = screen_size
self.rescale_factor = rescale_factor
self.headless = headless
self.record = record
self.show_wind_magnitude = show_wind_magnitude
self.show_wind_direction = show_wind_direction
self.mw_speed_min = mw_speed_min
self.mw_speed_max = mw_speed_max
self.dw_deg_min = dw_deg_min
self.dw_deg_max = dw_deg_max
# Map to track which pixels are on fire or have burned
self.fire_map = np.full(screen_size, BurnStatus.UNBURNED)
self.background: Optional[pygame.surface.Surface] = None
if not self.headless:
pygame.init()
# self.screen is for drawing all backgrounds/sprites at the simulation's scale
# self.display_screen is for actually displaying the simulation to the user
# These can have different sizes to do potential rescaling
if self.rescale_factor is None:
self.display_screen = pygame.display.set_mode(screen_size)
else:
rescale_size = (
self.screen_size[1] * self.rescale_factor,
self.screen_size[0] * self.rescale_factor,
)
self.display_screen = pygame.display.set_mode(rescale_size)
self.screen = pygame.Surface((screen_size[1], screen_size[0]))
pygame.display.set_caption("SimFire")
with resources.path("simfire.utils.assets", "fireline_logo.png") as path:
fireline_logo_path = path
pygame.display.set_icon(load_image(str(fireline_logo_path)))
# pygame returns as w, h
self.background = pygame.Surface(self.screen.get_size())
self.background = self.background.convert()
self.background.fill((0, 0, 0))
self.frames: Optional[List[Image.Image]] = None
if self.record:
self.frames = []
def _toggle_wind_magnitude_display(self):
"""
Toggle display of wind MAGNITUDE over the main screen.
"""
self.show_wind_magnitude = not self.show_wind_magnitude
if self.show_wind_magnitude is False:
log.info("Wind Magnitude OFF")
else:
log.info("Wind Magnitude ON")
return
def _toggle_wind_direction_display(self):
"""
Toggle display of wind DIRECTION over the main screen.
"""
self.show_wind_direction = not self.show_wind_direction
if self.show_wind_direction is False:
log.info("Wind Direction OFF")
else:
log.info("Wind Direction ON")
return
def _disable_wind_magnitude_display(self):
"""
Toggle display of wind DIRECTION over the main screen.
"""
self.show_wind_magnitude = False
def _disable_wind_direction_display(self):
"""
Toggle display of wind DIRECTION over the main screen.
"""
self.show_wind_direction = False
def _get_wind_direction_color(self, direction: float) -> Tuple[int, int, int]:
"""
Get the color and intensity representing direction based on wind direction.
0/360: Black, 90: Red, 180: White, Blue: 270
Returns tuple of RGBa where a is alpha channel or transparency of the color
Arguments:
direction: Float value of the angle 0-360.
ws_min: Minimum wind speed.
ws_max: Maximum wind speed.
"""
north_min = 0.0
north_max = 360.0
east = 90.0
south = 180.0
west = 270.0
colorRGB = (255.0, 255.0, 255.0) # Default white
# North to East, Red to Green
if direction >= north_min and direction < east:
angleRange = east - north_min
# Red
redMin = 255.0
redMax = 128.0
redRange = redMax - redMin # 255 - 128 red range from north to east
red = (((direction - north_min) * redRange) / angleRange) + redMin
# Green
greenMin = 0.0
greenMax = 255.0
greenRange = greenMax - greenMin # 0 - 255 red range from north to east
green = (((direction - north_min) * greenRange) / angleRange) + greenMin
colorRGB = (red, green, 0.0)
# East to South, Green to Teal
if direction >= east and direction < south:
angleRange = south - east
# Red
redMin = 128.0
redMax = 0.0
redRange = redMax - redMin # 128 - 0 red range from east to south
red = (((direction - east) * redRange) / angleRange) + redMin
# Blue
blueMin = 0
blueMax = 255
blueRange = blueMax - blueMin # 0 - 255 blue range from east to south
blue = (((direction - east) * blueRange) / angleRange) + blueMin
colorRGB = (red, 255, blue)
# South to West, Teal to Purple
if direction >= south and direction < west:
angleRange = west - south
# Red
redMin = 0
redMax = 128
redRange = redMax - redMin # 0 - 128 red range from south to west
red = (((direction - south) * redRange) / angleRange) + redMin
# Green
greenMin = 255
greenMax = 0
greenRange = greenMax - greenMin # 0 - 255 green range from south to west
green = (((direction - south) * greenRange) / angleRange) + greenMin
colorRGB = (red, green, 255)
# West to North, Purple to Red
if direction <= north_max and direction >= west:
angleRange = north_max - west
# Red
redMin = 128.0
redMax = 255.0
redRange = redMax - redMin # 128 - 255 red range from east to south
red = (((direction - west) * redRange) / angleRange) + redMin
# Blue
blueMin = 0
blueMax = 255
blueRange = blueMax - blueMin # 0 - 255 blue range from east to south
blue = (((direction - west) * blueRange) / angleRange) + blueMin
colorRGB = (red, 0, blue)
floorColorRGB = (
int(math.floor(colorRGB[0])),
int(math.floor(colorRGB[1])),
int(math.floor(colorRGB[2])),
)
return floorColorRGB
def _get_wind_mag_surf(
self, wind_magnitude_map: Union[Sequence[Sequence[float]], np.ndarray]
) -> pygame.surface.Surface:
"""
Compute the wind magnitude surface for display.
Arguments:
wind_magnitude_map: The map/array containing wind magnitudes at each pixel
location
Returns:
The PyGame Surface for the wind magnitude
"""
w_max = np.amax(wind_magnitude_map)
w_min = np.amin(wind_magnitude_map)
wind_speed_range = w_max - w_min
# Constant value wind at all locations. Set everything to middle value
if wind_speed_range == 0:
color_arr = np.full(self.screen.get_size(), 127, dtype=np.uint8)
wind_mag_surf = pygame.surfarray.make_surface(color_arr.swapaxes(0, 1))
else:
wind_mag_surf = pygame.Surface(self.screen.get_size())
for y_idx, y in enumerate(wind_magnitude_map):
for x_idx, x in enumerate(y):
w_mag = x
gradient = 255 - 0
color_mag = int(((w_mag - w_min) * gradient) / (wind_speed_range) + 0)
wind_mag_surf.set_at((x_idx, y_idx), pygame.Color(0, color_mag, 0))
return wind_mag_surf
def _get_wind_dir_surf(
self, wind_direction_map: Union[Sequence[Sequence[float]], np.ndarray]
) -> pygame.Surface:
"""
Compute the wind direction surface for display.
Arguments:
wind_direction_map: The map/array containing wind directions at each pixel
location
Returns:
The PyGame Surface for the wind direction
"""
wind_dir_surf = pygame.Surface(self.screen.get_size())
for y_idx, y in enumerate(wind_direction_map):
for x_idx, x in enumerate(y):
w_dir = x
color = self._get_wind_direction_color(w_dir)
pyColor = pygame.Color(color[0], color[1], color[2], a=191)
wind_dir_surf.set_at((x_idx, y_idx), pyColor)
return wind_dir_surf
def quit(self) -> None:
"""
Close the PyGame window and stop the `Game`
"""
pygame.display.quit()
pygame.quit()
def save(self, path: pathlib.Path, duration: int = 100) -> None:
"""Save a GIF of the simulation to a specified path
Arguments:
path: The path to save the GIF to with filename.
duration: The time to display the current frame of the GIF, in milliseconds.
"""
if self.frames is not None:
self.frames[0].save(
path,
save_all=True,
duration=duration,
loop=0,
append_images=self.frames[1:],
)
else:
log.error(
"self.frames is set to None when attempting to save. Make sure "
"self.frames is not set to None when saving a GIF."
)
raise ValueError
def update(
self,
terrain: Terrain,
fire_sprites: Sequence[Fire],
fireline_sprites: Sequence[FireLine],
agent_sprites: Sequence[Agent],
wind_magnitude_map: Union[Sequence[Sequence[float]], np.ndarray],
wind_direction_map: Union[Sequence[Sequence[float]], np.ndarray],
) -> GameStatus:
"""
Update the game display using the provided terrain, sprites, and
environment data. Most of the logic for the game is handled within
each sprite/manager, so this is mostly just calls to update everything.
Arguments:
terrain: The Terrain class that comprises the burnable area.
fire_sprites: A list of all Fire sprites that are actively burning.
fireline_sprites: A list of all FireLine sprites that are dug.
wind_magnitude_map: The map/array containing wind magnitudes at each pixel
location
wind_direction_map: The map/array containing wind directions at each pixel
location
"""
status = GameStatus.RUNNING
# Convert the sequences to list for list addition later
if not isinstance(fire_sprites, list):
fire_sprites = list(fire_sprites)
if not isinstance(fireline_sprites, list):
fireline_sprites = list(fireline_sprites)
if not isinstance(agent_sprites, list):
agent_sprites = list(agent_sprites)
if not self.headless:
for event in pygame.event.get():
if event.type == pygame.QUIT:
status = GameStatus.QUIT
if event.type == pygame.KEYDOWN:
keys = pygame.key.get_pressed()
if keys[pygame.K_m] is True:
self._disable_wind_direction_display()
self._toggle_wind_magnitude_display()
if keys[pygame.K_n] is True:
self._disable_wind_magnitude_display()
self._toggle_wind_direction_display()
# Create a layered group so that the fire appears on top
fire_sprites_group = pygame.sprite.LayeredUpdates(
*(fire_sprites + fireline_sprites)
)
agent_sprites_group = pygame.sprite.LayeredUpdates(*agent_sprites)
all_sprites = pygame.sprite.LayeredUpdates(
*agent_sprites_group, *fire_sprites_group, terrain
)
# Update and draw the sprites
if not self.headless:
if self.background is not None:
for sprite in all_sprites.sprites():
if sprite.rect is not None:
self.screen.blit(self.background, sprite.rect, sprite.rect)
fire_sprites_group.update()
agent_sprites_group.update()
terrain.update(self.fire_map)
all_sprites.draw(self.screen)
if self.rescale_factor is None:
self.display_screen.blit(self.screen, (0, 0))
else:
self.display_screen.blit(
pygame.transform.smoothscale_by(self.screen, self.rescale_factor),
(0, 0),
)
if self.record and self.frames is not None:
screen_bytes = pygame.image.tostring(self.screen, "RGB")
screen_im = Image.frombuffer(
"RGB", (self.screen_size[1], self.screen_size[0]), screen_bytes
)
self.frames.append(screen_im)
if self.show_wind_magnitude is True:
wind_mag_surf = self._get_wind_mag_surf(wind_magnitude_map)
self.screen.blit(wind_mag_surf, (0, 0))
if self.show_wind_direction is True:
wind_dir_surf = self._get_wind_dir_surf(wind_direction_map)
self.screen.blit(wind_dir_surf, (0, 0))
pygame.display.update()
return status