3 # This is the new open-adventure dungeon generator. It'll eventually
4 # replace the existing dungeon.c It currently outputs a .h and .c pair
9 yaml_name = "adventure.yaml"
15 h_template = """/* Generated from adventure.yaml - do not hand-hack! */
22 #define SILENT -1 /* no sound */
24 /* Symbols for cond bits */
25 #define COND_LIT 0 /* Light */
26 #define COND_OILY 1 /* If bit 2 is on: on for oil, off for water */
27 #define COND_FLUID 2 /* Liquid asset, see bit 1 */
28 #define COND_NOARRR 3 /* Pirate doesn't go here unless following */
29 #define COND_NOBACK 4 /* Cannot use "back" to move away */
31 #define COND_DEEP 6 /* Deep - e.g where dwarves are active */
32 #define COND_FOREST 7 /* In the forest */
33 #define COND_FORCED 8 /* Only one way in or out of here */
34 /* Bits past 10 indicate areas of interest to "hint" routines */
35 #define COND_HBASE 10 /* Base for location hint bits */
36 #define COND_HCAVE 11 /* Trying to get into cave */
37 #define COND_HBIRD 12 /* Trying to catch bird */
38 #define COND_HSNAKE 13 /* Trying to deal with snake */
39 #define COND_HMAZE 14 /* Lost in maze */
40 #define COND_HDARK 15 /* Pondering dark room */
41 #define COND_HWITT 16 /* At Witt's End */
42 #define COND_HCLIFF 17 /* Cliff with urn */
43 #define COND_HWOODS 18 /* Lost in forest */
44 #define COND_HOGRE 19 /* Trying to deal with ogre */
45 #define COND_HJADE 20 /* Found all treasures except jade */
48 const char* inventory;
54 }} object_description_t;
62 descriptions_t description;
69 const char* yes_response;
97 extern const location_t locations[];
98 extern const object_description_t object_descriptions[];
99 extern const char* arbitrary_messages[];
100 extern const class_t classes[];
101 extern const turn_threshold_t turn_thresholds[];
102 extern const obituary_t obituaries[];
103 extern const hint_t hints[];
104 extern long conditions[];
105 extern const vocabulary_t vocabulary[];
106 extern const long actspk[];
108 #define NLOCATIONS {}
113 #define NTHRESHOLDS {}
116 enum arbitrary_messages_refs {{
120 enum locations_refs {{
124 enum object_descriptions_refs {{
128 /* State definitions */
131 #endif /* end NEWDB_H */
134 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
139 const char* arbitrary_messages[] = {{
143 const class_t classes[] = {{
147 const turn_threshold_t turn_thresholds[] = {{
151 const location_t locations[] = {{
155 const object_description_t object_descriptions[] = {{
159 const obituary_t obituaries[] = {{
163 const hint_t hints[] = {{
167 long conditions[] = {{
171 const vocabulary_t vocabulary[] = {{
175 const long actspk[] = {{
183 def make_c_string(string):
184 """Render a Python string into C string literal format."""
187 string = string.replace("\n", "\\n")
188 string = string.replace("\t", "\\t")
189 string = string.replace('"', '\\"')
190 string = string.replace("'", "\\'")
191 string = '"' + string + '"'
195 reflist = [x[0] for x in l]
198 ref_str += " {},\n".format(ref)
199 ref_str = ref_str[:-1] # trim trailing newline
202 def get_arbitrary_messages(arb):
207 arb_str += template.format(make_c_string(item[1]))
208 arb_str = arb_str[:-1] # trim trailing newline
211 def get_class_messages(cls):
219 threshold = item["threshold"]
220 message = make_c_string(item["message"])
221 cls_str += template.format(threshold, message)
222 cls_str = cls_str[:-1] # trim trailing newline
225 def get_turn_thresholds(trn):
234 threshold = item["threshold"]
235 point_loss = item["point_loss"]
236 message = make_c_string(item["message"])
237 trn_str += template.format(threshold, point_loss, message)
238 trn_str = trn_str[:-1] # trim trailing newline
241 def get_locations(loc):
242 template = """ {{ // {}
252 for (i, item) in enumerate(loc):
253 short_d = make_c_string(item[1]["description"]["short"])
254 long_d = make_c_string(item[1]["description"]["long"])
255 sound = item[1].get("sound", "SILENT")
256 loud = "true" if item[1].get("loud") else "false"
257 loc_str += template.format(i, short_d, long_d, sound, loud)
258 loc_str = loc_str[:-1] # trim trailing newline
261 def get_object_descriptions(obj):
262 template = """ {{ // {}
267 .longs = (const char* []) {{
270 .sounds = (const char* []) {{
273 .texts = (const char* []) {{
279 for (i, item) in enumerate(obj):
281 i_msg = make_c_string(attr["inventory"])
283 if attr["longs"] == None:
284 longs_str = " " * 12 + "NULL,"
287 for l_msg in attr["longs"]:
288 if not isinstance(l_msg, str):
291 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
292 longs_str = longs_str[:-1] # trim trailing newline
295 statedefines += "/* States for %s */\n" % item[0]
296 for (i, (label, message)) in enumerate(labels):
297 if len(message) >= 45:
298 message = message[:45] + "..."
299 statedefines += "#define %s\t%d /* %s */\n" % (label, i, message)
302 if attr.get("sounds") == None:
303 sounds_str = " " * 12 + "NULL,"
305 for l_msg in attr["sounds"]:
306 sounds_str += " " * 12 + make_c_string(l_msg) + ",\n"
307 sounds_str = sounds_str[:-1] # trim trailing newline
309 if attr.get("texts") == None:
310 texts_str = " " * 12 + "NULL,"
312 for l_msg in attr["texts"]:
313 texts_str += " " * 12 + make_c_string(l_msg) + ",\n"
314 texts_str = texts_str[:-1] # trim trailing newline
315 locs = attr.get("locations", ["LOC_NOWHERE", "LOC_NOWHERE"])
316 immovable = attr.get("immovable", False)
318 if type(locs) == str:
319 locs = [locnames.index(locs), -1 if immovable else 0]
321 locs = [locnames.index(x) for x in locs]
323 sys.stderr.write("dungeon: unknown object location in %s\n" % locs)
325 treasure = "true" if attr.get("treasure") else "false"
326 obj_str += template.format(i, i_msg, locs[0], locs[1], treasure, longs_str, sounds_str, texts_str)
327 obj_str = obj_str[:-1] # trim trailing newline
330 def get_obituaries(obit):
338 query = make_c_string(o["query"])
339 yes = make_c_string(o["yes_response"])
340 obit_str += template.format(query, yes)
341 obit_str = obit_str[:-1] # trim trailing newline
344 def get_hints(hnt, arb):
356 item = member["hint"]
357 number = item["number"]
358 penalty = item["penalty"]
359 turns = item["turns"]
360 question = make_c_string(item["question"])
361 hint = make_c_string(item["hint"])
362 hnt_str += template.format(number, penalty, turns, question, hint)
363 hnt_str = hnt_str[:-1] # trim trailing newline
366 def get_condbits(locations):
368 for (name, loc) in locations:
369 conditions = loc["conditions"]
370 hints = loc.get("hints") or []
372 for flag in conditions:
374 flaglist.append(flag)
375 line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
376 trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
379 if line.startswith("|"):
383 cnd_str += " " + line + ",\t// " + name + "\n"
386 def recompose(type_word, value):
387 "Compose the internal code for a vocabulary word from its YAML entry"
388 parts = ("motion", "action", "object", "special")
390 return value + 1000 * parts.index(type_word)
392 sys.stderr.write("dungeon: %s is not a known word\n" % word)
395 sys.stderr.write("%s is not a known word classifier" % attrs["type"])
398 def get_vocabulary(vocabulary):
406 for vocab in vocabulary:
407 word = make_c_string(vocab["word"])
408 type_code = recompose(vocab["type"], vocab["value"])
409 value = vocab["value"]
410 voc_str += template.format(word, type_code, value)
411 voc_str = voc_str[:-1] # trim trailing newline
414 def get_actspk(actspk):
416 for (i, word) in actspk.items():
417 res += " %s,\n" % word
420 if __name__ == "__main__":
421 with open(yaml_name, "r") as f:
424 locnames = [x[0] for x in db["locations"]]
426 c = c_template.format(
428 get_arbitrary_messages(db["arbitrary_messages"]),
429 get_class_messages(db["classes"]),
430 get_turn_thresholds(db["turn_thresholds"]),
431 get_locations(db["locations"]),
432 get_object_descriptions(db["object_descriptions"]),
433 get_obituaries(db["obituaries"]),
434 get_hints(db["hints"], db["arbitrary_messages"]),
435 get_condbits(db["locations"]),
436 get_vocabulary(db["vocabulary"]),
437 get_actspk(db["actspk"]),
440 h = h_template.format(
441 len(db["locations"])-1,
442 len(db["object_descriptions"])-1,
445 len(db["obituaries"]),
446 len(db["turn_thresholds"]),
448 get_refs(db["arbitrary_messages"]),
449 get_refs(db["locations"]),
450 get_refs(db["object_descriptions"]),
454 with open(h_name, "w") as hf:
457 with open(c_name, "w") as cf: