Codechange: [Script] Use helper function over direct casting CompanyID

In the script's API `COMPANY_INVALID` has a value of -1, whereas the internal
game's `INVALID_COMPANY` has a value of 255. Since the script's API also has
a `COMPANY_SPECTATOR` with a value of 255, these enumerations cannot be easily
reconciled by casting. As such, replace all casts in the script API with
either ScriptCompany::FromScriptCompanyID or ScriptCompany::ToScriptCompanyID.

Also make clear whether CompanyID is ::CompanyID or ScriptCompany::CompanyID
by using either one of those over CompanyID in the script's API.
This commit is contained in:
Rubidium
2025-02-08 21:26:58 +01:00
committed by rubidium42
parent 8ab3e9f0a3
commit 4cda9f900d
37 changed files with 201 additions and 184 deletions

View File

@@ -50,11 +50,10 @@ static inline bool StoryPageElementTypeRequiresText(StoryPageElementType type)
EnforceDeityMode(STORY_PAGE_INVALID);
EnforcePrecondition(STORY_PAGE_INVALID, company == ScriptCompany::COMPANY_INVALID || ScriptCompany::ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID);
uint8_t c = company;
if (company == ScriptCompany::COMPANY_INVALID) c = INVALID_COMPANY;
::CompanyID c = ScriptCompany::FromScriptCompanyID(company);
if (!ScriptObject::Command<CMD_CREATE_STORY_PAGE>::Do(&ScriptInstance::DoCommandReturnStoryPageID,
(::CompanyID)c, title != nullptr ? title->GetEncodedText() : std::string{})) return STORY_PAGE_INVALID;
c, title != nullptr ? title->GetEncodedText() : std::string{})) return STORY_PAGE_INVALID;
/* In case of test-mode, we return StoryPageID 0 */
return static_cast<StoryPageID>(0);
@@ -177,10 +176,7 @@ static inline bool StoryPageElementTypeRequiresText(StoryPageElementType type)
{
EnforcePrecondition(ScriptCompany::COMPANY_INVALID, IsValidStoryPage(story_page_id));
CompanyID c = StoryPage::Get(story_page_id)->company;
ScriptCompany::CompanyID company = c == INVALID_COMPANY ? ScriptCompany::COMPANY_INVALID : (ScriptCompany::CompanyID)c;
return company;
return ScriptCompany::ToScriptCompanyID(StoryPage::Get(story_page_id)->company);
}
/* static */ ScriptDate::Date ScriptStoryPage::GetDate(StoryPageID story_page_id)