Search in sources :

Example 6 with AiCore

use of spacesettlers.objects.AiCore in project spacesettlers by amymcgovern.

the class Toroidal2DPhysics method advanceTime.

/**
 * Move all moveable objects and handle power ups.
 */
public void advanceTime(int currentTimeStep, Map<UUID, SpaceSettlersPowerupEnum> powerups) {
    this.currentTimeStep = currentTimeStep;
    // heal any base injuries
    for (Base base : bases) {
        base.updateEnergy(base.getHealingIncrement());
    }
    // detect collisions across all objects
    detectCollisions();
    // get the power ups and create any objects (weapons) as necessary
    for (UUID key : powerups.keySet()) {
        AbstractObject swobject = getObjectById(key);
        // if the object is not alive or it is not actionable, then ignore this
        if (!swobject.isAlive() || (!(swobject instanceof AbstractActionableObject))) {
            continue;
        }
        // otherwise, handle the power up
        handlePowerup((AbstractActionableObject) swobject, powerups.get(key));
    }
    // now move all objects that are moveable (which may include weapons)
    for (AbstractObject object : allObjects) {
        // skip non-moveable objects or dead object
        if (!object.isMoveable() || !object.isAlive()) {
            continue;
        }
        Position currentPosition = object.getPosition();
        // is it a ship that can be controlled?
        if (object.isControllable()) {
            Ship ship = (Ship) object;
            AbstractAction action = ship.getCurrentAction();
            // handle a null action
            if (action == null) {
                action = new DoNothingAction();
            }
            // need to clone the ship and space because otherwise the ship can affect
            // itself inside AbstractAction
            Movement actionMovement = action.getMovement(this.deepClone(), ship.deepClone());
            Position newPosition = applyMovement(currentPosition, actionMovement, timeStep);
            if (newPosition.isValid()) {
                ship.setPosition(newPosition);
            } else {
                ship.setPosition(currentPosition);
            }
            // spend ship energy proportional to its acceleration (old formula used velocity) and mass (new for space settlers
            // since resources cost mass)
            // double penalty = ENERGY_PENALTY * -Math.abs(ship.getPosition().getTotalTranslationalVelocity());
            double angularAccel = Math.abs(actionMovement.getAngularAccleration());
            double angularInertia = (3.0 * ship.getMass() * ship.getRadius() * angularAccel) / 2.0;
            double linearAccel = actionMovement.getTranslationalAcceleration().getMagnitude();
            double linearInertia = ship.getMass() * linearAccel;
            int penalty = (int) Math.floor(ENERGY_PENALTY * (angularInertia + linearInertia));
            ship.updateEnergy(-penalty);
            // this isn't the most general fix but it will work for now (also has to be done for bases)
            if (ship.isShielded()) {
                ship.updateEnergy(-PowerupToggleShield.SHIELD_STEP_COST);
            }
        // if (!ship.isAlive()) {
        // System.out.println("Ship " + ship.getTeamName() + ship.getId() + " is dead");
        // }
        } else {
            // move all other types of objects
            Position newPosition = moveOneTimestep(currentPosition);
            object.setPosition(newPosition);
        }
        // if any ships or bases are frozen, decrement their frozen count
        if (object instanceof AbstractActionableObject && !object.isControllable()) {
            AbstractActionableObject actionable = (AbstractActionableObject) object;
            actionable.decrementFreezeCount();
        }
    }
    // go through and see if any bases have died
    Set<Base> basesClone = new LinkedHashSet<Base>(bases);
    for (Base base : basesClone) {
        // this isn't the most general fix but it will work for now (also has to be done for bases)
        if (base.isShielded()) {
            base.updateEnergy(-PowerupToggleShield.SHIELD_STEP_COST);
        }
        if (!base.isAlive()) {
            base.setAlive(false);
            removeObject(base);
            base.getTeam().removeBase(base);
        }
    }
    // from when it was called inside updateEnergy
    for (Ship ship : ships) {
        if (ship.getEnergy() <= 0 && ship.isAlive() == true) {
            // drop any resources that the ship was carrying
            ResourcePile resources = ship.getResources();
            // Spawn a new AiCore with the same velocity magnitude and direction as its parent ship.
            // handle dropping the core if the ship died
            Position corePosition = ship.getPosition();
            corePosition.setTranslationalVelocity(ship.getPosition().getTranslationalVelocity());
            corePosition.setAngularVelocity(ship.getPosition().getAngularVelocity());
            AiCore shipCore = new AiCore(corePosition, ship.getTeamName(), ship.getTeamColor());
            this.addObject(shipCore);
            if (resources.getTotal() > 0) {
            // Position newPosition = ship.getPosition();
            // newPosition.setTranslationalVelocity(new Vector2D(0,0));
            // newPosition.setAngularVelocity(0.0);
            // Asteroid newAsteroid = new Asteroid(newPosition, true, ship.getRadius(), true, resources);
            // this.addObject(newAsteroid);
            // distributeResourcesToNearbyAsteroids(ship.getPosition(), resources);
            // System.out.println("Adding a new asteroid with resources " + newAsteroid.getResources().getTotal() +
            // " due to death, total is " + asteroids.size());
            // System.out.println("Ship died and " + resources.getTotal() + " has been added to an asteroid");
            }
            // set the ship to dead last (so we can grab its resources first)
            ship.setAlive(false);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ResourcePile(spacesettlers.objects.resources.ResourcePile) AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) Movement(spacesettlers.utilities.Movement) Position(spacesettlers.utilities.Position) Base(spacesettlers.objects.Base) AiCore(spacesettlers.objects.AiCore) AbstractObject(spacesettlers.objects.AbstractObject) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction)

Example 7 with AiCore

use of spacesettlers.objects.AiCore in project spacesettlers by amymcgovern.

the class CoreCollectorTeamClient method pickNearestFreeCore.

/**
 * Find the nearest free core to this ship
 * @param space
 * @param ship
 * @return
 */
private AiCore pickNearestFreeCore(Toroidal2DPhysics space, Ship ship) {
    Set<AiCore> cores = space.getCores();
    AiCore closestCore = null;
    double bestDistance = Double.POSITIVE_INFINITY;
    for (AiCore core : cores) {
        if (coreToShipMap.containsKey(core)) {
            continue;
        }
        double dist = space.findShortestDistance(ship.getPosition(), core.getPosition());
        if (dist < bestDistance) {
            bestDistance = dist;
            closestCore = core;
        }
    }
    return closestCore;
}
Also used : AiCore(spacesettlers.objects.AiCore)

Example 8 with AiCore

use of spacesettlers.objects.AiCore in project spacesettlers by amymcgovern.

the class AggressiveHeuristicAsteroidCollectorTeamClient method getAsteroidCollectorAction.

/**
 * Gets the action for the asteroid collecting ship
 * @param space
 * @param ship
 * @return
 */
private AbstractAction getAsteroidCollectorAction(Toroidal2DPhysics space, Ship ship) {
    AbstractAction current = ship.getCurrentAction();
    Position currentPosition = ship.getPosition();
    // aim for a beacon if there isn't enough energy
    if (ship.getEnergy() < 2000) {
        Beacon beacon = pickNearestBeacon(space, ship);
        AbstractAction newAction = null;
        // if there is no beacon, then just skip a turn
        if (beacon == null) {
            newAction = new DoNothingAction();
        } else {
            newAction = new MoveToObjectAction(space, currentPosition, beacon);
        }
        aimingForBase.put(ship.getId(), false);
        goingForCore.put(ship.getId(), false);
        return newAction;
    }
    // if the ship has enough resourcesAvailable, take it back to base
    if (ship.getResources().getTotal() > 500 || ship.getNumCores() > 0) {
        Base base = findNearestBase(space, ship);
        AbstractAction newAction = new MoveToObjectAction(space, currentPosition, base);
        aimingForBase.put(ship.getId(), true);
        goingForCore.put(ship.getId(), false);
        return newAction;
    }
    // did we bounce off the base?
    if (ship.getResources().getTotal() == 0 && ship.getEnergy() > 2000 && aimingForBase.containsKey(ship.getId()) && aimingForBase.get(ship.getId())) {
        current = null;
        aimingForBase.put(ship.getId(), false);
        goingForCore.put(ship.getId(), false);
    }
    // if there is a nearby core, go get it
    AiCore nearbyCore = pickNearestCore(space, ship, 100);
    if (nearbyCore != null) {
        Position newGoal = nearbyCore.getPosition();
        AbstractAction newAction = new MoveToObjectAction(space, currentPosition, nearbyCore);
        aimingForBase.put(ship.getId(), false);
        goingForCore.put(ship.getId(), true);
        return newAction;
    }
    // otherwise aim for the asteroid
    if (current == null || current.isMovementFinished(space)) {
        aimingForBase.put(ship.getId(), false);
        goingForCore.put(ship.getId(), false);
        Asteroid asteroid = pickHighestValueNearestFreeAsteroid(space, ship);
        AbstractAction newAction = null;
        if (asteroid == null) {
            // there is no asteroid available so collect a beacon
            Beacon beacon = pickNearestBeacon(space, ship);
            // if there is no beacon, then just skip a turn
            if (beacon == null) {
                newAction = new DoNothingAction();
            } else {
                newAction = new MoveToObjectAction(space, currentPosition, beacon);
            }
        } else {
            asteroidToShipMap.put(asteroid.getId(), ship);
            newAction = new MoveToObjectAction(space, currentPosition, asteroid, asteroid.getPosition().getTranslationalVelocity());
        }
        return newAction;
    } else {
        return ship.getCurrentAction();
    }
}
Also used : Asteroid(spacesettlers.objects.Asteroid) Position(spacesettlers.utilities.Position) AiCore(spacesettlers.objects.AiCore) Beacon(spacesettlers.objects.Beacon) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction) Base(spacesettlers.objects.Base) MoveToObjectAction(spacesettlers.actions.MoveToObjectAction)

Example 9 with AiCore

use of spacesettlers.objects.AiCore in project spacesettlers by amymcgovern.

the class AggressiveHeuristicAsteroidCollectorTeamClient method getWeaponShipAction.

/**
 * Gets the action for the weapons based ship
 * @param space
 * @param ship
 * @return
 */
private AbstractAction getWeaponShipAction(Toroidal2DPhysics space, Ship ship) {
    AbstractAction current = ship.getCurrentAction();
    Position currentPosition = ship.getPosition();
    // aim for a beacon if there isn't enough energy
    if (ship.getEnergy() < 2000) {
        Beacon beacon = pickNearestBeacon(space, ship);
        AbstractAction newAction = null;
        // if there is no beacon, then just skip a turn
        if (beacon == null) {
            newAction = new DoNothingAction();
        } else {
            newAction = new MoveToObjectAction(space, currentPosition, beacon);
        }
        aimingForBase.put(ship.getId(), false);
        goingForCore.put(ship.getId(), false);
        return newAction;
    }
    // if the ship has enough resourcesAvailable, take it back to base
    if (ship.getResources().getTotal() > 500 || ship.getNumCores() > 0) {
        Base base = findNearestBase(space, ship);
        AbstractAction newAction = new MoveToObjectAction(space, currentPosition, base);
        aimingForBase.put(ship.getId(), true);
        goingForCore.put(ship.getId(), false);
        return newAction;
    }
    // did we bounce off the base?
    if (ship.getResources().getTotal() == 0 && ship.getEnergy() > 2000 && aimingForBase.containsKey(ship.getId()) && aimingForBase.get(ship.getId())) {
        current = null;
        goingForCore.put(ship.getId(), false);
        aimingForBase.put(ship.getId(), false);
    }
    // if there is a nearby core, go get it
    AiCore nearbyCore = pickNearestCore(space, ship, 100);
    if (nearbyCore != null) {
        Position newGoal = nearbyCore.getPosition();
        AbstractAction newAction = new MoveToObjectAction(space, currentPosition, nearbyCore);
        goingForCore.put(ship.getId(), true);
        aimingForBase.put(ship.getId(), false);
        return newAction;
    }
    // otherwise aim for the nearest enemy ship
    if (current == null || current.isMovementFinished(space)) {
        aimingForBase.put(ship.getId(), false);
        goingForCore.put(ship.getId(), false);
        Ship enemy = pickNearestEnemyShip(space, ship);
        AbstractAction newAction = null;
        if (enemy == null) {
            // there is no enemy available so collect a beacon
            Beacon beacon = pickNearestBeacon(space, ship);
            // if there is no beacon, then just skip a turn
            if (beacon == null) {
                newAction = new DoNothingAction();
            } else {
                newAction = new MoveToObjectAction(space, currentPosition, beacon);
            }
        } else {
            newAction = new MoveToObjectAction(space, currentPosition, enemy, enemy.getPosition().getTranslationalVelocity());
        }
        return newAction;
    } else {
        return ship.getCurrentAction();
    }
}
Also used : Position(spacesettlers.utilities.Position) AiCore(spacesettlers.objects.AiCore) Beacon(spacesettlers.objects.Beacon) Ship(spacesettlers.objects.Ship) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction) Base(spacesettlers.objects.Base) MoveToObjectAction(spacesettlers.actions.MoveToObjectAction)

