module Dodge.Room.RunPast 
    ( lockedStart
    ) where
import Dodge.Cleat
import Geometry.Polygon
import Dodge.LevelGen.Data
import Dodge.LightSource
import Dodge.Data
import RandomHelp
import Dodge.Room.Door
import Dodge.Room.Link
import Dodge.Room.Procedural
import Dodge.Room.Corridor
import Dodge.PlacementSpot
import Dodge.Placement.Instance
import Dodge.Tree
import Dodge.Item.Random
import Geometry.Data
import Color
import LensHelp

import qualified Data.Set as S

roomCritLS :: RandomGen g => State g LightSource
roomCritLS = do
    col <- takeOne [0.5, 0.6]
    h   <- takeOne [30,35]
    rad <- takeOne [300]
    return $ lsPosColRad (V3 0 0 h) col rad

smallRoom :: Room
smallRoom = corridorWallN & rmRandPSs .~ [psRandRanges (15,25) (30,45) (pi,2*pi)]

lockedStart :: RandomGen g => Int -> State g (Tree Room)
lockedStart i = do
    h           <- state $ randomR (200,400::Float)
    thels       <- roomCritLS
    theweapon   <- randBlockBreakWeapon
    cenroom     <- shuffleLinks $ roomRectAutoLinks 40 h & rmPmnts .~ []
    theedge     <- takeOne $ map OnEdge [West,East]
    let linkcor = smallRoom & rmPmnts .~ [ spanLS thels (V2 0 65) (V2 40 65) ]
        n = length $ filter (elem theedge . _rlType) (_rmLinks cenroom)
        doorrooms = map (treePost . (triggerDoorRoom i:))
            $ [linkcor & rmPmnts .:~ plRRpt 0 randC1]
            : [linkcor,corridor,corridor,cleatOnward door]
            : replicate (n-2) [linkcor]
    return $ Node cenroom $
        map (over root $ rmConnectsTo .~ S.member theedge) doorrooms
        ++ [return smallRoom
                {_rmOutPmnt = [OutPlacement (putLitButOnPosExtTrig red useUnusedLnk) i]
                ,_rmPmnts = [plRRpt 0 (PutFlIt theweapon)]
                ,_rmBound = [rectNSWE 70 30 0 40]
                }
            ]
