Changeset - r28278:1fc682037ca2
[Not reviewed]
master
0 7 0
Tyler Trahan - 7 months ago 2023-11-26 16:12:02
tyler@tylertrahan.com
Codechange: Use Ticks for BaseConsist timetable fields
7 files changed with 13 insertions and 12 deletions:
0 comments (0 inline, 0 general)
src/base_consist.h
Show inline comments
 
@@ -9,26 +9,26 @@
 

	
 
#ifndef BASE_CONSIST_H
 
#define BASE_CONSIST_H
 

	
 
#include "order_type.h"
 
#include "timer/timer_game_tick.h"
 

	
 
/** Various front vehicle properties that are preserved when autoreplacing, using order-backup or switching front engines within a consist. */
 
struct BaseConsist {
 
	std::string name;                   ///< Name of vehicle
 

	
 
	/* Used for timetabling. */
 
	uint32_t current_order_time;               ///< How many ticks have passed since this order started.
 
	int32_t lateness_counter;                  ///< How many ticks late (or early if negative) this vehicle is.
 
	TimerGameTick::Ticks current_order_time;    ///< How many ticks have passed since this order started.
 
	TimerGameTick::Ticks lateness_counter;      ///< How many ticks late (or early if negative) this vehicle is.
 
	TimerGameTick::TickCounter timetable_start; ///< At what tick of TimerGameTick::counter the vehicle should start its timetable.
 

	
 
	uint16_t service_interval;            ///< The interval for (automatic) servicing; either in days or %.
 

	
 
	VehicleOrderID cur_real_order_index;///< The index to the current real (non-implicit) order
 
	VehicleOrderID cur_implicit_order_index;///< The index to the current implicit order
 

	
 
	uint16_t vehicle_flags;               ///< Used for gradual loading and other miscellaneous things (@see VehicleFlags enum)
 

	
 
	virtual ~BaseConsist() = default;
 

	
 
