2023-12-09 12:58:59 +00:00
|
|
|
module Server.Util where
|
|
|
|
|
|
|
|
import Control.Exception
|
|
|
|
|
2023-12-13 10:06:45 +00:00
|
|
|
import qualified Data.Matrix as M
|
|
|
|
|
|
|
|
import Linear
|
|
|
|
|
2023-12-09 12:58:59 +00:00
|
|
|
import System.Directory
|
|
|
|
|
|
|
|
import System.IO.Error
|
|
|
|
|
2023-12-13 10:06:45 +00:00
|
|
|
import System.Random
|
|
|
|
|
|
|
|
import Library.Types
|
|
|
|
|
2023-12-09 13:18:10 +00:00
|
|
|
-- | Remove a file if it exists
|
|
|
|
removeIfExists
|
|
|
|
:: FilePath -- File to remove
|
|
|
|
-> IO ()
|
2023-12-09 12:58:59 +00:00
|
|
|
removeIfExists fileName = removeFile fileName `catch` handleExists
|
|
|
|
where handleExists e
|
|
|
|
| isDoesNotExistError e = return ()
|
|
|
|
| otherwise = throwIO e
|
2023-12-13 10:06:45 +00:00
|
|
|
|
|
|
|
newWizard
|
|
|
|
:: Position
|
2024-11-25 02:58:31 +00:00
|
|
|
-> Float
|
2023-12-13 10:06:45 +00:00
|
|
|
-> Wizard
|
2024-11-25 02:58:31 +00:00
|
|
|
newWizard pos rot =
|
2023-12-13 10:06:45 +00:00
|
|
|
Wizard
|
|
|
|
{ wizardWands = []
|
2024-11-25 02:58:31 +00:00
|
|
|
, wizardRot = rot
|
2023-12-13 10:06:45 +00:00
|
|
|
, 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
|