Retour à la page de téléchargement
// Individual termite for Artificial Termites.
//
// Author Matthew Caryl
// Created 29.10.96
//
// Although under copywrite to the author (Matthew Caryl) this code can be copied and modified for non-commercial
// purposes as long as any derivatives contain this condition.
//
// Last modification: 2000-04-02 changed graphics Xavier Outhier
// Last modification: 2000-04-05 adding wheat and cheese Xavier Outhier
// Last modification: 2000-04-06 adding forbidden Xavier Outhier
package ArtificialTermites;
import java.awt.*;
final class Termite {
private World containingWorld;
private WorldApplet appletWorld;
int x, y;
int facing; // in range 0..7
boolean carryingSomething;
boolean carryingWood;
boolean carryingWheat;
boolean carryingCheese;
private static Color termiteColor = Color.red;
private static int facingXDelta[] = { 0, 1, 1, 1, 0, -1, -1, -1}; // x-delta for facing
private static int facingYDelta[] = {-1, -1, 0, 1, 1, 1, 0, -1}; // y-delta for facing
public Termite(World w, WorldApplet wa, int xcoord, int ycoord) {
containingWorld = w;
appletWorld = wa;
x = xcoord;
y = ycoord;
facing = randomFacing();
carryingSomething = false;
carryingWood = false;
carryingWheat = false;
carryingCheese = false;
}
public void paint() {
if (carryingWood == true)
{
appletWorld.paintBlock(x, y, appletWorld.grassAntWood);
}
else if (carryingCheese == true)
{
appletWorld.paintBlock(x, y, appletWorld.grassAntCheese);
}
else if (carryingWheat == true)
{
appletWorld.paintBlock(x, y, appletWorld.grassAntWheat);
}
else
{
appletWorld.paintBlock(x, y, appletWorld.grassAnt);
}
}
public void tick() {
appletWorld.paintBlock(x, y); // erase screen block
if (timeForTurn()) // maybe turn a bit
randomTurn();
while (forbiddenAt(facing))
randomTurn();
move(facing); // move forward
if (woodAt(facing)) {
if (carryingSomething==false) // if wood in front and not carrying then pick up
getWood(facing);
else if (!woodAt() && carryingWood) { // if wood in front and carrying and no wood here then
putWood(); // put down and about turn
turn(4);
}
}
else if (wheatAt(facing)) {
if (!carryingSomething) // if wheat in front and not carrying then pick up
getWheat(facing);
else if (!wheatAt() && carryingWheat) { // if wheat in front and carrying and no wood here then
putWheat(); // put down and about turn
turn(4);
}
}
else if (cheeseAt(facing)) {
if (!carryingSomething) // if cheese in front and not carrying then pick up
getCheese(facing);
else if (!cheeseAt() && carryingCheese) { // if cheese in front and carrying and no wood here then
putCheese(); // put down and about turn
turn(4);
}
}
paint(); // paint termite
}
private int randomFacing() {
return containingWorld.random(8);
}
private boolean timeForTurn() {
return containingWorld.random(10) == 0;
}
private void randomTurn() {
turn(containingWorld.random(3) - 1);
}
private void turn(int delta) {
facing = World.mod(facing + delta, 8);
}
private void move(int direction) {
x = World.mod(x + facingXDelta[World.mod(direction, 8)], containingWorld.width());
y = World.mod(y + facingYDelta[World.mod(direction, 8)], containingWorld.height());
}
private boolean forbiddenAt(int x, int y) {
return containingWorld.forbiddenAt(x, y);
}
private boolean woodAt(int x, int y) {
return containingWorld.woodAt(x, y);
}
private boolean wheatAt(int x, int y) {
return containingWorld.wheatAt(x, y);
}
private boolean cheeseAt(int x, int y) {
return containingWorld.cheeseAt(x, y);
}
private boolean woodAt() {
return woodAt(x, y);
}
private boolean wheatAt() {
return wheatAt(x, y);
}
private boolean cheeseAt() {
return cheeseAt(x, y);
}
private boolean forbiddenAt(int direction) {
return forbiddenAt(x + facingXDelta[World.mod(direction, 8)], y + facingYDelta[World.mod(direction, 8)]);
}
private boolean woodAt(int direction) {
return woodAt(x + facingXDelta[World.mod(direction, 8)], y + facingYDelta[World.mod(direction, 8)]);
}
private boolean wheatAt(int direction) {
return wheatAt(x + facingXDelta[World.mod(direction, 8)], y + facingYDelta[World.mod(direction, 8)]);
}
private boolean cheeseAt(int direction) {
return cheeseAt(x + facingXDelta[World.mod(direction, 8)], y + facingYDelta[World.mod(direction, 8)]);
}
private void getWood(int x, int y) {
containingWorld.getWood(x, y);
carryingSomething = true;
carryingWood = true;
carryingWheat = false;
carryingCheese = false;
}
private void getWheat(int x, int y) {
containingWorld.getWheat(x, y);
carryingSomething = true;
carryingWood = false;
carryingWheat = true;
carryingCheese = false;
}
private void getCheese(int x, int y) {
containingWorld.getCheese(x, y);
carryingSomething = true;
carryingWood = false;
carryingWheat = false;
carryingCheese = true;
}
private void getWood() {
getWood(x, y);
}
private void getWheat() {
getWheat(x, y);
}
private void getCheese() {
getCheese(x, y);
}
private void getWood(int direction) {
getWood(x + facingXDelta[World.mod(direction, 8)], y + facingYDelta[World.mod(direction, 8)]);
}
private void getWheat(int direction) {
getWheat(x + facingXDelta[World.mod(direction, 8)], y + facingYDelta[World.mod(direction, 8)]);
}
private void getCheese(int direction) {
getCheese(x + facingXDelta[World.mod(direction, 8)], y + facingYDelta[World.mod(direction, 8)]);
}
private void putWood(int x, int y) {
containingWorld.putWood(x, y);
carryingSomething = false;
carryingWood = false;
}
private void putWheat(int x, int y) {
containingWorld.putWheat(x, y);
carryingSomething = false;
carryingWheat = false;
}
private void putCheese(int x, int y) {
containingWorld.putCheese(x, y);
carryingSomething = false;
carryingCheese = false;
}
private void putWood() {
putWood(x, y);
}
private void putWheat() {
putWheat(x, y);
}
private void putCheese() {
putCheese(x, y);
}
private void putWood(int direction) {
putWood(x + facingXDelta[World.mod(direction, 8)], y + facingYDelta[World.mod(direction, 8)]);
}
private void putWheat(int direction) {
putWheat(x + facingXDelta[World.mod(direction, 8)], y + facingYDelta[World.mod(direction, 8)]);
}
private void putCheese(int direction) {
putCheese(x + facingXDelta[World.mod(direction, 8)], y + facingYDelta[World.mod(direction, 8)]);
}
}
Retour à la page de téléchargement