52380e6705c55a5774c15055c5d079eca892f084
[open-adventure.git] / newdungeon.py
1 #!/usr/bin/python3
2
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.
4
5 import yaml
6
7 yaml_name = "adventure.yaml"
8 h_name = "newdb.h"
9 c_name = "newdb.c"
10
11 h_template = """/* Generated from adventure.yaml - do not hand-hack! */
12
13 #include <stdio.h>
14
15 typedef struct {{
16   const char* inventory;
17   const char** longs;
18 }} object_description_t;
19
20 typedef struct {{
21   const char* small;
22   const char* big;
23 }} descriptions_t;
24
25 typedef struct {{
26   descriptions_t description;
27 }} location_t;
28
29 typedef struct {{
30   const char* query;
31   const char* yes_response;
32 }} obituary_t;
33
34 typedef struct {{
35   const int threshold;
36   const int point_loss;
37   const char* message;
38 }} turn_threshold_t;
39
40 typedef struct {{
41   const int threshold;
42   const char* message;
43 }} class_t;
44
45 typedef struct {{
46   const int number;
47   const int turns;
48   const int penalty;
49   const char* question;
50   const char* hint;
51 }} hint_t;
52
53 extern location_t locations[];
54 extern object_description_t object_descriptions[];
55 extern const char* arbitrary_messages[];
56 extern const class_t classes[];
57 extern turn_threshold_t turn_thresholds[];
58 extern obituary_t obituaries[];
59 extern hint_t hints[];
60
61 extern const size_t CLSSES;
62 extern const int maximum_deaths;
63 extern const int turn_threshold_count;
64
65 enum arbitrary_messages_refs {{
66 {}
67 }};
68
69 enum locations_refs {{
70 {}
71 }};
72
73 enum object_descriptions_refs {{
74 {}
75 }};
76
77 /* end */
78 """
79
80 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
81
82 #include "{}"
83
84 const char* arbitrary_messages[] = {{
85 {}
86 }};
87
88 const class_t classes[] = {{
89 {}
90 }};
91
92 turn_threshold_t turn_thresholds[] = {{
93 {}
94 }};
95
96 location_t locations[] = {{
97 {}
98 }};
99
100 object_description_t object_descriptions[] = {{
101 {}
102 }};
103
104 obituary_t obituaries[] = {{
105 {}
106 }};
107
108 hint_t hints[] = {{
109 {}
110 }};
111
112 const size_t CLSSES = {};
113 const int maximum_deaths = {};
114 const int turn_threshold_count = {};
115
116 /* end */
117 """
118
119 def make_c_string(string):
120     """Render a Python string into C string literal format."""
121     if string == None:
122         return "NULL"
123     string = string.replace("\n", "\\n")
124     string = string.replace("\t", "\\t")
125     string = string.replace('"', '\\"')
126     string = string.replace("'", "\\'")
127     string = '"' + string + '"'
128     return string
129
130 def get_refs(l):
131     reflist = [x[0] for x in l]
132     ref_str = ""
133     for ref in reflist:
134         ref_str += "    {},\n".format(ref)
135     ref_str = ref_str[:-1] # trim trailing newline
136     return ref_str
137
138 def get_arbitrary_messages(arb):
139     template = """    {},
140 """
141     arb_str = ""
142     for item in arb:
143         arb_str += template.format(make_c_string(item[1]))
144     arb_str = arb_str[:-1] # trim trailing newline
145     return arb_str
146
147 def get_class_messages(cls):
148     template = """    {{
149         .threshold = {},
150         .message = {},
151     }},
152 """
153     cls_str = ""
154     for item in cls:
155         threshold = item["threshold"]
156         message = make_c_string(item["message"])
157         cls_str += template.format(threshold, message)
158     cls_str = cls_str[:-1] # trim trailing newline
159     return cls_str
160
161 def get_turn_thresholds(trn):
162     template = """    {{
163         .threshold = {},
164         .point_loss = {},
165         .message = {},
166     }},
167 """
168     trn_str = ""
169     for item in trn:
170         threshold = item["threshold"]
171         point_loss = item["point_loss"]
172         message = make_c_string(item["message"])
173         trn_str += template.format(threshold, point_loss, message)
174     trn_str = trn_str[:-1] # trim trailing newline
175     return trn_str
176
177 def get_locations(loc):
178     template = """    {{
179         .description = {{
180             .small = {},
181             .big = {},
182         }},
183     }},
184 """
185     loc_str = ""
186     for item in loc:
187         short_d = make_c_string(item[1]["description"]["short"])
188         long_d = make_c_string(item[1]["description"]["long"])
189         loc_str += template.format(short_d, long_d)
190     loc_str = loc_str[:-1] # trim trailing newline
191     return loc_str
192
193 def get_object_descriptions(obj):
194     template = """    {{
195         .inventory = {},
196         .longs = (const char* []) {{
197 {}
198         }},
199     }},
200 """
201     obj_str = ""
202     for item in obj:
203         i_msg = make_c_string(item[1]["inventory"])
204         longs_str = ""
205         if item[1]["longs"] == None:
206             longs_str = " " * 12 + "NULL,"
207         else:
208             for l_msg in item[1]["longs"]:
209                 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
210             longs_str = longs_str[:-1] # trim trailing newline
211         obj_str += template.format(i_msg, longs_str)
212     obj_str = obj_str[:-1] # trim trailing newline
213     return obj_str
214
215 def get_obituaries(obit):
216     template = """    {{
217         .query = {},
218         .yes_response = {},
219     }},
220 """
221     obit_str = ""
222     for o in obit:
223         query = make_c_string(o["query"])
224         yes = make_c_string(o["yes_response"])
225         obit_str += template.format(query, yes)
226     obit_str = obit_str[:-1] # trim trailing newline
227     return obit_str
228
229 def get_hints(hnt, arb):
230     template = """    {{
231         .number = {},
232         .penalty = {},
233         .turns = {},
234         .question = {},
235         .hint = {},
236     }},
237 """
238     hnt_str = ""
239     md = dict(arb)
240     for item in hnt:
241         number = item["number"]
242         penalty = item["penalty"]
243         turns = item["turns"]
244         question = make_c_string(md[item["question"]])
245         hint = make_c_string(md[item["hint"]])
246         hnt_str += template.format(number, penalty, turns, question, hint)
247     hnt_str = hnt_str[:-1] # trim trailing newline
248     return hnt_str
249
250
251 if __name__ == "__main__":
252     with open(yaml_name, "r") as f:
253         db = yaml.load(f)
254
255     h = h_template.format(
256         get_refs(db["arbitrary_messages"]),
257         get_refs(db["locations"]),
258         get_refs(db["object_descriptions"]),
259     )
260
261     c = c_template.format(
262         h_name,
263         get_arbitrary_messages(db["arbitrary_messages"]),
264         get_class_messages(db["classes"]),
265         get_turn_thresholds(db["turn_thresholds"]),
266         get_locations(db["locations"]),
267         get_object_descriptions(db["object_descriptions"]),
268         get_obituaries(db["obituaries"]),
269         get_hints(db["hints"], db["arbitrary_messages"]),
270         len(db["classes"]),
271         len(db["obituaries"]),
272         len(db["turn_thresholds"]),
273     )
274
275     with open(h_name, "w") as hf:
276         hf.write(h)
277
278     with open(c_name, "w") as cf:
279         cf.write(c)
280
281 # end