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;
32 extern location_t locations[];
33 extern object_description_t object_descriptions[];
34 extern const char* arbitrary_messages[];
35 extern const char* class_messages[];
36 extern const char* turn_threshold_messages[];
37 extern obituary_t obituaries[];
40 extern int maximum_deaths;
42 enum arbitrary_messages_refs {{
46 enum class_messages_refs {{
50 enum turn_threshold_messages_refs {{
54 enum locations_refs {{
58 enum object_descriptions_refs {{
63 c_template = """#include "{}"
65 const char* arbitrary_messages[] = {{
69 const char* class_messages[] = {{
73 const char* turn_threshold_messages[] = {{
77 location_t locations[] = {{
81 object_description_t object_descriptions[] = {{
85 obituary_t obituaries[] = {{
90 int maximum_deaths = {};
93 def make_c_string(string):
94 """Render a Python string into C string literal format."""
97 string = string.replace("\n", "\\n")
98 string = string.replace("\t", "\\t")
99 string = string.replace('"', '\\"')
100 string = string.replace("'", "\\'")
101 string = '"' + string + '"'
105 reflist = [x[0] for x in l]
108 ref_str += " {},\n".format(ref)
109 ref_str = ref_str[:-1] # trim trailing newline
112 def get_arbitrary_messages(arb):
117 arb_str += template.format(make_c_string(item[1]))
118 arb_str = arb_str[:-1] # trim trailing newline
121 def get_class_messages(cls):
126 cls_str += template.format(make_c_string(item[1]))
127 cls_str = cls_str[:-1] # trim trailing newline
130 def get_turn_threshold_messages(trn):
135 trn_str += template.format(make_c_string(item[1]))
136 trn_str = trn_str[:-1] # trim trailing newline
139 def get_locations(loc):
149 short_d = make_c_string(item[1]["description"]["short"])
150 long_d = make_c_string(item[1]["description"]["long"])
151 loc_str += template.format(short_d, long_d)
152 loc_str = loc_str[:-1] # trim trailing newline
155 def get_object_descriptions(obj):
158 .longs = (const char* []) {{
165 i_msg = make_c_string(item[1]["inventory"])
167 if item[1]["longs"] == None:
168 longs_str = " " * 12 + "NULL,"
170 for l_msg in item[1]["longs"]:
171 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
172 longs_str = longs_str[:-1] # trim trailing newline
173 obj_str += template.format(i_msg, longs_str)
174 obj_str = obj_str[:-1] # trim trailing newline
177 def get_obituaries(obit):
185 query = make_c_string(o["query"])
186 yes = make_c_string(o["yes_response"])
187 obit_str += template.format(query, yes)
188 obit_str = obit_str[:-1] # trim trailing newline
191 with open(yaml_name, "r") as f:
194 h = h_template.format(
195 get_refs(db["arbitrary_messages"]),
196 get_refs(db["class_messages"]),
197 get_refs(db["turn_threshold_messages"]),
198 get_refs(db["locations"]),
199 get_refs(db["object_descriptions"]),
202 c = c_template.format(
204 get_arbitrary_messages(db["arbitrary_messages"]),
205 get_class_messages(db["class_messages"]),
206 get_turn_threshold_messages(db["turn_threshold_messages"]),
207 get_locations(db["locations"]),
208 get_object_descriptions(db["object_descriptions"]),
209 get_obituaries(db["obituaries"]),
210 len(db["class_messages"]),
211 len(db["obituaries"]),
214 with open(h_name, "w") as hf:
217 with open(c_name, "w") as cf: