// constructor public BoardModel() { for (int i = 0; i < Parameters.BOARD_SIZE; i++) { for (int j = 0; j < Parameters.BOARD_SIZE; j++) { boardLayout[i][j] = new GroundCell(); } } nest1 = new Nest(); // create nest boardLayout[nest1.getX()][nest1.getY()].hasNest = true; // add nest to board // create the food piles for (int i = 0; i < Parameters.NUM_OF_FOOD_PILES; i++) { int tempX = ((int) (Math.random() * (Parameters.BOARD_SIZE - 2))) + 1; // some randoms to place food piles. int tempY = ((int) (Math.random() * (Parameters.BOARD_SIZE - 2))) + 1; // check to see if location is empty of food and nest while (boardLayout[tempX][tempY].hasFood || boardLayout[tempX][tempY].hasNest) { tempX = ((int) (Math.random() * (Parameters.BOARD_SIZE - 2))) + 1; tempY = ((int) (Math.random() * (Parameters.BOARD_SIZE - 2))) + 1; } theFood[i] = new Food(tempX, tempY); boardLayout[tempX][tempY].hasFood = true; } }
// gets the new status of all the parts of the simulator and returns them in an integer array public GroundCell[][] updateMap() { moveAnts(); // move all the ants // check to see if there is enough food and space to spawn more ants while (nest1.getFoodStored() >= Parameters.FOOD_TO_SPAWN_NEW_ANT && nest1.checkForDeadAnts()) { nest1.createNewAnt(); // will spawn a new ant in a dead ant's place } this.resetBoard(); boardLayout[nest1.getX()][nest1.getY()].hasNest = true; // place the nest for (int i = 0; i < Parameters.NUM_OF_FOOD_PILES; ++i) // draw the food piles { if (theFood[i].isHasFoodLeft()) // check if food pile still has food in it { boardLayout[theFood[i].getX()][theFood[i].getY()].hasFood = true; } } for (int i = 0; i < Parameters.NUM_OF_ANTS; ++i) // draw the ants { if (nest1.theAnts[i].isAlive()) // check to see if ant is alive { if (nest1.theAnts[i].isHasFood()) // check if the ant is carrying food { // display ant carrying food boardLayout[nest1.theAnts[i].getX()][nest1.theAnts[i].getY()].hasAntWithFood = true; } else { boardLayout[nest1.theAnts[i].getX()][nest1.theAnts[i].getY()].hasAnt = true; // display an ant } } } return boardLayout; }
public void doGoal() { /*goals: 0: Does 1 damage to the nearest 2x2 square robots and sets their goal to 1. If no 2x2 square robots are around it does 10 (probably should be less) damage to green 2x2 square food. Upon destroying 10 things, it upgrades to the 6x6 red preditor. If no 2x2 square robots or 2x2 square food is around, it attacks the nearest nest with a damage of 1. If it destroys a nest it upgrades to a 6x6 red preditor. When destroyed it turns into 4x4 brown food.*/ boolean drawText = false; if (System.currentTimeMillis() - lastFeast > 8750) { numThingsEaten++; lastFeast = System.currentTimeMillis(); drawText = true; } if (numThingsEaten >= 10) { convertToRed6x6(); } else if (goal == 0) { Black2x2 closest = null; double closestMag = 0; Food closestFood = null; double closestFoodMag = 0; Nest closestNest = null; double closestNestMag = 0; Iterator<Animal> itAnimals = singleton.animals.iterator(); while (itAnimals.hasNext()) { Animal current = itAnimals.next(); if (!current.isDestroyed()) { if (current instanceof Black2x2) { if (closest == null) { closest = (Black2x2) current; closestMag = Math.pow(Math.pow(x - closest.getX(), 2) + Math.pow(y - closest.getY(), 2), .5); } else { double mag = Math.pow(Math.pow(x - current.getX(), 2) + Math.pow(y - current.getY(), 2), .5); if (mag < closestMag) { closest = (Black2x2) current; closestMag = mag; } } } } } Iterator<Food> itFood = singleton.food.iterator(); while (itFood.hasNext()) { Food current = itFood.next(); if (!current.isDestroyed() && current instanceof Food2x2) { if (closestFood == null) { closestFood = current; closestFoodMag = Math.pow( Math.pow(x - closestFood.getX(), 2) + Math.pow(y - closestFood.getY(), 2), .5); } else { double mag = Math.pow(Math.pow(x - current.getX(), 2) + Math.pow(y - current.getY(), 2), .5); if (mag < closestFoodMag) { closestFood = current; closestFoodMag = mag; } } } } Iterator<Nest> itNests = singleton.nests.iterator(); while (itNests.hasNext()) { Nest current = itNests.next(); if (!current.isDestroyed()) { if (closestNest == null) { closestNest = current; closestNestMag = Math.pow( Math.pow(x - closestNest.getX(), 2) + Math.pow(y - closestNest.getY(), 2), .5); } else { double mag = Math.pow(Math.pow(x - current.getX(), 2) + Math.pow(y - current.getY(), 2), .5); if (mag < closestNestMag) { closestNest = current; closestNestMag = mag; } } } } boolean moveToEnemy = false; boolean moveToFood = false; boolean dontMove = false; if (closest != null) { if (closestFood == null || closestMag < closestFoodMag) { if (!closest.isDestroyed()) { if (closestMag < 3) { damage(closest, 1); dontMove = true; if (closest.isDestroyed()) { numThingsEaten += 2; drawText = true; lastFeast = System.currentTimeMillis(); } } else if (closestMag < radius) { moveToEnemy = true; } } else { moveRandom(); } } else { if (!closestFood.isDestroyed()) { if (closestFoodMag < 3) { damage(closestFood, 1); if (closestFood.isDestroyed()) { numThingsEaten++; drawText = true; lastFeast = System.currentTimeMillis(); } dontMove = true; } else if (closestFoodMag < radius) { moveToFood = true; } } else { moveRandom(); } } } else { if (closestFood != null) { if (!closestFood.isDestroyed()) { if (closestFoodMag < 3) { damage(closestFood, 1); if (closestFood.isDestroyed()) { numThingsEaten++; drawText = true; lastFeast = System.currentTimeMillis(); } dontMove = true; } else if (closestFoodMag < radius) { moveToFood = true; } } } } if (moveToEnemy) { double deltaY = closest.getY() - y; double goalDir = Math.asin(deltaY / closestMag); if (closest.getX() <= x) { goalDir = Math.PI - goalDir; } dir = goalDir - .3 + .6 * Math.random(); moveStraight(); } else if (moveToFood) { double deltaY = closestFood.getY() - y; double goalDir = Math.asin(deltaY / closestFoodMag); if (closestFood.getX() <= x) { goalDir = Math.PI - goalDir; } dir = ((.7 + .6 * Math.random()) * goalDir + dir) / 2; if (x - 3 < closestFood.getX() && closestFood.getX() < x + 3) { dir = goalDir; } moveStraight(); } else if (!dontMove) { if (closestNest != null && closestNestMag < Main.nestSize / 2 + 3) { damage(closestNest, 1); if (closestNest.isDestroyed()) { convertToRed6x6(); } } else if (closestNest != null && closestNestMag < radius + Main.nestSize / 2) { double deltaY = closestNest.getY() - y; double goalDir = Math.asin(deltaY / closestNestMag); if (closestNest.getX() < x) { goalDir = Math.PI - goalDir; } dir = ((.7 + .6 * Math.random()) * goalDir + dir) / 2; if (x - 3 > closestNest.getX() && closestNest.getX() < x + 3) { dir = goalDir; } moveStraight(); } else { moveRandom(); } } } if (drawText) { GraphicLabel gl = null; if (numThingsEaten == 1) { gl = new GraphicLabel((int) x, (int) y, "1", "purple"); } else if (numThingsEaten == 2) { gl = new GraphicLabel((int) x, (int) y, "2", "magenta"); } else if (numThingsEaten == 3) { gl = new GraphicLabel((int) x, (int) y, "3", "pink"); } else if (numThingsEaten == 4) { gl = new GraphicLabel((int) x, (int) y, "4", "red"); } else if (numThingsEaten == 5) { gl = new GraphicLabel((int) x, (int) y, "5", "orange"); } else if (numThingsEaten == 6) { gl = new GraphicLabel((int) x, (int) y, "6", "yellow"); } else if (numThingsEaten == 7) { gl = new GraphicLabel((int) x, (int) y, "7", "cyan"); } else if (numThingsEaten == 8) { gl = new GraphicLabel((int) x, (int) y, "8", "green"); } else if (numThingsEaten == 9) { gl = new GraphicLabel((int) x, (int) y, "9", "blue"); } else if (numThingsEaten >= 10) { gl = new GraphicLabel((int) x, (int) y, "UPGRADE", "yellow"); } singleton.graphicLabels.add(gl); } }
// moves the ants and then checks where they are public void moveAnts() { // iterate through the ants for (int i = 0; i < Parameters.NUM_OF_ANTS; ++i) { // Check to see if the ant is carrying food. If it is, drop the pheromone if (nest1.theAnts[i].isHasFood() && boardLayout[nest1.theAnts[i].getX()][nest1.theAnts[i].getY()].isEmpty) { boardLayout[nest1.theAnts[i].getX()][nest1.theAnts[i].getY()].setPheromone( Parameters.PHEROMONE_STRENGTH + 1); } // Check if the cell has a pheromone on it. If it does, check the cells around it // and move in the direction of the food (weaker direction) if (boardLayout[nest1.theAnts[i].getX()][nest1.theAnts[i].getY()].getPheromone() > 0 && !nest1.theAnts[i].isHasFood()) { int antX = nest1.theAnts[i].getX(); int antY = nest1.theAnts[i].getY(); int pherStrength = boardLayout[antX][antY].getPheromone(); if (boardLayout[antX][antY + 1].getPheromone() < pherStrength && boardLayout[antX][antY + 1].getPheromone() > 0) { nest1.theAnts[i].move(antX, antY + 1); } else if (boardLayout[antX + 1][antY + 1].getPheromone() < pherStrength && boardLayout[antX + 1][antY + 1].getPheromone() > 0) { nest1.theAnts[i].move(antX + 1, antY + 1); } else if (boardLayout[antX + 1][antY].getPheromone() < pherStrength && boardLayout[antX + 1][antY].getPheromone() > 0) { nest1.theAnts[i].move(antX + 1, antY); } else if (boardLayout[antX + 1][antY - 1].getPheromone() < pherStrength && boardLayout[antX + 1][antY - 1].getPheromone() > 0) { nest1.theAnts[i].move(antX + 1, antY - 1); } else if (boardLayout[antX][antY - 1].getPheromone() < pherStrength && boardLayout[antX][antY - 1].getPheromone() > 0) { nest1.theAnts[i].move(antX, antY - 1); } else if (boardLayout[antX - 1][antY - 1].getPheromone() < pherStrength && boardLayout[antX - 1][antY - 1].getPheromone() > 0) { nest1.theAnts[i].move(antX - 1, antY - 1); } else if (boardLayout[antX - 1][antY].getPheromone() < pherStrength && boardLayout[antX - 1][antY].getPheromone() > 0) { nest1.theAnts[i].move(antX - 1, antY); } else if (boardLayout[antX - 1][antY + 1].getPheromone() < pherStrength && boardLayout[antX - 1][antY + 1].getPheromone() > 0) { nest1.theAnts[i].move(antX - 1, antY + 1); } else { // If no more surrounding cells have pheromones and the pheromone trail // doesn't lead to a food pile, move the ants to next space nest1.theAnts[i].move(); } } else { // move the ants to next space nest1.theAnts[i].move(); } // check if ant is at the nest if (nest1.theAnts[i].getX() == nest1.getX() && nest1.theAnts[i].getY() == nest1.getY()) { // check to see if the ant is carrying food if (nest1.theAnts[i].isHasFood()) { // drop off food at the nest nest1.theAnts[i].setHasFood(false); nest1.incrementFood(); } // Check if ant is hungry and if there is food at the nest if (nest1.theAnts[i].getRemainingLife() < Parameters.ANT_LIFESPAN / 2 && nest1.getFoodStored() > 1) { // restores the ants life remaining to full value nest1.theAnts[i].feedAnt(); nest1.decrementFood(); } } // check to see if an ant found food else if (boardLayout[nest1.theAnts[i].getX()][nest1.theAnts[i].getY()].hasFood) { // only pick up food if the ant isn't already carrying food if (!nest1.theAnts[i].isHasFood()) { nest1.theAnts[i].setHasFood(true); // set carrying food attribute to true on the ant int temp = getFoodPileIndex(nest1.theAnts[i]); // get index number of given food pile theFood[temp].decrementPile(); // decrement the food remaining } } } }