Files @ r21719:0f172a6b8939
Branch filter:

Location: cpp/openttd-patchpack/source/src/articulated_vehicles.cpp - annotation

peter1138
(svn r26862) -Codechange: Don't shrink widget size in new game window.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
r6772:59235ded8522
r6772:59235ded8522
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6772:59235ded8522
r6772:59235ded8522
r6772:59235ded8522
r6857:1e07df806ef1
r8144:1432edd15267
r14248:a9050881acd7
r17298:f10752811f69
r18291:faa18f9ceb25
r6772:59235ded8522
r11346:032b1fd8218f
r11346:032b1fd8218f
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r9723:ed84b192ea1c
r9723:ed84b192ea1c
r13388:29eb3d8bb664
r13388:29eb3d8bb664
r13388:29eb3d8bb664
r13388:29eb3d8bb664
r13388:29eb3d8bb664
r13388:29eb3d8bb664
r13388:29eb3d8bb664
r13388:29eb3d8bb664
r17111:4d3d3bfd7c40
r13388:29eb3d8bb664
r13388:29eb3d8bb664
r13388:29eb3d8bb664
r18291:faa18f9ceb25
r18291:faa18f9ceb25
r13388:29eb3d8bb664
r18291:faa18f9ceb25
r13388:29eb3d8bb664
r18291:faa18f9ceb25
r18291:faa18f9ceb25
r18291:faa18f9ceb25
r18291:faa18f9ceb25
r18291:faa18f9ceb25
r18291:faa18f9ceb25
r18291:faa18f9ceb25
r18291:faa18f9ceb25
r18291:faa18f9ceb25
r18291:faa18f9ceb25
r18291:faa18f9ceb25
r18291:faa18f9ceb25
r18291:faa18f9ceb25
r18291:faa18f9ceb25
r13388:29eb3d8bb664
r13388:29eb3d8bb664
r17632:92f095eb81a2
r19149:5ee950f53f03
r19149:5ee950f53f03
r19149:5ee950f53f03
r19149:5ee950f53f03
r19149:5ee950f53f03
r19149:5ee950f53f03
r19149:5ee950f53f03
r19149:5ee950f53f03
r19149:5ee950f53f03
r19149:5ee950f53f03
r17632:92f095eb81a2
r17632:92f095eb81a2
r17632:92f095eb81a2
r19944:25a78576fb5e
r17632:92f095eb81a2
r7595:4f881cfee2f3
r6772:59235ded8522
r13026:0c3f00ec0a74
r6772:59235ded8522
r10634:5fca17630ffa
r10634:5fca17630ffa
r10634:5fca17630ffa
r10634:5fca17630ffa
r11967:df0600d2c7e7
r7595:4f881cfee2f3
r11967:df0600d2c7e7
r7595:4f881cfee2f3
r17298:f10752811f69
r7595:4f881cfee2f3
r7595:4f881cfee2f3
r6772:59235ded8522
r9723:ed84b192ea1c
r17111:4d3d3bfd7c40
r6772:59235ded8522
r6772:59235ded8522
r7595:4f881cfee2f3
r7595:4f881cfee2f3
r6772:59235ded8522
r6772:59235ded8522
r6772:59235ded8522
r8559:0d8d367ca4c3
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r19944:25a78576fb5e
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r13209:e62009d81509
r9725:d6d7b42bd986
r11917:612c11f7ab47
r11188:143f3c64befd
r11188:143f3c64befd
r11188:143f3c64befd
r11403:ba31da6812eb
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r18668:b656d614c4fd
r19944:25a78576fb5e
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r13209:e62009d81509
r9725:d6d7b42bd986
r19097:ec2b0cea1565
r19097:ec2b0cea1565
r9725:d6d7b42bd986
r19097:ec2b0cea1565
r19097:ec2b0cea1565
r19097:ec2b0cea1565
r19097:ec2b0cea1565
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r18668:b656d614c4fd
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r17632:92f095eb81a2
r17632:92f095eb81a2
r17632:92f095eb81a2
r17632:92f095eb81a2
r17632:92f095eb81a2
r13211:4ba95564ba64
r8559:0d8d367ca4c3
r12254:2375853718ac
r13211:4ba95564ba64
r8559:0d8d367ca4c3
r9725:d6d7b42bd986
r13209:e62009d81509
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r16774:e8f36b899894
r8559:0d8d367ca4c3
r13211:4ba95564ba64
r8559:0d8d367ca4c3
r9723:ed84b192ea1c
r17111:4d3d3bfd7c40
r13388:29eb3d8bb664
r8559:0d8d367ca4c3
r13209:e62009d81509
r9725:d6d7b42bd986
r8559:0d8d367ca4c3
r8559:0d8d367ca4c3
r8559:0d8d367ca4c3
r8559:0d8d367ca4c3
r8559:0d8d367ca4c3
r9725:d6d7b42bd986
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r21706:ae3a142677ea
r11238:1abc5ab40770
r11238:1abc5ab40770
r11238:1abc5ab40770
r11238:1abc5ab40770
r11238:1abc5ab40770
r11238:1abc5ab40770
r11238:1abc5ab40770
r11238:1abc5ab40770
r11917:612c11f7ab47
r16774:e8f36b899894
r11238:1abc5ab40770
r13026:0c3f00ec0a74
r11238:1abc5ab40770
r11238:1abc5ab40770
r17111:4d3d3bfd7c40
r13388:29eb3d8bb664
r11238:1abc5ab40770
r11238:1abc5ab40770
r11238:1abc5ab40770
r11238:1abc5ab40770
r11238:1abc5ab40770
r11238:1abc5ab40770
r11238:1abc5ab40770
r11238:1abc5ab40770
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r18668:b656d614c4fd
r18668:b656d614c4fd
r18668:b656d614c4fd
r14014:448f8c2f27bc
r16774:e8f36b899894
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r17111:4d3d3bfd7c40
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r18668:b656d614c4fd
r18668:b656d614c4fd
r18668:b656d614c4fd
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r13211:4ba95564ba64
r9725:d6d7b42bd986
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r13211:4ba95564ba64
r9725:d6d7b42bd986
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r18668:b656d614c4fd
r9725:d6d7b42bd986
r18668:b656d614c4fd
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r19097:ec2b0cea1565
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r17175:412ca7ec4689
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r9725:d6d7b42bd986
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r18228:6cd8bae37157
r11346:032b1fd8218f
r14014:448f8c2f27bc
r14014:448f8c2f27bc
r13211:4ba95564ba64
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r12254:2375853718ac
r11346:032b1fd8218f
r11346:032b1fd8218f
r13209:e62009d81509
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r17175:412ca7ec4689
r11346:032b1fd8218f
r11346:032b1fd8218f
r18668:b656d614c4fd
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r11346:032b1fd8218f
r8559:0d8d367ca4c3
r17632:92f095eb81a2
r17632:92f095eb81a2
r17632:92f095eb81a2
r17632:92f095eb81a2
r13209:e62009d81509
r6772:59235ded8522
r13209:e62009d81509
r13026:0c3f00ec0a74
r6772:59235ded8522
r12001:671ce3a543a3
r9723:ed84b192ea1c
r13388:29eb3d8bb664
r17111:4d3d3bfd7c40
r13388:29eb3d8bb664
r6772:59235ded8522
r11897:7e0b37f90451
r11897:7e0b37f90451
r11897:7e0b37f90451
r6772:59235ded8522
r16778:660bd976d69f
r16778:660bd976d69f
r16778:660bd976d69f
r11917:612c11f7ab47
r6857:1e07df806ef1
r6857:1e07df806ef1
r6857:1e07df806ef1
r11980:193f7a6b6e37
r12109:90df01928018
r11980:193f7a6b6e37
r12001:671ce3a543a3
r12001:671ce3a543a3
r12001:671ce3a543a3
r11980:193f7a6b6e37
r11981:3840cc07cbe4
r11981:3840cc07cbe4
r6857:1e07df806ef1
r11980:193f7a6b6e37
r11188:143f3c64befd
r11980:193f7a6b6e37
r11980:193f7a6b6e37
r11188:143f3c64befd
r11980:193f7a6b6e37
r11980:193f7a6b6e37
r11188:143f3c64befd
r20252:76a6f1c78ce7
r6857:1e07df806ef1
r12293:15e6328523ce
r15608:7b580ec7448a
r15608:7b580ec7448a
r6857:1e07df806ef1
r11979:a450390d0f16
r12109:90df01928018
r11979:a450390d0f16
r12001:671ce3a543a3
r12001:671ce3a543a3
r12001:671ce3a543a3
r11979:a450390d0f16
r17441:1a320fa88183
r11979:a450390d0f16
r6857:1e07df806ef1
r11979:a450390d0f16
r11979:a450390d0f16
r6857:1e07df806ef1
r12014:63829fb1bd5f
r11188:143f3c64befd
r11979:a450390d0f16
r11979:a450390d0f16
r11188:143f3c64befd
r11979:a450390d0f16
r11979:a450390d0f16
r11188:143f3c64befd
r20252:76a6f1c78ce7
r6857:1e07df806ef1
r12294:7f9f5ab6fe85
r15608:7b580ec7448a
r15608:7b580ec7448a
r6857:1e07df806ef1
r6857:1e07df806ef1
r11301:9a3a26bbf491
r12001:671ce3a543a3
r12001:671ce3a543a3
r12001:671ce3a543a3
r12001:671ce3a543a3
r12001:671ce3a543a3
r12001:671ce3a543a3
r12001:671ce3a543a3
r12001:671ce3a543a3
r11301:9a3a26bbf491
r12001:671ce3a543a3
r12001:671ce3a543a3
r12001:671ce3a543a3
r12001:671ce3a543a3
r12001:671ce3a543a3
r12001:671ce3a543a3
r11301:9a3a26bbf491
r12001:671ce3a543a3
r6857:1e07df806ef1
r18854:41027c2278a2
r6772:59235ded8522
r6772:59235ded8522
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file articulated_vehicles.cpp Implementation of articulated vehicles. */

