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 const char* arbitrary_messages[];
100 extern const 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[];
107 #define NLOCATIONS {}
112 #define NTHRESHOLDS {}
114 enum arbitrary_messages_refs {{
118 enum locations_refs {{
122 enum object_descriptions_refs {{
126 /* State definitions */
129 #endif /* end NEWDB_H */
132 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
137 const char* arbitrary_messages[] = {{
141 const class_t classes[] = {{
145 const turn_threshold_t turn_thresholds[] = {{
149 const location_t locations[] = {{
153 const object_description_t object_descriptions[] = {{
157 const obituary_t obituaries[] = {{
161 const hint_t hints[] = {{
165 long conditions[] = {{
169 const vocabulary_t vocabulary[] = {{
176 def make_c_string(string):
177 """Render a Python string into C string literal format."""
180 string = string.replace("\n", "\\n")
181 string = string.replace("\t", "\\t")
182 string = string.replace('"', '\\"')
183 string = string.replace("'", "\\'")
184 string = '"' + string + '"'
188 reflist = [x[0] for x in l]
191 ref_str += " {},\n".format(ref)
192 ref_str = ref_str[:-1] # trim trailing newline
195 def get_arbitrary_messages(arb):
200 arb_str += template.format(make_c_string(item[1]))
201 arb_str = arb_str[:-1] # trim trailing newline
204 def get_class_messages(cls):
212 threshold = item["threshold"]
213 message = make_c_string(item["message"])
214 cls_str += template.format(threshold, message)
215 cls_str = cls_str[:-1] # trim trailing newline
218 def get_turn_thresholds(trn):
227 threshold = item["threshold"]
228 point_loss = item["point_loss"]
229 message = make_c_string(item["message"])
230 trn_str += template.format(threshold, point_loss, message)
231 trn_str = trn_str[:-1] # trim trailing newline
234 def get_locations(loc):
235 template = """ {{ // {}
245 for (i, item) in enumerate(loc):
246 short_d = make_c_string(item[1]["description"]["short"])
247 long_d = make_c_string(item[1]["description"]["long"])
248 sound = item[1].get("sound", "SILENT")
249 loud = "true" if item[1].get("loud") else "false"
250 loc_str += template.format(i, short_d, long_d, sound, loud)
251 loc_str = loc_str[:-1] # trim trailing newline
254 def get_object_descriptions(obj):
255 template = """ {{ // {}
260 .longs = (const char* []) {{
263 .sounds = (const char* []) {{
266 .texts = (const char* []) {{
272 for (i, item) in enumerate(obj):
274 i_msg = make_c_string(attr["inventory"])
276 if attr["longs"] == None:
277 longs_str = " " * 12 + "NULL,"
280 for l_msg in attr["longs"]:
281 if not isinstance(l_msg, str):
284 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
285 longs_str = longs_str[:-1] # trim trailing newline
288 statedefines += "/* States for %s */\n" % item[0]
289 for (i, (label, message)) in enumerate(labels):
290 if len(message) >= 45:
291 message = message[:45] + "..."
292 statedefines += "#define %s\t%d /* %s */\n" % (label, i, message)
295 if attr.get("sounds") == None:
296 sounds_str = " " * 12 + "NULL,"
298 for l_msg in attr["sounds"]:
299 sounds_str += " " * 12 + make_c_string(l_msg) + ",\n"
300 sounds_str = sounds_str[:-1] # trim trailing newline
302 if attr.get("texts") == None:
303 texts_str = " " * 12 + "NULL,"
305 for l_msg in attr["texts"]:
306 texts_str += " " * 12 + make_c_string(l_msg) + ",\n"
307 texts_str = texts_str[:-1] # trim trailing newline
308 locs = attr.get("locations", ["LOC_NOWHERE", "LOC_NOWHERE"])
309 immovable = attr.get("immovable", False)
311 if type(locs) == str:
312 locs = [locnames.index(locs), -1 if immovable else 0]
314 locs = [locnames.index(x) for x in locs]
316 sys.stderr.write("dungeon: unknown object location in %s\n" % locs)
318 treasure = "true" if attr.get("treasure") else "false"
319 obj_str += template.format(i, i_msg, locs[0], locs[1], treasure, longs_str, sounds_str, texts_str)
320 obj_str = obj_str[:-1] # trim trailing newline
323 def get_obituaries(obit):
331 query = make_c_string(o["query"])
332 yes = make_c_string(o["yes_response"])
333 obit_str += template.format(query, yes)
334 obit_str = obit_str[:-1] # trim trailing newline
337 def get_hints(hnt, arb):
349 item = member["hint"]
350 number = item["number"]
351 penalty = item["penalty"]
352 turns = item["turns"]
353 question = make_c_string(item["question"])
354 hint = make_c_string(item["hint"])
355 hnt_str += template.format(number, penalty, turns, question, hint)
356 hnt_str = hnt_str[:-1] # trim trailing newline
359 def get_condbits(locations):
361 for (name, loc) in locations:
362 conditions = loc["conditions"]
363 hints = loc.get("hints") or []
365 for flag in conditions:
367 flaglist.append(flag)
368 line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
369 trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
372 if line.startswith("|"):
376 cnd_str += " " + line + ",\t// " + name + "\n"
379 def recompose(type_word, value):
380 "Compose the internal code for a vocabulary word from its YAML entry"
381 parts = ("motion", "action", "object", "special")
383 return value + 1000 * parts.index(type_word)
385 sys.stderr.write("dungeon: %s is not a known word\n" % word)
388 sys.stderr.write("%s is not a known word classifier" % attrs["type"])
391 def get_vocabulary(vocabulary):
399 for vocab in vocabulary:
400 word = make_c_string(vocab["word"])
401 type_code = recompose(vocab["type"], vocab["value"])
402 value = vocab["value"]
403 voc_str += template.format(word, type_code, value)
404 voc_str = voc_str[:-1] # trim trailing newline
407 if __name__ == "__main__":
408 with open(yaml_name, "r") as f:
411 locnames = [x[0] for x in db["locations"]]
413 c = c_template.format(
415 get_arbitrary_messages(db["arbitrary_messages"]),
416 get_class_messages(db["classes"]),
417 get_turn_thresholds(db["turn_thresholds"]),
418 get_locations(db["locations"]),
419 get_object_descriptions(db["object_descriptions"]),
420 get_obituaries(db["obituaries"]),
421 get_hints(db["hints"], db["arbitrary_messages"]),
422 get_condbits(db["locations"]),
423 get_vocabulary(db["vocabulary"]),
426 h = h_template.format(
427 len(db["locations"])-1,
428 len(db["object_descriptions"])-1,
431 len(db["obituaries"]),
432 len(db["turn_thresholds"]),
433 get_refs(db["arbitrary_messages"]),
434 get_refs(db["locations"]),
435 get_refs(db["object_descriptions"]),
439 with open(h_name, "w") as hf:
442 with open(c_name, "w") as cf: