Search in sources :

Example 1 with IEnergySink

use of ic2.api.energy.tile.IEnergySink in project Galacticraft by micdoodle8.

the class EnergyNetwork method doProduce.

/**
 * Complete the energy transfer. Called internally on server tick end.
 *
 * @return Amount of energy SENT to all acceptors
 */
private float doProduce() {
    float sent = 0.0F;
    if (!this.availableAcceptors.isEmpty()) {
        float energyNeeded = this.totalRequested;
        float energyAvailable = this.totalEnergy;
        float reducor = 1.0F;
        float energyStorageReducor = 1.0F;
        if (energyNeeded > energyAvailable) {
            // If not enough energy, try reducing what goes into energy storage (if any)
            energyNeeded -= this.totalStorageExcess;
            // If there's still not enough, put the minimum into energy storage (if any) and, anyhow, reduce everything proportionately
            if (energyNeeded > energyAvailable) {
                energyStorageReducor = 0F;
                reducor = energyAvailable / energyNeeded;
            } else {
                // Energyavailable exceeds the total needed but only if storage does not fill all in one go - this is a common situation
                energyStorageReducor = (energyAvailable - energyNeeded) / this.totalStorageExcess;
            }
        }
        float currentSending;
        float sentToAcceptor;
        int tierProduced = Math.min(this.producersTierGC, this.networkTierGC);
        Object debugTE = null;
        try {
            for (Object tileEntity : this.availableAcceptors) {
                debugTE = tileEntity;
                // Exit the loop if there is no energy left at all (should normally not happen, should be some even for the last acceptor)
                if (sent >= energyAvailable) {
                    break;
                }
                // The base case is to give each acceptor what it is requesting
                currentSending = this.energyRequests.get(tileEntity);
                // If it's an energy store, we may need to damp it down if energyStorageReducor is less than 1
                if (currentSending > EnergyNetwork.ENERGY_STORAGE_LEVEL) {
                    currentSending = EnergyNetwork.ENERGY_STORAGE_LEVEL + (currentSending - EnergyNetwork.ENERGY_STORAGE_LEVEL) * energyStorageReducor;
                }
                // Reduce everything proportionately if there is not enough energy for all needs
                currentSending *= reducor;
                if (currentSending > energyAvailable - sent) {
                    currentSending = energyAvailable - sent;
                }
                EnumFacing sideFrom = this.availableconnectedDirections.get(tileEntity);
                if (tileEntity instanceof IElectrical) {
                    sentToAcceptor = ((IElectrical) tileEntity).receiveElectricity(sideFrom, currentSending, tierProduced, true);
                } else if (isMekLoaded && tileEntity instanceof IStrictEnergyAcceptor) {
                    sentToAcceptor = (float) ((IStrictEnergyAcceptor) tileEntity).transferEnergyToAcceptor(sideFrom, currentSending * EnergyConfigHandler.TO_MEKANISM_RATIO) / EnergyConfigHandler.TO_MEKANISM_RATIO;
                } else if (isIC2Loaded && tileEntity instanceof IEnergySink) {
                    double energySendingIC2 = currentSending * EnergyConfigHandler.TO_IC2_RATIO;
                    if (energySendingIC2 >= 1D) {
                        double result = 0;
                        try {
                            if (EnergyUtil.voltageParameterIC2) {
                                result = (Double) EnergyUtil.injectEnergyIC2.invoke(tileEntity, sideFrom.getOpposite(), energySendingIC2, 120D);
                            } else {
                                result = (Double) EnergyUtil.injectEnergyIC2.invoke(tileEntity, sideFrom.getOpposite(), energySendingIC2);
                            }
                        } catch (Exception ex) {
                            if (ConfigManagerCore.enableDebug) {
                                ex.printStackTrace();
                            }
                        }
                        sentToAcceptor = currentSending - (float) result / EnergyConfigHandler.TO_IC2_RATIO;
                        if (sentToAcceptor < 0F) {
                            sentToAcceptor = 0F;
                        }
                    } else {
                        sentToAcceptor = 0F;
                    }
                } else if (isRF2Loaded && tileEntity instanceof IEnergyReceiver) {
                    final int currentSendinginRF = (currentSending >= Integer.MAX_VALUE / EnergyConfigHandler.TO_RF_RATIO) ? Integer.MAX_VALUE : (int) (currentSending * EnergyConfigHandler.TO_RF_RATIO);
                    sentToAcceptor = ((IEnergyReceiver) tileEntity).receiveEnergy(sideFrom, currentSendinginRF, false) / EnergyConfigHandler.TO_RF_RATIO;
                } else {
                    sentToAcceptor = 0F;
                }
                if (sentToAcceptor / currentSending > 1.002F && sentToAcceptor > 0.01F) {
                    if (!this.spamstop) {
                        FMLLog.info("Energy network: acceptor took too much energy, offered " + currentSending + ", took " + sentToAcceptor + ". " + tileEntity.toString());
                        this.spamstop = true;
                    }
                    sentToAcceptor = currentSending;
                }
                sent += sentToAcceptor;
            }
        } catch (Exception e) {
            GCLog.severe("DEBUG Energy network loop issue, please report this");
            if (debugTE instanceof TileEntity) {
                GCLog.severe("Problem was likely caused by tile in dim " + GCCoreUtil.getDimensionID(((TileEntity) debugTE).getWorld()) + " at " + ((TileEntity) debugTE).getPos() + " Type:" + debugTE.getClass().getSimpleName());
            }
        }
    }
    if (EnergyNetwork.tickCount % 200 == 0) {
        this.spamstop = false;
    }
    float returnvalue = sent;
    if (returnvalue > this.totalEnergy) {
        returnvalue = this.totalEnergy;
    }
    if (returnvalue < 0F) {
        returnvalue = 0F;
    }
    return returnvalue;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) EnumFacing(net.minecraft.util.EnumFacing) IStrictEnergyAcceptor(mekanism.api.energy.IStrictEnergyAcceptor) IElectrical(micdoodle8.mods.galacticraft.api.transmission.tile.IElectrical) IEnergySink(ic2.api.energy.tile.IEnergySink) IEnergyReceiver(cofh.api.energy.IEnergyReceiver)