Example 10 with AiCore

use of spacesettlers.objects.AiCore in project spacesettlers by amymcgovern.

the class CollisionHandler method missileCollision.

/**
 * Collide with a missile
 * @param object1
 * @param object2
 */
private void missileCollision(Missile missile, AbstractObject object2) {
    // get the ship that fired this
    Ship firingShip = missile.getFiringShip();
    firingShip.decrementWeaponCount();
    // did it hit a ship?
    if (object2.getClass() == Ship.class) {
        Ship ship = (Ship) object2;
        // only take damageInflicted if not shielded
        if (!ship.isShielded()) {
            double initialEnergy = ship.getEnergy();
            ship.updateEnergy(missile.getDamage());
            if (ship.getEnergy() <= 0) {
                // if you killed the ship, only count the final amount of damage needed to kill it
                firingShip.incrementDamageInflicted((int) initialEnergy);
                ship.incrementDamageReceived(-(int) initialEnergy);
            } else {
                // otherwise a missile is a fixed amount of damage
                firingShip.incrementDamageInflicted(-missile.getDamage());
                ship.incrementDamageReceived(missile.getDamage());
            }
            // it hit a ship
            firingShip.incrementHitsInflicted();
        }
        // if the bullet killed the ship, credit the ship that hit it
        if (ship.getEnergy() <= 0) {
            // System.out.println("ship " + firingShip.getTeamName() + " stealing resourcesAvailable " + shipMoney + " from " + ship.getTeamName() + ship.getId());
            // it killed a ship
            firingShip.incrementKillsInflicted();
            ship.incrementKillsReceived();
        }
    }
    // did it hit a base?
    if (object2.getClass() == Base.class) {
        Base base = (Base) object2;
        // only take damageInflicted if not shielded
        if (!base.isShielded()) {
            double initialEnergy = base.getEnergy();
            base.updateEnergy(missile.getDamage());
            if (base.getEnergy() <= 0) {
                // if the base is dead, you can only count the energy it had prior to being dead
                firingShip.incrementDamageInflicted((int) initialEnergy);
                base.incrementDamageReceived(-(int) initialEnergy);
            // System.out.println("Firing at a dead base - should give only " + (int) -initialEnergy + " in damage");
            } else {
                // otherwise the missles count constant
                firingShip.incrementDamageInflicted(-missile.getDamage());
                base.incrementDamageReceived(missile.getDamage());
            }
            // it hit a base
            firingShip.incrementHitsInflicted();
        }
    }
    // Handle a bullet hitting a bullet
    if (object2.getClass() == Missile.class) {
        object2.setAlive(false);
        Ship otherFiringShip = ((Missile) object2).getFiringShip();
        otherFiringShip.decrementWeaponCount();
    }
    // Did the missile hit an AiCore? If so, damage the AiCore.
    if (object2.getClass() == AiCore.class) {
        AiCore core = (AiCore) object2;
        core.updateEnergy(-missile.getDamage());
    }
    // make the missile die
    missile.setAlive(false);
}
Also used : Missile(spacesettlers.objects.weapons.Missile) AiCore(spacesettlers.objects.AiCore) Ship(spacesettlers.objects.Ship) Base(spacesettlers.objects.Base)

Aggregations

AiCore (spacesettlers.objects.AiCore)11 Base (spacesettlers.objects.Base)7 Ship (spacesettlers.objects.Ship)6 AbstractAction (spacesettlers.actions.AbstractAction)5 DoNothingAction (spacesettlers.actions.DoNothingAction)5 Position (spacesettlers.utilities.Position)5 MoveToObjectAction (spacesettlers.actions.MoveToObjectAction)4 Beacon (spacesettlers.objects.Beacon)3 UUID (java.util.UUID)2 AbstractObject (spacesettlers.objects.AbstractObject)2 Asteroid (spacesettlers.objects.Asteroid)2 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 AbstractActionableObject (spacesettlers.objects.AbstractActionableObject)1 ResourcePile (spacesettlers.objects.resources.ResourcePile)1 EMP (spacesettlers.objects.weapons.EMP)1 Missile (spacesettlers.objects.weapons.Missile)1 Movement (spacesettlers.utilities.Movement)1