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 */
22 /* Symbols for cond bits */
23 #define COND_LIT 0 /* Light */
24 #define COND_OILY 1 /* If bit 2 is on: on for oil, off for water */
25 #define COND_FLUID 2 /* Liquid asset, see bit 1 */
26 #define COND_NOARRR 3 /* Pirate doesn't go here unless following */
27 #define COND_NOBACK 4 /* Cannot use "back" to move away */
29 #define COND_DEEP 6 /* Deep - e.g where dwarves are active */
30 #define COND_FOREST 7 /* In the forest */
31 #define COND_FORCED 8 /* Only one way in or out of here */
32 /* Bits past 10 indicate areas of interest to "hint" routines */
33 #define COND_HBASE 10 /* Base for location hint bits */
34 #define COND_HCAVE 11 /* Trying to get into cave */
35 #define COND_HBIRD 12 /* Trying to catch bird */
36 #define COND_HSNAKE 13 /* Trying to deal with snake */
37 #define COND_HMAZE 14 /* Lost in maze */
38 #define COND_HDARK 15 /* Pondering dark room */
39 #define COND_HWITT 16 /* At Witt's End */
40 #define COND_HCLIFF 17 /* Cliff with urn */
41 #define COND_HWOODS 18 /* Lost in forest */
42 #define COND_HOGRE 19 /* Trying to deal with ogre */
43 #define COND_HJADE 20 /* Found all treasures except jade */
46 const char* inventory;
51 }} object_description_t;
59 descriptions_t description;
66 const char* yes_response;
88 extern const location_t locations[];
89 extern const object_description_t object_descriptions[];
90 extern const const char* arbitrary_messages[];
91 extern const const class_t classes[];
92 extern const turn_threshold_t turn_thresholds[];
93 extern const obituary_t obituaries[];
94 extern const hint_t hints[];
95 extern long conditions[];
102 #define NTHRESHOLDS {}
104 enum arbitrary_messages_refs {{
108 enum locations_refs {{
112 enum object_descriptions_refs {{
116 /* State definitions */
119 #endif /* end NEWDB_H */
122 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
127 const char* arbitrary_messages[] = {{
131 const class_t classes[] = {{
135 const turn_threshold_t turn_thresholds[] = {{
139 const location_t locations[] = {{
143 const object_description_t object_descriptions[] = {{
147 const obituary_t obituaries[] = {{
151 const hint_t hints[] = {{
155 long conditions[] = {{
162 def make_c_string(string):
163 """Render a Python string into C string literal format."""
166 string = string.replace("\n", "\\n")
167 string = string.replace("\t", "\\t")
168 string = string.replace('"', '\\"')
169 string = string.replace("'", "\\'")
170 string = '"' + string + '"'
174 reflist = [x[0] for x in l]
177 ref_str += " {},\n".format(ref)
178 ref_str = ref_str[:-1] # trim trailing newline
181 def get_arbitrary_messages(arb):
186 arb_str += template.format(make_c_string(item[1]))
187 arb_str = arb_str[:-1] # trim trailing newline
190 def get_class_messages(cls):
198 threshold = item["threshold"]
199 message = make_c_string(item["message"])
200 cls_str += template.format(threshold, message)
201 cls_str = cls_str[:-1] # trim trailing newline
204 def get_turn_thresholds(trn):
213 threshold = item["threshold"]
214 point_loss = item["point_loss"]
215 message = make_c_string(item["message"])
216 trn_str += template.format(threshold, point_loss, message)
217 trn_str = trn_str[:-1] # trim trailing newline
220 def get_locations(loc):
232 short_d = make_c_string(item[1]["description"]["short"])
233 long_d = make_c_string(item[1]["description"]["long"])
234 sound = item[1].get("sound", "SILENT")
235 loud = "true" if item[1].get("loud") else "false"
236 loc_str += template.format(short_d, long_d, sound, loud)
237 loc_str = loc_str[:-1] # trim trailing newline
240 def get_object_descriptions(obj):
244 .longs = (const char* []) {{
247 .sounds = (const char* []) {{
250 .texts = (const char* []) {{
257 i_msg = make_c_string(item[1]["inventory"])
259 if item[1]["longs"] == None:
260 longs_str = " " * 12 + "NULL,"
263 for l_msg in item[1]["longs"]:
264 if not isinstance(l_msg, str):
267 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
268 longs_str = longs_str[:-1] # trim trailing newline
271 statedefines += "/* States for %s */\n" % item[0]
272 for (i, (label, message)) in enumerate(labels):
273 if len(message) >= 45:
274 message = message[:45] + "..."
275 statedefines += "#define %s\t%d /* %s */\n" % (label, i, message)
278 if item[1].get("sounds") == None:
279 sounds_str = " " * 12 + "NULL,"
281 for l_msg in item[1]["sounds"]:
282 sounds_str += " " * 12 + make_c_string(l_msg) + ",\n"
283 sounds_str = sounds_str[:-1] # trim trailing newline
285 if item[1].get("texts") == None:
286 texts_str = " " * 12 + "NULL,"
288 for l_msg in item[1]["texts"]:
289 texts_str += " " * 12 + make_c_string(l_msg) + ",\n"
290 texts_str = texts_str[:-1] # trim trailing newline
291 treasure = "true" if item[1].get("treasure") else "false"
292 obj_str += template.format(i_msg, treasure, longs_str, sounds_str, texts_str)
293 obj_str = obj_str[:-1] # trim trailing newline
296 def get_obituaries(obit):
304 query = make_c_string(o["query"])
305 yes = make_c_string(o["yes_response"])
306 obit_str += template.format(query, yes)
307 obit_str = obit_str[:-1] # trim trailing newline
310 def get_hints(hnt, arb):
322 item = member["hint"]
323 number = item["number"]
324 penalty = item["penalty"]
325 turns = item["turns"]
326 question = make_c_string(md[item["question"]])
327 hint = make_c_string(md[item["hint"]])
328 hnt_str += template.format(number, penalty, turns, question, hint)
329 hnt_str = hnt_str[:-1] # trim trailing newline
332 def get_condbits(locations):
334 for (name, loc) in locations:
335 conditions = loc["conditions"]
336 hints = loc.get("hints") or []
338 for flag in conditions:
340 flaglist.append(flag)
341 line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
342 trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
345 if line.startswith("|"):
349 cnd_str += " " + line + ",\t// " + name + "\n"
352 if __name__ == "__main__":
353 with open(yaml_name, "r") as f:
356 c = c_template.format(
358 get_arbitrary_messages(db["arbitrary_messages"]),
359 get_class_messages(db["classes"]),
360 get_turn_thresholds(db["turn_thresholds"]),
361 get_locations(db["locations"]),
362 get_object_descriptions(db["object_descriptions"]),
363 get_obituaries(db["obituaries"]),
364 get_hints(db["hints"], db["arbitrary_messages"]),
365 get_condbits(db["locations"]),
368 h = h_template.format(
369 len(db["locations"]),
370 len(db["object_descriptions"]),
373 len(db["obituaries"]),
374 len(db["turn_thresholds"]),
375 get_refs(db["arbitrary_messages"]),
376 get_refs(db["locations"]),
377 get_refs(db["object_descriptions"]),
381 with open(h_name, "w") as hf:
384 with open(c_name, "w") as cf: