initial framework added
[fp1415-soccerfun.git] / src / StdReferee / RefereeFunctions.icl
1 implementation module RefereeFunctions
2
3 import Referee
4
5 ball_left_field_at :: !FootballField -> RefereeAI` (Maybe Position)
6 ball_left_field_at field=:{fwidth,flength}
7 = \{RefereeInput | theBall}
8 -> case theBall of
9 Free ball = if (point_in_rectangle field_coordinates ball.ballPos.pxy)
10 Nothing
11 (Just (point_to_rectangle field_coordinates ball.ballPos.pxy))
12 gained = Nothing // footballers are incapable of leaving the football field
13 where
14 field_coordinates = ({px = scale -0.5 flength, py = scale -0.5 fwidth}, {px = scale 0.5 flength, py = scale 0.5 fwidth})
15
16 ball_in_goal :: !FootballField -> RefereeAI` (Maybe Home)
17 ball_in_goal field=:{flength}
18 = \{RefereeInput | theBall}
19 -> case theBall of
20 Free ball = let ball_inside = isbetween ball.ballPos.pxy.py south north && ball.ballPos.pz <= goal_height
21 in if (ball.ballPos.pxy.px < scale -0.5 flength && ball_inside) (Just West)
22 (if (ball.ballPos.pxy.px > scale 0.5 flength && ball_inside) (Just East)
23 Nothing
24 )
25 gained = Nothing // footballers must kick the ball in the goal to score
26 where
27 (north,south) = goal_poles field
28
29 half_of_game :: !PlayingTime -> RefereeAI` Half
30 half_of_game total_time
31 = \{RefereeInput | playingTime}
32 -> if (playingTime >= scale 0.5 total_time) FirstHalf SecondHalf
33
34 players_in_offside_position :: !FootballField !Home -> RefereeAI` (AssocList FootballerID Position)
35 players_in_offside_position field home
36 = \{RefereeInput | theBall,playingHalf,team1,team2}
37 -> let (players1,players2) = if (home == West && playingHalf == FirstHalf || home == East && playingHalf == SecondHalf)
38 (displacements team1, displacements team2)
39 (displacements team2, displacements team1)
40 ball = getFootball theBall (team1 ++ team2)
41 in offside_players field ball home players1 players2
42
43 /** offside_players field ball home team opponents:
44 returns the players from @team that are in offside position.
45 @home is the current home of @team, and @opponents are the current opponents.
46 @ball is the current ball.
47 */
48 offside_players :: !FootballField !Football !Home !Displacements !Displacements -> Displacements
49 offside_players field ball home team opponents
50 = [(player,pos) \\ (player,pos) <- team // a player is in offside position if:
51 | team_ord pos.px middle_x // he is at the opponent's half of the field, and
52 && team_ord pos.px opponent_x // he is closer to the opponent's base line than the 2nd-last opponent, and
53 && team_ord pos.px ball_x // he is closer to the opponent's base line than the ball
54 ]
55 where
56 middle_x = zero
57 ball_x = ball.ballPos.pxy.px
58 team_ord = if (home == West) (>) (<)
59 opponent_x = (snd ((sortBy opponent_ord opponents) !! 1)).px
60 opponent_ord = if (home == West) (\(_,p1) (_,p2) -> p1.px >= p2.px) (\(_,p1) (_,p2) -> p1.px <= p2.px)