1
0
Fork 0

Compare commits

...

3 Commits

Author SHA1 Message Date
Peter Nelson 467167f5bb
Merge 7bbe233377 into 7bb4940ebd 2025-07-19 08:31:41 +00:00
Peter Nelson 7bb4940ebd
Codechange: Use unique_ptr for all pointers in script instance. (#14339)
Removes manual memory management with new/delete.
2025-07-19 09:29:30 +01:00
Tabytac 7bbe233377
Feature: Add setting to disable vehicle intro date randomisation 2025-04-20 23:35:00 +01:00
9 changed files with 40 additions and 27 deletions

View File

@ -83,7 +83,7 @@ void AIInstance::Died()
void AIInstance::LoadDummyScript() void AIInstance::LoadDummyScript()
{ {
ScriptAllocatorScope alloc_scope(this->engine); ScriptAllocatorScope alloc_scope(this->engine.get());
Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI"); Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
} }

View File

@ -737,7 +737,12 @@ void StartupOneEngine(Engine *e, const TimerGameCalendar::YearMonthDay &aging_ym
/* Don't randomise the start-date in the first two years after gamestart to ensure availability /* Don't randomise the start-date in the first two years after gamestart to ensure availability
* of engines in early starting games. * of engines in early starting games.
* Note: TTDP uses fixed 1922 */ * Note: TTDP uses fixed 1922 */
e->intro_date = ei->base_intro <= TimerGameCalendar::ConvertYMDToDate(_settings_game.game_creation.starting_year + 2, 0, 1) ? ei->base_intro : (TimerGameCalendar::Date)GB(r, 0, 9) + ei->base_intro; TimerGameCalendar::Date begin_random_date = TimerGameCalendar::ConvertYMDToDate(_settings_game.game_creation.starting_year + 2, 0, 1);
if (_settings_game.vehicle.vehicle_intro_randomisation && ei->base_intro > begin_random_date) {
e->intro_date = ei->base_intro + GB(r, 0, 9);
} else {
e->intro_date = ei->base_intro;
}
if (e->intro_date <= TimerGameCalendar::date) { if (e->intro_date <= TimerGameCalendar::date) {
TimerGameCalendar::YearMonthDay intro_ymd = TimerGameCalendar::ConvertDateToYMD(e->intro_date); TimerGameCalendar::YearMonthDay intro_ymd = TimerGameCalendar::ConvertDateToYMD(e->intro_date);
int aging_months = aging_ymd.year.base() * 12 + aging_ymd.month; int aging_months = aging_ymd.year.base() * 12 + aging_ymd.month;

View File

@ -1521,6 +1521,9 @@ STR_CONFIG_SETTING_WARN_OLD_VEHICLE_HELPTEXT :When enabled, a
STR_CONFIG_SETTING_NEVER_EXPIRE_VEHICLES :Vehicles never expire: {STRING2} STR_CONFIG_SETTING_NEVER_EXPIRE_VEHICLES :Vehicles never expire: {STRING2}
STR_CONFIG_SETTING_NEVER_EXPIRE_VEHICLES_HELPTEXT :When enabled, all vehicle models remain available forever after their introduction STR_CONFIG_SETTING_NEVER_EXPIRE_VEHICLES_HELPTEXT :When enabled, all vehicle models remain available forever after their introduction
STR_CONFIG_SETTING_VEHICLE_INTRO_RANDOMISATION :Randomise vehicle introduction dates: {STRING2}
STR_CONFIG_SETTING_VEHICLE_INTRO_RANDOMISATION_HELPTEXT :When enabled, randomises vehicle introduction dates by up to one and a half years
STR_CONFIG_SETTING_TIMEKEEPING_UNITS :Timekeeping: {STRING2} STR_CONFIG_SETTING_TIMEKEEPING_UNITS :Timekeeping: {STRING2}
STR_CONFIG_SETTING_TIMEKEEPING_UNITS_HELPTEXT :Select the timekeeping units of the game. This cannot be changed later.{}{}Calendar-based is the classic OpenTTD experience, with a year consisting of 12 months, and each month having 28-31 days.{}{}In Wallclock-based time, cargo production and financials are instead based on one-minute increments, which is about as long as a 30 day month takes in Calendar-based mode. These are grouped into 12-minute periods, equivalent to a year in Calendar-based mode.{}{}In either mode there is always a classic calendar, which is used for introduction dates of vehicles, houses, and other infrastructure STR_CONFIG_SETTING_TIMEKEEPING_UNITS_HELPTEXT :Select the timekeeping units of the game. This cannot be changed later.{}{}Calendar-based is the classic OpenTTD experience, with a year consisting of 12 months, and each month having 28-31 days.{}{}In Wallclock-based time, cargo production and financials are instead based on one-minute increments, which is about as long as a 30 day month takes in Calendar-based mode. These are grouped into 12-minute periods, equivalent to a year in Calendar-based mode.{}{}In either mode there is always a classic calendar, which is used for introduction dates of vehicles, houses, and other infrastructure
###length 2 ###length 2

View File

@ -53,7 +53,7 @@ static ScriptStorage &GetStorage()
/* static */ ScriptInstance *ScriptObject::ActiveInstance::active = nullptr; /* static */ ScriptInstance *ScriptObject::ActiveInstance::active = nullptr;
ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance &instance) : alc_scope(instance.engine) ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance &instance) : alc_scope(instance.engine.get())
{ {
this->last_active = ScriptObject::ActiveInstance::active; this->last_active = ScriptObject::ActiveInstance::active;
ScriptObject::ActiveInstance::active = &instance; ScriptObject::ActiveInstance::active = &instance;
@ -230,8 +230,8 @@ ScriptObject::DisableDoCommandScope::DisableDoCommandScope()
/* static */ bool ScriptObject::CanSuspend() /* static */ bool ScriptObject::CanSuspend()
{ {
Squirrel *squirrel = ScriptObject::GetActiveInstance().engine; Squirrel &squirrel = *ScriptObject::GetActiveInstance().engine;
return GetStorage().allow_do_command && squirrel->CanSuspend(); return GetStorage().allow_do_command && squirrel.CanSuspend();
} }
/* static */ ScriptEventQueue &ScriptObject::GetEventQueue() /* static */ ScriptEventQueue &ScriptObject::GetEventQueue()

View File

@ -50,8 +50,8 @@ static void PrintFunc(bool error_msg, std::string_view message)
ScriptInstance::ScriptInstance(std::string_view api_name) ScriptInstance::ScriptInstance(std::string_view api_name)
{ {
this->storage = new ScriptStorage(); this->storage = std::make_unique<ScriptStorage>();
this->engine = new Squirrel(api_name); this->engine = std::make_unique<Squirrel>(api_name);
this->engine->SetPrintFunction(&PrintFunc); this->engine->SetPrintFunction(&PrintFunc);
} }
@ -59,10 +59,10 @@ void ScriptInstance::Initialize(const std::string &main_script, const std::strin
{ {
ScriptObject::ActiveInstance active(*this); ScriptObject::ActiveInstance active(*this);
this->controller = new ScriptController(company); this->controller = std::make_unique<ScriptController>(company);
/* Register the API functions and classes */ /* Register the API functions and classes */
this->engine->SetGlobalPointer(this->engine); this->engine->SetGlobalPointer(this->engine.get());
this->RegisterAPI(); this->RegisterAPI();
if (this->IsDead()) { if (this->IsDead()) {
/* Failed to register API; a message has already been logged. */ /* Failed to register API; a message has already been logged. */
@ -81,12 +81,11 @@ void ScriptInstance::Initialize(const std::string &main_script, const std::strin
} }
/* Create the main-class */ /* Create the main-class */
this->instance = new SQObject(); this->instance = std::make_unique<SQObject>();
if (!this->engine->CreateClassInstance(instance_name, this->controller, this->instance)) { if (!this->engine->CreateClassInstance(instance_name, this->controller.get(), this->instance.get())) {
/* If CreateClassInstance has returned false instance has not been /* If CreateClassInstance has returned false instance has not been
* registered with squirrel, so avoid trying to Release it by clearing it now */ * registered with squirrel, so avoid trying to Release it by clearing it now */
delete this->instance; this->instance.reset();
this->instance = nullptr;
this->Died(); this->Died();
return; return;
} }
@ -158,11 +157,10 @@ ScriptInstance::~ScriptInstance()
ScriptObject::ActiveInstance active(*this); ScriptObject::ActiveInstance active(*this);
this->in_shutdown = true; this->in_shutdown = true;
if (instance != nullptr) this->engine->ReleaseObject(this->instance); if (instance != nullptr) this->engine->ReleaseObject(this->instance.get());
if (engine != nullptr) delete this->engine;
delete this->storage; /* Engine must be reset explicitly in scope of the active instance. */
delete this->controller; this->engine.reset();
delete this->instance;
} }
void ScriptInstance::Continue() void ScriptInstance::Continue()
@ -179,11 +177,9 @@ void ScriptInstance::Died()
this->last_allocated_memory = this->GetAllocatedMemory(); // Update cache this->last_allocated_memory = this->GetAllocatedMemory(); // Update cache
if (this->instance != nullptr) this->engine->ReleaseObject(this->instance); if (this->instance != nullptr) this->engine->ReleaseObject(this->instance.get());
delete this->instance; this->engine.reset();
delete this->engine; this->instance.reset();
this->instance = nullptr;
this->engine = nullptr;
} }
void ScriptInstance::GameLoop() void ScriptInstance::GameLoop()

View File

@ -256,7 +256,7 @@ public:
void ReleaseSQObject(HSQOBJECT *obj); void ReleaseSQObject(HSQOBJECT *obj);
protected: protected:
class Squirrel *engine = nullptr; ///< A wrapper around the squirrel vm. std::unique_ptr<class Squirrel> engine; ///< A wrapper around the squirrel vm.
std::string api_version{}; ///< Current API used by this script. std::string api_version{}; ///< Current API used by this script.
/** /**
@ -288,9 +288,9 @@ protected:
virtual void LoadDummyScript() = 0; virtual void LoadDummyScript() = 0;
private: private:
class ScriptController *controller = nullptr; ///< The script main class. std::unique_ptr<class ScriptStorage> storage; ///< Some global information for each running script.
class ScriptStorage *storage = nullptr; ///< Some global information for each running script. std::unique_ptr<class ScriptController> controller; ///< The script main class.
SQObject *instance = nullptr; ///< Squirrel-pointer to the script main class. std::unique_ptr<SQObject> instance; ///< Squirrel-pointer to the script main class.
bool is_started = false; ///< Is the scripts constructor executed? bool is_started = false; ///< Is the scripts constructor executed?
bool is_dead = false; ///< True if the script has been stopped. bool is_dead = false; ///< True if the script has been stopped.

View File

@ -797,6 +797,8 @@ SettingsContainer &GetSettingsTree()
orders->Add(new SettingEntry("gui.quick_goto")); orders->Add(new SettingEntry("gui.quick_goto"));
orders->Add(new SettingEntry("gui.stop_location")); orders->Add(new SettingEntry("gui.stop_location"));
} }
vehicles->Add(new SettingEntry("vehicle.vehicle_intro_randomisation"));
} }
SettingsPage *limitations = main->Add(new SettingsPage(STR_CONFIG_SETTING_LIMITATIONS)); SettingsPage *limitations = main->Add(new SettingsPage(STR_CONFIG_SETTING_LIMITATIONS));

View File

@ -511,6 +511,7 @@ struct VehicleSettings {
uint8_t freight_trains; ///< value to multiply the weight of cargo by uint8_t freight_trains; ///< value to multiply the weight of cargo by
bool dynamic_engines; ///< enable dynamic allocation of engine data bool dynamic_engines; ///< enable dynamic allocation of engine data
bool never_expire_vehicles; ///< never expire vehicles bool never_expire_vehicles; ///< never expire vehicles
bool vehicle_intro_randomisation; ///< randomise the introduction dates of vehicles
uint8_t extend_vehicle_life; ///< extend vehicle life by this many years uint8_t extend_vehicle_life; ///< extend vehicle life by this many years
uint8_t road_side; ///< the side of the road vehicles drive on uint8_t road_side; ///< the side of the road vehicles drive on
uint8_t plane_crashes; ///< number of plane crashes, 0 = none, 1 = reduced, 2 = normal uint8_t plane_crashes; ///< number of plane crashes, 0 = none, 1 = reduced, 2 = normal

View File

@ -241,6 +241,12 @@ def = false
str = STR_CONFIG_SETTING_NEVER_EXPIRE_VEHICLES str = STR_CONFIG_SETTING_NEVER_EXPIRE_VEHICLES
strhelp = STR_CONFIG_SETTING_NEVER_EXPIRE_VEHICLES_HELPTEXT strhelp = STR_CONFIG_SETTING_NEVER_EXPIRE_VEHICLES_HELPTEXT
[SDT_BOOL]
var = vehicle.vehicle_intro_randomisation
def = true
str = STR_CONFIG_SETTING_VEHICLE_INTRO_RANDOMISATION
strhelp = STR_CONFIG_SETTING_VEHICLE_INTRO_RANDOMISATION_HELPTEXT
[SDT_VAR] [SDT_VAR]
var = vehicle.max_trains var = vehicle.max_trains
type = SLE_UINT16 type = SLE_UINT16