feat: controller grid cache

This commit is contained in:
Timo Ley 2022-12-04 12:39:35 +01:00
parent 32af3b7f2c
commit 285094b2e0
5 changed files with 166 additions and 78 deletions

View File

@ -0,0 +1,11 @@
package appeng.api.networking;
public interface IControllerCache extends IGridCache {
boolean requiresChannels();
boolean canRun();
boolean hasConflict();
}

View File

@ -37,10 +37,5 @@ public enum ControllerState {
/**
* Controller rules not followed, lock up while booting.
*/
CONTROLLER_CONFLICT,
/**
* Controller is online and has infinite channels
*/
CONTROLLER_INFINITE
CONTROLLER_CONFLICT
}

View File

@ -30,6 +30,7 @@ import appeng.api.features.IRegistryContainer;
import appeng.api.features.IWirelessTermHandler;
import appeng.api.features.IWorldGen.WorldGenType;
import appeng.api.movable.IMovableRegistry;
import appeng.api.networking.IControllerCache;
import appeng.api.networking.IGridCacheRegistry;
import appeng.api.networking.IWirelessCache;
import appeng.api.networking.crafting.ICraftingGrid;
@ -530,6 +531,7 @@ public final class Registration {
gcr.registerGridCache(ISecurityGrid.class, SecurityCache.class);
gcr.registerGridCache(ICraftingGrid.class, CraftingGridCache.class);
gcr.registerGridCache(IWirelessCache.class, WirelessGridCache.class);
gcr.registerGridCache(IControllerCache.class, ControllerGridCache.class);
registries.externalStorage().addExternalStorageInterface(new AEExternalHandler());

View File

@ -0,0 +1,77 @@
package appeng.me.cache;
import java.util.HashSet;
import java.util.Set;
import appeng.api.networking.IControllerCache;
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridHost;
import appeng.api.networking.IGridNode;
import appeng.api.networking.IGridStorage;
import appeng.core.AEConfig;
import appeng.core.features.AEFeature;
import appeng.tile.legacy.TileLegacyController;
import appeng.tile.networking.TileController;
public class ControllerGridCache implements IControllerCache {
Set<TileLegacyController> controllers = new HashSet<>();
Set<TileController> cores = new HashSet<>();
public ControllerGridCache(final IGrid grid) {
}
@Override
public void onUpdateTick() {}
@Override
public void removeNode(IGridNode gridNode, IGridHost machine) {
if (machine instanceof TileLegacyController) {
controllers.remove(machine);
} else if (machine instanceof TileController) {
cores.remove(machine);
}
}
@Override
public void addNode(IGridNode gridNode, IGridHost machine) {
if (machine instanceof TileLegacyController) {
controllers.add((TileLegacyController) machine);
} else if (machine instanceof TileController) {
cores.add((TileController) machine);
}
}
@Override
public void onSplit(IGridStorage destinationStorage) {}
@Override
public void onJoin(IGridStorage sourceStorage) {}
@Override
public void populateGridStorage(IGridStorage destinationStorage) {}
@Override
public boolean requiresChannels() {
if (!AEConfig.instance.isFeatureEnabled(AEFeature.Channels)) {
return false;
}
if (AEConfig.instance.HardLegacyController) {
return controllers.isEmpty() || cores.size() < 68;
} else {
return controllers.isEmpty();
}
}
@Override
public boolean canRun() {
return !hasConflict() && (!AEConfig.instance.NeedController || !controllers.isEmpty() || !cores.isEmpty());
}
@Override
public boolean hasConflict() {
return controllers.size() > 1;
}
}

View File

@ -42,8 +42,6 @@ import net.minecraftforge.common.util.ForgeDirection;
public class PathGridCache implements IPathingGrid {
private final LinkedList<PathSegment> active = new LinkedList<PathSegment>();
private final Set<TileController> controllers = new HashSet<TileController>();
private final Set<TileLegacyController> legacyControllers
= new HashSet<TileLegacyController>();
private final Set<IGridNode> requireChannels = new HashSet<IGridNode>();
private final Set<IGridNode> blockDense = new HashSet<IGridNode>();
private final IGrid myGrid;
@ -76,64 +74,18 @@ public class PathGridCache implements IPathingGrid {
this.booting = true;
this.updateNetwork = false;
this.setChannelsInUse(0);
IControllerCache ccache = this.myGrid.getCache(IControllerCache.class);
if (this.controllerState == ControllerState.CONTROLLER_INFINITE) {
final int used = this.calculateRequiredChannels();
final int nodes = this.myGrid.getNodes().size();
this.ticksUntilReady = 20 + Math.max(0, nodes / 100 - 20);
this.setChannelsByBlocks(nodes * used);
this.setChannelPowerUsage(this.getChannelsByBlocks() / 128.0);
this.myGrid.getPivot().beginVisit(new AdHocChannelUpdater(used));
} else if (this.controllerState == ControllerState.NO_CONTROLLER) {
if (AEConfig.instance.NeedController) {
this.ticksUntilReady = 20;
this.myGrid.getPivot().beginVisit(new AdHocChannelUpdater(0));
} else {
final int requiredChannels = this.calculateRequiredChannels();
int used = requiredChannels;
if (requiredChannels > 8) {
used = 0;
}
final int nodes = this.myGrid.getNodes().size();
this.setChannelsInUse(used);
this.ticksUntilReady = 20 + Math.max(0, nodes / 100 - 20);
this.setChannelsByBlocks(nodes * used);
this.setChannelPowerUsage(this.getChannelsByBlocks() / 128.0);
this.myGrid.getPivot().beginVisit(new AdHocChannelUpdater(used));
}
if (!ccache.canRun()) {
this.updateOffline();
} else if (this.controllerState == ControllerState.CONTROLLER_CONFLICT) {
this.ticksUntilReady = 20;
this.myGrid.getPivot().beginVisit(new AdHocChannelUpdater(0));
} else {
final int nodes = this.myGrid.getNodes().size();
this.ticksUntilReady = 20 + Math.max(0, nodes / 100 - 20);
final HashSet<IPathItem> closedList = new HashSet<IPathItem>();
this.semiOpen = new HashSet<IPathItem>();
// myGrid.getPivot().beginVisit( new AdHocChannelUpdater( 0 )
// );
for (final IGridNode node :
this.myGrid.getMachines(TileController.class)) {
closedList.add((IPathItem) node);
for (final IGridConnection gcc : node.getConnections()) {
final GridConnection gc = (GridConnection) gcc;
if (!(gc.getOtherSide(node).getMachine() instanceof TileController
)) {
final List<IPathItem> open = new LinkedList<IPathItem>();
closedList.add(gc);
open.add(gc);
gc.setControllerRoute((GridNode) node, true);
this.active.add(
new PathSegment(this, open, this.semiOpen, closedList)
);
}
}
}
this.updateOffline();
} else if (!ccache.requiresChannels()) {
this.updateInfinite();
} else if (this.controllerState == ControllerState.NO_CONTROLLER) {
this.updateNoController();
} else if (this.controllerState == ControllerState.CONTROLLER_ONLINE) {
this.updateController();
}
}
@ -150,8 +102,7 @@ public class PathGridCache implements IPathingGrid {
this.ticksUntilReady--;
if (this.active.isEmpty() && this.ticksUntilReady <= 0) {
if (this.controllerState == ControllerState.CONTROLLER_ONLINE
|| this.controllerState == ControllerState.CONTROLLER_INFINITE) {
if (this.controllerState == ControllerState.CONTROLLER_ONLINE) {
final Iterator<TileController> controllerIterator
= this.controllers.iterator();
if (controllerIterator.hasNext()) {
@ -177,7 +128,6 @@ public class PathGridCache implements IPathingGrid {
this.controllers.remove(machine);
this.recalculateControllerNextTick = true;
} else if (machine instanceof TileLegacyController) {
this.legacyControllers.remove(machine);
this.recalculateControllerNextTick = true;
}
@ -200,7 +150,6 @@ public class PathGridCache implements IPathingGrid {
this.controllers.add((TileController) machine);
this.recalculateControllerNextTick = true;
} else if (machine instanceof TileLegacyController) {
this.legacyControllers.add((TileLegacyController) machine);
this.recalculateControllerNextTick = true;
}
@ -230,10 +179,10 @@ public class PathGridCache implements IPathingGrid {
this.recalculateControllerNextTick = false;
final ControllerState old = this.controllerState;
if (this.legacyControllers.size() > 1) {
IControllerCache ccache = this.myGrid.getCache(IControllerCache.class);
if (ccache.hasConflict()) {
this.controllerState = ControllerState.CONTROLLER_CONFLICT;
} else if (this.legacyControllers.size() == 1 && !AEConfig.instance.HardLegacyController) {
this.controllerState = ControllerState.CONTROLLER_INFINITE;
} else if (this.controllers.isEmpty()) {
this.controllerState = ControllerState.NO_CONTROLLER;
} else {
@ -250,13 +199,7 @@ public class PathGridCache implements IPathingGrid {
startingNode.beginVisit(cv);
if (cv.isValid() && cv.getFound() == this.controllers.size()) {
if (this.legacyControllers.size() == 1 && this.controllers.size() >= 68) {
this.controllerState = ControllerState.CONTROLLER_INFINITE;
} else if (AEConfig.instance.isFeatureEnabled(AEFeature.Channels)) {
this.controllerState = ControllerState.CONTROLLER_ONLINE;
} else {
this.controllerState = ControllerState.CONTROLLER_INFINITE;
}
this.controllerState = ControllerState.CONTROLLER_ONLINE;
} else {
this.controllerState = ControllerState.CONTROLLER_CONFLICT;
}
@ -388,4 +331,64 @@ public class PathGridCache implements IPathingGrid {
public void setChannelsInUse(final int channelsInUse) {
this.channelsInUse = channelsInUse;
}
public void updateInfinite() {
final int used = this.calculateRequiredChannels();
final int nodes = this.myGrid.getNodes().size();
this.ticksUntilReady = 20 + Math.max(0, nodes / 100 - 20);
this.setChannelsByBlocks(nodes * used);
this.setChannelPowerUsage(this.getChannelsByBlocks() / 128.0);
this.myGrid.getPivot().beginVisit(new AdHocChannelUpdater(used));
}
public void updateNoController() {
final int requiredChannels = this.calculateRequiredChannels();
int used = requiredChannels;
if (requiredChannels > 8) {
used = 0;
}
final int nodes = this.myGrid.getNodes().size();
this.setChannelsInUse(used);
this.ticksUntilReady = 20 + Math.max(0, nodes / 100 - 20);
this.setChannelsByBlocks(nodes * used);
this.setChannelPowerUsage(this.getChannelsByBlocks() / 128.0);
this.myGrid.getPivot().beginVisit(new AdHocChannelUpdater(used));
}
public void updateController() {
final int nodes = this.myGrid.getNodes().size();
this.ticksUntilReady = 20 + Math.max(0, nodes / 100 - 20);
final HashSet<IPathItem> closedList = new HashSet<IPathItem>();
this.semiOpen = new HashSet<IPathItem>();
// myGrid.getPivot().beginVisit( new AdHocChannelUpdater( 0 )
// );
for (final IGridNode node :
this.myGrid.getMachines(TileController.class)) {
closedList.add((IPathItem) node);
for (final IGridConnection gcc : node.getConnections()) {
final GridConnection gc = (GridConnection) gcc;
if (!(gc.getOtherSide(node).getMachine() instanceof TileController
)) {
final List<IPathItem> open = new LinkedList<IPathItem>();
closedList.add(gc);
open.add(gc);
gc.setControllerRoute((GridNode) node, true);
this.active.add(
new PathSegment(this, open, this.semiOpen, closedList)
);
}
}
}
}
public void updateOffline() {
this.ticksUntilReady = 20;
this.myGrid.getPivot().beginVisit(new AdHocChannelUpdater(0));
}
}