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"
11 h_template = """#include <stdio.h>
14 const char* inventory;
16 }} object_description_t;
24 descriptions_t description;
29 const char* yes_response;
38 extern location_t locations[];
39 extern object_description_t object_descriptions[];
40 extern const char* arbitrary_messages[];
41 extern const char* class_messages[];
42 extern turn_threshold_t turn_thresholds[];
43 extern obituary_t obituaries[];
46 extern int maximum_deaths;
47 extern int turn_threshold_count;
49 enum arbitrary_messages_refs {{
53 enum class_messages_refs {{
57 enum locations_refs {{
61 enum object_descriptions_refs {{
66 c_template = """#include "{}"
68 const char* arbitrary_messages[] = {{
72 const char* class_messages[] = {{
76 turn_threshold_t turn_thresholds[] = {{
80 location_t locations[] = {{
84 object_description_t object_descriptions[] = {{
88 obituary_t obituaries[] = {{
93 int maximum_deaths = {};
94 int turn_threshold_count = {};
97 def make_c_string(string):
98 """Render a Python string into C string literal format."""
101 string = string.replace("\n", "\\n")
102 string = string.replace("\t", "\\t")
103 string = string.replace('"', '\\"')
104 string = string.replace("'", "\\'")
105 string = '"' + string + '"'
109 reflist = [x[0] for x in l]
112 ref_str += " {},\n".format(ref)
113 ref_str = ref_str[:-1] # trim trailing newline
116 def get_arbitrary_messages(arb):
121 arb_str += template.format(make_c_string(item[1]))
122 arb_str = arb_str[:-1] # trim trailing newline
125 def get_class_messages(cls):
130 cls_str += template.format(make_c_string(item[1]))
131 cls_str = cls_str[:-1] # trim trailing newline
134 def get_turn_thresholds(trn):
143 threshold = item["threshold"]
144 point_loss = item["point_loss"]
145 message = make_c_string(item["message"])
146 trn_str += template.format(threshold, point_loss, message)
147 trn_str = trn_str[:-1] # trim trailing newline
150 def get_locations(loc):
160 short_d = make_c_string(item[1]["description"]["short"])
161 long_d = make_c_string(item[1]["description"]["long"])
162 loc_str += template.format(short_d, long_d)
163 loc_str = loc_str[:-1] # trim trailing newline
166 def get_object_descriptions(obj):
169 .longs = (const char* []) {{
176 i_msg = make_c_string(item[1]["inventory"])
178 if item[1]["longs"] == None:
179 longs_str = " " * 12 + "NULL,"
181 for l_msg in item[1]["longs"]:
182 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
183 longs_str = longs_str[:-1] # trim trailing newline
184 obj_str += template.format(i_msg, longs_str)
185 obj_str = obj_str[:-1] # trim trailing newline
188 def get_obituaries(obit):
196 query = make_c_string(o["query"])
197 yes = make_c_string(o["yes_response"])
198 obit_str += template.format(query, yes)
199 obit_str = obit_str[:-1] # trim trailing newline
202 with open(yaml_name, "r") as f:
205 h = h_template.format(
206 get_refs(db["arbitrary_messages"]),
207 get_refs(db["class_messages"]),
208 get_refs(db["locations"]),
209 get_refs(db["object_descriptions"]),
212 c = c_template.format(
214 get_arbitrary_messages(db["arbitrary_messages"]),
215 get_class_messages(db["class_messages"]),
216 get_turn_thresholds(db["turn_thresholds"]),
217 get_locations(db["locations"]),
218 get_object_descriptions(db["object_descriptions"]),
219 get_obituaries(db["obituaries"]),
220 len(db["class_messages"]),
221 len(db["obituaries"]),
222 len(db["turn_thresholds"]),
225 with open(h_name, "w") as hf:
228 with open(c_name, "w") as cf: