p7/old/Format.hs

51 lines
1.2 KiB
Haskell
Raw Normal View History

2018-06-24 17:26:43 -04:00
module Format where
2018-06-24 19:21:29 -04:00
import System.Random
import State
import Timestamp
import Config
2018-07-01 16:47:13 -04:00
import Utils
2018-06-24 19:21:29 -04:00
currentFormat :: Timestamp -> State
currentFormat t = strictApplyN n nextFormat initialRotation
where
n = t `monthsSince` genesis
2018-06-24 17:26:43 -04:00
nextFormat :: State -> State
nextFormat (p, b, r) = (np, nb, nr)
where
ip = rotateOld p
2018-06-24 19:21:29 -04:00
(np, nr) = (addNewPack . addNewPack) (ip, r)
2018-06-24 17:26:43 -04:00
nb = rotateBox b
legalOutRot :: [OutRot] -> [OutRot]
legalOutRot x = filter (\(Or _ n) -> n == 0) x
updatePackAge :: [OutRot] -> [OutRot]
2018-06-24 19:21:29 -04:00
updatePackAge p = map (\(Or s n) -> (Or s $ max 0 (n-1))) p
2018-06-24 17:26:43 -04:00
setIllegal :: InRot -> OutRot
setIllegal (Ir n) = Or n 3
setLegal :: OutRot -> InRot
setLegal (Or n _) = Ir n
rotateOld :: Pool -> Pool
rotateOld (i, o) = (ni, no)
where
ni = drop 2 i
2018-06-24 19:21:29 -04:00
no = (updatePackAge o) ++ dropped
2018-06-24 17:26:43 -04:00
dropped = map setIllegal (take 2 i)
addNewPack :: (Pool, StdGen) -> (Pool, StdGen)
addNewPack ((i, o), r) = ((ni, no), nr)
where
lp = length $ legalOutRot o
(ip, nr) = randomR (0, lp-1) r
np = (legalOutRot o) !! ip
2018-06-24 19:21:29 -04:00
ni = i ++ [(setLegal np)]
2018-06-24 17:26:43 -04:00
no = filter (\x -> x /= np) o
rotateBox :: BoxQueue -> BoxQueue
rotateBox (Bq x) = Bq $ rotate 1 x