--{-# LANGUAGE TupleSections #-}
module Dodge.Room.Containing where
import Dodge.Data
import Dodge.Item.Display
import Dodge.Tree
import Dodge.LevelGen.Data
import Dodge.PlacementSpot
import RandomHelp
import Dodge.Room.Procedural
import Dodge.Room.Pillar
import Dodge.Room.Tanks
import Dodge.Room.Link
import Dodge.Room.Ngon
import Dodge.Cleat
import LensHelp
import Geometry
--import Dodge.Item.Equipment

roomsContaining :: RandomGen g => [Creature] -> [Item] -> State g (MetaTree Room String)
roomsContaining crs its = tToBTree str <$> roomsContaining' crs its
  where
    str = "roomsContaining "++ concatMap _crName crs ++ concatMap (head . itemDisplay) its

roomsContaining' :: RandomGen g => [Creature] -> [Item] -> State g (Tree Room)
roomsContaining' crs its = do
    endroom <- join $ takeOne
        [ roomPillarsSquare <&> rmPmnts ++.~ crsItmsUnused crs its
        , randomFourCornerRoomCrsIts crs its 
        , tanksRoom crs its
        , tanksPipesRoom <&> rmPmnts ++.~ crsItmsUnused crs its
        , roomPillarsContaining crs its
        , roomPillarsPassage <&> rmPmnts ++.~ crsItmsUnused crs its
        ]
    return (pure $ cleatOnward endroom)

roomPillarsContaining :: RandomGen g => [Creature] -> [Item] -> State g Room
roomPillarsContaining crs itms = do
    (w,wn) <- takeOne [(240,2),(340,3)]
    (h,hn) <- takeOne [(240,2),(340,3)]
    roomPillars 30 w h wn hn <&> rmPmnts .++~ crsItmsUnused crs itms

crsItmsUnused :: [Creature] -> [Item] -> [Placement]
crsItmsUnused crs itms = map (\it -> sps0 (PutFlIt it) & plSpot .~ anyUnusedSpot) itms
    ++ map (\cr -> sps0 (PutCrit cr) & plSpot .~ unusedSpotAwayFromLink 50) crs


pedestalRoom :: RandomGen g => Item -> State g Room
pedestalRoom it = do
    let flit = PutFlIt it
    x <- state $ randomR (150,250)
    ngon <- state $ randomR (5,9)
    r <- takeOne 
        [ shuffleLinks =<< (centerVaultRoom x x 50 <&> rmPmnts .:~ sps0 flit)
        , shuffleLinks $ roomRectAutoLinks (2*x) (2*x) & rmPmnts .:~ sPS (V2 x x) 0 flit
        , shuffleLinks $ roomNgon ngon x & rmPmnts .:~ sps0 flit
        ]
    r <&> rmName .++~ "ped-"
