RTS: Bulletproof algorithm
This commit is contained in:
@@ -112,7 +112,7 @@ main :: (): int
|
||||
brown := BROWN
|
||||
brown.a = 255/2
|
||||
|
||||
actor_color := GREEN
|
||||
actor_color := DARKGREEN
|
||||
actor_color.a = 255/2
|
||||
|
||||
past_actor_color := BLUE
|
||||
@@ -168,8 +168,7 @@ main :: (): int
|
||||
color := RAYWHITE
|
||||
if *it == 1 ;; color = GRAY
|
||||
if colliding == true
|
||||
color = MAROON
|
||||
color.a = 255/2
|
||||
color = Color{a = 100}
|
||||
|
||||
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}
|
||||
|
||||
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(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
|
||||
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}
|
||||
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
|
||||
|
||||
for path_i := 0, path_i < actor_it.open_paths.len, path_i += 1
|
||||
|
||||
@@ -19,7 +19,6 @@ MAP_Actor :: struct
|
||||
target_p: V2I
|
||||
map: *MAP_Map
|
||||
|
||||
state: int
|
||||
open_paths: Array(MAP_Path)
|
||||
close_paths: Array(MAP_Path)
|
||||
tiles_visited: Array(V2I)
|
||||
@@ -49,6 +48,11 @@ MAP_ActorSetP :: (actor: *MAP_Actor, p: V2I)
|
||||
*new_tile |= MAP_TILE_ACTOR_IS_STANDING
|
||||
actor.p = p
|
||||
|
||||
Reset(&actor.tiles_visited)
|
||||
Reset(&actor.history)
|
||||
Reset(&actor.open_paths)
|
||||
Reset(&actor.close_paths)
|
||||
|
||||
MAP_GetRandomP :: (m: *MAP_Map): V2I
|
||||
result := V2I{GetRandomValue(0, MAP_CurrentMap.x), GetRandomValue(0, MAP_CurrentMap.y)}
|
||||
return result
|
||||
@@ -137,22 +141,25 @@ MAP_MoveTowardsTarget :: (s: *MAP_Actor)
|
||||
Assert(*new_tile == 0)
|
||||
*new_tile |= MAP_TILE_ACTOR_IS_STANDING
|
||||
|
||||
s.state = MAP_PATH_SEARCHING
|
||||
Reset(&s.open_paths)
|
||||
Reset(&s.close_paths)
|
||||
Reset(&s.history)
|
||||
|
||||
MAP_PathFindStep :: (s: *MAP_Actor)
|
||||
if s.state == MAP_PATH_REACHED ;; return
|
||||
if s.open_paths.len == 0
|
||||
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)
|
||||
Add(&s.close_paths, it)
|
||||
|
||||
reached_target := it.p.x == s.target_p.x && it.p.y == s.target_p.y
|
||||
if reached_target
|
||||
s.state = MAP_PATH_REACHED
|
||||
return
|
||||
|
||||
// @language_todo: Add continue keyword
|
||||
|
||||
Reference in New Issue
Block a user