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.
8 yaml_name = "adventure.yaml"
13 """Add C escape sequences to a string."""
14 string = string.replace("\n", "\\n")
15 string = string.replace("\t", "\\t")
16 string = string.replace('"', '\\"')
17 string = string.replace("'", "\\'")
20 def quotewrap(string):
21 """Wrap a string in double quotes."""
22 return '"' + string + '"'
24 def write_regular_messages(name, h, c):
26 h += "enum {}_refs {{\n".format(name)
27 for key, text in dungeon[name]:
28 h += " {},\n".format(key)
31 c += "const char* {}[] = {{\n".format(name)
33 for key, text in dungeon[name]:
38 c += " \"{}\",\n".format(text)
44 with open(yaml_name, "r") as f:
45 dungeon = yaml.load(f)
47 h = """#include <stdio.h>
50 const char* inventory;
52 } object_description_t;
60 descriptions_t description;
63 extern location_t locations[];
64 extern object_description_t object_descriptions[];
65 extern const char* arbitrary_messages[];
66 extern const char* class_messages[];
67 extern const char* turn_threshold_messages[];
80 "turn_threshold_messages",
82 h, c = write_regular_messages(name, h, c)
84 h += "enum locations_refs {\n"
85 c += "location_t locations[] = {\n"
86 for key, data in dungeon["locations"]:
87 h += " {},\n".format(key)
90 short = quotewrap(c_escape(data["description"]["short"]))
91 except AttributeError:
94 long = quotewrap(c_escape(data["description"]["long"]))
95 except AttributeError:
104 """.format(short, long)
109 h += "enum object_descriptions_refs {\n"
110 c += "object_description_t object_descriptions[] = {\n"
111 for key, data in dungeon["object_descriptions"]:
113 data["inventory"] = "\"{}\"".format(c_escape(data["inventory"]))
114 except AttributeError:
115 data["inventory"] = "NULL"
116 h += " {},\n".format(key)
118 c += " .inventory = {},\n".format(data["inventory"])
121 c += " .longs = (const char* []) {\n"
122 for l in data["longs"]:
124 c += " \"{}\",\n".format(l)
126 except (TypeError, IndexError):
127 c += " .longs = NULL,\n"
134 """.format(len(dungeon["class_messages"]))
136 # finally, write out the files
141 for filename, string in d.items():
142 with open(filename, "w") as f: