/** Randomly places food in the environment. */ public void loadNewFood() { for (int i = 0; i < getTypeCount(); ++i) { for (int j = 0; j < foodData[i].initial; ++j) { Location l; int tries = 0; do { l = simulation.getTopology().getRandomLocation(); } while (tries++ < 100 && env.hasAnythingAt(l)); if (tries < 100) { env.addFood(l, i); } } } }
private void dropFood(int type) { float foodDrop = foodData[type].dropRate; while (simulation.getRandom().nextFloat() < foodDrop) { --foodDrop; Location l; int j = 0; do { ++j; l = simulation.getTopology().getRandomLocation(); } while (j < DROP_ATTEMPTS_MAX && env.hasAnythingAt(l)); if (j < DROP_ATTEMPTS_MAX) { env.addFood(l, type); } } }
private void growFood() { // create a new ArrayEnvironment and a new food type array // loop through all positions AbioticMutator abiotic = env.getPlugin(AbioticMutator.class); for (int y = 0; y < simulation.getTopology().height; ++y) { for (int x = 0; x < simulation.getTopology().width; ++x) { Location currentPos = new Location(x, y); if (!env.hasAnythingAt(currentPos)) { // we should grow food here // the following code block tests all adjacent squares // to this one and counts how many have food // as well how many of each food type exist double foodCount = 0; int[] mostFood = new int[getTypeCount()]; for (Direction dir : simulation.getTopology().ALL_4_WAY) { Location checkPos = simulation.getTopology().getAdjacent(currentPos, dir); if (checkPos != null && env.hasFood(checkPos)) { foodCount++; mostFood[env.getFoodType(checkPos)]++; } } // and if we have found any adjacent food, theres a // chance we want to grow food here if (foodCount > 0) { int max = 0; int growingType; // find the food that exists in the largest quantity for (int i = 1; i < mostFood.length; ++i) if (mostFood[i] > mostFood[max]) max = i; // give the max food an extra chance to be chosen if (sameFoodProb >= simulation.getRandom().nextFloat()) { growingType = max; } else { growingType = simulation.getRandom().nextInt(getTypeCount()); } // finally, we grow food according to a certain // amount of random chance ComplexFoodParams thisType = foodData[growingType]; float growRate = thisType.growRate; for (int i = 0; i < thisType.abioticParams.factorParams.length; i++) { AbioticPreferenceParam factorParams = thisType.abioticParams.factorParams[i].preference; float factorValue = abiotic.getValue(i, currentPos); float discomfort = 1 + factorParams.score(factorValue); growRate *= discomfort; } if (foodCount * growRate > 100 * simulation.getRandom().nextFloat()) { env.addFood(currentPos, growingType); } } } } } }