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[];
76 #define NTHRESHOLDS {}
78 enum arbitrary_messages_refs {{
82 enum locations_refs {{
86 enum object_descriptions_refs {{
90 /* State definitions */
93 #endif /* end NEWDB_H */
96 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
101 const char* arbitrary_messages[] = {{
105 const class_t classes[] = {{
109 turn_threshold_t turn_thresholds[] = {{
113 location_t locations[] = {{
117 object_description_t object_descriptions[] = {{
121 obituary_t obituaries[] = {{
129 long conditions[] = {{
136 def make_c_string(string):
137 """Render a Python string into C string literal format."""
140 string = string.replace("\n", "\\n")
141 string = string.replace("\t", "\\t")
142 string = string.replace('"', '\\"')
143 string = string.replace("'", "\\'")
144 string = '"' + string + '"'
148 reflist = [x[0] for x in l]
151 ref_str += " {},\n".format(ref)
152 ref_str = ref_str[:-1] # trim trailing newline
155 def get_arbitrary_messages(arb):
160 arb_str += template.format(make_c_string(item[1]))
161 arb_str = arb_str[:-1] # trim trailing newline
164 def get_class_messages(cls):
172 threshold = item["threshold"]
173 message = make_c_string(item["message"])
174 cls_str += template.format(threshold, message)
175 cls_str = cls_str[:-1] # trim trailing newline
178 def get_turn_thresholds(trn):
187 threshold = item["threshold"]
188 point_loss = item["point_loss"]
189 message = make_c_string(item["message"])
190 trn_str += template.format(threshold, point_loss, message)
191 trn_str = trn_str[:-1] # trim trailing newline
194 def get_locations(loc):
206 short_d = make_c_string(item[1]["description"]["short"])
207 long_d = make_c_string(item[1]["description"]["long"])
208 sound = item[1].get("sound", "SILENT")
209 loud = "true" if item[1].get("loud") else "false"
210 loc_str += template.format(short_d, long_d, sound, loud)
211 loc_str = loc_str[:-1] # trim trailing newline
214 def get_object_descriptions(obj):
217 .longs = (const char* []) {{
220 .sounds = (const char* []) {{
223 .texts = (const char* []) {{
230 i_msg = make_c_string(item[1]["inventory"])
232 if item[1]["longs"] == None:
233 longs_str = " " * 12 + "NULL,"
236 for l_msg in item[1]["longs"]:
237 if not isinstance(l_msg, str):
240 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
241 longs_str = longs_str[:-1] # trim trailing newline
244 statedefines += "/* States for %s */\n" % item[0]
245 for (i, (label, message)) in enumerate(labels):
246 if len(message) >= 45:
247 message = message[:45] + "..."
248 statedefines += "#define %s\t%d /* %s */\n" % (label, i, message)
251 if item[1].get("sounds") == None:
252 sounds_str = " " * 12 + "NULL,"
254 for l_msg in item[1]["sounds"]:
255 sounds_str += " " * 12 + make_c_string(l_msg) + ",\n"
256 sounds_str = sounds_str[:-1] # trim trailing newline
258 if item[1].get("texts") == None:
259 texts_str = " " * 12 + "NULL,"
261 for l_msg in item[1]["texts"]:
262 texts_str += " " * 12 + make_c_string(l_msg) + ",\n"
263 texts_str = texts_str[:-1] # trim trailing newline
264 obj_str += template.format(i_msg, longs_str, sounds_str, texts_str)
265 obj_str = obj_str[:-1] # trim trailing newline
268 def get_obituaries(obit):
276 query = make_c_string(o["query"])
277 yes = make_c_string(o["yes_response"])
278 obit_str += template.format(query, yes)
279 obit_str = obit_str[:-1] # trim trailing newline
282 def get_hints(hnt, arb):
294 item = member["hint"]
295 number = item["number"]
296 penalty = item["penalty"]
297 turns = item["turns"]
298 question = make_c_string(md[item["question"]])
299 hint = make_c_string(md[item["hint"]])
300 hnt_str += template.format(number, penalty, turns, question, hint)
301 hnt_str = hnt_str[:-1] # trim trailing newline
304 def get_condbits(locations):
306 for (name, loc) in locations:
307 conditions = loc["conditions"]
308 hints = loc.get("hints") or []
310 for flag in conditions:
312 flaglist.append(flag)
313 line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
314 trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
317 if line.startswith("|"):
321 cnd_str += " " + line + ",\t// " + name + "\n"
324 if __name__ == "__main__":
325 with open(yaml_name, "r") as f:
328 c = c_template.format(
330 get_arbitrary_messages(db["arbitrary_messages"]),
331 get_class_messages(db["classes"]),
332 get_turn_thresholds(db["turn_thresholds"]),
333 get_locations(db["locations"]),
334 get_object_descriptions(db["object_descriptions"]),
335 get_obituaries(db["obituaries"]),
336 get_hints(db["hints"], db["arbitrary_messages"]),
337 get_condbits(db["locations"]),
340 h = h_template.format(
343 len(db["obituaries"]),
344 len(db["turn_thresholds"]),
345 get_refs(db["arbitrary_messages"]),
346 get_refs(db["locations"]),
347 get_refs(db["object_descriptions"]),
351 with open(h_name, "w") as hf:
354 with open(c_name, "w") as cf: