Tutorials
In this section you find some code examples of what you have to do to create your own team / unit and you find too some code examples of what you must avoid!
This section is under construction.
Code Samples (J2SE 1.5)
Here you can find some code samples that can be very useful to understand the JaRTS' mode of function and discover how easy is develop your AI RTS team/unit.
DummyWorker
The DummyWorker is a simple worker unit that move over the map randomly and don't search for mines.
/* Simple worker that moves in a random way*/
import element.unit.Worker;
public class DummyWorker extends Worker
{
/**
* Just move randomly.
*/
public void updateAction()
{
// Chose a random value between 0 and 1
int x_movement = (int)(Math.random() * 1000) % 2;
int y_movement = (int)(Math.random() * 1000) % 2;
// Multiply the value by -1 in a random way
if (Math.random() > 0.5f)
x_movement = x_movement * -1;
if (Math.random() > 0.5f)
y_movement = y_movement * -1;
// Move the worker
this.move(this.getX() + x_movement, this.getY() + y_movement);
}
}
RandomWorker
The RandomWorker is a extension of the DummyWorker presented above but now the RandorWorker has a simple machine state.
/* Random worker that moves in a random way that can mine and deliver resources to the control center*/
import element.Element;
import element.terrain.Resource;
import element.unit.ControlCenter;
import element.unit.Worker;
public class RandomWorker extends Worker
{
// Worker's state machine
private static final byte STATE_SEARCH = 0;
private static final byte STATE_MINE = 1;
private static final byte STATE_RETURN = 2;
private static final byte STATE_DELIVER = 3;
// Store the worker's current state
private byte currentState;
// Store the mine that the worker is mining
private Resource currentMine;
public RandomWorker()
{
// Initial state
this.currentState = STATE_SEARCH;
this.currentMine = null;
}
public void updateAction()
{
// Choose the correct action
this.chooseAction();
}
private void chooseAction()
{
switch (this.currentState)
{
case STATE_SEARCH:
// Search mine
Resource res = this.findMine();
// If the mine is near from the worker, change the worker
// state to MINE,
// If the mine is not near from the worker, move
if (res == null)
{
this.randomMove();
}
else
{
this.currentState = STATE_MINE;
this.currentMine = res;
this.chooseAction();
}
break;
case STATE_MINE:
// If the mine still exists
if (this.currentMine != null)
{
// If the worker is full than change state to RETURN to
// the control center,
// If not, continue to mine
if (this.isFull())
{
this.currentState = STATE_RETURN;
this.chooseAction();
}
else
{
this.mine(this.currentMine);
}
}
else
{
// If the mine doesn't exist any more,
// change the state to SEARCH
this.currentState = STATE_SEARCH;
this.chooseAction();
}
break;
case STATE_RETURN:
// Try to find a near control center
ControlCenter cc = this.findCC();
// If there don't find, keep walking,
// if find, change the state to DELIVER
if (cc == null)
{
this.randomMove();
}
else
{
this.currentState = STATE_DELIVER;
this.chooseAction();
}
break;
case STATE_DELIVER:
// If the agent still has resource, keep delivering,
// If not, change the state to SEARCH
if (!this.isEmpty())
{
this.deliver();
}
else
{
this.currentState = STATE_SEARCH;
this.chooseAction();
}
break;
default:
break;
}
}
/**
* Random move
* Move in the map randomly.
*/
private void randomMove()
{
int x_movement = (int)(Math.random() * 1000) % 2;
int y_movement = (int)(Math.random() * 1000) % 2;
if (Math.random() > 0.5f)
x_movement = x_movement * -1;
if (Math.random() > 0.5f)
y_movement = y_movement * -1;
this.move(this.getX() + x_movement, this.getY() + y_movement);
}
/**
* Find a resource (mine) near from the worker.
* @return
*/
private Resource findMine()
{
Resource result = null;
int x = this.getX();
int y = this.getY();
for(int i = x-1; i <= x+1; i++)
{
for(int j = y-1; j <= y+1; j++)
{
Element element = this.getElementAt(i, j);
if (element instanceof Resource)
{
result = (Resource)element;
break;
}
}
}
return result;
}
/**
* Find a control center (cc) near from the worker.
* @return
*/
private ControlCenter findCC()
{
ControlCenter result = null;
int x = this.getX();
int y = this.getY();
for(int i = x-1; i <= x+1; i++)
{
for(int j = y-1; j <= y+1; j++)
{
Element element = this.getElementAt(i, j);
if (element instanceof ControlCenter)
{
result = (ControlCenter)element;
break;
}
}
}
return result;
}
/**
* Verify if the worker is full.
* @return
*/
private boolean isFull()
{
boolean result = true;
if (this.getResources() < this.getTotalCapacity())
{
result = false;
}
return result;
}
/**
* Verify if the worker is empty.
* @return
*/
private boolean isEmpty()
{
boolean result = false;
if (this.getResources() == 0)
{
result = true;
}
return result;
}
}
Code Mistakes
Here you can find some code examples that must be avoided.
Package
The JaRTS doesn't support the use of packages. So, be careful and don't use them!
/* Package example */
package blabla; // Retrieve this line of code to run normally.
import element.unit.Worker;
public class WorkerPackageExample extends Worker
{
// Some code...
}
Inner Class
The JaRTS support the use of inner class but be careful: the inner class must have the follow structure.
/* Inner class: GOOD example */
import element.unit.Worker;
public class WorkerGoodExample extends Worker
{
// Attributes of this class
InnerExample atributte1;
// Some code...
class InnerExample
{
// Inner class code
}
}
If you use a code structure like the follow example an Illegal Exception will be dispared.
/* Inner class: BAD example */
import element.unit.Worker;
public class WorkerBadExample extends Worker
{
// Attributes of this class
InnerExample atributte1;
// Some code...
}
class InnerExample
{
// Inner class code
}
Heading Level 3
07/10/2006 - Version 0.4b released!
06/10/2006 - Important information at the main page - Section Notes!
06/10/2006 - Version 0.4 released!
04/10/2006 - Version 0.3.1 released!
04/10/2006 - Version 0.3 released!
03/10/2006 - Tutorial section updated!
03/10/2006 - Version 0.3 in progress!
02/10/2006 - Site launching!