From 4acf3e4c3f4aebf66b82cc1d33aab221d27d800c Mon Sep 17 00:00:00 2001 From: maedhros Date: Fri, 1 Jun 2007 12:03:10 +0000 Subject: (svn r10009) -Codechange: Add and use Vehicle::IsPrimaryVehicle to replace individual checks depending on the vehicle type. --- src/aircraft.h | 1 + src/depot_gui.cpp | 4 ++-- src/economy.cpp | 5 +---- src/group_cmd.cpp | 14 ++++---------- src/order_gui.cpp | 4 ++-- src/roadveh.h | 1 + src/ship.h | 1 + src/train.h | 2 ++ src/vehicle.cpp | 21 ++++++--------------- src/vehicle.h | 11 +++++++++++ 10 files changed, 31 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/aircraft.h b/src/aircraft.h index 8af928495..f7cd1b457 100644 --- a/src/aircraft.h +++ b/src/aircraft.h @@ -135,6 +135,7 @@ struct Aircraft : public Vehicle { void UpdateDeltaXY(Direction direction); ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_AIRCRAFT_INC : EXPENSES_AIRCRAFT_RUN; } WindowClass GetVehicleListWindowClass() const { return WC_AIRCRAFT_LIST; } + bool IsPrimaryVehicle() const { return IsNormalAircraft(this); } }; #endif /* AIRCRAFT_H */ diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp index 8aea0298c..2d1cf1b41 100644 --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -488,10 +488,10 @@ static void HandleCloneVehClick(const Vehicle *v, const Window *w) if (v == NULL) return; - if (v->type == VEH_TRAIN && !IsFrontEngine(v)) { + if (v->HasFront() && !v->IsPrimaryVehicle()) { v = GetFirstVehicleInChain(v); /* Do nothing when clicking on a train in depot with no loc attached */ - if (!IsFrontEngine(v)) return; + if (v->type == VEH_TRAIN && !IsFrontEngine(v)) return; } switch (v->type) { diff --git a/src/economy.cpp b/src/economy.cpp index d57cf5314..dbec59eca 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -117,10 +117,7 @@ int UpdateCompanyRatingAndValue(Player *p, bool update) FOR_ALL_VEHICLES(v) { if (v->owner != owner) continue; - if ((v->type == VEH_TRAIN && IsFrontEngine(v)) || - v->type == VEH_ROAD || - (v->type == VEH_AIRCRAFT && IsNormalAircraft(v)) || - v->type == VEH_SHIP) { + if (IsPlayerBuildableVehicleType(v->type) && v->IsPrimaryVehicle()) { num++; if (v->age > 730) { /* Find the vehicle with the lowest amount of profit */ diff --git a/src/group_cmd.cpp b/src/group_cmd.cpp index 7eaef6b89..41f28af57 100644 --- a/src/group_cmd.cpp +++ b/src/group_cmd.cpp @@ -211,7 +211,7 @@ int32 CmdAddVehicleGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) } Vehicle *v = GetVehicle(p2); - if (v->owner != _current_player || (v->type == VEH_TRAIN && !IsFrontEngine(v))) return CMD_ERROR; + if (v->owner != _current_player || !v->IsPrimaryVehicle()) return CMD_ERROR; if (flags & DC_EXEC) { DecreaseGroupNumVehicle(v->group_id); @@ -254,14 +254,11 @@ int32 CmdAddSharedVehicleGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p Vehicle *v; VehicleType type = (VehicleType)p2; GroupID id_g = p1; - uint subtype = (type == VEH_AIRCRAFT) ? AIR_AIRCRAFT : 0; /* Find the first front engine which belong to the group id_g * then add all shared vehicles of this front engine to the group id_g */ FOR_ALL_VEHICLES(v) { - if ((v->type == type) && ( - (type == VEH_TRAIN && IsFrontEngine(v)) || - (type != VEH_TRAIN && v->subtype <= subtype))) { + if (v->type == type && v->IsPrimaryVehicle()) { if (v->group_id != id_g) continue; /* For each shared vehicles add it to the group */ @@ -295,14 +292,11 @@ int32 CmdRemoveAllVehiclesGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 if (flags & DC_EXEC) { GroupID old_g = p1; - uint subtype = (type == VEH_AIRCRAFT) ? AIR_AIRCRAFT : 0; Vehicle *v; /* Find each Vehicle that belongs to the group old_g and add it to the default group */ FOR_ALL_VEHICLES(v) { - if ((v->type == type) && ( - (type == VEH_TRAIN && IsFrontEngine(v)) || - (type != VEH_TRAIN && v->subtype <= subtype))) { + if (v->type == type && v->IsPrimaryVehicle()) { if (v->group_id != old_g) continue; /* Add The Vehicle to the default group */ @@ -348,7 +342,7 @@ int32 CmdSetGroupReplaceProtection(TileIndex tile, uint32 flags, uint32 p1, uint */ void RemoveVehicleFromGroup(const Vehicle *v) { - if (!IsValidVehicle(v) || v->type != VEH_TRAIN || !IsFrontEngine(v)) return; + if (!IsValidVehicle(v) || !(v->HasFront() && v->IsPrimaryVehicle())) return; if (!IsDefaultGroupID(v->group_id)) DecreaseGroupNumVehicle(v->group_id); } diff --git a/src/order_gui.cpp b/src/order_gui.cpp index 6d9f8b19f..5d69639d8 100644 --- a/src/order_gui.cpp +++ b/src/order_gui.cpp @@ -320,9 +320,9 @@ static bool HandleOrderVehClick(const Vehicle *v, const Vehicle *u, Window *w) { if (u->type != v->type) return false; - if (u->type == VEH_TRAIN && !IsFrontEngine(u)) { + if (u->HasFront() && !u->IsPrimaryVehicle()) { u = GetFirstVehicleInChain(u); - if (!IsFrontEngine(u)) return false; + if (!u->IsPrimaryVehicle()) return false; } // v is vehicle getting orders. Only copy/clone orders if vehicle doesn't have any orders yet diff --git a/src/roadveh.h b/src/roadveh.h index 3db44ff45..f3c2f46d8 100644 --- a/src/roadveh.h +++ b/src/roadveh.h @@ -43,6 +43,7 @@ struct RoadVehicle : public Vehicle { void UpdateDeltaXY(Direction direction); ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_ROADVEH_INC : EXPENSES_ROADVEH_RUN; } WindowClass GetVehicleListWindowClass() const { return WC_ROADVEH_LIST; } + bool IsPrimaryVehicle() const { return true; } }; #endif /* ROADVEH_H */ diff --git a/src/ship.h b/src/ship.h index f0435936d..b045beb05 100644 --- a/src/ship.h +++ b/src/ship.h @@ -45,6 +45,7 @@ struct Ship: public Vehicle { ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_SHIP_INC : EXPENSES_SHIP_RUN; } WindowClass GetVehicleListWindowClass() const { return WC_SHIPS_LIST; } void PlayLeaveStationSound() const; + bool IsPrimaryVehicle() const { return true; } }; #endif /* SHIP_H */ diff --git a/src/train.h b/src/train.h index 964ca446d..3d09d4cea 100644 --- a/src/train.h +++ b/src/train.h @@ -270,6 +270,8 @@ struct Train : public Vehicle { ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_TRAIN_INC : EXPENSES_TRAIN_RUN; } WindowClass GetVehicleListWindowClass() const { return WC_TRAINS_LIST; } void PlayLeaveStationSound() const; + bool IsPrimaryVehicle() const { return IsFrontEngine(this); } + bool HasFront() const { return true; } }; #endif /* TRAIN_H */ diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 0d30cf263..298bcfcaf 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -590,7 +590,7 @@ void DestroyVehicle(Vehicle *v) if (v->owner == _local_player) InvalidateAutoreplaceWindow(v->engine_type); if (IsValidGroupID(v->group_id)) GetGroup(v->group_id)->num_engines[v->engine_type]--; - if (v->type != VEH_TRAIN || IsFrontEngine(v)) DecreaseGroupNumVehicle(v->group_id); + if (v->IsPrimaryVehicle()) DecreaseGroupNumVehicle(v->group_id); } DeleteVehicleNews(v->index, INVALID_STRING_ID); @@ -1997,16 +1997,13 @@ void BuildDepotVehicleList(VehicleType type, TileIndex tile, Vehicle ***engine_l */ uint GenerateVehicleSortList(const Vehicle ***sort_list, uint16 *length_of_array, VehicleType type, PlayerID owner, uint32 index, uint16 window_type) { - const byte subtype = (type != VEH_AIRCRAFT) ? (byte)Train_Front : (byte)AIR_AIRCRAFT; uint n = 0; const Vehicle *v; switch (window_type) { case VLW_STATION_LIST: { FOR_ALL_VEHICLES(v) { - if (v->type == type && ( - (type == VEH_TRAIN && IsFrontEngine(v)) || - (type != VEH_TRAIN && v->subtype <= subtype))) { + if (v->type == type && v->IsPrimaryVehicle()) { const Order *order; FOR_VEHICLE_ORDERS(v, order) { @@ -2039,9 +2036,7 @@ uint GenerateVehicleSortList(const Vehicle ***sort_list, uint16 *length_of_array case VLW_STANDARD: { FOR_ALL_VEHICLES(v) { - if (v->type == type && v->owner == owner && ( - (type == VEH_TRAIN && IsFrontEngine(v)) || - (type != VEH_TRAIN && v->subtype <= subtype))) { + if (v->type == type && v->owner == owner && v->IsPrimaryVehicle()) { /* TODO find a better estimate on the total number of vehicles for current player */ if (n == *length_of_array) ExtendVehicleListSize(sort_list, length_of_array, GetNumVehicles()/4); (*sort_list)[n++] = v; @@ -2052,9 +2047,7 @@ uint GenerateVehicleSortList(const Vehicle ***sort_list, uint16 *length_of_array case VLW_DEPOT_LIST: { FOR_ALL_VEHICLES(v) { - if (v->type == type && ( - (type == VEH_TRAIN && IsFrontEngine(v)) || - (type != VEH_TRAIN && v->subtype <= subtype))) { + if (v->type == type && v->IsPrimaryVehicle()) { const Order *order; FOR_VEHICLE_ORDERS(v, order) { @@ -2071,10 +2064,8 @@ uint GenerateVehicleSortList(const Vehicle ***sort_list, uint16 *length_of_array case VLW_GROUP_LIST: FOR_ALL_VEHICLES(v) { - if (v->type == type && ( - (type == VEH_TRAIN && IsFrontEngine(v)) || - (type != VEH_TRAIN && v->subtype <= subtype) - ) && v->owner == owner && v->group_id == index) { + if (v->type == type && v->IsPrimaryVehicle() && + v->owner == owner && v->group_id == index) { if (n == *length_of_array) ExtendVehicleListSize(sort_list, length_of_array, GetNumVehicles() / 4); (*sort_list)[n++] = v; diff --git a/src/vehicle.h b/src/vehicle.h index f1f9aa481..ed5812420 100644 --- a/src/vehicle.h +++ b/src/vehicle.h @@ -399,6 +399,17 @@ struct Vehicle { * Play the sound associated with leaving the station */ virtual void PlayLeaveStationSound() const {} + + /** + * Whether this is the primary vehicle in the chain. + */ + virtual bool IsPrimaryVehicle() const { return false; } + + /** + * Whether this vehicle understands the concept of a front engine, so + * basically, if GetFirstVehicleInChain() can be called for it. + */ + virtual bool HasFront() const { return false; } }; /** -- cgit v1.2.3-54-g00ecf