src/autoreplace_cmd.cpp
changeset 7988 6075538f6111
parent 7754 8e7205edd6c7
child 7993 76b0eb3e19c9
equal deleted inserted replaced
7987:b3627df3493a 7988:6075538f6111
    40 
    40 
    41 			// copy the age of the cargo
    41 			// copy the age of the cargo
    42 			dest->day_counter  = source->day_counter;
    42 			dest->day_counter  = source->day_counter;
    43 			dest->tick_counter = source->tick_counter;
    43 			dest->tick_counter = source->tick_counter;
    44 
    44 
    45 		} while (source->cargo.Count() > 0 && (dest = dest->next) != NULL);
    45 		} while (source->cargo.Count() > 0 && (dest = dest->Next()) != NULL);
    46 		dest = v;
    46 		dest = v;
    47 	} while ((source = source->next) != NULL);
    47 	} while ((source = source->Next()) != NULL);
    48 
    48 
    49 	/*
    49 	/*
    50 	 * The of the train will be incorrect at this moment. This is due
    50 	 * The of the train will be incorrect at this moment. This is due
    51 	 * to the fact that removing the old wagon updates the weight of
    51 	 * to the fact that removing the old wagon updates the weight of
    52 	 * the complete train, which is without the weight of cargo we just
    52 	 * the complete train, which is without the weight of cargo we just
   108 	do {
   108 	do {
   109 		if (v->cargo_cap == 0) continue;
   109 		if (v->cargo_cap == 0) continue;
   110 		/* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */
   110 		/* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */
   111 		if (v->cargo_type == new_cargo_type) return CT_NO_REFIT;
   111 		if (v->cargo_type == new_cargo_type) return CT_NO_REFIT;
   112 		if (CanRefitTo(engine_type, v->cargo_type)) return v->cargo_type;
   112 		if (CanRefitTo(engine_type, v->cargo_type)) return v->cargo_type;
   113 	} while ((v = v->next) != NULL);
   113 	} while ((v = v->Next()) != NULL);
   114 	return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one
   114 	return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one
   115 }
   115 }
   116 
   116 
   117 /* Replaces a vehicle (used to be called autorenew)
   117 /* Replaces a vehicle (used to be called autorenew)
   118  * This function is only called from MaybeReplaceVehicle()
   118  * This function is only called from MaybeReplaceVehicle()
   187 				/* Being here shows a failure, which most likely is in GetNewCargoTypeForReplace() or incorrect estimation costs */
   187 				/* Being here shows a failure, which most likely is in GetNewCargoTypeForReplace() or incorrect estimation costs */
   188 				error("Autoreplace failed to refit. Replace engine %d to %d and refit to cargo %d", old_v->engine_type, new_v->engine_type, replacement_cargo_type);
   188 				error("Autoreplace failed to refit. Replace engine %d to %d and refit to cargo %d", old_v->engine_type, new_v->engine_type, replacement_cargo_type);
   189 			}
   189 			}
   190 		}
   190 		}
   191 
   191 
   192 		if (new_v->type == VEH_TRAIN && HASBIT(old_v->u.rail.flags, VRF_REVERSE_DIRECTION) && !IsMultiheaded(new_v) && !(new_v->next != NULL && IsArticulatedPart(new_v->next))) {
   192 		if (new_v->type == VEH_TRAIN && HASBIT(old_v->u.rail.flags, VRF_REVERSE_DIRECTION) && !IsMultiheaded(new_v) && !(new_v->Next() != NULL && IsArticulatedPart(new_v->Next()))) {
   193 			// we are autorenewing to a single engine, so we will turn it as the old one was turned as well
   193 			// we are autorenewing to a single engine, so we will turn it as the old one was turned as well
   194 			SETBIT(new_v->u.rail.flags, VRF_REVERSE_DIRECTION);
   194 			SETBIT(new_v->u.rail.flags, VRF_REVERSE_DIRECTION);
   195 		}
   195 		}
   196 
   196 
   197 		if (old_v->type == VEH_TRAIN && !IsFrontEngine(old_v)) {
   197 		if (old_v->type == VEH_TRAIN && !IsFrontEngine(old_v)) {
   245 			SetDParam(0, old_v->index);
   245 			SetDParam(0, old_v->index);
   246 			GetString(vehicle_name, STR_VEHICLE_NAME, lastof(vehicle_name));
   246 			GetString(vehicle_name, STR_VEHICLE_NAME, lastof(vehicle_name));
   247 		}
   247 		}
   248 	} else { // flags & DC_EXEC not set
   248 	} else { // flags & DC_EXEC not set
   249 		CommandCost tmp_move;
   249 		CommandCost tmp_move;
   250 		if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v) && old_v->next != NULL) {
   250 		if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v) && old_v->Next() != NULL) {
   251 			/* Verify that the wagons can be placed on the engine in question.
   251 			/* Verify that the wagons can be placed on the engine in question.
   252 			 * This is done by building an engine, test if the wagons can be added and then sell the test engine. */
   252 			 * This is done by building an engine, test if the wagons can be added and then sell the test engine. */
   253 			DoCommand(old_v->tile, new_engine_type, 3, DC_EXEC, GetCmdBuildVeh(old_v));
   253 			DoCommand(old_v->tile, new_engine_type, 3, DC_EXEC, GetCmdBuildVeh(old_v));
   254 			Vehicle *temp = GetVehicle(_new_vehicle_id);
   254 			Vehicle *temp = GetVehicle(_new_vehicle_id);
   255 			tmp_move = DoCommand(0, (temp->index << 16) | old_v->next->index, 1, 0, CMD_MOVE_RAIL_VEHICLE);
   255 			tmp_move = DoCommand(0, (temp->index << 16) | old_v->Next()->index, 1, 0, CMD_MOVE_RAIL_VEHICLE);
   256 			DoCommand(0, temp->index, 0, DC_EXEC, GetCmdSellVeh(old_v));
   256 			DoCommand(0, temp->index, 0, DC_EXEC, GetCmdSellVeh(old_v));
   257 		}
   257 		}
   258 
   258 
   259 		/* Ensure that the player will not end up having negative money while autoreplacing
   259 		/* Ensure that the player will not end up having negative money while autoreplacing
   260 		 * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */
   260 		 * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */