use of network.rs485.logisticspipes.world.WorldCoordinatesWrapper in project LogisticsPipes by RS485.
the class LogisticsPowerProviderTileEntity method update.
@Override
public void update() {
super.update();
pauseRequesting = false;
if (!init) {
if (MainProxy.isClient(getWorld())) {
LogisticsHUDRenderer.instance().add(this);
}
init = true;
}
double globalRequest = orders.values().stream().reduce(Double::sum).orElse(0.0);
if (globalRequest > 0) {
final double fullfillRatio = Math.min(1, Math.min(internalStorage, getMaxProvidePerTick()) / globalRequest);
if (fullfillRatio > 0) {
final Function<NeighborTileEntity<LogisticsTileGenericPipe>, CoreRoutedPipe> getPipe = (NeighborTileEntity<LogisticsTileGenericPipe> neighbor) -> (CoreRoutedPipe) neighbor.getTileEntity().pipe;
orders.entrySet().stream().map(routerIdToOrderCount -> new Pair<>(SimpleServiceLocator.routerManager.getRouter(routerIdToOrderCount.getKey()), Math.min(internalStorage, routerIdToOrderCount.getValue() * fullfillRatio))).filter(destinationToPower -> destinationToPower.getValue1() != null && destinationToPower.getValue1().getPipe() != null).forEach(destinationToPower -> new WorldCoordinatesWrapper(this).allNeighborTileEntities().stream().flatMap(neighbor -> LPNeighborTileEntityKt.optionalIs(neighbor, LogisticsTileGenericPipe.class).map(Stream::of).orElseGet(Stream::empty)).filter(neighbor -> neighbor.getTileEntity().pipe instanceof CoreRoutedPipe && !getPipe.apply(neighbor).stillNeedReplace()).flatMap(neighbor -> getPipe.apply(neighbor).getRouter().getDistanceTo(destinationToPower.getValue1()).stream().map(exitRoute -> new Pair<>(neighbor, exitRoute))).filter(neighborToExit -> neighborToExit.getValue2().containsFlag(PipeRoutingConnectionType.canPowerSubSystemFrom) && neighborToExit.getValue2().filters.stream().noneMatch(IFilter::blockPower)).findFirst().ifPresent(neighborToSource -> {
CoreRoutedPipe sourcePipe = getPipe.apply(neighborToSource.getValue1());
if (sourcePipe.isInitialized()) {
sourcePipe.container.addLaser(neighborToSource.getValue1().getOurDirection(), 1, getLaserColor(), true, true);
}
sendPowerLaserPackets(sourcePipe.getRouter(), destinationToPower.getValue1(), neighborToSource.getValue2().exitOrientation, neighborToSource.getValue2().exitOrientation != neighborToSource.getValue1().getDirection());
internalStorage -= destinationToPower.getValue2();
// because calculations with floats
if (internalStorage <= 0)
internalStorage = 0;
handlePower(destinationToPower.getValue1().getPipe(), destinationToPower.getValue2());
}));
}
}
orders.clear();
if (MainProxy.isServer(world)) {
if (internalStorage != lastUpdateStorage) {
updateClients();
lastUpdateStorage = internalStorage;
}
}
}
use of network.rs485.logisticspipes.world.WorldCoordinatesWrapper in project LogisticsPipes by RS485.
the class SneakyUpgradeConfigGuiProvider method getClientGui.
@Override
public Object getClientGui(EntityPlayer player) {
LogisticsTileGenericPipe bPipe = getTileAs(player.world, LogisticsTileGenericPipe.class);
if (!(bPipe.pipe instanceof CoreRoutedPipe)) {
return null;
}
List<DoubleCoordinates> list = new WorldCoordinatesWrapper(bPipe).connectedTileEntities().stream().filter(in -> SimpleServiceLocator.pipeInformationManager.isNotAPipe(in.getTileEntity())).map(in -> new DoubleCoordinates(in.getTileEntity())).collect(Collectors.toList());
if (list.isEmpty()) {
list = new WorldCoordinatesWrapper(bPipe).connectedTileEntities().stream().map(in -> new DoubleCoordinates(in.getTileEntity())).collect(Collectors.toList());
}
return new SneakyConfigurationPopup(list, getSlot(player, UpgradeSlot.class));
}
use of network.rs485.logisticspipes.world.WorldCoordinatesWrapper in project LogisticsPipes by RS485.
the class PowerSupplierHandler method requestICPower.
private void requestICPower() {
// Use Buffer
final List<LPNeighborTileEntity<TileEntity>> adjacentTileEntities = new WorldCoordinatesWrapper(pipe.container).allNeighborTileEntities();
double globalNeed = 0;
double[] need = new double[adjacentTileEntities.size()];
int i = 0;
for (NeighborTileEntity<TileEntity> adjacent : adjacentTileEntities) {
if (SimpleServiceLocator.IC2Proxy.isEnergySink(adjacent.getTileEntity())) {
if (pipe.canPipeConnect(adjacent.getTileEntity(), adjacent.getDirection())) {
if (SimpleServiceLocator.IC2Proxy.acceptsEnergyFrom(adjacent.getTileEntity(), pipe.container, adjacent.getOurDirection())) {
// TODO pipe.container must be IEnergySource
globalNeed += need[i] = SimpleServiceLocator.IC2Proxy.demandedEnergyUnits(adjacent.getTileEntity());
}
}
}
++i;
}
if (globalNeed != 0 && !Double.isNaN(globalNeed)) {
double fullfillable = Math.min(1, internalBufferIC2 / globalNeed);
i = 0;
for (NeighborTileEntity<TileEntity> adjacent : adjacentTileEntities) {
if (SimpleServiceLocator.IC2Proxy.isEnergySink(adjacent.getTileEntity()) && pipe.canPipeConnect(adjacent.getTileEntity(), adjacent.getDirection()) && SimpleServiceLocator.IC2Proxy.acceptsEnergyFrom(adjacent.getTileEntity(), pipe.container, adjacent.getOurDirection())) {
// TODO pipe.container must be IEnergySource
if (internalBufferIC2 + 1 < need[i] * fullfillable) {
return;
}
double toUse = Math.min(pipe.getUpgradeManager().getIC2PowerLevel(), need[i] * fullfillable);
double unUsed = SimpleServiceLocator.IC2Proxy.injectEnergyUnits(adjacent.getTileEntity(), adjacent.getOurDirection(), toUse);
double used = toUse - unUsed;
if (used > 0) {
// MainProxy.sendPacketToAllWatchingChunk(this.pipe.getX(), this.pipe.getZ(), MainProxy.getDimensionForWorld(this.pipe.getWorld()), PacketHandler.getPacket(PowerPacketLaser.class).setColor(LogisticsPowerProviderTileEntity.IC2_COLOR).setPos(this.pipe.getLPPosition()).setRenderBall(true).setDir(adTile.orientation).setLength(0.5F));
pipe.container.addLaser(adjacent.getDirection(), 0.5F, LogisticsPowerProviderTileEntity.IC2_COLOR, false, true);
internalBufferIC2 -= used;
}
if (internalBufferIC2 < 0) {
internalBufferIC2 = 0;
return;
}
}
++i;
}
}
// Rerequest Buffer
List<Pair<ISubSystemPowerProvider, List<IFilter>>> provider = pipe.getRouter().getSubSystemPowerProvider();
double available = 0;
outer: for (Pair<ISubSystemPowerProvider, List<IFilter>> pair : provider) {
for (IFilter filter : pair.getValue2()) {
if (filter.blockPower()) {
continue outer;
}
}
if (pair.getValue1().usePaused()) {
continue;
}
if (!pair.getValue1().getBrand().equals("EU")) {
continue;
}
available += pair.getValue1().getPowerLevel();
}
if (available > 0) {
double neededPower = PowerSupplierHandler.INTERNAL_IC2_BUFFER_MAX - internalBufferIC2;
if (neededPower > 0) {
if (pipe.useEnergy((int) (neededPower / 10000), false)) {
outer: for (Pair<ISubSystemPowerProvider, List<IFilter>> pair : provider) {
for (IFilter filter : pair.getValue2()) {
if (filter.blockPower()) {
continue outer;
}
}
if (pair.getValue1().usePaused()) {
continue;
}
if (!pair.getValue1().getBrand().equals("EU")) {
continue;
}
double requestamount = neededPower * (pair.getValue1().getPowerLevel() / available);
pair.getValue1().requestPower(pipe.getRouterId(), requestamount);
}
}
}
}
}
Aggregations