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 yaml_name = "adventure.yaml"
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 quotewrap(string):
21 """Wrap a string in double quotes."""
22 return '"' + string + '"'
24 def write_regular_messages(name, h, c):
26 h += "enum {}_refs {{\n".format(name)
27 for key, text in dungeon[name]:
28 h += " {},\n".format(key)
31 c += "const char* {}[] = {{\n".format(name)
33 for key, text in dungeon[name]:
38 c += " \"{}\",\n".format(text)
44 with open(yaml_name, "r") as f:
45 dungeon = yaml.load(f)
47 h = """#include <stdio.h>
50 const char* inventory;
52 } object_description_t;
60 descriptions_t description;
65 const char* yes_response;
68 extern location_t locations[];
69 extern object_description_t object_descriptions[];
70 extern const char* arbitrary_messages[];
71 extern const char* class_messages[];
72 extern const char* turn_threshold_messages[];
73 extern obituary_t obituaries[];
76 extern int maximum_deaths;
86 "turn_threshold_messages",
88 h, c = write_regular_messages(name, h, c)
90 h += "enum locations_refs {\n"
91 c += "location_t locations[] = {\n"
92 for key, data in dungeon["locations"]:
93 h += " {},\n".format(key)
96 short = quotewrap(c_escape(data["description"]["short"]))
97 except AttributeError:
100 long = quotewrap(c_escape(data["description"]["long"]))
101 except AttributeError:
110 """.format(short, long)
115 h += "enum object_descriptions_refs {\n"
116 c += "object_description_t object_descriptions[] = {\n"
117 for key, data in dungeon["object_descriptions"]:
119 data["inventory"] = "\"{}\"".format(c_escape(data["inventory"]))
120 except AttributeError:
121 data["inventory"] = "NULL"
122 h += " {},\n".format(key)
124 c += " .inventory = {},\n".format(data["inventory"])
127 c += " .longs = (const char* []) {\n"
128 for l in data["longs"]:
130 c += " \"{}\",\n".format(l)
132 except (TypeError, IndexError):
133 c += " .longs = NULL,\n"
138 c += "obituary_t obituaries[] = {\n"
139 for obit in dungeon["obituaries"]:
141 query = quotewrap(c_escape(obit["query"]))
142 yes_response = quotewrap(c_escape(obit["yes_response"]))
148 """.format(query, yes_response)
154 """.format(len(dungeon["class_messages"]))
157 int maximum_deaths = {};
158 """.format(len(dungeon["obituaries"]))
161 # finally, write out the files
166 for filename, string in d.items():
167 with open(filename, "w") as f: