1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
8 /** @file articulated_vehicles.cpp Implementation of articulated vehicles. */
9 
10 #include "stdafx.h"
11 #include "train.h"
12 #include "roadveh.h"
13 #include "vehicle_func.h"
14 #include "engine_func.h"
15 #include "company_func.h"
16 #include "newgrf.h"
17 
18 #include "table/strings.h"
19 
20 #include "safeguards.h"
21 
22 static const uint MAX_ARTICULATED_PARTS = 100; ///< Maximum of articulated parts per vehicle, i.e. when to abort calling the articulated vehicle callback.
23 
24 /**
25  * Determines the next articulated part to attach
26  * @param index Position in chain
27  * @param front_type Front engine type
28  * @param front Front engine
29  * @param mirrored Returns whether the part shall be flipped.
30  * @return engine to add or INVALID_ENGINE
31  */
GetNextArticulatedPart(uint index,EngineID front_type,Vehicle * front=nullptr,bool * mirrored=nullptr)32 static EngineID GetNextArticulatedPart(uint index, EngineID front_type, Vehicle *front = nullptr, bool *mirrored = nullptr)
33 {
34 	assert(front == nullptr || front->engine_type == front_type);
35 
36 	const Engine *front_engine = Engine::Get(front_type);
37 
38 	uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, index, 0, front_type, front);
39 	if (callback == CALLBACK_FAILED) return INVALID_ENGINE;
40 
41 	if (front_engine->GetGRF()->grf_version < 8) {
42 		/* 8 bits, bit 7 for mirroring */
43 		callback = GB(callback, 0, 8);
44 		if (callback == 0xFF) return INVALID_ENGINE;
45 		if (mirrored != nullptr) *mirrored = HasBit(callback, 7);
46 		callback = GB(callback, 0, 7);
47 	} else {
48 		/* 15 bits, bit 14 for mirroring */
49 		if (callback == 0x7FFF) return INVALID_ENGINE;
50 		if (mirrored != nullptr) *mirrored = HasBit(callback, 14);
51 		callback = GB(callback, 0, 14);
52 	}
53 
54 	return GetNewEngineID(front_engine->GetGRF(), front_engine->type, callback);
55 }
56 
57 /**
58  * Does a NewGRF report that this should be an articulated vehicle?
59  * @param engine_type The engine to check.
60  * @return True iff the articulated engine callback flag is set.
61  */
IsArticulatedEngine(EngineID engine_type)62 bool IsArticulatedEngine(EngineID engine_type)
63 {
64 	return HasBit(EngInfo(engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE);
65 }
66 
67 /**
68  * Count the number of articulated parts of an engine.
69  * @param engine_type The engine to get the number of parts of.
70  * @param purchase_window Whether we are in the scope of the purchase window or not, i.e. whether we cannot allocate vehicles.
71  * @return The number of parts.
72  */
CountArticulatedParts(EngineID engine_type,bool purchase_window)73 uint CountArticulatedParts(EngineID engine_type, bool purchase_window)
74 {
75 	if (!HasBit(EngInfo(engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return 0;
76 
77 	/* If we can't allocate a vehicle now, we can't allocate it in the command
78 	 * either, so it doesn't matter how many articulated parts there are. */
79 	if (!Vehicle::CanAllocateItem()) return 0;
80 
81 	Vehicle *v = nullptr;
82 	if (!purchase_window) {
83 		v = new Vehicle();
84 		v->engine_type = engine_type;
85 		v->owner = _current_company;
86 	}
87 
88 	uint i;
89 	for (i = 1; i < MAX_ARTICULATED_PARTS; i++) {
90 		if (GetNextArticulatedPart(i, engine_type, v) == INVALID_ENGINE) break;
91 	}
92 
93 	delete v;
94 
95 	return i - 1;
96 }
97 
98 
99 /**
100  * Returns the default (non-refitted) capacity of a specific EngineID.
101  * @param engine the EngineID of interest
102  * @param cargo_type returns the default cargo type, if needed
103  * @return capacity
104  */
GetVehicleDefaultCapacity(EngineID engine,CargoID * cargo_type)105 static inline uint16 GetVehicleDefaultCapacity(EngineID engine, CargoID *cargo_type)
106 {
107 	const Engine *e = Engine::Get(engine);
108 	CargoID cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
109 	if (cargo_type != nullptr) *cargo_type = cargo;
110 	if (cargo == CT_INVALID) return 0;
111 	return e->GetDisplayDefaultCapacity();
112 }
113 
114 /**
115  * Returns all cargoes a vehicle can carry.
116  * @param engine the EngineID of interest
117  * @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask
118  * @return bit set of CargoIDs
119  */
GetAvailableVehicleCargoTypes(EngineID engine,bool include_initial_cargo_type)120 static inline CargoTypes GetAvailableVehicleCargoTypes(EngineID engine, bool include_initial_cargo_type)
121 {
122 	const Engine *e = Engine::Get(engine);
123 	if (!e->CanCarryCargo()) return 0;
124 
125 	CargoTypes cargoes = e->info.refit_mask;
126 
127 	if (include_initial_cargo_type) {
128 		SetBit(cargoes, e->GetDefaultCargoType());
129 	}
130 
131 	return cargoes;
132 }
133 
134 /**
135  * Get the capacity of the parts of a given engine.
136  * @param engine The engine to get the capacities from.
137  * @return The cargo capacities.
138  */
GetCapacityOfArticulatedParts(EngineID engine)139 CargoArray GetCapacityOfArticulatedParts(EngineID engine)
140 {
141 	CargoArray capacity;
142 	const Engine *e = Engine::Get(engine);
143 
144 	CargoID cargo_type;
145 	uint16 cargo_capacity = GetVehicleDefaultCapacity(engine, &cargo_type);
146 	if (cargo_type < NUM_CARGO) capacity[cargo_type] = cargo_capacity;
147 
148 	if (!e->IsGroundVehicle()) return capacity;
149 
150 	if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return capacity;
151 
152 	for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
153 		EngineID artic_engine = GetNextArticulatedPart(i, engine);
154 		if (artic_engine == INVALID_ENGINE) break;
155 
156 		cargo_capacity = GetVehicleDefaultCapacity(artic_engine, &cargo_type);
157 		if (cargo_type < NUM_CARGO) capacity[cargo_type] += cargo_capacity;
158 	}
159 
160 	return capacity;
161 }
162 
163 /**
164  * Get the default cargoes and refits of an articulated vehicle.
165  * The refits are linked to a cargo rather than an articulated part to prevent a long list of parts.
166  * @param engine Model to investigate.
167  * @param[out] cargoes Total amount of units that can be transported, summed by cargo.
168  * @param[out] refits Whether a (possibly partial) refit for each cargo is possible.
169  * @param cargo_type Selected refitted cargo type
170  * @param cargo_capacity Capacity of selected refitted cargo type
171  */
GetArticulatedVehicleCargoesAndRefits(EngineID engine,CargoArray * cargoes,CargoTypes * refits,CargoID cargo_type,uint cargo_capacity)172 void GetArticulatedVehicleCargoesAndRefits(EngineID engine, CargoArray *cargoes, CargoTypes *refits, CargoID cargo_type, uint cargo_capacity)
173 {
174 	cargoes->Clear();
175 	*refits = 0;
176 
177 	const Engine *e = Engine::Get(engine);
178 
179 	if (cargo_type < NUM_CARGO && cargo_capacity > 0) {
180 		(*cargoes)[cargo_type] += cargo_capacity;
181 		if (IsEngineRefittable(engine)) SetBit(*refits, cargo_type);
182 	}
183 
184 	if (!e->IsGroundVehicle() || !HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;
185 
186 	for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
187 		EngineID artic_engine = GetNextArticulatedPart(i, engine);
188 		if (artic_engine == INVALID_ENGINE) break;
189 
190 		cargo_capacity = GetVehicleDefaultCapacity(artic_engine, &cargo_type);
191 		if (cargo_type < NUM_CARGO && cargo_capacity > 0) {
192 			(*cargoes)[cargo_type] += cargo_capacity;
193 			if (IsEngineRefittable(artic_engine)) SetBit(*refits, cargo_type);
194 		}
195 	}
196 }
197 
198 /**
199  * Checks whether any of the articulated parts is refittable
200  * @param engine the first part
201  * @return true if refittable
202  */
IsArticulatedVehicleRefittable(EngineID engine)203 bool IsArticulatedVehicleRefittable(EngineID engine)
204 {
205 	if (IsEngineRefittable(engine)) return true;
206 
207 	const Engine *e = Engine::Get(engine);
208 	if (!e->IsGroundVehicle()) return false;
209 
210 	if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return false;
211 
212 	for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
213 		EngineID artic_engine = GetNextArticulatedPart(i, engine);
214 		if (artic_engine == INVALID_ENGINE) break;
215 
216 		if (IsEngineRefittable(artic_engine)) return true;
217 	}
218 
219 	return false;
220 }
221 
222 /**
223  * Merges the refit_masks of all articulated parts.
224  * @param engine the first part
225  * @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask
226  * @param union_mask returns bit mask of CargoIDs which are a refit option for at least one articulated part
227  * @param intersection_mask returns bit mask of CargoIDs which are a refit option for every articulated part (with default capacity > 0)
228  */
GetArticulatedRefitMasks(EngineID engine,bool include_initial_cargo_type,CargoTypes * union_mask,CargoTypes * intersection_mask)229 void GetArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type, CargoTypes *union_mask, CargoTypes *intersection_mask)
230 {
231 	const Engine *e = Engine::Get(engine);
232 	CargoTypes veh_cargoes = GetAvailableVehicleCargoTypes(engine, include_initial_cargo_type);
233 	*union_mask = veh_cargoes;
234 	*intersection_mask = (veh_cargoes != 0) ? veh_cargoes : ALL_CARGOTYPES;
235 
236 	if (!e->IsGroundVehicle()) return;
237 	if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;
238 
239 	for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
240 		EngineID artic_engine = GetNextArticulatedPart(i, engine);
241 		if (artic_engine == INVALID_ENGINE) break;
242 
243 		veh_cargoes = GetAvailableVehicleCargoTypes(artic_engine, include_initial_cargo_type);
244 		*union_mask |= veh_cargoes;
245 		if (veh_cargoes != 0) *intersection_mask &= veh_cargoes;
246 	}
247 }
248 
249 /**
250  * Ors the refit_masks of all articulated parts.
251  * @param engine the first part
252  * @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask
253  * @return bit mask of CargoIDs which are a refit option for at least one articulated part
254  */
GetUnionOfArticulatedRefitMasks(EngineID engine,bool include_initial_cargo_type)255 CargoTypes GetUnionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
256 {
257 	CargoTypes union_mask, intersection_mask;
258 	GetArticulatedRefitMasks(engine, include_initial_cargo_type, &union_mask, &intersection_mask);
259 	return union_mask;
260 }
261 
262 /**
263  * Ands the refit_masks of all articulated parts.
264  * @param engine the first part
265  * @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask
266  * @return bit mask of CargoIDs which are a refit option for every articulated part (with default capacity > 0)
267  */
GetIntersectionOfArticulatedRefitMasks(EngineID engine,bool include_initial_cargo_type)268 CargoTypes GetIntersectionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
269 {
270 	CargoTypes union_mask, intersection_mask;
271 	GetArticulatedRefitMasks(engine, include_initial_cargo_type, &union_mask, &intersection_mask);
272 	return intersection_mask;
273 }
274 
275 
276 /**
277  * Tests if all parts of an articulated vehicle are refitted to the same cargo.
278  * Note: Vehicles not carrying anything are ignored
279  * @param v the first vehicle in the chain
280  * @param cargo_type returns the common CargoID if needed. (CT_INVALID if no part is carrying something or they are carrying different things)
281  * @return true if some parts are carrying different cargoes, false if all parts are carrying the same (nothing is also the same)
282  */
IsArticulatedVehicleCarryingDifferentCargoes(const Vehicle * v,CargoID * cargo_type)283 bool IsArticulatedVehicleCarryingDifferentCargoes(const Vehicle *v, CargoID *cargo_type)
284 {
285 	CargoID first_cargo = CT_INVALID;
286 
287 	do {
288 		if (v->cargo_type != CT_INVALID && v->GetEngine()->CanCarryCargo()) {
289 			if (first_cargo == CT_INVALID) first_cargo = v->cargo_type;
290 			if (first_cargo != v->cargo_type) {
291 				if (cargo_type != nullptr) *cargo_type = CT_INVALID;
292 				return true;
293 			}
294 		}
295 
296 		v = v->HasArticulatedPart() ? v->GetNextArticulatedPart() : nullptr;
297 	} while (v != nullptr);
298 
299 	if (cargo_type != nullptr) *cargo_type = first_cargo;
300 	return false;
301 }
302 
303 /**
304  * Checks whether the specs of freshly build articulated vehicles are consistent with the information specified in the purchase list.
305  * Only essential information is checked to leave room for magic tricks/workarounds to grfcoders.
306  * It checks:
307  *   For autoreplace/-renew:
308  *    - Default cargo type (without capacity)
309  *    - intersection and union of refit masks.
310  */
CheckConsistencyOfArticulatedVehicle(const Vehicle * v)311 void CheckConsistencyOfArticulatedVehicle(const Vehicle *v)
312 {
313 	const Engine *engine = v->GetEngine();
314 
315 	CargoTypes purchase_refit_union, purchase_refit_intersection;
316 	GetArticulatedRefitMasks(v->engine_type, true, &purchase_refit_union, &purchase_refit_intersection);
317 	CargoArray purchase_default_capacity = GetCapacityOfArticulatedParts(v->engine_type);
318 
319 	CargoTypes real_refit_union = 0;
320 	CargoTypes real_refit_intersection = ALL_CARGOTYPES;
321 	CargoArray real_default_capacity;
322 
323 	do {
324 		CargoTypes refit_mask = GetAvailableVehicleCargoTypes(v->engine_type, true);
325 		real_refit_union |= refit_mask;
326 		if (refit_mask != 0) real_refit_intersection &= refit_mask;
327 
328 		assert(v->cargo_type < NUM_CARGO);
329 		real_default_capacity[v->cargo_type] += v->cargo_cap;
330 
331 		v = v->HasArticulatedPart() ? v->GetNextArticulatedPart() : nullptr;
332 	} while (v != nullptr);
333 
334 	/* Check whether the vehicle carries more cargoes than expected */
335 	bool carries_more = false;
336 	for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
337 		if (real_default_capacity[cid] != 0 && purchase_default_capacity[cid] == 0) {
338 			carries_more = true;
339 			break;
340 		}
341 	}
342 
343 	/* show a warning once for each GRF after each game load */
344 	if (real_refit_union != purchase_refit_union || real_refit_intersection != purchase_refit_intersection || carries_more) {
345 		ShowNewGrfVehicleError(engine->index, STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ARTICULATED_CARGO, GBUG_VEH_REFIT, false);
346 	}
347 }
348 
349 /**
350  * Add the remaining articulated parts to the given vehicle.
351  * @param first The head of the articulated bit.
352  */
AddArticulatedParts(Vehicle * first)353 void AddArticulatedParts(Vehicle *first)
354 {
355 	VehicleType type = first->type;
356 	if (!HasBit(EngInfo(first->engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;
357 
358 	Vehicle *v = first;
359 	for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
360 		bool flip_image;
361 		EngineID engine_type = GetNextArticulatedPart(i, first->engine_type, first, &flip_image);
362 		if (engine_type == INVALID_ENGINE) return;
363 
364 		/* In the (very rare) case the GRF reported wrong number of articulated parts
365 		 * and we run out of available vehicles, bail out. */
366 		if (!Vehicle::CanAllocateItem()) return;
367 
368 		GroundVehicleCache *gcache = v->GetGroundVehicleCache();
369 		gcache->first_engine = v->engine_type; // Needs to be set before first callback
370 
371 		const Engine *e_artic = Engine::Get(engine_type);
372 		switch (type) {
373 			default: NOT_REACHED();
374 
375 			case VEH_TRAIN: {
376 				Train *front = Train::From(first);
377 				Train *t = new Train();
378 				v->SetNext(t);
379 				v = t;
380 
381 				t->subtype = 0;
382 				t->track = front->track;
383 				t->railtype = front->railtype;
384 
385 				t->spritenum = e_artic->u.rail.image_index;
386 				if (e_artic->CanCarryCargo()) {
387 					t->cargo_type = e_artic->GetDefaultCargoType();
388 					t->cargo_cap = e_artic->u.rail.capacity;  // Callback 36 is called when the consist is finished
389 				} else {
390 					t->cargo_type = front->cargo_type; // Needed for livery selection
391 					t->cargo_cap = 0;
392 				}
393 				t->refit_cap = 0;
394 
395 				t->SetArticulatedPart();
396 				break;
397 			}
398 
399 			case VEH_ROAD: {
400 				RoadVehicle *front = RoadVehicle::From(first);
401 				RoadVehicle *rv = new RoadVehicle();
402 				v->SetNext(rv);
403 				v = rv;
404 
405 				rv->subtype = 0;
406 				gcache->cached_veh_length = VEHICLE_LENGTH; // Callback is called when the consist is finished
407 				rv->state = RVSB_IN_DEPOT;
408 
409 				rv->roadtype = front->roadtype;
410 				rv->compatible_roadtypes = front->compatible_roadtypes;
411 
412 				rv->spritenum = e_artic->u.road.image_index;
413 				if (e_artic->CanCarryCargo()) {
414 					rv->cargo_type = e_artic->GetDefaultCargoType();
415 					rv->cargo_cap = e_artic->u.road.capacity;  // Callback 36 is called when the consist is finished
416 				} else {
417 					rv->cargo_type = front->cargo_type; // Needed for livery selection
418 					rv->cargo_cap = 0;
419 				}
420 				rv->refit_cap = 0;
421 
422 				rv->SetArticulatedPart();
423 				break;
424 			}
425 		}
426 
427 		/* get common values from first engine */
428 		v->direction = first->direction;
429 		v->owner = first->owner;
430 		v->tile = first->tile;
431 		v->x_pos = first->x_pos;
432 		v->y_pos = first->y_pos;
433 		v->z_pos = first->z_pos;
434 		v->date_of_last_service = first->date_of_last_service;
435 		v->build_year = first->build_year;
436 		v->vehstatus = first->vehstatus & ~VS_STOPPED;
437 
438 		v->cargo_subtype = 0;
439 		v->max_age = 0;
440 		v->engine_type = engine_type;
441 		v->value = 0;
442 		v->sprite_cache.sprite_seq.Set(SPR_IMG_QUERY);
443 		v->random_bits = VehicleRandomBits();
444 
445 		if (flip_image) v->spritenum++;
446 
447 		v->UpdatePosition();
448 	}
449 }
450