(svn r9831) -Codechange: more refactoring of the loading/unloading.

This commit is contained in:
rubidium 2007-05-13 21:24:58 +00:00
parent 2ccc060b4a
commit d78651f7b1
3 changed files with 42 additions and 99 deletions

View File

@ -1378,73 +1378,6 @@ static bool LoadWait(const Vehicle* v, const Vehicle* u)
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
@ -1477,7 +1410,7 @@ void VehiclePayment(Vehicle *front_v)
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 */
@ -1548,31 +1481,25 @@ void VehiclePayment(Vehicle *front_v)
/**
* 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);
@ -1591,8 +1518,6 @@ bool LoadUnloadVehicle(Vehicle *v)
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)) {
@ -1635,6 +1560,7 @@ bool LoadUnloadVehicle(Vehicle *v)
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 {
@ -1674,7 +1600,7 @@ bool LoadUnloadVehicle(Vehicle *v)
/* 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;
}
@ -1690,7 +1616,6 @@ bool LoadUnloadVehicle(Vehicle *v)
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,
@ -1716,25 +1641,43 @@ bool LoadUnloadVehicle(Vehicle *v)
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) {
@ -1761,8 +1704,6 @@ bool LoadUnloadVehicle(Vehicle *v)
if (result & 2) InvalidateWindow(WC_STATION_VIEW, last_visited);
}
return true;
}
void PlayersMonthlyLoop()

View File

@ -2941,7 +2941,9 @@ void Vehicle::HandleLoading(bool mode)
/* 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();

View File

@ -521,7 +521,7 @@ 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);