Add a function to convert a map to raw image data

This commit is contained in:
Emi Simpson 2023-02-11 22:19:37 -05:00
parent 1fafbafc31
commit c0d5d69309
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847

84
write_out.py Normal file
View file

@ -0,0 +1,84 @@
from emis_funky_funktions import *
from typing import Tuple
from world import Point, Terrain, World
# 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]) -> Tuple[int, ...]:
"""
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 tuple(
byte
for (pos, (terrain, _)) in enumerate(world.tiles)
for byte in (
(255, 0, 0)
if pos in path_linear else
terrain_to_color(terrain)
)
)
if __name__ == '__main__':
import doctest
doctest.testmod()