Example 2 with IEnergySink

use of ic2.api.energy.tile.IEnergySink in project Flux-Networks by SonarSonic.

the class EnergyUnitsHandler method addEnergy.

@Override
public long addEnergy(long add, TileEntity tile, EnumFacing dir, ActionType actionType) {
    if (tile instanceof IEnergyStorage) {
        IEnergyStorage sink = (IEnergyStorage) tile;
        int before = sink.getStored();
        if (!actionType.shouldSimulate()) {
            return sink.addEnergy((int) Math.min(sink.getCapacity() - before, add));
        } else {
            return Math.min(sink.getCapacity() - before, add);
        }
    } else if (tile instanceof IEnergySink) {
        IEnergySink sink = (IEnergySink) tile;
        double voltage = EnergyNet.instance.getPowerFromTier(sink.getSinkTier());
        double amount = Math.min(add, voltage);
        if (actionType.shouldSimulate()) {
            return (long) Math.min(amount, sink.getDemandedEnergy());
        } else {
            return (long) Math.floor(amount - sink.injectEnergy(dir, amount, voltage));
        }
    }
    return 0;
}
Also used : IEnergyStorage(ic2.api.tile.IEnergyStorage) IEnergySink(ic2.api.energy.tile.IEnergySink)

Example 3 with IEnergySink

use of ic2.api.energy.tile.IEnergySink in project Galacticraft by micdoodle8.

the class EnergyNetwork method doTickStartCalc.

/**
 * Refreshes all tiles in network, and updates requested energy
 */
