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.
7 yaml_name = "adventure.yaml"
11 h_template = """/* Generated from adventure.yaml - do not hand-hack! */
16 const char* inventory;
18 }} object_description_t;
26 descriptions_t description;
31 const char* yes_response;
53 extern location_t locations[];
54 extern object_description_t object_descriptions[];
55 extern const char* arbitrary_messages[];
56 extern const class_t classes[];
57 extern turn_threshold_t turn_thresholds[];
58 extern obituary_t obituaries[];
59 extern hint_t hints[];
61 extern const size_t CLSSES;
62 extern const int maximum_deaths;
63 extern const int turn_threshold_count;
65 enum arbitrary_messages_refs {{
69 enum locations_refs {{
73 enum object_descriptions_refs {{
80 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
84 const char* arbitrary_messages[] = {{
88 const class_t classes[] = {{
92 turn_threshold_t turn_thresholds[] = {{
96 location_t locations[] = {{
100 object_description_t object_descriptions[] = {{
104 obituary_t obituaries[] = {{
112 const size_t CLSSES = {};
113 const int maximum_deaths = {};
114 const int turn_threshold_count = {};
119 def make_c_string(string):
120 """Render a Python string into C string literal format."""
123 string = string.replace("\n", "\\n")
124 string = string.replace("\t", "\\t")
125 string = string.replace('"', '\\"')
126 string = string.replace("'", "\\'")
127 string = '"' + string + '"'
131 reflist = [x[0] for x in l]
134 ref_str += " {},\n".format(ref)
135 ref_str = ref_str[:-1] # trim trailing newline
138 def get_arbitrary_messages(arb):
143 arb_str += template.format(make_c_string(item[1]))
144 arb_str = arb_str[:-1] # trim trailing newline
147 def get_class_messages(cls):
155 threshold = item["threshold"]
156 message = make_c_string(item["message"])
157 cls_str += template.format(threshold, message)
158 cls_str = cls_str[:-1] # trim trailing newline
161 def get_turn_thresholds(trn):
170 threshold = item["threshold"]
171 point_loss = item["point_loss"]
172 message = make_c_string(item["message"])
173 trn_str += template.format(threshold, point_loss, message)
174 trn_str = trn_str[:-1] # trim trailing newline
177 def get_locations(loc):
187 short_d = make_c_string(item[1]["description"]["short"])
188 long_d = make_c_string(item[1]["description"]["long"])
189 loc_str += template.format(short_d, long_d)
190 loc_str = loc_str[:-1] # trim trailing newline
193 def get_object_descriptions(obj):
196 .longs = (const char* []) {{
203 i_msg = make_c_string(item[1]["inventory"])
205 if item[1]["longs"] == None:
206 longs_str = " " * 12 + "NULL,"
208 for l_msg in item[1]["longs"]:
209 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
210 longs_str = longs_str[:-1] # trim trailing newline
211 obj_str += template.format(i_msg, longs_str)
212 obj_str = obj_str[:-1] # trim trailing newline
215 def get_obituaries(obit):
223 query = make_c_string(o["query"])
224 yes = make_c_string(o["yes_response"])
225 obit_str += template.format(query, yes)
226 obit_str = obit_str[:-1] # trim trailing newline
229 def get_hints(hnt, arb):
241 number = item["number"]
242 penalty = item["penalty"]
243 turns = item["turns"]
244 question = make_c_string(md[item["question"]])
245 hint = make_c_string(md[item["hint"]])
246 hnt_str += template.format(number, penalty, turns, question, hint)
247 hnt_str = hnt_str[:-1] # trim trailing newline
251 if __name__ == "__main__":
252 with open(yaml_name, "r") as f:
255 h = h_template.format(
256 get_refs(db["arbitrary_messages"]),
257 get_refs(db["locations"]),
258 get_refs(db["object_descriptions"]),
261 c = c_template.format(
263 get_arbitrary_messages(db["arbitrary_messages"]),
264 get_class_messages(db["classes"]),
265 get_turn_thresholds(db["turn_thresholds"]),
266 get_locations(db["locations"]),
267 get_object_descriptions(db["object_descriptions"]),
268 get_obituaries(db["obituaries"]),
269 get_hints(db["hints"], db["arbitrary_messages"]),
271 len(db["obituaries"]),
272 len(db["turn_thresholds"]),
275 with open(h_name, "w") as hf:
278 with open(c_name, "w") as cf: