Review of the offside rule implementation
Virtual Werder 3D Team


As we have observed some strange offside decisions (sorry, no logfiles) in
version 0.4, we have decided to investigate the problem by having a look at
the source. Now, just a few hours ago, version 0.5 was released, but a quick
look showed that the problems we found in 0.4 are still present. This is
what we found:

The code is actually not an implementation of the official FIFA offside
rule, but only something similar.

The FIFA offside rule, taken from
http://www.fifa.com/documents/fifa/laws/LOTG2005_e.pdf, states:

---- BEGIN QUOTE ----
It is not an offence in itself to be in an offside position. A player is in
an offside position if:
    * he is nearer to his opponents' goal line than both the ball and the
      second last opponent.

A player is not in an offside position if
    * he is in his own half of the field of play or
    * he is level with the second last opponent or
    * he is level with the last two opponents.
----- END QUOTE -----

The critical section here is "nearer to his opponents' goal line than both
the ball and the second last opponent". We could not find that the ball
position was taken into account in the current implementation. Instead, a
player seems to be judged to be in offside position when he is nearer to his
opponents' goal line than the second last opponent as the only criterion.

This could possibly lead to
(K = Keeper, D = Defender, A = Attacker, o = Ball)

---------GOAL---------
          K

     Ao

            A
       D

being judged as offside by the server, if the ball is passed from the left
attacker to his team member on the right. But this is not offside according
to FIFA.


FIFA continues:

---- BEGIN QUOTE ----
A player in an offside position is only penalised if, at the moment the ball
touches or is played by one of his team, he is, in the opinion of the
referee, involved in active play by:

    * interfering with play or
    * interfering with an opponent or
    * gaining an advantage by being in that position.
----- END QUOTE -----

In the code, we could find the following comment:
---- BEGIN QUOTE ----
    // if the agent,that touches the ball, is in offside position and was in 
    // offside position before the last shot, change the mode to offside
----- END QUOTE -----

There is a distinct difference here. According to FIFA, it is only relevant
to be in offside position in the moment the ball is played by one's team
member. It is NOT important if a player is in offside position in the moment
he receives the ball.

This could lead to

(moment when the pass           (moment when the pass
is executed)                    is received)

---------GOAL---------          ---------GOAL---------
          K                               K

                                    D
     A                                Ao
    D
         o                                A
         A

being allowed by the server. Following FIFA, this is offside.

The two problems we described above, have (hopefully) been fixed in the
attached patch file for the 0.5 version.
Use: patch soccerruleaspect.cpp offside-patch
Note: This patch has not been thoroughly tested. The server still runs, but
we do not know if everything is as intended.


There are some more problems, which we could not fix, as this would involve
more complex changes, and we do not know the code well enough to do that.

FIFA:

---- BEGIN QUOTE ----
There is no offside offence if a player receives the ball directly from:

    * a goal kick or
    * a throw-in or
    * a corner kick
----- END QUOTE -----

(Probably, one needs to remember which playmode was before "play_on"...)

and another:

---- BEGIN QUOTE ----
The definitions of elements of involvement in active play are as follows:

    * Interfering with play means playing or touching the ball passed or
      touched by a team-mate.

    * Interfering with an opponent means preventing an opponent from playing
      or being able to play the ball by clearly obstructing the opponent's
      line of vision or movements or making a gesture or movement which, in
      the opinion of the referee, deceives or distracts an opponent.

    * Gaining an advantage by being in that position means playing a ball
      that rebounds to him off a post or the crossbar having been in an
      offside position or playing a ball that rebounds to him off an
      opponent having been in an offside position.
----- END QUOTE -----

The first of these points is what is currently implemented.

The second seems quite hard to decide from server perspective, especially
when having to judge if the player "makes distracting gestures" or not. ;-)
One could probably drop this point for the implementation, but at least
this change to the official rules should be mentioned in the manual.

The third is something that could again lead to problems in the current
implementation. As we see it, every time the ball comes from an opposing
player, the situation is decided not to be offside in the current
implementation. The official rule differentiates between rebounding from an
opponent (which is offside) and the ball being played by that opponent
(which is not offside).

A situation could be:

---------GOAL---------
          K

             A
     Ao

The attacker performs a shot at the goal. The ball only hits the keeper and
bounces to the right attacker. This is offside according to FIFA, while the
server would see the ball coming from an opponent and allow this situation.
However, if the keeper got the ball under control and then plays the ball so
that it gets to the right attacker, it is not offside for FIFA.
For the server, this means we need a criterion for "just bouncing or really
playing". A possible idea could be to check if the keeper uses the kick
effector (defining this as "playing the ball") or not (defining as
"bouncing").
