ai-lab-one/write_out.py

93 lines
2.3 KiB
Python

from emis_funky_funktions import *
from typing import Tuple
from world import Point, Terrain, World
from PIL import Image
# A function is just a dictionary with class
def terrain_to_color(t: Terrain) -> Tuple[int, int, int]:
"""
Maps a terrain to the color used to denote it in the spec
Where the spec is
https://cs.rit.edu/~jro/courses/intelSys/labs/orienteering/
Return format is a 3-tuple of integer values between 0 and 255, representing RGB
components of a pixel.
>>> terrain_to_color(Terrain.MEDIUM_FOREST)
(2, 208, 60)
"""
match t:
case Terrain.OPEN_LAND:
return (248,148,18)
case Terrain.ROUGH_MEADOW:
return (255,192,0)
case Terrain.EASY_FOREST:
return (255,255,255)
case Terrain.MEDIUM_FOREST:
return (2,208,60)
case Terrain.WALK_FOREST:
return (2,136,40)
case Terrain.BRUSH:
return (5,73,24)
case Terrain.WET:
return (0,0,255)
case Terrain.ROAD:
return (71,51,3)
case Terrain.FOOTPATH:
return (0,0,0)
case Terrain.OOB:
return (205,0,101)
def render_map_with_path(world: World, path: Iterable[Point]) -> bytes:
"""
Compute a series of RGB bytes depicting a path through the world
Uses the colors specified in `terrain_to_color()` for depicting the terrain, with the
path itself drawn in pure, aggressive red (#f00).
>>> tiles = [
... (Terrain.OPEN_LAND, 0), (Terrain.ROUGH_MEADOW, 0), (Terrain.WET, 0),
... (Terrain.OPEN_LAND, 0), (Terrain.ROUGH_MEADOW, 0), (Terrain.WALK_FOREST, 0),
... (Terrain.BRUSH, 0), (Terrain.BRUSH, 0), (Terrain.MEDIUM_FOREST, 0)
... ]
>>> path = [(0, 0), (1, 0), (1, 1), (2, 1)]
>>> world = World(tiles, width=3)
>>> render_map_with_path(world, path) #doctest: +NORMALIZE_WHITESPACE
(255, 0, 0,
255, 0, 0,
0, 0, 255,
248, 148, 18,
255, 0, 0,
255, 0, 0,
5, 73, 24,
5, 73, 24,
2, 208, 60)
"""
path_linear = {
x + y * world.width
for (x, y) in path
}
return bytes(
byte
for (pos, (terrain, _)) in enumerate(world.tiles)
for byte in (
(255, 0, 0)
if pos in path_linear else
terrain_to_color(terrain)
)
)
def save_map(file_path: str, world: World, world_path: Iterable[Point]):
Image.frombytes(
'RGB',
(world.width, len(world.tiles) // world.width),
render_map_with_path(world, world_path)
).save(file_path)
if __name__ == '__main__':
import doctest
doctest.testmod()