Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ComputerCraft integrations to more devices #6849

Open
wants to merge 7 commits into
base: mc1.18/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve ComputerCraft integration of Train Stations
- Emit CC events train_imminent, train_arrival and train_departure when
  these occur on the station
- Add CC canTrainReach() function to test if the train stopped at the
  station can reach a (schedule-compatible) destination
- Add CC distanceTo() function to get the shortest distance to a
  (schedule-compatible) destination at the moment, using the train
  stopped in the station
  • Loading branch information
ElementW committed Sep 5, 2024
commit 9c8c891c42798ee5c02e9f5c39a693d81e13c138
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.simibubi.create.compat.computercraft.events;

import org.jetbrains.annotations.NotNull;


import com.simibubi.create.content.trains.entity.Train;

public class StationTrainPresenceEvent implements ComputerEvent {

public enum Type {
IMMINENT("train_imminent"),
ARRIVAL("train_arrival"),
DEPARTURE("train_departure");

public final String name;

Type(String name) {
this.name = name;
}
}

public Type type;
public @NotNull Train train;

public StationTrainPresenceEvent(Type type, @NotNull Train train) {
this.type = type;
this.train = train;
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.simibubi.create.compat.computercraft.implementation.peripherals;

import java.util.ArrayList;
import java.util.Map;

import javax.annotation.Nullable;
import java.util.regex.PatternSyntaxException;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;


import com.simibubi.create.AllPackets;
import com.simibubi.create.foundation.utility.Pair;
import com.simibubi.create.compat.computercraft.events.ComputerEvent;
import com.simibubi.create.compat.computercraft.events.StationTrainPresenceEvent;
import com.simibubi.create.compat.computercraft.implementation.CreateLuaTable;
import com.simibubi.create.content.trains.entity.Train;
import com.simibubi.create.content.trains.graph.DiscoveredPath;
import com.simibubi.create.content.trains.graph.EdgePointType;
import com.simibubi.create.content.trains.schedule.Schedule;
import com.simibubi.create.content.trains.schedule.destination.DestinationInstruction;
import com.simibubi.create.content.trains.station.GlobalStation;
import com.simibubi.create.content.trains.station.StationBlockEntity;
import com.simibubi.create.content.trains.station.TrainEditPacket;
Expand All @@ -19,6 +27,7 @@
import dan200.computercraft.api.lua.IArguments;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.MethodResult;
import net.minecraft.nbt.ByteTag;
import net.minecraft.nbt.CollectionTag;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -164,6 +173,46 @@ public final void setSchedule(IArguments arguments) throws LuaException {
train.runtime.setSchedule(schedule, autoSchedule);
}

/**
* @return Path (if available) and boolean indicating if destination exists at all.
*/
private Pair<@Nullable DiscoveredPath, @NotNull Boolean> findPath(String destinationFilter) throws LuaException {
Train train = getTrainOrThrow();

String regex = DestinationInstruction.getFilterForRegex(destinationFilter);
boolean anyMatch = false;
ArrayList<GlobalStation> validStations = new ArrayList<>();
try {
for (GlobalStation globalStation : train.graph.getPoints(EdgePointType.STATION)) {
if (!globalStation.name.matches(regex))
continue;
anyMatch = true;
validStations.add(globalStation);
}
} catch (PatternSyntaxException ignored) {}

DiscoveredPath best = train.navigation.findPathTo(validStations, Double.MAX_VALUE);
if (best == null)
return Pair.of(null, anyMatch);
return Pair.of(best, true);
}

@LuaFunction
public MethodResult canTrainReach(String destinationFilter) throws LuaException {
Pair<@Nullable DiscoveredPath, @NotNull Boolean> path = findPath(destinationFilter);
if (path.getFirst() != null)
return MethodResult.of(true, null);
return MethodResult.of(false, path.getSecond() ? "cannot-reach" : "no-target");
}

@LuaFunction
public MethodResult distanceTo(String destinationFilter) throws LuaException {
Pair<@Nullable DiscoveredPath, @NotNull Boolean> path = findPath(destinationFilter);
if (path.getFirst() != null)
return MethodResult.of(path.getFirst().distance, null);
return MethodResult.of(null, path.getSecond() ? "cannot-reach" : "no-target");
}

private @NotNull Train getTrainOrThrow() throws LuaException {
GlobalStation station = blockEntity.getStation();
if (station == null)
Expand Down Expand Up @@ -268,6 +317,13 @@ else if (value instanceof Map<?, ?> v && v.containsKey(1.0)) { // List
throw new LuaException("unknown object type " + value.getClass().getName());
}

@Override
public void prepareComputerEvent(@NotNull ComputerEvent event) {
if (event instanceof StationTrainPresenceEvent stpe) {
queueEvent(stpe.type.name, stpe.train.name.getString());
}
}

@NotNull
@Override
public String getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import org.jetbrains.annotations.NotNull;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -44,9 +46,12 @@ public ItemStack getSecondLineIcon() {
public String getFilter() {
return getLabelText();
}

public String getFilterForRegex() {
String filter = getFilter();
return getFilterForRegex(getFilter());
}

public static String getFilterForRegex(@NotNull String filter) {
if (filter.isBlank())
return filter;
return "\\Q" + filter.replace("*", "\\E.*\\Q") + "\\E";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@

import javax.annotation.Nullable;

import com.simibubi.create.content.trains.graph.DiscoveredPath;

import org.jetbrains.annotations.NotNull;

import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllItems;
import com.simibubi.create.AllPackets;
import com.simibubi.create.AllSoundEvents;
import com.simibubi.create.Create;
import com.simibubi.create.compat.computercraft.events.StationTrainPresenceEvent;
import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour;
import com.simibubi.create.compat.computercraft.ComputerCraftProxy;
import com.simibubi.create.content.contraptions.AssemblyException;
Expand All @@ -38,6 +37,7 @@
import com.simibubi.create.content.trains.entity.Train;
import com.simibubi.create.content.trains.entity.TrainPacket;
import com.simibubi.create.content.trains.entity.TravellingPoint;
import com.simibubi.create.content.trains.graph.DiscoveredPath;
import com.simibubi.create.content.trains.graph.EdgePointType;
import com.simibubi.create.content.trains.graph.TrackEdge;
import com.simibubi.create.content.trains.graph.TrackGraph;
Expand Down Expand Up @@ -259,6 +259,21 @@ public void tick() {
imminentTrain.runtime.displayLinkUpdateRequested = false;
}

if (!level.isClientSide && computerBehaviour.hasAttachedComputer()) {
if (this.imminentTrain == null && imminentTrain != null)
computerBehaviour.prepareComputerEvent(
new StationTrainPresenceEvent(StationTrainPresenceEvent.Type.IMMINENT, imminentTrain));
if (newlyArrived) {
if (trainPresent)
computerBehaviour.prepareComputerEvent(
new StationTrainPresenceEvent(StationTrainPresenceEvent.Type.ARRIVAL, imminentTrain));
else
computerBehaviour.prepareComputerEvent(
new StationTrainPresenceEvent(StationTrainPresenceEvent.Type.DEPARTURE,
Create.RAILWAYS.trains.get(this.imminentTrain)));
}
}

if (newlyArrived)
applyAutoSchedule();

Expand Down