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:
Richard Freimer authored and GitHub committed 2019-04-30 10:25:23 -04:00
1 parent 445556fef7
commit b3663a37a9
37 files changed
+689 -641

No files matched your search

@@ -1,9 +1,8 @@
package mekanism.common.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
import mekanism.common.base.target.FluidHandlerTarget;
import mekanism.common.capabilities.Capabilities;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
@@ -50,13 +49,8 @@ public final class PipeUtils {
/**
* Gets all the acceptors around a tile entity.
*
* @param tileEntity - center tile entity
* @return array of IFluidHandlers
*/
public static IFluidHandler[] getConnectedAcceptors(TileEntity tileEntity) {
return getConnectedAcceptors(tileEntity.getPos(), tileEntity.getWorld());
}
public static IFluidHandler[] getConnectedAcceptors(BlockPos pos, World world) {
IFluidHandler[] acceptors = new IFluidHandler[]{null, null, null, null, null, null};
@@ -83,44 +77,34 @@ public final class PipeUtils {
* @param from - the TileEntity to output from
* @return the amount of gas emitted
*/
public static int emit(List<EnumFacing> sides, FluidStack stack, TileEntity from) {
if (stack == null) {
public static int emit(Set<EnumFacing> sides, FluidStack stack, TileEntity from) {
if (stack == null || stack.amount == 0) {
return 0;
}
List<IFluidHandler> availableAcceptors = new ArrayList<>();
IFluidHandler[] possibleAcceptors = getConnectedAcceptors(from);
for (IFluidHandler handler : possibleAcceptors) {
if (handler != null && canFill(handler, stack)) {
availableAcceptors.add(handler);
//Fake that we have one target given we know that no sides will overlap
// This allows us to have slightly better performance
FluidHandlerTarget target = new FluidHandlerTarget(stack);
for (EnumFacing orientation : sides) {
TileEntity acceptor = from.getWorld().getTileEntity(from.getPos().offset(orientation));
if (acceptor == null) {
continue;
}
}
Collections.shuffle(availableAcceptors);
int toSend = stack.amount;
int prevSending = toSend;
if (!availableAcceptors.isEmpty()) {
int divider = availableAcceptors.size();
int remaining = toSend % divider;
int sending = (toSend - remaining) / divider;
for (IFluidHandler acceptor : availableAcceptors) {
int currentSending = sending;
if (remaining > 0) {
currentSending++;
remaining--;
EnumFacing opposite = orientation.getOpposite();
if (CapabilityUtils.hasCapability(acceptor, CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, opposite)) {
IFluidHandler handler = CapabilityUtils.getCapability(acceptor,
CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, opposite);
if (handler != null && canFill(handler, stack)) {
target.addHandler(opposite, handler);
}
EnumFacing dir = EnumFacing.byIndex(Arrays.asList(possibleAcceptors).indexOf(acceptor)).getOpposite();
toSend -= acceptor.fill(copy(stack, currentSending), true);
}
}
return prevSending - toSend;
int curHandlers = target.getHandlers().size();
if (curHandlers > 0) {
Set<FluidHandlerTarget> targets = new HashSet<>();
targets.add(target);
return EmitUtils.sendToAcceptors(targets, curHandlers, stack.amount, stack);
}
return 0;
}
public static FluidStack copy(FluidStack fluid, int amount) {