module Server.Util where import Control.Exception import qualified Data.Matrix as M import Linear import System.Directory import System.IO.Error import System.Random import Library.Types -- | Remove a file if it exists removeIfExists :: FilePath -- File to remove -> IO () removeIfExists fileName = removeFile fileName `catch` handleExists where handleExists e | isDoesNotExistError e = return () | otherwise = throwIO e newWizard :: Position -> Wizard newWizard pos = Wizard { wizardWands = [] , wizardRot = 3 , wizardPos = pos , wizardMana = 100 , wizardHealth = 100 , wizardEffects = [] } rollInitPosition :: ServerMap -> IO Position rollInitPosition arena = do r <- randomRIO (1, M.nrows arena) c <- randomRIO (1, M.ncols arena) if arena M.! (r, c) == Air then pure $ (fromIntegral <$> V2 r c) + V2 0.5 0.5 else rollInitPosition arena