134 bool new_front = false; |
135 bool new_front = false; |
135 Vehicle *new_v = NULL; |
136 Vehicle *new_v = NULL; |
136 char vehicle_name[32]; |
137 char vehicle_name[32]; |
137 CargoID replacement_cargo_type; |
138 CargoID replacement_cargo_type; |
138 |
139 |
139 new_engine_type = EngineReplacementForPlayer(p, old_v->engine_type); |
140 /* If the vehicle belongs to a group, check if the group is protected from the global autoreplace. |
140 if (new_engine_type == INVALID_ENGINE) new_engine_type = old_v->engine_type; |
141 * If not, chek if an global auto replacement is defined */ |
|
142 new_engine_type = (IsValidGroupID(old_v->group_id) && GetGroup(old_v->group_id)->replace_protection) ? |
|
143 INVALID_ENGINE : |
|
144 EngineReplacementForPlayer(p, old_v->engine_type, DEFAULT_GROUP); |
|
145 |
|
146 /* If we don't set new_egnine_type previously, we try to check if an autoreplacement was defined |
|
147 * for the group and the engine_type of the vehicle */ |
|
148 if (new_engine_type == INVALID_ENGINE && !IsDefaultGroupID(old_v->group_id)) { |
|
149 new_engine_type = EngineReplacementForPlayer(p, old_v->engine_type, old_v->group_id); |
|
150 } |
141 |
151 |
142 replacement_cargo_type = GetNewCargoTypeForReplace(old_v, new_engine_type); |
152 replacement_cargo_type = GetNewCargoTypeForReplace(old_v, new_engine_type); |
143 |
153 |
144 /* check if we can't refit to the needed type, so no replace takes place to prevent the vehicle from altering cargo type */ |
154 /* check if we can't refit to the needed type, so no replace takes place to prevent the vehicle from altering cargo type */ |
145 if (replacement_cargo_type == CT_INVALID) return 0; |
155 if (replacement_cargo_type == CT_INVALID) return 0; |
163 |
173 |
164 if (flags & DC_EXEC) { |
174 if (flags & DC_EXEC) { |
165 new_v = GetVehicle(_new_vehicle_id); |
175 new_v = GetVehicle(_new_vehicle_id); |
166 *w = new_v; //we changed the vehicle, so MaybeReplaceVehicle needs to work on the new one. Now we tell it what the new one is |
176 *w = new_v; //we changed the vehicle, so MaybeReplaceVehicle needs to work on the new one. Now we tell it what the new one is |
167 |
177 |
|
178 new_v->group_id = old_v->group_id; |
168 /* refit if needed */ |
179 /* refit if needed */ |
169 if (replacement_cargo_type != CT_NO_REFIT) { |
180 if (replacement_cargo_type != CT_NO_REFIT) { |
170 if (CmdFailed(DoCommand(0, new_v->index, replacement_cargo_type, DC_EXEC, GetCmdRefitVeh(new_v)))) { |
181 if (CmdFailed(DoCommand(0, new_v->index, replacement_cargo_type, DC_EXEC, GetCmdRefitVeh(new_v)))) { |
171 /* Being here shows a failure, which most likely is in GetNewCargoTypeForReplace() or incorrect estimation costs */ |
182 /* Being here shows a failure, which most likely is in GetNewCargoTypeForReplace() or incorrect estimation costs */ |
172 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); |
183 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); |
192 new_v->cur_order_index = old_v->cur_order_index; |
203 new_v->cur_order_index = old_v->cur_order_index; |
193 ChangeVehicleViewWindow(old_v, new_v); |
204 ChangeVehicleViewWindow(old_v, new_v); |
194 new_v->profit_this_year = old_v->profit_this_year; |
205 new_v->profit_this_year = old_v->profit_this_year; |
195 new_v->profit_last_year = old_v->profit_last_year; |
206 new_v->profit_last_year = old_v->profit_last_year; |
196 new_v->service_interval = old_v->service_interval; |
207 new_v->service_interval = old_v->service_interval; |
|
208 new_v->group_id = old_v->group_id; |
197 new_front = true; |
209 new_front = true; |
198 new_v->unitnumber = old_v->unitnumber; // use the same unit number |
210 new_v->unitnumber = old_v->unitnumber; // use the same unit number |
199 new_v->dest_tile = old_v->dest_tile; |
211 new_v->dest_tile = old_v->dest_tile; |
200 |
212 |
201 new_v->current_order = old_v->current_order; |
213 new_v->current_order = old_v->current_order; |
209 } |
221 } |
210 |
222 |
211 if (temp_v != NULL) { |
223 if (temp_v != NULL) { |
212 DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
224 DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
213 } |
225 } |
|
226 } else if (!IsDefaultGroupID(old_v->group_id) && IsValidGroupID(old_v->group_id)) { |
|
227 /* Increase the new num engines of the group for the ships, aircraft, and road vehicles |
|
228 The old new num engine is decrease in the destroyvehicle function */ |
|
229 GetGroup(old_v->group_id)->num_engines[new_v->engine_type]++; |
214 } |
230 } |
215 } |
231 } |
216 /* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */ |
232 /* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */ |
217 MoveVehicleCargo(new_v->type == VEH_TRAIN ? GetFirstVehicleInChain(new_v) : new_v, old_v); |
233 MoveVehicleCargo(new_v->type == VEH_TRAIN ? GetFirstVehicleInChain(new_v) : new_v, old_v); |
218 |
234 |
303 |
319 |
304 // check if the vehicle should be replaced |
320 // check if the vehicle should be replaced |
305 if (!p->engine_renew || |
321 if (!p->engine_renew || |
306 w->age - w->max_age < (p->engine_renew_months * 30) || // replace if engine is too old |
322 w->age - w->max_age < (p->engine_renew_months * 30) || // replace if engine is too old |
307 w->max_age == 0) { // rail cars got a max age of 0 |
323 w->max_age == 0) { // rail cars got a max age of 0 |
308 if (!EngineHasReplacementForPlayer(p, w->engine_type)) // updates to a new model |
324 /* If the vehicle belongs to a group, check if the group is protected from the global autoreplace. |
|
325 If not, chek if an global auto remplacement is defined */ |
|
326 if (IsValidGroupID(w->group_id)) { |
|
327 if (!EngineHasReplacementForPlayer(p, w->engine_type, w->group_id) && ( |
|
328 GetGroup(w->group_id)->replace_protection || |
|
329 !EngineHasReplacementForPlayer(p, w->engine_type, DEFAULT_GROUP))) { |
|
330 continue; |
|
331 } |
|
332 } else if (!EngineHasReplacementForPlayer(p, w->engine_type, DEFAULT_GROUP)) { |
309 continue; |
333 continue; |
|
334 } |
310 } |
335 } |
311 |
336 |
312 /* Now replace the vehicle */ |
337 /* Now replace the vehicle */ |
313 temp_cost = ReplaceVehicle(&w, flags, cost); |
338 temp_cost = ReplaceVehicle(&w, flags, cost); |
314 |
339 |