# Note: rules files can now use '#' as a comment character 
# (but only if it's the first char in a line)
# An alternative to this file is to use the -cmdline option on ./trilearn_player 
# and input coach commands via stdin. 
#

(define (definer "RegLeft" (rec (pt -52 35) (pt -5 -35) )) )
(define (definer "RegRight" (rec (pt -5 -35) (pt 52 35) )) )
# RegLeft and RegRight overlap a little

(define (definer "RegCenter" (rec (pt -5 -35) (pt 5 35) )) )
(define (definer "RegLeftGoal" (rec (pt -52 -35) (pt -45 35) )) )
(define (definer "RegLeftGoalSpace" (rec (pt -52 -10) (pt -45 10) )) )

(define (definer "RegRightGoal" (rec (pt 40 -20) (pt 52 20) )) )
#(define (definer "BallPos" (rec ((pt ball)+(pt -2 -2)) ((pt ball)+(pt 1 1)) )  ))

#(define (definer "CloseToBall" (rec ((pt ball) - (pt 0.5 0.5)) ((pt ball) + (pt 0.5 0.5)))))
# a circle around the ball:
(define (definer "CloseToBall" (arc (pt ball) 0 0.5 0 360) ))
(define (definer "RegAroundBall" (arc (pt ball) 0 10 0 360) ))

(define (definer "RegLeftOfBall" (rec (pt -52.5 -34) (((pt ball) * (pt 1 0)) + (pt 0 34)))))
(define (definer "RegRightOfBall" (rec (((pt ball) * (pt 1 0)) + (pt 0 34)) (pt 52.5 -34))))

(define (definer "RegVeryDefense" (rec (pt -52.5 -34) (pt -30 34))))
(define (definer "RegDefense" (rec (pt -52.5 -34) (pt -15 34))))
(define (definer "RegMidfield" (rec (pt -15 -34) (pt 15 34))))
(define (definer "RegOffense" (rec (pt 15 -34) (pt 52.5 34))))

(define (definer "RegBallSideTop" (rec ((pt ball) * (pt 1 0) + (pt -10 0)) ((pt ball) * (pt 1 0) + (pt 10 -20)))))
(define (definer "RegBallSideBot" (rec ((pt ball) * (pt 1 0) + (pt -10 0)) ((pt ball) * (pt 1 0) + (pt 10 20)))))

#-----------------------------------------------------------------------------------------

# Defender offsides and marking

# defensive strategies

# if the ball is in the right side of the field, pull defenders up!
# We have to separate the regions because the UTA players will all go to middle
# otherwise.

(define (definer "RegOffsides%D4" (rec (pt -15 -28) (pt -10 -15))))
(define (definer "RegOffsides%D3" (rec (pt -15 -15) (pt -10 0))))
(define (definer "RegOffsides%D2" (rec (pt -15 0) (pt -10 25))))
(define (definer "RegOffsides%D1" (rec (pt -15 15) (pt -10 28))))
(define (definerule PullOffsides direc ((and (playm play_on) (bpos "RegRight")) (do our {%D1} (pos "RegOffsides%D1")) (do our {%D2} (pos "RegOffsides%D2")) (do our {%D3} (pos "RegOffsides%D3")) (do our {%D4} (pos "RegOffsides%D4")) )))
# turn the rules on
(rule (on PullOffsides))

# if the player is a defender and the ball is behind it, go straight to a point left of the ball
#(define (definerule GetBehindBall direc ((and (playm play_on) (ppos our {%D1 %D2 %D3 %D4} 1 4 "RegRightOfBall")) (do our {%D1 3 4 %D4} (pos "RegLeftOfBall")))))
# a more complicated version so that only the players who are right of the ball go there
#(define (definerule GetBehindBall direc ((and (playm play_on) (unum X {%D1 3 4 %D4} (ppos our {X} 1 4 "RegRightOfBall")) (do our {X} (pos "RegLeftOfBall")))))
# now exapnded out
(define (definerule GetBehindBall%D1 direc ((and (playm play_on) (ppos our {%D1} 1 1 "RegRightOfBall")) (do our {%D1} (pos "RegLeftOfBall")))) (definerule GetBehindBall%D2 direc ((and (playm play_on) (ppos our {%D2} 1 1 "RegRightOfBall")) (do our {%D2} (pos "RegLeftOfBall")))) (definerule GetBehindBall%D3 direc ((and (playm play_on) (ppos our {%D3} 1 1 "RegRightOfBall")) (do our {%D3} (pos "RegLeftOfBall")))) (definerule GetBehindBall%D4 direc ((and (playm play_on) (ppos our {%D4} 1 1 "RegRightOfBall")) (do our {%D4} (pos "RegLeftOfBall")))) )
# turn the rules on
(rule (on (GetBehindBall%D1 GetBehindBall%D2 GetBehindBall%D3 GetBehindBall%D4)))


