module SoccerFun.Referee where
import SoccerFun.Team
import SoccerFun.Types
import SoccerFun.Ball
import SoccerFun.Field
import SoccerFun.Geometry
import System.Random
import Control.Monad.State
import Control.Monad.Identity
data Referee = ∀memory. Referee { rname ∷ String
, rbrain ∷ Brain (RefereeAI (memory,StdGen)) memory
}
instance Show Referee where
show r = rname r
cloneReferee ∷ Brain (RefereeAI (m,StdGen)) m → Referee → Referee
cloneReferee brain (Referee rname rbrain )
= Referee rname brain
type RefereeBrain memory = (memory,RefereeAI (memory,StdGen))
type RefereeAI state = PlayingTime → TimeUnit
→ BallState
→ Half
→ Team
→ Team
→ state
→ ([RefereeAction],state)
defaultReferee ∷ Referee
defaultReferee = Referee { rname = "Default"
, rbrain = Brain {m = undefined, ai = \_ _ _ _ _ _ st → ([ContinueGame],st)}
}
randomlessRefereeAI ∷ (RefereeAI memory) → RefereeAI (memory,StdGen)
randomlessRefereeAI brainf = randomless brainf where
randomless brainf playingtime unit maybeBall half team1 team2 (memory,seed) = runIdentity $ do
(decisions,memory) ← return $ brainf playingtime unit maybeBall half team1 team2 memory
return $ (decisions,(memory,seed))
amnesiaRefereeAI ∷ (RefereeAI StdGen) → RefereeAI (memory,StdGen)
amnesiaRefereeAI brainf = amnesia brainf where
amnesia brainf playingtime unit maybeBall half team1 team2 (memory,seed) = runIdentity $ do
(decisions,seed) ← return $ brainf playingtime unit maybeBall half team1 team2 seed
return (decisions,(memory,seed))
witlessRefereeAI ∷ (RefereeAI a) → RefereeAI (memory,StdGen)
witlessRefereeAI brainf = witless brainf where
witless brainf playingtime unit maybeBall half team1 team2 state =
(fst (brainf playingtime unit maybeBall half team1 team2 undefined),state)
instance NameOf Referee where nameOf r = rname r
isReprimandPlayer ∷ RefereeAction → Bool
isReprimandPlayer (ReprimandPlayer _ _) = True
isReprimandPlayer _ = False
isHands ∷ RefereeAction → Bool
isHands (Hands _) = True
isHands _ = False
isTackleDetected ∷ RefereeAction → Bool
isTackleDetected (TackleDetected _) = True
isTackleDetected _ = False
isSchwalbeDetected ∷ RefereeAction → Bool
isSchwalbeDetected (SchwalbeDetected _) = True
isSchwalbeDetected _ = False
isTheaterDetected ∷ RefereeAction → Bool
isTheaterDetected (TheaterDetected _) = True
isTheaterDetected _ = False
isDangerousPlay ∷ RefereeAction → Bool
isDangerousPlay (DangerousPlay _) = True
isDangerousPlay _ = False
isGameOver ∷ RefereeAction → Bool
isGameOver GameOver = True
isGameOver _ = False
isPauseGame ∷ RefereeAction → Bool
isPauseGame PauseGame = True
isPauseGame _ = False
isAddTime ∷ RefereeAction → Bool
isAddTime (AddTime _) = True
isAddTime _ = False
isEndHalf ∷ RefereeAction → Bool
isEndHalf EndHalf = True
isEndHalf _ = False
isGoal ∷ RefereeAction → Bool
isGoal (Goal _) = True
isGoal _ = False
isOffside ∷ RefereeAction → Bool
isOffside (Offside _) = True
isOffside _ = False
isDirectFreeKick ∷ RefereeAction → Bool
isDirectFreeKick (DirectFreeKick _ _ ) = True
isDirectFreeKick _ = False
isGoalKick ∷ RefereeAction → Bool
isGoalKick (GoalKick _) = True
isGoalKick _ = False
isCorner ∷ RefereeAction → Bool
isCorner (Corner _ _) = True
isCorner _ = False
isThrowIn ∷ RefereeAction → Bool
isThrowIn (ThrowIn _ _) = True
isThrowIn _ = False
isPenalty ∷ RefereeAction → Bool
isPenalty (Penalty _) = True
isPenalty _ = False
isCenterKick ∷ RefereeAction → Bool
isCenterKick (CenterKick _) = True
isCenterKick _ = False
isAdvantage ∷ RefereeAction → Bool
isAdvantage (Advantage _) = True
isAdvantage _ = False
isOwnBallIllegally ∷ RefereeAction → Bool
isOwnBallIllegally (OwnBallIllegally _) = True
isOwnBallIllegally _ = False
isDisplacePlayers ∷ RefereeAction → Bool
isDisplacePlayers (DisplacePlayers _) = True
isDisplacePlayers _ = False
isContinueGame ∷ RefereeAction → Bool
isContinueGame ContinueGame = True
isContinueGame _ = False
isTellMessage ∷ RefereeAction → Bool
isTellMessage (TellMessage _) = True
isTellMessage _ = False
isGoal4ATeam ∷ ATeam → RefereeAction → Bool
isGoal4ATeam t (Goal t') = t == t'
isGoal4ATeam _ _ = False
getKickPos ∷ Field → Half → RefereeAction → Maybe Position
getKickPos field half (GoalKick team) = Just $ Position { py = (fwidth field)/2.0
, px = if (team == Team1 && half == FirstHalf || team == Team2 && half == SecondHalf)
then penaltyAreaDepth
else (flength field) penaltyAreaDepth }
getKickPos field half (Corner team edge) = Just $ Position { px = if (team == Team1 && half == SecondHalf || team == Team2 && half == FirstHalf)
then halfRadiusCornerKickArea
else ((flength field) halfRadiusCornerKickArea)
, py = if (edge == North)
then halfRadiusCornerKickArea
else ((fwidth field) halfRadiusCornerKickArea)
}
where
halfRadiusCornerKickArea = radiusCornerKickArea / 2.0
getKickPos field half (Penalty team) = Just $ Position { py = (fwidth field)/2.0
, px = if (team == Team1 && half == SecondHalf || team == Team2 && half == FirstHalf)
then penaltySpotDepth
else ((flength field) penaltySpotDepth)
}
getKickPos field _ (CenterKick _) = Just $ Position { px = (flength field)/2.0
, py = (fwidth field) /2.0
}
getKickPos _ _ (DirectFreeKick _ pos) = Just pos
getKickPos _ _ (ThrowIn _ pos) = Just pos
getKickPos _ _ _ = Nothing