Skip to content

Commit 251e840

Browse files
[Feature] Add ITrackV2 for complex logic (and bump to 1.3) (#15)
* ref: modernize buildscript and mapping * feat: add PathingContext for packed data * ref: add more predefined gauges * feat: add ITrackV2 for complex pathing logics * bump version to 1.3 * ref: refactor PathingContext * ref: add Vec3 getter * rename WheelData to PathingData * ref: add comment on roll * ref: remove Vec3 as it may introduce more unnecessary calculation * remove final on PathingData * change visibility of PathingData.position & roll * Revert "ref: remove Vec3 as it may introduce more unnecessary calculation" * ref: reconstruct PathingData * ref: cleanup calculation * ref: rename * ref: add back ITrackBlock compat * ref: rename vanillaPos back to pos * ref: change return of ITrackV2.getNextPosition to void * fix prev commit * docs
1 parent 75e7a82 commit 251e840

10 files changed

Lines changed: 203 additions & 84 deletions

File tree

build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import cam72cam.universalmodcore.Util;
1+
import cam72cam.universalmodcore.Util
22

33
buildscript {
44
repositories {
5-
jcenter()
6-
maven { url = "http://files.minecraftforge.net/maven" }
5+
mavenCentral()
6+
maven { url = "http://maven.minecraftforge.net/" }
77
maven { url = "https://teamopenindustry.cc/maven" }
88
}
99
dependencies {
@@ -16,7 +16,7 @@ apply plugin: 'net.minecraftforge.gradle.forge'
1616
apply plugin: 'maven'
1717

1818

19-
String baseVersion = "1.2"
19+
String baseVersion = "1.3"
2020
if (!"release".equalsIgnoreCase(System.getProperty("target"))) {
2121
baseVersion += "-" + Util.GitRevision()
2222
}
@@ -30,15 +30,15 @@ compileJava {
3030
}
3131

3232
minecraft {
33-
version = "1.12.2-14.23.0.2529"
33+
version = "1.12.2-14.23.5.2847"
3434
runDir = "run"
3535

3636
// the mappings can be changed at any time, and must be in the following format.
3737
// snapshot_YYYYMMDD snapshot are built nightly.
3838
// stable_# stables are built at the discretion of the MCP team.
3939
// Use non-default mappings at your own risk. they may not always work.
4040
// simply re-run your setup task after changing the mappings to update your workspace.
41-
mappings = "snapshot_20171003"
41+
mappings = "stable_39"
4242
// makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.
4343
}
4444

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.5-bin.zip

src/main/java/trackapi/TrackAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
public class TrackAPI
77
{
88
public static final String MODID = "trackapi";
9-
public static final String VERSION = "1.2";
9+
public static final String VERSION = "1.3";
1010
}

src/main/java/trackapi/compat/MinecraftRail.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
import net.minecraft.util.math.BlockPos;
77
import net.minecraft.util.math.Vec3d;
88
import net.minecraft.world.World;
9-
import trackapi.lib.Gauges;
10-
import trackapi.lib.ITrack;
9+
import trackapi.lib.*;
1110

1211
import java.util.HashMap;
1312
import java.util.Map;
1413

15-
public class MinecraftRail implements ITrack {
16-
private static Map<EnumRailDirection, Vec3d> vectors = new HashMap<>();
17-
private static Map<EnumRailDirection, Vec3d> centers = new HashMap<>();
14+
/**
15+
* Wrapper for vanilla rail
16+
*/
17+
public class MinecraftRail implements ITrackV2 {
18+
private static final Map<EnumRailDirection, Vec3d> vectors = new HashMap<>();
19+
private static final Map<EnumRailDirection, Vec3d> centers = new HashMap<>();
1820
static {
1921
Vec3d north = new Vec3d(0, 0, 1);
2022
Vec3d south = new Vec3d(0, 0, -1);
@@ -46,8 +48,8 @@ public class MinecraftRail implements ITrack {
4648
}
4749

4850

49-
private EnumRailDirection direction;
50-
private BlockPos pos;
51+
private final EnumRailDirection direction;
52+
private final BlockPos pos;
5153

5254
public MinecraftRail(World world, BlockPos pos) {
5355
this.pos = pos;
@@ -57,33 +59,37 @@ public MinecraftRail(World world, BlockPos pos) {
5759
}
5860

5961
@Override
60-
public double getTrackGauge() {
61-
return Gauges.MINECRAFT;
62+
public double[] getTrackGauges() {
63+
return new double[]{Gauges.MINECRAFT};
6264
}
6365

6466
@Override
65-
public Vec3d getNextPosition(Vec3d currentPosition, Vec3d motion) {
66-
Vec3d trackMovement = vectors.get(direction);
67+
public<D extends PathingData> void getNextPosition(D inputData, Vec3d motion, double gauge) {
68+
Vec3d currentPosition = inputData.getPos();
69+
70+
Vec3d trackMovement = vectors.get(direction);
6771
Vec3d trackCenter = centers.get(direction);
6872

69-
Vec3d posRelativeToCenter = currentPosition.subtractReverse(new Vec3d(pos).add(trackCenter));
70-
double distanceToCenter = posRelativeToCenter.lengthVector();
73+
Vec3d pos = new Vec3d(this.pos).add(trackCenter);
74+
Vec3d posRelativeToCenter = currentPosition.subtractReverse(pos);
75+
double distanceToCenter = posRelativeToCenter.length();
7176

7277
// Determine if trackMovement should be positive or negative as relative to block center
7378
boolean trackPosMotionInverted = posRelativeToCenter.distanceTo(trackMovement) < posRelativeToCenter.scale(-1).distanceTo(trackMovement);
7479

7580
boolean trackMotionInverted = motion.distanceTo(trackMovement) > motion.scale(-1).distanceTo(trackMovement);
7681

77-
Vec3d newPosition = new Vec3d(pos).add(trackCenter);
78-
//Correct new pos to track alignment
79-
newPosition = newPosition.add(trackMovement.scale(trackPosMotionInverted ? -distanceToCenter : distanceToCenter));
80-
// Move new pos along track alignment
81-
newPosition = newPosition.add(trackMovement.scale(trackMotionInverted ? -motion.lengthVector() : motion.lengthVector()));
82-
return newPosition;
82+
Vec3d newPosition = pos;
83+
double factor =
84+
//Correct new pos to track alignment
85+
(trackPosMotionInverted ? -distanceToCenter : distanceToCenter)
86+
//And Move new pos along track alignment
87+
+ (trackMotionInverted ? -motion.length() : motion.length());
88+
newPosition = newPosition.add(trackMovement.scale(factor));
89+
inputData.setPos(newPosition).setRoll(0d);
8390
}
8491

8592
public static boolean isRail(World world, BlockPos pos) {
8693
return BlockRailBase.isRailBlock(world, pos);
8794
}
88-
8995
}
Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
11
package trackapi.lib;
22

3+
/**
4+
* Some gauge constants in meters
5+
*/
36
public class Gauges {
47
/**
5-
* US Standard Gauge in meters
8+
* Minecraft Gauge
9+
*/
10+
public static final double MINECRAFT = 0.632;
11+
12+
/**
13+
* 2 Foot 6 Inches Narrow Gauge
14+
*/
15+
public static final double TWO_FOOT_SIX_INCHES = 0.762;
16+
17+
/**
18+
* 3 Foot Narrow Gauge
19+
*/
20+
public static final double THREE_FOOT = 0.9144;
21+
22+
/**
23+
* Meter Gauge
24+
*/
25+
public static final double METER = 1.000;
26+
27+
/**
28+
* Cape gauge
29+
*/
30+
public static final double CAPE = 1.067;
31+
32+
/**
33+
* US Standard Gauge
634
*/
735
public static final double STANDARD = 1.435;
836

937
/**
10-
* Minecraft Gauge in meters
38+
* Russian Standard Gauge
1139
*/
12-
public static final double MINECRAFT = 0.632;
40+
public static final double RU_STANDARD = 1.524;
41+
42+
/**
43+
* Brunel Gauge
44+
*/
45+
public static final double BRUNEL = 2.140;
1346
}

src/main/java/trackapi/lib/ITrack.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@ public interface ITrack {
77
/**
88
* The distance between the rails measured in meters
99
*
10-
* @see Gauges#STANDARD
11-
* @see Gauges#MINECRAFT
10+
* @see Gauges
1211
*/
13-
public double getTrackGauge();
12+
double getTrackGauge();
1413

1514
/**
16-
* Used by rolling stock to look up their next position.
15+
* Used by rolling stocks to look up their next position.
1716
*
18-
* @param currentPosition - Current entity or bogey position
19-
* @param rotationYaw - Current entity rotation in degrees
20-
* @param bogieYaw - Current bogey rotation in degrees (set to rotationYaw if unused)
21-
* @param distance - Distanced traveled in meters
22-
* @return The new position of the entity or bogey
17+
* @param currentPosition Current position of entity or bogey
18+
* @param motion Current velocity of entity or bogey
19+
* @return Next found position on the track
2320
*/
24-
public Vec3d getNextPosition(Vec3d currentPosition, Vec3d motion);
21+
Vec3d getNextPosition(Vec3d currentPosition, Vec3d motion);
2522
}

src/main/java/trackapi/lib/ITrackBlock.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@
55
import net.minecraft.world.World;
66

77
/**
8-
* Compatibility layer for block only tracks
9-
*
8+
* Compatibility layer between <code>ITrack</code> and blocks which only contain tracks
9+
* @deprecated Use <code>ITrackV2</code> instead for forward compatibility, instances of this class will only be returned when querying <code>ITrack</code>
1010
*/
11+
@Deprecated
1112
public interface ITrackBlock {
1213

1314
/**
1415
* The distance between the rails measured in meters
1516
*
16-
* @see Gauges#STANDARD
17-
* @see Gauges#MINECRAFT
17+
* @see Gauges
1818
*/
19-
public double getTrackGauge(World world, BlockPos pos);
19+
double getTrackGauge(World world, BlockPos pos);
2020

2121
/**
22-
* Used by rolling stock to look up their next position.
23-
*
24-
* @param currentPosition - Current entity or bogey position
25-
* @param rotationYaw - Current entity rotation in degrees
26-
* @param bogieYaw - Current bogey rotation in degrees (set to rotationYaw if unused)
27-
* @param distance - Distanced traveled in meters
28-
* @return The new position of the entity or bogey
22+
* Used by rolling stock to look up their next position (and related data).
23+
*
24+
* @param world World to query
25+
* @param pos Position of the block
26+
* @param currentPosition Current entity or bogey position
27+
* @param motion Current velocity of entity or bogey
28+
* @return Next found position on the track
2929
*/
30-
public Vec3d getNextPosition(World world, BlockPos pos, Vec3d currentPosition, Vec3d motion);
30+
Vec3d getNextPosition(World world, BlockPos pos, Vec3d currentPosition, Vec3d motion);
3131
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package trackapi.lib;
2+
3+
import net.minecraft.util.math.Vec3d;
4+
5+
public interface ITrackV2 extends ITrack {
6+
7+
/**
8+
* Find available gauges within this track
9+
*
10+
* @return All available gauges
11+
*/
12+
double[] getTrackGauges();
13+
14+
/**
15+
* Used by rolling stocks to look up their next position and related data
16+
* @param inputData Mutable PathingData contains input parameters, like position and roll, and will be overridden with output data
17+
* @param motion Current velocity of entity or bogey
18+
* @param gauge Gauge of the pathing stock
19+
*/
20+
<D extends PathingData> void getNextPosition(D inputData, Vec3d motion, double gauge);
21+
22+
//Overrides for forward compatibility, don't use
23+
@Override
24+
@Deprecated
25+
default double getTrackGauge() {
26+
return getTrackGauges()[0];
27+
}
28+
29+
@Override
30+
@Deprecated
31+
default Vec3d getNextPosition(Vec3d currentPosition, Vec3d motion) {
32+
PathingData data = new PathingData(currentPosition, 0d);
33+
getNextPosition(data, motion, getTrackGauge());
34+
return data.getPos();
35+
}
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package trackapi.lib;
2+
3+
import net.minecraft.util.math.Vec3d;
4+
5+
/**
6+
* Mutable data storaging object used by stocks to query data
7+
*/
8+
public class PathingData {
9+
private Vec3d pos;
10+
private double roll;
11+
12+
public PathingData(Vec3d pos, double roll) {
13+
this.pos = pos;
14+
this.roll = roll;
15+
}
16+
17+
public Vec3d getPos() {
18+
return pos;
19+
}
20+
21+
public PathingData setPos(Vec3d pos) {
22+
this.pos = pos;
23+
return this;
24+
}
25+
26+
public double getRoll() {
27+
return roll;
28+
}
29+
30+
public PathingData setRoll(double roll) {
31+
this.roll = roll;
32+
return this;
33+
}
34+
}

0 commit comments

Comments
 (0)