Skip to content

Commit

Permalink
feat: added system to select pathType when more is possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Uhutown committed Sep 15, 2024
1 parent 2708b25 commit 92d5e12
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.troblecodings.signals.core.TrainNumber;
import com.troblecodings.signals.enums.EnumGuiMode;
import com.troblecodings.signals.enums.LinkType;
import com.troblecodings.signals.enums.PathType;
import com.troblecodings.signals.enums.PathwayRequestResult;
import com.troblecodings.signals.enums.SignalBoxNetwork;
import com.troblecodings.signals.handler.SignalBoxHandler;
Expand All @@ -48,7 +49,7 @@ public class ContainerSignalBox extends ContainerBase implements UIClientSync, I
protected final Map<Point, List<MainSignalIdentifier>> greenSignals = new HashMap<>();
protected final Map<BlockPos, List<SubsidiaryState>> possibleSubsidiaries = new HashMap<>();
protected final Map<Point, Map<ModeSet, SubsidiaryEntry>> enabledSubsidiaryTypes = new HashMap<>();
protected final List<Map.Entry<Point, Point>> nextPathways = new ArrayList<>();
protected final Map<Map.Entry<Point, Point>, PathType> nextPathways = new HashMap<>();
protected final Map<BlockPos, List<Point>> validInConnections = new HashMap<>();
protected SignalBoxGrid grid;
private final Map<BlockPos, LinkType> propertiesForType = new HashMap<>();
Expand Down Expand Up @@ -87,11 +88,12 @@ public void sendAllDataToRemote() {
buffer.putBlockPos(pos);
buffer.putByte((byte) type.ordinal());
});
final List<Map.Entry<Point, Point>> nextPathways = grid.getNextPathways();
final Map<Map.Entry<Point, Point>, PathType> nextPathways = grid.getNextPathways();
buffer.putByte((byte) nextPathways.size());
nextPathways.forEach(entry -> {
nextPathways.forEach((entry, pathType) -> {
entry.getKey().writeNetwork(buffer);
entry.getValue().writeNetwork(buffer);
buffer.putEnumValue(pathType);
});
final Map<BlockPos, List<Point>> validInConnections = new HashMap<>();
positions.entrySet().stream().filter(entry -> entry.getValue().equals(LinkType.SIGNALBOX))
Expand Down Expand Up @@ -155,7 +157,8 @@ public void deserializeClient(final ReadBuffer buffer) {
for (int i = 0; i < nextPathwaySize; i++) {
final Point start = Point.of(buffer);
final Point end = Point.of(buffer);
nextPathways.add(Maps.immutableEntry(start, end));
final PathType type = buffer.getEnumValue(PathType.class);
nextPathways.put(Maps.immutableEntry(start, end), type);
}
final int validInConnectionsSize = buffer.getByteToUnsignedInt();
for (int i = 0; i < validInConnectionsSize; i++) {
Expand Down Expand Up @@ -211,7 +214,8 @@ public void deserializeClient(final ReadBuffer buffer) {
final PathwayRequestResult result = buffer.getEnumValue(PathwayRequestResult.class);
final Point start = Point.of(buffer);
final Point end = Point.of(buffer);
nextPathways.add(Maps.immutableEntry(start, end));
final PathType type = buffer.getEnumValue(PathType.class);
nextPathways.put(Maps.immutableEntry(start, end), type);
infoUpdates.accept(I18Wrapper.format("error." + result.getName()) + " - "
+ I18Wrapper.format("info.pathwaysaver"));
break;
Expand Down Expand Up @@ -336,16 +340,18 @@ public void deserializeServer(final ReadBuffer buffer) {
case REQUEST_PW: {
final Point start = Point.of(buffer);
final Point end = Point.of(buffer);
final PathwayRequestResult request = grid.requestWay(start, end);
final PathType type = buffer.getEnumValue(PathType.class);
final PathwayRequestResult request = grid.requestWay(start, end, type);
if (!request.isPass()) {
final SignalBoxNode endNode = grid.getNode(end);
if (request.canBeAddedToSaver() && !endNode.containsOutConnection()
&& grid.addNextPathway(start, end)) {
&& grid.addNextPathway(start, end, type)) {
final WriteBuffer sucess = new WriteBuffer();
sucess.putEnumValue(SignalBoxNetwork.ADDED_TO_SAVER);
sucess.putEnumValue(request);
start.writeNetwork(sucess);
end.writeNetwork(sucess);
sucess.putEnumValue(type);
OpenSignalsMain.network.sendTo(info.player, sucess);
break;
}
Expand Down
34 changes: 30 additions & 4 deletions src/main/java/com/troblecodings/signals/guis/GuiSignalBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import com.troblecodings.signals.enums.EnumGuiMode;
import com.troblecodings.signals.enums.EnumPathUsage;
import com.troblecodings.signals.enums.LinkType;
import com.troblecodings.signals.enums.PathType;
import com.troblecodings.signals.enums.PathwayRequestResult;
import com.troblecodings.signals.enums.ShowTypes;
import com.troblecodings.signals.enums.SignalBoxNetwork;
import com.troblecodings.signals.enums.SignalBoxPage;
Expand Down Expand Up @@ -294,7 +296,7 @@ private void tileNormal(final UIEntity tile, final UISignalBoxTile currentTile)
return;
}
if (currentTile.isValidEnd()) {
sendPWRequest(currentTile.getNode());
checkForMultiplePathTypes(lastTile.getNode(), currentTile.getNode());
this.resetTileSelection();
return;
}
Expand All @@ -303,6 +305,29 @@ private void tileNormal(final UIEntity tile, final UISignalBoxTile currentTile)
tile.add(new UIClickable(e -> openNodeShortcuts(currentTile.getNode(), e), 1));
}

private void checkForMultiplePathTypes(final SignalBoxNode start, final SignalBoxNode end) {
final List<PathType> possibleTypes = start.getPossibleTypes(end);
if (possibleTypes.isEmpty()) {
infoUpdate(I18Wrapper
.format("error." + PathwayRequestResult.NO_EQUAL_PATH_TYPE.getName()));
} else if (possibleTypes.size() == 1) {
sendPWRequest(lastTile.getPoint(), end.getPoint(), possibleTypes.get(0));
} else if (possibleTypes.size() > 1) {
push(GuiElements.createScreen(entity -> {
entity.add(GuiElements.createButton(I18Wrapper.format("btn.return"), e -> pop()));
entity.add(GuiElements.createSpacerV(10));
entity.add(GuiElements.createLabel(I18Wrapper.format("gui.signalbox.choosetypes"),
0xffffff));
entity.add(GuiElements.createSpacerV(10));
possibleTypes
.forEach(type -> entity.add(GuiElements.createButton(type.name(), e -> {
sendPWRequest(start.getPoint(), end.getPoint(), type);
pop();
})));
}));
}
}

private void resetSelection(final UIEntity entity) {
final UIEntity parent = entity.getParent();
parent.findRecursive(UIClickable.class).forEach(click -> click.setVisible(true));
Expand Down Expand Up @@ -681,13 +706,14 @@ private void disableBottomEntity() {
bottomEntity.getParent().update();
}

private void sendPWRequest(final SignalBoxNode currentNode) {
private void sendPWRequest(final Point start, final Point end, final PathType type) {
if (!allPacketsRecived)
return;
final WriteBuffer buffer = new WriteBuffer();
buffer.putEnumValue(SignalBoxNetwork.REQUEST_PW);
lastTile.getPoint().writeNetwork(buffer);
currentNode.getPoint().writeNetwork(buffer);
start.writeNetwork(buffer);
end.writeNetwork(buffer);
buffer.putEnumValue(type);
OpenSignalsMain.network.sendTo(info.player, buffer);
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/troblecodings/signals/guis/SidePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -865,14 +865,13 @@ private void addSavedPathsToUI() {
final UIScrollBox scrollbox = new UIScrollBox(UIBox.VBOX, 2);
list.add(scrollbox);

gui.container.nextPathways.forEach(entry -> {
gui.container.nextPathways.forEach((entry, pathType) -> {
final UIEntity layout = new UIEntity();
layout.setHeight(20);
layout.setInheritWidth(true);
layout.add(new UIBox(UIBox.HBOX, 2));
final UIEntity button = GuiElements
.createButton("Start: " + entry.getKey().toShortString() + ", End: "
+ entry.getValue().toShortString());
final UIEntity button = GuiElements.createButton(entry.getKey().toShortString()
+ " to " + entry.getValue().toShortString() + " as " + pathType.name());
layout.add(button);
layout.add(
GuiElements.createButton(I18Wrapper.format("info.usage.show"), 40, _u -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ private PathwayData requestInterSignalBoxPathway(final SignalBoxGrid grid) {
return;
}
final PathwayRequestResult endRequeset = SignalBoxUtil.requestPathway(endGrid,
otherStartPoint.get(), otherEndPoint.get());
otherStartPoint.get(), otherEndPoint.get(), PathType.NORMAL);
if (endRequeset.isPass()) {
returnResult.set(endRequeset.getPathwayData());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.troblecodings.signals.core.SubsidiaryState;
import com.troblecodings.signals.core.TrainNumber;
import com.troblecodings.signals.enums.EnumPathUsage;
import com.troblecodings.signals.enums.PathType;
import com.troblecodings.signals.enums.PathwayRequestResult;
import com.troblecodings.signals.enums.SignalBoxNetwork;
import com.troblecodings.signals.handler.SignalBoxHandler;
Expand All @@ -49,10 +50,11 @@ public class SignalBoxGrid implements INetworkSavable {
private static final String NEXT_PATHWAYS = "nextPathways";
private static final String START_POINT = "startPoint";
private static final String END_POINT = "endPoint";
private static final String PATH_TYPE = "pathType";

protected final Map<Point, SignalBoxPathway> startsToPath = new HashMap<>();
protected final Map<Point, SignalBoxPathway> endsToPath = new HashMap<>();
protected final List<Map.Entry<Point, Point>> nextPathways = new ArrayList<>();
protected final Map<Map.Entry<Point, Point>, PathType> nextPathways = new HashMap<>();
protected final Map<Point, SignalBoxNode> modeGrid = new HashMap<>();
protected final SignalBoxFactory factory;
protected SignalBoxTileEntity tile;
Expand Down Expand Up @@ -150,8 +152,8 @@ protected void updateToNet(final SignalBoxPathway pathway) {
OpenSignalsMain.network.sendTo(tile.get(0).getPlayer(), buffer);
}

public PathwayRequestResult requestWay(final Point p1, final Point p2) {
final PathwayRequestResult result = SignalBoxUtil.requestPathway(this, p1, p2);
public PathwayRequestResult requestWay(final Point p1, final Point p2, final PathType type) {
final PathwayRequestResult result = SignalBoxUtil.requestPathway(this, p1, p2, type);
if (!result.isPass())
return result;
final PathwayData data = result.getPathwayData();
Expand Down Expand Up @@ -276,47 +278,52 @@ private void tryReset(final List<SignalBoxPathway> pathways, final BlockPos pos)
tryNextPathways();
}

private final List<Map.Entry<Point, Point>> toAdd = new ArrayList<>();
private final Map<Map.Entry<Point, Point>, PathType> toAdd = new HashMap<>();
private boolean executingForEach = false;

private void tryNextPathways() {
executingForEach = true;
nextPathways.removeIf(entry -> {
final PathwayRequestResult request = requestWay(entry.getKey(), entry.getValue());
final Map<Map.Entry<Point, Point>, PathType> toRemove = new HashMap<>();
nextPathways.forEach((entry, type) -> {
final PathwayRequestResult request = requestWay(entry.getKey(), entry.getValue(), type);
if (request == PathwayRequestResult.PASS) {
if (tile == null || !tile.isBlocked())
return true;
if (tile == null || !tile.isBlocked()) {
toRemove.put(entry, type);
return;
}
final WriteBuffer buffer = new WriteBuffer();
buffer.putEnumValue(SignalBoxNetwork.REMOVE_SAVEDPW);
entry.getKey().writeNetwork(buffer);
entry.getValue().writeNetwork(buffer);
OpenSignalsMain.network.sendTo(tile.get(0).getPlayer(), buffer);
return true;
toRemove.put(entry, type);
return;
}
return false;
});
executingForEach = false;
toAdd.forEach(nextPathways::add);
toRemove.keySet().forEach(nextPathways::remove);
toRemove.clear();
toAdd.forEach(nextPathways::put);
toAdd.clear();
if (startsToPath.isEmpty())
nextPathways.clear();
}

public List<Map.Entry<Point, Point>> getNextPathways() {
return ImmutableList.copyOf(nextPathways);
public Map<Map.Entry<Point, Point>, PathType> getNextPathways() {
return ImmutableMap.copyOf(nextPathways);
}

public boolean addNextPathway(final Point start, final Point end) {
public boolean addNextPathway(final Point start, final Point end, final PathType type) {
final Map.Entry<Point, Point> entry = Maps.immutableEntry(start, end);
if (!nextPathways.contains(entry)) {
if (!nextPathways.containsKey(entry)) {
final SignalBoxPathway pw = startsToPath.get(start);
if (pw != null && pw.isInterSignalBoxPathway()) {
return false;
}
if (executingForEach) {
toAdd.add(entry);
toAdd.put(entry, type);
} else {
nextPathways.add(entry);
nextPathways.put(entry, type);
}
return true;
}
Expand Down Expand Up @@ -369,14 +376,15 @@ public void writePathways(final NBTWrapper tag) {
pathway.write(path);
return path;
})::iterator);
tag.putList(NEXT_PATHWAYS, nextPathways.stream().map(entry -> {
tag.putList(NEXT_PATHWAYS, nextPathways.entrySet().stream().map(entry -> {
final NBTWrapper wrapper = new NBTWrapper();
final NBTWrapper start = new NBTWrapper();
entry.getKey().write(start);
entry.getKey().getKey().write(start);
final NBTWrapper end = new NBTWrapper();
entry.getValue().write(end);
entry.getKey().getValue().write(end);
wrapper.putWrapper(START_POINT, start);
wrapper.putWrapper(END_POINT, end);
wrapper.putString(PATH_TYPE, entry.getValue().name());
return wrapper;
})::iterator);
}
Expand Down Expand Up @@ -426,7 +434,13 @@ public void readPathways(final NBTWrapper tag) {
start.read(comp.getWrapper(START_POINT));
final Point end = new Point();
end.read(comp.getWrapper(END_POINT));
nextPathways.add(Maps.immutableEntry(start, end));
if (comp.contains(PATH_TYPE)) {
nextPathways.put(Maps.immutableEntry(start, end),
PathType.valueOf(comp.getString(PATH_TYPE)));
} else {
nextPathways.put(Maps.immutableEntry(start, end),
SignalBoxUtil.getPathTypeFrom(modeGrid.get(start), modeGrid.get(end)));
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,10 @@ public Optional<PathOptionEntry> getOption(final Optional<ModeSet> mode) {
return mode.flatMap(this::getOption);
}

public PathType getPathType(final SignalBoxNode other) {
public List<PathType> getPossibleTypes(final SignalBoxNode other) {
final List<PathType> possibleTypes = new ArrayList<>();
if (other == null || other.getPoint().equals(this.getPoint()))
return PathType.NONE;
return possibleTypes;
final Set<EnumGuiMode> thisMode = this.possibleModes.keySet().stream()
.map(mode -> mode.mode).collect(Collectors.toSet());

Expand All @@ -233,9 +234,9 @@ public PathType getPathType(final SignalBoxNode other) {
final boolean otherContains = Arrays.stream(type.getModes())
.anyMatch(otherMode::contains);
if (thisContains && otherContains)
return type;
possibleTypes.add(type);
}
return PathType.NONE;
return possibleTypes;
}

public PathwayRequestResult canMakePath(final Path path, final PathType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ protected boolean loadTileAndExecute(final BlockPos tilePos,

public void checkReRequest() {
if (isAutoPathway) {
grid.requestWay(originalFirstPoint, getLastPoint());
grid.requestWay(originalFirstPoint, getLastPoint(), getPathType());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,11 @@ public Point getPoint() {
}

public static PathwayRequestResult requestPathway(final SignalBoxGrid grid, final Point p1,
final Point p2) {
final Point p2, final PathType pathType) {
final Map<Point, SignalBoxNode> modeGrid = grid.modeGrid;
if (!modeGrid.containsKey(p1) || !modeGrid.containsKey(p2))
return PathwayRequestResult.NOT_IN_GRID;
final SignalBoxNode lastNode = modeGrid.get(p2);
final SignalBoxNode firstNode = modeGrid.get(p1);
final PathType pathType = firstNode.getPathType(lastNode);
if (pathType.equals(PathType.NONE))
return PathwayRequestResult.NO_EQUAL_PATH_TYPE;

Expand Down Expand Up @@ -209,4 +207,13 @@ public static int getDefaultCosts(final ModeSet mode) {
}
}

public static PathType getPathTypeFrom(final SignalBoxNode start, final SignalBoxNode end) {
final List<PathType> possilbeTypes = start.getPossibleTypes(end);
if (!possilbeTypes.isEmpty()) {
return possilbeTypes.get(0);
} else {
return PathType.NONE;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import com.troblecodings.linkableapi.ILinkableTile;
import com.troblecodings.signals.OpenSignalsMain;
import com.troblecodings.signals.blocks.SignalBox;
import com.troblecodings.signals.enums.PathType;
import com.troblecodings.signals.enums.PathwayRequestResult;
import com.troblecodings.signals.signalbox.Point;
import com.troblecodings.signals.signalbox.SignalBoxGrid;
import com.troblecodings.signals.signalbox.SignalBoxTileEntity;
import com.troblecodings.signals.signalbox.SignalBoxUtil;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
Expand Down Expand Up @@ -62,10 +64,14 @@ public void saveWrapper(final NBTWrapper wrapper) {
public void requestPathway() {
loadChunkAndGetTile(SignalBoxTileEntity.class, world, linkedSignalBox, (tile, _u) -> {
final SignalBoxGrid grid = tile.getSignalBoxGrid();
final PathwayRequestResult result = grid.requestWay(pathway.getKey(),
pathway.getValue());
if (!result.isPass() && result.canBeAddedToSaver() && addPWToSaver) {
grid.addNextPathway(pathway.getKey(), pathway.getValue());
final PathType type = SignalBoxUtil.getPathTypeFrom(grid.getNode(pathway.getKey()),
grid.getNode(pathway.getValue()));
if (!type.equals(PathType.NONE)) {
final PathwayRequestResult result = grid.requestWay(pathway.getKey(),
pathway.getValue(), type);
if (!result.isPass() && result.canBeAddedToSaver() && addPWToSaver) {
grid.addNextPathway(pathway.getKey(), pathway.getValue(), type);
}
}
});

Expand Down

0 comments on commit 92d5e12

Please sign in to comment.