Improved the implementation of tile_num_beyond function

This commit is contained in:
NovaRain
2021-03-29 11:54:56 +08:00
parent b37b5a2e76
commit 4244ebd8c5
+6 -32
View File
@@ -21,7 +21,8 @@ static bool TileExists(long tile) {
} }
// Fixed and improved implementation of tile_num_beyond_ engine function // Fixed and improved implementation of tile_num_beyond_ engine function
// compared to the original implementation, this function gets the hex (tile) from the constructed line more correctly // - correctly gets the tile from the constructed line
// - fixed the range when the target tile was positioned at the maximum distance
long __fastcall Tilemap::tile_num_beyond(long sourceTile, long targetTile, long maxRange) { long __fastcall Tilemap::tile_num_beyond(long sourceTile, long targetTile, long maxRange) {
if (maxRange <= 0 || sourceTile == targetTile) return sourceTile; if (maxRange <= 0 || sourceTile == targetTile) return sourceTile;
@@ -58,35 +59,6 @@ long __fastcall Tilemap::tile_num_beyond(long sourceTile, long targetTile, long
long diffX_x2 = 2 * std::abs(diffX); long diffX_x2 = 2 * std::abs(diffX);
long diffY_x2 = 2 * std::abs(diffY); long diffY_x2 = 2 * std::abs(diffY);
// Shift the starting point depending on the direction of the line building
// to reduce the inaccuracy when getting the tile from the x/y coordinates
// TODO: find a better way without having to shift the point
long direction = (source_X != target_X) ? fo::func::tile_dir(sourceTile, targetTile) : -1;
//fo::func::debug_printf("\ntile_dir: %d", direction);
switch (direction) {
case 0:
target_X += 8;
target_Y -= 4;
break;
case 1:
target_X += 15;
break;
case 2:
target_X += 8;
target_Y += 4;
break;
case 3:
target_X -= 8;
target_Y += 4;
break;
case 4:
target_X -= 15;
break;
case 5:
target_X -= 8;
target_Y -= 4;
break;
}
const int step = 4; const int step = 4;
long stepCounter = step - 1; long stepCounter = step - 1;
@@ -98,7 +70,8 @@ long __fastcall Tilemap::tile_num_beyond(long sourceTile, long targetTile, long
//fo::func::debug_printf("\ntile_num: %d [x:%d y:%d]", tile, target_X, target_Y); //fo::func::debug_printf("\ntile_num: %d [x:%d y:%d]", tile, target_X, target_Y);
if (tile != lastTile) { if (tile != lastTile) {
if (!TileExists(tile)) { if (!TileExists(tile)) {
if (++currentRange >= maxRange || fo::func::tile_on_edge(tile)) return tile; long dist = fo::func::tile_dist(targetTile, tile);
if ((dist + currentRange) >= maxRange || fo::func::tile_on_edge(tile)) return tile;
buildLineTiles.push_back(tile); buildLineTiles.push_back(tile);
} }
lastTile = tile; lastTile = tile;
@@ -131,7 +104,8 @@ long __fastcall Tilemap::tile_num_beyond(long sourceTile, long targetTile, long
//fo::func::debug_printf("\ntile_num: %d [x:%d y:%d]", tile, target_X, target_Y); //fo::func::debug_printf("\ntile_num: %d [x:%d y:%d]", tile, target_X, target_Y);
if (tile != lastTile) { if (tile != lastTile) {
if (!TileExists(tile)) { if (!TileExists(tile)) {
if (++currentRange >= maxRange || fo::func::tile_on_edge(tile)) return tile; long dist = fo::func::tile_dist(targetTile, tile);
if ((dist + currentRange) >= maxRange || fo::func::tile_on_edge(tile)) return tile;
buildLineTiles.push_back(tile); buildLineTiles.push_back(tile);
} }
lastTile = tile; lastTile = tile;