diff --git a/fcore/enemy-behaviour.sml b/fcore/enemy-behaviour.sml index 2e07149..abf4c51 100644 --- a/fcore/enemy-behaviour.sml +++ b/fcore/enemy-behaviour.sml @@ -4,18 +4,15 @@ struct fun canWalkAhead (x, y, wallTree, platformTree) = let - val ww = Constants.worldWidth - val wh = Constants.worldHeight - val y = y + Constants.enemySize - 5 val searchHeight = 10 val searchWidth = Constants.moveEnemyBy in - QuadTree.hasCollisionAt - (x, y, searchWidth, searchHeight, 0, 0, ww, wh, ~1, wallTree) + QuadHelp.hasCollisionAt + (x, y, searchWidth, searchHeight, wallTree) orelse - QuadTree.hasCollisionAt - (x, y, searchWidth, searchHeight, 0, 0, ww, wh, ~1, platformTree) + QuadHelp.hasCollisionAt + (x, y, searchWidth, searchHeight, platformTree) end (* same function takes either wallTree or platformTree and returns true @@ -31,11 +28,8 @@ struct val width = Constants.enemySize val height = Platform.platHeight - - val ww = Constants.worldWidth - val wh = Constants.worldHeight in - QuadTree.hasCollisionAt (ex, ey, width, height, 0, 0, ww, wh, ~1, tree) + QuadHelp.hasCollisionAt (ex, ey, width, height, tree) end fun getPatrollPatches (enemy: enemy, wallTree, platformTree, acc) = @@ -68,19 +62,11 @@ struct val searchWidth = Constants.moveEnemyBy val searchHeight = Constants.enemySize - 5 - val ww = Constants.worldWidth - val wh = Constants.worldHeight - - val hasWallAhead = QuadTree.hasCollisionAt + val hasWallAhead = QuadHelp.hasCollisionAt ( searchStartX , y , searchWidth , searchHeight - , 0 - , 0 - , ww - , wh - , ~1 , wallTree ) in @@ -102,19 +88,11 @@ struct val searchWidth = Constants.moveEnemyBy val searchHeight = Constants.enemySize - 5 - val ww = Constants.worldWidth - val wh = Constants.worldHeight - - val hasWallAhead = QuadTree.hasCollisionAt + val hasWallAhead = QuadHelp.hasCollisionAt ( searchStartX , y , searchWidth , searchHeight - , 0 - , 0 - , ww - , wh - , ~1 , wallTree ) in @@ -155,12 +133,11 @@ struct val searchWidth = Constants.playerSize val searchHeight = Constants.worldHeight - y - val ww = Constants.worldWidth - val wh = Constants.worldHeight - - val collisions = QuadTree.getCollisions - (x, y, searchWidth, searchHeight, 0, 0, ww, wh, ~1, platformTree) + val collisions = QuadHelp.getCollisions + (x, y, searchWidth, searchHeight, platformTree) val checkY = y + Constants.playerSize + + val wh = Constants.worldHeight in getHighestPlatform (collisions, platforms, wh, ~1, checkY) end @@ -174,11 +151,9 @@ struct val y = y + Constants.enemySize - val ww = Constants.worldWidth + val collisions = QuadHelp.getCollisions + (x, y, searchWidth, searchHeight, platformTree) val wh = Constants.worldHeight - - val collisions = QuadTree.getCollisions - (x, y, searchWidth, searchHeight, 0, 0, ww, wh, ~1, platformTree) in getHighestPlatform (collisions, platforms, wh, ~1, y) end diff --git a/fcore/enemy.sml b/fcore/enemy.sml index 64490ca..6bbcdcc 100644 --- a/fcore/enemy.sml +++ b/fcore/enemy.sml @@ -34,11 +34,9 @@ struct val {x, y, health, ...} = enemy val size = Constants.enemySize - val ww = Constants.worldWidth - val wh = Constants.worldHeight - val hasCollision = QuadTree.hasCollisionAt - (x, y, size, size, 0, 0, ww, wh, ~1, projectileTree) + val hasCollision = QuadHelp.hasCollisionAt + (x, y, size, size, projectileTree) in if hasCollision then if health = 1 then @@ -182,10 +180,8 @@ struct val {id, x, y, ...} = Vector.sub (enemyVec, pos) val size = Constants.enemySize - val ww = Constants.worldWidth - val wh = Constants.worldHeight - val acc = QuadTree.insert (x, y, size, size, 0, 0, ww, wh, id, acc) + val acc = QuadHelp.insert (x, y, size, size, id, acc) in helpGenerateTree (pos + 1, enemyVec, acc) end diff --git a/fcore/game-type.sml b/fcore/game-type.sml index 1e95f7b..ccce201 100644 --- a/fcore/game-type.sml +++ b/fcore/game-type.sml @@ -178,8 +178,20 @@ struct val plat8 = {id = 8, x = 970, y = 815, width = 303} val plat9 = {id = 9, x = 959, y = 705, width = 303} val plat10 = {id = 10, x = 970, y = 759, width = 303} + val plat11 = {id = 11, x = 970, y = 595, width = 303} + val plat12 = {id = 12, x = 959, y = 535, width = 303} + val plat13 = {id = 13, x = 970, y = 495, width = 303} + val plat14 = {id = 14, x = 1000, y = 415, width = 303} + val plat15 = {id = 15, x = 1000, y = 335, width = 303} + val plat16 = {id = 16, x = 1000, y = 295, width = 303} + val plat17 = {id = 17, x = 155, y = 599, width = 199} + val plat18 = {id = 18, x = 155, y = 499, width = 199} + val plat19 = {id = 19, x = 155, y = 399, width = 199} val platforms = Vector.fromList - [plat1, plat2, plat3, plat4, plat5, plat6, plat7, plat8, plat9, plat10] + [plat1, plat2, plat3, plat4, plat5, plat6, plat7, plat8, plat9, plat10, + plat11, plat12, plat13 + , plat14, plat15, plat16, plat17, plat18, + plat19] val platformTree = Platform.generateTree platforms val enemy1 = @@ -189,7 +201,7 @@ struct , health = 1 , xAxis = STAY_STILL , yAxis = FALLING - , variant = EnemyVariants.FOLLOW_SLIME + , variant = EnemyVariants.PATROL_SLIME , platID = ~1 , nextPlatID = ~1 } diff --git a/fcore/physics.sml b/fcore/physics.sml index 5ebf86d..22b8027 100644 --- a/fcore/physics.sml +++ b/fcore/physics.sml @@ -84,7 +84,7 @@ struct val ww = Constants.worldWidth val wh = Constants.worldHeight in - QuadTree.hasCollisionAt (x, y, width, height, 0, 0, ww, wh, ~1, tree) + QuadHelp.hasCollisionAt (x, y, width, height, tree) end fun standingOnAreaID (x, y, tree) = @@ -92,12 +92,27 @@ struct val y = y + Fn.entitySize - 1 val width = Fn.entitySize - val height = Platform.platHeight + val height = Platform.platHeight + 2 + + val plat1 = {id = 1, x = 155, y = 911, width = 199} + val plat2 = {id = 2, x = 355, y = 759, width = 555} + val plat3 = {id = 3, x = 355, y = 659, width = 111} + val plat4 = {id = 4, x = 155, y = 855, width = 99} + val plat5 = {id = 5, x = 155, y = 811, width = 199} + val plat6 = {id = 6, x = 155, y = 710, width = 199} + val plat7 = {id = 7, x = 301, y = 855, width = 99} + val plat8 = {id = 8, x = 970, y = 815, width = 303} + val plat9 = {id = 9, x = 959, y = 705, width = 303} + val plat10 = {id = 10, x = 970, y = 759, width = 303} val ww = Constants.worldWidth val wh = Constants.worldHeight - in - QuadTree.getItemID (x, y, width, height, 0, 0, ww, wh, tree) + + val _ = print "START getItemID\n" + val r = + QuadHelp.getItemID (x, y, width, height, tree) + val _ = print "FINISH getItemID\n" + in r end fun getWallPatches (x, y, walls, wallTree, acc) = @@ -110,8 +125,8 @@ struct (* check collision with wall to the left *) val acc = let - val leftWallID = QuadTree.getItemID - (x - 1, y, 1, 1, 0, 0, ww, wh, wallTree) + val leftWallID = QuadHelp.getItemID + (x - 1, y, 1, 1, wallTree) in if leftWallID <> ~1 then let @@ -129,8 +144,8 @@ struct (* check collision with wall to the right *) val acc = let - val rightWallID = QuadTree.getItemID - (x + size - 1, y, 1, 1, 0, 0, ww, wh, wallTree) + val rightWallID = QuadHelp.getItemID + (x + size - 1, y, 1, 1, wallTree) in if rightWallID <> ~1 then let @@ -145,8 +160,8 @@ struct end (* check collision with wall below *) - val downWallID = QuadTree.getItemID - (x + moveBy + 1, y + size, 1, 1, 0, 0, ww, wh, wallTree) + val downWallID = QuadHelp.getItemID + (x + moveBy + 1, y + size, 1, 1, wallTree) in if downWallID <> ~1 then let @@ -172,23 +187,14 @@ struct val ww = Constants.worldWidth val wh = Constants.worldHeight - val platCollisions = QuadTree.getCollisionsBelow - (x, y, size, size, 0, 0, ww, wh, 0, platformTree) - val platID = standingOnAreaID (x, y, platformTree) val acc = [] val acc = if platID <> ~1 then - (case yAxis of - DROP_BELOW_PLATFORM => - (* pass through, allowing player to drop below the platform *) - acc - | JUMPING _ => - (* pass through, allowing player to jump above the platform *) - acc - | FLOATING _ => + (print ("platID: " ^ Int.toString platID ^ "\n"); case yAxis of + JUMPING _ => (* pass through, allowing player to jump above the platform *) acc | _ => @@ -216,8 +222,8 @@ struct * so we do not drop below any platforms again * *) if - QuadTree.hasCollisionAt - (x, y, size, size, 0, 0, ww, wh, ~1, platformTree) + QuadHelp.hasCollisionAt + (x, y, size, size, platformTree) then acc else Fn.W_Y_AXIS FALLING :: acc | _ => acc diff --git a/fcore/platform.sml b/fcore/platform.sml index 10063fb..fb9ed40 100644 --- a/fcore/platform.sml +++ b/fcore/platform.sml @@ -11,8 +11,8 @@ struct else let val {id, x, y, width} = Vector.sub (platVec, pos) - val acc = QuadTree.insert - (x, y, width, platHeight, 0, 0, 1920, 1080, id, acc) + val acc = QuadHelp.insert + (x, y, width, platHeight, id, acc) in helpGenerateTree (pos + 1, platVec, acc) end diff --git a/fcore/player-enemy.sml b/fcore/player-enemy.sml index 8c65545..f8e79a1 100644 --- a/fcore/player-enemy.sml +++ b/fcore/player-enemy.sml @@ -47,8 +47,8 @@ struct (case attacked of NOT_ATTACKED => let - val enemyCollisions = QuadTree.getCollisions - (x, y, size, size, 0, 0, 1920, 1080, 0, enemyTree) + val enemyCollisions = QuadHelp.getCollisions + (x, y, size, size, enemyTree) val player = Player.enemyCollisionReaction @@ -72,8 +72,8 @@ struct if amt = Constants.attackedLimit then (* if reached limit, detect enemies again *) let - val enemyCollisions = QuadTree.getCollisions - (x, y, size, size, 0, 0, 1920, 1080, 0, enemyTree) + val enemyCollisions = QuadHelp.getCollisions + (x, y, size, size, enemyTree) val player = Player.exitAttackedAndCheckEnemies diff --git a/fcore/player.sml b/fcore/player.sml index 7a468a4..b7de862 100644 --- a/fcore/player.sml +++ b/fcore/player.sml @@ -320,6 +320,9 @@ struct let val player = #player game + val _ = print ("(playerX, playerY) = (" ^Int.toString (#x player) ^ ", " ^ + Int.toString (#y player) ^ ")\n") + val patches = getProjectilePatches player val player = PlayerPatch.withPatches (player, patches) @@ -427,8 +430,8 @@ struct val ww = Constants.worldWidth val wh = Constants.worldHeight - val enemyCollisions = QuadTree.getCollisions - (x, y, size, size, 0, 0, ww, wh, 0, enemyTree) + val enemyCollisions = QuadHelp.getCollisions + (x, y, size, size, enemyTree) in Vector.fromList enemyCollisions end diff --git a/fcore/projectile.sml b/fcore/projectile.sml index a0975be..813af4d 100644 --- a/fcore/projectile.sml +++ b/fcore/projectile.sml @@ -11,7 +11,7 @@ struct val size = projectileSizeInt val {x, y, facing = _} = Vector.sub (projectiles, pos) - val acc = QuadTree.insert (x, y, size, size, 0, 0, 1920, 1080, pos, acc) + val acc = QuadHelp.insert (x, y, size, size, pos, acc) in helpGenerateTree (pos + 1, projectiles, acc) end diff --git a/fcore/quad-tree-fold.sml b/fcore/quad-tree-fold.sml deleted file mode 100644 index 7ccd7b8..0000000 --- a/fcore/quad-tree-fold.sml +++ /dev/null @@ -1,160 +0,0 @@ -signature QUAD_FOLDER = -sig - type env - type state - val fState: state * env * int -> state -end - -functor MakeQuadFolder(Fn: QUAD_FOLDER) = -struct - open QuadTreeType - - fun visitTopLeft (iX, iY, iW, iH, qX, qY, qW, qH) = - let - val midX = qW div 2 + qX - val midY = qH div 2 + qY - in - iX <= midX andalso iY <= midY - end - - fun visitTopRight (iX, iY, iW, iH, qX, qY, qW, qH) = - let - val midX = qW div 2 + qX - val midY = qH div 2 + qY - in - iX >= midX andalso iY <= midY - end - - fun visitBottomLeft (iX, iY, iW, iH, qX, qY, qW, qH) = - let - val midX = qW div 2 + qX - val midY = qH div 2 + qY - - val iFinishY = iY + iH - in - iX <= midX andalso iFinishY >= midY - end - - fun visitBottomRight (iX, iY, iW, iH, qX, qY, qW, qH) = - let - val midX = qW div 2 + qX - val midY = qH div 2 + qY - - val iFinishX = iX + iH - val iFinishY = iY + iH - in - iFinishX >= midX andalso iFinishY >= midY - end - - fun foldVec (iX, iY, iW, iH, pos, elements, state, env) = - if pos = Vector.length elements then - state - else - let - val {itemID, ...} = Vector.sub (elements, pos) - val state = Fn.fState (state, env, itemID) - in - foldVec (iX, iY, iW, iH, pos + 1, elements, state, env) - end - - fun foldRegion - ( itemX - , itemY - , itemW - , itemH - , quadX - , quadY - , quadW - , quadH - , env: Fn.env - , state: Fn.state - , tree: QuadTreeType.t - ) = - case tree of - NODE {topLeft, topRight, bottomLeft, bottomRight} => - let - (* fold over intersecting elements in this vector first *) - val halfW = quadW div 2 - val halfH = quadH div 2 - - val midX = halfW + quadX - val midY = halfH + quadY - - val iX = itemX - val iY = itemY - val iW = itemW - val iH = itemH - - val qX = quadX - val qY = quadY - val qW = quadW - val qH = quadH - - val vtl = visitTopLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vtr = visitTopRight (iX, iY, iW, iH, qX, qY, qW, qH) - val vbl = visitBottomLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vbr = visitBottomRight (iX, iY, iW, iH, qX, qY, qW, qH) - - val state = - if vtl then - foldRegion - (iX, iY, iW, iH, qX, qY, halfW, halfH, env, state, topLeft) - else - state - - val state = - if vtr then - foldRegion - ( itemX - , itemY - , itemW - , itemH - , midX - , quadY - , halfW - , halfH - , env - , state - , topRight - ) - else - state - - val state = - if vbl then - foldRegion - ( itemX - , itemY - , itemW - , itemH - , quadX - , midY - , halfW - , halfH - , env - , state - , bottomLeft - ) - else - state - in - if vbr then - foldRegion - ( itemX - , itemY - , itemW - , itemH - , midX - , midY - , halfW - , halfH - , env - , state - , bottomRight - ) - else - state - end - | LEAF elements => - foldVec (itemX, itemY, itemW, itemH, 0, elements, state, env) -end diff --git a/fcore/quad-tree-type.sml b/fcore/quad-tree-type.sml index 4c442db..12fce83 100644 --- a/fcore/quad-tree-type.sml +++ b/fcore/quad-tree-type.sml @@ -3,8 +3,17 @@ sig type item = {itemID: int, startX: int, startY: int, width: int, height: int} datatype t = - NODE of {topLeft: t, topRight: t, bottomLeft: t, bottomRight: t} - | LEAF of item vector + NODE of + { topLeft: t + , topRight: t + , bottomLeft: t + , bottomRight: t + , x: int + , y: int + , w: int + , h: int + } + | LEAF of {items: item vector, x: int, y: int, w: int, h: int} datatype quadrant = TOP_LEFT @@ -19,8 +28,17 @@ struct type item = {itemID: int, startX: int, startY: int, width: int, height: int} datatype t = - NODE of {topLeft: t, topRight: t, bottomLeft: t, bottomRight: t} - | LEAF of item vector + NODE of + { topLeft: t + , topRight: t + , bottomLeft: t + , bottomRight: t + , x: int + , y: int + , w: int + , h: int + } + | LEAF of {items: item vector, x: int, y: int, w: int, h: int} datatype quadrant = TOP_LEFT diff --git a/fcore/quad-tree.sml b/fcore/quad-tree.sml index 9371635..5674ba0 100644 --- a/fcore/quad-tree.sml +++ b/fcore/quad-tree.sml @@ -2,8 +2,6 @@ signature QUAD_TREE = sig type t - val empty: t - datatype collision_side = QUERY_ON_LEFT_SIDE | QUERY_ON_TOP_SIDE @@ -11,33 +9,22 @@ sig | QUERY_ON_BOTTOM_SIDE val insert: int * int * int * int * - int * int * int * int * int * t -> t - val fromItem: int * int * int * int * int -> t - val getCollisions: int * int * int * int * - int * int * int * int * int * t -> int list val helpGetCollisions: int * int * int * int * - int * int * int * int * int * int list * t -> int list - val getCollisionSides: int * int * int * int * int * int * int * int * int * t - -> (collision_side * int) list - - val getCollisionsBelow: int * int * int * int * int * int * int * int * int * t - -> int list - val hasCollisionAt: int * int * int * int * - int * int * int * int * int * t -> bool val getItemID: int * int * int * int * - int * int * int * int * t -> int + + val create: int * int -> t end structure QuadTree: QUAD_TREE = @@ -46,41 +33,97 @@ struct type item = QuadTreeType.item + fun create (width, height) = + LEAF { + items = Vector.fromList [], + x = 0, + y = 0, + w = width, + h = height + } + + fun isColliding (ix, iy, ifx, ify, cx, cy, cfx, cfy) = + ix < cfx andalso + ifx > cx andalso + iy < cfy andalso + ify > cy + + fun isCollidingPlus (ix, iy, iw, ih, cx, cy, cw, ch) = + let + val ifx = ix + iw + val ify = iy + ih + val cfx = cx + cw + val cfy = cy + ch + in + isColliding (ix, iy, ifx, ify, cx, cy, cfx, cfy) + end + fun visitTopLeft (iX, iY, iW, iH, qX, qY, qW, qH) = let - val midX = qW div 2 + qX - val midY = qH div 2 + qY + val hw = qW div 2 + val hh = qH div 2 + + val ifx = iX + iW + val ify = iY + iH + + val qmx = qX + hw + val qmy = qY + hh + + val qfx = qX + qW + val qfy = qY + qH in - iX <= midX andalso iY <= midY + isColliding (iX, iY, ifx, ify, qX, qY, qmx, qmy) end fun visitTopRight (iX, iY, iW, iH, qX, qY, qW, qH) = let - val midX = qW div 2 + qX - val midY = qH div 2 + qY + val hw = qW div 2 + val hh = qH div 2 + + val ifx = iX + iW + val ify = iY + iH + + val qmx = qX + hw + val qmy = qY + hh + + val qfx = qX + qW + val qfy = qY + qH in - iX >= midX andalso iY <= midY + isColliding (iX, iY, ifx, ify, qmx, qY, qfx, qmy) end fun visitBottomLeft (iX, iY, iW, iH, qX, qY, qW, qH) = let - val midX = qW div 2 + qX - val midY = qH div 2 + qY + val hw = qW div 2 + val hh = qH div 2 - val iFinishY = iY + iH + val ifx = iX + iW + val ify = iY + iH + + val qmx = qX + hw + val qmy = qY + hh + + val qfx = qX + qW + val qfy = qY + qH in - iX <= midX andalso iFinishY >= midY + isColliding (iX, iY, ifx, ify, qX, qmy, qmx, qfy) end fun visitBottomRight (iX, iY, iW, iH, qX, qY, qW, qH) = let - val midX = qW div 2 + qX - val midY = qH div 2 + qY + val hw = qW div 2 + val hh = qH div 2 - val iFinishX = iX + iH - val iFinishY = iY + iH + val ifx = iX + iW + val ify = iY + iH + + val qmx = qX + hw + val qmy = qY + hh + + val qfx = qX + qW + val qfy = qY + qH in - iFinishX >= midX andalso iFinishY >= midY + isColliding (iX, iY, ifx, ify, qmx, qmy, qfx, qfy) end fun mkItem (id, startX, startY, width, height) : item = @@ -91,114 +134,93 @@ struct , height = height } - fun itemToString {itemID, startX, startY, width, height} = - String.concat - [ "{itemID = " - , Int.toString itemID - , ", startX = " - , Int.toString startX - , ", startY = " - , Int.toString startY - , ", width = " - , Int.toString width - , ", height = " - , Int.toString height - , "}" - ] - type t = QuadTreeType.t - val empty = LEAF (Vector.fromList []) - - fun fromItem (itemID, startX, startY, width, height) = - let - val item = mkItem (itemID, startX, startY, width, height) - val elements = Vector.fromList [item] - in - LEAF elements - end - (* max size of vector before we split it further *) val maxSize = 3 - fun isItemInQuad (iX, iY, iWidth, iHeight, qX, qY, qWidth, qHeight) = - iX >= qX andalso iY >= qY andalso iWidth <= qWidth - andalso iHeight <= qHeight - - fun whichQuadrant - (itemX, itemY, itemWidth, itemHeight, quadX, quadY, quadWidth, quadHeight) = + fun mkTopLeft (x, y, w, h, items) = let - (* calculate quadrants *) - val halfWidth = quadWidth div 2 - val halfHeight = quadHeight div 2 - - val middleX = quadX + halfWidth - val middleY = quadY + halfHeight - - val isInTopLeft = isItemInQuad - ( itemX - , itemY - , itemWidth - , itemHeight - , quadX - , quadY - , halfWidth - , halfHeight - ) - - val isInTopRight = isItemInQuad - ( itemX - , itemY - , itemWidth - , itemHeight - , middleX - , quadY - , halfWidth - , halfHeight - ) - - val isInBottomLeft = isItemInQuad - ( itemX - , itemY - , itemWidth - , itemHeight - , quadX - , middleY - , halfWidth - , halfHeight - ) - - val isInBottomRight = isItemInQuad - ( itemX - , itemY - , itemWidth - , itemHeight - , middleX - , middleY - , halfWidth - , halfHeight - ) + val items = Vector.fromList items + val hw = w div 2 + val hh = h div 2 in - if isInTopLeft then TOP_LEFT - else if isInTopRight then TOP_RIGHT - else if isInBottomLeft then BOTTOM_LEFT - else if isInBottomRight then BOTTOM_RIGHT - else PARENT_QUADRANT + LEAF { + items = items, + x = x, + y = y, + w = hw, + h = hh + } end - fun splitLeaf (qX, qY, qW, qH, tl, tr, bl, br, elements, pos) = + fun mkTopRight (x, y, w, h, items) = + let + val items = Vector.fromList items + val hw = w div 2 + val hh = h div 2 + val x = x + hw + in + LEAF { + items = items, + x = x, + y = y, + w = hw, + h = hh + } + end + + fun mkBottomLeft (x, y, w, h, items) = + let + val items = Vector.fromList items + val hw = w div 2 + val hh = h div 2 + val y = y + hh + in + LEAF { + items = items, + x = x, + y = y, + w = hw, + h = hh + } + end + + fun mkBottomRight (x, y, w, h, items) = + let + val items = Vector.fromList items + val hw = w div 2 + val hh = h div 2 + val x = x + hw + val y = y + hh + in + LEAF { + items = items, + x = x, + y = y, + w = hw, + h = hh + } + end + + fun splitLeaf (x, y, w, h, tl: item list, tr: item list, bl: item list, br: + item list, elements, pos) = if pos < 0 then let - val tl = Vector.fromList tl - val tr = Vector.fromList tr - val bl = Vector.fromList bl - val br = Vector.fromList br + val tl = mkTopLeft (x, y, w, h, tl) + val tr = mkTopRight (x, y, w, h, tr) + val bl = mkBottomLeft (x, y, w, h, bl) + val br = mkBottomRight (x, y, w, h, br) in NODE - { topLeft = LEAF tl - , topRight = LEAF tr - , bottomLeft = LEAF bl - , bottomRight = LEAF br + { topLeft = tl + , topRight = tr + , bottomLeft = bl + , bottomRight = br + , x = x + , y = y + , w = w + , h = h } end else @@ -206,10 +228,10 @@ struct val item = Vector.sub (elements, pos) val {startX = iX, startY = iY, width = iW, height = iH, ...} = item - val vtl = visitTopLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vtr = visitTopRight (iX, iY, iW, iH, qX, qY, qW, qH) - val vbl = visitBottomLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vbr = visitBottomRight (iX, iY, iW, iH, qX, qY, qW, qH) + val vtl = visitTopLeft (iX, iY, iW, iH, x, y, w, h) + val vtr = visitTopRight (iX, iY, iW, iH, x, y, w, h) + val vbl = visitBottomLeft (iX, iY, iW, iH, x, y, w, h) + val vbr = visitBottomRight (iX, iY, iW, iH, x, y, w, h) val tl = if vtl then item :: tl else tl @@ -219,159 +241,83 @@ struct val br = if vbr then item :: br else br in - splitLeaf (qX, qY, qW, qH, tl, tr, bl, br, elements, pos - 1) + splitLeaf (x, y, w, h, tl, tr, bl, br, elements, pos - 1) end - fun insert - ( itemX - , itemY - , itemWidth - , itemHeight - , quadX - , quadY - , quadWidth - , quadHeight - , itemID - , tree: t - ) = + fun insert (iX, iY, iW, iH, itemID, tree: t) = case tree of - NODE {topLeft, topRight, bottomLeft, bottomRight} => + NODE {topLeft, topRight, bottomLeft, bottomRight, x, y, w, h} => + if isCollidingPlus (iX, iY, iW, iH, x, y, w, h) then let - val halfW = quadWidth div 2 - val halfH = quadHeight div 2 - - val midX = halfW + quadX - val midY = halfH + quadY - - val iX = itemX - val iY = itemY - val iW = itemWidth - val iH = itemHeight - - val qX = quadX - val qY = quadY - val qW = quadWidth - val qH = quadHeight - - val vtl = visitTopLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vtr = visitTopRight (iX, iY, iW, iH, qX, qY, qW, qH) - val vbl = visitBottomLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vbr = visitBottomRight (iX, iY, iW, iH, qX, qY, qW, qH) - - val tl = - if vtl then - insert (iX, iY, iW, iH, qX, qY, halfW, halfH, itemID, topLeft) - else - topLeft - - val tr = - if vtr then - insert (iX, iY, iW, iH, midX, qY, halfW, halfH, itemID, topRight) - else - topRight - - val bl = - if vbl then - insert - (iX, iY, iW, iH, qX, midY, halfW, halfH, itemID, bottomLeft) - else - bottomLeft - - val br = - if vbr then - insert - (iX, iY, iW, iH, midX, midY, halfW, halfH, itemID, bottomRight) - else - bottomRight + (* we are not necessarily inserting into all nodes. + * If isCollidingPlus returns false recursively, + * we return the same node back. *) + val tl = insert (iX, iY, iW, iH, itemID, topLeft) + val tr = insert (iX, iY, iW, iH, itemID, topRight) + val bl = insert (iX, iY, iW, iH, itemID, bottomLeft) + val br = insert (iX, iY, iW, iH, itemID, bottomRight) in - NODE {topLeft = tl, topRight = tr, bottomLeft = bl, bottomRight = br} + NODE {topLeft = tl, topRight = tr, bottomLeft = bl, bottomRight = br + , x = x, y = y, w = w, h = h + } end - | LEAF elements => - if Vector.length elements + 1 > maxSize then - (* have to calculate quadrants and split *) - let - val pos = Vector.length elements - 1 - val item = mkItem (itemID, itemX, itemY, itemWidth, itemHeight) + else + tree + | LEAF {items, x, y, w, h} => + if isCollidingPlus (iX, iY, iW, iH, x, y, w, h) then + if Vector.length items + 1 > maxSize then + (* have to calculate quadrants and split *) + let + val pos = Vector.length items - 1 + val item = mkItem (itemID, iX, iY, iW, iH) - val halfW = quadWidth div 2 - val halfH = quadHeight div 2 + val vtl = visitTopLeft (iX, iY, iW, iH, x, y, w, h) + val vtr = visitTopRight (iX, iY, iW, iH, x, y, w, h) + val vbl = visitBottomLeft (iX, iY, iW, iH, x, y, w, h) + val vbr = visitBottomRight (iX, iY, iW, iH, x, y, w, h) - val midX = halfW + quadX - val midY = halfH + quadY + val tl = if vtl then [item] else [] - val iX = itemX - val iY = itemY - val iW = itemWidth - val iH = itemHeight + val tr = if vtr then [item] else [] - val qX = quadX - val qY = quadY - val qW = quadWidth - val qH = quadHeight + val bl = if vbl then [item] else [] - val vtl = visitTopLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vtr = visitTopRight (iX, iY, iW, iH, qX, qY, qW, qH) - val vbl = visitBottomLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vbr = visitBottomRight (iX, iY, iW, iH, qX, qY, qW, qH) - - val pos = Vector.length elements - 1 - val item = mkItem (itemID, itemX, itemY, itemWidth, itemHeight) - - val tl = if vtl then [item] else [] - - val tr = if vtr then [item] else [] - - val bl = if vbl then [item] else [] - - val br = if vbr then [item] else [] - - val pe = [] - in - splitLeaf - ( quadX - , quadY - , quadWidth - , quadHeight - , tl - , tr - , bl - , br - , elements - , pos - ) - end - else - (* can insert itemID in elements vector *) - let - val item = mkItem (itemID, itemX, itemY, itemWidth, itemHeight) - val elements = Vector.concat [elements, Vector.fromList [item]] - in - LEAF elements - end - - fun isBetween (start, checkStart, finish, checkFinish) = - (* if check containhs start/finish *) - (checkStart <= start andalso checkFinish >= finish) - orelse - (* if start/finish containhs check *) - (start <= checkStart andalso finish >= checkFinish) - orelse - (* if checkStart between start and finish *) - (start <= checkStart andalso finish >= checkStart) - orelse - (* if checkFinish is between start and finish *) - (start <= checkFinish andalso finish >= checkFinish) + val br = if vbr then [item] else [] + in + splitLeaf + ( x + , y + , w + , h + , tl + , tr + , bl + , br + , items + , pos + ) + end + else + (* can insert itemID in items vector *) + let + val item = mkItem (itemID, iX, iY, iW, iH) + val items = Vector.concat [items, Vector.fromList [item]] + in + LEAF {items = items, x = x, y = y, w = w, h = h} + end + else + (* bounds of new item don't fit inside leaf so return old tree *) + tree fun isColliding (iX, iY, iW, iH, itemID, checkWith: item) = let - val itemEndX = iX + iW - val itemEndY = iY + iH - val {itemID = checkID, startX, startY, width, height, ...} = checkWith - val endX = startX + width - val endY = startY + height + val {itemID = checkID, startX = cX, startY = cY, width = cW, height = cH, ...} = checkWith in - isBetween (iX, startX, itemEndX, endX) - andalso isBetween (iY, startY, itemEndY, endY) andalso itemID <> checkID + iX < cX + cW andalso + iX + iW > cX andalso + iY < cY + cH andalso + iY + iH > cY andalso + itemID <> checkID end fun getCollisionsVec (iX, iY, iW, iH, itemID, pos, elements, acc) = @@ -387,130 +333,86 @@ struct getCollisionsVec (iX, iY, iW, iH, itemID, pos + 1, elements, acc) end - fun getCollisionsAll (iX, iY, iW, iH, qW, qH, itemID, acc, tree) = + fun getCollisionsAll (iX, iY, iW, iH, itemID, acc, tree) = case tree of - NODE {topLeft, topRight, bottomLeft, bottomRight} => - let - val halfWidth = qW div 2 - val halfHeight = qH div 2 + NODE {topLeft, topRight, bottomLeft, bottomRight, x, y, w, h} => + if isCollidingPlus (iX, iY, iW, iH, x, y, w, h) then + let + val acc = getCollisionsAll + (iX, iY, iW, iH, itemID, acc, topLeft) - val acc = getCollisionsAll - (iX, iY, iW, iH, halfWidth, halfHeight, itemID, acc, topLeft) + val acc = getCollisionsAll + (iX, iY, iW, iH, itemID, acc, topRight) - val acc = getCollisionsAll - (iX, iY, iW, iH, halfWidth, halfHeight, itemID, acc, topRight) - - val acc = getCollisionsAll - (iX, iY, iW, iH, halfWidth, halfHeight, itemID, acc, bottomLeft) - in - getCollisionsAll - (iX, iY, iW, iH, halfWidth, halfWidth, itemID, acc, bottomRight) - end - | LEAF elements => - getCollisionsVec (iX, iY, iW, iH, itemID, 0, elements, acc) + val acc = getCollisionsAll + (iX, iY, iW, iH, itemID, acc, bottomLeft) + in + getCollisionsAll + (iX, iY, iW, iH, itemID, acc, bottomRight) + end + else + acc + | LEAF {items, x, y, w, h} => + if isCollidingPlus (iX, iY, iW, iH, x, y, w, h) then + getCollisionsVec (iX, iY, iW, iH, itemID, 0, items, acc) + else acc fun helpGetCollisions - ( itemX - , itemY - , itemWidth - , itemHeight - , quadX - , quadY - , quadWidth - , quadHeight + ( iX + , iY + , iW + , iH , itemID , acc , tree: t ) = case tree of - NODE {topLeft, topRight, bottomLeft, bottomRight} => + NODE {topLeft, topRight, bottomLeft, bottomRight, x, y, w, h} => + if isCollidingPlus (iX, iY, iW, iH, x, y, w, h) then let - val halfW = quadWidth div 2 - val halfH = quadHeight div 2 - - val midX = halfW + quadX - val midY = halfH + quadY - - val iX = itemX - val iY = itemY - val iW = itemWidth - val iH = itemHeight - - val qX = quadX - val qY = quadY - val qW = quadWidth - val qH = quadHeight - - val vtl = visitTopLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vtr = visitTopRight (iX, iY, iW, iH, qX, qY, qW, qH) - val vbl = visitBottomLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vbr = visitBottomRight (iX, iY, iW, iH, qX, qY, qW, qH) - val acc = - if vtl then helpGetCollisions - (iX, iY, iW, iH, qX, qY, halfW, halfH, itemID, acc, topLeft) - else - acc + (iX, iY, iW, iH, itemID, acc, topLeft) val acc = - if vtr then helpGetCollisions - (iX, iY, iW, iH, midX, qY, halfW, halfH, itemID, acc, topRight) - else - acc + (iX, iY, iW, iH, itemID, acc, topRight) val acc = - if vbl then helpGetCollisions ( iX , iY , iW , iH - , qX - , midY - , halfW - , halfH , itemID , acc , bottomLeft ) - else - acc - - val acc = - if vbl then + in helpGetCollisions ( iX , iY , iW , iH - , midX - , midY - , halfW - , halfH , itemID , acc , bottomRight ) - else - acc - in - acc end - | LEAF elements => - getCollisionsVec - (itemX, itemY, itemWidth, itemHeight, itemID, 0, elements, acc) + else + acc + | LEAF {items, x, y, w, h} => + if isCollidingPlus (iX, iY, iW, iH, x, y, w, h) then + getCollisionsVec + (iX, iY, iW, iH, itemID, 0, items, acc) + else + acc fun getCollisions ( itemX , itemY , itemWidth , itemHeight - , quadX - , quadY - , quadWidth - , quadHeight , itemID , tree ) = @@ -519,10 +421,6 @@ struct , itemY , itemWidth , itemHeight - , quadX - , quadY - , quadWidth - , quadHeight , itemID , [] , tree @@ -576,328 +474,6 @@ struct QUERY_ON_BOTTOM_SIDE end - (* like getCollisionsVec, but instead of consing just the itemID, - * it also conses the "collision-side" information. - * *) - fun getCollisionSideVec (iX, iY, iW, iH, itemID, pos, elements, acc) = - if pos = Vector.length elements then - acc - else - let - val item = Vector.sub (elements, pos) - val acc = - if isColliding (iX, iY, iW, iH, itemID, item) then - let val side = getCollisionSide (iX, iY, iW, iH, item) - in (side, #itemID item) :: acc - end - else - acc - in - getCollisionSideVec (iX, iY, iW, iH, itemID, pos + 1, elements, acc) - end - - fun getCollisionSidesAll (iX, iY, iW, iH, qW, qH, itemID, acc, tree) = - case tree of - NODE {topLeft, topRight, bottomLeft, bottomRight} => - let - val halfWidth = qW div 2 - val halfHeight = qH div 2 - - val acc = getCollisionSidesAll - (iX, iY, iW, iH, halfWidth, halfHeight, itemID, acc, topLeft) - - val acc = getCollisionSidesAll - (iX, iY, iW, iH, halfWidth, halfHeight, itemID, acc, topRight) - - val acc = getCollisionSidesAll - (iX, iY, iW, iH, halfWidth, halfHeight, itemID, acc, bottomLeft) - in - getCollisionSidesAll - (iX, iY, iW, iH, halfWidth, halfWidth, itemID, acc, bottomRight) - end - | LEAF elements => - getCollisionSideVec (iX, iY, iW, iH, itemID, 0, elements, acc) - - fun helpGetCollisionSides - ( itemX - , itemY - , itemWidth - , itemHeight - , quadX - , quadY - , quadWidth - , quadHeight - , itemID - , acc - , tree: t - ) = - case tree of - NODE {topLeft, topRight, bottomLeft, bottomRight} => - let - val halfW = quadWidth div 2 - val halfH = quadHeight div 2 - - val midX = halfW + quadX - val midY = halfH + quadY - - val iX = itemX - val iY = itemY - val iW = itemWidth - val iH = itemHeight - - val qX = quadX - val qY = quadY - val qW = quadWidth - val qH = quadHeight - - val vtl = visitTopLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vtr = visitTopRight (iX, iY, iW, iH, qX, qY, qW, qH) - val vbl = visitBottomLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vbr = visitBottomRight (iX, iY, iW, iH, qX, qY, qW, qH) - - val acc = - if vtl then - helpGetCollisionSides - (iX, iY, iW, iH, qX, qY, halfW, halfH, itemID, acc, topLeft) - else - acc - - val acc = - if vtr then - helpGetCollisionSides - (iX, iY, iW, iH, midX, qY, halfW, halfH, itemID, acc, topRight) - else - acc - - val acc = - if vbl then - helpGetCollisionSides - ( iX - , iY - , iW - , iH - , qX - , midY - , halfW - , halfH - , itemID - , acc - , bottomLeft - ) - else - acc - - val acc = - if vbl then - helpGetCollisionSides - ( iX - , iY - , iW - , iH - , midX - , midY - , halfW - , halfH - , itemID - , acc - , bottomRight - ) - else - acc - in - acc - end - | LEAF elements => - getCollisionSideVec - (itemX, itemY, itemWidth, itemHeight, itemID, 0, elements, acc) - - fun getCollisionSides - ( itemX - , itemY - , itemWidth - , itemHeight - , quadX - , quadY - , quadWidth - , quadHeight - , itemID - , tree - ) = - helpGetCollisionSides - ( itemX - , itemY - , itemWidth - , itemHeight - , quadX - , quadY - , quadWidth - , quadHeight - , itemID - , [] - , tree - ) - - fun getCollisionsBelowVec (iX, iY, iW, iH, itemID, pos, elements, acc) = - if pos = Vector.length elements then - acc - else - let - val item = Vector.sub (elements, pos) - val {itemID = curID, ...} = item - in - if isColliding (iX, iY, iW, iH, itemID, item) then - case getCollisionSide (iX, iY, iW, iH, item) of - QUERY_ON_BOTTOM_SIDE => - getCollisionsBelowVec - (iX, iY, iW, iH, itemID, pos + 1, elements, curID :: acc) - | _ => - getCollisionsBelowVec - (iX, iY, iW, iH, itemID, pos + 1, elements, acc) - else - getCollisionsBelowVec (iX, iY, iW, iH, itemID, pos + 1, elements, acc) - end - - fun getCollisionsBelowAll (iX, iY, iW, iH, qW, qH, itemID, acc, tree) = - case tree of - NODE {topLeft, topRight, bottomLeft, bottomRight} => - let - val halfWidth = qW div 2 - val halfHeight = qH div 2 - - val acc = getCollisionsBelowAll - (iX, iY, iW, iH, halfWidth, halfHeight, itemID, acc, topLeft) - - val acc = getCollisionsBelowAll - (iX, iY, iW, iH, halfWidth, halfHeight, itemID, acc, topRight) - - val acc = getCollisionsBelowAll - (iX, iY, iW, iH, halfWidth, halfHeight, itemID, acc, bottomLeft) - in - getCollisionsBelowAll - (iX, iY, iW, iH, halfWidth, halfWidth, itemID, acc, bottomRight) - end - | LEAF elements => - getCollisionsBelowVec (iX, iY, iW, iH, itemID, 0, elements, acc) - - fun helpGetCollisionsBelow - ( itemX - , itemY - , itemWidth - , itemHeight - , quadX - , quadY - , quadWidth - , quadHeight - , itemID - , acc - , tree: t - ) = - case tree of - NODE {topLeft, topRight, bottomLeft, bottomRight} => - let - val halfW = quadWidth div 2 - val halfH = quadHeight div 2 - - val midX = halfW + quadX - val midY = halfH + quadY - - val iX = itemX - val iY = itemY - val iW = itemWidth - val iH = itemHeight - - val qX = quadX - val qY = quadY - val qW = quadWidth - val qH = quadHeight - - val vtl = visitTopLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vtr = visitTopRight (iX, iY, iW, iH, qX, qY, qW, qH) - val vbl = visitBottomLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vbr = visitBottomRight (iX, iY, iW, iH, qX, qY, qW, qH) - - val acc = - if vtl then - helpGetCollisionsBelow - (iX, iY, iW, iH, qX, qY, halfW, halfH, itemID, acc, topLeft) - else - acc - - val acc = - if vtr then - helpGetCollisionsBelow - (iX, iY, iW, iH, midX, qY, halfW, halfH, itemID, acc, topRight) - else - acc - - val acc = - if vbl then - helpGetCollisionsBelow - ( iX - , iY - , iW - , iH - , qX - , midY - , halfW - , halfH - , itemID - , acc - , bottomLeft - ) - else - acc - - val acc = - if vbl then - helpGetCollisionsBelow - ( iX - , iY - , iW - , iH - , midX - , midY - , halfW - , halfH - , itemID - , acc - , bottomRight - ) - else - acc - in - acc - end - | LEAF elements => - getCollisionsBelowVec - (itemX, itemY, itemWidth, itemHeight, itemID, 0, elements, acc) - - fun getCollisionsBelow - ( itemX - , itemY - , itemWidth - , itemHeight - , quadX - , quadY - , quadWidth - , quadHeight - , itemID - , tree - ) = - helpGetCollisionsBelow - ( itemX - , itemY - , itemWidth - , itemHeight - , quadX - , quadY - , quadWidth - , quadHeight - , itemID - , [] - , tree - ) - fun hasCollisionAtVec (iX, iY, iW, iH, itemID, pos, elements) = if pos = Vector.length elements then false @@ -905,86 +481,38 @@ struct let val item = Vector.sub (elements, pos) in - if isColliding (iX, iY, iW, iH, itemID, item) then - let - val _ = print - ("quad-tree.sml: has collision: \n" ^ itemToString item ^ "\n") - in - true - end - else - hasCollisionAtVec (iX, iY, iW, iH, itemID, pos + 1, elements) + isColliding (iX, iY, iW, iH, itemID, item) orelse + hasCollisionAtVec (iX, iY, iW, iH, itemID, pos + 1, elements) end fun hasCollisionAt - ( itemX - , itemY - , itemWidth - , itemHeight - , quadX - , quadY - , quadWidth - , quadHeight + ( iX + , iY + , iW + , iH , itemID , tree ) = case tree of - NODE {topLeft, topRight, bottomLeft, bottomRight} => - let - val halfW = quadWidth div 2 - val halfH = quadHeight div 2 - - val midX = halfW + quadX - val midY = halfH + quadY - - val iX = itemX - val iY = itemY - val iW = itemWidth - val iH = itemHeight - - val qX = quadX - val qY = quadY - val qW = quadWidth - val qH = quadHeight - - val vtl = visitTopLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vtr = visitTopRight (iX, iY, iW, iH, qX, qY, qW, qH) - val vbl = visitBottomLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vbr = visitBottomRight (iX, iY, iW, iH, qX, qY, qW, qH) - - val tl = - if vtl then - hasCollisionAt - (iX, iY, iW, iH, qX, qY, halfW, halfH, itemID, topLeft) - else - false - - val tr = - if vtr then - hasCollisionAt - (iX, iY, iW, iH, midX, qY, halfW, halfH, itemID, topRight) - else - false - - val bl = - if vbl then - hasCollisionAt - (iX, iY, iW, iH, qX, midY, halfW, halfH, itemID, bottomLeft) - else - false - - val br = - if vbl then - hasCollisionAt - (iX, iY, iW, iH, midX, midY, halfW, halfH, itemID, bottomRight) - else - false - in - tl orelse tr orelse bl orelse br - end - | LEAF elements => - hasCollisionAtVec - (itemX, itemY, itemWidth, itemHeight, itemID, 0, elements) + NODE {topLeft, topRight, bottomLeft, bottomRight, x, y, w, h} => + if isCollidingPlus (iX, iY, iW, iH, x, y, w, h) then + hasCollisionAt + (iX, iY, iW, iH, itemID, topLeft) + orelse + hasCollisionAt + (iX, iY, iW, iH, itemID, topRight) + orelse + hasCollisionAt + (iX, iY, iW, iH, itemID, bottomLeft) + orelse + hasCollisionAt + (iX, iY, iW, iH, itemID, bottomRight) + else + false + | LEAF {items, x, y, w, h} => + if isCollidingPlus (iX, iY, iW, iH, x, y, w, h) then + hasCollisionAtVec (iX, iY, iW, iH, itemID, 0, items) + else false fun getItemIDVec (iX, iY, iW, iH, pos, elements) = if pos = Vector.length elements then @@ -997,62 +525,29 @@ struct else getItemIDVec (iX, iY, iW, iH, pos + 1, elements) end - fun getItemID (itemX, itemY, itemW, itemH, quadX, quadY, quadW, quadH, tree) = + fun getItemID (iX, iY, iW, iH, tree) = case tree of - NODE {topLeft, topRight, bottomLeft, bottomRight} => - let - val halfW = quadW div 2 - val halfH = quadH div 2 + NODE {topLeft, topRight, bottomLeft, bottomRight, x, y, w, h} => + if isCollidingPlus (iX, iY, iW, iH, x, y, w, h) then + let + val try1 = getItemID (iX, iY, iW, iH, topLeft) + val try2 = getItemID (iX, iY, iW, iH, topRight) + val try3 = getItemID (iX, iY, iW, iH, bottomLeft) + val try4 = getItemID (iX, iY, iW, iH, bottomRight) - val midX = halfW + quadX - val midY = halfH + quadY - - val iX = itemX - val iY = itemY - val iW = itemW - val iH = itemH - - val qX = quadX - val qY = quadY - val qW = quadW - val qH = quadH - - val vtl = visitTopLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vtr = visitTopRight (iX, iY, iW, iH, qX, qY, qW, qH) - val vbl = visitBottomLeft (iX, iY, iW, iH, qX, qY, qW, qH) - val vbr = visitBottomRight (iX, iY, iW, iH, qX, qY, qW, qH) - - val try1 = - if vtl then - getItemID (iX, iY, iW, iH, qX, qY, halfW, halfH, topLeft) - else - ~1 - - val try2 = - if vtr then - getItemID (iX, iY, iW, iH, midX, qY, halfW, halfH, topRight) - else - ~1 - - val try3 = - if vbl then - getItemID (iX, iY, iW, iH, qX, midY, halfW, halfH, bottomLeft) - else - ~1 - - val try4 = - if vbl then - getItemID (iX, iY, iW, iH, midX, midY, halfW, halfH, bottomRight) - else - ~1 - - (* get max: we assume query was narrow enough - * that only one ID is valid *) - val a = Int.max (try1, try2) - val a = Int.max (a, try3) - val a = Int.max (a, try4) - in - a - end - | LEAF elements => getItemIDVec (itemX, itemY, itemW, itemH, 0, elements) + (* get max: we assume query was narrow enough + * that only one ID is valid *) + val a = Int.max (try1, try2) + val a = Int.max (a, try3) + val a = Int.max (a, try4) + in + a + end + else + ~1 + | LEAF {items, x, y, w, h} => + if isCollidingPlus (iX, iY, iW, iH, x, y, w, h) then + getItemIDVec (iX, iY, iW, iH, 0, items) + else + ~1 end diff --git a/fcore/wall.sml b/fcore/wall.sml index 5ba2ff1..e558937 100644 --- a/fcore/wall.sml +++ b/fcore/wall.sml @@ -6,8 +6,8 @@ struct else let val {id, x, y, width, height} = Vector.sub (wallVec, pos) - val acc = QuadTree.insert - (x, y, width, height, 0, 0, 1920, 1080, id, acc) + val acc = QuadHelp.insert + (x, y, width, height, id, acc) in helpGenerateTree (pos + 1, wallVec, acc) end diff --git a/ffi/export.h b/ffi/export.h index 9c2eec7..b7a07c9 100644 --- a/ffi/export.h +++ b/ffi/export.h @@ -157,8 +157,6 @@ typedef Pointer Objptr; extern "C" { #endif -MLLIB_PUBLIC(void mltonKeyCallback (Int32 x0, Int32 x1, Int32 x2, Int32 x3);) -MLLIB_PUBLIC(void mltonFramebufferSizeCallback (Real32 x0, Real32 x1);) #undef MLLIB_PRIVATE #undef MLLIB_PUBLIC diff --git a/oms.mlb b/oms.mlb index d8547de..6270485 100644 --- a/oms.mlb +++ b/oms.mlb @@ -5,7 +5,6 @@ fcore/constants.sml fcore/quad-tree-type.sml fcore/quad-tree.sml -fcore/quad-tree-fold.sml fcore/bin-search.sml fcore/bin-vec.sml