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"
13 h_template = """/* Generated from adventure.yaml - do not hand-hack! */
20 #define SILENT -1 /* no sound */
23 const char* inventory;
27 }} object_description_t;
35 descriptions_t description;
42 const char* yes_response;
64 extern location_t locations[];
65 extern object_description_t object_descriptions[];
66 extern const char* arbitrary_messages[];
67 extern const class_t classes[];
68 extern turn_threshold_t turn_thresholds[];
69 extern obituary_t obituaries[];
70 extern hint_t hints[];
71 extern long conditions[];
78 #define NTHRESHOLDS {}
80 enum arbitrary_messages_refs {{
84 enum locations_refs {{
88 enum object_descriptions_refs {{
92 /* State definitions */
95 #endif /* end NEWDB_H */
98 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
103 const char* arbitrary_messages[] = {{
107 const class_t classes[] = {{
111 turn_threshold_t turn_thresholds[] = {{
115 location_t locations[] = {{
119 object_description_t object_descriptions[] = {{
123 obituary_t obituaries[] = {{
131 long conditions[] = {{
138 def make_c_string(string):
139 """Render a Python string into C string literal format."""
142 string = string.replace("\n", "\\n")
143 string = string.replace("\t", "\\t")
144 string = string.replace('"', '\\"')
145 string = string.replace("'", "\\'")
146 string = '"' + string + '"'
150 reflist = [x[0] for x in l]
153 ref_str += " {},\n".format(ref)
154 ref_str = ref_str[:-1] # trim trailing newline
157 def get_arbitrary_messages(arb):
162 arb_str += template.format(make_c_string(item[1]))
163 arb_str = arb_str[:-1] # trim trailing newline
166 def get_class_messages(cls):
174 threshold = item["threshold"]
175 message = make_c_string(item["message"])
176 cls_str += template.format(threshold, message)
177 cls_str = cls_str[:-1] # trim trailing newline
180 def get_turn_thresholds(trn):
189 threshold = item["threshold"]
190 point_loss = item["point_loss"]
191 message = make_c_string(item["message"])
192 trn_str += template.format(threshold, point_loss, message)
193 trn_str = trn_str[:-1] # trim trailing newline
196 def get_locations(loc):
208 short_d = make_c_string(item[1]["description"]["short"])
209 long_d = make_c_string(item[1]["description"]["long"])
210 sound = item[1].get("sound", "SILENT")
211 loud = "true" if item[1].get("loud") else "false"
212 loc_str += template.format(short_d, long_d, sound, loud)
213 loc_str = loc_str[:-1] # trim trailing newline
216 def get_object_descriptions(obj):
219 .longs = (const char* []) {{
222 .sounds = (const char* []) {{
225 .texts = (const char* []) {{
232 i_msg = make_c_string(item[1]["inventory"])
234 if item[1]["longs"] == None:
235 longs_str = " " * 12 + "NULL,"
238 for l_msg in item[1]["longs"]:
239 if not isinstance(l_msg, str):
242 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
243 longs_str = longs_str[:-1] # trim trailing newline
246 statedefines += "/* States for %s */\n" % item[0]
247 for (i, (label, message)) in enumerate(labels):
248 if len(message) >= 45:
249 message = message[:45] + "..."
250 statedefines += "#define %s\t%d /* %s */\n" % (label, i, message)
253 if item[1].get("sounds") == None:
254 sounds_str = " " * 12 + "NULL,"
256 for l_msg in item[1]["sounds"]:
257 sounds_str += " " * 12 + make_c_string(l_msg) + ",\n"
258 sounds_str = sounds_str[:-1] # trim trailing newline
260 if item[1].get("texts") == None:
261 texts_str = " " * 12 + "NULL,"
263 for l_msg in item[1]["texts"]:
264 texts_str += " " * 12 + make_c_string(l_msg) + ",\n"
265 texts_str = texts_str[:-1] # trim trailing newline
266 obj_str += template.format(i_msg, longs_str, sounds_str, texts_str)
267 obj_str = obj_str[:-1] # trim trailing newline
270 def get_obituaries(obit):
278 query = make_c_string(o["query"])
279 yes = make_c_string(o["yes_response"])
280 obit_str += template.format(query, yes)
281 obit_str = obit_str[:-1] # trim trailing newline
284 def get_hints(hnt, arb):
296 item = member["hint"]
297 number = item["number"]
298 penalty = item["penalty"]
299 turns = item["turns"]
300 question = make_c_string(md[item["question"]])
301 hint = make_c_string(md[item["hint"]])
302 hnt_str += template.format(number, penalty, turns, question, hint)
303 hnt_str = hnt_str[:-1] # trim trailing newline
306 def get_condbits(locations):
308 for (name, loc) in locations:
309 conditions = loc["conditions"]
310 hints = loc.get("hints") or []
312 for flag in conditions:
314 flaglist.append(flag)
315 line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
316 trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
319 if line.startswith("|"):
323 cnd_str += " " + line + ",\t// " + name + "\n"
326 if __name__ == "__main__":
327 with open(yaml_name, "r") as f:
330 c = c_template.format(
332 get_arbitrary_messages(db["arbitrary_messages"]),
333 get_class_messages(db["classes"]),
334 get_turn_thresholds(db["turn_thresholds"]),
335 get_locations(db["locations"]),
336 get_object_descriptions(db["object_descriptions"]),
337 get_obituaries(db["obituaries"]),
338 get_hints(db["hints"], db["arbitrary_messages"]),
339 get_condbits(db["locations"]),
342 h = h_template.format(
343 len(db["locations"]),
344 len(db["object_descriptions"]),
347 len(db["obituaries"]),
348 len(db["turn_thresholds"]),
349 get_refs(db["arbitrary_messages"]),
350 get_refs(db["locations"]),
351 get_refs(db["object_descriptions"]),
355 with open(h_name, "w") as hf:
358 with open(c_name, "w") as cf: