Skip to content

Commit

Permalink
Overhauled variable internals to align VM names with variable names. …
Browse files Browse the repository at this point in the history
…Allows setting values via parent_ or vehicle_ prefix per #1760.
  • Loading branch information
DonBruce64 committed Sep 2, 2024
1 parent 0cfac9b commit 2f7f99d
Show file tree
Hide file tree
Showing 14 changed files with 359 additions and 548 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ public final void setTo(double value, boolean sendPacket) {
}
}

public final void setActive(boolean active, boolean sendPacket) {
setTo(active ? 1 : 0, sendPacket);
}

public final void adjustBy(double value, boolean sendPacket) {
if (!isConstant) {
setInternal(currentValue + value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ public static void doTick(AEntityA_Base entity) {
AEntityD_Definable<?> definable = (AEntityD_Definable<?>) entity;
//Need to do this before updating as these require knowledge of prior states.
entity.world.beginProfiling("VariableModifiers", true);
definable.setVariableDefaults();
definable.updateVariableModifiers();
if (definable instanceof AEntityF_Multipart) {
((AEntityF_Multipart<?>) definable).allParts.forEach(part -> part.updateVariableModifiers());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import minecrafttransportsimulator.jsondefs.JSONDecor;
import minecrafttransportsimulator.jsondefs.JSONDecor.DecorComponentType;
import minecrafttransportsimulator.jsondefs.JSONItem.ItemComponentType;
import minecrafttransportsimulator.jsondefs.JSONVariableModifier;
import minecrafttransportsimulator.mcinterface.AWrapperWorld;
import minecrafttransportsimulator.mcinterface.IWrapperNBT;
import minecrafttransportsimulator.mcinterface.IWrapperPlayer;
Expand All @@ -24,9 +23,9 @@
*/
public class TileEntityDecor extends ATileEntityBase<JSONDecor> {
//Variables
public final ComputedVariable clicked;
public final ComputedVariable activated;
private float lightLevel;
public final ComputedVariable clickedVar;
public final ComputedVariable activatedVar;
private final ComputedVariable lightLevelVar;
public boolean craftedItem;

public TileEntityDecor(AWrapperWorld world, Point3D position, IWrapperPlayer placingPlayer, ItemDecor item, IWrapperNBT data) {
Expand All @@ -47,8 +46,9 @@ public TileEntityDecor(AWrapperWorld world, Point3D position, IWrapperPlayer pla
boundingBox.depthRadius = definition.decor.width / 2D;
}

this.clicked = new ComputedVariable(this, "clicked", data);
this.activated = new ComputedVariable(this, "activated", data);
this.clickedVar = new ComputedVariable(this, "clicked", data);
this.activatedVar = new ComputedVariable(this, "activated", data);
this.lightLevelVar = new ComputedVariable(this, "lightLevel");
}

@Override
Expand All @@ -60,8 +60,8 @@ public void update() {

super.update();
//Reset clicked state.
if(clicked.isActive) {
clicked.toggle(false);
if (clickedVar.isActive) {
clickedVar.toggle(false);
}
}

Expand All @@ -87,8 +87,8 @@ public boolean interact(IWrapperPlayer player) {
playersInteracting.add(player);
InterfaceManager.packetInterface.sendToAllClients(new PacketEntityInteractGUI(this, player, true));
} else {
clicked.setTo(1, true);
activated.toggle(true);
clickedVar.setTo(1, true);
activatedVar.toggle(true);
}
} else if (definition.decor.type == DecorComponentType.SEAT) {
setRider(player, true);
Expand All @@ -98,8 +98,8 @@ public boolean interact(IWrapperPlayer player) {
playersInteracting.add(player);
InterfaceManager.packetInterface.sendToAllClients(new PacketEntityInteractGUI(this, player, true));
}
clicked.setTo(1, true);
activated.toggle(true);
clickedVar.setTo(1, true);
activatedVar.toggle(true);
}
return true;
}
Expand All @@ -111,26 +111,12 @@ public int getRotationIncrement() {

@Override
public float getLightProvided() {
return lightLevel;
return (float) lightLevelVar.currentValue;
}

@Override
public void updateVariableModifiers() {
lightLevel = definition.decor.lightLevel;

//Adjust current variables to modifiers, if any exist.
if (definition.variableModifiers != null) {
for (JSONVariableModifier modifier : definition.variableModifiers) {
switch (modifier.variable) {
case "lightLevel":
lightLevel = (float) adjustVariable(modifier, lightLevel);
break;
default:
ComputedVariable variable = getOrCreateVariable(modifier.variable);
variable.setTo(adjustVariable(modifier, variable.currentValue), false);
break;
}
}
}
public void setVariableDefaults() {
super.setVariableDefaults();
lightLevelVar.setTo(definition.decor.lightLevel, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import minecrafttransportsimulator.blocks.components.ABlockBase.Axis;
import minecrafttransportsimulator.blocks.tileentities.components.ATileEntityPole_Component;
import minecrafttransportsimulator.items.instances.ItemPoleComponent;
import minecrafttransportsimulator.jsondefs.JSONVariableModifier;
import minecrafttransportsimulator.mcinterface.IWrapperNBT;
import minecrafttransportsimulator.mcinterface.IWrapperPlayer;

Expand All @@ -14,10 +13,11 @@
* @author don_bruce
*/
public class TileEntityPole_StreetLight extends ATileEntityPole_Component {
private float lightLevel;
private ComputedVariable lightLevelVar;

public TileEntityPole_StreetLight(TileEntityPole core, IWrapperPlayer placingPlayer, Axis axis, ItemPoleComponent item, IWrapperNBT data) {
super(core, placingPlayer, axis, item, data);
this.lightLevelVar = new ComputedVariable(this, "lightLevel");
}

@Override
Expand All @@ -32,26 +32,12 @@ public void update() {

@Override
public float getLightProvided() {
return lightLevel;
return (float) lightLevelVar.currentValue;
}

@Override
public void updateVariableModifiers() {
lightLevel = 12F / 15F;

//Adjust current variables to modifiers, if any exist.
if (definition.variableModifiers != null) {
for (JSONVariableModifier modifier : definition.variableModifiers) {
switch (modifier.variable) {
case "lightLevel":
lightLevel = (float) adjustVariable(modifier, lightLevel);
break;
default:
ComputedVariable variable = getOrCreateVariable(modifier.variable);
variable.setTo(adjustVariable(modifier, variable.currentValue), false);
break;
}
}
}
public void setVariableDefaults() {
super.setVariableDefaults();
lightLevelVar.setTo(12F / 15F, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
import minecrafttransportsimulator.mcinterface.IWrapperPlayer;
import minecrafttransportsimulator.mcinterface.InterfaceManager;
import minecrafttransportsimulator.packets.instances.PacketEntityInteractGUI;
import minecrafttransportsimulator.packets.instances.PacketEntityVariableIncrement;
import minecrafttransportsimulator.packets.instances.PacketEntityVariableSet;
import minecrafttransportsimulator.packets.instances.PacketEntityVariableToggle;
import minecrafttransportsimulator.packloading.PackParser;
import minecrafttransportsimulator.rendering.AModelParser;
import minecrafttransportsimulator.rendering.DurationDelayClock;
Expand Down Expand Up @@ -1182,6 +1179,14 @@ public void runRotation(DurationDelayClock clock, float partialTicks) {
}
}

/**
* Called to set the default values of all variables. Must be run before any other updates that could
* affect these values.
*/
public void setVariableDefaults() {
//Nothing for this level.
}

/**
* Called to update the variable modifiers for this entity.
*/
Expand Down Expand Up @@ -1269,41 +1274,12 @@ public IWrapperNBT save(IWrapperNBT data) {
return data;
}

/**
* Indicates that this field is a derived value from
* one of the variables in {@link AEntityD_Definable#variables}.
* Variables that are derived are parsed from the map every update.
* To modify them you will need to update their values in the respective
* variable set via
* {@link PacketEntityVariableToggle},
* {@link PacketEntityVariableSet},
* {@link PacketEntityVariableIncrement}
*/
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.FIELD})
public @interface DerivedValue {
}

/**
* Indicates that this field is able to be modified via variable modification
* by the code in {@link AEntityE_Interactable#updateVariableModifiers()},
* This annotation is only for variables that are NOT derived from states
* and annotated with {@link DerivedValue}, as those variables can inherently
* be modified as they are derived from the variable states.
* by the code in {@link AEntityD_Definable#updateVariableModifiers()},
*/
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.FIELD})
public @interface ModifiableValue {
}

/**
* Indicates that this field is a modified version of a field annotated with
* {@link ModifiableValue}. This is done to prevent modifying the parsed
* definition entry that contains the value, which is why it's stored
* in a new variable that gets aligned every tick before updates.
*/
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.FIELD})
public @interface ModifiedValue {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,11 @@ abstract class AEntityVehicleD_Moving extends AEntityVehicleC_Colliding {
private final IWrapperPlayer placingPlayer;

//Properties
@ModifiedValue
public double currentSteeringForceIgnoresSpeed;
@ModifiedValue
public double currentSteeringForceFactor;
@ModifiedValue
public double currentBrakingFactor;
@ModifiedValue
public double currentOverSteer;
@ModifiedValue
public double currentUnderSteer;
public final ComputedVariable steeringForceIgnoresSpeedVar;
public final ComputedVariable steeringForceFactorVar;
public final ComputedVariable brakingFactorVar;
public final ComputedVariable overSteerVar;
public final ComputedVariable underSteerVar;

//Road-following data.
protected RoadFollowingState frontFollower;
Expand Down Expand Up @@ -141,6 +136,12 @@ public AEntityVehicleD_Moving(AWrapperWorld world, IWrapperPlayer placingPlayer,
addVariable(this.brakeVar = new ComputedVariable(this, "brake", data));
addVariable(this.parkingBrakeVar = new ComputedVariable(this, "p_brake", data));
addVariable(this.lockedVar = new ComputedVariable(this, "locked", data));

addVariable(this.steeringForceIgnoresSpeedVar = new ComputedVariable(this, "steeringForceIgnoresSpeed"));
addVariable(this.steeringForceFactorVar = new ComputedVariable(this, "steeringForceFactor"));
addVariable(this.brakingFactorVar = new ComputedVariable(this, "brakingFactor"));
addVariable(this.overSteerVar = new ComputedVariable(this, "overSteer"));
addVariable(this.underSteerVar = new ComputedVariable(this, "underSteer"));
}

@Override
Expand Down Expand Up @@ -268,6 +269,16 @@ public void doPostUpdateLogic() {
}
}

@Override
public void setVariableDefaults() {
super.setVariableDefaults();
steeringForceIgnoresSpeedVar.setTo(definition.motorized.steeringForceIgnoresSpeed ? 1 : 0, false);
steeringForceFactorVar.setTo(definition.motorized.steeringForceFactor, false);
brakingFactorVar.setTo(definition.motorized.brakingFactor, false);
overSteerVar.setTo(definition.motorized.overSteer, false);
underSteerVar.setTo(definition.motorized.underSteer, false);
}

@Override
public void updatePartList() {
super.updatePartList();
Expand Down Expand Up @@ -370,7 +381,7 @@ private RoadFollowingState getFollower() {
*/
private void performGroundOperations() {
//Get braking force and apply it to the motions.
double brakingFactor = towedByConnection == null ? getBrakingForce() * currentBrakingFactor : 0;
double brakingFactor = towedByConnection == null ? getBrakingForce() * brakingFactorVar.currentValue : 0;
if (brakingFactor > 0) {
double brakingForce = 20F * brakingFactor / currentMass;
if (brakingForce > velocity) {
Expand Down Expand Up @@ -422,16 +433,16 @@ private void performGroundOperations() {
if (this.towedByConnection == null) {
double overSteerForce = Math.max(velocity / 4, 1);
if (definition.motorized.overSteerAccel != 0) {
weightTransfer += ((motion.dotProduct(motion, false) - prevMotion.dotProduct(prevMotion, false)) * weightTransfer) * currentOverSteer;
weightTransfer += ((motion.dotProduct(motion, false) - prevMotion.dotProduct(prevMotion, false)) * weightTransfer) * overSteerVar.currentValue;
if (Math.abs(weightTransfer) > Math.abs(definition.motorized.overSteerAccel) && Math.abs(weightTransfer) > Math.abs(definition.motorized.overSteerDecel)) {
weightTransfer = definition.motorized.overSteerAccel;
} else if (Math.abs(weightTransfer) < Math.abs(definition.motorized.overSteerDecel) && weightTransfer < Math.abs(definition.motorized.overSteerAccel)) {
weightTransfer = definition.motorized.overSteerDecel;
}
} else {
weightTransfer = currentOverSteer;
weightTransfer = overSteerVar.currentValue;
}
rotation.angles.y += crossProduct.y * weightTransfer + (Math.abs(crossProduct.y) * -currentUnderSteer * turningForce) * overSteerForce;
rotation.angles.y += crossProduct.y * weightTransfer + (Math.abs(crossProduct.y) * -underSteerVar.currentValue * turningForce) * overSteerForce;
}

//If we are offset, adjust our angle.
Expand Down Expand Up @@ -472,7 +483,7 @@ private float getBrakingForce() {
//This is both grounded ground devices, and liquid collision boxes that are set as such.
if (brakingPower > 0) {
for (PartGroundDevice groundDevice : groundDeviceCollective.groundedGroundDevices) {
brakingFactor += groundDevice.currentMotiveFriction;
brakingFactor += groundDevice.motiveFrictionVar.currentValue;
}
if (brakingPower > 0) {
brakingFactor += 0.15D * brakingPower * groundDeviceCollective.getNumberBoxesInLiquid();
Expand All @@ -491,7 +502,7 @@ private float getSkiddingForce() {
float skiddingFactor = 0;
//First check grounded ground devices.
for (PartGroundDevice groundDevice : groundDeviceCollective.groundedGroundDevices) {
skiddingFactor += groundDevice.currentLateralFriction;
skiddingFactor += groundDevice.lateralFrictionVar.currentValue;
}

//Now check if any collision boxes are in liquid. Needed for maritime vehicles.
Expand Down Expand Up @@ -550,10 +561,10 @@ private double getTurningForce() {
//This is opposite of the torque-based forces for control surfaces.
turningForce = steeringAngle / turningDistance;
//Decrease force by the speed of the vehicle. If we are going fast, we can't turn as quickly.
if (groundVelocity > 0.35D && currentSteeringForceIgnoresSpeed == 0) {
turningForce *= Math.pow(0.3F, (groundVelocity * (1 - currentSteeringForceFactor) - 0.35D));
} else if (currentSteeringForceIgnoresSpeed != 0) {
turningForce *= currentSteeringForceFactor;
if (groundVelocity > 0.35D && !steeringForceIgnoresSpeedVar.isActive) {
turningForce *= Math.pow(0.3F, (groundVelocity * (1 - steeringForceFactorVar.currentValue) - 0.35D));
} else if (steeringForceIgnoresSpeedVar.isActive) {
turningForce *= steeringForceFactorVar.currentValue;
}
//Calculate the force the steering produces. Start with adjusting the steering factor by the ground velocity.
//This is because the faster we go the quicker we need to turn to keep pace with the vehicle's movement.
Expand Down
Loading

0 comments on commit 2f7f99d

Please sign in to comment.