/* * CodeInvaders farming strategy * version 0.1a * Author: Allan M. Espinosa * Contact: yecartes_AT_gmail_DOT_com * Blog: http://allan.88-mph.net/blog/ * This strategy was initially designed as a demonstration for the * Ateneo Junior Summer Workshop in May 2007. The main goal is to * beam an initial amount of energy to the home planet. Remaining * energy is used to fuel the drones to gather the nearest energy * sources and then rebeam to the home planet. * * Scores from this strategy can reach up to 400+ points. Extremely * larger than the score if you deposited all the energy to the home * planet. * * The main blog post for this strategy can be found at * http://allan.88-mph.net/blog/entry/ajss-codeinvaders/ and * http:// * * Feel free to modify the code to suite your own taste provided this * block of comments is retained or I am cited for the code variations * of your strategy. * */ import com.ibm.invaders.*; /** * This is the class that you must implement to enable your ship to move within * the CodeInvaders environment. Adding code to these methods will give your ship * its personality and allow it to compete. */ public class MySpaceShip extends SpaceShip { /* (non-Javadoc) * @see com.ibm.invaders.SpaceShip#initialize() */ public void initialize() { // put implementation here } /* (non-Javadoc) * @see com.ibm.invaders.SpaceShip#move(int) */ public IObject getNearestEnergy(IControlledShip ship) { IObject[] source = World.getEnergySources(); int nearest = 0; double nearestDistance = Double.MAX_VALUE; for(int i = 0; i < source.length; i++) { double distance = ship.getDistanceTo(source[i]); if( distance < nearestDistance && ship == getNearestDrone(source[i]) ) { nearestDistance = distance; nearest = i; } } return source[nearest]; } public IControlledShip getNearestDrone(IObject ener) { int nearest = 0; double nearestDistance = Double.MAX_VALUE; IControlledShip[] drone = getMyDrones(); for(int i = 0; i < drone.length; i++) { double distance = ener.getDistanceTo(drone[i]); if(distance < nearestDistance && droneMode[i] == 0) { nearestDistance = distance; nearest = i; } } return drone[nearest]; } public void move(IControlledShip ship, IObject dest) { double headDiff = ship.getHeadingTo(dest) - ship.getHeading(); if(Math.abs(headDiff) < 15.) { double speed = Math.sqrt( ship.getXSpeed() * ship.getXSpeed() + ship.getYSpeed() * ship.getYSpeed() ); if(ship.getDistanceTo(dest) < 150. && speed > 50.) { ship.setThrust(0.); } else { ship.setThrust(ship.MAX_THRUST * 0.75); } } else { if(headDiff < 45. && ship.getDistanceTo(dest) > 125.) { ship.setThrust(50.); } else { ship.setThrust(0.); } ship.turnToHeading(ship.getHeadingTo(dest)); } } double getTravelTime(IControlledShip ship, IObject dest) { double accelaration = ship.MAX_THRUST * 0.5 * 0.0075; double distance = ship.getDistanceTo(dest); double time = Math.sqrt(2 * accelaration * distance); return time; } /* * getCost: * energy to consume if ship were to move to dest * an overestimate */ double getCost(IControlledShip ship, IObject dest) { double time = getTravelTime(ship, dest); double costDist = time * ship.MAX_THRUST * 0.5 * 0.002; double costTurn = 0.; double cost = costDist + costTurn; return cost; } int getTurnsLeft() { return World.MAX_TURNS - World.getCurrentTurn(); } /* * 0 : farm * 1 : beam */ static int[] droneMode = {0, 0, 0}; public void move(int lastMoveTime) { beamEnergy(); IControlledShip[] drone = getMyDrones(); for(int i = 0; i < drone.length; i++) { IObject energy = getNearestEnergy(drone[i]); double cost = getCost(drone[i], this) + getCost(drone[i], energy); if( cost < 200. && drone[i].getEnergy() < 200. && getTurnsLeft() > getTravelTime(drone[i], this) ) { droneMode[i] = 0; move(drone[i], energy); } else { if(drone[i].getDistanceTo(this) <= 200.) { drone[i].beamEnergy(this); droneMode[i] = 1; drone[i].setThrust(0.); drone[i].setRotationThrust(0.); } else { /* * check kung pwedeng makipasa na lang */ for(int j = 0; j < drone.length; j++) { if(i == j) { continue; } if( drone[i].getDistanceTo(drone[j]) <= 200. && drone[j].getDistanceTo(this) <= 200.) { drone[i].beamEnergy(drone[j]); drone[j].beamEnergy(this); droneMode[j] = 1; droneMode[i] = 1; drone[i].setThrust(0.); drone[j].setThrust(0.); drone[i].setRotationThrust(0.); drone[j].setRotationThrust(0.); } } droneMode[i] = 0; move(drone[i], this); } } } } }