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
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 intBALLType constant: ball (cf class Puck)protected intchannel_nPlayer number of the channelprotected intchannel_tTeam number of the channeldoubleenergyEnergy of the last lookprotected static intNODEType constant: node (cf class Puck)doublepenaltyPenalty of the last lookPuck[]pucksArray of objects of the last look which were seen or heardPuckselfPointer to itself inside the object array of the last lookdoubletimeDate of the last lookprotected static intUNITType constant: unit (cf class Puck)Fields inherited from class hoverball.Hovlet
background, BANNER, foreground, hovlets, icon, main, menubar, parent, title -
Constructor Summary
-
Method Summary
Modifier and Type Method Description protected voidaction(double Q, double F_L, double F_R)Sends an action.protected voidaction(double Q, double F_L, double F_R, java.lang.String message)Sends an action and a message.protected voidaction(java.lang.String message)Sends a message.protected booleanconnecting(java.lang.String server)[Implementation]protected voiddebug(Debug debug)Sets a debug object.protected voiddebug(Debug debug, int color)Sets a debug object with the specified color.protected voiddisconnecting()[Implementation]protected static java.lang.Stringformat(double d)protected static java.lang.Stringformat(Complex c)protected static java.lang.Stringformat(Matrix M)protected static java.lang.Stringformat(Vector v)protected booleanlook()Requests a new look.protected abstract voidloop()Empty for the implementation of the thinking loop.protected doubleoption(java.lang.String key)Returns the value of a Simulator option that has been set on check in.protected Puckpuck(int what, int t, int n)Returns the puck with the specified identity within the puck array.protected Puckpuck(java.lang.String id)Returns the puck with the specified identity within the puck array.
-
Field Details
-
NODE
protected static final int NODEType constant: node (cf class Puck)- See Also:
- Constant Field Values
-
BALL
protected static final int BALLType constant: ball (cf class Puck)- See Also:
- Constant Field Values
-
UNIT
protected static final int UNITType constant: unit (cf class Puck)- See Also:
- Constant Field Values
-
channel_t
protected int channel_tTeam number of the channel -
channel_n
protected int channel_nPlayer number of the channel -
time
public double timeDate of the last look -
energy
public double energyEnergy of the last look -
penalty
public double penaltyPenalty of the last look -
pucks
Array of objects of the last look which were seen or heard -
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 namename- unit name
-
Unit
public Unit(java.lang.String name, int color)Opens a unit.- Parameters:
name- unit namecolor- color
-
Unit
public Unit(java.lang.String team, java.lang.String name, int color)Opens a unit.- Parameters:
team- team namename- unit namecolor- color
-
-
Method Details
-
puck
Returns the puck with the specified identity within the puck array.- Parameters:
what- object typet- team numbern- puck number- Returns:
- the puck (or
null, if not found)
-
puck
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,pucksandself.The thread is stopped until the unit gets a new look from the Simulator. If the game is interrupted the method returns
falseand 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- PolarizationF_L- engine power leftF_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- PolarizationF_L- engine power leftF_R- engine power rightmessage- message
-
loop
protected abstract void loop()Empty for the implementation of the thinking loop. -
debug
Sets a debug object.- Parameters:
debug- debug object
-
debug
Sets a debug object with the specified color.- Parameters:
debug- debug objectcolor- color
-
format
protected static java.lang.String format(double d) -
format
-
format
-
format
-
connecting
protected final boolean connecting(java.lang.String server)[Implementation]- Overrides:
connectingin classHovlet- Parameters:
server- network address- Returns:
true, if connection succeeded
-
disconnecting
protected final void disconnecting()[Implementation]- Overrides:
disconnectingin classHovlet
-