Merge pull request #1257 from markwhi/miner_chargepad_fixes

DynamicNetwork capcity optimizations and two small bugfixes.
This commit is contained in:
Aidan 2014-03-26 09:15:52 -04:00
commit 42903c9dfe
6 changed files with 102 additions and 55 deletions

View file

@ -44,12 +44,14 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
public GasNetwork(IGridTransmitter<GasNetwork>... varPipes) public GasNetwork(IGridTransmitter<GasNetwork>... varPipes)
{ {
transmitters.addAll(Arrays.asList(varPipes)); transmitters.addAll(Arrays.asList(varPipes));
updateCapacity();
register(); register();
} }
public GasNetwork(Collection<IGridTransmitter<GasNetwork>> collection) public GasNetwork(Collection<IGridTransmitter<GasNetwork>> collection)
{ {
transmitters.addAll(collection); transmitters.addAll(collection);
updateCapacity();
register(); register();
} }
@ -94,6 +96,7 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
gasScale = getScale(); gasScale = getScale();
updateCapacity();
refresh(); refresh();
register(); register();
} }
@ -300,6 +303,7 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
{ {
Set<IGridTransmitter<GasNetwork>> iterTubes = (Set<IGridTransmitter<GasNetwork>>)transmitters.clone(); Set<IGridTransmitter<GasNetwork>> iterTubes = (Set<IGridTransmitter<GasNetwork>>)transmitters.clone();
Iterator<IGridTransmitter<GasNetwork>> it = iterTubes.iterator(); Iterator<IGridTransmitter<GasNetwork>> it = iterTubes.iterator();
boolean networkChanged = false;
possibleAcceptors.clear(); possibleAcceptors.clear();
acceptorDirections.clear(); acceptorDirections.clear();
@ -311,6 +315,7 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
if(conductor == null || ((TileEntity)conductor).isInvalid()) if(conductor == null || ((TileEntity)conductor).isInvalid())
{ {
it.remove(); it.remove();
networkChanged = true;
transmitters.remove(conductor); transmitters.remove(conductor);
} }
else { else {
@ -333,6 +338,10 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
} }
} }
} }
if (networkChanged) {
updateCapacity();
}
} }
@Override @Override

View file

