Search in sources :

Example 1 with PurchaseTypes

use of spacesettlers.actions.PurchaseTypes in project spacesettlers by amymcgovern.

the class AggressiveFlagCollectorTeamClient method getTeamPurchases.

@Override
public /**
 * If there is enough resourcesAvailable, buy a base.  Place it by finding a ship that is sufficiently
 * far away from the existing bases
 */
Map<UUID, PurchaseTypes> getTeamPurchases(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects, ResourcePile resourcesAvailable, PurchaseCosts purchaseCosts) {
    HashMap<UUID, PurchaseTypes> purchases = new HashMap<UUID, PurchaseTypes>();
    double BASE_BUYING_DISTANCE = 200;
    boolean bought_base = false;
    int numBases, numShips;
    // count the number of ships for the base/ship buying algorithm
    numShips = 0;
    for (AbstractActionableObject actionableObject : actionableObjects) {
        if (actionableObject instanceof Ship) {
            numShips++;
        }
    }
    // try to balance
    if (purchaseCosts.canAfford(PurchaseTypes.BASE, resourcesAvailable)) {
        for (AbstractActionableObject actionableObject : actionableObjects) {
            if (actionableObject instanceof Ship) {
                Ship ship = (Ship) actionableObject;
                Set<Base> bases = space.getBases();
                // how far away is this ship to a base of my team?
                boolean buyBase = true;
                numBases = 0;
                for (Base base : bases) {
                    if (base.getTeamName().equalsIgnoreCase(getTeamName())) {
                        numBases++;
                        double distance = space.findShortestDistance(ship.getPosition(), base.getPosition());
                        if (distance < BASE_BUYING_DISTANCE) {
                            buyBase = false;
                        }
                    }
                }
                if (buyBase && numBases < numShips) {
                    purchases.put(ship.getId(), PurchaseTypes.BASE);
                    bought_base = true;
                    System.out.println("Aggressive Flag Collector is buying a base!");
                    break;
                }
            }
        }
    }
    // can I buy a ship?
    if (purchaseCosts.canAfford(PurchaseTypes.SHIP, resourcesAvailable) && bought_base == false) {
        for (AbstractActionableObject actionableObject : actionableObjects) {
            if (actionableObject instanceof Base) {
                Base base = (Base) actionableObject;
                purchases.put(base.getId(), PurchaseTypes.SHIP);
                System.out.println("Aggressive Flag Collector is buying a ship!");
                break;
            }
        }
    }
    return purchases;
}
Also used : AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) HashMap(java.util.HashMap) PurchaseTypes(spacesettlers.actions.PurchaseTypes) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) Base(spacesettlers.objects.Base)

Example 2 with PurchaseTypes

use of spacesettlers.actions.PurchaseTypes in project spacesettlers by amymcgovern.

the class AggressiveHeuristicAsteroidCollectorTeamClient method getTeamPurchases.

@Override
public /**
 * If there is enough resourcesAvailable, buy a base.  Place it by finding a ship that is sufficiently
 * far away from the existing bases
 */
Map<UUID, PurchaseTypes> getTeamPurchases(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects, ResourcePile resourcesAvailable, PurchaseCosts purchaseCosts) {
    HashMap<UUID, PurchaseTypes> purchases = new HashMap<UUID, PurchaseTypes>();
    double BASE_BUYING_DISTANCE = 200;
    boolean bought_base = false;
    if (purchaseCosts.canAfford(PurchaseTypes.BASE, resourcesAvailable)) {
        for (AbstractActionableObject actionableObject : actionableObjects) {
            if (actionableObject instanceof Ship) {
                Ship ship = (Ship) actionableObject;
                Set<Base> bases = space.getBases();
                // how far away is this ship to a base of my team?
                double maxDistance = Double.MIN_VALUE;
                for (Base base : bases) {
                    if (base.getTeamName().equalsIgnoreCase(getTeamName())) {
                        double distance = space.findShortestDistance(ship.getPosition(), base.getPosition());
                        if (distance > maxDistance) {
                            maxDistance = distance;
                        }
                    }
                }
                if (maxDistance > BASE_BUYING_DISTANCE) {
                    purchases.put(ship.getId(), PurchaseTypes.BASE);
                    bought_base = true;
                    // System.out.println("Buying a base!!");
                    break;
                }
            }
        }
    }
    // see if you can buy EMPs
    if (purchaseCosts.canAfford(PurchaseTypes.POWERUP_EMP_LAUNCHER, resourcesAvailable)) {
        for (AbstractActionableObject actionableObject : actionableObjects) {
            if (actionableObject instanceof Ship) {
                Ship ship = (Ship) actionableObject;
                if (!ship.getId().equals(asteroidCollectorID) && !ship.isValidPowerup(PurchaseTypes.POWERUP_EMP_LAUNCHER.getPowerupMap())) {
                    purchases.put(ship.getId(), PurchaseTypes.POWERUP_EMP_LAUNCHER);
                }
            }
        }
    }
    // can I buy a ship?
    if (purchaseCosts.canAfford(PurchaseTypes.SHIP, resourcesAvailable) && bought_base == false) {
        for (AbstractActionableObject actionableObject : actionableObjects) {
            if (actionableObject instanceof Base) {
                Base base = (Base) actionableObject;
                purchases.put(base.getId(), PurchaseTypes.SHIP);
                break;
            }
        }
    }
    return purchases;
}
Also used : AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) HashMap(java.util.HashMap) PurchaseTypes(spacesettlers.actions.PurchaseTypes) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) Base(spacesettlers.objects.Base)

Example 3 with PurchaseTypes

use of spacesettlers.actions.PurchaseTypes in project spacesettlers by amymcgovern.

the class Team method getTeamPurchases.

/**
 * Ask the team if they want to purchase anything this turn.  You can only
 * purchase one item per turn and only if you have enough resourcesAvailable.
 *
 * @param space
 * @return
 */
public Map<UUID, PurchaseTypes> getTeamPurchases(Toroidal2DPhysics space) {
    Map<UUID, PurchaseTypes> purchase = new HashMap<UUID, PurchaseTypes>();
    final Toroidal2DPhysics clonedSpace = space.deepClone();
    final Set<AbstractActionableObject> clonedActionableObjects = getTeamActionableObjectsClone(space);
    final PurchaseCosts clonedPurchaseCost = getPurchaseCostClone();
    final ResourcePile clonedResources = new ResourcePile(availableResources);
    // if the previous thread call hasn't finished, then just return default
    if (executor == null || executor.isTerminated()) {
        executor = Executors.newSingleThreadExecutor();
    } else {
        return purchase;
    }
    // System.out.println("exec " + executor.isTerminated());
    Future<Map<UUID, PurchaseTypes>> future = executor.submit(new Callable<Map<UUID, PurchaseTypes>>() {

        public Map<UUID, PurchaseTypes> call() throws Exception {
            return teamClient.getTeamPurchases(clonedSpace, clonedActionableObjects, clonedResources, clonedPurchaseCost);
        }
    });
    try {
        // start
        purchase = future.get(SpaceSettlersSimulator.TEAM_ACTION_TIMEOUT, TimeUnit.MILLISECONDS);
    // finished in time
    } catch (TimeoutException e) {
        // was terminated
        // return empty map, don't buy anything
        System.out.println(getTeamName() + " timed out in getTeamPurchases");
        purchase = new HashMap<UUID, PurchaseTypes>();
    } catch (InterruptedException e) {
        // we were interrupted (should not happen but lets be good programmers)
        // return empty map, don't buy anything
        purchase = new HashMap<UUID, PurchaseTypes>();
        e.printStackTrace();
    } catch (ExecutionException e) {
        // the executor threw and exception (should not happen but lets be good programmers)
        // return empty map, don't buy anything
        purchase = new HashMap<UUID, PurchaseTypes>();
        e.printStackTrace();
    } catch (RejectedExecutionException e) {
        System.err.println("exec" + executor.isTerminated());
        e.printStackTrace();
    } catch (Exception e) {
        purchase = new HashMap<UUID, PurchaseTypes>();
        e.printStackTrace();
    }
    executor.shutdownNow();
    return purchase;
}
Also used : ResourcePile(spacesettlers.objects.resources.ResourcePile) AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) HashMap(java.util.HashMap) PurchaseTypes(spacesettlers.actions.PurchaseTypes) TimeoutException(java.util.concurrent.TimeoutException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Toroidal2DPhysics(spacesettlers.simulator.Toroidal2DPhysics) PurchaseCosts(spacesettlers.actions.PurchaseCosts) UUID(java.util.UUID) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with PurchaseTypes

use of spacesettlers.actions.PurchaseTypes in project spacesettlers by amymcgovern.

the class SpaceSettlersSimulator method handlePurchases.

/**
 * Handle purchases for a team
 *
 * @param team
 * @param purchases
 */
private void handlePurchases(Team team, Map<UUID, PurchaseTypes> purchases) {
    // handle teams that don't purchase
    if (purchases == null) {
        return;
    }
    for (UUID key : purchases.keySet()) {
        PurchaseTypes purchase = purchases.get(key);
        // skip the purchase if there isn't enough resourcesAvailable
        if (!team.canAfford(purchase)) {
            continue;
        }
        // get the object where the item is to be purchased (on on whom it is to be purchased)
        AbstractActionableObject purchasingObject = (AbstractActionableObject) simulatedSpace.getObjectById(key);
        // can only make purchases for your team
        if (!purchasingObject.getTeamName().equalsIgnoreCase(team.getTeamName())) {
            continue;
        }
        switch(purchase) {
            case BASE:
                // only purchase if this is a ship (can't buy a base next to a base)
                if (purchasingObject instanceof Ship) {
                    Ship ship = (Ship) purchasingObject;
                    // set the base just away from the ship (to avoid a collision)
                    Position newPosition = simulatedSpace.getRandomFreeLocationInRegion(random, Base.BASE_RADIUS, (int) ship.getPosition().getX(), (int) ship.getPosition().getY(), (6 * (ship.getRadius() + Base.BASE_RADIUS)));
                    // make the new base and add it to the lists
                    Base base = new Base(newPosition, team.getTeamName(), team, false);
                    simulatedSpace.addObject(base);
                    team.addBase(base);
                    // charge the team for the purchase
                    team.decrementAvailableResources(team.getCurrentCost(purchase));
                    team.updateCost(purchase);
                }
                break;
            case SHIP:
                // can only buy if there are enough ships
                if (team.getShips().size() >= team.getMaxNumberShips())
                    break;
                // Ships can only be purchased near a base (which launches them)
                if (purchasingObject instanceof Base) {
                    Base base = (Base) purchasingObject;
                    // set the new ship just away from the base (to avoid a collision)
                    Position newPosition = simulatedSpace.getRandomFreeLocationInRegion(random, Ship.SHIP_RADIUS, (int) base.getPosition().getX(), (int) base.getPosition().getY(), (10 * (base.getRadius() + Ship.SHIP_RADIUS)));
                    // make the new ship and add it to the lists
                    Ship ship = new Ship(team.getTeamName(), team.getTeamColor(), newPosition);
                    simulatedSpace.addObject(ship);
                    team.addShip(ship);
                    // charge the team for the purchase
                    team.decrementAvailableResources(team.getCurrentCost(purchase));
                    team.updateCost(purchase);
                }
                break;
            case POWERUP_SHIELD:
                purchasingObject.addPowerup(SpaceSettlersPowerupEnum.TOGGLE_SHIELD);
                // charge the team for the purchase
                team.decrementAvailableResources(team.getCurrentCost(purchase));
                team.updateCost(purchase);
                System.out.println("Buying a shield");
                break;
            case POWERUP_EMP_LAUNCHER:
                // only purchase if this is a ship (can't buy a base next to a base)
                if (purchasingObject instanceof Ship) {
                    purchasingObject.addPowerup(SpaceSettlersPowerupEnum.FIRE_EMP);
                    // charge the team for the purchase
                    team.decrementAvailableResources(team.getCurrentCost(purchase));
                    team.updateCost(purchase);
                    System.out.println("Buying a emp launcher");
                }
                break;
            case POWERUP_DOUBLE_BASE_HEALING_SPEED:
                // this can only be purchased on bases
                if (purchasingObject instanceof Base) {
                    purchasingObject.addPowerup(SpaceSettlersPowerupEnum.DOUBLE_BASE_HEALING_SPEED);
                    // charge the team for the purchase
                    team.decrementAvailableResources(team.getCurrentCost(purchase));
                    team.updateCost(purchase);
                    System.out.println("Buying a healing doubler for a base");
                }
                break;
            case POWERUP_DOUBLE_MAX_ENERGY:
                purchasingObject.addPowerup(SpaceSettlersPowerupEnum.DOUBLE_MAX_ENERGY);
                // charge the team for the purchase
                team.decrementAvailableResources(team.getCurrentCost(purchase));
                team.updateCost(purchase);
                System.out.println("Buying a energy doubler");
                break;
            case POWERUP_DOUBLE_WEAPON_CAPACITY:
                purchasingObject.addPowerup(SpaceSettlersPowerupEnum.DOUBLE_WEAPON_CAPACITY);
                // charge the team for the purchase
                team.decrementAvailableResources(team.getCurrentCost(purchase));
                team.updateCost(purchase);
                System.out.println("Buying a weapons doubler");
                break;
            case NOTHING:
                break;
            default:
                break;
        }
    }
}
Also used : AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) Position(spacesettlers.utilities.Position) PurchaseTypes(spacesettlers.actions.PurchaseTypes) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) Base(spacesettlers.objects.Base)

