diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2023-11-08 18:35:12 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-08 17:35:12 -0600 |
commit | bbbbc71bee25cfd22c5304f98f5a7881383585a3 (patch) | |
tree | d27581db7b8db03da4b731fe8c2d5072d3162cf8 /utils/assign_ids.rb | |
download | lingo-apworld-bbbbc71bee25cfd22c5304f98f5a7881383585a3.tar.gz lingo-apworld-bbbbc71bee25cfd22c5304f98f5a7881383585a3.tar.bz2 lingo-apworld-bbbbc71bee25cfd22c5304f98f5a7881383585a3.zip |
Lingo: New game (#1806)
Co-authored-by: Aaron Wagener <mmmcheese158@gmail.com> Co-authored-by: Fabian Dill <Berserker66@users.noreply.github.com> Co-authored-by: Phar <zach@alliware.com>
Diffstat (limited to 'utils/assign_ids.rb')
-rw-r--r-- | utils/assign_ids.rb | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/utils/assign_ids.rb b/utils/assign_ids.rb new file mode 100644 index 0000000..9e1ce67 --- /dev/null +++ b/utils/assign_ids.rb | |||
@@ -0,0 +1,178 @@ | |||
1 | # This utility goes through the provided Lingo config and assigns item and | ||
2 | # location IDs to entities that require them (such as doors and panels). These | ||
3 | # IDs are output in a separate yaml file. If the output file already exists, | ||
4 | # then it will be updated with any newly assigned IDs rather than overwritten. | ||
5 | # In this event, all new IDs will be greater than any already existing IDs, | ||
6 | # even if there are gaps in the ID space; this is to prevent collision when IDs | ||
7 | # are retired. | ||
8 | # | ||
9 | # This utility should be run whenever logically new items or locations are | ||
10 | # required. If an item or location is created that is logically equivalent to | ||
11 | # one that used to exist, this utility should not be used, and instead the ID | ||
12 | # file should be manually edited so that the old ID can be reused. | ||
13 | |||
14 | require 'set' | ||
15 | require 'yaml' | ||
16 | |||
17 | configpath = ARGV[0] | ||
18 | outputpath = ARGV[1] | ||
19 | |||
20 | next_item_id = 444400 | ||
21 | next_location_id = 444400 | ||
22 | |||
23 | location_id_by_name = {} | ||
24 | |||
25 | old_generated = YAML.load_file(outputpath) | ||
26 | File.write(outputpath + ".old", old_generated.to_yaml) | ||
27 | |||
28 | if old_generated.include? "special_items" then | ||
29 | old_generated["special_items"].each do |name, id| | ||
30 | if id >= next_item_id then | ||
31 | next_item_id = id + 1 | ||
32 | end | ||
33 | end | ||
34 | end | ||
35 | if old_generated.include? "special_locations" then | ||
36 | old_generated["special_locations"].each do |name, id| | ||
37 | if id >= next_location_id then | ||
38 | next_location_id = id + 1 | ||
39 | end | ||
40 | end | ||
41 | end | ||
42 | if old_generated.include? "panels" then | ||
43 | old_generated["panels"].each do |room, panels| | ||
44 | panels.each do |name, id| | ||
45 | if id >= next_location_id then | ||
46 | next_location_id = id + 1 | ||
47 | end | ||
48 | location_name = "#{room} - #{name}" | ||
49 | location_id_by_name[location_name] = id | ||
50 | end | ||
51 | end | ||
52 | end | ||
53 | if old_generated.include? "doors" then | ||
54 | old_generated["doors"].each do |room, doors| | ||
55 | doors.each do |name, ids| | ||
56 | if ids.include? "location" then | ||
57 | if ids["location"] >= next_location_id then | ||
58 | next_location_id = ids["location"] + 1 | ||
59 | end | ||
60 | end | ||
61 | if ids.include? "item" then | ||
62 | if ids["item"] >= next_item_id then | ||
63 | next_item_id = ids["item"] + 1 | ||
64 | end | ||
65 | end | ||
66 | end | ||
67 | end | ||
68 | end | ||
69 | if old_generated.include? "door_groups" then | ||
70 | old_generated["door_groups"].each do |name, id| | ||
71 | if id >= next_item_id then | ||
72 | next_item_id = id + 1 | ||
73 | end | ||
74 | end | ||
75 | end | ||
76 | if old_generated.include? "progression" then | ||
77 | old_generated["progression"].each do |name, id| | ||
78 | if id >= next_item_id then | ||
79 | next_item_id = id + 1 | ||
80 | end | ||
81 | end | ||
82 | end | ||
83 | |||
84 | door_groups = Set[] | ||
85 | |||
86 | config = YAML.load_file(configpath) | ||
87 | config.each do |room_name, room_data| | ||
88 | if room_data.include? "panels" | ||
89 | room_data["panels"].each do |panel_name, panel| | ||
90 | unless old_generated.include? "panels" and old_generated["panels"].include? room_name and old_generated["panels"][room_name].include? panel_name then | ||
91 | old_generated["panels"] ||= {} | ||
92 | old_generated["panels"][room_name] ||= {} | ||
93 | old_generated["panels"][room_name][panel_name] = next_location_id | ||
94 | |||
95 | location_name = "#{room_name} - #{panel_name}" | ||
96 | location_id_by_name[location_name] = next_location_id | ||
97 | |||
98 | next_location_id += 1 | ||
99 | end | ||
100 | end | ||
101 | end | ||
102 | end | ||
103 | |||
104 | config.each do |room_name, room_data| | ||
105 | if room_data.include? "doors" | ||
106 | room_data["doors"].each do |door_name, door| | ||
107 | if door.include? "event" and door["event"] then | ||
108 | next | ||
109 | end | ||
110 | |||
111 | unless door.include? "skip_item" and door["skip_item"] then | ||
112 | unless old_generated.include? "doors" and old_generated["doors"].include? room_name and old_generated["doors"][room_name].include? door_name and old_generated["doors"][room_name][door_name].include? "item" then | ||
113 | old_generated["doors"] ||= {} | ||
114 | old_generated["doors"][room_name] ||= {} | ||
115 | old_generated["doors"][room_name][door_name] ||= {} | ||
116 | old_generated["doors"][room_name][door_name]["item"] = next_item_id | ||
117 | |||
118 | next_item_id += 1 | ||
119 | end | ||
120 | |||
121 | if door.include? "group" and not door_groups.include? door["group"] then | ||
122 | door_groups.add(door["group"]) | ||
123 | |||
124 | unless old_generated.include? "door_groups" and old_generated["door_groups"].include? door["group"] then | ||
125 | old_generated["door_groups"] ||= {} | ||
126 | old_generated["door_groups"][door["group"]] = next_item_id | ||
127 | |||
128 | next_item_id += 1 | ||
129 | end | ||
130 | end | ||
131 | end | ||
132 | |||
133 | unless door.include? "skip_location" and door["skip_location"] then | ||
134 | location_name = "" | ||
135 | if door.include? "location_name" then | ||
136 | location_name = door["location_name"] | ||
137 | elsif door.include? "panels" then | ||
138 | location_name = door["panels"].map do |panel| | ||
139 | if panel.kind_of? Hash then | ||
140 | panel | ||
141 | else | ||
142 | {"room" => room_name, "panel" => panel} | ||
143 | end | ||
144 | end.sort_by {|panel| panel["room"]}.chunk {|panel| panel["room"]}.map do |room_panels| | ||
145 | room_panels[0] + " - " + room_panels[1].map{|panel| panel["panel"]}.join(", ") | ||
146 | end.join(" and ") | ||
147 | end | ||
148 | |||
149 | if location_id_by_name.has_key? location_name then | ||
150 | old_generated["doors"] ||= {} | ||
151 | old_generated["doors"][room_name] ||= {} | ||
152 | old_generated["doors"][room_name][door_name] ||= {} | ||
153 | old_generated["doors"][room_name][door_name]["location"] = location_id_by_name[location_name] | ||
154 | elsif not (old_generated.include? "doors" and old_generated["doors"].include? room_name and old_generated["doors"][room_name].include? door_name and old_generated["doors"][room_name][door_name].include? "location") then | ||
155 | old_generated["doors"] ||= {} | ||
156 | old_generated["doors"][room_name] ||= {} | ||
157 | old_generated["doors"][room_name][door_name] ||= {} | ||
158 | old_generated["doors"][room_name][door_name]["location"] = next_location_id | ||
159 | |||
160 | next_location_id += 1 | ||
161 | end | ||
162 | end | ||
163 | end | ||
164 | end | ||
165 | |||
166 | if room_data.include? "progression" | ||
167 | room_data["progression"].each do |progression_name, pdata| | ||
168 | unless old_generated.include? "progression" and old_generated["progression"].include? progression_name then | ||
169 | old_generated["progression"] ||= {} | ||
170 | old_generated["progression"][progression_name] = next_item_id | ||
171 | |||
172 | next_item_id += 1 | ||
173 | end | ||
174 | end | ||
175 | end | ||
176 | end | ||
177 | |||
178 | File.write(outputpath, old_generated.to_yaml) | ||