Skip to content

Commit

Permalink
yet another try at energy issues
Browse files Browse the repository at this point in the history
fix requesters not being added to the energy grid


partially revert the energy 'fixes'


potential desync on item count


energy and items change
  • Loading branch information
PrototypeTrousers committed Sep 17, 2021
1 parent 19f928a commit 517bd77
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 66 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ aechannel=stable
aebuild=7
aegroup=appeng
aebasename=appliedenergistics2
trousers=omni-fixes-v46t
trousers=omni-fixes-v46aa

#########################################################
# Versions #
Expand Down
190 changes: 132 additions & 58 deletions src/main/java/appeng/me/cache/EnergyGridCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@
package appeng.me.cache;


import java.util.*;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.NavigableSet;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;

import com.google.common.base.Preconditions;
import com.google.common.collect.*;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import com.google.common.collect.Sets;

import appeng.api.config.AccessRestriction;
import appeng.api.config.Actionable;
Expand Down Expand Up @@ -70,15 +81,15 @@ public class EnergyGridCache implements IEnergyGrid
// Should only be modified from the add/remove methods below to guard against
// concurrent modifications
private final double averageLength = 40.0;
private final Multimap<Integer,IAEPowerStorage> providers = HashMultimap.create();
private final Set<IAEPowerStorage> providers = new LinkedHashSet<>();
// Used to track whether an extraction is currently in progress, to fail fast
// when something externally
// modifies the energy grid.
private boolean ongoingExtractOperation = false;

// Should only be modified from the add/remove methods below to guard against
// concurrent modifications
private final Multimap<Integer,IAEPowerStorage> requesters = HashMultimap.create();
private final Set<IAEPowerStorage> requesters = new LinkedHashSet<>();
// Used to track whether an injection is currently in progress, to fail fast
// when something externally
// modifies the energy grid.
Expand Down Expand Up @@ -115,16 +126,16 @@ public class EnergyGridCache implements IEnergyGrid
private double lastStoredPower = -1;

private final GridPowerStorage localStorage = new GridPowerStorage();
private Set<IAEPowerStorage> providersToRemove = new HashSet<>();
private Set<IAEPowerStorage> requestersToRemove = new HashSet<>();
private Set<IAEPowerStorage> providerToRemove = new HashSet<>();
private Set<IAEPowerStorage> requesterToRemove = new HashSet<>();
private Set<IAEPowerStorage> providersToAdd = new HashSet<>();
private Set<IAEPowerStorage> requestersToAdd = new HashSet<>();
private Set<IAEPowerStorage> requesterToAdd = new HashSet<>();

public EnergyGridCache( final IGrid g )
{
this.myGrid = g;
this.requesters.put( 0, this.localStorage );
this.providers.put( 1, this.localStorage );
this.requesters.add( this.localStorage );
this.providers.add( this.localStorage );
}

