Package hoverball

Class Unit

java.lang.Object
hoverball.Hovlet
hoverball.Unit
Direct Known Subclasses:
Human

public abstract class Unit
extends Hovlet
The class Unit, based on the class Hovlet, represents the base class for all Java units. The Unit connects as Unit Client to the Simulator and facilitates the creation of your own players by offering a great deal of integrated functions.

After the definition of the constructor, where you can specify team and player name, you only have to implement the thinking loop loop(). Please consider that

  • You should exit the looping method when the game has been interrupted, for it is called up again when the game continues. This can be achieved by the correct handling of the method look() waiting for the next "look" of the Simulator. If the game has been interrupted, look() returns the value false, else true. This means that the looping structure
    public void loop()
    {
       while(look())
       {
          ...
       }
    }
    
    answers the purpose.

  • Before looping all check in parameters are known, and inside the loop - after calling look() - the current object positions which can be accessed in the array pucks.

  • The action should be sent at the end of the loop by action(...).

  • Debug objects can always be set by debug(...).

  • The main panel of a unit hovlet is unused. You can use it to display your own analyses.


    Example of a trivial Java unit:

    import hoverball.*;
    import hoverball.math.*;
     
    public class Clumsy extends Unit
    {
       public Clumsy ()
       {
          super(null,"Clumsy",0x8888FF);  // name "Clumsy", color light blue
       }
     
       public void loop ()
       {
          Sphere sphere = new Sphere(option("world.radius"));  // read out parameters...
          double Qmax = option("unit.charge.max");
          double Fmax = option("unit.engine.max");
     
          while (look())
          {
             Puck ball = puck(BALL,0,1);       // that's the ball
             Puck goal = puck(BALL,self.t,1);  // that's the goal
     
             if (ball == null) action(0,-Fmax,Fmax);     // does not see ball? turn!
             else {                                      // does see ball:
                Vector a = (goal != null)? Vector.vec(goal.X.c,ball.X.c) :  // calculate
                                           Vector.vec(ball.X.c,self.X.c);   // axis
                Matrix X = Matrix.mul(ball.X,Matrix.rot(a,(self.r+ball.r)/sphere.rad));
                Complex x = sphere.warp(X.c);            // X is shot position
     
                double l = -x.arg() + Math.max(0.2,1-10*Math.abs(x.arg())); // turn and
                double r =  x.arg() + Math.max(0.2,1-10*Math.abs(x.arg())); // go to X
                double q = (x.abs() < ball.r)? 0.5 : 0;  // if at X: shoot!
     
                action(q*Qmax,l*Fmax,r*Fmax);
             }
          }
       }
    }
    
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static int BALL
      Type constant: ball  (cf class Puck)
      protected int channel_n
      Player number of the channel
      protected int channel_t
      Team number of the channel
      double energy
      Energy of the last look
      protected static int NODE
      Type constant: node  (cf class Puck)
      double penalty
      Penalty of the last look
      Puck[] pucks
      Array of objects of the last look which were seen or heard
      Puck self
      Pointer to itself inside the object array of the last look
      double time
      Date of the last look
      protected static int UNIT
      Type constant: unit  (cf class Puck)

      Fields inherited from class hoverball.Hovlet

      background, BANNER, foreground, hovlets, icon, main, menubar, parent, title
    • Constructor Summary

      Constructors 
      Constructor Description
      Unit​(java.lang.String name)
      Opens a unit.
      Unit​(java.lang.String name, int color)
      Opens a unit.
      Unit​(java.lang.String team, java.lang.String name)
      Opens a unit.
      Unit​(java.lang.String team, java.lang.String name, int color)
      Opens a unit.
    • Method Summary

      Modifier and Type Method Description
      protected void action​(double Q, double F_L, double F_R)
      Sends an action.
      protected void action​(double Q, double F_L, double F_R, java.lang.String message)
      Sends an action and a message.
      protected void action​(java.lang.String message)
      Sends a message.
      protected boolean connecting​(java.lang.String server)
      [Implementation]
      protected void debug​(Debug debug)
      Sets a debug object.
      protected void debug​(Debug debug, int color)
      Sets a debug object with the specified color.
      protected void disconnecting()
      [Implementation]
      protected static java.lang.String format​(double d)  
      protected static java.lang.String format​(Complex c)  
      protected static java.lang.String format​(Matrix M)  
      protected static java.lang.String format​(Vector v)  
      protected boolean look()
      Requests a new look.
      protected abstract void loop()
      Empty for the implementation of the thinking loop.
      protected double option​(java.lang.String key)
      Returns the value of a Simulator option that has been set on check in.
      protected Puck puck​(int what, int t, int n)
      Returns the puck with the specified identity within the puck array.
      protected Puck puck​(java.lang.String id)
      Returns the puck with the specified identity within the puck array.

      Methods inherited from class hoverball.Hovlet

      add, browse, close, connect, disconnect, locate, locate, show, status, title

      Methods inherited from class java.lang.Object

      clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Details

      • NODE

        protected static final int NODE
        Type constant: node  (cf class Puck)
        See Also:
        Constant Field Values
      • BALL

        protected static final int BALL
        Type constant: ball  (cf class Puck)
        See Also:
        Constant Field Values
      • UNIT

        protected static final int UNIT
        Type constant: unit  (cf class Puck)
        See Also:
        Constant Field Values
      • channel_t

        protected int channel_t
        Team number of the channel
      • channel_n

        protected int channel_n
        Player number of the channel
      • time

        public double time
        Date of the last look
      • energy

        public double energy
        Energy of the last look
      • penalty

        public double penalty
        Penalty of the last look
      • pucks

        public Puck[] pucks
        Array of objects of the last look which were seen or heard
      • self

        public Puck self
        Pointer to itself inside the object array of the last look
    • Constructor Details

      • Unit

        public Unit​(java.lang.String name)
        Opens a unit.
        Parameters:
        name - unit name
      • Unit

        public Unit​(java.lang.String team, java.lang.String name)
        Opens a unit.
        Parameters:
        team - team name
        name - unit name
      • Unit

        public Unit​(java.lang.String name, int color)
        Opens a unit.
        Parameters:
        name - unit name
        color - color
      • Unit

        public Unit​(java.lang.String team, java.lang.String name, int color)
        Opens a unit.
        Parameters:
        team - team name
        name - unit name
        color - color
    • Method Details

      • puck

        protected Puck puck​(int what, int t, int n)
        Returns the puck with the specified identity within the puck array.
        Parameters:
        what - object type
        t - team number
        n - puck number
        Returns:
        the puck (or null, if not found)
      • puck

        protected Puck puck​(java.lang.String id)
        Returns the puck with the specified identity within the puck array.
        Parameters:
        id - Identity string
        Returns:
        the puck (or null, if not found)
      • option

        protected double option​(java.lang.String key)
        Returns the value of a Simulator option that has been set on check in.

        Not every Simulator option is sent to the units.

        Parameters:
        key - Simulator option
        Returns:
        the value
      • look

        protected boolean look()
        Requests a new look.

        This method should be called in the unit's thinking loop. It updates the variables time, pucks and self.

        The thread is stopped until the unit gets a new look from the Simulator. If the game is interrupted the method returns false and the loop should be quit.

        Returns:
        true, if the game continues
      • action

        protected void action​(double Q, double F_L, double F_R)
        Sends an action.

        If a parameter of the action is to not be modified it can be replaced by the value Double.NaN.

        Parameters:
        Q - Polarization
        F_L - engine power left
        F_R - engine power right
      • action

        protected void action​(java.lang.String message)
        Sends a message.
        Parameters:
        message - message
      • action

        protected void action​(double Q, double F_L, double F_R, java.lang.String message)
        Sends an action and a message.

        If a parameter of the action is to not be modified it can be replaced by the value Double.NaN.

        Parameters:
        Q - Polarization
        F_L - engine power left
        F_R - engine power right
        message - message
      • loop

        protected abstract void loop()
        Empty for the implementation of the thinking loop.
      • debug

        protected void debug​(Debug debug)
        Sets a debug object.
        Parameters:
        debug - debug object
      • debug

        protected void debug​(Debug debug, int color)
        Sets a debug object with the specified color.
        Parameters:
        debug - debug object
        color - color
      • format

        protected static java.lang.String format​(double d)
      • format

        protected static java.lang.String format​(Complex c)
      • format

        protected static java.lang.String format​(Vector v)
      • format

        protected static java.lang.String format​(Matrix M)
      • connecting

        protected final boolean connecting​(java.lang.String server)
        [Implementation]
        Overrides:
        connecting in class Hovlet
        Parameters:
        server - network address
        Returns:
        true, if connection succeeded
      • disconnecting

        protected final void disconnecting()
        [Implementation]
        Overrides:
        disconnecting in class Hovlet