#include "stdafx.h"
#include "train.h"
#include "roadveh.h"
#include "vehicle_func.h"
#include "engine_func.h"
#include "company_func.h"
#include "newgrf.h"

#include "table/strings.h"

#include "safeguards.h"

static const uint MAX_ARTICULATED_PARTS = 100; ///< Maximum of articulated parts per vehicle, i.e. when to abort calling the articulated vehicle callback.

/**
 * Determines the next articulated part to attach
 * @param index Position in chain
 * @param front_type Front engine type
 * @param front Front engine
 * @param mirrored Returns whether the part shall be flipped.
 * @return engine to add or INVALID_ENGINE
 */
static EngineID GetNextArticulatedPart(uint index, EngineID front_type, Vehicle *front = NULL, bool *mirrored = NULL)
{
	assert(front == NULL || front->engine_type == front_type);

	const Engine *front_engine = Engine::Get(front_type);

	uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, index, 0, front_type, front);
	if (callback == CALLBACK_FAILED) return INVALID_ENGINE;

	if (front_engine->GetGRF()->grf_version < 8) {
		/* 8 bits, bit 7 for mirroring */
		callback = GB(callback, 0, 8);
		if (callback == 0xFF) return INVALID_ENGINE;
		if (mirrored != NULL) *mirrored = HasBit(callback, 7);
		callback = GB(callback, 0, 7);
	} else {
		/* 15 bits, bit 14 for mirroring */
		if (callback == 0x7FFF) return INVALID_ENGINE;
		if (mirrored != NULL) *mirrored = HasBit(callback, 14);
		callback = GB(callback, 0, 14);
	}

	return GetNewEngineID(front_engine->GetGRF(), front_engine->type, callback);
}

