initial framework added
[fp1415-soccerfun.git] / src / Game / FootballerFunctions.icl
1 implementation module FootballerFunctions
2
3 import Footballer
4
5 returnAI` :: !FootballerAction -> FootballerAI`
6 returnAI` action = const action
7
8 move` :: !Speed !Angle -> FootballerAI`
9 move` speed angle = returnAI` (Move speed angle)
10
11 halt` :: FootballerAI`
12 halt` = move` zero zero
13
14 rotate` :: !Angle -> FootballerAI`
15 rotate` angle = move` zero angle
16
17 ahead` :: !Velocity -> FootballerAI`
18 ahead` v = \input=:{me} -> move` {direction=me.nose,velocity=v} zero input
19
20 fix` :: !Position !Metre -> FootballerAI`
21 fix` point eps = \input=:{me} ->
22 let distance = dist me point
23 angle = bearing zero me point
24 rotate = bearing me.nose me point
25 v = ms (max 6.0 (toReal distance))
26 in if (distance <= eps)
27 (halt` input)
28 (move` {direction=angle,velocity=v} rotate input)
29
30 kick` :: !Position -> FootballerAI`
31 kick` point = \input=:{me} ->
32 let ball = getBall input
33 angle = bearing zero me point
34 v = ms (2.0 * (toReal (dist me point)))
35 in if (dist me ball <= maxKickReach me)
36 (KickBall {vxy = {direction=angle,velocity=v},vz=ms 1.0})
37 (halt` input)
38
39 track_ball` :: !Metre -> FootballerAI`
40 track_ball` eps = \input -> fix` (getBall input).ballPos.pxy eps input
41
42 amnesia :: FootballerAI` -> FootballerAI m
43 amnesia f = \(input,m) -> (f input,m)
44
45 returnAI :: (FootballerAction -> FootballerAI m)
46 returnAI = amnesia o returnAI`
47
48 move :: (Speed Angle -> FootballerAI m)
49 move = \speed angle -> amnesia (move` speed angle)
50
51 halt :: (FootballerAI m)
52 halt = amnesia halt`
53
54 rotate :: (Angle -> FootballerAI m)
55 rotate = amnesia o rotate`
56
57 ahead :: (Velocity -> FootballerAI m)
58 ahead = amnesia o ahead`
59
60 fix :: (Position Metre -> FootballerAI m)
61 fix = \point eps -> amnesia (fix` point eps)
62
63 kick :: (Position -> FootballerAI m)
64 kick = amnesia o kick`
65
66 track_ball :: (Metre -> FootballerAI m)
67 track_ball = \eps -> amnesia (track_ball` eps)
68
69 centerOfGoal :: !Home !FootballField -> Position
70 centerOfGoal home field = {zero & px = if (home==West) (~half_length) half_length}
71 where
72 half_length = scale 0.5 field.flength
73
74 (team) infix 9 :: !Footballer ![Footballer] -> [Footballer]
75 (team) player players = filter (sameClub player) players
76
77 (opponents) infix 9 :: !Footballer ![Footballer] -> [Footballer]
78 (opponents) player players = filter (not o (sameClub player)) players
79
80 getBall :: !BrainInput -> Football
81 getBall {football,me,others}= getFootball football [me : others]
82
83 :: HomeM m = { home :: !Home, mem :: !m }
84
85 educate` :: (Home -> FootballerAI`) -> FootballerAI (HomeM m)
86 educate` home_ai = \(input=:{referee},memory) ->
87 let new_home = if (any isEndHalf referee) other id memory.home
88 action = home_ai new_home input
89 in (action, {memory & home = new_home})
90
91 educate :: (Home -> FootballerAI m) -> FootballerAI (HomeM m)
92 educate home_ai = \(input=:{referee},memory) ->
93 let new_home = if (any isEndHalf referee) other id memory.home
94 (action,new_memory) = home_ai new_home (input,memory.mem)
95 in (action, {memory & home = new_home, mem = new_memory})