	void CopyConsistPropertiesFrom(const BaseConsist *src);
src/economy.cpp
Show inline comments
 
@@ -1863,25 +1863,25 @@ static void LoadUnloadVehicle(Vehicle *f
 
	ClrBit(front->vehicle_flags, VF_STOP_LOADING);
 
	if (anything_loaded || anything_unloaded) {
 
		if (_settings_game.order.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 };
 

	
 
			new_load_unload_ticks = gradual_loading_wait_time[front->type];
 
		}
 
		/* We loaded less cargo than possible for all cargo types and it's not full
 
		 * load and we're not supposed to wait any longer: stop loading. */
 
		if (!anything_unloaded && full_load_amount == 0 && reservation_left == 0 && !(front->current_order.GetLoadType() & OLFB_FULL_LOAD) &&
 
				front->current_order_time >= (uint)std::max(front->current_order.GetTimetabledWait() - front->lateness_counter, 0)) {
 
				front->current_order_time >= std::max(front->current_order.GetTimetabledWait() - front->lateness_counter, 0)) {
 
			SetBit(front->vehicle_flags, VF_STOP_LOADING);
 
		}
 

	
 
		UpdateLoadUnloadTicks(front, st, new_load_unload_ticks);
 
	} else {
 
		UpdateLoadUnloadTicks(front, st, 20); // We need the ticks for link refreshing.
 
		bool finished_loading = true;
 
		if (front->current_order.GetLoadType() & OLFB_FULL_LOAD) {
 
			if (front->current_order.GetLoadType() == OLF_FULL_LOAD_ANY) {
 
				/* if the aircraft carries passengers and is NOT full, then
 
				 * continue loading, no matter how much mail is in */
 
				if ((front->type == VEH_AIRCRAFT && IsCargoInClass(front->cargo_type, CC_PASSENGERS) && front->cargo_cap > front->cargo.StoredCount()) ||
src/linkgraph/refresh.cpp
Show inline comments
 
@@ -224,26 +224,25 @@ void LinkRefresher::RefreshStats(const O
 
			/* This estimates the travel time of the link as the time needed
 
			 * to travel between the stations at half the max speed of the consist.
 
			 * The result is in tiles/tick (= 2048 km-ish/h). */
 
			uint32_t time_estimate = DistanceManhattan(st->xy, st_to->xy) * 4096U / this->vehicle->GetDisplayMaxSpeed();
 

	
 
			/* If the vehicle is currently full loading, increase the capacities at the station
 
			 * where it is loading by an estimate of what it would have transported if it wasn't
 
			 * loading. Don't do that if the vehicle has been waiting for longer than the entire
 
			 * order list is supposed to take, though. If that is the case the total duration is
 
			 * probably far off and we'd greatly overestimate the capacity by increasing.*/
 
			if (this->is_full_loading && this->vehicle->orders != nullptr &&
 
					st->index == vehicle->last_station_visited &&
 
					this->vehicle->orders->GetTotalDuration() >
 
					(TimerGameTick::Ticks)this->vehicle->current_order_time) {
 
					this->vehicle->orders->GetTotalDuration() > this->vehicle->current_order_time) {
 
				uint effective_capacity = cargo_quantity * this->vehicle->load_unload_ticks;
 
				if (effective_capacity > (uint)this->vehicle->orders->GetTotalDuration()) {
 
					IncreaseStats(st, c, next_station, effective_capacity /
 
							this->vehicle->orders->GetTotalDuration(), 0, 0,
 
							EUM_INCREASE | restricted_mode);
 
				} else if (RandomRange(this->vehicle->orders->GetTotalDuration()) < effective_capacity) {
 
					IncreaseStats(st, c, next_station, 1, 0, 0, EUM_INCREASE | restricted_mode);
 
				} else {
 
					IncreaseStats(st, c, next_station, cargo_quantity, 0, time_estimate, EUM_REFRESH | restricted_mode);
 
				}
 
			} else {
 
				IncreaseStats(st, c, next_station, cargo_quantity, 0, time_estimate, EUM_REFRESH | restricted_mode);
src/saveload/saveload.h
Show inline comments
 
@@ -356,24 +356,25 @@ enum SaveLoadVersion : uint16_t {
 
	SLV_SAVEGAME_ID,                        ///< 313  PR#10719 Add an unique ID to every savegame (used to deduplicate surveys).
 
	SLV_STRING_GAMELOG,                     ///< 314  PR#10801 Use std::string in gamelog.
 

	
 
	SLV_INDUSTRY_CARGO_REORGANISE,          ///< 315  PR#10853 Industry accepts/produced data reorganised.
 
	SLV_PERIODS_IN_TRANSIT_RENAME,          ///< 316  PR#11112 Rename days in transit to (cargo) periods in transit.
 
	SLV_NEWGRF_LAST_SERVICE,                ///< 317  PR#11124 Added stable date_of_last_service to avoid NewGRF trouble.
 
	SLV_REMOVE_LOADED_AT_XY,                ///< 318  PR#11276 Remove loaded_at_xy variable from CargoPacket.
 
	SLV_CARGO_TRAVELLED,                    ///< 319  PR#11283 CargoPacket now tracks how far it travelled inside a vehicle.
 

	
 
	SLV_STATION_RATING_CHEAT,               ///< 320  PR#11346 Add cheat to fix station ratings at 100%.
 
	SLV_TIMETABLE_START_TICKS,              ///< 321  PR#11468 Convert timetable start from a date to ticks.
 
	SLV_TIMETABLE_START_TICKS_FIX,          ///< 322  PR#11557 Fix for missing convert timetable start from a date to ticks.
 
	SLV_TIMETABLE_TICKS_TYPE,               ///< 323  PR#11435 Convert timetable current order time to ticks.
 

	
 
	SL_MAX_VERSION,                         ///< Highest possible saveload version
 
};
 

	
 
/** Save or load result codes. */
 
enum SaveOrLoadResult {
 
	SL_OK     = 0, ///< completed successfully
 
	SL_ERROR  = 1, ///< error that was caught before internal structures were modified
 
	SL_REINIT = 2, ///< error that was caught in the middle of updating game state, need to clear it. (can only happen during load)
 
};
 

	
 
/** Deals with the type of the savegame, independent of extension */
src/saveload/vehicle_sl.cpp
Show inline comments
 
@@ -711,25 +711,26 @@ public:
 
		SLEG_CONDVAR("cargo_feeder_share", _cargo_feeder_share, SLE_FILE_I32 | SLE_VAR_I64,  SLV_51,  SLV_65),
 
		SLEG_CONDVAR("cargo_feeder_share", _cargo_feeder_share, SLE_INT64,                   SLV_65,  SLV_68),
 
		SLE_CONDVAR(Vehicle, value,                 SLE_FILE_I32 | SLE_VAR_I64,   SL_MIN_VERSION,  SLV_65),
 
		SLE_CONDVAR(Vehicle, value,                 SLE_INT64,                   SLV_65, SL_MAX_VERSION),
 

	
 
		SLE_CONDVAR(Vehicle, random_bits,           SLE_FILE_U8 | SLE_VAR_U16,    SLV_2, SLV_EXTEND_VEHICLE_RANDOM),
 
		SLE_CONDVAR(Vehicle, random_bits,           SLE_UINT16,                   SLV_EXTEND_VEHICLE_RANDOM, SL_MAX_VERSION),
 
		SLE_CONDVAR(Vehicle, waiting_triggers,      SLE_UINT8,                    SLV_2, SL_MAX_VERSION),
 

	
 
		SLE_CONDREF(Vehicle, next_shared,           REF_VEHICLE,                  SLV_2, SL_MAX_VERSION),
 
		SLE_CONDVAR(Vehicle, group_id,              SLE_UINT16,                  SLV_60, SL_MAX_VERSION),
 

	
 
		SLE_CONDVAR(Vehicle, current_order_time,    SLE_UINT32,                  SLV_67, SL_MAX_VERSION),
 
		SLE_CONDVAR(Vehicle, current_order_time,    SLE_FILE_U32 | SLE_VAR_I32,  SLV_67, SLV_TIMETABLE_TICKS_TYPE),
 
		SLE_CONDVAR(Vehicle, current_order_time,    SLE_INT32,                   SLV_TIMETABLE_TICKS_TYPE, SL_MAX_VERSION),
 
		SLE_CONDVAR(Vehicle, last_loading_tick,     SLE_UINT64,                   SLV_LAST_LOADING_TICK, SL_MAX_VERSION),
 
		SLE_CONDVAR(Vehicle, lateness_counter,      SLE_INT32,                   SLV_67, SL_MAX_VERSION),
 
	};
 
#if defined(_MSC_VER) && (_MSC_VER == 1915 || _MSC_VER == 1916)
 
		return description;
 
	}
 
#endif
 
	inline const static SaveLoadCompatTable compat_description = _vehicle_common_sl_compat;
 

	
 
	void Save(Vehicle *v) const override
 
	{
 
		SlObject(v, this->GetDescription());
src/timetable_cmd.cpp
Show inline comments
 
@@ -255,25 +255,25 @@ CommandCost CmdSetVehicleOnTime(DoComman
 
	Vehicle *v = Vehicle::GetIfValid(veh);
 
	if (v == nullptr || !v->IsPrimaryVehicle() || v->orders == nullptr) return CMD_ERROR;
 

	
 
	/* A vehicle can't be late if its timetable hasn't started.
 
	 * If we're setting all vehicles in the group, we handle that below. */
 
	if (!apply_to_group && !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return CommandCost(STR_ERROR_TIMETABLE_NOT_STARTED);
 

	
 
	CommandCost ret = CheckOwnership(v->owner);
 
	if (ret.Failed()) return ret;
 

	
 
	if (flags & DC_EXEC) {
 
		if (apply_to_group) {
 
			int32_t most_late = 0;
 
			TimerGameTick::Ticks most_late = 0;
 
			for (Vehicle *u = v->FirstShared(); u != nullptr; u = u->NextShared()) {
 
				/* A vehicle can't be late if its timetable hasn't started. */
 
				if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) continue;
 

	
 
				if (u->lateness_counter > most_late) {
 
					most_late = u->lateness_counter;
 
				}
 
			}
 
			if (most_late > 0) {
 
				for (Vehicle *u = v->FirstShared(); u != nullptr; u = u->NextShared()) {
 
					/* A vehicle can't be late if its timetable hasn't started. */
 
					if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) continue;
 
@@ -445,25 +445,25 @@ CommandCost CmdAutofillTimetable(DoComma
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 
/**
 
 * Update the timetable for the vehicle.
 
 * @param v The vehicle to update the timetable for.
 
 * @param travelling Whether we just travelled or waited at a station.
 
 */
 
void UpdateVehicleTimetable(Vehicle *v, bool travelling)
 
{
 
	uint time_taken = v->current_order_time;
 
	TimerGameTick::Ticks time_taken = v->current_order_time;
 

	
 
	v->current_order_time = 0;
 

	
 
	if (v->current_order.IsType(OT_IMPLICIT)) return; // no timetabling of auto orders
 

	
 
	if (v->cur_real_order_index >= v->GetNumOrders()) return;
 
	Order *real_current_order = v->GetOrder(v->cur_real_order_index);
 
	assert(real_current_order != nullptr);
 

	
 
	VehicleOrderID first_manual_order = 0;
 
	for (Order *o = v->GetFirstOrder(); o != nullptr && o->IsType(OT_IMPLICIT); o = o->next) {
 
		++first_manual_order;
 
@@ -505,57 +505,57 @@ void UpdateVehicleTimetable(Vehicle *v, 
 
	/* Before modifying waiting times, check whether we want to preserve bigger ones. */
 
	if (!real_current_order->IsType(OT_CONDITIONAL) &&
 
			(travelling || time_taken > real_current_order->GetWaitTime() || remeasure_wait_time)) {
 
		/* Round the time taken up to the nearest day, as this will avoid
 
		 * confusion for people who are timetabling in days, and can be
 
		 * adjusted later by people who aren't.
 
		 * For trains/aircraft multiple movement cycles are done in one
 
		 * tick. This makes it possible to leave the station and process
 
		 * e.g. a depot order in the same tick, causing it to not fill
 
		 * the timetable entry like is done for road vehicles/ships.
 
		 * Thus always make sure at least one tick is used between the
 
		 * processing of different orders when filling the timetable. */
 
		uint time_to_set = CeilDiv(std::max(time_taken, 1U), Ticks::DAY_TICKS) * Ticks::DAY_TICKS;
 
		uint time_to_set = CeilDiv(std::max(time_taken, 1), Ticks::DAY_TICKS) * Ticks::DAY_TICKS;
 

	
 
		if (travelling && (autofilling || !real_current_order->IsTravelTimetabled())) {
 
			ChangeTimetable(v, v->cur_real_order_index, time_to_set, MTF_TRAVEL_TIME, autofilling);
 
		} else if (!travelling && (autofilling || !real_current_order->IsWaitTimetabled())) {
 
			ChangeTimetable(v, v->cur_real_order_index, time_to_set, MTF_WAIT_TIME, autofilling);
 
		}
 
	}
 

	
 
	if (v->cur_real_order_index == first_manual_order && travelling) {
 
		/* If we just started we would have returned earlier and have not reached
 
		 * this code. So obviously, we have completed our round: So turn autofill
 
		 * off again. */
 
		ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
 
		ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
 
	}
 

	
 
	if (autofilling) return;
 

	
 
	uint timetabled = travelling ? real_current_order->GetTimetabledTravel() :
 
	TimerGameTick::Ticks timetabled = travelling ? real_current_order->GetTimetabledTravel() :
 
			real_current_order->GetTimetabledWait();
 

	
 
	/* Vehicles will wait at stations if they arrive early even if they are not
 
	 * timetabled to wait there, so make sure the lateness counter is updated
 
	 * when this happens. */
 
	if (timetabled == 0 && (travelling || v->lateness_counter >= 0)) return;
 

	
 
	v->lateness_counter -= (timetabled - time_taken);
 

	
 
	/* When we are more late than this timetabled bit takes we (somewhat expensively)
 
	 * check how many ticks the (fully filled) timetable has. If a timetable cycle is
 
	 * shorter than the amount of ticks we are late we reduce the lateness by the
 
	 * length of a full cycle till lateness is less than the length of a timetable
 
	 * cycle. When the timetable isn't fully filled the cycle will be Ticks::INVALID_TICKS. */
 
	if (v->lateness_counter > (int)timetabled) {
 
	if (v->lateness_counter > timetabled) {
 
		TimerGameTick::Ticks cycle = v->orders->GetTimetableTotalDuration();
 
		if (cycle != Ticks::INVALID_TICKS && v->lateness_counter > cycle) {
 
			v->lateness_counter %= cycle;
 
		}
 
	}
 

	
 
	for (v = v->FirstShared(); v != nullptr; v = v->NextShared()) {
 
		SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
 
	}
 
}
src/vehicle.cpp
Show inline comments
 
@@ -2324,25 +2324,25 @@ void Vehicle::ResetRefitCaps()
 
	for (Vehicle *v = this; v != nullptr; v = v->Next()) v->refit_cap = v->cargo_cap;
 
}
 

	
 
/**
 
 * 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 Vehicle::HandleLoading(bool mode)
 
{
 
	switch (this->current_order.GetType()) {
 
		case OT_LOADING: {
 
			uint wait_time = std::max(this->current_order.GetTimetabledWait() - this->lateness_counter, 0);
 
			TimerGameTick::Ticks wait_time = std::max(this->current_order.GetTimetabledWait() - this->lateness_counter, 0);
 

	
 
			/* Not the first call for this tick, or still loading */
 
			if (mode || !HasBit(this->vehicle_flags, VF_LOADING_FINISHED) || this->current_order_time < wait_time) return;
 

	
 
			this->PlayLeaveStationSound();
 

	
 
			this->LeaveStation();
 

	
 
			/* Only advance to next order if we just loaded at the current one */
 
			const Order *order = this->GetOrder(this->cur_implicit_order_index);
 
			if (order == nullptr ||
 
					(!order->IsType(OT_IMPLICIT) && !order->IsType(OT_GOTO_STATION)) ||
0 comments (0 inline, 0 general)