From 057cc766ed026b7b6ceaafbd40cfc190881b1d78 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Sat, 11 Feb 2023 23:58:01 -0500 Subject: [PATCH] Add a function to save an actual image --- main.py | 29 +++++++++++------------------ write_out.py | 13 +++++++++++-- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/main.py b/main.py index 1f8cbd2..f05b14d 100644 --- a/main.py +++ b/main.py @@ -3,29 +3,22 @@ from emis_funky_funktions import * from sys import argv from operator import eq -from a_star import pathfind +from a_star import pathfind_multi from read_in import load_world_from_paths from world import Point, World - -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 - ) +from write_out import save_map def main(terrain_path: str, elevation_path: str, path_output: str, image_output: str): - print( - pathfind_world( - unwrap_r( - load_world_from_paths(terrain_path, elevation_path) - ), - Point(200, 475), - Point(200, 200) - ) + world = unwrap_r(load_world_from_paths(terrain_path, elevation_path)) + maybe_path = pathfind_multi( + world.neighbors, + world.heuristic, + [Point(200, 475), Point(200, 100)] ) + 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__': main(*argv[1:]) \ No newline at end of file diff --git a/write_out.py b/write_out.py index 6558703..14ce6a0 100644 --- a/write_out.py +++ b/write_out.py @@ -4,6 +4,8 @@ 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]: """ @@ -40,7 +42,7 @@ def terrain_to_color(t: Terrain) -> Tuple[int, int, int]: case Terrain.OOB: 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 @@ -69,7 +71,7 @@ def render_map_with_path(world: World, path: Iterable[Point]) -> Tuple[int, ...] x + y * world.width for (x, y) in path } - return tuple( + return bytes( byte for (pos, (terrain, _)) in enumerate(world.tiles) 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__': import doctest doctest.testmod() \ No newline at end of file