From b8028ecd54f248aa93b29f5ad0a96b00bcb56663 Mon Sep 17 00:00:00 2001 From: nek0 Date: Thu, 15 Feb 2018 03:50:51 +0100 Subject: [PATCH] now building facilities --- src/Floorplan.hs | 53 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/src/Floorplan.hs b/src/Floorplan.hs index 9d1110e..5e2f39f 100644 --- a/src/Floorplan.hs +++ b/src/Floorplan.hs @@ -50,12 +50,13 @@ buildHallFloorIO fc = do buildHallFloor :: FloorConfig -> StdGen -> Matrix TileState buildHallFloor fc gen = - let empty = emptyFloor fc - (g1, withElv) = buildElevator fc (placeHalls gen fc empty) - (g2, withIW) = buildInnerWalls g1 withElv - withOW = buildOuterWalls withIW - closed = closeOffices withOW - in closed + let empty = emptyFloor fc + (g1, withElv) = buildElevator fc (placeHalls gen fc empty) + (g2, withIW) = buildInnerWalls g1 withElv + withOW = buildOuterWalls withIW + closed = closeOffices withOW + (g3, facilities) = buildFacilities g2 fc closed + in facilities emptyFloor :: FloorConfig -> Matrix TileState emptyFloor fc = @@ -270,3 +271,43 @@ buildOuterWalls input = , ncols horz ] in vert + +buildFacilities + :: StdGen + -> FloorConfig + -> Matrix TileState + -> (StdGen, Matrix TileState) +buildFacilities gen fc input = + let flood ts mat coords@(cr, cc) = + let cur = mat M.! coords + altered = M.setElem ts coords mat + in + if cur == ts || cur /= Offi + then mat + else foldl (\acc cs -> flood ts acc cs) + altered + [ (cr + 1, cc) + , (cr - 1, cc) + , (cr, cc + 1) + , (cr, cc - 1) + ] + nearests = map (findNearestOffice input) (facilities fc) + in (foldl (\(agen, acc) x -> + let (numfac, ngen) = randomR (0 :: Int, 1 :: Int) agen + facil = if numfac == 1 then Kitc else Toil + in (ngen, flood facil acc x) + ) (gen, input) nearests) + +findNearestOffice + :: Matrix TileState + -> (Int, Int) + -> (Int, Int) +findNearestOffice mat (rrr, ccc) = + let matcoord = (,) <$> [1 .. nrows mat] <*> [1 .. ncols mat] + distance (ar, ac) = (ar - rrr) ^ 2 + (ac - ccc) ^ 2 + inlist = zip matcoord (M.toList mat) + in foldl (\acc@(arow, acol) x@(xc@(crow, ccol), ts) -> + if ts == Offi && distance acc > distance xc + then xc + else acc + ) (fst $ head inlist) inlist