1
0
Fork 0

(svn r23118) -Feature: [NoAI] Allow AIs to query the amount of remaining operations for the current tick

release/1.2
rubidium 2011-11-04 23:20:14 +00:00
parent 02913f40b0
commit 65d0d19b16
10 changed files with 52 additions and 6 deletions

View File

@ -10,9 +10,11 @@ function Regression::TestInit()
{ {
print(""); print("");
print("--TestInit--"); print("--TestInit--");
print(" Ops: " + this.GetOpsTillSuspend());
print(" TickTest: " + this.GetTick()); print(" TickTest: " + this.GetTick());
this.Sleep(1); this.Sleep(1);
print(" TickTest: " + this.GetTick()); print(" TickTest: " + this.GetTick());
print(" Ops: " + this.GetOpsTillSuspend());
print(" SetCommandDelay: " + AIController.SetCommandDelay(1)); print(" SetCommandDelay: " + AIController.SetCommandDelay(1));
print(" IsValid(vehicle.plane_speed): " + AIGameSettings.IsValid("vehicle.plane_speed")); print(" IsValid(vehicle.plane_speed): " + AIGameSettings.IsValid("vehicle.plane_speed"));
print(" vehicle.plane_speed: " + AIGameSettings.GetValue("vehicle.plane_speed")); print(" vehicle.plane_speed: " + AIGameSettings.GetValue("vehicle.plane_speed"));
@ -166,6 +168,8 @@ function Regression::TestInit()
foreach (idx, val in list) { foreach (idx, val in list) {
print(" " + idx); print(" " + idx);
} }
print(" Ops: " + this.GetOpsTillSuspend());
} }
function Regression::Std() function Regression::Std()

View File

@ -1,7 +1,9 @@
--TestInit-- --TestInit--
Ops: 9988
TickTest: 1 TickTest: 1
TickTest: 2 TickTest: 2
Ops: 9990
SetCommandDelay: (null : 0x00000000) SetCommandDelay: (null : 0x00000000)
IsValid(vehicle.plane_speed): true IsValid(vehicle.plane_speed): true
vehicle.plane_speed: 2 vehicle.plane_speed: 2
@ -79,6 +81,7 @@
20 20
30 30
40 40
Ops: 8673
--Std-- --Std--
abs(-21): 21 abs(-21): 21

View File

@ -789,3 +789,8 @@ bool AIInstance::CallLoad()
sq_pop(vm, 4); sq_pop(vm, 4);
return true; return true;
} }
SQInteger AIInstance::GetOpsTillSuspend()
{
return this->engine->GetOpsTillSuspend();
}

View File

@ -172,6 +172,13 @@ public:
* call from within a function called by the AI. * call from within a function called by the AI.
*/ */
void Suspend(); 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: private:
class AIController *controller; ///< The AI main class. class AIController *controller; ///< The AI main class.
class AIStorage *storage; ///< Some global information for each running AI. class AIStorage *storage; ///< Some global information for each running AI.

View File

@ -26,6 +26,7 @@
* \li AICompany::GetQuarterlyCargoDelivered * \li AICompany::GetQuarterlyCargoDelivered
* \li AICompany::GetQuarterlyPerformanceRating * \li AICompany::GetQuarterlyPerformanceRating
* \li AICompany::GetQuarterlyCompanyValue * \li AICompany::GetQuarterlyCompanyValue
* \li AIController::GetOpsTillSuspend
* \li AITown::GetTownAuthority * \li AITown::GetTownAuthority
* \li AIVehicle::ERR_VEHICLE_TOO_LONG in case vehicle length limit is reached * \li AIVehicle::ERR_VEHICLE_TOO_LONG in case vehicle length limit is reached
* *

View File

@ -66,6 +66,11 @@ AIController::~AIController()
return ::Company::Get(_current_company)->ai_instance->GetController()->ticks; 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) /* static */ int AIController::GetSetting(const char *name)
{ {
return AIConfig::GetConfig(_current_company)->GetSetting(name); return AIConfig::GetConfig(_current_company)->GetSetting(name);

View File

@ -51,6 +51,16 @@ public:
*/ */
static uint GetTick(); 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. * Get the value of one of your settings you set via info.nut.
* @param name The name of the setting. * @param name The name of the setting.

View File

@ -14,6 +14,7 @@ void SQAIController_Register(Squirrel *engine)
DefSQClass <AIController> SQAIController("AIController"); DefSQClass <AIController> SQAIController("AIController");
SQAIController.PreRegister(engine); SQAIController.PreRegister(engine);
SQAIController.DefSQStaticMethod(engine, &AIController::GetTick, "GetTick", 1, "."); 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::SetCommandDelay, "SetCommandDelay", 2, ".i");
SQAIController.DefSQStaticMethod(engine, &AIController::Sleep, "Sleep", 2, ".i"); SQAIController.DefSQStaticMethod(engine, &AIController::Sleep, "Sleep", 2, ".i");
SQAIController.DefSQStaticMethod(engine, &AIController::GetSetting, "GetSetting", 2, ".s"); SQAIController.DefSQStaticMethod(engine, &AIController::GetSetting, "GetSetting", 2, ".s");

View File

@ -553,3 +553,8 @@ bool Squirrel::CanSuspend()
{ {
return sq_can_suspend(this->vm); return sq_can_suspend(this->vm);
} }
SQInteger Squirrel::GetOpsTillSuspend()
{
return this->vm->_ops_till_suspend;
}

View File

@ -253,6 +253,11 @@ public:
* Are we allowed to suspend the squirrel script at this moment? * Are we allowed to suspend the squirrel script at this moment?
*/ */
bool CanSuspend(); bool CanSuspend();
/**
* How many operations can we execute till suspension?
*/
SQInteger GetOpsTillSuspend();
}; };
#endif /* SQUIRREL_HPP */ #endif /* SQUIRREL_HPP */