initial framework added
[fp1415-soccerfun.git] / src / StdReferee / RefereeCoach_Passing_Assignment.icl
1 implementation module RefereeCoach_Passing_Assignment
2
3 import StdEnvExt
4 import Referee
5 import Team_Opponent_Passing_Assignment
6
7 RefereeCoach_Passing :: !FootballField -> Referee
8 RefereeCoach_Passing field = { name = "RefereeCoach Passing"
9 , brain = { memory = initMem
10 , ai = randomlessRefereeAI (brain field)
11 }
12 , refActionPics = []
13 }
14
15 :: Memory = { first :: !Bool
16 , kickedBy1 :: !Bool // PA: oh dear, you should use [Bool], or better: if in sequence: Int
17 , kickedBy2 :: !Bool
18 , kickedBy3 :: !Bool
19 , kickedBy4 :: !Bool
20 , kickedBy5 :: !Bool
21 , kickedBy6 :: !Bool
22 , pos1 :: !Position // PA: dito, but now [Position]
23 , pos2 :: !Position
24 , pos3 :: !Position
25 , pos4 :: !Position
26 , pos5 :: !Position
27 , pos6 :: !Position
28 , practiceFailed :: !Bool
29 , gameOver :: !Bool
30 }
31
32 initMem :: Memory
33 initMem = { first = True
34 , kickedBy1 = False
35 , kickedBy2 = False
36 , kickedBy3 = False
37 , kickedBy4 = False
38 , kickedBy5 = False
39 , kickedBy6 = False
40 , pos1 = zero
41 , pos2 = zero
42 , pos3 = zero
43 , pos4 = zero
44 , pos5 = zero
45 , pos6 = zero
46 , practiceFailed = False
47 , gameOver = False
48 }
49
50 brain :: !FootballField !(!RefereeInput,!Memory) -> (!RefereeOutput,!Memory)
51 brain field ({RefereeInput | theBall=ballState, team1, team2}, memory)
52 | memory.gameOver // game over because of succes or failure
53 = ([GameOver],memory)
54 | memory.practiceFailed // assignment has failed
55 = ([TellMessage "Assignment failed, correct your team and try again"],{memory & gameOver = True})
56 | length players <> 6 // Check if there are indeed six fielders (to prevent run time errors)
57 = ([TellMessage "Number of fielders should be six. Correct your team and try again."],{memory & gameOver = True})
58 # (refActions,memory) = if memory.first // when first, alter ball pos and remember all positions
59 ([DirectFreeKick studentHome ballKickoff],{updateAllPos memory & first = False})
60 ([],memory)
61 # playersMovedToFar = getPlayersThatMovedTooFar memory // check if players did not move more than 10 meters from their starting positions
62 | not (isEmpty playersMovedToFar)
63 = (refActions ++ getMessagesForTooFarPlayers playersMovedToFar,{memory & practiceFailed = True})
64 | not memory.kickedBy1 // first player has not kicked the ball yet
65 # (more,memory) = checkIfPlayerKickedBall 1 memory
66 = (refActions++more,memory)
67 | not memory.kickedBy2 // second player has not kicked the ball yet
68 # (more,memory) = checkIfPlayerKickedBall 2 memory
69 = (refActions++more,memory)
70 | not memory.kickedBy3 // third player has not kicked the ball yet
71 # (more,memory) = checkIfPlayerKickedBall 3 memory
72 = (refActions++more,memory)
73 | not memory.kickedBy4 // fourth player has not kicked the ball yet
74 # (more,memory) = checkIfPlayerKickedBall 4 memory
75 = (refActions++more,memory)
76 | not memory.kickedBy5 // fifth player has not kicked the ball yet
77 # (more,memory) = checkIfPlayerKickedBall 5 memory
78 = (refActions++more,memory)
79 | not memory.kickedBy6 // sixth player has not kicked the ball yet
80 # (more,memory) = checkIfPlayerKickedBall 6 memory
81 = (refActions++more,memory)
82 | ballIsGoingInWrongDirection // ball is going in the wrong direction
83 = (refActions ++ [TellMessage "ball should be played towards the goal"],{memory & practiceFailed = True})
84 | ballBehindLine // ball is behind the line
85 | theBall.ballPos.pz > goal_height // ball is over
86 = (refActions ++ [TellMessage "Try shooting less high, the ball went over"],{memory & practiceFailed = True})
87 | isbetween theBall.ballPos.pxy.py south_pole north_pole
88 = (refActions ++ [TellMessage "Well Done! Move on to the next assignment!"],{memory & gameOver = True})
89 | otherwise
90 = (refActions ++ [TellMessage "Ball is out, try shooting in the goal next time"],{memory & practiceFailed = True})
91 | theBall.ballSpeed.vxy.velocity == zero // ball stopped moving
92 = (refActions ++ [TellMessage "Try shooting harder, the ball stopped moving forwards"],{memory & practiceFailed = True})
93 | otherwise // ball may be on its way to the goal
94 = (refActions,memory)
95 where
96 theBall = getFootball ballState (team1 ++ team2)
97 (compTeam,(studentTeam,studentHome)) = if (stringStarts (nameOf team1) base_TeamName_Opponent_Passing) (team1,(team2,East)) (team2,(team1,West))
98 ballKickoff = {zero & px = if (studentHome == West) (scale -0.5 field.flength + penalty_area_depth) (scale 0.5 field.flength - penalty_area_depth)}
99 getMessagesForTooFarPlayers = map (\fb -> TellMessage ("Your player with number " <+++ fb.playerID.playerNr <+++ " moved further than 10 meters"))
100 (north_pole,south_pole) = goal_poles field
101
102 players = filter isFielder studentTeam
103 stud1 = players!!0
104 stud2 = players!!1
105 stud3 = players!!2
106 stud4 = players!!3
107 stud5 = players!!4
108 stud6 = players!!5
109 stud1fa = getAction stud1.playerID
110 stud2fa = getAction stud2.playerID
111 stud3fa = getAction stud3.playerID
112 stud4fa = getAction stud4.playerID
113 stud5fa = getAction stud5.playerID
114 stud6fa = getAction stud6.playerID
115
116 getAction :: !FootballerID -> Maybe FootballerEffect
117 getAction playerID = case [fb.effect \\ fb <- team1 ++ team2 | identify_player playerID fb] of
118 [effect:_] = effect
119 _ = Nothing
120
121 ballIsGoingInWrongDirection = if (studentHome == West) > < theBall.ballSpeed.vxy.direction (rad (0.5*pi)) &&
122 if (studentHome == East) < > theBall.ballSpeed.vxy.direction (rad (1.5*pi))
123 ballBehindLine = abs theBall.ballPos.pxy.px > scale 0.5 field.flength
124
125 updateAllPos :: !Memory -> Memory
126 updateAllPos memory = {memory & pos1 = stud1.pos
127 , pos2 = stud2.pos
128 , pos3 = stud3.pos
129 , pos4 = stud4.pos
130 , pos5 = stud5.pos
131 , pos6 = stud6.pos
132 }
133
134 getPlayersThatMovedTooFar :: !Memory -> [Footballer]
135 getPlayersThatMovedTooFar memory
136 = [fb \\ fb <- players & pos <- [memory.pos1,memory.pos2,memory.pos3,memory.pos4,memory.pos5,memory.pos6] | dist fb pos > m 10.0]
137
138 checkIfPlayerKickedBall :: !Int !Memory -> (![RefereeAction],!Memory)
139 checkIfPlayerKickedBall i memory
140 # (studfa,stud) = case i of
141 1 -> (stud1fa,stud1)
142 2 -> (stud2fa,stud2)
143 3 -> (stud3fa,stud3)
144 4 -> (stud4fa,stud4)
145 5 -> (stud5fa,stud5)
146 6 -> (stud6fa,stud6)
147 | perhaps isKickedBall studfa = ([], updateMemory i memory)
148 | perhaps isCaughtBall studfa = ([Hands stud.playerID, ReprimandPlayer stud.playerID RedCard],{memory & practiceFailed = True})
149 | otherwise = ([], memory)
150 where
151 updateMemory :: !Int !Memory -> Memory
152 updateMemory 1 m = {m & kickedBy1 = True}
153 updateMemory 2 m = {m & kickedBy2 = True}
154 updateMemory 3 m = {m & kickedBy3 = True}
155 updateMemory 4 m = {m & kickedBy4 = True}
156 updateMemory 5 m = {m & kickedBy5 = True}
157 updateMemory 6 m = {m & kickedBy6 = True}