|
1 /* $Id$ */ |
|
2 |
|
3 #include "stdafx.h" |
|
4 #include "openttd.h" |
|
5 #include "roadveh.h" |
|
6 #include "ship.h" |
|
7 #include "table/strings.h" |
|
8 #include "functions.h" |
|
9 #include "news.h" |
|
10 #include "command.h" |
|
11 #include "player.h" |
|
12 #include "engine.h" |
|
13 #include "debug.h" |
|
14 #include "vehicle_gui.h" |
|
15 #include "depot.h" |
|
16 #include "train.h" |
|
17 #include "aircraft.h" |
|
18 #include "cargotype.h" |
|
19 |
|
20 |
|
21 /* |
|
22 * move the cargo from one engine to another if possible |
|
23 */ |
|
24 static void MoveVehicleCargo(Vehicle *dest, Vehicle *source) |
|
25 { |
|
26 Vehicle *v = dest; |
|
27 int units_moved; |
|
28 |
|
29 do { |
|
30 do { |
|
31 if (source->cargo_type != dest->cargo_type) |
|
32 continue; // cargo not compatible |
|
33 |
|
34 if (dest->cargo_count == dest->cargo_cap) |
|
35 continue; // the destination vehicle is already full |
|
36 |
|
37 units_moved = min(source->cargo_count, dest->cargo_cap - dest->cargo_count); |
|
38 source->cargo_count -= units_moved; |
|
39 dest->cargo_count += units_moved; |
|
40 dest->cargo_source = source->cargo_source; |
|
41 |
|
42 // copy the age of the cargo |
|
43 dest->cargo_days = source->cargo_days; |
|
44 dest->day_counter = source->day_counter; |
|
45 dest->tick_counter = source->tick_counter; |
|
46 |
|
47 } while (source->cargo_count > 0 && (dest = dest->next) != NULL); |
|
48 dest = v; |
|
49 } while ((source = source->next) != NULL); |
|
50 |
|
51 /* |
|
52 * The of the train will be incorrect at this moment. This is due |
|
53 * to the fact that removing the old wagon updates the weight of |
|
54 * the complete train, which is without the weight of cargo we just |
|
55 * moved back into some (of the) new wagon(s). |
|
56 */ |
|
57 if (dest->type == VEH_TRAIN) TrainConsistChanged(dest->first); |
|
58 } |
|
59 |
|
60 static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, const EngineID engine_type) |
|
61 { |
|
62 const Order *o; |
|
63 const Vehicle *u; |
|
64 |
|
65 if (v->type == VEH_TRAIN) { |
|
66 u = GetFirstVehicleInChain(v); |
|
67 } else { |
|
68 u = v; |
|
69 } |
|
70 |
|
71 FOR_VEHICLE_ORDERS(u, o) { |
|
72 if (!(o->refit_cargo < NUM_CARGO)) continue; |
|
73 if (!CanRefitTo(v->engine_type, o->refit_cargo)) continue; |
|
74 if (!CanRefitTo(engine_type, o->refit_cargo)) return false; |
|
75 } |
|
76 |
|
77 return true; |
|
78 } |
|
79 |
|
80 /** |
|
81 * Function to find what type of cargo to refit to when autoreplacing |
|
82 * @param *v Original vehicle, that is being replaced |
|
83 * @param engine_type The EngineID of the vehicle that is being replaced to |
|
84 * @return The cargo type to replace to |
|
85 * CT_NO_REFIT is returned if no refit is needed |
|
86 * CT_INVALID is returned when both old and new vehicle got cargo capacity and refitting the new one to the old one's cargo type isn't possible |
|
87 */ |
|
88 static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type) |
|
89 { |
|
90 bool new_cargo_capacity = true; |
|
91 CargoID new_cargo_type = CT_INVALID; |
|
92 |
|
93 switch (v->type) { |
|
94 case VEH_TRAIN: |
|
95 new_cargo_capacity = (RailVehInfo(engine_type)->capacity > 0); |
|
96 new_cargo_type = RailVehInfo(engine_type)->cargo_type; |
|
97 break; |
|
98 |
|
99 case VEH_ROAD: |
|
100 new_cargo_capacity = (RoadVehInfo(engine_type)->capacity > 0); |
|
101 new_cargo_type = RoadVehInfo(engine_type)->cargo_type; |
|
102 break; |
|
103 case VEH_SHIP: |
|
104 new_cargo_capacity = (ShipVehInfo(engine_type)->capacity > 0); |
|
105 new_cargo_type = ShipVehInfo(engine_type)->cargo_type; |
|
106 break; |
|
107 |
|
108 case VEH_AIRCRAFT: |
|
109 /* all aircraft starts as passenger planes with cargo capacity |
|
110 * new_cargo_capacity is always true for aircraft, which is the init value. No need to set it here */ |
|
111 new_cargo_type = CT_PASSENGERS; |
|
112 break; |
|
113 |
|
114 default: NOT_REACHED(); break; |
|
115 } |
|
116 |
|
117 if (!new_cargo_capacity) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity |
|
118 |
|
119 if (v->cargo_type == new_cargo_type || CanRefitTo(engine_type, v->cargo_type)) { |
|
120 if (VerifyAutoreplaceRefitForOrders(v, engine_type)) { |
|
121 return v->cargo_type == new_cargo_type ? (CargoID)CT_NO_REFIT : v->cargo_type; |
|
122 } else { |
|
123 return CT_INVALID; |
|
124 } |
|
125 } |
|
126 if (v->type != VEH_TRAIN) return CT_INVALID; // We can't refit the vehicle to carry the cargo we want |
|
127 |
|
128 /* Below this line it's safe to assume that the vehicle in question is a train */ |
|
129 |
|
130 if (v->cargo_cap != 0) return CT_INVALID; // trying to replace a vehicle with cargo capacity into another one with incompatible cargo type |
|
131 |
|
132 /* the old engine didn't have cargo capacity, but the new one does |
|
133 * now we will figure out what cargo the train is carrying and refit to fit this */ |
|
134 v = GetFirstVehicleInChain(v); |
|
135 do { |
|
136 if (v->cargo_cap == 0) continue; |
|
137 /* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */ |
|
138 if (v->cargo_type == new_cargo_type) return CT_NO_REFIT; |
|
139 if (CanRefitTo(engine_type, v->cargo_type)) return v->cargo_type; |
|
140 } while ((v=v->next) != NULL); |
|
141 return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one |
|
142 } |
|
143 |
|
144 /* Replaces a vehicle (used to be called autorenew) |
|
145 * This function is only called from MaybeReplaceVehicle() |
|
146 * Must be called with _current_player set to the owner of the vehicle |
|
147 * @param w Vehicle to replace |
|
148 * @param flags is the flags to use when calling DoCommand(). Mainly DC_EXEC counts |
|
149 * @return value is cost of the replacement or CMD_ERROR |
|
150 */ |
|
151 static int32 ReplaceVehicle(Vehicle **w, byte flags, int32 total_cost) |
|
152 { |
|
153 int32 cost; |
|
154 int32 sell_value; |
|
155 Vehicle *old_v = *w; |
|
156 const Player *p = GetPlayer(old_v->owner); |
|
157 EngineID new_engine_type; |
|
158 const UnitID cached_unitnumber = old_v->unitnumber; |
|
159 bool new_front = false; |
|
160 Vehicle *new_v = NULL; |
|
161 char vehicle_name[32]; |
|
162 CargoID replacement_cargo_type; |
|
163 |
|
164 new_engine_type = EngineReplacementForPlayer(p, old_v->engine_type); |
|
165 if (new_engine_type == INVALID_ENGINE) new_engine_type = old_v->engine_type; |
|
166 |
|
167 replacement_cargo_type = GetNewCargoTypeForReplace(old_v, new_engine_type); |
|
168 |
|
169 /* check if we can't refit to the needed type, so no replace takes place to prevent the vehicle from altering cargo type */ |
|
170 if (replacement_cargo_type == CT_INVALID) return 0; |
|
171 |
|
172 sell_value = DoCommand(0, old_v->index, 0, DC_QUERY_COST, GetCmdSellVeh(old_v)); |
|
173 |
|
174 /* We give the player a loan of the same amount as the sell value. |
|
175 * This is needed in case he needs the income from the sale to build the new vehicle. |
|
176 * We take it back if building fails or when we really sell the old engine */ |
|
177 SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
178 SubtractMoneyFromPlayer(sell_value); |
|
179 |
|
180 cost = DoCommand(old_v->tile, new_engine_type, 3, flags, GetCmdBuildVeh(old_v)); |
|
181 if (CmdFailed(cost)) { |
|
182 SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
183 SubtractMoneyFromPlayer(-sell_value); // Take back the money we just gave the player |
|
184 return cost; |
|
185 } |
|
186 |
|
187 if (replacement_cargo_type != CT_NO_REFIT) cost += GetRefitCost(new_engine_type); // add refit cost |
|
188 |
|
189 if (flags & DC_EXEC) { |
|
190 new_v = GetVehicle(_new_vehicle_id); |
|
191 *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 |
|
192 |
|
193 /* refit if needed */ |
|
194 if (replacement_cargo_type != CT_NO_REFIT) { |
|
195 if (CmdFailed(DoCommand(0, new_v->index, replacement_cargo_type, DC_EXEC, GetCmdRefitVeh(new_v)))) { |
|
196 /* Being here shows a failure, which most likely is in GetNewCargoTypeForReplace() or incorrect estimation costs */ |
|
197 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); |
|
198 } |
|
199 } |
|
200 |
|
201 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))) { |
|
202 // we are autorenewing to a single engine, so we will turn it as the old one was turned as well |
|
203 SETBIT(new_v->u.rail.flags, VRF_REVERSE_DIRECTION); |
|
204 } |
|
205 |
|
206 if (old_v->type == VEH_TRAIN && !IsFrontEngine(old_v)) { |
|
207 /* this is a railcar. We need to move the car into the train |
|
208 * We add the new engine after the old one instead of replacing it. It will give the same result anyway when we |
|
209 * sell the old engine in a moment |
|
210 */ |
|
211 DoCommand(0, (GetPrevVehicleInChain(old_v)->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
|
212 /* Now we move the old one out of the train */ |
|
213 DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
|
214 } else { |
|
215 // copy/clone the orders |
|
216 DoCommand(0, (old_v->index << 16) | new_v->index, IsOrderListShared(old_v) ? CO_SHARE : CO_COPY, DC_EXEC, CMD_CLONE_ORDER); |
|
217 new_v->cur_order_index = old_v->cur_order_index; |
|
218 ChangeVehicleViewWindow(old_v, new_v); |
|
219 new_v->profit_this_year = old_v->profit_this_year; |
|
220 new_v->profit_last_year = old_v->profit_last_year; |
|
221 new_v->service_interval = old_v->service_interval; |
|
222 new_front = true; |
|
223 new_v->unitnumber = old_v->unitnumber; // use the same unit number |
|
224 |
|
225 new_v->current_order = old_v->current_order; |
|
226 if (old_v->type == VEH_TRAIN && GetNextVehicle(old_v) != NULL){ |
|
227 Vehicle *temp_v = GetNextVehicle(old_v); |
|
228 |
|
229 // move the entire train to the new engine, excluding the old engine |
|
230 if (IsMultiheaded(old_v) && temp_v == old_v->u.rail.other_multiheaded_part) { |
|
231 // we got front and rear of a multiheaded engine right after each other. We should work with the next in line instead |
|
232 temp_v = GetNextVehicle(temp_v); |
|
233 } |
|
234 |
|
235 if (temp_v != NULL) { |
|
236 DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
|
237 } |
|
238 } |
|
239 } |
|
240 /* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */ |
|
241 MoveVehicleCargo(new_v->type == VEH_TRAIN ? GetFirstVehicleInChain(new_v) : new_v, old_v); |
|
242 |
|
243 // Get the name of the old vehicle if it has a custom name. |
|
244 if (!IsCustomName(old_v->string_id)) { |
|
245 vehicle_name[0] = '\0'; |
|
246 } else { |
|
247 GetName(vehicle_name, old_v->string_id & 0x7FF, lastof(vehicle_name)); |
|
248 } |
|
249 } else { // flags & DC_EXEC not set |
|
250 /* Ensure that the player will not end up having negative money while autoreplacing |
|
251 * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */ |
|
252 if (p->money64 < (cost + total_cost)) { |
|
253 SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
254 SubtractMoneyFromPlayer(-sell_value); // Pay back the loan |
|
255 return CMD_ERROR; |
|
256 } |
|
257 } |
|
258 |
|
259 /* Take back the money we just gave the player just before building the vehicle |
|
260 * The player will get the same amount now that the sale actually takes place */ |
|
261 SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
262 SubtractMoneyFromPlayer(-sell_value); |
|
263 |
|
264 /* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */ |
|
265 cost += DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v)); |
|
266 |
|
267 if (new_front) { |
|
268 /* now we assign the old unitnumber to the new vehicle */ |
|
269 new_v->unitnumber = cached_unitnumber; |
|
270 } |
|
271 |
|
272 /* Transfer the name of the old vehicle */ |
|
273 if ((flags & DC_EXEC) && vehicle_name[0] != '\0') { |
|
274 _cmd_text = vehicle_name; |
|
275 DoCommand(0, new_v->index, 0, DC_EXEC, CMD_NAME_VEHICLE); |
|
276 } |
|
277 |
|
278 return cost; |
|
279 } |
|
280 |
|
281 /** replaces a vehicle if it's set for autoreplace or is too old |
|
282 * (used to be called autorenew) |
|
283 * @param v The vehicle to replace |
|
284 * if the vehicle is a train, v needs to be the front engine |
|
285 * @param check Checks if the replace is valid. No action is done at all |
|
286 * @param display_costs If set, a cost animation is shown (only if check is false) |
|
287 * @return CMD_ERROR if something went wrong. Otherwise the price of the replace |
|
288 */ |
|
289 int32 MaybeReplaceVehicle(Vehicle *v, bool check, bool display_costs) |
|
290 { |
|
291 Vehicle *w; |
|
292 const Player *p = GetPlayer(v->owner); |
|
293 byte flags = 0; |
|
294 int32 cost, temp_cost = 0; |
|
295 bool stopped; |
|
296 |
|
297 /* Remember the length in case we need to trim train later on |
|
298 * If it's not a train, the value is unused |
|
299 * round up to the length of the tiles used for the train instead of the train length instead |
|
300 * Useful when newGRF uses custom length */ |
|
301 uint16 old_total_length = (v->type == VEH_TRAIN ? |
|
302 (v->u.rail.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE : |
|
303 -1 |
|
304 ); |
|
305 |
|
306 |
|
307 _current_player = v->owner; |
|
308 |
|
309 assert(IsPlayerBuildableVehicleType(v)); |
|
310 |
|
311 assert(v->vehstatus & VS_STOPPED); // the vehicle should have been stopped in VehicleEnteredDepotThisTick() if needed |
|
312 |
|
313 /* Remember the flag v->leave_depot_instantly because if we replace the vehicle, the vehicle holding this flag will be sold |
|
314 * If it is set, then we only stopped the vehicle to replace it (if needed) and we will need to start it again. |
|
315 * We also need to reset the flag since it should remain false except from when the vehicle enters a depot until autoreplace is handled in the same tick */ |
|
316 stopped = v->leave_depot_instantly; |
|
317 v->leave_depot_instantly = false; |
|
318 |
|
319 for (;;) { |
|
320 cost = 0; |
|
321 w = v; |
|
322 do { |
|
323 if (w->type == VEH_TRAIN && IsMultiheaded(w) && !IsTrainEngine(w)) { |
|
324 /* we build the rear ends of multiheaded trains with the front ones */ |
|
325 continue; |
|
326 } |
|
327 |
|
328 // check if the vehicle should be replaced |
|
329 if (!p->engine_renew || |
|
330 w->age - w->max_age < (p->engine_renew_months * 30) || // replace if engine is too old |
|
331 w->max_age == 0) { // rail cars got a max age of 0 |
|
332 if (!EngineHasReplacementForPlayer(p, w->engine_type)) // updates to a new model |
|
333 continue; |
|
334 } |
|
335 |
|
336 /* Now replace the vehicle */ |
|
337 temp_cost = ReplaceVehicle(&w, flags, cost); |
|
338 |
|
339 if (flags & DC_EXEC && |
|
340 (w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE)) { |
|
341 /* now we bought a new engine and sold the old one. We need to fix the |
|
342 * pointers in order to avoid pointing to the old one for trains: these |
|
343 * pointers should point to the front engine and not the cars |
|
344 */ |
|
345 v = w; |
|
346 } |
|
347 |
|
348 if (!CmdFailed(temp_cost)) { |
|
349 cost += temp_cost; |
|
350 } |
|
351 } while (w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != NULL); |
|
352 |
|
353 if (!(flags & DC_EXEC) && (p->money64 < (int32)(cost + p->engine_renew_money) || cost == 0)) { |
|
354 if (!check && p->money64 < (int32)(cost + p->engine_renew_money) && ( _local_player == v->owner ) && cost != 0) { |
|
355 StringID message; |
|
356 SetDParam(0, v->unitnumber); |
|
357 switch (v->type) { |
|
358 case VEH_TRAIN: message = STR_TRAIN_AUTORENEW_FAILED; break; |
|
359 case VEH_ROAD: message = STR_ROADVEHICLE_AUTORENEW_FAILED; break; |
|
360 case VEH_SHIP: message = STR_SHIP_AUTORENEW_FAILED; break; |
|
361 case VEH_AIRCRAFT: message = STR_AIRCRAFT_AUTORENEW_FAILED; break; |
|
362 // This should never happen |
|
363 default: NOT_REACHED(); message = 0; break; |
|
364 } |
|
365 |
|
366 AddNewsItem(message, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0); |
|
367 } |
|
368 if (stopped) v->vehstatus &= ~VS_STOPPED; |
|
369 if (display_costs) _current_player = OWNER_NONE; |
|
370 return CMD_ERROR; |
|
371 } |
|
372 |
|
373 if (flags & DC_EXEC) { |
|
374 break; // we are done replacing since the loop ran once with DC_EXEC |
|
375 } else if (check) { |
|
376 /* It's a test only and we know that we can do this |
|
377 * NOTE: payment for wagon removal is NOT included in this price */ |
|
378 return cost; |
|
379 } |
|
380 // now we redo the loop, but this time we actually do stuff since we know that we can do it |
|
381 flags |= DC_EXEC; |
|
382 } |
|
383 |
|
384 /* If setting is on to try not to exceed the old length of the train with the replacement */ |
|
385 if (v->type == VEH_TRAIN && p->renew_keep_length) { |
|
386 Vehicle *temp; |
|
387 w = v; |
|
388 |
|
389 while (v->u.rail.cached_total_length > old_total_length) { |
|
390 // the train is too long. We will remove cars one by one from the start of the train until it's short enough |
|
391 while (w != NULL && RailVehInfo(w->engine_type)->railveh_type != RAILVEH_WAGON) { |
|
392 w = GetNextVehicle(w); |
|
393 } |
|
394 if (w == NULL) { |
|
395 // we failed to make the train short enough |
|
396 SetDParam(0, v->unitnumber); |
|
397 AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0); |
|
398 break; |
|
399 } |
|
400 temp = w; |
|
401 w = GetNextVehicle(w); |
|
402 DoCommand(0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
|
403 MoveVehicleCargo(v, temp); |
|
404 cost += DoCommand(0, temp->index, 0, DC_EXEC, CMD_SELL_RAIL_WAGON); |
|
405 } |
|
406 } |
|
407 |
|
408 if (stopped) v->vehstatus &= ~VS_STOPPED; |
|
409 if (display_costs) { |
|
410 if (IsLocalPlayer()) ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost); |
|
411 _current_player = OWNER_NONE; |
|
412 } |
|
413 return cost; |
|
414 } |