Skip to content

Commit

Permalink
feat: added system for presignal repeater
Browse files Browse the repository at this point in the history
  • Loading branch information
Uhutown committed Sep 25, 2023
1 parent ab05d06 commit 969eba4
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public enum SignalBoxNetwork {
SEND_POS_ENTRY, SEND_INT_ENTRY, REMOVE_ENTRY, REQUEST_PW, REMOVE_POS, RESET_PW, SEND_GRID,
SEND_PW_UPDATE, RESET_ALL_PW, SEND_CHANGED_MODES, REQUEST_LINKED_POS, NO_PW_FOUND,
REQUEST_SUBSIDIARY, SEND_ZS2_ENTRY, UPDATE_RS_OUTPUT, NO_OUTPUT_UPDATE, OUTPUT_UPDATE,
RESET_SUBSIDIARY, SET_AUTO_POINT, SEND_NAME;
RESET_SUBSIDIARY, SET_AUTO_POINT, SEND_NAME, SEND_SIGNAL_REPEATER;

}
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@ public void deserializeServer(final ReadBuffer buffer) {
node.setCustomText(buffer.getString());
break;
}
case SEND_SIGNAL_REPEATER: {
final Point point = Point.of(buffer);
final ModeSet modeSet = ModeSet.of(buffer);
final boolean state = buffer.getBoolean();
final SignalBoxNode node = tile.getSignalBoxGrid().getNode(point);
final Optional<PathOptionEntry> option = node.getOption(modeSet);
if (option.isPresent()) {
option.get().setEntry(PathEntryType.SIGNAL_REPEATER, state);
} else {
node.addAndSetEntry(modeSet, PathEntryType.SIGNAL_REPEATER, state);
}
}
default:
break;
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/troblecodings/signals/guis/GuiSignalBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ private void setupModeSettings(final UIEntity parent, final EnumGuiMode mode,
case VP:
selectLink(parent, node, option, entrySet, LinkType.SIGNAL, PathEntryType.SIGNAL,
mode, rotation);
final Optional<Boolean> opt = option.getEntry(PathEntryType.SIGNAL_REPEATER);
parent.add(
GuiElements.createBoolElement(BoolIntegerables.of("signal_repeater"), e -> {
final boolean state = e == 1 ? true : false;
sendSignalRepeater(node.getPoint(), modeSet, state);
option.setEntry(PathEntryType.SIGNAL_REPEATER, state);
}, opt.isPresent() && opt.get() ? 1 : 0));
break;
case HP: {
parent.add(GuiElements.createBoolElement(BoolIntegerables.of("auto_pathway"), e -> {
Expand Down Expand Up @@ -934,6 +941,17 @@ private void sendName(final Point point, final String name) {
OpenSignalsMain.network.sendTo(info.player, buffer);
}

private void sendSignalRepeater(final Point point, final ModeSet mode, final boolean state) {
if (!allPacketsRecived)
return;
final WriteBuffer buffer = new WriteBuffer();
buffer.putEnumValue(SignalBoxNetwork.SEND_SIGNAL_REPEATER);
point.writeNetwork(buffer);
mode.writeNetwork(buffer);
buffer.putBoolean(state);
OpenSignalsMain.network.sendTo(info.player, buffer);
}

private void reset() {
lowerEntity.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class FunctionParsingInfo {
PARAMETER_PARSER.put(ValuePack.class, FunctionParsingInfo::getPredicate);
PARAMETER_PARSER.put(StringInteger.class, FunctionParsingInfo::getStringInt);
PARAMETER_PARSER.put(String.class, FunctionParsingInfo::getString);
PARAMETER_PARSER.put(Boolean.class, FunctionParsingInfo::getBoolean);
}

public String argument;
Expand Down Expand Up @@ -115,6 +116,10 @@ public Object getString() {
return argument;
}

public Object getBoolean() {
return Boolean.valueOf(argument);
}

public Map<String, MethodInfo> getTable() {
return translationTable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ private LogicParser() {
TRANSLATION_TABLE.put("zs2value", new MethodInfo(String.class, "zs2value",
obj -> PredicateHolder.zs2Value((String) obj[0]), String.class));

TRANSLATION_TABLE.put("signalrepeater", new MethodInfo(Boolean.class, "signalrepeater",
obj -> PredicateHolder.signalRepeater((boolean) obj[0]), Boolean.class));

TRANSLATION_TABLE.forEach((name, info) -> UNIVERSAL_TRANSLATION_TABLE.put(name,
new MethodInfo(Map.class, name, objects -> {
final Predicate original = info.blockState.apply(objects);
Expand Down
36 changes: 12 additions & 24 deletions src/main/java/com/troblecodings/signals/parser/PredicateHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,46 +63,34 @@ public static Predicate<Integer> speed(final StringInteger stringInt) {
final int speed = stringInt.integer;
switch (values) {
case GREATER:
return s -> {
return s > speed;
};
return s -> s > speed;

case GREATEREQUALS:
return s -> {
return s >= speed;
};
return s -> s >= speed;

case EQUALS:
return s -> {
return s == speed;
};
return s -> s == speed;

case SMALLEREQUALS:
return s -> {
return s <= speed;
};
return s -> s <= speed;

case SMALLER:
return s -> {
return s < speed;
};
return s -> s < speed;

case UNEQUALS:
return s -> {
return s != speed;
};
return s -> s != speed;

default:
return s -> {
return s == speed;
};
return s -> s == speed;
}

}

public static Predicate<String> zs2Value(final String value) {
return s -> {
return s.equalsIgnoreCase(value);
};
return s -> s.equalsIgnoreCase(value);
}

public static Predicate<Boolean> signalRepeater(final boolean state) {
return s -> s.booleanValue() == state;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.troblecodings.signals.signalbox;

import java.util.Objects;

import net.minecraft.core.BlockPos;

public class MainSignalIdentifier {

public final Point point;
public final ModeSet mode;
public final BlockPos pos;

public MainSignalIdentifier(Point point, ModeSet mode, BlockPos pos) {
this.point = point;
this.mode = mode;
this.pos = pos;
}

@Override
public int hashCode() {
return Objects.hash(mode, point, pos);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MainSignalIdentifier other = (MainSignalIdentifier) obj;
return Objects.equals(mode, other.mode) && Objects.equals(point, other.point)
&& Objects.equals(pos, other.pos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.troblecodings.signals.signalbox;

import java.util.Objects;

import net.minecraft.core.BlockPos;

public class OtherSignalIdentifier extends MainSignalIdentifier {

public final boolean isRepeater;

public OtherSignalIdentifier(Point point, ModeSet mode, BlockPos pos,
final boolean isRepeater) {
super(point, mode, pos);
this.isRepeater = isRepeater;
}

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + Objects.hash(isRepeater);
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
OtherSignalIdentifier other = (OtherSignalIdentifier) obj;
return isRepeater == other.isRepeater && super.equals(obj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private void updateToNet(final SignalBoxPathway pathway) {
return;
final List<SignalBoxNode> nodes = pathway.getListOfNodes();
final WriteBuffer buffer = new WriteBuffer();
buffer.putByte((byte) SignalBoxNetwork.SEND_PW_UPDATE.ordinal());
buffer.putEnumValue(SignalBoxNetwork.SEND_PW_UPDATE);
buffer.putInt(nodes.size());
nodes.forEach(node -> {
node.getPoint().writeNetwork(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public class SignalBoxPathway {
private Point lastPoint = new Point();
private int speed = -1;
private String zs2Value = "";
private Optional<Entry<BlockPos, BlockPos>> signalPositions = Optional.empty();
private Optional<BlockPos> lastSignal = Optional.empty();
private ImmutableList<BlockPos> distantSignalPositions = ImmutableList.of();
private Optional<Entry<MainSignalIdentifier, MainSignalIdentifier>> signalPositions = Optional
.empty();
private Optional<MainSignalIdentifier> lastSignal = Optional.empty();
private ImmutableList<OtherSignalIdentifier> distantSignalPositions = ImmutableList.of();
private Map<Point, SignalBoxNode> modeGrid = null;
private boolean emptyOrBroken = false;
private Level world;
Expand Down Expand Up @@ -86,7 +87,7 @@ public SignalBoxPathway(final Map<Point, SignalBoxNode> modeGrid,
private void initalize() {
final AtomicInteger atomic = new AtomicInteger(Integer.MAX_VALUE);
final AtomicReference<Byte> zs2Value = new AtomicReference<>((byte) -1);
final Builder<BlockPos> distantPosBuilder = ImmutableList.builder();
final Builder<OtherSignalIdentifier> distantPosBuilder = ImmutableList.builder();
mapOfBlockingPositions.clear();
mapOfResetPositions.clear();
foreachEntry((optionEntry, node) -> {
Expand All @@ -102,19 +103,25 @@ private void initalize() {
final Rotation rotation = SignalBoxUtil
.getRotationFromDelta(node.getPoint().delta(path.point1));
for (final EnumGuiMode mode : Arrays.asList(EnumGuiMode.VP, EnumGuiMode.RS)) {
node.getOption(new ModeSet(mode, rotation))
.ifPresent(option -> option.getEntry(PathEntryType.SIGNAL)
.ifPresent(position -> distantPosBuilder.add(position)));
final ModeSet modeSet = new ModeSet(mode, rotation);
node.getOption(modeSet).ifPresent(
option -> option.getEntry(PathEntryType.SIGNAL).ifPresent(position -> {
final Optional<Boolean> repeaterOption = option
.getEntry(PathEntryType.SIGNAL_REPEATER);
distantPosBuilder.add(
new OtherSignalIdentifier(node.getPoint(), modeSet, position,
repeaterOption.isPresent() && repeaterOption.get()));
}));
}
}, null);
this.distantSignalPositions = distantPosBuilder.build();
final SignalBoxNode firstNode = this.listOfNodes.get(this.listOfNodes.size() - 1);
this.firstPoint = firstNode.getPoint();
final BlockPos firstPos = makeFromNext(type, firstNode,
final MainSignalIdentifier firstPos = makeFromNext(type, firstNode,
this.listOfNodes.get(this.listOfNodes.size() - 2), Rotation.NONE);
final SignalBoxNode lastNode = this.listOfNodes.get(0);
this.lastPoint = lastNode.getPoint();
final BlockPos lastPos = makeFromNext(type, lastNode, this.listOfNodes.get(1),
final MainSignalIdentifier lastPos = makeFromNext(type, lastNode, this.listOfNodes.get(1),
Rotation.CLOCKWISE_180);
if (lastPos != null) {
lastSignal = Optional.of(lastPos);
Expand All @@ -128,15 +135,16 @@ private void initalize() {
this.zs2Value = JsonEnumHolder.ZS32.getObjFromID(Byte.toUnsignedInt(zs2Value.get()));
}

private BlockPos makeFromNext(final PathType type, final SignalBoxNode first,
private MainSignalIdentifier makeFromNext(final PathType type, final SignalBoxNode first,
final SignalBoxNode next, final Rotation pRotation) {
final Point delta = first.getPoint().delta(next.getPoint());
final Rotation rotation = SignalBoxUtil.getRotationFromDelta(delta).getRotated(pRotation);
for (final EnumGuiMode mode : type.getModes()) {
final BlockPos possiblePosition = first.getOption(new ModeSet(mode, rotation))
final ModeSet modeSet = new ModeSet(mode, rotation);
final BlockPos possiblePosition = first.getOption(modeSet)
.flatMap(option -> option.getEntry(PathEntryType.SIGNAL)).orElse(null);
if (possiblePosition != null)
return possiblePosition;
return new MainSignalIdentifier(first.getPoint(), modeSet, possiblePosition);
}
return null;
}
Expand Down Expand Up @@ -239,26 +247,26 @@ public void updatePathwaySignals() {
final PosIdentifier identifier = new PosIdentifier(tilePos, world);
SignalStateInfo lastInfo = null;
if (lastSignal.isPresent()) {
final Signal nextSignal = SignalBoxHandler.getSignal(identifier, lastSignal.get());
final Signal nextSignal = SignalBoxHandler.getSignal(identifier, lastSignal.get().pos);
if (nextSignal != null)
lastInfo = new SignalStateInfo(world, lastSignal.get(), nextSignal);
lastInfo = new SignalStateInfo(world, lastSignal.get().pos, nextSignal);
}
final SignalStateInfo lastSignalInfo = lastInfo;
this.signalPositions.ifPresent(entry -> {
if (isBlocked)
return;
final Signal first = SignalBoxHandler.getSignal(identifier, entry.getKey());
final Signal first = SignalBoxHandler.getSignal(identifier, entry.getKey().pos);
if (first == null)
return;
final SignalStateInfo firstInfo = new SignalStateInfo(world, entry.getKey(), first);
final SignalStateInfo firstInfo = new SignalStateInfo(world, entry.getKey().pos, first);
SignalConfig.change(new ConfigInfo(firstInfo, lastSignalInfo, speed, zs2Value, type));
});
distantSignalPositions.forEach(position -> {
final Signal current = SignalBoxHandler.getSignal(identifier, position);
final Signal current = SignalBoxHandler.getSignal(identifier, position.pos);
if (current == null)
return;
SignalConfig.change(new ConfigInfo(new SignalStateInfo(world, position, current),
lastSignalInfo, speed, zs2Value, type));
SignalConfig.change(new ConfigInfo(new SignalStateInfo(world, position.pos, current),
lastSignalInfo, speed, zs2Value, type, position.isRepeater));
});
}

Expand All @@ -269,20 +277,20 @@ public void resetPathway() {
private void resetFirstSignal() {
this.signalPositions.ifPresent(entry -> {
final Signal current = SignalBoxHandler.getSignal(new PosIdentifier(tilePos, world),
entry.getKey());
entry.getKey().pos);
if (current == null)
return;
SignalConfig.reset(new SignalStateInfo(world, entry.getKey(), current));
SignalConfig.reset(new SignalStateInfo(world, entry.getKey().pos, current));
});
}

private void resetOther() {
distantSignalPositions.forEach(position -> {
final Signal current = SignalBoxHandler.getSignal(new PosIdentifier(tilePos, world),
position);
position.pos);
if (current == null)
return;
SignalConfig.reset(new SignalStateInfo(world, position, current));
SignalConfig.reset(new SignalStateInfo(world, position.pos, current));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ public class ConfigInfo {
public final int speed;
public final String zs2Value;
public final PathType type;
public final boolean isSignalRepeater;

public ConfigInfo(final SignalStateInfo currentinfo, final SignalStateInfo nextinfo,
final int speed, final String zs2Value, final PathType type) {
this(currentinfo, nextinfo, speed, zs2Value, type, false);
}

public ConfigInfo(final SignalStateInfo currentinfo, final SignalStateInfo nextinfo,
final int speed, final String zs2Value, final PathType type,
final boolean isSignalRepeater) {
this.currentinfo = currentinfo;
this.nextinfo = nextinfo;
this.speed = speed;
this.zs2Value = zs2Value;
this.type = type;
this.isSignalRepeater = isSignalRepeater;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private static void changeIfPresent(final List<ConfigProperty> values, final Con
}
object.put(Integer.class, info.speed);
object.put(String.class, info.zs2Value);
object.put(Boolean.class, info.isSignalRepeater);
final Map<SEProperty, String> propertiesToSet = new HashMap<>();
values.forEach(property -> {
if (property.test(object)) {
Expand Down
Loading

0 comments on commit 969eba4

Please sign in to comment.