diff options
-rw-r--r-- | src/ai/api/ai_controller.cpp | 2 | ||||
-rw-r--r-- | src/ai/api/ai_object.cpp | 7 | ||||
-rw-r--r-- | src/ai/api/ai_object.hpp | 11 |
3 files changed, 16 insertions, 4 deletions
diff --git a/src/ai/api/ai_controller.cpp b/src/ai/api/ai_controller.cpp index 88e666db4..6852e1142 100644 --- a/src/ai/api/ai_controller.cpp +++ b/src/ai/api/ai_controller.cpp @@ -30,7 +30,7 @@ /* static */ void AIController::Sleep(int ticks) { - if (!AIObject::GetAllowDoCommand()) { + if (!AIObject::CanSuspend()) { throw AI_FatalError("You are not allowed to call Sleep in your constructor, Save(), Load(), and any valuator."); } diff --git a/src/ai/api/ai_object.cpp b/src/ai/api/ai_object.cpp index 5f9031442..45012c20c 100644 --- a/src/ai/api/ai_object.cpp +++ b/src/ai/api/ai_object.cpp @@ -170,6 +170,11 @@ void AIObject::SetAllowDoCommand(bool allow) bool AIObject::GetAllowDoCommand() { + return GetStorage()->allow_do_command; +} + +bool AIObject::CanSuspend() +{ Squirrel *squirrel = Company::Get(_current_company)->ai_instance->engine; return GetStorage()->allow_do_command && squirrel->CanSuspend(); } @@ -197,7 +202,7 @@ int AIObject::GetCallbackVariable(int index) bool AIObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text, AISuspendCallbackProc *callback) { - if (AIObject::GetAllowDoCommand() == false) { + if (!AIObject::CanSuspend()) { throw AI_FatalError("You are not allowed to execute any DoCommand (even indirect) in your constructor, Save(), Load(), and any valuator."); } diff --git a/src/ai/api/ai_object.hpp b/src/ai/api/ai_object.hpp index e60fc35c5..21404d7e3 100644 --- a/src/ai/api/ai_object.hpp +++ b/src/ai/api/ai_object.hpp @@ -140,8 +140,15 @@ protected: static GroupID GetNewGroupID(); /** - * Get the latest stored allow_do_command. - * If this is false, you are not allowed to do any DoCommands. + * Can we suspend the AI at this moment? + */ + static bool CanSuspend(); + + /** + * Get the internal value of allow_do_command. This can differ + * from CanSuspend() if the reason we are not allowed + * to execute a DoCommand is in squirrel and not the API. + * In that case use this function to restore the previous value. */ static bool GetAllowDoCommand(); |