From 8c0b4439af3fedaa89a9f46946c2d5e894477b38 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sat, 22 Apr 2023 09:15:34 +0200 Subject: [PATCH] Core: Polymorphism works better then expected hmm... --- build/examples/polymorphism.core | 32 +++++---------- build/rtsgame/main.core | 19 ++++----- build/rtsgame/map.core | 68 ++++++++++++++++---------------- 3 files changed, 52 insertions(+), 67 deletions(-) diff --git a/build/examples/polymorphism.core b/build/examples/polymorphism.core index b1538b8..0afc241 100644 --- a/build/examples/polymorphism.core +++ b/build/examples/polymorphism.core @@ -35,11 +35,6 @@ MakeArray :: (a: *int, count: int): Array(int) } return result -// :Multiple arguments -// @todo: maybe disallow multiple arguments in current form -// and use polimorphism. Then we could make var unpacking, -// unpack structs making it more powerful -// a,b := MultipleArgs() // @todo var unpacking MultipleArgs :: (): Tuple(int, F32) return {32, 32} @@ -52,13 +47,6 @@ PolyType :: (a: $T): T GetCount :: (a: int): int return a -// @todo: this is allowed, shouldn't be -// Test :: (a: int, b: int = 10, c: int???) - -// @todo: -// Add :: (arr: *Array($T), item: T) -// return - C :: #import "LibC.core" Add :: (arr: *Array($T), val: T) @@ -83,22 +71,22 @@ main :: (argc: int, argv: **char): int // c := MakeArray(buff, GetCount(GetCount(32))) - - a := MultipleArgs() + a, b := MultipleArgs() + Assert(a == 32 && b == 32) Add(&array, 32) Add(&second_array, 32) Add(&third_array, 32) Add(&fourth, 32) Add(&fifth, 32) - // Add(&sixth, {32}) // @todo this is possible ! + Add(&sixth, {}) - // value := PolyLambda(**int) - // PolyType_r1 := PolyType(10) - // PolyType_r2 := PolyType(int) - PolyType_r5 := PolyType(seventh) - // PolyType_r3 := PolyType(test) - // PolyType_r4 := PolyType(test_a) - // PolyType_r5 := PolyType(sixth) + value := PolyLambda(**int) + PolyType_r1 := PolyType(10) + PolyType_r2 := PolyType(int) + PolyType_r3 := PolyType(test) + PolyType_r4 := PolyType(test_a) + PolyType_r5 := PolyType(sixth) + PolyType_r6 := PolyType(seventh) return 0 \ No newline at end of file diff --git a/build/rtsgame/main.core b/build/rtsgame/main.core index 82e2ac9..bc7c67f 100644 --- a/build/rtsgame/main.core +++ b/build/rtsgame/main.core @@ -1,5 +1,3 @@ -/*@language_refactor: Remove tuples and simplify, Make var unpacking general*/ - /*@language_todo: make polymorphism checking more robust*/ /*@language_todo: labeled block @@ -25,7 +23,7 @@ thing := 1 */ #import "raylib.core" -#load "array.core" +A :: #load "array.core" MAP :: #load "map.core" sqrtf :: #foreign (value: F32): F32 @@ -47,12 +45,12 @@ Mode := 0 RectX :: 16 RectY :: 16 Dt: F32 -ANI_SetTiles: Array(ANI_SetTile) +ANI_SetTiles: A.Array(ANI_SetTile) MouseSelecting := false MouseSelectionPivot: Vector2 MouseSelectionBox: Rectangle -MouseSelectedActors: Array(*MAP.Actor) // @todo: ids +MouseSelectedActors: A.Array(*MAP.Actor) // @todo: ids main :: (): int MAP.Init() @@ -88,7 +86,6 @@ main :: (): int MouseP = GetMousePosition() Dt = GetFrameTime() map := &MAP.CurrentMap - ai := GetIndex(&map.actors, map.actors.data) MouseSelecting = false if IsMouseButtonDown(MOUSE_BUTTON_LEFT) @@ -144,9 +141,9 @@ main :: (): int if Mode == 0 if colliding && IsMouseButtonDown(MOUSE_BUTTON_LEFT) - Add(&ANI_SetTiles, {true, {x,y}}) + A.Add(&ANI_SetTiles, {true, {x,y}}) if colliding && IsMouseButtonDown(MOUSE_BUTTON_RIGHT) - Add(&ANI_SetTiles, {false, {x,y}}) + A.Add(&ANI_SetTiles, {false, {x,y}}) if colliding == true ;; color = {a = 100} DrawRectangleRec(r2, color) @@ -181,7 +178,7 @@ main :: (): int tile_it.t += Dt*8 if remove - UnorderedRemove(&ANI_SetTiles, tile_it) + A.UnorderedRemove(&ANI_SetTiles, tile_it) tile_i -= 1 for i := 0, i < map.actors.len, i += 1 @@ -232,7 +229,7 @@ main :: (): int MAP.SetTargetP(actor_it, p) if MouseSelecting - Reset(&MouseSelectedActors) + A.Reset(&MouseSelectedActors) COLOR_SelectionBox := GREEN COLOR_SelectionBox.a = 255/2 @@ -241,7 +238,7 @@ main :: (): int actor_box := MAP.Rect(actor_it.p) if CheckCollisionRecs(actor_box, MouseSelectionBox) - Add(&MouseSelectedActors, actor_it) + A.Add(&MouseSelectedActors, actor_it) DrawRectangleRec(MouseSelectionBox, COLOR_SelectionBox) diff --git a/build/rtsgame/map.core b/build/rtsgame/map.core index e17abfe..faea5e1 100644 --- a/build/rtsgame/map.core +++ b/build/rtsgame/map.core @@ -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