diff --git a/src/aircraft.h b/src/aircraft.h index b9704c4ee9..2aa05ace57 100644 --- a/src/aircraft.h +++ b/src/aircraft.h @@ -124,6 +124,7 @@ struct Aircraft : public Vehicle { Money GetRunningCost() const { return AircraftVehInfo(this->engine_type)->running_cost * _price.aircraft_running; } bool IsInDepot() const { return (this->vehstatus & VS_HIDDEN) != 0 && IsHangarTile(this->tile); } void Tick(); + void OnNewDay(); }; #endif /* AIRCRAFT_H */ diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index 1f5083e847..7df232fae5 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -723,27 +723,27 @@ static void CheckIfAircraftNeedsService(Vehicle *v) } } -void OnNewDay_Aircraft(Vehicle *v) +void Aircraft::OnNewDay() { - if (!IsNormalAircraft(v)) return; + if (!IsNormalAircraft(this)) return; - if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v); + if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this); - CheckOrders(v); + CheckOrders(this); - CheckVehicleBreakdown(v); - AgeVehicle(v); - CheckIfAircraftNeedsService(v); + CheckVehicleBreakdown(this); + AgeVehicle(this); + CheckIfAircraftNeedsService(this); - if (v->vehstatus & VS_STOPPED) return; + if (this->vehstatus & VS_STOPPED) return; - CommandCost cost = CommandCost(EXPENSES_AIRCRAFT_RUN, GetVehicleProperty(v, 0x0E, AircraftVehInfo(v->engine_type)->running_cost) * _price.aircraft_running / 364); + CommandCost cost = CommandCost(EXPENSES_AIRCRAFT_RUN, GetVehicleProperty(this, 0x0E, AircraftVehInfo(this->engine_type)->running_cost) * _price.aircraft_running / 364); - v->profit_this_year -= cost.GetCost() >> 8; + this->profit_this_year -= cost.GetCost() >> 8; - SubtractMoneyFromPlayerFract(v->owner, cost); + SubtractMoneyFromPlayerFract(this->owner, cost); - InvalidateWindow(WC_VEHICLE_DETAILS, v->index); + InvalidateWindow(WC_VEHICLE_DETAILS, this->index); InvalidateWindowClasses(WC_AIRCRAFT_LIST); } diff --git a/src/date.cpp b/src/date.cpp index e71ddd7c9b..f382fc416f 100644 --- a/src/date.cpp +++ b/src/date.cpp @@ -165,24 +165,6 @@ Date ConvertYMDToDate(Year year, Month month, Day day) /** Functions used by the IncreaseDate function */ -extern void OnNewDay_Train(Vehicle *v); -extern void OnNewDay_RoadVeh(Vehicle *v); -extern void OnNewDay_Aircraft(Vehicle *v); -extern void OnNewDay_Ship(Vehicle *v); -static void OnNewDay_EffectVehicle(Vehicle *v) { /* empty */ } -extern void OnNewDay_DisasterVehicle(Vehicle *v); - -typedef void OnNewVehicleDayProc(Vehicle *v); - -static OnNewVehicleDayProc * _on_new_vehicle_day_proc[] = { - OnNewDay_Train, - OnNewDay_RoadVeh, - OnNewDay_Ship, - OnNewDay_Aircraft, - OnNewDay_EffectVehicle, - OnNewDay_DisasterVehicle, -}; - extern void WaypointsDailyLoop(); extern void ChatMessageDailyLoop(); extern void EnginesDailyLoop(); @@ -225,7 +207,7 @@ static void RunVehicleDayProc(uint daytick) if (v->IsValid()) { /* Call the 32-day callback if needed */ CheckVehicle32Day(v); - _on_new_vehicle_day_proc[v->type](v); + v->OnNewDay(); } } } diff --git a/src/disaster_cmd.cpp b/src/disaster_cmd.cpp index bc4b11050f..42ebc9ba25 100644 --- a/src/disaster_cmd.cpp +++ b/src/disaster_cmd.cpp @@ -749,12 +749,6 @@ void DisasterVehicle::Tick() _disastervehicle_tick_procs[this->subtype](this); } - -void OnNewDay_DisasterVehicle(Vehicle *v) -{ - // not used -} - typedef void DisasterInitProc(); diff --git a/src/roadveh.h b/src/roadveh.h index fbcde1bdfe..3beced163d 100644 --- a/src/roadveh.h +++ b/src/roadveh.h @@ -75,6 +75,7 @@ struct RoadVehicle : public Vehicle { Money GetRunningCost() const { return RoadVehInfo(this->engine_type)->running_cost * _price.roadveh_running; } bool IsInDepot() const { return this->u.road.state == RVSB_IN_DEPOT; } void Tick(); + void OnNewDay(); }; byte GetRoadVehLength(const Vehicle *v); diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 45216f88cf..f374197a32 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -2004,33 +2004,33 @@ static void CheckIfRoadVehNeedsService(Vehicle *v) InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); } -void OnNewDay_RoadVeh(Vehicle *v) +void RoadVehicle::OnNewDay() { CommandCost cost(EXPENSES_ROADVEH_RUN); - if (!IsRoadVehFront(v)) return; + if (!IsRoadVehFront(this)) return; - if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v); - if (v->u.road.blocked_ctr == 0) CheckVehicleBreakdown(v); + if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this); + if (this->u.road.blocked_ctr == 0) CheckVehicleBreakdown(this); - AgeVehicle(v); - CheckIfRoadVehNeedsService(v); + AgeVehicle(this); + CheckIfRoadVehNeedsService(this); - CheckOrders(v); + CheckOrders(this); /* Current slot has expired */ - if (v->current_order.type == OT_GOTO_STATION && v->u.road.slot != NULL && v->u.road.slot_age-- == 0) { + if (this->current_order.type == OT_GOTO_STATION && this->u.road.slot != NULL && this->u.road.slot_age-- == 0) { DEBUG(ms, 3, "Slot expired for vehicle %d (index %d) at stop 0x%X", - v->unitnumber, v->index, v->u.road.slot->xy); - ClearSlot(v); + this->unitnumber, this->index, this->u.road.slot->xy); + ClearSlot(this); } - if (v->vehstatus & VS_STOPPED) return; + if (this->vehstatus & VS_STOPPED) return; /* update destination */ - if (v->current_order.type == OT_GOTO_STATION && v->u.road.slot == NULL && !(v->vehstatus & VS_CRASHED)) { - Station *st = GetStation(v->current_order.dest); - RoadStop *rs = st->GetPrimaryRoadStop(v); + if (this->current_order.type == OT_GOTO_STATION && this->u.road.slot == NULL && !(this->vehstatus & VS_CRASHED)) { + Station *st = GetStation(this->current_order.dest); + RoadStop *rs = st->GetPrimaryRoadStop(this); RoadStop *best = NULL; if (rs != NULL) { @@ -2040,16 +2040,16 @@ void OnNewDay_RoadVeh(Vehicle *v) * 2) we're somewhere close to the station rectangle (to make sure we do assign * slots even if the station and its road stops are incredibly spread out) */ - if (DistanceManhattan(v->tile, rs->xy) < 16 || st->rect.PtInExtendedRect(TileX(v->tile), TileY(v->tile), 2)) { + if (DistanceManhattan(this->tile, rs->xy) < 16 || st->rect.PtInExtendedRect(TileX(this->tile), TileY(this->tile), 2)) { uint dist, badness; uint minbadness = UINT_MAX; DEBUG(ms, 2, "Attempting to obtain a slot for vehicle %d (index %d) at station %d (0x%X)", - v->unitnumber, v->index, st->index, st->xy + this->unitnumber, this->index, st->index, st->xy ); /* Now we find the nearest road stop that has a free slot */ - for (; rs != NULL; rs = rs->GetNextRoadStop(v)) { - dist = RoadFindPathToStop(v, rs->xy); + for (; rs != NULL; rs = rs->GetNextRoadStop(this)) { + dist = RoadFindPathToStop(this, rs->xy); if (dist == UINT_MAX) { DEBUG(ms, 4, " stop 0x%X is unreachable, not treating further", rs->xy); continue; @@ -2070,29 +2070,29 @@ void OnNewDay_RoadVeh(Vehicle *v) best->num_vehicles++; DEBUG(ms, 3, "Assigned to stop 0x%X", best->xy); - v->u.road.slot = best; - v->dest_tile = best->xy; - v->u.road.slot_age = 14; + this->u.road.slot = best; + this->dest_tile = best->xy; + this->u.road.slot_age = 14; } else { DEBUG(ms, 3, "Could not find a suitable stop"); } } else { DEBUG(ms, 5, "Distance from station too far. Postponing slotting for vehicle %d (index %d) at station %d, (0x%X)", - v->unitnumber, v->index, st->index, st->xy); + this->unitnumber, this->index, st->index, st->xy); } } else { DEBUG(ms, 4, "No road stop for vehicle %d (index %d) at station %d (0x%X)", - v->unitnumber, v->index, st->index, st->xy); + this->unitnumber, this->index, st->index, st->xy); } } - cost = CommandCost(EXPENSES_ROADVEH_RUN, RoadVehInfo(v->engine_type)->running_cost * _price.roadveh_running / 364); + cost = CommandCost(EXPENSES_ROADVEH_RUN, RoadVehInfo(this->engine_type)->running_cost * _price.roadveh_running / 364); - v->profit_this_year -= cost.GetCost() >> 8; + this->profit_this_year -= cost.GetCost() >> 8; - SubtractMoneyFromPlayerFract(v->owner, cost); + SubtractMoneyFromPlayerFract(this->owner, cost); - InvalidateWindow(WC_VEHICLE_DETAILS, v->index); + InvalidateWindow(WC_VEHICLE_DETAILS, this->index); InvalidateWindowClasses(WC_ROADVEH_LIST); } diff --git a/src/ship.h b/src/ship.h index 923cfe6d38..2d96cbe1d3 100644 --- a/src/ship.h +++ b/src/ship.h @@ -41,6 +41,7 @@ struct Ship: public Vehicle { Money GetRunningCost() const { return ShipVehInfo(this->engine_type)->running_cost * _price.ship_running; } bool IsInDepot() const { return this->u.ship.state == 0x80; } void Tick(); + void OnNewDay(); }; #endif /* SHIP_H */ diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 11d7f572eb..530512727d 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -170,27 +170,27 @@ static void CheckIfShipNeedsService(Vehicle *v) InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); } -void OnNewDay_Ship(Vehicle *v) +void Ship::OnNewDay() { CommandCost cost(EXPENSES_SHIP_RUN); - if ((++v->day_counter & 7) == 0) - DecreaseVehicleValue(v); + if ((++this->day_counter & 7) == 0) + DecreaseVehicleValue(this); - CheckVehicleBreakdown(v); - AgeVehicle(v); - CheckIfShipNeedsService(v); + CheckVehicleBreakdown(this); + AgeVehicle(this); + CheckIfShipNeedsService(this); - CheckOrders(v); + CheckOrders(this); - if (v->vehstatus & VS_STOPPED) return; + if (this->vehstatus & VS_STOPPED) return; - cost.AddCost(GetVehicleProperty(v, 0x0F, ShipVehInfo(v->engine_type)->running_cost) * _price.ship_running / 364); - v->profit_this_year -= cost.GetCost() >> 8; + cost.AddCost(GetVehicleProperty(this, 0x0F, ShipVehInfo(this->engine_type)->running_cost) * _price.ship_running / 364); + this->profit_this_year -= cost.GetCost() >> 8; - SubtractMoneyFromPlayerFract(v->owner, cost); + SubtractMoneyFromPlayerFract(this->owner, cost); - InvalidateWindow(WC_VEHICLE_DETAILS, v->index); + InvalidateWindow(WC_VEHICLE_DETAILS, this->index); /* we need this for the profit */ InvalidateWindowClasses(WC_SHIPS_LIST); } diff --git a/src/train.h b/src/train.h index 7a373913cf..05b7951826 100644 --- a/src/train.h +++ b/src/train.h @@ -304,6 +304,7 @@ struct Train : public Vehicle { bool IsInDepot() const { return CheckTrainInDepot(this, false) != -1; } bool IsStoppedInDepot() const { return CheckTrainStoppedInDepot(this) >= 0; } void Tick(); + void OnNewDay(); }; #endif /* TRAIN_H */ diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 8916420708..612f383b16 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -3625,38 +3625,38 @@ static void CheckIfTrainNeedsService(Vehicle *v) InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); } -void OnNewDay_Train(Vehicle *v) +void Train::OnNewDay() { - if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v); + if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this); - if (IsFrontEngine(v)) { - CheckVehicleBreakdown(v); - AgeVehicle(v); + if (IsFrontEngine(this)) { + CheckVehicleBreakdown(this); + AgeVehicle(this); - CheckIfTrainNeedsService(v); + CheckIfTrainNeedsService(this); - CheckOrders(v); + CheckOrders(this); /* update destination */ - if (v->current_order.type == OT_GOTO_STATION) { - TileIndex tile = GetStation(v->current_order.dest)->train_tile; - if (tile != 0) v->dest_tile = tile; + if (this->current_order.type == OT_GOTO_STATION) { + TileIndex tile = GetStation(this->current_order.dest)->train_tile; + if (tile != 0) this->dest_tile = tile; } - if ((v->vehstatus & VS_STOPPED) == 0) { + if ((this->vehstatus & VS_STOPPED) == 0) { /* running costs */ - CommandCost cost(EXPENSES_TRAIN_RUN, v->GetRunningCost() / 364); + CommandCost cost(EXPENSES_TRAIN_RUN, this->GetRunningCost() / 364); - v->profit_this_year -= cost.GetCost() >> 8; + this->profit_this_year -= cost.GetCost() >> 8; - SubtractMoneyFromPlayerFract(v->owner, cost); + SubtractMoneyFromPlayerFract(this->owner, cost); - InvalidateWindow(WC_VEHICLE_DETAILS, v->index); + InvalidateWindow(WC_VEHICLE_DETAILS, this->index); InvalidateWindowClasses(WC_TRAINS_LIST); } - } else if (IsTrainEngine(v)) { + } else if (IsTrainEngine(this)) { /* Also age engines that aren't front engines */ - AgeVehicle(v); + AgeVehicle(this); } } diff --git a/src/vehicle_base.h b/src/vehicle_base.h index f6eedf51ae..cce77aa4dd 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -425,6 +425,11 @@ public: */ virtual void Tick() {}; + /** + * Calls the new day handler of the vehicle + */ + virtual void OnNewDay() {}; + /** * Gets the running cost of a vehicle that can be sent into SetDParam for string processing. * @return the vehicle's running cost