tracer/src/Interior.hs

143 lines
4.3 KiB
Haskell
Raw Normal View History

2018-03-03 16:03:17 +00:00
module Interior where
import Affection as A
import Data.Matrix as M
import qualified Data.Map.Strict as Map
import Data.List as L
import Data.Maybe (isNothing, isJust)
import Linear.V2
2018-03-03 16:03:17 +00:00
import System.Random
-- internal imports
import Navigation
2018-04-14 11:34:28 +00:00
import Util
2018-03-03 16:03:17 +00:00
import Types.Interior
import Types.Map
import Types.UserData
2018-03-03 16:42:24 +00:00
placeInteriorIO
:: Matrix TileState
-> Matrix (Maybe ImgId)
-> [ReachPoint]
2018-03-03 16:42:24 +00:00
-> [Graph]
-> IO (Matrix (Maybe ImgId), [ReachPoint])
placeInteriorIO imat imgmat irp graph =
2018-03-03 16:03:17 +00:00
do
rnd <- newStdGen
let (_, matps) =
2018-03-03 16:03:17 +00:00
foldl
traverseGraph
(rnd, (imgmat, irp))
2018-03-03 16:03:17 +00:00
graph
return matps
2018-03-03 16:03:17 +00:00
where
traverseGraph
:: (StdGen, (Matrix (Maybe ImgId), [ReachPoint]))
2018-03-03 16:03:17 +00:00
-> Graph
-> (StdGen, (Matrix (Maybe ImgId), [ReachPoint]))
2018-03-03 16:03:17 +00:00
traverseGraph acc (GHall sub) =
foldl traverseGraph acc sub
traverseGraph put@(rng, (mat, rp)) (GRoom _ bnds) =
2018-03-03 16:03:17 +00:00
let applicable =
reverse (L.sortBy (\a b -> size a `compare` size b) (
L.filter
2018-03-05 20:11:38 +00:00
(\a -> clusterRoom a == roomType && size a <= size bnds)
2018-03-03 16:03:17 +00:00
[minBound .. maxBound] :: [Cluster])
)
roomType = fst (head $ reverse $ L.sortBy
(\a b -> snd a `compare` snd b) $ Map.toList $ foldl
(\acc a -> if a `Map.member` acc
then Map.insert a (acc Map.! a + 1) acc
else Map.insert a 1 acc
)
Map.empty
(M.submatrix
(fst $ matmin bnds) (fst $ matmax bnds)
(snd $ matmin bnds) (snd $ matmax bnds)
imat
)
)
in
foldl
(\(orng, (omat, orp)) -> placeCluster orng bnds 1 omat orp)
put
applicable
placeCluster
:: StdGen
-> Boundaries Int
-> Int
-> Matrix (Maybe ImgId)
-> [ReachPoint]
-> Cluster
-> (StdGen, (Matrix (Maybe ImgId), [ReachPoint]))
placeCluster rng bnds try mat rp appl =
2018-03-03 16:42:24 +00:00
let (pr, g1) = randomR (fst (matmin bnds) - 1, fst (matmax bnds) - 1) rng
(pc, g2) = randomR (snd (matmin bnds) - 1, snd (matmax bnds) - 1) g1
2018-03-03 16:03:17 +00:00
freeRoom = foldl
(\acc a -> if a == Nothing then acc + 1 else acc)
0
(M.toList $ M.submatrix
(fst $ matmin bnds) (fst $ matmax bnds)
(snd $ matmin bnds) (snd $ matmax bnds)
mat
)
cmat = clusterMat appl
newmat = insertMat cmat mat (pr, pc)
exits = filter
(\p -> pointType p == RoomExit && inBounds (pointCoord p) bnds)
rp
reaches = map (+ V2 (pr - 1) (pc - 1))
(map pointCoord (clusterPoints appl))
oldreaches = foldl (\acc p ->
if pointType p /= RoomExit && inBounds (pointCoord p) bnds
then pointCoord p : acc
else acc
)
[]
rp
newrp = rp ++ map (ReachPoint Table) reaches
2018-03-03 16:03:17 +00:00
in
2018-03-05 20:11:38 +00:00
if try > 10 || fromIntegral freeRoom < size appl
then (g2, (mat, rp))
2018-03-03 16:03:17 +00:00
else
if (pr + nrows cmat - 1 > fst (matmax bnds) ||
2018-03-03 16:42:24 +00:00
pc + ncols cmat - 1 > snd (matmax bnds) ||
2018-03-03 16:03:17 +00:00
any (/= clusterRoom appl)
(M.toList (M.submatrix
2018-03-03 16:42:24 +00:00
pr (pr + nrows cmat - 1)
pc (pc + ncols cmat - 1)
2018-03-03 16:03:17 +00:00
imat
)) ||
any isJust
(M.toList (M.submatrix
2018-03-03 16:42:24 +00:00
pr (pr + nrows cmat - 1)
pc (pc + ncols cmat - 1)
2018-03-03 16:03:17 +00:00
mat
))) ||
not (isReachable newmat (oldreaches ++ reaches) exits)
then placeCluster g2 bnds (try + 1) mat rp appl
2018-03-03 16:42:24 +00:00
else placeCluster
g2 bnds (try + 1) newmat newrp appl
2018-03-03 16:03:17 +00:00
insertMat
:: Matrix (Maybe a)
-> Matrix (Maybe a)
-> (Int, Int)
-> Matrix (Maybe a)
insertMat i into (roffs, coffs) =
if roffs + nrows i > nrows into || coffs + ncols i > ncols into
then error "insertMat: matrix to be inserted does not fit!"
else foldl
(\mat (ir, ic) ->
2018-03-03 16:42:24 +00:00
if isNothing (mat M.! (ir + roffs - 1, ic + coffs - 1))
then M.setElem (i M.! (ir, ic)) (ir + roffs - 1, ic + coffs - 1) mat
2018-03-03 16:03:17 +00:00
else mat
)
into
((,) <$> [1 .. nrows i] <*> [1 .. ncols i])