private ProductionChainMissionData getMissionData(IAIPlayer player, INavigableVessel vessel) { return (ProductionChainMissionData) player.getTradeMission(vessel); }
private boolean isVesselEligibleForCollecting(IAIPlayer player, INavigableVessel vessel) { return !(player.getTradeMission(vessel) instanceof CollectWaresMissionData || player.getTradeMission(vessel) instanceof ISpecialMission); }
/** * Check weather the vessel should be engaged in trading. * @param vessel not engaged in trading * @return true if the vassel should execute a trade step */ public boolean shouldBeTrading(INavigableVessel vessel) { IAIPlayer player = (IAIPlayer) vessel.getOwner(); ITradeMissionData missionData = player.getTradeMission(vessel); return missionData == null || !(missionData instanceof ISpecialMission); } }
@VisibleForTesting void handleBuyWaresInStartCity(IAIPlayer player, INavigableVessel vessel, ICity city) { IProductionConsumptionKnowledge globalKnowledge = player.getProductionAndConsumptionKnowledge(); DynamicTradeRouteMissionData missionData = (DynamicTradeRouteMissionData) player.getTradeMission(vessel); ICity nextStop = findNextStopForBuying(city, globalKnowledge, Collections.singletonList(missionData.getWare()), vessel, city); HashSet<IWare> buyWares = new HashSet<>(); buyWares.add(missionData.getWare()); addDefaultTradeSteps(vessel, player, city, nextStop, new HashSet<>(), buyWares, false); } @VisibleForTesting
public Optional<IShip> selectShip(IAIPlayer player) { List<INavigableVessel> vessels = player.getSelectableVessels(); IShip strongestShip = null; int strongest = 0; for (INavigableVessel vessel : vessels) { if (vessel instanceof IShip && !(player.getTradeMission(vessel) instanceof ISpecialMission)) { int strength = shipService.calculateShipsWeaponsStrength(vessel); if (strongestShip == null) { strongestShip = (IShip) vessel; strongest = strength; } else if (strength > strongest) { strongestShip = (IShip) vessel; strongest = strength; } } } return Optional.ofNullable(strongestShip); } }
/** * Check weather the vessel should return to the hometown * @param player for whom to check the returnal to the hometown * @param vessel which should return to the hometown * @return true if the vessle should return to the hometown */ @VisibleForTesting boolean shouldReturnToHometown(IAIPlayer player, INavigableVessel vessel) { if (player.getCompany().getCash() < lowMoney) { return true; } else { List<IWare> waresOfInterest = ((SupplyCityMissionData) player.getTradeMission(vessel)).getRequiredWares(); if (vessel.getCapacity() == 0) { for (EWare ware : EWare.values()) { if (vessel.getWare(ware).getAmount() > 0 && !waresOfInterest.contains(ware)) { return false; } } return true; } else { return false; } } }
@Override public void handleShipArrivesInPort(IShipEntersPortEvent event) { INavigableVessel vessel = event.getShip(); if (isMatchingTradeStrategy(vessel)) { IAIPlayer player = (IAIPlayer) vessel.getOwner(); ICity city = event.getCity(); SupplyCityMissionData missionData = (SupplyCityMissionData) player.getTradeMission(vessel); ICity targetCity = missionData.getTargetCity(); CentralStorageStrategyHint hint = (CentralStorageStrategyHint) player.getPlayerContext().getHints(targetCity).stream().filter(h -> h instanceof CentralStorageStrategyHint).findFirst().get(); CentralStorageHintContext context = (CentralStorageHintContext) hint.getContext(); if (city.equals(targetCity)) { // - unload oal to storage TransferToOfficeTradeStep transferToOfficeStep = createTransferToOfficeTradeStep(vessel, city); append(player, vessel, transferToOfficeStep); // - find best city to supply wares ICity nextStop = findProvidingWares(city, context.getWares(), context.getSuppliedCities(), player, vessel); // buy wares needed there ICityProductionConsumptionKnowledge cityKnowledge = player.getProductionAndConsumptionKnowledge().getKnowlege(nextStop); List<Pair<IWare, Number>> needs = getMostNeededWares(cityKnowledge, vessel); HashSet<IWare> buyWares = new HashSet<>(); needs.stream().forEach(pair -> buyWares.add(pair.getKey())); buyWares.removeAll(context.getWares()); addDefaultTradeSteps(vessel, player, city, nextStop, new HashSet<>(context.getWares()), buyWares, false); } else { addDefaultTradeSteps(vessel, player, city, targetCity, new HashSet<>(context.getWares()), new HashSet<>(context.getWares()), false); } executeTradeSteps(player, vessel); } }
final ICity city = event.getCity(); final IAIPlayer player = (IAIPlayer) vessel.getOwner(); final CollectWaresMissionData tradeMission = (CollectWaresMissionData) player.getTradeMission(vessel); final ICity destination = tradeMission.getCity(); boolean haveEverything = false;
@Override public void handleShipArrivesInPort(IShipEntersPortEvent event) { INavigableVessel vessel = event.getShip(); if (isMatchingTradeStrategy(vessel)) { IAIPlayer player = (IAIPlayer) vessel.getOwner(); ICity city = event.getCity(); CentralStorageDeliverMissionData missionData = (CentralStorageDeliverMissionData) player.getTradeMission(vessel); if (city.equals(missionData.getSourceCity())) { // Sell the wares not on the list // refill and restart handleTradeInCentralStorage(player, vessel, city, missionData.getContext()); } else { // calculate the wares needed in cities down the road and sell accordingly // buy wares that are needed and not in the delivery list handleTradeInSuppliedCity(player, vessel, city, missionData); } executeTradeSteps(player, vessel); } } @VisibleForTesting
@Override public void handleShipArrivesInPort(IShipEntersPortEvent event) { INavigableVessel vessel = event.getShip(); if (isMatchingTradeStrategy(vessel)) { IAIPlayer player = (IAIPlayer) vessel.getOwner(); ICity city = event.getCity(); IProductionConsumptionKnowledge globalKnowledge = player.getProductionAndConsumptionKnowledge(); DynamicTradeRouteMissionData missionData = (DynamicTradeRouteMissionData) player.getTradeMission(vessel); int wareAmount = vessel.getWare(missionData.getWare()).getAmount(); if (wareAmount > 0) { // TODO: andi 10/27/17 when selling all wares in this city we still travel one further: #631 List<IWare> wareOfInterest = Collections.singletonList(missionData.getWare()); ICity nextStop = findNextStopForSelling(city, globalKnowledge, wareOfInterest, vessel, city); addDefaultTradeSteps(vessel, player, city, nextStop, new HashSet<>(), new HashSet<>(), false); } else { Pair<ICity, IWare> pair = findClosestCityProducingTradableWare(player, vessel); missionData.setWare(pair.getValue()); handleBuyWaresReturnToStartCity(player, vessel, city, pair.getKey()); } } } }
/** * {@inheritDoc}<br> * Choose the nearest city from the set defined in the trade route. * @param vessel that should travel to a city, so that the trade cycle can be reinitialized * @return */ @Override public ICity getCityToRestartTradeCycle(INavigableVessel vessel) { ITradeMissionData missionData = ((IAIPlayer)vessel.getOwner()).getTradeMission(vessel); ITradeRoute route = ((TradeRouteMissionData)missionData).getTradeRoute(); double distance = Double.MAX_VALUE; ICity destination = null; for (ITradeRouteStop stop : route.getTradeStops()) { double d = vessel.getLocation().distance(stop.getTradeStop().getCoordinates()); if (d < distance) { distance = d; destination = stop.getTradeStop(); } } return destination; }
ITradeMissionData missionData = player.getTradeMission(vessel); if (missionData == null || !(missionData instanceof TradeRouteMissionData)) { missionData = selectTradeRoute(city);
ICity city = optCity.get(); ICity hometown = player.getHometown(); List<IWare> waresOfInterest = ((SupplyCityMissionData) player.getTradeMission(vessel)).getRequiredWares(); IProductionConsumptionKnowledge knowledge = player.getProductionAndConsumptionKnowledge(); ICityProductionConsumptionKnowledge knowledgeCurrentTown = knowledge.getKnowlege(city);
IProductionConsumptionKnowledge knowlege = player.getProductionAndConsumptionKnowledge(); knowlege.updateKnowledge(event.getCity()); ITradeMissionData missionData = player.getTradeMission(vessel); if (missionData instanceof ISpecialMission) { if (missionData instanceof IBlockadeMission) {
@VisibleForTesting void handleTradeInCentralStorage(IAIPlayer player, INavigableVessel vessel, ICity centralStorageCity, CentralStorageHintContext context) { CentralStorageDeliverMissionData missionData = (CentralStorageDeliverMissionData) player.getTradeMission(vessel);