/** * Executes the Task. If the Task contains sub tasks, it will call exeute() on those as well. If * the Task is unsucessful (which sometimes is important to see) the method returns false, else it * will return true. * * @return a boolean stating if the Task was sucessful. */ public boolean execute() throws GeneralTaskException { // Is never done, therefore no check for exception // Creates the move task and executes it /* TODO There is a problem here for low FPS. This code beneath is corrected so that the program will not hang up, but it also makes it possible for the turtle to, quite often, walk outside the radius and then walk back the next cycle. When FPS are very low the turtle would go very far from the center object and then back again, like a jojo. A possible other solution would be to check if the movement would be outside the radius, and then in some way shorten or replace the turtle so that it stays at the maximum radius, alternatively even bounces on the edge of the circle that is defined by the radius. A low FPS would still make strange results, though. Another problem is that if the could should be rewritten to test the the position before setting it, it would force the relative speed to to be calculated with the old speed twice, once here to check the position and once in the MoveTask. */ Vector vector = velocity.getVector(); Point point = position.getPoint(); float direction = vector.getDirection(); // Checks if the individual has wobbled outside the radius if (centerObject.getPoint().distanceTo(point) > radius) { // Corrects the angle to point directly to the centerObject direction = point.angleTo(centerObject.getPoint()); } vector = new Vector(randomizeDirection(direction), vector.getMagnitude()); MoveTask moveTask = new MoveTask(vector, relativeSpeed); moveTask.setPrivateInformation(position, velocity, characteristics, resources); moveTask.execute(); return true; }
/** * Sets the safety according to the given Point. The safety is calculated through the distance * from the light pillar, with a constant (getSacrificedUnitsDistanceRatio). * * @param point the Point describing the position of the owner of the characteristics. */ void setSafety(Point point) { LightPillarNode lightPillar = World.getWorld().getLightPillar(); float distance = point.distanceTo(lightPillar.getPoint()); distance = distance - lightPillar.getInfluenceRadius(); if (distance > 0) { // Decreases the safety (which is 1) with the percentual value // that is defined by safetyDecreaseFactor. The decrease is made // distance times, so that safety = safetyDecrease ^ distance. safety = (float) Math.pow(World.getEnvironment().getSafetyDecreaseFactor(), distance); } else { safety = 1; } }