| 183 | } |
| 184 | |
| 185 | void ScriptInstance::GameLoop() |
| 186 | { |
| 187 | ScriptObject::ActiveInstance active(*this); |
| 188 | |
| 189 | if (this->IsDead()) return; |
| 190 | if (this->engine->HasScriptCrashed()) { |
| 191 | /* The script crashed during saving, kill it here. */ |
| 192 | this->Died(); |
| 193 | return; |
| 194 | } |
| 195 | if (this->is_paused) return; |
| 196 | this->controller->ticks++; |
| 197 | |
| 198 | if (this->suspend < -1) this->suspend++; // Multiplayer suspend, increase up to -1. |
| 199 | if (this->suspend < 0) return; // Multiplayer suspend, wait for Continue(). |
| 200 | if (--this->suspend > 0) return; // Singleplayer suspend, decrease to 0. |
| 201 | |
| 202 | _current_company = ScriptObject::GetCompany(); |
| 203 | |
| 204 | /* If there is a callback to call, call that first */ |
| 205 | if (this->callback != nullptr) { |
| 206 | if (this->is_save_data_on_stack) { |
| 207 | sq_poptop(this->engine->GetVM()); |
| 208 | this->is_save_data_on_stack = false; |
| 209 | } |
| 210 | try { |
| 211 | this->callback(*this); |
| 212 | } catch (Script_Suspend &e) { |
| 213 | this->suspend = e.GetSuspendTime(); |
| 214 | this->callback = e.GetSuspendCallback(); |
| 215 | |
| 216 | return; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | this->suspend = 0; |
| 221 | this->callback = nullptr; |
| 222 | |
| 223 | if (!this->is_started) { |
| 224 | try { |
| 225 | { |
| 226 | ScriptObject::DisableDoCommandScope disabler{}; |
| 227 | /* Run the constructor if it exists. Don't allow any DoCommands in it. */ |
| 228 | if (this->engine->MethodExists(*this->instance, "constructor")) { |
| 229 | if (!this->engine->CallMethod(*this->instance, "constructor", MAX_CONSTRUCTOR_OPS) || this->engine->IsSuspended()) { |
| 230 | if (this->engine->IsSuspended()) ScriptLog::Error("This script took too long to initialize. Script is not started."); |
| 231 | this->Died(); |
| 232 | return; |
| 233 | } |
| 234 | } |
| 235 | if (!this->CallLoad() || this->engine->IsSuspended()) { |
| 236 | if (this->engine->IsSuspended()) ScriptLog::Error("This script took too long in the Load function. Script is not started."); |
| 237 | this->Died(); |
| 238 | return; |
| 239 | } |
| 240 | } |
| 241 | /* Start the script by calling Start() */ |
| 242 | if (!this->engine->CallMethod(*this->instance, "Start", _settings_game.script.script_max_opcode_till_suspend) || !this->engine->IsSuspended()) this->Died(); |
nothing calls this directly
no test coverage detected