6#include "absl/strings/match.h"
7#include "absl/strings/numbers.h"
8#include "absl/strings/str_cat.h"
9#include "absl/strings/str_split.h"
10#include "nlohmann/json.hpp"
18 std::ostringstream oss;
19 oss <<
"Map " <<
map_id <<
" @ (" <<
x <<
"," <<
y <<
"): "
25 std::ostringstream json;
27 json <<
" \"id\": \"" <<
id <<
"\",\n";
28 json <<
" \"prompt\": \"" <<
prompt <<
"\",\n";
29 json <<
" \"ai_service\": \"" <<
ai_service <<
"\",\n";
30 json <<
" \"reasoning\": \"" <<
reasoning <<
"\",\n";
31 json <<
" \"status\": ";
35 json <<
"\"pending\"";
38 json <<
"\"accepted\"";
41 json <<
"\"rejected\"";
44 json <<
"\"applied\"";
49 json <<
" \"changes\": [\n";
50 for (
size_t i = 0; i <
changes.size(); ++i) {
51 const auto& change =
changes[i];
53 json <<
" \"map_id\": " << change.map_id <<
",\n";
54 json <<
" \"x\": " << change.x <<
",\n";
55 json <<
" \"y\": " << change.y <<
",\n";
56 json <<
" \"old_tile\": \"0x" << std::hex << change.old_tile
58 json <<
" \"new_tile\": \"0x" << std::hex << change.new_tile <<
"\"\n";
74 if (!json.contains(field)) {
75 return absl::InvalidArgumentError(
76 absl::StrCat(
"Missing field '", field,
"' in proposal change"));
79 if (json[field].is_number_integer()) {
80 int value = json[field].get<
int>();
81 if (value < 0 || value > 0xFFFF) {
82 return absl::InvalidArgumentError(
83 absl::StrCat(
"Tile value for '", field,
"' out of range: ", value));
85 return static_cast<uint16_t
>(value);
88 if (json[field].is_string()) {
89 std::string value = json[field].get<std::string>();
91 return absl::InvalidArgumentError(
92 absl::StrCat(
"Tile value for '", field,
"' is empty"));
96 if (absl::StartsWith(value,
"0x") || absl::StartsWith(value,
"0X")) {
97 if (value.size() <= 2) {
98 return absl::InvalidArgumentError(
99 absl::StrCat(
"Invalid hex tile value for '", field,
100 "': ", json[field].get<std::string>()));
102 value = value.substr(2);
103 unsigned int parsed = 0;
104 if (!absl::SimpleHexAtoi(value, &parsed) || parsed > 0xFFFF) {
105 return absl::InvalidArgumentError(
106 absl::StrCat(
"Invalid hex tile value for '", field,
107 "': ", json[field].get<std::string>()));
109 return static_cast<uint16_t
>(parsed);
112 unsigned int parsed = 0;
113 if (!absl::SimpleAtoi(value, &parsed) || parsed > 0xFFFF) {
114 return absl::InvalidArgumentError(
115 absl::StrCat(
"Invalid tile value for '", field,
116 "': ", json[field].get<std::string>()));
118 return static_cast<uint16_t
>(parsed);
121 return absl::InvalidArgumentError(
122 absl::StrCat(
"Unsupported JSON type for tile field '", field,
"'"));
126 if (absl::StartsWith(status_text,
"accept")) {
129 if (absl::StartsWith(status_text,
"reject")) {
132 if (absl::StartsWith(status_text,
"apply")) {
141 const std::string& json_text) {
144 json = nlohmann::json::parse(json_text);
145 }
catch (
const nlohmann::json::parse_error& error) {
146 return absl::InvalidArgumentError(
147 absl::StrCat(
"Failed to parse proposal JSON: ", error.what()));
152 if (!json.contains(
"id") || !json[
"id"].is_string()) {
153 return absl::InvalidArgumentError(
154 "Proposal JSON must include string field 'id'");
156 proposal.
id = json[
"id"].get<std::string>();
158 if (!json.contains(
"prompt") || !json[
"prompt"].is_string()) {
159 return absl::InvalidArgumentError(
160 "Proposal JSON must include string field 'prompt'");
162 proposal.
prompt = json[
"prompt"].get<std::string>();
164 if (json.contains(
"ai_service") && json[
"ai_service"].is_string()) {
165 proposal.
ai_service = json[
"ai_service"].get<std::string>();
168 if (json.contains(
"reasoning") && json[
"reasoning"].is_string()) {
169 proposal.
reasoning = json[
"reasoning"].get<std::string>();
172 if (json.contains(
"status")) {
173 if (!json[
"status"].is_string()) {
174 return absl::InvalidArgumentError(
175 "Proposal 'status' must be a string value");
177 proposal.
status = ParseStatus(json[
"status"].get<std::string>());
182 if (json.contains(
"changes")) {
183 if (!json[
"changes"].is_array()) {
184 return absl::InvalidArgumentError(
185 "Proposal 'changes' field must be an array");
188 for (
const auto& change_json : json[
"changes"]) {
189 if (!change_json.is_object()) {
190 return absl::InvalidArgumentError(
191 "Each change entry must be a JSON object");
195 if (!change_json.contains(
"map_id") ||
196 !change_json[
"map_id"].is_number_integer()) {
197 return absl::InvalidArgumentError(
198 "Tile change missing integer field 'map_id'");
200 change.
map_id = change_json[
"map_id"].get<
int>();
202 if (!change_json.contains(
"x") || !change_json[
"x"].is_number_integer()) {
203 return absl::InvalidArgumentError(
204 "Tile change missing integer field 'x'");
206 change.
x = change_json[
"x"].get<
int>();
208 if (!change_json.contains(
"y") || !change_json[
"y"].is_number_integer()) {
209 return absl::InvalidArgumentError(
210 "Tile change missing integer field 'y'");
212 change.
y = change_json[
"y"].get<
int>();
215 ParseTileValue(change_json,
"old_tile"));
217 ParseTileValue(change_json,
"new_tile"));
219 proposal.
changes.push_back(change);
223 if (proposal.
changes.empty()) {
224 return absl::InvalidArgumentError(
225 "Proposal JSON did not include any tile16 changes");
228 proposal.
created_at = std::chrono::system_clock::now();
229 if (json.contains(
"created_at_ms") && json[
"created_at_ms"].is_number()) {
230 auto millis = json[
"created_at_ms"].get<int64_t>();
231 proposal.
created_at = std::chrono::system_clock::time_point(
232 std::chrono::milliseconds(millis));
240 auto now = std::chrono::system_clock::now();
241 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
242 now.time_since_epoch())
245 std::ostringstream oss;
246 oss <<
"proposal_" << ms;
251 const std::string& command,
Rom* rom) {
253 std::vector<std::string> parts = absl::StrSplit(command,
' ');
255 if (parts.size() < 10) {
256 return absl::InvalidArgumentError(
257 absl::StrCat(
"Invalid command format: ", command));
260 if (parts[0] !=
"overworld" || parts[1] !=
"set-tile") {
261 return absl::InvalidArgumentError(
262 absl::StrCat(
"Not a set-tile command: ", command));
268 for (
size_t i = 2; i < parts.size(); i += 2) {
269 if (i + 1 >= parts.size())
272 const std::string& flag = parts[i];
273 const std::string& value = parts[i + 1];
275 if (flag ==
"--map") {
276 change.
map_id = std::stoi(value);
277 }
else if (flag ==
"--x") {
278 change.
x = std::stoi(value);
279 }
else if (flag ==
"--y") {
280 change.
y = std::stoi(value);
281 }
else if (flag ==
"--tile") {
283 change.
new_tile =
static_cast<uint16_t
>(std::stoi(value,
nullptr, 16));
290 auto status = overworld.
Load(rom);
296 if (change.
map_id < 0x40) {
298 }
else if (change.
map_id < 0x80) {
312absl::StatusOr<std::vector<Tile16Change>>
317 std::vector<std::string> parts = absl::StrSplit(command,
' ');
319 if (parts.size() < 12) {
320 return absl::InvalidArgumentError(
321 absl::StrCat(
"Invalid set-area command format: ", command));
324 if (parts[0] !=
"overworld" || parts[1] !=
"set-area") {
325 return absl::InvalidArgumentError(
326 absl::StrCat(
"Not a set-area command: ", command));
329 int map_id = 0, x = 0, y = 0, width = 1, height = 1;
330 uint16_t new_tile = 0;
333 for (
size_t i = 2; i < parts.size(); i += 2) {
334 if (i + 1 >= parts.size())
337 const std::string& flag = parts[i];
338 const std::string& value = parts[i + 1];
340 if (flag ==
"--map") {
341 map_id = std::stoi(value);
342 }
else if (flag ==
"--x") {
343 x = std::stoi(value);
344 }
else if (flag ==
"--y") {
345 y = std::stoi(value);
346 }
else if (flag ==
"--width") {
347 width = std::stoi(value);
348 }
else if (flag ==
"--height") {
349 height = std::stoi(value);
350 }
else if (flag ==
"--tile") {
351 new_tile =
static_cast<uint16_t
>(std::stoi(value,
nullptr, 16));
356 std::vector<Tile16Change> changes;
359 auto status = overworld.
Load(rom);
367 }
else if (map_id < 0x80) {
374 for (
int dy = 0; dy < height; ++dy) {
375 for (
int dx = 0; dx < width; ++dx) {
382 changes.push_back(change);
387 for (
int dy = 0; dy < height; ++dy) {
388 for (
int dx = 0; dx < width; ++dx) {
395 changes.push_back(change);
403absl::StatusOr<std::vector<Tile16Change>>
409 std::vector<std::string> parts = absl::StrSplit(command,
' ');
411 if (parts.size() < 8) {
412 return absl::InvalidArgumentError(
413 absl::StrCat(
"Invalid replace-tile command format: ", command));
416 if (parts[0] !=
"overworld" || parts[1] !=
"replace-tile") {
417 return absl::InvalidArgumentError(
418 absl::StrCat(
"Not a replace-tile command: ", command));
422 uint16_t old_tile = 0, new_tile = 0;
423 int x_min = 0, y_min = 0, x_max = 31, y_max = 31;
426 for (
size_t i = 2; i < parts.size(); i += 2) {
427 if (i + 1 >= parts.size())
430 const std::string& flag = parts[i];
431 const std::string& value = parts[i + 1];
433 if (flag ==
"--map") {
434 map_id = std::stoi(value);
435 }
else if (flag ==
"--old-tile") {
436 old_tile =
static_cast<uint16_t
>(std::stoi(value,
nullptr, 16));
437 }
else if (flag ==
"--new-tile") {
438 new_tile =
static_cast<uint16_t
>(std::stoi(value,
nullptr, 16));
439 }
else if (flag ==
"--x-min") {
440 x_min = std::stoi(value);
441 }
else if (flag ==
"--y-min") {
442 y_min = std::stoi(value);
443 }
else if (flag ==
"--x-max") {
444 x_max = std::stoi(value);
445 }
else if (flag ==
"--y-max") {
446 y_max = std::stoi(value);
451 return absl::FailedPreconditionError(
452 "ROM must be loaded to scan for tiles to replace");
456 auto status = overworld.
Load(rom);
464 }
else if (map_id < 0x80) {
471 std::vector<Tile16Change> changes;
472 for (
int y = y_min; y <= y_max; ++y) {
473 for (
int x = x_min; x <= x_max; ++x) {
474 uint16_t current_tile = overworld.
GetTile(x, y);
475 if (current_tile == old_tile) {
482 changes.push_back(change);
487 if (changes.empty()) {
488 std::ostringstream oss;
489 oss <<
"0x" << std::hex << old_tile;
490 return absl::NotFoundError(absl::StrCat(
"No tiles matching ", oss.str(),
491 " found in specified area"));
498 const std::string& prompt,
const std::vector<std::string>& commands,
499 const std::string& ai_service,
Rom* rom) {
504 proposal.
created_at = std::chrono::system_clock::now();
508 for (
const auto& command : commands) {
510 if (command.empty() || command[0] ==
'#') {
515 if (absl::StrContains(command,
"overworld set-tile")) {
517 if (change_or.ok()) {
518 proposal.
changes.push_back(change_or.value());
520 return change_or.status();
522 }
else if (absl::StrContains(command,
"overworld set-area")) {
524 if (changes_or.ok()) {
526 changes_or.value().begin(),
527 changes_or.value().end());
529 return changes_or.status();
531 }
else if (absl::StrContains(command,
"overworld replace-tile")) {
533 if (changes_or.ok()) {
535 changes_or.value().begin(),
536 changes_or.value().end());
538 return changes_or.status();
543 if (proposal.
changes.empty()) {
544 return absl::InvalidArgumentError(
545 "No valid tile16 changes found in commands");
549 " tile16 changes from prompt");
557 return absl::FailedPreconditionError(
"ROM not loaded");
561 auto status = overworld.
Load(rom);
567 for (
const auto& change : proposal.
changes) {
569 if (change.map_id < 0x40) {
571 }
else if (change.map_id < 0x80) {
578 overworld.
SetTile(change.x, change.y, change.new_tile);
584 return absl::OkStatus();
589 if (!before_rom || !before_rom->
is_loaded()) {
590 return absl::FailedPreconditionError(
"Before ROM not loaded");
593 if (!after_rom || !after_rom->
is_loaded()) {
594 return absl::FailedPreconditionError(
"After ROM not loaded");
597 if (proposal.
changes.empty()) {
598 return absl::InvalidArgumentError(
"No changes to visualize");
602 int min_x = INT_MAX, min_y = INT_MAX;
603 int max_x = INT_MIN, max_y = INT_MIN;
604 int map_id = proposal.
changes[0].map_id;
606 for (
const auto& change : proposal.
changes) {
607 if (change.x < min_x)
609 if (change.y < min_y)
611 if (change.x > max_x)
613 if (change.y > max_y)
619 min_x = std::max(0, min_x - padding);
620 min_y = std::max(0, min_y - padding);
621 max_x = std::min(31, max_x + padding);
622 max_y = std::min(31, max_y + padding);
624 int width = (max_x - min_x + 1) * 16;
625 int height = (max_y - min_y + 1) * 16;
628 int diff_width = width * 2 + 8;
629 int diff_height = height;
631 std::vector<uint8_t> diff_data(diff_width * diff_height, 0x00);
632 gfx::Bitmap diff_bitmap(diff_width, diff_height, 8, diff_data);
638 auto before_status = before_overworld.
Load(before_rom);
639 if (!before_status.ok()) {
640 return before_status;
643 auto after_status = after_overworld.
Load(after_rom);
644 if (!after_status.ok()) {
652 }
else if (map_id < 0x80) {
669 for (
int y = min_y; y <= max_y; ++y) {
670 for (
int x = min_x; x <= max_x; ++x) {
671 uint16_t before_tile = before_overworld.
GetTile(x, y);
672 uint16_t after_tile = after_overworld.
GetTile(x, y);
674 bool is_changed = (before_tile != after_tile);
678 int pixel_x = (x - min_x) * 16;
679 int pixel_y = (y - min_y) * 16;
680 for (
int py = 0; py < 16; ++py) {
681 for (
int px = 0; px < 16; ++px) {
682 diff_bitmap.
SetPixel(pixel_x + px, pixel_y + py, color);
687 int right_offset = width + 8;
688 for (
int py = 0; py < 16; ++py) {
689 for (
int px = 0; px < 16; ++px) {
690 diff_bitmap.
SetPixel(right_offset + pixel_x + px, pixel_y + py,
698 for (
int y = 0; y < diff_height; ++y) {
699 for (
int x = 0; x < 8; ++x) {
700 diff_bitmap.
SetPixel(width + x, y, separator_color);
709 std::ofstream file(path);
710 if (!file.is_open()) {
711 return absl::InvalidArgumentError(
712 absl::StrCat(
"Failed to open file for writing: ", path));
715 file << proposal.
ToJson();
718 return absl::OkStatus();
722 const std::string& path) {
723 std::ifstream file(path);
724 if (!file.is_open()) {
725 return absl::InvalidArgumentError(
726 absl::StrCat(
"Failed to open file for reading: ", path));
729 std::stringstream buffer;
730 buffer << file.rdbuf();
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
std::string GenerateProposalId() const
Generate a unique proposal ID.
absl::StatusOr< Tile16Proposal > GenerateFromCommands(const std::string &prompt, const std::vector< std::string > &commands, const std::string &ai_service, Rom *rom)
Generate a tile16 proposal from an AI-generated command list.
absl::StatusOr< Tile16Proposal > LoadProposal(const std::string &path)
Load a proposal from a JSON file.
absl::StatusOr< std::vector< Tile16Change > > ParseReplaceTileCommand(const std::string &command, Rom *rom)
Parse a "overworld replace-tile" command into multiple Tile16Changes.
absl::StatusOr< gfx::Bitmap > GenerateDiff(const Tile16Proposal &proposal, Rom *before_rom, Rom *after_rom)
Generate a visual diff bitmap for a proposal.
absl::Status ApplyProposal(const Tile16Proposal &proposal, Rom *rom)
Apply a proposal to a ROM (typically a sandbox).
absl::Status SaveProposal(const Tile16Proposal &proposal, const std::string &path)
Save a proposal to a JSON file for later review.
absl::StatusOr< std::vector< Tile16Change > > ParseSetAreaCommand(const std::string &command, Rom *rom)
Parse a "overworld set-area" command into multiple Tile16Changes.
absl::StatusOr< Tile16Change > ParseSetTileCommand(const std::string &command, Rom *rom)
Parse a single "overworld set-tile" command into a Tile16Change.
Represents a bitmap image optimized for SNES ROM hacking.
void SetPixel(int x, int y, const SnesColor &color)
Set a pixel at the given x,y coordinates with SNES color.
Represents the full Overworld data, light and dark world.
void set_current_world(int world)
absl::Status Load(Rom *rom)
Load all overworld data from ROM.
uint16_t GetTile(int x, int y) const
void SetTile(int x, int y, uint16_t tile_id)
#define ASSIGN_OR_RETURN(type_variable_name, expression)
absl::StatusOr< uint16_t > ParseTileValue(const nlohmann::json &json, const char *field)
Represents a single tile16 change in a proposal.
std::string ToString() const
Represents a proposal for tile16 edits on the overworld.
std::chrono::system_clock::time_point created_at
std::vector< Tile16Change > changes
std::string ToJson() const
static absl::StatusOr< Tile16Proposal > FromJson(const std::string &json)