changeset 4725:f12e2bdde955 draft

(svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot() This revealed duplicated code like aircraft lists got invalidated twice Moved invalidation of the vehicle detail window to VehicleServiceInDepot() as it should always be updated when serviced
author bjarni <bjarni@openttd.org>
date Wed, 04 Oct 2006 12:01:59 +0000
parents 8ebbf44badd1
children 2f7f2e7e694b
files aircraft.h aircraft_cmd.c rail_cmd.c road_cmd.c roadveh_cmd.c ship.h ship_cmd.c train_cmd.c vehicle.c vehicle.h
diffstat 10 files changed, 93 insertions(+), 197 deletions(-) [+]
line wrap: on
line diff
--- a/aircraft.h
+++ b/aircraft.h
@@ -21,5 +21,6 @@
 uint16 AircraftDefaultCargoCapacity(CargoID cid, EngineID engine_type);
 
 void CcCloneAircraft(bool success, TileIndex tile, uint32 p1, uint32 p2);
+inline void HandleAircraftEnterHangar(Vehicle *v);
 
 #endif /* AIRCRAFT_H */
--- a/aircraft_cmd.c
+++ b/aircraft_cmd.c
@@ -833,14 +833,17 @@
 	}
 }
 
-static void ServiceAircraft(Vehicle *v)
+/** Handle Aircraft specific tasks when a an Aircraft enters a hangar
+ * Made inline because it's only called from one location (VehicleEnterDepot)
+ * Can't be moved to vehicle.c because it calls static Aircraft specific functions
+ * @param *v Vehicle that enters the hangar
+ */
+inline void HandleAircraftEnterHangar(Vehicle *v)
 {
 	Vehicle *u;
 
-	v->cur_speed = 0;
 	v->subspeed = 0;
 	v->progress = 0;
-	v->vehstatus |= VS_HIDDEN;
 
 	u = v->next;
 	u->vehstatus |= VS_HIDDEN;
@@ -851,10 +854,6 @@
 	}
 
 	SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos);
-	InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
-
-	VehicleServiceInDepot(v);
-	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
 }
 
 static void PlayAircraftSound(const Vehicle* v)
@@ -1385,48 +1384,6 @@
 	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
 }
 
