(svn r10009) -Codechange: Add and use Vehicle::IsPrimaryVehicle to replace individual checks depending on the vehicle type.

pull/155/head
maedhros 17 years ago
parent 2181e11c18
commit fa548f1a6f

@ -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 */

@ -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) {

@ -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 */

@ -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);
}

@ -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

@ -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 */

@ -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 */

@ -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 */

@ -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;

@ -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; }
};
/**

Loading…
Cancel
Save