diff --git a/build.bat b/build.bat index 8e18089..690841b 100644 --- a/build.bat +++ b/build.bat @@ -1,7 +1,7 @@ @echo off call "..\misc\compile_setup.bat" -bld --dont_compile_core +rem bld --dont_compile_core cd build core_main.exe rtsgame/main.core bld --dont_compile_core --link=vendor/raylib/windows/raylibdll.lib diff --git a/build/rtsgame/array.core b/build/rtsgame/array.core index 27bb719..a9768ee 100644 --- a/build/rtsgame/array.core +++ b/build/rtsgame/array.core @@ -88,9 +88,10 @@ InsertSortedDecreasing :: (a: *Array($T), item: T) for i := 0, i < a.len, i += 1 it := a.data + i if it.value_to_sort_by <= item.value_to_sort_by + insert_index = i Insert(a, item, i) break - + if insert_index == -1 Add(a, item) diff --git a/build/rtsgame/main.core b/build/rtsgame/main.core index 6268ea1..04eaecb 100644 --- a/build/rtsgame/main.core +++ b/build/rtsgame/main.core @@ -98,6 +98,9 @@ main :: (): int InitWindow(WinX, WinY, "Testing") SetTargetFPS(60) + MAP_PathFindStart(MAP_CurrentMap.actors.data) + MAP_PathFindStep() + for !WindowShouldClose() WinX = GetScreenWidth() WinY = GetScreenHeight() @@ -107,6 +110,8 @@ main :: (): int if IsKeyPressed(KEY_F1) ;; Mode := 0 if IsKeyPressed(KEY_F2) ;; Mode := 1 + if IsKeyDown(KEY_SPACE) + MAP_PathFindStep() BeginDrawing() ClearBackground(RAYWHITE) @@ -122,7 +127,6 @@ main :: (): int colliding := CheckCollisionPointRec(MouseP, r) if Mode == 0 && colliding && IsMouseButtonDown(MOUSE_BUTTON_LEFT) ;; *it = 1 - // if Mode == 0 && colliding && IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) ;; GuyP = V2I{x,y} color := GREEN if *it == 1 ;; color = RED @@ -138,9 +142,18 @@ main :: (): int DrawRectangleRec(r, YELLOW) DrawRectangleRec(target_r, PURPLE) + for i := 0, i < MAP_OpenPaths.len, i += 1 + it := MAP_OpenPaths.data + i + r := Rectangle{it.p.x->F32 * RectX, it.p.y->F32 * RectY, RectX, RectY} + DrawRectangleRec(r, ORANGE) + + if MAP_ReachedTarget + for i := 0, i < MAP_History.len, i += 1 + it := MAP_History.data + i + r := Rectangle{it.p.x->F32 * RectX, it.p.y->F32 * RectY, RectX, RectY} + DrawRectangleRec(r, BLACK) + DrawText("Reached target !", 0, 0, 32, BLACK) - // DrawFPS(0, 0) - // DrawText(TextFormat("Testing %d", 32), 100, 100, 20, MAROON) EndDrawing() return 0 diff --git a/build/rtsgame/map.core b/build/rtsgame/map.core index 0a669c3..feb559e 100644 --- a/build/rtsgame/map.core +++ b/build/rtsgame/map.core @@ -1,24 +1,86 @@ MAP_CurrentMap: MAP_Map - +MAP_OpenPaths: Array(MAP_Path) +MAP_ClosePaths: Array(MAP_Path) +MAP_History: Array(MAP_Path) +MAP_TargetP: V2I +MAP_StartP: V2I +MAP_ReachedTarget := false MAP_Tile :: int MAP_Map :: struct data: *MAP_Tile x: int y: int - actors: Array(Actor) + actors: Array(MAP_Actor) -Actor :: struct +MAP_Actor :: struct p: V2I target_p: V2I +MAP_Path :: struct + value_to_sort_by: int // distance from target + p: V2I + came_from: V2I MAP_Init :: () - MAP_X :: 60 - MAP_Y :: 40 - - map_data: [MAP_X*MAP_Y]int - MAP_CurrentMap = MAP_Map{data = &map_data[0], x = MAP_X, y = MAP_Y} + MAP_CurrentMap.x = 60 + MAP_CurrentMap.y = 40 + MAP_CurrentMap.data = malloc(SizeOf(MAP_Tile) * MAP_CurrentMap.x->U64 * MAP_CurrentMap.y->U64) Add(&MAP_CurrentMap.actors, {{4, 4}, {8, 8}}) +MAP_InsertOpenPath :: (p: V2I, target_p: V2I, came_from: V2I) + if p.x < 0 || p.x >= MAP_CurrentMap.x ;; return + if p.y < 0 || p.y >= MAP_CurrentMap.y ;; return + if MAP_CurrentMap.data[p.x + p.y * MAP_CurrentMap.x] == 1 ;; return + + for i := 0, i < MAP_ClosePaths.len, i += 1 + it := MAP_ClosePaths.data + i + if it.p.x == p.x && it.p.y == p.y ;; return + + for i := 0, i < MAP_OpenPaths.len, i += 1 + it := MAP_OpenPaths.data + i + if it.p.x == p.x && it.p.y == p.y ;; return + + + dx := target_p.x - p.x + dy := target_p.y - p.y + d := dx*dx + dy*dy + InsertSortedDecreasing(&MAP_OpenPaths, {d, p, came_from}) + +MAP_PathFindStart :: (actor: *MAP_Actor) + Reset(&MAP_OpenPaths) + Reset(&MAP_ClosePaths) + MAP_TargetP = actor.target_p + MAP_StartP = actor.p + MAP_InsertOpenPath(actor.p, actor.target_p, actor.p) + +MAP_GetCloseP :: (p: V2I): *MAP_Path + for i := 0, i < MAP_ClosePaths.len, i += 1 + it := MAP_ClosePaths.data + i + if it.p.x == p.x && it.p.y == p.y ;; return it + + Assert(false) + return 0 + +MAP_PathFindStep :: () + if MAP_ReachedTarget ;; return + + it := Pop(&MAP_OpenPaths) + Add(&MAP_ClosePaths, it) + + MAP_ReachedTarget = it.p.x == MAP_TargetP.x && it.p.y == MAP_TargetP.y + if MAP_ReachedTarget + Add(&MAP_History, it) + for !(it.p.x == MAP_StartP.x && it.p.y == MAP_StartP.y) + it = *MAP_GetCloseP(it.came_from) + Add(&MAP_History, it) + return + + // @language_todo: Add continue keyword + // if x == 0 && y == 0 ;; continue + for y := -1, y <= 1, y += 1 + for x := -1, x <= 1, x += 1 + if (x == 0 && y == 0) == false + p := V2I{it.p.x + x, it.p.y + y} + MAP_InsertOpenPath(p, MAP_TargetP, it.p)