Add: [Script] ScriptEventCompanyRename (#12878)

This commit is contained in:
Björn Wärmedal 2025-01-14 10:24:28 +01:00 committed by GitHub
parent 3a7cfafe51
commit 9ab936f76b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 72 additions and 3 deletions

View File

@ -2035,6 +2035,13 @@ function Regression::Start()
print(" VehicleID: " + c.GetVehicleID()); print(" VehicleID: " + c.GetVehicleID());
} break; } break;
case AIEvent.ET_COMPANY_RENAMED: {
local c = AIEventCompanyRenamed.Convert(e);
print(" EventName: CompanyRenamed");
print(" CompanyID: " + c.GetCompanyID());
print(" CompanyName: " + c.GetNewName());
} break;
default: default:
print(" Unknown Event"); print(" Unknown Event");
break; break;

View File

@ -9711,6 +9711,16 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetDestinationType(): 1 GetDestinationType(): 1
GetDestinationIndex(): 7 GetDestinationIndex(): 7
GetCargoType(): 0 GetCargoType(): 0
GetNextEvent: instance
GetEventType: 33
EventName: CompanyRenamed
CompanyID: 1
CompanyName: Regression
GetNextEvent: instance
GetEventType: 33
EventName: CompanyRenamed
CompanyID: 1
CompanyName: Little Frutford Transport
IsEventWaiting: false IsEventWaiting: false
--Math-- --Math--
@ -9748,9 +9758,9 @@ ERROR: IsEnd() is invalid as Begin() is never called
--Valuate() with excessive CPU usage-- --Valuate() with excessive CPU usage--
Your script made an error: excessive CPU usage in valuator function Your script made an error: excessive CPU usage in valuator function
*FUNCTION [unknown()] regression/main.nut line [2051] *FUNCTION [unknown()] regression/main.nut line [2058]
*FUNCTION [Valuate()] NATIVE line [-1] *FUNCTION [Valuate()] NATIVE line [-1]
*FUNCTION [Start()] regression/main.nut line [2052] *FUNCTION [Start()] regression/main.nut line [2059]
[id] 0 [id] 0
[this] TABLE [this] TABLE
@ -9759,7 +9769,7 @@ Your script made an error: excessive CPU usage in valuator function
[this] INSTANCE [this] INSTANCE
Your script made an error: excessive CPU usage in valuator function Your script made an error: excessive CPU usage in valuator function
*FUNCTION [Start()] regression/main.nut line [2052] *FUNCTION [Start()] regression/main.nut line [2059]
[Infinite] CLOSURE [Infinite] CLOSURE
[list] INSTANCE [list] INSTANCE

View File

@ -424,6 +424,8 @@ set_name:;
c->name_2 = strp; c->name_2 = strp;
MarkWholeScreenDirty(); MarkWholeScreenDirty();
AI::BroadcastNewEvent(new ScriptEventCompanyRenamed(c->index, name));
Game::NewEvent(new ScriptEventCompanyRenamed(c->index, name));
if (c->is_ai) { if (c->is_ai) {
auto cni = std::make_unique<CompanyNewsInformation>(c); auto cni = std::make_unique<CompanyNewsInformation>(c);
@ -1184,6 +1186,11 @@ CommandCost CmdRenameCompany(DoCommandFlag flags, const std::string &text)
} }
MarkWholeScreenDirty(); MarkWholeScreenDirty();
CompanyAdminUpdate(c); CompanyAdminUpdate(c);
SetDParam(0, c->index);
std::string new_name = GetString(STR_COMPANY_NAME);
AI::BroadcastNewEvent(new ScriptEventCompanyRenamed(c->index, new_name));
Game::NewEvent(new ScriptEventCompanyRenamed(c->index, new_name));
} }
return CommandCost(); return CommandCost();

View File

@ -19,6 +19,7 @@
* *
* API additions: * API additions:
* \li AIEventVehicleCrashed::GetVictims * \li AIEventVehicleCrashed::GetVictims
* \li AIEventCompanyRenamed
* *
* \b 14.0 * \b 14.0
* *

View File

@ -19,6 +19,7 @@
* *
* API additions: * API additions:
* \li GSEventVehicleCrashed::GetVictims * \li GSEventVehicleCrashed::GetVictims
* \li GSEventCompanyRenamed
* *
* \b 14.0 * \b 14.0
* *

View File

@ -57,6 +57,7 @@ public:
ET_STORYPAGE_BUTTON_CLICK, ET_STORYPAGE_BUTTON_CLICK,
ET_STORYPAGE_TILE_SELECT, ET_STORYPAGE_TILE_SELECT,
ET_STORYPAGE_VEHICLE_SELECT, ET_STORYPAGE_VEHICLE_SELECT,
ET_COMPANY_RENAMED,
}; };
/** /**

View File

@ -346,6 +346,48 @@ private:
ScriptCompany::CompanyID owner; ///< The new company. ScriptCompany::CompanyID owner; ///< The new company.
}; };
/**
* Event Company Renamed, indicating a company has changed name.
* @api ai game
*/
class ScriptEventCompanyRenamed : public ScriptEvent {
public:
#ifndef DOXYGEN_API
/**
* @param owner The company that is renamed.
*/
ScriptEventCompanyRenamed(CompanyID company, const std::string &new_name) :
ScriptEvent(ET_COMPANY_RENAMED),
company(static_cast<ScriptCompany::CompanyID>(company)),
new_name(new_name)
{}
#endif /* DOXYGEN_API */
/**
* Convert an ScriptEvent to the real instance.
* @param instance The instance to convert.
* @return The converted instance.
*/
static ScriptEventCompanyRenamed *Convert(ScriptEvent *instance) { return static_cast<ScriptEventCompanyRenamed *>(instance); }
/**
* Get the CompanyID of the company that has been renamed.
* @return The CompanyID of the company.
*/
ScriptCompany::CompanyID GetCompanyID() { return this->company; }
/**
* Get the new name of the company.
* @return The new name of the company.
*/
std::optional<std::string> GetNewName() { return this->new_name; }
private:
ScriptCompany::CompanyID company; ///< The company that was renamed.
std::string new_name; ///< The new name of the company.
};
/** /**
* Event Company In Trouble, indicating a company is in trouble and might go * Event Company In Trouble, indicating a company is in trouble and might go
* bankrupt soon. * bankrupt soon.