private int calculateAgility(List<IShip> ships) { int agility = 0; for (IShip ship : ships) { agility += calculateAgility(ship); } return agility; } @VisibleForTesting
@Override public void run() { seafaringService.travelBetweenCities(vessel, destinationCity); } };
private int calculateStrength(List<IShip> ships) { int strength = 0; for (IShip ship : ships) { strength += calculateShipStrength(ship); } return strength; }
private void addEdges(Point2D from, Point2D to, boolean isCity, boolean initial) { if (isCity || isOnSea(to)) { if (!graph.containsNode(to)) { if (initial) { graph.addNodeInternal(to); } else { graph.addNode(to, true); } } heuristicProvider.getHeuristic(); double weight = calculateWeight(from, to); if (initial) { graph.addEdgeInternal(from, to, weight); } else { graph.addEdge(from, to, weight); } weight = calculateWeight(to, from); if (initial) { graph.addEdgeInternal(to, from, weight); } else { graph.addEdge(to, from, weight); } } }
/** * Travel between two locations, where neither is a city. * @param vessel travelling between two locations at sea * @param location destination location */ public void travelNotBetweenCities(INavigableVessel vessel, Point2D location) { List<Point2D> path = travelTo(vessel, location); Preconditions.checkNotNull(path); pathConverter.createPath(vessel, path, 1); VesselPositionUpdateData vesselPositionUpdateData = interpolators.get(vessel); vesselPositionUpdateData.setDestinationCity(false); vesselPositionUpdateData.setSourceCity(false); }
private void updateCapturedShipInternal(List<IShip> capturedDefendingShips, List<IShip> capturedAttackingShips, boolean attackShipTurn, IShip shipWithMaxSailors, IShip captureShip, SeaFightContext context) { final int numberOfSailors = captureShip.getNumberOfSailors(); int halfTheSailors = numberOfSailors /2; captureShip.setNumberOfSailors(numberOfSailors - halfTheSailors); IShipOwner previousOwner = shipWithMaxSailors.getOwner(); removeFromFleet(shipWithMaxSailors, previousOwner); shipWithMaxSailors.setNumberOfSailors(halfTheSailors); shipWithMaxSailors.setOwner(captureShip.getOwner()); if (context.getType() == ESeaFightType.BLOCKADE && attackShipTurn) { IBlockade blockade = ((BlockadeSeafightContext)context).getBlockade(); blockade.getShipsToAuction().add(captureShip); } else { addToFleet(shipWithMaxSailors, captureShip.getOwner()); } if (attackShipTurn) { // shipWithMaxSailors belongs to attacking fleet capturedAttackingShips.add(shipWithMaxSailors); } else { capturedDefendingShips.add(shipWithMaxSailors); } }
@VisibleForTesting void updateCapturedShip(List<IShip> capturedDefendingShips, List<IShip> capturedAttackingShips, boolean attackShipTurn, IShip shipWithMaxSailors, IShip captureShip, SeaFightContext context) { if (captureShip != null) { int calculateCaptureSailors = captureShip(shipWithMaxSailors, captureShip); if (calculateCaptureSailors < 0) { // the captureShip defeated shipWithMaxSailors updateCapturedShipInternal(capturedDefendingShips, capturedAttackingShips, attackShipTurn, shipWithMaxSailors, captureShip, context); } else { // the shipWithMaxSailors defeated captureShip updateCapturedShipInternal(capturedAttackingShips, capturedDefendingShips, attackShipTurn, captureShip, shipWithMaxSailors, context); } } }
/** * Handle the even when a new pirate becomes available. * @param pirateEvent new pirate emerged */ @Subscribe public void handleNewPirate(NewPirateEvent pirateEvent) { IShip ship = (IShip) pirateEvent.getPirate().getShip(); Point2D adjustedSource = aStarHeuristicProvider.findClosest(ship.getLocation()); ship.setLocation(adjustedSource); selectDestination(pirateEvent.getPirate()); }
/** * Handle the event for the finished repair. * @param event repair has finished */ @Subscribe public void handleRepairFinished(RepairFinishedEvent event) { if (handlePirateShip(event.getShip())) { INavigableVessel vessel = event.getShip(); Point2D destination = mapService.getRandomPointAtSeaNear(vessel.getLocation()); seafaringService.travelFromCity(vessel, destination); vessel.togglePirateFlag(); } }
public List<INavigableVessel> getShipsInSegment(Point2D location) { int[] index = calculateIndices(location); List<WeakReference<INavigableVessel>> references = segments[index[0]][index[1]]; ArrayList<INavigableVessel> copyList = new ArrayList<>(); for (WeakReference<INavigableVessel> reference : references) { if (reference.get() != null) { copyList.add(reference.get()); } } return copyList; }
/** * Add a new destination Node to the graph. * The new point is added to the heuristic as target and to the graph * together with edges to its nearest neighbors and they back to the new * node. * @param newPoint new location to add to the graph * @param isCity flag indicating if the new location is a city, which means the coordinates * may not be located in the sea area. */ public void addDestinationPoint(Point2D newPoint, boolean isCity) { addDestinationPointInternal(newPoint, isCity); } @VisibleForTesting
/** * Convert a vessel into a list of ships. * @param vessel input vessel of type IConvoy, IShipGroup or simple IShip * @return List of type IShip */ public List<IShip> explodeShipList(INavigableVessel vessel) { if (vessel instanceof IConvoy) { return explodeShipList((IConvoy) vessel); } else if (vessel instanceof IShipGroup) { return explodeShipList((IShipGroup)vessel); } else { return explodeShipList((IShip) vessel); } }
private void redistributeShips(List<GenericPair<List<IShip>, List<IShip>>> pairings, boolean attackShipTurn, List<IShip> survivingShips) { for (IShip ship : survivingShips) { GenericPair<List<IShip>, List<IShip>> weakestPairing = findWeakest(pairings, attackShipTurn); if (attackShipTurn) { weakestPairing.getFirst().add(ship); } else { weakestPairing.getSecond().add(ship); } } }
/** * Find the closest point in the graph for a defined point. * @param point for which the closest defined point should be looked up. * @return closest point to <code>point</code> */ public Point2D findClosest(Point2D point) { double distance = Double.MAX_VALUE; Point2D p = null; for (Point2D tempPoint : getHeuristic().keySet()) { if (point.distance(tempPoint) < distance) { distance = point.distance(tempPoint); p = tempPoint; } } return p; }
/** * Send a vessel to be repaired. * @param vessel to be repaired */ private void sendForRepairs(INavigableVessel vessel) { vessel.togglePirateFlag(); ICity city = cityService.findNearestCity(vessel.getLocation()); seafaringService.travelToCity(vessel, city); }
private ObjectDoubleMap<Point2D> calculateHeuristicForSource(Point2D source, List<ICity> cities) { ObjectDoubleMap<Point2D> distance = new ObjectDoubleScatterMap<>(); for (ICity city : cities) { final Point2D coordinates = city.getCoordinates(); double dist = calculateWeight(source, coordinates); distance.put(coordinates, dist); } return distance; } @VisibleForTesting
/** * Travel from a city to a place that is not a city. * @param vessel traveling from a city to a location at sea * @param location destination location */ public void travelFromCity(INavigableVessel vessel, Point2D location) { List<Point2D> path = travelTo(vessel, location); Preconditions.checkNotNull(path); pathConverter.createPath(vessel, path, 1); VesselPositionUpdateData vesselPositionUpdateData = interpolators.get(vessel); vesselPositionUpdateData.setDestinationCity(false); vesselPositionUpdateData.setSourceCity(true); } }
public void add(INavigableVessel ship) { int[] index = calculateIndices(ship.getLocation()); segments[index[0]][index[1]].add(new WeakReference(ship)); }
/** * Travel from one city to another. * @param vessel which travels between cities * @param toCity destination city */ public void travelBetweenCities(INavigableVessel vessel, ICity toCity) { List<Point2D> path = travelTo(vessel, toCity.getCoordinates()); Preconditions.checkNotNull(path); pathConverter.createPath(vessel, path, 1); VesselPositionUpdateData vesselPositionUpdateData = interpolators.get(vessel); vesselPositionUpdateData.setDestinationCity(true); vesselPositionUpdateData.setSourceCity(true); }
/** * Travel from a location that is not a city to a city. * @param vessel traveling to a city * @param toCity destination city */ public void travelToCity(INavigableVessel vessel, ICity toCity) { List<Point2D> path = travelTo(vessel, toCity.getCoordinates()); Preconditions.checkNotNull(path); pathConverter.createPath(vessel, path, 1); VesselPositionUpdateData vesselPositionUpdateData = interpolators.get(vessel); vesselPositionUpdateData.setDestinationCity(true); vesselPositionUpdateData.setSourceCity(false); }