@ -30,7 +30,10 @@ public abstract class DynamicNetwork<A, N extends DynamicNetwork<A, N>> implemen
private List<DelayQueue> updateQueue = new ArrayList<DelayQueue>(); private List<DelayQueue> updateQueue = new ArrayList<DelayQueue>();
protected int ticksSinceCreate = 0; protected int ticksSinceCreate = 0;
protected int capacity = 0;
protected double meanCapacity = 0;
protected boolean fixed = false; protected boolean fixed = false;
protected boolean needsUpdate = false; protected boolean needsUpdate = false;
@ -44,6 +47,7 @@ public abstract class DynamicNetwork<A, N extends DynamicNetwork<A, N>> implemen
public void addAllTransmitters(Set<IGridTransmitter<N>> newTransmitters) public void addAllTransmitters(Set<IGridTransmitter<N>> newTransmitters)
{ {
transmitters.addAll(newTransmitters); transmitters.addAll(newTransmitters);
updateCapacity();
} }
public boolean isFirst(IGridTransmitter<N> transmitter) public boolean isFirst(IGridTransmitter<N> transmitter)
@ -55,7 +59,8 @@ public abstract class DynamicNetwork<A, N extends DynamicNetwork<A, N>> implemen
public void removeTransmitter(IGridTransmitter<N> transmitter) public void removeTransmitter(IGridTransmitter<N> transmitter)
{ {
transmitters.remove(transmitter); transmitters.remove(transmitter);
updateCapacity();
if(transmitters.size() == 0) if(transmitters.size() == 0)
{ {
deregister(); deregister();
@ -107,20 +112,33 @@ public abstract class DynamicNetwork<A, N extends DynamicNetwork<A, N>> implemen
return possibleAcceptors.size(); return possibleAcceptors.size();
} }
public int getCapacity() protected synchronized void updateCapacity() {
{ updateMeanCapacity();
return (int)getMeanCapacity() * transmitters.size(); capacity = (int)meanCapacity * transmitters.size();
} }
/** /**
* Override this if things can have variable capacity along the network. * Override this if things can have variable capacity along the network.
* @return An 'average' value of capacity. Calculate it how you will. * @return An 'average' value of capacity. Calculate it how you will.
*/ */
public double getMeanCapacity() protected synchronized void updateMeanCapacity() {
{ if (transmitters.size() > 0) {
return transmitters.size() > 0 ? transmitters.iterator().next().getCapacity() : 0; meanCapacity = transmitters.iterator().next().getCapacity();
} else {
meanCapacity = 0;
}
} }
public int getCapacity()
{
return capacity;
}
public double getMeanCapacity()
{
return meanCapacity;
}
@Override @Override
public void tick() public void tick()
{ {

View file

@ -41,12 +41,14 @@ public class EnergyNetwork extends DynamicNetwork<TileEntity, EnergyNetwork>
public EnergyNetwork(IGridTransmitter<EnergyNetwork>... varCables) public EnergyNetwork(IGridTransmitter<EnergyNetwork>... varCables)
{ {
transmitters.addAll(Arrays.asList(varCables)); transmitters.addAll(Arrays.asList(varCables));
updateCapacity();
register(); register();
} }
public EnergyNetwork(Collection<IGridTransmitter<EnergyNetwork>> collection) public EnergyNetwork(Collection<IGridTransmitter<EnergyNetwork>> collection)
{ {
transmitters.addAll(collection); transmitters.addAll(collection);
updateCapacity();
register(); register();
} }
@ -80,46 +82,46 @@ public class EnergyNetwork extends DynamicNetwork<TileEntity, EnergyNetwork>
register(); register();
} }
@Override @Override
public double getMeanCapacity() protected synchronized void updateMeanCapacity()
{ {
//Use the harmonic mean. Because we're mean. int numCables = transmitters.size();
int numCables = transmitters.size(); double reciprocalSum = 0;
double reciprocalSum = 0;
for(IGridTransmitter<EnergyNetwork> cable : transmitters)
{
reciprocalSum += 1.0/(double)cable.getCapacity();
}
for(IGridTransmitter<EnergyNetwork> cable : transmitters) meanCapacity = (double)numCables / reciprocalSum;
{
reciprocalSum += 1.0/(double)cable.getCapacity();
}
return (double)numCables / reciprocalSum;
} }
@Override @Override
public void onNetworksCreated(List<EnergyNetwork> networks) public void onNetworksCreated(List<EnergyNetwork> networks)
{ {
if(FMLCommonHandler.instance().getEffectiveSide().isServer()) if(FMLCommonHandler.instance().getEffectiveSide().isServer())
{ {
double[] caps = new double[networks.size()]; double[] caps = new double[networks.size()];
double cap = 0; double cap = 0;
for(EnergyNetwork network : networks) for(EnergyNetwork network : networks)
{ {
caps[networks.indexOf(network)] = network.getCapacity(); double networkCapacity = network.getCapacity();
cap += network.getCapacity(); caps[networks.indexOf(network)] = networkCapacity;
} cap += networkCapacity;
}
electricityStored = Math.min(cap, electricityStored);
electricityStored = Math.min(cap, electricityStored);
double[] percent = ListUtils.percent(caps);
double[] percent = ListUtils.percent(caps);
for(EnergyNetwork network : networks)
{ for(EnergyNetwork network : networks)
network.electricityStored = round(percent[networks.indexOf(network)]*electricityStored); {
} network.electricityStored = round(percent[networks.indexOf(network)]*electricityStored);
} }
} }
}
public synchronized double getEnergyNeeded() public synchronized double getEnergyNeeded()
{ {
if(FMLCommonHandler.instance().getEffectiveSide().isClient()) if(FMLCommonHandler.instance().getEffectiveSide().isClient())
@ -312,7 +314,8 @@ public class EnergyNetwork extends DynamicNetwork<TileEntity, EnergyNetwork>
{ {
Set<IGridTransmitter<EnergyNetwork>> iterCables = (Set<IGridTransmitter<EnergyNetwork>>)transmitters.clone(); Set<IGridTransmitter<EnergyNetwork>> iterCables = (Set<IGridTransmitter<EnergyNetwork>>)transmitters.clone();
Iterator<IGridTransmitter<EnergyNetwork>> it = iterCables.iterator(); Iterator<IGridTransmitter<EnergyNetwork>> it = iterCables.iterator();
boolean networkChanged = false;
possibleAcceptors.clear(); possibleAcceptors.clear();
acceptorDirections.clear(); acceptorDirections.clear();
@ -324,6 +327,7 @@ public class EnergyNetwork extends DynamicNetwork<TileEntity, EnergyNetwork>
{ {
it.remove(); it.remove();
transmitters.remove(conductor); transmitters.remove(conductor);
networkChanged = true;
} }
else { else {
conductor.setTransmitterNetwork(this); conductor.setTransmitterNetwork(this);
@ -346,6 +350,10 @@ public class EnergyNetwork extends DynamicNetwork<TileEntity, EnergyNetwork>
} }
} }
if (networkChanged) {
this.updateCapacity();
}
needsUpdate = true; needsUpdate = true;
} }
@ -448,6 +456,7 @@ public class EnergyNetwork extends DynamicNetwork<TileEntity, EnergyNetwork>
network.joulesTransmitted = joulesTransmitted; network.joulesTransmitted = joulesTransmitted;
network.lastPowerScale = lastPowerScale; network.lastPowerScale = lastPowerScale;
network.electricityStored += electricityStored; network.electricityStored += electricityStored;
network.updateCapacity();
return network; return network;
} }

