diff --git a/regression/regression/main.nut b/regression/regression/main.nut index fce258a739..d20b2bb298 100644 --- a/regression/regression/main.nut +++ b/regression/regression/main.nut @@ -2035,6 +2035,13 @@ function Regression::Start() print(" VehicleID: " + c.GetVehicleID()); } break; + case AIEvent.ET_COMPANY_RENAMED: { + local c = AIEventCompanyRenamed.Convert(e); + print(" EventName: CompanyRenamed"); + print(" CompanyID: " + c.GetCompanyID()); + print(" CompanyName: " + c.GetNewName()); + } break; + default: print(" Unknown Event"); break; diff --git a/regression/regression/result.txt b/regression/regression/result.txt index 27a81675a9..9c14a3bc57 100644 --- a/regression/regression/result.txt +++ b/regression/regression/result.txt @@ -9711,6 +9711,16 @@ ERROR: IsEnd() is invalid as Begin() is never called GetDestinationType(): 1 GetDestinationIndex(): 7 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 --Math-- @@ -9748,9 +9758,9 @@ ERROR: IsEnd() is invalid as Begin() is never called --Valuate() with excessive CPU usage-- 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 [Start()] regression/main.nut line [2052] +*FUNCTION [Start()] regression/main.nut line [2059] [id] 0 [this] TABLE @@ -9759,7 +9769,7 @@ Your script made an error: excessive CPU usage in valuator function [this] INSTANCE 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 [list] INSTANCE diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index 1a6d96583c..48bf2afbc7 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -424,6 +424,8 @@ set_name:; c->name_2 = strp; MarkWholeScreenDirty(); + AI::BroadcastNewEvent(new ScriptEventCompanyRenamed(c->index, name)); + Game::NewEvent(new ScriptEventCompanyRenamed(c->index, name)); if (c->is_ai) { auto cni = std::make_unique(c); @@ -1184,6 +1186,11 @@ CommandCost CmdRenameCompany(DoCommandFlag flags, const std::string &text) } MarkWholeScreenDirty(); 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(); diff --git a/src/script/api/ai_changelog.hpp b/src/script/api/ai_changelog.hpp index b07b85c749..0992a6e080 100644 --- a/src/script/api/ai_changelog.hpp +++ b/src/script/api/ai_changelog.hpp @@ -19,6 +19,7 @@ * * API additions: * \li AIEventVehicleCrashed::GetVictims + * \li AIEventCompanyRenamed * * \b 14.0 * diff --git a/src/script/api/game_changelog.hpp b/src/script/api/game_changelog.hpp index 3967726288..bbd1f876a0 100644 --- a/src/script/api/game_changelog.hpp +++ b/src/script/api/game_changelog.hpp @@ -19,6 +19,7 @@ * * API additions: * \li GSEventVehicleCrashed::GetVictims + * \li GSEventCompanyRenamed * * \b 14.0 * diff --git a/src/script/api/script_event.hpp b/src/script/api/script_event.hpp index 2ff813aa81..4758ac2e03 100644 --- a/src/script/api/script_event.hpp +++ b/src/script/api/script_event.hpp @@ -57,6 +57,7 @@ public: ET_STORYPAGE_BUTTON_CLICK, ET_STORYPAGE_TILE_SELECT, ET_STORYPAGE_VEHICLE_SELECT, + ET_COMPANY_RENAMED, }; /** diff --git a/src/script/api/script_event_types.hpp b/src/script/api/script_event_types.hpp index 776a31ff8d..c8403f5ec6 100644 --- a/src/script/api/script_event_types.hpp +++ b/src/script/api/script_event_types.hpp @@ -346,6 +346,48 @@ private: 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(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(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 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 * bankrupt soon.