-
-static void AircraftEnterHangar(Vehicle *v)
-{
-	Order old_order;
-
-	ServiceAircraft(v);
-	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
-
-	TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT);
-
-	if (v->current_order.type == OT_GOTO_DEPOT) {
-		int32 cost;
-
-		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
-
-		old_order = v->current_order;
-		v->current_order.type = OT_NOTHING;
-		v->current_order.flags = 0;
-
-		_current_player = v->owner;
-		cost = DoCommand(v->tile, v->index, old_order.refit_cargo | old_order.refit_subtype << 8, DC_EXEC, CMD_REFIT_AIRCRAFT);
-		if (!CmdFailed(cost) && v->owner == _local_player && cost != 0) ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost);
-
-		if (HASBIT(old_order.flags, OFB_PART_OF_ORDERS)) {
-			v->cur_order_index++;
-		} else if (HASBIT(old_order.flags, OFB_HALT_IN_DEPOT)) { // force depot visit
-			v->vehstatus |= VS_STOPPED;
-			InvalidateWindowClasses(WC_AIRCRAFT_LIST);
-
-			if (v->owner == _local_player) {
-				SetDParam(0, v->unitnumber);
-				AddNewsItem(
-					STR_A014_AIRCRAFT_IS_WAITING_IN,
-					NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
-					v->index,
-					0
-				);
-			}
-		}
-	}
-}
-
 static void AircraftLand(Vehicle *v)
 {
 	v->sprite_width = v->sprite_height = 2;
@@ -1493,7 +1450,7 @@
 
 static void AircraftEventHandler_EnterHangar(Vehicle *v, const AirportFTAClass *Airport)
 {
-	AircraftEnterHangar(v);
+	VehicleEnterDepot(v);
 	v->u.air.state = Airport->layout[v->u.air.pos].heading;
 }
 
--- a/rail_cmd.c
+++ b/rail_cmd.c
@@ -2006,7 +2006,7 @@
 			v->vehstatus |= VS_HIDDEN; /* hide it */
 			v->direction = ReverseDir(v->direction);
 			if (v->next == NULL)
-				TrainEnterDepot(v, tile);
+				VehicleEnterDepot(v);
 			v->tile = tile;
 			InvalidateWindow(WC_VEHICLE_DEPOT, tile);
 			return 4;
--- a/road_cmd.c
+++ b/road_cmd.c
@@ -22,8 +22,6 @@
 #include "yapf/yapf.h"
 #include "depot.h"
 
-void RoadVehEnterDepot(Vehicle *v);
-
 
 static uint CountRoadBits(RoadBits r)
 {
@@ -1057,7 +1055,7 @@
 			if (v->type == VEH_Road &&
 					v->u.road.frame == 11 &&
 					_roadveh_enter_depot_unk0[GetRoadDepotDirection(tile)] == v->u.road.state) {
-				RoadVehEnterDepot(v);
+				VehicleEnterDepot(v);
 				return 4;
 			}
 			break;
--- a/roadveh_cmd.c
+++ b/roadveh_cmd.c
@@ -1575,52 +1575,6 @@
 	RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
 }
 
-void RoadVehEnterDepot(Vehicle *v)
-{
-	v->u.road.state = 254;
-	v->vehstatus |= VS_HIDDEN;
-
-	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
-
-	VehicleServiceInDepot(v);
-
-	TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT);
-
-	if (v->current_order.type == OT_GOTO_DEPOT) {
-		Order t;
-		int32 cost;
-
-		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
-
-		t = v->current_order;
-		v->current_order.type = OT_DUMMY;
-		v->current_order.flags = 0;
-
-		_current_player = v->owner;
-		cost = DoCommand(v->tile, v->index, t.refit_cargo | t.refit_subtype << 8, DC_EXEC, CMD_REFIT_ROAD_VEH);
-		if (!CmdFailed(cost) && v->owner == _local_player && cost != 0) ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost);
-
-		// Part of the orderlist?
-		if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) {
-			v->cur_order_index++;
-		} else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) {
-			v->vehstatus |= VS_STOPPED;
-			if (v->owner == _local_player) {
-				SetDParam(0, v->unitnumber);
-				AddNewsItem(
-					STR_9016_ROAD_VEHICLE_IS_WAITING,
-					NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
-					v->index,
-					0
-				);
-			}
-		}
-	}
-
-	InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
-	InvalidateWindowClasses(WC_ROADVEH_LIST);
-}
-
 static void AgeRoadVehCargo(Vehicle *v)
 {
 	if (_age_cargo_skip_counter != 0) return;
--- a/ship.h
+++ b/ship.h
@@ -6,6 +6,7 @@
 #include "vehicle.h"
 
 void CcCloneShip(bool success, TileIndex tile, uint32 p1, uint32 p2);
+void RecalcShipStuff(Vehicle *v);
 
 static inline bool IsShipInDepot(const Vehicle* v)
 {
--- a/ship_cmd.c
+++ b/ship_cmd.c
@@ -316,7 +316,7 @@
 	v->sprite_height = GB(x, 24, 8);
 }
 
-static void RecalcShipStuff(Vehicle *v)
+void RecalcShipStuff(Vehicle *v)
 {
 	UpdateShipDeltaXY(v, v->direction);
 	v->cur_image = GetShipImage(v, v->direction);
@@ -391,50 +391,6 @@
 	return ShipVehInfo(engine_type)->base_cost * (_price.ship_base>>3)>>5;
 }
 
-static void ShipEnterDepot(Vehicle *v)
-{
-	v->u.ship.state = 0x80;
-	v->vehstatus |= VS_HIDDEN;
-	v->cur_speed = 0;
-	RecalcShipStuff(v);
-
-	VehicleServiceInDepot(v);
-
-	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
-
-	TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT);
-
-	if (v->current_order.type == OT_GOTO_DEPOT) {
-		Order t;
-		int32 cost;
-
-		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
-
-		t = v->current_order;
-		v->current_order.type = OT_DUMMY;
-		v->current_order.flags = 0;
-
-		_current_player = v->owner;
-		cost = DoCommand(v->tile, v->index, t.refit_cargo | t.refit_subtype << 8, DC_EXEC, CMD_REFIT_SHIP);
-		if (!CmdFailed(cost) && v->owner == _local_player && cost != 0) ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost);
-
-		if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) {
-			v->cur_order_index++;
-		} else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) {
-			v->vehstatus |= VS_STOPPED;
-			if (v->owner == _local_player) {
-				SetDParam(0, v->unitnumber);
-				AddNewsItem(
-					STR_981C_SHIP_IS_WAITING_IN_DEPOT,
-					NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
-					v->index,
-					0);
-			}
-		}
-	}
-	InvalidateWindowClasses(WC_SHIPS_LIST);
-}
-
 static void ShipArrivesAt(const Vehicle* v, Station* st)
 {
 	/* Check if station was ever visited before */
@@ -731,7 +687,7 @@
 					if (v->dest_tile == gp.new_tile) {
 						if (v->current_order.type == OT_GOTO_DEPOT) {
 							if ((gp.x&0xF)==8 && (gp.y&0xF)==8) {
-								ShipEnterDepot(v);
+								VehicleEnterDepot(v);
 								return;
 							}
 						} else if (v->current_order.type == OT_GOTO_STATION) {
--- a/train_cmd.c
+++ b/train_cmd.c
@@ -3476,55 +3476,6 @@
 	}
 }
 
-
-void TrainEnterDepot(Vehicle *v, TileIndex tile)
-{
-	UpdateSignalsOnSegment(tile, GetRailDepotDirection(tile));
-
-	if (!IsFrontEngine(v)) v = GetFirstVehicleInChain(v);
-
-	VehicleServiceInDepot(v);
-
-	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
-
-	v->load_unload_time_rem = 0;
-	v->cur_speed = 0;
-
-	TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT);
-
-	if (v->current_order.type == OT_GOTO_DEPOT) {
-		Order t;
-		int32 cost;
-
-		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
-
-		t = v->current_order;
-		v->current_order.type = OT_DUMMY;
-		v->current_order.flags = 0;
-
-		_current_player = v->owner;
-		cost = DoCommand(v->tile, v->index, t.refit_cargo | t.refit_subtype << 8, DC_EXEC, CMD_REFIT_RAIL_VEHICLE);
-		if (!CmdFailed(cost) && v->owner == _local_player && cost != 0) ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost);
-
-		if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) { // Part of the orderlist?
-			v->u.rail.days_since_order_progr = 0;
-			v->cur_order_index++;
-		} else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) { // User initiated?
-			v->vehstatus |= VS_STOPPED;
-			if (v->owner == _local_player) {
-				SetDParam(0, v->unitnumber);
-				AddNewsItem(
-					STR_8814_TRAIN_IS_WAITING_IN_DEPOT,
-					NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
-					v->index,
-					0
-				);
-			}
-		}
-	}
-	InvalidateWindowClasses(WC_TRAINS_LIST);
-}
-
 #define MAX_ACCEPTABLE_DEPOT_DIST 16
 
 static void CheckIfTrainNeedsService(Vehicle *v)