/**
 * Does a NewGRF report that this should be an articulated vehicle?
 * @param engine_type The engine to check.
 * @return True iff the articulated engine callback flag is set.
 */
bool IsArticulatedEngine(EngineID engine_type)
{
	return HasBit(EngInfo(engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE);
}

/**
 * Count the number of articulated parts of an engine.
 * @param engine_type The engine to get the number of parts of.
 * @param purchase_window Whether we are in the scope of the purchase window or not, i.e. whether we cannot allocate vehicles.
 * @return The number of parts.
 */
uint CountArticulatedParts(EngineID engine_type, bool purchase_window)
{
	if (!HasBit(EngInfo(engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return 0;

	/* If we can't allocate a vehicle now, we can't allocate it in the command
	 * either, so it doesn't matter how many articulated parts there are. */
	if (!Vehicle::CanAllocateItem()) return 0;

	Vehicle *v = NULL;
	if (!purchase_window) {
		v = new Vehicle();
		v->engine_type = engine_type;
		v->owner = _current_company;
	}

	uint i;
	for (i = 1; i < MAX_ARTICULATED_PARTS; i++) {
		if (GetNextArticulatedPart(i, engine_type, v) == INVALID_ENGINE) break;
	}

	delete v;

	return i - 1;
}


/**
 * Returns the default (non-refitted) capacity of a specific EngineID.
 * @param engine the EngineID of interest
 * @param cargo_type returns the default cargo type, if needed
 * @return capacity
 */
static inline uint16 GetVehicleDefaultCapacity(EngineID engine, CargoID *cargo_type)
{
	const Engine *e = Engine::Get(engine);
	CargoID cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
	if (cargo_type != NULL) *cargo_type = cargo;
	if (cargo == CT_INVALID) return 0;
	return e->GetDisplayDefaultCapacity();
}

/**
 * Returns all cargoes a vehicle can carry.
 * @param engine the EngineID of interest
 * @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask
 * @return bit set of CargoIDs
 */
static inline uint32 GetAvailableVehicleCargoTypes(EngineID engine, bool include_initial_cargo_type)
{
	const Engine *e = Engine::Get(engine);
	if (!e->CanCarryCargo()) return 0;

	uint32 cargoes = e->info.refit_mask;

	if (include_initial_cargo_type) {
		SetBit(cargoes, e->GetDefaultCargoType());
	}

	return cargoes;
}

/**
 * Get the capacity of the parts of a given engine.
 * @param engine The engine to get the capacities from.
 * @return The cargo capacities.
 */
CargoArray GetCapacityOfArticulatedParts(EngineID engine)
{
	CargoArray capacity;
	const Engine *e = Engine::Get(engine);

	CargoID cargo_type;
	uint16 cargo_capacity = GetVehicleDefaultCapacity(engine, &cargo_type);
	if (cargo_type < NUM_CARGO) capacity[cargo_type] = cargo_capacity;

	if (!e->IsGroundVehicle()) return capacity;

	if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return capacity;

	for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
		EngineID artic_engine = GetNextArticulatedPart(i, engine);
		if (artic_engine == INVALID_ENGINE) break;

		cargo_capacity = GetVehicleDefaultCapacity(artic_engine, &cargo_type);
		if (cargo_type < NUM_CARGO) capacity[cargo_type] += cargo_capacity;
	}

	return capacity;
}

/**
 * Get the default cargoes and refits of an articulated vehicle.
 * The refits are linked to a cargo rather than an articulated part to prevent a long list of parts.
 * @param engine Model to investigate.
 * @param[out] cargoes Total amount of units that can be transported, summed by cargo.
 * @param[out] refits Whether a (possibly partial) refit for each cargo is possible.
 */
void GetArticulatedVehicleCargoesAndRefits(EngineID engine, CargoArray *cargoes, uint32 *refits)
{
	cargoes->Clear();
	*refits = 0;

	const Engine *e = Engine::Get(engine);

	CargoID cargo_type;
	uint16 cargo_capacity = GetVehicleDefaultCapacity(engine, &cargo_type);
	if (cargo_type < NUM_CARGO && cargo_capacity > 0) {
		(*cargoes)[cargo_type] += cargo_capacity;
		if (IsEngineRefittable(engine)) SetBit(*refits, cargo_type);
	}

	if (!e->IsGroundVehicle() || !HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;

	for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
		EngineID artic_engine = GetNextArticulatedPart(i, engine);
		if (artic_engine == INVALID_ENGINE) break;

		cargo_capacity = GetVehicleDefaultCapacity(artic_engine, &cargo_type);
		if (cargo_type < NUM_CARGO && cargo_capacity > 0) {
			(*cargoes)[cargo_type] += cargo_capacity;
			if (IsEngineRefittable(artic_engine)) SetBit(*refits, cargo_type);
		}
	}
}

/**
 * Checks whether any of the articulated parts is refittable
 * @param engine the first part
 * @return true if refittable
 */
bool IsArticulatedVehicleRefittable(EngineID engine)
{
	if (IsEngineRefittable(engine)) return true;

	const Engine *e = Engine::Get(engine);
	if (!e->IsGroundVehicle()) return false;

	if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return false;

	for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
		EngineID artic_engine = GetNextArticulatedPart(i, engine);
		if (artic_engine == INVALID_ENGINE) break;

		if (IsEngineRefittable(artic_engine)) return true;
	}

	return false;
}

/**
 * Merges the refit_masks of all articulated parts.
 * @param engine the first part
 * @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask
 * @param union_mask returns bit mask of CargoIDs which are a refit option for at least one articulated part
 * @param intersection_mask returns bit mask of CargoIDs which are a refit option for every articulated part (with default capacity > 0)
 */
void GetArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type, uint32 *union_mask, uint32 *intersection_mask)
{
	const Engine *e = Engine::Get(engine);
	uint32 veh_cargoes = GetAvailableVehicleCargoTypes(engine, include_initial_cargo_type);
	*union_mask = veh_cargoes;
	*intersection_mask = (veh_cargoes != 0) ? veh_cargoes : UINT32_MAX;

	if (!e->IsGroundVehicle()) return;
	if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;

	for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
		EngineID artic_engine = GetNextArticulatedPart(i, engine);
		if (artic_engine == INVALID_ENGINE) break;

		veh_cargoes = GetAvailableVehicleCargoTypes(artic_engine, include_initial_cargo_type);
		*union_mask |= veh_cargoes;
		if (veh_cargoes != 0) *intersection_mask &= veh_cargoes;
	}
}

