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

@@ -35,11 +35,6 @@ MakeArray :: (a: *int, count: int): Array(int)
} }
return result 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) MultipleArgs :: (): Tuple(int, F32)
return {32, 32} return {32, 32}
@@ -52,13 +47,6 @@ PolyType :: (a: $T): T
GetCount :: (a: int): int GetCount :: (a: int): int
return a 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" C :: #import "LibC.core"
Add :: (arr: *Array($T), val: T) Add :: (arr: *Array($T), val: T)
@@ -83,22 +71,22 @@ main :: (argc: int, argv: **char): int
// c := MakeArray(buff, GetCount(GetCount(32))) // c := MakeArray(buff, GetCount(GetCount(32)))
a, b := MultipleArgs()
a := MultipleArgs() Assert(a == 32 && b == 32)
Add(&array, 32) Add(&array, 32)
Add(&second_array, 32) Add(&second_array, 32)
Add(&third_array, 32) Add(&third_array, 32)
Add(&fourth, 32) Add(&fourth, 32)
Add(&fifth, 32) Add(&fifth, 32)
// Add(&sixth, {32}) // @todo this is possible ! Add(&sixth, {})
// value := PolyLambda(**int) value := PolyLambda(**int)
// PolyType_r1 := PolyType(10) PolyType_r1 := PolyType(10)
// PolyType_r2 := PolyType(int) PolyType_r2 := PolyType(int)
PolyType_r5 := PolyType(seventh) PolyType_r3 := PolyType(test)
// PolyType_r3 := PolyType(test) PolyType_r4 := PolyType(test_a)
// PolyType_r4 := PolyType(test_a) PolyType_r5 := PolyType(sixth)
// PolyType_r5 := PolyType(sixth) PolyType_r6 := PolyType(seventh)
return 0 return 0

View File

@@ -1,5 +1,3 @@
/*@language_refactor: Remove tuples and simplify, Make var unpacking general*/
/*@language_todo: make polymorphism checking more robust*/ /*@language_todo: make polymorphism checking more robust*/
/*@language_todo: labeled block /*@language_todo: labeled block
@@ -25,7 +23,7 @@ thing := 1
*/ */
#import "raylib.core" #import "raylib.core"
#load "array.core" A :: #load "array.core"
MAP :: #load "map.core" MAP :: #load "map.core"
sqrtf :: #foreign (value: F32): F32 sqrtf :: #foreign (value: F32): F32
@@ -47,12 +45,12 @@ Mode := 0
RectX :: 16 RectX :: 16
RectY :: 16 RectY :: 16
Dt: F32 Dt: F32
ANI_SetTiles: Array(ANI_SetTile) ANI_SetTiles: A.Array(ANI_SetTile)
MouseSelecting := false MouseSelecting := false
MouseSelectionPivot: Vector2 MouseSelectionPivot: Vector2
MouseSelectionBox: Rectangle MouseSelectionBox: Rectangle
MouseSelectedActors: Array(*MAP.Actor) // @todo: ids MouseSelectedActors: A.Array(*MAP.Actor) // @todo: ids
main :: (): int main :: (): int
MAP.Init() MAP.Init()
@@ -88,7 +86,6 @@ main :: (): int
MouseP = GetMousePosition() MouseP = GetMousePosition()
Dt = GetFrameTime() Dt = GetFrameTime()
map := &MAP.CurrentMap map := &MAP.CurrentMap
ai := GetIndex(&map.actors, map.actors.data)
MouseSelecting = false MouseSelecting = false
if IsMouseButtonDown(MOUSE_BUTTON_LEFT) if IsMouseButtonDown(MOUSE_BUTTON_LEFT)
@@ -144,9 +141,9 @@ main :: (): int
if Mode == 0 if Mode == 0
if colliding && IsMouseButtonDown(MOUSE_BUTTON_LEFT) 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) 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} if colliding == true ;; color = {a = 100}
DrawRectangleRec(r2, color) DrawRectangleRec(r2, color)
@@ -181,7 +178,7 @@ main :: (): int
tile_it.t += Dt*8 tile_it.t += Dt*8
if remove if remove
UnorderedRemove(&ANI_SetTiles, tile_it) A.UnorderedRemove(&ANI_SetTiles, tile_it)
tile_i -= 1 tile_i -= 1
for i := 0, i < map.actors.len, i += 1 for i := 0, i < map.actors.len, i += 1
@@ -232,7 +229,7 @@ main :: (): int
MAP.SetTargetP(actor_it, p) MAP.SetTargetP(actor_it, p)
if MouseSelecting if MouseSelecting
Reset(&MouseSelectedActors) A.Reset(&MouseSelectedActors)
COLOR_SelectionBox := GREEN COLOR_SelectionBox := GREEN
COLOR_SelectionBox.a = 255/2 COLOR_SelectionBox.a = 255/2
@@ -241,7 +238,7 @@ main :: (): int
actor_box := MAP.Rect(actor_it.p) actor_box := MAP.Rect(actor_it.p)
if CheckCollisionRecs(actor_box, MouseSelectionBox) if CheckCollisionRecs(actor_box, MouseSelectionBox)
Add(&MouseSelectedActors, actor_it) A.Add(&MouseSelectedActors, actor_it)
DrawRectangleRec(MouseSelectionBox, COLOR_SelectionBox) DrawRectangleRec(MouseSelectionBox, COLOR_SelectionBox)

View File

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