Search in sources :

Example 21 with Asteroid

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

the class SpaceSettlersSimulator method advanceTime.

/**
 * Advance time one step
 */
void advanceTime() {
    // update the team info (to send into the space for use by other teams)
    updateTeamInfo();
    ExecutorService teamExecutor;
    if (debug) {
        teamExecutor = Executors.newSingleThreadExecutor();
    } else {
        teamExecutor = Executors.newCachedThreadPool();
    }
    Map<Team, Future<Map<UUID, AbstractAction>>> clientActionFutures = new HashMap<Team, Future<Map<UUID, AbstractAction>>>();
    // get the actions from each team
    for (Team team : teams) {
        clientActionFutures.put(team, teamExecutor.submit(new AdvanceTimeCallable(team)));
    }
    for (Team team : teams) {
        Map<UUID, AbstractAction> teamActions;
        try {
            teamActions = clientActionFutures.get(team).get();
        } catch (InterruptedException e) {
            // something went wrong...return empty map
            teamActions = new HashMap<UUID, AbstractAction>();
        } catch (ExecutionException e) {
            // something went wrong...return empty map
            teamActions = new HashMap<UUID, AbstractAction>();
        }
        // get the actions for each ship
        for (Ship ship : team.getShips()) {
            // if the client forgets to set an action, set it to DoNothing
            if (teamActions == null || !teamActions.containsKey(ship.getId())) {
                teamActions.put(ship.getId(), new DoNothingAction());
            }
            ship.setCurrentAction(teamActions.get(ship.getId()));
        }
    }
    teamExecutor.shutdown();
    // get the power ups being used on this turn
    Map<UUID, SpaceSettlersPowerupEnum> allPowerups = new HashMap<UUID, SpaceSettlersPowerupEnum>();
    for (Team team : teams) {
        Map<UUID, SpaceSettlersPowerupEnum> powerups = team.getTeamPowerups(simulatedSpace);
        if (powerups != null) {
            for (UUID key : powerups.keySet()) {
                // verify power ups belong to this team
                if (!team.isValidTeamID(key)) {
                    continue;
                }
                // get the object and ensure it can have a power up on it
                AbstractObject swObject = simulatedSpace.getObjectById(key);
                if (!(swObject instanceof AbstractActionableObject)) {
                    continue;
                }
                // verify that the object has the power up associated with it
                AbstractActionableObject actionableObject = (AbstractActionableObject) swObject;
                if (actionableObject.isValidPowerup(powerups.get(key))) {
                    allPowerups.put(key, powerups.get(key));
                }
            }
        }
    }
    // now update the physics on all objects
    simulatedSpace.advanceTime(this.getTimestep(), allPowerups);
    // and end any actions inside the team
    for (Team team : teams) {
        team.getTeamMovementEnd(simulatedSpace);
    }
    // handle purchases at the end of a turn (so ships will have movements next turn)
    for (Team team : teams) {
        // now get purchases for the team
        Map<UUID, PurchaseTypes> purchases = team.getTeamPurchases(simulatedSpace);
        handlePurchases(team, purchases);
    }
    // cleanup and remove dead weapons
    simulatedSpace.cleanupDeadWeapons();
    // cleanup and remove dead cores
    simulatedSpace.cleanupDeadCores();
    // respawn any objects that died (and that should respawn - this includes Flags)
    final double asteroidMaxVelocity = simConfig.getRandomAsteroids().getMaxInitialVelocity();
    simulatedSpace.respawnDeadObjects(random, asteroidMaxVelocity);
    // spawn new asteroids with a small probability (up to the maximum number allowed)
    int maxAsteroids = simConfig.getRandomAsteroids().getMaximumNumberAsteroids();
    int numAsteroids = simulatedSpace.getAsteroids().size();
    if (numAsteroids < maxAsteroids) {
        if (random.nextDouble() < ASTEROID_SPAWN_PROBABILITY) {
            // System.out.println("Spawning a new asteroid");
            Asteroid asteroid = createNewRandomAsteroid(simConfig.getRandomAsteroids());
            simulatedSpace.addObject(asteroid);
        }
    }
    updateScores();
// for (Team team : teams) {
// for (Ship ship : team.getShips()) {
// System.out.println("Ship " + ship.getTeamName() + ship.getId() + " has resourcesAvailable " + ship.getMoney());
// }
// }
}
Also used : AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) HashMap(java.util.HashMap) PurchaseTypes(spacesettlers.actions.PurchaseTypes) SpaceSettlersPowerupEnum(spacesettlers.objects.powerups.SpaceSettlersPowerupEnum) Asteroid(spacesettlers.objects.Asteroid) AbstractObject(spacesettlers.objects.AbstractObject) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Team(spacesettlers.clients.Team) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) ExecutionException(java.util.concurrent.ExecutionException) AbstractAction(spacesettlers.actions.AbstractAction) HashMap(java.util.HashMap) Map(java.util.Map) DoNothingAction(spacesettlers.actions.DoNothingAction)