/**
 * Ors the refit_masks of all articulated parts.
 * @param engine the first part
 * @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask
 * @return bit mask of CargoIDs which are a refit option for at least one articulated part
 */
uint32 GetUnionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
{
	uint32 union_mask, intersection_mask;
	GetArticulatedRefitMasks(engine, include_initial_cargo_type, &union_mask, &intersection_mask);
	return union_mask;
}

/**
 * Ands the refit_masks of all articulated parts.
 * @param engine the first part
 * @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask
 * @return bit mask of CargoIDs which are a refit option for every articulated part (with default capacity > 0)
 */
uint32 GetIntersectionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
{
	uint32 union_mask, intersection_mask;
	GetArticulatedRefitMasks(engine, include_initial_cargo_type, &union_mask, &intersection_mask);
	return intersection_mask;
}


/**
 * Tests if all parts of an articulated vehicle are refitted to the same cargo.
 * Note: Vehicles not carrying anything are ignored
 * @param v the first vehicle in the chain
 * @param cargo_type returns the common CargoID if needed. (CT_INVALID if no part is carrying something or they are carrying different things)
 * @return true if some parts are carrying different cargoes, false if all parts are carrying the same (nothing is also the same)
 */
bool IsArticulatedVehicleCarryingDifferentCargoes(const Vehicle *v, CargoID *cargo_type)
{
	CargoID first_cargo = CT_INVALID;

	do {
		if (v->cargo_type != CT_INVALID && v->GetEngine()->CanCarryCargo()) {
			if (first_cargo == CT_INVALID) first_cargo = v->cargo_type;
			if (first_cargo != v->cargo_type) {
				if (cargo_type != NULL) *cargo_type = CT_INVALID;
				return true;
			}
		}

		v = v->HasArticulatedPart() ? v->GetNextArticulatedPart() : NULL;
	} while (v != NULL);

	if (cargo_type != NULL) *cargo_type = first_cargo;
	return false;
}

