Update to 14.0-beta1

This commit is contained in:
dP
2024-02-04 02:18:17 +05:30
parent 79037e2c65
commit 33ef333b57
1325 changed files with 138465 additions and 70987 deletions

View File

@@ -10,15 +10,51 @@
#include "stdafx.h"
#include "command_func.h"
#include "company_func.h"
#include "date_func.h"
#include "timer/timer_game_tick.h"
#include "timer/timer_game_economy.h"
#include "window_func.h"
#include "vehicle_base.h"
#include "timetable_cmd.h"
#include "timetable.h"
#include "table/strings.h"
#include "safeguards.h"
/**
* Get the TimerGameTick::TickCounter tick of a given date.
* @param start_date The date when the timetable starts.
* @return The first tick of this date.
*/
TimerGameTick::TickCounter GetStartTickFromDate(TimerGameEconomy::Date start_date)
{
/* Calculate the offset in ticks from the current date. */
TimerGameTick::Ticks tick_offset = (start_date - TimerGameEconomy::date).base() * Ticks::DAY_TICKS;
/* Compensate for the current date_fract. */
tick_offset -= TimerGameEconomy::date_fract;
/* Return the current tick plus the offset. */
return TimerGameTick::counter + tick_offset;
}
/**
* Get a date from a given start tick of timetable.
* @param start_tick The TimerGameTick::TickCounter when the timetable starts.
* @return The date when we reach this tick.
*/
TimerGameEconomy::Date GetDateFromStartTick(TimerGameTick::TickCounter start_tick)
{
/* Calculate the offset in ticks from the current counter tick. */
TimerGameTick::Ticks tick_offset = start_tick - TimerGameTick::counter;
/* Compensate for the current date_fract. */
tick_offset += TimerGameEconomy::date_fract;
/* Return the current date plus the offset in days. */
return TimerGameEconomy::date + (tick_offset / Ticks::DAY_TICKS);
}
/**
* Change/update a particular timetable entry.
* @param v The vehicle to change the timetable of.
@@ -27,9 +63,10 @@
* @param mtf Which part of the timetable entry to change.
* @param timetabled If the new value is explicitly timetabled.
*/
static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 val, ModifyTimetableFlags mtf, bool timetabled)
static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16_t val, ModifyTimetableFlags mtf, bool timetabled)
{
Order *order = v->GetOrder(order_number);
assert(order != nullptr);
int total_delta = 0;
int timetable_delta = 0;
@@ -93,7 +130,7 @@ static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 val,
* 0 to clear times, UINT16_MAX to clear speed limit.
* @return the cost of this operation or an error
*/
CommandCost CmdChangeTimetable(DoCommandFlag flags, VehicleID veh, VehicleOrderID order_number, ModifyTimetableFlags mtf, uint16 data)
CommandCost CmdChangeTimetable(DoCommandFlag flags, VehicleID veh, VehicleOrderID order_number, ModifyTimetableFlags mtf, uint16_t data)
{
Vehicle *v = Vehicle::GetIfValid(veh);
if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
@@ -168,6 +205,12 @@ CommandCost CmdChangeTimetable(DoCommandFlag flags, VehicleID veh, VehicleOrderI
default:
break;
}
/* Unbunching data is no longer valid for any vehicle in this shared order group. */
Vehicle *u = v->FirstShared();
for (; u != nullptr; u = u->NextShared()) {
u->ResetDepotUnbunching();
}
}
return CommandCost();
@@ -182,7 +225,7 @@ CommandCost CmdChangeTimetable(DoCommandFlag flags, VehicleID veh, VehicleOrderI
* 0 to clear times, UINT16_MAX to clear speed limit.
* @return the cost of this operation or an error
*/
CommandCost CmdBulkChangeTimetable(DoCommandFlag flags, VehicleID veh, ModifyTimetableFlags mtf, uint16 data)
CommandCost CmdBulkChangeTimetable(DoCommandFlag flags, VehicleID veh, ModifyTimetableFlags mtf, uint16_t data)
{
Vehicle *v = Vehicle::GetIfValid(veh);
if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
@@ -210,19 +253,50 @@ CommandCost CmdBulkChangeTimetable(DoCommandFlag flags, VehicleID veh, ModifyTim
* Clear the lateness counter to make the vehicle on time.
* @param flags Operation to perform.
* @param veh Vehicle with the orders to change.
* @param apply_to_group Set to reset the late counter for all vehicles sharing the orders.
* @return the cost of this operation or an error
*/
CommandCost CmdSetVehicleOnTime(DoCommandFlag flags, VehicleID veh)
CommandCost CmdSetVehicleOnTime(DoCommandFlag flags, VehicleID veh, bool apply_to_group)
{
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) {
v->lateness_counter = 0;
SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
if (apply_to_group) {
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;
}
/* Unbunching data is no longer valid. */
u->ResetDepotUnbunching();
}
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;
u->lateness_counter -= most_late;
SetWindowDirty(WC_VEHICLE_TIMETABLE, u->index);
}
}
} else {
v->lateness_counter = 0;
/* Unbunching data is no longer valid. */
v->ResetDepotUnbunching();
SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
}
}
return CommandCost();
@@ -271,10 +345,10 @@ static bool VehicleTimetableSorter(Vehicle * const &a, Vehicle * const &b)
* @param flags Operation to perform.
* @param veh_id Vehicle ID.
* @param timetable_all Set to set timetable start for all vehicles sharing this order
* @param start_date The timetable start date.
* @param start_tick The TimerGameTick::counter tick when the timetable starts.
* @return The error or cost of the operation.
*/
CommandCost CmdSetTimetableStart(DoCommandFlag flags, VehicleID veh_id, bool timetable_all, Date start_date)
CommandCost CmdSetTimetableStart(DoCommandFlag flags, VehicleID veh_id, bool timetable_all, TimerGameTick::TickCounter start_tick)
{
Vehicle *v = Vehicle::GetIfValid(veh_id);
if (v == nullptr || !v->IsPrimaryVehicle() || v->orders == nullptr) return CMD_ERROR;
@@ -282,14 +356,23 @@ CommandCost CmdSetTimetableStart(DoCommandFlag flags, VehicleID veh_id, bool tim
CommandCost ret = CheckOwnership(v->owner);
if (ret.Failed()) return ret;
int total_duration = v->orders->GetTimetableTotalDuration();
TimerGameTick::Ticks total_duration = v->orders->GetTimetableTotalDuration();
/* Don't let a timetable start more than 15 years into the future or 1 year in the past. */
if (start_date < 0 || start_date > MAX_DAY) return CMD_ERROR;
if (start_date - _date > 15 * DAYS_IN_LEAP_YEAR) return CMD_ERROR;
if (_date - start_date > DAYS_IN_LEAP_YEAR) return CMD_ERROR;
TimerGameEconomy::Date start_date = GetDateFromStartTick(start_tick);
/* Don't let a timetable start at an invalid date. */
if (start_date < 0 || start_date > EconomyTime::MAX_DATE) return CMD_ERROR;
/* Don't let a timetable start more than 15 years into the future... */
if (start_date - TimerGameEconomy::date > TimerGameEconomy::DateAtStartOfYear(MAX_TIMETABLE_START_YEARS)) return CMD_ERROR;
/* ...or 1 year in the past. */
if (TimerGameEconomy::date - start_date > EconomyTime::DAYS_IN_LEAP_YEAR) return CMD_ERROR;
/* If trying to distribute start dates over a shared order group, we need to know the total duration. */
if (timetable_all && !v->orders->IsCompleteTimetable()) return CommandCost(STR_ERROR_TIMETABLE_INCOMPLETE);
if (timetable_all && start_date + total_duration / DAY_TICKS > MAX_DAY) return CMD_ERROR;
/* Don't allow invalid start dates for other vehicles in the shared order group. */
if (timetable_all && start_date + (total_duration / Ticks::DAY_TICKS) > EconomyTime::MAX_DATE) return CMD_ERROR;
if (flags & DC_EXEC) {
std::vector<Vehicle *> vehs;
@@ -311,11 +394,14 @@ CommandCost CmdSetTimetableStart(DoCommandFlag flags, VehicleID veh_id, bool tim
int idx = 0;
for (Vehicle *w : vehs) {
w->lateness_counter = 0;
ClrBit(w->vehicle_flags, VF_TIMETABLE_STARTED);
/* Do multiplication, then division to reduce rounding errors. */
w->timetable_start = start_date + idx * total_duration / num_vehs / DAY_TICKS;
w->timetable_start = start_tick + (idx * total_duration / num_vehs);
/* Unbunching data is no longer valid. */
v->ResetDepotUnbunching();
SetWindowDirty(WC_VEHICLE_TIMETABLE, w->index);
++idx;
}
@@ -382,7 +468,7 @@ CommandCost CmdAutofillTimetable(DoCommandFlag flags, VehicleID veh, bool autofi
*/
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;
@@ -390,6 +476,7 @@ void UpdateVehicleTimetable(Vehicle *v, bool travelling)
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) {
@@ -407,7 +494,7 @@ void UpdateVehicleTimetable(Vehicle *v, bool travelling)
just_started = !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
if (v->timetable_start != 0) {
v->lateness_counter = (_date - v->timetable_start) * DAY_TICKS + _date_fract;
v->lateness_counter = TimerGameTick::counter - v->timetable_start;
v->timetable_start = 0;
}
@@ -432,16 +519,15 @@ void UpdateVehicleTimetable(Vehicle *v, bool travelling)
/* 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.
/* Round up to the smallest unit of time commonly shown in the GUI (seconds) to avoid confusion.
* Players timetabling in Ticks can adjust later.
* 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), DAY_TICKS) * DAY_TICKS;
uint time_to_set = CeilDiv(std::max(time_taken, 1), Ticks::TICKS_PER_SECOND) * Ticks::TICKS_PER_SECOND;
if (travelling && (autofilling || !real_current_order->IsTravelTimetabled())) {
ChangeTimetable(v, v->cur_real_order_index, time_to_set, MTF_TRAVEL_TIME, autofilling);
@@ -460,7 +546,7 @@ void UpdateVehicleTimetable(Vehicle *v, bool travelling)
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
@@ -474,10 +560,10 @@ void UpdateVehicleTimetable(Vehicle *v, bool travelling)
* 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 INVALID_TICKS. */
if (v->lateness_counter > (int)timetabled) {
Ticks cycle = v->orders->GetTimetableTotalDuration();
if (cycle != INVALID_TICKS && v->lateness_counter > cycle) {
* cycle. When the timetable isn't fully filled the cycle will be Ticks::INVALID_TICKS. */
if (v->lateness_counter > timetabled) {
TimerGameTick::Ticks cycle = v->orders->GetTimetableTotalDuration();
if (cycle != Ticks::INVALID_TICKS && v->lateness_counter > cycle) {
v->lateness_counter %= cycle;
}
}