From 4244ebd8c52c226650921ae351c4d1e826a33576 Mon Sep 17 00:00:00 2001 From: NovaRain Date: Mon, 29 Mar 2021 11:54:56 +0800 Subject: [PATCH] Improved the implementation of tile_num_beyond function --- sfall/Game/tilemap.cpp | 38 ++++++-------------------------------- 1 file changed, 6 insertions(+), 32 deletions(-) diff --git a/sfall/Game/tilemap.cpp b/sfall/Game/tilemap.cpp index 7b04138d..57812f32 100644 --- a/sfall/Game/tilemap.cpp +++ b/sfall/Game/tilemap.cpp @@ -21,7 +21,8 @@ static bool TileExists(long tile) { } // 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) { 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 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; 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); if (tile != lastTile) { 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); } 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); if (tile != lastTile) { 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); } lastTile = tile;