Add a function to save an actual image

This commit is contained in:
Emi Simpson 2023-02-11 23:58:01 -05:00
parent 3b56092319
commit 057cc766ed
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847
2 changed files with 22 additions and 20 deletions

29
main.py
View file

@ -3,29 +3,22 @@ from emis_funky_funktions import *
from sys import argv from sys import argv
from operator import eq from operator import eq
from a_star import pathfind from a_star import pathfind_multi
from read_in import load_world_from_paths from read_in import load_world_from_paths
from world import Point, World from world import Point, World
from write_out import save_map
def pathfind_world(world: World, start: Point, end: Point) -> Option[Tuple[List[Point], int]]:
"Given a `World` with a start and finish point embedded, find the best route"
return pathfind(
world.neighbors,
p(world.heuristic, end),
p(eq, end),
start
)
def main(terrain_path: str, elevation_path: str, path_output: str, image_output: str): def main(terrain_path: str, elevation_path: str, path_output: str, image_output: str):
print( world = unwrap_r(load_world_from_paths(terrain_path, elevation_path))
pathfind_world( maybe_path = pathfind_multi(
unwrap_r( world.neighbors,
load_world_from_paths(terrain_path, elevation_path) world.heuristic,
), [Point(200, 475), Point(200, 100)]
Point(200, 475),
Point(200, 200)
)
) )
print(maybe_path)
path, cost = unwrap_opt(maybe_path)
print(f'Path found! Estimated time: {cost//60000000} minutes')
save_map(path_output, world, path)
if __name__ == '__main__': if __name__ == '__main__':
main(*argv[1:]) main(*argv[1:])

View file

@ -4,6 +4,8 @@ from typing import Tuple
from world import Point, Terrain, World from world import Point, Terrain, World
from PIL import Image
# A function is just a dictionary with class # A function is just a dictionary with class
def terrain_to_color(t: Terrain) -> Tuple[int, int, int]: def terrain_to_color(t: Terrain) -> Tuple[int, int, int]:
""" """
@ -40,7 +42,7 @@ def terrain_to_color(t: Terrain) -> Tuple[int, int, int]:
case Terrain.OOB: case Terrain.OOB:
return (205,0,101) return (205,0,101)
def render_map_with_path(world: World, path: Iterable[Point]) -> Tuple[int, ...]: def render_map_with_path(world: World, path: Iterable[Point]) -> bytes:
""" """
Compute a series of RGB bytes depicting a path through the world Compute a series of RGB bytes depicting a path through the world
@ -69,7 +71,7 @@ def render_map_with_path(world: World, path: Iterable[Point]) -> Tuple[int, ...]
x + y * world.width x + y * world.width
for (x, y) in path for (x, y) in path
} }
return tuple( return bytes(
byte byte
for (pos, (terrain, _)) in enumerate(world.tiles) for (pos, (terrain, _)) in enumerate(world.tiles)
for byte in ( for byte in (
@ -79,6 +81,13 @@ def render_map_with_path(world: World, path: Iterable[Point]) -> Tuple[int, ...]
) )
) )
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__': if __name__ == '__main__':
import doctest import doctest
doctest.testmod() doctest.testmod()