changeset 13054:1fa162c4d082 draft

(svn r17552) -Codechange: rename load_unload_time_rem to something more generic as it's used for more than just load/unload. Also add some documentation about where it is used.
author rubidium <rubidium@openttd.org>
date Tue, 15 Sep 2009 20:58:44 +0000
parents a65b1859dfcd
children 84858725fc2a
files src/aircraft_cmd.cpp src/economy.cpp src/newgrf_engine.cpp src/roadveh_cmd.cpp src/saveload/oldloader_sl.cpp src/saveload/vehicle_sl.cpp src/train_cmd.cpp src/vehicle.cpp src/vehicle_base.h
diffstat 9 files changed, 41 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -325,7 +325,7 @@
 		v->name = NULL;
 //		v->next_order_param = v->next_order = 0;
 
-//		v->load_unload_time_rem = 0;
+//		v->time_counter = 0;
 //		v->progress = 0;
 		v->last_station_visited = INVALID_STATION;
 //		v->destination_coords = 0;
@@ -1048,7 +1048,7 @@
 	count = UpdateAircraftSpeed(v, speed_limit, hard_limit);
 	if (count == 0) return false;
 
-	if (v->load_unload_time_rem != 0) v->load_unload_time_rem--;
+	if (v->time_counter != 0) v->time_counter--;
 
 	do {
 
@@ -1072,13 +1072,13 @@
 			Direction newdir = GetDirectionTowards(v, x + amd->x, y + amd->y);
 			if (newdir != v->direction) {
 				if (amd->flag & AMED_SLOWTURN && v->number_consecutive_turns < 8) {
-					if (v->load_unload_time_rem == 0 || newdir == v->last_direction) {
+					if (v->time_counter == 0 || newdir == v->last_direction) {
 						if (newdir == v->last_direction) {
 							v->number_consecutive_turns = 0;
 						} else {
 							v->number_consecutive_turns++;
 						}
-						v->load_unload_time_rem = 2 * _settings_game.vehicle.plane_speed;
+						v->time_counter = 2 * _settings_game.vehicle.plane_speed;
 						v->last_direction = v->direction;
 						v->direction = newdir;
 					}
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -1069,7 +1069,7 @@
 	ClrBit(front_v->vehicle_flags, VF_LOADING_FINISHED);
 
 	/* Start unloading in at the first possible moment */
-	front_v->load_unload_time_rem = 1;
+	front_v->time_counter = 1;
 
 	if ((front_v->current_order.GetUnloadType() & OUFB_NO_UNLOAD) == 0) {
 		for (Vehicle *v = front_v; v != NULL; v = v->Next()) {
@@ -1095,10 +1095,10 @@
 {
 	assert(v->current_order.IsType(OT_LOADING));
 
-	assert(v->load_unload_time_rem != 0);
+	assert(v->time_counter != 0);
 
 	/* We have not waited enough time till the next round of loading/unloading */
-	if (--v->load_unload_time_rem != 0) {
+	if (--v->time_counter != 0) {
 		if (_settings_game.order.improved_load && (v->current_order.GetLoadType() & OLFB_FULL_LOAD)) {
 			/* 'Reserve' this cargo for this vehicle, because we were first. */
 			for (; v != NULL; v = v->Next()) {
@@ -1116,7 +1116,7 @@
 		/* The train reversed in the station. Take the "easy" way
 		 * out and let the train just leave as it always did. */
 		SetBit(v->vehicle_flags, VF_LOADING_FINISHED);
-		v->load_unload_time_rem = 1;
+		v->time_counter = 1;
 		return;
 	}
 
@@ -1347,7 +1347,7 @@
 	}
 
 	/* Always wait at least 1, otherwise we'll wait 'infinitively' long. */
-	v->load_unload_time_rem = max(1, unloading_time);
+	v->time_counter = max(1, unloading_time);
 
 	if (completely_emptied) {
 		TriggerVehicle(v, VEHICLE_TRIGGER_EMPTY);
--- a/src/newgrf_engine.cpp
+++ b/src/newgrf_engine.cpp
@@ -709,8 +709,8 @@
 		case 0x0B: return v->current_order.GetDestination();
 		case 0x0C: return v->GetNumOrders();
 		case 0x0D: return v->cur_order_index;
-		case 0x10: return v->load_unload_time_rem;
-		case 0x11: return GB(v->load_unload_time_rem, 8, 8);
+		case 0x10: return v->time_counter;
+		case 0x11: return GB(v->time_counter, 8, 8);
 		case 0x12: return max(v->date_of_last_service - DAYS_TILL_ORIGINAL_BASE_YEAR, 0);
 		case 0x13: return GB(max(v->date_of_last_service - DAYS_TILL_ORIGINAL_BASE_YEAR, 0), 8, 8);
 		case 0x14: return v->service_interval;
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -245,7 +245,7 @@
 		v->value = cost.GetCost();
 //		v->day_counter = 0;
 //		v->next_order_param = v->next_order = 0;
-//		v->load_unload_time_rem = 0;
+//		v->time_counter = 0;
 //		v->progress = 0;
 
 //		v->overtaking = 0;
--- a/src/saveload/oldloader_sl.cpp
+++ b/src/saveload/oldloader_sl.cpp
@@ -1134,7 +1134,7 @@
 	OCL_NULL ( 1 ), ///< num_orders, now calculated
 	OCL_SVAR(  OC_UINT8, Vehicle, cur_order_index ),
 	OCL_SVAR(   OC_TILE, Vehicle, dest_tile ),
-	OCL_SVAR( OC_UINT16, Vehicle, load_unload_time_rem ),
+	OCL_SVAR( OC_UINT16, Vehicle, time_counter ),
 	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Vehicle, date_of_last_service ),
 	OCL_SVAR( OC_UINT16, Vehicle, service_interval ),
 	OCL_SVAR( OC_FILE_U8 | OC_VAR_U16, Vehicle, last_station_visited ),
--- a/src/saveload/vehicle_sl.cpp
+++ b/src/saveload/vehicle_sl.cpp
@@ -496,7 +496,7 @@
 		 SLE_CONDVAR(Vehicle, build_year,            SLE_FILE_U8 | SLE_VAR_I32,    0,  30),
 		 SLE_CONDVAR(Vehicle, build_year,            SLE_INT32,                   31, SL_MAX_VERSION),
 
-		     SLE_VAR(Vehicle, load_unload_time_rem,  SLE_UINT16),
+		     SLE_VAR(Vehicle, time_counter,          SLE_UINT16),
 		SLEG_CONDVAR(         _cargo_paid_for,       SLE_UINT16,                  45, SL_MAX_VERSION),
 		 SLE_CONDVAR(Vehicle, vehicle_flags,         SLE_UINT8,                   40, SL_MAX_VERSION),
 
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -1648,7 +1648,7 @@
 		/* When loading the vehicle is already stopped. No need to change that. */
 		if (v->current_order.IsType(OT_LOADING)) return;
 
-		v->load_unload_time_rem = 0;
+		v->time_counter = 0;
 
 		/* Stop train */
 		v->cur_speed = 0;
@@ -1988,7 +1988,7 @@
 	} else if (HasBit(v->flags, VRF_TRAIN_STUCK)) {
 		/* A train not inside a PBS block can't be stuck. */
 		ClrBit(v->flags, VRF_TRAIN_STUCK);
-		v->load_unload_time_rem = 0;
+		v->time_counter = 0;
 	}
 }
 
@@ -2445,12 +2445,12 @@
 
 	if (v->force_proceed == 0) {
 		/* force proceed was not pressed */
-		if (++v->load_unload_time_rem < 37) {
+		if (++v->time_counter < 37) {
 			SetWindowClassesDirty(WC_TRAINS_LIST);
 			return true;
 		}
 
-		v->load_unload_time_rem = 0;
+		v->time_counter = 0;
 
 		seg_state = _settings_game.pf.reserve_paths ? SIGSEG_PBS : UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
 		if (seg_state == SIGSEG_FULL || HasDepotReservation(v->tile)) {
@@ -3196,7 +3196,7 @@
 	}
 
 	if (HasBit(v->flags, VRF_TRAIN_STUCK)) {
-		v->load_unload_time_rem = 0;
+		v->time_counter = 0;
 		SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
 	}
 	ClrBit(v->flags, VRF_TRAIN_STUCK);
@@ -3768,12 +3768,12 @@
 							v->cur_speed = 0;
 							v->subspeed = 0;
 							v->progress = 255 - 100;
-							if (_settings_game.pf.wait_oneway_signal == 255 || ++v->load_unload_time_rem < _settings_game.pf.wait_oneway_signal * 20) return;
+							if (_settings_game.pf.wait_oneway_signal == 255 || ++v->time_counter < _settings_game.pf.wait_oneway_signal * 20) return;
 						} else if (HasSignalOnTrackdir(gp.new_tile, i)) {
 							v->cur_speed = 0;
 							v->subspeed = 0;
 							v->progress = 255 - 10;
-							if (_settings_game.pf.wait_twoway_signal == 255 || ++v->load_unload_time_rem < _settings_game.pf.wait_twoway_signal * 73) {
+							if (_settings_game.pf.wait_twoway_signal == 255 || ++v->time_counter < _settings_game.pf.wait_twoway_signal * 73) {
 								DiagDirection exitdir = TrackdirToExitdir(i);
 								TileIndex o_tile = TileAddByDiagDir(gp.new_tile, exitdir);
 
@@ -3787,7 +3787,7 @@
 						/* If we would reverse but are currently in a PBS block and
 						 * reversing of stuck trains is disabled, don't reverse. */
 						if (_settings_game.pf.wait_for_pbs_path == 255 && UpdateSignalsOnSegment(v->tile, enterdir, v->owner) == SIGSEG_PBS) {
-							v->load_unload_time_rem = 0;
+							v->time_counter = 0;
 							return;
 						}
 						goto reverse_train_direction;
@@ -3881,7 +3881,7 @@
 				}
 
 				if (v->IsFrontEngine()) {
-					v->load_unload_time_rem = 0;
+					v->time_counter = 0;
 
 					/* If we are approching a crossing that is reserved, play the sound now. */
 					TileIndex crossing = TrainApproachingCrossingTile(v);
@@ -3973,7 +3973,7 @@
 	if (prev != NULL) error("Disconnecting train");
 
 reverse_train_direction:
-	v->load_unload_time_rem = 0;
+	v->time_counter = 0;
 	v->cur_speed = 0;
 	v->subspeed = 0;
 	ReverseTrainDirection(v);
@@ -4351,7 +4351,7 @@
 
 	bool valid_order = !v->current_order.IsType(OT_NOTHING) && v->current_order.GetType() != OT_CONDITIONAL;
 	if (ProcessOrders(v) && CheckReverseTrain(v)) {
-		v->load_unload_time_rem = 0;
+		v->time_counter = 0;
 		v->cur_speed = 0;
 		v->subspeed = 0;
 		ReverseTrainDirection(v);
@@ -4373,17 +4373,17 @@
 
 	/* Handle stuck trains. */
 	if (!mode && HasBit(v->flags, VRF_TRAIN_STUCK)) {
-		++v->load_unload_time_rem;
+		++v->time_counter;
 
 		/* Should we try reversing this tick if still stuck? */
-		bool turn_around = v->load_unload_time_rem % (_settings_game.pf.wait_for_pbs_path * DAY_TICKS) == 0 && _settings_game.pf.wait_for_pbs_path < 255;
-
-		if (!turn_around && v->load_unload_time_rem % _settings_game.pf.path_backoff_interval != 0 && v->force_proceed == 0) return true;
+		bool turn_around = v->time_counter % (_settings_game.pf.wait_for_pbs_path * DAY_TICKS) == 0 && _settings_game.pf.wait_for_pbs_path < 255;
+
+		if (!turn_around && v->time_counter % _settings_game.pf.path_backoff_interval != 0 && v->force_proceed == 0) return true;
 		if (!TryPathReserve(v)) {
 			/* Still stuck. */
 			if (turn_around) ReverseTrainDirection(v);
 
-			if (HasBit(v->flags, VRF_TRAIN_STUCK) && v->load_unload_time_rem > 2 * _settings_game.pf.wait_for_pbs_path * DAY_TICKS) {
+			if (HasBit(v->flags, VRF_TRAIN_STUCK) && v->time_counter > 2 * _settings_game.pf.wait_for_pbs_path * DAY_TICKS) {
 				/* Show message to player. */
 				if (_settings_client.gui.lost_train_warn && v->owner == _local_company) {
 					SetDParam(0, v->index);
@@ -4393,12 +4393,12 @@
 						v->index
 					);
 				}
-				v->load_unload_time_rem = 0;
+				v->time_counter = 0;
 			}
 			/* Exit if force proceed not pressed, else reset stuck flag anyway. */
 			if (v->force_proceed == 0) return true;
 			ClrBit(v->flags, VRF_TRAIN_STUCK);
-			v->load_unload_time_rem = 0;
+			v->time_counter = 0;
 			SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
 		}
 	}
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -966,7 +966,7 @@
 			if (_settings_client.gui.show_track_reservation) MarkTileDirtyByTile(t->tile);
 
 			UpdateSignalsOnSegment(t->tile, INVALID_DIAGDIR, t->owner);
-			t->load_unload_time_rem = 0;
+			t->time_counter = 0;
 			ClrBit(t->flags, VRF_TOGGLE_REVERSE);
 			TrainConsistChanged(t, true);
 			break;
--- a/src/vehicle_base.h
+++ b/src/vehicle_base.h
@@ -182,7 +182,14 @@
 	} orders;
 
 	byte vehicle_flags;             ///< Used for gradual loading and other miscellaneous things (@see VehicleFlags enum)
-	uint16 load_unload_time_rem;
+
+	/**
+	 * Multi purpose variable used as counter for:
+	 *  - loading/unloading: ticks to wait before starting next cycle.
+	 *  - aircraft: ticks between each turn to prevent > 45 degree turns.
+	 *  - trains: ticks waiting in front of a signal, ticks being stuck or a counter for forced proceeding through signals.
+	 */
+	uint16 time_counter;
 
 	GroupID group_id;               ///< Index of group Pool array