/**
 * Checks whether the specs of freshly build articulated vehicles are consistent with the information specified in the purchase list.
 * Only essential information is checked to leave room for magic tricks/workarounds to grfcoders.
 * It checks:
 *   For autoreplace/-renew:
 *    - Default cargo type (without capacity)
 *    - intersection and union of refit masks.
 */
void CheckConsistencyOfArticulatedVehicle(const Vehicle *v)
{
	const Engine *engine = v->GetEngine();

	uint32 purchase_refit_union, purchase_refit_intersection;
	GetArticulatedRefitMasks(v->engine_type, true, &purchase_refit_union, &purchase_refit_intersection);
	CargoArray purchase_default_capacity = GetCapacityOfArticulatedParts(v->engine_type);

	uint32 real_refit_union = 0;
	uint32 real_refit_intersection = UINT_MAX;
	CargoArray real_default_capacity;

	do {
		uint32 refit_mask = GetAvailableVehicleCargoTypes(v->engine_type, true);
		real_refit_union |= refit_mask;
		if (refit_mask != 0) real_refit_intersection &= refit_mask;

		assert(v->cargo_type < NUM_CARGO);
		real_default_capacity[v->cargo_type] += v->cargo_cap;

		v = v->HasArticulatedPart() ? v->GetNextArticulatedPart() : NULL;
	} while (v != NULL);

	/* Check whether the vehicle carries more cargoes than expected */
	bool carries_more = false;
	for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
		if (real_default_capacity[cid] != 0 && purchase_default_capacity[cid] == 0) {
			carries_more = true;
			break;
		}
	}

	/* show a warning once for each GRF after each game load */
	if (real_refit_union != purchase_refit_union || real_refit_intersection != purchase_refit_intersection || carries_more) {
		ShowNewGrfVehicleError(engine->index, STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ARTICULATED_CARGO, GBUG_VEH_REFIT, false);
	}
}

