Err too much.
This commit is contained in:
Vendored
+85
-69
@@ -51,9 +51,34 @@ public class EnergyGridCache implements IEnergyGrid
|
||||
double prev_extra = 0;
|
||||
double extra = 0;
|
||||
|
||||
IAEPowerStorage lastProvider;
|
||||
Set<IAEPowerStorage> providers = new LinkedHashSet();
|
||||
|
||||
IAEPowerStorage lastRequestor;
|
||||
Set<IAEPowerStorage> requesters = new LinkedHashSet();
|
||||
|
||||
private IAEPowerStorage getFirstRequestor()
|
||||
{
|
||||
if ( lastRequestor == null )
|
||||
{
|
||||
Iterator<IAEPowerStorage> i = requesters.iterator();
|
||||
lastRequestor = i.hasNext() ? i.next() : null;
|
||||
}
|
||||
|
||||
return lastRequestor;
|
||||
}
|
||||
|
||||
private IAEPowerStorage getFirstProvider()
|
||||
{
|
||||
if ( lastProvider == null )
|
||||
{
|
||||
Iterator<IAEPowerStorage> i = providers.iterator();
|
||||
lastProvider = i.hasNext() ? i.next() : null;
|
||||
}
|
||||
|
||||
return lastProvider;
|
||||
}
|
||||
|
||||
Set<IEnergyGridProvider> gproviders = new LinkedHashSet();
|
||||
|
||||
final IGrid myGrid;
|
||||
@@ -131,11 +156,14 @@ public class EnergyGridCache implements IEnergyGrid
|
||||
{
|
||||
while (i > 0 && !requesters.isEmpty())
|
||||
{
|
||||
IAEPowerStorage node = requesters.iterator().next();
|
||||
IAEPowerStorage node = getFirstRequestor();
|
||||
|
||||
i = node.injectAEPower( i, Actionable.MODULATE );
|
||||
if ( i > 0 )
|
||||
{
|
||||
requesters.remove( node );
|
||||
lastRequestor = null;
|
||||
}
|
||||
}
|
||||
|
||||
extra = i;
|
||||
@@ -145,10 +173,13 @@ public class EnergyGridCache implements IEnergyGrid
|
||||
return i;
|
||||
}
|
||||
|
||||
Set<IEnergyGrid> seen = new HashSet();
|
||||
|
||||
@Override
|
||||
public double extractAEPower(double amt, Actionable mode, PowerMultiplier pm)
|
||||
{
|
||||
return pm.divide( extractAEPower( pm.multiply( amt ), mode, new HashSet() ) );
|
||||
seen.clear();
|
||||
return pm.divide( extractAEPower( pm.multiply( amt ), mode, seen ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -203,45 +234,17 @@ public class EnergyGridCache implements IEnergyGrid
|
||||
if ( ps.getPowerFlow() != AccessRestriction.WRITE )
|
||||
globalAvailablePower -= ps.getAECurrentPower();
|
||||
|
||||
if ( lastProvider == machine )
|
||||
lastProvider = null;
|
||||
|
||||
if ( lastRequestor == machine )
|
||||
lastRequestor = null;
|
||||
|
||||
providers.remove( machine );
|
||||
requesters.remove( machine );
|
||||
}
|
||||
}
|
||||
|
||||
// recalculate the whole thing?
|
||||
public void reset(IGrid grid)
|
||||
{
|
||||
providers = new LinkedHashSet();
|
||||
requesters = new LinkedHashSet();
|
||||
|
||||
// re-calculate power info
|
||||
for (Class c : grid.getMachinesClasses())
|
||||
{
|
||||
if ( IAEPowerStorage.class.isAssignableFrom( c ) )
|
||||
{
|
||||
for (Object obj : grid.getMachines( c ))
|
||||
{
|
||||
IAEPowerStorage ps = (IAEPowerStorage) obj;
|
||||
if ( ps.isAEPublicPowerStorage() )
|
||||
{
|
||||
double max = ps.getAEMaxPower();
|
||||
double current = ps.getAECurrentPower();
|
||||
|
||||
if ( current > 0 && ps.getPowerFlow() != AccessRestriction.WRITE )
|
||||
{
|
||||
providers.add( ps );
|
||||
globalAvailablePower += ps.getAECurrentPower();
|
||||
}
|
||||
|
||||
if ( current < max && ps.getPowerFlow() != AccessRestriction.READ )
|
||||
requesters.add( ps );
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdateTick(IGrid grid)
|
||||
{
|
||||
@@ -280,7 +283,6 @@ public class EnergyGridCache implements IEnergyGrid
|
||||
|
||||
private void publicPowerState(boolean newState, IGrid grid)
|
||||
{
|
||||
|
||||
if ( publicHasPower == newState )
|
||||
return;
|
||||
|
||||
@@ -311,44 +313,18 @@ public class EnergyGridCache implements IEnergyGrid
|
||||
@Override
|
||||
public double extractAEPower(double amt, Actionable mode, Set<IEnergyGrid> seen)
|
||||
{
|
||||
if ( seen.contains( this ) )
|
||||
if ( !seen.add( this ) )
|
||||
return 0;
|
||||
|
||||
seen.add( this );
|
||||
|
||||
double extractedPower = extra;
|
||||
|
||||
if ( mode == Actionable.SIMULATE )
|
||||
{
|
||||
Iterator<IAEPowerStorage> it = providers.iterator();
|
||||
while (extractedPower < amt && it.hasNext())
|
||||
{
|
||||
IAEPowerStorage node = it.next();
|
||||
|
||||
double req = amt - extractedPower;
|
||||
double newPower = node.extractAEPower( req, Actionable.SIMULATE, PowerMultiplier.ONE );
|
||||
extractedPower += newPower;
|
||||
}
|
||||
extractedPower += simulateExtract( extractedPower, amt );
|
||||
return extractedPower;
|
||||
}
|
||||
else
|
||||
{
|
||||
extra = 0;
|
||||
|
||||
while (extractedPower < amt && !providers.isEmpty())
|
||||
{
|
||||
IAEPowerStorage node = providers.iterator().next();
|
||||
|
||||
double req = amt - extractedPower;
|
||||
double newPower = node.extractAEPower( req, Actionable.MODULATE, PowerMultiplier.ONE );
|
||||
extractedPower += newPower;
|
||||
|
||||
if ( newPower < req )
|
||||
providers.remove( node );
|
||||
}
|
||||
|
||||
// add power used to the current tick.
|
||||
totalDrainPastTicks[0] += extractedPower;
|
||||
}
|
||||
extractedPower += doExtract( extractedPower, amt );
|
||||
|
||||
// got more then we wanted?
|
||||
if ( extractedPower > amt )
|
||||
@@ -361,8 +337,9 @@ public class EnergyGridCache implements IEnergyGrid
|
||||
|
||||
if ( extractedPower < amt )
|
||||
{
|
||||
for (IEnergyGridProvider egp : gproviders)
|
||||
extractedPower += egp.extractAEPower( amt - extractedPower, mode, seen );
|
||||
Iterator<IEnergyGridProvider> i = gproviders.iterator();
|
||||
while (extractedPower < amt && i.hasNext())
|
||||
extractedPower += i.next().extractAEPower( amt - extractedPower, mode, seen );
|
||||
}
|
||||
|
||||
// go less or the correct amount?
|
||||
@@ -370,6 +347,45 @@ public class EnergyGridCache implements IEnergyGrid
|
||||
return extractedPower;
|
||||
}
|
||||
|
||||
private double doExtract(double extractedPower, double amt)
|
||||
{
|
||||
extra = 0;
|
||||
|
||||
while (extractedPower < amt && !providers.isEmpty())
|
||||
{
|
||||
IAEPowerStorage node = getFirstProvider();
|
||||
|
||||
double req = amt - extractedPower;
|
||||
double newPower = node.extractAEPower( req, Actionable.MODULATE, PowerMultiplier.ONE );
|
||||
extractedPower += newPower;
|
||||
|
||||
if ( newPower < req )
|
||||
{
|
||||
providers.remove( node );
|
||||
lastProvider = null;
|
||||
}
|
||||
}
|
||||
|
||||
totalDrainPastTicks[0] += extractedPower;
|
||||
return extractedPower;
|
||||
}
|
||||
|
||||
private double simulateExtract(double extractedPower, double amt)
|
||||
{
|
||||
Iterator<IAEPowerStorage> it = providers.iterator();
|
||||
|
||||
while (extractedPower < amt && it.hasNext())
|
||||
{
|
||||
IAEPowerStorage node = it.next();
|
||||
|
||||
double req = amt - extractedPower;
|
||||
double newPower = node.extractAEPower( req, Actionable.SIMULATE, PowerMultiplier.ONE );
|
||||
extractedPower += newPower;
|
||||
}
|
||||
|
||||
return extractedPower;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNetworkPowered()
|
||||
{
|
||||
|
||||
Vendored
+1
-14
@@ -33,19 +33,6 @@ public class NetworkMonitor<T extends IAEStack<T>> extends MEMonitorHandler<T>
|
||||
|
||||
final static public LinkedList depth = new LinkedList();
|
||||
|
||||
private BaseActionSource src;
|
||||
|
||||
public void setSource(BaseActionSource src)
|
||||
{
|
||||
this.src = src;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BaseActionSource getSource()
|
||||
{
|
||||
return src;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void postChange(T diff, BaseActionSource src)
|
||||
{
|
||||
@@ -86,7 +73,7 @@ public class NetworkMonitor<T extends IAEStack<T>> extends MEMonitorHandler<T>
|
||||
if ( sendEvent )
|
||||
{
|
||||
sendEvent = false;
|
||||
myGridCache.myGrid.postEvent( new MENetworkStorageEvent( getStorageList(), myChannel ) );
|
||||
myGridCache.myGrid.postEvent( new MENetworkStorageEvent( this, myChannel ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Vendored
+63
-33
@@ -1,6 +1,5 @@
|
||||
package appeng.me.cache;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
|
||||
import appeng.api.networking.IGrid;
|
||||
@@ -8,7 +7,9 @@ import appeng.api.networking.IGridCache;
|
||||
import appeng.api.networking.IGridHost;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.IGridStorage;
|
||||
import appeng.parts.misc.PartP2PTunnel;
|
||||
import appeng.core.AELog;
|
||||
import appeng.me.cache.helpers.TunnelCollection;
|
||||
import appeng.parts.p2p.PartP2PTunnel;
|
||||
|
||||
import com.google.common.collect.LinkedHashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
@@ -16,8 +17,9 @@ import com.google.common.collect.Multimap;
|
||||
public class P2PCache implements IGridCache
|
||||
{
|
||||
|
||||
private HashMap<Long, PartP2PTunnel> inputs = new HashMap();
|
||||
private Multimap<Long, PartP2PTunnel> Tunnels = LinkedHashMultimap.create();
|
||||
final private HashMap<Long, PartP2PTunnel> inputs = new HashMap();
|
||||
final private Multimap<Long, PartP2PTunnel> outputs = LinkedHashMultimap.create();
|
||||
final private TunnelCollection NullColl = new TunnelCollection<PartP2PTunnel>( null, null );
|
||||
|
||||
final IGrid myGrid;
|
||||
|
||||
@@ -25,50 +27,78 @@ public class P2PCache implements IGridCache
|
||||
myGrid = g;
|
||||
}
|
||||
|
||||
public void updateFreq(PartP2PTunnel t, long NewFreq)
|
||||
{
|
||||
outputs.remove( t.freq, t );
|
||||
inputs.remove( t.freq );
|
||||
|
||||
t.freq = NewFreq;
|
||||
|
||||
if ( t.output )
|
||||
outputs.put( t.freq, t );
|
||||
else
|
||||
inputs.put( t.freq, t );
|
||||
|
||||
AELog.info( "update-" + (t.output ? "output: " : "input: ") + t.freq );
|
||||
updateTunnel( t.freq, t.output );
|
||||
updateTunnel( t.freq, !t.output );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addNode(IGrid grid, IGridNode node, IGridHost machine)
|
||||
{
|
||||
if ( machine instanceof PartP2PTunnel )
|
||||
reset( grid );
|
||||
{
|
||||
PartP2PTunnel t = (PartP2PTunnel) machine;
|
||||
AELog.info( "add-" + (t.output ? "output: " : "input: ") + t.freq );
|
||||
|
||||
if ( t.output )
|
||||
outputs.put( t.freq, t );
|
||||
else
|
||||
inputs.put( t.freq, t );
|
||||
|
||||
updateTunnel( t.freq, !t.output );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeNode(IGrid grid, IGridNode node, IGridHost machine)
|
||||
{
|
||||
if ( machine instanceof PartP2PTunnel )
|
||||
reset( grid );
|
||||
}
|
||||
|
||||
public void reset(IGrid grid)
|
||||
{
|
||||
|
||||
Tunnels.clear();
|
||||
inputs.clear();
|
||||
|
||||
for (IGridNode n : grid.getMachines( PartP2PTunnel.class ))
|
||||
{
|
||||
PartP2PTunnel p = (PartP2PTunnel) n.getMachine();
|
||||
if ( p.freq > 0 )
|
||||
{
|
||||
if ( p.output )
|
||||
Tunnels.put( p.freq, p );
|
||||
else if ( inputs.containsKey( p.freq ) )
|
||||
p.output = true;
|
||||
else
|
||||
inputs.put( p.freq, p );
|
||||
}
|
||||
PartP2PTunnel t = (PartP2PTunnel) machine;
|
||||
AELog.info( "rmv-" + (t.output ? "output: " : "input: ") + t.freq );
|
||||
|
||||
if ( t.output )
|
||||
outputs.remove( t.freq, t );
|
||||
else
|
||||
inputs.remove( t.freq );
|
||||
|
||||
updateTunnel( t.freq, !t.output );
|
||||
}
|
||||
|
||||
for (PartP2PTunnel p : inputs.values())
|
||||
p.onChange();
|
||||
|
||||
for (PartP2PTunnel p : Tunnels.values())
|
||||
p.onChange();
|
||||
}
|
||||
|
||||
public Collection<PartP2PTunnel> getOutputs(long freq)
|
||||
private void updateTunnel(long freq, boolean updateOutputs)
|
||||
{
|
||||
return Tunnels.get( freq );
|
||||
if ( updateOutputs )
|
||||
{
|
||||
for (PartP2PTunnel p : outputs.values())
|
||||
p.onChange();
|
||||
}
|
||||
else
|
||||
{
|
||||
for (PartP2PTunnel p : inputs.values())
|
||||
p.onChange();
|
||||
}
|
||||
}
|
||||
|
||||
public TunnelCollection<PartP2PTunnel> getOutputs(long freq, Class<? extends PartP2PTunnel> c)
|
||||
{
|
||||
PartP2PTunnel in = inputs.get( freq );
|
||||
if ( in == null )
|
||||
return NullColl;
|
||||
|
||||
return inputs.get( freq ).getCollection( outputs.get( freq ) );
|
||||
}
|
||||
|
||||
public PartP2PTunnel getInput(long freq)
|
||||
|
||||
Vendored
-13
@@ -14,13 +14,11 @@ import appeng.api.networking.ticking.TickRateModulation;
|
||||
import appeng.api.networking.ticking.TickingRequest;
|
||||
import appeng.core.AELog;
|
||||
import appeng.me.cache.helpers.TickTracker;
|
||||
import appeng.util.Platform;
|
||||
|
||||
public class TickManagerCache implements ITickManager
|
||||
{
|
||||
|
||||
private long currentTick = 0;
|
||||
private long lastInnerTime = 0;
|
||||
|
||||
final IGrid myGrid;
|
||||
|
||||
@@ -40,11 +38,6 @@ public class TickManagerCache implements ITickManager
|
||||
return currentTick;
|
||||
}
|
||||
|
||||
public long getInnerTime()
|
||||
{
|
||||
return lastInnerTime;
|
||||
}
|
||||
|
||||
public long getAvgNanoTime(IGridNode node)
|
||||
{
|
||||
TickTracker tt = awake.get( node );
|
||||
@@ -70,13 +63,7 @@ public class TickManagerCache implements ITickManager
|
||||
{
|
||||
// remove tt..
|
||||
upcomingTicks.poll();
|
||||
|
||||
long startTime = Platform.nanoTime();
|
||||
TickRateModulation mod = tt.gt.tickingRequest( tt.node, diff );
|
||||
long estimatedTime = Platform.nanoTime() - startTime;
|
||||
lastInnerTime += estimatedTime;
|
||||
|
||||
tt.LastFiveTicksTime = ((tt.LastFiveTicksTime * 4) / 5) + estimatedTime;
|
||||
|
||||
switch (mod)
|
||||
{
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package appeng.me.cache.helpers;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
import appeng.parts.p2p.PartP2PTunnel;
|
||||
import appeng.util.iterators.NullIterator;
|
||||
|
||||
public class TunnelCollection<T extends PartP2PTunnel> implements Iterable<T>
|
||||
{
|
||||
|
||||
final Class clz;
|
||||
Collection<T> tunnelsource;
|
||||
|
||||
public TunnelCollection(Collection<T> src, Class c) {
|
||||
tunnelsource = src;
|
||||
clz = c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator()
|
||||
{
|
||||
if ( tunnelsource == null )
|
||||
return new NullIterator();
|
||||
return new TunnelIterator( tunnelsource, clz );
|
||||
}
|
||||
|
||||
public void setSource(Collection<T> c)
|
||||
{
|
||||
tunnelsource = c;
|
||||
}
|
||||
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return iterator().hasNext();
|
||||
}
|
||||
}
|
||||
Vendored
+52
@@ -0,0 +1,52 @@
|
||||
package appeng.me.cache.helpers;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
import appeng.parts.p2p.PartP2PTunnel;
|
||||
|
||||
public class TunnelIterator<T extends PartP2PTunnel> implements Iterator<T>
|
||||
{
|
||||
|
||||
Iterator<T> wrapped;
|
||||
Class targetType;
|
||||
T Next;
|
||||
|
||||
private void findNext()
|
||||
{
|
||||
while (Next == null && wrapped.hasNext())
|
||||
{
|
||||
Next = wrapped.next();
|
||||
if ( !targetType.isInstance( Next ) )
|
||||
Next = null;
|
||||
}
|
||||
}
|
||||
|
||||
public TunnelIterator(Collection<T> tunnelsource, Class clz) {
|
||||
wrapped = tunnelsource.iterator();
|
||||
targetType = clz;
|
||||
findNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext()
|
||||
{
|
||||
findNext();
|
||||
return Next != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next()
|
||||
{
|
||||
T tmp = Next;
|
||||
Next = null;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove()
|
||||
{
|
||||
// no.
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user