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;
62 descriptions_t description;
69 const char* yes_response;
97 extern const location_t locations[];
98 extern const object_t objects[];
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 {}
117 enum arbitrary_messages_refs {{
121 enum locations_refs {{
129 /* State definitions */
132 #endif /* end NEWDB_H */
135 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
140 const char* arbitrary_messages[] = {{
144 const class_t classes[] = {{
148 const turn_threshold_t turn_thresholds[] = {{
152 const location_t locations[] = {{
156 const object_t objects[] = {{
160 const obituary_t obituaries[] = {{
164 const hint_t hints[] = {{
168 long conditions[] = {{
172 const vocabulary_t vocabulary[] = {{
176 const long actspk[] = {{
184 def make_c_string(string):
185 """Render a Python string into C string literal format."""
188 string = string.replace("\n", "\\n")
189 string = string.replace("\t", "\\t")
190 string = string.replace('"', '\\"')
191 string = string.replace("'", "\\'")
192 string = '"' + string + '"'
196 reflist = [x[0] for x in l]
199 ref_str += " {},\n".format(ref)
200 ref_str = ref_str[:-1] # trim trailing newline
203 def get_arbitrary_messages(arb):
208 arb_str += template.format(make_c_string(item[1]))
209 arb_str = arb_str[:-1] # trim trailing newline
212 def get_class_messages(cls):
220 threshold = item["threshold"]
221 message = make_c_string(item["message"])
222 cls_str += template.format(threshold, message)
223 cls_str = cls_str[:-1] # trim trailing newline
226 def get_turn_thresholds(trn):
235 threshold = item["threshold"]
236 point_loss = item["point_loss"]
237 message = make_c_string(item["message"])
238 trn_str += template.format(threshold, point_loss, message)
239 trn_str = trn_str[:-1] # trim trailing newline
242 def get_locations(loc):
243 template = """ {{ // {}
253 for (i, item) in enumerate(loc):
254 short_d = make_c_string(item[1]["description"]["short"])
255 long_d = make_c_string(item[1]["description"]["long"])
256 sound = item[1].get("sound", "SILENT")
257 loud = "true" if item[1].get("loud") else "false"
258 loc_str += template.format(i, short_d, long_d, sound, loud)
259 loc_str = loc_str[:-1] # trim trailing newline
262 def get_objects(obj):
263 template = """ {{ // {}
268 .longs = (const char* []) {{
271 .sounds = (const char* []) {{
274 .texts = (const char* []) {{
280 for (i, item) in enumerate(obj):
282 i_msg = make_c_string(attr["inventory"])
284 if attr["longs"] == None:
285 longs_str = " " * 12 + "NULL,"
288 for l_msg in attr["longs"]:
289 if not isinstance(l_msg, str):
292 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
293 longs_str = longs_str[:-1] # trim trailing newline
296 statedefines += "/* States for %s */\n" % item[0]
297 for (i, (label, message)) in enumerate(labels):
298 if len(message) >= 45:
299 message = message[:45] + "..."
300 statedefines += "#define %s\t%d /* %s */\n" % (label, i, message)
303 if attr.get("sounds") == None:
304 sounds_str = " " * 12 + "NULL,"
306 for l_msg in attr["sounds"]:
307 sounds_str += " " * 12 + make_c_string(l_msg) + ",\n"
308 sounds_str = sounds_str[:-1] # trim trailing newline
310 if attr.get("texts") == None:
311 texts_str = " " * 12 + "NULL,"
313 for l_msg in attr["texts"]:
314 texts_str += " " * 12 + make_c_string(l_msg) + ",\n"
315 texts_str = texts_str[:-1] # trim trailing newline
316 locs = attr.get("locations", ["LOC_NOWHERE", "LOC_NOWHERE"])
317 immovable = attr.get("immovable", False)
319 if type(locs) == str:
320 locs = [locnames.index(locs), -1 if immovable else 0]
322 locs = [locnames.index(x) for x in locs]
324 sys.stderr.write("dungeon: unknown object location in %s\n" % locs)
326 treasure = "true" if attr.get("treasure") else "false"
327 obj_str += template.format(i, i_msg, locs[0], locs[1], treasure, longs_str, sounds_str, texts_str)
328 obj_str = obj_str[:-1] # trim trailing newline
331 def get_obituaries(obit):
339 query = make_c_string(o["query"])
340 yes = make_c_string(o["yes_response"])
341 obit_str += template.format(query, yes)
342 obit_str = obit_str[:-1] # trim trailing newline
345 def get_hints(hnt, arb):
357 item = member["hint"]
358 number = item["number"]
359 penalty = item["penalty"]
360 turns = item["turns"]
361 question = make_c_string(item["question"])
362 hint = make_c_string(item["hint"])
363 hnt_str += template.format(number, penalty, turns, question, hint)
364 hnt_str = hnt_str[:-1] # trim trailing newline
367 def get_condbits(locations):
369 for (name, loc) in locations:
370 conditions = loc["conditions"]
371 hints = loc.get("hints") or []
373 for flag in conditions:
375 flaglist.append(flag)
376 line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
377 trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
380 if line.startswith("|"):
384 cnd_str += " " + line + ",\t// " + name + "\n"
387 def recompose(type_word, value):
388 "Compose the internal code for a vocabulary word from its YAML entry"
389 parts = ("motion", "action", "object", "special")
391 return value + 1000 * parts.index(type_word)
393 sys.stderr.write("dungeon: %s is not a known word\n" % word)
396 sys.stderr.write("%s is not a known word classifier" % attrs["type"])
399 def get_vocabulary(vocabulary):
407 for vocab in vocabulary:
408 word = make_c_string(vocab["word"])
409 type_code = recompose(vocab["type"], vocab["value"])
410 value = vocab["value"]
411 voc_str += template.format(word, type_code, value)
412 voc_str = voc_str[:-1] # trim trailing newline
415 def get_actspk(actspk):
417 for (i, word) in actspk.items():
418 res += " %s,\n" % word
421 if __name__ == "__main__":
422 with open(yaml_name, "r") as f:
425 locnames = [x[0] for x in db["locations"]]
427 c = c_template.format(
429 get_arbitrary_messages(db["arbitrary_messages"]),
430 get_class_messages(db["classes"]),
431 get_turn_thresholds(db["turn_thresholds"]),
432 get_locations(db["locations"]),
433 get_objects(db["objects"]),
434 get_obituaries(db["obituaries"]),
435 get_hints(db["hints"], db["arbitrary_messages"]),
436 get_condbits(db["locations"]),
437 get_vocabulary(db["vocabulary"]),
438 get_actspk(db["actspk"]),
441 h = h_template.format(
442 len(db["locations"])-1,
443 len(db["objects"])-1,
445 len(db["classes"])-1,
446 len(db["obituaries"]),
447 len(db["turn_thresholds"]),
449 len(db["vocabulary"]),
450 get_refs(db["arbitrary_messages"]),
451 get_refs(db["locations"]),
452 get_refs(db["objects"]),
456 with open(h_name, "w") as hf:
459 with open(c_name, "w") as cf: