RTS: Incremental path finding, Selection box, refactor

This commit is contained in:
Krzosa Karol
2023-04-20 13:36:50 +02:00
parent 21d97a4397
commit 7037416ea7
2 changed files with 110 additions and 66 deletions

View File

@@ -29,6 +29,14 @@ MAP_Path :: struct
p: V2I
came_from: V2I
MAP_Rectangle :: (p: V2I): Rectangle
result := Rectangle{p.x->F32 * RectX, p.y->F32 * RectY, RectX, RectY}
return result
MAP_Circle :: (p: V2I): Vector2
result := Vector2{p.x->F32 * RectX + RectX/2, p.y->F32 * RectY + RectY/2}
return result
MAP_AddActor :: (map: *MAP_Map, p: V2I): *MAP_Actor
Add(&map.actors, {p, p, map})
Assert(map.data[p.x + p.y * map.x] == 0)
@@ -82,10 +90,10 @@ MAP_RandomizeActors :: ()
map := &MAP_CurrentMap
for i := 0, i < map.actors.len, i += 1
it := map.actors.data + i
it.p = MAP_GetRandomUnblockedP(&MAP_CurrentMap)
p := MAP_GetRandomUnblockedP(&MAP_CurrentMap)
MAP_ActorSetP(it, p)
it.target_p = MAP_GetRandomUnblockedP(&MAP_CurrentMap)
MAP_InsertOpenPath :: (s: *MAP_Actor, p: V2I, came_from: V2I, ignore_blocks: bool = false)
if p.x < 0 || p.x >= s.map.x ;; return
if p.y < 0 || p.y >= s.map.y ;; return
@@ -99,7 +107,6 @@ MAP_InsertOpenPath :: (s: *MAP_Actor, p: V2I, came_from: V2I, ignore_blocks: boo
it := s.open_paths.data + i
if it.p.x == p.x && it.p.y == p.y ;; return
dx := s.target_p.x - p.x
dy := s.target_p.y - p.y
d := dx*dx + dy*dy
@@ -110,7 +117,7 @@ MAP_GetCloseP :: (s: *MAP_Actor, p: V2I): *MAP_Path
it := s.close_paths.data + i
if it.p.x == p.x && it.p.y == p.y ;; return it
Assert(false)
Assert(false, "Invalid codepath")
return 0
MAP_RecomputeHistory :: (s: *MAP_Actor)
@@ -118,35 +125,51 @@ MAP_RecomputeHistory :: (s: *MAP_Actor)
Reset(&s.history)
it := GetLast(&s.close_paths)
Add(&s.history, *it)
for !(it.p.x == s.p.x && it.p.y == s.p.y)
Pop(&s.history)
for i := 0,,i += 1
if it.p.x == s.p.x && it.p.y == s.p.y ;; break
if i > 512
Reset(&s.history)
break
it = MAP_GetCloseP(s, it.came_from)
Add(&s.history, *it)
Pop(&s.history)
MAP_MoveTowardsTarget :: (s: *MAP_Actor)
tile := s.map.data + s.p.x + s.p.y * s.map.x
Assert((*tile & MAP_TILE_ACTOR_IS_STANDING) != 0)
Assert((*tile & MAP_TILE_BLOCKER) == 0)
if s.history.len > 0
step := Pop(&s.history)
new_tile := s.map.data + step.p.x + step.p.y * s.map.x
if *new_tile == 0
Add(&s.tiles_visited, s.p)
s.p = step.p
*tile &= ~MAP_TILE_ACTOR_IS_STANDING
*new_tile |= MAP_TILE_ACTOR_IS_STANDING
MAP_RecomputeHistory(s)
if s.history.len > 1
*tile &= ~MAP_TILE_ACTOR_IS_STANDING
Assert((*tile & MAP_TILE_ACTOR_IS_STANDING) == 0)
MAP_PathFindUpdate :: (map: *MAP_Map)
for actor_i := 0, actor_i < map.actors.len, actor_i += 1
s := map.actors.data + actor_i
for i := 0, i < s.history.len, i += 1
it := s.history.data + i
Add(&s.tiles_visited, s.p)
step := s.history.data[s.history.len-2]
s.p = step.p
new_tile := s.map.data + s.p.x + s.p.y * s.map.x
Assert(*new_tile == 0)
*new_tile |= MAP_TILE_ACTOR_IS_STANDING
Reset(&s.open_paths)
Reset(&s.close_paths)
Reset(&s.history)
tile := s.map.data[it.p.x + it.p.y * s.map.x]
if tile != 0
Reset(&s.open_paths)
Reset(&s.close_paths)
Reset(&s.history)
break
MAP_PathFindStep :: (s: *MAP_Actor)
if s.open_paths.len == 0
// Reset if we didnt find solution
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 == false
Reset(&s.open_paths)
Reset(&s.close_paths)
Reset(&s.history)
MAP_InsertOpenPath(s, s.p, s.p, ignore_blocks = true)
if s.close_paths.len != 0
@@ -158,9 +181,6 @@ MAP_PathFindStep :: (s: *MAP_Actor)
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
return
// @language_todo: Add continue keyword
// if x == 0 && y == 0 ;; continue
@@ -170,3 +190,5 @@ MAP_PathFindStep :: (s: *MAP_Actor)
if (x == 0 && y == 0) == false
p := V2I{it.p.x + x, it.p.y + y}
MAP_InsertOpenPath(s, p, it.p)
MAP_RecomputeHistory(s)