3 # This is the new open-adventure dungeon generator. It'll eventually replace the existing dungeon.c It currently outputs a .h and .c pair for C code.
8 json_name = "adventure.json"
13 """Add C escape sequences to a string."""
14 string = string.replace("\n", "\\n")
15 string = string.replace("\t", "\\t")
16 string = string.replace('"', '\\"')
17 string = string.replace("'", "\\'")
20 def write_regular_messages(name, h, c):
22 h += "enum {}_refs {{\n".format(name)
23 c += "char* {}[] = {{\n".format(name)
26 for key, text in dungeon[name].items():
27 h += " {},\n".format(key)
32 c += " \"{}\",\n".format(text)
41 with open(json_name, "r") as f:
42 dungeon = json.load(f, object_pairs_hook = collections.OrderedDict)
44 h = """#include <stdio.h>
49 } object_description_t;
51 extern char* long_location_descriptions[];
52 extern char* short_location_descriptions[];
53 extern object_description_t object_descriptions[];
54 extern char* arbitrary_messages[];
55 extern char* class_messages[];
56 extern char* turn_threshold_messages[];
68 "long_location_descriptions",
69 "short_location_descriptions",
71 "turn_threshold_messages",
73 h, c = write_regular_messages(name, h, c)
75 h += "enum object_descriptions_refs {\n"
76 c += "object_description_t object_descriptions[] = {\n"
77 for key, data in dungeon["object_descriptions"].items():
79 data["inventory"] = "\"{}\"".format(c_escape(data["inventory"]))
80 except AttributeError:
81 data["inventory"] = "NULL"
82 h += " {},\n".format(key)
84 c += " .inventory = {},\n".format(data["inventory"])
87 c += " .longs = (char* []) {\n"
88 for l in data["longs"]:
90 c += " \"{}\"\n".format(l)
92 except (TypeError, IndexError):
93 c += " .longs = NULL,\n"
102 for filename, string in d.items():
103 with open(filename, "w") as f: