Changeset - r6611:8efb6702b97b
[Not reviewed]
master
0 3 0
rubidium - 17 years ago 2007-05-13 21:24:58
rubidium@openttd.org
(svn r9831) -Codechange: more refactoring of the loading/unloading.
3 files changed with 42 insertions and 99 deletions:
0 comments (0 inline, 0 general)
src/economy.cpp
Show inline comments
 
@@ -1375,79 +1375,12 @@ static bool LoadWait(const Vehicle* v, c
 
		}
 
	}
 

	
 
	return false;
 
}
 

	
 
static bool CanFillVehicle_FullLoadAny(Vehicle *v)
 
{
 
	uint32 full = 0, not_full = 0;
 
	const GoodsEntry *ge = GetStation(v->last_station_visited)->goods;
 

	
 
	/* special handling of aircraft */
 

	
 
	/* if the aircraft carries passengers and is NOT full, then
 
	 *continue loading, no matter how much mail is in */
 
	if (v->type == VEH_AIRCRAFT &&
 
			IsCargoInClass(v->cargo_type, CC_PASSENGERS) &&
 
			v->cargo_cap != v->cargo_count) {
 
		return true;
 
	}
 

	
 
	/* patch should return "true" to continue loading, i.e. when there is no cargo type that is fully loaded. */
 
	do {
 
		/* Should never happen, but just in case future additions change this */
 
		assert(v->cargo_type<32);
 

	
 
		if (v->cargo_cap != 0) {
 
			uint32 mask = 1 << v->cargo_type;
 

	
 
			if (!HASBIT(v->vehicle_flags, VF_CARGO_UNLOADING) && v->cargo_cap == v->cargo_count) {
 
				full |= mask;
 
			} else if (GB(ge[v->cargo_type].waiting_acceptance, 0, 12) > 0 ||
 
					(HASBIT(v->vehicle_flags, VF_CARGO_UNLOADING) && (ge[v->cargo_type].waiting_acceptance & 0x8000))) {
 
				/* If there is any cargo waiting, or this vehicle is still unloading
 
				 * and the station accepts the cargo, don't leave the station. */
 
				return true;
 
			} else {
 
				not_full |= mask;
 
			}
 
		}
 
	} while ((v = v->next) != NULL);
 

	
 
	/* continue loading if there is a non full cargo type and no cargo type that is full */
 
	return not_full && (full & ~not_full) == 0;
 
}
 

	
 

	
 
static bool CanFillVehicle(Vehicle *front_v)
 
{
 
	TileIndex tile = front_v->tile;
 

	
 
	assert(IsTileType(tile, MP_STATION) ||
 
			(front_v->type == VEH_SHIP && (
 
				IsTileType(TILE_ADDXY(tile,  1,  0), MP_STATION) ||
 
				IsTileType(TILE_ADDXY(tile, -1,  0), MP_STATION) ||
 
				IsTileType(TILE_ADDXY(tile,  0,  1), MP_STATION) ||
 
				IsTileType(TILE_ADDXY(tile,  0, -1), MP_STATION) ||
 
				IsTileType(TILE_ADDXY(tile, -2,  0), MP_STATION)
 
			)));
 

	
 
	bool full_load = HASBIT(front_v->current_order.flags, OFB_FULL_LOAD);
 

	
 
	/* If patch is active, use alternative CanFillVehicle-function */
 
	if (_patches.full_load_any && full_load) return CanFillVehicle_FullLoadAny(front_v);
 

	
 
	Vehicle *v = front_v;
 
	do {
 
		if (HASBIT(v->vehicle_flags, VF_CARGO_UNLOADING) || (full_load && v->cargo_count != v->cargo_cap)) return true;
 
	} while ((v = v->next) != NULL);
 

	
 
	return !HASBIT(front_v->vehicle_flags, VF_LOADING_FINISHED);
 
}
 

	
 
/**
 
 * Performs the vehicle payment _and_ marks the vehicle to be unloaded.
 
 * @param front_v the vehicle to be unloaded
 
 */
 
void VehiclePayment(Vehicle *front_v)
 
