Skip to content

Commit

Permalink
GetInfo/TileID - Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghabry committed Jan 10, 2025
1 parent 0cc19c1 commit ecddd23
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4080,17 +4080,16 @@ bool Game_Interpreter::CommandManiacGetGameInfo(lcf::rpg::EventCommand const& co

int event_id;
int var = com.parameters[2];

switch (com.parameters[1]) {
case 0: // Get map size
Main_Data::game_variables->Set(var, Game_Map::GetTilesX());
Main_Data::game_variables->Set(var + 1, Game_Map::GetTilesY());
break;
case 1: { // Get tile info

var = com.parameters[7];

auto tile_layer = com.parameters[2]; // 0: Lower || 1: Upper
int32_t tile_layer = com.parameters[2]; // 0: Lower || 1: Upper
Rect tile_coords;

tile_coords.x = ValueOrVariableBitfield(com.parameters[0], 1, com.parameters[3]);
Expand All @@ -4101,7 +4100,7 @@ bool Game_Interpreter::CommandManiacGetGameInfo(lcf::rpg::EventCommand const& co
if (tile_coords.width <= 0 || tile_coords.height <= 0) return true;

auto tiles = Game_Map::GetTilesIdAt(tile_coords, tile_layer);

for (int i = 0; i < tile_coords.width * tile_coords.height; i++) {
Main_Data::game_variables->Set(var + i, tiles[i]);
}
Expand Down
9 changes: 5 additions & 4 deletions src/game_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1885,24 +1885,25 @@ void Game_Map::ReplaceTileAt(int x, int y, int new_id, int layer) {
layer_vec[pos] = static_cast<int16_t>(new_id);
}

int Game_Map::GetTileIdAt(int x, int y, int layer, bool chipIdOrIndex) {
int Game_Map::GetTileIdAt(int x, int y, int layer, bool chip_id_or_index) {
if (x < 0 || x >= map->width || y < 0 || y >= map->height) {
return 0; // Return 0 for out-of-bounds coordinates
}

auto pos = x + y * map->width;
auto& layer_vec = layer >= 1 ? map->upper_layer : map->lower_layer;

int tile_output = chipIdOrIndex ? layer_vec[pos] : ChipIdToIndex(layer_vec[pos]);
int tile_output = chip_id_or_index ? layer_vec[pos] : ChipIdToIndex(layer_vec[pos]);
if (layer >= 1) tile_output -= BLOCK_F_INDEX;

return tile_output;
}

std::vector<int> Game_Map::GetTilesIdAt(Rect coords, int layer, bool chipIdOrIndex) {
std::vector<int> Game_Map::GetTilesIdAt(Rect coords, int layer, bool chip_id_or_index) {
std::vector<int> tiles_collection;
for (int i = 0; i < coords.height; ++i) {
for (int j = 0; j < coords.width; ++j) {
tiles_collection.emplace_back(Game_Map::GetTileIdAt(coords.x + j, coords.y + i, layer, chipIdOrIndex));
tiles_collection.emplace_back(Game_Map::GetTileIdAt(coords.x + j, coords.y + i, layer, chip_id_or_index));
}
}
return tiles_collection;
Expand Down
4 changes: 2 additions & 2 deletions src/game_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,8 @@ namespace Game_Map {
int SubstituteUp(int old_id, int new_id);
void ReplaceTileAt(int x, int y, int new_id, int layer);

int GetTileIdAt(int x, int y, int layer, bool chipIdOrIndex = false);
std::vector<int> GetTilesIdAt(Rect coords, int layer, bool chipIdOrIndex = false);
int GetTileIdAt(int x, int y, int layer, bool chip_id_or_index = false);
std::vector<int> GetTilesIdAt(Rect coords, int layer, bool chip_id_or_index = false);

/**
* Checks if its possible to step onto the tile at (x,y)
Expand Down

0 comments on commit ecddd23

Please sign in to comment.