Example 22 with Asteroid

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

the class Toroidal2DPhysics method respawnDeadObjects.

/**
 * Respawns any dead objects in new random locations.  Ships
 * have a delay before they can respawn.
 */
public void respawnDeadObjects(Random random, double asteroidMaxVelocity) {
    for (AbstractObject object : allObjects) {
        if (!object.isAlive() && object.canRespawn()) {
            Position newPosition = null;
            // flags should re-spawn at a randomly chosen starting location
            if (object instanceof Flag) {
                Flag flag = (Flag) object;
                newPosition = flag.getNewStartingPosition(random);
                // ensure their starting location is free (to handle the thought bug the class
                // introduced of putting a ship or a base where the flag should spawn)
                newPosition = getRandomFreeLocationInRegion(random, flag.getRadius() * 2, (int) newPosition.getX(), (int) newPosition.getY(), 75);
            } else {
                // note this is time 2 in order to ensure objects don't spawn touching (and just to get
                // them a bit farther apart
                newPosition = getRandomFreeLocation(random, object.getRadius() * 2);
            }
            object.setPosition(newPosition);
            object.setAlive(true);
            object.setDrawable(true);
            // reset the UUID if it is a asteroid or beacon
            if (object instanceof Asteroid || object instanceof Beacon) {
                object.resetId();
            }
            // make moveable asteroids move again when they respawn
            if (object.isMoveable() && !(object instanceof Flag)) {
                Vector2D randomMotion = Vector2D.getRandom(random, asteroidMaxVelocity);
                object.getPosition().setTranslationalVelocity(randomMotion);
            }
        }
    }
}
Also used : Asteroid(spacesettlers.objects.Asteroid) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) AbstractObject(spacesettlers.objects.AbstractObject) Beacon(spacesettlers.objects.Beacon) Flag(spacesettlers.objects.Flag)

Example 23 with Asteroid

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

the class Toroidal2DPhysics method distributeResourcesToNearbyAsteroids.

/**
 * Distribute the specified resources to nearby mineable asteroids (this happens when a ship dies)
 * Right now it drops it on the single nearest asteroid but that may change if this ends up making
 * massive asteroids
 *
 * @param position
 * @param resources
 */
private void distributeResourcesToNearbyAsteroids(Position position, ResourcePile resources) {
    double nearestDistance = Double.MAX_VALUE;
    Asteroid nearestAsteroid = null;
    // first find the nearest asteroid
    for (Asteroid asteroid : asteroids) {
        double dist = findShortestDistance(position, asteroid.getPosition());
        if (dist < nearestDistance) {
            nearestDistance = dist;
            nearestAsteroid = asteroid;
        }
    }
    // if it is mineable, just add the resources
    nearestAsteroid.addResources(resources);
    if (!nearestAsteroid.isMineable()) {
        // transform it to mineable
        nearestAsteroid.setMineable(true);
    }
}
Also used : Asteroid(spacesettlers.objects.Asteroid)