private void doTickStartCalc() {
    this.tickDone = EnergyNetwork.tickCount;
    this.totalSent = 0F;
    this.refreshAcceptors();
    if (!EnergyUtil.initialisedIC2Methods) {
        EnergyUtil.initialiseIC2Methods();
    }
    if (this.conductors.size() == 0) {
        return;
    }
    this.loopPrevention = true;
    this.availableAcceptors.clear();
    this.availableconnectedDirections.clear();
    this.energyRequests.clear();
    this.totalRequested = 0.0F;
    this.totalStorageExcess = 0F;
    if (!this.connectedAcceptors.isEmpty()) {
        float e;
        final Iterator<EnumFacing> acceptorDirection = this.connectedDirections.iterator();
        for (Object acceptor : this.connectedAcceptors) {
            // This tries all sides of the acceptor which are connected (see refreshAcceptors())
            EnumFacing sideFrom = acceptorDirection.next();
            // But the grid will only put energy into the acceptor from one side - once it's in availableAcceptors
            if (!this.ignoreAcceptors.contains(acceptor) && !this.availableAcceptors.contains(acceptor)) {
                e = 0.0F;
                if (acceptor instanceof IElectrical) {
                    e = ((IElectrical) acceptor).getRequest(sideFrom);
                } else if (isMekLoaded && acceptor instanceof IStrictEnergyAcceptor) {
                    e = (float) ((((IStrictEnergyAcceptor) acceptor).getMaxEnergy() - ((IStrictEnergyAcceptor) acceptor).getEnergy()) / EnergyConfigHandler.TO_MEKANISM_RATIO);
                } else if (isIC2Loaded && acceptor instanceof IEnergySink) {
                    double result = 0;
                    try {
                        result = (Double) EnergyUtil.demandedEnergyIC2.invoke(acceptor);
                    } catch (Exception ex) {
                        if (ConfigManagerCore.enableDebug) {
                            ex.printStackTrace();
                        }
                    }
                    // Cap IC2 power transfer at 128EU/t for standard Alu wire, 256EU/t for heavy Alu wire
                    result = Math.min(result, this.networkTierGC * 128D);
                    e = (float) result / EnergyConfigHandler.TO_IC2_RATIO;
                } else if (isRF2Loaded && acceptor instanceof IEnergyReceiver) {
                    e = ((IEnergyReceiver) acceptor).receiveEnergy(sideFrom, Integer.MAX_VALUE, true) / EnergyConfigHandler.TO_RF_RATIO;
                }
                if (e > 0.0F) {
                    this.availableAcceptors.add(acceptor);
                    this.availableconnectedDirections.put(acceptor, sideFrom);
                    this.energyRequests.put(acceptor, e);
                    this.totalRequested += e;
                    if (e > EnergyNetwork.ENERGY_STORAGE_LEVEL) {
                        this.totalStorageExcess += e - EnergyNetwork.ENERGY_STORAGE_LEVEL;
                    }
                }
            }
        }
    }
    this.loopPrevention = false;
}
Also used : EnumFacing(net.minecraft.util.EnumFacing) IStrictEnergyAcceptor(mekanism.api.energy.IStrictEnergyAcceptor) IElectrical(micdoodle8.mods.galacticraft.api.transmission.tile.IElectrical) IEnergySink(ic2.api.energy.tile.IEnergySink) IEnergyReceiver(cofh.api.energy.IEnergyReceiver)

Aggregations

IEnergySink (ic2.api.energy.tile.IEnergySink)3 IEnergyReceiver (cofh.api.energy.IEnergyReceiver)2 IStrictEnergyAcceptor (mekanism.api.energy.IStrictEnergyAcceptor)2 IElectrical (micdoodle8.mods.galacticraft.api.transmission.tile.IElectrical)2 EnumFacing (net.minecraft.util.EnumFacing)2 IEnergyStorage (ic2.api.tile.IEnergyStorage)1 TileEntity (net.minecraft.tileentity.TileEntity)1