Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Commit

Permalink
Add train halting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenny-Hui committed Dec 13, 2023
1 parent 33b9acf commit 89ea8c6
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 17 deletions.
27 changes: 23 additions & 4 deletions src/main/java/com/lx862/mtrtm/TransitManager.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.lx862.mtrtm;

import com.lx862.mtrtm.config.Config;
import it.unimi.dsi.fastutil.longs.Long2LongArrayMap;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.longs.LongList;
import com.lx862.mtrtm.data.TrainState;
import it.unimi.dsi.fastutil.longs.*;
import net.fabricmc.api.ModInitializer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class TransitManager implements ModInitializer {
public static final Logger LOGGER = LogManager.getLogger("TransitManager");
public static final LongList stopPathGenDepotList = new LongArrayList();
public static final LongList disableTrainCollision = new LongArrayList();
public static final Long2IntOpenHashMap trainStateList = new Long2IntOpenHashMap();
public static final Long2LongArrayMap pathGenerationTimer = new Long2LongArrayMap();

@Override
Expand All @@ -23,4 +22,24 @@ public void onInitialize() {
CommandHandler.registerCommands(dispatcher);
});
}

public static boolean getTrainState(long trainId, TrainState trainState) {
int state = trainStateList.get(trainId);
int pos = trainState.getPos();

return ((state >> pos) & 1) == 1;
}

public static void setTrainState(long trainId, TrainState trainState, boolean value) {
int state = trainStateList.getOrDefault(trainId, 0);
int pos = trainState.getPos();
if(value) {
state = state | (1 << pos);
} else {
state = state & ~(1 << pos);
}


trainStateList.put(trainId, state);
}
}
42 changes: 30 additions & 12 deletions src/main/java/com/lx862/mtrtm/commands/train.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.lx862.mtrtm.TransitManager;
import com.lx862.mtrtm.Util;
import com.lx862.mtrtm.data.ExposedTrainData;
import com.lx862.mtrtm.data.TrainState;
import com.lx862.mtrtm.mixin.TrainAccessorMixin;
import com.lx862.mtrtm.mixin.TrainServerAccessorMixin;
import com.mojang.brigadier.CommandDispatcher;
Expand Down Expand Up @@ -34,12 +35,15 @@ public class train {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("train")
.requires(ctx -> ctx.hasPermission(2))
.then(Commands.literal("collision")
.then(Commands.literal("disable")
.executes(context -> setCollision(context, true))
.then(Commands.literal("toggleCollision")
.executes(context -> toggleCollision(context))
)
.then(Commands.literal("halt")
.then(Commands.literal("dwell")
.executes(context -> haltDwell(context))
)
.then(Commands.literal("enable")
.executes(context -> setCollision(context, false))
.then(Commands.literal("speed")
.executes(context -> haltSpeed(context))
)
)
.then(Commands.literal("board")
Expand Down Expand Up @@ -191,16 +195,30 @@ private static int skipDwell(CommandContext<CommandSourceStack> context) {
return 1;
}

private static int setCollision(CommandContext<CommandSourceStack> context, boolean disable) {
private static int haltDwell(CommandContext<CommandSourceStack> context) {
ExposedTrainData nearestTrain = getNearestTrainOrError(context);
boolean halted = TransitManager.getTrainState(nearestTrain.train.id, TrainState.HALT_DWELL);
TransitManager.setTrainState(nearestTrain.train.id, TrainState.HALT_DWELL, !halted);

if(!disable) {
TransitManager.disableTrainCollision.remove(nearestTrain.train.sidingId);
} else {
TransitManager.disableTrainCollision.add(nearestTrain.train.sidingId);
}
context.getSource().sendSuccess(Mappings.literalText("Dwell timer for the nearest train has been " + (!halted ? "paused" : "resumed")).withStyle(ChatFormatting.GREEN), false);
return 1;
}

private static int haltSpeed(CommandContext<CommandSourceStack> context) {
ExposedTrainData nearestTrain = getNearestTrainOrError(context);
boolean halted = TransitManager.getTrainState(nearestTrain.train.id, TrainState.HALT_SPEED);
TransitManager.setTrainState(nearestTrain.train.id, TrainState.HALT_SPEED, !halted);

context.getSource().sendSuccess(Mappings.literalText("The nearest train has " + (!halted ? "been brought to a halt" : "resumed it's operation")).withStyle(ChatFormatting.GREEN), false);
return 1;
}

private static int toggleCollision(CommandContext<CommandSourceStack> context) {
ExposedTrainData nearestTrain = getNearestTrainOrError(context);
boolean skipCollision = TransitManager.getTrainState(nearestTrain.train.id, TrainState.SKIP_COLLISION);
TransitManager.setTrainState(nearestTrain.train.id, TrainState.SKIP_COLLISION, !skipCollision);

context.getSource().sendSuccess(Mappings.literalText("Collision detection for the nearest train is now " + (disable ? "disabled" : "enabled")).withStyle(ChatFormatting.GREEN), false);
context.getSource().sendSuccess(Mappings.literalText("Collision detection for the nearest train is now " + (!skipCollision ? "disabled" : "enabled")).withStyle(ChatFormatting.GREEN), false);
return 1;
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/lx862/mtrtm/data/TrainState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.lx862.mtrtm.data;

public enum TrainState {
SKIP_COLLISION(0),
HALT_DWELL(1),
HALT_SPEED(2);

private final int pos;

TrainState(int pos) {
this.pos = pos;
}

public int getPos() {
return pos;
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/lx862/mtrtm/mixin/TrainMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.lx862.mtrtm.mixin;

import com.lx862.mtrtm.TransitManager;
import com.lx862.mtrtm.data.TrainState;
import mtr.data.Depot;
import mtr.data.Train;
import net.minecraft.world.level.Level;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(value = Train.class, remap = false)
public class TrainMixin {
@Shadow protected float speed;

@Shadow protected float elapsedDwellTicks;

/* Disable train collision */
@Inject(method = "simulateTrain", at = @At("TAIL"), cancellable = true)
public void isRailBlocked(Level world, float ticksElapsed, Depot depot, CallbackInfo ci) {
long trainId = ((Train)((Object)this)).id;
if(TransitManager.getTrainState(trainId, TrainState.HALT_SPEED)) {
this.speed = 0;
}
if(TransitManager.getTrainState(trainId, TrainState.HALT_DWELL)) {
this.elapsedDwellTicks -= ticksElapsed;
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/lx862/mtrtm/mixin/TrainServerMixin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lx862.mtrtm.mixin;

import com.lx862.mtrtm.TransitManager;
import com.lx862.mtrtm.data.TrainState;
import mtr.data.*;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -13,7 +14,8 @@ public class TrainServerMixin {
/* Disable train collision */
@Inject(method = "isRailBlocked", at = @At("HEAD"), cancellable = true)
public void isRailBlocked(int checkIndex, CallbackInfoReturnable<Boolean> cir) {
if(TransitManager.disableTrainCollision.contains(((Train)((TrainServer)(Object)this)).sidingId)) {
long trainId = ((Train)((TrainServer)(Object)this)).id;
if(TransitManager.getTrainState(trainId, TrainState.SKIP_COLLISION)) {
cir.setReturnValue(false);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/mtrtm.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"RailwayDataRouteFinderModuleMixin",
"SidingAccessorMixin",
"TrainAccessorMixin",
"TrainMixin",
"TrainServerAccessorMixin",
"TrainServerMixin"
],
Expand Down

0 comments on commit 89ea8c6

Please sign in to comment.