/**
 * Add the remaining articulated parts to the given vehicle.
 * @param first The head of the articulated bit.
 */
void AddArticulatedParts(Vehicle *first)
{
	VehicleType type = first->type;
	if (!HasBit(EngInfo(first->engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;

	Vehicle *v = first;
	for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
		bool flip_image;
		EngineID engine_type = GetNextArticulatedPart(i, first->engine_type, first, &flip_image);
		if (engine_type == INVALID_ENGINE) return;

		/* In the (very rare) case the GRF reported wrong number of articulated parts
		 * and we run out of available vehicles, bail out. */
		if (!Vehicle::CanAllocateItem()) return;

		GroundVehicleCache *gcache = v->GetGroundVehicleCache();
		gcache->first_engine = v->engine_type; // Needs to be set before first callback

		const Engine *e_artic = Engine::Get(engine_type);
		switch (type) {
			default: NOT_REACHED();

			case VEH_TRAIN: {
				Train *front = Train::From(first);
				Train *t = new Train();
				v->SetNext(t);
				v = t;

				t->subtype = 0;
				t->track = front->track;
				t->railtype = front->railtype;

				t->spritenum = e_artic->u.rail.image_index;
				if (e_artic->CanCarryCargo()) {
					t->cargo_type = e_artic->GetDefaultCargoType();
					t->cargo_cap = e_artic->u.rail.capacity;  // Callback 36 is called when the consist is finished
				} else {
					t->cargo_type = front->cargo_type; // Needed for livery selection
					t->cargo_cap = 0;
				}
				t->refit_cap = 0;

				t->SetArticulatedPart();
				break;
			}

			case VEH_ROAD: {
				RoadVehicle *front = RoadVehicle::From(first);
				RoadVehicle *rv = new RoadVehicle();
				v->SetNext(rv);
				v = rv;

				rv->subtype = 0;
				gcache->cached_veh_length = VEHICLE_LENGTH; // Callback is called when the consist is finished
				rv->state = RVSB_IN_DEPOT;

				rv->roadtype = front->roadtype;
				rv->compatible_roadtypes = front->compatible_roadtypes;

				rv->spritenum = e_artic->u.road.image_index;
				if (e_artic->CanCarryCargo()) {
					rv->cargo_type = e_artic->GetDefaultCargoType();
					rv->cargo_cap = e_artic->u.road.capacity;  // Callback 36 is called when the consist is finished
				} else {
					rv->cargo_type = front->cargo_type; // Needed for livery selection
					rv->cargo_cap = 0;
				}
				rv->refit_cap = 0;

				rv->SetArticulatedPart();
				break;
			}
		}

		/* get common values from first engine */
		v->direction = first->direction;
		v->owner = first->owner;
		v->tile = first->tile;
		v->x_pos = first->x_pos;
		v->y_pos = first->y_pos;
		v->z_pos = first->z_pos;
		v->build_year = first->build_year;
		v->vehstatus = first->vehstatus & ~VS_STOPPED;

		v->cargo_subtype = 0;
		v->max_age = 0;
		v->engine_type = engine_type;
		v->value = 0;
		v->cur_image = SPR_IMG_QUERY;
		v->random_bits = VehicleRandomBits();

		if (flip_image) v->spritenum++;

		VehicleUpdatePosition(v);
	}
}