Skip to content

Commit

Permalink
Improve train jump behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenny-Hui committed Dec 13, 2023
1 parent 89ea8c6 commit dc7c733
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/main/java/com/lx862/mtrtm/commands/train.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,30 +122,43 @@ private static int jump(CommandContext<CommandSourceStack> context, boolean next
} else if(isSiding) {
targetDistance = distances.get(0);
} else {
int i = 0;
IntList validPathIndex = new IntArrayList();
boolean justOneMorePath = false;
for(int i = 0; i < trainData.train.path.size(); i++) {
int pIndex;
if(next) {
pIndex = i;
} else {
pIndex = trainData.train.path.size() - 1 - i;
}

PathData path = trainData.train.path.get(pIndex);

for(PathData path : trainData.train.path) {
boolean isStoppablePlatform = path.dwellTime > 0 && path.rail.railType == RailType.PLATFORM;

if((isPlatform && isStoppablePlatform) || isPath) {
validPathIndex.add(i);
double dist = distances.get(i);
if(dist > currentRailProgress) {
if(!next) {
boolean last2Path = !isPlatform;
dist = distances.get(validPathIndex.getInt(Math.max(0, validPathIndex.size() - 1 - (last2Path ? 2 : 1))));
}
double dist = distances.get(pIndex);
if(next && dist > currentRailProgress) {
targetDistance = dist;
pathIndex = i;
pathIndex = pIndex;
break;
}

if(!next && dist < currentRailProgress) {

if((trainData.train.getSpeed() == 0) || isPlatform || (trainData.train.getSpeed() > 0 && justOneMorePath) /* 1 more path if train is running */) {
targetDistance = dist;
pathIndex = pIndex;
break;
}

justOneMorePath = true;
}
}
i++;
}
}

if(targetDistance != -1) {
((TrainAccessorMixin)trainData.train).setNextStoppingIndex(pathIndex);
((TrainAccessorMixin)trainData.train).setNextStoppingIndex(Math.max(pathIndex, ((TrainAccessorMixin) trainData.train).getNextStoppingIndex()));
((TrainAccessorMixin)trainData.train).setRailProgress(targetDistance);
MtrUtil.syncTrainToPlayers(trainData.train, context.getSource().getLevel().players());

Expand Down

0 comments on commit dc7c733

Please sign in to comment.