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 = """/* Generated from adventure.yaml - do not hand-hack! */
18 #define SILENT -1 /* no sound */
21 const char* inventory;
23 }} object_description_t;
31 descriptions_t description;
38 const char* yes_response;
60 extern location_t locations[];
61 extern object_description_t object_descriptions[];
62 extern const char* arbitrary_messages[];
63 extern const class_t classes[];
64 extern turn_threshold_t turn_thresholds[];
65 extern obituary_t obituaries[];
66 extern hint_t hints[];
67 extern long conditions[];
69 extern const size_t CLSSES;
70 extern const int maximum_deaths;
71 extern const int turn_threshold_count;
74 enum arbitrary_messages_refs {{
78 enum locations_refs {{
82 enum object_descriptions_refs {{
86 #endif /* end NEWDB_H */
89 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
94 const char* arbitrary_messages[] = {{
98 const class_t classes[] = {{
102 turn_threshold_t turn_thresholds[] = {{
106 location_t locations[] = {{
110 object_description_t object_descriptions[] = {{
114 obituary_t obituaries[] = {{
122 long conditions[] = {{
126 const size_t CLSSES = {};
127 const int maximum_deaths = {};
128 const int turn_threshold_count = {};
133 def make_c_string(string):
134 """Render a Python string into C string literal format."""
137 string = string.replace("\n", "\\n")
138 string = string.replace("\t", "\\t")
139 string = string.replace('"', '\\"')
140 string = string.replace("'", "\\'")
141 string = '"' + string + '"'
145 reflist = [x[0] for x in l]
148 ref_str += " {},\n".format(ref)
149 ref_str = ref_str[:-1] # trim trailing newline
152 def get_arbitrary_messages(arb):
157 arb_str += template.format(make_c_string(item[1]))
158 arb_str = arb_str[:-1] # trim trailing newline
161 def get_class_messages(cls):
169 threshold = item["threshold"]
170 message = make_c_string(item["message"])
171 cls_str += template.format(threshold, message)
172 cls_str = cls_str[:-1] # trim trailing newline
175 def get_turn_thresholds(trn):
184 threshold = item["threshold"]
185 point_loss = item["point_loss"]
186 message = make_c_string(item["message"])
187 trn_str += template.format(threshold, point_loss, message)
188 trn_str = trn_str[:-1] # trim trailing newline
191 def get_locations(loc):
203 short_d = make_c_string(item[1]["description"]["short"])
204 long_d = make_c_string(item[1]["description"]["long"])
205 sound = item[1].get("sound", "SILENT")
206 loud = "true" if item[1].get("loud") else "false"
207 loc_str += template.format(short_d, long_d, sound, loud)
208 loc_str = loc_str[:-1] # trim trailing newline
211 def get_object_descriptions(obj):
214 .longs = (const char* []) {{
221 i_msg = make_c_string(item[1]["inventory"])
223 if item[1]["longs"] == None:
224 longs_str = " " * 12 + "NULL,"
226 for l_msg in item[1]["longs"]:
227 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
228 longs_str = longs_str[:-1] # trim trailing newline
229 obj_str += template.format(i_msg, longs_str)
230 obj_str = obj_str[:-1] # trim trailing newline
233 def get_obituaries(obit):
241 query = make_c_string(o["query"])
242 yes = make_c_string(o["yes_response"])
243 obit_str += template.format(query, yes)
244 obit_str = obit_str[:-1] # trim trailing newline
247 def get_hints(hnt, arb):
259 item = member["hint"]
260 number = item["number"]
261 penalty = item["penalty"]
262 turns = item["turns"]
263 question = make_c_string(md[item["question"]])
264 hint = make_c_string(md[item["hint"]])
265 hnt_str += template.format(number, penalty, turns, question, hint)
266 hnt_str = hnt_str[:-1] # trim trailing newline
269 def get_condbits(locations):
271 for (name, loc) in locations:
272 conditions = loc["conditions"]
273 hints = loc.get("hints") or []
275 for flag in conditions:
277 flaglist.append(flag)
278 line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
279 trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
282 if line.startswith("|"):
286 cnd_str += " " + line + ",\t// " + name + "\n"
289 if __name__ == "__main__":
290 with open(yaml_name, "r") as f:
293 h = h_template.format(
295 get_refs(db["arbitrary_messages"]),
296 get_refs(db["locations"]),
297 get_refs(db["object_descriptions"]),
300 c = c_template.format(
302 get_arbitrary_messages(db["arbitrary_messages"]),
303 get_class_messages(db["classes"]),
304 get_turn_thresholds(db["turn_thresholds"]),
305 get_locations(db["locations"]),
306 get_object_descriptions(db["object_descriptions"]),
307 get_obituaries(db["obituaries"]),
308 get_hints(db["hints"], db["arbitrary_messages"]),
309 get_condbits(db["locations"]),
311 len(db["obituaries"]),
312 len(db["turn_thresholds"]),
315 with open(h_name, "w") as hf:
318 with open(c_name, "w") as cf: