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;
43 extern location_t locations[];
44 extern object_description_t object_descriptions[];
45 extern const char* arbitrary_messages[];
46 extern const class_t classes[];
47 extern turn_threshold_t turn_thresholds[];
48 extern obituary_t obituaries[];
51 extern int maximum_deaths;
52 extern int turn_threshold_count;
54 enum arbitrary_messages_refs {{
58 enum locations_refs {{
62 enum object_descriptions_refs {{
67 c_template = """#include "{}"
69 const char* arbitrary_messages[] = {{
73 const class_t classes[] = {{
77 turn_threshold_t turn_thresholds[] = {{
81 location_t locations[] = {{
85 object_description_t object_descriptions[] = {{
89 obituary_t obituaries[] = {{
94 int maximum_deaths = {};
95 int turn_threshold_count = {};
98 def make_c_string(string):
99 """Render a Python string into C string literal format."""
102 string = string.replace("\n", "\\n")
103 string = string.replace("\t", "\\t")
104 string = string.replace('"', '\\"')
105 string = string.replace("'", "\\'")
106 string = '"' + string + '"'
110 reflist = [x[0] for x in l]
113 ref_str += " {},\n".format(ref)
114 ref_str = ref_str[:-1] # trim trailing newline
117 def get_arbitrary_messages(arb):
122 arb_str += template.format(make_c_string(item[1]))
123 arb_str = arb_str[:-1] # trim trailing newline
126 def get_class_messages(cls):
134 threshold = item["threshold"]
135 message = make_c_string(item["message"])
136 cls_str += template.format(threshold, message)
137 cls_str = cls_str[:-1] # trim trailing newline
140 def get_turn_thresholds(trn):
149 threshold = item["threshold"]
150 point_loss = item["point_loss"]
151 message = make_c_string(item["message"])
152 trn_str += template.format(threshold, point_loss, message)
153 trn_str = trn_str[:-1] # trim trailing newline
156 def get_locations(loc):
166 short_d = make_c_string(item[1]["description"]["short"])
167 long_d = make_c_string(item[1]["description"]["long"])
168 loc_str += template.format(short_d, long_d)
169 loc_str = loc_str[:-1] # trim trailing newline
172 def get_object_descriptions(obj):
175 .longs = (const char* []) {{
182 i_msg = make_c_string(item[1]["inventory"])
184 if item[1]["longs"] == None:
185 longs_str = " " * 12 + "NULL,"
187 for l_msg in item[1]["longs"]:
188 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
189 longs_str = longs_str[:-1] # trim trailing newline
190 obj_str += template.format(i_msg, longs_str)
191 obj_str = obj_str[:-1] # trim trailing newline
194 def get_obituaries(obit):
202 query = make_c_string(o["query"])
203 yes = make_c_string(o["yes_response"])
204 obit_str += template.format(query, yes)
205 obit_str = obit_str[:-1] # trim trailing newline
208 with open(yaml_name, "r") as f:
211 h = h_template.format(
212 get_refs(db["arbitrary_messages"]),
213 get_refs(db["locations"]),
214 get_refs(db["object_descriptions"]),
217 c = c_template.format(
219 get_arbitrary_messages(db["arbitrary_messages"]),
220 get_class_messages(db["classes"]),
221 get_turn_thresholds(db["turn_thresholds"]),
222 get_locations(db["locations"]),
223 get_object_descriptions(db["object_descriptions"]),
224 get_obituaries(db["obituaries"]),
226 len(db["obituaries"]),
227 len(db["turn_thresholds"]),
230 with open(h_name, "w") as hf:
233 with open(c_name, "w") as cf: