/*
* 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.Ship;
import com.mlarktar.spacewar.sprites.ShipAI;
import com.mlarktar.spacewar.sprites.SpaceItem;
/**
* @author malar
*
* This rule decides when to execute a hyper jump
*/
public class HyperspaceOut extends AIRule {
static final int HYPERTHRESHOLD =
Integer.parseInt(
Parameters.getParameters().getProperty(
"shipai.hyperthreshold",
"50"));
/**
* @param ship
*/
public HyperspaceOut(ShipAI ship) {
super(ship);
setPriority(10);
setFunction(AIRule.SPECIAL);
}
/* (non-Javadoc)
* @see AIRule#evaluate()
*/
public void evaluate() {
Rectangle myPos = ship.getBounds();
Collection items =
ship.getParent().getSpaceItems(
new Rectangle(
(int) (myPos.getCenterX() - HYPERTHRESHOLD),
(int) (myPos.getCenterY() - HYPERTHRESHOLD),
HYPERTHRESHOLD * 2,
HYPERTHRESHOLD * 2));
// removes self
items.remove(ship);
// removes any object that does less than 8 units of hitpoints
// so it doesnt jump for every misile that it is fired
Iterator i = items.iterator();
while (i.hasNext()) {
SpaceItem si = (SpaceItem) i.next();
if (si.getHitPoints() < ShipAI.HYPERCOST) {
i.remove();
}
// doesnt hyper if it is about to collide with a weaker
// ship. :P
if (si instanceof Ship) {
Ship siship = (Ship) si;
if (siship.getShieldLevel() < ship.getShieldLevel()) {
i.remove();
}
}
}
if (!items.isEmpty()) {
ship.addRule(this);
}
}
/* (non-Javadoc)
* @see AIRule#execute()
*/
public void execute() {
ship.hyperSpace();
}
}