Core: Brought back named loads which are a bit counter intuitive
This commit is contained in:
@@ -1,65 +1,65 @@
|
||||
MAP_CurrentMap: MAP_Map
|
||||
CurrentMap: Map
|
||||
|
||||
MAP_PATH_SEARCHING :: 0
|
||||
MAP_PATH_REACHED :: 1
|
||||
MAP_PATH_UNREACHABLE :: 2
|
||||
PATH_SEARCHING :: 0
|
||||
PATH_REACHED :: 1
|
||||
PATH_UNREACHABLE :: 2
|
||||
|
||||
MAP_TILE_BLOCKER :: 1
|
||||
MAP_TILE_ACTOR_IS_STANDING :: 2
|
||||
TILE_BLOCKER :: 1
|
||||
TILE_ACTOR_IS_STANDING :: 2
|
||||
|
||||
MAP_Tile :: int
|
||||
MAP_Map :: struct
|
||||
data: *MAP_Tile
|
||||
Tile :: int
|
||||
Map :: struct
|
||||
data: *Tile
|
||||
x: int
|
||||
y: int
|
||||
actors: Array(MAP_Actor)
|
||||
actors: Array(Actor)
|
||||
|
||||
MAP_Actor :: struct
|
||||
Actor :: struct
|
||||
p: V2I
|
||||
target_p: V2I
|
||||
map: *MAP_Map
|
||||
map: *Map
|
||||
|
||||
open_paths: Array(MAP_Path)
|
||||
close_paths: Array(MAP_Path)
|
||||
open_paths: Array(Path)
|
||||
close_paths: Array(Path)
|
||||
tiles_visited: Array(V2I)
|
||||
history: Array(MAP_Path)
|
||||
history: Array(Path)
|
||||
|
||||
MAP_Path :: struct
|
||||
Path :: struct
|
||||
value_to_sort_by: int // distance from target
|
||||
p: V2I
|
||||
came_from: V2I
|
||||
|
||||
MAP_Rectangle :: (p: V2I): Rectangle
|
||||
Rect :: (p: V2I): Rectangle
|
||||
result := Rectangle{p.x->F32 * RectX, p.y->F32 * RectY, RectX, RectY}
|
||||
return result
|
||||
|
||||
MAP_Circle :: (p: V2I): Vector2
|
||||
Circle :: (p: V2I): Vector2
|
||||
result := Vector2{p.x->F32 * RectX + RectX/2, p.y->F32 * RectY + RectY/2}
|
||||
return result
|
||||
|
||||
MAP_ScreenToMap :: (p: Vector2): V2I
|
||||
ScreenToMap :: (p: Vector2): V2I
|
||||
p0 := p.x / RectX
|
||||
p1 := p.y / RectY
|
||||
result := V2I{p0->int, p1->int}
|
||||
return result
|
||||
|
||||
MAP_AddActor :: (map: *MAP_Map, p: V2I): *MAP_Actor
|
||||
AddActor :: (map: *Map, p: V2I): *Actor
|
||||
Add(&map.actors, {p, p, map})
|
||||
Assert(map.data[p.x + p.y * map.x] == 0)
|
||||
map.data[p.x + p.y * map.x] |= MAP_TILE_ACTOR_IS_STANDING
|
||||
actor := GetLast(&MAP_CurrentMap.actors)
|
||||
map.data[p.x + p.y * map.x] |= TILE_ACTOR_IS_STANDING
|
||||
actor := GetLast(&CurrentMap.actors)
|
||||
return actor
|
||||
|
||||
MAP_ActorSetP :: (actor: *MAP_Actor, p: V2I)
|
||||
ActorSetP :: (actor: *Actor, p: V2I)
|
||||
map := actor.map
|
||||
new_tile := map.data + p.x + p.y * map.x
|
||||
if *new_tile != 0 ;; return
|
||||
|
||||
tile := map.data + actor.p.x + actor.p.y * map.x
|
||||
Assert((*tile & MAP_TILE_ACTOR_IS_STANDING) != 0)
|
||||
*tile &= ~MAP_TILE_ACTOR_IS_STANDING
|
||||
Assert((*tile & TILE_ACTOR_IS_STANDING) != 0)
|
||||
*tile &= ~TILE_ACTOR_IS_STANDING
|
||||
|
||||
*new_tile |= MAP_TILE_ACTOR_IS_STANDING
|
||||
*new_tile |= TILE_ACTOR_IS_STANDING
|
||||
actor.p = p
|
||||
|
||||
Reset(&actor.tiles_visited)
|
||||
@@ -67,48 +67,48 @@ MAP_ActorSetP :: (actor: *MAP_Actor, p: V2I)
|
||||
Reset(&actor.open_paths)
|
||||
Reset(&actor.close_paths)
|
||||
|
||||
MAP_SetTargetP :: (s: *MAP_Actor, p: V2I)
|
||||
SetTargetP :: (s: *Actor, p: V2I)
|
||||
s.target_p = p
|
||||
Reset(&s.tiles_visited)
|
||||
Reset(&s.history)
|
||||
Reset(&s.open_paths)
|
||||
Reset(&s.close_paths)
|
||||
|
||||
MAP_GetRandomP :: (m: *MAP_Map): V2I
|
||||
result := V2I{GetRandomValue(0, MAP_CurrentMap.x - 1), GetRandomValue(0, MAP_CurrentMap.y - 1)}
|
||||
GetRandomP :: (m: *Map): V2I
|
||||
result := V2I{GetRandomValue(0, CurrentMap.x - 1), GetRandomValue(0, CurrentMap.y - 1)}
|
||||
return result
|
||||
|
||||
MAP_GetRandomUnblockedP :: (m: *MAP_Map): V2I
|
||||
GetRandomUnblockedP :: (m: *Map): V2I
|
||||
for i := 0, i < 128, i += 1
|
||||
p := MAP_GetRandomP(m)
|
||||
p := GetRandomP(m)
|
||||
if m.data[p.x + p.y * m.x] == 0
|
||||
return p
|
||||
|
||||
Assert(false, "Invalid codepath")
|
||||
r: V2I; return r
|
||||
|
||||
MAP_Init :: ()
|
||||
MAP_CurrentMap.x = WinX / RectX
|
||||
MAP_CurrentMap.y = WinY / RectY
|
||||
bytes := sizeof(MAP_Tile) * MAP_CurrentMap.x->U64 * MAP_CurrentMap.y->U64
|
||||
MAP_CurrentMap.data = malloc(bytes)
|
||||
memset(MAP_CurrentMap.data, 0, bytes)
|
||||
Init :: ()
|
||||
CurrentMap.x = WinX / RectX
|
||||
CurrentMap.y = WinY / RectY
|
||||
bytes := sizeof(Tile) * CurrentMap.x->U64 * CurrentMap.y->U64
|
||||
CurrentMap.data = malloc(bytes)
|
||||
memset(CurrentMap.data, 0, bytes)
|
||||
|
||||
actor := MAP_AddActor(&MAP_CurrentMap, MAP_GetRandomUnblockedP(&MAP_CurrentMap))
|
||||
actor.target_p = MAP_GetRandomUnblockedP(&MAP_CurrentMap)
|
||||
actor := AddActor(&CurrentMap, GetRandomUnblockedP(&CurrentMap))
|
||||
actor.target_p = GetRandomUnblockedP(&CurrentMap)
|
||||
|
||||
actor2 := MAP_AddActor(&MAP_CurrentMap, MAP_GetRandomUnblockedP(&MAP_CurrentMap))
|
||||
actor2.target_p = MAP_GetRandomUnblockedP(&MAP_CurrentMap)
|
||||
actor2 := AddActor(&CurrentMap, GetRandomUnblockedP(&CurrentMap))
|
||||
actor2.target_p = GetRandomUnblockedP(&CurrentMap)
|
||||
|
||||
MAP_RandomizeActors :: ()
|
||||
map := &MAP_CurrentMap
|
||||
RandomizeActors :: ()
|
||||
map := &CurrentMap
|
||||
for i := 0, i < map.actors.len, i += 1
|
||||
it := map.actors.data + i
|
||||
p := MAP_GetRandomUnblockedP(&MAP_CurrentMap)
|
||||
MAP_ActorSetP(it, p)
|
||||
it.target_p = MAP_GetRandomUnblockedP(&MAP_CurrentMap)
|
||||
p := GetRandomUnblockedP(&CurrentMap)
|
||||
ActorSetP(it, p)
|
||||
it.target_p = GetRandomUnblockedP(&CurrentMap)
|
||||
|
||||
MAP_InsertOpenPath :: (s: *MAP_Actor, p: V2I, came_from: V2I, ignore_blocks: bool = false)
|
||||
InsertOpenPath :: (s: *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
|
||||
if ignore_blocks == false && s.map.data[p.x + p.y * s.map.x] != 0 ;; return
|
||||
@@ -126,7 +126,7 @@ MAP_InsertOpenPath :: (s: *MAP_Actor, p: V2I, came_from: V2I, ignore_blocks: boo
|
||||
d := dx*dx + dy*dy
|
||||
InsertSortedDecreasing(&s.open_paths, {d, p, came_from})
|
||||
|
||||
MAP_GetCloseP :: (s: *MAP_Actor, p: V2I): *MAP_Path
|
||||
GetCloseP :: (s: *Actor, p: V2I): *Path
|
||||
for i := 0, i < s.close_paths.len, i += 1
|
||||
it := s.close_paths.data + i
|
||||
if it.p.x == p.x && it.p.y == p.y ;; return it
|
||||
@@ -134,7 +134,7 @@ MAP_GetCloseP :: (s: *MAP_Actor, p: V2I): *MAP_Path
|
||||
Assert(false, "Invalid codepath")
|
||||
return 0
|
||||
|
||||
MAP_RecomputeHistory :: (s: *MAP_Actor)
|
||||
RecomputeHistory :: (s: *Actor)
|
||||
if s.close_paths.len > 1
|
||||
Reset(&s.history)
|
||||
it := GetLast(&s.close_paths)
|
||||
@@ -144,11 +144,11 @@ MAP_RecomputeHistory :: (s: *MAP_Actor)
|
||||
if i > 512
|
||||
Reset(&s.history)
|
||||
break
|
||||
it = MAP_GetCloseP(s, it.came_from)
|
||||
it = GetCloseP(s, it.came_from)
|
||||
Add(&s.history, *it)
|
||||
Pop(&s.history)
|
||||
|
||||
MAP_MoveTowardsTarget :: (s: *MAP_Actor)
|
||||
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)
|
||||
@@ -156,10 +156,10 @@ MAP_MoveTowardsTarget :: (s: *MAP_Actor)
|
||||
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
|
||||
*tile &= ~TILE_ACTOR_IS_STANDING
|
||||
*new_tile |= TILE_ACTOR_IS_STANDING
|
||||
|
||||
MAP_PathFindUpdate :: (map: *MAP_Map)
|
||||
PathFindUpdate :: (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
|
||||
@@ -172,9 +172,9 @@ MAP_PathFindUpdate :: (map: *MAP_Map)
|
||||
Reset(&s.history)
|
||||
break
|
||||
|
||||
MAP_PathFind(s)
|
||||
PathFind(s)
|
||||
|
||||
MAP_PathFindStep :: (s: *MAP_Actor, compute_history: bool = true): bool
|
||||
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
|
||||
@@ -185,7 +185,7 @@ MAP_PathFindStep :: (s: *MAP_Actor, compute_history: bool = true): bool
|
||||
Reset(&s.close_paths)
|
||||
Reset(&s.history)
|
||||
|
||||
MAP_InsertOpenPath(s, s.p, s.p, ignore_blocks = true)
|
||||
InsertOpenPath(s, s.p, s.p, ignore_blocks = true)
|
||||
|
||||
if s.close_paths.len != 0
|
||||
last := GetLast(&s.close_paths)
|
||||
@@ -201,13 +201,13 @@ MAP_PathFindStep :: (s: *MAP_Actor, compute_history: bool = true): bool
|
||||
for x := -1, x <= 1, x += 1
|
||||
if x == 0 && y == 0 ;; continue
|
||||
p := V2I{it.p.x + x, it.p.y + y}
|
||||
MAP_InsertOpenPath(s, p, it.p)
|
||||
InsertOpenPath(s, p, it.p)
|
||||
|
||||
if compute_history ;; MAP_RecomputeHistory(s)
|
||||
if compute_history ;; RecomputeHistory(s)
|
||||
return false
|
||||
|
||||
MAP_PathFind :: (s: *MAP_Actor)
|
||||
PathFind :: (s: *Actor)
|
||||
for i := 0, i < 32, i += 1
|
||||
done := MAP_PathFindStep(s, false)
|
||||
done := PathFindStep(s, false)
|
||||
if done ;; break
|
||||
MAP_RecomputeHistory(s)
|
||||
RecomputeHistory(s)
|
||||
|
||||
Reference in New Issue
Block a user