RTS: Incremental path finding, Selection box, refactor
This commit is contained in:
@@ -77,7 +77,16 @@ Add(&guys, {100, 100})
|
||||
#load "array.core"
|
||||
#load "map.core"
|
||||
|
||||
V2I :: struct ;; x: int; y: int
|
||||
sqrtf :: #foreign (value: F32): F32
|
||||
|
||||
V2I :: struct
|
||||
x: int
|
||||
y: int
|
||||
|
||||
ANI_SetTile :: struct
|
||||
set: bool
|
||||
p: V2I
|
||||
t: F32
|
||||
|
||||
WinX := 1280
|
||||
WinY := 720
|
||||
@@ -88,13 +97,12 @@ Mode := 0
|
||||
RectX :: 16
|
||||
RectY :: 16
|
||||
Dt: F32
|
||||
|
||||
ANI_SetTile :: struct
|
||||
set: bool
|
||||
p: V2I
|
||||
t: F32
|
||||
ANI_SetTiles: Array(ANI_SetTile)
|
||||
|
||||
MouseSelecting := false
|
||||
MouseSelectionPivot: Vector2
|
||||
MouseSelectionBox: Rectangle
|
||||
|
||||
main :: (): int
|
||||
MAP_Init()
|
||||
|
||||
@@ -130,20 +138,39 @@ main :: (): int
|
||||
Dt = GetFrameTime()
|
||||
map := &MAP_CurrentMap
|
||||
|
||||
MouseSelecting = false
|
||||
if IsMouseButtonDown(MOUSE_BUTTON_LEFT)
|
||||
MouseSelecting = true
|
||||
if IsMouseButtonPressed(MOUSE_BUTTON_LEFT)
|
||||
MouseSelectionPivot = MouseP
|
||||
MouseSelectionBox = Rectangle{
|
||||
MouseSelectionPivot.x,
|
||||
MouseSelectionPivot.y,
|
||||
MouseP.x - MouseSelectionPivot.x,
|
||||
MouseP.y - MouseSelectionPivot.y
|
||||
}
|
||||
|
||||
if IsKeyPressed(KEY_F1)
|
||||
Mode = 0
|
||||
|
||||
if IsKeyPressed(KEY_F2)
|
||||
Mode = 1
|
||||
|
||||
if IsKeyPressed(KEY_F1) ;; Mode = 0
|
||||
if IsKeyPressed(KEY_F2) ;; Mode = 1
|
||||
if IsKeyPressed(KEY_F3)
|
||||
for i := 0, i < map.actors.len, i += 1
|
||||
it := map.actors.data + i
|
||||
MAP_MoveTowardsTarget(it)
|
||||
|
||||
if IsKeyPressed(KEY_F4)
|
||||
MAP_RandomizeActors()
|
||||
|
||||
if IsKeyDown(KEY_SPACE)
|
||||
for i := 0, i < map.actors.len, i += 1
|
||||
it := map.actors.data + i
|
||||
MAP_PathFindStep(it)
|
||||
|
||||
MAP_PathFindUpdate(map)
|
||||
|
||||
BeginDrawing()
|
||||
ClearBackground(RAYWHITE)
|
||||
|
||||
@@ -157,18 +184,19 @@ main :: (): int
|
||||
r2: Rectangle = {r.x + 1, r.y + 1, r.width - 2, r.height - 2}
|
||||
|
||||
colliding := CheckCollisionPointRec(MouseP, r)
|
||||
|
||||
if Mode == 0 && colliding && IsMouseButtonDown(MOUSE_BUTTON_LEFT)
|
||||
Add(&ANI_SetTiles, {true, {x,y}})
|
||||
if Mode == 0 && colliding && IsMouseButtonDown(MOUSE_BUTTON_RIGHT)
|
||||
Add(&ANI_SetTiles, {false, {x,y}})
|
||||
if Mode == 1 && colliding && IsMouseButtonPressed(MOUSE_BUTTON_LEFT) ;; MAP_ActorSetP(MAP_CurrentMap.actors.data, {x,y})
|
||||
if Mode == 1 && colliding && IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) ;; MAP_CurrentMap.actors.data[0].target_p = V2I{x,y}
|
||||
|
||||
color := RAYWHITE
|
||||
if *it == 1 ;; color = GRAY
|
||||
if colliding == true
|
||||
color = Color{a = 100}
|
||||
if *it == 1 ;; color = GRAY
|
||||
|
||||
|
||||
if Mode == 0
|
||||
if colliding && IsMouseButtonDown(MOUSE_BUTTON_LEFT)
|
||||
Add(&ANI_SetTiles, {true, {x,y}})
|
||||
if colliding && IsMouseButtonDown(MOUSE_BUTTON_RIGHT)
|
||||
Add(&ANI_SetTiles, {false, {x,y}})
|
||||
// if Mode == 1 && colliding && IsMouseButtonPressed(MOUSE_BUTTON_LEFT) ;; MAP_ActorSetP(MAP_CurrentMap.actors.data, {x,y})
|
||||
// if Mode == 1 && colliding && IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) ;; MAP_CurrentMap.actors.data[0].target_p = V2I{x,y}
|
||||
|
||||
if colliding == true ;; color = Color{a = 100}
|
||||
|
||||
DrawRectangleRec(r2, color)
|
||||
|
||||
@@ -205,53 +233,48 @@ main :: (): int
|
||||
UnorderedRemove(&ANI_SetTiles, tile_it)
|
||||
tile_i -= 1
|
||||
|
||||
|
||||
|
||||
for i := 0, i < map.actors.len, i += 1
|
||||
actor_it := map.actors.data + i
|
||||
// r := Rectangle{actor_it.p.x->F32 * RectX, actor_it.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}
|
||||
target_r := MAP_Rectangle(actor_it.target_p)
|
||||
|
||||
main_p := Vector2{actor_it.p.x->F32 * RectX + RectX/2, actor_it.p.y->F32 * RectY + RectY/2}
|
||||
main_p := MAP_Circle(actor_it.p)
|
||||
DrawCircleV(main_p, RectX/2, actor_color)
|
||||
// DrawRectangleRec(r, actor_color)
|
||||
DrawRectangleRec(target_r, target_color)
|
||||
|
||||
smaller_the_further: F32 = 0
|
||||
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}
|
||||
p := MAP_Circle({tile_it.x, tile_it.y})
|
||||
DrawCircleV(p, RectX/2 - smaller_the_further, past_actor_color)
|
||||
smaller_the_further += 0.3
|
||||
smaller_the_further += 0.5
|
||||
|
||||
for path_i := 0, path_i < actor_it.open_paths.len, path_i += 1
|
||||
path_it := actor_it.open_paths.data + path_i
|
||||
path_r := Rectangle{path_it.p.x->F32 * RectX, path_it.p.y->F32 * RectY, RectX, RectY}
|
||||
path_r := MAP_Rectangle(path_it.p)
|
||||
DrawRectangleRec(path_r, orange)
|
||||
s := TextFormat("%d", sqrtf(path_it.value_to_sort_by->F32)->int)
|
||||
DrawText(s, path_r.x->int, path_r.y->int, 1, RAYWHITE)
|
||||
|
||||
for path_i := 0, path_i < actor_it.close_paths.len, path_i += 1
|
||||
path_it := actor_it.close_paths.data + path_i
|
||||
path_r := Rectangle{path_it.p.x->F32 * RectX, path_it.p.y->F32 * RectY, RectX, RectY}
|
||||
path_r := MAP_Rectangle(path_it.p)
|
||||
DrawRectangleRec(path_r, brown)
|
||||
|
||||
MAP_RecomputeHistory(actor_it)
|
||||
for path_i := 0, path_i < actor_it.history.len, path_i += 1
|
||||
path_it := actor_it.history.data + path_i
|
||||
// path_r := Rectangle{path_it.p.x->F32 * RectX, path_it.p.y->F32 * RectY, RectX, RectY}
|
||||
|
||||
x0 := path_it.came_from.x->F32 * RectX + RectX/2
|
||||
y0 := path_it.came_from.y->F32 * RectY + RectY/2
|
||||
|
||||
x1 := path_it.p.x->F32 * RectX + RectX/2
|
||||
y1 := path_it.p.y->F32 * RectY + RectY/2
|
||||
|
||||
p0 := Vector2{x0, y0}
|
||||
p1 := Vector2{x1, y1}
|
||||
p0 := MAP_Circle(path_it.came_from)
|
||||
p1 := MAP_Circle(path_it.p)
|
||||
|
||||
DrawLineEx(p0, p1, 5, LIGHTGRAY)
|
||||
DrawCircleV(p0, 4, LIGHTGRAY)
|
||||
DrawCircleV(p1, 4, LIGHTGRAY)
|
||||
|
||||
if Mode == 1 && MouseSelecting
|
||||
COLOR_SelectionBox := GREEN
|
||||
COLOR_SelectionBox.a = 255/2
|
||||
DrawRectangleRec(MouseSelectionBox, COLOR_SelectionBox)
|
||||
|
||||
menu_open := false
|
||||
if menu_open
|
||||
text_size := 24
|
||||
@@ -262,7 +285,6 @@ main :: (): int
|
||||
text_y -= text_size
|
||||
DrawText("F4 :: Randomize actors", text_p, text_y, text_size, GRAY)
|
||||
text_y -= text_size
|
||||
|
||||
DrawText("F3 :: Simulate actors", text_p, text_y, text_size, GRAY)
|
||||
text_y -= text_size
|
||||
text: *char = "Mode(F1) :: Block placing"
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user