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.
8 yaml_name = "adventure.yaml"
13 """Add C escape sequences to a string."""
14 string = string.replace("\n", "\\n")
15 string = string.replace("\t", "\\t")
16 string = string.replace('"', '\\"')
17 string = string.replace("'", "\\'")
20 def write_regular_messages(name, h, c):
22 if name != "short_location_descriptions":
23 h += "enum {}_refs {{\n".format(name)
24 for key, text in dungeon[name]:
25 h += " {},\n".format(key)
28 c += "char* {}[] = {{\n".format(name)
30 for key, text in dungeon[name]:
35 c += " \"{}\",\n".format(text)
41 with open(yaml_name, "r") as f:
42 dungeon = yaml.load(f)
44 h = """#include <stdio.h>
49 } object_description_t;
51 extern char* long_location_descriptions[];
52 extern char* short_location_descriptions[];
53 extern object_description_t object_descriptions[];
54 extern char* arbitrary_messages[];
55 extern char* class_messages[];
56 extern char* turn_threshold_messages[];
68 "long_location_descriptions",
69 "short_location_descriptions",
71 "turn_threshold_messages",
73 h, c = write_regular_messages(name, h, c)
75 h += "enum object_descriptions_refs {\n"
76 c += "object_description_t object_descriptions[] = {\n"
77 for key, data in dungeon["object_descriptions"]:
79 data["inventory"] = "\"{}\"".format(c_escape(data["inventory"]))
80 except AttributeError:
81 data["inventory"] = "NULL"
82 h += " {},\n".format(key)
84 c += " .inventory = {},\n".format(data["inventory"])
87 c += " .longs = (char* []) {\n"
88 for l in data["longs"]:
90 c += " \"{}\",\n".format(l)
92 except (TypeError, IndexError):
93 c += " .longs = NULL,\n"
100 """.format(len(dungeon["class_messages"]))
102 # finally, write out the files
107 for filename, string in d.items():
108 with open(filename, "w") as f: