Skip to content

Commit

Permalink
Change and simplify algorithm for vehicle repair cost.
Browse files Browse the repository at this point in the history
This is due to absurdly high repair costs for old vehicles with
old algorithm, due to implicit exponential increases.
Get rid of variable in vehicle struct.
  • Loading branch information
JGRennison committed Jan 2, 2017
1 parent a71a6ec commit 7ecdebd
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/saveload/extended_ver_sl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const SlxiSubChunkInfo _sl_xv_sub_chunk_infos[] = {
{ XSLFI_IMPROVED_BREAKDOWNS, XSCF_NULL, 4, 4, "improved_breakdowns", NULL, NULL, NULL },
{ XSLFI_TT_WAIT_IN_DEPOT, XSCF_NULL, 1, 1, "tt_wait_in_depot", NULL, NULL, NULL },
{ XSLFI_AUTO_TIMETABLE, XSCF_NULL, 4, 4, "auto_timetables", NULL, NULL, NULL },
{ XSLFI_VEHICLE_REPAIR_COST, XSCF_NULL, 1, 1, "vehicle_repair_cost", NULL, NULL, NULL },
{ XSLFI_VEHICLE_REPAIR_COST, XSCF_NULL, 2, 2, "vehicle_repair_cost", NULL, NULL, NULL },
{ XSLFI_ENH_VIEWPORT_PLANS, XSCF_IGNORABLE_ALL, 1, 1, "enh_viewport_plans", NULL, NULL, "PLAN,PLLN" },
{ XSLFI_INFRA_SHARING, XSCF_NULL, 1, 1, "infra_sharing", NULL, NULL, NULL },
{ XSLFI_VARIABLE_DAY_LENGTH, XSCF_NULL, 1, 1, "variable_day_length", NULL, NULL, NULL },
Expand Down
20 changes: 1 addition & 19 deletions src/saveload/vehicle_sl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,24 +368,6 @@ void AfterLoadVehicles(bool part_of_load)
v->SetServiceIntervalIsPercent(c->settings.vehicle.servint_ispercent);
}
}

if (SlXvIsFeatureMissing(XSLFI_VEHICLE_REPAIR_COST)) {
/* repair cost is value for new vehicles and each week +/256 part for old */
FOR_ALL_VEHICLES(v) {
if (!v->IsPrimaryVehicle()) continue;

v->repair_cost = v->value;
for (int w = 0; w < v->age / 7; w++, v->repair_cost += v->repair_cost >> 8);
//DEBUG(misc,0, "eid#%d, value=%lld, weeks=%d/%d, repair cost=%lld",
// v->engine_type, (int64)v->value, v->age, v->max_age, (int64)v->repair_cost );

if (v->age > v->max_age) {
Date weeks = (v->age - v->max_age) / 7;
for (int w = 0; w < weeks; w++, v->repair_cost += v->repair_cost >> 8);
//DEBUG(misc,0, "OLD: value=%lld, weeks=%d, repair cost=%lld", (int64)v->value, weeks, (int64)v->repair_cost );
}
}
}
}

CheckValidVehicles();
Expand Down Expand Up @@ -714,7 +696,7 @@ const SaveLoad *GetVehicleDescription(VehicleType vt)
SLEG_CONDVAR( _cargo_loaded_at_xy, SLE_UINT32, 51, 67),
SLE_CONDVAR(Vehicle, value, SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
SLE_CONDVAR(Vehicle, value, SLE_INT64, 65, SL_MAX_VERSION),
SLE_CONDVAR_X(Vehicle, repair_cost, SLE_INT64, 0, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_VEHICLE_REPAIR_COST)),
SLE_CONDNULL_X(8, 0, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_VEHICLE_REPAIR_COST, 1, 1)),

SLE_CONDVAR(Vehicle, random_bits, SLE_UINT8, 2, SL_MAX_VERSION),
SLE_CONDVAR(Vehicle, waiting_triggers, SLE_UINT8, 2, SL_MAX_VERSION),
Expand Down
8 changes: 4 additions & 4 deletions src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,11 @@ void VehicleServiceInDepot(Vehicle *v)
}
assert(type != INVALID_EXPENSES);

Money vehicle_new_value = v->GetEngine()->GetCost();

// The static cast is to fix compilation on (old) MSVC as the overload for OverflowSafeInt operator / is ambiguous.
Money repair_cost = (v->breakdowns_since_last_service * v->repair_cost / static_cast<uint>(_settings_game.vehicle.repair_cost)) + 1;
Money repair_cost = (v->breakdowns_since_last_service * vehicle_new_value / static_cast<uint>(_settings_game.vehicle.repair_cost)) + 1;
if (v->age > v->max_age) repair_cost <<= 1;
CommandCost cost(type, repair_cost);
v->First()->profit_this_year -= cost.GetCost() << 8;
SubtractMoneyFromCompany(cost);
Expand Down Expand Up @@ -1379,9 +1382,6 @@ Vehicle *CheckClickOnVehicle(const ViewPort *vp, int x, int y)
void DecreaseVehicleValue(Vehicle *v)
{
v->value -= v->value >> 8;
if ( v->age > v->max_age ) { // double cost for each max_age days after max_age
v->repair_cost += v->repair_cost >> 8;
}
SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
}

Expand Down
1 change: 0 additions & 1 deletion src/vehicle_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ struct Vehicle : VehiclePool::PoolItem<&_vehicle_pool>, BaseVehicle, BaseConsist
Money profit_last_year; ///< Profit last year << 8, low 8 bits are fract
Money profit_lifetime; ///< Profit lifetime << 8, low 8 bits are fract
Money value; ///< Value of the vehicle
Money repair_cost; ///< Cost to repair one breakdown

CargoPayment *cargo_payment; ///< The cargo payment we're currently in

Expand Down
1 change: 0 additions & 1 deletion src/vehicle_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ CommandCost CmdBuildVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
if (value.Succeeded() && flags & DC_EXEC) {
v->unitnumber = unit_num;
v->value = value.GetCost();
v->repair_cost = value.GetCost();

InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
InvalidateWindowClassesData(GetWindowClassForVehicleType(type), 0);
Expand Down

0 comments on commit 7ecdebd

Please sign in to comment.