small thing
[fp1415-soccerfun.git] / src / Team_MartLubbers.icl
1 implementation module Team_MartLubbers
2
3 import Footballer
4 import FootballerFunctions
5
6 Names:: [String]
7 Names = [
8 "Odin",
9 "Frigg",
10 "Thor",
11 "Balder",
12 "Njord",
13 "Freyr",
14 "Freyja",
15 "Tyr",
16 "Bragi",
17 "Heimdall",
18 "Hoder",
19 "Vidar",
20 "Vale",
21 "Ullr",
22 "Forseti"]
23
24 Team_MartLubbers :: !Home !FootballField -> Team
25 Team_MartLubbers home field
26 | home==West = westTeam
27 | otherwise = eastTeam
28 where
29 eastTeam = mirror field westTeam
30 westTeam = [keeper : fielders]
31 clubname = base_TeamName_MartLubbers +++ if (home == West) "_W" "_E"
32 keeper = Aesir clubname home field {zero & px=scale -0.5 field.flength} 1 "Thor"
33 fielders = [Aesir clubname home field {px=scale (-0.5*dx) field.flength,py=scale (0.5*dy) field.fwidth} nr name
34 \\ (dx,dy) <- west_positions_fielders
35 & nr <- [2..]
36 & name <- Names
37 ]
38 west_positions_fielders = [(0.20, 0.40)
39 ,(0.20,-0.40)
40 ,(0.23, 0.00)
41 ,(0.50, 0.45)
42 ,(0.50,-0.45)
43 ,(0.60, 0.00)
44 ,(0.70, 0.35)
45 ,(0.70,-0.35)
46 ,(0.90, 0.05)
47 ,(0.90,-0.05)
48 ]
49
50 :: Mem = {home :: !Home, origpos :: Position, seed :: !RandomSeed}
51
52 mirrorMem :: !Mem !FootballField -> Mem
53 mirrorMem mm field = {mm & home=other mm.home, origpos=mirror field mm.origpos}
54
55 Aesir :: !ClubName !Home !FootballField !Position !PlayersNumber !String -> Footballer
56 Aesir club home field position nr name
57 = { playerID = {clubName=club,playerNr=nr}
58 , name = name +++ "_" <+++ nr
59 , length = min_length
60 , pos = position
61 , nose = zero
62 , speed = zero
63 , skills = (Running, Kicking, Rotating)
64 , effect = Nothing
65 , stamina = max_stamina
66 , health = max_health
67 , brain = {memory=if (home == West) mm (mirrorMem mm field), ai = mind field}
68 }
69 where
70 mm = {home=West, origpos=position, seed=nullRandomSeed}
71
72 nextRandomNumber :: !Mem -> (Int, !Mem)
73 nextRandomNumber mm=:{seed} = let (i, newseed) = random seed in (i rem 100, {mm & seed=newseed})
74
75 mind :: !FootballField !(!BrainInput, !Mem) -> (!BrainOutput, !Mem)
76 mind field (x=:{referee,football,others,me}, mm=:{home,origpos})
77 // In case of the end of the half, mirror memory
78 | any (isEndHalf) referee = mind field ({x & referee=filter (\x.not (isEndHalf x)) referee}, mirrorMem mm field)
79 // In case of a pause or end, just halt
80 | any (isPauseGame) referee || any (isGameOver) referee = halt (x, mm)
81 // TODO:: The ball is free
82 | ballIsFree football
83 // I'm closest to the ball
84 | isClosest us me ballPos.pxy = afterfix (\(_,_).(GainBall, mm)) ballPos.pxy prec (x, mm)
85 // TODO: Some other player is the closest to the ball
86 | otherwise = halt (x, mm)
87 // TODO: We have the ball
88 | any (\x.ballIsGainedBy x.playerID football) us = halt (x, mm)
89 // TODO: I have the ball
90 | ballIsGainedBy me.playerID football
91 // TODO: Someone else has the ball
92 | otherwise = halt (x, mm)
93 // TODO: The ball is with the others
94 | otherwise = halt (x, mm)
95 where
96 ballPos = (getBall x).ballPos
97 them = me opponents others
98 us = me team others
99 prec = scale 0.5 (maxKickReach me)
100
101 closest :: [Footballer] Position -> Footballer
102 closest xs p = minListBy (\x y.(dist x.pos p) < (dist y.pos p)) xs
103
104 isClosest :: [Footballer] Footballer Position -> Bool
105 isClosest xs x p = closest [x:xs] p == x
106
107 closerToGoal :: !Home -> Metre Metre -> Bool
108 closerToGoal East = (<)
109 closerToGoal West = (>)
110
111 distToLine :: Position Position Position -> Metre
112 distToLine a b c = (abs (((b.px-a.px)*(a.py-c.py))-((a.px-c.px)*(b.py-a.py))))/d
113 where
114 (*) m1 m2 = (m (toReal m1)/(toReal m2))
115 (/) m1 m2 = (m (toReal m1)/(toReal m2))
116 d = sqrt(((b.px-a.px)*(b.px-a.px))+((b.py-a.py)*(b.py-a.py)))
117
118 afterfix :: (FootballerAI Mem) !Position !Metre !(!BrainInput, !Mem) -> (BrainOutput, !Mem)
119 afterfix after point diff (input=:{me,others}, mm)
120 # (i, mm) = nextRandomNumber mm
121 | d < diff = after (input, mm)
122 // There is a enemy standing in the way and I feel like it
123 | (dist closestOpp me.pos) < (maxTackleReach me) && i < 15 = (Tackle closestOpp.playerID (ms 6.0), mm)
124 // There is no enemy standing in the way
125 | otherwise = (move, mm)
126 where
127 closestOpp = closest (me opponents others) me.pos
128 d = dist me point
129 a = bearing zero me point
130 r = bearing me.nose me point
131 v = ms (max 6.0 (toReal d))
132 move = Move {direction=a, velocity=v} r
133
134 base_TeamName_MartLubbers :: String
135 base_TeamName_MartLubbers = "Æsir"