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;
52 }} object_description_t;
60 descriptions_t description;
67 const char* yes_response;
89 extern const location_t locations[];
90 extern const object_description_t object_descriptions[];
91 extern const const char* arbitrary_messages[];
92 extern const const class_t classes[];
93 extern const turn_threshold_t turn_thresholds[];
94 extern const obituary_t obituaries[];
95 extern const hint_t hints[];
96 extern long conditions[];
103 #define NTHRESHOLDS {}
105 enum arbitrary_messages_refs {{
109 enum locations_refs {{
113 enum object_descriptions_refs {{
117 /* State definitions */
120 #endif /* end NEWDB_H */
123 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
128 const char* arbitrary_messages[] = {{
132 const class_t classes[] = {{
136 const turn_threshold_t turn_thresholds[] = {{
140 const location_t locations[] = {{
144 const object_description_t object_descriptions[] = {{
148 const obituary_t obituaries[] = {{
152 const hint_t hints[] = {{
156 long conditions[] = {{
163 def make_c_string(string):
164 """Render a Python string into C string literal format."""
167 string = string.replace("\n", "\\n")
168 string = string.replace("\t", "\\t")
169 string = string.replace('"', '\\"')
170 string = string.replace("'", "\\'")
171 string = '"' + string + '"'
175 reflist = [x[0] for x in l]
178 ref_str += " {},\n".format(ref)
179 ref_str = ref_str[:-1] # trim trailing newline
182 def get_arbitrary_messages(arb):
187 arb_str += template.format(make_c_string(item[1]))
188 arb_str = arb_str[:-1] # trim trailing newline
191 def get_class_messages(cls):
199 threshold = item["threshold"]
200 message = make_c_string(item["message"])
201 cls_str += template.format(threshold, message)
202 cls_str = cls_str[:-1] # trim trailing newline
205 def get_turn_thresholds(trn):
214 threshold = item["threshold"]
215 point_loss = item["point_loss"]
216 message = make_c_string(item["message"])
217 trn_str += template.format(threshold, point_loss, message)
218 trn_str = trn_str[:-1] # trim trailing newline
221 def get_locations(loc):
233 short_d = make_c_string(item[1]["description"]["short"])
234 long_d = make_c_string(item[1]["description"]["long"])
235 sound = item[1].get("sound", "SILENT")
236 loud = "true" if item[1].get("loud") else "false"
237 loc_str += template.format(short_d, long_d, sound, loud)
238 loc_str = loc_str[:-1] # trim trailing newline
241 def get_object_descriptions(obj):
247 .longs = (const char* []) {{
250 .sounds = (const char* []) {{
253 .texts = (const char* []) {{
261 i_msg = make_c_string(attr["inventory"])
263 if attr["longs"] == None:
264 longs_str = " " * 12 + "NULL,"
267 for l_msg in attr["longs"]:
268 if not isinstance(l_msg, str):
271 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
272 longs_str = longs_str[:-1] # trim trailing newline
275 statedefines += "/* States for %s */\n" % item[0]
276 for (i, (label, message)) in enumerate(labels):
277 if len(message) >= 45:
278 message = message[:45] + "..."
279 statedefines += "#define %s\t%d /* %s */\n" % (label, i, message)
282 if attr.get("sounds") == None:
283 sounds_str = " " * 12 + "NULL,"
285 for l_msg in attr["sounds"]:
286 sounds_str += " " * 12 + make_c_string(l_msg) + ",\n"
287 sounds_str = sounds_str[:-1] # trim trailing newline
289 if attr.get("texts") == None:
290 texts_str = " " * 12 + "NULL,"
292 for l_msg in attr["texts"]:
293 texts_str += " " * 12 + make_c_string(l_msg) + ",\n"
294 texts_str = texts_str[:-1] # trim trailing newline
295 locs = attr.get("locations", ["LOC_NOWHERE", "LOC_NOWHERE"])
296 immovable = attr.get("immovable", False)
297 if type(locs) == str:
298 locs = [locnames.index(locs), -1 if immovable else 0]
300 locs = [locnames.index(x) for x in locs]
301 treasure = "true" if attr.get("treasure") else "false"
302 obj_str += template.format(i_msg, locs[0], locs[1], treasure, longs_str, sounds_str, texts_str)
303 obj_str = obj_str[:-1] # trim trailing newline
306 def get_obituaries(obit):
314 query = make_c_string(o["query"])
315 yes = make_c_string(o["yes_response"])
316 obit_str += template.format(query, yes)
317 obit_str = obit_str[:-1] # trim trailing newline
320 def get_hints(hnt, arb):
332 item = member["hint"]
333 number = item["number"]
334 penalty = item["penalty"]
335 turns = item["turns"]
336 question = make_c_string(md[item["question"]])
337 hint = make_c_string(md[item["hint"]])
338 hnt_str += template.format(number, penalty, turns, question, hint)
339 hnt_str = hnt_str[:-1] # trim trailing newline
342 def get_condbits(locations):
344 for (name, loc) in locations:
345 conditions = loc["conditions"]
346 hints = loc.get("hints") or []
348 for flag in conditions:
350 flaglist.append(flag)
351 line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
352 trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
355 if line.startswith("|"):
359 cnd_str += " " + line + ",\t// " + name + "\n"
362 if __name__ == "__main__":
363 with open(yaml_name, "r") as f:
366 locnames = [x[0] for x in db["locations"]]
368 c = c_template.format(
370 get_arbitrary_messages(db["arbitrary_messages"]),
371 get_class_messages(db["classes"]),
372 get_turn_thresholds(db["turn_thresholds"]),
373 get_locations(db["locations"]),
374 get_object_descriptions(db["object_descriptions"]),
375 get_obituaries(db["obituaries"]),
376 get_hints(db["hints"], db["arbitrary_messages"]),
377 get_condbits(db["locations"]),
380 h = h_template.format(
381 len(db["locations"]),
382 len(db["object_descriptions"]),
385 len(db["obituaries"]),
386 len(db["turn_thresholds"]),
387 get_refs(db["arbitrary_messages"]),
388 get_refs(db["locations"]),
389 get_refs(db["object_descriptions"]),
393 with open(h_name, "w") as hf:
396 with open(c_name, "w") as cf: