(svn r1707) - Feature: [autoreplace] road vehicles now tries to find a depot if they needs to be replaced
authorbjarni
Fri, 28 Jan 2005 17:31:42 +0000
changeset 1203 c95a16972da5
parent 1202 7d8b86bd8ba2
child 1204 94bc5d4587c1
(svn r1707) - Feature: [autoreplace] road vehicles now tries to find a depot if they needs to be replaced
roadveh_cmd.c
--- a/roadveh_cmd.c	Fri Jan 28 15:31:04 2005 +0000
+++ b/roadveh_cmd.c	Fri Jan 28 17:31:42 2005 +0000
@@ -291,6 +291,9 @@
 	return GetDepotByTile(rfdd.tile);
 }
 
+/*  Send a road vehicle to the nearest depot
+	p1 = index of the road vehicle
+	p2 = bit 0 = do not stop in depot, bit 1 = have depot in orders */
 int32 CmdSendRoadVehToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
 	Vehicle *v = GetVehicle(p1);
@@ -299,6 +302,10 @@
 	if (v->type != VEH_Road || !CheckOwnership(v->owner))
 		return CMD_ERROR;
 
+	if (HASBIT(p2, 0)) v->set_for_replacement = true;
+
+	if (HASBIT(p2, 1)) return CMD_ERROR;   // vehicle has a depot in schedule. It just needed to set set_for_replacement
+
 	if (v->current_order.type == OT_GOTO_DEPOT) {
 		if (flags & DC_EXEC) {
 			if (v->current_order.flags & OF_UNLOAD)
@@ -316,7 +323,7 @@
 
 	if (flags & DC_EXEC) {
 		v->current_order.type = OT_GOTO_DEPOT;
-		v->current_order.flags = OF_NON_STOP | OF_FULL_LOAD;
+		v->current_order.flags = p2 == 0 ? OF_NON_STOP | OF_FULL_LOAD : 0;
 		v->current_order.station = (byte)depot;
 		v->dest_tile = _depots[depot].xy;
 		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
@@ -568,7 +575,7 @@
 
 	if (v->current_order.type == OT_GOTO_DEPOT &&
 			(v->current_order.flags & (OF_UNLOAD | OF_FULL_LOAD)) == (OF_UNLOAD | OF_FULL_LOAD) &&
-			!VehicleNeedsService(v)) {
+			!VehicleNeedsService(v) && !v->set_for_replacement) {
 		v->cur_order_index++;
 	}
 
@@ -638,6 +645,21 @@
 
 	v->cur_order_index++;
 	InvalidateVehicleOrder(v);
+
+	if (v->current_order.type != OT_GOTO_DEPOT && v->owner == _local_player) {
+		// only the vehicle owner needs to calculate the rest (locally)
+		if ((_autoreplace_array[v->engine_type] != v->engine_type) ||
+			(_patches.autorenew && v->age - v->max_age > (_patches.autorenew_months * 30))) {
+			byte flags = 1;
+			// the flags means, bit 0 = needs to go to depot, bit 1 = have depot in orders
+			if (VehicleHasDepotOrders(v)) SETBIT(flags, 1);
+			if (!(HASBIT(flags, 1) && v->set_for_replacement)) {
+				_current_player = _local_player;
+				DoCommandP(v->tile, v->index, flags, NULL, CMD_SEND_ROADVEH_TO_DEPOT | CMD_SHOW_NO_ERROR);
+				_current_player = OWNER_NONE;
+			}
+		}
+	}
 }
 
 static void StartRoadVehSound(Vehicle *v)
@@ -1431,10 +1453,10 @@
 {
 	int i;
 
-	if (_patches.servint_roadveh == 0)
+	if (_patches.servint_roadveh == 0 && !v->set_for_replacement)
 		return;
 
-	if (!VehicleNeedsService(v))
+	if (!VehicleNeedsService(v) && !v->set_for_replacement)
 		return;
 
 	if (v->vehstatus & VS_STOPPED)
@@ -1452,7 +1474,10 @@
 	i = FindClosestRoadDepot(v);
 
 	if (i < 0 || GetTileDist(v->tile, (&_depots[i])->xy) > 12) {
-		if (v->current_order.type == OT_GOTO_DEPOT) {
+		if (v->current_order.type == OT_GOTO_DEPOT && !(
+			GetTileDist(v->tile, v->dest_tile) > 25 && v->set_for_replacement)) {
+			/*  a vehicle needs a greater distance to a depot to loose it than to find it since
+			they can circle forever othervise if they are in a loop with an unlucky distance	*/
 			v->current_order.type = OT_DUMMY;
 			v->current_order.flags = 0;
 			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);