refactoring -> change_map_size

git-svn-id: svn://ammerhai.com/home/mike/pokemon_repo@12 24008968-59e6-ed4c-a10b-0b2c954b24ab
This commit is contained in:
mikeb 2021-03-02 16:13:32 +00:00
parent 63088db2a9
commit 00d9c03843
3 changed files with 44 additions and 133 deletions

Binary file not shown.

Binary file not shown.

View File

@ -172,153 +172,64 @@ void change_map_size(char direction, int amount) {
auto old_map_width = map_width;
auto old_map_height = map_height;
if (direction == 'W' || direction == 'E') {
int32 new_x_offset = 0;
int32 new_y_offset = 0;
int32 old_x_offset = 0;
int32 old_y_offset = 0;
int32 to_fill_width = map_width;
int32 to_fill_height = map_height;
int32 to_fill_x_offset = 0;
int32 to_fill_y_offset = 0;
if (direction == 'W') {
player.pos_x = player.pos_x + amount;
map_width += amount;
to_fill_width = amount;
if (amount < 0)
old_x_offset = -amount;
else
new_x_offset = amount;
}
if (direction == 'N' || direction == 'S') {
if (direction == 'N') {
player.pos_y = player.pos_y + amount;
map_height += amount;
to_fill_height = amount;
if (amount < 0)
old_y_offset = -amount;
else
new_y_offset = amount;
}
if (direction == 'E') {
map_width += amount;
to_fill_width = amount;
to_fill_x_offset = old_map_width;
}
if (direction == 'S') {
map_height += amount;
to_fill_height = amount;
to_fill_y_offset = old_map_height;
}
map_tiles = (Tile*)malloc(map_width * map_height * sizeof(Tile));
#if 0
switch (direction) {
case 'W': {
for (int y = 0; y < min(old_map_height, map_height); y++) {
for (int x = 0; x < min(old_map_width, map_width); x++) {
if (amount < 0) {
map_tiles[(y + 0) * map_width + x] = old_map[y * old_map_width + (x - amount)];
}
else {
map_tiles[(y + 0) * map_width + (x + amount)] = old_map[y * old_map_width + x];
}
}
}
for (int y = 0; y < map_height; y++) {
for (int x = 0; x < amount; x++) {
map_tiles[y * map_width + (x + 0)].type = 0;
}
}
player.pos_x = player.pos_x + amount;
log("Changed map size: WEST");
} break;
case 'E': {
for (int y = 0; y < min(old_map_height, map_height); y++) {
for (int x = 0; x < min(old_map_width, map_width); x++) {
map_tiles[(y + 0) * map_width + (x + 0)] = old_map[y * old_map_width + (x)];
}
}
for (int y = 0; y < map_height; y++) {
for (int x = 0; x < amount; x++) {
map_tiles[y * map_width + (x + old_map_width)].type = 0;
}
}
log("Changed map size: EAST");
} break;
case 'N': {
for (int y = 0; y < min(old_map_height, map_height); y++) {
for (int x = 0; x < min(old_map_width, map_width); x++) {
if (amount < 0) {
map_tiles[y * map_width + (x + 0)] = old_map[(y - amount) * old_map_width + x];
}
else {
map_tiles[(y + amount) * map_width + (x + 0)] = old_map[y * old_map_width + x];
}
}
}
for (int y = 0; y < amount; y++) {
for (int x = 0; x < map_width; x++) {
map_tiles[(y + 0) * map_width + x].type = 0;
}
}
player.pos_y = player.pos_y + amount;
log("Changed map size: NORTH");
} break;
case 'S': {
for (int y = 0; y < min(old_map_height, map_height); y++) {
for (int x = 0; x < min(old_map_width, map_width); x++) {
map_tiles[(y + 0) * map_width + (x + 0)] = old_map[y * old_map_width + x];
}
}
for (int y = 0; y < amount; y++) {
for (int x = 0; x < map_width; x++) {
map_tiles[(y + old_map_height) * map_width + x].type = 0;
}
}
log("Changed map size: SOUTH");
} break;
}
#endif
for (int y = 0; y < min(old_map_height, map_height); y++) {
for (int x = 0; x < min(old_map_width, map_width); x++) {
if (direction == 'W') {
if (amount < 0) {
map_tiles[(y + 0) * map_width + x] = old_map[y * old_map_width + (x - amount)];
}
else {
map_tiles[(y + 0) * map_width + (x + amount)] = old_map[y * old_map_width + x];
}
}
if (direction == 'E') {
map_tiles[(y + 0) * map_width + (x + 0)] = old_map[y * old_map_width + (x)];
}
map_tiles[(y + new_y_offset) * map_width + (x + new_x_offset)] = old_map[(y + old_y_offset) * old_map_width + (x + old_x_offset)];
}
}
for (int y = 0; y < map_height; y++) {
for (int x = 0; x < amount; x++) {
if (direction == 'W')
map_tiles[y * map_width + (x + 0)].type = 0;
if (direction == 'E')
map_tiles[y * map_width + (x + old_map_width)].type = 0;
for (int y = 0; y < to_fill_height; y++) {
for (int x = 0; x < to_fill_width; x++) {
map_tiles[(y + to_fill_y_offset) * map_width + (x + to_fill_x_offset)].type = 0;
}
}
if (direction == 'W')
player.pos_x = player.pos_x + amount;
for (int y = 0; y < min(old_map_height, map_height); y++) {
for (int x = 0; x < min(old_map_width, map_width); x++) {
if (direction == 'N') {
if (amount < 0) {
map_tiles[y * map_width + (x + 0)] = old_map[(y - amount) * old_map_width + x];
}
else {
map_tiles[(y + amount) * map_width + (x + 0)] = old_map[y * old_map_width + x];
}
}
if (direction == 'S') {
map_tiles[(y + 0) * map_width + (x + 0)] = old_map[y * old_map_width + x];
}
}
}
for (int y = 0; y < amount; y++) {
for (int x = 0; x < map_width; x++) {
if(direction == 'N')
map_tiles[(y + 0) * map_width + x].type = 0;
if(direction == 'S')
map_tiles[(y + old_map_height) * map_width + x].type = 0;
}
}
if (direction == 'N')
player.pos_y = player.pos_y + amount;
player.pos_x = clamp(0, player.pos_x, map_width - 1);
player.pos_y = clamp(0, player.pos_y, map_height - 1);