Refactor path zoning

This commit is contained in:
2022-07-23 13:47:54 +01:00
parent 94d5691f46
commit d8b1a0c71e
10 changed files with 85 additions and 25 deletions
+4 -2
View File
@@ -236,8 +236,10 @@ data World = World
, _corpses :: IM.IntMap Corpse , _corpses :: IM.IntMap Corpse
, _clickMousePos :: Point2 , _clickMousePos :: Point2
, _pathGraph :: Gr Point2 PathEdge , _pathGraph :: Gr Point2 PathEdge
, _pnZoning :: Zoning [] (Int,Point2) --, _pnZoning :: Zoning [] (Int,Point2)
, _peZoning :: Zoning [] (Int,Int,PathEdge) --, _peZoning :: Zoning [] (Int,Int,PathEdge)
, _pnZoning :: IM.IntMap (IM.IntMap [(Int,Point2)]) --Zoning IM.IntMap Creature
, _peZoning :: IM.IntMap (IM.IntMap [(Int,Int,PathEdge)]) --Zoning IM.IntMap Creature
, _hud :: HUD , _hud :: HUD
, _lightSources :: IM.IntMap LightSource , _lightSources :: IM.IntMap LightSource
, _tempLightSources :: [TempLightSource] , _tempLightSources :: [TempLightSource]
+2 -6
View File
@@ -1,9 +1,5 @@
module Dodge.Default.World where module Dodge.Default.World where
import Dodge.Data import Dodge.Data
import Dodge.Zone.Size
import Dodge.Zone.Object
--import Geometry.Vector3D
import Geometry.Zone
import Geometry.Data import Geometry.Data
import Geometry.Polygon import Geometry.Polygon
@@ -76,8 +72,8 @@ defaultWorld = World
, _clickMousePos = V2 0 0 , _clickMousePos = V2 0 0
, _pathGraph = Data.Graph.Inductive.Graph.empty , _pathGraph = Data.Graph.Inductive.Graph.empty
-- , _pathGraphP = mempty -- , _pathGraphP = mempty
, _pnZoning = Zoning mempty peZoneSize (zonePos snd) , _pnZoning = mempty
, _peZoning = Zoning mempty peZoneSize (\x (_,_,e) -> zoneOfSeg x (_peStart e) (_peEnd e)) , _peZoning = mempty
, _hud = HUD , _hud = HUD
{ _hudElement = DisplayInventory NoSubInventory { _hudElement = DisplayInventory NoSubInventory
, _carteCenter = V2 0 0 , _carteCenter = V2 0 0
+3 -5
View File
@@ -2,10 +2,10 @@
module Dodge.Layout module Dodge.Layout
( generateLevelFromRoomList ( generateLevelFromRoomList
) where ) where
import Dodge.Zoning.Pathing
import Data.Tile import Data.Tile
import Dodge.Data import Dodge.Data
import Dodge.Path import Dodge.Path
import Dodge.Zone.Update
import Dodge.ShiftPoint import Dodge.ShiftPoint
import Dodge.Placement.PlaceSpot import Dodge.Placement.PlaceSpot
--import Dodge.LevelGen.Data --import Dodge.LevelGen.Data
@@ -45,10 +45,8 @@ generateLevelFromRoomList gr' w = initWallZoning
, _gameRooms = gameRoomsFromRooms (IM.elems rs') , _gameRooms = gameRoomsFromRooms (IM.elems rs')
, _pathGraph = path , _pathGraph = path
} }
& pnZoning %~ (\zn -> foldl' (flip $ updateZoning (:)) (zn & znObjects .~ mempty) & pnZoning .~ foldl' (flip $ zonePn) mempty (labNodes path)
(labNodes path)) & peZoning .~ foldl' (flip $ zonePe) mempty (labEdges path)
& peZoning %~ (\zn -> foldl' (flip $ updateZoning (:)) (zn & znObjects .~ mempty)
(labEdges path))
where where
(_,path) = pairsToGraph pairPath' (_,path) = pairsToGraph pairPath'
pairPath = foldMap _rmPath rs pairPath = foldMap _rmPath rs
+6 -5
View File
@@ -11,9 +11,10 @@ module Dodge.Path
, bfsNodePoints , bfsNodePoints
, fusePairs , fusePairs
) where ) where
import Dodge.Zoning.Pathing
import Dodge.Zoning.Base
import Dodge.Data import Dodge.Data
import Dodge.Base.Collide import Dodge.Base.Collide
import Dodge.Zone
import Geometry.Data import Geometry.Data
import Geometry import Geometry
@@ -52,7 +53,7 @@ walkableNodeNear :: World -> Point2 -> Maybe Int
walkableNodeNear w p = fmap fst . find (flip (isWalkable p) w . snd) $ nodesNear walkableNodeNear w p = fmap fst . find (flip (isWalkable p) w . snd) $ nodesNear
where where
--nodesNear = runIdentity . S.toList_ $ nearPoint _pnZoning p w --nodesNear = runIdentity . S.toList_ $ nearPoint _pnZoning p w
nodesNear = runIdentity . S.toList_ $ aroundPoint _pnZoning p w nodesNear = zonesExtract (w ^. pnZoning) $ zonesAroundPoint pnZoneSize p
makePathBetweenPs :: Point2 -> Point2 -> World -> Maybe [Point2] makePathBetweenPs :: Point2 -> Point2 -> World -> Maybe [Point2]
makePathBetweenPs a b w = mapMaybe (lab $ _pathGraph w) <$> makePathBetween a b w makePathBetweenPs a b w = mapMaybe (lab $ _pathGraph w) <$> makePathBetween a b w
@@ -137,12 +138,12 @@ addEdges nodemap gr = runIdentity . S.fold_ f (mempty,gr) id
obstructPathsCrossing :: EdgeObstacle -> Point2 -> Point2 -> World -> ( World, [(Int,Int,PathEdge)]) obstructPathsCrossing :: EdgeObstacle -> Point2 -> Point2 -> World -> ( World, [(Int,Int,PathEdge)])
obstructPathsCrossing obstacletype sp' ep w = obstructPathsCrossing obstacletype sp' ep w =
( w & pathGraph %~ updateedges ( w & pathGraph %~ updateedges
, runIdentity $ S.toList_ es , es
) )
where where
es = S.filter edgecrosses $ nearSeg _peZoning sp' ep w es = filter edgecrosses $ pesNearSeg sp' ep w
edgecrosses (_,_,pe) = isJust $ intersectSegSeg sp' ep (_peStart pe) (_peEnd pe) edgecrosses (_,_,pe) = isJust $ intersectSegSeg sp' ep (_peStart pe) (_peEnd pe)
updateedges gr = runIdentity $ S.fold_ updateedge gr id es updateedges gr = foldl' updateedge gr es
updateedge gr (x,y,pe) updateedge gr (x,y,pe)
= insEdge (x,y,pe & peObstacles . at obstacletype ?~ ()) $ delEdge (x,y) gr = insEdge (x,y,pe & peObstacles . at obstacletype ?~ ()) $ delEdge (x,y) gr
+1 -1
View File
@@ -1,7 +1,7 @@
module Dodge.Render.ShapePicture module Dodge.Render.ShapePicture
( worldSPic ( worldSPic
) where ) where
import Dodge.Zoning.Wall import Dodge.Zoning
import Dodge.Button.Draw import Dodge.Button.Draw
import Dodge.LightSource.Draw import Dodge.LightSource.Draw
import Dodge.Targeting.Draw import Dodge.Targeting.Draw
+4 -4
View File
@@ -1,10 +1,10 @@
module Dodge.Zone.Size where module Dodge.Zone.Size where
pnZoneSize :: Float --pnZoneSize :: Float
pnZoneSize = 100 --pnZoneSize = 100
clZoneSize :: Float clZoneSize :: Float
clZoneSize = 20 clZoneSize = 20
peZoneSize :: Float --peZoneSize :: Float
peZoneSize = 50 --peZoneSize = 50
+2
View File
@@ -1,7 +1,9 @@
module Dodge.Zoning module Dodge.Zoning
( module Dodge.Zoning.Creature ( module Dodge.Zoning.Creature
, module Dodge.Zoning.Wall , module Dodge.Zoning.Wall
, module Dodge.Zoning.Pathing
) where ) where
import Dodge.Zoning.Creature import Dodge.Zoning.Creature
import Dodge.Zoning.Wall import Dodge.Zoning.Wall
import Dodge.Zoning.Pathing
+6 -1
View File
@@ -65,6 +65,11 @@ zoneMonoid (V2 x y) a = IM.insertWith f x $ IM.singleton y a
deZoneIX :: Int -> IM.IntMap (IM.IntMap IS.IntSet) -> Int2 -> IM.IntMap (IM.IntMap IS.IntSet) deZoneIX :: Int -> IM.IntMap (IM.IntMap IS.IntSet) -> Int2 -> IM.IntMap (IM.IntMap IS.IntSet)
deZoneIX i im (V2 x y) = im & ix x . ix y %~ IS.delete i deZoneIX i im (V2 x y) = im & ix x . ix y %~ IS.delete i
zoneOfPoint'' :: Float -> Point2 -> V2 Int zoneOfPoint'' :: Float -> Point2 -> Int2
{-# INLINE zoneOfPoint'' #-} {-# INLINE zoneOfPoint'' #-}
zoneOfPoint'' s = fmap (divTo s) zoneOfPoint'' s = fmap (divTo s)
zonesAroundPoint :: Float -> Point2 -> [Int2]
zonesAroundPoint s p = [V2 a b | a <- [x-1..x+1] , b <- [y-1..y+1] ]
where
V2 x y = zoneOfPoint' s p
+1 -1
View File
@@ -5,7 +5,7 @@ import Dodge.Zoning.Base
import Dodge.Data import Dodge.Data
import Geometry import Geometry
import Data.Foldable --import Data.Foldable
import Control.Lens import Control.Lens
import qualified IntMapHelp as IM import qualified IntMapHelp as IM
+56
View File
@@ -0,0 +1,56 @@
module Dodge.Zoning.Pathing
where
import Geometry.Vector
import Dodge.Zoning.Base
import Dodge.Data
import Geometry
import Data.Foldable
import Control.Lens
import qualified IntMapHelp as IM
pnsNearPoint :: Point2 -> World -> [(Int,Point2)]
pnsNearPoint p w = zoneExtract (zoneOfPoint' pnZoneSize p) (w ^. pnZoning)
pnsNearSeg :: Point2 -> Point2 -> World -> [(Int,Point2)]
pnsNearSeg sp ep w = zonesExtract (w ^. pnZoning) (zoneOfSeg' pnZoneSize sp ep)
pnsNearRect :: Point2 -> Point2 -> World -> [(Int,Point2)]
pnsNearRect sp ep w = zonesExtract (w ^. pnZoning) $ zoneOfRect' pnZoneSize sp ep
pnsNearCirc :: Point2 -> Float -> World -> [(Int,Point2)]
pnsNearCirc p r = pnsNearRect (p +.+ V2 r r) (p -.- V2 r r)
pnZoneSize :: Float
pnZoneSize = 50
zoneOfPn :: (Int,Point2) -> Int2
zoneOfPn = zoneOfPoint'' pnZoneSize . snd
zonePn :: (Int,Point2) -> IM.IntMap (IM.IntMap [(Int,Point2)]) -> IM.IntMap (IM.IntMap [(Int,Point2)])
zonePn pn = zoneMonoid (zoneOfPn pn) [pn]
pesNearPoint :: Point2 -> World -> [(Int,Int,PathEdge)]
pesNearPoint p w = zoneExtract (zoneOfPoint' peZoneSize p) (w ^. peZoning)
pesNearSeg :: Point2 -> Point2 -> World -> [(Int,Int,PathEdge)]
pesNearSeg sp ep w = zonesExtract (w ^. peZoning) (zoneOfSeg' peZoneSize sp ep)
pesNearRect :: Point2 -> Point2 -> World -> [(Int,Int,PathEdge)]
pesNearRect sp ep w = zonesExtract (w ^. peZoning) $ zoneOfRect' peZoneSize sp ep
pesNearCirc :: Point2 -> Float -> World -> [(Int,Int,PathEdge)]
pesNearCirc p r = pesNearRect (p +.+ V2 r r) (p -.- V2 r r)
peZoneSize :: Float
peZoneSize = 50
zoneOfPe :: (Int,Int,PathEdge) -> [Int2]
zoneOfPe (_,_,pe) = zoneOfSeg' peZoneSize (_peStart pe) (_peEnd pe)
zonePe :: (Int,Int,PathEdge) -> IM.IntMap (IM.IntMap [(Int,Int,PathEdge)]) -> IM.IntMap (IM.IntMap [(Int,Int,PathEdge)])
zonePe pe im = foldl' f im (zoneOfPe pe)
where
f im' i2 = zoneMonoid i2 [pe] im'