Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 11 additions & 10 deletions src/main/java/train/client/gui/GuiLoco2.java
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,16 @@ protected void drawGuiContainerBackgroundLayer(float f, int t, int g) {
fontRendererObj.drawStringWithShadow("Carts pulled: " + guiDetails. get("cartsPulled"), 1, 10, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Mass pulled: " + guiDetails.get("massPulled") + " tons", 1, 20, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Speed reduction: " + guiDetails.get("slowDown") + " km/h", 1, 30, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Accel reduction: " + (Math.round(guiDetails.get("accelSlowDown").getAsDouble() * 1000) / 1000), 1, 40, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Brake reduction: " + (Math.round(guiDetails.get("brakeSlowDown").getAsDouble() * 1000) / 1000), 1, 50, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Fuel consumption: " + ((loco.getFuelConsumption() * 0.2) + "").substring(0, Math.min(((loco.getFuelConsumption() * 0.2) + "").length(), 4)) + " mB/s", 1,
60, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Fuel: " + loco.getFuel(), 1, 70, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Power: " + loco.transportMetricHorsePower() + " Mhp", 1, 80, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("State: " + loco.getState(), 1, 90, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Heat level: " + loco.getOverheatLevel(), 1, 100, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Maximum Speed: " + (loco.getCustomSpeedGUI()) + " km/h" + " (" + (loco.getCustomSpeedGUI() + guiDetails.get("slowDown").getAsFloat()) + "km/h)", 1, 110, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Destination: " + (loco.getDestinationGUI()), 1, 120, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Accel reduction: " + guiDetails.get("accelSlowDown"), 1, 40, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Brake reduction: " + guiDetails.get("brakeSlowDown"), 1, 50, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Fuel increase: " + guiDetails.get("fuelUseChange"), 1, 60, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Fuel base consumption: " + ((loco.getFuelConsumption() * 0.2) + "").substring(0, Math.min(((loco.getFuelConsumption() * 0.2) + "").length(), 4)) + " mB/s", 1,
70, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Fuel: " + loco.getFuel(), 1, 80, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Power: " + loco.transportMetricHorsePower() + " Mhp", 1, 90, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("State: " + loco.getState(), 1, 100, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Heat level: " + loco.getOverheatLevel(), 1, 110, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Maximum Speed: " + (loco.getCustomSpeedGUI()) + " km/h" + " (" + (loco.getCustomSpeedGUI() + guiDetails.get("slowDown").getAsFloat()) + "km/h)", 1, 120, 0xFFFFFF);
fontRendererObj.drawStringWithShadow("Destination: " + (loco.getDestinationGUI()), 1, 130, 0xFFFFFF);
}
}
54 changes: 54 additions & 0 deletions src/main/java/train/common/api/AbstractTrains.java
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,60 @@ private AbstractTrains findFront(ArrayList<AbstractTrains> transports) {
return transports.get(0);
}

/**
* Finds the direction from which a locomotive is pulling/pushing from.
* @return Returns 1 if from front, -1 if from back, or 0 if no pulling locomotive exists or this is the pulling locomotive.
*/
protected int pullingLocomotiveDirection() {
if (this instanceof Locomotive) {
if (!((Locomotive) this).canBePulled) {
return 0;
}
}

ArrayList<AbstractTrains> visited = new ArrayList<>(); // In case somebody makes a circular train
visited.add(this);

boolean visitingFront = true;

AbstractTrains previousTrain = this;
AbstractTrains train = frontLink;
while (!visited.contains(train)) {
if (train == null) {
// If we have reached the front end, reset and start from the back. If we reached that other end too, break.
if (visitingFront) {
visitingFront = false;
train = backLink;
previousTrain = this;
continue;
}
else {
break;
}
}

visited.add(train);

if (train instanceof Locomotive) {
if (!((Locomotive) train).canBePulled) {
return visitingFront ? 1 : -1;
}
}

// Trains can link front to front and back to back, so keep traversing toward whatever side we didn't come from
if (train.frontLink != previousTrain) {
previousTrain = train;
train = train.frontLink;
}
else {
previousTrain = train;
train = train.backLink;
}
}
// Both front and back didn't find anything, so there's no pulling locomotive.
return 0;
}

/**
* @return Returns String ArrayList of trusted players' usernames.
*/
Expand Down
81 changes: 40 additions & 41 deletions src/main/java/train/common/api/EntityBogie.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ protected void func_145821_a(int x, int y, int z, double maxSpeed, double slopeA
}

private void moveOnTCRail(int i, int j, int k, Block l) {
limitSpeedOnTCRail();

if(l instanceof BlockTCRail) {
if(!TCRailTypes.isCrossingTrack((TileTCRail) worldObj.getTileEntity(i, j, k)) && !TCRailTypes.isDiagonalCrossingTrack((TileTCRail) worldObj.getTileEntity(i,j,k))) {
Expand Down Expand Up @@ -437,25 +436,19 @@ private void moveOnTC90TurnRail(int j,double radius, double startX, double start
velocity[1] = Math.copySign(norm_cpx * motionSqrt, railPathZ2);
}

private void limitSpeedOnTCRail() {
double maxSpeed = Math.min(3.0D, getMaxCartSpeedOnRail());
private void limitSpeed(AbstractTrains host, double speedMagnitude) {

if (this.motionX < -maxSpeed) {

this.motionX = -maxSpeed;
}
else if (this.motionX > maxSpeed) {

this.motionX = maxSpeed;
}

if (this.motionZ < -maxSpeed) {

this.motionZ = -maxSpeed;
// Default speed for most carts
double maxSpeed = this.getMaxCartSpeedOnRail();
// Current max speed for locos
if (host instanceof Locomotive) {
maxSpeed = Math.min(maxSpeed,SpeedHandler.convertSpeed((double)((Locomotive)host).getCurrentMaxSpeed()));
}
else if (this.motionZ > maxSpeed) {

this.motionZ = maxSpeed;
if (speedMagnitude > maxSpeed) {
double overspeedFactor = speedMagnitude/maxSpeed;
velocity[0] /= overspeedFactor;
velocity[1] /= overspeedFactor;
}
}

Expand All @@ -465,31 +458,32 @@ public GameProfile getOwner() {
return this.entityMainTrain.getOwner();
}

/*
* Velocity needs to be added relative to each bogie's current rotation to prevent drifting between them, as the host's rotation doesn't match when entering curves.
* Always adding in the direction of existing movement prevents reverse, and is unpredictable with null starting velocity, so we compare the bogie's rotation to the host's.
*/
public void addVelocity(AbstractTrains host, double speed) {
//cache rotation so it only has to be processed once per tick
Vec3f vec = CommonUtil.rotatePoint(new Vec3f(1,0,0),0,180 + host.rotationYaw,0);
velocity[0] += speed * vec.xCoord;
velocity[1] += speed * vec.zCoord;
}
Vec3f bogieRotation = CommonUtil.rotatePoint(new Vec3f(1,0,0),0,180+(float)Math.toDegrees(Math.atan2(velocity[1], velocity[0])),0);
Vec3f hostRotation = CommonUtil.rotatePoint(new Vec3f(1,0,0),0,180+host.rotationYaw,0);
int direction = bogieRotation.dotProduct(hostRotation) >= 0 ? 1 : -1;

public void multiplyVelocity(AbstractTrains host, double mult) {
Vec3f vec = CommonUtil.rotatePoint(new Vec3f(1,0,0),0,180 + host.rotationYaw,0);
velocity[0] *= (mult * vec.xCoord);
velocity[1] *= (mult * vec.zCoord);
velocity[0] += speed * bogieRotation.xCoord * direction;
velocity[1] += speed * bogieRotation.zCoord * direction;
}

public void addLinking(AbstractTrains host, double speed){
//cache rotation so it only has to be processed once per tick
Vec3f vec = CommonUtil.rotatePoint(new Vec3f(1,0,0),0,180+host.rotationYaw,0);
velocity[2]+=speed*vec.xCoord;
velocity[3]+=speed*vec.zCoord;
public void multiplyVelocity(double mult) {
velocity[0] *= mult;
velocity[1] *= mult;
}

public void drag(AbstractTrains host, double drag){
velocity[0]*=drag;
velocity[1]*=drag;
}
public void setVelocity(AbstractTrains host, double speed){
Vec3f bogieRotation = CommonUtil.rotatePoint(new Vec3f(1,0,0),0,180+(float)Math.toDegrees(Math.atan2(velocity[1], velocity[0])),0);
Vec3f hostRotation = CommonUtil.rotatePoint(new Vec3f(1,0,0),0,180+host.rotationYaw,0);
int direction = bogieRotation.dotProduct(hostRotation) >= 0 ? 1 : -1;

velocity[0] = speed * bogieRotation.xCoord * direction;
velocity[1] = speed * bogieRotation.zCoord * direction;
}

public World getWorld(){return worldObj;}

Expand Down Expand Up @@ -539,12 +533,13 @@ public void minecartMove(AbstractTrains host) {
oldBlockZ=zFloor;
}



//move on rails
isOnRail = true;
double speedMagnitude = Math.sqrt(Math.pow(velocity[0],2)+Math.pow(velocity[1],2))+Math.sqrt(Math.pow(velocity[2],2)+Math.pow(velocity[3],2));
limitSpeed(host, speedMagnitude);
if (l instanceof BlockRailBase) {
this.yOffset=0.3425f;
loopVanilla(host, Math.abs(velocity[0])+Math.abs(velocity[1])+Math.abs(velocity[2])+Math.abs(velocity[3]), (BlockRailBase) l);
loopVanilla(host, speedMagnitude, (BlockRailBase) l);
} else if (l instanceof BlockTCRail || l instanceof BlockTCRailGag){
this.yOffset=0.425f;
moveOnTCRail(xFloor, yFloor, zFloor, l);
Expand All @@ -553,6 +548,7 @@ public void minecartMove(AbstractTrains host) {
yFloor++;
posX+=(velocity[2]+velocity[0])*0.5;
posZ+=(velocity[3]+velocity[1])*0.5;
isOnRail = false;
}
velocity[2]=0;velocity[3]=0;
}
Expand Down Expand Up @@ -626,6 +622,9 @@ private void moveBogieVanilla(double currentMotion){
railPathX = (martix[railMetadata][2][0]);
railPathZ = (martix[railMetadata][2][1]);

railPathX = Math.copySign(Math.sqrt(Math.abs(railPathX)),railPathX);
railPathZ = Math.copySign(Math.sqrt(Math.abs(railPathZ)),railPathZ);

//cover moving reverse of track direction using the rotation from the closed loop rather than the full motion
if((velocity[0]+velocity[2]) * railPathX + (velocity[1]+velocity[3]) * railPathZ <= 0.0D) {
railPathX = -railPathX;
Expand All @@ -634,15 +633,15 @@ private void moveBogieVanilla(double currentMotion){

setPositionRelative((currentMotion * railPathX), 0, (currentMotion * railPathZ));

motionSqrt = Math.abs(velocity[0])+Math.abs(velocity[1]);
motionSqrt = Math.sqrt(Math.pow(velocity[0],2)+Math.pow(velocity[1],2));
velocity[0] = (float)(motionSqrt * railPathX);
velocity[1] = (float)(motionSqrt * railPathZ);

motionSqrt = Math.abs(velocity[2])+Math.abs(velocity[3]);
motionSqrt = Math.sqrt(Math.pow(velocity[2],2)+Math.pow(velocity[3],2));
velocity[2] = (float)(motionSqrt * railPathX);
velocity[3] = (float)(motionSqrt * railPathZ);

motionSqrt = Math.abs(velocity[0])+Math.abs(velocity[1])+Math.abs(velocity[2])+Math.abs(velocity[3]);
motionSqrt = Math.sqrt(Math.pow(velocity[0],2)+Math.pow(velocity[1],2))+Math.sqrt(Math.pow(velocity[2],2)+Math.pow(velocity[3],2));

//define the rail path again, to center the transport.
railPathX2 = xFloor + 0.5D + martix[railMetadata][0][0];
Expand Down
Loading