View file

@ -42,12 +42,14 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
public FluidNetwork(IGridTransmitter<FluidNetwork>... varPipes) public FluidNetwork(IGridTransmitter<FluidNetwork>... varPipes)
{ {
transmitters.addAll(Arrays.asList(varPipes)); transmitters.addAll(Arrays.asList(varPipes));
updateCapacity();
register(); register();
} }
public FluidNetwork(Collection<IGridTransmitter<FluidNetwork>> collection) public FluidNetwork(Collection<IGridTransmitter<FluidNetwork>> collection)
{ {
transmitters.addAll(collection); transmitters.addAll(collection);
updateCapacity();
register(); register();
} }
@ -92,6 +94,8 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
fluidScale = getScale(); fluidScale = getScale();
updateCapacity();
refresh(); refresh();
register(); register();
} }
@ -298,7 +302,8 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
{ {
Set<IGridTransmitter<FluidNetwork>> iterPipes = (Set<IGridTransmitter<FluidNetwork>>)transmitters.clone(); Set<IGridTransmitter<FluidNetwork>> iterPipes = (Set<IGridTransmitter<FluidNetwork>>)transmitters.clone();
Iterator it = iterPipes.iterator(); Iterator it = iterPipes.iterator();
boolean networkChanged = false;
possibleAcceptors.clear(); possibleAcceptors.clear();
acceptorDirections.clear(); acceptorDirections.clear();
@ -309,6 +314,7 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
if(conductor == null || ((TileEntity)conductor).isInvalid()) if(conductor == null || ((TileEntity)conductor).isInvalid())
{ {
it.remove(); it.remove();
networkChanged = true;
transmitters.remove(conductor); transmitters.remove(conductor);
} }
else { else {
@ -331,6 +337,10 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
} }
} }
} }
if(networkChanged) {
updateCapacity();
}
} }
@Override @Override

View file

@ -11,6 +11,7 @@ import mekanism.common.tile.TileEntityDigitalMiner;
import mekanism.common.util.MekanismUtils; import mekanism.common.util.MekanismUtils;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
public class ThreadMinerSearch extends Thread public class ThreadMinerSearch extends Thread
{ {
@ -66,7 +67,8 @@ public class ThreadMinerSearch extends Thread
continue; continue;
} }
if(tileEntity.worldObj.getBlockTileEntity(x, y, z) instanceof IBoundingBlock) TileEntity bte;
if ((bte = tileEntity.worldObj.getBlockTileEntity(x, y, z)) != null && bte instanceof IBoundingBlock)
{ {
continue; continue;
} }

View file

@ -53,9 +53,8 @@ public class TileEntityChargepad extends TileEntityElectricBlock implements IAct
if(!worldObj.isRemote) if(!worldObj.isRemote)
{ {
isActive = false; isActive = false;
List<EntityLivingBase> entities = worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(xCoord, yCoord, zCoord, xCoord+1, yCoord+0.2, zCoord+1));
List<EntityLiving> entities = worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(xCoord, yCoord, zCoord, xCoord+1, yCoord+0.2, zCoord+1));
for(EntityLivingBase entity : entities) for(EntityLivingBase entity : entities)
{ {
if(entity instanceof EntityPlayer || entity instanceof EntityRobit) if(entity instanceof EntityPlayer || entity instanceof EntityRobit)