feat: energy improvements

This commit is contained in:
Timo Ley 2023-01-29 11:23:41 +01:00
parent 58bf3f3acd
commit 447383f3ce
6 changed files with 57 additions and 22 deletions

View file

@ -2,6 +2,7 @@ package appeng.api.networking;
import java.util.Set;
import appeng.api.config.Actionable;
import appeng.api.networking.crafting.ICraftingCPU;
public interface IControllerCache extends IGridCache {
@ -17,5 +18,9 @@ public interface IControllerCache extends IGridCache {
IGridHost getController();
Set<ICraftingCPU> getCPUs();
double injectPower(double amt, Actionable mode);
double getEnergyDemand();
}

View file

@ -1,7 +1,6 @@
package appeng.me.cache;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import appeng.api.config.Actionable;
@ -11,7 +10,7 @@ import appeng.api.networking.IGridHost;
import appeng.api.networking.IGridNode;
import appeng.api.networking.IGridStorage;
import appeng.api.networking.crafting.ICraftingCPU;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.networking.energy.IAEPowerStorage;
import appeng.core.AEConfig;
import appeng.core.features.AEFeature;
import appeng.me.cluster.implementations.InternalCraftingCPU;
@ -104,5 +103,23 @@ public class ControllerGridCache implements IControllerCache {
}
return cpus;
}
@Override
public double injectPower(double amt, Actionable mode) {
IGridHost controller = getController();
if (controller instanceof IAEPowerStorage) {
return ((IAEPowerStorage)controller).injectAEPower(amt, mode);
}
return amt;
}
@Override
public double getEnergyDemand() {
IGridHost controller = getController();
if (controller instanceof IAEPowerStorage) {
return ((IAEPowerStorage)controller).getAEMaxPower() - ((IAEPowerStorage)controller).getAECurrentPower();
}
return 0;
}
}

View file

@ -269,10 +269,13 @@ public class EnergyGridCache implements IEnergyGrid {
return 0;
}
IControllerCache cg = this.myGrid.getCache(IControllerCache.class);
final double ignore = this.extra;
amt += this.extra;
if (mode == Actionable.SIMULATE) {
amt = cg.injectPower(amt, mode);
final Iterator<IAEPowerStorage> it = this.requesters.iterator();
while (amt > 0 && it.hasNext()) {
final IAEPowerStorage node = it.next();
@ -287,6 +290,7 @@ public class EnergyGridCache implements IEnergyGrid {
this.tickInjectionPerTick += amt - ignore;
// totalInjectionPastTicks[0] += i;
amt = cg.injectPower(amt, mode);
while (amt > 0 && !this.requesters.isEmpty()) {
final IAEPowerStorage node = this.getFirstRequester();

View file

@ -9,6 +9,8 @@ import appeng.api.events.LocatableEventAnnounce;
import appeng.api.events.LocatableEventAnnounce.LocatableEvent;
import appeng.api.features.ILocatable;
import appeng.api.networking.events.MENetworkCraftingCpuChange;
import appeng.api.networking.events.MENetworkPowerStorage;
import appeng.api.networking.events.MENetworkPowerStorage.PowerEventType;
import appeng.me.GridAccessException;
import appeng.me.cluster.implementations.InternalCraftingCPU;
import appeng.tile.TileEvent;
@ -58,6 +60,7 @@ public class TileLegacyController extends AENetworkPowerTile implements ILocatab
if (ticksSinceRefresh % 10 == 0) {
ticksSinceRefresh = 0;
updatePowerLevel();
updatePower();
updateCPUs();
}
}
@ -107,6 +110,16 @@ public class TileLegacyController extends AENetworkPowerTile implements ILocatab
return ret;
}
public void updatePower() {
if (this.getAECurrentPower() > 10.0) {
try {
this.getProxy().getGrid().postEvent(new MENetworkPowerStorage(this, PowerEventType.PROVIDE_POWER));
} catch (GridAccessException e) {
// :P
}
}
}
public void updatePowerLevel() {
this.powerLevel = (int
) Math.ceil((5.0 * this.getInternalCurrentPower() / this.getInternalMaxPower()));

View file

@ -1,16 +1,15 @@
package appeng.tile.legacy;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import appeng.api.config.Actionable;
import appeng.api.networking.GridFlags;
import appeng.api.networking.IControllerCache;
import appeng.api.networking.IGridHost;
import appeng.api.networking.IGridNode;
import appeng.api.networking.energy.IAEPowerStorage;
import appeng.api.networking.energy.IEnergyGrid;
import appeng.api.networking.energy.IEnergyGridProvider;
import appeng.api.util.AECableType;
@ -56,17 +55,24 @@ public class TilePowerRelay extends AEBasePoweredTile implements IGridProxyable,
}
public void onUpdatePower() {
final double split = this.getAECurrentPower() / 6; // TODO: only care for sides, which demand energy
double current = 0.0;
List<ForgeDirection> demanding = new ArrayList<>();
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
try {
IEnergyGrid eg = this.getProxyForSide(dir).getEnergy();
if (eg.getEnergyDemand(1000.0) > 1.0) {
demanding.add(dir);
}
} catch (GridAccessException e) {
// :P
}
}
if (demanding.isEmpty()) return;
final double split = this.getAECurrentPower() / demanding.size();
double current = 0.0;
for (ForgeDirection dir : demanding) {
double leftover = split;
try {
IEnergyGrid eg = this.getProxyForSide(dir).getEnergy();
IControllerCache cg = this.getProxyForSide(dir).getGrid().getCache(IControllerCache.class);
IGridHost controller = cg.getController();
if (controller instanceof IAEPowerStorage) {
leftover = ((IAEPowerStorage)controller).injectAEPower(leftover, Actionable.MODULATE);
}
double demand = eg.getEnergyDemand(leftover);
leftover = eg.injectPower(Math.min(leftover, demand), Actionable.MODULATE);
current += leftover;

View file

@ -19,9 +19,6 @@
package appeng.tile.networking;
import appeng.api.config.Actionable;
import appeng.api.networking.IControllerCache;
import appeng.api.networking.IGridHost;
import appeng.api.networking.energy.IAEPowerStorage;
import appeng.api.networking.energy.IEnergyGrid;
import appeng.api.util.AECableType;
import appeng.me.GridAccessException;
@ -76,14 +73,7 @@ public class TileEnergyAcceptor extends AENetworkPowerTile {
protected double funnelPowerIntoStorage(final double power, final Actionable mode) {
try {
final IEnergyGrid grid = this.getProxy().getEnergy();
final IControllerCache cgc = this.getProxy().getGrid().getCache(IControllerCache.class);
double leftOver = power;
if (cgc.hasController()) {
IGridHost controller = cgc.getController();
if (controller instanceof IAEPowerStorage) {
leftOver = ((IAEPowerStorage)controller).injectAEPower(leftOver, mode);
}
}
leftOver = grid.injectPower(leftOver, mode);
if (mode == Actionable.SIMULATE) {
return leftOver;