summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2011-02-20 20:28:49 +0000
committerrubidium <rubidium@openttd.org>2011-02-20 20:28:49 +0000
commite9fe58d7bf7876f41b3deb1fe1295bab8897ed98 (patch)
tree7ed7343f18f89e0c994e764961ed59b2582e27b8 /src
parent7d24d642d96820cbf61b38bcda6efb4b6c6f47a8 (diff)
downloadopenttd-e9fe58d7bf7876f41b3deb1fe1295bab8897ed98.tar.xz
(svn r22120) -Change: [NoAI] Prevent AIs from getting consistently over their allowed amount of operations by subtracting the amount they went over "budget" from the budget for the next "tick".
Diffstat (limited to 'src')
-rw-r--r--src/script/squirrel.cpp13
-rw-r--r--src/script/squirrel.hpp1
2 files changed, 14 insertions, 0 deletions
diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp
index 504f41a64..b9151e727 100644
--- a/src/script/squirrel.cpp
+++ b/src/script/squirrel.cpp
@@ -188,7 +188,19 @@ bool Squirrel::MethodExists(HSQOBJECT instance, const char *method_name)
bool Squirrel::Resume(int suspend)
{
assert(!this->crashed);
+ /* Did we use more operations than we should have in the
+ * previous tick? If so, subtract that from the current run. */
+ if (this->overdrawn_ops > 0 && suspend > 0) {
+ this->overdrawn_ops -= suspend;
+ /* Do we need to wait even more? */
+ if (this->overdrawn_ops >= 0) return true;
+
+ /* We can now only run whatever is "left". */
+ suspend = -this->overdrawn_ops;
+ }
+
this->crashed = !sq_resumecatch(this->vm, suspend);
+ this->overdrawn_ops = -this->vm->_ops_till_suspend;
return this->vm->_suspended != 0;
}
@@ -310,6 +322,7 @@ Squirrel::Squirrel()
this->print_func = NULL;
this->global_pointer = NULL;
this->crashed = false;
+ this->overdrawn_ops = 0;
/* Handle compile-errors ourself, so we can display it nicely */
sq_setcompilererrorhandler(this->vm, &Squirrel::CompileError);
diff --git a/src/script/squirrel.hpp b/src/script/squirrel.hpp
index d50dc356a..1ada519cc 100644
--- a/src/script/squirrel.hpp
+++ b/src/script/squirrel.hpp
@@ -22,6 +22,7 @@ private:
void *global_pointer; ///< Can be set by who ever initializes Squirrel
SQPrintFunc *print_func; ///< Points to either NULL, or a custom print handler
bool crashed; ///< True if the squirrel script made an error.
+ int overdrawn_ops; ///< The amount of operations we have overdrawn.
/**
* The internal RunError handler. It looks up the real error and calls RunError with it.