Skip to content

Commit edec538

Browse files
authored
Merge pull request #3306 from EasyRPG-NewFeatures/Primekick-RewriteMapPartial
Partially implement Maniacs Command 3015: RewriteMap
2 parents c256675 + a5c3aed commit edec538

File tree

9 files changed

+307
-30
lines changed

9 files changed

+307
-30
lines changed

src/game_interpreter.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4553,12 +4553,41 @@ bool Game_Interpreter::CommandManiacKeyInputProcEx(lcf::rpg::EventCommand const&
45534553
return true;
45544554
}
45554555

4556-
bool Game_Interpreter::CommandManiacRewriteMap(lcf::rpg::EventCommand const&) {
4556+
bool Game_Interpreter::CommandManiacRewriteMap(lcf::rpg::EventCommand const& com) {
45574557
if (!Player::IsPatchManiac()) {
45584558
return true;
45594559
}
45604560

4561-
Output::Warning("Maniac Patch: Command RewriteMap not supported");
4561+
int mode = com.parameters[0];
4562+
bool is_replace_range = com.parameters[1] != 0;
4563+
bool is_upper_layer = com.parameters[2] != 0;
4564+
4565+
int tile_index = ValueOrVariableBitfield(mode, 0, com.parameters[3]);
4566+
int x_start = ValueOrVariableBitfield(mode, 1, com.parameters[4]);
4567+
int y_start = ValueOrVariableBitfield(mode, 2, com.parameters[5]);
4568+
int width = ValueOrVariableBitfield(mode, 3, com.parameters[6]);
4569+
int height = ValueOrVariableBitfield(mode, 4, com.parameters[7]);
4570+
4571+
bool disable_autotile = com.parameters[8] != 0;
4572+
4573+
Scene_Map* scene = (Scene_Map*)Scene::Find(Scene::Map).get();
4574+
if (!scene)
4575+
return true;
4576+
4577+
if (is_upper_layer) {
4578+
for (auto y = y_start; y < y_start + height; ++y) {
4579+
for (auto x = x_start; x < x_start + width; ++x) {
4580+
scene->spriteset->ReplaceUpAt(x, y, tile_index);
4581+
}
4582+
}
4583+
} else {
4584+
for (auto y = y_start; y < y_start + height; ++y) {
4585+
for (auto x = x_start; x < x_start + width; ++x) {
4586+
scene->spriteset->ReplaceDownAt(x, y, tile_index, disable_autotile);
4587+
}
4588+
}
4589+
}
4590+
45624591
return true;
45634592
}
45644593

src/game_map.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,6 +1879,12 @@ int Game_Map::SubstituteUp(int old_id, int new_id) {
18791879
return DoSubstitute(map_info.upper_tiles, old_id, new_id);
18801880
}
18811881

1882+
void Game_Map::ReplaceTileAt(int x, int y, int new_id, int layer) {
1883+
auto pos = x + y * map->width;
1884+
auto& layer_vec = layer >= 1 ? map->upper_layer : map->lower_layer;
1885+
layer_vec[pos] = static_cast<int16_t>(new_id);
1886+
}
1887+
18821888
std::string Game_Map::ConstructMapName(int map_id, bool is_easyrpg) {
18831889
std::stringstream ss;
18841890
ss << "Map" << std::setfill('0') << std::setw(4) << map_id;

src/game_map.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ namespace Game_Map {
641641
Game_Vehicle* GetVehicle(Game_Vehicle::Type which);
642642
int SubstituteDown(int old_id, int new_id);
643643
int SubstituteUp(int old_id, int new_id);
644+
void ReplaceTileAt(int x, int y, int new_id, int layer);
644645

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

src/map_data.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ static constexpr int NUM_LOWER_TILES = BLOCK_F_INDEX;
5959
static constexpr int NUM_UPPER_TILES = BLOCK_F_TILES;
6060
static constexpr int NUM_TILES = NUM_LOWER_TILES + NUM_UPPER_TILES;
6161

62+
// Bit positions for neighbors
63+
static constexpr uint8_t NEIGHBOR_NW = 0x80; // 0b10000000
64+
static constexpr uint8_t NEIGHBOR_N = 0x40; // 0b01000000
65+
static constexpr uint8_t NEIGHBOR_NE = 0x20; // 0b00100000
66+
static constexpr uint8_t NEIGHBOR_W = 0x10; // 0b00010000
67+
static constexpr uint8_t NEIGHBOR_E = 0x08; // 0b00001000
68+
static constexpr uint8_t NEIGHBOR_SW = 0x04; // 0b00000100
69+
static constexpr uint8_t NEIGHBOR_S = 0x02; // 0b00000010
70+
static constexpr uint8_t NEIGHBOR_SE = 0x01; // 0b00000001
71+
72+
6273
/** Passability flags. */
6374
namespace Passable {
6475
enum Passable {
@@ -94,4 +105,26 @@ inline int ChipIdToIndex(int chip_id) {
94105
return 0;
95106
}
96107

108+
inline int IndexToChipId(int index) {
109+
if (index >= BLOCK_A_INDEX && index < BLOCK_B_INDEX) {
110+
return BLOCK_A + (index - BLOCK_A_INDEX) * BLOCK_A_STRIDE;
111+
}
112+
else if (index >= BLOCK_B_INDEX && index < BLOCK_C_INDEX) {
113+
return BLOCK_B + (index - BLOCK_B_INDEX) * BLOCK_B_STRIDE;
114+
}
115+
else if (index >= BLOCK_C_INDEX && index < BLOCK_D_INDEX) {
116+
return BLOCK_C + (index - BLOCK_C_INDEX) * BLOCK_C_STRIDE;
117+
}
118+
else if (index >= BLOCK_D_INDEX && index < BLOCK_E_INDEX) {
119+
return BLOCK_D + (index - BLOCK_D_INDEX) * BLOCK_D_STRIDE;
120+
}
121+
else if (index >= BLOCK_E_INDEX && index < BLOCK_F_INDEX) {
122+
return BLOCK_E + (index - BLOCK_E_INDEX) * BLOCK_E_STRIDE;
123+
}
124+
else if (index >= BLOCK_F_INDEX) {
125+
return BLOCK_F + (index - BLOCK_F_INDEX) * BLOCK_F_STRIDE;
126+
}
127+
return 0;
128+
}
129+
97130
#endif

src/spriteset_map.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "bitmap.h"
3131
#include "player.h"
3232
#include "drawable_list.h"
33+
#include "map_data.h"
3334

3435
Spriteset_Map::Spriteset_Map() {
3536
panorama = std::make_unique<Plane>();
@@ -173,6 +174,16 @@ void Spriteset_Map::SubstituteUp(int old_id, int new_id) {
173174
}
174175
}
175176

177+
void Spriteset_Map::ReplaceDownAt(int x, int y, int tile_index, bool disable_autotile) {
178+
auto tile_id = IndexToChipId(tile_index);
179+
tilemap->SetMapTileDataDownAt(x, y, tile_id, disable_autotile);
180+
}
181+
182+
void Spriteset_Map::ReplaceUpAt(int x, int y, int tile_index) {
183+
auto tile_id = IndexToChipId(tile_index);
184+
tilemap->SetMapTileDataUpAt(x, y, tile_id);
185+
}
186+
176187
bool Spriteset_Map::RequireClear(DrawableList& drawable_list) {
177188
if (drawable_list.empty()) {
178189
return true;

src/spriteset_map.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ class Spriteset_Map {
7171
*/
7272
void SubstituteUp(int old_id, int new_id);
7373

74+
/**
75+
* Replaces a single tile in lower layer at the given coordinates.
76+
*/
77+
void ReplaceDownAt(int x, int y, int tile_index, bool disable_autotile);
78+
79+
/**
80+
* Replaces a single tile in upper layer at the given coordinates.
81+
*/
82+
void ReplaceUpAt(int x, int y, int tile_index);
83+
7484
/**
7585
* @return true if we should clear the screen before drawing the map
7686
*/

src/tilemap.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ class Tilemap {
3636

3737
const std::vector<short>& GetMapDataDown() const;
3838
void SetMapDataDown(std::vector<short> down);
39+
void SetMapTileDataDownAt(int x, int y, int tile_id, bool disable_autotile);
3940

4041
const std::vector<short>& GetMapDataUp() const;
4142
void SetMapDataUp(std::vector<short> up);
43+
void SetMapTileDataUpAt(int x, int y, int tile_id);
4244

4345
const std::vector<unsigned char>& GetPassableUp() const;
4446
void SetPassableUp(std::vector<unsigned char> up);
@@ -81,6 +83,10 @@ inline void Tilemap::SetMapDataDown(std::vector<short> down) {
8183
layer_down.SetMapData(std::move(down));
8284
}
8385

86+
inline void Tilemap::SetMapTileDataDownAt(int x, int y, int tile_id, bool disable_autotile) {
87+
layer_down.SetMapTileDataAt(x, y, tile_id, disable_autotile);
88+
}
89+
8490
inline const std::vector<short>& Tilemap::GetMapDataUp() const {
8591
return layer_up.GetMapData();
8692
}
@@ -89,6 +95,10 @@ inline void Tilemap::SetMapDataUp(std::vector<short> up) {
8995
layer_up.SetMapData(std::move(up));
9096
}
9197

98+
inline void Tilemap::SetMapTileDataUpAt(int x, int y, int tile_id) {
99+
layer_down.SetMapTileDataAt(x, y, tile_id, true);
100+
}
101+
92102
inline const std::vector<unsigned char>& Tilemap::GetPassableDown() const {
93103
return layer_down.GetPassable();
94104
}

0 commit comments

Comments
 (0)