2020-03-22 00:45:04 +01:00
|
|
|
#pragma once
|
2016-11-16 23:23:59 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
#include "Core/Utility.hpp"
|
2016-11-16 23:23:59 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Execute callbacks for specific entities after specific intervals of time.
|
|
|
|
*/
|
|
|
|
class Tasks
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Simplify future changes to a single point of change.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
typedef int64_t Time;
|
2016-11-16 23:23:59 +01:00
|
|
|
typedef SQInteger Interval;
|
2021-01-30 07:51:39 +01:00
|
|
|
typedef uint32_t Iterator;
|
2016-11-17 10:27:07 +01:00
|
|
|
typedef LightObj Argument;
|
2016-11-16 23:23:59 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Structure that represents a task and keeps track of the task information.
|
|
|
|
*/
|
|
|
|
struct Task
|
|
|
|
{
|
|
|
|
// ----------------------------------------------------------------------------------------
|
|
|
|
SQHash mHash; // The hash of the referenced function object.
|
2016-11-17 15:02:00 +01:00
|
|
|
String mTag; // An arbitrary string which represents the tag.
|
2016-11-17 10:27:07 +01:00
|
|
|
LightObj mSelf; // A reference to `this`as a script object.
|
|
|
|
LightObj mFunc; // A reference to the managed function object.
|
|
|
|
LightObj mInst; // A reference to the associated entity object.
|
2016-11-17 11:29:58 +01:00
|
|
|
LightObj mData; // A reference to the arbitrary data associated with this instance.
|
2016-11-16 23:23:59 +01:00
|
|
|
Iterator mIterations; // Number of iterations before self destruct.
|
|
|
|
Interval mInterval; // Interval between task invocations.
|
2022-03-02 17:57:11 +01:00
|
|
|
int16_t mEntity; // The identifier of the entity to which is belongs.
|
|
|
|
uint8_t mType; // The type of the entity to which is belongs.
|
|
|
|
uint8_t mArgc; // The number of arguments that the task must forward.
|
2016-11-17 11:59:29 +01:00
|
|
|
Argument mArgv[8]; // The arguments that the task must forward.
|
2016-11-16 23:23:59 +01:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Default constructor.
|
|
|
|
*/
|
2020-03-22 05:53:04 +01:00
|
|
|
Task() noexcept
|
2016-11-16 23:23:59 +01:00
|
|
|
: mHash(0)
|
2016-11-17 15:02:00 +01:00
|
|
|
, mTag()
|
2016-11-17 10:27:07 +01:00
|
|
|
, mSelf()
|
2016-11-16 23:23:59 +01:00
|
|
|
, mFunc()
|
2016-11-17 10:27:07 +01:00
|
|
|
, mInst()
|
2016-11-17 11:29:58 +01:00
|
|
|
, mData()
|
2016-11-16 23:23:59 +01:00
|
|
|
, mIterations(0)
|
|
|
|
, mInterval(0)
|
|
|
|
, mEntity(-1)
|
2020-03-22 05:53:04 +01:00
|
|
|
, mType(0)
|
2016-11-16 23:23:59 +01:00
|
|
|
, mArgc(0)
|
|
|
|
, mArgv()
|
|
|
|
{
|
2016-11-17 10:27:07 +01:00
|
|
|
/* ... */
|
2016-11-16 23:23:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Copy constructor. (disabled)
|
|
|
|
*/
|
|
|
|
Task(const Task & o) = delete;
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Move constructor. (disabled)
|
|
|
|
*/
|
|
|
|
Task(Task && o) = delete;
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Release managed script resources.
|
|
|
|
*/
|
|
|
|
~Task()
|
|
|
|
{
|
2016-11-17 18:59:47 +01:00
|
|
|
Terminate();
|
2016-11-16 23:23:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Copy assignment operator. (disabled)
|
|
|
|
*/
|
|
|
|
Task & operator = (const Task & o) = delete;
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Move assignment operator. (disabled)
|
|
|
|
*/
|
|
|
|
Task & operator = (Task && o) = delete;
|
|
|
|
|
2016-11-17 11:29:58 +01:00
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Used by the script engine to convert an instance of this type to a string.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD const String & ToString() const
|
2016-11-17 11:29:58 +01:00
|
|
|
{
|
2016-11-17 22:09:35 +01:00
|
|
|
return mTag;
|
2016-11-17 11:29:58 +01:00
|
|
|
}
|
|
|
|
|
2016-11-16 23:23:59 +01:00
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Initializes the task parameters. (assumes previous values are already released)
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
void Init(HSQOBJECT & inst, HSQOBJECT & func, Interval intrv, Iterator itr, int32_t id, int32_t type);
|
2016-11-16 23:23:59 +01:00
|
|
|
|
2016-11-17 18:59:47 +01:00
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Release managed script resources.
|
|
|
|
*/
|
|
|
|
void Release();
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Execute the managed task.
|
|
|
|
*/
|
|
|
|
Interval Execute();
|
|
|
|
|
2016-11-16 23:23:59 +01:00
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Clear the arguments.
|
|
|
|
*/
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
// Now release the arguments
|
|
|
|
for (auto & a : mArgv)
|
|
|
|
{
|
|
|
|
a.Release();
|
|
|
|
}
|
|
|
|
// Reset the counter
|
|
|
|
mArgc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
2016-11-17 18:59:47 +01:00
|
|
|
* Terminate the task.
|
2016-11-17 11:29:58 +01:00
|
|
|
*/
|
|
|
|
void Terminate()
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
2016-11-17 15:02:00 +01:00
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the associated user tag.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD const String & GetTag() const
|
2016-11-17 15:02:00 +01:00
|
|
|
{
|
|
|
|
return mTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Modify the associated user tag.
|
|
|
|
*/
|
2019-02-17 16:23:59 +01:00
|
|
|
void SetTag(StackStrF & tag)
|
2016-11-17 15:02:00 +01:00
|
|
|
{
|
2020-03-22 05:53:04 +01:00
|
|
|
mTag.assign(tag.mPtr, static_cast< size_t >(ClampMin(tag.mLen, 0)));
|
2016-11-17 15:02:00 +01:00
|
|
|
}
|
|
|
|
|
2016-11-17 11:29:58 +01:00
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the instance to entity instance.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD const LightObj & GetInst() const
|
2016-11-17 11:29:58 +01:00
|
|
|
{
|
|
|
|
return mInst;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the function object.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD const LightObj & GetFunc() const
|
2016-11-17 11:29:58 +01:00
|
|
|
{
|
|
|
|
return mFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Modify the function object.
|
|
|
|
*/
|
|
|
|
void SetFunc(const Function & func)
|
|
|
|
{
|
|
|
|
// Validate the specified
|
|
|
|
if (!sq_isclosure(func.GetFunc()) && !sq_isnativeclosure(func.GetFunc()))
|
|
|
|
{
|
2021-02-03 16:50:39 +01:00
|
|
|
STHROWF("Invalid callback type {}", SqTypeName(mFunc.GetType()));
|
2016-11-17 11:29:58 +01:00
|
|
|
}
|
|
|
|
// Grab the virtual machine once
|
2020-04-27 12:10:54 +02:00
|
|
|
HSQUIRRELVM vm = SqVM();
|
2016-11-17 11:29:58 +01:00
|
|
|
// Remember the current stack size
|
|
|
|
const StackGuard sg(vm);
|
|
|
|
// Push the callback on the stack
|
|
|
|
sq_pushobject(vm, func.GetFunc());
|
|
|
|
// Grab the hash of the callback object
|
|
|
|
mHash = sq_gethash(vm, -1);
|
|
|
|
// Now store the function without the environment
|
|
|
|
mFunc = LightObj(func.GetFunc());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the arbitrary user data object.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD const LightObj & GetData() const
|
2016-11-17 11:29:58 +01:00
|
|
|
{
|
|
|
|
return mData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Modify the arbitrary user data object.
|
|
|
|
*/
|
|
|
|
void SetData(const LightObj & data)
|
|
|
|
{
|
|
|
|
mData = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the execution interval.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD SQInteger GetInterval() const
|
2016-11-17 11:29:58 +01:00
|
|
|
{
|
|
|
|
return ConvTo< SQInteger >::From(mInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Modify the execution interval.
|
|
|
|
*/
|
|
|
|
void SetInterval(SQInteger itr)
|
|
|
|
{
|
|
|
|
mInterval = ClampMin(ConvTo< Interval >::From(itr), static_cast< Interval >(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the number of iterations.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD SQInteger GetIterations() const
|
2016-11-17 11:29:58 +01:00
|
|
|
{
|
|
|
|
return ConvTo< SQInteger >::From(mIterations);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Modify the number of iterations.
|
|
|
|
*/
|
|
|
|
void SetIterations(SQInteger itr)
|
|
|
|
{
|
|
|
|
mIterations = ConvTo< Iterator >::From(itr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the number of arguments to be forwarded.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD SQInteger GetArguments() const
|
2016-11-17 11:29:58 +01:00
|
|
|
{
|
|
|
|
return ConvTo< SQInteger >::From(mArgc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------------------
|
|
|
|
* Retrieve a certain argument.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD const Argument & GetArgument(SQInteger arg) const
|
2016-11-17 11:29:58 +01:00
|
|
|
{
|
2021-01-30 07:51:39 +01:00
|
|
|
constexpr uint32_t argvn = (sizeof(mArgv) / sizeof(mArgv[0]));
|
2016-11-17 11:29:58 +01:00
|
|
|
// Cast the index to the proper value
|
2021-01-30 07:51:39 +01:00
|
|
|
uint8_t idx = ConvTo< uint8_t >::From(arg);
|
2016-11-17 11:29:58 +01:00
|
|
|
// Validate the specified index
|
|
|
|
if (idx >= argvn)
|
|
|
|
{
|
2021-02-03 16:50:39 +01:00
|
|
|
STHROWF("The specified index is out of range: {} >= {}", idx, argvn);
|
2016-11-17 11:29:58 +01:00
|
|
|
}
|
|
|
|
// Return the requested argument
|
|
|
|
return mArgv[idx];
|
|
|
|
}
|
2016-11-16 23:23:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
static Time s_Last; // Last time point.
|
|
|
|
static Time s_Prev; // Previous time point.
|
|
|
|
static Interval s_Intervals[SQMOD_MAX_TASKS]; // List of intervals to be processed.
|
|
|
|
static Task s_Tasks[SQMOD_MAX_TASKS]; // List of tasks to be executed.
|
|
|
|
|
2020-03-22 05:53:04 +01:00
|
|
|
public:
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Copy assignment operator. (disabled)
|
|
|
|
*/
|
|
|
|
Tasks & operator = (const Tasks & o) = delete;
|
|
|
|
|
2016-11-16 23:23:59 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
2020-03-22 05:53:04 +01:00
|
|
|
* Move assignment operator. (disabled)
|
|
|
|
*/
|
|
|
|
Tasks & operator = (Tasks && o) = delete;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-11-16 23:23:59 +01:00
|
|
|
* Default constructor. (disabled)
|
|
|
|
*/
|
|
|
|
Tasks() = delete;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Copy constructor. (disabled)
|
|
|
|
*/
|
|
|
|
Tasks(const Tasks & o) = delete;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Move constructor. (disabled)
|
|
|
|
*/
|
|
|
|
Tasks(Tasks && o) = delete;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Destructor. (disabled)
|
|
|
|
*/
|
|
|
|
~Tasks() = delete;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Process all active tasks and update elapsed time.
|
|
|
|
*/
|
|
|
|
static void Process();
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Initialize all resources and prepare for startup.
|
|
|
|
*/
|
|
|
|
static void Initialize();
|
|
|
|
|
2016-11-17 10:27:07 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Register the task class.
|
|
|
|
*/
|
|
|
|
static void Register(HSQUIRRELVM vm);
|
|
|
|
|
2016-11-16 23:23:59 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Release all resources and prepare for shutdown.
|
|
|
|
*/
|
|
|
|
static void Deinitialize();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the instance of the specified entity.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
static LightObj & FindEntity(int32_t id, int32_t type);
|
2016-11-16 23:23:59 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Find an unoccupied task slot.
|
|
|
|
*/
|
|
|
|
static SQInteger FindUnused();
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Locate the first task with the specified parameters.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
static SQInteger Find(int32_t id, int32_t type, SQInteger & pos, HSQUIRRELVM vm);
|
2016-11-16 23:23:59 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Attempt to create a task with the specified parameters.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
static SQInteger Create(int32_t id, int32_t type, HSQUIRRELVM vm);
|
2016-11-16 23:23:59 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Attempt to remove the task with the specified parameters.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
static SQInteger Remove(int32_t id, int32_t type, HSQUIRRELVM vm);
|
2016-11-16 23:23:59 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See if a task with the specified parameters exists.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
static SQInteger Exists(int32_t id, int32_t type, HSQUIRRELVM vm);
|
2016-11-16 23:23:59 +01:00
|
|
|
|
2016-11-17 15:02:00 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Cleanup all tasks associated with the specified entity.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
static const Task & FindByTag(int32_t id, int32_t type, StackStrF & tag);
|
2016-11-17 15:02:00 +01:00
|
|
|
|
2016-11-16 23:23:59 +01:00
|
|
|
public:
|
|
|
|
|
2016-11-17 18:59:47 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the number of used tasks slots.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD static SQInteger GetUsed()
|
2016-11-17 18:59:47 +01:00
|
|
|
{
|
|
|
|
SQInteger n = 0;
|
|
|
|
// Iterate task list
|
|
|
|
for (const auto & t : s_Tasks)
|
|
|
|
{
|
|
|
|
if (VALID_ENTITY(t.mEntity))
|
|
|
|
{
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Return the final count
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2016-11-16 23:23:59 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Cleanup all tasks associated with the specified entity.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
static void Cleanup(int32_t id, int32_t type);
|
2016-11-16 23:23:59 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Forwards calls to create tasks.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
template < typename Entity, int32_t Type > static SQInteger MakeTask(HSQUIRRELVM vm)
|
2016-11-16 23:23:59 +01:00
|
|
|
{
|
|
|
|
// The entity instance
|
2021-01-30 07:51:39 +01:00
|
|
|
const Entity * inst;
|
2016-11-16 23:23:59 +01:00
|
|
|
// Attempt to extract the instance
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Fetch the instance from the stack
|
|
|
|
inst = Var< const Entity * >(vm, 1).value;
|
|
|
|
// Do we have a valid instance?
|
|
|
|
if (!inst)
|
|
|
|
{
|
|
|
|
STHROWF("Invalid entity instance");
|
|
|
|
}
|
2021-01-30 07:51:39 +01:00
|
|
|
// Validate the std::exception entity instance
|
2016-11-16 23:23:59 +01:00
|
|
|
inst->Validate();
|
|
|
|
}
|
2021-01-30 07:51:39 +01:00
|
|
|
catch (const std::exception & e)
|
2016-11-16 23:23:59 +01:00
|
|
|
{
|
|
|
|
return sq_throwerror(vm, e.what());
|
|
|
|
}
|
|
|
|
// Forward the call and return the result
|
|
|
|
return Create(inst->GetID(), Type, vm);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Forwards calls to remove tasks.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
template < typename Entity, int32_t Type > static SQInteger DropTask(HSQUIRRELVM vm)
|
2016-11-16 23:23:59 +01:00
|
|
|
{
|
|
|
|
// The entity instance
|
2021-01-30 07:51:39 +01:00
|
|
|
const Entity * inst;
|
2016-11-16 23:23:59 +01:00
|
|
|
// Attempt to extract the instance
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Fetch the instance from the stack
|
|
|
|
inst = Var< const Entity * >(vm, 1).value;
|
|
|
|
// Do we have a valid instance?
|
|
|
|
if (!inst)
|
|
|
|
{
|
|
|
|
STHROWF("Invalid entity instance");
|
|
|
|
}
|
|
|
|
// Validate the actual entity instance
|
|
|
|
inst->Validate();
|
|
|
|
}
|
2021-01-30 07:51:39 +01:00
|
|
|
catch (const std::exception & e)
|
2016-11-16 23:23:59 +01:00
|
|
|
{
|
|
|
|
return sq_throwerror(vm, e.what());
|
|
|
|
}
|
|
|
|
// Forward the call and return the result
|
|
|
|
return Remove(inst->GetID(), Type, vm);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Forwards calls to check tasks.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
template < typename Entity, int32_t Type > static SQInteger DoesTask(HSQUIRRELVM vm)
|
2016-11-16 23:23:59 +01:00
|
|
|
{
|
|
|
|
// The entity instance
|
2021-01-30 07:51:39 +01:00
|
|
|
const Entity * inst;
|
2016-11-16 23:23:59 +01:00
|
|
|
// Attempt to extract the instance
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Fetch the instance from the stack
|
|
|
|
inst = Var< const Entity * >(vm, 1).value;
|
|
|
|
// Do we have a valid instance?
|
|
|
|
if (!inst)
|
|
|
|
{
|
|
|
|
STHROWF("Invalid entity instance");
|
|
|
|
}
|
|
|
|
// Validate the actual entity instance
|
|
|
|
inst->Validate();
|
|
|
|
}
|
2021-01-30 07:51:39 +01:00
|
|
|
catch (const std::exception & e)
|
2016-11-16 23:23:59 +01:00
|
|
|
{
|
|
|
|
return sq_throwerror(vm, e.what());
|
|
|
|
}
|
|
|
|
// Forward the call and return the result
|
|
|
|
return Exists(inst->GetID(), Type, vm);
|
|
|
|
}
|
2016-11-17 15:02:00 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Forwards calls to find tasks.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
template < typename Entity, int32_t Type > static SQInteger FindTask(HSQUIRRELVM vm)
|
2016-11-17 15:02:00 +01:00
|
|
|
{
|
|
|
|
// Was the tag string specified?
|
|
|
|
if (sq_gettop(vm) <= 1)
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, "Missing tag string");
|
|
|
|
}
|
|
|
|
// The entity instance
|
2021-01-30 07:51:39 +01:00
|
|
|
const Entity * inst;
|
2016-11-17 15:02:00 +01:00
|
|
|
// Attempt to extract the instance
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Fetch the instance from the stack
|
|
|
|
inst = Var< const Entity * >(vm, 1).value;
|
|
|
|
// Do we have a valid instance?
|
|
|
|
if (!inst)
|
|
|
|
{
|
|
|
|
STHROWF("Invalid entity instance");
|
|
|
|
}
|
|
|
|
// Validate the actual entity instance
|
|
|
|
inst->Validate();
|
|
|
|
}
|
2021-01-30 07:51:39 +01:00
|
|
|
catch (const std::exception & e)
|
2016-11-17 15:02:00 +01:00
|
|
|
{
|
|
|
|
return sq_throwerror(vm, e.what());
|
|
|
|
}
|
|
|
|
// Attempt to generate the string value
|
2018-07-29 23:58:27 +02:00
|
|
|
StackStrF tag(vm, 2);
|
2016-11-17 15:02:00 +01:00
|
|
|
// Have we failed to retrieve the string?
|
2018-07-29 23:58:27 +02:00
|
|
|
if (SQ_FAILED(tag.Proc(true)))
|
2016-11-17 15:02:00 +01:00
|
|
|
{
|
|
|
|
return tag.mRes; // Propagate the error!
|
|
|
|
}
|
|
|
|
// Attempt to find the specified task
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Perform the search
|
|
|
|
const Task & task = FindByTag(inst->GetID(), Type, tag);
|
|
|
|
// Now push the instance on the stack
|
|
|
|
sq_pushobject(vm, task.mSelf.mObj);
|
|
|
|
}
|
2021-01-30 07:51:39 +01:00
|
|
|
catch (const std::exception & e)
|
2016-11-17 15:02:00 +01:00
|
|
|
{
|
|
|
|
return sq_throwerror(vm, e.what());
|
|
|
|
}
|
|
|
|
// Specify that this function returns a value
|
|
|
|
return 1;
|
|
|
|
}
|
2016-11-16 23:23:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // Namespace:: SqMod
|