class Ant { int fieldX; int fieldY; int oldFieldX; int oldFieldY; float antSize; color colour; int dirX; int dirY; boolean carryingFood; ArrayList visitedFields; //wo war ich schon int remStep = 0; int maxRemStep = 24; //wieviele stellen merke ich mir Ant() { // setPositionRandom(); setPosition(theWorld.nestX, theWorld.nestY); antSize = stage.getCellSize() *0.8; // antSize = 5; colour = color(255, 255, 255, 255); dirX = round (random(-1,1)); dirY = round (random(-1,1)); carryingFood = false; visitedFields = new ArrayList(); } //METHODS ##################################################################### void antLogic() { Field tmpField = (Field) theWorld.getField(getPositionX(), getPositionY()); if (carryingFood) { colour=#FF0000; spreadPheromone(); moveToNest(); } else if(tmpField.getFoodSmell() > 0.1) { followFoodSmell(); } else { moveRandomly(); } // } } void spreadPheromone() { Field currField = (Field) theWorld.getField(getPositionX(), getPositionY()); currField.increaseFoodSmell(50); } void moveRandomly() { ArrayList possibleNeighbours = new ArrayList(); possibleNeighbours = theWorld.getValidNeighbours(getPositionX(), getPositionY(), 1); Field currField = (Field) possibleNeighbours.get(0); boolean fieldNotPossible = true; for (int i=0; i0) carryFood(); } void carryFood() { carryingFood=true; Field currField = (Field) theWorld.getField(getPositionX(), getPositionY()); currField.setFoodAmount(currField.getFoodAmount()-1); } void followFoodSmell() { boolean smellToFollow=false; Field currField; ArrayList foodInNeighbourhood = new ArrayList(); foodInNeighbourhood = theWorld.getFoodInNeighbourhood(getPositionX(), getPositionY()); if (foodInNeighbourhood.size()>0) { currField = (Field) foodInNeighbourhood.get(0); int x = currField.posX; int y = currField.posY; setPosition(x, y); } else { ArrayList smellingNeighbours = new ArrayList(); smellingNeighbours = theWorld.getBestSmellNeighbours(getPositionX(), getPositionY()); if (smellingNeighbours.size()>1)// && !carryingFood) { currField = (Field) smellingNeighbours.get(0); colour = color(#00FF00); int x = currField.posX; int y = currField.posY; setPosition(x, y); // smellToFollow = true; } else { currField = (Field) theWorld.getField(getPositionX(), getPositionY()); moveRandomly(); } } if (currField.getFoodAmount()>0) carryFood(); // return smellToFollow; } void moveToNest() { if (getPositionX() == theWorld.nestX && getPositionY() == theWorld.nestY) { carryingFood=false; theWorld.nestSize++; println(theWorld.nestSize); }else { ArrayList possibleNeighbours = new ArrayList(); possibleNeighbours = theWorld.getValidNeighbours(getPositionX(), getPositionY(), 1); // int index = (int) random(possibleNeighbours.size()); // println(index); Field currField; boolean smartMove; int k=0; do { smartMove = true; currField = (Field) possibleNeighbours.get(k); for (int i=0; i < visitedFields.size(); i++) { Field tempField = (Field) visitedFields.get(i); if (currField.getX() == tempField.getX() && currField.getY() == tempField.getY()) { // println("Hier war ich schon."); smartMove = false; break; //break the forLoop, the object has been found } } k++; } while(smartMove==false && k maxRemStep) remStep = 0; int x = currField.posX; int y = currField.posY; // x = constrain(getPositionX() +x, 1, theWorld.getWidth()-2); // y = constrain(getPositionY() +y, 1, theWorld.getHeight()-2); setPosition(x, y); } } void setPosition(int x, int y) { fieldX = x; fieldY = y; } int getPositionX() { return fieldX; } int getPositionY() { return fieldY; } void setPositionRandom() { do { fieldX = (int) random(1, theWorld.getWidth()-1); fieldY = (int) random(1, theWorld.getHeight()-1); } while (theWorld.fieldMem[fieldX][fieldY][1] != 0); //true means there's an obstacle } void drawAnt() { fill(colour); if (carryingFood) { fill(#FF0000); } pushMatrix(); translate ( -(theWorld.getWidth()*stage.getCellSize()/2.0) ,-(theWorld.getHeight()*stage.getCellSize()/2.0) ,0 ); translate ( (fieldX*stage.getCellSize())+(stage.getCellSize()/2.0), (fieldY*stage.getCellSize())+(stage.getCellSize()/2.0), antSize/1.9 ); noStroke(); box(antSize); popMatrix(); } }