diff --git a/src/script/api/game_changelog.hpp b/src/script/api/game_changelog.hpp index bdf211cff7..30ac4b7612 100644 --- a/src/script/api/game_changelog.hpp +++ b/src/script/api/game_changelog.hpp @@ -18,6 +18,8 @@ * This version is not yet released. The following changes are not set in stone yet. * * API additions: + * \li GSCompanyMode::IsValid + * \li GSCompanyMode::IsDeity * \li GSTown::ROAD_LAYOUT_RANDOM * \li GSVehicle::IsPrimaryVehicle * diff --git a/src/script/api/script_companymode.cpp b/src/script/api/script_companymode.cpp index a15d6f45d2..ef738dc4ad 100644 --- a/src/script/api/script_companymode.cpp +++ b/src/script/api/script_companymode.cpp @@ -26,3 +26,13 @@ ScriptCompanyMode::~ScriptCompanyMode() { ScriptObject::SetCompany(this->last_company); } + +/* static */ bool ScriptCompanyMode::IsValid() +{ + return ScriptObject::GetCompany() != OWNER_DEITY; +} + +/* static */ bool ScriptCompanyMode::IsDeity() +{ + return ScriptObject::GetCompany() == OWNER_DEITY; +} diff --git a/src/script/api/script_companymode.hpp b/src/script/api/script_companymode.hpp index c64fdcb08a..6c2e13530f 100644 --- a/src/script/api/script_companymode.hpp +++ b/src/script/api/script_companymode.hpp @@ -47,6 +47,22 @@ public: * in when the instance was created. */ ~ScriptCompanyMode(); + + /** + * Check whether a company mode is valid. In other words, are commands + * being executed under some company. + * @return true When a company mode is valid. + * @post !ScriptCompanyMode::IsDeity(). + */ + static bool IsValid(); + + /** + * Check whether the company mode is not active, i.e. whether we are a deity. + * In other words, are commands are not being executed under some company. + * @return true When we are a deity, i.e. company mode is not active. + * @post !ScriptCompanyMode::IsValid(). + */ + static bool IsDeity(); }; #endif /* SCRIPT_COMPANYMODE_HPP */ diff --git a/src/script/api/script_error.hpp b/src/script/api/script_error.hpp index f4b3832ef5..e053350188 100644 --- a/src/script/api/script_error.hpp +++ b/src/script/api/script_error.hpp @@ -11,6 +11,7 @@ #define SCRIPT_ERROR_HPP #include "script_object.hpp" +#include "script_companymode.hpp" #include /** @@ -47,6 +48,20 @@ return returnval; \ } +/** + * Helper to enforce the precondition that the company mode is valid. + * @param returnval The value to return on failure. + */ +#define EnforceCompanyModeValid(returnval) \ + EnforcePrecondition(returnval, ScriptCompanyMode::IsValid()) + +/** + * Helper to enforce the precondition that we are in a deity mode. + * @param returnval The value to return on failure. + */ +#define EnforceDeityMode(returnval) \ + EnforcePrecondition(returnval, ScriptCompanyMode::IsDeity()) + /** * Class that handles all error related functions. * @api ai game