mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-26 14:52:58 +00:00
Emitting logic overhaul (#5422)
* First pass improving performance of EnergyNetwork emitting Only sends power once per tick per accept now. Lots of comments of design included so that I have a backup of my thought process/solutions if I run into some blocker. * Optimized implementation slightly, still more to be done * Cleanup some comments * Move send energy to acceptor logic to CableUtils Replace old emit logic in CableUtils with using the newer logic * Inline getConnections to slightly cut down on number of operations ran * Fix a couple bugs with targets that have multiple sides Also improve performance reducing frequency at amountPerChanged needs to be ran * Improve performance of CableUtils.emit by faking we have one 6 sided target as opposed to having 6 one sided targets * Allow simulating insertion into the EnergyAcceptorWrapper for IC2 * Fix using wrong value for total acceptor count when acceptors have multiple sides * Use an EnumMap instead of a HashMap for the Target * First pass converting gas and fluid network emission to target system * Improve general fluid and gas emitting using the utility method Change a couple things to using Sets rather than lists for emitting so that we can ensure each side is only done once. * Document and cleanup method/variable names of target classes * Start deduplicating emitting code into a EmitUtils file Logic for Gas and Fluid emitting fully generic now * Partially extract how we keep track of the split to make it easier to deduplicate the common code * Start moving shifting needed to given to the targets themselves * Use generics to deduplicate a lot of the remaining duplication in the new emitting code * Fix having broke energy emitting and also further cleanup some code * Half the number of loops the HeatNetwork does onUpdate * Fix potentially divide by zero errors * Deprecate and remove implementations of getAcceptors as it is no longer needed * Never round up from doubles for sending power stops energy networks going negative. * Fix more spots where we were accidentally rounding upwards/clamping improperly for energy * Cleanup impl to use less memory and adjust better as sending if the possible amount changed/should be adjusted Also removed the need for a specific IntegerTypeTarget intermediary class * Cleanup the docs of some of the changed things * Given tickEmit is private now we already know it is only being called serverside * Improve documentation slightly and rename a method to be more accurate * Remove limit on IC2 given it uses doubles as well now * Rename amountPer to amountPerTarget
This commit is contained in:
1 parent
445556fef7
commit
b3663a37a9
37 files changed
+689
-641
No files matched your search
@@ -3,25 +3,21 @@ package mekanism.common.util;
|
||||
import cofh.redstoneflux.api.IEnergyConnection;
|
||||
import cofh.redstoneflux.api.IEnergyProvider;
|
||||
import cofh.redstoneflux.api.IEnergyReceiver;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import mekanism.api.Coord4D;
|
||||
import mekanism.api.energy.IStrictEnergyAcceptor;
|
||||
import mekanism.api.energy.IStrictEnergyOutputter;
|
||||
import mekanism.api.transmitters.TransmissionType;
|
||||
import mekanism.common.base.EnergyAcceptorWrapper;
|
||||
import mekanism.common.base.IEnergyWrapper;
|
||||
import mekanism.common.base.target.EnergyAcceptorTarget;
|
||||
import mekanism.common.capabilities.Capabilities;
|
||||
import mekanism.common.config.MekanismConfig;
|
||||
import mekanism.common.integration.ic2.IC2Integration;
|
||||
import net.darkhax.tesla.api.ITeslaConsumer;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.energy.CapabilityEnergy;
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
|
||||
public final class CableUtils {
|
||||
|
||||
@@ -35,29 +31,6 @@ public final class CableUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the adjacent connections to a TileEntity, from a subset of its sides.
|
||||
*
|
||||
* @param tileEntity - center TileEntity
|
||||
* @param sideFunction - set of sides to check
|
||||
* @return boolean[] of adjacent connections
|
||||
*/
|
||||
public static boolean[] getConnections(TileEntity tileEntity, Function<EnumFacing, Boolean> sideFunction) {
|
||||
boolean[] connectable = new boolean[]{false, false, false, false, false, false};
|
||||
Coord4D coord = Coord4D.get(tileEntity);
|
||||
|
||||
for (EnumFacing side : EnumFacing.values()) {
|
||||
if (sideFunction.apply(side)) {
|
||||
TileEntity tile = coord.offset(side).getTileEntity(tileEntity.getWorld());
|
||||
|
||||
connectable[side.ordinal()] = isValidAcceptorOnSide(tileEntity, tile, side);
|
||||
connectable[side.ordinal()] |= isCable(tile);
|
||||
}
|
||||
}
|
||||
|
||||
return connectable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the adjacent connections to a TileEntity, from a subset of its sides.
|
||||
*
|
||||
@@ -159,103 +132,38 @@ public final class CableUtils {
|
||||
}
|
||||
|
||||
public static void emit(IEnergyWrapper emitter) {
|
||||
if (!((TileEntity) emitter).getWorld().isRemote && MekanismUtils.canFunction((TileEntity) emitter)) {
|
||||
TileEntity tileEntity = (TileEntity) emitter;
|
||||
if (!tileEntity.getWorld().isRemote && MekanismUtils.canFunction(tileEntity)) {
|
||||
double energyToSend = Math.min(emitter.getEnergy(), emitter.getMaxOutput());
|
||||
|
||||
if (energyToSend > 0) {
|
||||
List<EnumFacing> outputtingSides = new LinkedList<>();
|
||||
boolean[] connectable = getConnections((TileEntity) emitter, emitter::sideIsOutput);
|
||||
|
||||
Coord4D coord = Coord4D.get(tileEntity);
|
||||
//Fake that we have one target given we know that no sides will overlap
|
||||
// This allows us to have slightly better performance
|
||||
EnergyAcceptorTarget target = new EnergyAcceptorTarget();
|
||||
for (EnumFacing side : EnumFacing.values()) {
|
||||
if (connectable[side.ordinal()]) {
|
||||
outputtingSides.add(side);
|
||||
if (emitter.sideIsOutput(side)) {
|
||||
TileEntity tile = coord.offset(side).getTileEntity(tileEntity.getWorld());
|
||||
//If it can accept energy or it is a cable
|
||||
if (tile != null && (isValidAcceptorOnSide(tileEntity, tile, side) || isCable(tile))) {
|
||||
//Get the opposite side as the current side is relative to us
|
||||
EnumFacing opposite = side.getOpposite();
|
||||
EnergyAcceptorWrapper acceptor = EnergyAcceptorWrapper.get(tile, opposite);
|
||||
if (acceptor != null && acceptor.canReceiveEnergy(opposite) && acceptor
|
||||
.needsEnergy(opposite)) {
|
||||
target.addHandler(opposite, acceptor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!outputtingSides.isEmpty()) {
|
||||
double sent = 0;
|
||||
boolean tryAgain = false;
|
||||
int i = 0;
|
||||
|
||||
do {
|
||||
double prev = sent;
|
||||
sent += emit_do(emitter, outputtingSides, energyToSend - sent, tryAgain);
|
||||
|
||||
tryAgain = energyToSend - sent > 0 && sent - prev > 0 && i < 100;
|
||||
|
||||
i++;
|
||||
} while (tryAgain);
|
||||
|
||||
int curHandlers = target.getHandlers().size();
|
||||
if (curHandlers > 0) {
|
||||
Set<EnergyAcceptorTarget> targets = new HashSet<>();
|
||||
targets.add(target);
|
||||
double sent = EmitUtils.sendToAcceptors(targets, curHandlers, energyToSend);
|
||||
emitter.setEnergy(emitter.getEnergy() - sent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static double emit_do(IEnergyWrapper emitter, List<EnumFacing> outputtingSides, double totalToSend,
|
||||
boolean tryAgain) {
|
||||
double remains = totalToSend % outputtingSides.size();
|
||||
double splitSend = (totalToSend - remains) / outputtingSides.size();
|
||||
double sent = 0;
|
||||
|
||||
for (Iterator<EnumFacing> it = outputtingSides.iterator(); it.hasNext(); ) {
|
||||
EnumFacing side = it.next();
|
||||
|
||||
TileEntity tileEntity = Coord4D.get((TileEntity) emitter).offset(side)
|
||||
.getTileEntity(((TileEntity) emitter).getWorld());
|
||||
double toSend = splitSend + remains;
|
||||
remains = 0;
|
||||
|
||||
double prev = sent;
|
||||
sent += emit_do_do(emitter, tileEntity, side, toSend, tryAgain);
|
||||
|
||||
if (sent - prev == 0) {
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
|
||||
return sent;
|
||||
}
|
||||
|
||||
private static double emit_do_do(IEnergyWrapper from, TileEntity tileEntity, EnumFacing side, double currentSending,
|
||||
boolean tryAgain) {
|
||||
double sent = 0;
|
||||
|
||||
if (CapabilityUtils.hasCapability(tileEntity, Capabilities.ENERGY_ACCEPTOR_CAPABILITY, side.getOpposite())) {
|
||||
IStrictEnergyAcceptor acceptor = CapabilityUtils
|
||||
.getCapability(tileEntity, Capabilities.ENERGY_ACCEPTOR_CAPABILITY, side.getOpposite());
|
||||
|
||||
if (acceptor.canReceiveEnergy(side.getOpposite())) {
|
||||
sent += acceptor.acceptEnergy(side.getOpposite(), currentSending, false);
|
||||
}
|
||||
} else if (MekanismUtils.useTesla() && CapabilityUtils
|
||||
.hasCapability(tileEntity, Capabilities.TESLA_CONSUMER_CAPABILITY, side.getOpposite())) {
|
||||
ITeslaConsumer consumer = CapabilityUtils
|
||||
.getCapability(tileEntity, Capabilities.TESLA_CONSUMER_CAPABILITY, side.getOpposite());
|
||||
sent += consumer
|
||||
.givePower(Math.round(currentSending * MekanismConfig.current().general.TO_TESLA.val()), false)
|
||||
* MekanismConfig.current().general.FROM_TESLA.val();
|
||||
} else if (MekanismUtils.useForge() && CapabilityUtils
|
||||
.hasCapability(tileEntity, CapabilityEnergy.ENERGY, side.getOpposite())) {
|
||||
IEnergyStorage storage = CapabilityUtils
|
||||
.getCapability(tileEntity, CapabilityEnergy.ENERGY, side.getOpposite());
|
||||
sent += storage
|
||||
.receiveEnergy((int) Math.round(
|
||||
Math.min(Integer.MAX_VALUE, currentSending * MekanismConfig.current().general.TO_FORGE.val())),
|
||||
false) * MekanismConfig.current().general.FROM_FORGE.val();
|
||||
} else if (MekanismUtils.useRF() && tileEntity instanceof IEnergyReceiver) {
|
||||
IEnergyReceiver handler = (IEnergyReceiver) tileEntity;
|
||||
|
||||
if (handler.canConnectEnergy(side.getOpposite())) {
|
||||
int toSend = Math.min((int) Math.round(currentSending * MekanismConfig.current().general.TO_RF.val()),
|
||||
Integer.MAX_VALUE);
|
||||
int used = handler.receiveEnergy(side.getOpposite(), toSend, false);
|
||||
sent += used * MekanismConfig.current().general.FROM_RF.val();
|
||||
}
|
||||
} else if (MekanismUtils.useIC2()) {
|
||||
sent += IC2Integration.emitEnergy(from, tileEntity, side, currentSending);
|
||||
}
|
||||
|
||||
return sent;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user