Changeset - r6594:cd6701cee0ee
[Not reviewed]
master
0 6 0
rubidium - 17 years ago 2007-05-07 16:21:34
rubidium@openttd.org
(svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
6 files changed with 45 insertions and 109 deletions:
0 comments (0 inline, 0 general)
src/aircraft_cmd.cpp
Show inline comments
 
@@ -1407,50 +1407,24 @@ static void ProcessAircraftOrder(Vehicle
 
	InvalidateVehicleOrder(v);
 

	
 
	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
 
}
 

	
 
void Aircraft::MarkDirty()
 
{
 
		this->cur_image = GetAircraftImage(this, this->direction);
 
		if (this->subtype == AIR_HELICOPTER) this->next->next->cur_image = GetRotorImage(this);
 
		MarkAllViewportsDirty(this->left_coord, this->top_coord, this->right_coord + 1, this->bottom_coord + 1);
 
}
 

	
 
static void HandleAircraftLoading(Vehicle *v, int mode)
 
{
 
	switch (v->current_order.type) {
 
		case OT_LOADING: {
 
			if (mode != 0) return;
 
			if (--v->load_unload_time_rem != 0) return;
 

	
 
			if (LoadUnloadVehicle(v)) return;
 

	
 
			Order b = v->current_order;
 
			v->LeaveStation();
 
			v->current_order.Free();
 
			v->MarkDirty();
 
			if (!(b.flags & OF_NON_STOP)) return;
 
			break;
 
		}
 

	
 
		case OT_DUMMY: break;
 

	
 
		default: return;
 
	}
 

	
 
	v->cur_order_index++;
 
	InvalidateVehicleOrder(v);
 
}
 

	
 
static void CrashAirplane(Vehicle *v)
 
{
 
	v->vehstatus |= VS_CRASHED;
 
	v->u.air.crashed_counter = 0;
 

	
 
	CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
 

	
 
	InvalidateWindow(WC_VEHICLE_VIEW, v->index);
 

	
 
	uint amt = 2;
 
	if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) amt += v->cargo_count;
 
	SetDParam(0, amt);
 
@@ -2121,25 +2095,25 @@ static void AircraftEventHandler(Vehicle
 

	
 
	/* aircraft is broken down? */
 
	if (v->breakdown_ctr != 0) {
 
		if (v->breakdown_ctr <= 2) {
 
			HandleBrokenAircraft(v);
 
		} else {
 
			v->breakdown_ctr--;
 
		}
 
	}
 

	
 
	HandleAircraftSmoke(v);
 
	ProcessAircraftOrder(v);
 
	HandleAircraftLoading(v, loop);
 
	v->HandleLoading(loop != 0);
 

	
 
	if (v->current_order.type >= OT_LOADING) return;
 

	
 
	AirportGoToNextPosition(v);
 
}
 

	
 
void Aircraft_Tick(Vehicle *v)
 
{
 
	if (!IsNormalAircraft(v)) return;
 

	
 
	if (v->subtype == AIR_HELICOPTER) HelicopterTickHandler(v);
 

	
src/roadveh_cmd.cpp
Show inline comments
 
@@ -744,49 +744,24 @@ static void ProcessRoadVehOrder(Vehicle 
 
		case OT_GOTO_DEPOT:
 
			v->dest_tile = GetDepot(order->dest)->xy;
 
			break;
 

	
 
		default:
 
			v->dest_tile = 0;
 
			break;
 
	}
 

	
 
	InvalidateVehicleOrder(v);
 
}
 

	
 
static void HandleRoadVehLoading(Vehicle *v)
 
{
 
	switch (v->current_order.type) {
 
		case OT_LOADING: {
 
			Order b;
 

	
 
			if (--v->load_unload_time_rem != 0) return;
 

	
 
			if (LoadUnloadVehicle(v)) return;
 

	
 
			b = v->current_order;
 
			v->LeaveStation();
 
			if (!(b.flags & OF_NON_STOP)) return;
 
			break;
 
		}
 

	
 
		case OT_DUMMY: break;
 

	
 
		default: return;
 
	}
 

	
 
	v->cur_order_index++;
 
	InvalidateVehicleOrder(v);
 
}
 

	
 
static void StartRoadVehSound(const Vehicle* v)
 
{
 
	if (!PlayVehicleSound(v, VSE_START)) {
 
		SoundFx s = RoadVehInfo(v->engine_type)->sfx;
 
		if (s == SND_19_BUS_START_PULL_AWAY && (v->tick_counter & 3) == 0)
 
			s = SND_1A_BUS_START_PULL_AWAY_WITH_HORN;
 
		SndPlayVehicleFx(s, v);
 
	}
 
}
 

	
 
struct RoadVehFindData {
 
	int x;
 
@@ -1295,25 +1270,25 @@ static void RoadVehController(Vehicle *v
 
	/* road vehicle has broken down? */
 
	if (v->breakdown_ctr != 0) {
 
		if (v->breakdown_ctr <= 2) {
 
			HandleBrokenRoadVeh(v);
 
			return;
 
		}
 
		v->breakdown_ctr--;
 
	}
 

	
 
	if (v->vehstatus & VS_STOPPED) return;
 

	
 
	ProcessRoadVehOrder(v);
 
	HandleRoadVehLoading(v);
 
	v->HandleLoading();
 

	
 
	if (v->current_order.type == OT_LOADING) return;
 

	
 
	if (IsRoadVehInDepot(v)) {
 
		/* Vehicle is about to leave a depot */
 
		DiagDirection dir;
 
		const RoadDriveEntry* rdp;
 
		Trackdir tdir;
 

	
 
		v->cur_speed = 0;
 

	
 
		dir = GetRoadDepotDirection(v->tile);
src/ship_cmd.cpp
Show inline comments
 
@@ -296,49 +296,24 @@ static void ProcessShipOrder(Vehicle *v)
 
		}
 
	} else if (order->type == OT_GOTO_DEPOT) {
 
		v->dest_tile = GetDepot(order->dest)->xy;
 
	} else {
 
		v->dest_tile = 0;
 
	}
 

	
 
	InvalidateVehicleOrder(v);
 

	
 
	InvalidateWindowClasses(WC_SHIPS_LIST);
 
}
 

	
 
static void HandleShipLoading(Vehicle *v)
 
{
 
	switch (v->current_order.type) {
 
		case OT_LOADING: {
 
			if (--v->load_unload_time_rem != 0) return;
 

	
 
			if (LoadUnloadVehicle(v)) return;
 

	
 
			v->PlayLeaveStationSound();
 

	
 
			Order b = v->current_order;
 
			v->LeaveStation();
 
			if (!(b.flags & OF_NON_STOP)) return;
 
			break;
 
		}
 

	
 
		case OT_DUMMY: break;
 

	
 
		default: return;
 
	}
 

	
 
	v->cur_order_index++;
 
	InvalidateVehicleOrder(v);
 
}
 

	
 
void Ship::UpdateDeltaXY(Direction direction)
 
{
 
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
 
	static const uint32 _delta_xy_table[8] = {
 
		MKIT( 6,  6,  -3,  -3),
 
		MKIT( 6, 32,  -3, -16),
 
		MKIT( 6,  6,  -3,  -3),
 
		MKIT(32,  6, -16,  -3),
 
		MKIT( 6,  6,  -3,  -3),
 
		MKIT( 6, 32,  -3, -16),
 
		MKIT( 6,  6,  -3,  -3),
 
		MKIT(32,  6, -16,  -3),
 
@@ -672,25 +647,25 @@ static void ShipController(Vehicle *v)
 

	
 
	if (v->breakdown_ctr != 0) {
 
		if (v->breakdown_ctr <= 2) {
 
			HandleBrokenShip(v);
 
			return;
 
		}
 
		v->breakdown_ctr--;
 
	}
 

	
 
	if (v->vehstatus & VS_STOPPED) return;
 

	
 
	ProcessShipOrder(v);
 
	HandleShipLoading(v);
 
	v->HandleLoading();
 

	
 
	if (v->current_order.type == OT_LOADING) return;
 

	
 
	CheckShipLeaveDepot(v);
 

	
 
	if (!ShipAccelerate(v)) return;
 

	
 
	BeginVehicleMove(v);
 

	
 
	GetNewVehiclePosResult gp = GetNewVehiclePos(v);
 
	if (gp.old_tile == gp.new_tile) {
 
		/* Staying in tile */
src/train_cmd.cpp
Show inline comments
 
@@ -2517,53 +2517,24 @@ void Train::MarkDirty()
 
{
 
	Vehicle *v = this;
 
	do {
 
		v->cur_image = GetTrainImage(v, v->direction);
 
		MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1);
 
	} while ((v = v->next) != NULL);
 

	
 
	/* need to update acceleration and cached values since the goods on the train changed. */
 
	TrainCargoChanged(this);
 
	UpdateTrainAcceleration(this);
 
}
 

	
 
static void HandleTrainLoading(Vehicle *v, bool mode)
 
{
 
	switch (v->current_order.type) {
 
		case OT_LOADING: {
 
			if (mode) return;
 

	
 
			if (--v->load_unload_time_rem) return;
 

	
 
			if (LoadUnloadVehicle(v)) return;
 

	
 
			v->PlayLeaveStationSound();
 

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

	
 
			/* If this was not the final order, don't remove it from the list. */
 
			if (!(b.flags & OF_NON_STOP)) return;
 
			break;
 
		}
 

	
 
		case OT_DUMMY: break;
 

	
 
		default: return;
 
	}
 

	
 
	v->cur_order_index++;
 
	InvalidateVehicleOrder(v);
 
}
 

	
 
static int UpdateTrainSpeed(Vehicle *v)
 
{
 
	uint accel;
 

	
 
	if (v->vehstatus & VS_STOPPED || HASBIT(v->u.rail.flags, VRF_REVERSING)) {
 
		if (_patches.realistic_acceleration) {
 
			accel = GetTrainAcceleration(v, AM_BRAKE) * 2;
 
		} else {
 
			accel = v->acceleration * -2;
 
		}
 
	} else {
 
		if (_patches.realistic_acceleration) {
 
@@ -3324,25 +3295,25 @@ static void TrainLocoHandler(Vehicle *v,
 

	
 
	/* exit if train is stopped */
 
	if (v->vehstatus & VS_STOPPED && v->cur_speed == 0) return;
 

	
 
	if (ProcessTrainOrder(v)) {
 
		v->load_unload_time_rem = 0;
 
		v->cur_speed = 0;
 
		v->subspeed = 0;
 
		ReverseTrainDirection(v);
 
		return;
 
	}
 

	
 
	HandleTrainLoading(v, mode);
 
	v->HandleLoading(mode);
 

	
 
	if (v->current_order.type == OT_LOADING) return;
 

	
 
	if (CheckTrainStayInDepot(v)) return;
 

	
 
	if (!mode) HandleLocomotiveSmokeCloud(v);
 

	
 
	int j = UpdateTrainSpeed(v);
 
	if (j == 0) {
 
		/* if the vehicle has speed 0, update the last_speed field. */
 
		if (v->cur_speed != 0) return;
 
	} else {
src/vehicle.cpp
Show inline comments
 
@@ -2920,20 +2920,54 @@ void Vehicle::BeginLoading()
 
	this->MarkDirty();
 
}
 

	
 
void Vehicle::LeaveStation()
 
{
 
	assert(current_order.type == OT_LOADING);
 
	current_order.type = OT_LEAVESTATION;
 
	current_order.flags = 0;
 
	GetStation(this->last_station_visited)->loading_vehicles.remove(this);
 
}
 

	
 

	
 
void Vehicle::HandleLoading(bool mode)
 
{
 
	switch (this->current_order.type) {
 
		case OT_LOADING: {
 
			/* Not the first call for this tick */
 
			if (mode) return;
 

	
 
			/* 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;
 

	
 
			this->PlayLeaveStationSound();
 

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

	
 
			/* If this was not the final order, don't remove it from the list. */
 
			if (!(b.flags & OF_NON_STOP)) return;
 
			break;
 
		}
 

	
 
		case OT_DUMMY: break;
 

	
 
		default: return;
 
	}
 

	
 
	this->cur_order_index++;
 
	InvalidateVehicleOrder(this);
 
}
 

	
 

	
 
void SpecialVehicle::UpdateDeltaXY(Direction direction)
 
{
 
	this->x_offs        = 0;
 
	this->y_offs        = 0;
 
	this->sprite_width  = 1;
 
	this->sprite_height = 1;
 
	this->z_height      = 1;
 
}
src/vehicle.h
Show inline comments
 
@@ -308,24 +308,31 @@ struct Vehicle {
 
		VehicleRail rail;
 
		VehicleAir air;
 
		VehicleRoad road;
 
		VehicleSpecial special;
 
		VehicleDisaster disaster;
 
		VehicleShip ship;
 
	} u;
 

	
 
	void BeginLoading();
 
	void LeaveStation();
 

	
 
	/**
 
	 * Handle the loading of the vehicle; when not it skips through dummy
 
	 * orders and does nothing in all other cases.
 
	 * @param mode is the non-first call for this vehicle in this tick?
 
	 */
 
	void HandleLoading(bool mode = false);
 

	
 
	/**
 
	 * An overriden version of new, so you can use the vehicle instance
 
	 * instead of a newly allocated piece of memory.
 
	 * @param size the size of the variable (unused)
 
	 * @param v    the vehicle to use as 'storage' backend
 
	 * @return the memory that is 'allocated'
 
	 */
 
	void* operator new(size_t size, Vehicle *v) { return v; }
 

	
 
	/**
 
	 * 'Free' the memory allocated by the overriden new.
 
	 * @param p the memory to 'free'
 
	 * @param v the vehicle that was given to 'new' on creation.
0 comments (0 inline, 0 general)