--- a/vehicle.c
+++ b/vehicle.c
@@ -106,6 +106,7 @@
 	v->date_of_last_service = _date;
 	v->breakdowns_since_last_service = 0;
 	v->reliability = GetEngine(v->engine_type)->reliability;
+	InvalidateWindow(WC_VEHICLE_DETAILS, v->index); // ensure that last service date and reliability are updated
 }
 
 bool VehicleNeedsService(const Vehicle *v)
@@ -2416,6 +2417,84 @@
 	return (flags & DC_EXEC) ? 0 : CMD_ERROR;
 }
 
+void VehicleEnterDepot(Vehicle *v)
+{
+	switch (v->type) {
+		case VEH_Train:
+			InvalidateWindowClasses(WC_TRAINS_LIST);
+			if (!IsFrontEngine(v)) v = GetFirstVehicleInChain(v);
+			UpdateSignalsOnSegment(v->tile, GetRailDepotDirection(v->tile));
+			v->load_unload_time_rem = 0;
+			break;
+
+		case VEH_Road:
+			InvalidateWindowClasses(WC_ROADVEH_LIST);
+			v->u.road.state = 254;
+			break;
+
+		case VEH_Ship:
+			InvalidateWindowClasses(WC_SHIPS_LIST);
+			v->u.ship.state = 0x80;
+			RecalcShipStuff(v);
+			break;
+
+		case VEH_Aircraft:
+			InvalidateWindowClasses(WC_AIRCRAFT_LIST);
+			HandleAircraftEnterHangar(v);
+			break;
+		default: NOT_REACHED();
+	}
+
+	InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
+
+	v->vehstatus |= VS_HIDDEN;
+	v->cur_speed = 0;
+
+	VehicleServiceInDepot(v);
+
+	TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT);
+
+	if (v->current_order.type == OT_GOTO_DEPOT) {
+		Order t;
+
+		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
+
+		t = v->current_order;
+		v->current_order.type = OT_DUMMY;
+		v->current_order.flags = 0;
+
+		if (t.refit_cargo != CT_NO_REFIT) {
+			int32 cost;
+
+			_current_player = v->owner;
+			cost = DoCommand(v->tile, v->index, t.refit_cargo | t.refit_subtype << 8, DC_EXEC, CMD_REFIT_VEH(v->type));
+			if (!CmdFailed(cost) && v->owner == _local_player && cost != 0) ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost);
+		}
+
+		if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) {
+			/* Part of orders */
+			if (v->type == VEH_Train) v->u.rail.days_since_order_progr = 0;
+			v->cur_order_index++;
+		} else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) {
+			/* Force depot visit */
+			v->vehstatus |= VS_STOPPED;
+			if (v->owner == _local_player) {
+				StringID string;
+
+				switch (v->type) {
+					case VEH_Train:    string = STR_8814_TRAIN_IS_WAITING_IN_DEPOT; break;
+					case VEH_Road:     string = STR_9016_ROAD_VEHICLE_IS_WAITING;   break;
+					case VEH_Ship:     string = STR_981C_SHIP_IS_WAITING_IN_DEPOT;  break;
+					case VEH_Aircraft: string = STR_A014_AIRCRAFT_IS_WAITING_IN;    break;
+					default: NOT_REACHED(); string = STR_EMPTY; // Set the string to something to avoid a compiler warning
+				}
+
+				SetDParam(0, v->unitnumber);
+				AddNewsItem(string, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),	v->index, 0);
+			}
+		}
+	}
+}
 
 /** Give a custom name to your vehicle
  * @param tile unused
--- a/vehicle.h
+++ b/vehicle.h
@@ -272,8 +272,6 @@
 
 void ViewportAddVehicles(DrawPixelInfo *dpi);
 
-void TrainEnterDepot(Vehicle *v, TileIndex tile);
-
 /* train_cmd.h */
 int GetTrainImage(const Vehicle* v, Direction direction);
 int GetAircraftImage(const Vehicle* v, Direction direction);
@@ -320,6 +318,7 @@
 uint GenerateVehicleSortList(const Vehicle*** sort_list, uint16 *length_of_array, byte type, PlayerID owner, StationID station, OrderID order, uint16 depot_airport_index, uint16 window_type);
 void BuildDepotVehicleList(byte type, TileIndex tile, Vehicle ***engine_list, uint16 *engine_list_length, uint16 *engine_count, Vehicle ***wagon_list, uint16 *wagon_list_length, uint16 *wagon_count);
 int32 SendAllVehiclesToDepot(byte type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id);
+void VehicleEnterDepot(Vehicle *v);
 
 /* Flags to add to p2 for goto depot commands */
 /* Note: bits 8-10 are used for VLW flags */