Changeset - r13388:29eb3d8bb664
[Not reviewed]
master
0 1 0
frosch - 15 years ago 2009-10-30 21:51:36
frosch@openttd.org
(svn r17907) -Codechange: Deduplicate result interpretation of articulated vehicle callback.
1 file changed with 31 insertions and 20 deletions:
0 comments (0 inline, 0 general)
src/articulated_vehicles.cpp
Show inline comments
 
@@ -17,12 +17,31 @@
 

	
 
#include "table/strings.h"
 
#include "table/sprites.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 GetNextArticPart(uint index, EngineID front_type, Vehicle *front = NULL, bool *mirrored = false)
 
{
 
	assert(front == NULL || front->engine_type == front_type);
 

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

	
 
	if (mirrored != NULL) *mirrored = HasBit(callback, 7);
 
	return GetNewEngineID(GetEngineGRF(front_type), Engine::Get(front_type)->type, GB(callback, 0, 7));
 
}
 

	
 
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. */
 
@@ -33,14 +52,13 @@ uint CountArticulatedParts(EngineID engi
 
		v = new Vehicle();
 
		v->engine_type = engine_type;
 
	}
 

	
 
	uint i;
 
	for (i = 1; i < MAX_ARTICULATED_PARTS; i++) {
 
		uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, engine_type, v);
 
		if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) break;
 
		if (GetNextArticPart(i, engine_type, v) == INVALID_ENGINE) break;
 
	}
 

	
 
	delete v;
 

	
 
	return i - 1;
 
}
 
@@ -92,16 +110,14 @@ CargoArray GetCapacityOfArticulatedParts
 

	
 
	if (e->type != VEH_TRAIN && e->type != VEH_ROAD) return capacity;
 

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

	
 
	for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
 
		uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, engine, NULL);
 
		if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) break;
 

	
 
		EngineID artic_engine = GetNewEngineID(GetEngineGRF(engine), e->type, GB(callback, 0, 7));
 
		EngineID artic_engine = GetNextArticPart(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;
 
@@ -119,16 +135,15 @@ bool IsArticulatedVehicleRefittable(Engi
 
	const Engine *e = Engine::Get(engine);
 
	if (e->type != VEH_TRAIN && e->type != VEH_ROAD) return false;
 

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

	
 
	for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
 
		uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, engine, NULL);
 
		if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) break;
 
		EngineID artic_engine = GetNextArticPart(i, engine);
 
		if (artic_engine == INVALID_ENGINE) break;
 

	
 
		EngineID artic_engine = GetNewEngineID(GetEngineGRF(engine), e->type, GB(callback, 0, 7));
 
		if (IsEngineRefittable(artic_engine)) return true;
 
	}
 

	
 
	return false;
 
}
 

	
 
@@ -145,16 +160,15 @@ uint32 GetUnionOfArticulatedRefitMasks(E
 

	
 
	if (e->type != VEH_TRAIN && e->type != VEH_ROAD) return cargos;
 

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

	
 
	for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
 
		uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, engine, NULL);
 
		if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) break;
 
		EngineID artic_engine = GetNextArticPart(i, engine);
 
		if (artic_engine == INVALID_ENGINE) break;
 

	
 
		EngineID artic_engine = GetNewEngineID(GetEngineGRF(engine), e->type, GB(callback, 0, 7));
 
		cargos |= GetAvailableVehicleCargoTypes(artic_engine, include_initial_cargo_type);
 
	}
 

	
 
	return cargos;
 
}
 

	
 
@@ -174,16 +188,15 @@ uint32 GetIntersectionOfArticulatedRefit
 

	
 
	if (e->type != VEH_TRAIN && e->type != VEH_ROAD) return cargos;
 

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

	
 
	for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
 
		uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, engine, NULL);
 
		if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) break;
 
		EngineID artic_engine = GetNextArticPart(i, engine);
 
		if (artic_engine == INVALID_ENGINE) break;
 

	
 
		EngineID artic_engine = GetNewEngineID(GetEngineGRF(engine), e->type, GB(callback, 0, 7));
 
		veh_cargos = GetAvailableVehicleCargoTypes(artic_engine, include_initial_cargo_type);
 
		if (veh_cargos != 0) cargos &= veh_cargos;
 
	}
 

	
 
	return cargos;
 
}
 
@@ -290,22 +303,20 @@ 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++) {
 
		uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, first->engine_type, first);
 
		if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) return;
 
		bool flip_image;
 
		EngineID engine_type = GetNextArticPart(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;
 

	
 
		EngineID engine_type = GetNewEngineID(GetEngineGRF(first->engine_type), type, GB(callback, 0, 7));
 
		bool flip_image = HasBit(callback, 7);
 

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

	
 
			case VEH_TRAIN: {
 
				Train *front = Train::From(first);
0 comments (0 inline, 0 general)