Skip to content

Instantly share code, notes, and snippets.

@andythenorth
Created April 10, 2021 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andythenorth/b6437ff6904afd090526266170f558c6 to your computer and use it in GitHub Desktop.
Save andythenorth/b6437ff6904afd090526266170f558c6 to your computer and use it in GitHub Desktop.
diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp
index ee14098c28..4b0020599c 100644
--- a/src/newgrf_engine.cpp
+++ b/src/newgrf_engine.cpp
@@ -644,6 +644,8 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object,
SetBit(v->grf_cache.cache_valid, NCVV_POSITION_IN_VEHICLE);
}
return v->grf_cache.position_in_vehicle;
+ case 0x4E: // time since vehicle loaded cargo (in cargo age units)
+ return ClampU(v->cargo_time_on_this_vehicle, 0, 0xFF);
/* Variables which use the parameter */
case 0x60: // Count consist's engine ID occurrence
diff --git a/src/table/newgrf_debug_data.h b/src/table/newgrf_debug_data.h
index e35b2eecbf..9da0b521fb 100644
--- a/src/table/newgrf_debug_data.h
+++ b/src/table/newgrf_debug_data.h
@@ -61,6 +61,7 @@ static const NIVariable _niv_vehicles[] = {
NIV(0x4B, "long date of last service"),
NIV(0x4C, "current max speed"),
NIV(0x4D, "position in articulated vehicle"),
+ NIV(0x4E, "time the cargo has been on this vehicle (cargo age units)"),
NIV(0x60, "count vehicle id occurrences"),
// 0x61 not useful, since it requires register 0x10F
NIV(0x62, "curvature/position difference to other vehicle"),
diff --git a/src/vehicle.cpp b/src/vehicle.cpp
index a9af24c55a..f27e977c83 100644
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -352,6 +352,7 @@ Vehicle::Vehicle(VehicleType type)
this->first = this;
this->colourmap = PAL_NONE;
this->cargo_age_counter = 1;
+ this->cargo_time_on_this_vehicle = 0;
this->last_station_visited = INVALID_STATION;
this->last_loading_station = INVALID_STATION;
}
@@ -979,6 +980,7 @@ void CallVehicleTicks()
if (--v->cargo_age_counter == 0) {
v->cargo.AgeCargo();
v->cargo_age_counter = v->vcache.cached_cargo_age_period;
+ v->cargo_time_on_this_vehicle = v->cargo_time_on_this_vehicle + 1;
}
}
@@ -2274,6 +2276,7 @@ void Vehicle::LeaveStation()
HideFillingPercent(&this->fill_percent_te_id);
trip_occupancy = CalcPercentVehicleFilled(this, nullptr);
+ this->cargo_time_on_this_vehicle = 0;
if (this->type == VEH_TRAIN && !(this->vehstatus & VS_CRASHED)) {
/* Trigger station animation (trains only) */
diff --git a/src/vehicle_base.h b/src/vehicle_base.h
index f80faf1e39..c10c78b409 100644
--- a/src/vehicle_base.h
+++ b/src/vehicle_base.h
@@ -317,6 +317,7 @@ public:
uint16 refit_cap; ///< Capacity left over from before last refit.
VehicleCargoList cargo; ///< The cargo this vehicle is carrying
uint16 cargo_age_counter; ///< Ticks till cargo is aged next.
+ uint16 cargo_time_on_this_vehicle; ///< How long since this vehicle loaded the cargo (in 'cargo age' units).
int8 trip_occupancy; ///< NOSAVE: Occupancy of vehicle of the current trip (updated after leaving a station).
byte day_counter; ///< Increased by one for each day
@andythenorth
Copy link
Author

var_0x4E_result

@andythenorth
Copy link
Author

switch (FEAT_TRAINS, PARENT, ${vehicle.id}_switch_because_i_fail_at_logic, var[0x4E, 0, 0xFFFFFFF]) {
0..5: return ${consist.running_cost};
return ${20 * consist.running_cost};
}
switch (FEAT_TRAINS, PARENT, ${vehicle.id}_switch_running_cost_factor, current_speed > 0) {
0: ${consist.running_cost};
return ${vehicle.id}_switch_because_i_fail_at_logic;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment