summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2011-11-04 23:20:14 +0000
committerrubidium <rubidium@openttd.org>2011-11-04 23:20:14 +0000
commit65d0d19b1616e5867afdf9d9f1c68972d2c3f2b0 (patch)
tree75d5607cc3393395003137b92c580a4bf8dae3f2
parent02913f40b0ab1a7771e96e4c6af4ef15b6953cc7 (diff)
downloadopenttd-65d0d19b1616e5867afdf9d9f1c68972d2c3f2b0.tar.xz
(svn r23118) -Feature: [NoAI] Allow AIs to query the amount of remaining operations for the current tick
-rw-r--r--bin/ai/regression/regression.nut4
-rw-r--r--bin/ai/regression/regression.txt3
-rw-r--r--src/ai/ai_instance.cpp5
-rw-r--r--src/ai/ai_instance.hpp7
-rw-r--r--src/ai/api/ai_changelog.hpp1
-rw-r--r--src/ai/api/ai_controller.cpp5
-rw-r--r--src/ai/api/ai_controller.hpp10
-rw-r--r--src/ai/api/ai_controller.hpp.sq13
-rw-r--r--src/script/squirrel.cpp5
-rw-r--r--src/script/squirrel.hpp5
10 files changed, 52 insertions, 6 deletions
diff --git a/bin/ai/regression/regression.nut b/bin/ai/regression/regression.nut
index 05243346a..341c5d4ce 100644
--- a/bin/ai/regression/regression.nut
+++ b/bin/ai/regression/regression.nut
@@ -10,9 +10,11 @@ function Regression::TestInit()
{
print("");
print("--TestInit--");
+ print(" Ops: " + this.GetOpsTillSuspend());
print(" TickTest: " + this.GetTick());
this.Sleep(1);
print(" TickTest: " + this.GetTick());
+ print(" Ops: " + this.GetOpsTillSuspend());
print(" SetCommandDelay: " + AIController.SetCommandDelay(1));
print(" IsValid(vehicle.plane_speed): " + AIGameSettings.IsValid("vehicle.plane_speed"));
print(" vehicle.plane_speed: " + AIGameSettings.GetValue("vehicle.plane_speed"));
@@ -166,6 +168,8 @@ function Regression::TestInit()
foreach (idx, val in list) {
print(" " + idx);
}
+
+ print(" Ops: " + this.GetOpsTillSuspend());
}
function Regression::Std()
diff --git a/bin/ai/regression/regression.txt b/bin/ai/regression/regression.txt
index 3eb611eaf..dc82f9093 100644
--- a/bin/ai/regression/regression.txt
+++ b/bin/ai/regression/regression.txt
@@ -1,7 +1,9 @@
--TestInit--
+ Ops: 9988
TickTest: 1
TickTest: 2
+ Ops: 9990
SetCommandDelay: (null : 0x00000000)
IsValid(vehicle.plane_speed): true
vehicle.plane_speed: 2
@@ -79,6 +81,7 @@
20
30
40
+ Ops: 8673
--Std--
abs(-21): 21
diff --git a/src/ai/ai_instance.cpp b/src/ai/ai_instance.cpp
index d08c36f63..13180b53f 100644
--- a/src/ai/ai_instance.cpp
+++ b/src/ai/ai_instance.cpp
@@ -789,3 +789,8 @@ bool AIInstance::CallLoad()
sq_pop(vm, 4);
return true;
}
+
+SQInteger AIInstance::GetOpsTillSuspend()
+{
+ return this->engine->GetOpsTillSuspend();
+}
diff --git a/src/ai/ai_instance.hpp b/src/ai/ai_instance.hpp
index 718531feb..d66854563 100644
--- a/src/ai/ai_instance.hpp
+++ b/src/ai/ai_instance.hpp
@@ -172,6 +172,13 @@ public:
* call from within a function called by the AI.
*/
void Suspend();
+
+ /**
+ * Get the number of operations the AI can execute before being suspended.
+ * This function is safe to call from within a function called by the AI.
+ * @return The number of operations to execute.
+ */
+ SQInteger GetOpsTillSuspend();
private:
class AIController *controller; ///< The AI main class.
class AIStorage *storage; ///< Some global information for each running AI.
diff --git a/src/ai/api/ai_changelog.hpp b/src/ai/api/ai_changelog.hpp
index 67875146e..30c524ffa 100644
--- a/src/ai/api/ai_changelog.hpp
+++ b/src/ai/api/ai_changelog.hpp
@@ -26,6 +26,7 @@
* \li AICompany::GetQuarterlyCargoDelivered
* \li AICompany::GetQuarterlyPerformanceRating
* \li AICompany::GetQuarterlyCompanyValue
+ * \li AIController::GetOpsTillSuspend
* \li AITown::GetTownAuthority
* \li AIVehicle::ERR_VEHICLE_TOO_LONG in case vehicle length limit is reached
*
diff --git a/src/ai/api/ai_controller.cpp b/src/ai/api/ai_controller.cpp
index 9a2c552c0..c6130303d 100644
--- a/src/ai/api/ai_controller.cpp
+++ b/src/ai/api/ai_controller.cpp
@@ -66,6 +66,11 @@ AIController::~AIController()
return ::Company::Get(_current_company)->ai_instance->GetController()->ticks;
}
+/* static */ int AIController::GetOpsTillSuspend()
+{
+ return ::Company::Get(_current_company)->ai_instance->GetOpsTillSuspend();
+}
+
/* static */ int AIController::GetSetting(const char *name)
{
return AIConfig::GetConfig(_current_company)->GetSetting(name);
diff --git a/src/ai/api/ai_controller.hpp b/src/ai/api/ai_controller.hpp
index 2ab7c245b..ce74d39c9 100644
--- a/src/ai/api/ai_controller.hpp
+++ b/src/ai/api/ai_controller.hpp
@@ -52,6 +52,16 @@ public:
static uint GetTick();
/**
+ * Get the number of operations the AI may still execute this tick.
+ * @return The amount of operations left to execute.
+ * @note This number can go negative when certain uninteruptable
+ * operations are executed. The amount of operations that you go
+ * over the limit will be deducted from the next tick you would
+ * be allowed to run.
+ */
+ static int GetOpsTillSuspend();
+
+ /**
* Get the value of one of your settings you set via info.nut.
* @param name The name of the setting.
* @return the value for the setting, or -1 if the setting is not known.
diff --git a/src/ai/api/ai_controller.hpp.sq b/src/ai/api/ai_controller.hpp.sq
index ec8ddca83..fdfb47788 100644
--- a/src/ai/api/ai_controller.hpp.sq
+++ b/src/ai/api/ai_controller.hpp.sq
@@ -13,11 +13,12 @@ void SQAIController_Register(Squirrel *engine)
{
DefSQClass <AIController> SQAIController("AIController");
SQAIController.PreRegister(engine);
- SQAIController.DefSQStaticMethod(engine, &AIController::GetTick, "GetTick", 1, ".");
- SQAIController.DefSQStaticMethod(engine, &AIController::SetCommandDelay, "SetCommandDelay", 2, ".i");
- SQAIController.DefSQStaticMethod(engine, &AIController::Sleep, "Sleep", 2, ".i");
- SQAIController.DefSQStaticMethod(engine, &AIController::GetSetting, "GetSetting", 2, ".s");
- SQAIController.DefSQStaticMethod(engine, &AIController::GetVersion, "GetVersion", 1, ".");
- SQAIController.DefSQStaticMethod(engine, &AIController::Print, "Print", 3, ".bs");
+ SQAIController.DefSQStaticMethod(engine, &AIController::GetTick, "GetTick", 1, ".");
+ SQAIController.DefSQStaticMethod(engine, &AIController::GetOpsTillSuspend, "GetOpsTillSuspend", 1, ".");
+ SQAIController.DefSQStaticMethod(engine, &AIController::SetCommandDelay, "SetCommandDelay", 2, ".i");
+ SQAIController.DefSQStaticMethod(engine, &AIController::Sleep, "Sleep", 2, ".i");
+ SQAIController.DefSQStaticMethod(engine, &AIController::GetSetting, "GetSetting", 2, ".s");
+ SQAIController.DefSQStaticMethod(engine, &AIController::GetVersion, "GetVersion", 1, ".");
+ SQAIController.DefSQStaticMethod(engine, &AIController::Print, "Print", 3, ".bs");
SQAIController.PostRegister(engine);
}
diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp
index 890630eeb..e4657a7dd 100644
--- a/src/script/squirrel.cpp
+++ b/src/script/squirrel.cpp
@@ -553,3 +553,8 @@ bool Squirrel::CanSuspend()
{
return sq_can_suspend(this->vm);
}
+
+SQInteger Squirrel::GetOpsTillSuspend()
+{
+ return this->vm->_ops_till_suspend;
+}
diff --git a/src/script/squirrel.hpp b/src/script/squirrel.hpp
index 1ada519cc..69e01e647 100644
--- a/src/script/squirrel.hpp
+++ b/src/script/squirrel.hpp
@@ -253,6 +253,11 @@ public:
* Are we allowed to suspend the squirrel script at this moment?
*/
bool CanSuspend();
+
+ /**
+ * How many operations can we execute till suspension?
+ */
+ SQInteger GetOpsTillSuspend();
};
#endif /* SQUIRREL_HPP */