2013-12-27 23:59:59 +01:00
|
|
|
package appeng.me.cache;
|
|
|
|
|
2014-02-21 07:10:38 +01:00
|
|
|
import java.util.HashMap;
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
import java.util.Set;
|
2014-02-21 07:10:38 +01:00
|
|
|
import java.util.TreeSet;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
import appeng.api.config.AccessRestriction;
|
|
|
|
import appeng.api.config.Actionable;
|
|
|
|
import appeng.api.config.PowerMultiplier;
|
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridBlock;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.IGridStorage;
|
|
|
|
import appeng.api.networking.energy.IAEPowerStorage;
|
|
|
|
import appeng.api.networking.energy.IEnergyGrid;
|
|
|
|
import appeng.api.networking.energy.IEnergyGridProvider;
|
2014-02-21 07:10:38 +01:00
|
|
|
import appeng.api.networking.energy.IEnergyWatcher;
|
|
|
|
import appeng.api.networking.energy.IEnergyWatcherHost;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.networking.events.MENetworkEventSubscribe;
|
2014-03-18 04:58:58 +01:00
|
|
|
import appeng.api.networking.events.MENetworkPostCacheConstruction;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.networking.events.MENetworkPowerIdleChange;
|
|
|
|
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
|
|
|
import appeng.api.networking.events.MENetworkPowerStorage;
|
2014-03-18 04:58:58 +01:00
|
|
|
import appeng.api.networking.pathing.IPathingGrid;
|
2014-02-21 07:10:38 +01:00
|
|
|
import appeng.api.networking.storage.IStackWatcherHost;
|
2014-06-08 18:26:33 +02:00
|
|
|
import appeng.me.Grid;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.me.GridNode;
|
2014-02-21 07:10:38 +01:00
|
|
|
import appeng.me.energy.EnergyThreshold;
|
|
|
|
import appeng.me.energy.EnergyWatcher;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
public class EnergyGridCache implements IEnergyGrid
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* estimated power available.
|
|
|
|
*/
|
|
|
|
int availableTicksSinceUpdate = 0;
|
|
|
|
double globalAvailablePower = 0;
|
2014-03-06 06:45:14 +01:00
|
|
|
double globalMaxPower = 0;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* idle draw.
|
|
|
|
*/
|
|
|
|
double drainPerTick = 0;
|
2014-01-24 17:33:31 +01:00
|
|
|
|
|
|
|
final double AvgLength = 40.0;
|
|
|
|
|
|
|
|
double avgDrainPerTick = 0;
|
|
|
|
double avgInjectionPerTick = 0;
|
|
|
|
|
|
|
|
double tickDrainPerTick = 0;
|
|
|
|
double tickInjectionPerTick = 0;
|
|
|
|
|
2014-02-04 17:11:29 +01:00
|
|
|
// Double[] totalDrainPastTicks = new Double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
|
|
|
|
// 0.0, 0.0, 0.0, 0.0, 0.0 };
|
|
|
|
// Double[] totalInjectionPastTicks = new Double[] { 0.0, 0.0, 0.0, 0.0,
|
|
|
|
// 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* power status
|
|
|
|
*/
|
|
|
|
boolean publicHasPower = false;
|
|
|
|
boolean hasPower = true;
|
|
|
|
long ticksSinceHasPowerChange = 900;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* excess power in the system.
|
|
|
|
*/
|
|
|
|
double extra = 0;
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
IAEPowerStorage lastProvider;
|
2013-12-27 23:59:59 +01:00
|
|
|
Set<IAEPowerStorage> providers = new LinkedHashSet();
|
2014-01-20 17:41:37 +01:00
|
|
|
|
|
|
|
IAEPowerStorage lastRequestor;
|
2013-12-27 23:59:59 +01:00
|
|
|
Set<IAEPowerStorage> requesters = new LinkedHashSet();
|
|
|
|
|
2014-02-21 07:10:38 +01:00
|
|
|
public TreeSet<EnergyThreshold> interests = new TreeSet<EnergyThreshold>();
|
|
|
|
private HashMap<IGridNode, IEnergyWatcher> watchers = new HashMap<IGridNode, IEnergyWatcher>();
|
|
|
|
|
2014-02-06 05:36:10 +01:00
|
|
|
private double buffer()
|
|
|
|
{
|
|
|
|
return providers.isEmpty() ? 1000.0 : 0.0;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
Set<IEnergyGridProvider> gproviders = new LinkedHashSet();
|
|
|
|
|
|
|
|
final IGrid myGrid;
|
2014-03-18 04:58:58 +01:00
|
|
|
PathGridCache pgc;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
public EnergyGridCache(IGrid g) {
|
|
|
|
myGrid = g;
|
|
|
|
}
|
|
|
|
|
2014-03-18 04:58:58 +01:00
|
|
|
@MENetworkEventSubscribe
|
|
|
|
public void postInit(MENetworkPostCacheConstruction pcc)
|
|
|
|
{
|
|
|
|
pgc = myGrid.getCache( IPathingGrid.class );
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@MENetworkEventSubscribe
|
|
|
|
public void EnergyNodeChanges(MENetworkPowerIdleChange ev)
|
|
|
|
{
|
|
|
|
// update power usage based on event.
|
|
|
|
GridNode node = (GridNode) ev.node;
|
|
|
|
IGridBlock gb = node.getGridBlock();
|
|
|
|
|
|
|
|
double newDraw = gb.getIdlePowerUsage();
|
|
|
|
double diffDraw = newDraw - node.previousDraw;
|
|
|
|
node.previousDraw = newDraw;
|
|
|
|
|
|
|
|
drainPerTick += diffDraw;
|
|
|
|
}
|
|
|
|
|
|
|
|
@MENetworkEventSubscribe
|
|
|
|
public void EnergyNodeChanges(MENetworkPowerStorage ev)
|
|
|
|
{
|
|
|
|
if ( ev.storage.isAEPublicPowerStorage() )
|
|
|
|
{
|
|
|
|
switch (ev.type)
|
|
|
|
{
|
|
|
|
case PROVIDE_POWER:
|
|
|
|
if ( ev.storage.getPowerFlow() != AccessRestriction.WRITE )
|
|
|
|
providers.add( ev.storage );
|
|
|
|
break;
|
|
|
|
case REQUEST_POWER:
|
|
|
|
if ( ev.storage.getPowerFlow() != AccessRestriction.READ )
|
|
|
|
requesters.add( ev.storage );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
(new RuntimeException( "Attempt to ask the IEnergyGrid to charge a non public energy store." )).printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getEnergyDemand(double maxRequired)
|
|
|
|
{
|
2014-06-07 04:08:14 +02:00
|
|
|
double required = buffer() - extra;
|
2013-12-27 23:59:59 +01:00
|
|
|
Iterator<IAEPowerStorage> it = requesters.iterator();
|
|
|
|
while (required < maxRequired && it.hasNext())
|
|
|
|
{
|
|
|
|
IAEPowerStorage node = it.next();
|
|
|
|
if ( node.getPowerFlow() != AccessRestriction.READ )
|
|
|
|
required += Math.max( 0.0, node.getAEMaxPower() - node.getAECurrentPower() );
|
|
|
|
}
|
|
|
|
return required;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double injectPower(double i, Actionable mode)
|
|
|
|
{
|
2014-02-07 06:51:19 +01:00
|
|
|
double ignore = extra;
|
2013-12-27 23:59:59 +01:00
|
|
|
i += extra;
|
|
|
|
|
|
|
|
if ( mode == Actionable.SIMULATE )
|
|
|
|
{
|
|
|
|
Iterator<IAEPowerStorage> it = requesters.iterator();
|
|
|
|
while (i > 0 && it.hasNext())
|
|
|
|
{
|
|
|
|
IAEPowerStorage node = it.next();
|
|
|
|
i = node.injectAEPower( i, Actionable.SIMULATE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-07 06:51:19 +01:00
|
|
|
tickInjectionPerTick += i - ignore;
|
2014-01-24 17:33:31 +01:00
|
|
|
// totalInjectionPastTicks[0] += i;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
while (i > 0 && !requesters.isEmpty())
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
IAEPowerStorage node = getFirstRequestor();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
i = node.injectAEPower( i, Actionable.MODULATE );
|
|
|
|
if ( i > 0 )
|
2014-01-20 17:41:37 +01:00
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
requesters.remove( node );
|
2014-01-20 17:41:37 +01:00
|
|
|
lastRequestor = null;
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extra = i;
|
|
|
|
}
|
|
|
|
|
2014-02-06 05:36:10 +01:00
|
|
|
return Math.max( 0.0, i - buffer() );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
Set<IEnergyGrid> seen = new HashSet();
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
public double extractAEPower(double amt, Actionable mode, PowerMultiplier pm)
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
seen.clear();
|
|
|
|
return pm.divide( extractAEPower( pm.multiply( amt ), mode, seen ) );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-04 17:11:29 +01:00
|
|
|
public void addNode(IGridNode node, IGridHost machine)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( machine instanceof IEnergyGridProvider )
|
|
|
|
gproviders.add( (IEnergyGridProvider) machine );
|
|
|
|
|
|
|
|
// idle draw...
|
|
|
|
GridNode gnode = (GridNode) node;
|
|
|
|
IGridBlock gb = gnode.getGridBlock();
|
|
|
|
gnode.previousDraw = gb.getIdlePowerUsage();
|
|
|
|
drainPerTick += gnode.previousDraw;
|
|
|
|
|
|
|
|
// power storage
|
|
|
|
if ( machine instanceof IAEPowerStorage )
|
|
|
|
{
|
|
|
|
IAEPowerStorage ps = (IAEPowerStorage) machine;
|
|
|
|
if ( ps.isAEPublicPowerStorage() )
|
|
|
|
{
|
|
|
|
double max = ps.getAEMaxPower();
|
|
|
|
double current = ps.getAECurrentPower();
|
|
|
|
|
2014-03-29 10:03:35 +01:00
|
|
|
if ( ps.getPowerFlow() != AccessRestriction.WRITE )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-03-15 07:58:21 +01:00
|
|
|
globalMaxPower += ps.getAEMaxPower();
|
2014-03-29 10:03:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( current > 0 && ps.getPowerFlow() != AccessRestriction.WRITE )
|
|
|
|
{
|
|
|
|
globalAvailablePower += current;
|
2013-12-27 23:59:59 +01:00
|
|
|
providers.add( ps );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( current < max && ps.getPowerFlow() != AccessRestriction.READ )
|
|
|
|
requesters.add( ps );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-21 07:10:38 +01:00
|
|
|
if ( machine instanceof IEnergyWatcherHost )
|
|
|
|
{
|
|
|
|
IEnergyWatcherHost swh = (IEnergyWatcherHost) machine;
|
|
|
|
EnergyWatcher iw = new EnergyWatcher( this, (IEnergyWatcherHost) swh );
|
|
|
|
watchers.put( node, iw );
|
|
|
|
swh.updateWatcher( iw );
|
|
|
|
}
|
|
|
|
|
2014-02-04 17:11:29 +01:00
|
|
|
myGrid.postEventTo( node, new MENetworkPowerStatusChange() );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-04 17:11:29 +01:00
|
|
|
public void removeNode(IGridNode node, IGridHost machine)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( machine instanceof IEnergyGridProvider )
|
|
|
|
gproviders.remove( machine );
|
|
|
|
|
|
|
|
// idle draw.
|
|
|
|
GridNode gnode = (GridNode) node;
|
|
|
|
drainPerTick -= gnode.previousDraw;
|
|
|
|
|
|
|
|
// power storage.
|
|
|
|
if ( machine instanceof IAEPowerStorage )
|
|
|
|
{
|
|
|
|
IAEPowerStorage ps = (IAEPowerStorage) machine;
|
2014-03-12 02:54:04 +01:00
|
|
|
if ( ps.isAEPublicPowerStorage() )
|
|
|
|
{
|
|
|
|
if ( ps.getPowerFlow() != AccessRestriction.WRITE )
|
2014-03-15 07:58:21 +01:00
|
|
|
{
|
|
|
|
globalMaxPower -= ps.getAEMaxPower();
|
2014-03-12 02:54:04 +01:00
|
|
|
globalAvailablePower -= ps.getAECurrentPower();
|
2014-03-15 07:58:21 +01:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-03-12 02:54:04 +01:00
|
|
|
if ( lastProvider == machine )
|
|
|
|
lastProvider = null;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-03-12 02:54:04 +01:00
|
|
|
if ( lastRequestor == machine )
|
|
|
|
lastRequestor = null;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-03-12 02:54:04 +01:00
|
|
|
providers.remove( machine );
|
|
|
|
requesters.remove( machine );
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
2014-02-21 07:10:38 +01:00
|
|
|
|
|
|
|
if ( machine instanceof IStackWatcherHost )
|
|
|
|
{
|
|
|
|
IEnergyWatcher myWatcher = watchers.get( machine );
|
|
|
|
if ( myWatcher != null )
|
|
|
|
{
|
|
|
|
myWatcher.clear();
|
|
|
|
watchers.remove( machine );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-02-21 07:10:38 +01:00
|
|
|
double lastStoredPower = -1;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
2014-02-04 17:11:29 +01:00
|
|
|
public void onUpdateTick()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-02-21 07:10:38 +01:00
|
|
|
if ( !interests.isEmpty() )
|
|
|
|
{
|
|
|
|
double oldPower = lastStoredPower;
|
|
|
|
lastStoredPower = getStoredPower();
|
|
|
|
|
|
|
|
EnergyThreshold low = new EnergyThreshold( Math.min( oldPower, lastStoredPower ), null );
|
|
|
|
EnergyThreshold high = new EnergyThreshold( Math.max( oldPower, lastStoredPower ), null );
|
|
|
|
for (EnergyThreshold th : interests.subSet( low, true, high, true ))
|
|
|
|
{
|
|
|
|
((EnergyWatcher) th.watcher).post( this );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-24 17:33:31 +01:00
|
|
|
avgDrainPerTick *= (AvgLength - 1) / AvgLength;
|
|
|
|
avgInjectionPerTick *= (AvgLength - 1) / AvgLength;
|
|
|
|
|
|
|
|
avgDrainPerTick += tickDrainPerTick / AvgLength;
|
|
|
|
avgInjectionPerTick += tickInjectionPerTick / AvgLength;
|
|
|
|
|
|
|
|
tickDrainPerTick = 0;
|
|
|
|
tickInjectionPerTick = 0;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
// next tick is here...
|
2014-01-24 17:33:31 +01:00
|
|
|
// for (int x = 0; x < totalDrainPastTicks.length - 1; x++)
|
|
|
|
// totalDrainPastTicks[x + 1] = totalDrainPastTicks[x];
|
|
|
|
// totalDrainPastTicks[0] = 0.0;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-24 17:33:31 +01:00
|
|
|
// for (int x = 0; x < totalInjectionPastTicks.length - 1; x++)
|
|
|
|
// totalInjectionPastTicks[x + 1] = totalInjectionPastTicks[x];
|
|
|
|
// totalInjectionPastTicks[0] = 0.0;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
// power information.
|
|
|
|
double drained = extractAEPower( getIdlePowerUsage(), Actionable.MODULATE, PowerMultiplier.CONFIG );
|
2014-04-23 04:55:06 +02:00
|
|
|
boolean currentlyHasPower = drained >= drainPerTick - 0.001;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
// ticks since change..
|
|
|
|
if ( currentlyHasPower == hasPower )
|
|
|
|
ticksSinceHasPowerChange++;
|
|
|
|
else
|
|
|
|
ticksSinceHasPowerChange = 0;
|
|
|
|
|
|
|
|
// update status..
|
|
|
|
hasPower = currentlyHasPower;
|
|
|
|
|
|
|
|
// update public status, this buffers power ups for 30 ticks.
|
|
|
|
if ( hasPower && ticksSinceHasPowerChange > 30 )
|
2014-02-04 17:11:29 +01:00
|
|
|
publicPowerState( true, myGrid );
|
2013-12-27 23:59:59 +01:00
|
|
|
else if ( !hasPower )
|
2014-02-04 17:11:29 +01:00
|
|
|
publicPowerState( false, myGrid );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
availableTicksSinceUpdate++;
|
2014-02-06 05:36:10 +01:00
|
|
|
// if ( extra > 32 )
|
|
|
|
// injectPower( 0.0, Actionable.MODULATE );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void publicPowerState(boolean newState, IGrid grid)
|
|
|
|
{
|
|
|
|
if ( publicHasPower == newState )
|
|
|
|
return;
|
|
|
|
|
|
|
|
publicHasPower = newState;
|
2014-06-08 18:26:33 +02:00
|
|
|
((Grid) myGrid).setImportantFlag( 0, publicHasPower );
|
2013-12-27 23:59:59 +01:00
|
|
|
grid.postEvent( new MENetworkPowerStatusChange() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* refresh current stored power.
|
|
|
|
*/
|
|
|
|
public void refreshPower()
|
|
|
|
{
|
|
|
|
availableTicksSinceUpdate = 0;
|
|
|
|
globalAvailablePower = 0;
|
|
|
|
for (IAEPowerStorage p : providers)
|
|
|
|
globalAvailablePower += p.getAECurrentPower();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getStoredPower()
|
|
|
|
{
|
|
|
|
if ( availableTicksSinceUpdate > 90 )
|
|
|
|
refreshPower();
|
|
|
|
|
2014-03-12 02:54:04 +01:00
|
|
|
return Math.max( 0.0, globalAvailablePower );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-03-06 06:45:14 +01:00
|
|
|
@Override
|
|
|
|
public double getMaxStoredPower()
|
|
|
|
{
|
|
|
|
return globalMaxPower;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
public double extractAEPower(double amt, Actionable mode, Set<IEnergyGrid> seen)
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
if ( !seen.add( this ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
double extractedPower = extra;
|
|
|
|
|
|
|
|
if ( mode == Actionable.SIMULATE )
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
extractedPower += simulateExtract( extractedPower, amt );
|
2014-01-31 01:49:50 +01:00
|
|
|
|
|
|
|
if ( extractedPower < amt )
|
|
|
|
{
|
|
|
|
Iterator<IEnergyGridProvider> i = gproviders.iterator();
|
|
|
|
while (extractedPower < amt && i.hasNext())
|
|
|
|
extractedPower += i.next().extractAEPower( amt - extractedPower, mode, seen );
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
return extractedPower;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
else
|
2014-02-06 05:36:10 +01:00
|
|
|
{
|
|
|
|
extra = 0;
|
|
|
|
extractedPower = doExtract( extractedPower, amt );
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
// got more then we wanted?
|
|
|
|
if ( extractedPower > amt )
|
|
|
|
{
|
|
|
|
extra = extractedPower - amt;
|
|
|
|
globalAvailablePower -= amt;
|
|
|
|
|
2014-02-07 06:51:19 +01:00
|
|
|
tickDrainPerTick += amt;
|
2013-12-27 23:59:59 +01:00
|
|
|
return amt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( extractedPower < amt )
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
Iterator<IEnergyGridProvider> i = gproviders.iterator();
|
|
|
|
while (extractedPower < amt && i.hasNext())
|
|
|
|
extractedPower += i.next().extractAEPower( amt - extractedPower, mode, seen );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// go less or the correct amount?
|
|
|
|
globalAvailablePower -= extractedPower;
|
2014-02-07 06:51:19 +01:00
|
|
|
tickDrainPerTick += extractedPower;
|
2013-12-27 23:59:59 +01:00
|
|
|
return extractedPower;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
private double doExtract(double extractedPower, double amt)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-24 17:33:31 +01:00
|
|
|
// totalDrainPastTicks[0] += extractedPower;
|
2014-01-20 17:41:37 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
public boolean isNetworkPowered()
|
|
|
|
{
|
|
|
|
return publicHasPower;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getIdlePowerUsage()
|
|
|
|
{
|
2014-03-18 04:58:58 +01:00
|
|
|
return drainPerTick + pgc.channelPowerUsage;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getAvgPowerUsage()
|
|
|
|
{
|
2014-01-24 17:33:31 +01:00
|
|
|
return avgDrainPerTick;/*
|
|
|
|
* double out = 0;
|
|
|
|
*
|
2014-02-06 05:36:10 +01:00
|
|
|
* for (double x : totalDrainPastTicks) out += x;
|
2014-01-24 17:33:31 +01:00
|
|
|
*
|
|
|
|
* return out / totalDrainPastTicks.length;
|
|
|
|
*/
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getAvgPowerInjection()
|
|
|
|
{
|
2014-01-24 17:33:31 +01:00
|
|
|
return avgInjectionPerTick;/*
|
|
|
|
* double out = 0;
|
|
|
|
*
|
2014-02-06 05:36:10 +01:00
|
|
|
* for (double x : totalInjectionPastTicks) out += x;
|
2014-01-24 17:33:31 +01:00
|
|
|
*
|
2014-02-06 05:36:10 +01:00
|
|
|
* return out / totalInjectionPastTicks.length;
|
2014-01-24 17:33:31 +01:00
|
|
|
*/
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSplit(IGridStorage storageB)
|
|
|
|
{
|
|
|
|
extra /= 2;
|
|
|
|
storageB.dataObject().setDouble( "extraEnergy", extra );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onJoin(IGridStorage storageB)
|
|
|
|
{
|
|
|
|
extra += storageB.dataObject().getDouble( "extraEnergy" );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void populateGridStorage(IGridStorage storage)
|
|
|
|
{
|
|
|
|
storage.dataObject().setDouble( "extraEnergy", this.extra );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|