# if the ball is in our side of the field, start marking opponents!
(define (definer "RegAlwaysMark" (rec (pt -52 35) (pt -5 -35) )) )
(define (definer "RegSometimesMark" (rec (pt -5 35) (pt 25 -35) )) )
(define (definer "RegAlwaysPullUp" (rec (pt 25 35) (pt 52.5 -35) )) )
(define (definer "RegMarkAroundBall" (rec ((pt ball) - (pt 20 30)) ((pt ball) + (pt 20 30)))))

(define (definerule MarkPlayer direc ((and (playm play_on) (not (bpos "RegAlwaysPullUp")) (or (bpos "RegAlwaysMark") (and (not (bowner our {0})) (bpos "RegSometimesMark"))) (unum MarkTarg {0}) (ppos opp {MarkTarg} 1 11 "RegMarkAroundBall")) (do our {%D1 %D2 %D3 %D4} (mark {MarkTarg})))))
# turn the rules on
(rule(on MarkPlayer))

# in a dangerous def setting, have the middle defenders crash the ball
(define (definer "RegTopBox" (rec (pt -45 -20) (pt -30 20))))
(define (definerule DefCloseIn direc ((bpos "RegTopBox") (do our {%D2 %D3} (intercept)))))

#-----------------------------------------------------------------------------------------
# Midfielders stuff

# Bring the midfielders back if the ball comes defensively
(define (definerule MidBackUp%M1 direc ((and (bpos "RegVeryDefense")) (do our {%M1} (pos (rec (pt -35 -30) (pt -15 -10)))))))
(define (definerule MidBackUp%M2 direc ((and (bpos "RegVeryDefense")) (do our {%M2} (pos (rec (pt -35 -10) (pt -15 10)))))))
(define (definerule MidBackUp%M3 direc ((and (bpos "RegVeryDefense")) (do our {%M3} (pos (rec (pt -35 10) (pt -15 30)))))))

#-----------------------------------------------------------------------------------------
# Forwards stuff

# Bring the midfielders back if the ball comes defensively
(define (definerule ForBackUp%F1 direc ((and (bpos "RegVeryDefense")) (do our {%F1} (pos (rec (pt -15 -30) (pt 5 -10)))))))
(define (definerule ForBackUp%F2 direc ((and (bpos "RegVeryDefense")) (do our {%F2} (pos (rec (pt -15 -10) (pt 5 10)))))))
(define (definerule ForBackUp%F3 direc ((and (bpos "RegVeryDefense")) (do our {%F3} (pos (rec (pt -15 10) (pt 5 30)))))))


#-----------------------------------------------------------------------------------------
# handling the ball - these should all be really simple so that the action picker can choose based on priority levels

# only defenders and midfielders should clear the ball
(define (definerule ClearBall direc ((and (playm play_on) (unum DefenderMid {%D1 %D2 %D3 %D4 %M1 %M2 %M3}) (bpos (arc (pt our DefenderMid) 0 .7 0 360)) (bpos "RegLeft")) (do our {DefenderMid} (clear "RegLeft")))))
(rule (on ClearBall))

# all players should consider pass forwards if the ball is kickable
#(define (definerule PassBall direc ((and (playm play_on) (unum Passer {0}) (bpos (arc (pt our Passer) 0 0.7 0 360)) (unum Passee {0}) (ppos our {Passee} 1 11 "RegRightOfBall")) (do our {Passer} (pass {Passee})))))
#(rule (on PassBall))

# all players should consider dribbling if the ball is kickable
#(define (definerule DribbleDownField direc ((and (unum Dribbler {0}) (bpos "RegRight")) (do our {Dribbler} (dribble "RegRightGoal")))))
#(rule (off DribbleDownField))

# all players should consider taking a shot
#(define (definerule ShootBall direc ((and (unum Shooter {0}) (bpos (arc (pt our Defender) 0 .7 0 360))) (do our {Shooter} (shoot)))))
#(rule (on ShootBall))

# all players should consider a tackle
#(define (definerule TackleBall direc ((unum Tackler {0}) (do our {Tackler} (tackle {0})))))
#(rule (on TackleBall))

#-----------------------------------------------------------------------------------------