Example 5 with PurchaseTypes

use of spacesettlers.actions.PurchaseTypes in project spacesettlers by amymcgovern.

the class PacifistFlagCollectorTeamClient method getTeamPurchases.

@Override
public /**
 * If there is enough resourcesAvailable, buy a base.  Place it by finding a ship that is sufficiently
 * far away from the existing bases
 */
Map<UUID, PurchaseTypes> getTeamPurchases(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects, ResourcePile resourcesAvailable, PurchaseCosts purchaseCosts) {
    HashMap<UUID, PurchaseTypes> purchases = new HashMap<UUID, PurchaseTypes>();
    double BASE_BUYING_DISTANCE = 200;
    boolean bought_base = false;
    int numBases, numShips;
    // count the number of ships for the base/ship buying algorithm
    numShips = 0;
    for (AbstractActionableObject actionableObject : actionableObjects) {
        if (actionableObject instanceof Ship) {
            numShips++;
        }
    }
    // try to balance
    if (purchaseCosts.canAfford(PurchaseTypes.BASE, resourcesAvailable)) {
        for (AbstractActionableObject actionableObject : actionableObjects) {
            if (actionableObject instanceof Ship) {
                Ship ship = (Ship) actionableObject;
                Set<Base> bases = space.getBases();
                // how far away is this ship to a base of my team?
                boolean buyBase = true;
                numBases = 0;
                for (Base base : bases) {
                    if (base.getTeamName().equalsIgnoreCase(getTeamName())) {
                        numBases++;
                        double distance = space.findShortestDistance(ship.getPosition(), base.getPosition());
                        if (distance < BASE_BUYING_DISTANCE) {
                            buyBase = false;
                        }
                    }
                }
                if (buyBase && numBases < numShips) {
                    purchases.put(ship.getId(), PurchaseTypes.BASE);
                    bought_base = true;
                    System.out.println("Pacifist Flag Collector is buying a base!");
                    break;
                }
            }
        }
    }
    // can I buy a ship?
    if (purchaseCosts.canAfford(PurchaseTypes.SHIP, resourcesAvailable) && bought_base == false) {
        for (AbstractActionableObject actionableObject : actionableObjects) {
            if (actionableObject instanceof Base) {
                Base base = (Base) actionableObject;
                purchases.put(base.getId(), PurchaseTypes.SHIP);
                System.out.println("Pacifist Flag Collector is buying a ship!");
                break;
            }
        }
    }
    return purchases;
}
Also used : AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) HashMap(java.util.HashMap) PurchaseTypes(spacesettlers.actions.PurchaseTypes) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) Base(spacesettlers.objects.Base)

Aggregations

UUID (java.util.UUID)8 PurchaseTypes (spacesettlers.actions.PurchaseTypes)8 AbstractActionableObject (spacesettlers.objects.AbstractActionableObject)8 HashMap (java.util.HashMap)7 Ship (spacesettlers.objects.Ship)7 Base (spacesettlers.objects.Base)6 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AbstractAction (spacesettlers.actions.AbstractAction)1 DoNothingAction (spacesettlers.actions.DoNothingAction)1 PurchaseCosts (spacesettlers.actions.PurchaseCosts)1 Team (spacesettlers.clients.Team)1 AbstractObject (spacesettlers.objects.AbstractObject)1 Asteroid (spacesettlers.objects.Asteroid)1 SpaceSettlersPowerupEnum (spacesettlers.objects.powerups.SpaceSettlersPowerupEnum)1 ResourcePile (spacesettlers.objects.resources.ResourcePile)1