Files
Landtrain/tools/java/LandtrainConstraintAuthHelper.java
2026-02-12 15:51:25 -05:00

66 lines
2.0 KiB
Java

package zombie.vehicles;
import java.util.HashSet;
import java.util.Set;
import zombie.characters.IsoGameCharacter;
/**
* Resolves the effective driver for constraint auth in chained towing.
* For middle vehicles in a chain, scan through links so long trains still resolve authority.
*/
public final class LandtrainConstraintAuthHelper {
private LandtrainConstraintAuthHelper() {
}
public static IsoGameCharacter resolveConstraintDriver(BaseVehicle vehicle) {
if (vehicle == null) {
return null;
}
IsoGameCharacter driver = findDriverAlongChain(vehicle, true);
if (driver != null) return driver;
return findDriverAlongChain(vehicle, false);
}
private static IsoGameCharacter findDriverAlongChain(BaseVehicle start, boolean towardFront) {
BaseVehicle cursor = start;
Set<Integer> visited = new HashSet<>();
while (cursor != null) {
int id = cursor.getId();
if (!visited.add(id)) {
// Safety: malformed link graph; stop scanning.
return null;
}
IsoGameCharacter driver = cursor.getDriver();
if (driver != null) {
return driver;
}
cursor = towardFront ? cursor.getVehicleTowedBy() : cursor.getVehicleTowing();
}
return null;
}
public static boolean isEnterBlockedLandtrain(
BaseVehicle vehicle, IsoGameCharacter chr, int seat) {
if (isVehicleBeingTowedInLandtrain(vehicle)) {
return true;
}
return vehicle != null && vehicle.isExitBlocked(chr, seat);
}
public static boolean isEnterBlocked2Landtrain(BaseVehicle vehicle, int seat) {
if (isVehicleBeingTowedInLandtrain(vehicle)) {
return true;
}
return vehicle != null && vehicle.isExitBlocked2(seat);
}
private static boolean isVehicleBeingTowedInLandtrain(BaseVehicle vehicle) {
return vehicle != null && vehicle.getVehicleTowedBy() != null;
}
}