Last active
August 3, 2019 10:30
-
-
Save kyoukaya/32d353d0b8a874a71797550208bcba14 to your computer and use it in GitHub Desktop.
2511T2 Dungeon Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
""" | |
Levels in CSV are a lot easier to visualize! | |
This short script converts to and fro 2511's dungeon JSON format to a simple CSV format. | |
Goals are kinda iffy, sorry! You can change the goal conditions when converting from CSV | |
to JSON by editing this global variable: `goals = OR(goal("exit"), goal("treasure"))`. | |
It's on line 52 at the moment. | |
Example output: https://cdn.discordapp.com/attachments/474517240117723139/607158167050387476/unknown.png | |
""" | |
import csv | |
from sys import argv | |
import json | |
shorthand_to_type = { | |
"pl": "player", | |
"en": "enemy", | |
"wl": "wall", | |
"bl": "boulder", | |
"dr": "door", | |
"sw": "switch", | |
"ex": "exit", | |
"ky": "key", | |
"sr": "sword", | |
"bm": "bomb", | |
"tr": "treasure", | |
"in": "invincibility", | |
} | |
type_to_shorthand = {v: k for k, v in shorthand_to_type.items()} | |
def OR(*args): | |
ret = {"goal": "OR"} | |
subgoals = [x for x in args] | |
ret["subgoals"] = subgoals | |
return ret | |
def AND(*args): | |
ret = {"goal": "AND"} | |
subgoals = [x for x in args] | |
ret["subgoals"] = subgoals | |
return ret | |
def goal(string: str): | |
return {"goal": string} | |
goals = OR(goal("exit"), goal("treasure")) | |
def json_to_csv(path: str): | |
with open(path, "r") as fin: | |
obj = json.load(fin) | |
width = obj["width"] | |
height = obj["height"] | |
rows = [[" " for _ in range(width)] for _ in range(height)] | |
entities = obj["entities"] | |
for entity in entities: | |
rows[entity["y"]][entity["x"]] = type_to_shorthand[entity["type"]] | |
with open(path.rstrip("json") + "csv", "w") as fout: | |
writer = csv.writer(fout) | |
writer.writerows(rows) | |
def csv_to_json(path: str): | |
temp_rows = [] | |
height = 0 | |
width = 0 | |
with open(path, "r") as fin: | |
reader = csv.reader(fin) | |
for line in reader: | |
height += 1 | |
width = max(len(line), width) | |
temp_rows.append(line) | |
entities = [] | |
for y, row in enumerate(temp_rows): | |
for x, col in enumerate(row): | |
if col == " ": | |
continue | |
entities.append({"x": x, "y": y, "type": shorthand_to_type[col]}) | |
with open(path.rstrip("csv") + "json", "w") as fout: | |
jsout = { | |
"width": width, | |
"height": height, | |
"entities": entities, | |
"goal-condition": goals, | |
} | |
json.dump(jsout, fout, indent=4) | |
if __name__ == "__main__": | |
if len(argv) < 3 or argv[1] not in ["writejson", "writecsv"]: | |
print( | |
f"{argv[0]} takes a dungeon in csv format and convers it to json, and" | |
+ f"vice-versa.\nusage: {argv[0]} (writecsv|writejson) (*.json|*.csv)" | |
) | |
exit(1) | |
if argv[1] == "writejson": | |
if not argv[2].endswith(".csv"): | |
print(".csv input required") | |
exit(1) | |
csv_to_json(argv[2]) | |
if argv[1] == "writecsv": | |
if not argv[2].endswith(".json"): | |
print(".json input required") | |
exit(1) | |
json_to_csv(argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment