/*
* This file is part of jSpaceWar.
*
* jSpaceWar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* jSpaceWar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with jSpaceWar. If not, see <http://www.gnu.org/licenses/>.
*/
package com.mlarktar.spacewar.rules;
import java.awt.Rectangle;
import java.util.Collection;
import java.util.Iterator;
import com.mlarktar.spacewar.Parameters;
import com.mlarktar.spacewar.sprites.Planet;
import com.mlarktar.spacewar.sprites.Ship;
import com.mlarktar.spacewar.sprites.ShipAI;
import com.mlarktar.spacewar.sprites.SpaceItem;
/**
* @author malar
*
* This rule helps the ship avoid the planet thus saving energy.
*/
public class AvoidPlanet extends AIRule {
public static final int DANGERZONE = Integer.parseInt(Parameters.getParameters().getProperty("shipia.planetzone", "200"));
private int turnShip;
private boolean doBoosters;
private double prevDistance;
private Planet planet;
private boolean planetSought;
/**
* @param ship
*/
public AvoidPlanet(ShipAI ship) {
super(ship);
setPriority(3);
setFunction(AIRule.MOVEMENT);
}
/* (non-Javadoc)
* @see AIRule#evaluate()
*/
public void evaluate() {
if (!planetSought) {
// tries only at first to find the planet
findPlanet();
planetSought = true;
}
analyzeAngle();
analyzeSpeed();
}
/**
*
*/
private void analyzeSpeed() {
if (planet != null) {
double currDistance = ship.getDistanceTo(planet);
if (currDistance <= prevDistance) {
doBoosters = true;
if (currDistance < DANGERZONE) {
ship.addRule(this);
}
}
prevDistance = currDistance;
}
}
/* (non-Javadoc)
* @see AIRule#execute()
*/
public void execute() {
if (turnShip != 0) {
ship.turnShip(turnShip);
} else {
if (doBoosters) {
ship.engageBoosters();
doBoosters = false;
}
}
}
private void findPlanet() {
Rectangle allItems = ship.getParent().getBounds();
Collection items = ship.getParent().getSpaceItems(allItems);
// removes self
items.remove(ship);
// sees if there are any INCOMMING misiles and fires a
// laser at them
Iterator i = items.iterator();
while (i.hasNext()) {
SpaceItem si = (SpaceItem) i.next();
if (si instanceof Planet) {
this.planet = (Planet) si;
break;
}
}
}
/* This function determines the angle the ship should have and acts accordingly. */
private void analyzeAngle() {
if (planet != null) {
double toAngle = ship.getAngleTo(planet);
if (toAngle >= Math.PI) {
toAngle -= Math.PI;
} else {
toAngle += Math.PI;
}
double fromAngle = ship.getAngle();
double forward, backward;
if (Math.abs(toAngle - fromAngle) > Math.PI / 4) {
if (toAngle > fromAngle) {
forward = toAngle - fromAngle;
backward = Math.PI * 2 - toAngle + fromAngle;
} else {
backward = fromAngle - toAngle;
forward = toAngle + Math.PI * 2 - fromAngle;
}
if (forward > backward) {
turnShip = Ship.CLOCKWISE;
} else {
turnShip = Ship.COUNTERCLOCKWISE;
}
} else {
turnShip = 0;
}
}
}
}