Fix backtracking bug

This commit is contained in:
2021-11-23 19:09:39 +00:00
parent 6e4b36fd82
commit a66ea1d922
6 changed files with 49 additions and 23 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ initialAnoTree :: RandomGen g => Tree [Annotation g]
initialAnoTree = padSucWithCorridors $ treeFromTrunk initialAnoTree = padSucWithCorridors $ treeFromTrunk
[[AnoApplyInt 0 startRoom] [[AnoApplyInt 0 startRoom]
, [PassthroughLockKeyLists 2 lockRoomKeyItems itemRooms] , [PassthroughLockKeyLists 2 lockRoomKeyItems itemRooms]
-- , [AnoApplyInt 1 lasSensorTurretTest] , [AnoApplyInt 1 lasSensorTurretTest]
-- ,[ChainAnos -- ,[ChainAnos
-- [[SetLabel 0 $ return $ roomRectAutoLinks 200 200 -- [[SetLabel 0 $ return $ roomRectAutoLinks 200 200
-- & rmExtPmnt ?~ triggerSwitchSPicLight (drawSwitchWire red red) -- & rmExtPmnt ?~ triggerSwitchSPicLight (drawSwitchWire red red)
+3 -3
View File
@@ -169,13 +169,13 @@ gameRoomFromRoom rm = GameRoom
,p -.- 10 *.* unitVectorAtAngle a ,p -.- 10 *.* unitVectorAtAngle a
] ]
unpos (UsedOutLink _ p a) = doubleShift p a unpos (UsedOutLink _ p a) = doubleShift p a
unpos (UsedInLink p a) = doubleShift p a unpos (UsedInLink _ p a) = doubleShift p a
unpos _ = [] unpos _ = []
undir (UsedOutLink _ _ a) = Just $ 0.5*pi + a + snd (_rmShift rm) undir (UsedOutLink _ _ a) = Just $ 0.5*pi + a + snd (_rmShift rm)
undir (UsedInLink _ a) = Just $ 0.5*pi + a + snd (_rmShift rm) undir (UsedInLink _ _ a) = Just $ 0.5*pi + a + snd (_rmShift rm)
undir _ = Nothing undir _ = Nothing
closePoints x y = roundPoint2 x == roundPoint2 y closePoints x y = roundPoint2 x == roundPoint2 y
getDir (UsedInLink _ a:_) = a + snd (_rmShift rm) getDir (UsedInLink _ _ a:_) = a + snd (_rmShift rm)
getDir (_:xs) = getDir xs getDir (_:xs) = getDir xs
getDir _ = 0 -- fallback getDir _ = 0 -- fallback
+1 -1
View File
@@ -99,7 +99,7 @@ data RoomWire
WallWire Point2 Float Float WallWire Point2 Float Float
data RoomPos data RoomPos
= UsedOutLink Int Point2 Float = UsedOutLink Int Point2 Float
| UsedInLink Point2 Float | UsedInLink Int Point2 Float
| UnusedLink Point2 Float | UnusedLink Point2 Float
| PosPl Point2 Float | PosPl Point2 Float
| LabPos Int RoomPos | LabPos Int RoomPos
+9 -9
View File
@@ -118,14 +118,14 @@ shiftRoomBy shift r = r
. (tileX %~ shiftPointBy shift ) . (tileX %~ shiftPointBy shift )
) )
& rmViewpoints %~ map (shiftPointBy shift) & rmViewpoints %~ map (shiftPointBy shift)
shiftRoomShiftToLink :: (Point2,Float) -> Room -> Room --shiftRoomShiftToLink :: (Point2,Float) -> Room -> Room
shiftRoomShiftToLink l r --shiftRoomShiftToLink l r
= shiftRoomShiftBy l -- = shiftRoomShiftBy l
. shiftRoomShiftBy (V2 0 0 , pi-a) -- . shiftRoomShiftBy (V2 0 0 , pi-a)
$ shiftRoomShiftBy (V2 0 0 -.- p , 0) -- $ shiftRoomShiftBy (V2 0 0 -.- p , 0)
r -- r
where -- where
(p,a) = head $ _rmInLinks r -- (p,a) = head $ _rmInLinks r
shiftRoomShiftToLink' :: (Point2,Float) -> (Point2,Float) -> Room -> Room shiftRoomShiftToLink' :: (Point2,Float) -> (Point2,Float) -> Room -> Room
shiftRoomShiftToLink' l inlink r shiftRoomShiftToLink' l inlink r
@@ -156,7 +156,7 @@ finalLinksUpdate :: Room -> Room
finalLinksUpdate rm = case _rmInLinks rm of finalLinksUpdate rm = case _rmInLinks rm of
(_:_) -> rm (_:_) -> rm
& rmInLinks %~ tail & rmInLinks %~ tail
& rmPos %~ ( (uncurry UsedInLink (head inlnks) :) & rmPos %~ ( (uncurry (UsedInLink 0) (head inlnks) :)
. (map (uncurry UnusedLink) (outlnks ++ tail inlnks) ++) ) . (map (uncurry UnusedLink) (outlnks ++ tail inlnks) ++) )
_ -> rm _ -> rm
where where
+1 -1
View File
@@ -5,7 +5,7 @@ import Geometry
extractRoomPos :: RoomPos -> (Point2,Float) extractRoomPos :: RoomPos -> (Point2,Float)
extractRoomPos rp = case rp of extractRoomPos rp = case rp of
UsedOutLink _ p a -> (p,a) UsedOutLink _ p a -> (p,a)
UsedInLink p a -> (p,a) UsedInLink _ p a -> (p,a)
UnusedLink p a -> (p,a) UnusedLink p a -> (p,a)
PosPl p a -> (p,a) PosPl p a -> (p,a)
LabPos _ rp' -> extractRoomPos rp' LabPos _ rp' -> extractRoomPos rp'
+34 -8
View File
@@ -20,7 +20,7 @@ import Control.Lens hiding (Empty, (<|) , (|>))
type RoomInt = (Room,Int) type RoomInt = (Room,Int)
positionRoomsFromTree :: Tree RoomInt -> IO (Maybe [Room]) positionRoomsFromTree :: Tree RoomInt -> IO (Maybe [Room])
positionRoomsFromTree (Node (r,i) ts) = posRms (map pointsToPoly $ _rmBound r) (r,i) 0 ts Empty positionRoomsFromTree (Node (r,i) ts) = printHeader >> posRms (map pointsToPoly $ _rmBound r) (r,i) 0 ts Empty
posRms :: [ConvexPoly] posRms :: [ConvexPoly]
-> RoomInt -> RoomInt
@@ -31,12 +31,12 @@ posRms :: [ConvexPoly]
posRms bounds (parent,_) _ [] st = case st of posRms bounds (parent,_) _ [] st = case st of
Empty -> return $ Just [parent] Empty -> return $ Just [parent]
Node childi ts :<| tseq Node childi ts :<| tseq
-> fmap (finalLinksUpdate parent:) <$> posRms bounds childi 0 ts tseq -> fmap (parent:) <$> posRms bounds childi 0 ts tseq
posRms bounds parenti@(parent,_) numChild (t@(Node childi _):ts) tseq = do posRms bounds parenti@(parent,_) numChild (t@(Node childi _):ts) tseq = do
printInfo parenti numChild childi printInfoCheckNum parenti numChild childi
tryParentLinks (0::Int) (_rmOutLinks parent) tryParentLinks (0::Int) (_rmOutLinks parent)
where where
tryParentLinks _ [] = putStrLn "all link pairs tried" >> return Nothing tryParentLinks _ [] = putStrLn "no viable link pairs, backtrack" >> return Nothing
tryParentLinks j (l:ls) = do tryParentLinks j (l:ls) = do
tryChildLinks (0::Int) (_rmInLinks $ fst childi) tryChildLinks (0::Int) (_rmInLinks $ fst childi)
where where
@@ -46,15 +46,33 @@ posRms bounds parenti@(parent,_) numChild (t@(Node childi _):ts) tseq = do
| otherwise = do | otherwise = do
putStrLn $ show j ++ "-"++show numinlink putStrLn $ show j ++ "-"++show numinlink
mayrs <- posRms (convexBounds ++ bounds) mayrs <- posRms (convexBounds ++ bounds)
(first upr parenti) (numChild + 1) ts (tseq |> shiftedt) (first updateparent parenti) (numChild + 1) ts (tseq |> shiftedt)
return mayrs case mayrs of
Just rms -> return $ Just rms
Nothing -> printInfo parenti numChild childi
>> tryChildLinks (numinlink+1) ils
where where
convexBounds = map pointsToPoly $ _rmBound r' convexBounds = map pointsToPoly $ _rmBound r'
clipping = or (convexPolysOverlap <$> convexBounds <*> bounds) clipping = or (convexPolysOverlap <$> convexBounds <*> bounds)
upr rm = doLnkEff l $ rm & rmOutLinks %~ delete l & rmPos %~ (uncurry (UsedOutLink numChild) l :) updateparent rm = doLnkEff l $ rm & rmOutLinks %~ delete l & rmPos %~ (uncurry (UsedOutLink numChild) l :)
l' = shiftLinkBy (_rmShift parent) l l' = shiftLinkBy (_rmShift parent) l
r' = doRoomShift . fst $ rootLabel shiftedt r' = doRoomShift . fst $ rootLabel shiftedt
shiftedt = applyToRoot (first $ shiftRoomShiftToLink' l' il) t updatechild rm = rm & rmInLinks %~ delete il
& rmPos %~ ( (uncurry (UsedInLink 0) il:)
. (map (uncurry UnusedLink) (_rmOutLinks rm ++ newInLinks) ++) )
where
newInLinks = delete il $ _rmInLinks rm
shiftedt = applyToRoot (first $ updatechild . shiftRoomShiftToLink' l' il) t
printColumns :: [Int] -> [String] -> IO ()
printColumns (i:is) (s:strs) = putStr (rightPad i ' ' s) >> printColumns is strs
printColumns _ strs = putStrLn $ concat strs
theColumns :: [String] -> IO ()
theColumns = printColumns [20,5,20,20]
printHeader :: IO ()
printHeader = theColumns ["Parent","Cnum","Child","Link pair"]
printInfo :: RoomInt -> Int -> RoomInt -> IO () printInfo :: RoomInt -> Int -> RoomInt -> IO ()
printInfo (parentrm,parenti) childn (childrm,childi) = do printInfo (parentrm,parenti) childn (childrm,childi) = do
@@ -63,6 +81,14 @@ printInfo (parentrm,parenti) childn (childrm,childi) = do
++ rpns 20 (_rmName childrm ++ "-" ++ show childi ) ++ rpns 20 (_rmName childrm ++ "-" ++ show childi )
where where
rpns x s = rightPadNoSquash x ' ' s rpns x s = rightPadNoSquash x ' ' s
printInfoCheckNum :: RoomInt -> Int -> RoomInt -> IO ()
printInfoCheckNum p 0 c = printInfo p 0 c
printInfoCheckNum _ childn (childrm,childi) = do
putStr $ rpns 20 ("")
++ rpns 5 (show childn ++ " ")
++ rpns 20 (_rmName childrm ++ "-" ++ show childi )
where
rpns x s = rightPadNoSquash x ' ' s
doLnkEff :: (Point2,Float) -> Room -> Room doLnkEff :: (Point2,Float) -> Room -> Room
doLnkEff x rm = case _rmLinkEff rm of doLnkEff x rm = case _rmLinkEff rm of