forked from s464965/WMICraft
41 lines
1.0 KiB
Python
41 lines
1.0 KiB
Python
import json
|
|
import random
|
|
import string
|
|
from datetime import datetime
|
|
|
|
import numpy
|
|
import numpy.typing as npt
|
|
from os import listdir
|
|
from os.path import isfile, join
|
|
|
|
|
|
|
|
# Save map to file
|
|
def export_map(grid: npt.NDArray):
|
|
json_data = {"map": grid.tolist()}
|
|
|
|
now = datetime.now()
|
|
file_name = "map_" + now.strftime("%Y_%m_%d_%H_%M_%S") + ".json"
|
|
|
|
with open("resources/maps/" + file_name, "w") as write_file:
|
|
json.dump(json_data, write_file)
|
|
print("Saved map to file " + file_name)
|
|
|
|
|
|
def import_random_map() -> object:
|
|
path = "resources/maps"
|
|
files = [f for f in listdir(path) if isfile(join(path, f))]
|
|
random_map_name = random.choice(files)
|
|
return import_map(random_map_name)
|
|
|
|
|
|
# Read map from file
|
|
def import_map(file: string) -> object:
|
|
with open("resources/maps/" + file, "r") as read_file:
|
|
print("Reading map from file " + file)
|
|
decoded_json = json.load(read_file)
|
|
|
|
decoded_grid = numpy.asarray(decoded_json["map"])
|
|
print(decoded_grid)
|
|
return decoded_grid.tolist()
|