6298
|
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 |
{
|
6308
|
90 |
CargoID new_cargo_type = GetEngineCargoType(engine_type);
|
6298
|
91 |
|
6308
|
92 |
if (new_cargo_type == CT_INVALID) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity
|
6298
|
93 |
|
|
94 |
if (v->cargo_type == new_cargo_type || CanRefitTo(engine_type, v->cargo_type)) {
|
|
95 |
if (VerifyAutoreplaceRefitForOrders(v, engine_type)) {
|
|
96 |
return v->cargo_type == new_cargo_type ? (CargoID)CT_NO_REFIT : v->cargo_type;
|
|
97 |
} else {
|
|
98 |
return CT_INVALID;
|
|
99 |
}
|
|
100 |
}
|
|
101 |
if (v->type != VEH_TRAIN) return CT_INVALID; // We can't refit the vehicle to carry the cargo we want
|
|
102 |
|
|
103 |
/* Below this line it's safe to assume that the vehicle in question is a train */
|
|
104 |
|
|
105 |
if (v->cargo_cap != 0) return CT_INVALID; // trying to replace a vehicle with cargo capacity into another one with incompatible cargo type
|
|
106 |
|
|
107 |
/* the old engine didn't have cargo capacity, but the new one does
|
|
108 |
* now we will figure out what cargo the train is carrying and refit to fit this */
|
|
109 |
v = GetFirstVehicleInChain(v);
|
|
110 |
do {
|
|
111 |
if (v->cargo_cap == 0) continue;
|
|
112 |
/* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */
|
|
113 |
if (v->cargo_type == new_cargo_type) return CT_NO_REFIT;
|
|
114 |
if (CanRefitTo(engine_type, v->cargo_type)) return v->cargo_type;
|
6308
|
115 |
} while ((v = v->next) != NULL);
|
6298
|
116 |
return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one
|
|
117 |
}
|
|
118 |
|
|
119 |
/* Replaces a vehicle (used to be called autorenew)
|
|
120 |
* This function is only called from MaybeReplaceVehicle()
|
|
121 |
* Must be called with _current_player set to the owner of the vehicle
|
|
122 |
* @param w Vehicle to replace
|
|
123 |
* @param flags is the flags to use when calling DoCommand(). Mainly DC_EXEC counts
|
|
124 |
* @return value is cost of the replacement or CMD_ERROR
|
|
125 |
*/
|
|
126 |
static int32 ReplaceVehicle(Vehicle **w, byte flags, int32 total_cost)
|
|
127 |
{
|
|
128 |
int32 cost;
|
|
129 |
int32 sell_value;
|
|
130 |
Vehicle *old_v = *w;
|
|
131 |
const Player *p = GetPlayer(old_v->owner);
|
|
132 |
EngineID new_engine_type;
|
|
133 |
const UnitID cached_unitnumber = old_v->unitnumber;
|
|
134 |
bool new_front = false;
|
|
135 |
Vehicle *new_v = NULL;
|
|
136 |
char vehicle_name[32];
|
|
137 |
CargoID replacement_cargo_type;
|
|
138 |
|
|
139 |
new_engine_type = EngineReplacementForPlayer(p, old_v->engine_type);
|
|
140 |
if (new_engine_type == INVALID_ENGINE) new_engine_type = old_v->engine_type;
|
|
141 |
|
|
142 |
replacement_cargo_type = GetNewCargoTypeForReplace(old_v, new_engine_type);
|
|
143 |
|
|
144 |
/* 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;
|
|
146 |
|
|
147 |
sell_value = DoCommand(0, old_v->index, 0, DC_QUERY_COST, GetCmdSellVeh(old_v));
|
|
148 |
|
|
149 |
/* We give the player a loan of the same amount as the sell value.
|
|
150 |
* This is needed in case he needs the income from the sale to build the new vehicle.
|
|
151 |
* We take it back if building fails or when we really sell the old engine */
|
|
152 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
153 |
SubtractMoneyFromPlayer(sell_value);
|
|
154 |
|
|
155 |
cost = DoCommand(old_v->tile, new_engine_type, 3, flags, GetCmdBuildVeh(old_v));
|
|
156 |
if (CmdFailed(cost)) {
|
|
157 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
158 |
SubtractMoneyFromPlayer(-sell_value); // Take back the money we just gave the player
|
|
159 |
return cost;
|
|
160 |
}
|
|
161 |
|
|
162 |
if (replacement_cargo_type != CT_NO_REFIT) cost += GetRefitCost(new_engine_type); // add refit cost
|
|
163 |
|
|
164 |
if (flags & DC_EXEC) {
|
|
165 |
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
|
|
167 |
|
|
168 |
/* refit if needed */
|
|
169 |
if (replacement_cargo_type != CT_NO_REFIT) {
|
|
170 |
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 */
|
|
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);
|
|
173 |
}
|
|
174 |
}
|
|
175 |
|
|
176 |
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))) {
|
|
177 |
// we are autorenewing to a single engine, so we will turn it as the old one was turned as well
|
|
178 |
SETBIT(new_v->u.rail.flags, VRF_REVERSE_DIRECTION);
|
|
179 |
}
|
|
180 |
|
|
181 |
if (old_v->type == VEH_TRAIN && !IsFrontEngine(old_v)) {
|
|
182 |
/* this is a railcar. We need to move the car into the train
|
|
183 |
* We add the new engine after the old one instead of replacing it. It will give the same result anyway when we
|
|
184 |
* sell the old engine in a moment
|
|
185 |
*/
|
|
186 |
DoCommand(0, (GetPrevVehicleInChain(old_v)->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
|
|
187 |
/* Now we move the old one out of the train */
|
|
188 |
DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
|
|
189 |
} else {
|
|
190 |
// copy/clone the orders
|
|
191 |
DoCommand(0, (old_v->index << 16) | new_v->index, IsOrderListShared(old_v) ? CO_SHARE : CO_COPY, DC_EXEC, CMD_CLONE_ORDER);
|
|
192 |
new_v->cur_order_index = old_v->cur_order_index;
|
|
193 |
ChangeVehicleViewWindow(old_v, new_v);
|
|
194 |
new_v->profit_this_year = old_v->profit_this_year;
|
|
195 |
new_v->profit_last_year = old_v->profit_last_year;
|
|
196 |
new_v->service_interval = old_v->service_interval;
|
|
197 |
new_front = true;
|
|
198 |
new_v->unitnumber = old_v->unitnumber; // use the same unit number
|
|
199 |
|
|
200 |
new_v->current_order = old_v->current_order;
|
|
201 |
if (old_v->type == VEH_TRAIN && GetNextVehicle(old_v) != NULL){
|
|
202 |
Vehicle *temp_v = GetNextVehicle(old_v);
|
|
203 |
|
|
204 |
// move the entire train to the new engine, excluding the old engine
|
|
205 |
if (IsMultiheaded(old_v) && temp_v == old_v->u.rail.other_multiheaded_part) {
|
|
206 |
// we got front and rear of a multiheaded engine right after each other. We should work with the next in line instead
|
|
207 |
temp_v = GetNextVehicle(temp_v);
|
|
208 |
}
|
|
209 |
|
|
210 |
if (temp_v != NULL) {
|
|
211 |
DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
|
|
212 |
}
|
|
213 |
}
|
|
214 |
}
|
|
215 |
/* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */
|
|
216 |
MoveVehicleCargo(new_v->type == VEH_TRAIN ? GetFirstVehicleInChain(new_v) : new_v, old_v);
|
|
217 |
|
|
218 |
// Get the name of the old vehicle if it has a custom name.
|
|
219 |
if (!IsCustomName(old_v->string_id)) {
|
|
220 |
vehicle_name[0] = '\0';
|
|
221 |
} else {
|
|
222 |
GetName(vehicle_name, old_v->string_id & 0x7FF, lastof(vehicle_name));
|
|
223 |
}
|
|
224 |
} else { // flags & DC_EXEC not set
|
|
225 |
/* Ensure that the player will not end up having negative money while autoreplacing
|
|
226 |
* This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */
|
|
227 |
if (p->money64 < (cost + total_cost)) {
|
|
228 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
229 |
SubtractMoneyFromPlayer(-sell_value); // Pay back the loan
|
|
230 |
return CMD_ERROR;
|
|
231 |
}
|
|
232 |
}
|
|
233 |
|
|
234 |
/* Take back the money we just gave the player just before building the vehicle
|
|
235 |
* The player will get the same amount now that the sale actually takes place */
|
|
236 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
237 |
SubtractMoneyFromPlayer(-sell_value);
|
|
238 |
|
|
239 |
/* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */
|
|
240 |
cost += DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v));
|
|
241 |
|
|
242 |
if (new_front) {
|
|
243 |
/* now we assign the old unitnumber to the new vehicle */
|
|
244 |
new_v->unitnumber = cached_unitnumber;
|
|
245 |
}
|
|
246 |
|
|
247 |
/* Transfer the name of the old vehicle */
|
|
248 |
if ((flags & DC_EXEC) && vehicle_name[0] != '\0') {
|
|
249 |
_cmd_text = vehicle_name;
|
|
250 |
DoCommand(0, new_v->index, 0, DC_EXEC, CMD_NAME_VEHICLE);
|
|
251 |
}
|
|
252 |
|
|
253 |
return cost;
|
|
254 |
}
|
|
255 |
|
|
256 |
/** replaces a vehicle if it's set for autoreplace or is too old
|
|
257 |
* (used to be called autorenew)
|
|
258 |
* @param v The vehicle to replace
|
|
259 |
* if the vehicle is a train, v needs to be the front engine
|
|
260 |
* @param check Checks if the replace is valid. No action is done at all
|
|
261 |
* @param display_costs If set, a cost animation is shown (only if check is false)
|
|
262 |
* @return CMD_ERROR if something went wrong. Otherwise the price of the replace
|
|
263 |
*/
|
|
264 |
int32 MaybeReplaceVehicle(Vehicle *v, bool check, bool display_costs)
|
|
265 |
{
|
|
266 |
Vehicle *w;
|
|
267 |
const Player *p = GetPlayer(v->owner);
|
|
268 |
byte flags = 0;
|
|
269 |
int32 cost, temp_cost = 0;
|
|
270 |
bool stopped;
|
|
271 |
|
|
272 |
/* Remember the length in case we need to trim train later on
|
|
273 |
* If it's not a train, the value is unused
|
|
274 |
* round up to the length of the tiles used for the train instead of the train length instead
|
|
275 |
* Useful when newGRF uses custom length */
|
|
276 |
uint16 old_total_length = (v->type == VEH_TRAIN ?
|
|
277 |
(v->u.rail.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE :
|
|
278 |
-1
|
|
279 |
);
|
|
280 |
|
|
281 |
|
|
282 |
_current_player = v->owner;
|
|
283 |
|
|
284 |
assert(IsPlayerBuildableVehicleType(v));
|
|
285 |
|
|
286 |
assert(v->vehstatus & VS_STOPPED); // the vehicle should have been stopped in VehicleEnteredDepotThisTick() if needed
|
|
287 |
|
|
288 |
/* Remember the flag v->leave_depot_instantly because if we replace the vehicle, the vehicle holding this flag will be sold
|
|
289 |
* If it is set, then we only stopped the vehicle to replace it (if needed) and we will need to start it again.
|
|
290 |
* 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 */
|
|
291 |
stopped = v->leave_depot_instantly;
|
|
292 |
v->leave_depot_instantly = false;
|
|
293 |
|
|
294 |
for (;;) {
|
|
295 |
cost = 0;
|
|
296 |
w = v;
|
|
297 |
do {
|
|
298 |
if (w->type == VEH_TRAIN && IsMultiheaded(w) && !IsTrainEngine(w)) {
|
|
299 |
/* we build the rear ends of multiheaded trains with the front ones */
|
|
300 |
continue;
|
|
301 |
}
|
|
302 |
|
|
303 |
// check if the vehicle should be replaced
|
|
304 |
if (!p->engine_renew ||
|
|
305 |
w->age - w->max_age < (p->engine_renew_months * 30) || // replace if engine is too old
|
|
306 |
w->max_age == 0) { // rail cars got a max age of 0
|
|
307 |
if (!EngineHasReplacementForPlayer(p, w->engine_type)) // updates to a new model
|
|
308 |
continue;
|
|
309 |
}
|
|
310 |
|
|
311 |
/* Now replace the vehicle */
|
|
312 |
temp_cost = ReplaceVehicle(&w, flags, cost);
|
|
313 |
|
|
314 |
if (flags & DC_EXEC &&
|
|
315 |
(w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE)) {
|
|
316 |
/* now we bought a new engine and sold the old one. We need to fix the
|
|
317 |
* pointers in order to avoid pointing to the old one for trains: these
|
|
318 |
* pointers should point to the front engine and not the cars
|
|
319 |
*/
|
|
320 |
v = w;
|
|
321 |
}
|
|
322 |
|
|
323 |
if (!CmdFailed(temp_cost)) {
|
|
324 |
cost += temp_cost;
|
|
325 |
}
|
|
326 |
} while (w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != NULL);
|
|
327 |
|
|
328 |
if (!(flags & DC_EXEC) && (p->money64 < (int32)(cost + p->engine_renew_money) || cost == 0)) {
|
|
329 |
if (!check && p->money64 < (int32)(cost + p->engine_renew_money) && ( _local_player == v->owner ) && cost != 0) {
|
|
330 |
StringID message;
|
|
331 |
SetDParam(0, v->unitnumber);
|
|
332 |
switch (v->type) {
|
|
333 |
case VEH_TRAIN: message = STR_TRAIN_AUTORENEW_FAILED; break;
|
|
334 |
case VEH_ROAD: message = STR_ROADVEHICLE_AUTORENEW_FAILED; break;
|
|
335 |
case VEH_SHIP: message = STR_SHIP_AUTORENEW_FAILED; break;
|
|
336 |
case VEH_AIRCRAFT: message = STR_AIRCRAFT_AUTORENEW_FAILED; break;
|
|
337 |
// This should never happen
|
|
338 |
default: NOT_REACHED(); message = 0; break;
|
|
339 |
}
|
|
340 |
|
|
341 |
AddNewsItem(message, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
|
|
342 |
}
|
|
343 |
if (stopped) v->vehstatus &= ~VS_STOPPED;
|
|
344 |
if (display_costs) _current_player = OWNER_NONE;
|
|
345 |
return CMD_ERROR;
|
|
346 |
}
|
|
347 |
|
|
348 |
if (flags & DC_EXEC) {
|
|
349 |
break; // we are done replacing since the loop ran once with DC_EXEC
|
|
350 |
} else if (check) {
|
|
351 |
/* It's a test only and we know that we can do this
|
|
352 |
* NOTE: payment for wagon removal is NOT included in this price */
|
|
353 |
return cost;
|
|
354 |
}
|
|
355 |
// now we redo the loop, but this time we actually do stuff since we know that we can do it
|
|
356 |
flags |= DC_EXEC;
|
|
357 |
}
|
|
358 |
|
|
359 |
/* If setting is on to try not to exceed the old length of the train with the replacement */
|
|
360 |
if (v->type == VEH_TRAIN && p->renew_keep_length) {
|
|
361 |
Vehicle *temp;
|
|
362 |
w = v;
|
|
363 |
|
|
364 |
while (v->u.rail.cached_total_length > old_total_length) {
|
|
365 |
// the train is too long. We will remove cars one by one from the start of the train until it's short enough
|
|
366 |
while (w != NULL && RailVehInfo(w->engine_type)->railveh_type != RAILVEH_WAGON) {
|
|
367 |
w = GetNextVehicle(w);
|
|
368 |
}
|
|
369 |
if (w == NULL) {
|
|
370 |
// we failed to make the train short enough
|
|
371 |
SetDParam(0, v->unitnumber);
|
|
372 |
AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
|
|
373 |
break;
|
|
374 |
}
|
|
375 |
temp = w;
|
|
376 |
w = GetNextVehicle(w);
|
|
377 |
DoCommand(0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
|
|
378 |
MoveVehicleCargo(v, temp);
|
|
379 |
cost += DoCommand(0, temp->index, 0, DC_EXEC, CMD_SELL_RAIL_WAGON);
|
|
380 |
}
|
|
381 |
}
|
|
382 |
|
|
383 |
if (stopped) v->vehstatus &= ~VS_STOPPED;
|
|
384 |
if (display_costs) {
|
|
385 |
if (IsLocalPlayer()) ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost);
|
|
386 |
_current_player = OWNER_NONE;
|
|
387 |
}
|
|
388 |
return cost;
|
|
389 |
}
|