@MENetworkEventSubscribe
Expand Down Expand Up @@ -157,13 +168,27 @@ public void storagePowerChangeHandler( final MENetworkPowerStorage ev )
case PROVIDE_POWER:
if( ev.storage.getPowerFlow() != AccessRestriction.WRITE )
{
this.providersToAdd.add( ev.storage );
if( !ongoingExtractOperation )
{
addProvider( ev.storage );
}
else
{
this.providersToAdd.add( ev.storage );
}
}
break;
case REQUEST_POWER:
if( ev.storage.getPowerFlow() != AccessRestriction.READ )
{
this.requestersToAdd.add( ev.storage );
if( !ongoingInjectOperation )
{
addRequester( ev.storage );
}
else
{
this.requesterToAdd.add( ev.storage );
}
}
break;
}
Expand Down Expand Up @@ -293,7 +318,7 @@ private void refreshPower()
{
this.availableTicksSinceUpdate = 0;
this.globalAvailablePower = 0;
for( final IAEPowerStorage p : this.providers.values() )
for( final IAEPowerStorage p : this.providers )
{
this.globalAvailablePower += p.getAECurrentPower();
}
Expand All @@ -310,45 +335,55 @@ public double extractProviderPower( final double amt, final Actionable mode )
{
double extractedPower = 0;

providersToAdd.forEach( provider -> {
if( provider != localStorage )
{
providers.put( 0, provider );
}
} );
this.providers.addAll( providersToAdd );
providersToAdd.clear();

final Iterator<IAEPowerStorage> it = this.providers.values().iterator();
final Iterator<IAEPowerStorage> it = this.providers.iterator();

ongoingExtractOperation = true;
boolean ls = false;
try
{
while ( extractedPower < amt && it.hasNext() )
{
final IAEPowerStorage node = it.next();

if( node == localStorage && mode == Actionable.MODULATE )
{
ls = true;
continue;
}

final double req = amt - extractedPower;
final double newPower = node.extractAEPower( req, mode, PowerMultiplier.ONE );
extractedPower += newPower;

if( newPower < req && mode == Actionable.MODULATE )
{
if (node != localStorage)
it.remove();
it.remove();
}
}
} finally
{
providersToRemove.forEach( provider -> {
if( provider != localStorage )
{
providers.remove( 0, provider );
}
} );
this.providersToRemove.clear();
ongoingExtractOperation = false;
}

if( ls && extractedPower < amt )
{
final double req = amt - extractedPower;
final double newPower = localStorage.extractAEPower( req, mode, PowerMultiplier.ONE );

extractedPower += newPower;

if( newPower < req )
{
providers.remove( localStorage );
}
}

providers.removeIf( p -> providerToRemove.contains( p ) );
this.providerToRemove.clear();

final double result = Math.min( extractedPower, amt );

if( mode == Actionable.MODULATE )
Expand All @@ -370,15 +405,10 @@ public double injectProviderPower( double amt, final Actionable mode )
{
final double originalAmount = amt;

requestersToAdd.forEach( requester -> {
if( requester != localStorage )
{
requesters.put( 1, requester );
}
} );
requestersToAdd.clear();
this.requesters.addAll( requesterToAdd );
requesterToAdd.clear();

final Iterator<IAEPowerStorage> it = this.requesters.values().iterator();
final Iterator<IAEPowerStorage> it = this.requesters.iterator();

ongoingInjectOperation = true;
try
Expand All @@ -391,22 +421,17 @@ public double injectProviderPower( double amt, final Actionable mode )

if( amt > 0 && mode == Actionable.MODULATE )
{
if( node != localStorage )
it.remove();
it.remove();
}
}
} finally
{
requestersToRemove.forEach( requester -> {
if( requester != localStorage )
{
requesters.remove( 1, requester );
}
} );
this.requestersToRemove.clear();
ongoingInjectOperation = false;
}

requesters.removeIf( r -> requesterToRemove.contains( r ) );
this.requesterToRemove.clear();

final double overflow = Math.max( 0.0, amt );

if( mode == Actionable.MODULATE )
Expand All @@ -422,8 +447,8 @@ public double getProviderEnergyDemand( final double maxRequired )
{
double required = 0;

final Iterator<IAEPowerStorage> it = this.requesters.values().iterator();
while( required < maxRequired && it.hasNext() )
final Iterator<IAEPowerStorage> it = this.requesters.iterator();
while ( required < maxRequired && it.hasNext() )
{
final IAEPowerStorage node = it.next();
if( node.getPowerFlow() != AccessRestriction.READ )
Expand Down Expand Up @@ -484,11 +509,7 @@ public double injectPower( final double amt, final Actionable mode )
@Override
public double getStoredPower()
{
if( this.availableTicksSinceUpdate > 90 )
{
this.refreshPower();
}

this.refreshPower();
return Math.max( 0.0, this.globalAvailablePower );
}

Expand Down Expand Up @@ -561,8 +582,22 @@ public void removeNode( final IGridNode node, final IGridHost machine )
this.globalMaxPower -= ps.getAEMaxPower();
this.globalAvailablePower -= ps.getAECurrentPower();
}
this.providersToRemove.add( ps );
this.requestersToRemove.add( ps );
if( !ongoingExtractOperation )
{
removeProvider( ps );
}
else
{
this.providerToRemove.add( ps );
}
if( !ongoingInjectOperation )
{
removeRequester( ps );
}
else
{
this.requesterToRemove.add( ps );
}
}
}

Expand All @@ -578,6 +613,31 @@ public void removeNode( final IGridNode node, final IGridHost machine )
}
}

private void addRequester(IAEPowerStorage requester) {
Preconditions.checkState(!ongoingInjectOperation,
"Cannot modify energy requesters while energy is being injected.");
this.requesters.add(requester);
}

private void removeRequester(IAEPowerStorage requester) {
Preconditions.checkState(!ongoingInjectOperation,
"Cannot modify energy requesters while energy is being injected.");
this.requesters.remove(requester);
}

private void addProvider(IAEPowerStorage provider) {
Preconditions.checkState(!ongoingExtractOperation,
"Cannot modify energy providers while energy is being extracted.");
this.providers.add(provider);
}

private void removeProvider(IAEPowerStorage provider) {
Preconditions.checkState(!ongoingExtractOperation,
"Cannot modify energy providers while energy is being extracted.");
this.providers.remove(provider);
}


@Override
public void addNode( final IGridNode node, final IGridHost machine )
{
Expand Down Expand Up @@ -609,12 +669,26 @@ public void addNode( final IGridNode node, final IGridHost machine )
if( current > 0 && ps.getPowerFlow() != AccessRestriction.WRITE )
{
this.globalAvailablePower += current;
this.providersToAdd.add( ps );
if( !ongoingExtractOperation )
{
addProvider( ps );
}
else
{
this.providersToAdd.add( ps );
}
}

if( current < max && ps.getPowerFlow() != AccessRestriction.READ )
{
this.requestersToAdd.add( ps );
if( !ongoingInjectOperation )
{
addRequester( ps );
}
else
{
this.requesterToAdd.add( ps );
}
}
}
}
Expand Down Expand Up @@ -729,7 +803,7 @@ private void removeCurrentAEPower( double amount )
{
this.stored -= amount;

if( stored < MAX_BUFFER_STORAGE - 0.001 )
if( this.stored < MAX_BUFFER_STORAGE - 0.001 )
{
EnergyGridCache.this.myGrid.postEvent( new MENetworkPowerStorage( this, PowerEventType.REQUEST_POWER ) );
}
Expand All @@ -741,4 +815,4 @@ private void removeCurrentAEPower( double amount )
}
}
}
}
}
2 changes: 0 additions & 2 deletions src/main/java/appeng/me/cache/GridStorageCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,6 @@ public void cellUpdate( final MENetworkCellArrayUpdate ev )
}

tracker.applyChanges();

this.storageMonitors.forEach( ( channel, monitor ) -> monitor.forceUpdate() );
}

private <T extends IAEStack<T>, C extends IStorageChannel<T>> void postChangesToNetwork( final C chan, final int upOrDown, final IItemList<T> availableItems, final IActionSource src )
Expand Down
Loading

0 comments on commit 517bd77

Please sign in to comment.