Example 24 with Asteroid

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

the class TestCollisionHandler method testElasticCollisionsShipToMoveableAsteroidY.

@Test
public void testElasticCollisionsShipToMoveableAsteroidY() {
    Ship ship1;
    Asteroid asteroid;
    Position ship1Pos = new Position(0, 0, Math.PI / 4);
    ship1Pos.setTranslationalVelocity(new Vector2D(0, 20));
    ship1 = new Ship("team1", Color.BLUE, ship1Pos);
    Position asteroid2Pos = new Position(0, 10, -Math.PI / 4);
    asteroid2Pos.setTranslationalVelocity(new Vector2D(0, -10));
    asteroid = new Asteroid(asteroid2Pos, false, 10, true, .33, .33, .34);
    ship1.setMass(asteroid.getMass());
    collisionHandler.collide(ship1, asteroid, space);
    assertEquals(ship1.getPosition().getTranslationalVelocityX(), 0, 0.01);
    assertEquals(ship1.getPosition().getTranslationalVelocityY(), -10.0, 0.01);
    assertEquals(asteroid.getPosition().getTranslationalVelocityX(), 0, 0.01);
    assertEquals(asteroid.getPosition().getTranslationalVelocityY(), 20, 0.01);
}
Also used : Asteroid(spacesettlers.objects.Asteroid) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) Ship(spacesettlers.objects.Ship) Test(org.junit.Test)

Example 25 with Asteroid

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

the class ObjectInfoPanel method updateData.

/**
 * Update the GUI for this step using the simulator data as needed
 * @param simulator
 */
public void updateData(SpaceSettlersSimulator simulator) {
    // do not update if there is no selected object
    if (this.selectedObject == null) {
        return;
    }
    String name = "";
    if (selectedObject.getClass() == Asteroid.class) {
        Asteroid asteroid = (Asteroid) selectedObject;
        if (asteroid.isMineable()) {
            name = "Mineable asteroid";
        } else {
            name = "Non-mineable asteroid";
        }
    } else if (selectedObject.getClass() == Base.class) {
        Base base = (Base) selectedObject;
        name = "Base for " + base.getTeamName();
    } else if (selectedObject.getClass() == Ship.class) {
        Ship ship = (Ship) selectedObject;
        name = "Ship for " + ship.getTeamName();
    } else if (selectedObject.getClass() == Beacon.class) {
        Beacon beacon = (Beacon) selectedObject;
        name = "Beacon";
    }
    objectName.setText(name);
    innerPanel.updateData(simulator);
    resourcesPanel.updateData(selectedObject);
}
Also used : Asteroid(spacesettlers.objects.Asteroid) Beacon(spacesettlers.objects.Beacon) Ship(spacesettlers.objects.Ship) Base(spacesettlers.objects.Base)

Aggregations

Asteroid (spacesettlers.objects.Asteroid)25 Position (spacesettlers.utilities.Position)14 Beacon (spacesettlers.objects.Beacon)9 Ship (spacesettlers.objects.Ship)9 Base (spacesettlers.objects.Base)8 Vector2D (spacesettlers.utilities.Vector2D)8 Test (org.junit.Test)6 AbstractAction (spacesettlers.actions.AbstractAction)6 DoNothingAction (spacesettlers.actions.DoNothingAction)6 MoveToObjectAction (spacesettlers.actions.MoveToObjectAction)5 Team (spacesettlers.clients.Team)2 AbstractObject (spacesettlers.objects.AbstractObject)2 AiCore (spacesettlers.objects.AiCore)2 Flag (spacesettlers.objects.Flag)2 ResourcePile (spacesettlers.objects.resources.ResourcePile)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 UUID (java.util.UUID)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1