From 1062e138d628d95b7dcde4108ffb5dff92616742 Mon Sep 17 00:00:00 2001 From: doyle Date: Sat, 23 Sep 2023 17:38:10 +1000 Subject: [PATCH] fp: Improve the A* situation, but its not quite fixed yet --- feely_pona_game.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/feely_pona_game.cpp b/feely_pona_game.cpp index 06f8e0c..b56a9fd 100644 --- a/feely_pona_game.cpp +++ b/feely_pona_game.cpp @@ -481,7 +481,8 @@ static Dqn_Slice FP_Game_AStarPathFind(FP_Game *game, DQN_DEFER { Dqn_DSMap_Deinit(&astar_info); }; // NOTE: Enumerate the entities that are collidable ============================================ - auto zone_enum_collidables = Dqn_Profiler_BeginZoneWithIndex(DQN_STRING8("FP_Update: A* enumerate collidables"), FP_ProfileZone_FPUpdate_AStarEnumerateCollidables); + bool dest_tile_is_non_traversable = false; + auto zone_enum_collidables = Dqn_Profiler_BeginZoneWithIndex(DQN_STRING8("FP_Update: A* enumerate collidables"), FP_ProfileZone_FPUpdate_AStarEnumerateCollidables); for (FP_GameEntityIterator it = {}; FP_Game_DFSPreOrderWalkEntityTree(game, &it, game->root_entity); ) { FP_GameEntity const *walk_entity = it.entity; if (entity == walk_entity->handle) @@ -502,6 +503,9 @@ static Dqn_Slice FP_Game_AStarPathFind(FP_Game *game, FP_GameAStarNode *node = Dqn_DSMap_MakeKeyU64(&astar_info, tile_u64).value; node->non_traversable = true; node->tile = Dqn_V2I_InitNx2(x, y); + + if (node->tile == dest_tile) + dest_tile_is_non_traversable = true; } } } @@ -556,6 +560,16 @@ static Dqn_Slice FP_Game_AStarPathFind(FP_Game *game, Dqn_DSMapResult next_cost_result = Dqn_DSMap_MakeKeyU64(&astar_info, next_tile_u64); next_cost_result.value->tile = next_tile; + // NOTE: We got as close as possible, we know it's impossible to + // reach, we will stop here. + // TODO(doyle): Doesn't work in the general case, what if there's a + // 2 consecutive blocks that are not traversable, we will never + // realise that + if (dest_tile == next_tile && dest_tile_is_non_traversable) { + Dqn_FArray_Clear(&frontier); + break; + } + if (next_cost_result.value->non_traversable) continue; @@ -599,6 +613,7 @@ static Dqn_Slice FP_Game_AStarPathFind(FP_Game *game, } Dqn_Profiler_EndZone(zone_astar_expand); + #if 0 TELY_Renderer *renderer = &platform->renderer; for (uint32_t old_index = 1 /*Sentinel*/; old_index < astar_info.occupied; old_index++) { Dqn_DSMapSlot const *slot = astar_info.slots + old_index; @@ -606,6 +621,7 @@ static Dqn_Slice FP_Game_AStarPathFind(FP_Game *game, Dqn_V2 pos = FP_Game_TilePosToWorldPos(game, node->tile) + (game->tile_size * .5f); TELY_Render_CircleColourV4(renderer, pos, 4.f, TELY_RenderShapeMode_Fill, TELY_COLOUR_BLUE_CADET_V4); } + #endif Dqn_usize slice_size = 0; for (Dqn_V2I it = last_successful_tile; it != src_tile; slice_size++) {