initial framework added
[fp1415-soccerfun.git] / src / StdReferee / RefereeCoach_DeepPass_Assignment.icl
1 implementation module RefereeCoach_DeepPass_Assignment
2
3 import StdEnvExt
4 import Referee
5 import Team_Opponent_DeepPass_Assignment
6
7 RefereeCoach_DeepPass :: !FootballField -> Referee
8 RefereeCoach_DeepPass field = { name = "RefereeCoach DeepPass"
9 , brain = { memory = initMem
10 , ai = randomlessRefereeAI (brain field)
11 }
12 , refActionPics = []
13 }
14
15 :: Memory = { first :: !Bool
16 , practiceFailed :: !Bool
17 , gameOver :: !Bool
18 , ballKicked :: !Bool
19 , ballGained :: !Bool
20 , positions :: !(!Position,!Position)
21 }
22
23 initMem :: Memory
24 initMem = { first = True
25 , practiceFailed = False
26 , gameOver = False
27 , ballKicked = False
28 , ballGained = False
29 , positions = (zero,zero)
30 }
31
32 brain :: !FootballField !(!RefereeInput,!Memory) -> (!RefereeOutput,!Memory)
33 brain field ({RefereeInput | theBall=ballState,team1,team2},memory)
34 | memory.gameOver // game over because of succes or failure
35 = ([GameOver],memory)
36 | memory.practiceFailed // assignment has failed
37 = ([TellMessage "Assignment failed, correct your team and try again."],stop memory)
38 | length (filter isFielder studentTeam) <> 2 // check if there are indeed two fielders in the students team
39 = ([TellMessage "There should be two fielders in student team."],stop memory)
40 # (refActions,memory) = if memory.first // when first, alter ball pos and remember all positions
41 ([DirectFreeKick studentHome ballKickoff],{updateAllPos memory & first = False})
42 ([],memory)
43 # playersMovedToFar = getPlayersThatMovedTooFar memory
44 | not (isEmpty playersMovedToFar) // check if players did not move more than 10 meters from their starting positions
45 = (refActions ++ getMessagesForTooFarPlayers playersMovedToFar,fail memory)
46 | ballGainedByComputer
47 = (refActions ++ [TellMessage ("Computer gained the ball")],fail memory)
48 | not memory.ballKicked
49 | perhaps isKickedBall kickerAction = (refActions,kick memory)
50 | perhaps isTackled kickerAction = (refActions ++ [TackleDetected kickerID, ReprimandPlayer kickerID RedCard],fail memory)
51 | perhaps isTackled gainerAction = (refActions ++ [TackleDetected gainerID, ReprimandPlayer gainerID RedCard],fail memory)
52 | otherwise = (refActions,memory)
53 | not memory.ballGained
54 | perhaps isTackled kickerAction = (refActions ++ [TackleDetected kickerID, ReprimandPlayer kickerID RedCard],fail memory)
55 | perhaps isTackled gainerAction = (refActions ++ [TackleDetected gainerID, ReprimandPlayer gainerID RedCard],fail memory)
56 | perhaps isGainedBall gainerAction = (refActions ++ [TellMessage "Well Done! Move on to the next assignment!"], stop memory)
57 | otherwise = (refActions,memory)
58 | otherwise
59 = (refActions,memory)
60 where
61 (compTeam,studentTeam,studentHome) = if (stringStarts (nameOf team1) base_TeamName_Opponent_DeepPass) (team1,team2,East) (team2,team1,West)
62 (kicker, gainer) = let fielders = filter isFielder studentTeam in (fielders!!0,fielders!!1)
63 (kickerID,gainerID) = (kicker.playerID,gainer.playerID)
64 kickerAction = getAction kickerID
65 gainerAction = getAction gainerID
66 ballKickoff = if (studentHome == West)
67 {py=scale -0.05 field.fwidth, px=scale 0.06 field.flength}
68 {py=scale 0.05 field.fwidth, px=scale -0.06 field.flength}
69 ballGainedByComputer = any (\fb -> ballIsGainedBy fb.playerID ballState) compTeam
70
71 fail memory = {memory & practiceFailed = True}
72 stop memory = {memory & gameOver = True}
73 kick memory = {memory & ballKicked = True}
74
75 getAction :: !FootballerID -> Maybe FootballerEffect
76 getAction playerID = case [fb.effect \\ fb <- team1 ++ team2 | identify_player playerID fb] of
77 [effect:_] = effect
78 _ = Nothing
79
80 updateAllPos :: !Memory -> Memory
81 updateAllPos memory = {memory & positions = (kicker.pos,gainer.pos)}
82
83 getPlayersThatMovedTooFar :: !Memory -> [Footballer]
84 getPlayersThatMovedTooFar {positions=(kicker_pos,gainer_pos)}
85 = [fb \\ (fb,pos) <- [(kicker,kicker_pos),(gainer,gainer_pos)] | dist fb pos > m 10.0]
86
87 getMessagesForTooFarPlayers = map (\fb -> TellMessage ("Your player with number " <+++ fb.playerID.playerNr <+++ " moved further than 10 meters"))