{
 
@@ -1474,13 +1407,13 @@ void VehiclePayment(Vehicle *front_v)
 

	
 
	/* Start unloading in at the first possible moment */
 
	front_v->load_unload_time_rem = 1;
 

	
 
	for (Vehicle *v = front_v; v != NULL; v = v->next) {
 
		/* No cargo to unload */
 
		if (v->cargo_cap == 0) continue;
 
		if (v->cargo_cap == 0 || v->cargo_count == 0) continue;
 

	
 
		SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
 
		/* All cargo has already been paid for, no need to pay again */
 
		if (v->cargo_count == v->cargo_paid_for) continue;
 

	
 
		GoodsEntry *ge = &st->goods[v->cargo_type];
 
@@ -1545,37 +1478,31 @@ void VehiclePayment(Vehicle *front_v)
 
	_current_player = old_player;
 
}
 

	
 
/**
 
 * Loads/unload the vehicle if possible.
 
 * @param v the vehicle to be (un)loaded
 
 * @return true if something was (un)loaded. False if the train is ready to leave.
 
 */
 
bool LoadUnloadVehicle(Vehicle *v)
 
void LoadUnloadVehicle(Vehicle *v)
 
{
 
	if (!CanFillVehicle(v)) return false;
 

	
 
	int unloading_time = 20;
 
	int unloading_time = 0;
 
	Vehicle *u = v;
 
	int result = 0;
 
	uint cap;
 

	
 
	bool completely_empty = true;
 
	bool anything_loaded = false;
 
	bool completely_empty  = true;
 
	bool anything_unloaded = false;
 
	bool anything_loaded   = false;
 
	uint32 cargo_not_full  = 0;
 
	uint32 cargo_full      = 0;
 
	int total_cargo_feeder_share = 0; // the feeder cash amount for the goods being loaded/unloaded in this load step
 

	
 
	assert(v->current_order.type == OT_LOADING);
 

	
 
	v->cur_speed = 0;
 

	
 
	/* Loading can only have finished when all the cargo has been unloaded, and
 
	 * there is nothing left to load. It's easier to clear this if the
 
	 * conditions haven't been met than attempting to check them all before
 
	 * enabling though. */
 
	SETBIT(v->vehicle_flags, VF_LOADING_FINISHED);
 

	
 
	StationID last_visited = v->last_station_visited;
 
	Station *st = GetStation(last_visited);
 

	
 
	for (; v != NULL; v = v->next) {
 
		if (v->cargo_cap == 0) continue;
 

	
 
@@ -1588,14 +1515,12 @@ bool LoadUnloadVehicle(Vehicle *v)
 
		GoodsEntry *ge = &st->goods[v->cargo_type];
 
		uint count = GB(ge->waiting_acceptance, 0, 12);
 

	
 
		if (HASBIT(v->vehicle_flags, VF_CARGO_UNLOADING)) {
 
			uint16 amount_unloaded = _patches.gradual_loading ? min(v->cargo_count, load_amount) : v->cargo_count;
 

	
 
			CLRBIT(u->vehicle_flags, VF_LOADING_FINISHED);
 

	
 
			if (v->cargo_source != last_visited && ge->waiting_acceptance & 0x8000 && !(u->current_order.flags & OF_TRANSFER)) {
 
				result |= 1;
 
			} else if (u->current_order.flags & (OF_UNLOAD | OF_TRANSFER)) {
 
				if (count == 0) {
 
					/* No goods waiting at station */
 
					ge->enroute_time    = v->cargo_days;
 
@@ -1632,12 +1557,13 @@ bool LoadUnloadVehicle(Vehicle *v)
 

	
 
			unloading_time += amount_unloaded;
 

	
 
			v->cargo_count -= amount_unloaded;
 
			v->cargo_paid_for -= min(amount_unloaded, v->cargo_paid_for);
 

	
 
			anything_unloaded = true;
 
			if (_patches.gradual_loading && v->cargo_count != 0) {
 
				completely_empty = false;
 
			} else {
 
				/* We have finished unloading (cargo count == 0) */
 
				CLRBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
 
			}
 
@@ -1671,13 +1597,13 @@ bool LoadUnloadVehicle(Vehicle *v)
 

	
 
			if (v->cargo_count == 0) TriggerVehicle(v, VEHICLE_TRIGGER_NEW_CARGO);
 

	
 
			/* Skip loading this vehicle if another train/vehicle is already handling
 
			 * the same cargo type at this station */
 
			if (_patches.improved_load && (u->current_order.flags & OF_FULL_LOAD) && LoadWait(v,u)) {
 
				CLRBIT(u->vehicle_flags, VF_LOADING_FINISHED);
 
				SETBIT(cargo_not_full, v->cargo_type);
 
				continue;
 
			}
 

	
 
			/* TODO: Regarding this, when we do gradual loading, we
 
			 * should first unload all vehicles and then start
 
			 * loading them. Since this will cause
 
@@ -1687,13 +1613,12 @@ bool LoadUnloadVehicle(Vehicle *v)
 
			 * removed; that's how TTDPatch behaves too. --pasky */
 
			completely_empty = false;
 
			anything_loaded = true;
 

	
 
			if (cap > count) cap = count;
 
			if (_patches.gradual_loading) cap = min(cap, load_amount);
 
			if (cap < count) CLRBIT(u->vehicle_flags, VF_LOADING_FINISHED);
 

	
 
			/* cargoshare is proportioned by the amount due to unload
 
			 * Otherwise, with gradual loading, 100% of credits would be taken immediately,
 
			 * even if the cargo volume represents a tiny percent of the whole.
 
			 * ge->unload_pending holds the amount that has been credited, but has not yet been unloaded.
 
			 */
 
@@ -1713,31 +1638,49 @@ bool LoadUnloadVehicle(Vehicle *v)
 
			v->cargo_source = ge->enroute_from;
 
			v->cargo_source_xy = ge->enroute_from_xy;
 
			v->cargo_days = ge->enroute_time;
 
			result |= 2;
 
			st->last_vehicle_type = v->type;
 
		}
 

	
 
		if (v->cargo_count == v->cargo_cap) {
 
			SETBIT(cargo_full, v->cargo_type);
 
		} else {
 
			SETBIT(cargo_not_full, v->cargo_type);
 
		}
 
	}
 

	
 
	v = u;
 

	
 
	v->cargo_feeder_share += total_cargo_feeder_share;
 

	
 
	if (_patches.gradual_loading) {
 
		/* The time it takes to load one 'slice' of cargo or passengers depends
 
		 * on the vehicle type - the values here are those found in TTDPatch */
 
		uint gradual_loading_wait_time[] = { 40, 20, 10, 20 };
 
	if (anything_loaded || anything_unloaded) {
 
		if (_patches.gradual_loading) {
 
			/* The time it takes to load one 'slice' of cargo or passengers depends
 
			* on the vehicle type - the values here are those found in TTDPatch */
 
			const uint gradual_loading_wait_time[] = { 40, 20, 10, 20 };
 

	
 
		unloading_time = gradual_loading_wait_time[v->type];
 
		if (HASBIT(v->vehicle_flags, VF_LOADING_FINISHED)) {
 
			if (anything_loaded) {
 
				unloading_time += 20;
 
			} else {
 
				unloading_time = 20;
 
			unloading_time = gradual_loading_wait_time[v->type];
 
		}
 
	} else {
 
		bool finished_loading = true;
 
		if (HASBIT(v->current_order.flags, OFB_FULL_LOAD)) {
 
			if (_patches.full_load_any) {
 
				/* if the aircraft carries passengers and is NOT full, then
 
				 * continue loading, no matter how much mail is in */
 
				if ((v->type == VEH_AIRCRAFT && IsCargoInClass(v->cargo_type, CC_PASSENGERS) && v->cargo_cap != v->cargo_count) ||
 
						(cargo_not_full && (cargo_full & ~cargo_not_full) == 0)) { // There are stull non-full cargos
 
					finished_loading = false;
 
				}
 
			} else if (cargo_not_full != 0) {
 
				finished_loading = false;
 
			}
 
		}
 
		unloading_time = 20;
 

	
 
		SB(v->vehicle_flags, VF_LOADING_FINISHED, 1, finished_loading);
 
	}
 

	
 
	if (v->type == VEH_TRAIN) {
 
		/* Each platform tile is worth 2 rail vehicles. */
 
		int overhang = v->u.rail.cached_total_length - st->GetPlatformLength(v->tile) * TILE_SIZE;
 
		if (overhang > 0) {
 
@@ -1758,14 +1701,12 @@ bool LoadUnloadVehicle(Vehicle *v)
 

	
 
		st->MarkTilesDirty();
 
		v->MarkDirty();
 

	
 
		if (result & 2) InvalidateWindow(WC_STATION_VIEW, last_visited);
 
	}
 

	
 
	return true;
 
}
 

	
 
void PlayersMonthlyLoop()
 
{
 
	PlayersGenStatistics();
 
	if (_patches.inflation && _cur_year < MAX_YEAR)
src/vehicle.cpp
Show inline comments
 
@@ -2938,13 +2938,15 @@ void Vehicle::HandleLoading(bool mode)
 

	
 
			/* We have not waited enough time till the next round of loading/unloading */
 
			if (--this->load_unload_time_rem) return;
 

	
 
			/* Load/unload the vehicle; when it actually did something
 
			 * we do not leave the station. */
 
			if (LoadUnloadVehicle(this)) return;
 
			LoadUnloadVehicle(this);
 

	
 
			if (!HASBIT(this->vehicle_flags, VF_LOADING_FINISHED)) return;
 

	
 
			this->PlayLeaveStationSound();
 

	
 
			Order b = this->current_order;
 
			this->LeaveStation();
 

	
src/vehicle.h
Show inline comments
 
@@ -518,13 +518,13 @@ void BeginVehicleMove(Vehicle *v);
 
void EndVehicleMove(Vehicle *v);
 

	
 
void ShowAircraftViewWindow(const Vehicle* v);
 

	
 
UnitID GetFreeUnitNumber(byte type);
 

	
 
bool LoadUnloadVehicle(Vehicle *v);
 
void LoadUnloadVehicle(Vehicle *v);
 

	
 
void TrainConsistChanged(Vehicle *v);
 
void TrainPowerChanged(Vehicle *v);
 
int32 GetTrainRunningCost(const Vehicle *v);
 

	
 
int CheckTrainStoppedInDepot(const Vehicle *v);
0 comments (0 inline, 0 general)