RTS: Bulletproof algorithm

This commit is contained in:
Krzosa Karol
2023-04-19 16:16:11 +02:00
parent 2d5c2d89bc
commit 21d97a4397
3 changed files with 17 additions and 10 deletions

View File

@@ -112,7 +112,7 @@ main :: (): int
brown := BROWN brown := BROWN
brown.a = 255/2 brown.a = 255/2
actor_color := GREEN actor_color := DARKGREEN
actor_color.a = 255/2 actor_color.a = 255/2
past_actor_color := BLUE past_actor_color := BLUE
@@ -168,8 +168,7 @@ main :: (): int
color := RAYWHITE color := RAYWHITE
if *it == 1 ;; color = GRAY if *it == 1 ;; color = GRAY
if colliding == true if colliding == true
color = MAROON color = Color{a = 100}
color.a = 255/2
DrawRectangleRec(r2, color) DrawRectangleRec(r2, color)
@@ -214,7 +213,7 @@ main :: (): int
target_r := Rectangle{actor_it.target_p.x->F32 * RectX, actor_it.target_p.y->F32 * RectY, RectX, RectY} target_r := Rectangle{actor_it.target_p.x->F32 * RectX, actor_it.target_p.y->F32 * RectY, RectX, RectY}
main_p := Vector2{actor_it.p.x->F32 * RectX + RectX/2, actor_it.p.y->F32 * RectY + RectY/2} main_p := Vector2{actor_it.p.x->F32 * RectX + RectX/2, actor_it.p.y->F32 * RectY + RectY/2}
DrawCircleV(main_p, RectX/3, actor_color) DrawCircleV(main_p, RectX/2, actor_color)
// DrawRectangleRec(r, actor_color) // DrawRectangleRec(r, actor_color)
DrawRectangleRec(target_r, target_color) DrawRectangleRec(target_r, target_color)
@@ -222,7 +221,7 @@ main :: (): int
for tile_i := actor_it.tiles_visited.len - 1, tile_i >= 0, tile_i -= 1 for tile_i := actor_it.tiles_visited.len - 1, tile_i >= 0, tile_i -= 1
tile_it := actor_it.tiles_visited.data + tile_i tile_it := actor_it.tiles_visited.data + tile_i
p := Vector2{tile_it.x->F32 * RectX + RectX/2, tile_it.y->F32 * RectY + RectY/2} p := Vector2{tile_it.x->F32 * RectX + RectX/2, tile_it.y->F32 * RectY + RectY/2}
DrawCircleV(p, RectX/3 - smaller_the_further, past_actor_color) DrawCircleV(p, RectX/2 - smaller_the_further, past_actor_color)
smaller_the_further += 0.3 smaller_the_further += 0.3
for path_i := 0, path_i < actor_it.open_paths.len, path_i += 1 for path_i := 0, path_i < actor_it.open_paths.len, path_i += 1

View File

@@ -19,7 +19,6 @@ MAP_Actor :: struct
target_p: V2I target_p: V2I
map: *MAP_Map map: *MAP_Map
state: int
open_paths: Array(MAP_Path) open_paths: Array(MAP_Path)
close_paths: Array(MAP_Path) close_paths: Array(MAP_Path)
tiles_visited: Array(V2I) tiles_visited: Array(V2I)
@@ -49,6 +48,11 @@ MAP_ActorSetP :: (actor: *MAP_Actor, p: V2I)
*new_tile |= MAP_TILE_ACTOR_IS_STANDING *new_tile |= MAP_TILE_ACTOR_IS_STANDING
actor.p = p actor.p = p
Reset(&actor.tiles_visited)
Reset(&actor.history)
Reset(&actor.open_paths)
Reset(&actor.close_paths)
MAP_GetRandomP :: (m: *MAP_Map): V2I MAP_GetRandomP :: (m: *MAP_Map): V2I
result := V2I{GetRandomValue(0, MAP_CurrentMap.x), GetRandomValue(0, MAP_CurrentMap.y)} result := V2I{GetRandomValue(0, MAP_CurrentMap.x), GetRandomValue(0, MAP_CurrentMap.y)}
return result return result
@@ -137,22 +141,25 @@ MAP_MoveTowardsTarget :: (s: *MAP_Actor)
Assert(*new_tile == 0) Assert(*new_tile == 0)
*new_tile |= MAP_TILE_ACTOR_IS_STANDING *new_tile |= MAP_TILE_ACTOR_IS_STANDING
s.state = MAP_PATH_SEARCHING
Reset(&s.open_paths) Reset(&s.open_paths)
Reset(&s.close_paths) Reset(&s.close_paths)
Reset(&s.history) Reset(&s.history)
MAP_PathFindStep :: (s: *MAP_Actor) MAP_PathFindStep :: (s: *MAP_Actor)
if s.state == MAP_PATH_REACHED ;; return
if s.open_paths.len == 0 if s.open_paths.len == 0
MAP_InsertOpenPath(s, s.p, s.p, ignore_blocks = true) MAP_InsertOpenPath(s, s.p, s.p, ignore_blocks = true)
if s.close_paths.len != 0
last := GetLast(&s.close_paths)
reached_target := last.p.x == s.target_p.x && last.p.y == s.target_p.y
if reached_target
return
it := Pop(&s.open_paths) it := Pop(&s.open_paths)
Add(&s.close_paths, it) Add(&s.close_paths, it)
reached_target := it.p.x == s.target_p.x && it.p.y == s.target_p.y reached_target := it.p.x == s.target_p.x && it.p.y == s.target_p.y
if reached_target if reached_target
s.state = MAP_PATH_REACHED
return return
// @language_todo: Add continue keyword // @language_todo: Add continue keyword

View File

@@ -1 +1,2 @@
cloc *.hpp *.cpp *.py *.h cloc *.hpp *.cpp *.py *.h
git rev-list --all --count