Mercurial > hg > openttd
changeset 12114:f020ec6be498 draft
(svn r16527) -Codechange: use static member functions instead of simple casts when converting Vehicle to specialised vehicle types. Includes safety check
author | smatz <smatz@openttd.org> |
---|---|
date | Sat, 06 Jun 2009 16:54:22 +0000 |
parents | d1451dc437a3 |
children | 6318d41ef9ef |
files | src/ai/api/ai_vehicle.cpp src/aircraft_gui.cpp src/articulated_vehicles.cpp src/autoreplace_cmd.cpp src/depot_gui.cpp src/disaster_cmd.cpp src/economy.cpp src/newgrf_engine.cpp src/npf.cpp src/openttd.cpp src/order_cmd.cpp src/pbs.cpp src/rail_cmd.cpp src/road_cmd.cpp src/roadveh_cmd.cpp src/roadveh_gui.cpp src/saveload/afterload.cpp src/saveload/oldloader_sl.cpp src/saveload/vehicle_sl.cpp src/signal.cpp src/station_cmd.cpp src/train_cmd.cpp src/train_gui.cpp src/tunnelbridge_cmd.cpp src/vehicle.cpp src/vehicle_base.h src/vehicle_cmd.cpp src/vehicle_gui.cpp src/vehicle_type.h src/vehiclelist.cpp src/water_cmd.cpp src/yapf/follow_track.hpp src/yapf/yapf_costrail.hpp src/yapf/yapf_destrail.hpp src/yapf/yapf_rail.cpp src/yapf/yapf_road.cpp |
diffstat | 36 files changed, 185 insertions(+), 161 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ai/api/ai_vehicle.cpp +++ b/src/ai/api/ai_vehicle.cpp @@ -46,11 +46,11 @@ case VEH_ROAD: { uint total_length = 0; for (const Vehicle *u = v; u != NULL; u = u->Next()) { - total_length += ((const RoadVehicle *)u)->rcache.cached_veh_length; + total_length += ::RoadVehicle::From(u)->rcache.cached_veh_length; } return total_length; } - case VEH_TRAIN: return ((const Train *)v)->tcache.cached_total_length; + case VEH_TRAIN: return ::Train::From(v)->tcache.cached_total_length; default: return -1; } } @@ -86,11 +86,11 @@ EnforcePrecondition(false, ::Vehicle::Get(source_vehicle_id)->type == VEH_TRAIN); EnforcePrecondition(false, dest_vehicle_id == -1 || ::Vehicle::Get(dest_vehicle_id)->type == VEH_TRAIN); - const Train *v = (const Train *)::Vehicle::Get(source_vehicle_id); + const Train *v = ::Train::Get(source_vehicle_id); while (source_wagon-- > 0) v = GetNextUnit(v); const Train *w = NULL; if (dest_vehicle_id != -1) { - w = (const Train *)::Vehicle::Get(dest_vehicle_id); + w = ::Train::Get(dest_vehicle_id); while (dest_wagon-- > 0) w = GetNextUnit(w); } @@ -137,7 +137,7 @@ EnforcePrecondition(false, IsValidVehicle(vehicle_id) && wagon < GetNumWagons(vehicle_id)); EnforcePrecondition(false, ::Vehicle::Get(vehicle_id)->type == VEH_TRAIN); - const Train *v = (const Train *)::Vehicle::Get(vehicle_id); + const Train *v = ::Train::Get(vehicle_id); while (wagon-- > 0) v = GetNextUnit(v); return AIObject::DoCommand(0, v->index, sell_attached_wagons ? 1 : 0, CMD_SELL_RAIL_WAGON); @@ -244,7 +244,7 @@ const Vehicle *v = ::Vehicle::Get(vehicle_id); if (v->type == VEH_TRAIN) { - while (wagon-- > 0) v = GetNextUnit((const Train *)v); + while (wagon-- > 0) v = GetNextUnit(::Train::From(v)); } return v->engine_type; } @@ -282,7 +282,7 @@ const Vehicle *v = ::Vehicle::Get(vehicle_id); if (v->type == VEH_TRAIN) { - while (wagon-- > 0) v = GetNextUnit((const Train *)v); + while (wagon-- > 0) v = GetNextUnit(::Train::From(v)); } return v->age; } @@ -369,7 +369,7 @@ if (!IsValidVehicle(vehicle_id)) return AIRoad::ROADTYPE_INVALID; if (GetVehicleType(vehicle_id) != VT_ROAD) return AIRoad::ROADTYPE_INVALID; - return (AIRoad::RoadType)((RoadVehicle*)::Vehicle::Get(vehicle_id))->roadtype; + return (AIRoad::RoadType)(::RoadVehicle::Get(vehicle_id))->roadtype; } /* static */ int32 AIVehicle::GetCapacity(VehicleID vehicle_id, CargoID cargo) @@ -412,8 +412,8 @@ const Vehicle *v = ::Vehicle::Get(vehicle_id); switch (v->type) { - case VEH_ROAD: return RoadVehHasArticPart(v); - case VEH_TRAIN: return EngineHasArticPart(v); + case VEH_ROAD: return ::RoadVehHasArticPart(v); + case VEH_TRAIN: return ::EngineHasArticPart(v); default: NOT_REACHED(); } }
--- a/src/aircraft_gui.cpp +++ b/src/aircraft_gui.cpp @@ -67,7 +67,7 @@ SpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v); DrawSprite(v->GetImage(DIR_W), pal, x + 25, y + 10); if (v->subtype == AIR_HELICOPTER) { - const Aircraft *a = (const Aircraft *)v; + const Aircraft *a = Aircraft::From(v); SpriteID rotor_sprite = GetCustomRotorSprite(a, true); if (rotor_sprite == 0) rotor_sprite = SPR_ROTOR_STOPPED; DrawSprite(rotor_sprite, PAL_NONE, x + 25, y + 5);
--- a/src/articulated_vehicles.cpp +++ b/src/articulated_vehicles.cpp @@ -208,7 +208,7 @@ switch (v->type) { case VEH_TRAIN: - v = (EngineHasArticPart((const Train *)v) ? GetNextArticPart((const Train *)v) : NULL); + v = (EngineHasArticPart(Train::From(v)) ? GetNextArticPart(Train::From(v)) : NULL); break; case VEH_ROAD: @@ -256,7 +256,7 @@ switch (v->type) { case VEH_TRAIN: - v = (EngineHasArticPart((const Train *)v) ? GetNextArticPart((const Train *)v) : NULL); + v = (EngineHasArticPart(Train::From(v)) ? GetNextArticPart(Train::From(v)) : NULL); break; case VEH_ROAD: @@ -305,7 +305,7 @@ default: NOT_REACHED(); case VEH_TRAIN: { - Train *front = (Train *)first; + Train *front = Train::From(first); Train *t = new Train(); v->SetNext(t); v = t; @@ -328,7 +328,7 @@ } break; case VEH_ROAD: { - RoadVehicle *front = (RoadVehicle *)first; + RoadVehicle *front = RoadVehicle::From(first); RoadVehicle *rv = new RoadVehicle(); v->SetNext(rv); v = rv;
--- a/src/autoreplace_cmd.cpp +++ b/src/autoreplace_cmd.cpp @@ -92,18 +92,18 @@ assert(!part_of_chain || new_head->IsPrimaryVehicle()); /* Loop through source parts */ for (Vehicle *src = old_veh; src != NULL; src = src->Next()) { - if (!part_of_chain && src->type == VEH_TRAIN && src != old_veh && src != ((Train *)old_veh)->other_multiheaded_part && !IsArticulatedPart(src)) { + if (!part_of_chain && src->type == VEH_TRAIN && src != old_veh && src != Train::From(old_veh)->other_multiheaded_part && !IsArticulatedPart(src)) { /* Skip vehicles, which do not belong to old_veh */ - src = GetLastEnginePart((Train *)src); + src = GetLastEnginePart(Train::From(src)); continue; } if (src->cargo_type >= NUM_CARGO || src->cargo.Count() == 0) continue; /* Find free space in the new chain */ for (Vehicle *dest = new_head; dest != NULL && src->cargo.Count() > 0; dest = dest->Next()) { - if (!part_of_chain && dest->type == VEH_TRAIN && dest != new_head && dest != ((Train *)new_head)->other_multiheaded_part && !IsArticulatedPart(dest)) { + if (!part_of_chain && dest->type == VEH_TRAIN && dest != new_head && dest != Train::From(new_head)->other_multiheaded_part && !IsArticulatedPart(dest)) { /* Skip vehicles, which do not belong to new_head */ - dest = GetLastEnginePart((Train *)dest); + dest = GetLastEnginePart(Train::From(dest)); continue; } if (dest->cargo_type != src->cargo_type) continue; @@ -116,7 +116,7 @@ } /* Update train weight etc., the old vehicle will be sold anyway */ - if (part_of_chain && new_head->type == VEH_TRAIN) TrainConsistChanged((Train *)new_head, true); + if (part_of_chain && new_head->type == VEH_TRAIN) TrainConsistChanged(Train::From(new_head), true); } /** @@ -269,7 +269,7 @@ } /* Try to reverse the vehicle, but do not care if it fails as the new type might not be reversible */ - if (new_veh->type == VEH_TRAIN && HasBit(((Train *)old_veh)->flags, VRF_REVERSE_DIRECTION)) { + if (new_veh->type == VEH_TRAIN && HasBit(Train::From(old_veh)->flags, VRF_REVERSE_DIRECTION)) { DoCommand(0, new_veh->index, true, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION); } @@ -403,10 +403,10 @@ if (old_head->type == VEH_TRAIN) { /* Store the length of the old vehicle chain, rounded up to whole tiles */ - uint16 old_total_length = (((Train *)old_head)->tcache.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE; + uint16 old_total_length = (Train::From(old_head)->tcache.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE; int num_units = 0; ///< Number of units in the chain - for (Train *w = (Train *)old_head; w != NULL; w = GetNextUnit(w)) num_units++; + for (Train *w = Train::From(old_head); w != NULL; w = GetNextUnit(w)) num_units++; Train **old_vehs = CallocT<Train *>(num_units); ///< Will store vehicles of the old chain in their order Train **new_vehs = CallocT<Train *>(num_units); ///< New vehicles corresponding to old_vehs or NULL if no replacement @@ -416,7 +416,7 @@ * Note: The replacement vehicles can only successfully build as long as the old vehicles are still in their chain */ int i; Train *w; - for (w = (Train *)old_head, i = 0; w != NULL; w = GetNextUnit(w), i++) { + for (w = Train::From(old_head), i = 0; w != NULL; w = GetNextUnit(w), i++) { assert(i < num_units); old_vehs[i] = w; @@ -432,7 +432,7 @@ /* Note: When autoreplace has already failed here, old_vehs[] is not completely initialized. But it is also not needed. */ if (cost.Succeeded()) { /* Separate the head, so we can start constructing the new chain */ - Train *second = GetNextUnit((Train *)old_head); + Train *second = GetNextUnit(Train::From(old_head)); if (second != NULL) cost.AddCost(MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true)); assert(GetNextUnit(new_head) == NULL); @@ -538,10 +538,10 @@ * Note: The vehicle attach callback is disabled here :) */ if ((flags & DC_EXEC) == 0) { /* Separate the head, so we can reattach the old vehicles */ - Train *second = GetNextUnit((Train *)old_head); + Train *second = GetNextUnit(Train::From(old_head)); if (second != NULL) MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true); - assert(GetNextUnit((Train *)old_head) == NULL); + assert(GetNextUnit(Train::From(old_head)) == NULL); for (int i = num_units - 1; i > 0; i--) { CommandCost ret = MoveVehicle(old_vehs[i], old_head, DC_EXEC | DC_AUTOREPLACE, false); @@ -632,7 +632,7 @@ bool any_replacements = false; while (w != NULL && !any_replacements) { any_replacements = (GetNewEngineType(w, c) != INVALID_ENGINE); - w = (!free_wagon && w->type == VEH_TRAIN ? GetNextUnit((Train *)w) : NULL); + w = (!free_wagon && w->type == VEH_TRAIN ? GetNextUnit(Train::From(w)) : NULL); } if (any_replacements) {
--- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -272,7 +272,7 @@ DrawTrainImage(v, x + 21, sprite_y, this->sel, this->hscroll.cap + 4, this->hscroll.pos); /* Number of wagons relative to a standard length wagon (rounded up) */ - SetDParam(0, (((const Train *)v)->tcache.cached_total_length + 7) / 8); + SetDParam(0, (Train::From(v)->tcache.cached_total_length + 7) / 8); DrawString(this->widget[DEPOT_WIDGET_MATRIX].left, this->widget[DEPOT_WIDGET_MATRIX].right - 1, y + 4, STR_TINY_BLACK, TC_FROMSTRING, SA_RIGHT); // Draw the counter break; @@ -328,7 +328,7 @@ hnum = 8; for (uint num = 0; num < this->vehicle_list.Length(); num++) { const Vehicle *v = this->vehicle_list[num]; - hnum = max(hnum, ((const Train *)v)->tcache.cached_total_length); + hnum = max(hnum, Train::From(v)->tcache.cached_total_length); } /* Always have 1 empty row, so people can change the setting of the train */ SetVScrollCount(w, this->vehicle_list.Length() + this->wagon_list.Length() + 1); @@ -450,7 +450,7 @@ x += skip; /* find the vehicle in this row that was clicked */ - while (v != NULL && (x -= ((const Train *)v)->tcache.cached_veh_length) >= 0) v = v->Next(); + while (v != NULL && (x -= Train::From(v)->tcache.cached_veh_length) >= 0) v = v->Next(); /* if an articulated part was selected, find its parent */ while (v != NULL && IsArticulatedPart(v)) v = v->Previous(); @@ -514,11 +514,11 @@ switch (v->type) { case VEH_TRAIN: - _cursor.short_vehicle_offset = 16 - ((const Train *)v)->tcache.cached_veh_length * 2; + _cursor.short_vehicle_offset = 16 - Train::From(v)->tcache.cached_veh_length * 2; break; case VEH_ROAD: - _cursor.short_vehicle_offset = 16 - ((const RoadVehicle *)v)->rcache.cached_veh_length * 2; + _cursor.short_vehicle_offset = 16 - RoadVehicle::From(v)->rcache.cached_veh_length * 2; break; default:
--- a/src/disaster_cmd.cpp +++ b/src/disaster_cmd.cpp @@ -313,9 +313,8 @@ return false; } else { /* Target a vehicle */ - Vehicle *u_tmp = Vehicle::Get(v->dest_tile); - assert(u_tmp != NULL && u_tmp->type == VEH_ROAD && IsRoadVehFront(u_tmp)); - RoadVehicle *u = (RoadVehicle *)u_tmp; + RoadVehicle *u = RoadVehicle::Get(v->dest_tile); + assert(u != NULL && u->type == VEH_ROAD && IsRoadVehFront(u)); uint dist = Delta(v->x_pos, u->x_pos) + Delta(v->y_pos, u->y_pos);
--- a/src/economy.cpp +++ b/src/economy.cpp @@ -1314,7 +1314,7 @@ /* update stats */ int t; switch (u->type) { - case VEH_TRAIN: t = ((Train *)u)->tcache.cached_max_speed; break; + case VEH_TRAIN: t = Train::From(u)->tcache.cached_max_speed; break; case VEH_ROAD: t = u->max_speed / 2; break; default: t = u->max_speed; break; } @@ -1423,7 +1423,7 @@ if (v->type == VEH_TRAIN) { /* Each platform tile is worth 2 rail vehicles. */ - int overhang = ((Train *)v)->tcache.cached_total_length - st->GetPlatformLength(v->tile) * TILE_SIZE; + int overhang = Train::From(v)->tcache.cached_total_length - st->GetPlatformLength(v->tile) * TILE_SIZE; if (overhang > 0) { unloading_time <<= 1; unloading_time += (overhang * unloading_time) / 8;
--- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -434,9 +434,9 @@ if (!Company::IsValidID(_current_company)) return 0; l = GetEngineLivery(engine, _current_company, INVALID_ENGINE, NULL); } else if (v->type == VEH_TRAIN) { - l = GetEngineLivery(v->engine_type, v->owner, ((const Train *)v)->tcache.first_engine, v); + l = GetEngineLivery(v->engine_type, v->owner, Train::From(v)->tcache.first_engine, v); } else if (v->type == VEH_ROAD) { - l = GetEngineLivery(v->engine_type, v->owner, ((const RoadVehicle *)v)->rcache.first_engine, v); + l = GetEngineLivery(v->engine_type, v->owner, RoadVehicle::From(v)->rcache.first_engine, v); } else { l = GetEngineLivery(v->engine_type, v->owner, INVALID_ENGINE, v); } @@ -534,7 +534,7 @@ memset(common_subtypes, 0, sizeof(common_subtypes)); for (u = v; u != NULL; u = u->Next()) { - if (v->type == VEH_TRAIN) user_def_data |= ((const Train *)u)->tcache.user_def_data; + if (v->type == VEH_TRAIN) user_def_data |= Train::From(u)->tcache.user_def_data; /* Skip empty engines */ if (u->cargo_cap == 0) continue; @@ -590,7 +590,7 @@ uint16 altitude = v->z_pos - w->z_pos; // Aircraft height - shadow height byte airporttype = ATP_TTDP_LARGE; - const Station *st = GetTargetAirportIfValid((Aircraft *)v); + const Station *st = GetTargetAirportIfValid(Aircraft::From(v)); if (st != NULL) { switch (st->airport_type) { @@ -668,7 +668,7 @@ uint16 modflags = 0; if (v->type == VEH_TRAIN) { - const Train *t = (const Train *)v; + const Train *t = Train::From(v); const Train *u = IsTrainWagon(v) && HasBit(v->vehicle_flags, VRF_POWEREDWAGON) ? t->First() : t; RailType railtype = GetRailType(v->tile); bool powered = IsTrainEngine(v) || (IsTrainWagon(v) && HasBit(v->vehicle_flags, VRF_POWEREDWAGON)); @@ -736,7 +736,7 @@ case 0x47: return GB(Engine::Get(v->engine_type)->internal_id, 8, 8); case 0x48: if (v->type != VEH_TRAIN || v->spritenum != 0xFD) return v->spritenum; - return HasBit(((Train *)v)->flags, VRF_REVERSE_DIRECTION) ? 0xFE : 0xFD; + return HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION) ? 0xFE : 0xFD; case 0x49: return v->day_counter; case 0x4A: return v->breakdowns_since_last_service; @@ -768,7 +768,7 @@ /* Vehicle specific properties */ switch (v->type) { case VEH_TRAIN: { - Train *t = (Train *)v; + Train *t = Train::From(v); switch (variable - 0x80) { case 0x62: return t->track; case 0x66: return t->railtype; @@ -784,7 +784,7 @@ } break; case VEH_ROAD: { - RoadVehicle *rv = (RoadVehicle *)v; + RoadVehicle *rv = RoadVehicle::From(v); switch (variable - 0x80) { case 0x62: return rv->state; case 0x64: return rv->blocked_ctr; @@ -797,7 +797,7 @@ } break; case VEH_AIRCRAFT: { - Aircraft *a = (Aircraft *)v; + Aircraft *a = Aircraft::From(v); switch (variable - 0x80) { case 0x62: return MapAircraftMovementState(a); // Current movement state case 0x63: return a->targetairport; // Airport to which the action refers @@ -886,10 +886,10 @@ /* We always use cached value, except for callbacks because the override spriteset * to use may be different than the one cached. It happens for callback 0x15 (refit engine), * as v->cargo_type is temporary changed to the new type */ - group = use_cache ? ((const Train *)v)->tcache.cached_override : GetWagonOverrideSpriteSet(v->engine_type, v->cargo_type, ((const Train *)v)->tcache.first_engine); + group = use_cache ? Train::From(v)->tcache.cached_override : GetWagonOverrideSpriteSet(v->engine_type, v->cargo_type, Train::From(v)->tcache.first_engine); if (group != NULL) return group; } else if (v->type == VEH_ROAD) { - group = GetWagonOverrideSpriteSet(v->engine_type, v->cargo_type, ((const RoadVehicle *)v)->rcache.first_engine); + group = GetWagonOverrideSpriteSet(v->engine_type, v->cargo_type, RoadVehicle::From(v)->rcache.first_engine); if (group != NULL) return group; } } @@ -952,7 +952,7 @@ bool UsesWagonOverride(const Vehicle *v) { assert(v->type == VEH_TRAIN); - return ((const Train *)v)->tcache.cached_override != NULL; + return Train::From(v)->tcache.cached_override != NULL; } /**
--- a/src/npf.cpp +++ b/src/npf.cpp @@ -425,7 +425,7 @@ /** Find any safe and free tile. */ static int32 NPFFindSafeTile(AyStar *as, OpenListNode *current) { - const Train *v = (const Train *)((NPFFindStationOrTileData*)as->user_target)->v; + const Train *v = Train::From(((NPFFindStationOrTileData *)as->user_target)->v); return IsSafeWaitingPosition(v, current->path.node.tile, current->path.node.direction, true, _settings_game.pf.forbid_90_deg) && @@ -506,7 +506,7 @@ if (as->user_target != NULL && ((NPFFindStationOrTileData*)as->user_target)->reserve_path && as->user_data[NPF_TYPE] == TRANSPORT_RAIL) { /* Path reservation is requested. */ - const Train *v = (const Train *)((NPFFindStationOrTileData*)as->user_target)->v; + const Train *v = Train::From(((NPFFindStationOrTileData *)as->user_target)->v); const PathNode *target = FindSafePosition(¤t->path, v); ftd->node = target->node;
--- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1119,7 +1119,7 @@ switch (v->type) { case VEH_ROAD: { - RoadVehicle *rv = (RoadVehicle *)v; + RoadVehicle *rv = RoadVehicle::From(v); RoadVehicleCache cache = rv->rcache; RoadVehUpdateCache(rv); @@ -1130,7 +1130,7 @@ case VEH_TRAIN: { uint length = 0; - Train *t = (Train *)v; + Train *t = Train::From(v); for (Vehicle *u = t; u != NULL; u = u->Next()) length++; TrainCache *wagons = MallocT<TrainCache>(length); @@ -1151,7 +1151,7 @@ } break; case VEH_AIRCRAFT: { - Aircraft *a = (Aircraft *)v; + Aircraft *a = Aircraft::From(v); AircraftCache cache = a->acache; UpdateAircraftCache(a);
--- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -21,6 +21,7 @@ #include "settings_type.h" #include "core/pool_func.hpp" #include "aircraft.h" +#include "roadveh.h" #include "table/strings.h" @@ -734,7 +735,7 @@ if (flags & DC_EXEC) { v->cur_order_index = sel_ord; - if (v->type == VEH_ROAD) ClearSlot((RoadVehicle *)v); + if (v->type == VEH_ROAD) ClearSlot(RoadVehicle::From(v)); if (v->current_order.IsType(OT_LOADING)) v->LeaveStation(); @@ -1360,7 +1361,7 @@ case VEH_TRAIN: return st->train_tile; case VEH_AIRCRAFT: return st->airport_tile; case VEH_SHIP: return st->dock_tile; - case VEH_ROAD: return st->GetPrimaryRoadStop((RoadVehicle *)v)->xy; + case VEH_ROAD: return st->GetPrimaryRoadStop(RoadVehicle::From(v))->xy; } } @@ -1615,7 +1616,7 @@ if (v->type == VEH_TRAIN && reverse) DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION); if (v->type == VEH_AIRCRAFT) { - Aircraft *a = (Aircraft *)v; + Aircraft *a = Aircraft::From(v); if (a->state == FLYING && a->targetairport != destination) { /* The aircraft is now heading for a different hangar than the next in the orders */ extern void AircraftNextAirportPos_and_Order(Aircraft *a); @@ -1734,13 +1735,13 @@ if (v->type == VEH_AIRCRAFT) { /* Aircraft do something vastly different here, so handle separately */ extern void HandleMissingAircraftOrders(Aircraft *v); - HandleMissingAircraftOrders((Aircraft *)v); + HandleMissingAircraftOrders(Aircraft::From(v)); return false; } v->current_order.Free(); v->dest_tile = 0; - if (v->type == VEH_ROAD) ClearSlot((RoadVehicle *)v); + if (v->type == VEH_ROAD) ClearSlot(RoadVehicle::From(v)); return false; }
--- a/src/pbs.cpp +++ b/src/pbs.cpp @@ -235,7 +235,7 @@ if (v->type != VEH_TRAIN || (v->vehstatus & VS_CRASHED)) return NULL; - Train *t = (Train *)v; + Train *t = Train::From(v); if (HasBit((TrackBits)t->track, TrackdirToTrack(info->res.trackdir))) { t = t->First();
--- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -92,7 +92,7 @@ if (v->type != VEH_TRAIN) return NULL; - Train *t = (Train *)v; + Train *t = Train::From(v); if ((t->track != rail_bits) && !TracksOverlap(t->track | rail_bits)) return NULL; _error_message = VehicleInTheWayErrMsg(v); @@ -1235,7 +1235,7 @@ if (v->type == VEH_TRAIN && !IsArticulatedPart(v)) { const RailVehicleInfo *rvi = RailVehInfo(v->engine_type); - if (GetVehicleProperty(v, 0x0B, rvi->power) != 0) TrainPowerChanged((Train *)v->First()); + if (GetVehicleProperty(v, 0x0B, rvi->power) != 0) TrainPowerChanged(Train::From(v)->First()); } return NULL; @@ -2461,7 +2461,7 @@ /* this routine applies only to trains in depot tiles */ if (u->type != VEH_TRAIN || !IsRailDepotTile(tile)) return VETSB_CONTINUE; - Train *v = (Train *)u; + Train *v = Train::From(u); /* depot direction */ dir = GetRailDepotDirection(tile);
--- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -1541,7 +1541,7 @@ case ROAD_TILE_DEPOT: { if (v->type != VEH_ROAD) break; - RoadVehicle *rv = (RoadVehicle *)v; + RoadVehicle *rv = RoadVehicle::From(v); if (rv->frame == RVC_DEPOT_STOP_FRAME && _roadveh_enter_depot_dir[GetRoadDepotDirection(tile)] == rv->state) { rv->state = RVSB_IN_DEPOT;
--- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -762,7 +762,7 @@ if (++front->blocked_ctr > 1480) return NULL; - return (RoadVehicle *)rvf.best; + return RoadVehicle::From(rvf.best); } static void RoadVehArrivesAt(const RoadVehicle *v, Station *st)
--- a/src/roadveh_gui.cpp +++ b/src/roadveh_gui.cpp @@ -130,7 +130,7 @@ int highlight_w = 0; for (int dx = 0; v != NULL && dx < max_length ; v = v->Next()) { - int width = ((const RoadVehicle *)v)->rcache.cached_veh_length; + int width = RoadVehicle::From(v)->rcache.cached_veh_length; if (dx + width > 0 && dx <= max_length) { SpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
--- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -912,9 +912,9 @@ continue; } if (v->type == VEH_TRAIN) { - ((Train *)v)->track = TRACK_BIT_WORMHOLE; + Train::From(v)->track = TRACK_BIT_WORMHOLE; } else { - ((RoadVehicle *)v)->state = RVSB_WORMHOLE; + RoadVehicle::From(v)->state = RVSB_WORMHOLE; } } }
--- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -177,7 +177,7 @@ /* We haven't used this bit for stations for ages */ if (v->type == VEH_ROAD) { - RoadVehicle *rv = (RoadVehicle *)v; + RoadVehicle *rv = RoadVehicle::From(v); if (rv->state != RVSB_IN_DEPOT && rv->state != RVSB_WORMHOLE) { ClrBit(rv->state, RVS_IS_STOPPING); } @@ -1316,7 +1316,7 @@ }; if (v->spritenum / 2 >= lengthof(spriteset_rail)) return false; v->spritenum = spriteset_rail[v->spritenum / 2]; // adjust railway sprite set offset - ((Train *)v)->railtype = type == 0x25 ? 1 : 0; // monorail / rail + Train::From(v)->railtype = type == 0x25 ? 1 : 0; // monorail / rail break; }
--- a/src/saveload/vehicle_sl.cpp +++ b/src/saveload/vehicle_sl.cpp @@ -242,8 +242,8 @@ if (part_of_load) v->fill_percent_te_id = INVALID_TE_ID; v->first = NULL; - if (v->type == VEH_TRAIN) ((Train *)v)->tcache.first_engine = INVALID_ENGINE; - if (v->type == VEH_ROAD) ((RoadVehicle *)v)->rcache.first_engine = INVALID_ENGINE; + if (v->type == VEH_TRAIN) Train::From(v)->tcache.first_engine = INVALID_ENGINE; + if (v->type == VEH_ROAD) RoadVehicle::From(v)->rcache.first_engine = INVALID_ENGINE; v->cargo.InvalidateCache(); } @@ -308,10 +308,10 @@ assert(v->first != NULL); if (v->type == VEH_TRAIN && (IsFrontEngine(v) || IsFreeWagon(v))) { - if (IsFrontEngine(v)) ((Train *)v)->tcache.last_speed = v->cur_speed; // update displayed train speed - TrainConsistChanged((Train *)v, false); + if (IsFrontEngine(v)) Train::From(v)->tcache.last_speed = v->cur_speed; // update displayed train speed + TrainConsistChanged(Train::From(v), false); } else if (v->type == VEH_ROAD && IsRoadVehFront(v)) { - RoadVehUpdateCache((RoadVehicle *)v); + RoadVehUpdateCache(RoadVehicle::From(v)); } } @@ -335,7 +335,7 @@ FOR_ALL_VEHICLES(v) { switch (v->type) { case VEH_ROAD: { - RoadVehicle *rv = (RoadVehicle *)v; + RoadVehicle *rv = RoadVehicle::From(v); rv->roadtype = HasBit(EngInfo(v->First()->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD; rv->compatible_roadtypes = RoadTypeToRoadTypes(rv->roadtype); } @@ -356,10 +356,10 @@ /* In the case of a helicopter we will update the rotor sprites */ if (v->subtype == AIR_HELICOPTER) { Vehicle *rotor = shadow->Next(); - rotor->cur_image = GetRotorImage((Aircraft *)v); + rotor->cur_image = GetRotorImage(Aircraft::From(v)); } - UpdateAircraftCache((Aircraft *)v); + UpdateAircraftCache(Aircraft::From(v)); } break; default: break;
--- a/src/signal.cpp +++ b/src/signal.cpp @@ -184,7 +184,7 @@ /** Check whether there is a train on rail, not in a depot */ static Vehicle *TrainOnTileEnum(Vehicle *v, void *) { - if (v->type != VEH_TRAIN || ((Train *)v)->track == TRACK_BIT_DEPOT) return NULL; + if (v->type != VEH_TRAIN || Train::From(v)->track == TRACK_BIT_DEPOT) return NULL; return v; }
--- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -1488,7 +1488,7 @@ static Vehicle *ClearRoadStopStatusEnum(Vehicle *v, void *) { - if (v->type == VEH_ROAD) ((RoadVehicle *)v)->state &= RVSB_ROAD_STOP_TRACKDIR_MASK; + if (v->type == VEH_ROAD) RoadVehicle::From(v)->state &= RVSB_ROAD_STOP_TRACKDIR_MASK; return NULL; } @@ -2617,7 +2617,7 @@ int station_ahead; int station_length; - int stop = GetTrainStopLocation(station_id, tile, (Train *)v, &station_ahead, &station_length); + int stop = GetTrainStopLocation(station_id, tile, Train::From(v), &station_ahead, &station_length); /* Stop whenever that amount of station ahead + the distance from the * begin of the platform to the stop location is longer than the length @@ -2645,7 +2645,7 @@ } } } else if (v->type == VEH_ROAD) { - RoadVehicle *rv = (RoadVehicle *)v; + RoadVehicle *rv = RoadVehicle::From(v); if (rv->state < RVSB_IN_ROAD_STOP && !IsReversingRoadTrackdir((Trackdir)rv->state) && rv->frame == 0) { if (IsRoadStop(tile) && IsRoadVehFront(v)) { /* Attempt to allocate a parking bay in a road stop */
--- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -1725,7 +1725,7 @@ TileIndex tile = *(TileIndex*)data; - if (TrainApproachingCrossingTile((Train *)v) != tile) return NULL; + if (TrainApproachingCrossingTile(Train::From(v)) != tile) return NULL; return v; } @@ -1800,7 +1800,7 @@ { Train *base = v; Train *first = base; // first vehicle to move - Train *last = (Train *)GetLastVehicleInChain(v); // last vehicle to move + Train *last = Train::From(GetLastVehicleInChain(v)); // last vehicle to move uint length = CountVehiclesInChain(v); while (length > 2) { @@ -1849,7 +1849,7 @@ Train *base = v; Train *first = base; // first vehicle to move - Train *last = (Train *)GetLastVehicleInChain(v); // last vehicle to move + Train *last = Train::From(GetLastVehicleInChain(v)); // last vehicle to move uint length = CountVehiclesInChain(v); /* we have to make sure all wagons that leave a depot because of train reversing are moved coorectly @@ -3559,7 +3559,7 @@ Vehicle *coll = v->First(); /* can't collide with own wagons && can't crash in depot && the same height level */ - if (coll != tcc->v && ((Train *)v)->track != TRACK_BIT_DEPOT && abs(v->z_pos - tcc->v->z_pos) < 6) { + if (coll != tcc->v && Train::From(v)->track != TRACK_BIT_DEPOT && abs(v->z_pos - tcc->v->z_pos) < 6) { int x_diff = v->x_pos - tcc->v->x_pos; int y_diff = v->y_pos - tcc->v->y_pos; @@ -3567,8 +3567,8 @@ if (x_diff * x_diff + y_diff * y_diff > 25) return NULL; /* crash both trains */ - tcc->num += TrainCrashed((Train *)tcc->v); - tcc->num += TrainCrashed((Train *)coll); + tcc->num += TrainCrashed(Train::From(tcc->v)); + tcc->num += TrainCrashed(Train::From(coll)); /* Try to reserve all tiles directly under the crashed trains. * As there might be more than two trains involved, we have to do that for all vehicles */ @@ -3632,8 +3632,8 @@ DiagDirection exitdir = *(DiagDirection *)data; /* front engine of a train, not inside wormhole or depot, not crashed */ - if (v->type == VEH_TRAIN && IsFrontEngine(v) && (((Train *)v)->track & TRACK_BIT_MASK) != 0 && !(v->vehstatus & VS_CRASHED)) { - if (v->cur_speed <= 5 && TrainExitDir(v->direction, ((Train *)v)->track) == exitdir) return v; + if (v->type == VEH_TRAIN && IsFrontEngine(v) && (Train::From(v)->track & TRACK_BIT_MASK) && !(v->vehstatus & VS_CRASHED)) { + if (v->cur_speed <= 5 && TrainExitDir(v->direction, Train::From(v)->track) == exitdir) return v; } return NULL; @@ -3942,11 +3942,11 @@ TrackBits *trackbits = (TrackBits *)data; if (v->type == VEH_TRAIN && (v->vehstatus & VS_CRASHED) != 0) { - if ((((Train *)v)->track & TRACK_BIT_WORMHOLE) == TRACK_BIT_WORMHOLE) { + if ((Train::From(v)->track & TRACK_BIT_WORMHOLE) == TRACK_BIT_WORMHOLE) { /* Vehicle is inside a wormhole, v->track contains no useful value then. */ *trackbits |= DiagDirToDiagTrackBits(GetTunnelBridgeDirection(v->tile)); } else { - *trackbits |= ((Train *)v)->track; + *trackbits |= Train::From(v)->track; } }
--- a/src/train_gui.cpp +++ b/src/train_gui.cpp @@ -80,7 +80,7 @@ _cur_dpi = &tmp_dpi; do { - int width = ((const Train *)v)->tcache.cached_veh_length; + int width = Train::From(v)->tcache.cached_veh_length; if (dx + width > 0) { if (dx <= count) { @@ -236,7 +236,7 @@ do { SpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v); DrawSprite(u->GetImage(DIR_W), pal, x + WagonLengthToPixels(4 + dx), y + 6 + (is_custom_sprite(RailVehInfo(u->engine_type)->image_index) ? _traininfo_vehicle_pitch : 0)); - dx += ((const Train *)u)->tcache.cached_veh_length; + dx += Train::From(u)->tcache.cached_veh_length; u = u->Next(); } while (u != NULL && IsArticulatedPart(u) && u->cargo_cap == 0);
--- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -1385,7 +1385,7 @@ DiagDirection vdir; if (v->type == VEH_TRAIN) { - Train *t = (Train *)v; + Train *t = Train::From(v); fc = (x & 0xF) + (y << 4); vdir = DirToDiagDir(t->direction); @@ -1414,7 +1414,7 @@ return VETSB_ENTERED_WORMHOLE; } } else if (v->type == VEH_ROAD) { - RoadVehicle *rv = (RoadVehicle *)v; + RoadVehicle *rv = RoadVehicle::From(v); fc = (x & 0xF) + (y << 4); vdir = DirToDiagDir(v->direction); @@ -1464,18 +1464,18 @@ } switch (v->type) { case VEH_TRAIN: { - Train *t = (Train *)v; + Train *t = Train::From(v); t->track = TRACK_BIT_WORMHOLE; ClrBit(t->flags, VRF_GOINGUP); ClrBit(t->flags, VRF_GOINGDOWN); } break; case VEH_ROAD: - ((RoadVehicle *)v)->state = RVSB_WORMHOLE; + RoadVehicle::From(v)->state = RVSB_WORMHOLE; break; case VEH_SHIP: - ((Ship *)v)->state = TRACK_BIT_WORMHOLE; + Ship::From(v)->state = TRACK_BIT_WORMHOLE; break; default: NOT_REACHED(); @@ -1485,7 +1485,7 @@ v->tile = tile; switch (v->type) { case VEH_TRAIN: { - Train *t = (Train *)v; + Train *t = Train::From(v); if (t->track == TRACK_BIT_WORMHOLE) { t->track = (DiagDirToAxis(dir) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y); return VETSB_ENTERED_WORMHOLE; @@ -1493,7 +1493,7 @@ } break; case VEH_ROAD: { - RoadVehicle *rv = (RoadVehicle *)v; + RoadVehicle *rv = RoadVehicle::From(v); if (rv->state == RVSB_WORMHOLE) { rv->state = _road_exit_tunnel_state[dir]; rv->frame = 0; @@ -1502,7 +1502,7 @@ } break; case VEH_SHIP: { - Ship *ship = (Ship *)v; + Ship *ship = Ship::From(v); if (ship->state == TRACK_BIT_WORMHOLE) { ship->state = (DiagDirToAxis(dir) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y); return VETSB_ENTERED_WORMHOLE;
--- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -524,9 +524,9 @@ if (this->IsPrimaryVehicle()) DecreaseGroupNumVehicle(this->group_id); } - if (this->type == VEH_ROAD) ClearSlot((RoadVehicle *)this); + if (this->type == VEH_ROAD) ClearSlot(RoadVehicle::From(this)); if (this->type == VEH_AIRCRAFT && this->IsPrimaryVehicle()) { - Aircraft *a = (Aircraft *)this; + Aircraft *a = Aircraft::From(this); Station *st = GetTargetAirportIfValid(a); if (st != NULL) { const AirportFTA *layout = st->Airport()->layout; @@ -984,8 +984,8 @@ if (!IsFrontEngine(v)) v = v->First(); UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner); v->load_unload_time_rem = 0; - ClrBit(((Train *)v)->flags, VRF_TOGGLE_REVERSE); - TrainConsistChanged((Train *)v, true); + ClrBit(Train::From(v)->flags, VRF_TOGGLE_REVERSE); + TrainConsistChanged(Train::From(v), true); break; case VEH_ROAD: @@ -995,13 +995,13 @@ case VEH_SHIP: InvalidateWindowClasses(WC_SHIPS_LIST); - static_cast<Ship*>(v)->state = TRACK_BIT_DEPOT; + Ship::From(v)->state = TRACK_BIT_DEPOT; RecalcShipStuff(v); break; case VEH_AIRCRAFT: InvalidateWindowClasses(WC_AIRCRAFT_LIST); - HandleAircraftEnterHangar((Aircraft *)v); + HandleAircraftEnterHangar(Aircraft::From(v)); break; default: NOT_REACHED(); } @@ -1429,9 +1429,9 @@ SpriteID GetVehiclePalette(const Vehicle *v) { if (v->type == VEH_TRAIN) { - return GetEngineColourMap(v->engine_type, v->owner, ((const Train *)v)->tcache.first_engine, v); + return GetEngineColourMap(v->engine_type, v->owner, Train::From(v)->tcache.first_engine, v); } else if (v->type == VEH_ROAD) { - return GetEngineColourMap(v->engine_type, v->owner, ((const RoadVehicle *)v)->rcache.first_engine, v); + return GetEngineColourMap(v->engine_type, v->owner, RoadVehicle::From(v)->rcache.first_engine, v); } return GetEngineColourMap(v->engine_type, v->owner, INVALID_ENGINE, v); @@ -1493,7 +1493,7 @@ * might not be marked as wanting a reservation, e.g. * when an overlength train gets turned around in a station. */ if (UpdateSignalsOnSegment(this->tile, TrackdirToExitdir(this->GetVehicleTrackdir()), this->owner) == SIGSEG_PBS || _settings_game.pf.reserve_paths) { - TryPathReserve((Train *)this, true, true); + TryPathReserve(Train::From(this), true, true); } } } @@ -1577,7 +1577,7 @@ if (this->type == VEH_TRAIN && reverse) DoCommand(this->tile, this->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION); if (this->type == VEH_AIRCRAFT) { - Aircraft *a = (Aircraft *)this; + Aircraft *a = Aircraft::From(this); if (a->state == FLYING && a->targetairport != destination) { /* The aircraft is now heading for a different hangar than the next in the orders */ extern void AircraftNextAirportPos_and_Order(Aircraft *a); @@ -1747,7 +1747,7 @@ */ bool CanVehicleUseStation(const Vehicle *v, const Station *st) { - if (v->type == VEH_ROAD) return st->GetPrimaryRoadStop((const RoadVehicle *)v) != NULL; + if (v->type == VEH_ROAD) return st->GetPrimaryRoadStop(RoadVehicle::From(v)) != NULL; return CanVehicleUseStation(v->engine_type, st); }
--- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -573,6 +573,28 @@ { return IsValidID(index) ? Get(index) : NULL ; } + + /** + * Converts a Vehicle to SpecializedVehicle with type checking. + * @param v Vehicle pointer + * @return pointer to SpecializedVehicle + */ + static FORCEINLINE T *From(Vehicle *v) + { + assert(v->type == Type); + return (T *)v; + } + + /** + * Converts a const Vehicle to const SpecializedVehicle with type checking. + * @param v Vehicle pointer + * @return pointer to SpecializedVehicle + */ + static FORCEINLINE const T *From(const Vehicle *v) + { + assert(v->type == Type); + return (const T *)v; + } }; #define FOR_ALL_VEHICLES_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, vehicle_index, var, 0) if (var->type == name::EXPECTED_TYPE)
--- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -67,7 +67,7 @@ switch (v->type) { case VEH_TRAIN: - if ((v->vehstatus & VS_STOPPED) && ((Train *)v)->tcache.cached_power == 0) return_cmd_error(STR_TRAIN_START_NO_CATENARY); + if ((v->vehstatus & VS_STOPPED) && Train::From(v)->tcache.cached_power == 0) return_cmd_error(STR_TRAIN_START_NO_CATENARY); break; case VEH_SHIP: @@ -75,7 +75,7 @@ break; case VEH_AIRCRAFT: { - Aircraft *a = (Aircraft *)v; + Aircraft *a = Aircraft::From(v); /* cannot stop airplane when in flight, or when taking off / landing */ if (a->state >= STARTTAKEOFF && a->state < TERM7) return_cmd_error(STR_ERROR_AIRCRAFT_IS_IN_FLIGHT); } break; @@ -152,7 +152,7 @@ if (!vehicle_list_window) { if (vehicle_type == VEH_TRAIN) { - if (CheckTrainInDepot((const Train *)v, false) == -1) continue; + if (CheckTrainInDepot(Train::From(v), false) == -1) continue; } else { if (!(v->vehstatus & VS_HIDDEN)) continue; } @@ -349,7 +349,7 @@ if (!CheckOwnership(v->owner)) return CMD_ERROR; - if (v->type == VEH_TRAIN && (!IsFrontEngine(v) || ((Train *)v)->crash_anim_pos >= 4400)) return CMD_ERROR; + if (v->type == VEH_TRAIN && (!IsFrontEngine(v) || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR; /* check that we can allocate enough vehicles */ if (!(flags & DC_EXEC)) { @@ -381,8 +381,8 @@ if (flags & DC_EXEC) { w = Vehicle::Get(_new_vehicle_id); - if (v->type == VEH_TRAIN && HasBit(((Train *)v)->flags, VRF_REVERSE_DIRECTION)) { - SetBit(((Train *)w)->flags, VRF_REVERSE_DIRECTION); + if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) { + SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION); } if (v->type == VEH_TRAIN && !IsFrontEngine(v)) { @@ -403,7 +403,7 @@ } w_rear = w; // trains needs to know the last car in the train, so they can add more in next loop } - } while (v->type == VEH_TRAIN && (v = GetNextVehicle((Train *)v)) != NULL); + } while (v->type == VEH_TRAIN && (v = GetNextVehicle(Train::From(v))) != NULL); if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) { /* for trains this needs to be the front engine due to the callback function */ @@ -437,7 +437,7 @@ } if (w->type == VEH_TRAIN && EngineHasArticPart(w)) { - w = GetNextArticPart((Train *)w); + w = GetNextArticPart(Train::From(w)); } else if (w->type == VEH_ROAD && RoadVehHasArticPart(w)) { w = w->Next(); } else { @@ -453,7 +453,7 @@ } if (v->type == VEH_TRAIN && EngineHasArticPart(v)) { - v = GetNextArticPart((Train *)v); + v = GetNextArticPart(Train::From(v)); } else if (v->type == VEH_ROAD && RoadVehHasArticPart(v)) { v = v->Next(); } else { @@ -461,8 +461,8 @@ } } while (v != NULL); - if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = GetNextVehicle((Train *)w); - } while (v->type == VEH_TRAIN && (v = GetNextVehicle((Train *)v)) != NULL); + if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = GetNextVehicle(Train::From(w)); + } while (v->type == VEH_TRAIN && (v = GetNextVehicle(Train::From(v))) != NULL); if (flags & DC_EXEC) { /*
--- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -603,7 +603,7 @@ { int r = 0; if ((*a)->type == VEH_TRAIN && (*b)->type == VEH_TRAIN) { - r = ((const Train *)(*a))->tcache.cached_max_speed - ((const Train *)(*b))->tcache.cached_max_speed; + r = Train::From(*a)->tcache.cached_max_speed - Train::From(*b)->tcache.cached_max_speed; } else { r = (*a)->max_speed - (*b)->max_speed; } @@ -636,13 +636,13 @@ int r = 0; switch ((*a)->type) { case VEH_TRAIN: - r = ((const Train *)(*a))->tcache.cached_total_length - ((const Train *)(*b))->tcache.cached_total_length; + r = Train::From(*a)->tcache.cached_total_length - Train::From(*b)->tcache.cached_total_length; break; case VEH_ROAD: { const RoadVehicle *u; - for (u = (const RoadVehicle *)*a; u != NULL; u = u->Next()) r += u->rcache.cached_veh_length; - for (u = (const RoadVehicle *)*b; u != NULL; u = u->Next()) r -= u->rcache.cached_veh_length; + for (u = RoadVehicle::From(*a); u != NULL; u = u->Next()) r += u->rcache.cached_veh_length; + for (u = RoadVehicle::From(*b); u != NULL; u = u->Next()) r -= u->rcache.cached_veh_length; } break; default: NOT_REACHED(); @@ -1471,10 +1471,10 @@ switch (v->type) { case VEH_TRAIN: SetDParam(2, v->GetDisplayMaxSpeed()); - SetDParam(1, ((const Train *)v)->tcache.cached_power); - SetDParam(0, ((const Train *)v)->tcache.cached_weight); - SetDParam(3, ((const Train *)v)->tcache.cached_max_te / 1000); - DrawString(2, this->width - 2, 25, (_settings_game.vehicle.train_acceleration_model != TAM_ORIGINAL && ((const Train *)v)->railtype != RAILTYPE_MAGLEV) ? + SetDParam(1, Train::From(v)->tcache.cached_power); + SetDParam(0, Train::From(v)->tcache.cached_weight); + SetDParam(3, Train::From(v)->tcache.cached_max_te / 1000); + DrawString(2, this->width - 2, 25, (_settings_game.vehicle.train_acceleration_model != TAM_ORIGINAL && Train::From(v)->railtype != RAILTYPE_MAGLEV) ? STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED_MAX_TE : STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED); break; @@ -1943,7 +1943,7 @@ } else if (v->vehstatus & VS_STOPPED) { if (v->type == VEH_TRAIN) { if (v->cur_speed == 0) { - if (((const Train *)v)->tcache.cached_power == 0) { + if (Train::From(v)->tcache.cached_power == 0) { str = STR_TRAIN_NO_POWER; } else { str = STR_VEHICLE_STATUS_STOPPED; @@ -1955,7 +1955,7 @@ } else { // no train str = STR_VEHICLE_STATUS_STOPPED; } - } else if (v->type == VEH_TRAIN && HasBit(((const Train *)v)->flags, VRF_TRAIN_STUCK)) { + } else if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_TRAIN_STUCK)) { str = STR_TRAIN_STUCK; } else { // vehicle is in a "normal" state, show current order switch (v->current_order.GetType()) {
--- a/src/vehicle_type.h +++ b/src/vehicle_type.h @@ -28,6 +28,8 @@ struct RoadVehicle; struct Ship; struct Aircraft; +struct EffectVehicle; +struct DisasterVehicle; struct BaseVehicle {
--- a/src/vehiclelist.cpp +++ b/src/vehiclelist.cpp @@ -29,7 +29,7 @@ switch (type) { case VEH_TRAIN: if (IsArticulatedPart(v) || IsRearDualheaded(v)) continue; - if (((const Train *)v)->track != TRACK_BIT_DEPOT) continue; + if (Train::From(v)->track != TRACK_BIT_DEPOT) continue; if (wagons != NULL && IsFreeWagon(v->First())) { if (individual_wagons || IsFreeWagon(v)) *wagons->Append() = v; continue;
--- a/src/water_cmd.cpp +++ b/src/water_cmd.cpp @@ -813,22 +813,22 @@ /* FreeTrainTrackReservation() calls GetVehicleTrackdir() that doesn't like crashed vehicles. * In this case, v->direction matches v->u.rail.track, so we can do this (it wasn't crashed before) */ v->vehstatus &= ~VS_CRASHED; - FreeTrainTrackReservation((Train *)v); + FreeTrainTrackReservation(Train::From(v)); v->vehstatus |= VS_CRASHED; } - ((Train *)v)->crash_anim_pos = 4000; // max 4440, disappear pretty fast + Train::From(v)->crash_anim_pos = 4000; // max 4440, disappear pretty fast InvalidateWindowClassesData(WC_TRAINS_LIST, 0); break; case VEH_ROAD: if (IsRoadVehFront(v)) pass += 1; // driver - ((RoadVehicle *)v)->crashed_ctr = 2000; // max 2220, disappear pretty fast + RoadVehicle::From(v)->crashed_ctr = 2000; // max 2220, disappear pretty fast InvalidateWindowClassesData(WC_ROADVEH_LIST, 0); break; case VEH_AIRCRAFT: pass += 2; // driver - ((Aircraft *)v)->crashed_counter = 9000; // max 10000, disappear pretty fast + Aircraft::From(v)->crashed_counter = 9000; // max 10000, disappear pretty fast InvalidateWindowClassesData(WC_AIRCRAFT_LIST, 0); break; }
--- a/src/yapf/follow_track.hpp +++ b/src/yapf/follow_track.hpp @@ -55,7 +55,7 @@ { assert(!IsRailTT() || (v != NULL && v->type == VEH_TRAIN)); m_veh = v; - Init(v != NULL ? v->owner : INVALID_OWNER, IsRailTT() && railtype_override == INVALID_RAILTYPES ? ((const Train *)v)->compatible_railtypes : railtype_override, pPerf); + Init(v != NULL ? v->owner : INVALID_OWNER, IsRailTT() && railtype_override == INVALID_RAILTYPES ? Train::From(v)->compatible_railtypes : railtype_override, pPerf); } FORCEINLINE void Init(Owner o, RailTypes railtype_override, CPerformanceTimer *pPerf) @@ -76,7 +76,7 @@ FORCEINLINE static TransportType TT() {return Ttr_type_;} FORCEINLINE static bool IsWaterTT() {return TT() == TRANSPORT_WATER;} FORCEINLINE static bool IsRailTT() {return TT() == TRANSPORT_RAIL;} - FORCEINLINE bool IsTram() {return IsRoadTT() && HasBit(((const RoadVehicle *)m_veh)->compatible_roadtypes, ROADTYPE_TRAM);} + FORCEINLINE bool IsTram() {return IsRoadTT() && HasBit(RoadVehicle::From(m_veh)->compatible_roadtypes, ROADTYPE_TRAM);} FORCEINLINE static bool IsRoadTT() {return TT() == TRANSPORT_ROAD;} FORCEINLINE static bool Allow90degTurns() {return T90deg_turns_allowed_;} FORCEINLINE static bool DoTrackMasking() {return IsRailTT() && Tmask_reserved_tracks;} @@ -106,7 +106,7 @@ m_old_tile = old_tile; m_old_td = old_td; m_err = EC_NONE; - assert(((TrackStatusToTrackdirBits(GetTileTrackStatus(m_old_tile, TT(), IsRoadTT() && m_veh != NULL ? ((const RoadVehicle *)m_veh)->compatible_roadtypes : 0)) & TrackdirToTrackdirBits(m_old_td)) != 0) || + assert(((TrackStatusToTrackdirBits(GetTileTrackStatus(m_old_tile, TT(), IsRoadTT() && m_veh != NULL ? RoadVehicle::From(m_veh)->compatible_roadtypes : 0)) & TrackdirToTrackdirBits(m_old_td)) != 0) || (IsTram() && GetSingleTramBit(m_old_tile) != INVALID_DIAGDIR)); // Disable the assertion for single tram bits m_exitdir = TrackdirToExitdir(m_old_td); if (ForcedReverse()) return true; @@ -207,7 +207,7 @@ if (IsRailTT() && IsPlainRailTile(m_new_tile)) { m_new_td_bits = (TrackdirBits)(GetTrackBits(m_new_tile) * 0x101); } else { - m_new_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(m_new_tile, TT(), IsRoadTT() && m_veh != NULL ? ((const RoadVehicle *)m_veh)->compatible_roadtypes : 0)); + m_new_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(m_new_tile, TT(), IsRoadTT() && m_veh != NULL ? RoadVehicle::From(m_veh)->compatible_roadtypes : 0)); if (IsTram() && m_new_td_bits == 0) { /* GetTileTrackStatus() returns 0 for single tram bits.
--- a/src/yapf/yapf_costrail.hpp +++ b/src/yapf/yapf_costrail.hpp @@ -247,8 +247,8 @@ const Vehicle *v = Yapf().GetVehicle(); assert(v != NULL); assert(v->type == VEH_TRAIN); - assert(((const Train *)v)->tcache.cached_total_length != 0); - int missing_platform_length = (((const Train *)v)->tcache.cached_total_length + TILE_SIZE - 1) / TILE_SIZE - platform_length; + assert(Train::From(v)->tcache.cached_total_length != 0); + int missing_platform_length = (Train::From(v)->tcache.cached_total_length + TILE_SIZE - 1) / TILE_SIZE - platform_length; if (missing_platform_length < 0) { /* apply penalty for longer platform than needed */ cost += Yapf().PfGetSettings().rail_longer_platform_penalty + Yapf().PfGetSettings().rail_longer_platform_per_tile_penalty * -missing_platform_length;
--- a/src/yapf/yapf_destrail.hpp +++ b/src/yapf/yapf_destrail.hpp @@ -13,8 +13,8 @@ public: void SetDestination(const Vehicle *v, bool override_rail_type = false) { - m_compatible_railtypes = ((const Train *)v)->compatible_railtypes; - if (override_rail_type) m_compatible_railtypes |= GetRailTypeInfo(((const Train *)v)->railtype)->compatible_railtypes; + m_compatible_railtypes = Train::From(v)->compatible_railtypes; + if (override_rail_type) m_compatible_railtypes |= GetRailTypeInfo(Train::From(v)->railtype)->compatible_railtypes; } bool IsCompatibleRailType(RailType rt) @@ -91,8 +91,8 @@ FORCEINLINE bool PfDetectDestination(TileIndex tile, Trackdir td) { return - IsSafeWaitingPosition((const Train *)Yapf().GetVehicle(), tile, td, true, !TrackFollower::Allow90degTurns()) && - IsWaitingPositionFree((const Train *)Yapf().GetVehicle(), tile, td, !TrackFollower::Allow90degTurns()); + IsSafeWaitingPosition(Train::From(Yapf().GetVehicle()), tile, td, true, !TrackFollower::Allow90degTurns()) && + IsWaitingPositionFree(Train::From(Yapf().GetVehicle()), tile, td, !TrackFollower::Allow90degTurns()); } /** Called by YAPF to calculate cost estimate. Calculates distance to the destination
--- a/src/yapf/yapf_rail.cpp +++ b/src/yapf/yapf_rail.cpp @@ -54,7 +54,7 @@ bool FindSafePositionProc(TileIndex tile, Trackdir td) { - if (IsSafeWaitingPosition((const Train *)Yapf().GetVehicle(), tile, td, true, !TrackFollower::Allow90degTurns())) { + if (IsSafeWaitingPosition(Train::From(Yapf().GetVehicle()), tile, td, true, !TrackFollower::Allow90degTurns())) { m_res_dest = tile; m_res_dest_td = td; return false; // Stop iterating segment @@ -149,7 +149,7 @@ } /* Don't bother if the target is reserved. */ - if (!IsWaitingPositionFree((const Train *)Yapf().GetVehicle(), m_res_dest, m_res_dest_td)) return false; + if (!IsWaitingPositionFree(Train::From(Yapf().GetVehicle()), m_res_dest, m_res_dest_td)) return false; for (Node *node = m_res_node; node->m_parent != NULL; node = node->m_parent) { node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::ReserveSingleTrack); @@ -411,7 +411,7 @@ if (target != NULL) target->tile = INVALID_TILE; /* set origin and destination nodes */ - PBSTileInfo origin = FollowTrainReservation((const Train *)v); + PBSTileInfo origin = FollowTrainReservation(Train::From(v)); Yapf().SetOrigin(origin.tile, origin.trackdir, INVALID_TILE, INVALID_TRACKDIR, 1, true); Yapf().SetDestination(v); @@ -536,9 +536,9 @@ bool YapfCheckReverseTrain(const Vehicle *vt) { - const Train *v = (const Train *)vt; + const Train *v = Train::From(vt); /* last wagon */ - const Train *last_veh = (const Train *)GetLastVehicleInChain(v); + const Train *last_veh = Train::From(GetLastVehicleInChain(v)); /* get trackdirs of both ends */ Trackdir td = v->GetVehicleTrackdir(); @@ -602,7 +602,7 @@ const Vehicle *last_veh = GetLastVehicleInChain(v); - PBSTileInfo origin = FollowTrainReservation((const Train *)v); + PBSTileInfo origin = FollowTrainReservation(Train::From(v)); TileIndex last_tile = last_veh->tile; Trackdir td_rev = ReverseTrackdir(last_veh->GetVehicleTrackdir());
--- a/src/yapf/yapf_road.cpp +++ b/src/yapf/yapf_road.cpp @@ -299,13 +299,13 @@ /* our source tile will be the next vehicle tile (should be the given one) */ TileIndex src_tile = tile; /* get available trackdirs on the start tile */ - TrackdirBits src_trackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, ((const RoadVehicle *)v)->compatible_roadtypes)); + TrackdirBits src_trackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, RoadVehicle::From(v)->compatible_roadtypes)); /* select reachable trackdirs only */ src_trackdirs &= DiagdirReachesTrackdirs(enterdir); /* get available trackdirs on the destination tile */ TileIndex dest_tile = v->dest_tile; - TrackdirBits dest_trackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(dest_tile, TRANSPORT_ROAD, ((const RoadVehicle *)v)->compatible_roadtypes)); + TrackdirBits dest_trackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(dest_tile, TRANSPORT_ROAD, RoadVehicle::From(v)->compatible_roadtypes)); /* set origin and destination nodes */ Yapf().SetOrigin(src_tile, src_trackdirs); @@ -349,7 +349,7 @@ /* set destination tile, trackdir * get available trackdirs on the destination tile */ - TrackdirBits dst_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, TRANSPORT_ROAD, ((const RoadVehicle *)v)->compatible_roadtypes)); + TrackdirBits dst_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, TRANSPORT_ROAD, RoadVehicle::From(v)->compatible_roadtypes)); Yapf().SetDestination(dst_tile, dst_td_bits); /* if path not found - return distance = UINT_MAX */ @@ -374,7 +374,7 @@ /* set origin (tile, trackdir) */ TileIndex src_tile = v->tile; Trackdir src_td = v->GetVehicleTrackdir(); - if ((TrackStatusToTrackdirBits(GetTileTrackStatus(src_tile, TRANSPORT_ROAD, ((const RoadVehicle *)v)->compatible_roadtypes)) & TrackdirToTrackdirBits(src_td)) == 0) { + if ((TrackStatusToTrackdirBits(GetTileTrackStatus(src_tile, TRANSPORT_ROAD, RoadVehicle::From(v)->compatible_roadtypes)) & TrackdirToTrackdirBits(src_td)) == 0) { /* sometimes the roadveh is not on the road (it resides on non-existing track) * how should we handle that situation? */ return false; @@ -471,7 +471,7 @@ { TileIndex tile = v->tile; Trackdir trackdir = v->GetVehicleTrackdir(); - if ((TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, ((const RoadVehicle *)v)->compatible_roadtypes)) & TrackdirToTrackdirBits(trackdir)) == 0) { + if ((TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, RoadVehicle::From(v)->compatible_roadtypes)) & TrackdirToTrackdirBits(trackdir)) == 0) { return NULL; }