From 7425bf779d8bd989d51f3844c5e70bbe352962cc Mon Sep 17 00:00:00 2001 From: yexo Date: Mon, 25 May 2009 21:55:47 +0000 Subject: (svn r16425) -Change [FS#2869]: Stop an AI when it takes too long to initialize or load --- src/ai/ai_instance.cpp | 14 +++++++++++--- src/script/squirrel.cpp | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/ai/ai_instance.cpp b/src/ai/ai_instance.cpp index 0ab577a0e..2edb7db42 100644 --- a/src/ai/ai_instance.cpp +++ b/src/ai/ai_instance.cpp @@ -304,9 +304,17 @@ void AIInstance::GameLoop() AIObject::SetAllowDoCommand(false); /* Run the constructor if it exists. Don't allow any DoCommands in it. */ if (this->engine->MethodExists(*this->instance, "constructor")) { - if (!this->engine->CallMethod(*this->instance, "constructor")) { this->Died(); return; } + if (!this->engine->CallMethod(*this->instance, "constructor", 100000) || this->engine->IsSuspended()) { + if (this->engine->IsSuspended()) AILog::Error("This AI took too long to initialize. AI is not started."); + this->Died(); + return; + } + } + if (!this->CallLoad() || this->engine->IsSuspended()) { + if (this->engine->IsSuspended()) AILog::Error("This AI took too long in the Load function. AI is not started."); + this->Died(); + return; } - if (!this->CallLoad()) { this->Died(); return; } AIObject::SetAllowDoCommand(true); /* Start the AI by calling Start() */ if (!this->engine->CallMethod(*this->instance, "Start", _settings_game.ai.ai_max_opcode_till_suspend) || !this->engine->IsSuspended()) this->Died(); @@ -702,7 +710,7 @@ bool AIInstance::CallLoad() /* Call the AI load function. sq_call removes the arguments (but not the * function pointer) from the stack. */ - if (SQ_FAILED(sq_call(vm, 3, SQFalse, SQFalse))) return false; + if (SQ_FAILED(sq_call(vm, 3, SQFalse, SQFalse, 100000))) return false; /* Pop 1) The version, 2) the savegame data, 3) the object instance, 4) the function pointer. */ sq_pop(vm, 4); diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp index f31b813da..6737fd3ef 100644 --- a/src/script/squirrel.cpp +++ b/src/script/squirrel.cpp @@ -209,7 +209,7 @@ bool Squirrel::CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT if (ret != NULL) sq_getstackobj(vm, -1, ret); /* Reset the top, but don't do so for the AI main function, as we need * a correct stack when resuming. */ - if (suspend == -1) sq_settop(this->vm, top); + if (!this->IsSuspended()) sq_settop(this->vm, top); /* Restore the return-value location. */ this->vm->_suspended_target = last_target; -- cgit v1.2.3-54-g00ecf