ExtractorModule extends Module implements IExtractorModule, IExtractorModuleController { @Override protected void onNewTurn() { Tile location = getGameObject().getLocation().get(); Maybe<Integer> availableResources = location.getResourceQuantity( resourceType ); if (!availableResources.isPresent()) return; int newAvailableResources = Math.max( 0, availableResources.get() - speed ); int minedResources = availableResources.get() - newAvailableResources; if (!availableResources.isPresent()) return; PredicateNN<IGameObject> foundFunction = gameObject -> { for (final ContainerModule containerModule : gameObject.getModules( ModuleType.CONTAINER )) if (containerModule.getAvailable() > 0) return true; return false; }; NNFunctionNN<PathUtils.Step<IGameObject>, Double> costFunction = gameObjectStep -> 1d; NNFunctionNN<IGameObject, Stream<? extends IGameObject>> neighboursFunction = neighbour -> neighbour.getLocation().get().neighbours().stream() .map( new Function<ITile, IGameObject>() { @Override public IGameObject apply(final ITile tile) { return tile.getContents().orNull(); } } ).filter( gameObject -> gameObject != null ); while (minedResources > 0) { Optional<PathUtils.Path<IGameObject>> path = PathUtils.find( getGameObject(), foundFunction, costFunction, Constants.MAX_DISTANCE_TO_CONTAINER, neighboursFunction ); if (!path.isPresent()) break; for (final ContainerModule containerModule : path.get().getTarget().getModules( ModuleType.CONTAINER )) minedResources -= containerModule.addStock( minedResources ); } newAvailableResources += minedResources; location.setResourceQuantity( resourceType, newAvailableResources ); logger.trc( "unstocked resources: %d %s, left in tile: %d", minedResources, resourceType, newAvailableResources ); } protected  ExtractorModule(final ImmutableResourceCost resourceCost, final ResourceType resourceType, final int speed); @Override ResourceType getResourceType(); @Override int getSpeed(); @Override IExtractorModuleController getController(); @Override IExtractorModule getModule();  }
@Test public void testOnNewTurn() throws Exception { ExtractorModule extractorModule = ExtractorModule.createWithStandardResourceCost().resourceType( ResourceType.FUEL ).speed( 5 ); ContainerModule unconnectedContainerModule = ContainerModule.createWithStandardResourceCost() .resourceType( ResourceType.FUEL ) .capacity( 3 ); createUnit( testUnitType( "Extractor", extractorModule ), 0, 0 ); createUnit( testUnitType( "Unconnected Container", unconnectedContainerModule ), 3, 0 ); extractorModule.getGameObject().getLocation().get().setResourceQuantity( ResourceType.FUEL, 10 ); assertEquals( (int) extractorModule.getGameObject().getLocation().get().getResourceQuantity( ResourceType.FUEL ).get(), 10 ); extractorModule.onNewTurn(); assertEquals( (int) extractorModule.getGameObject().getLocation().get().getResourceQuantity( ResourceType.FUEL ).get(), 10 ); ContainerModule smallConnectedContainerModule = ContainerModule.createWithStandardResourceCost() .resourceType( ResourceType.FUEL ) .capacity( 3 ); createUnit( testUnitType( "Small Connected Container", smallConnectedContainerModule ), 1, 0 ); assertEquals( smallConnectedContainerModule.getAvailable(), 3 ); extractorModule.onNewTurn(); assertEquals( (int) extractorModule.getGameObject().getLocation().get().getResourceQuantity( ResourceType.FUEL ).get(), 7 ); assertEquals( smallConnectedContainerModule.getAvailable(), 0 ); extractorModule.onNewTurn(); assertEquals( (int) extractorModule.getGameObject().getLocation().get().getResourceQuantity( ResourceType.FUEL ).get(), 7 ); assertEquals( smallConnectedContainerModule.getAvailable(), 0 ); ContainerModule bigConnectedContainerModule = ContainerModule.createWithStandardResourceCost() .resourceType( ResourceType.FUEL ) .capacity( 15 ); createUnit( testUnitType( "Big Connected Container", bigConnectedContainerModule ), 0, 1 ); assertEquals( smallConnectedContainerModule.getAvailable(), 0 ); assertEquals( bigConnectedContainerModule.getAvailable(), 15 ); extractorModule.onNewTurn(); assertEquals( (int) extractorModule.getGameObject().getLocation().get().getResourceQuantity( ResourceType.FUEL ).get(), 2 ); assertEquals( bigConnectedContainerModule.getAvailable(), 10 ); extractorModule.onNewTurn(); assertFalse( extractorModule.getGameObject().getLocation().get().getResourceQuantity( ResourceType.FUEL ).isPresent() ); assertEquals( bigConnectedContainerModule.getAvailable(), 8 ); assertEquals( bigConnectedContainerModule.getStock(), 7 ); }