Core: Polymorphism works better then expected hmm...

This commit is contained in:
Krzosa Karol
2023-04-22 09:15:34 +02:00
parent 4657b4460e
commit 8c0b4439af
3 changed files with 52 additions and 67 deletions

View File

@@ -12,17 +12,17 @@ Map :: struct
data: *Tile
x: int
y: int
actors: Array(Actor)
actors: A.Array(Actor)
Actor :: struct
p: V2I
target_p: V2I
map: *Map
open_paths: Array(Path)
close_paths: Array(Path)
tiles_visited: Array(V2I)
history: Array(Path)
open_paths: A.Array(Path)
close_paths: A.Array(Path)
tiles_visited: A.Array(V2I)
history: A.Array(Path)
Path :: struct
value_to_sort_by: int // distance from target
@@ -44,10 +44,10 @@ ScreenToMap :: (p: Vector2): V2I
return result
AddActor :: (map: *Map, p: V2I): *Actor
Add(&map.actors, {p, p, map})
A.Add(&map.actors, {p, p, map})
Assert(map.data[p.x + p.y * map.x] == 0)
map.data[p.x + p.y * map.x] |= TILE_ACTOR_IS_STANDING
actor := GetLast(&CurrentMap.actors)
actor := A.GetLast(&CurrentMap.actors)
return actor
ActorSetP :: (actor: *Actor, p: V2I)
@@ -62,17 +62,17 @@ ActorSetP :: (actor: *Actor, p: V2I)
*new_tile |= TILE_ACTOR_IS_STANDING
actor.p = p
Reset(&actor.tiles_visited)
Reset(&actor.history)
Reset(&actor.open_paths)
Reset(&actor.close_paths)
A.Reset(&actor.tiles_visited)
A.Reset(&actor.history)
A.Reset(&actor.open_paths)
A.Reset(&actor.close_paths)
SetTargetP :: (s: *Actor, p: V2I)
s.target_p = p
Reset(&s.tiles_visited)
Reset(&s.history)
Reset(&s.open_paths)
Reset(&s.close_paths)
A.Reset(&s.tiles_visited)
A.Reset(&s.history)
A.Reset(&s.open_paths)
A.Reset(&s.close_paths)
GetRandomP :: (m: *Map): V2I
result: V2I = {GetRandomValue(0, CurrentMap.x - 1), GetRandomValue(0, CurrentMap.y - 1)}
@@ -124,7 +124,7 @@ InsertOpenPath :: (s: *Actor, p: V2I, came_from: V2I, ignore_blocks: bool = fals
dx := s.target_p.x - p.x
dy := s.target_p.y - p.y
d := dx*dx + dy*dy
InsertSortedDecreasing(&s.open_paths, {d, p, came_from})
A.InsertSortedDecreasing(&s.open_paths, {d, p, came_from})
GetCloseP :: (s: *Actor, p: V2I): *Path
for i := 0, i < s.close_paths.len, i += 1
@@ -136,25 +136,25 @@ GetCloseP :: (s: *Actor, p: V2I): *Path
RecomputeHistory :: (s: *Actor)
if s.close_paths.len > 1
Reset(&s.history)
it := GetLast(&s.close_paths)
Add(&s.history, *it)
A.Reset(&s.history)
it := A.GetLast(&s.close_paths)
A.Add(&s.history, *it)
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)
A.Reset(&s.history)
break
it = GetCloseP(s, it.came_from)
Add(&s.history, *it)
Pop(&s.history)
A.Add(&s.history, *it)
A.Pop(&s.history)
MoveTowardsTarget :: (s: *Actor)
tile := s.map.data + s.p.x + s.p.y * s.map.x
if s.history.len > 0
step := Pop(&s.history)
step := A.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)
A.Add(&s.tiles_visited, s.p)
s.p = step.p
*tile &= ~TILE_ACTOR_IS_STANDING
*new_tile |= TILE_ACTOR_IS_STANDING
@@ -167,9 +167,9 @@ PathFindUpdate :: (map: *Map)
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)
A.Reset(&s.open_paths)
A.Reset(&s.close_paths)
A.Reset(&s.history)
break
PathFind(s)
@@ -178,23 +178,23 @@ PathFindStep :: (s: *Actor, compute_history: bool = true): bool
if s.open_paths.len == 0
// Reset if we didnt find solution
if s.close_paths.len != 0
last := GetLast(&s.close_paths)
last := A.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)
A.Reset(&s.open_paths)
A.Reset(&s.close_paths)
A.Reset(&s.history)
InsertOpenPath(s, s.p, s.p, ignore_blocks = true)
if s.close_paths.len != 0
last := GetLast(&s.close_paths)
last := A.GetLast(&s.close_paths)
reached_target := last.p.x == s.target_p.x && last.p.y == s.target_p.y
if reached_target
return true
it := Pop(&s.open_paths)
Add(&s.close_paths, it)
it := A.Pop(&s.open_paths)
A.Add(&s.close_paths, it)
for y := -1, y <= 1, y += 1