yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
resource_context_builder.cc
Go to the documentation of this file.
2
3#include <sstream>
4
5namespace yaze {
6namespace cli {
7
9 std::ostringstream oss;
10 oss << "Common Tile16s:\n";
11 oss << " - 0x020: Grass\n";
12 oss << " - 0x022: Dirt\n";
13 oss << " - 0x02E: Tree\n";
14 oss << " - 0x003: Bush\n";
15 oss << " - 0x004: Rock\n";
16 oss << " - 0x021: Flower\n";
17 oss << " - 0x023: Sand\n";
18 oss << " - 0x14C: Water (top edge)\n";
19 oss << " - 0x14D: Water (middle)\n";
20 oss << " - 0x14E: Water (bottom edge)\n";
21 return oss.str();
22}
23
25 if (!rom_ || !rom_->is_loaded()) {
26 return "Overworld Maps: (ROM not loaded)\n";
27 }
28
29 auto* label_mgr = rom_->resource_label();
30 if (!label_mgr || !label_mgr->labels_loaded_) {
31 return "Overworld Maps: (No labels file loaded)\n";
32 }
33
34 std::ostringstream oss;
35 oss << "Overworld Maps:\n";
36
37 // Check if "overworld" labels exist
38 auto it = label_mgr->labels_.find("overworld");
39 if (it != label_mgr->labels_.end()) {
40 for (const auto& [key, value] : it->second) {
41 oss << " - " << key << ": \"" << value << "\"\n";
42 }
43 } else {
44 // Provide defaults
45 oss << " - 0: \"Light World\"\n";
46 oss << " - 1: \"Dark World\"\n";
47 oss << " - 3: \"Desert\"\n";
48 }
49
50 return oss.str();
51}
52
54 if (!rom_ || !rom_->is_loaded()) {
55 return "Dungeons: (ROM not loaded)\n";
56 }
57
58 auto* label_mgr = rom_->resource_label();
59 if (!label_mgr || !label_mgr->labels_loaded_) {
60 return "Dungeons: (No labels file loaded)\n";
61 }
62
63 std::ostringstream oss;
64 oss << "Dungeons:\n";
65
66 // Check if "dungeon" labels exist
67 auto it = label_mgr->labels_.find("dungeon");
68 if (it != label_mgr->labels_.end()) {
69 for (const auto& [key, value] : it->second) {
70 oss << " - " << key << ": \"" << value << "\"\n";
71 }
72 } else {
73 // Provide vanilla defaults
74 oss << " - 0x00: \"Hyrule Castle\"\n";
75 oss << " - 0x02: \"Eastern Palace\"\n";
76 oss << " - 0x04: \"Desert Palace\"\n";
77 oss << " - 0x06: \"Tower of Hera\"\n";
78 }
79
80 return oss.str();
81}
82
84 if (!rom_ || !rom_->is_loaded()) {
85 return "Entrances: (ROM not loaded)\n";
86 }
87
88 auto* label_mgr = rom_->resource_label();
89 if (!label_mgr || !label_mgr->labels_loaded_) {
90 return "Entrances: (No labels file loaded)\n";
91 }
92
93 std::ostringstream oss;
94 oss << "Entrances:\n";
95
96 // Check if "entrance" labels exist
97 auto it = label_mgr->labels_.find("entrance");
98 if (it != label_mgr->labels_.end()) {
99 for (const auto& [key, value] : it->second) {
100 oss << " - " << key << ": \"" << value << "\"\n";
101 }
102 } else {
103 // Provide vanilla defaults
104 oss << " - 0x00: \"Link's House\"\n";
105 oss << " - 0x01: \"Sanctuary\"\n";
106 }
107
108 return oss.str();
109}
110
112 if (!rom_ || !rom_->is_loaded()) {
113 return "Rooms: (ROM not loaded)\n";
114 }
115
116 auto* label_mgr = rom_->resource_label();
117 if (!label_mgr || !label_mgr->labels_loaded_) {
118 return "Rooms: (No labels file loaded)\n";
119 }
120
121 std::ostringstream oss;
122 oss << "Rooms:\n";
123
124 // Check if "room" labels exist
125 auto it = label_mgr->labels_.find("room");
126 if (it != label_mgr->labels_.end()) {
127 for (const auto& [key, value] : it->second) {
128 oss << " - " << key << ": \"" << value << "\"\n";
129 }
130 } else {
131 oss << " (No room labels defined)\n";
132 }
133
134 return oss.str();
135}
136
138 if (!rom_ || !rom_->is_loaded()) {
139 return "Sprites: (ROM not loaded)\n";
140 }
141
142 auto* label_mgr = rom_->resource_label();
143 if (!label_mgr || !label_mgr->labels_loaded_) {
144 return "Sprites: (No labels file loaded)\n";
145 }
146
147 std::ostringstream oss;
148 oss << "Sprites:\n";
149
150 // Check if "sprite" labels exist
151 auto it = label_mgr->labels_.find("sprite");
152 if (it != label_mgr->labels_.end()) {
153 for (const auto& [key, value] : it->second) {
154 oss << " - " << key << ": \"" << value << "\"\n";
155 }
156 } else {
157 // Provide vanilla defaults
158 oss << " - 0x00: \"Soldier\"\n";
159 oss << " - 0x01: \"Octorok\"\n";
160 }
161
162 return oss.str();
163}
164
165absl::StatusOr<std::string> ResourceContextBuilder::BuildResourceContext() {
166 if (!rom_) {
167 return absl::InvalidArgumentError("ROM pointer is null");
168 }
169
170 std::ostringstream context;
171
172 context << "=== AVAILABLE RESOURCES ===\n\n";
173
174 // Add overworld maps
175 context << ExtractOverworldLabels() << "\n";
176
177 // Add dungeons
178 context << ExtractDungeonLabels() << "\n";
179
180 // Add entrances
181 context << ExtractEntranceLabels() << "\n";
182
183 // Add rooms (if any)
184 context << ExtractRoomLabels() << "\n";
185
186 // Add sprites
187 context << ExtractSpriteLabels() << "\n";
188
189 // Add common tile16 reference
190 context << GetCommonTile16Reference() << "\n";
191
192 context << "=== INSTRUCTIONS ===\n";
193 context << "1. Use the resource labels when they're available\n";
194 context << "2. If a user refers to a custom name, check the labels above\n";
195 context << "3. Always provide tile16 IDs as hex values (0x###)\n";
196 context << "4. Explain which resources you're using in your reasoning\n";
197
198 return context.str();
199}
200
201absl::StatusOr<std::map<std::string, std::string>>
202ResourceContextBuilder::GetLabels(const std::string& category) {
203 if (!rom_ || !rom_->is_loaded()) {
204 return absl::FailedPreconditionError("ROM not loaded");
205 }
206
207 auto* label_mgr = rom_->resource_label();
208 if (!label_mgr) {
209 return absl::FailedPreconditionError("No resource label manager");
210 }
211
212 if (!label_mgr->labels_loaded_) {
213 return absl::FailedPreconditionError("No labels file loaded");
214 }
215
216 std::map<std::string, std::string> result;
217
218 auto it = label_mgr->labels_.find(category);
219 if (it != label_mgr->labels_.end()) {
220 for (const auto& [key, value] : it->second) {
221 result[key] = value;
222 }
223 }
224
225 return result;
226}
227
228absl::StatusOr<std::string> ResourceContextBuilder::ExportToJson() {
229 if (!rom_ || !rom_->is_loaded()) {
230 return absl::InvalidArgumentError("ROM not loaded");
231 }
232
233 auto* label_mgr = rom_->resource_label();
234 if (!label_mgr || !label_mgr->labels_loaded_) {
235 return absl::InvalidArgumentError("No labels file loaded");
236 }
237
238 std::ostringstream json;
239 json << "{\n";
240
241 bool first_category = true;
242 for (const auto& [category, labels] : label_mgr->labels_) {
243 if (!first_category) json << ",\n";
244 first_category = false;
245
246 json << " \"" << category << "\": {\n";
247
248 bool first_label = true;
249 for (const auto& [key, value] : labels) {
250 if (!first_label) json << ",\n";
251 first_label = false;
252
253 json << " \"" << key << "\": \"" << value << "\"";
254 }
255
256 json << "\n }";
257 }
258
259 json << "\n}\n";
260
261 return json.str();
262}
263
264} // namespace cli
265} // namespace yaze
266
core::ResourceLabelManager * resource_label()
Definition rom.h:220
bool is_loaded() const
Definition rom.h:197
std::string ExtractSpriteLabels()
Extract sprite labels.
absl::StatusOr< std::string > ExportToJson()
Export all labels to JSON format.
std::string GetCommonTile16Reference()
Add common tile16 reference for AI.
std::string ExtractDungeonLabels()
Extract dungeon labels.
std::string ExtractRoomLabels()
Extract room labels.
absl::StatusOr< std::string > BuildResourceContext()
Build a complete resource context string for AI prompts.
std::string ExtractOverworldLabels()
Extract overworld map labels.
absl::StatusOr< std::map< std::string, std::string > > GetLabels(const std::string &category)
Get labels for a specific resource category.
std::string ExtractEntranceLabels()
Extract entrance labels.
Main namespace for the application.
std::unordered_map< std::string, std::unordered_map< std::string, std::string > > labels_
Definition project.h:235