mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-27 23:33:08 +00:00
v5.4.0 Beta #1
Lots of things, too tired to list. Will give full list when this build is released.
This commit is contained in:
1 parent
d58222b028
commit
5cf1654733
66 files changed
+1378
-247
No files matched your search
@@ -0,0 +1,109 @@
|
||||
package mekanism.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
|
||||
import mekanism.api.EnumGas;
|
||||
import mekanism.api.IGasAcceptor;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.common.ForgeDirection;
|
||||
|
||||
public class GasTransferProtocol
|
||||
{
|
||||
/** List of iterated tubes, to prevent infinite loops. */
|
||||
public ArrayList<TileEntity> iteratedTubes = new ArrayList<TileEntity>();
|
||||
|
||||
/** List of IGasAcceptors that can take in the type of gas requested. */
|
||||
public ArrayList<IGasAcceptor> availableAcceptors = new ArrayList<IGasAcceptor>();
|
||||
|
||||
/** Pointer of this calculation */
|
||||
public TileEntity pointer;
|
||||
|
||||
/** Type of gas to distribute */
|
||||
public EnumGas transferType;
|
||||
|
||||
/** Amount of gas to distribute */
|
||||
public int gasToSend;
|
||||
|
||||
/**
|
||||
* GasTransferProtocol -- a calculation used to distribute gasses through a tube network.
|
||||
* @param head - pointer tile entity
|
||||
* @param type - type of gas to distribute
|
||||
* @param amount - amount of gas to distribute
|
||||
*/
|
||||
public GasTransferProtocol(TileEntity head, EnumGas type, int amount)
|
||||
{
|
||||
pointer = head;
|
||||
transferType = type;
|
||||
gasToSend = amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursive loop that iterates through connected tubes and adds connected acceptors to an ArrayList.
|
||||
* @param tile - pointer tile entity
|
||||
*/
|
||||
public void loopThrough(TileEntity tile)
|
||||
{
|
||||
IGasAcceptor[] acceptors = MekanismUtils.getConnectedAcceptors(tile);
|
||||
|
||||
for(IGasAcceptor acceptor : acceptors)
|
||||
{
|
||||
if(acceptor != null)
|
||||
{
|
||||
if(acceptor.canReceiveGas(ForgeDirection.getOrientation(Arrays.asList(acceptors).indexOf(acceptor)).getOpposite(), transferType))
|
||||
{
|
||||
availableAcceptors.add(acceptor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
iteratedTubes.add(tile);
|
||||
|
||||
TileEntity[] tubes = MekanismUtils.getConnectedTubes(tile);
|
||||
|
||||
for(TileEntity tube : tubes)
|
||||
{
|
||||
if(tube != null)
|
||||
{
|
||||
if(!iteratedTubes.contains(tube))
|
||||
{
|
||||
loopThrough(tube);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the protocol and distributes the gas.
|
||||
* @return rejected gas
|
||||
*/
|
||||
public int calculate()
|
||||
{
|
||||
loopThrough(pointer);
|
||||
|
||||
if(!availableAcceptors.isEmpty())
|
||||
{
|
||||
boolean sentRemaining = false;
|
||||
int divider = availableAcceptors.size();
|
||||
int remaining = gasToSend % divider;
|
||||
int sending = (gasToSend-remaining)/divider;
|
||||
|
||||
for(IGasAcceptor acceptor : availableAcceptors)
|
||||
{
|
||||
int currentSending = sending;
|
||||
|
||||
if(remaining > 0)
|
||||
{
|
||||
currentSending++;
|
||||
remaining--;
|
||||
}
|
||||
|
||||
acceptor.transferGasToAcceptor(currentSending, transferType);
|
||||
gasToSend -= currentSending;
|
||||
}
|
||||
}
|
||||
|
||||
return gasToSend;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user