1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-19 16:47:14 +02:00

Migrated the host module to C++ exceptions as well.

Also enabled the latest C++ revision in the project.
Replaced the Random library with the one provided by C++11.
Implemented a simple AES256 encryption class.
Various other fixes and improvements.
This commit is contained in:
Sandu Liviu Catalin
2016-03-10 05:57:13 +02:00
parent 3162221e7f
commit 70e5f0ba21
124 changed files with 14873 additions and 14062 deletions

View File

@ -14,6 +14,14 @@ const AABB AABB::MAX = AABB(Vector3::MIN, Vector3::MAX);
// ------------------------------------------------------------------------------------------------
SQChar AABB::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger AABB::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("AABB");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
AABB::AABB()
: min(-1.0), max(1.0)
@ -49,27 +57,6 @@ AABB::AABB(const Vector3 & vmin, const Vector3 & vmax)
/* ... */
}
// ------------------------------------------------------------------------------------------------
AABB::AABB(const AABB & b)
: min(b.min), max(b.max)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
AABB::~AABB()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator = (const AABB & b)
{
min = b.min;
max = b.max;
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator = (Value s)
{
@ -304,7 +291,7 @@ Int32 AABB::Cmp(const AABB & o) const
// ------------------------------------------------------------------------------------------------
CSStr AABB::ToString() const
{
return ToStringF("%f,%f,%f,%f,%f,%f", min.x, min.y, min.z, max.x, max.y, max.z);
return ToStrF("%f,%f,%f,%f,%f,%f", min.x, min.y, min.z, max.x, max.y, max.z);
}
// ------------------------------------------------------------------------------------------------
@ -392,6 +379,7 @@ void Register_AABB(HSQUIRRELVM vm)
.Prop(_SC("abs"), &AABB::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &AABB::ToString)
.SquirrelFunc(_SC("_typename"), &AABB::Typename)
.Func(_SC("_cmp"), &AABB::Cmp)
/* Metamethods */
.Func<AABB (AABB::*)(const AABB &) const>(_SC("_add"), &AABB::operator +)

View File

@ -8,304 +8,328 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent an axis aligned bounding box in three-dimensional space.
*/
struct AABB
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const AABB NIL;
static const AABB MIN;
static const AABB MAX;
static const AABB MIN;
static const AABB MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The minimum and maximum components of this type.
*/
Vector3 min, max;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
AABB();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a an equally sized and perfectly shaped box from a scalar value.
*/
AABB(Value sv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a an equally sized but imperfectly shaped box from individual components of a
* three-dimensional point.
*/
AABB(Value xv, Value yv, Value zv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a an unequally sized and imperfectly shaped box from individual components of two
* three-dimensional points.
*/
AABB(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a an unequally sized and imperfectly shaped box from two three-dimensional
* vectors representing two three-dimensional points.
*/
AABB(const Vector3 & vmin, const Vector3 & vmax);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
AABB(const AABB & o);
AABB(const AABB & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~AABB();
AABB(AABB && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
AABB & operator = (const AABB & o);
~AABB() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
AABB & operator = (const AABB & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
AABB & operator = (AABB && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
AABB & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Three-dimensional vector assignment operator.
*/
AABB & operator = (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Four-dimensional vector assignment operator threated as a three-dimensional vector.
*/
AABB & operator = (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
AABB & operator += (const AABB & b);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
AABB & operator -= (const AABB & b);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
AABB & operator *= (const AABB & b);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
AABB & operator /= (const AABB & b);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
AABB & operator %= (const AABB & b);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
AABB & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
AABB & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
AABB & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
AABB & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
AABB & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
AABB & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
AABB & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
AABB operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
AABB operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
AABB operator + (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
AABB operator - (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
AABB operator * (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
AABB operator / (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
AABB operator % (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
AABB operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
AABB operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
AABB operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
AABB operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
AABB operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
AABB operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
AABB operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set an equally sized and perfectly shaped box from a scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set an equally sized but imperfectly shaped box from individual components of a
* three-dimensional point.
*/
void Set(Value nx, Value ny, Value nz);
/* --------------------------------------------------------------------------------------------
* ...
* Set an unequally sized and imperfectly shaped box from individual components of two
* three-dimensional points.
*/
void Set(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax);
/* --------------------------------------------------------------------------------------------
* ...
* Set the same box from another instance of this type.
*/
void Set(const AABB & b);
/* --------------------------------------------------------------------------------------------
* ...
* Set an equally sized and imperfectly shaped box from a single three-dimensional vector
* representing a single three-dimensional point.
*/
void Set(const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Set an unequally sized and imperfectly shaped box from two three-dimensional vectors
* representing two three-dimensional points.
*/
void Set(const Vector3 & nmin, const Vector3 & nmax);
/* --------------------------------------------------------------------------------------------
* ...
* Set an equally sized and imperfectly shaped box from a single four-dimensional vector
* representing a single three-dimensional point.
*/
void Set(const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Set an unequally sized and imperfectly shaped box from two four-dimensional vectors
* representing two three-dimensional points.
*/
void Set(const Vector4 & nmin, const Vector4 & nmax);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -314,7 +338,7 @@ static const AABB MAX;
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
AABB Abs() const;
};

View File

@ -1,10 +1,10 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Buffer.hpp"
#include "Buffer.hpp"
// ------------------------------------------------------------------------------------------------
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <cstdlib>
#include <cstring>
#include <cstdarg>
#include <exception>
#include <stdexcept>
@ -14,7 +14,7 @@ namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Compute the next power of two for the specified number.
*/
static inline unsigned int NPow2(unsigned int num)
inline unsigned int NextPow2(unsigned int num)
{
--num;
num |= num >> 1;
@ -40,9 +40,7 @@ void ThrowMemExcept(const char * msg, ...)
int ret = vsnprintf(buffer, sizeof(buffer), msg, args);
// Check for formatting errors
if (ret < 0)
{
throw std::runtime_error("Unknown memory error");
}
// Throw the actual exception
throw std::runtime_error(buffer);
}
@ -53,12 +51,10 @@ void ThrowMemExcept(const char * msg, ...)
static Buffer::Pointer AllocMem(Buffer::SzType size)
{
// Attempt to allocate memory directly
Buffer::Pointer ptr = (Buffer::Pointer)malloc(size);
Buffer::Pointer ptr = reinterpret_cast< Buffer::Pointer >(malloc(size));
// Validate the allocated memory
if (!ptr)
{
ThrowMemExcept("Unable to allocate (%u) bytes of memory", size);
}
// Return the allocated memory
return ptr;
}
@ -75,36 +71,36 @@ class MemCat
public:
// --------------------------------------------------------------------------------------------
typedef Buffer::Value Value;
typedef Buffer::Value Value; // The type of value used to represent a byte.
// --------------------------------------------------------------------------------------------
typedef Buffer::Reference Reference;
typedef Buffer::ConstRef ConstRef;
typedef Buffer::Reference Reference; // A reference to the stored value type.
typedef Buffer::ConstRef ConstRef; // A const reference to the stored value type.
// --------------------------------------------------------------------------------------------
typedef Buffer::Pointer Pointer;
typedef Buffer::ConstPtr ConstPtr;
typedef Buffer::Pointer Pointer; // A pointer to the stored value type.
typedef Buffer::ConstPtr ConstPtr; // A const pointer to the stored value type.
// --------------------------------------------------------------------------------------------
typedef Buffer::SzType SzType;
typedef Buffer::SzType SzType; // The type used to represent size in general.
private:
/* --------------------------------------------------------------------------------------------
*
* Structure used to store a memory chunk in the linked list.
*/
struct Node
{
// ----------------------------------------------------------------------------------------
SzType mCap;
Pointer mPtr;
Node* mNext;
SzType mCap; /* The size of the memory chunk. */
Pointer mPtr; /* Pointer to the memory chunk. */
Node* mNext; /* The next node in the list. */
/* ----------------------------------------------------------------------------------------
* Base constructor.
*/
Node(Node * next)
: mCap(0), mPtr(NULL), mNext(next)
: mCap(0), mPtr(nullptr), mNext(next)
{
/* ... */
}
@ -120,7 +116,7 @@ private:
* Default constructor.
*/
MemCat()
: m_Head(NULL)
: m_Head(nullptr)
{
/* ... */
}
@ -130,20 +126,18 @@ private:
*/
~MemCat()
{
for (Node * node = m_Head, * next = NULL; node; node = next)
for (Node * node = m_Head, * next = nullptr; node; node = next)
{
// Free the memory (if any)
if (node->mPtr)
{
free(node->mPtr);
}
// Save the next node
next = node->mNext;
// Release the node instance
delete node;
}
// Explicitly set the head node to null
m_Head = NULL;
m_Head = nullptr;
}
/* --------------------------------------------------------------------------------------------
@ -151,20 +145,18 @@ private:
*/
void Clear()
{
for (Node * node = m_Head, * next = NULL; node; node = next)
for (Node * node = m_Head, * next = nullptr; node; node = next)
{
// Free the memory (if any)
if (node->mPtr)
{
free(node->mPtr);
}
// Save the next node
next = node->mNext;
// Release the node instance
Push(node);
}
// Explicitly set the head node to null
m_Head = NULL;
m_Head = nullptr;
}
/* --------------------------------------------------------------------------------------------
@ -174,21 +166,17 @@ private:
{
// NOTE: Function assumes (size > 0)
// Find a buffer large enough to satisfy the requested size
for (Node * node = m_Head, * prev = NULL; node; prev = node, node = node->mNext)
for (Node * node = m_Head, * prev = nullptr; node; prev = node, node = node->mNext)
{
// Is this buffer large enough?
if (node->mCap >= size)
{
// Was there a previous node?
if (prev)
{
prev->mNext = node->mNext;
}
// Probably this was the head
else
{
m_Head = node->mNext;
}
// Assign the memory
ptr = node->mPtr;
// Assign the size
@ -200,10 +188,11 @@ private:
}
}
// Round up the size to a power of two number
size = (size & (size - 1)) ? NPow2(size) : size;
size = (size & (size - 1)) ? NextPow2(size) : size;
// Allocate the memory directly
ptr = AllocMem(size);
// See if the memory could be allocated
// (shouldn't reach this point if allocation failed)
if (!ptr)
{
// Revert the size
@ -219,9 +208,7 @@ private:
void Drop(Pointer & ptr, SzType & size)
{
if (!ptr)
{
ThrowMemExcept("Cannot store invalid memory buffer");
}
// Request a node instance
Node * node = Pull();
// Assign the specified memory
@ -245,9 +232,7 @@ private:
s_Nodes = new Node(s_Nodes);
// Validate the head node
if (!s_Nodes)
{
ThrowMemExcept("Unable to allocate memory nodes");
}
}
}
@ -256,10 +241,9 @@ private:
*/
static Node * Pull()
{
// Are there any nodes available?
if (!s_Nodes)
{
Make();
}
Make(); // Make some!
// Grab the head node
Node * node = s_Nodes;
// Promote the next node as the head
@ -275,9 +259,7 @@ private:
{
// See if the node is even valid
if (!node)
{
ThrowMemExcept("Attempting to push invalid node");
}
// Demote the current head node
node->mNext = s_Nodes;
// Promote as the head node
@ -286,7 +268,7 @@ private:
};
// ------------------------------------------------------------------------------------------------
MemCat::Node * MemCat::s_Nodes = NULL;
MemCat::Node * MemCat::s_Nodes = nullptr;
/* ------------------------------------------------------------------------------------------------
* Lightweight memory allocator to reduce the overhead of small allocations.
@ -294,8 +276,8 @@ MemCat::Node * MemCat::s_Nodes = NULL;
class Memory
{
// --------------------------------------------------------------------------------------------
friend class Buffer;
friend class MemRef;
friend class Buffer; // Allow the buffer type to access the memory categories.
friend class MemRef; // Allow the memory manager reference to create new instances.
private:
@ -316,7 +298,7 @@ private:
*/
~Memory()
{
for (MemCat::Node * node = MemCat::s_Nodes, * next = NULL; node; node = next)
for (MemCat::Node * node = MemCat::s_Nodes, * next = nullptr; node; node = next)
{
// Save the next node
next = node->mNext;
@ -324,13 +306,15 @@ private:
delete node;
}
// Explicitly set the head node to null
MemCat::s_Nodes = NULL;
MemCat::s_Nodes = nullptr;
}
private:
// --------------------------------------------------------------------------------------------
MemCat m_Small, m_Medium, m_Large;
MemCat m_Small; // Small memory allocations of <= 1024 bytes.
MemCat m_Medium; // Medium memory allocations of <= 4096 bytes.
MemCat m_Large; // Large memory allocations of <= 4096 bytes.
};
// ------------------------------------------------------------------------------------------------
@ -340,9 +324,7 @@ MemRef MemRef::s_Mem;
void MemRef::Grab()
{
if (m_Ptr)
{
++(*m_Ref);
}
}
// ------------------------------------------------------------------------------------------------
@ -352,8 +334,8 @@ void MemRef::Drop()
{
delete m_Ptr;
delete m_Ref;
m_Ptr = NULL;
m_Ref = NULL;
m_Ptr = nullptr;
m_Ref = nullptr;
}
}
@ -369,13 +351,9 @@ const MemRef & MemRef::Get()
return s_Mem;
}
// ------------------------------------------------------------------------------------------------
Buffer::Pointer Buffer::s_Ptr = NULL;
Buffer::SzType Buffer::s_Cap = 0;
// ------------------------------------------------------------------------------------------------
Buffer::Buffer(const Buffer & o)
: m_Ptr(NULL)
: m_Ptr(nullptr)
, m_Cap(0)
, m_Mem(o.m_Mem)
{
@ -389,10 +367,9 @@ Buffer::Buffer(const Buffer & o)
// ------------------------------------------------------------------------------------------------
Buffer::~Buffer()
{
// Do we have a buffer?
if (m_Ptr)
{
Release();
}
Release(); // Release it!
}
// ------------------------------------------------------------------------------------------------
@ -400,24 +377,25 @@ Buffer & Buffer::operator = (const Buffer & o)
{
if (m_Ptr != o.m_Ptr)
{
// Can we work in the current buffer?
if (m_Cap && o.m_Cap <= m_Cap)
{
// It's safe to copy the data
memcpy(m_Ptr, o.m_Ptr, m_Cap);
}
// Do we even have data to copy?
else if (!o.m_Cap)
{
// Do we have a buffer?
if (m_Ptr)
{
Release();
}
Release(); // Release it!
}
else
{
// Do we have a buffer?
if (m_Ptr)
{
Release();
}
Release(); // Release it!
// Request a larger buffer
Request(o.m_Cap);
// Now it's safe to copy the data
memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
}
}
@ -433,67 +411,51 @@ void Buffer::Request(SzType n)
if (!m_Mem)
{
// Round up the size to a power of two number
n = (n & (n - 1)) ? NPow2(n) : n;
n = (n & (n - 1)) ? NextPow2(n) : n;
// Allocate the memory directly
m_Ptr = AllocMem(n);
}
// Find out in which category does this buffer reside
else if (n <= 1024)
{
m_Mem->m_Small.Grab(m_Ptr, n);
}
else if (n <= 4096)
{
m_Mem->m_Medium.Grab(m_Ptr, n);
}
else
{
m_Mem->m_Large.Grab(m_Ptr, n);
}
// If no errors occured then we can set the size
m_Cap= n;
// If no errors occurred then we can set the size
m_Cap = n;
}
// ------------------------------------------------------------------------------------------------
void Buffer::Release()
{
// TODO: Implement a limit on how much memory can actually be pooled.
// Is there a memory manager available?
if (!m_Mem)
{
// Deallocate the memory directly
free(m_Ptr);
}
free(m_Ptr); // Deallocate the memory directly
// Find out to which category does this buffer belong
else if (m_Cap <= 1024)
{
m_Mem->m_Small.Drop(m_Ptr, m_Cap);
}
else if (m_Cap <= 4096)
{
m_Mem->m_Medium.Drop(m_Ptr, m_Cap);
}
else
{
m_Mem->m_Large.Drop(m_Ptr, m_Cap);
}
// Explicitly reset the buffer
m_Ptr = NULL;
m_Ptr = nullptr;
m_Cap = 0;
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::Write(SzType pos, ConstPtr data, SzType size)
{
// Make sure the pos is not out of bounds
// Make sure the position is not out of bounds
if (pos > m_Cap || !data || !size)
{
return 0;
}
// See if the buffer size must be adjusted
else if ((pos + size) >= m_Cap)
{
// Backup current data
Buffer bkp = Adjust< Value >(pos + size);
// Allocate a larger memory chunk and backup old data
Buffer bkp(Adjust< Value >(NextPow2(pos + size)));
// Copy data back from the old buffer
memcpy(m_Ptr, bkp.m_Ptr, bkp.m_Cap);
}
@ -506,48 +468,44 @@ Buffer::SzType Buffer::Write(SzType pos, ConstPtr data, SzType size)
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, ...)
{
// Make sure the pos is not out of bounds
if (pos > m_Cap)
{
return 0;
}
// Initialize the arguments list
// Initialize the variable argument list
va_list args;
va_start(args, fmt);
// Initial attempt to write to the current buffer
// (if empty, it should tell us the necessary size)
int ret = vsnprintf(m_Ptr + pos, m_Cap - pos, fmt, args);
// Do we need a bigger buffer?
if ((pos + ret) >= m_Cap)
{
// Backup current data
Buffer bkp = Adjust< Value >(pos + ret);
// Copy data back from the old buffer
memcpy(m_Ptr, bkp.m_Ptr, bkp.m_Cap);
// Argument list was modified during the initial format
va_end(args);
va_start(args, fmt);
// Resume writting the requested information
ret = vsnprintf(m_Ptr + pos, m_Cap - pos, fmt, args);
}
// Finalize the arguments list
// Call the function that takes the variable argument list
SzType ret = WriteF(pos, fmt, args);
// Finalize the variable argument list
va_end(args);
// Return the size of the written data in bytes
return (ret < 0) ? 0 : (SzType)ret;
// Return the result
return ret;
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, va_list args)
{
// Make sure the pos is not out of bounds
// Make sure the position is not out of bounds
if (pos > m_Cap)
{
return 0;
}
// Backup the variable argument list
va_list args_cpy;
va_copy(args_cpy, args);
// Attempt to write to the current buffer
int ret = vsnprintf(m_Ptr + pos, m_Cap - pos, fmt, args);
// Return the size of the written data in bytes
return (ret < 0) ? 0 : (SzType)ret;
// (if empty, it should tell us the necessary size)
int ret = vsnprintf(m_Ptr + pos, m_Cap, fmt, args);
// Do we need a bigger buffer?
if ((pos + ret) >= m_Cap)
{
// Allocate a larger memory chunk and backup old data
Buffer bkp(Adjust< Value >(NextPow2(pos + ret)));
// Copy data back from the old buffer
memcpy(m_Ptr, bkp.m_Ptr, bkp.m_Cap);
// Retry writing the requested information
ret = vsnprintf(m_Ptr + pos, m_Cap, fmt, args_cpy);
}
// Return the value 0 if data could not be written
if (ret < 0)
return 0;
// Return the number of written characters
return static_cast< SzType >(ret);
}
} // Namespace:: SQMod
} // Namespace:: SqMod

View File

@ -2,7 +2,10 @@
#define _BASE_BUFFER_HPP_
// ------------------------------------------------------------------------------------------------
#include <assert.h>
#include <cassert>
// ------------------------------------------------------------------------------------------------
#include <utility>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -64,6 +67,17 @@ public:
Grab();
}
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
MemRef(MemRef && o)
: m_Ptr(o.m_Ptr), m_Ref(o.m_Ref)
{
o.m_Ptr = nullptr;
o.m_Ref = nullptr;
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
@ -87,6 +101,22 @@ public:
return *this;
}
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
MemRef & operator = (MemRef && o)
{
if (m_Ptr != o.m_Ptr)
{
Drop();
m_Ptr = o.m_Ptr;
m_Ref = o.m_Ref;
o.m_Ptr = nullptr;
o.m_Ref = nullptr;
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Perform an equality comparison between two memory managers.
*/
@ -116,7 +146,7 @@ public:
*/
Memory * operator -> () const
{
assert(m_Ptr != NULL);
assert(m_Ptr);
return m_Ptr;
}
@ -125,7 +155,7 @@ public:
*/
Memory & operator * () const
{
assert(m_Ptr != NULL);
assert(m_Ptr);
return *m_Ptr;
}
};
@ -134,43 +164,58 @@ public:
void ThrowMemExcept(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Reusable buffer memory for quick allocations.
* Reusable and re-scalable buffer memory for quick memory allocations.
*/
class Buffer
{
public:
// --------------------------------------------------------------------------------------------
typedef char Value; /* The type of value used to represent a byte. */
typedef char Value; // The type of value used to represent a byte.
// --------------------------------------------------------------------------------------------
typedef Value & Reference; /* A reference to the stored value type. */
typedef const Value & ConstRef; /* A const reference to the stored value type. */
typedef Value & Reference; // A reference to the stored value type.
typedef const Value & ConstRef; // A const reference to the stored value type.
// --------------------------------------------------------------------------------------------
typedef Value * Pointer; /* A pointer to the stored value type. */
typedef const Value * ConstPtr; /* A const pointer to the stored value type. */
typedef Value * Pointer; // A pointer to the stored value type.
typedef const Value * ConstPtr; // A const pointer to the stored value type.
// --------------------------------------------------------------------------------------------
typedef unsigned int SzType; /* The type used to represent size in general. */
typedef unsigned int SzType; // The type used to represent size in general.
private:
/* --------------------------------------------------------------------------------------------
* Construct and take ownership of the specified buffer.
*/
Buffer(Pointer & ptr, SzType & cap, const MemRef & mem)
: m_Ptr(ptr)
, m_Cap(cap)
, m_Mem(mem)
{
ptr = nullptr;
cap = 0;
}
public:
/* --------------------------------------------------------------------------------------------
* Default constructor (null). Not null of a previous buffer was marked as movable.
*/
Buffer()
: m_Ptr(s_Ptr)
, m_Cap(s_Cap)
: m_Ptr(nullptr)
, m_Cap(0)
, m_Mem(MemRef::Get())
{
s_Ptr = NULL;
s_Cap = 0;
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Explicit size constructor.
*/
Buffer(SzType n)
: m_Ptr(NULL)
: m_Ptr(nullptr)
, m_Cap(0)
, m_Mem(MemRef::Get())
{
@ -182,6 +227,15 @@ public:
*/
Buffer(const Buffer & o);
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Buffer(Buffer && o)
: m_Ptr(o.m_Ptr), m_Cap(o.m_Cap), m_Mem(o.m_Mem)
{
o.m_Ptr = nullptr;
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
@ -192,6 +246,23 @@ public:
*/
Buffer & operator = (const Buffer & o);
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Buffer & operator = (Buffer && o)
{
if (m_Ptr != o.m_Ptr)
{
if (m_Ptr)
Release();
m_Ptr = o.m_Ptr;
m_Cap = o.m_Cap;
m_Mem = o.m_Mem;
o.m_Ptr = nullptr;
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
@ -251,7 +322,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T > T * Get()
template < typename T = Value> T * Get()
{
return reinterpret_cast< T * >(m_Ptr);
}
@ -259,7 +330,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T > const T * Get() const
template < typename T = Value> const T * Get() const
{
return reinterpret_cast< const T * >(m_Ptr);
}
@ -267,7 +338,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the a certain element.
*/
template < typename T > T & At(SzType n)
template < typename T = Value> T & At(SzType n)
{
assert(n < m_Cap);
return reinterpret_cast< T * >(m_Ptr)[n];
@ -276,7 +347,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the a certain element.
*/
template < typename T > const T & At(SzType n) const
template < typename T = Value> const T & At(SzType n) const
{
assert(n < m_Cap);
return reinterpret_cast< const T * >(m_Ptr)[n];
@ -285,7 +356,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T > T * Begin()
template < typename T = Value> T * Begin()
{
return reinterpret_cast< T * >(m_Ptr);
}
@ -293,7 +364,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T > const T * Begin() const
template < typename T = Value> const T * Begin() const
{
return reinterpret_cast< const T * >(m_Ptr);
}
@ -301,7 +372,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T > T * End()
template < typename T = Value> T * End()
{
return reinterpret_cast< T * >(m_Ptr) + (m_Cap / sizeof(T));
}
@ -309,7 +380,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T > const T * End() const
template < typename T = Value> const T * End() const
{
return reinterpret_cast< const T * >(m_Ptr) + (m_Cap / sizeof(T));
}
@ -333,7 +404,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve maximum elements it can hold for a certain type.
*/
template < typename T > static SzType Max()
template < typename T = Value> static SzType Max()
{
return (0xFFFFFFFF / sizeof(T));
}
@ -341,7 +412,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the current buffer capacity in element count.
*/
template < typename T > SzType Size() const
template < typename T = Value> SzType Size() const
{
return (m_Cap / sizeof(T));
}
@ -357,32 +428,28 @@ public:
/* --------------------------------------------------------------------------------------------
* Makes sure there is enough capacity to hold the specified element count.
*/
template < typename T > Buffer Adjust(SzType n)
template < typename T = Value> Buffer Adjust(SzType n)
{
// Do we meet the minimum size?
if (n < 8)
{
n = 8;
}
n = 8; // Adjust to minimum size
// See if the requested capacity doesn't exceed the limit
if (n > Max< T >())
{
ThrowMemExcept("Requested buffer of (%u) elements exceeds the (%u) limit", n, Max< T >());
}
// Is there an existing buffer?
else if (n && !m_Cap)
{
// Request the memory
Request(n * sizeof(T));
}
Request(n * sizeof(T)); // Request the memory
// Should the size be increased?
else if (n > m_Cap)
{
// Backup the current memory
Move();
Buffer bkp(m_Ptr, m_Cap, m_Mem);
// Request the memory
Request(n * sizeof(T));
// Return the backup
return std::move(bkp);
}
// Return an empty buffer or the backup (if any)
// Return an empty buffer
return Buffer();
}
@ -392,9 +459,7 @@ public:
void Reset()
{
if (m_Ptr)
{
Release();
}
}
/* --------------------------------------------------------------------------------------------
@ -437,17 +502,6 @@ protected:
*/
void Release();
/* --------------------------------------------------------------------------------------------
* Moves the internal buffer to the global members to be taken over by the next instance.
*/
void Move()
{
s_Ptr = m_Ptr;
s_Cap = m_Cap;
m_Ptr = NULL;
m_Cap = 0;
}
private:
// --------------------------------------------------------------------------------------------
@ -456,10 +510,6 @@ private:
// --------------------------------------------------------------------------------------------
MemRef m_Mem;
// --------------------------------------------------------------------------------------------
static Pointer s_Ptr; /* Pointer to a moved memory buffer. */
static SzType s_Cap; /* The total size of the moved buffer. */
};
} // Namespace:: SqMod

View File

@ -14,6 +14,14 @@ const Circle Circle::MAX = Circle(NumLimit< Circle::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Circle::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Circle::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Circle");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Circle::Circle()
: pos(0.0, 0.0), rad(0.0)
@ -42,27 +50,6 @@ Circle::Circle(Value xv, Value yv, Value rv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Circle::Circle(const Circle & o)
: pos(o.pos), rad(o.rad)
{
}
// ------------------------------------------------------------------------------------------------
Circle::~Circle()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator = (const Circle & o)
{
pos = o.pos;
rad = o.rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator = (Value r)
{
@ -341,7 +328,7 @@ Int32 Circle::Cmp(const Circle & o) const
// ------------------------------------------------------------------------------------------------
CSStr Circle::ToString() const
{
return ToStringF("%f,%f,%f", pos.x, pos.y, rad);
return ToStrF("%f,%f,%f", pos.x, pos.y, rad);
}
// ------------------------------------------------------------------------------------------------
@ -395,35 +382,28 @@ void Circle::Generate()
void Circle::Generate(Value min, Value max, bool r)
{
if (EpsLt(max, min))
{
SqThrow("max value is lower than min value");
}
SqThrowF("max value is lower than min value");
else if (r)
{
rad = GetRandomFloat32(min, max);
}
else
{
pos.Generate(min, max);
}
}
void Circle::Generate(Value xmin, Value xmax, Value ymin, Value ymax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin))
SqThrowF("max value is lower than min value");
pos.Generate(xmin, xmax, ymin, ymax);
}
void Circle::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value rmin, Value rmax)
{
if (EpsLt(rmax, rmin))
{
SqThrow("max value is lower than min value");
}
else
{
pos.Generate(xmin, xmax, ymin, ymax);
rad = GetRandomFloat32(rmin, rmax);
}
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(rmax, rmin))
SqThrowF("max value is lower than min value");
pos.Generate(xmin, xmax, ymin, ymax);
rad = GetRandomFloat32(rmin, rmax);
}
// ------------------------------------------------------------------------------------------------
@ -452,6 +432,7 @@ void Register_Circle(HSQUIRRELVM vm)
.Prop(_SC("abs"), &Circle::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &Circle::ToString)
.SquirrelFunc(_SC("_typename"), &Circle::Typename)
.Func(_SC("_cmp"), &Circle::Cmp)
/* Metamethods */
.Func<Circle (Circle::*)(const Circle &) const>(_SC("_add"), &Circle::operator +)

View File

@ -9,355 +9,370 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent a two-dimensional circle.
*/
struct Circle
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Circle NIL;
static const Circle MIN;
static const Circle MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The position and radius components of this type.
*/
Vector2 pos;
Value rad;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Circle();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a circle at position 0,0 using the specified radius.
*/
Circle(Value rv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a circle at the specified position using the specified radius.
*/
Circle(const Vector2 & pv, Value rv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a circle at the specified position using the specified radius.
*/
Circle(Value xv, Value yv, Value rv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Circle(const Circle & o);
Circle(const Circle & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~Circle();
Circle(Circle && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Circle & operator = (const Circle & o);
~Circle() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Circle & operator = (const Circle & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Circle & operator = (Circle && o) = default;
/* --------------------------------------------------------------------------------------------
* Radius assignment operator.
*/
Circle & operator = (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Position assignment operator.
*/
Circle & operator = (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Circle & operator += (const Circle & c);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Circle & operator -= (const Circle & c);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Circle & operator *= (const Circle & c);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Circle & operator /= (const Circle & c);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Circle & operator %= (const Circle & c);
/* --------------------------------------------------------------------------------------------
* ...
* Radius addition assignment operator.
*/
Circle & operator += (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius subtraction assignment operator.
*/
Circle & operator -= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius multiplication assignment operator.
*/
Circle & operator *= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius division assignment operator.
*/
Circle & operator /= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius modulo assignment operator.
*/
Circle & operator %= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Position addition assignment operator.
*/
Circle & operator += (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position subtraction assignment operator.
*/
Circle & operator -= (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position multiplication assignment operator.
*/
Circle & operator *= (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position division assignment operator.
*/
Circle & operator /= (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position modulo assignment operator.
*/
Circle & operator %= (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Circle & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Circle & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Circle operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Circle operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Circle operator + (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Circle operator - (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Circle operator * (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Circle operator / (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Circle operator % (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius addition operator.
*/
Circle operator + (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius subtraction operator.
*/
Circle operator - (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius multiplication operator.
*/
Circle operator * (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius division operator.
*/
Circle operator / (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius modulo operator.
*/
Circle operator % (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position addition operator.
*/
Circle operator + (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position subtraction operator.
*/
Circle operator - (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position multiplication operator.
*/
Circle operator * (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position division operator.
*/
Circle operator / (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position modulo operator.
*/
Circle operator % (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Circle operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Circle operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set the specified radius.
*/
void Set(Value nr);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the circle from another instance of this type.
*/
void Set(const Circle & nc);
/* --------------------------------------------------------------------------------------------
* ...
* Set the position from the specified position.
*/
void Set(const Vector2 & np);
/* --------------------------------------------------------------------------------------------
* ...
* Set the specified position and radius.
*/
void Set(const Vector2 & np, Value nr);
/* --------------------------------------------------------------------------------------------
* ...
* Set the specified position.
*/
void Set(Value nx, Value ny);
/* --------------------------------------------------------------------------------------------
* ...
* Set the specified position and radius.
*/
void Set(Value nx, Value ny, Value nr);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly sized and positioned circle.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly sized or positioned circle within the specified bounds.
*/
void Generate(Value min, Value max, bool r);
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly positioned circle within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax);
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly sized and positioned circle within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value rmin, Value rmax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -365,7 +380,7 @@ struct Circle
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
Circle Abs() const;
};

View File

@ -15,6 +15,14 @@ const Color3 Color3::MAX = Color3(NumLimit< Color3::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Color3::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Color3::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Color3");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Color3::Color3()
: r(0), g(0), b(0)
@ -36,28 +44,6 @@ Color3::Color3(Value rv, Value gv, Value bv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color3::Color3(const Color3 & o)
: r(o.r), g(o.g), b(o.b)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color3::~Color3()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator = (const Color3 & o)
{
r = o.r;
g = o.g;
b = o.b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator = (Value s)
{
@ -449,7 +435,7 @@ Int32 Color3::Cmp(const Color3 & o) const
// ------------------------------------------------------------------------------------------------
CSStr Color3::ToString() const
{
return ToStringF("%u,%u,%u", r, g, b);
return ToStrF("%u,%u,%u", r, g, b);
}
// ------------------------------------------------------------------------------------------------
@ -502,9 +488,9 @@ Uint32 Color3::GetRGB() const
void Color3::SetRGB(Uint32 p)
{
r = Value((p >> 16) & 0xFF);
g = Value((p >> 8) & 0xFF);
b = Value((p) & 0xFF);
r = static_cast< Value >((p >> 16) & 0xFF);
g = static_cast< Value >((p >> 8) & 0xFF);
b = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
@ -515,9 +501,9 @@ Uint32 Color3::GetRGBA() const
void Color3::SetRGBA(Uint32 p)
{
r = Value((p >> 24) & 0xFF);
g = Value((p >> 16) & 0xFF);
b = Value((p >> 8) & 0xFF);
r = static_cast< Value >((p >> 24) & 0xFF);
g = static_cast< Value >((p >> 16) & 0xFF);
b = static_cast< Value >((p >> 8) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
@ -528,9 +514,9 @@ Uint32 Color3::GetARGB() const
void Color3::SetARGB(Uint32 p)
{
r = Value((p >> 16) & 0xFF);
g = Value((p >> 8) & 0xFF);
b = Value((p) & 0xFF);
r = static_cast< Value >((p >> 16) & 0xFF);
g = static_cast< Value >((p >> 8) & 0xFF);
b = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
@ -544,29 +530,21 @@ void Color3::Generate()
void Color3::Generate(Value min, Value max)
{
if (max < min)
{
SqThrow("max value is lower than min value");
}
else
{
r = GetRandomUint8(min, max);
g = GetRandomUint8(min, max);
b = GetRandomUint8(min, max);
}
SqThrowF("max value is lower than min value");
r = GetRandomUint8(min, max);
g = GetRandomUint8(min, max);
b = GetRandomUint8(min, max);
}
void Color3::Generate(Value rmin, Value rmax, Value gmin, Value gmax, Value bmin, Value bmax)
{
if (rmax < rmin || gmax < gmin || bmax < bmin)
{
SqThrow("max value is lower than min value");
}
else
{
r = GetRandomUint8(rmin, rmax);
g = GetRandomUint8(gmin, gmax);
b = GetRandomUint8(bmin, bmax);
}
SqThrowF("max value is lower than min value");
r = GetRandomUint8(rmin, rmax);
g = GetRandomUint8(gmin, gmax);
b = GetRandomUint8(bmin, bmax);
}
// ------------------------------------------------------------------------------------------------
@ -578,9 +556,9 @@ void Color3::Random()
// ------------------------------------------------------------------------------------------------
void Color3::Inverse()
{
r = Value(~r);
g = Value(~g);
b = Value(~b);
r = static_cast< Value >(~r);
g = static_cast< Value >(~g);
b = static_cast< Value >(~b);
}
// ================================================================================================
@ -606,6 +584,7 @@ void Register_Color3(HSQUIRRELVM vm)
.Prop(_SC("str"), &Color3::SetCol)
/* Core Metamethods */
.Func(_SC("_tostring"), &Color3::ToString)
.SquirrelFunc(_SC("_typename"), &Color3::Typename)
.Func(_SC("_cmp"), &Color3::Cmp)
/* Metamethods */
.Func<Color3 (Color3::*)(const Color3 &) const>(_SC("_add"), &Color3::operator +)

View File

@ -8,434 +8,449 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent an opaque RGB color.
*/
struct Color3
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef unsigned char Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Color3 NIL;
static const Color3 MIN;
static const Color3 MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The red, green and blue components of this type.
*/
Value r, g, b;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Color3();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a color with all components with the same specified color.
*/
Color3(Value sv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct with individually specified red, green and blue colors.
*/
Color3(Value rv, Value gv, Value bv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Color3(const Color3 & o);
Color3(const Color3 & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~Color3();
Color3(Color3 && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Color3 & operator = (const Color3 & o);
~Color3() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Color3 & operator = (const Color3 & o) = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Color3 & operator = (Color3 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Color3 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Named color assignment operator.
*/
Color3 & operator = (CSStr name);
/* --------------------------------------------------------------------------------------------
* ...
* Transparent color assignment operator.
*/
Color3 & operator = (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Color3 & operator += (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Color3 & operator -= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Color3 & operator *= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Color3 & operator /= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Color3 & operator %= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise AND assignment operator.
*/
Color3 & operator &= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise OR assignment operator.
*/
Color3 & operator |= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise XOR assignment operator.
*/
Color3 & operator ^= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise left shift assignment operator.
*/
Color3 & operator <<= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise right shift assignment operator.
*/
Color3 & operator >>= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
Color3 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
Color3 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
Color3 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
Color3 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
Color3 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise AND assignment operator.
*/
Color3 & operator &= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise OR assignment operator.
*/
Color3 & operator |= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise XOR assignment operator.
*/
Color3 & operator ^= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise left shift assignment operator.
*/
Color3 & operator <<= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise right shift assignment operator.
*/
Color3 & operator >>= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Color3 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Color3 & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Color3 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Color3 operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Color3 operator + (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Color3 operator - (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Color3 operator * (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Color3 operator / (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Color3 operator % (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise AND operator.
*/
Color3 operator & (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise OR operator.
*/
Color3 operator | (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise XOR operator.
*/
Color3 operator ^ (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise shift left operator.
*/
Color3 operator << (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise shift right operator.
*/
Color3 operator >> (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
Color3 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
Color3 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
Color3 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
Color3 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
Color3 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise AND operator.
*/
Color3 operator & (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise OR operator.
*/
Color3 operator | (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise XOR operator.
*/
Color3 operator ^ (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise shift left operator.
*/
Color3 operator << (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise shift right operator.
*/
Color3 operator >> (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Color3 operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Color3 operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise NOT operator.
*/
Color3 operator ~ () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Implicit conversion to transparent color.
*/
operator Color4 () const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nr, Value ng, Value nb);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from another instance of this type.
*/
void Set(const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from an opaque color.
*/
void Set(const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr str, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values from the identified color.
*/
void SetCol(CSStr name);
/* --------------------------------------------------------------------------------------------
* ...
* Get the component values packed inside an integer value.
*/
Uint32 GetRGB() const;
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values wxtracted from an integer value.
*/
void SetRGB(Uint32 p);
/* --------------------------------------------------------------------------------------------
* ...
* Get the component values packed inside an integer value.
*/
Uint32 GetRGBA() const;
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values wxtracted from an integer value.
*/
void SetRGBA(Uint32 p);
/* --------------------------------------------------------------------------------------------
* ...
* Get the component values packed inside an integer value.
*/
Uint32 GetARGB() const;
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values wxtracted from an integer value.
*/
void SetARGB(Uint32 p);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value rmin, Value rmax, Value gmin, Value gmax, Value bmin, Value bmax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -443,12 +458,12 @@ struct Color3
}
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values to a randomly chosen color.
*/
void Random();
/* --------------------------------------------------------------------------------------------
* ...
* Inverse the color.
*/
void Inverse();
};

View File

@ -15,6 +15,14 @@ const Color4 Color4::MAX = Color4(NumLimit< Color4::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Color4::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Color4::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Color4");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Color4::Color4()
: r(0), g(0), b(0), a(0)
@ -24,7 +32,7 @@ Color4::Color4()
// ------------------------------------------------------------------------------------------------
Color4::Color4(Value sv)
: r(sv), g(sv), b(sv), a(sv)
: r(sv), g(sv), b(sv), a(0)
{
/* ... */
}
@ -43,29 +51,6 @@ Color4::Color4(Value rv, Value gv, Value bv, Value av)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color4::Color4(const Color4 & o)
: r(o.r), g(o.g), b(o.b), a(o.a)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color4::~Color4()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator = (const Color4 & o)
{
r = o.r;
g = o.g;
b = o.b;
a = o.a;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator = (Value s)
{
@ -482,7 +467,7 @@ Int32 Color4::Cmp(const Color4 & o) const
// ------------------------------------------------------------------------------------------------
CSStr Color4::ToString() const
{
return ToStringF("%u,%u,%u,%u", r, g, b, a);
return ToStrF("%u,%u,%u,%u", r, g, b, a);
}
// ------------------------------------------------------------------------------------------------
@ -546,9 +531,9 @@ Uint32 Color4::GetRGB() const
void Color4::SetRGB(Uint32 p)
{
r = Value((p >> 16) & 0xFF);
g = Value((p >> 8) & 0xFF);
b = Value((p) & 0xFF);
r = static_cast< Value >((p >> 16) & 0xFF);
g = static_cast< Value >((p >> 8) & 0xFF);
b = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
@ -559,10 +544,10 @@ Uint32 Color4::GetRGBA() const
void Color4::SetRGBA(Uint32 p)
{
r = Value((p >> 24) & 0xFF);
g = Value((p >> 16) & 0xFF);
b = Value((p >> 8) & 0xFF);
a = Value((p) & 0xFF);
r = static_cast< Value >((p >> 24) & 0xFF);
g = static_cast< Value >((p >> 16) & 0xFF);
b = static_cast< Value >((p >> 8) & 0xFF);
a = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
@ -573,10 +558,10 @@ Uint32 Color4::GetARGB() const
void Color4::SetARGB(Uint32 p)
{
a = Value((p >> 24) & 0xFF);
r = Value((p >> 16) & 0xFF);
g = Value((p >> 8) & 0xFF);
b = Value((p) & 0xFF);
a = static_cast< Value >((p >> 24) & 0xFF);
r = static_cast< Value >((p >> 16) & 0xFF);
g = static_cast< Value >((p >> 8) & 0xFF);
b = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
@ -591,31 +576,23 @@ void Color4::Generate()
void Color4::Generate(Value min, Value max)
{
if (max < min)
{
SqThrow("max value is lower than min value");
}
else
{
r = GetRandomUint8(min, max);
g = GetRandomUint8(min, max);
b = GetRandomUint8(min, max);
a = GetRandomUint8(min, max);
}
SqThrowF("max value is lower than min value");
r = GetRandomUint8(min, max);
g = GetRandomUint8(min, max);
b = GetRandomUint8(min, max);
a = GetRandomUint8(min, max);
}
void Color4::Generate(Value rmin, Value rmax, Value gmin, Value gmax, Value bmin, Value bmax, Value amin, Value amax)
{
if (rmax < rmin || gmax < gmin || bmax < bmin || amax < amin)
{
SqThrow("max value is lower than min value");
}
else
{
r = GetRandomUint8(rmin, rmax);
g = GetRandomUint8(gmin, gmax);
b = GetRandomUint8(bmin, bmax);
a = GetRandomUint8(bmin, bmax);
}
SqThrowF("max value is lower than min value");
r = GetRandomUint8(rmin, rmax);
g = GetRandomUint8(gmin, gmax);
b = GetRandomUint8(bmin, bmax);
a = GetRandomUint8(bmin, bmax);
}
// ------------------------------------------------------------------------------------------------
@ -627,10 +604,10 @@ void Color4::Random()
// ------------------------------------------------------------------------------------------------
void Color4::Inverse()
{
r = Value(~r);
g = Value(~g);
b = Value(~b);
a = Value(~a);
r = static_cast< Value >(~r);
g = static_cast< Value >(~g);
b = static_cast< Value >(~b);
a = static_cast< Value >(~a);
}
// ================================================================================================
@ -658,6 +635,7 @@ void Register_Color4(HSQUIRRELVM vm)
.Prop(_SC("str"), &Color4::SetCol)
/* Core Metamethods */
.Func(_SC("_tostring"), &Color4::ToString)
.SquirrelFunc(_SC("_typename"), &Color4::Typename)
.Func(_SC("_cmp"), &Color4::Cmp)
/* Metamethods */
.Func<Color4 (Color4::*)(const Color4 &) const>(_SC("_add"), &Color4::operator +)

View File

@ -8,444 +8,459 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent a transparent RGBA color.
*/
struct Color4
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef unsigned char Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Color4 NIL;
static const Color4 MIN;
static const Color4 MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The red, green and blue components of this type.
*/
Value r, g, b, a;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Color4();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a color with all components with the same specified color.
*/
Color4(Value sv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct with individually specified red, green and blue colors.
*/
Color4(Value rv, Value gv, Value bv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct with individually specified red, green, blue and alpha colors.
*/
Color4(Value rv, Value gv, Value bv, Value av);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Color4(const Color4 & o);
Color4(const Color4 & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~Color4();
Color4(Color4 && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Color4 & operator = (const Color4 & o);
~Color4() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Color4 & operator = (const Color4 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Color4 & operator = (Color4 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Color4 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Named color assignment operator.
*/
Color4 & operator = (CSStr name);
/* --------------------------------------------------------------------------------------------
* ...
* Opaque color assignment operator.
*/
Color4 & operator = (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Color4 & operator += (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Color4 & operator -= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Color4 & operator *= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Color4 & operator /= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Color4 & operator %= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise AND assignment operator.
*/
Color4 & operator &= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise OR assignment operator.
*/
Color4 & operator |= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise XOR assignment operator.
*/
Color4 & operator ^= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise left shift assignment operator.
*/
Color4 & operator <<= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise right shift assignment operator.
*/
Color4 & operator >>= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
Color4 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
Color4 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
Color4 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
Color4 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
Color4 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise AND assignment operator.
*/
Color4 & operator &= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise OR assignment operator.
*/
Color4 & operator |= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise XOR assignment operator.
*/
Color4 & operator ^= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise left shift assignment operator.
*/
Color4 & operator <<= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise right shift assignment operator.
*/
Color4 & operator >>= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Color4 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Color4 & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Color4 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Color4 operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Color4 operator + (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Color4 operator - (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Color4 operator * (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Color4 operator / (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Color4 operator % (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise AND operator.
*/
Color4 operator & (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise OR operator.
*/
Color4 operator | (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise XOR operator.
*/
Color4 operator ^ (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise shift left operator.
*/
Color4 operator << (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise shift right operator.
*/
Color4 operator >> (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
Color4 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
Color4 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
Color4 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
Color4 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
Color4 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise AND operator.
*/
Color4 operator & (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise OR operator.
*/
Color4 operator | (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise XOR operator.
*/
Color4 operator ^ (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise shift left operator.
*/
Color4 operator << (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise shift right operator.
*/
Color4 operator >> (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Color4 operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Color4 operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise NOT operator.
*/
Color4 operator ~ () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Implicit conversion to opaque color.
*/
operator Color3 () const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nr, Value ng, Value nb);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nr, Value ng, Value nb, Value na);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from another instance of this type.
*/
void Set(const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from an opaque color.
*/
void Set(const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr name, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values from the identified color.
*/
void SetCol(CSStr name);
/* --------------------------------------------------------------------------------------------
* ...
* Get the component values packed inside an integer value.
*/
Uint32 GetRGB() const;
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values wxtracted from an integer value.
*/
void SetRGB(Uint32 p);
/* --------------------------------------------------------------------------------------------
* ...
* Get the component values packed inside an integer value.
*/
Uint32 GetRGBA() const;
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values wxtracted from an integer value.
*/
void SetRGBA(Uint32 p);
/* --------------------------------------------------------------------------------------------
* ...
* Get the component values packed inside an integer value.
*/
Uint32 GetARGB() const;
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values wxtracted from an integer value.
*/
void SetARGB(Uint32 p);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value rmin, Value rmax, Value gmin, Value gmax, Value bmin, Value bmax, Value amin, Value amax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -453,12 +468,12 @@ struct Color4
}
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values to a randomly chosen color.
*/
void Random();
/* --------------------------------------------------------------------------------------------
* ...
* Inverse the color.
*/
void Inverse();
};

View File

@ -16,6 +16,14 @@ const Quaternion Quaternion::MAX = Quaternion(NumLimit< Quaternion::Value >::Max
// ------------------------------------------------------------------------------------------------
SQChar Quaternion::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Quaternion::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Quaternion");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion()
: x(0.0), y(0.0), z(0.0), w(0.0)
@ -44,29 +52,6 @@ Quaternion::Quaternion(Value xv, Value yv, Value zv, Value wv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion(const Quaternion & o)
: x(o.x), y(o.y), z(o.z), w(o.w)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Quaternion::~Quaternion()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator = (const Quaternion & o)
{
x = o.x;
y = o.y;
z = o.z;
w = o.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator = (Value s)
{
@ -338,7 +323,7 @@ Int32 Quaternion::Cmp(const Quaternion & o) const
// ------------------------------------------------------------------------------------------------
CSStr Quaternion::ToString() const
{
return ToStringF("%f,%f,%f,%f", x, y, z, w);
return ToStrF("%f,%f,%f,%f", x, y, z, w);
}
// ------------------------------------------------------------------------------------------------
@ -408,31 +393,23 @@ void Quaternion::Generate()
void Quaternion::Generate(Value min, Value max)
{
if (EpsLt(max, min))
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
z = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
z = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
}
void Quaternion::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value wmin, Value wmax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(zmax, zmin) || EpsLt(wmax, wmin))
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(xmin, xmax);
y = GetRandomFloat32(ymin, ymax);
z = GetRandomFloat32(zmin, zmax);
y = GetRandomFloat32(ymin, ymax);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(xmin, xmax);
y = GetRandomFloat32(ymin, ymax);
z = GetRandomFloat32(zmin, zmax);
y = GetRandomFloat32(ymin, ymax);
}
// ------------------------------------------------------------------------------------------------
@ -463,6 +440,7 @@ void Register_Quaternion(HSQUIRRELVM vm)
.Prop(_SC("abs"), &Quaternion::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &Quaternion::ToString)
.SquirrelFunc(_SC("_typename"), &Quaternion::Typename)
.Func(_SC("_cmp"), &Quaternion::Cmp)
/* Metamethods */
.Func<Quaternion (Quaternion::*)(const Quaternion &) const>(_SC("_add"), &Quaternion::operator +)

View File

@ -8,34 +8,34 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Quaternion class for representing rotations.
*/
struct Quaternion
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Quaternion NIL;
static const Quaternion MIN;
static const Quaternion MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The x, y, z and w components of this type.
*/
Value x, y, z, w;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Quaternion();
@ -55,257 +55,272 @@ struct Quaternion
Quaternion(Value xv, Value yv, Value zv, Value wv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Quaternion(const Quaternion & o);
Quaternion(const Quaternion & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~Quaternion();
Quaternion(Quaternion && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Quaternion & operator = (const Quaternion & o);
~Quaternion() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Quaternion & operator = (const Quaternion & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Quaternion & operator = (Quaternion && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Quaternion & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Euler assignment operator.
*/
Quaternion & operator = (const Vector3 & q);
/* --------------------------------------------------------------------------------------------
* ...
* Four-dimensional vector assignment operator threated as a three-dimensional vector.
*/
Quaternion & operator = (const Vector4 & q);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Quaternion & operator += (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Quaternion & operator -= (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Quaternion & operator *= (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Quaternion & operator /= (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Quaternion & operator %= (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
Quaternion & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
Quaternion & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
Quaternion & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
Quaternion & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
Quaternion & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Quaternion & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Quaternion & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Quaternion operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Quaternion operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Quaternion operator + (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Quaternion operator - (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Quaternion operator * (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Quaternion operator / (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Quaternion operator % (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
Quaternion operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
Quaternion operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
Quaternion operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
Quaternion operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
Quaternion operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Quaternion operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Quaternion operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nx, Value ny, Value nz);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nx, Value ny, Value nz, Value nw);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from another instance of this type.
*/
void Set(const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from a three-dimensional vector as euler rotation.
*/
void Set(const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from a four-dimensional vector.
*/
void Set(const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value wmin, Value wmax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -313,7 +328,7 @@ struct Quaternion
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
Quaternion Abs() const;
};

View File

@ -35,6 +35,11 @@ PluginFuncs* _Func = NULL;
PluginCallbacks* _Clbk = NULL;
PluginInfo* _Info = NULL;
/* ------------------------------------------------------------------------------------------------
* Common buffer to reduce memory allocations. To be immediately copied uppon return!
*/
static SQChar g_Buffer[4096];
// ------------------------------------------------------------------------------------------------
const char NumLimit< char >::Min = CHAR_MIN;
const signed char NumLimit< signed char >::Min = SCHAR_MIN;
@ -99,30 +104,52 @@ bool SToB(CSStr str)
}
// ------------------------------------------------------------------------------------------------
CSStr ToStrF(CCStr fmt, ...)
void SqThrowF(CCStr fmt, ...)
{
static char buf[128];
// Initialize the argument list
va_list args;
va_start (args, fmt);
int ret = vsnprintf(buf, sizeof(buf), fmt, args);
if (ret < 0)
{
SqThrow("Failed to run the specified string format");
buf[0] = 0;
}
// Write the requested contents
if (snprintf(g_Buffer, sizeof(g_Buffer), fmt, args) < 0)
strcpy(g_Buffer, "Unknown error has occurred");
// Release the argument list
va_end(args);
return buf;
// Throw the exception with the resulted message
throw Sqrat::Exception(g_Buffer);
}
// ------------------------------------------------------------------------------------------------
CSStr ToStrF(CCStr fmt, ...)
{
// Prepare the arguments list
va_list args;
va_start (args, fmt);
// Attempt to run the specified format
int ret = vsnprintf(g_Buffer, sizeof(g_Buffer), fmt, args);
// See if the format function failed
if (ret < 0)
SqThrowF("Failed to run the specified string format");
// Finalized the arguments list
va_end(args);
// Return the resulted string
return g_Buffer;
}
// ------------------------------------------------------------------------------------------------
CSStr ToStringF(CCStr fmt, ...)
{
// Acquire a moderately sized buffer
Buffer b(128);
// Prepare the arguments list
va_list args;
va_start (args, fmt);
// Attempt to run the specified format
if (b.WriteF(0, fmt, args) == 0)
b.At< SQChar >(0) = 0;
// Make sure the string is null terminated
b.At(0) = 0;
// Finalized the arguments list
va_end(args);
// Return the resulted string
return b.Get< SQChar >();
}
@ -280,22 +307,17 @@ const Color3 & GetRandomColor()
// ------------------------------------------------------------------------------------------------
Color3 GetColor(CSStr name)
{
Uint32 len = 0;
// See if we actually have something to search for
if(!name || (len = (Uint32)strlen(name)) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return Color3::NIL;
}
if(!name || *name == 0)
SqThrowF("Cannot extract values from an empty string");
// Clone the string into an editable version
CCStr str = StrJustAlphaNum(name);
str = StrToLowercase(str);
// See if we still have a valid name after the cleanup
if((len = (Uint32)strlen(name)) <= 0)
{
SqThrow("Cannot extract values from an invalid string: %s", name);
return Color3::NIL;
}
if(!str || *str == 0)
SqThrowF("Cannot extract values from an invalid string: %s", name);
// Calculate the name length
const Uint32 len = strlen(str);
// Get the most significant characters used to identify a weapon
SQChar a = str[0], b = 0, c = 0, d = str[len-1];
// Look for deeper specifiers
@ -885,19 +907,17 @@ Color3 GetColor(CSStr name)
}
// ------------------------------------------------------------------------------------------------
AABB GetAABB(CSStr str, SQChar delim)
const AABB & GetAABB(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %f , %f , %f , %f , %f , %f ");
static AABB box;
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
box.Clear();
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return box;
}
else if (delim != AABB::Delim)
if (delim != AABB::Delim)
{
fs[4] = delim;
fs[9] = delim;
@ -920,19 +940,17 @@ AABB GetAABB(CSStr str, SQChar delim)
}
// ------------------------------------------------------------------------------------------------
Circle GetCircle(CSStr str, SQChar delim)
const Circle & GetCircle(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %f , %f , %f ");
static Circle circle;
//circle.Clear();
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return circle;
}
else if (delim != Circle::Delim)
circle.Clear();
if (delim != Circle::Delim)
{
fs[4] = delim;
fs[9] = delim;
@ -949,16 +967,15 @@ Circle GetCircle(CSStr str, SQChar delim)
}
// ------------------------------------------------------------------------------------------------
Color3 GetColor3(CSStr str, SQChar delim)
const Color3 & GetColor3(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %u , %u , %u ");
static Color3 col;
Uint32 r = 0, g = 0, b = 0;
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return Color3();
}
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
else if (delim != Color3::Delim)
{
fs[4] = delim;
@ -972,19 +989,23 @@ Color3 GetColor3(CSStr str, SQChar delim)
sscanf(str, &fs[0], &r, &g, &b);
return Color3(Color3::Value(r), Color3::Value(g), Color3::Value(b));
col.r = static_cast< Color4::Value >(r);
col.g = static_cast< Color4::Value >(g);
col.b = static_cast< Color4::Value >(b);
return col;
}
Color4 GetColor4(CSStr str, SQChar delim)
// ------------------------------------------------------------------------------------------------
const Color4 & GetColor4(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %u , %u , %u , %u ");
static Color4 col;
Uint32 r = 0, g = 0, b = 0, a = 0;
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return Color4();
}
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
else if (delim != Color4::Delim)
{
fs[4] = delim;
@ -1000,23 +1021,26 @@ Color4 GetColor4(CSStr str, SQChar delim)
sscanf(str, &fs[0], &r, &g, &b, &a);
return Color4(Color4::Value(r), Color4::Value(g), Color4::Value(b), Color4::Value(a));
col.r = static_cast< Color4::Value >(r);
col.g = static_cast< Color4::Value >(g);
col.b = static_cast< Color4::Value >(b);
col.a = static_cast< Color4::Value >(a);
return col;
}
// ------------------------------------------------------------------------------------------------
Quaternion GetQuaternion(CSStr str, SQChar delim)
const Quaternion & GetQuaternion(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %f , %f , %f , %f ");
static Quaternion quat;
//quat.Clear();
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return quat;
}
else if (delim != Quaternion::Delim)
quat.Clear();
if (delim != Quaternion::Delim)
{
fs[4] = delim;
fs[9] = delim;
@ -1034,19 +1058,18 @@ Quaternion GetQuaternion(CSStr str, SQChar delim)
return quat;
}
Sphere GetSphere(CSStr str, SQChar delim)
// ------------------------------------------------------------------------------------------------
const Sphere & GetSphere(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %f , %f , %f , %f ");
static Sphere sphere;
//sphere.Clear();
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return sphere;
}
else if (delim != Sphere::Delim)
sphere.Clear();
if (delim != Sphere::Delim)
{
fs[4] = delim;
fs[9] = delim;
@ -1065,52 +1088,40 @@ Sphere GetSphere(CSStr str, SQChar delim)
}
// ------------------------------------------------------------------------------------------------
Vector2 GetVector2(CSStr str, SQChar delim)
const Vector2 & GetVector2(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %f , %f ");
static Vector2 vec;
//vec.Clear();
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return vec;
}
else if (delim != Vector2::Delim)
{
vec.Clear();
if (delim != Vector2::Delim)
fs[4] = delim;
}
else
{
fs[4] = Vector2::Delim;
}
sscanf(str, &fs[0], &vec.x, &vec.y);
return vec;
}
Vector2i GetVector2i(CSStr str, SQChar delim)
const Vector2i & GetVector2i(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %d , %d ");
static Vector2i vec;
//vec.Clear();
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return vec;
}
else if (delim != Vector2i::Delim)
{
vec.Clear();
if (delim != Vector2i::Delim)
fs[4] = delim;
}
else
{
fs[4] = Vector2i::Delim;
}
sscanf(str, &fs[0], &vec.x, &vec.y);
@ -1118,19 +1129,17 @@ Vector2i GetVector2i(CSStr str, SQChar delim)
}
// ------------------------------------------------------------------------------------------------
Vector3 GetVector3(CSStr str, SQChar delim)
const Vector3 & GetVector3(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %f , %f , %f ");
static Vector3 vec;
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
vec.Clear();
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return vec;
}
else if (delim != Vector3::Delim)
if (delim != Vector3::Delim)
{
fs[4] = delim;
fs[9] = delim;
@ -1146,19 +1155,17 @@ Vector3 GetVector3(CSStr str, SQChar delim)
return vec;
}
Vector4 GetVector4(CSStr str, SQChar delim)
const Vector4 & GetVector4(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %f , %f , %f , %f ");
static Vector4 vec;
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
vec.Clear();
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return vec;
}
else if (delim != Vector4::Delim)
if (delim != Vector4::Delim)
{
fs[4] = delim;
fs[9] = delim;

View File

@ -5,8 +5,8 @@
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
#include <math.h>
#include <assert.h>
#include <cmath>
#include <cassert>
// ------------------------------------------------------------------------------------------------
#include <vcmp.h>
@ -19,7 +19,7 @@ namespace SqMod {
extern const SQChar * g_EmptyStr;
/* ------------------------------------------------------------------------------------------------
* Proxies to comunicate with the server.
* Proxies to communicate with the server.
*/
extern PluginFuncs* _Func;
extern PluginCallbacks* _Clbk;
@ -28,7 +28,9 @@ extern PluginInfo* _Info;
// ------------------------------------------------------------------------------------------------
template < typename T > struct NumLimit;
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Basic minimum and maximum values for primitive numeric types.
*/
template <> struct NumLimit< char > { static const char Min, Max; };
template <> struct NumLimit< signed char > { static const signed char Min, Max; };
template <> struct NumLimit< unsigned char > { static const unsigned char Min, Max; };
@ -44,7 +46,58 @@ template <> struct NumLimit< float > { static const float Min, Max; };
template <> struct NumLimit< double > { static const double Min, Max; };
template <> struct NumLimit< long double > { static const long double Min, Max; };
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Implements RAII to restore the VM stack to it's initial size on function exit.
*/
struct StackGuard
{
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
StackGuard(HSQUIRRELVM vm)
: m_Top(sq_gettop(vm)), m_VM(vm)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
StackGuard(const StackGuard &) = delete;
/* --------------------------------------------------------------------------------------------
* Move constructor. (disabled)
*/
StackGuard(StackGuard &&) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~StackGuard()
{
sq_pop(m_VM, sq_gettop(m_VM) - m_Top);
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
*/
StackGuard & operator = (const StackGuard &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
StackGuard & operator = (StackGuard &&) = delete;
private:
// --------------------------------------------------------------------------------------------
Int32 m_Top; /* The top of the stack when this instance was created. */
HSQUIRRELVM m_VM; /* The VM where the stack should be restored. */
};
/* ------------------------------------------------------------------------------------------------
* Perform an equality comparison between two values taking into account floating point issues.
*/
template< typename T > inline bool EpsEq(const T a, const T b)
{
return abs(a - b) <= 0;
@ -60,7 +113,9 @@ template <> inline bool EpsEq(const Float64 a, const Float64 b)
return fabs(a - b) <= 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Perform a less than comparison between two values taking into account floating point issues.
*/
template< typename T > inline bool EpsLt(const T a, const T b)
{
return !EpsEq(a, b) && (a < b);
@ -76,7 +131,9 @@ template <> inline bool EpsLt(const Float64 a, const Float64 b)
return !EpsEq(a, b) && (a - b) < 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Perform a greater than comparison between two values taking into account floating point issues.
*/
template< typename T > inline bool EpsGt(const T a, const T b)
{
return !EpsEq(a, b) && (a > b);
@ -92,7 +149,10 @@ template <> inline bool EpsGt(const Float64 a, const Float64 b)
return !EpsEq(a, b) && (a - b) > 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Perform a less than or equal comparison between two values taking into account
* floating point issues.
*/
template< typename T > inline bool EpsLtEq(const T a, const T b)
{
return !EpsEq(a, b) || (a < b);
@ -108,7 +168,10 @@ template <> inline bool EpsLtEq(const Float64 a, const Float64 b)
return !EpsEq(a, b) || (a - b) < 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Perform a greater than or equal comparison between two values taking into account
* floating point issues.
*/
template< typename T > inline bool EpsGtEq(const T a, const T b)
{
return !EpsEq(a, b) || (a > b);
@ -124,7 +187,9 @@ template <> inline bool EpsGtEq(const Float64 a, const Float64 b)
return !EpsEq(a, b) || (a - b) > 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Force a value to be within a certain range.
*/
template< typename T > inline T Clamp(T val, T min, T max)
{
return val < min ? min : (val > max ? max : val);
@ -144,6 +209,142 @@ inline Uint32 NextPow2(Uint32 num)
return ++num;
}
/* ------------------------------------------------------------------------------------------------
* Output a message only if the _DEBUG was defined.
*/
void OutputDebug(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted user message to the console.
*/
void OutputMessage(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted error message to the console.
*/
void OutputError(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Retrieve a reference to a null script object.
*/
Object & NullObject();
/* ------------------------------------------------------------------------------------------------
* Retrieve a reference to a null/empty script array.
*/
Array & NullArray();
/* ------------------------------------------------------------------------------------------------
* Retrieve a reference to a null script function.
*/
Function & NullFunction();
/* ------------------------------------------------------------------------------------------------
* Create a script object from the specified value on the default VM.
*/
template < typename T > Object MakeObject(const T & v)
{
PushVar< T >(DefaultVM::Get(), v);
Var< Object > var(DefaultVM::Get(), -1);
sq_pop(DefaultVM::Get(), 1);
return var.value;
}
/* ------------------------------------------------------------------------------------------------
* Create a script object from the specified value on the specified VM.
*/
template < typename T > Object MakeObject(HSQUIRRELVM vm, const T & v)
{
PushVar< T >(vm, v);
Var< Object > var(vm, -1);
sq_pop(vm, 1);
return var.value;
}
/* ------------------------------------------------------------------------------------------------
* Simple function to check whether the specified string can be considered as a boolean value
*/
bool SToB(CSStr str);
/* ------------------------------------------------------------------------------------------------
* Generate a formatted string and throw it as a sqrat exception.
*/
void SqThrowF(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
* Quickly generate a formatted string on a small static buffer without any memory allocations.
*/
CSStr ToStrF(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
* Generate a formatted string on a temporary buffer and return the string but not the buffer.
*/
CSStr ToStringF(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
* Obtain a randomly chosen color from a list of known colors.
*/
const Color3 & GetRandomColor();
/* ------------------------------------------------------------------------------------------------
* Attempt to identify the color in the specified name and return it.
*/
Color3 GetColor(CSStr name);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the AABB type from a string.
*/
const AABB & GetAABB(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Circle type from a string.
*/
const Circle & GetCircle(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Color3 type from a string.
*/
const Color3 & GetColor3(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Color4 type from a string.
*/
const Color4 & GetColor4(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Quaternion type from a string.
*/
const Quaternion & GetQuaternion(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Sphere type from a string.
*/
const Sphere & GetSphere(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Vector2 type from a string.
*/
const Vector2 & GetVector2(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Vector2i type from a string.
*/
const Vector2i & GetVector2i(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Vector3 type from a string.
*/
const Vector3 & GetVector3(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Vector4 type from a string.
*/
const Vector4 & GetVector4(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
*/
// ------------------------------------------------------------------------------------------------
void LogDbg(CCStr fmt, ...);
void LogUsr(CCStr fmt, ...);
@ -180,84 +381,6 @@ bool cLogSWrn(bool cond, CCStr fmt, ...);
bool cLogSErr(bool cond, CCStr fmt, ...);
bool cLogSFtl(bool cond, CCStr fmt, ...);
// ------------------------------------------------------------------------------------------------
void SqThrow(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
* Output a message only if the _DEBUG was defined.
*/
void OutputDebug(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted user message to the console.
*/
void OutputMessage(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted error message to the console.
*/
void OutputError(const char * msg, ...);
// ------------------------------------------------------------------------------------------------
Object & NullObject();
// ------------------------------------------------------------------------------------------------
Array & NullArray();
// ------------------------------------------------------------------------------------------------
Function & NullFunction();
// ------------------------------------------------------------------------------------------------
template < typename T > Object MakeObject(const T & v)
{
PushVar< T >(DefaultVM::Get(), v);
Var< Object > var(DefaultVM::Get(), -1);
sq_pop(DefaultVM::Get(), 1);
return var.value;
}
// ------------------------------------------------------------------------------------------------
template < typename T > Object MakeObject(HSQUIRRELVM vm, const T & v)
{
PushVar< T >(vm, v);
Var< Object > var(vm, -1);
sq_pop(vm, 1);
return var.value;
}
/* ------------------------------------------------------------------------------------------------
* Simple function to check whether the specified string can be considered as a boolean value
*/
bool SToB(CSStr str);
/* ------------------------------------------------------------------------------------------------
*
*/
CSStr ToStrF(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
*
*/
CSStr ToStringF(CCStr fmt, ...);
// ------------------------------------------------------------------------------------------------
const Color3 & GetRandomColor();
/* ------------------------------------------------------------------------------------------------
* Value extractors.
*/
Color3 GetColor(CSStr name);
AABB GetAABB(CSStr str, SQChar delim);
Circle GetCircle(CSStr str, SQChar delim);
Color3 GetColor3(CSStr str, SQChar delim);
Color4 GetColor4(CSStr str, SQChar delim);
Quaternion GetQuaternion(CSStr str, SQChar delim);
Sphere GetSphere(CSStr str, SQChar delim);
Vector2 GetVector2(CSStr str, SQChar delim);
Vector2i GetVector2i(CSStr str, SQChar delim);
Vector3 GetVector3(CSStr str, SQChar delim);
Vector4 GetVector4(CSStr str, SQChar delim);
} // Namespace:: SqMod
#endif // _BASE_SHARED_HPP_

View File

@ -14,6 +14,14 @@ const Sphere Sphere::MAX = Sphere(NumLimit< Sphere::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Sphere::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Sphere::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Sphere");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Sphere::Sphere()
: pos(0.0), rad(0.0)
@ -42,27 +50,6 @@ Sphere::Sphere(Value xv, Value yv, Value zv, Value rv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Sphere::Sphere(const Sphere & o)
: pos(o.pos), rad(o.rad)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Sphere::~Sphere()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator = (const Sphere & o)
{
pos = o.pos;
rad = o.rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator = (Value r)
{
@ -341,7 +328,7 @@ Int32 Sphere::Cmp(const Sphere & o) const
// ------------------------------------------------------------------------------------------------
CSStr Sphere::ToString() const
{
return ToStringF("%f,%f,%f,%f", pos.x, pos.y, pos.z, rad);
return ToStrF("%f,%f,%f,%f", pos.x, pos.y, pos.z, rad);
}
// ------------------------------------------------------------------------------------------------
@ -395,35 +382,28 @@ void Sphere::Generate()
void Sphere::Generate(Value min, Value max, bool r)
{
if (EpsLt(max, min))
{
SqThrow("max value is lower than min value");
}
SqThrowF("max value is lower than min value");
else if (r)
{
rad = GetRandomFloat32(min, max);
}
else
{
pos.Generate(min, max);
}
}
void Sphere::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(zmax, zmin))
SqThrowF("max value is lower than min value");
pos.Generate(xmin, xmax, ymin, ymax, zmin, zmax);
}
void Sphere::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value rmin, Value rmax)
{
if (EpsLt(rmax, rmin))
{
SqThrow("max value is lower than min value");
}
else
{
pos.Generate(xmin, xmax, ymin, ymax, zmin, zmax);
rad = GetRandomFloat32(rmin, rmax);
}
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(zmax, zmin) || EpsLt(rmax, rmin))
SqThrowF("max value is lower than min value");
pos.Generate(xmin, xmax, ymin, ymax, zmin, zmax);
rad = GetRandomFloat32(rmin, rmax);
}
// ------------------------------------------------------------------------------------------------
@ -452,6 +432,7 @@ void Register_Sphere(HSQUIRRELVM vm)
.Prop(_SC("abs"), &Sphere::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &Sphere::ToString)
.SquirrelFunc(_SC("_typename"), &Sphere::Typename)
.Func(_SC("_cmp"), &Sphere::Cmp)
/* Metamethods */
.Func<Sphere (Sphere::*)(const Sphere &) const>(_SC("_add"), &Sphere::operator +)

View File

@ -9,355 +9,370 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent a three-dimensional sphere.
*/
struct Sphere
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Sphere NIL;
static const Sphere MIN;
static const Sphere MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The position and radius components of this type.
*/
Vector3 pos;
Value rad;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Sphere();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a sphere at position 0,0,0 using the specified radius.
*/
Sphere(Value rv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a sphere at the specified position using the specified radius.
*/
Sphere(const Vector3 & pv, Value rv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a sphere at the specified position using the specified radius.
*/
Sphere(Value xv, Value yv, Value zv, Value rv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Sphere(const Sphere & o);
Sphere(const Sphere & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~Sphere();
Sphere(Sphere && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Sphere & operator = (const Sphere & o);
~Sphere() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Sphere & operator = (const Sphere & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Sphere & operator = (Sphere && o) = default;
/* --------------------------------------------------------------------------------------------
* Radius assignment operator.
*/
Sphere & operator = (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Position assignment operator.
*/
Sphere & operator = (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Sphere & operator += (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Sphere & operator -= (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Sphere & operator *= (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Sphere & operator /= (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Sphere & operator %= (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* ...
* Radius addition assignment operator.
*/
Sphere & operator += (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius subtraction assignment operator.
*/
Sphere & operator -= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius multiplication assignment operator.
*/
Sphere & operator *= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius division assignment operator.
*/
Sphere & operator /= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius modulo assignment operator.
*/
Sphere & operator %= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Position addition assignment operator.
*/
Sphere & operator += (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position subtraction assignment operator.
*/
Sphere & operator -= (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position multiplication assignment operator.
*/
Sphere & operator *= (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position division assignment operator.
*/
Sphere & operator /= (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position modulo assignment operator.
*/
Sphere & operator %= (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Sphere & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Sphere & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Sphere operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Sphere operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Sphere operator + (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Sphere operator - (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Sphere operator * (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Sphere operator / (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Sphere operator % (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius addition operator.
*/
Sphere operator + (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius subtraction operator.
*/
Sphere operator - (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius multiplication operator.
*/
Sphere operator * (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius division operator.
*/
Sphere operator / (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius modulo operator.
*/
Sphere operator % (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position addition operator.
*/
Sphere operator + (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position subtraction operator.
*/
Sphere operator - (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position multiplication operator.
*/
Sphere operator * (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position division operator.
*/
Sphere operator / (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position modulo operator.
*/
Sphere operator % (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Sphere operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Sphere operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set the specified radius.
*/
void Set(Value nr);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the sphere from another instance of this type.
*/
void Set(const Sphere & ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set the position from the specified position.
*/
void Set(const Vector3 & np);
/* --------------------------------------------------------------------------------------------
* ...
* Set the specified position and radius.
*/
void Set(const Vector3 & np, Value nr);
/* --------------------------------------------------------------------------------------------
* ...
* Set the specified position.
*/
void Set(Value nx, Value ny, Value nz);
/* --------------------------------------------------------------------------------------------
* ...
* Set the specified position and radius.
*/
void Set(Value nx, Value ny, Value nz, Value nr);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly sized and positioned sphere.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly sized or positioned sphere within the specified bounds.
*/
void Generate(Value min, Value max, bool r);
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly positioned sphere within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax);
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly sized and positioned sphere within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value rmin, Value rmax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -365,7 +380,7 @@ struct Sphere
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
Sphere Abs() const;
};

View File

@ -15,6 +15,14 @@ const Vector2 Vector2::MAX = Vector2(NumLimit< Vector2::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Vector2::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Vector2::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Vector2");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Vector2::Vector2()
: x(0.0), y(0.0)
@ -36,27 +44,6 @@ Vector2::Vector2(Value xv, Value yv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2::Vector2(const Vector2 & o)
: x(o.x), y(o.y)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2::~Vector2()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator = (const Vector2 & o)
{
x = o.x;
y = o.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator = (Value s)
{
@ -290,7 +277,7 @@ Int32 Vector2::Cmp(const Vector2 & o) const
// ------------------------------------------------------------------------------------------------
CSStr Vector2::ToString() const
{
return ToStringF("%f,%f", x, y);
return ToStrF("%f,%f", x, y);
}
// ------------------------------------------------------------------------------------------------
@ -335,27 +322,19 @@ void Vector2::Generate()
void Vector2::Generate(Value min, Value max)
{
if (EpsLt(max, min))
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
}
void Vector2::Generate(Value xmin, Value xmax, Value ymin, Value ymax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin))
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(ymin, ymax);
y = GetRandomFloat32(xmin, xmax);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(ymin, ymax);
y = GetRandomFloat32(xmin, xmax);
}
// ------------------------------------------------------------------------------------------------
@ -383,6 +362,7 @@ void Register_Vector2(HSQUIRRELVM vm)
.Prop(_SC("abs"), &Vector2::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &Vector2::ToString)
.SquirrelFunc(_SC("_typename"), &Vector2::Typename)
.Func(_SC("_cmp"), &Vector2::Cmp)
/* Metamethods */
.Func<Vector2 (Vector2::*)(const Vector2 &) const>(_SC("_add"), &Vector2::operator +)

View File

@ -8,289 +8,304 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent a two-dimensional vector.
*/
struct Vector2
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Vector2 NIL;
static const Vector2 MIN;
static const Vector2 MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The x and y components of this type.
*/
Value x, y;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Vector2();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the same scalar value for all components.
*/
Vector2(Value sv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the specified component values.
*/
Vector2(Value xv, Value yv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Vector2(const Vector2 & o);
Vector2(const Vector2 & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~Vector2();
Vector2(Vector2 && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Vector2 & operator = (const Vector2 & o);
~Vector2() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Vector2 & operator = (const Vector2 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Vector2 & operator = (Vector2 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Vector2 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* String assignment operator.
*/
Vector2 & operator = (CSStr values);
/* --------------------------------------------------------------------------------------------
* ...
* Integral two-dimensional vector assignment.
*/
Vector2 & operator = (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Vector2 & operator += (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Vector2 & operator -= (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Vector2 & operator *= (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Vector2 & operator /= (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Vector2 & operator %= (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
Vector2 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
Vector2 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
Vector2 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
Vector2 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
Vector2 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Vector2 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Vector2 & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Vector2 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Vector2 operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Vector2 operator + (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Vector2 operator - (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Vector2 operator * (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Vector2 operator / (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Vector2 operator % (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
Vector2 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
Vector2 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
Vector2 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
Vector2 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
Vector2 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Vector2 operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Vector2 operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nx, Value ny);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from another instance of this type.
*/
void Set(const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from an integral two-dimensional vector.
*/
void Set(const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -298,7 +313,7 @@ struct Vector2
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
Vector2 Abs() const;
};

View File

@ -15,6 +15,14 @@ const Vector2i Vector2i::MAX = Vector2i(NumLimit< Vector2i::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Vector2i::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Vector2i::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Vector2i");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Vector2i::Vector2i()
: x(0), y(0)
@ -36,27 +44,6 @@ Vector2i::Vector2i(Value xv, Value yv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2i::Vector2i(const Vector2i & o)
: x(o.x), y(o.y)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2i::~Vector2i()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator = (const Vector2i & o)
{
x = o.x;
y = o.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator = (Value s)
{
@ -416,7 +403,7 @@ Int32 Vector2i::Cmp(const Vector2i & o) const
// ------------------------------------------------------------------------------------------------
CSStr Vector2i::ToString() const
{
return ToStringF("%d,%d", x, y);
return ToStrF("%d,%d", x, y);
}
// ------------------------------------------------------------------------------------------------
@ -461,27 +448,19 @@ void Vector2i::Generate()
void Vector2i::Generate(Value min, Value max)
{
if (max < min)
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomInt32(min, max);
y = GetRandomInt32(min, max);
}
SqThrowF("max value is lower than min value");
x = GetRandomInt32(min, max);
y = GetRandomInt32(min, max);
}
void Vector2i::Generate(Value xmin, Value xmax, Value ymin, Value ymax)
{
if (xmax < xmin || ymax < ymin)
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomInt32(ymin, ymax);
y = GetRandomInt32(xmin, xmax);
}
SqThrowF("max value is lower than min value");
x = GetRandomInt32(ymin, ymax);
y = GetRandomInt32(xmin, xmax);
}
// ------------------------------------------------------------------------------------------------
@ -509,6 +488,7 @@ void Register_Vector2i(HSQUIRRELVM vm)
.Prop(_SC("abs"), &Vector2i::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &Vector2i::ToString)
.SquirrelFunc(_SC("_typename"), &Vector2i::Typename)
.Func(_SC("_cmp"), &Vector2i::Cmp)
/* Metamethods */
.Func<Vector2i (Vector2i::*)(const Vector2i &) const>(_SC("_add"), &Vector2i::operator +)

View File

@ -8,394 +8,409 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent a two-dimensional vector using integral values.
*/
struct Vector2i
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef int Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Vector2i NIL;
static const Vector2i MIN;
static const Vector2i MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The x and y components of this type.
*/
Value x, y;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Vector2i();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the same scalar value for all components.
*/
Vector2i(Value sv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the specified component values.
*/
Vector2i(Value xv, Value yv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Vector2i(const Vector2i & o);
Vector2i(const Vector2i & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
~Vector2i();
Vector2i(Vector2i && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Vector2i & operator = (const Vector2i & o);
~Vector2i() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Vector2i & operator = (const Vector2i & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Vector2i & operator = (Vector2i && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Vector2i & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* String assignment operator.
*/
Vector2i & operator = (CSStr values);
/* --------------------------------------------------------------------------------------------
* ...
* Real two-dimensional vector assignment.
*/
Vector2i & operator = (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Vector2i & operator += (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Vector2i & operator -= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Vector2i & operator *= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Vector2i & operator /= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Vector2i & operator %= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise AND assignment operator.
*/
Vector2i & operator &= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise OR assignment operator.
*/
Vector2i & operator |= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise XOR assignment operator.
*/
Vector2i & operator ^= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise left shift assignment operator.
*/
Vector2i & operator <<= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise right shift assignment operator.
*/
Vector2i & operator >>= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
Vector2i & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
Vector2i & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
Vector2i & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
Vector2i & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
Vector2i & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise AND assignment operator.
*/
Vector2i & operator &= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise OR assignment operator.
*/
Vector2i & operator |= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise XOR assignment operator.
*/
Vector2i & operator ^= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise left shift assignment operator.
*/
Vector2i & operator <<= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise right shift assignment operator.
*/
Vector2i & operator >>= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Vector2i & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Vector2i & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Vector2i operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Vector2i operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Vector2i operator + (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Vector2i operator - (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Vector2i operator * (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Vector2i operator / (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Vector2i operator % (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise AND operator.
*/
Vector2i operator & (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise OR operator.
*/
Vector2i operator | (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise XOR operator.
*/
Vector2i operator ^ (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise shift left operator.
*/
Vector2i operator << (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise shift right operator.
*/
Vector2i operator >> (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
Vector2i operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
Vector2i operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
Vector2i operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
Vector2i operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
Vector2i operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise AND operator.
*/
Vector2i operator & (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise OR operator.
*/
Vector2i operator | (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise XOR operator.
*/
Vector2i operator ^ (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise shift left operator.
*/
Vector2i operator << (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise shift right operator.
*/
Vector2i operator >> (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Vector2i operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Vector2i operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise NOT operator.
*/
Vector2i operator ~ () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nx, Value ny);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from another instance of this type.
*/
void Set(const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from a real two-dimensional vector.
*/
void Set(const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -403,7 +418,7 @@ struct Vector2i
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
Vector2i Abs() const;
};

View File

@ -16,6 +16,14 @@ const Vector3 Vector3::MAX = Vector3(NumLimit< Vector3::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Vector3::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Vector3::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Vector3");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Vector3::Vector3()
: x(0.0), y(0.0), z(0.0)
@ -37,28 +45,6 @@ Vector3::Vector3(Value xv, Value yv, Value zv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector3::Vector3(const Vector3 & o)
: x(o.x), y(o.y), z(o.z)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector3::~Vector3()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator = (const Vector3 & o)
{
x = o.x;
y = o.y;
z = o.z;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator = (Value s)
{
@ -310,7 +296,7 @@ Int32 Vector3::Cmp(const Vector3 & o) const
// ------------------------------------------------------------------------------------------------
CSStr Vector3::ToString() const
{
return ToStringF("%f,%f,%f", x, y, z);
return ToStrF("%f,%f,%f", x, y, z);
}
// ------------------------------------------------------------------------------------------------
@ -367,29 +353,21 @@ void Vector3::Generate()
void Vector3::Generate(Value min, Value max)
{
if (EpsLt(max, min))
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
z = GetRandomFloat32(min, max);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
z = GetRandomFloat32(min, max);
}
void Vector3::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(zmax, zmin))
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(xmin, xmax);
y = GetRandomFloat32(ymin, ymax);
z = GetRandomFloat32(zmin, zmax);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(xmin, xmax);
y = GetRandomFloat32(ymin, ymax);
z = GetRandomFloat32(zmin, zmax);
}
// ------------------------------------------------------------------------------------------------
@ -418,6 +396,7 @@ void Register_Vector3(HSQUIRRELVM vm)
.Prop(_SC("abs"), &Vector3::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &Vector3::ToString)
.SquirrelFunc(_SC("_typename"), &Vector3::Typename)
.Func(_SC("_cmp"), &Vector3::Cmp)
/* Metamethods */
.Func<Vector3 (Vector3::*)(const Vector3 &) const>(_SC("_add"), &Vector3::operator +)

View File

@ -8,294 +8,309 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent a three-dimensional vector.
*/
struct Vector3
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Vector3 NIL;
static const Vector3 MIN;
static const Vector3 MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The x, y and z components of this type.
*/
Value x, y, z;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Vector3();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the same scalar value for all components.
*/
Vector3(Value sv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the specified component values.
*/
Vector3(Value xv, Value yv, Value zv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Vector3(const Vector3 & o);
Vector3(const Vector3 & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
~Vector3();
Vector3(Vector3 && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Vector3 & operator = (const Vector3 & o);
~Vector3() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Vector3 & operator = (const Vector3 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Vector3 & operator = (Vector3 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Vector3 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Four-dimensional vector assignment.
*/
Vector3 & operator = (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Quaternion rotation assignment.
*/
Vector3 & operator = (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Vector3 & operator += (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Vector3 & operator -= (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Vector3 & operator *= (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Vector3 & operator /= (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Vector3 & operator %= (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
Vector3 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
Vector3 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
Vector3 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
Vector3 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
Vector3 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Vector3 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Vector3 & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Vector3 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Vector3 operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Vector3 operator + (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Vector3 operator - (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Vector3 operator * (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Vector3 operator / (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Vector3 operator % (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
Vector3 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
Vector3 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
Vector3 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
Vector3 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
Vector3 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Vector3 operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Vector3 operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nx, Value ny, Value nz);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from another instance of this type.
*/
void Set(const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from a four-dimensional vector.
*/
void Set(const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from a quaternion rotation.
*/
void Set(const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -303,7 +318,7 @@ struct Vector3
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
Vector3 Abs() const;
};

View File

@ -16,6 +16,14 @@ const Vector4 Vector4::MAX = Vector4(NumLimit< Vector4::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Vector4::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Vector4::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Vector4");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Vector4::Vector4()
: x(0.0), y(0.0), z(0.0), w(0.0)
@ -44,29 +52,6 @@ Vector4::Vector4(Value xv, Value yv, Value zv, Value wv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector4::Vector4(const Vector4 & o)
: x(o.x), y(o.y), z(o.z), w(o.w)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector4::~Vector4()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator = (const Vector4 & o)
{
x = o.x;
y = o.y;
z = o.z;
w = o.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator = (Value s)
{
@ -335,7 +320,7 @@ Int32 Vector4::Cmp(const Vector4 & o) const
// ------------------------------------------------------------------------------------------------
CSStr Vector4::ToString() const
{
return ToStringF("%f,%f,%f,%f", x, y, z, w);
return ToStrF("%f,%f,%f,%f", x, y, z, w);
}
// ------------------------------------------------------------------------------------------------
@ -405,31 +390,23 @@ void Vector4::Generate()
void Vector4::Generate(Value min, Value max)
{
if (max < min)
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
z = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
z = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
}
void Vector4::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value wmin, Value wmax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(zmax, zmin) || EpsLt(wmax, wmin))
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(xmin, xmax);
y = GetRandomFloat32(ymin, ymax);
z = GetRandomFloat32(zmin, zmax);
y = GetRandomFloat32(ymin, ymax);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(xmin, xmax);
y = GetRandomFloat32(ymin, ymax);
z = GetRandomFloat32(zmin, zmax);
y = GetRandomFloat32(ymin, ymax);
}
// ------------------------------------------------------------------------------------------------
@ -460,6 +437,7 @@ void Register_Vector4(HSQUIRRELVM vm)
.Prop(_SC("abs"), &Vector4::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &Vector4::ToString)
.SquirrelFunc(_SC("_typename"), &Vector4::Typename)
.Func(_SC("_cmp"), &Vector4::Cmp)
/* Metamethods */
.Func<Vector4 (Vector4::*)(const Vector4 &) const>(_SC("_add"), &Vector4::operator +)

View File

@ -8,304 +8,319 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent a four-dimensional vector.
*/
struct Vector4
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Vector4 NIL;
static const Vector4 MIN;
static const Vector4 MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The x, y, z and w components of this type.
*/
Value x, y, z, w;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Vector4();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the same scalar value for all components.
*/
Vector4(Value sv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the specified component values.
*/
Vector4(Value xv, Value yv, Value zv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the specified component values.
*/
Vector4(Value xv, Value yv, Value zv, Value wv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Vector4(const Vector4 & o);
Vector4(const Vector4 & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~Vector4();
Vector4(Vector4 && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Vector4 & operator = (const Vector4 & o);
~Vector4() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Vector4 & operator = (const Vector4 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Vector4 & operator = (Vector4 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Vector4 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Three-dimensional vector assignment operator.
*/
Vector4 & operator = (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Quaternion rotation assignment operator.
*/
Vector4 & operator = (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Vector4 & operator += (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Vector4 & operator -= (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Vector4 & operator *= (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Vector4 & operator /= (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Vector4 & operator %= (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
Vector4 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
Vector4 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
Vector4 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
Vector4 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
Vector4 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Vector4 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Vector4 & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Vector4 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Vector4 operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Vector4 operator + (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Vector4 operator - (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Vector4 operator * (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Vector4 operator / (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Vector4 operator % (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
Vector4 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
Vector4 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
Vector4 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
Vector4 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
Vector4 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Vector4 operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Vector4 operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nx, Value ny, Value nz);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nx, Value ny, Value nz, Value nw);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from another instance of this type.
*/
void Set(const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from a three-dimensional vector.
*/
void Set(const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from a quaternion rotation.
*/
void Set(const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value wmin, Value wmax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -313,7 +328,7 @@ struct Vector4
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
Vector4 Abs() const;
};

File diff suppressed because it is too large Load Diff

View File

@ -165,10 +165,19 @@ protected:
*/
template < typename T > void SqError(Int32 type, CSStr msg, T data)
{
if (!m_OnError.IsNull())
// Is there a callback that deals with errors?
if (m_OnError.IsNull())
return;
// Attempt to forward the error to that callback
try
{
m_OnError.Execute< Int32, CSStr, T >(type, msg, data);
}
catch (const Sqrat::Exception & e)
{
// We can only log this incident and in the future maybe also include the location
LogErr("Command error callback failed [%s]", e.Message().c_str());
}
}
/* --------------------------------------------------------------------------------------------
@ -350,7 +359,7 @@ protected:
/* --------------------------------------------------------------------------------------------
*
*/
bool ProcSpec(CSStr spec);
void ProcSpec(CSStr spec);
private:

View File

@ -48,13 +48,6 @@ extern void TerminateCommand();
// ------------------------------------------------------------------------------------------------
Core * _Core = NULL;
// ------------------------------------------------------------------------------------------------
static void CalculateStringIDs(SQChar arr[][8], Uint32 num)
{
for (Uint32 n = 0; n < num; n++)
snprintf(arr[n], 8, "%d", n);
}
// ------------------------------------------------------------------------------------------------
Core::Core()
: m_State(0)
@ -100,19 +93,6 @@ bool Core::Init()
m_Textdraws.resize(SQMOD_TEXTDRAW_POOL);
m_Vehicles.resize(SQMOD_VEHICLE_POOL);
LogDbg("Pre-calculating entity string identifiers");
// Pre-calculate all possible entity IDs for fast string conversion
CalculateStringIDs(CBlip::s_StrID, SQMOD_BLIP_POOL);
CalculateStringIDs(CCheckpoint::s_StrID, SQMOD_CHECKPOINT_POOL);
CalculateStringIDs(CForcefield::s_StrID, SQMOD_FORCEFIELD_POOL);
CalculateStringIDs(CKeybind::s_StrID, SQMOD_KEYBIND_POOL);
CalculateStringIDs(CObject::s_StrID, SQMOD_OBJECT_POOL);
CalculateStringIDs(CPickup::s_StrID, SQMOD_PICKUP_POOL);
CalculateStringIDs(CPlayer::s_StrID, SQMOD_PLAYER_POOL);
CalculateStringIDs(CSprite::s_StrID, SQMOD_SPRITE_POOL);
CalculateStringIDs(CTextdraw::s_StrID, SQMOD_TEXTDRAW_POOL);
CalculateStringIDs(CVehicle::s_StrID, SQMOD_VEHICLE_POOL);
LogDbg("Initializing entity options to defaults");
// Initialize player messaging options to default values
for (Players::iterator itr = m_Players.begin(); itr != m_Players.end(); ++itr)
@ -154,148 +134,130 @@ bool Core::Init()
// Attempt to read the database port number
try
{
Ulong num = strtoul(conf.GetValue("Config", "StackSize", "0"), NULL, 10);
// Make sure that the retrieved number is in range
Ulong num = conf.GetLongValue("Config", "StackSize", SQMOD_STACK_SIZE);
// Make sure that the retrieved number is within range
if (!num)
{
throw std::out_of_range("stack size too small");
}
else if (num >= NumLimit< Uint16 >::Max)
{
else if (num >= std::numeric_limits< Uint16 >::max())
throw std::out_of_range("stack size too big");
}
// Save the port number
stack_size = (Uint16)num;
stack_size = static_cast< Uint16 >(num);
}
catch (const std::exception & e)
{
LogWrn("Unable to obtain the stack size [%s]", e.what());
LogWrn("Unable to obtain a valid stack size [%s]", e.what());
}
LogDbg("Creating virtual machine with a stack size (%d)", stack_size);
LogDbg("Creating a virtual machine with a stack size of (%d)", stack_size);
// Attempt to create the VM
m_VM = sq_open(stack_size);
// See if the virtual machine could be created
if (cLogFtl(!m_VM, "Unable to create the virtual machine"))
{
m_VM = NULL;
// Explicitly prevent further use of this pointer
m_VM = nullptr;
// Unable to load the plugin properly
return false;
}
// Set this as the default VM and enable error handling
// Set this as the default VM
DefaultVM::Set(m_VM);
// Enable error handling
ErrorHandling::Enable(true);
LogDbg("Registering the standard libraries");
// Register the standard library on the root table
// Push the root table on the stack
sq_pushroottable(m_VM);
// Register the standard library on the pushed table
sqstd_register_iolib(m_VM);
sqstd_register_bloblib(m_VM);
sqstd_register_mathlib(m_VM);
sqstd_register_systemlib(m_VM);
sqstd_register_stringlib(m_VM);
// Pop the root table from the stack
sq_pop(m_VM, 1);
LogDbg("Setting the base output function");
// Tell the VM to use these functions to output user on error messages
sq_setprintfunc(m_VM, PrintFunc, ErrorFunc);
LogDbg("Setting the base error handlers");
// Tell the VM to trigger this function on compile time errors
sq_setcompilererrorhandler(m_VM, CompilerErrorHandler);
// Push the runtime error handler on the stack and create a closure
sq_newclosure(m_VM, RuntimeErrorHandler, 0);
// Tell the VM to trigger this function on runtime errors
sq_seterrorhandler(m_VM);
LogDbg("Registering the plug-in API");
// Attempt to register the plugin API
if (cLogFtl(!RegisterAPI(m_VM), "Unable to register the plug-in API"))
return false;
return false; // Can't execute scripts without a valid API!
// Attempt to retrieve the list of strings specified in the config
CSimpleIniA::TNamesDepend scripts;
conf.GetAllValues("Scripts", "Source", scripts);
// See if any script was specified
if (scripts.size() <= 0)
if (scripts.size() <= 0 && !conf.GetBoolValue("Config", "EmptyInit", false))
{
LogWrn("No scripts specified in the configuration file");
// No point in loading the plug-in
return false;
}
// Sort the list in it's original order
scripts.sort(CSimpleIniA::Entry::LoadOrder());
// Process each specified script path
for (CSimpleIniA::TNamesDepend::iterator itr = scripts.begin(); itr != scripts.end(); ++itr)
else
{
// Get the file path as a string
String path(itr->pItem);
// See if the specified script path is valid
if (path.empty())
{
// Simply ignore it
continue;
}
// See if it wasn't already loaded
else if (m_Scripts.find(path) != m_Scripts.end())
{
LogWrn("Script was specified before: %s", path.c_str());
// No point in loading it again
continue;
}
// Create a new script container and insert it into the script pool
std::pair< Scripts::iterator, bool > res = m_Scripts.insert(Scripts::value_type(path, Script(m_VM)));
// We don't compile the scripts yet. We just store their path and prepare the objects.
if (!res.second)
{
LogErr("Unable to queue script: %s", path.c_str());
// Drop all previous scripts
m_Scripts.clear();
// Failed to compile the specified script
return false;
}
// Sort the list in it's original order
scripts.sort(CSimpleIniA::Entry::LoadOrder());
// Process each specified script paths
for (const auto & script : scripts)
// Attempt to queue the specified script path for loading
LoadScript(script.pItem);
}
// See if any script could be compiled
// See if any script could be queued for loading
if (m_Scripts.empty() && !conf.GetBoolValue("Config", "EmptyInit", false))
{
LogErr("No scripts compiled. No reason to load the plug-in");
LogErr("No scripts loaded. No reason to load the plug-in");
// No point in loading the plug-in
return false;
}
LogDbg("Reading the options from the general section");
// Read options only after compilation was successful
// Read options only after loading was successful
CSimpleIniA::TNamesDepend options;
// Are there any options to load?
// Are there any options to read?
if (conf.GetAllKeys("Options", options) || options.size() > 0)
{
// Process all the specified keys under the [Options] section
for (CSimpleIniA::TNamesDepend::iterator itr = options.begin(); itr != options.end(); ++itr)
for (const auto & option : options)
{
CSimpleIniA::TNamesDepend optlist;
// Get all keys with the same name
if (!conf.GetAllValues("Options", itr->pItem, optlist))
{
CSimpleIniA::TNamesDepend values;
// Get the values of all keys with the same name
if (!conf.GetAllValues("Options", option.pItem, values))
continue;
}
// Sort the keys in their original order
optlist.sort(CSimpleIniA::Entry::LoadOrder());
// Process each option and overwrite existing values
for (CSimpleIniA::TNamesDepend::iterator opt = optlist.begin(); opt != optlist.end(); ++opt)
{
m_Options[itr->pItem] = opt->pItem;
}
values.sort(CSimpleIniA::Entry::LoadOrder());
// Save each option option and overwrite existing value
for (const auto & value : values)
m_Options[option.pItem] = value.pItem;
}
}
else
{
LogInf("No options specified in the configuration file");
}
LogDbg("Applying the specified logging filters");
// Apply the specified logging filters only after initialization was completed
if (!SToB(conf.GetValue("Log", "Debug", "true"))) _Log->DisableLevel(LL_DBG);
if (!SToB(conf.GetValue("Log", "User", "true"))) _Log->DisableLevel(LL_USR);
if (!SToB(conf.GetValue("Log", "Success", "true"))) _Log->DisableLevel(LL_SCS);
if (!SToB(conf.GetValue("Log", "Info", "true"))) _Log->DisableLevel(LL_INF);
if (!SToB(conf.GetValue("Log", "Warning", "true"))) _Log->DisableLevel(LL_WRN);
if (!SToB(conf.GetValue("Log", "Error", "true"))) _Log->DisableLevel(LL_ERR);
if (!SToB(conf.GetValue("Log", "Fatal", "true"))) _Log->DisableLevel(LL_FTL);
if (!conf.GetBoolValue("Log", "Debug", true))
_Log->DisableLevel(LL_DBG);
if (!conf.GetBoolValue("Log", "User", true))
_Log->DisableLevel(LL_USR);
if (!conf.GetBoolValue("Log", "Success", true))
_Log->DisableLevel(LL_SCS);
if (!conf.GetBoolValue("Log", "Info", true))
_Log->DisableLevel(LL_INF);
if (!conf.GetBoolValue("Log", "Warning", true))
_Log->DisableLevel(LL_WRN);
if (!conf.GetBoolValue("Log", "Error", true))
_Log->DisableLevel(LL_ERR);
if (!conf.GetBoolValue("Log", "Fatal", true))
_Log->DisableLevel(LL_FTL);
// Initialization successful
return true;
@ -306,7 +268,8 @@ bool Core::Load()
{
// Are there any scripts to execute?
if (cLogErr(m_Scripts.empty(), "No scripts to execute. Plug-in has no purpose"))
return false;
return false; // No reason to load the plug-in
LogDbg("Signaling outside plugins to register their API");
// Signal outside plugins to do their monkey business
_Func->SendCustomCommand(0xDEADBABE, "");
@ -316,18 +279,29 @@ bool Core::Load()
for (Scripts::iterator itr = m_Scripts.begin(); itr != m_Scripts.end(); ++itr)
{
// Attempt to load and compile the script file
itr->second.CompileFile(itr->first);
// See if any compile time error occurred during compilation
if (Error::Occurred(m_VM))
return false; /* Failed to load properly */
// Attempt to execute the script
itr->second.Run();
// See if the executed script had any errors
if (Error::Occurred(m_VM))
// Failed to execute scripts
return false; /* Failed to load properly */
else
LogScs("Successfully executed script: %s", itr->first.c_str());
try
{
itr->second.CompileFile(itr->first);
}
catch (const Sqrat::Exception & e)
{
LogFtl("Unable to compile: %s", itr->first.c_str());
// Failed to load properly
return false;
}
// Attempt to execute the compiled script code
try
{
itr->second.Run();
}
catch (const Sqrat::Exception & e)
{
LogFtl("Unable to execute: %s", itr->first.c_str());
// Failed to load properly
return false;
}
// At this point the script should be completely loaded
LogScs("Successfully executed script: %s", itr->first.c_str());
}
// Successfully loaded
return true;
@ -382,6 +356,25 @@ void Core::SetOption(const String & name, const String & value)
m_Options[name] = value;
}
// ------------------------------------------------------------------------------------------------
bool Core::LoadScript(CSStr filepath)
{
// Is the specified path empty?
if (!filepath || *filepath == 0)
return false; // Simply ignore it
// Get the file path as a string
String path(filepath);
// See if it wasn't already loaded
if (m_Scripts.find(path) != m_Scripts.end())
LogWrn("Script was specified before: %s", path.c_str());
// We don't compile the scripts yet. We just store their path and prepare the objects.
else
// Create a new script container and insert it into the script pool
m_Scripts.emplace(std::move(path), Script(m_VM));
// At this point the script exists in the pool
return true;
}
// ------------------------------------------------------------------------------------------------
void Core::PrintFunc(HSQUIRRELVM vm, CSStr msg, ...)
{
@ -1720,8 +1713,8 @@ void Core::ResetInst(BlipInst & inst)
inst.mWorld = -1;
inst.mScale = -1;
inst.mSprID = -1;
//inst.mPosition.Clear();
//inst.mColor.Clear();
inst.mPosition.Clear();
inst.mColor.Clear();
}
void Core::ResetInst(CheckpointInst & inst)
@ -1740,9 +1733,9 @@ void Core::ResetInst(KeybindInst & inst)
{
inst.mID = -1;
inst.mFlags = ENF_DEFAULT;
inst.mPrimary = -1;
inst.mSecondary = -1;
inst.mAlternative = -1;
inst.mFirst = -1;
inst.mSecond = -1;
inst.mThird = -1;
inst.mRelease = -1;
}

View File

@ -155,9 +155,9 @@ protected:
Object mObj;
// ----------------------------------------------------------------------------------------
Int32 mPrimary;
Int32 mSecondary;
Int32 mAlternative;
Int32 mFirst;
Int32 mSecond;
Int32 mThird;
Int32 mRelease;
// ----------------------------------------------------------------------------------------
@ -392,6 +392,8 @@ protected:
Function mOnDisembark;
};
public:
// --------------------------------------------------------------------------------------------
typedef std::vector< BlipInst > Blips;
typedef std::vector< CheckpointInst > Checkpoints;
@ -483,8 +485,15 @@ public:
/* --------------------------------------------------------------------------------------------
* State mutators.
*/
void SetState(Int32 val) { m_State = val; }
Int32 GetState() const { return m_State; }
void SetState(Int32 val)
{
m_State = val;
}
Int32 GetState() const
{
return m_State;
}
/* --------------------------------------------------------------------------------------------
* Option mutators.
@ -495,7 +504,15 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the virtual machine.
*/
HSQUIRRELVM GetVM() const { return m_VM; }
HSQUIRRELVM GetVM() const
{
return m_VM;
}
/* --------------------------------------------------------------------------------------------
* Adds a script to the load queue.
*/
bool LoadScript(CSStr filepath);
protected:
@ -578,6 +595,20 @@ public:
TextdrawInst & GetTextdraw(Int32 id) { return m_Textdraws.at(id); }
VehicleInst & GetVehicle(Int32 id) { return m_Vehicles.at(id); }
/* --------------------------------------------------------------------------------------------
* Pool retrievers.
*/
const Blips & GetBlips() const { return m_Blips; }
const Checkpoints & GetCheckpoints() const { return m_Checkpoints; }
const Forcefields & GetForcefields() const { return m_Forcefields; }
const Keybinds & GetKeybinds() const { return m_Keybinds; }
const Objects & GetObjects() const { return m_Objects; }
const Pickups & GetPickups() const { return m_Pickups; }
const Players & GetPlayers() const { return m_Players; }
const Sprites & GetSprites() const { return m_Sprites; }
const Textdraws & GetTextdraws() const { return m_Textdraws; }
const Vehicles & GetVehicles() const { return m_Vehicles; }
protected:
/* --------------------------------------------------------------------------------------------

View File

@ -6,15 +6,20 @@
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQChar CBlip::s_StrID[SQMOD_BLIP_POOL][8];
const Int32 CBlip::Max = SQMOD_BLIP_POOL;
// ------------------------------------------------------------------------------------------------
const Int32 CBlip::Max = SQMOD_BLIP_POOL;
SQInteger CBlip::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("SqBlip");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
CBlip::CBlip(Int32 id)
: m_ID(VALID_ENTITYGETEX(id, SQMOD_BLIP_POOL))
, m_Tag(VALID_ENTITY(m_ID) ? s_StrID[m_ID] : _SC("-1"))
, m_Tag(ToStrF("%d", id))
{
/* ... */
}
@ -36,153 +41,183 @@ Int32 CBlip::Cmp(const CBlip & o) const
return -1;
}
CSStr CBlip::ToString() const
// ------------------------------------------------------------------------------------------------
const String & CBlip::ToString() const
{
return VALID_ENTITYEX(m_ID, SQMOD_BLIP_POOL) ? s_StrID[m_ID] : _SC("-1");
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
CSStr CBlip::GetTag() const
const String & CBlip::GetTag() const
{
return m_Tag.c_str();
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
void CBlip::SetTag(CSStr tag)
{
m_Tag.assign(tag);
}
// ------------------------------------------------------------------------------------------------
Object & CBlip::GetData()
{
if (Validate())
return m_Data;
return NullObject();
// Validate the managed identifier
Validate();
// Return the requested information
return m_Data;
}
// ------------------------------------------------------------------------------------------------
void CBlip::SetData(Object & data)
{
if (Validate())
m_Data = data;
// Validate the managed identifier
Validate();
// Apply the specified value
m_Data = data;
}
// ------------------------------------------------------------------------------------------------
bool CBlip::Destroy(Int32 header, Object & payload)
{
// Validate the managed identifier
Validate();
// Perform the requested operation
return _Core->DelBlip(m_ID, header, payload);
}
// ------------------------------------------------------------------------------------------------
bool CBlip::BindEvent(Int32 evid, Object & env, Function & func) const
void CBlip::BindEvent(Int32 evid, Object & env, Function & func) const
{
if (!Validate())
return false;
// Validate the managed identifier
Validate();
// Obtain the function instance called for this event
Function & event = _Core->GetBlipEvent(m_ID, evid);
// Is the specified callback function null?
if (func.IsNull())
event.Release();
event.Release(); // Then release the current callback
// Assign the specified environment and function
else
event = Function(env.GetVM(), env, func.GetFunc());
return true;
}
// ------------------------------------------------------------------------------------------------
Int32 CBlip::GetWorld() const
{
if (Validate())
return _Core->GetBlip(m_ID).mWorld;
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetBlip(m_ID).mWorld;
}
// ------------------------------------------------------------------------------------------------
Int32 CBlip::GetScale() const
{
if (Validate())
return _Core->GetBlip(m_ID).mScale;
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetBlip(m_ID).mScale;
}
// ------------------------------------------------------------------------------------------------
const Vector3 & CBlip::GetPosition() const
{
if (Validate())
return _Core->GetBlip(m_ID).mPosition;
return Vector3::NIL;
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetBlip(m_ID).mPosition;
}
// ------------------------------------------------------------------------------------------------
const Color4 & CBlip::GetColor() const
{
if (Validate())
return _Core->GetBlip(m_ID).mColor;
return Color4::NIL;
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetBlip(m_ID).mColor;
}
// ------------------------------------------------------------------------------------------------
Int32 CBlip::GetSprID() const
{
if (Validate())
return _Core->GetBlip(m_ID).mSprID;
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetBlip(m_ID).mSprID;
}
// ------------------------------------------------------------------------------------------------
Float32 CBlip::GetPosX() const
{
if (Validate())
return _Core->GetBlip(m_ID).mPosition.x;
return 0;
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetBlip(m_ID).mPosition.x;
}
// ------------------------------------------------------------------------------------------------
Float32 CBlip::GetPosY() const
{
if (Validate())
return _Core->GetBlip(m_ID).mPosition.y;
return 0;
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetBlip(m_ID).mPosition.y;
}
// ------------------------------------------------------------------------------------------------
Float32 CBlip::GetPosZ() const
{
if (Validate())
return _Core->GetBlip(m_ID).mPosition.z;
return 0;
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetBlip(m_ID).mPosition.z;
}
// ------------------------------------------------------------------------------------------------
Int32 CBlip::GetColorR() const
{
if (Validate())
return _Core->GetBlip(m_ID).mColor.r;
return 0;
}
Int32 CBlip::GetColorG() const
{
if (Validate())
return _Core->GetBlip(m_ID).mColor.g;
return 0;
}
Int32 CBlip::GetColorB() const
{
if (Validate())
return _Core->GetBlip(m_ID).mColor.b;
return 0;
}
Int32 CBlip::GetColorA() const
{
if (Validate())
return _Core->GetBlip(m_ID).mColor.a;
return 0;
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetBlip(m_ID).mColor.r;
}
// ------------------------------------------------------------------------------------------------
static Object & CreateBlipEx(Int32 world, Float32 x, Float32 y, Float32 z, Int32 scale,
Int32 CBlip::GetColorG() const
{
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetBlip(m_ID).mColor.g;
}
// ------------------------------------------------------------------------------------------------
Int32 CBlip::GetColorB() const
{
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetBlip(m_ID).mColor.b;
}
// ------------------------------------------------------------------------------------------------
Int32 CBlip::GetColorA() const
{
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetBlip(m_ID).mColor.a;
}
// ------------------------------------------------------------------------------------------------
static Object & Blip_CreateEx(Int32 world, Float32 x, Float32 y, Float32 z, Int32 scale,
Uint8 r, Uint8 g, Uint8 b, Uint8 a, Int32 sprid)
{
return _Core->NewBlip(-1, world, x, y, z, scale, SQMOD_PACK_RGBA(r, g, b, a), sprid,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateBlipEx(Int32 world, Float32 x, Float32 y, Float32 z, Int32 scale,
static Object & Blip_CreateEx(Int32 world, Float32 x, Float32 y, Float32 z, Int32 scale,
Uint8 r, Uint8 g, Uint8 b, Uint8 a, Int32 sprid,
Int32 header, Object & payload)
{
@ -191,14 +226,14 @@ static Object & CreateBlipEx(Int32 world, Float32 x, Float32 y, Float32 z, Int32
}
// ------------------------------------------------------------------------------------------------
static Object & CreateBlipEx(Int32 index, Int32 world, Float32 x, Float32 y, Float32 z,
static Object & Blip_CreateEx(Int32 index, Int32 world, Float32 x, Float32 y, Float32 z,
Int32 scale, Uint8 r, Uint8 g, Uint8 b, Uint8 a, Int32 sprid)
{
return _Core->NewBlip(index, world, x, y, z, scale, SQMOD_PACK_RGBA(r, g, b, a), sprid,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateBlipEx(Int32 index, Int32 world, Float32 x, Float32 y, Float32 z, Int32 scale,
static Object & Blip_CreateEx(Int32 index, Int32 world, Float32 x, Float32 y, Float32 z, Int32 scale,
Uint8 r, Uint8 g, Uint8 b, Uint8 a, Int32 sprid,
Int32 header, Object & payload)
{
@ -207,14 +242,14 @@ static Object & CreateBlipEx(Int32 index, Int32 world, Float32 x, Float32 y, Flo
}
// ------------------------------------------------------------------------------------------------
static Object & CreateBlip(Int32 world, const Vector3 & pos, Int32 scale, const Color4 & color,
static Object & Blip_Create(Int32 world, const Vector3 & pos, Int32 scale, const Color4 & color,
Int32 sprid)
{
return _Core->NewBlip(-1, world, pos.x, pos.y, pos.z, scale, color.GetRGBA(), sprid,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateBlip(Int32 world, const Vector3 & pos, Int32 scale, const Color4 & color,
static Object & Blip_Create(Int32 world, const Vector3 & pos, Int32 scale, const Color4 & color,
Int32 sprid, Int32 header, Object & payload)
{
return _Core->NewBlip(-1, world, pos.x, pos.y, pos.z, scale, color.GetRGBA(), sprid,
@ -222,41 +257,82 @@ static Object & CreateBlip(Int32 world, const Vector3 & pos, Int32 scale, const
}
// ------------------------------------------------------------------------------------------------
static Object & CreateBlip(Int32 index, Int32 world, const Vector3 & pos, Int32 scale,
static Object & Blip_Create(Int32 index, Int32 world, const Vector3 & pos, Int32 scale,
const Color4 & color, Int32 sprid)
{
return _Core->NewBlip(index, world, pos.x, pos.y, pos.z, scale, color.GetRGBA(), sprid,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateBlip(Int32 index, Int32 world, const Vector3 & pos, Int32 scale,
static Object & Blip_Create(Int32 index, Int32 world, const Vector3 & pos, Int32 scale,
const Color4 & color, Int32 sprid, Int32 header, Object & payload)
{
return _Core->NewBlip(index, world, pos.x, pos.y, pos.z, scale, color.GetRGBA(), sprid,
header, payload);
}
// ------------------------------------------------------------------------------------------------
static const Object & Blip_FindByID(Int32 id)
{
// Perform a range check on the specified identifier
if (INVALID_ENTITYEX(id, SQMOD_BLIP_POOL))
SqThrowF("The specified blip identifier is invalid: %d", id);
// Obtain the ends of the entity pool
Core::Blips::const_iterator itr = _Core->GetBlips().cbegin();
Core::Blips::const_iterator end = _Core->GetBlips().cend();
// Process each entity in the pool
for (; itr != end; ++itr)
{
// Does the identifier match the specified one?
if (itr->mID == id)
return itr->mObj; // Stop searching and return this entity
}
// Unable to locate a blip matching the specified identifier
return NullObject();
}
static const Object & Blip_FindByTag(CSStr tag)
{
// Perform a validity check on the specified tag
if (!tag || *tag == 0)
SqThrowF("The specified blip tag is invalid: null/empty");
// Obtain the ends of the entity pool
Core::Blips::const_iterator itr = _Core->GetBlips().cbegin();
Core::Blips::const_iterator end = _Core->GetBlips().cend();
// Process each entity in the pool
for (; itr != end; ++itr)
{
// Does this entity even exist and does the tag match the specified one?
if (itr->mInst != nullptr && itr->mInst->GetTag().compare(tag) == 0)
return itr->mObj; // Stop searching and return this entity
}
// Unable to locate a blip matching the specified tag
return NullObject();
}
// ================================================================================================
void Register_CBlip(HSQUIRRELVM vm)
{
RootTable(vm).Bind(_SC("SqBlip"),
Class< CBlip, NoConstructor< CBlip > >(vm, _SC("SqBlip"))
/* Metamethods */
// Metamethods
.Func(_SC("_cmp"), &CBlip::Cmp)
.SquirrelFunc(_SC("_typename"), &CBlip::Typename)
.Func(_SC("_tostring"), &CBlip::ToString)
/* Core Properties */
// Static values
.SetStaticValue(_SC("MaxID"), CBlip::Max)
// Core Properties
.Prop(_SC("ID"), &CBlip::GetID)
.Prop(_SC("Tag"), &CBlip::GetTag, &CBlip::SetTag)
.Prop(_SC("Data"), &CBlip::GetData, &CBlip::SetData)
.Prop(_SC("MaxID"), &CBlip::GetMaxID)
.Prop(_SC("Active"), &CBlip::IsActive)
/* Core Functions */
// Core Functions
.Func(_SC("Bind"), &CBlip::BindEvent)
/* Core Overloads */
// Core Overloads
.Overload< bool (CBlip::*)(void) >(_SC("Destroy"), &CBlip::Destroy)
.Overload< bool (CBlip::*)(Int32) >(_SC("Destroy"), &CBlip::Destroy)
.Overload< bool (CBlip::*)(Int32, Object &) >(_SC("Destroy"), &CBlip::Destroy)
/* Properties */
// Properties
.Prop(_SC("World"), &CBlip::GetWorld)
.Prop(_SC("Scale"), &CBlip::GetScale)
.Prop(_SC("Pos"), &CBlip::GetPosition)
@ -270,25 +346,24 @@ void Register_CBlip(HSQUIRRELVM vm)
.Prop(_SC("G"), &CBlip::GetColorG)
.Prop(_SC("B"), &CBlip::GetColorB)
.Prop(_SC("A"), &CBlip::GetColorA)
// Static Overloads
.StaticOverload< Object & (*)(Int32, Float32, Float32, Float32, Int32, Uint8, Uint8, Uint8, Uint8, Int32) >
(_SC("CreateEx"), &Blip_CreateEx)
.StaticOverload< Object & (*)(Int32, Float32, Float32, Float32, Int32, Uint8, Uint8, Uint8, Uint8, Int32, Int32, Object &) >
(_SC("CreateEx"), &Blip_CreateEx)
.StaticOverload< Object & (*)(Int32, Int32, Float32, Float32, Float32, Int32, Uint8, Uint8, Uint8, Uint8, Int32) >
(_SC("CreateEx"), &Blip_CreateEx)
.StaticOverload< Object & (*)(Int32, Int32, Float32, Float32, Float32, Int32, Uint8, Uint8, Uint8, Uint8, Int32, Int32, Object &) >
(_SC("CreateEx"), &Blip_CreateEx)
.StaticOverload< Object & (*)(Int32, const Vector3 &, Int32, const Color4 &, Int32) >
(_SC("Create"), &Blip_Create)
.StaticOverload< Object & (*)(Int32, const Vector3 &, Int32, const Color4 &, Int32, Int32, Object &) >
(_SC("Create"), &Blip_Create)
.StaticOverload< Object & (*)(Int32, Int32, const Vector3 &, Int32, const Color4 &, Int32) >
(_SC("Create"), &Blip_Create)
.StaticOverload< Object & (*)(Int32, Int32, const Vector3 &, Int32, const Color4 &, Int32, Int32, Object &) >
(_SC("Create"), &Blip_Create)
);
RootTable(vm)
.Overload< Object & (*)(Int32, Float32, Float32, Float32, Int32, Uint8, Uint8, Uint8, Uint8, Int32) >
(_SC("CreateBlipEx"), &CreateBlipEx)
.Overload< Object & (*)(Int32, Float32, Float32, Float32, Int32, Uint8, Uint8, Uint8, Uint8, Int32, Int32, Object &) >
(_SC("CreateBlipEx"), &CreateBlipEx)
.Overload< Object & (*)(Int32, Int32, Float32, Float32, Float32, Int32, Uint8, Uint8, Uint8, Uint8, Int32) >
(_SC("CreateBlipEx"), &CreateBlipEx)
.Overload< Object & (*)(Int32, Int32, Float32, Float32, Float32, Int32, Uint8, Uint8, Uint8, Uint8, Int32, Int32, Object &) >
(_SC("CreateBlipEx"), &CreateBlipEx)
.Overload< Object & (*)(Int32, const Vector3 &, Int32, const Color4 &, Int32) >
(_SC("CreateBlip"), &CreateBlip)
.Overload< Object & (*)(Int32, const Vector3 &, Int32, const Color4 &, Int32, Int32, Object &) >
(_SC("CreateBlip"), &CreateBlip)
.Overload< Object & (*)(Int32, Int32, const Vector3 &, Int32, const Color4 &, Int32) >
(_SC("CreateBlip"), &CreateBlip)
.Overload< Object & (*)(Int32, Int32, const Vector3 &, Int32, const Color4 &, Int32, Int32, Object &) >
(_SC("CreateBlip"), &CreateBlip);
}
} // Namespace:: SqMod
} // Namespace:: SqMod

View File

@ -8,7 +8,7 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Manages Blip instances.
* Manages a single blip entity.
*/
class CBlip
{
@ -17,20 +17,19 @@ class CBlip
private:
/* --------------------------------------------------------------------------------------------
* Cached identifiers for fast integer to string conversion.
*/
static SQChar s_StrID[SQMOD_BLIP_POOL][8];
/* --------------------------------------------------------------------------------------------
* Identifier of the managed entity.
*/
Int32 m_ID;
/* --------------------------------------------------------------------------------------------
* User tag and data associated with this instance.
* User tag associated with this instance.
*/
String m_Tag;
/* --------------------------------------------------------------------------------------------
* User data associated with this instance.
*/
Object m_Data;
/* --------------------------------------------------------------------------------------------
@ -38,20 +37,22 @@ private:
*/
CBlip(Int32 id);
public:
/* --------------------------------------------------------------------------------------------
* Maximum possible number that could represent an identifier for this entity type.
*/
static const Int32 Max;
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
CBlip(const CBlip &);
CBlip(const CBlip &) = delete;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
* Move constructor. (disabled)
*/
CBlip & operator = (const CBlip &);
public:
// --------------------------------------------------------------------------------------------
static const Int32 Max;
CBlip(CBlip &&) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
@ -59,14 +60,22 @@ public:
~CBlip();
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity.
* Copy assignment operator. (disabled)
*/
bool Validate() const
CBlip & operator = (const CBlip &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
CBlip & operator = (CBlip &&) = delete;
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity otherwise throw an exception.
*/
void Validate() const
{
if (VALID_ENTITY(m_ID))
return true;
SqThrow("Invalid blip reference [%s]", m_Tag.c_str());
return false;
if (INVALID_ENTITY(m_ID))
SqThrowF("Invalid blip reference [%s]", m_Tag.c_str());
}
/* --------------------------------------------------------------------------------------------
@ -77,27 +86,33 @@ public:
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
const String & ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Retrieve the identifier of the entity managed by this instance.
*/
Int32 GetID() const { return m_ID; }
/* --------------------------------------------------------------------------------------------
* Retrieve the maximum possible identifier to an entity of this type.
*/
Int32 GetMaxID() const { return SQMOD_BLIP_POOL; }
Int32 GetID() const
{
return m_ID;
}
/* --------------------------------------------------------------------------------------------
* Check whether this instance manages a valid entity.
*/
bool IsActive() const { return VALID_ENTITY(m_ID); }
bool IsActive() const
{
return VALID_ENTITY(m_ID);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the associated user tag.
*/
CSStr GetTag() const;
const String & GetTag() const;
/* --------------------------------------------------------------------------------------------
* Modify the associated user tag.
@ -115,29 +130,89 @@ public:
void SetData(Object & data);
/* --------------------------------------------------------------------------------------------
* Destroy the managed entity instance.
* Destroy the managed blip entity.
*/
bool Destroy()
{
return Destroy(0, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed blip entity.
*/
bool Destroy(Int32 header)
{
return Destroy(header, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed blip entity.
*/
bool Destroy(Int32 header, Object & payload);
bool Destroy() { return Destroy(0, NullObject()); }
bool Destroy(Int32 header) { return Destroy(header, NullObject()); }
// --------------------------------------------------------------------------------------------
bool BindEvent(Int32 evid, Object & env, Function & func) const;
/* --------------------------------------------------------------------------------------------
* Bind to an event supported by this entity type.
*/
void BindEvent(Int32 evid, Object & env, Function & func) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Retrieve the world in which the referenced blip entity exists.
*/
Int32 GetWorld() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the scale of the managed blip entity.
*/
Int32 GetScale() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position of the managed blip entity.
*/
const Vector3 & GetPosition() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the color of the managed blip entity.
*/
const Color4 & GetColor() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the identifier of the sprite used by the managed blip entity.
*/
Int32 GetSprID() const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the x axis of the managed blip entity.
*/
Float32 GetPosX() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the y axis of the managed blip entity.
*/
Float32 GetPosY() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the z axis of the managed blip entity.
*/
Float32 GetPosZ() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the red color of the managed blip entity.
*/
Int32 GetColorR() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the green color of the managed blip entity.
*/
Int32 GetColorG() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the blue color of the managed blip entity.
*/
Int32 GetColorB() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the alpha transparency of the managed blip entity.
*/
Int32 GetColorA() const;
};

View File

@ -19,15 +19,20 @@ Uint32 CCheckpoint::s_ColorB;
Uint32 CCheckpoint::s_ColorA;
// ------------------------------------------------------------------------------------------------
SQChar CCheckpoint::s_StrID[SQMOD_CHECKPOINT_POOL][8];
const Int32 CCheckpoint::Max = SQMOD_CHECKPOINT_POOL;
// ------------------------------------------------------------------------------------------------
const Int32 CCheckpoint::Max = SQMOD_CHECKPOINT_POOL;
SQInteger CCheckpoint::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("SqCheckpoint");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
CCheckpoint::CCheckpoint(Int32 id)
: m_ID(VALID_ENTITYGETEX(id, SQMOD_CHECKPOINT_POOL))
, m_Tag(VALID_ENTITY(m_ID) ? s_StrID[m_ID] : _SC("-1"))
, m_Tag(ToStrF("%d", id))
{
/* ... */
}
@ -49,282 +54,373 @@ Int32 CCheckpoint::Cmp(const CCheckpoint & o) const
return -1;
}
CSStr CCheckpoint::ToString() const
// ------------------------------------------------------------------------------------------------
const String & CCheckpoint::ToString() const
{
return VALID_ENTITYEX(m_ID, SQMOD_CHECKPOINT_POOL) ? s_StrID[m_ID] : _SC("-1");
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
CSStr CCheckpoint::GetTag() const
const String & CCheckpoint::GetTag() const
{
return m_Tag.c_str();
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
void CCheckpoint::SetTag(CSStr tag)
{
m_Tag.assign(tag);
}
// ------------------------------------------------------------------------------------------------
Object & CCheckpoint::GetData()
{
if (Validate())
return m_Data;
return NullObject();
// Validate the managed identifier
Validate();
// Return the requested information
return m_Data;
}
// ------------------------------------------------------------------------------------------------
void CCheckpoint::SetData(Object & data)
{
if (Validate())
m_Data = data;
// Validate the managed identifier
Validate();
// Apply the specified value
m_Data = data;
}
// ------------------------------------------------------------------------------------------------
bool CCheckpoint::Destroy(Int32 header, Object & payload)
{
// Validate the managed identifier
Validate();
// Perform the requested operation
return _Core->DelCheckpoint(m_ID, header, payload);
}
// ------------------------------------------------------------------------------------------------
bool CCheckpoint::BindEvent(Int32 evid, Object & env, Function & func) const
void CCheckpoint::BindEvent(Int32 evid, Object & env, Function & func) const
{
if (!Validate())
return false;
// Validate the managed identifier
Validate();
// Obtain the function instance called for this event
Function & event = _Core->GetCheckpointEvent(m_ID, evid);
// Is the specified callback function null?
if (func.IsNull())
event.Release();
event.Release(); // Then release the current callback
// Assign the specified environment and function
else
event = Function(env.GetVM(), env, func.GetFunc());
return true;
}
// ------------------------------------------------------------------------------------------------
bool CCheckpoint::IsStreamedFor(CPlayer & player) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
return _Func->IsCheckpointStreamedForPlayer(m_ID, player.GetID());
return false;
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->IsCheckpointStreamedForPlayer(m_ID, player.GetID());
}
// ------------------------------------------------------------------------------------------------
Int32 CCheckpoint::GetWorld() const
{
if (Validate())
return _Func->GetCheckpointWorld(m_ID);
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->GetCheckpointWorld(m_ID);
}
// ------------------------------------------------------------------------------------------------
void CCheckpoint::SetWorld(Int32 world) const
{
if (Validate())
_Func->SetCheckpointWorld(m_ID, world);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetCheckpointWorld(m_ID, world);
}
// ------------------------------------------------------------------------------------------------
const Color4 & CCheckpoint::GetColor() const
{
// Validate the managed identifier
Validate();
// Clear previous color information, if any
s_Color4.Clear();
if (Validate())
{
_Func->GetCheckpointColor(m_ID, &s_ColorR, &s_ColorG, &s_ColorB, &s_ColorA);
s_Color4.Set(s_ColorR, s_ColorG, s_ColorB, s_ColorA);
}
// Query the server for the color values
_Func->GetCheckpointColor(m_ID, &s_ColorR, &s_ColorG, &s_ColorB, &s_ColorA);
// Convert and assign the retrieved values
s_Color4.Set(s_ColorR, s_ColorG, s_ColorB, s_ColorA);
// Return the requested information
return s_Color4;
}
// ------------------------------------------------------------------------------------------------
void CCheckpoint::SetColor(const Color4 & col) const
{
if (Validate())
_Func->SetCheckpointColor(m_ID, col.r, col.g, col.b, col.a);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetCheckpointColor(m_ID, col.r, col.g, col.b, col.a);
}
// ------------------------------------------------------------------------------------------------
void CCheckpoint::SetColorEx(Uint8 r, Uint8 g, Uint8 b, Uint8 a) const
{
if (Validate())
_Func->SetCheckpointColor(m_ID, r, g, b, a);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetCheckpointColor(m_ID, r, g, b, a);
}
// ------------------------------------------------------------------------------------------------
const Vector3 & CCheckpoint::GetPosition() const
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.Clear();
if (Validate())
_Func->GetCheckpointPos(m_ID, &s_Vector3.x, &s_Vector3.y, &s_Vector3.z);
// Query the server for the position values
_Func->GetCheckpointPos(m_ID, &s_Vector3.x, &s_Vector3.y, &s_Vector3.z);
// Return the requested information
return s_Vector3;
}
// ------------------------------------------------------------------------------------------------
void CCheckpoint::SetPosition(const Vector3 & pos) const
{
if (Validate())
_Func->SetCheckpointPos(m_ID, pos.x, pos.y, pos.z);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetCheckpointPos(m_ID, pos.x, pos.y, pos.z);
}
// ------------------------------------------------------------------------------------------------
void CCheckpoint::SetPositionEx(Float32 x, Float32 y, Float32 z) const
{
if (Validate())
_Func->SetCheckpointPos(m_ID, x, y, z);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetCheckpointPos(m_ID, x, y, z);
}
// ------------------------------------------------------------------------------------------------
Float32 CCheckpoint::GetRadius() const
{
if (Validate())
_Func->GetCheckpointRadius(m_ID);
return 0;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->GetCheckpointRadius(m_ID);
}
// ------------------------------------------------------------------------------------------------
void CCheckpoint::SetRadius(Float32 radius) const
{
if (Validate())
_Func->SetCheckpointRadius(m_ID, radius);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetCheckpointRadius(m_ID, radius);
}
// ------------------------------------------------------------------------------------------------
Object & CCheckpoint::GetOwner() const
{
if (Validate())
return _Core->GetPlayer(_Func->GetCheckpointOwner(m_ID)).mObj;
return NullObject();
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetPlayer(_Func->GetCheckpointOwner(m_ID)).mObj;
}
// ------------------------------------------------------------------------------------------------
Int32 CCheckpoint::GetOwnerID() const
{
if (Validate())
return _Func->GetCheckpointOwner(m_ID);
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->GetCheckpointOwner(m_ID);
}
// ------------------------------------------------------------------------------------------------
Float32 CCheckpoint::GetPosX() const
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.x = 0;
if (Validate())
_Func->GetCheckpointPos(m_ID, &s_Vector3.x, NULL, NULL);
// Query the server for the requested component value
_Func->GetCheckpointPos(m_ID, &s_Vector3.x, NULL, NULL);
// Return the requested information
return s_Vector3.x;
}
// ------------------------------------------------------------------------------------------------
Float32 CCheckpoint::GetPosY() const
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.y = 0;
if (Validate())
_Func->GetCheckpointPos(m_ID, NULL, &s_Vector3.y, NULL);
// Query the server for the requested component value
_Func->GetCheckpointPos(m_ID, NULL, &s_Vector3.y, NULL);
// Return the requested information
return s_Vector3.y;
}
// ------------------------------------------------------------------------------------------------
Float32 CCheckpoint::GetPosZ() const
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.z = 0;
if (Validate())
_Func->GetCheckpointPos(m_ID, NULL, NULL, &s_Vector3.z);
// Query the server for the requested component value
_Func->GetCheckpointPos(m_ID, NULL, NULL, &s_Vector3.z);
// Return the requested information
return s_Vector3.z;
}
// ------------------------------------------------------------------------------------------------
void CCheckpoint::SetPosX(Float32 x) const
{
if (Validate())
{
_Func->GetCheckpointPos(m_ID, NULL, &s_Vector3.y, &s_Vector3.z);
_Func->SetCheckpointPos(m_ID, x, s_Vector3.y, s_Vector3.z);
}
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetCheckpointPos(m_ID, NULL, &s_Vector3.y, &s_Vector3.z);
// Perform the requested operation
_Func->SetCheckpointPos(m_ID, x, s_Vector3.y, s_Vector3.z);
}
// ------------------------------------------------------------------------------------------------
void CCheckpoint::SetPosY(Float32 y) const
{
if (Validate())
{
_Func->GetCheckpointPos(m_ID, &s_Vector3.x, NULL, &s_Vector3.z);
_Func->SetCheckpointPos(m_ID, s_Vector3.x, y, s_Vector3.z);
}
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetCheckpointPos(m_ID, &s_Vector3.x, NULL, &s_Vector3.z);
// Perform the requested operation
_Func->SetCheckpointPos(m_ID, s_Vector3.x, y, s_Vector3.z);
}
// ------------------------------------------------------------------------------------------------
void CCheckpoint::SetPosZ(Float32 z) const
{
if (Validate())
{
_Func->GetCheckpointPos(m_ID, &s_Vector3.x, &s_Vector3.y, NULL);
_Func->SetCheckpointPos(m_ID, s_Vector3.z, s_Vector3.y, z);
}
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetCheckpointPos(m_ID, &s_Vector3.x, &s_Vector3.y, NULL);
// Perform the requested operation
_Func->SetCheckpointPos(m_ID, s_Vector3.z, s_Vector3.y, z);
}
// ------------------------------------------------------------------------------------------------
Uint32 CCheckpoint::GetColR() const
{
// Validate the managed identifier
Validate();
// Clear previous color information, if any
s_ColorR = 0;
if (Validate())
_Func->GetCheckpointColor(m_ID, &s_ColorR, NULL, NULL, NULL);
// Query the server for the requested component value
_Func->GetCheckpointColor(m_ID, &s_ColorR, NULL, NULL, NULL);
// Return the requested information
return s_ColorR;
}
// ------------------------------------------------------------------------------------------------
Uint32 CCheckpoint::GetColG() const
{
// Validate the managed identifier
Validate();
// Clear previous color information, if any
s_ColorG = 0;
if (Validate())
_Func->GetCheckpointColor(m_ID, NULL, &s_ColorG, NULL, NULL);
// Query the server for the requested component value
_Func->GetCheckpointColor(m_ID, NULL, &s_ColorG, NULL, NULL);
// Return the requested information
return s_ColorG;
}
// ------------------------------------------------------------------------------------------------
Uint32 CCheckpoint::GetColB() const
{
// Validate the managed identifier
Validate();
// Clear previous color information, if any
s_ColorB = 0;
if (Validate())
_Func->GetCheckpointColor(m_ID, NULL, NULL, &s_ColorB, NULL);
// Query the server for the requested component value
_Func->GetCheckpointColor(m_ID, NULL, NULL, &s_ColorB, NULL);
// Return the requested information
return s_ColorB;
}
// ------------------------------------------------------------------------------------------------
Uint32 CCheckpoint::GetColA() const
{
// Validate the managed identifier
Validate();
// Clear previous color information, if any
s_ColorA = 0;
if (Validate())
_Func->GetCheckpointColor(m_ID, NULL, NULL, NULL, &s_ColorA);
// Query the server for the requested component value
_Func->GetCheckpointColor(m_ID, NULL, NULL, NULL, &s_ColorA);
// Return the requested information
return s_ColorA;
}
// ------------------------------------------------------------------------------------------------
void CCheckpoint::SetColR(Uint32 r) const
{
if (Validate())
{
_Func->GetCheckpointColor(m_ID, NULL, &s_ColorG, &s_ColorB, &s_ColorA);
_Func->SetCheckpointColor(m_ID, r, s_ColorG, s_ColorB, s_ColorA);
}
}
void CCheckpoint::SetColG(Uint32 g) const
{
if (Validate())
{
_Func->GetCheckpointColor(m_ID, &s_ColorR, NULL, &s_ColorB, &s_ColorA);
_Func->SetCheckpointColor(m_ID, s_ColorR, g, s_ColorB, s_ColorA);
}
}
void CCheckpoint::SetColB(Uint32 b) const
{
if (Validate())
{
_Func->GetCheckpointColor(m_ID, &s_ColorB, &s_ColorG, NULL, &s_ColorA);
_Func->SetCheckpointColor(m_ID, s_ColorB, s_ColorG, b, s_ColorA);
}
}
void CCheckpoint::SetColA(Uint32 a) const
{
if (Validate())
{
_Func->GetCheckpointColor(m_ID, &s_ColorA, &s_ColorG, &s_ColorB, NULL);
_Func->SetCheckpointColor(m_ID, s_ColorA, s_ColorG, s_ColorB, a);
}
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetCheckpointColor(m_ID, NULL, &s_ColorG, &s_ColorB, &s_ColorA);
// Perform the requested operation
_Func->SetCheckpointColor(m_ID, r, s_ColorG, s_ColorB, s_ColorA);
}
// ------------------------------------------------------------------------------------------------
static Object & CreateCheckpointEx(CPlayer & player, Int32 world, Float32 x, Float32 y, Float32 z,
void CCheckpoint::SetColG(Uint32 g) const
{
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetCheckpointColor(m_ID, &s_ColorR, NULL, &s_ColorB, &s_ColorA);
// Perform the requested operation
_Func->SetCheckpointColor(m_ID, s_ColorR, g, s_ColorB, s_ColorA);
}
// ------------------------------------------------------------------------------------------------
void CCheckpoint::SetColB(Uint32 b) const
{
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetCheckpointColor(m_ID, &s_ColorB, &s_ColorG, NULL, &s_ColorA);
// Perform the requested operation
_Func->SetCheckpointColor(m_ID, s_ColorB, s_ColorG, b, s_ColorA);
}
// ------------------------------------------------------------------------------------------------
void CCheckpoint::SetColA(Uint32 a) const
{
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetCheckpointColor(m_ID, &s_ColorA, &s_ColorG, &s_ColorB, NULL);
// Perform the requested operation
_Func->SetCheckpointColor(m_ID, s_ColorA, s_ColorG, s_ColorB, a);
}
// ------------------------------------------------------------------------------------------------
static Object & Checkpoint_CreateEx(CPlayer & player, Int32 world, Float32 x, Float32 y, Float32 z,
Uint8 r, Uint8 g, Uint8 b, Uint8 a, Float32 radius)
{
return _Core->NewCheckpoint(player.GetID(), world, x, y, z, r, g, b, a, radius,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateCheckpointEx(CPlayer & player, Int32 world, Float32 x, Float32 y, Float32 z,
static Object & Checkpoint_CreateEx(CPlayer & player, Int32 world, Float32 x, Float32 y, Float32 z,
Uint8 r, Uint8 g, Uint8 b, Uint8 a, Float32 radius,
Int32 header, Object & payload)
{
@ -332,7 +428,7 @@ static Object & CreateCheckpointEx(CPlayer & player, Int32 world, Float32 x, Flo
}
// ------------------------------------------------------------------------------------------------
static Object & CreateCheckpoint(CPlayer & player, Int32 world, const Vector3 & pos,
static Object & Checkpoint_Create(CPlayer & player, Int32 world, const Vector3 & pos,
const Color4 & color, Float32 radius)
{
return _Core->NewCheckpoint(player.GetID(), world, pos.x, pos.y, pos.z,
@ -340,7 +436,7 @@ static Object & CreateCheckpoint(CPlayer & player, Int32 world, const Vector3 &
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateCheckpoint(CPlayer & player, Int32 world, const Vector3 & pos,
static Object & Checkpoint_Create(CPlayer & player, Int32 world, const Vector3 & pos,
const Color4 & color, Float32 radius, Int32 header, Object & payload)
{
return _Core->NewCheckpoint(player.GetID(), world, pos.x, pos.y, pos.z,
@ -352,22 +448,24 @@ void Register_CCheckpoint(HSQUIRRELVM vm)
{
RootTable(vm).Bind(_SC("SqCheckpoint"),
Class< CCheckpoint, NoConstructor< CCheckpoint > >(vm, _SC("SqCheckpoint"))
/* Metamethods */
// Metamethods
.Func(_SC("_cmp"), &CCheckpoint::Cmp)
.SquirrelFunc(_SC("_typename"), &CCheckpoint::Typename)
.Func(_SC("_tostring"), &CCheckpoint::ToString)
/* Core Properties */
// Static values
.SetStaticValue(_SC("MaxID"), CCheckpoint::Max)
// Core Properties
.Prop(_SC("ID"), &CCheckpoint::GetID)
.Prop(_SC("Tag"), &CCheckpoint::GetTag, &CCheckpoint::SetTag)
.Prop(_SC("Data"), &CCheckpoint::GetData, &CCheckpoint::SetData)
.Prop(_SC("MaxID"), &CCheckpoint::GetMaxID)
.Prop(_SC("Active"), &CCheckpoint::IsActive)
/* Core Functions */
// Core Functions
.Func(_SC("Bind"), &CCheckpoint::BindEvent)
/* Core Overloads */
// Core Overloads
.Overload< bool (CCheckpoint::*)(void) >(_SC("Destroy"), &CCheckpoint::Destroy)
.Overload< bool (CCheckpoint::*)(Int32) >(_SC("Destroy"), &CCheckpoint::Destroy)
.Overload< bool (CCheckpoint::*)(Int32, Object &) >(_SC("Destroy"), &CCheckpoint::Destroy)
/* Properties */
// Properties
.Prop(_SC("World"), &CCheckpoint::GetWorld, &CCheckpoint::SetWorld)
.Prop(_SC("Color"), &CCheckpoint::GetColor, &CCheckpoint::SetColor)
.Prop(_SC("Pos"), &CCheckpoint::GetPosition, &CCheckpoint::SetPosition)
@ -382,22 +480,21 @@ void Register_CCheckpoint(HSQUIRRELVM vm)
.Prop(_SC("G"), &CCheckpoint::GetColG, &CCheckpoint::SetColG)
.Prop(_SC("B"), &CCheckpoint::GetColB, &CCheckpoint::SetColB)
.Prop(_SC("A"), &CCheckpoint::GetColA, &CCheckpoint::SetColA)
/* Functions */
// Functions
.Func(_SC("StreamedFor"), &CCheckpoint::IsStreamedFor)
.Func(_SC("SetColor"), &CCheckpoint::SetColorEx)
.Func(_SC("SetPos"), &CCheckpoint::SetPositionEx)
.Func(_SC("SetPosition"), &CCheckpoint::SetPositionEx)
// Static Overloads
.StaticOverload< Object & (*)(CPlayer &, Int32, Float32, Float32, Float32, Uint8, Uint8, Uint8, Uint8, Float32) >
(_SC("CreateEx"), &Checkpoint_CreateEx)
.StaticOverload< Object & (*)(CPlayer &, Int32, Float32, Float32, Float32, Uint8, Uint8, Uint8, Uint8, Float32, Int32, Object &) >
(_SC("CreateEx"), &Checkpoint_CreateEx)
.StaticOverload< Object & (*)(CPlayer &, Int32, const Vector3 &, const Color4 &, Float32) >
(_SC("Create"), &Checkpoint_Create)
.StaticOverload< Object & (*)(CPlayer &, Int32, const Vector3 &, const Color4 &, Float32, Int32, Object &) >
(_SC("Create"), &Checkpoint_Create)
);
RootTable(vm)
.Overload< Object & (*)(CPlayer &, Int32, Float32, Float32, Float32, Uint8, Uint8, Uint8, Uint8, Float32) >
(_SC("CreateCheckpointEx"), &CreateCheckpointEx)
.Overload< Object & (*)(CPlayer &, Int32, Float32, Float32, Float32, Uint8, Uint8, Uint8, Uint8, Float32, Int32, Object &) >
(_SC("CreateCheckpointEx"), &CreateCheckpointEx)
.Overload< Object & (*)(CPlayer &, Int32, const Vector3 &, const Color4 &, Float32) >
(_SC("CreateCheckpoint"), &CreateCheckpoint)
.Overload< Object & (*)(CPlayer &, Int32, const Vector3 &, const Color4 &, Float32, Int32, Object &) >
(_SC("CreateCheckpoint"), &CreateCheckpoint);
}
} // Namespace:: SqMod
} // Namespace:: SqMod

View File

@ -8,7 +8,7 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Manages Checkpoint instances.
* Manages a single checkpoint entity.
*/
class CCheckpoint
{
@ -24,20 +24,19 @@ private:
// --------------------------------------------------------------------------------------------
static Uint32 s_ColorR, s_ColorG, s_ColorB, s_ColorA;
/* --------------------------------------------------------------------------------------------
* Cached identifiers for fast integer to string conversion.
*/
static SQChar s_StrID[SQMOD_CHECKPOINT_POOL][8];
/* --------------------------------------------------------------------------------------------
* Identifier of the managed entity.
*/
Int32 m_ID;
/* --------------------------------------------------------------------------------------------
* User tag and data associated with this instance.
* User tag associated with this instance.
*/
String m_Tag;
/* --------------------------------------------------------------------------------------------
* User data associated with this instance.
*/
Object m_Data;
/* --------------------------------------------------------------------------------------------
@ -45,20 +44,22 @@ private:
*/
CCheckpoint(Int32 id);
public:
/* --------------------------------------------------------------------------------------------
* Maximum possible number that could represent an identifier for this entity type.
*/
static const Int32 Max;
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
CCheckpoint(const CCheckpoint &);
CCheckpoint(const CCheckpoint &) = delete;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
* Move constructor. (disabled)
*/
CCheckpoint & operator = (const CCheckpoint &);
public:
// --------------------------------------------------------------------------------------------
static const Int32 Max;
CCheckpoint(CCheckpoint &&) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
@ -66,14 +67,22 @@ public:
~CCheckpoint();
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity.
* Copy assignment operator. (disabled)
*/
bool Validate() const
CCheckpoint & operator = (const CCheckpoint &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
CCheckpoint & operator = (CCheckpoint &&) = delete;
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity otherwise throw an exception.
*/
void Validate() const
{
if (VALID_ENTITY(m_ID))
return true;
SqThrow("Invalid checkpoint reference [%s]", m_Tag.c_str());
return false;
if (INVALID_ENTITY(m_ID))
SqThrowF("Invalid checkpoint reference [%s]", m_Tag.c_str());
}
/* --------------------------------------------------------------------------------------------
@ -84,27 +93,33 @@ public:
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
const String & ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Retrieve the identifier of the entity managed by this instance.
*/
Int32 GetID() const { return m_ID; }
/* --------------------------------------------------------------------------------------------
* Retrieve the maximum possible identifier to an entity of this type.
*/
Int32 GetMaxID() const { return SQMOD_CHECKPOINT_POOL; }
Int32 GetID() const
{
return m_ID;
}
/* --------------------------------------------------------------------------------------------
* Check whether this instance manages a valid entity.
*/
bool IsActive() const { return VALID_ENTITY(m_ID); }
bool IsActive() const
{
return VALID_ENTITY(m_ID);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the associated user tag.
*/
CSStr GetTag() const;
const String & GetTag() const;
/* --------------------------------------------------------------------------------------------
* Modify the associated user tag.
@ -120,45 +135,166 @@ public:
* Modify the associated user data.
*/
void SetData(Object & data);
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Destroy the managed checkpoint entity.
*/
bool Destroy()
{
return Destroy(0, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed checkpoint entity.
*/
bool Destroy(Int32 header)
{
return Destroy(header, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed checkpoint entity.
*/
bool Destroy(Int32 header, Object & payload);
bool Destroy() { return Destroy(0, NullObject()); }
bool Destroy(Int32 header) { return Destroy(header, NullObject()); }
// --------------------------------------------------------------------------------------------
bool BindEvent(Int32 evid, Object & env, Function & func) const;
/* --------------------------------------------------------------------------------------------
* Bind to an event supported by this entity type.
*/
void BindEvent(Int32 evid, Object & env, Function & func) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* See if the managed checkpoint entity is streamed for the specified player.
*/
bool IsStreamedFor(CPlayer & player) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the world in which the managed checkpoint entity exists.
*/
Int32 GetWorld() const;
/* --------------------------------------------------------------------------------------------
* Modify the world in which the managed checkpoint entity exists.
*/
void SetWorld(Int32 world) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the color of the managed checkpoint entity.
*/
const Color4 & GetColor() const;
/* --------------------------------------------------------------------------------------------
* Modify the color of the managed checkpoint entity.
*/
void SetColor(const Color4 & col) const;
/* --------------------------------------------------------------------------------------------
* Modify the color of the managed checkpoint entity.
*/
void SetColorEx(Uint8 r, Uint8 g, Uint8 b, Uint8 a) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position of the managed checkpoint entity.
*/
const Vector3 & GetPosition() const;
/* --------------------------------------------------------------------------------------------
* Modify the position of the managed checkpoint entity.
*/
void SetPosition(const Vector3 & pos) const;
/* --------------------------------------------------------------------------------------------
* Modify the position of the managed checkpoint entity.
*/
void SetPositionEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the radius of the managed checkpoint entity.
*/
Float32 GetRadius() const;
/* --------------------------------------------------------------------------------------------
* Modify the radius of the managed checkpoint entity.
*/
void SetRadius(Float32 radius) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the owner of the managed checkpoint entity.
*/
Object & GetOwner() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the owner identifier of the managed checkpoint entity.
*/
Int32 GetOwnerID() const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the x axis of the managed checkpoint entity.
*/
Float32 GetPosX() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the y axis of the managed checkpoint entity.
*/
Float32 GetPosY() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the z axis of the managed checkpoint entity.
*/
Float32 GetPosZ() const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the x axis of the managed checkpoint entity.
*/
void SetPosX(Float32 x) const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the y axis of the managed checkpoint entity.
*/
void SetPosY(Float32 y) const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the z axis of the managed checkpoint entity.
*/
void SetPosZ(Float32 z) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Retrieve the red color of the managed checkpoint entity.
*/
Uint32 GetColR() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the green color of the managed checkpoint entity.
*/
Uint32 GetColG() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the blue color of the managed checkpoint entity.
*/
Uint32 GetColB() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the alpha transparency of the managed checkpoint entity.
*/
Uint32 GetColA() const;
/* --------------------------------------------------------------------------------------------
* Modify the red color of the managed checkpoint entity.
*/
void SetColR(Uint32 r) const;
/* --------------------------------------------------------------------------------------------
* Modify the green color of the managed checkpoint entity.
*/
void SetColG(Uint32 g) const;
/* --------------------------------------------------------------------------------------------
* Modify the blue color of the managed checkpoint entity.
*/
void SetColB(Uint32 b) const;
/* --------------------------------------------------------------------------------------------
* Modify the alpha transparency of the managed checkpoint entity.
*/
void SetColA(Uint32 a) const;
};

View File

@ -18,15 +18,20 @@ Uint32 CForcefield::s_ColorG;
Uint32 CForcefield::s_ColorB;
// ------------------------------------------------------------------------------------------------
SQChar CForcefield::s_StrID[SQMOD_FORCEFIELD_POOL][8];
const Int32 CForcefield::Max = SQMOD_FORCEFIELD_POOL;
// ------------------------------------------------------------------------------------------------
const Int32 CForcefield::Max = SQMOD_FORCEFIELD_POOL;
SQInteger CForcefield::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("SqForcefield");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
CForcefield::CForcefield(Int32 id)
: m_ID(VALID_ENTITYGETEX(id, SQMOD_FORCEFIELD_POOL))
, m_Tag(VALID_ENTITY(m_ID) ? s_StrID[m_ID] : _SC("-1"))
, m_Tag(ToStrF("%d", id))
{
/* ... */
}
@ -48,265 +53,349 @@ Int32 CForcefield::Cmp(const CForcefield & o) const
return -1;
}
CSStr CForcefield::ToString() const
// ------------------------------------------------------------------------------------------------
const String & CForcefield::ToString() const
{
return VALID_ENTITYEX(m_ID, SQMOD_FORCEFIELD_POOL) ? s_StrID[m_ID] : _SC("-1");
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
CSStr CForcefield::GetTag() const
const String & CForcefield::GetTag() const
{
return m_Tag.c_str();
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
void CForcefield::SetTag(CSStr tag)
{
m_Tag.assign(tag);
}
// ------------------------------------------------------------------------------------------------
Object & CForcefield::GetData()
{
if (Validate())
return m_Data;
return NullObject();
// Validate the managed identifier
Validate();
// Return the requested information
return m_Data;
}
// ------------------------------------------------------------------------------------------------
void CForcefield::SetData(Object & data)
{
if (Validate())
m_Data = data;
// Validate the managed identifier
Validate();
// Apply the specified value
m_Data = data;
}
// ------------------------------------------------------------------------------------------------
bool CForcefield::Destroy(Int32 header, Object & payload)
{
// Validate the managed identifier
Validate();
// Perform the requested operation
return _Core->DelForcefield(m_ID, header, payload);
}
// ------------------------------------------------------------------------------------------------
bool CForcefield::BindEvent(Int32 evid, Object & env, Function & func) const
void CForcefield::BindEvent(Int32 evid, Object & env, Function & func) const
{
if (!Validate())
return false;
// Validate the managed identifier
Validate();
// Obtain the function instance called for this event
Function & event = _Core->GetForcefieldEvent(m_ID, evid);
// Is the specified callback function null?
if (func.IsNull())
event.Release();
event.Release(); // Then release the current callback
// Assign the specified environment and function
else
event = Function(env.GetVM(), env, func.GetFunc());
return true;
}
// ------------------------------------------------------------------------------------------------
bool CForcefield::IsStreamedFor(CPlayer & player) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
return _Func->IsSphereStreamedForPlayer(m_ID, player.GetID());
return false;
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->IsSphereStreamedForPlayer(m_ID, player.GetID());
}
// ------------------------------------------------------------------------------------------------
Int32 CForcefield::GetWorld() const
{
if (Validate())
return _Func->GetSphereWorld(m_ID);
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->GetSphereWorld(m_ID);
}
// ------------------------------------------------------------------------------------------------
void CForcefield::SetWorld(Int32 world) const
{
if (Validate())
_Func->SetSphereWorld(m_ID, world);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSphereWorld(m_ID, world);
}
// ------------------------------------------------------------------------------------------------
const Color3 & CForcefield::GetColor() const
{
// Validate the managed identifier
Validate();
// Clear previous color information, if any
s_Color3.Clear();
if (Validate())
{
_Func->GetSphereColor(m_ID, &s_ColorR, &s_ColorG, &s_ColorB);
s_Color3.Set(s_ColorR, s_ColorG, s_ColorB);
}
// Query the server for the color values
_Func->GetSphereColor(m_ID, &s_ColorR, &s_ColorG, &s_ColorB);
// Convert and assign the retrieved values
s_Color3.Set(s_ColorR, s_ColorG, s_ColorB);
// Return the requested information
return s_Color3;
}
// ------------------------------------------------------------------------------------------------
void CForcefield::SetColor(const Color3 & col) const
{
if (Validate())
_Func->SetSphereColor(m_ID, col.r, col.g, col.b);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSphereColor(m_ID, col.r, col.g, col.b);
}
// ------------------------------------------------------------------------------------------------
void CForcefield::SetColorEx(Uint8 r, Uint8 g, Uint8 b) const
{
if (Validate())
_Func->SetSphereColor(m_ID, r, g, b);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSphereColor(m_ID, r, g, b);
}
// ------------------------------------------------------------------------------------------------
const Vector3 & CForcefield::GetPosition() const
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.Clear();
if (Validate())
_Func->GetSpherePos(m_ID, &s_Vector3.x, &s_Vector3.y, &s_Vector3.z);
// Query the server for the position values
_Func->GetSpherePos(m_ID, &s_Vector3.x, &s_Vector3.y, &s_Vector3.z);
// Return the requested information
return s_Vector3;
}
// ------------------------------------------------------------------------------------------------
void CForcefield::SetPosition(const Vector3 & pos) const
{
if (Validate())
_Func->SetSpherePos(m_ID, pos.x, pos.y, pos.z);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSpherePos(m_ID, pos.x, pos.y, pos.z);
}
// ------------------------------------------------------------------------------------------------
void CForcefield::SetPositionEx(Float32 x, Float32 y, Float32 z) const
{
if (Validate())
_Func->SetSpherePos(m_ID, x, y, z);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSpherePos(m_ID, x, y, z);
}
// ------------------------------------------------------------------------------------------------
Float32 CForcefield::GetRadius() const
{
if (Validate())
_Func->GetSphereRadius(m_ID);
return 0;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->GetSphereRadius(m_ID);
}
// ------------------------------------------------------------------------------------------------
void CForcefield::SetRadius(Float32 radius) const
{
if (Validate())
_Func->SetSphereRadius(m_ID, radius);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSphereRadius(m_ID, radius);
}
// ------------------------------------------------------------------------------------------------
Object & CForcefield::GetOwner() const
{
if (Validate())
return _Core->GetPlayer(_Func->GetSphereOwner(m_ID)).mObj;
return NullObject();
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetPlayer(_Func->GetSphereOwner(m_ID)).mObj;
}
// ------------------------------------------------------------------------------------------------
Int32 CForcefield::GetOwnerID() const
{
if (Validate())
_Func->GetSphereOwner(m_ID);
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->GetSphereOwner(m_ID);
}
// ------------------------------------------------------------------------------------------------
Float32 CForcefield::GetPosX() const
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.x = 0;
if (Validate())
_Func->GetSpherePos(m_ID, &s_Vector3.x, NULL, NULL);
// Query the server for the requested component value
_Func->GetSpherePos(m_ID, &s_Vector3.x, NULL, NULL);
// Return the requested information
return s_Vector3.x;
}
// ------------------------------------------------------------------------------------------------
Float32 CForcefield::GetPosY() const
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.y = 0;
if (Validate())
_Func->GetSpherePos(m_ID, NULL, &s_Vector3.y, NULL);
// Query the server for the requested component value
_Func->GetSpherePos(m_ID, NULL, &s_Vector3.y, NULL);
// Return the requested information
return s_Vector3.y;
}
// ------------------------------------------------------------------------------------------------
Float32 CForcefield::GetPosZ() const
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.z = 0;
if (Validate())
_Func->GetSpherePos(m_ID, NULL, NULL, &s_Vector3.z);
// Query the server for the requested component value
_Func->GetSpherePos(m_ID, NULL, NULL, &s_Vector3.z);
// Return the requested information
return s_Vector3.z;
}
// ------------------------------------------------------------------------------------------------
void CForcefield::SetPosX(Float32 x) const
{
if (Validate())
{
_Func->GetSpherePos(m_ID, NULL, &s_Vector3.y, &s_Vector3.z);
_Func->SetSpherePos(m_ID, x, s_Vector3.y, s_Vector3.z);
}
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetSpherePos(m_ID, NULL, &s_Vector3.y, &s_Vector3.z);
// Perform the requested operation
_Func->SetSpherePos(m_ID, x, s_Vector3.y, s_Vector3.z);
}
// ------------------------------------------------------------------------------------------------
void CForcefield::SetPosY(Float32 y) const
{
if (Validate())
{
_Func->GetSpherePos(m_ID, &s_Vector3.x, NULL, &s_Vector3.z);
_Func->SetSpherePos(m_ID, s_Vector3.x, y, s_Vector3.z);
}
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetSpherePos(m_ID, &s_Vector3.x, NULL, &s_Vector3.z);
// Perform the requested operation
_Func->SetSpherePos(m_ID, s_Vector3.x, y, s_Vector3.z);
}
// ------------------------------------------------------------------------------------------------
void CForcefield::SetPosZ(Float32 z) const
{
if (Validate())
{
_Func->GetSpherePos(m_ID, &s_Vector3.x, &s_Vector3.y, NULL);
_Func->SetSpherePos(m_ID, s_Vector3.z, s_Vector3.y, z);
}
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetSpherePos(m_ID, &s_Vector3.x, &s_Vector3.y, NULL);
// Perform the requested operation
_Func->SetSpherePos(m_ID, s_Vector3.z, s_Vector3.y, z);
}
// ------------------------------------------------------------------------------------------------
Uint32 CForcefield::GetColR() const
{
// Validate the managed identifier
Validate();
// Clear previous color information, if any
s_ColorR = 0;
if (Validate())
_Func->GetSphereColor(m_ID, &s_ColorR, NULL, NULL);
// Query the server for the requested component value
_Func->GetSphereColor(m_ID, &s_ColorR, NULL, NULL);
// Return the requested information
return s_ColorR;
}
// ------------------------------------------------------------------------------------------------
Uint32 CForcefield::GetColG() const
{
// Validate the managed identifier
Validate();
// Query the server for the requested component value
s_ColorG = 0;
if (Validate())
_Func->GetSphereColor(m_ID, NULL, &s_ColorG, NULL);
// Query the server for the requested component value
_Func->GetSphereColor(m_ID, NULL, &s_ColorG, NULL);
// Return the requested information
return s_ColorG;
}
// ------------------------------------------------------------------------------------------------
Uint32 CForcefield::GetColB() const
{
// Validate the managed identifier
Validate();
// Query the server for the requested component value
s_ColorB = 0;
if (Validate())
_Func->GetSphereColor(m_ID, NULL, NULL, &s_ColorB);
// Query the server for the requested component value
_Func->GetSphereColor(m_ID, NULL, NULL, &s_ColorB);
// Return the requested information
return s_ColorB;
}
// ------------------------------------------------------------------------------------------------
void CForcefield::SetColR(Uint32 r) const
{
if (Validate())
{
_Func->GetSphereColor(m_ID, NULL, &s_ColorG, &s_ColorB);
_Func->SetSphereColor(m_ID, r, s_ColorG, s_ColorB);
}
}
void CForcefield::SetColG(Uint32 g) const
{
if (Validate())
{
_Func->GetSphereColor(m_ID, &s_ColorR, NULL, &s_ColorB);
_Func->SetSphereColor(m_ID, s_ColorR, g, s_ColorB);
}
}
void CForcefield::SetColB(Uint32 b) const
{
if (Validate())
{
_Func->GetSphereColor(m_ID, &s_ColorB, &s_ColorG, NULL);
_Func->SetSphereColor(m_ID, s_ColorB, s_ColorG, b);
}
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetSphereColor(m_ID, NULL, &s_ColorG, &s_ColorB);
// Perform the requested operation
_Func->SetSphereColor(m_ID, r, s_ColorG, s_ColorB);
}
// ------------------------------------------------------------------------------------------------
static Object & CreateForcefieldEx(CPlayer & player, Int32 world, Float32 x, Float32 y, Float32 z,
void CForcefield::SetColG(Uint32 g) const
{
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetSphereColor(m_ID, &s_ColorR, NULL, &s_ColorB);
// Perform the requested operation
_Func->SetSphereColor(m_ID, s_ColorR, g, s_ColorB);
}
// ------------------------------------------------------------------------------------------------
void CForcefield::SetColB(Uint32 b) const
{
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetSphereColor(m_ID, &s_ColorB, &s_ColorG, NULL);
// Perform the requested operation
_Func->SetSphereColor(m_ID, s_ColorB, s_ColorG, b);
}
// ------------------------------------------------------------------------------------------------
static Object & Forcefield_CreateEx(CPlayer & player, Int32 world, Float32 x, Float32 y, Float32 z,
Uint8 r, Uint8 g, Uint8 b, Float32 radius)
{
return _Core->NewForcefield(player.GetID(), world, x, y, z, r, g, b, radius,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateForcefieldEx(CPlayer & player, Int32 world, Float32 x, Float32 y, Float32 z,
static Object & Forcefield_CreateEx(CPlayer & player, Int32 world, Float32 x, Float32 y, Float32 z,
Uint8 r, Uint8 g, Uint8 b, Float32 radius,
Int32 header, Object & payload)
{
@ -314,14 +403,14 @@ static Object & CreateForcefieldEx(CPlayer & player, Int32 world, Float32 x, Flo
}
// ------------------------------------------------------------------------------------------------
static Object & CreateForcefield(CPlayer & player, Int32 world, const Vector3 & pos,
static Object & Forcefield_Create(CPlayer & player, Int32 world, const Vector3 & pos,
const Color3 & color, Float32 radius)
{
return _Core->NewForcefield(player.GetID(), world, pos.x, pos.y, pos.z, color.r, color.g, color.b, radius,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateForcefield(CPlayer & player, Int32 world, const Vector3 & pos,
static Object & Forcefield_Create(CPlayer & player, Int32 world, const Vector3 & pos,
const Color3 & color, Float32 radius, Int32 header, Object & payload)
{
return _Core->NewForcefield(player.GetID(), world, pos.x, pos.y, pos.z, color.r, color.g, color.b, radius,
@ -333,22 +422,24 @@ void Register_CForcefield(HSQUIRRELVM vm)
{
RootTable(vm).Bind(_SC("SqForcefield"),
Class< CForcefield, NoConstructor< CForcefield > >(vm, _SC("SqForcefield"))
/* Metamethods */
// Metamethods
.Func(_SC("_cmp"), &CForcefield::Cmp)
.SquirrelFunc(_SC("_typename"), &CForcefield::Typename)
.Func(_SC("_tostring"), &CForcefield::ToString)
/* Core Properties */
// Static values
.SetStaticValue(_SC("MaxID"), CForcefield::Max)
// Core Properties
.Prop(_SC("ID"), &CForcefield::GetID)
.Prop(_SC("Tag"), &CForcefield::GetTag, &CForcefield::SetTag)
.Prop(_SC("Data"), &CForcefield::GetData, &CForcefield::SetData)
.Prop(_SC("MaxID"), &CForcefield::GetMaxID)
.Prop(_SC("Active"), &CForcefield::IsActive)
/* Core Functions */
// Core Functions
.Func(_SC("Bind"), &CForcefield::BindEvent)
/* Core Overloads */
// Core Overloads
.Overload< bool (CForcefield::*)(void) >(_SC("Destroy"), &CForcefield::Destroy)
.Overload< bool (CForcefield::*)(Int32) >(_SC("Destroy"), &CForcefield::Destroy)
.Overload< bool (CForcefield::*)(Int32, Object &) >(_SC("Destroy"), &CForcefield::Destroy)
/* Properties */
// Properties
.Prop(_SC("World"), &CForcefield::GetWorld, &CForcefield::SetWorld)
.Prop(_SC("Color"), &CForcefield::GetColor, &CForcefield::SetColor)
.Prop(_SC("Pos"), &CForcefield::GetPosition, &CForcefield::SetPosition)
@ -362,22 +453,21 @@ void Register_CForcefield(HSQUIRRELVM vm)
.Prop(_SC("R"), &CForcefield::GetColR, &CForcefield::SetColR)
.Prop(_SC("G"), &CForcefield::GetColG, &CForcefield::SetColG)
.Prop(_SC("B"), &CForcefield::GetColB, &CForcefield::SetColB)
/* Functions */
// Functions
.Func(_SC("StreamedFor"), &CForcefield::IsStreamedFor)
.Func(_SC("SetColor"), &CForcefield::SetColorEx)
.Func(_SC("SetPos"), &CForcefield::SetPositionEx)
.Func(_SC("SetPosition"), &CForcefield::SetPositionEx)
// Static Overloads
.StaticOverload< Object & (*)(CPlayer &, Int32, Float32, Float32, Float32, Uint8, Uint8, Uint8, Float32) >
(_SC("CreateEx"), &Forcefield_CreateEx)
.StaticOverload< Object & (*)(CPlayer &, Int32, Float32, Float32, Float32, Uint8, Uint8, Uint8, Float32, Int32, Object &) >
(_SC("CreateEx"), &Forcefield_CreateEx)
.StaticOverload< Object & (*)(CPlayer &, Int32, const Vector3 &, const Color3 &, Float32) >
(_SC("Create"), &Forcefield_Create)
.StaticOverload< Object & (*)(CPlayer &, Int32, const Vector3 &, const Color3 &, Float32, Int32, Object &) >
(_SC("Create"), &Forcefield_Create)
);
RootTable(vm)
.Overload< Object & (*)(CPlayer &, Int32, Float32, Float32, Float32, Uint8, Uint8, Uint8, Float32) >
(_SC("CreateForcefieldEx"), &CreateForcefieldEx)
.Overload< Object & (*)(CPlayer &, Int32, Float32, Float32, Float32, Uint8, Uint8, Uint8, Float32, Int32, Object &) >
(_SC("CreateForcefieldEx"), &CreateForcefieldEx)
.Overload< Object & (*)(CPlayer &, Int32, const Vector3 &, const Color3 &, Float32) >
(_SC("CreateForcefield"), &CreateForcefield)
.Overload< Object & (*)(CPlayer &, Int32, const Vector3 &, const Color3 &, Float32, Int32, Object &) >
(_SC("CreateForcefield"), &CreateForcefield);
}
} // Namespace:: SqMod
} // Namespace:: SqMod

View File

@ -8,7 +8,7 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Manages Forcefield instances.
* Manages a single forcefield entity.
*/
class CForcefield
{
@ -24,20 +24,19 @@ private:
// --------------------------------------------------------------------------------------------
static Uint32 s_ColorR, s_ColorG, s_ColorB;
/* --------------------------------------------------------------------------------------------
* Cached identifiers for fast integer to string conversion.
*/
static SQChar s_StrID[SQMOD_FORCEFIELD_POOL][8];
/* --------------------------------------------------------------------------------------------
* Identifier of the managed entity.
*/
Int32 m_ID;
/* --------------------------------------------------------------------------------------------
* User tag and data associated with this instance.
* User tag associated with this instance.
*/
String m_Tag;
/* --------------------------------------------------------------------------------------------
* User data associated with this instance.
*/
Object m_Data;
/* --------------------------------------------------------------------------------------------
@ -45,20 +44,22 @@ private:
*/
CForcefield(Int32 id);
public:
/* --------------------------------------------------------------------------------------------
* Maximum possible number that could represent an identifier for this entity type.
*/
static const Int32 Max;
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
CForcefield(const CForcefield &);
CForcefield(const CForcefield &) = delete;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
* Move constructor. (disabled)
*/
CForcefield & operator = (const CForcefield &);
public:
// --------------------------------------------------------------------------------------------
static const Int32 Max;
CForcefield(CForcefield &&) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
@ -66,14 +67,22 @@ public:
~CForcefield();
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity.
* Copy assignment operator. (disabled)
*/
bool Validate() const
CForcefield & operator = (const CForcefield &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
CForcefield & operator = (CForcefield &&) = delete;
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity otherwise throw an exception.
*/
void Validate() const
{
if (VALID_ENTITY(m_ID))
return true;
SqThrow("Invalid forcefield reference [%s]", m_Tag.c_str());
return false;
if (INVALID_ENTITY(m_ID))
SqThrowF("Invalid forcefield reference [%s]", m_Tag.c_str());
}
/* --------------------------------------------------------------------------------------------
@ -84,27 +93,33 @@ public:
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
const String & ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Retrieve the identifier of the entity managed by this instance.
*/
Int32 GetID() const { return m_ID; }
/* --------------------------------------------------------------------------------------------
* Retrieve the maximum possible identifier to an entity of this type.
*/
Int32 GetMaxID() const { return SQMOD_FORCEFIELD_POOL; }
Int32 GetID() const
{
return m_ID;
}
/* --------------------------------------------------------------------------------------------
* Check whether this instance manages a valid entity.
*/
bool IsActive() const { return VALID_ENTITY(m_ID); }
bool IsActive() const
{
return VALID_ENTITY(m_ID);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the associated user tag.
*/
CSStr GetTag() const;
const String & GetTag() const;
/* --------------------------------------------------------------------------------------------
* Modify the associated user tag.
@ -121,43 +136,155 @@ public:
*/
void SetData(Object & data);
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Destroy the managed forcefield entity.
*/
bool Destroy()
{
return Destroy(0, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed forcefield entity.
*/
bool Destroy(Int32 header)
{
return Destroy(header, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed forcefield entity.
*/
bool Destroy(Int32 header, Object & payload);
bool Destroy() { return Destroy(0, NullObject()); }
bool Destroy(Int32 header) { return Destroy(header, NullObject()); }
// --------------------------------------------------------------------------------------------
bool BindEvent(Int32 evid, Object & env, Function & func) const;
/* --------------------------------------------------------------------------------------------
* Bind to an event supported by this entity type.
*/
void BindEvent(Int32 evid, Object & env, Function & func) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* See if the managed forcefield entity is streamed for the specified player.
*/
bool IsStreamedFor(CPlayer & player) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the world in which the managed forcefield entity exists.
*/
Int32 GetWorld() const;
/* --------------------------------------------------------------------------------------------
* Modify the world in which the managed forcefield entity exists.
*/
void SetWorld(Int32 world) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the color of the managed forcefield entity.
*/
const Color3 & GetColor() const;
/* --------------------------------------------------------------------------------------------
* Modify the color of the managed forcefield entity.
*/
void SetColor(const Color3 & col) const;
/* --------------------------------------------------------------------------------------------
* Modify the color of the managed forcefield entity.
*/
void SetColorEx(Uint8 r, Uint8 g, Uint8 b) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position of the managed forcefield entity.
*/
const Vector3 & GetPosition() const;
/* --------------------------------------------------------------------------------------------
* Modify the position of the managed forcefield entity.
*/
void SetPosition(const Vector3 & pos) const;
/* --------------------------------------------------------------------------------------------
* Modify the position of the managed forcefield entity.
*/
void SetPositionEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the radius of the managed forcefield entity.
*/
Float32 GetRadius() const;
/* --------------------------------------------------------------------------------------------
* Modify the radius of the managed forcefield entity.
*/
void SetRadius(Float32 radius) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the owner of the managed forcefield entity.
*/
Object & GetOwner() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the owner identifier of the managed forcefield entity.
*/
Int32 GetOwnerID() const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the x axis of the managed forcefield entity.
*/
Float32 GetPosX() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the y axis of the managed forcefield entity.
*/
Float32 GetPosY() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the z axis of the managed forcefield entity.
*/
Float32 GetPosZ() const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the x axis of the managed forcefield entity.
*/
void SetPosX(Float32 x) const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the y axis of the managed forcefield entity.
*/
void SetPosY(Float32 y) const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the z axis of the managed forcefield entity.
*/
void SetPosZ(Float32 z) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Retrieve the red color of the managed forcefield entity.
*/
Uint32 GetColR() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the green color of the managed forcefield entity.
*/
Uint32 GetColG() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the blue color of the managed forcefield entity.
*/
Uint32 GetColB() const;
/* --------------------------------------------------------------------------------------------
* Modify the red color of the managed forcefield entity.
*/
void SetColR(Uint32 r) const;
/* --------------------------------------------------------------------------------------------
* Modify the green color of the managed forcefield entity.
*/
void SetColG(Uint32 g) const;
/* --------------------------------------------------------------------------------------------
* Modify the blue color of the managed forcefield entity.
*/
void SetColB(Uint32 b) const;
};

View File

@ -6,15 +6,20 @@
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQChar CKeybind::s_StrID[SQMOD_KEYBIND_POOL][8];
const Int32 CKeybind::Max = SQMOD_KEYBIND_POOL;
// ------------------------------------------------------------------------------------------------
const Int32 CKeybind::Max = SQMOD_KEYBIND_POOL;
SQInteger CKeybind::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("SqKeybind");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
CKeybind::CKeybind(Int32 id)
: m_ID(VALID_ENTITYGETEX(id, SQMOD_KEYBIND_POOL))
, m_Tag(VALID_ENTITY(m_ID) ? s_StrID[m_ID] : _SC("-1"))
, m_Tag(ToStrF("%d", id))
{
/* ... */
}
@ -36,108 +41,124 @@ Int32 CKeybind::Cmp(const CKeybind & o) const
return -1;
}
CSStr CKeybind::ToString() const
// ------------------------------------------------------------------------------------------------
const String & CKeybind::ToString() const
{
return VALID_ENTITYEX(m_ID, SQMOD_KEYBIND_POOL) ? s_StrID[m_ID] : _SC("-1");
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
CSStr CKeybind::GetTag() const
const String & CKeybind::GetTag() const
{
return m_Tag.c_str();
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
void CKeybind::SetTag(CSStr tag)
{
m_Tag.assign(tag);
}
// ------------------------------------------------------------------------------------------------
Object & CKeybind::GetData()
{
if (Validate())
return m_Data;
return NullObject();
// Validate the managed identifier
Validate();
// Return the requested information
return m_Data;
}
// ------------------------------------------------------------------------------------------------
void CKeybind::SetData(Object & data)
{
if (Validate())
m_Data = data;
// Validate the managed identifier
Validate();
// Apply the specified value
m_Data = data;
}
// ------------------------------------------------------------------------------------------------
bool CKeybind::Destroy(Int32 header, Object & payload)
{
// Validate the managed identifier
Validate();
// Perform the requested operation
return _Core->DelKeybind(m_ID, header, payload);
}
// ------------------------------------------------------------------------------------------------
bool CKeybind::BindEvent(Int32 evid, Object & env, Function & func) const
void CKeybind::BindEvent(Int32 evid, Object & env, Function & func) const
{
if (!Validate())
return false;
// Validate the managed identifier
Validate();
// Obtain the function instance called for this event
Function & event = _Core->GetKeybindEvent(m_ID, evid);
// Is the specified callback function null?
if (func.IsNull())
event.Release();
event.Release(); // Then release the current callback
// Assign the specified environment and function
else
event = Function(env.GetVM(), env, func.GetFunc());
return true;
}
// ------------------------------------------------------------------------------------------------
Int32 CKeybind::GetPrimary() const
Int32 CKeybind::GetFirst() const
{
if (Validate())
return _Core->GetKeybind(m_ID).mPrimary;
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetKeybind(m_ID).mFirst;
}
Int32 CKeybind::GetSecondary() const
// ------------------------------------------------------------------------------------------------
Int32 CKeybind::GetSecond() const
{
if (Validate())
return _Core->GetKeybind(m_ID).mSecondary;
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetKeybind(m_ID).mSecond;
}
Int32 CKeybind::GetAlternative() const
// ------------------------------------------------------------------------------------------------
Int32 CKeybind::GetThird() const
{
if (Validate())
return _Core->GetKeybind(m_ID).mAlternative;
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetKeybind(m_ID).mThird;
}
// ------------------------------------------------------------------------------------------------
bool CKeybind::IsRelease() const
{
if (Validate())
return _Core->GetKeybind(m_ID).mRelease;
return false;
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetKeybind(m_ID).mRelease;
}
// ------------------------------------------------------------------------------------------------
static Object & CreateKeybindEx(Int32 slot, bool release, Int32 primary, Int32 secondary,
static Object & Keybind_CreateEx(Int32 slot, bool release, Int32 primary, Int32 secondary,
Int32 alternative)
{
return _Core->NewKeybind(slot, release, primary, secondary, alternative,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateKeybindEx(Int32 slot, bool release, Int32 primary, Int32 secondary,
static Object & Keybind_CreateEx(Int32 slot, bool release, Int32 primary, Int32 secondary,
Int32 alternative, Int32 header, Object & payload)
{
return _Core->NewKeybind(slot, release, primary, secondary, alternative, header, payload);
}
// ------------------------------------------------------------------------------------------------
static Object & CreateKeybind(bool release, Int32 primary, Int32 secondary, Int32 alternative)
static Object & Keybind_Create(bool release, Int32 primary, Int32 secondary, Int32 alternative)
{
return _Core->NewKeybind(-1, release, primary, secondary, alternative,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateKeybind(bool release, Int32 primary, Int32 secondary, Int32 alternative,
static Object & Keybind_Create(bool release, Int32 primary, Int32 secondary, Int32 alternative,
Int32 header, Object & payload)
{
return _Core->NewKeybind(-1, release, primary, secondary, alternative, header, payload);
@ -148,37 +169,38 @@ void Register_CKeybind(HSQUIRRELVM vm)
{
RootTable(vm).Bind(_SC("SqKeybind"),
Class< CKeybind, NoConstructor< CKeybind > >(vm, _SC("SqKeybind"))
/* Metamethods */
// Metamethods
.Func(_SC("_cmp"), &CKeybind::Cmp)
.SquirrelFunc(_SC("_typename"), &CKeybind::Typename)
.Func(_SC("_tostring"), &CKeybind::ToString)
/* Core Properties */
// Static values
.SetStaticValue(_SC("MaxID"), CKeybind::Max)
// Core Properties
.Prop(_SC("ID"), &CKeybind::GetID)
.Prop(_SC("Tag"), &CKeybind::GetTag, &CKeybind::SetTag)
.Prop(_SC("Data"), &CKeybind::GetData, &CKeybind::SetData)
.Prop(_SC("MaxID"), &CKeybind::GetMaxID)
.Prop(_SC("Active"), &CKeybind::IsActive)
/* Core Functions */
// Core Functions
.Func(_SC("Bind"), &CKeybind::BindEvent)
/* Core Overloads */
// Core Overloads
.Overload< bool (CKeybind::*)(void) >(_SC("Destroy"), &CKeybind::Destroy)
.Overload< bool (CKeybind::*)(Int32) >(_SC("Destroy"), &CKeybind::Destroy)
.Overload< bool (CKeybind::*)(Int32, Object &) >(_SC("Destroy"), &CKeybind::Destroy)
/* Properties */
.Prop(_SC("Primary"), &CKeybind::GetPrimary)
.Prop(_SC("Secondary"), &CKeybind::GetSecondary)
.Prop(_SC("Alternative"), &CKeybind::GetAlternative)
// Properties
.Prop(_SC("First"), &CKeybind::GetFirst)
.Prop(_SC("Second"), &CKeybind::GetSecond)
.Prop(_SC("Third"), &CKeybind::GetThird)
.Prop(_SC("Release"), &CKeybind::IsRelease)
// Static Overloads
.StaticOverload< Object & (*)(Int32, bool, Int32, Int32, Int32) >
(_SC("CreateEx"), &Keybind_CreateEx)
.StaticOverload< Object & (*)(Int32, bool, Int32, Int32, Int32, Int32, Object &) >
(_SC("CreateEx"), &Keybind_CreateEx)
.StaticOverload< Object & (*)(bool, Int32, Int32, Int32) >
(_SC("Create"), &Keybind_Create)
.StaticOverload< Object & (*)(bool, Int32, Int32, Int32, Int32, Object &) >
(_SC("Create"), &Keybind_Create)
);
RootTable(vm)
.Overload< Object & (*)(Int32, bool, Int32, Int32, Int32) >
(_SC("CreateKeybindEx"), &CreateKeybindEx)
.Overload< Object & (*)(Int32, bool, Int32, Int32, Int32, Int32, Object &) >
(_SC("CreateKeybindEx"), &CreateKeybindEx)
.Overload< Object & (*)(bool, Int32, Int32, Int32) >
(_SC("CreateKeybind"), &CreateKeybind)
.Overload< Object & (*)(bool, Int32, Int32, Int32, Int32, Object &) >
(_SC("CreateKeybind"), &CreateKeybind);
}
} // Namespace:: SqMod

View File

@ -8,7 +8,7 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Manages Keybind instances.
* Manages a single keybind entity.
*/
class CKeybind
{
@ -17,20 +17,19 @@ class CKeybind
private:
/* --------------------------------------------------------------------------------------------
* Cached identifiers for fast integer to string conversion.
*/
static SQChar s_StrID[SQMOD_KEYBIND_POOL][8];
/* --------------------------------------------------------------------------------------------
* Identifier of the managed entity.
*/
Int32 m_ID;
/* --------------------------------------------------------------------------------------------
* User tag and data associated with this instance.
* User tag associated with this instance.
*/
String m_Tag;
/* --------------------------------------------------------------------------------------------
* User data associated with this instance.
*/
Object m_Data;
/* --------------------------------------------------------------------------------------------
@ -38,20 +37,22 @@ private:
*/
CKeybind(Int32 id);
public:
/* --------------------------------------------------------------------------------------------
* Maximum possible number that could represent an identifier for this entity type.
*/
static const Int32 Max;
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
CKeybind(const CKeybind &);
CKeybind(const CKeybind &) = delete;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
* Move constructor. (disabled)
*/
CKeybind & operator = (const CKeybind &);
public:
// --------------------------------------------------------------------------------------------
static const Int32 Max;
CKeybind(CKeybind &&) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
@ -59,14 +60,22 @@ public:
~CKeybind();
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity.
* Copy assignment operator. (disabled)
*/
bool Validate() const
CKeybind & operator = (const CKeybind &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
CKeybind & operator = (CKeybind &&) = delete;
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity instance otherwise throw an exception.
*/
void Validate() const
{
if (VALID_ENTITY(m_ID))
return true;
SqThrow("Invalid keybind reference [%s]", m_Tag.c_str());
return false;
if (INVALID_ENTITY(m_ID))
SqThrowF("Invalid keybind reference [%s]", m_Tag.c_str());
}
/* --------------------------------------------------------------------------------------------
@ -77,27 +86,33 @@ public:
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
const String & ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Retrieve the identifier of the entity managed by this instance.
*/
Int32 GetID() const { return m_ID; }
/* --------------------------------------------------------------------------------------------
* Retrieve the maximum possible identifier to an entity of this type.
*/
Int32 GetMaxID() const { return SQMOD_KEYBIND_POOL; }
Int32 GetID() const
{
return m_ID;
}
/* --------------------------------------------------------------------------------------------
* Check whether this instance manages a valid entity.
*/
bool IsActive() const { return VALID_ENTITY(m_ID); }
bool IsActive() const
{
return VALID_ENTITY(m_ID);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the associated user tag.
*/
CSStr GetTag() const;
const String & GetTag() const;
/* --------------------------------------------------------------------------------------------
* Modify the associated user tag.
@ -114,18 +129,50 @@ public:
*/
void SetData(Object & data);
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Destroy the managed destroy entity.
*/
bool Destroy()
{
return Destroy(0, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed destroy entity.
*/
bool Destroy(Int32 header)
{
return Destroy(header, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed destroy entity.
*/
bool Destroy(Int32 header, Object & payload);
bool Destroy() { return Destroy(0, NullObject()); }
bool Destroy(Int32 header) { return Destroy(header, NullObject()); }
// --------------------------------------------------------------------------------------------
bool BindEvent(Int32 evid, Object & env, Function & func) const;
/* --------------------------------------------------------------------------------------------
* Bind to an event supported by this entity type.
*/
void BindEvent(Int32 evid, Object & env, Function & func) const;
// --------------------------------------------------------------------------------------------
Int32 GetPrimary() const;
Int32 GetSecondary() const;
Int32 GetAlternative() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the first key code of the managed keybind entity.
*/
Int32 GetFirst() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the second key code of the managed keybind entity.
*/
Int32 GetSecond() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the third key code of the managed keybind entity.
*/
Int32 GetThird() const;
/* --------------------------------------------------------------------------------------------
* See whether the managed keybind entity reacts to key release events.
*/
bool IsRelease() const;
};

View File

@ -13,15 +13,20 @@ Vector3 CObject::s_Vector3;
Quaternion CObject::s_Quaternion;
// ------------------------------------------------------------------------------------------------
SQChar CObject::s_StrID[SQMOD_OBJECT_POOL][8];
const Int32 CObject::Max = SQMOD_OBJECT_POOL;
// ------------------------------------------------------------------------------------------------
const Int32 CObject::Max = SQMOD_OBJECT_POOL;
SQInteger CObject::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("SqObject");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
CObject::CObject(Int32 id)
: m_ID(VALID_ENTITYGETEX(id, SQMOD_OBJECT_POOL))
, m_Tag(VALID_ENTITY(m_ID) ? s_StrID[m_ID] : _SC("-1"))
, m_Tag(ToStrF("%d", id))
{
/* ... */
}
@ -43,372 +48,517 @@ Int32 CObject::Cmp(const CObject & o) const
return -1;
}
CSStr CObject::ToString() const
// ------------------------------------------------------------------------------------------------
const String & CObject::ToString() const
{
return VALID_ENTITYEX(m_ID, SQMOD_OBJECT_POOL) ? s_StrID[m_ID] : _SC("-1");
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
CSStr CObject::GetTag() const
const String & CObject::GetTag() const
{
return m_Tag.c_str();
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
void CObject::SetTag(CSStr tag)
{
m_Tag.assign(tag);
}
// ------------------------------------------------------------------------------------------------
Object & CObject::GetData()
{
if (Validate())
return m_Data;
return NullObject();
// Validate the managed identifier
Validate();
// Return the requested information
return m_Data;
}
// ------------------------------------------------------------------------------------------------
void CObject::SetData(Object & data)
{
if (Validate())
m_Data = data;
// Validate the managed identifier
Validate();
// Apply the specified value
m_Data = data;
}
// ------------------------------------------------------------------------------------------------
bool CObject::Destroy(Int32 header, Object & payload)
{
// Validate the managed identifier
Validate();
// Perform the requested operation
return _Core->DelObject(m_ID, header, payload);
}
// ------------------------------------------------------------------------------------------------
bool CObject::BindEvent(Int32 evid, Object & env, Function & func) const
void CObject::BindEvent(Int32 evid, Object & env, Function & func) const
{
if (!Validate())
return false;
// Validate the managed identifier
Validate();
// Obtain the function instance called for this event
Function & event = _Core->GetObjectEvent(m_ID, evid);
// Is the specified callback function null?
if (func.IsNull())
event.Release();
event.Release(); // Then release the current callback
// Assign the specified environment and function
else
event = Function(env.GetVM(), env, func.GetFunc());
return true;
}
// ------------------------------------------------------------------------------------------------
bool CObject::IsStreamedFor(CPlayer & player) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
return _Func->IsObjectStreamedForPlayer(m_ID, player.GetID());
return false;
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->IsObjectStreamedForPlayer(m_ID, player.GetID());
}
// ------------------------------------------------------------------------------------------------
Int32 CObject::GetModel() const
{
if (Validate())
return _Func->GetObjectModel(m_ID);
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->GetObjectModel(m_ID);
}
// ------------------------------------------------------------------------------------------------
Int32 CObject::GetWorld() const
{
if (Validate())
return _Func->GetObjectWorld(m_ID);
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->GetObjectWorld(m_ID);
}
// ------------------------------------------------------------------------------------------------
void CObject::SetWorld(Int32 world) const
{
if (Validate())
_Func->SetObjectWorld(m_ID, world);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetObjectWorld(m_ID, world);
}
// ------------------------------------------------------------------------------------------------
Int32 CObject::GetAlpha() const
{
if (Validate())
return _Func->GetObjectAlpha(m_ID);
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->GetObjectAlpha(m_ID);
}
// ------------------------------------------------------------------------------------------------
void CObject::SetAlpha(Int32 alpha) const
{
if (Validate())
_Func->SetObjectAlpha(m_ID, alpha, 0);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetObjectAlpha(m_ID, alpha, 0);
}
// ------------------------------------------------------------------------------------------------
void CObject::SetAlphaEx(Int32 alpha, Int32 time) const
{
if (Validate())
_Func->SetObjectAlpha(m_ID, alpha, time);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetObjectAlpha(m_ID, alpha, time);
}
// ------------------------------------------------------------------------------------------------
void CObject::MoveTo(const Vector3 & pos, Int32 time) const
{
if (Validate())
_Func->MoveObjectTo(m_ID, pos.x, pos.y, pos.z, time);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveObjectTo(m_ID, pos.x, pos.y, pos.z, time);
}
// ------------------------------------------------------------------------------------------------
void CObject::MoveToEx(Float32 x, Float32 y, Float32 z, Int32 time) const
{
if (Validate())
_Func->MoveObjectTo(m_ID, x, y, z, time);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveObjectTo(m_ID, x, y, z, time);
}
// ------------------------------------------------------------------------------------------------
void CObject::MoveBy(const Vector3 & pos, Int32 time) const
{
if (Validate())
_Func->MoveObjectBy(m_ID, pos.x, pos.y, pos.z, time);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveObjectBy(m_ID, pos.x, pos.y, pos.z, time);
}
// ------------------------------------------------------------------------------------------------
void CObject::MoveByEx(Float32 x, Float32 y, Float32 z, Int32 time) const
{
if (Validate())
_Func->MoveObjectBy(m_ID, x, y, z, time);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveObjectBy(m_ID, x, y, z, time);
}
// ------------------------------------------------------------------------------------------------
const Vector3 & CObject::GetPosition()
{
// Validate the managed identifier
Validate();
// Clear previous position information
s_Vector3.Clear();
if (Validate())
_Func->GetObjectPos(m_ID, &s_Vector3.x, &s_Vector3.y, &s_Vector3.z);
// Query the server for the position values
_Func->GetObjectPos(m_ID, &s_Vector3.x, &s_Vector3.y, &s_Vector3.z);
// Return the requested information
return s_Vector3;
}
// ------------------------------------------------------------------------------------------------
void CObject::SetPosition(const Vector3 & pos) const
{
if (Validate())
_Func->SetObjectPos(m_ID, pos.x, pos.y, pos.z);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetObjectPos(m_ID, pos.x, pos.y, pos.z);
}
// ------------------------------------------------------------------------------------------------
void CObject::SetPositionEx(Float32 x, Float32 y, Float32 z) const
{
if (Validate())
_Func->SetObjectPos(m_ID, x, y, z);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetObjectPos(m_ID, x, y, z);
}
// ------------------------------------------------------------------------------------------------
void CObject::RotateTo(const Quaternion & rot, Int32 time) const
{
if (Validate())
_Func->RotObjectTo(m_ID, rot.x, rot.y, rot.z, rot.w, time);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->RotObjectTo(m_ID, rot.x, rot.y, rot.z, rot.w, time);
}
// ------------------------------------------------------------------------------------------------
void CObject::RotateToEx(Float32 x, Float32 y, Float32 z, Float32 w, Int32 time) const
{
if (Validate())
_Func->RotObjectTo(m_ID, x, y, z, w, time);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->RotObjectTo(m_ID, x, y, z, w, time);
}
// ------------------------------------------------------------------------------------------------
void CObject::RotateToEuler(const Vector3 & rot, Int32 time) const
{
if (Validate())
_Func->RotObjectToEuler(m_ID, rot.x, rot.y, rot.z, time);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->RotObjectToEuler(m_ID, rot.x, rot.y, rot.z, time);
}
// ------------------------------------------------------------------------------------------------
void CObject::RotateToEulerEx(Float32 x, Float32 y, Float32 z, Int32 time) const
{
if (Validate())
_Func->RotObjectToEuler(m_ID, x, y, z, time);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->RotObjectToEuler(m_ID, x, y, z, time);
}
// ------------------------------------------------------------------------------------------------
void CObject::RotateBy(const Quaternion & rot, Int32 time) const
{
if (Validate())
_Func->RotObjectBy(m_ID, rot.x, rot.y, rot.z, rot.w, time);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->RotObjectBy(m_ID, rot.x, rot.y, rot.z, rot.w, time);
}
// ------------------------------------------------------------------------------------------------
void CObject::RotateByEx(Float32 x, Float32 y, Float32 z, Float32 w, Int32 time) const
{
if (Validate())
_Func->RotObjectBy(m_ID, x, y, z, w, time);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->RotObjectBy(m_ID, x, y, z, w, time);
}
// ------------------------------------------------------------------------------------------------
void CObject::RotateByEuler(const Vector3 & rot, Int32 time) const
{
if (Validate())
_Func->RotObjectByEuler(m_ID, rot.x, rot.y, rot.z, time);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->RotObjectByEuler(m_ID, rot.x, rot.y, rot.z, time);
}
// ------------------------------------------------------------------------------------------------
void CObject::RotateByEulerEx(Float32 x, Float32 y, Float32 z, Int32 time) const
{
if (Validate())
_Func->RotObjectByEuler(m_ID, x, y, z, time);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->RotObjectByEuler(m_ID, x, y, z, time);
}
// ------------------------------------------------------------------------------------------------
const Quaternion & CObject::GetRotation()
{
// Validate the managed identifier
Validate();
// Clear previous rotation information
s_Quaternion.Clear();
if (Validate())
_Func->GetObjectRot(m_ID, &s_Quaternion.x, &s_Quaternion.y, &s_Quaternion.z, &s_Quaternion.w);
// Query the server for the rotation values
_Func->GetObjectRot(m_ID, &s_Quaternion.x, &s_Quaternion.y, &s_Quaternion.z, &s_Quaternion.w);
// Return the requested information
return s_Quaternion;
}
// ------------------------------------------------------------------------------------------------
const Vector3 & CObject::GetRotationEuler()
{
// Validate the managed identifier
Validate();
// Clear previous rotation information
s_Vector3.Clear();
if (Validate())
_Func->GetObjectRotEuler(m_ID, &s_Vector3.x, &s_Vector3.y, &s_Vector3.z);
// Query the server for the rotation values
_Func->GetObjectRotEuler(m_ID, &s_Vector3.x, &s_Vector3.y, &s_Vector3.z);
// Return the requested information
return s_Vector3;
}
// ------------------------------------------------------------------------------------------------
bool CObject::GetShotReport() const
{
if (Validate())
return _Func->IsObjectShotReport(m_ID);
return false;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->IsObjectShotReport(m_ID);
}
// ------------------------------------------------------------------------------------------------
void CObject::SetShotReport(bool toggle) const
{
if (Validate())
_Func->SetObjectShotReport(m_ID, toggle);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetObjectShotReport(m_ID, toggle);
}
// ------------------------------------------------------------------------------------------------
bool CObject::GetBumpReport() const
{
if (Validate())
return _Func->IsObjectBumpReport(m_ID);
return false;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->IsObjectBumpReport(m_ID);
}
// ------------------------------------------------------------------------------------------------
void CObject::SetBumpReport(bool toggle) const
{
if (Validate())
_Func->SetObjectBumpReport(m_ID, toggle);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetObjectBumpReport(m_ID, toggle);
}
// ------------------------------------------------------------------------------------------------
Float32 CObject::GetPosX() const
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.x = 0;
if (Validate())
_Func->GetObjectPos(m_ID, &s_Vector3.x, NULL, NULL);
// Query the server for the requested component value
_Func->GetObjectPos(m_ID, &s_Vector3.x, NULL, NULL);
// Return the requested information
return s_Vector3.x;
}
// ------------------------------------------------------------------------------------------------
Float32 CObject::GetPosY() const
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.y = 0;
if (Validate())
_Func->GetObjectPos(m_ID, NULL, &s_Vector3.y, NULL);
// Query the server for the requested component value
_Func->GetObjectPos(m_ID, NULL, &s_Vector3.y, NULL);
// Return the requested information
return s_Vector3.y;
}
// ------------------------------------------------------------------------------------------------
Float32 CObject::GetPosZ() const
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.z = 0;
if (Validate())
_Func->GetObjectPos(m_ID, NULL, NULL, &s_Vector3.z);
// Query the server for the requested component value
_Func->GetObjectPos(m_ID, NULL, NULL, &s_Vector3.z);
// Return the requested information
return s_Vector3.z;
}
// ------------------------------------------------------------------------------------------------
void CObject::SetPosX(Float32 x) const
{
if (Validate())
{
_Func->GetObjectPos(m_ID, NULL, &s_Vector3.y, &s_Vector3.z);
_Func->SetObjectPos(m_ID, x, s_Vector3.y, s_Vector3.z);
}
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetObjectPos(m_ID, NULL, &s_Vector3.y, &s_Vector3.z);
// Perform the requested operation
_Func->SetObjectPos(m_ID, x, s_Vector3.y, s_Vector3.z);
}
// ------------------------------------------------------------------------------------------------
void CObject::SetPosY(Float32 y) const
{
if (Validate())
{
_Func->GetObjectPos(m_ID, &s_Vector3.x, NULL, &s_Vector3.z);
_Func->SetObjectPos(m_ID, s_Vector3.x, y, s_Vector3.z);
}
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetObjectPos(m_ID, &s_Vector3.x, NULL, &s_Vector3.z);
// Perform the requested operation
_Func->SetObjectPos(m_ID, s_Vector3.x, y, s_Vector3.z);
}
// ------------------------------------------------------------------------------------------------
void CObject::SetPosZ(Float32 z) const
{
if (Validate())
{
_Func->GetObjectPos(m_ID, &s_Vector3.x, &s_Vector3.y, NULL);
_Func->SetObjectPos(m_ID, s_Vector3.z, s_Vector3.y, z);
}
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->GetObjectPos(m_ID, &s_Vector3.x, &s_Vector3.y, NULL);
// Perform the requested operation
_Func->SetObjectPos(m_ID, s_Vector3.z, s_Vector3.y, z);
}
// ------------------------------------------------------------------------------------------------
Float32 CObject::GetRotX() const
{
// Validate the managed identifier
Validate();
// Clear previous rotation information, if any
s_Quaternion.x = 0;
if (Validate())
_Func->GetObjectRot(m_ID, &s_Quaternion.x, NULL, NULL, NULL);
// Query the server for the requested component value
_Func->GetObjectRot(m_ID, &s_Quaternion.x, NULL, NULL, NULL);
// Return the requested information
return s_Quaternion.x;
}
// ------------------------------------------------------------------------------------------------
Float32 CObject::GetRotY() const
{
// Validate the managed identifier
Validate();
// Clear previous rotation information, if any
s_Quaternion.y = 0;
if (Validate())
_Func->GetObjectRot(m_ID, NULL, &s_Quaternion.y, NULL, NULL);
// Query the server for the requested component value
_Func->GetObjectRot(m_ID, NULL, &s_Quaternion.y, NULL, NULL);
// Return the requested information
return s_Quaternion.y;
}
// ------------------------------------------------------------------------------------------------
Float32 CObject::GetRotZ() const
{
// Validate the managed identifier
Validate();
// Clear previous rotation information, if any
s_Quaternion.z = 0;
if (Validate())
_Func->GetObjectRot(m_ID, NULL, NULL, &s_Quaternion.z, NULL);
// Query the server for the requested component value
_Func->GetObjectRot(m_ID, NULL, NULL, &s_Quaternion.z, NULL);
// Return the requested information
return s_Quaternion.z;
}
// ------------------------------------------------------------------------------------------------
Float32 CObject::GetRotW() const
{
// Validate the managed identifier
Validate();
// Clear previous rotation information, if any
s_Quaternion.w = 0;
if (Validate())
_Func->GetObjectRot(m_ID, NULL, NULL, NULL, &s_Quaternion.w);
// Query the server for the requested component value
_Func->GetObjectRot(m_ID, NULL, NULL, NULL, &s_Quaternion.w);
// Return the requested information
return s_Quaternion.w;
}
// ------------------------------------------------------------------------------------------------
Float32 CObject::GetERotX() const
{
// Validate the managed identifier
Validate();
// Clear previous rotation information, if any
s_Vector3.x = 0;
if (Validate())
_Func->GetObjectRotEuler(m_ID, &s_Vector3.x, NULL, NULL);
// Query the server for the requested component value
_Func->GetObjectRotEuler(m_ID, &s_Vector3.x, NULL, NULL);
// Return the requested information
return s_Vector3.x;
}
// ------------------------------------------------------------------------------------------------
Float32 CObject::GetERotY() const
{
// Validate the managed identifier
Validate();
// Clear previous rotation information, if any
s_Vector3.y = 0;
if (Validate())
_Func->GetObjectRotEuler(m_ID, NULL, &s_Vector3.y, NULL);
// Query the server for the requested component value
_Func->GetObjectRotEuler(m_ID, NULL, &s_Vector3.y, NULL);
// Return the requested information
return s_Vector3.y;
}
// ------------------------------------------------------------------------------------------------
Float32 CObject::GetERotZ() const
{
// Validate the managed identifier
Validate();
// Clear previous rotation information, if any
s_Vector3.z = 0;
if (Validate())
_Func->GetObjectRotEuler(m_ID, NULL, NULL, &s_Vector3.z);
// Query the server for the requested component value
_Func->GetObjectRotEuler(m_ID, NULL, NULL, &s_Vector3.z);
// Return the requested information
return s_Vector3.z;
}
// ------------------------------------------------------------------------------------------------
static Object & CreateObjectEx(Int32 model, Int32 world, Float32 x, Float32 y, Float32 z,
static Object & Object_CreateEx(Int32 model, Int32 world, Float32 x, Float32 y, Float32 z,
Int32 alpha)
{
return _Core->NewObject(model, world, x, y, z, alpha, SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateObjectEx(Int32 model, Int32 world, Float32 x, Float32 y, Float32 z,
static Object & Object_CreateEx(Int32 model, Int32 world, Float32 x, Float32 y, Float32 z,
Int32 alpha, Int32 header, Object & payload)
{
return _Core->NewObject(model, world, x, y, z, alpha, header, payload);
}
// ------------------------------------------------------------------------------------------------
static Object & CreateObject(Int32 model, Int32 world, const Vector3 & pos, Int32 alpha)
static Object & Object_Create(Int32 model, Int32 world, const Vector3 & pos, Int32 alpha)
{
return _Core->NewObject(model, world, pos.x, pos.y, pos.z, alpha,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateObject(Int32 model, Int32 world, const Vector3 & pos, Int32 alpha,
static Object & Object_Create(Int32 model, Int32 world, const Vector3 & pos, Int32 alpha,
Int32 header, Object & payload)
{
return _Core->NewObject(model, world, pos.x, pos.y, pos.z, alpha, header, payload);
@ -419,22 +569,24 @@ void Register_CObject(HSQUIRRELVM vm)
{
RootTable(vm).Bind(_SC("SqObject"),
Class< CObject, NoConstructor< CObject > >(vm, _SC("SqObject"))
/* Metamethods */
// Metamethods
.Func(_SC("_cmp"), &CObject::Cmp)
.SquirrelFunc(_SC("_typename"), &CObject::Typename)
.Func(_SC("_tostring"), &CObject::ToString)
/* Core Properties */
// Static values
.SetStaticValue(_SC("MaxID"), CObject::Max)
// Core Properties
.Prop(_SC("ID"), &CObject::GetID)
.Prop(_SC("Tag"), &CObject::GetTag, &CObject::SetTag)
.Prop(_SC("Data"), &CObject::GetData, &CObject::SetData)
.Prop(_SC("MaxID"), &CObject::GetMaxID)
.Prop(_SC("Active"), &CObject::IsActive)
/* Core Functions */
// Core Functions
.Func(_SC("Bind"), &CObject::BindEvent)
/* Core Overloads */
// Core Overloads
.Overload< bool (CObject::*)(void) >(_SC("Destroy"), &CObject::Destroy)
.Overload< bool (CObject::*)(Int32) >(_SC("Destroy"), &CObject::Destroy)
.Overload< bool (CObject::*)(Int32, Object &) >(_SC("Destroy"), &CObject::Destroy)
/* Properties */
// Properties
.Prop(_SC("Model"), &CObject::GetModel)
.Prop(_SC("World"), &CObject::GetWorld, &CObject::SetWorld)
.Prop(_SC("Alpha"), &CObject::GetAlpha, &CObject::SetAlpha)
@ -455,11 +607,11 @@ void Register_CObject(HSQUIRRELVM vm)
.Prop(_SC("EX"), &CObject::GetERotX)
.Prop(_SC("EY"), &CObject::GetERotY)
.Prop(_SC("EZ"), &CObject::GetERotZ)
/* Functions */
// Functions
.Func(_SC("StreamedFor"), &CObject::IsStreamedFor)
.Func(_SC("SetAlpha"), &CObject::SetAlphaEx)
.Func(_SC("SetPosition"), &CObject::SetPositionEx)
/* Overloads */
// Overloads
.Overload< void (CObject::*)(const Vector3 &, Int32) const >
(_SC("MoveTo"), &CObject::MoveTo)
.Overload< void (CObject::*)(Float32, Float32, Float32, Int32) const >
@ -484,17 +636,16 @@ void Register_CObject(HSQUIRRELVM vm)
(_SC("RotateByEuler"), &CObject::RotateByEuler)
.Overload< void (CObject::*)(Float32, Float32, Float32, Int32) const >
(_SC("RotateByEuler"), &CObject::RotateByEulerEx)
// Static Overloads
.StaticOverload< Object & (*)(Int32, Int32, Float32, Float32, Float32, Int32) >
(_SC("CreateEx"), &Object_CreateEx)
.StaticOverload< Object & (*)(Int32, Int32, Float32, Float32, Float32, Int32, Int32, Object &) >
(_SC("CreateEx"), &Object_CreateEx)
.StaticOverload< Object & (*)(Int32, Int32, const Vector3 &, Int32) >
(_SC("Create"), &Object_Create)
.StaticOverload< Object & (*)(Int32, Int32, const Vector3 &, Int32, Int32, Object &) >
(_SC("Create"), &Object_Create)
);
RootTable(vm)
.Overload< Object & (*)(Int32, Int32, Float32, Float32, Float32, Int32) >
(_SC("CreateObjectEx"), &CreateObjectEx)
.Overload< Object & (*)(Int32, Int32, Float32, Float32, Float32, Int32, Int32, Object &) >
(_SC("CreateObjectEx"), &CreateObjectEx)
.Overload< Object & (*)(Int32, Int32, const Vector3 &, Int32) >
(_SC("CreateObject"), &CreateObject)
.Overload< Object & (*)(Int32, Int32, const Vector3 &, Int32, Int32, Object &) >
(_SC("CreateObject"), &CreateObject);
}
} // Namespace:: SqMod
} // Namespace:: SqMod

View File

@ -8,7 +8,7 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Manages Object instances.
* Manages a single object entity.
*/
class CObject
{
@ -21,20 +21,19 @@ private:
static Vector3 s_Vector3;
static Quaternion s_Quaternion;
/* --------------------------------------------------------------------------------------------
* Cached identifiers for fast integer to string conversion.
*/
static SQChar s_StrID[SQMOD_OBJECT_POOL][8];
/* --------------------------------------------------------------------------------------------
* Identifier of the managed entity.
*/
Int32 m_ID;
/* --------------------------------------------------------------------------------------------
* User tag and data associated with this instance.
* User tag associated with this instance.
*/
String m_Tag;
/* --------------------------------------------------------------------------------------------
* User data associated with this instance.
*/
Object m_Data;
/* --------------------------------------------------------------------------------------------
@ -42,20 +41,22 @@ private:
*/
CObject(Int32 id);
public:
/* --------------------------------------------------------------------------------------------
* Maximum possible number that could represent an identifier for this entity type.
*/
static const Int32 Max;
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
CObject(const CObject &);
CObject(const CObject &) = delete;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
* Move constructor. (disabled)
*/
CObject & operator = (const CObject &);
public:
// --------------------------------------------------------------------------------------------
static const Int32 Max;
CObject(CObject &&) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
@ -63,14 +64,22 @@ public:
~CObject();
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity.
* Copy assignment operator. (disabled)
*/
bool Validate() const
CObject & operator = (const CObject &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
CObject & operator = (CObject &&) = delete;
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity instance otherwise throw an exception.
*/
void Validate() const
{
if (VALID_ENTITY(m_ID))
return true;
SqThrow("Invalid object reference [%s]", m_Tag.c_str());
return false;
if (INVALID_ENTITY(m_ID))
SqThrowF("Invalid object reference [%s]", m_Tag.c_str());
}
/* --------------------------------------------------------------------------------------------
@ -81,27 +90,33 @@ public:
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
const String & ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Retrieve the identifier of the entity managed by this instance.
*/
Int32 GetID() const { return m_ID; }
/* --------------------------------------------------------------------------------------------
* Retrieve the maximum possible identifier to an entity of this type.
*/
Int32 GetMaxID() const { return SQMOD_OBJECT_POOL; }
Int32 GetID() const
{
return m_ID;
}
/* --------------------------------------------------------------------------------------------
* Check whether this instance manages a valid entity.
*/
bool IsActive() const { return VALID_ENTITY(m_ID); }
bool IsActive() const
{
return VALID_ENTITY(m_ID);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the associated user tag.
*/
CSStr GetTag() const;
const String & GetTag() const;
/* --------------------------------------------------------------------------------------------
* Modify the associated user tag.
@ -118,59 +133,235 @@ public:
*/
void SetData(Object & data);
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Destroy the managed object entity.
*/
bool Destroy()
{
return Destroy(0, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed object entity.
*/
bool Destroy(Int32 header)
{
return Destroy(header, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed object entity.
*/
bool Destroy(Int32 header, Object & payload);
bool Destroy() { return Destroy(0, NullObject()); }
bool Destroy(Int32 header) { return Destroy(header, NullObject()); }
// --------------------------------------------------------------------------------------------
bool BindEvent(Int32 evid, Object & env, Function & func) const;
/* --------------------------------------------------------------------------------------------
* Bind to an event supported by this entity type.
*/
void BindEvent(Int32 evid, Object & env, Function & func) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* See if the managed object entity is streamed for the specified player.
*/
bool IsStreamedFor(CPlayer & player) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the model of the managed object entity.
*/
Int32 GetModel() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the world in which the managed object entity exists.
*/
Int32 GetWorld() const;
/* --------------------------------------------------------------------------------------------
* Modify the world in which the managed object entity exists.
*/
void SetWorld(Int32 world) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the alpha of the managed object entity.
*/
Int32 GetAlpha() const;
/* --------------------------------------------------------------------------------------------
* Modify the alpha of the managed object entity.
*/
void SetAlpha(Int32 alpha) const;
/* --------------------------------------------------------------------------------------------
* Modify the alpha of the managed object entity over the specified time.
*/
void SetAlphaEx(Int32 alpha, Int32 time) const;
/* --------------------------------------------------------------------------------------------
* Move the managed object entity to the specified position over the specified time.
*/
void MoveTo(const Vector3 & pos, Int32 time) const;
/* --------------------------------------------------------------------------------------------
* Move the managed object entity to the specified position over the specified time.
*/
void MoveToEx(Float32 x, Float32 y, Float32 z, Int32 time) const;
/* --------------------------------------------------------------------------------------------
* Move the managed object entity by the specified position over the specified time.
*/
void MoveBy(const Vector3 & pos, Int32 time) const;
/* --------------------------------------------------------------------------------------------
* Move the managed object entity by the specified position over the specified time.
*/
void MoveByEx(Float32 x, Float32 y, Float32 z, Int32 time) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position of the managed object entity.
*/
const Vector3 & GetPosition();
/* --------------------------------------------------------------------------------------------
* Modify the position of the managed object entity.
*/
void SetPosition(const Vector3 & pos) const;
/* --------------------------------------------------------------------------------------------
* Modify the position of the managed object entity.
*/
void SetPositionEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Rotate the managed object entity to the specified rotation over the specified time.
*/
void RotateTo(const Quaternion & rot, Int32 time) const;
/* --------------------------------------------------------------------------------------------
* Rotate the managed object entity to the specified rotation over the specified time.
*/
void RotateToEx(Float32 x, Float32 y, Float32 z, Float32 w, Int32 time) const;
/* --------------------------------------------------------------------------------------------
* Rotate the managed object entity to the specified euler rotation over the specified time.
*/
void RotateToEuler(const Vector3 & rot, Int32 time) const;
/* --------------------------------------------------------------------------------------------
* Rotate the managed object entity to the specified euler rotation over the specified time.
*/
void RotateToEulerEx(Float32 x, Float32 y, Float32 z, Int32 time) const;
/* --------------------------------------------------------------------------------------------
* Rotate the managed object entity by the specified rotation over the specified time.
*/
void RotateBy(const Quaternion & rot, Int32 time) const;
/* --------------------------------------------------------------------------------------------
* Rotate the managed object entity by the specified rotation over the specified time.
*/
void RotateByEx(Float32 x, Float32 y, Float32 z, Float32 w, Int32 time) const;
/* --------------------------------------------------------------------------------------------
* Rotate the managed object entity by the specified euler rotation over the specified time.
*/
void RotateByEuler(const Vector3 & rot, Int32 time) const;
/* --------------------------------------------------------------------------------------------
* Rotate the managed object entity by the specified euler rotation over the specified time.
*/
void RotateByEulerEx(Float32 x, Float32 y, Float32 z, Int32 time) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the rotation of the managed object entity.
*/
const Quaternion & GetRotation();
/* --------------------------------------------------------------------------------------------
* Retrieve the euler rotation of the managed object entity.
*/
const Vector3 & GetRotationEuler();
/* --------------------------------------------------------------------------------------------
* See whether the managed object entity reports gunshots.
*/
bool GetShotReport() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed object entity reports gunshots.
*/
void SetShotReport(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed object entity reports player bumps.
*/
bool GetBumpReport() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed object entity reports player bumps.
*/
void SetBumpReport(bool toggle) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the x axis of the managed object entity.
*/
Float32 GetPosX() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the y axis of the managed object entity.
*/
Float32 GetPosY() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the z axis of the managed object entity.
*/
Float32 GetPosZ() const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the x axis of the managed object entity.
*/
void SetPosX(Float32 x) const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the y axis of the managed object entity.
*/
void SetPosY(Float32 y) const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the z axis of the managed object entity.
*/
void SetPosZ(Float32 z) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Retrieve the rotation on the x axis of the managed object entity.
*/
Float32 GetRotX() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the rotation on the y axis of the managed object entity.
*/
Float32 GetRotY() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the rotation on the z axis of the managed object entity.
*/
Float32 GetRotZ() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the rotation amount of the managed object entity.
*/
Float32 GetRotW() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the euler rotation on the x axis of the managed object entity.
*/
Float32 GetERotX() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the euler rotation on the y axis of the managed object entity.
*/
Float32 GetERotY() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the euler rotation on the z axis of the managed object entity.
*/
Float32 GetERotZ() const;
};

View File

@ -11,15 +11,20 @@ namespace SqMod {
Vector3 CPickup::s_Vector3;
// ------------------------------------------------------------------------------------------------
SQChar CPickup::s_StrID[SQMOD_PICKUP_POOL][8];
const Int32 CPickup::Max = SQMOD_PICKUP_POOL;
// ------------------------------------------------------------------------------------------------
const Int32 CPickup::Max = SQMOD_PICKUP_POOL;
SQInteger CPickup::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("SqPickup");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
CPickup::CPickup(Int32 id)
: m_ID(VALID_ENTITYGETEX(id, SQMOD_PICKUP_POOL))
, m_Tag(VALID_ENTITY(m_ID) ? s_StrID[m_ID] : _SC("-1"))
, m_Tag(ToStrF("%d", id))
{
/* ... */
}
@ -41,221 +46,289 @@ Int32 CPickup::Cmp(const CPickup & o) const
return -1;
}
CSStr CPickup::ToString() const
// ------------------------------------------------------------------------------------------------
const String & CPickup::ToString() const
{
return VALID_ENTITYEX(m_ID, SQMOD_PICKUP_POOL) ? s_StrID[m_ID] : _SC("-1");
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
CSStr CPickup::GetTag() const
const String & CPickup::GetTag() const
{
return m_Tag.c_str();
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
void CPickup::SetTag(CSStr tag)
{
m_Tag.assign(tag);
}
// ------------------------------------------------------------------------------------------------
Object & CPickup::GetData()
{
if (Validate())
return m_Data;
return NullObject();
// Validate the managed identifier
Validate();
// Return the requested information
return m_Data;
}
// ------------------------------------------------------------------------------------------------
void CPickup::SetData(Object & data)
{
if (Validate())
m_Data = data;
// Validate the managed identifier
Validate();
// Apply the specified value
m_Data = data;
}
// ------------------------------------------------------------------------------------------------
bool CPickup::Destroy(Int32 header, Object & payload)
{
// Validate the managed identifier
Validate();
// Perform the requested operation
return _Core->DelPickup(m_ID, header, payload);
}
// ------------------------------------------------------------------------------------------------
bool CPickup::BindEvent(Int32 evid, Object & env, Function & func) const
void CPickup::BindEvent(Int32 evid, Object & env, Function & func) const
{
if (!Validate())
return false;
// Validate the managed identifier
Validate();
// Obtain the function instance called for this event
Function & event = _Core->GetPickupEvent(m_ID, evid);
// Is the specified callback function null?
if (func.IsNull())
event.Release();
event.Release(); // Then release the current callback
// Assign the specified environment and function
else
event = Function(env.GetVM(), env, func.GetFunc());
return true;
}
// ------------------------------------------------------------------------------------------------
bool CPickup::IsStreamedFor(CPlayer & player) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
return _Func->IsPickupStreamedForPlayer(m_ID, player.GetID());
return false;
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->IsPickupStreamedForPlayer(m_ID, player.GetID());
}
// ------------------------------------------------------------------------------------------------
Int32 CPickup::GetModel() const
{
if (Validate())
return _Func->PickupGetModel(m_ID);
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->PickupGetModel(m_ID);
}
// ------------------------------------------------------------------------------------------------
Int32 CPickup::GetWorld() const
{
if (Validate())
return _Func->GetPickupWorld(m_ID);
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->GetPickupWorld(m_ID);
}
// ------------------------------------------------------------------------------------------------
void CPickup::SetWorld(Int32 world) const
{
if (Validate())
_Func->SetPickupWorld(m_ID, world);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetPickupWorld(m_ID, world);
}
// ------------------------------------------------------------------------------------------------
Int32 CPickup::GetAlpha() const
{
if (Validate())
return _Func->GetVehicleModel(m_ID);
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->GetVehicleModel(m_ID);
}
// ------------------------------------------------------------------------------------------------
void CPickup::SetAlpha(Int32 alpha) const
{
if (Validate())
_Func->PickupSetAlpha(m_ID, alpha);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->PickupSetAlpha(m_ID, alpha);
}
// ------------------------------------------------------------------------------------------------
bool CPickup::GetAutomatic() const
{
if (Validate())
return _Func->PickupIsAutomatic(m_ID);
return false;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->PickupIsAutomatic(m_ID);
}
// ------------------------------------------------------------------------------------------------
void CPickup::SetAutomatic(bool toggle) const
{
if (Validate())
_Func->PickupSetAutomatic(m_ID, toggle);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->PickupSetAutomatic(m_ID, toggle);
}
// ------------------------------------------------------------------------------------------------
Int32 CPickup::GetAutoTimer() const
{
if (Validate())
return _Func->GetPickupAutoTimer(m_ID);
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->GetPickupAutoTimer(m_ID);
}
// ------------------------------------------------------------------------------------------------
void CPickup::SetAutoTimer(Int32 timer) const
{
if (Validate())
_Func->SetPickupAutoTimer(m_ID, timer);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetPickupAutoTimer(m_ID, timer);
}
// ------------------------------------------------------------------------------------------------
void CPickup::Refresh() const
{
if (Validate())
_Func->PickupRefresh(m_ID);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->PickupRefresh(m_ID);
}
// ------------------------------------------------------------------------------------------------
const Vector3 & CPickup::GetPosition()
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.Clear();
if (Validate())
_Func->PickupGetPos(m_ID, &s_Vector3.x, &s_Vector3.y, &s_Vector3.z);
// Query the server for the position values
_Func->PickupGetPos(m_ID, &s_Vector3.x, &s_Vector3.y, &s_Vector3.z);
// Return the requested information
return s_Vector3;
}
// ------------------------------------------------------------------------------------------------
void CPickup::SetPosition(const Vector3 & pos) const
{
if (Validate())
_Func->PickupSetPos(m_ID, pos.x, pos.y, pos.z);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->PickupSetPos(m_ID, pos.x, pos.y, pos.z);
}
// ------------------------------------------------------------------------------------------------
void CPickup::SetPositionEx(Float32 x, Float32 y, Float32 z) const
{
if (Validate())
_Func->PickupSetPos(m_ID, x, y, z);
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->PickupSetPos(m_ID, x, y, z);
}
// ------------------------------------------------------------------------------------------------
Int32 CPickup::GetQuantity() const
{
if (Validate())
return _Func->PickupGetQuantity(m_ID);
return -1;
// Validate the managed identifier
Validate();
// Return the requested information
return _Func->PickupGetQuantity(m_ID);
}
// ------------------------------------------------------------------------------------------------
Float32 CPickup::GetPosX() const
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.x = 0;
if (Validate())
_Func->PickupGetPos(m_ID, &s_Vector3.x, NULL, NULL);
// Query the server for the requested component value
_Func->PickupGetPos(m_ID, &s_Vector3.x, NULL, NULL);
// Return the requested information
return s_Vector3.x;
}
// ------------------------------------------------------------------------------------------------
Float32 CPickup::GetPosY() const
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.y = 0;
if (Validate())
_Func->PickupGetPos(m_ID, NULL, &s_Vector3.y, NULL);
// Query the server for the requested component value
_Func->PickupGetPos(m_ID, NULL, &s_Vector3.y, NULL);
// Return the requested information
return s_Vector3.y;
}
// ------------------------------------------------------------------------------------------------
Float32 CPickup::GetPosZ() const
{
// Validate the managed identifier
Validate();
// Clear previous position information, if any
s_Vector3.z = 0;
if (Validate())
_Func->PickupGetPos(m_ID, NULL, NULL, &s_Vector3.z);
// Query the server for the requested component value
_Func->PickupGetPos(m_ID, NULL, NULL, &s_Vector3.z);
// Return the requested information
return s_Vector3.z;
}
// ------------------------------------------------------------------------------------------------
void CPickup::SetPosX(Float32 x) const
{
if (Validate())
{
_Func->PickupGetPos(m_ID, NULL, &s_Vector3.y, &s_Vector3.z);
_Func->PickupSetPos(m_ID, x, s_Vector3.y, s_Vector3.z);
}
}
void CPickup::SetPosY(Float32 y) const
{
if (Validate())
{
_Func->PickupGetPos(m_ID, &s_Vector3.x, NULL, &s_Vector3.z);
_Func->PickupSetPos(m_ID, s_Vector3.x, y, s_Vector3.z);
}
}
void CPickup::SetPosZ(Float32 z) const
{
if (Validate())
{
_Func->PickupGetPos(m_ID, &s_Vector3.x, &s_Vector3.y, NULL);
_Func->PickupSetPos(m_ID, s_Vector3.z, s_Vector3.y, z);
}
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->PickupGetPos(m_ID, NULL, &s_Vector3.y, &s_Vector3.z);
// Perform the requested operation
_Func->PickupSetPos(m_ID, x, s_Vector3.y, s_Vector3.z);
}
// ------------------------------------------------------------------------------------------------
static Object & CreatePickupEx(Int32 model, Int32 world, Int32 quantity,
void CPickup::SetPosY(Float32 y) const
{
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->PickupGetPos(m_ID, &s_Vector3.x, NULL, &s_Vector3.z);
// Perform the requested operation
_Func->PickupSetPos(m_ID, s_Vector3.x, y, s_Vector3.z);
}
// ------------------------------------------------------------------------------------------------
void CPickup::SetPosZ(Float32 z) const
{
// Validate the managed identifier
Validate();
// Retrieve the current values for unchanged components
_Func->PickupGetPos(m_ID, &s_Vector3.x, &s_Vector3.y, NULL);
// Perform the requested operation
_Func->PickupSetPos(m_ID, s_Vector3.z, s_Vector3.y, z);
}
// ------------------------------------------------------------------------------------------------
static Object & Pickup_CreateEx(Int32 model, Int32 world, Int32 quantity,
Float32 x, Float32 y, Float32 z, Int32 alpha, bool automatic)
{
return _Core->NewPickup(model, world, quantity, x, y, z, alpha, automatic,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreatePickupEx(Int32 model, Int32 world, Int32 quantity,
static Object & Pickup_CreateEx(Int32 model, Int32 world, Int32 quantity,
Float32 x, Float32 y, Float32 z, Int32 alpha, bool automatic,
Int32 header, Object & payload)
{
@ -263,14 +336,14 @@ static Object & CreatePickupEx(Int32 model, Int32 world, Int32 quantity,
}
// ------------------------------------------------------------------------------------------------
static Object & CreatePickup(Int32 model, Int32 world, Int32 quantity, const Vector3 & pos,
static Object & Pickup_Create(Int32 model, Int32 world, Int32 quantity, const Vector3 & pos,
Int32 alpha, bool automatic)
{
return _Core->NewPickup(model, world, quantity, pos.x, pos.y, pos.z, alpha, automatic,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreatePickup(Int32 model, Int32 world, Int32 quantity, const Vector3 & pos,
static Object & Pickup_Create(Int32 model, Int32 world, Int32 quantity, const Vector3 & pos,
Int32 alpha, bool automatic, Int32 header, Object & payload)
{
return _Core->NewPickup(model, world, quantity, pos.x, pos.y, pos.z, alpha, automatic,
@ -282,22 +355,24 @@ void Register_CPickup(HSQUIRRELVM vm)
{
RootTable(vm).Bind(_SC("SqPickup"),
Class< CPickup, NoConstructor< CPickup > >(vm, _SC("SqPickup"))
/* Metamethods */
// Metamethods
.Func(_SC("_cmp"), &CPickup::Cmp)
.SquirrelFunc(_SC("_typename"), &CPickup::Typename)
.Func(_SC("_tostring"), &CPickup::ToString)
/* Core Properties */
// Static values
.SetStaticValue(_SC("MaxID"), CPickup::Max)
// Core Properties
.Prop(_SC("ID"), &CPickup::GetID)
.Prop(_SC("Tag"), &CPickup::GetTag, &CPickup::SetTag)
.Prop(_SC("Data"), &CPickup::GetData, &CPickup::SetData)
.Prop(_SC("MaxID"), &CPickup::GetMaxID)
.Prop(_SC("Active"), &CPickup::IsActive)
/* Core Functions */
// Core Functions
.Func(_SC("Bind"), &CPickup::BindEvent)
/* Core Overloads */
// Core Overloads
.Overload< bool (CPickup::*)(void) >(_SC("Destroy"), &CPickup::Destroy)
.Overload< bool (CPickup::*)(Int32) >(_SC("Destroy"), &CPickup::Destroy)
.Overload< bool (CPickup::*)(Int32, Object &) >(_SC("Destroy"), &CPickup::Destroy)
/* Properties */
// Properties
.Prop(_SC("Model"), &CPickup::GetModel)
.Prop(_SC("World"), &CPickup::GetWorld, &CPickup::SetWorld)
.Prop(_SC("Alpha"), &CPickup::GetAlpha, &CPickup::SetAlpha)
@ -311,22 +386,21 @@ void Register_CPickup(HSQUIRRELVM vm)
.Prop(_SC("X"), &CPickup::GetPosX, &CPickup::SetPosX)
.Prop(_SC("Y"), &CPickup::GetPosY, &CPickup::SetPosY)
.Prop(_SC("Z"), &CPickup::GetPosZ, &CPickup::SetPosZ)
/* Functions */
// Functions
.Func(_SC("StreamedFor"), &CPickup::IsStreamedFor)
.Func(_SC("Refresh"), &CPickup::Refresh)
.Func(_SC("SetPos"), &CPickup::SetPositionEx)
.Func(_SC("SetPosition"), &CPickup::SetPositionEx)
// Static Overloads
.StaticOverload< Object & (*)(Int32, Int32, Int32, Float32, Float32, Float32, Int32, bool) >
(_SC("CreateEx"), &Pickup_CreateEx)
.StaticOverload< Object & (*)(Int32, Int32, Int32, Float32, Float32, Float32, Int32, bool, Int32, Object &) >
(_SC("CreateEx"), &Pickup_CreateEx)
.StaticOverload< Object & (*)(Int32, Int32, Int32, const Vector3 &, Int32, bool) >
(_SC("Create"), &Pickup_Create)
.StaticOverload< Object & (*)(Int32, Int32, Int32, const Vector3 &, Int32, bool, Int32, Object &) >
(_SC("Create"), &Pickup_Create)
);
RootTable(vm)
.Overload< Object & (*)(Int32, Int32, Int32, Float32, Float32, Float32, Int32, bool) >
(_SC("CreatePickupEx"), &CreatePickupEx)
.Overload< Object & (*)(Int32, Int32, Int32, Float32, Float32, Float32, Int32, bool, Int32, Object &) >
(_SC("CreatePickupEx"), &CreatePickupEx)
.Overload< Object & (*)(Int32, Int32, Int32, const Vector3 &, Int32, bool) >
(_SC("CreatePickup"), &CreatePickup)
.Overload< Object & (*)(Int32, Int32, Int32, const Vector3 &, Int32, bool, Int32, Object &) >
(_SC("CreatePickup"), &CreatePickup);
}
} // Namespace:: SqMod

View File

@ -8,7 +8,7 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Manages Pickup instances.
* Manages a single pickup entity.
*/
class CPickup
{
@ -20,20 +20,19 @@ private:
// --------------------------------------------------------------------------------------------
static Vector3 s_Vector3;
/* --------------------------------------------------------------------------------------------
* Cached identifiers for fast integer to string conversion.
*/
static SQChar s_StrID[SQMOD_PICKUP_POOL][8];
/* --------------------------------------------------------------------------------------------
* Identifier of the managed entity.
*/
Int32 m_ID;
/* --------------------------------------------------------------------------------------------
* User tag and data associated with this instance.
* User tag associated with this instance.
*/
String m_Tag;
/* --------------------------------------------------------------------------------------------
* User data associated with this instance.
*/
Object m_Data;
/* --------------------------------------------------------------------------------------------
@ -41,20 +40,22 @@ private:
*/
CPickup(Int32 id);
public:
/* --------------------------------------------------------------------------------------------
* Maximum possible number that could represent an identifier for this entity type.
*/
static const Int32 Max;
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
CPickup(const CPickup &);
CPickup(const CPickup &) = delete;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
* Move constructor. (disabled)
*/
CPickup & operator = (const CPickup &);
public:
// --------------------------------------------------------------------------------------------
static const Int32 Max;
CPickup(CPickup &&) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
@ -62,14 +63,22 @@ public:
~CPickup();
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity.
* Copy assignment operator. (disabled)
*/
bool Validate() const
CPickup & operator = (const CPickup &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
CPickup & operator = (CPickup &&) = delete;
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity instance otherwise throw an exception.
*/
void Validate() const
{
if (VALID_ENTITY(m_ID))
return true;
SqThrow("Invalid pickup reference [%s]", m_Tag.c_str());
return false;
if (INVALID_ENTITY(m_ID))
SqThrowF("Invalid pickup reference [%s]", m_Tag.c_str());
}
/* --------------------------------------------------------------------------------------------
@ -80,27 +89,33 @@ public:
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
const String & ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Retrieve the identifier of the entity managed by this instance.
*/
Int32 GetID() const { return m_ID; }
/* --------------------------------------------------------------------------------------------
* Retrieve the maximum possible identifier to an entity of this type.
*/
Int32 GetMaxID() const { return SQMOD_PICKUP_POOL; }
Int32 GetID() const
{
return m_ID;
}
/* --------------------------------------------------------------------------------------------
* Check whether this instance manages a valid entity.
*/
bool IsActive() const { return VALID_ENTITY(m_ID); }
bool IsActive() const
{
return VALID_ENTITY(m_ID);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the associated user tag.
*/
CSStr GetTag() const;
const String & GetTag() const;
/* --------------------------------------------------------------------------------------------
* Modify the associated user tag.
@ -117,37 +132,135 @@ public:
*/
void SetData(Object & data);
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Destroy the managed pickup entity.
*/
bool Destroy()
{
return Destroy(0, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed pickup entity.
*/
bool Destroy(Int32 header)
{
return Destroy(header, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed pickup entity.
*/
bool Destroy(Int32 header, Object & payload);
bool Destroy() { return Destroy(0, NullObject()); }
bool Destroy(Int32 header) { return Destroy(header, NullObject()); }
// --------------------------------------------------------------------------------------------
bool BindEvent(Int32 evid, Object & env, Function & func) const;
/* --------------------------------------------------------------------------------------------
* Bind to an event supported by this entity type.
*/
void BindEvent(Int32 evid, Object & env, Function & func) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* See if the managed pickup entity is streamed for the specified player.
*/
bool IsStreamedFor(CPlayer & player) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the model of the managed pickup entity.
*/
Int32 GetModel() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the world in which the managed pickup entity exists.
*/
Int32 GetWorld() const;
/* --------------------------------------------------------------------------------------------
* Mpdify the world in which the managed pickup entity exists.
*/
void SetWorld(Int32 world) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the alpha of the managed pickup entity.
*/
Int32 GetAlpha() const;
/* --------------------------------------------------------------------------------------------
* Mpdify the alpha of the managed pickup entity.
*/
void SetAlpha(Int32 alpha) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed pickup entity is automatic.
*/
bool GetAutomatic() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed pickup entity is automatic.
*/
void SetAutomatic(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the automatic timer of the managed pickup entity.
*/
Int32 GetAutoTimer() const;
/* --------------------------------------------------------------------------------------------
* Mpdify the automatic timer of the managed pickup entity.
*/
void SetAutoTimer(Int32 timer) const;
/* --------------------------------------------------------------------------------------------
* Refresh the managed pickup entity.
*/
void Refresh() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position of the managed pickup entity.
*/
const Vector3 & GetPosition();
/* --------------------------------------------------------------------------------------------
* Mpdify the position of the managed pickup entity.
*/
void SetPosition(const Vector3 & pos) const;
/* --------------------------------------------------------------------------------------------
* Mpdify the position of the managed pickup entity.
*/
void SetPositionEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the quantity of the managed pickup entity.
*/
Int32 GetQuantity() const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the x axis of the managed pickup entity.
*/
Float32 GetPosX() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the y axis of the managed pickup entity.
*/
Float32 GetPosY() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the z axis of the managed pickup entity.
*/
Float32 GetPosZ() const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the x axis of the managed pickup entity.
*/
void SetPosX(Float32 x) const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the y axis of the managed pickup entity.
*/
void SetPosY(Float32 y) const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the z axis of the managed pickup entity.
*/
void SetPosZ(Float32 z) const;
};

File diff suppressed because it is too large Load Diff

View File

@ -8,7 +8,7 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Manages Player instances.
* Manages a single player entity.
*/
class CPlayer
{
@ -24,20 +24,19 @@ private:
// --------------------------------------------------------------------------------------------
static SQChar s_Buffer[SQMOD_PLAYER_TMP_BUFFER];
/* --------------------------------------------------------------------------------------------
* Cached identifiers for fast integer to string conversion.
*/
static SQChar s_StrID[SQMOD_PLAYER_POOL][8];
/* --------------------------------------------------------------------------------------------
* Identifier of the managed entity.
*/
Int32 m_ID;
/* --------------------------------------------------------------------------------------------
* User tag and data associated with this instance.
* User tag associated with this instance.
*/
String m_Tag;
/* --------------------------------------------------------------------------------------------
* User data associated with this instance.
*/
Object m_Data;
/* --------------------------------------------------------------------------------------------
@ -45,35 +44,45 @@ private:
*/
CPlayer(Int32 id);
public:
/* --------------------------------------------------------------------------------------------
* Maximum possible number that could represent an identifier for this entity type.
*/
static const Int32 Max;
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
CPlayer(const CPlayer &);
CPlayer(const CPlayer &) = delete;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
* Move constructor. (disabled)
*/
CPlayer & operator = (const CPlayer &);
public:
// --------------------------------------------------------------------------------------------
static const Int32 Max;
CPlayer(CPlayer &&) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~CPlayer();
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
*/
CPlayer & operator = (const CPlayer &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
CPlayer & operator = (CPlayer &&) = delete;
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity.
*/
bool Validate() const
void Validate() const
{
if (VALID_ENTITY(m_ID))
return true;
SqThrow("Invalid player reference [%s]", m_Tag.c_str());
return false;
if (INVALID_ENTITY(m_ID))
SqThrowF("Invalid player reference [%s]", m_Tag.c_str());
}
/* --------------------------------------------------------------------------------------------
@ -84,27 +93,33 @@ public:
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
const String & ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Retrieve the identifier of the entity managed by this instance.
*/
Int32 GetID() const { return m_ID; }
/* --------------------------------------------------------------------------------------------
* Retrieve the maximum possible identifier to an entity of this type.
*/
Int32 GetMaxID() const { return SQMOD_PLAYER_POOL; }
Int32 GetID() const
{
return m_ID;
}
/* --------------------------------------------------------------------------------------------
* Check whether this instance manages a valid entity.
*/
bool IsActive() const { return VALID_ENTITY(m_ID); }
bool IsActive() const
{
return VALID_ENTITY(m_ID);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the associated user tag.
*/
CSStr GetTag() const;
const String & GetTag() const;
/* --------------------------------------------------------------------------------------------
* Modify the associated user tag.
@ -121,141 +136,639 @@ public:
*/
void SetData(Object & data);
// --------------------------------------------------------------------------------------------
bool BindEvent(Int32 evid, Object & env, Function & func) const;
/* --------------------------------------------------------------------------------------------
* Bind to an event supported by this entity type.
*/
void BindEvent(Int32 evid, Object & env, Function & func) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* See if the managed player entity is streamed for the specified player.
*/
bool IsStreamedFor(CPlayer & player) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the class of the managed player entity.
*/
Int32 GetClass() const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity has administrator privileges.
*/
bool GetAdmin() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed player entity has administrator privileges.
*/
void SetAdmin(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the ip address of the managed player entity.
*/
CSStr GetIP() const;
/* --------------------------------------------------------------------------------------------
* Kick the managed player entity from the server.
*/
void Kick() const;
/* --------------------------------------------------------------------------------------------
* Ban the managed player entity from the server.
*/
void Ban() const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity is connected.
*/
bool IsConnected() const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity is spawned.
*/
bool IsSpawned() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the key of the managed player entity.
*/
Uint32 GetKey() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the world in which the managed player entity exists.
*/
Int32 GetWorld() const;
/* --------------------------------------------------------------------------------------------
* Modify the world in which the managed player entity exists.
*/
void SetWorld(Int32 world) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the secondary world of the managed player entity.
*/
Int32 GetSecWorld() const;
/* --------------------------------------------------------------------------------------------
* Modify the secondary world of the managed player entity.
*/
void SetSecWorld(Int32 world) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the unique world of the managed player entity.
*/
Int32 GetUniqueWorld() const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity is compatible with the specified world.
*/
bool IsWorldCompatible(Int32 world) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the nick name of the managed player entity.
*/
CSStr GetName() const;
/* --------------------------------------------------------------------------------------------
* Modify the nick name of the managed player entity.
*/
void SetName(CSStr name) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the team of the managed player entity.
*/
Int32 GetTeam() const;
/* --------------------------------------------------------------------------------------------
* Modify the team of the managed player entity.
*/
void SetTeam(Int32 team) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the skin identifier of the managed player entity.
*/
Int32 GetSkin() const;
/* --------------------------------------------------------------------------------------------
* Modify the skin identifier of the managed player entity.
*/
void SetSkin(Int32 skin) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the color of the managed player entity.
*/
const Color3 & GetColor() const;
/* --------------------------------------------------------------------------------------------
* Modify the color of the managed player entity.
*/
void SetColor(const Color3 & color) const;
/* --------------------------------------------------------------------------------------------
* Modify the color of the managed player entity.
*/
void SetColorEx(Uint8 r, Uint8 g, Uint8 b) const;
/* --------------------------------------------------------------------------------------------
* Force the managed player entity to spawn in the game.
*/
void ForceSpawn() const;
/* --------------------------------------------------------------------------------------------
* Force the managed player entity to select a class.
*/
void ForceSelect() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the money amount of the managed player entity.
*/
Int32 GetMoney() const;
/* --------------------------------------------------------------------------------------------
* Modify the money amount of the managed player entity.
*/
void SetMoney(Int32 amount) const;
/* --------------------------------------------------------------------------------------------
* Give a certain amount of money to the managed player entity.
*/
void GiveMoney(Int32 amount) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the score of the managed player entity.
*/
Int32 GetScore() const;
/* --------------------------------------------------------------------------------------------
* Modify the score of the managed player entity.
*/
void SetScore(Int32 score) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the connection latency of the managed player entity.
*/
Int32 GetPing() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the frames per second of the managed player entity.
*/
Float32 GetFPS() const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity is typing.
*/
bool IsTyping() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the unique user identifier of the managed player entity.
*/
CSStr GetUID() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the unique user identifier version 2 of the managed player entity.
*/
CSStr GetUID2() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the current health of the managed player entity.
*/
Float32 GetHealth() const;
/* --------------------------------------------------------------------------------------------
* Modify the health of the managed player entity.
*/
void SetHealth(Float32 amount) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the current health of the managed player entity.
*/
Float32 GetArmor() const;
/* --------------------------------------------------------------------------------------------
* Modify the health of the managed player entity.
*/
void SetArmor(Float32 amount) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the immunity flags of the managed player entity.
*/
Int32 GetImmunity() const;
/* --------------------------------------------------------------------------------------------
* Modify the immunity flags of the managed player entity.
*/
void SetImmunity(Int32 flags) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position of the managed player entity.
*/
const Vector3 & GetPosition() const;
/* --------------------------------------------------------------------------------------------
* Modify the position of the managed player entity.
*/
void SetPosition(const Vector3 & pos) const;
/* --------------------------------------------------------------------------------------------
* Modify the position of the managed player entity.
*/
void SetPositionEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the speed of the managed player entity.
*/
const Vector3 & GetSpeed() const;
/* --------------------------------------------------------------------------------------------
* Modify the speed of the managed player entity.
*/
void SetSpeed(const Vector3 & vel) const;
/* --------------------------------------------------------------------------------------------
* Modify the speed of the managed player entity.
*/
void SetSpeedEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Modify the speed of the managed player entity.
*/
void AddSpeed(const Vector3 & vel) const;
/* --------------------------------------------------------------------------------------------
* Modify the speed of the managed player entity.
*/
void AddSpeedEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the heading angle of the managed player entity.
*/
Float32 GetHeading() const;
/* --------------------------------------------------------------------------------------------
* Modify the heading angle of the managed player entity.
*/
void SetHeading(Float32 angle) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the alpha of the managed player entity.
*/
Int32 GetAlpha() const;
/* --------------------------------------------------------------------------------------------
* Modify the alpha of the managed player entity.
*/
void SetAlpha(Int32 alpha, Int32 fade) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the vehicle status of the managed player entity.
*/
Int32 GetVehicleStatus() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the occupied vehicle slot by the managed player entity.
*/
Int32 GetOccupiedSlot() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the vehicle in which the managed player entity is embarked.
*/
Object & GetVehicle() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the vehicle identifier in which the managed player entity is embarked.
*/
Int32 GetVehicleID() const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity can be controlled.
*/
bool GetControllable() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed player entity can be controlled.
*/
void SetControllable(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity can driveby.
*/
bool GetDriveby() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed player entity can driveby.
*/
void SetDriveby(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity has white scanlines.
*/
bool GetWhiteScanlines() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed player entity has white scanlines.
*/
void SetWhiteScanlines(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity has green scanlines.
*/
bool GetGreenScanlines() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed player entity has green scanlines.
*/
void SetGreenScanlines(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity has widescreen.
*/
bool GetWidescreen() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed player entity has widescreen.
*/
void SetWidescreen(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity displays markers.
*/
bool GetShowMarkers() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed player entity displays markers.
*/
void SetShowMarkers(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity has attacking privileges.
*/
bool GetAttackPriv() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed player entity has attacking privileges.
*/
void SetAttackPriv(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity has markers.
*/
bool GetHasMarker() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed player entity has markers.
*/
void SetHasMarker(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity has chat tags.
*/
bool GetChatTags() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed player entity has chat tags.
*/
void SetChatTags(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity is under drunk effects.
*/
bool GetDrunkEffects() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed player entity is under drunk effects.
*/
void SetDrunkEffects(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the weapon identifier of the managed player entity.
*/
Int32 GetWeapon() const;
/* --------------------------------------------------------------------------------------------
* Modify the weapon of the managed player entity.
*/
void SetWeapon(Int32 wep, Int32 ammo) const;
/* --------------------------------------------------------------------------------------------
* Give a weapon of the managed player entity.
*/
void GiveWeapon(Int32 wep, Int32 ammo) const;
/* --------------------------------------------------------------------------------------------
* Strip the managed player entity of all weapons.
*/
void StripWeapons() const;
/* --------------------------------------------------------------------------------------------
* Modify the camera position of the managed player entity.
*/
void SetCameraPosition(const Vector3 & pos, const Vector3 & aim) const;
/* --------------------------------------------------------------------------------------------
* Modify the camera position of the managed player entity.
*/
void SetCameraPosition(Float32 xp, Float32 yp, Float32 zp, Float32 xa, Float32 ya, Float32 za) const;
/* --------------------------------------------------------------------------------------------
* Restore the camera position of the managed player entity.
*/
void RestoreCamera() const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity has camera locked.
*/
bool IsCameraLocked() const;
/* --------------------------------------------------------------------------------------------
* Modify the animation of the managed player entity.
*/
void SetAnimation(Int32 group, Int32 anim) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the wanted level of the managed player entity.
*/
Int32 GetWantedLevel() const;
/* --------------------------------------------------------------------------------------------
* Modify the wanted level of the managed player entity.
*/
void SetWantedLevel(Int32 level) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the vehicle that the managed player entity is standing on.
*/
Object & StandingOnVehicle() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the object that the managed player entity is standing on.
*/
Object & StandingOnObject() const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity is away.
*/
bool IsAway() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the player that the managed player entity is spectating.
*/
Object & GetSpectator() const;
/* --------------------------------------------------------------------------------------------
* Set the managed player entity to spectate the specified player entity.
*/
void SetSpectator(CPlayer & target) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity is burning.
*/
bool IsBurning() const;
/* --------------------------------------------------------------------------------------------
* See whether the managed player entity is crouched.
*/
bool IsCrouched() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the current state of the managed player entity.
*/
Int32 GetState() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the current action of the managed player entity.
*/
Int32 GetAction() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the game keys of the managed player entity.
*/
Int32 GetGameKeys() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the aim position of the managed player entity.
*/
const Vector3 & GetAimPos() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the aim direction of the managed player entity.
*/
const Vector3 & GetAimDir() const;
/* --------------------------------------------------------------------------------------------
* Embark the managed player entity into the specified vehicle entity.
*/
void Embark(CVehicle & vehicle) const;
/* --------------------------------------------------------------------------------------------
* Embark the managed player entity into the specified vehicle entity.
*/
void Embark(CVehicle & vehicle, Int32 slot, bool allocate, bool warp) const;
/* --------------------------------------------------------------------------------------------
* Disembark the managed player entity from the currently embarked vehicle entity.
*/
void Disembark() const;
/* --------------------------------------------------------------------------------------------
* Redirect the managed player entity to the specified server.
*/
bool Redirect(CSStr ip, Uint32 port, CSStr nick, CSStr pass, CSStr user);
/* --------------------------------------------------------------------------------------------
* Retrieve the authority level of the managed player entity.
*/
Int32 GetAuthority() const;
/* --------------------------------------------------------------------------------------------
* Modify the authority level of the managed player entity.
*/
void SetAuthority(Int32 level) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the message prefix at the specified index for the managed player entity.
*/
CSStr GetMessagePrefix(Uint32 index) const;
/* --------------------------------------------------------------------------------------------
* Modify the message prefix at the specified index for the managed player entity.
*/
void SetMessagePrefix(Uint32 index, CSStr prefix) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the message color for the managed player entity.
*/
Uint32 GetMessageColor() const;
/* --------------------------------------------------------------------------------------------
* Modify the message color for the managed player entity.
*/
void SetMessageColor(Uint32 color) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the announcement style for the managed player entity.
*/
Int32 GetAnnounceStyle() const;
/* --------------------------------------------------------------------------------------------
* Modify the announcement style for the managed player entity.
*/
void SetAnnounceStyle(Int32 style) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the x axis of the managed player entity.
*/
Float32 GetPosX() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the y axis of the managed player entity.
*/
Float32 GetPosY() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the z axis of the managed player entity.
*/
Float32 GetPosZ() const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the x axis of the managed player entity.
*/
void SetPosX(Float32 x) const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the y axis of the managed player entity.
*/
void SetPosY(Float32 y) const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the z axis of the managed player entity.
*/
void SetPosZ(Float32 z) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Send a formatted colored message to the managed player entity.
*/
static SQInteger Msg(HSQUIRRELVM vm);
static SQInteger MsgP(HSQUIRRELVM vm);
static SQInteger MsgEx(HSQUIRRELVM vm);
static SQInteger Message(HSQUIRRELVM vm);
static SQInteger Announce(HSQUIRRELVM vm);
static SQInteger AnnounceEx(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Send a formatted message with a prefix to the managed player entity.
*/
static SQInteger MsgP(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Send a formatted colored message to the managed player entity.
*/
static SQInteger MsgEx(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Send a formatted message to the managed player entity.
*/
static SQInteger Message(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Send a formatted announcement message to the managed player entity.
*/
static SQInteger Announce(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Send a formatted announcement message to the managed player entity.
*/
static SQInteger AnnounceEx(HSQUIRRELVM vm);
};
} // Namespace:: SqMod

View File

@ -8,15 +8,20 @@
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQChar CSprite::s_StrID[SQMOD_SPRITE_POOL][8];
const Int32 CSprite::Max = SQMOD_SPRITE_POOL;
// ------------------------------------------------------------------------------------------------
const Int32 CSprite::Max = SQMOD_SPRITE_POOL;
SQInteger CSprite::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("SqSprite");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
CSprite::CSprite(Int32 id)
: m_ID(VALID_ENTITYGETEX(id, SQMOD_SPRITE_POOL))
, m_Tag(VALID_ENTITY(m_ID) ? s_StrID[m_ID] : _SC("-1"))
, m_Tag(ToStrF("%d", id))
{
/* ... */
}
@ -38,301 +43,407 @@ Int32 CSprite::Cmp(const CSprite & o) const
return -1;
}
CSStr CSprite::ToString() const
// ------------------------------------------------------------------------------------------------
const String & CSprite::ToString() const
{
return VALID_ENTITYEX(m_ID, SQMOD_SPRITE_POOL) ? s_StrID[m_ID] : _SC("-1");
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
CSStr CSprite::GetTag() const
const String & CSprite::GetTag() const
{
return m_Tag.c_str();
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetTag(CSStr tag)
{
m_Tag.assign(tag);
}
// ------------------------------------------------------------------------------------------------
Object & CSprite::GetData()
{
if (Validate())
return m_Data;
return NullObject();
// Validate the managed identifier
Validate();
// Return the requested information
return m_Data;
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetData(Object & data)
{
if (Validate())
m_Data = data;
// Validate the managed identifier
Validate();
// Apply the specified value
m_Data = data;
}
// ------------------------------------------------------------------------------------------------
bool CSprite::Destroy(Int32 header, Object & payload)
{
// Validate the managed identifier
Validate();
// Perform the requested operation
return _Core->DelSprite(m_ID, header, payload);
}
// ------------------------------------------------------------------------------------------------
bool CSprite::BindEvent(Int32 evid, Object & env, Function & func) const
void CSprite::BindEvent(Int32 evid, Object & env, Function & func) const
{
if (!Validate())
return false;
// Validate the managed identifier
Validate();
// Obtain the function instance called for this event
Function & event = _Core->GetSpriteEvent(m_ID, evid);
// Is the specified callback function null?
if (func.IsNull())
event.Release();
event.Release(); // Then release the current callback
// Assign the specified environment and function
else
event = Function(env.GetVM(), env, func.GetFunc());
return true;
}
// ------------------------------------------------------------------------------------------------
void CSprite::ShowAll() const
{
if (Validate())
_Func->ShowSprite(m_ID, -1);
}
void CSprite::ShowFor(CPlayer & player) const
{
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
_Func->ShowSprite(m_ID, player.GetID());
}
void CSprite::ShowRange(Int32 first, Int32 last) const
{
if (first > last)
SqThrow("Invalid player range: %d > %d", first, last);
else if (Validate())
for (; first <= last; ++first)
{
if (_Func->IsPlayerConnected(first))
_Func->ShowSprite(m_ID, first);
}
}
void CSprite::HideAll() const
{
if (Validate())
_Func->HideSprite(m_ID, -1);
}
void CSprite::HideFor(CPlayer & player) const
{
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
_Func->HideSprite(m_ID, player.GetID());
}
void CSprite::HideRange(Int32 first, Int32 last) const
{
if (first > last)
SqThrow("Invalid player range: %d > %d", first, last);
else if (Validate())
for (; first <= last; ++first)
{
if (_Func->IsPlayerConnected(first))
_Func->HideSprite(m_ID, first);
}
}
void CSprite::SetPositionAll(const Vector2i & pos) const
{
if (Validate())
_Func->MoveSprite(m_ID, -1, pos.x, pos.y);
}
void CSprite::SetPositionAllEx(Int32 x, Int32 y) const
{
if (Validate())
_Func->MoveSprite(m_ID, -1, x, y);
}
void CSprite::SetPositionFor(CPlayer & player, const Vector2i & pos) const
{
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
_Func->MoveSprite(m_ID, player.GetID(), pos.x, pos.y);
}
void CSprite::SetPositionForEx(CPlayer & player, Int32 x, Int32 y) const
{
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
_Func->MoveSprite(m_ID, player.GetID(), x, y);
}
void CSprite::SetPositionRange(Int32 first, Int32 last, const Vector2i & pos) const
{
if (first > last)
SqThrow("Invalid player range: %d > %d", first, last);
else if (Validate())
for (; first <= last; ++first)
{
if (_Func->IsPlayerConnected(first))
_Func->MoveSprite(m_ID, first, pos.x, pos.y);
}
}
void CSprite::SetCenterAll(const Vector2i & pos) const
{
if (Validate())
_Func->SetSpriteCenter(m_ID, -1, pos.x, pos.y);
}
void CSprite::SetCenterAllEx(Int32 x, Int32 y) const
{
if (Validate())
_Func->SetSpriteCenter(m_ID, -1, x, y);
}
void CSprite::SetCenterFor(CPlayer & player, const Vector2i & pos) const
{
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
_Func->SetSpriteCenter(m_ID, player.GetID(), pos.x, pos.y);
}
void CSprite::SetCenterForEx(CPlayer & player, Int32 x, Int32 y) const
{
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
_Func->SetSpriteCenter(m_ID, player.GetID(), x, y);
}
void CSprite::SetCenterRange(Int32 first, Int32 last, const Vector2i & pos) const
{
if (first > last)
SqThrow("Invalid player range: %d > %d", first, last);
else if (Validate())
for (; first <= last; ++first)
{
if (_Func->IsPlayerConnected(first))
_Func->SetSpriteCenter(m_ID, first, pos.x, pos.y);
}
}
void CSprite::SetRotationAll(Float32 rot) const
{
if (Validate())
_Func->RotateSprite(m_ID, -1, rot);
}
void CSprite::SetRotationFor(CPlayer & player, Float32 rot) const
{
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
_Func->RotateSprite(m_ID, player.GetID(), rot);
}
void CSprite::SetRotationRange(Int32 first, Int32 last, Float32 rot) const
{
if (first > last)
SqThrow("Invalid player range: %d > %d", first, last);
else if (Validate())
for (; first <= last; ++first)
{
if (_Func->IsPlayerConnected(first))
_Func->RotateSprite(m_ID, first, rot);
}
}
void CSprite::SetAlphaAll(Uint8 alpha) const
{
if (Validate())
_Func->SetSpriteAlpha(m_ID, -1, alpha);
}
void CSprite::SetAlphaFor(CPlayer & player, Uint8 alpha) const
{
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
_Func->SetSpriteAlpha(m_ID, player.GetID(), alpha);
}
void CSprite::SetAlphaRange(Int32 first, Int32 last, Uint8 alpha) const
{
if (first > last)
SqThrow("Invalid player range: %d > %d", first, last);
else if (Validate())
for (; first <= last; ++first)
{
if (_Func->IsPlayerConnected(first))
_Func->SetSpriteAlpha(m_ID, first, alpha);
}
}
CSStr CSprite::GetFilePath() const
{
if (Validate())
_Core->GetSprite(m_ID).mPath.c_str();
return g_EmptyStr;
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->ShowSprite(m_ID, -1);
}
// ------------------------------------------------------------------------------------------------
static Object & CreateSpriteEx(CSStr file, Int32 xp, Int32 yp, Int32 xr, Int32 yr,
void CSprite::ShowFor(CPlayer & player) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->ShowSprite(m_ID, player.GetID());
}
// ------------------------------------------------------------------------------------------------
void CSprite::ShowRange(Int32 first, Int32 last) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then show this textdraw on his client
_Func->ShowSprite(m_ID, first);
}
}
// ------------------------------------------------------------------------------------------------
void CSprite::HideAll() const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->HideSprite(m_ID, -1);
}
// ------------------------------------------------------------------------------------------------
void CSprite::HideFor(CPlayer & player) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->HideSprite(m_ID, player.GetID());
}
// ------------------------------------------------------------------------------------------------
void CSprite::HideRange(Int32 first, Int32 last) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then hide this textdraw on his client
_Func->HideSprite(m_ID, first);
}
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetPositionAll(const Vector2i & pos) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveSprite(m_ID, -1, pos.x, pos.y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetPositionAllEx(Int32 x, Int32 y) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveSprite(m_ID, -1, x, y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetPositionFor(CPlayer & player, const Vector2i & pos) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveSprite(m_ID, player.GetID(), pos.x, pos.y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetPositionForEx(CPlayer & player, Int32 x, Int32 y) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveSprite(m_ID, player.GetID(), x, y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetPositionRange(Int32 first, Int32 last, const Vector2i & pos) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then move this textdraw on his client
_Func->MoveSprite(m_ID, first, pos.x, pos.y);
}
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetCenterAll(const Vector2i & pos) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSpriteCenter(m_ID, -1, pos.x, pos.y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetCenterAllEx(Int32 x, Int32 y) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSpriteCenter(m_ID, -1, x, y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetCenterFor(CPlayer & player, const Vector2i & pos) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSpriteCenter(m_ID, player.GetID(), pos.x, pos.y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetCenterForEx(CPlayer & player, Int32 x, Int32 y) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSpriteCenter(m_ID, player.GetID(), x, y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetCenterRange(Int32 first, Int32 last, const Vector2i & pos) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then center this textdraw on his client
_Func->SetSpriteCenter(m_ID, first, pos.x, pos.y);
}
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetRotationAll(Float32 rot) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->RotateSprite(m_ID, -1, rot);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetRotationFor(CPlayer & player, Float32 rot) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->RotateSprite(m_ID, player.GetID(), rot);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetRotationRange(Int32 first, Int32 last, Float32 rot) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then rotate this textdraw on his client
_Func->RotateSprite(m_ID, first, rot);
}
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetAlphaAll(Uint8 alpha) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSpriteAlpha(m_ID, -1, alpha);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetAlphaFor(CPlayer & player, Uint8 alpha) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSpriteAlpha(m_ID, player.GetID(), alpha);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetAlphaRange(Int32 first, Int32 last, Uint8 alpha) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then colorize this textdraw on his client
_Func->SetSpriteAlpha(m_ID, first, alpha);
}
}
// ------------------------------------------------------------------------------------------------
const String & CSprite::GetFilePath() const
{
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetSprite(m_ID).mPath;
}
// ------------------------------------------------------------------------------------------------
static Object & Sprite_CreateEx(CSStr file, Int32 xp, Int32 yp, Int32 xr, Int32 yr,
Float32 angle, Int32 alpha, bool rel)
{
return _Core->NewSprite(-1, file, xp, yp, xr, yr, angle, alpha, rel,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateSpriteEx(CSStr file, Int32 xp, Int32 yp, Int32 xr, Int32 yr,
// ------------------------------------------------------------------------------------------------
static Object & Sprite_CreateEx(CSStr file, Int32 xp, Int32 yp, Int32 xr, Int32 yr,
Float32 angle, Int32 alpha, bool rel, Int32 header, Object & payload)
{
return _Core->NewSprite(-1, file, xp, yp, xr, yr, angle, alpha, rel, header, payload);
}
// ------------------------------------------------------------------------------------------------
static Object & CreateSpriteEx(Int32 index, CSStr file, Int32 xp, Int32 yp, Int32 xr, Int32 yr,
static Object & Sprite_CreateEx(Int32 index, CSStr file, Int32 xp, Int32 yp, Int32 xr, Int32 yr,
Float32 angle, Int32 alpha, bool rel)
{
return _Core->NewSprite(index, file, xp, yp, xr, yr, angle, alpha, rel,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateSpriteEx(Int32 index, CSStr file, Int32 xp, Int32 yp, Int32 xr, Int32 yr,
// ------------------------------------------------------------------------------------------------
static Object & Sprite_CreateEx(Int32 index, CSStr file, Int32 xp, Int32 yp, Int32 xr, Int32 yr,
Float32 angle, Int32 alpha, bool rel, Int32 header, Object & payload)
{
return _Core->NewSprite(index, file, xp, yp, xr, yr, angle, alpha, rel, header, payload);
}
// ------------------------------------------------------------------------------------------------
static Object & CreateSprite(CSStr file, const Vector2i & pos, const Vector2i & rot,
static Object & Sprite_Create(CSStr file, const Vector2i & pos, const Vector2i & rot,
Float32 angle, Int32 alpha, bool rel)
{
return _Core->NewSprite(-1, file, pos.x, pos.y, rot.x, rot.y, angle, alpha, rel,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateSprite(CSStr file, const Vector2i & pos, const Vector2i & rot,
// ------------------------------------------------------------------------------------------------
static Object & Sprite_Create(CSStr file, const Vector2i & pos, const Vector2i & rot,
Float32 angle, Int32 alpha, bool rel, Int32 header, Object & payload)
{
return _Core->NewSprite(-1, file, pos.x, pos.y, rot.x, rot.y, angle, alpha, rel, header, payload);
}
// ------------------------------------------------------------------------------------------------
static Object & CreateSprite(Int32 index, CSStr file, const Vector2i & pos, const Vector2i & rot,
static Object & Sprite_Create(Int32 index, CSStr file, const Vector2i & pos, const Vector2i & rot,
Float32 angle, Int32 alpha, bool rel)
{
return _Core->NewSprite(index, file, pos.x, pos.y, rot.x, rot.y, angle, alpha, rel,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateSprite(Int32 index, CSStr file, const Vector2i & pos, const Vector2i & rot,
// ------------------------------------------------------------------------------------------------
static Object & Sprite_Create(Int32 index, CSStr file, const Vector2i & pos, const Vector2i & rot,
Float32 angle, Int32 alpha, bool rel, Int32 header, Object & payload)
{
return _Core->NewSprite(index, file, pos.x, pos.y, rot.x, rot.y, angle, alpha, rel, header, payload);
@ -343,24 +454,26 @@ void Register_CSprite(HSQUIRRELVM vm)
{
RootTable(vm).Bind(_SC("SqSprite"),
Class< CSprite, NoConstructor< CSprite > >(vm, _SC("SqSprite"))
/* Metamethods */
// Metamethods
.Func(_SC("_cmp"), &CSprite::Cmp)
.SquirrelFunc(_SC("_typename"), &CSprite::Typename)
.Func(_SC("_tostring"), &CSprite::ToString)
/* Core Properties */
// Static values
.SetStaticValue(_SC("MaxID"), CSprite::Max)
// Core Properties
.Prop(_SC("ID"), &CSprite::GetID)
.Prop(_SC("Tag"), &CSprite::GetTag, &CSprite::SetTag)
.Prop(_SC("Data"), &CSprite::GetData, &CSprite::SetData)
.Prop(_SC("MaxID"), &CSprite::GetMaxID)
.Prop(_SC("Active"), &CSprite::IsActive)
/* Core Functions */
// Core Functions
.Func(_SC("Bind"), &CSprite::BindEvent)
/* Core Overloads */
// Core Overloads
.Overload< bool (CSprite::*)(void) >(_SC("Destroy"), &CSprite::Destroy)
.Overload< bool (CSprite::*)(Int32) >(_SC("Destroy"), &CSprite::Destroy)
.Overload< bool (CSprite::*)(Int32, Object &) >(_SC("Destroy"), &CSprite::Destroy)
/* Properties */
// Properties
.Prop(_SC("Path"), &CSprite::GetFilePath)
/* Functions */
// Functions
.Func(_SC("ShowAll"), &CSprite::ShowAll)
.Func(_SC("ShowTo"), &CSprite::ShowFor)
.Func(_SC("ShowFor"), &CSprite::ShowFor)
@ -377,7 +490,7 @@ void Register_CSprite(HSQUIRRELVM vm)
.Func(_SC("SetAlphaAll"), &CSprite::SetAlphaAll)
.Func(_SC("SetAlphaFor"), &CSprite::SetAlphaFor)
.Func(_SC("SetAlphaRange"), &CSprite::SetAlphaRange)
/* Overloads */
// Overloads
.Overload< void (CSprite::*)(const Vector2i &) const >
(_SC("SetPositionAll"), &CSprite::SetPositionAll)
.Overload< void (CSprite::*)(Int32, Int32) const >
@ -394,25 +507,24 @@ void Register_CSprite(HSQUIRRELVM vm)
(_SC("SetCenterFor"), &CSprite::SetCenterFor)
.Overload< void (CSprite::*)(CPlayer &, Int32, Int32) const >
(_SC("SetCenterFor"), &CSprite::SetCenterForEx)
// Static Overloads
.StaticOverload< Object & (*)(CSStr, Int32, Int32, Int32, Int32, Float32, Int32, bool rel) >
(_SC("CreateEx"), &Sprite_CreateEx)
.StaticOverload< Object & (*)(CSStr, Int32, Int32, Int32, Int32, Float32, Int32, bool rel, Int32, Object &) >
(_SC("CreateEx"), &Sprite_CreateEx)
.StaticOverload< Object & (*)(Int32, CSStr, Int32, Int32, Int32, Int32, Float32, Int32, bool rel) >
(_SC("CreateEx"), &Sprite_CreateEx)
.StaticOverload< Object & (*)(Int32, CSStr, Int32, Int32, Int32, Int32, Float32, Int32, bool rel, Int32, Object &) >
(_SC("CreateEx"), &Sprite_CreateEx)
.StaticOverload< Object & (*)(CSStr, const Vector2i &, const Vector2i &, Float32, Int32, bool) >
(_SC("Create"), &Sprite_Create)
.StaticOverload< Object & (*)(CSStr, const Vector2i &, const Vector2i &, Float32, Int32, bool, Int32, Object &) >
(_SC("Create"), &Sprite_Create)
.StaticOverload< Object & (*)(Int32, CSStr, const Vector2i &, const Vector2i &, Float32, Int32, bool) >
(_SC("Create"), &Sprite_Create)
.StaticOverload< Object & (*)(Int32, CSStr, const Vector2i &, const Vector2i &, Float32, Int32, bool, Int32, Object &) >
(_SC("Create"), &Sprite_Create)
);
RootTable(vm)
.Overload< Object & (*)(CSStr, Int32, Int32, Int32, Int32, Float32, Int32, bool rel) >
(_SC("CreateSpriteEx"), &CreateSpriteEx)
.Overload< Object & (*)(CSStr, Int32, Int32, Int32, Int32, Float32, Int32, bool rel, Int32, Object &) >
(_SC("CreateSpriteEx"), &CreateSpriteEx)
.Overload< Object & (*)(Int32, CSStr, Int32, Int32, Int32, Int32, Float32, Int32, bool rel) >
(_SC("CreateSpriteEx"), &CreateSpriteEx)
.Overload< Object & (*)(Int32, CSStr, Int32, Int32, Int32, Int32, Float32, Int32, bool rel, Int32, Object &) >
(_SC("CreateSpriteEx"), &CreateSpriteEx)
.Overload< Object & (*)(CSStr, const Vector2i &, const Vector2i &, Float32, Int32, bool) >
(_SC("CreateSprite"), &CreateSprite)
.Overload< Object & (*)(CSStr, const Vector2i &, const Vector2i &, Float32, Int32, bool, Int32, Object &) >
(_SC("CreateSprite"), &CreateSprite)
.Overload< Object & (*)(Int32, CSStr, const Vector2i &, const Vector2i &, Float32, Int32, bool) >
(_SC("CreateSprite"), &CreateSprite)
.Overload< Object & (*)(Int32, CSStr, const Vector2i &, const Vector2i &, Float32, Int32, bool, Int32, Object &) >
(_SC("CreateSprite"), &CreateSprite);
}
} // Namespace:: SqMod
} // Namespace:: SqMod

View File

@ -8,7 +8,7 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Manages Sprite instances.
* Manages a single sprite entity.
*/
class CSprite
{
@ -17,20 +17,19 @@ class CSprite
private:
/* --------------------------------------------------------------------------------------------
* Cached identifiers for fast integer to string conversion.
*/
static SQChar s_StrID[SQMOD_SPRITE_POOL][8];
/* --------------------------------------------------------------------------------------------
* Identifier of the managed entity.
*/
Int32 m_ID;
/* --------------------------------------------------------------------------------------------
* User tag and data associated with this instance.
* User tag associated with this instance.
*/
String m_Tag;
/* --------------------------------------------------------------------------------------------
* User data associated with this instance.
*/
Object m_Data;
/* --------------------------------------------------------------------------------------------
@ -38,35 +37,45 @@ private:
*/
CSprite(Int32 id);
public:
/* --------------------------------------------------------------------------------------------
* Maximum possible number that could represent an identifier for this entity type.
*/
static const Int32 Max;
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
CSprite(const CSprite &);
CSprite(const CSprite &) = delete;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
* Move constructor. (disabled)
*/
CSprite & operator = (const CSprite &);
public:
// --------------------------------------------------------------------------------------------
static const Int32 Max;
CSprite(CSprite &&) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~CSprite();
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
*/
CSprite & operator = (const CSprite &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
CSprite & operator = (CSprite &&) = delete;
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity.
*/
bool Validate() const
void Validate() const
{
if (VALID_ENTITY(m_ID))
return true;
SqThrow("Invalid sprite reference [%s]", m_Tag.c_str());
return false;
if (INVALID_ENTITY(m_ID))
SqThrowF("Invalid sprite reference [%s]", m_Tag.c_str());
}
/* --------------------------------------------------------------------------------------------
@ -77,27 +86,33 @@ public:
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
const String & ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Retrieve the identifier of the entity managed by this instance.
*/
Int32 GetID() const { return m_ID; }
/* --------------------------------------------------------------------------------------------
* Retrieve the maximum possible identifier to an entity of this type.
*/
Int32 GetMaxID() const { return SQMOD_SPRITE_POOL; }
Int32 GetID() const
{
return m_ID;
}
/* --------------------------------------------------------------------------------------------
* Check whether this instance manages a valid entity.
*/
bool IsActive() const { return VALID_ENTITY(m_ID); }
bool IsActive() const
{
return VALID_ENTITY(m_ID);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the associated user tag.
*/
CSStr GetTag() const;
const String & GetTag() const;
/* --------------------------------------------------------------------------------------------
* Modify the associated user tag.
@ -114,38 +129,146 @@ public:
*/
void SetData(Object & data);
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Destroy the managed sprite entity.
*/
bool Destroy()
{
return Destroy(0, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed sprite entity.
*/
bool Destroy(Int32 header)
{
return Destroy(header, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed sprite entity.
*/
bool Destroy(Int32 header, Object & payload);
bool Destroy() { return Destroy(0, NullObject()); }
bool Destroy(Int32 header) { return Destroy(header, NullObject()); }
// --------------------------------------------------------------------------------------------
bool BindEvent(Int32 evid, Object & env, Function & func) const;
/* --------------------------------------------------------------------------------------------
* Bind to an event supported by this entity type.
*/
void BindEvent(Int32 evid, Object & env, Function & func) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Show the managed sprite entity to all players on the server.
*/
void ShowAll() const;
/* --------------------------------------------------------------------------------------------
* Show the managed sprite entity to the specified player entity.
*/
void ShowFor(CPlayer & player) const;
/* --------------------------------------------------------------------------------------------
* Show the managed sprite entity to all players in the specified range.
*/
void ShowRange(Int32 first, Int32 last) const;
/* --------------------------------------------------------------------------------------------
* Hide the managed sprite entity from all players on the server.
*/
void HideAll() const;
/* --------------------------------------------------------------------------------------------
* Hide the managed sprite entity from the specified player entity.
*/
void HideFor(CPlayer & player) const;
/* --------------------------------------------------------------------------------------------
* Hide the managed sprite entity from all players in the specified range.
*/
void HideRange(Int32 first, Int32 last) const;
/* --------------------------------------------------------------------------------------------
* Set the position of the managed sprite entity for all players on the server.
*/
void SetPositionAll(const Vector2i & pos) const;
/* --------------------------------------------------------------------------------------------
* Set the position of the managed sprite entity for all players on the server.
*/
void SetPositionAllEx(Int32 x, Int32 y) const;
/* --------------------------------------------------------------------------------------------
* Set the position of the managed sprite entity for the specified player entity.
*/
void SetPositionFor(CPlayer & player, const Vector2i & pos) const;
/* --------------------------------------------------------------------------------------------
* Set the position of the managed sprite entity for the specified player entity.
*/
void SetPositionForEx(CPlayer & player, Int32 x, Int32 y) const;
/* --------------------------------------------------------------------------------------------
* Set the position of the managed sprite entity for all players in the specified range.
*/
void SetPositionRange(Int32 first, Int32 last, const Vector2i & pos) const;
/* --------------------------------------------------------------------------------------------
* Set the center of the managed sprite entity for all players on the server.
*/
void SetCenterAll(const Vector2i & pos) const;
/* --------------------------------------------------------------------------------------------
* Set the center of the managed sprite entity for all players on the server.
*/
void SetCenterAllEx(Int32 x, Int32 y) const;
/* --------------------------------------------------------------------------------------------
* Set the center of the managed sprite entity for the specified player entity.
*/
void SetCenterFor(CPlayer & player, const Vector2i & pos) const;
/* --------------------------------------------------------------------------------------------
* Set the center of the managed sprite entity for the specified player entity.
*/
void SetCenterForEx(CPlayer & player, Int32 x, Int32 y) const;
/* --------------------------------------------------------------------------------------------
* Set the center of the managed sprite entity for all players in the specified range.
*/
void SetCenterRange(Int32 first, Int32 last, const Vector2i & pos) const;
void SetRotationAll(SQFloat rot) const;
void SetRotationFor(CPlayer & player, SQFloat rot) const;
void SetRotationRange(Int32 first, Int32 last, SQFloat rot) const;
/* --------------------------------------------------------------------------------------------
* Set the rotation of the managed sprite entity for all players on the server.
*/
void SetRotationAll(Float32 rot) const;
/* --------------------------------------------------------------------------------------------
* Set the rotation of the managed sprite entity for the specified player entity.
*/
void SetRotationFor(CPlayer & player, Float32 rot) const;
/* --------------------------------------------------------------------------------------------
* Set the rotation of the managed sprite entity for all players in the specified range.
*/
void SetRotationRange(Int32 first, Int32 last, Float32 rot) const;
/* --------------------------------------------------------------------------------------------
* Set the alpha of the managed sprite entity for all players on the server.
*/
void SetAlphaAll(Uint8 alpha) const;
/* --------------------------------------------------------------------------------------------
* Set the alpha of the managed sprite entity for the specified player entity.
*/
void SetAlphaFor(CPlayer & player, Uint8 alpha) const;
/* --------------------------------------------------------------------------------------------
* Set the alpha of the managed sprite entity for all players in the specified range.
*/
void SetAlphaRange(Int32 first, Int32 last, Uint8 alpha) const;
CSStr GetFilePath() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the file path of the texture used by the managed sprite entity.
*/
const String & GetFilePath() const;
};
} // Namespace:: SqMod

View File

@ -8,15 +8,20 @@
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQChar CTextdraw::s_StrID[SQMOD_TEXTDRAW_POOL][8];
const Int32 CTextdraw::Max = SQMOD_TEXTDRAW_POOL;
// ------------------------------------------------------------------------------------------------
const Int32 CTextdraw::Max = SQMOD_TEXTDRAW_POOL;
SQInteger CTextdraw::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("SqTextdraw");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
CTextdraw::CTextdraw(Int32 id)
: m_ID(VALID_ENTITYGETEX(id, SQMOD_TEXTDRAW_POOL))
, m_Tag(VALID_ENTITY(m_ID) ? s_StrID[m_ID] : _SC("-1"))
, m_Tag(ToStrF("%d", id))
{
/* ... */
}
@ -38,206 +43,283 @@ Int32 CTextdraw::Cmp(const CTextdraw & o) const
return -1;
}
CSStr CTextdraw::ToString() const
// ------------------------------------------------------------------------------------------------
const String & CTextdraw::ToString() const
{
return VALID_ENTITYEX(m_ID, SQMOD_TEXTDRAW_POOL) ? s_StrID[m_ID] : _SC("-1");
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
CSStr CTextdraw::GetTag() const
const String & CTextdraw::GetTag() const
{
return m_Tag.c_str();
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::SetTag(CSStr tag)
{
m_Tag.assign(tag);
}
// ------------------------------------------------------------------------------------------------
Object & CTextdraw::GetData()
{
if (Validate())
return m_Data;
return NullObject();
// Validate the managed identifier
Validate();
// Return the requested information
return m_Data;
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::SetData(Object & data)
{
if (Validate())
m_Data = data;
// Validate the managed identifier
Validate();
// Apply the specified value
m_Data = data;
}
// ------------------------------------------------------------------------------------------------
bool CTextdraw::Destroy(Int32 header, Object & payload)
{
// Validate the managed identifier
Validate();
// Perform the requested operation
return _Core->DelTextdraw(m_ID, header, payload);
}
// ------------------------------------------------------------------------------------------------
bool CTextdraw::BindEvent(Int32 evid, Object & env, Function & func) const
void CTextdraw::BindEvent(Int32 evid, Object & env, Function & func) const
{
if (!Validate())
return false;
// Validate the managed identifier
Validate();
// Obtain the function instance called for this event
Function & event = _Core->GetTextdrawEvent(m_ID, evid);
// Is the specified callback function null?
if (func.IsNull())
event.Release();
event.Release(); // Then release the current callback
// Assign the specified environment and function
else
event = Function(env.GetVM(), env, func.GetFunc());
return true;
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::ShowAll() const
{
if (Validate())
_Func->ShowTextdraw(m_ID, -1);
}
void CTextdraw::ShowFor(CPlayer & player) const
{
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
_Func->ShowTextdraw(m_ID, player.GetID());
}
void CTextdraw::ShowRange(Int32 first, Int32 last) const
{
if (first > last)
SqThrow("Invalid player range: %d > %d", first, last);
else if (Validate())
for (; first <= last; ++first)
{
if (_Func->IsPlayerConnected(first))
_Func->ShowTextdraw(m_ID, first);
}
}
void CTextdraw::HideAll() const
{
if (Validate())
_Func->HideTextdraw(m_ID, -1);
}
void CTextdraw::HideFor(CPlayer & player) const
{
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
_Func->HideTextdraw(m_ID, player.GetID());
}
void CTextdraw::HideRange(Int32 first, Int32 last) const
{
if (first > last)
SqThrow("Invalid player range: %d > %d", first, last);
else if (Validate())
for (; first <= last; ++first)
{
if (_Func->IsPlayerConnected(first))
_Func->HideTextdraw(m_ID, first);
}
}
void CTextdraw::SetPositionAll(const Vector2i & pos) const
{
if (Validate())
_Func->MoveTextdraw(m_ID, -1, pos.x, pos.y);
}
void CTextdraw::SetPositionAllEx(Int32 x, Int32 y) const
{
if (Validate())
_Func->MoveTextdraw(m_ID, -1, x, y);
}
void CTextdraw::SetPositionFor(CPlayer & player, const Vector2i & pos) const
{
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
_Func->MoveTextdraw(m_ID, player.GetID(), pos.x, pos.y);
}
void CTextdraw::SetPositionForEx(CPlayer & player, Int32 x, Int32 y) const
{
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
_Func->MoveTextdraw(m_ID, player.GetID(), x, y);
}
void CTextdraw::SetPositionRange(Int32 first, Int32 last, const Vector2i & pos) const
{
if (first > last)
SqThrow("Invalid player range: %d > %d", first, last);
else if (Validate())
for (; first <= last; ++first)
{
if (_Func->IsPlayerConnected(first))
_Func->MoveTextdraw(m_ID, first, pos.x, pos.y);
}
}
void CTextdraw::SetColorAll(const Color4 & col) const
{
if (Validate())
_Func->SetTextdrawColour(m_ID, -1, col.GetRGBA());
}
void CTextdraw::SetColorAllEx(Uint8 r, Uint8 g, Uint8 b, Uint8 a) const
{
if (Validate())
_Func->SetTextdrawColour(m_ID, -1, SQMOD_PACK_RGBA(r, g, b, a));
}
void CTextdraw::SetColorFor(CPlayer & player, const Color4 & col) const
{
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
_Func->SetTextdrawColour(m_ID, player.GetID(), col.GetRGBA());
}
void CTextdraw::SetColorForEx(CPlayer & player, Uint8 r, Uint8 g, Uint8 b, Uint8 a) const
{
if (!player.IsActive())
SqThrow("Invalid player argument: null");
else if (Validate())
_Func->SetTextdrawColour(m_ID, player.GetID(), SQMOD_PACK_RGBA(r, g, b, a));
}
void CTextdraw::SetColorRange(Int32 first, Int32 last, const Color4 & col) const
{
if (first > last)
SqThrow("Invalid player range: %d > %d", first, last);
else if (Validate())
for (const Uint32 color = col.GetRGBA(); first <= last; ++first)
{
if (_Func->IsPlayerConnected(first))
_Func->SetTextdrawColour(m_ID, first, color);
}
}
CSStr CTextdraw::GetText() const
{
if (Validate())
_Core->GetTextdraw(m_ID).mText.c_str();
return g_EmptyStr;
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->ShowTextdraw(m_ID, -1);
}
// ------------------------------------------------------------------------------------------------
static Object & CreateTextdrawEx(CSStr text, Int32 xp, Int32 yp,
void CTextdraw::ShowFor(CPlayer & player) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->ShowTextdraw(m_ID, player.GetID());
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::ShowRange(Int32 first, Int32 last) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then show this textdraw on his client
_Func->ShowTextdraw(m_ID, first);
}
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::HideAll() const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->HideTextdraw(m_ID, -1);
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::HideFor(CPlayer & player) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->HideTextdraw(m_ID, player.GetID());
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::HideRange(Int32 first, Int32 last) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then hide this textdraw on his client
_Func->HideTextdraw(m_ID, first);
}
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::SetPositionAll(const Vector2i & pos) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveTextdraw(m_ID, -1, pos.x, pos.y);
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::SetPositionAllEx(Int32 x, Int32 y) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveTextdraw(m_ID, -1, x, y);
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::SetPositionFor(CPlayer & player, const Vector2i & pos) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveTextdraw(m_ID, player.GetID(), pos.x, pos.y);
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::SetPositionForEx(CPlayer & player, Int32 x, Int32 y) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveTextdraw(m_ID, player.GetID(), x, y);
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::SetPositionRange(Int32 first, Int32 last, const Vector2i & pos) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then move this textdraw on his client
_Func->MoveTextdraw(m_ID, first, pos.x, pos.y);
}
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::SetColorAll(const Color4 & col) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetTextdrawColour(m_ID, -1, col.GetRGBA());
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::SetColorAllEx(Uint8 r, Uint8 g, Uint8 b, Uint8 a) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetTextdrawColour(m_ID, -1, SQMOD_PACK_RGBA(r, g, b, a));
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::SetColorFor(CPlayer & player, const Color4 & col) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetTextdrawColour(m_ID, player.GetID(), col.GetRGBA());
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::SetColorForEx(CPlayer & player, Uint8 r, Uint8 g, Uint8 b, Uint8 a) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetTextdrawColour(m_ID, player.GetID(), SQMOD_PACK_RGBA(r, g, b, a));
}
// ------------------------------------------------------------------------------------------------
void CTextdraw::SetColorRange(Int32 first, Int32 last, const Color4 & col) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (const Uint32 color = col.GetRGBA(); first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then colorize this textdraw on his client
_Func->SetTextdrawColour(m_ID, first, color);
}
}
// ------------------------------------------------------------------------------------------------
const String & CTextdraw::GetText() const
{
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetTextdraw(m_ID).mText;
}
// ------------------------------------------------------------------------------------------------
static Object & Textdraw_CreateEx(CSStr text, Int32 xp, Int32 yp,
Uint8 r, Uint8 g, Uint8 b, Uint8 a, bool rel)
{
return _Core->NewTextdraw(SQMOD_UNKNOWN, text, xp, yp, SQMOD_PACK_ARGB(a, r, g, b), rel,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateTextdrawEx(CSStr text, Int32 xp, Int32 yp,
// ------------------------------------------------------------------------------------------------
static Object & Textdraw_CreateEx(CSStr text, Int32 xp, Int32 yp,
Uint8 r, Uint8 g, Uint8 b, Uint8 a, bool rel,
Int32 header, Object & payload)
{
@ -246,14 +328,15 @@ static Object & CreateTextdrawEx(CSStr text, Int32 xp, Int32 yp,
}
// ------------------------------------------------------------------------------------------------
static Object & CreateTextdrawEx(Int32 index, CSStr text, Int32 xp, Int32 yp,
static Object & Textdraw_CreateEx(Int32 index, CSStr text, Int32 xp, Int32 yp,
Uint8 r, Uint8 g, Uint8 b, Uint8 a, bool rel)
{
return _Core->NewTextdraw(index,text, xp, yp, SQMOD_PACK_ARGB(a, r, g, b), rel,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateTextdrawEx(Int32 index, CSStr text, Int32 xp, Int32 yp,
// ------------------------------------------------------------------------------------------------
static Object & Textdraw_CreateEx(Int32 index, CSStr text, Int32 xp, Int32 yp,
Uint8 r, Uint8 g, Uint8 b, Uint8 a, bool rel,
Int32 header, Object & payload)
{
@ -262,13 +345,14 @@ static Object & CreateTextdrawEx(Int32 index, CSStr text, Int32 xp, Int32 yp,
}
// ------------------------------------------------------------------------------------------------
static Object & CreateTextdraw(CSStr text, const Vector2i & pos, const Color4 & color, bool rel)
static Object & Textdraw_Create(CSStr text, const Vector2i & pos, const Color4 & color, bool rel)
{
return _Core->NewTextdraw(SQMOD_UNKNOWN, text, pos.x, pos.y, color.GetARGB(), rel,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateTextdraw(CSStr text, const Vector2i & pos, const Color4 & color, bool rel,
// ------------------------------------------------------------------------------------------------
static Object & Textdraw_Create(CSStr text, const Vector2i & pos, const Color4 & color, bool rel,
Int32 header, Object & payload)
{
return _Core->NewTextdraw(SQMOD_UNKNOWN, text, pos.x, pos.y, color.GetARGB(), rel,
@ -276,14 +360,15 @@ static Object & CreateTextdraw(CSStr text, const Vector2i & pos, const Color4 &
}
// ------------------------------------------------------------------------------------------------
static Object & CreateTextdraw(Int32 index, CSStr text, const Vector2i & pos, const Color4 & color,
static Object & Textdraw_Create(Int32 index, CSStr text, const Vector2i & pos, const Color4 & color,
bool rel)
{
return _Core->NewTextdraw(index, text, pos.x, pos.y, color.GetARGB(), rel,
SQMOD_CREATE_DEFAULT, NullObject());
}
static Object & CreateTextdraw(Int32 index, CSStr text, const Vector2i & pos, const Color4 & color,
// ------------------------------------------------------------------------------------------------
static Object & Textdraw_Create(Int32 index, CSStr text, const Vector2i & pos, const Color4 & color,
bool rel, Int32 header, Object & payload)
{
return _Core->NewTextdraw(index, text, pos.x, pos.y, color.GetARGB(), rel, header, payload);
@ -294,24 +379,26 @@ void Register_CTextdraw(HSQUIRRELVM vm)
{
RootTable(vm).Bind(_SC("SqTextdraw"),
Class< CTextdraw, NoConstructor< CTextdraw > >(vm, _SC("SqTextdraw"))
/* Metamethods */
// Metamethods
.Func(_SC("_cmp"), &CTextdraw::Cmp)
.SquirrelFunc(_SC("_typename"), &CTextdraw::Typename)
.Func(_SC("_tostring"), &CTextdraw::ToString)
/* Core Properties */
// Static values
.SetStaticValue(_SC("MaxID"), CTextdraw::Max)
// Core Properties
.Prop(_SC("ID"), &CTextdraw::GetID)
.Prop(_SC("Tag"), &CTextdraw::GetTag, &CTextdraw::SetTag)
.Prop(_SC("Data"), &CTextdraw::GetData, &CTextdraw::SetData)
.Prop(_SC("MaxID"), &CTextdraw::GetMaxID)
.Prop(_SC("Active"), &CTextdraw::IsActive)
/* Core Functions */
// Core Functions
.Func(_SC("Bind"), &CTextdraw::BindEvent)
/* Core Overloads */
// Core Overloads
.Overload< bool (CTextdraw::*)(void) >(_SC("Destroy"), &CTextdraw::Destroy)
.Overload< bool (CTextdraw::*)(Int32) >(_SC("Destroy"), &CTextdraw::Destroy)
.Overload< bool (CTextdraw::*)(Int32, Object &) >(_SC("Destroy"), &CTextdraw::Destroy)
/* Properties */
// Properties
.Prop(_SC("Text"), &CTextdraw::GetText)
/* Functions */
// Functions
.Func(_SC("ShowAll"), &CTextdraw::ShowAll)
.Func(_SC("ShowTo"), &CTextdraw::ShowFor)
.Func(_SC("ShowFor"), &CTextdraw::ShowFor)
@ -322,7 +409,7 @@ void Register_CTextdraw(HSQUIRRELVM vm)
.Func(_SC("HideRange"), &CTextdraw::HideRange)
.Func(_SC("SetPositionRange"), &CTextdraw::SetPositionRange)
.Func(_SC("SetColorRange"), &CTextdraw::SetColorRange)
/* Overloads */
// Overloads
.Overload< void (CTextdraw::*)(const Vector2i &) const >
(_SC("SetPositionAll"), &CTextdraw::SetPositionAll)
.Overload< void (CTextdraw::*)(Int32, Int32) const >
@ -339,25 +426,24 @@ void Register_CTextdraw(HSQUIRRELVM vm)
(_SC("SetColorFor"), &CTextdraw::SetColorFor)
.Overload< void (CTextdraw::*)(CPlayer &, Uint8, Uint8, Uint8, Uint8) const >
(_SC("SetColorFor"), &CTextdraw::SetColorForEx)
// Static Overloads
.StaticOverload< Object & (*)(CSStr, Int32, Int32, Uint8, Uint8, Uint8, Uint8, bool) >
(_SC("CreateEx"), &Textdraw_CreateEx)
.StaticOverload< Object & (*)(CSStr, Int32, Int32, Uint8, Uint8, Uint8, Uint8, bool, Int32, Object &) >
(_SC("CreateEx"), &Textdraw_CreateEx)
.StaticOverload< Object & (*)(Int32, CSStr, Int32, Int32, Uint8, Uint8, Uint8, Uint8, bool) >
(_SC("CreateEx"), &Textdraw_CreateEx)
.StaticOverload< Object & (*)(Int32, CSStr, Int32, Int32, Uint8, Uint8, Uint8, Uint8, bool, Int32, Object &) >
(_SC("CreateEx"), &Textdraw_CreateEx)
.StaticOverload< Object & (*)(CSStr, const Vector2i &, const Color4 &, bool) >
(_SC("Create"), &Textdraw_Create)
.StaticOverload< Object & (*)(CSStr, const Vector2i &, const Color4 &, bool, Int32, Object &) >
(_SC("Create"), &Textdraw_Create)
.StaticOverload< Object & (*)(Int32, CSStr, const Vector2i &, const Color4 &, bool) >
(_SC("Create"), &Textdraw_Create)
.StaticOverload< Object & (*)(Int32, CSStr, const Vector2i &, const Color4 &, bool, Int32, Object &) >
(_SC("Create"), &Textdraw_Create)
);
RootTable(vm)
.Overload< Object & (*)(CSStr, Int32, Int32, Uint8, Uint8, Uint8, Uint8, bool) >
(_SC("CreateTextdrawEx"), &CreateTextdrawEx)
.Overload< Object & (*)(CSStr, Int32, Int32, Uint8, Uint8, Uint8, Uint8, bool, Int32, Object &) >
(_SC("CreateTextdrawEx"), &CreateTextdrawEx)
.Overload< Object & (*)(Int32, CSStr, Int32, Int32, Uint8, Uint8, Uint8, Uint8, bool) >
(_SC("CreateTextdrawEx"), &CreateTextdrawEx)
.Overload< Object & (*)(Int32, CSStr, Int32, Int32, Uint8, Uint8, Uint8, Uint8, bool, Int32, Object &) >
(_SC("CreateTextdrawEx"), &CreateTextdrawEx)
.Overload< Object & (*)(CSStr, const Vector2i &, const Color4 &, bool) >
(_SC("CreateTextdraw"), &CreateTextdraw)
.Overload< Object & (*)(CSStr, const Vector2i &, const Color4 &, bool, Int32, Object &) >
(_SC("CreateTextdraw"), &CreateTextdraw)
.Overload< Object & (*)(Int32, CSStr, const Vector2i &, const Color4 &, bool) >
(_SC("CreateTextdraw"), &CreateTextdraw)
.Overload< Object & (*)(Int32, CSStr, const Vector2i &, const Color4 &, bool, Int32, Object &) >
(_SC("CreateTextdraw"), &CreateTextdraw);
}
} // Namespace:: SqMod
} // Namespace:: SqMod

View File

@ -8,7 +8,7 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Manages Textdraw instances.
* Manages a single textdraw entity.
*/
class CTextdraw
{
@ -17,20 +17,19 @@ class CTextdraw
private:
/* --------------------------------------------------------------------------------------------
* Cached identifiers for fast integer to string conversion.
*/
static SQChar s_StrID[SQMOD_TEXTDRAW_POOL][8];
/* --------------------------------------------------------------------------------------------
* Identifier of the managed entity.
*/
Int32 m_ID;
/* --------------------------------------------------------------------------------------------
* User tag and data associated with this instance.
* User tag associated with this instance.
*/
String m_Tag;
/* --------------------------------------------------------------------------------------------
* User data associated with this instance.
*/
Object m_Data;
/* --------------------------------------------------------------------------------------------
@ -38,35 +37,45 @@ private:
*/
CTextdraw(Int32 id);
public:
/* --------------------------------------------------------------------------------------------
* Maximum possible number that could represent an identifier for this entity type.
*/
static const Int32 Max;
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
CTextdraw(const CTextdraw &);
CTextdraw(const CTextdraw &) = delete;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
* Move constructor. (disabled)
*/
CTextdraw & operator = (const CTextdraw &);
public:
// --------------------------------------------------------------------------------------------
static const Int32 Max;
CTextdraw(CTextdraw &&) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~CTextdraw();
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
*/
CTextdraw & operator = (const CTextdraw &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
CTextdraw & operator = (CTextdraw &&) = delete;
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity.
*/
bool Validate() const
void Validate() const
{
if (VALID_ENTITY(m_ID))
return true;
SqThrow("Invalid textdraw reference [%s]", m_Tag.c_str());
return false;
if (INVALID_ENTITY(m_ID))
SqThrowF("Invalid textdraw reference [%s]", m_Tag.c_str());
}
/* --------------------------------------------------------------------------------------------
@ -77,27 +86,33 @@ public:
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
const String & ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Retrieve the identifier of the entity managed by this instance.
*/
Int32 GetID() const { return m_ID; }
/* --------------------------------------------------------------------------------------------
* Retrieve the maximum possible identifier to an entity of this type.
*/
Int32 GetMaxID() const { return SQMOD_TEXTDRAW_POOL; }
Int32 GetID() const
{
return m_ID;
}
/* --------------------------------------------------------------------------------------------
* Check whether this instance manages a valid entity.
*/
bool IsActive() const { return VALID_ENTITY(m_ID); }
bool IsActive() const
{
return VALID_ENTITY(m_ID);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the associated user tag.
*/
CSStr GetTag() const;
const String & GetTag() const;
/* --------------------------------------------------------------------------------------------
* Modify the associated user tag.
@ -114,32 +129,116 @@ public:
*/
void SetData(Object & data);
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Destroy the managed textdraw entity.
*/
bool Destroy()
{
return Destroy(0, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed textdraw entity.
*/
bool Destroy(Int32 header)
{
return Destroy(header, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed textdraw entity.
*/
bool Destroy(Int32 header, Object & payload);
bool Destroy() { return Destroy(0, NullObject()); }
bool Destroy(Int32 header) { return Destroy(header, NullObject()); }
// --------------------------------------------------------------------------------------------
bool BindEvent(Int32 evid, Object & env, Function & func) const;
/* --------------------------------------------------------------------------------------------
* Bind to an event supported by this entity type.
*/
void BindEvent(Int32 evid, Object & env, Function & func) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Show the managed textdraw entity to all players on the server.
*/
void ShowAll() const;
/* --------------------------------------------------------------------------------------------
* Show the managed textdraw entity to the specified player entity.
*/
void ShowFor(CPlayer & player) const;
/* --------------------------------------------------------------------------------------------
* Show the managed textdraw entity to all players in the specified range.
*/
void ShowRange(Int32 first, Int32 last) const;
/* --------------------------------------------------------------------------------------------
* Hide the managed textdraw entity from all players on the server.
*/
void HideAll() const;
/* --------------------------------------------------------------------------------------------
* Hide the managed textdraw entity from the specified player entity.
*/
void HideFor(CPlayer & player) const;
/* --------------------------------------------------------------------------------------------
* Hide the managed textdraw entity from all players in the specified range.
*/
void HideRange(Int32 first, Int32 last) const;
/* --------------------------------------------------------------------------------------------
* Set the position of the managed textdraw entity for all players on the server.
*/
void SetPositionAll(const Vector2i & pos) const;
/* --------------------------------------------------------------------------------------------
* Set the position of the managed textdraw entity for all players on the server.
*/
void SetPositionAllEx(Int32 x, Int32 y) const;
/* --------------------------------------------------------------------------------------------
* Set the position of the managed textdraw entity for the specified player entity.
*/
void SetPositionFor(CPlayer & player, const Vector2i & pos) const;
/* --------------------------------------------------------------------------------------------
* Set the position of the managed textdraw entity for the specified player entity.
*/
void SetPositionForEx(CPlayer & player, Int32 x, Int32 y) const;
/* --------------------------------------------------------------------------------------------
* Set the position of the managed textdraw entity for all players in the specified range.
*/
void SetPositionRange(Int32 first, Int32 last, const Vector2i & pos) const;
/* --------------------------------------------------------------------------------------------
* Set the center of the managed textdraw entity for all players on the server.
*/
void SetColorAll(const Color4 & col) const;
/* --------------------------------------------------------------------------------------------
* Set the color of the managed textdraw entity for all players on the server.
*/
void SetColorAllEx(Uint8 r, Uint8 g, Uint8 b, Uint8 a) const;
/* --------------------------------------------------------------------------------------------
* Set the color of the managed textdraw entity for the specified player entity.
*/
void SetColorFor(CPlayer & player, const Color4 & col) const;
/* --------------------------------------------------------------------------------------------
* Set the color of the managed textdraw entity for the specified player entity.
*/
void SetColorForEx(CPlayer & player, Uint8 r, Uint8 g, Uint8 b, Uint8 a) const;
/* --------------------------------------------------------------------------------------------
* Set the color of the managed textdraw entity for all players in the specified range.
*/
void SetColorRange(Int32 first, Int32 last, const Color4 & col) const;
CSStr GetText() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the text string used by the managed textdraw entity.
*/
const String & GetText() const;
};
} // Namespace:: SqMod

File diff suppressed because it is too large Load Diff

View File

@ -8,7 +8,7 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Manages Vehicle instances.
* Manages a single vehicle entity.
*/
class CVehicle
{
@ -22,20 +22,19 @@ private:
static Vector4 s_Vector4;
static Quaternion s_Quaternion;
/* --------------------------------------------------------------------------------------------
* Cached identifiers for fast integer to string conversion.
*/
static SQChar s_StrID[SQMOD_VEHICLE_POOL][8];
/* --------------------------------------------------------------------------------------------
* Identifier of the managed entity.
*/
Int32 m_ID;
/* --------------------------------------------------------------------------------------------
* User tag and data associated with this instance.
* User tag associated with this instance.
*/
String m_Tag;
/* --------------------------------------------------------------------------------------------
* User data associated with this instance.
*/
Object m_Data;
/* --------------------------------------------------------------------------------------------
@ -43,35 +42,45 @@ private:
*/
CVehicle(Int32 id);
public:
/* --------------------------------------------------------------------------------------------
* Maximum possible number that could represent an identifier for this entity type.
*/
static const Int32 Max;
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
CVehicle(const CVehicle &);
CVehicle(const CVehicle &) = delete;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
* Move constructor. (disabled)
*/
CVehicle & operator = (const CVehicle &);
public:
// --------------------------------------------------------------------------------------------
static const Int32 Max;
CVehicle(CVehicle &&) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~CVehicle();
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
*/
CVehicle & operator = (const CVehicle &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
CVehicle & operator = (CVehicle &&) = delete;
/* --------------------------------------------------------------------------------------------
* See whether this instance manages a valid entity.
*/
bool Validate() const
void Validate() const
{
if (VALID_ENTITY(m_ID))
return true;
SqThrow("Invalid vehicle reference [%s]", m_Tag.c_str());
return false;
if (INVALID_ENTITY(m_ID))
SqThrowF("Invalid vehicle reference [%s]", m_Tag.c_str());
}
/* --------------------------------------------------------------------------------------------
@ -82,27 +91,33 @@ public:
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
const String & ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Retrieve the identifier of the entity managed by this instance.
*/
Int32 GetID() const { return m_ID; }
/* --------------------------------------------------------------------------------------------
* Retrieve the maximum possible identifier to an entity of this type.
*/
Int32 GetMaxID() const { return SQMOD_VEHICLE_POOL; }
Int32 GetID() const
{
return m_ID;
}
/* --------------------------------------------------------------------------------------------
* Check whether this instance manages a valid entity.
*/
bool IsActive() const { return VALID_ENTITY(m_ID); }
bool IsActive() const
{
return VALID_ENTITY(m_ID);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the associated user tag.
*/
CSStr GetTag() const;
const String & GetTag() const;
/* --------------------------------------------------------------------------------------------
* Modify the associated user tag.
@ -119,126 +134,560 @@ public:
*/
void SetData(Object & data);
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Destroy the managed vehicle entity.
*/
bool Destroy()
{
return Destroy(0, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed vehicle entity.
*/
bool Destroy(Int32 header)
{
return Destroy(header, NullObject());
}
/* --------------------------------------------------------------------------------------------
* Destroy the managed vehicle entity.
*/
bool Destroy(Int32 header, Object & payload);
bool Destroy() { return Destroy(0, NullObject()); }
bool Destroy(Int32 header) { return Destroy(header, NullObject()); }
// --------------------------------------------------------------------------------------------
bool BindEvent(Int32 evid, Object & env, Function & func) const;
/* --------------------------------------------------------------------------------------------
* Bind to an event supported by this entity type.
*/
void BindEvent(Int32 evid, Object & env, Function & func) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* See if the managed vehicle entity is streamed for the specified player.
*/
bool IsStreamedFor(CPlayer & player) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the synchronization source of the managed vehicle entity.
*/
Int32 GetSyncSource() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the synchronization type of the managed vehicle entity.
*/
Int32 GetSyncType() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the world in which the managed vehicle entity exists.
*/
Int32 GetWorld() const;
/* --------------------------------------------------------------------------------------------
* Modify the world in which the managed vehicle entity exists.
*/
void SetWorld(Int32 world) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the vehicle model of the managed vehicle entity.
*/
Int32 GetModel() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the slot occupant from the managed vehicle entity.
*/
Object & GetOccupant(Int32 slot) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the slot occupant identifier from the managed vehicle entity.
*/
Int32 GetOccupantID(Int32 slot) const;
/* --------------------------------------------------------------------------------------------
* Respawn the managed vehicle entity.
*/
void Respawn() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the immunity flags of the managed vehicle entity.
*/
Int32 GetImmunity() const;
/* --------------------------------------------------------------------------------------------
* Modify the immunity flags of the managed vehicle entity.
*/
void SetImmunity(Int32 flags) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed vehicle entity is wrecked.
*/
bool IsWrecked() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position of the managed vehicle entity.
*/
const Vector3 & GetPosition() const;
/* --------------------------------------------------------------------------------------------
* Modify the position of the managed vehicle entity.
*/
void SetPosition(const Vector3 & pos) const;
/* --------------------------------------------------------------------------------------------
* Modify the position of the managed vehicle entity.
*/
void SetPositionEx(const Vector3 & pos, bool empty) const;
/* --------------------------------------------------------------------------------------------
* Modify the position of the managed vehicle entity.
*/
void SetPositionEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Modify the position of the managed vehicle entity.
*/
void SetPositionEx(Float32 x, Float32 y, Float32 z, bool empty) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the rotation of the managed vehicle entity.
*/
const Quaternion & GetRotation() const;
/* --------------------------------------------------------------------------------------------
* Modify the rotation of the managed vehicle entity.
*/
void SetRotation(const Quaternion & rot) const;
/* --------------------------------------------------------------------------------------------
* Modify the rotation of the managed vehicle entity.
*/
void SetRotationEx(Float32 x, Float32 y, Float32 z, Float32 w) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the euler rotation of the managed vehicle entity.
*/
const Vector3 & GetRotationEuler() const;
/* --------------------------------------------------------------------------------------------
* Modify the euler rotation of the managed vehicle entity.
*/
void SetRotationEuler(const Vector3 & rot) const;
/* --------------------------------------------------------------------------------------------
* Modify the euler rotation of the managed vehicle entity.
*/
void SetRotationEulerEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the speed of the managed vehicle entity.
*/
const Vector3 & GetSpeed() const;
/* --------------------------------------------------------------------------------------------
* Modify the speed of the managed vehicle entity.
*/
void SetSpeed(const Vector3 & vel) const;
/* --------------------------------------------------------------------------------------------
* Modify the speed of the managed vehicle entity.
*/
void SetSpeedEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Modify the speed of the managed vehicle entity.
*/
void AddSpeed(const Vector3 & vel) const;
/* --------------------------------------------------------------------------------------------
* Modify the speed of the managed vehicle entity.
*/
void AddSpeedEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the relative speed of the managed vehicle entity.
*/
const Vector3 & GetRelSpeed() const;
/* --------------------------------------------------------------------------------------------
* Modify the relative speed of the managed vehicle entity.
*/
void SetRelSpeed(const Vector3 & vel) const;
/* --------------------------------------------------------------------------------------------
* Modify the relative speed of the managed vehicle entity.
*/
void SetRelSpeedEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Modify the relative speed of the managed vehicle entity.
*/
void AddRelSpeed(const Vector3 & vel) const;
/* --------------------------------------------------------------------------------------------
* Modify the relative speed of the managed vehicle entity.
*/
void AddRelSpeedEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the turn speed of the managed vehicle entity.
*/
const Vector3 & GetTurnSpeed() const;
/* --------------------------------------------------------------------------------------------
* Modify the turn speed of the managed vehicle entity.
*/
void SetTurnSpeed(const Vector3 & vel) const;
/* --------------------------------------------------------------------------------------------
* Modify the turn speed of the managed vehicle entity.
*/
void SetTurnSpeedEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Modify the turn speed of the managed vehicle entity.
*/
void AddTurnSpeed(const Vector3 & vel) const;
/* --------------------------------------------------------------------------------------------
* Modify the turn speed of the managed vehicle entity.
*/
void AddTurnSpeedEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the relative turn speed of the managed vehicle entity.
*/
const Vector3 & GetRelTurnSpeed() const;
/* --------------------------------------------------------------------------------------------
* Modify the relative turn speed of the managed vehicle entity.
*/
void SetRelTurnSpeed(const Vector3 & vel) const;
/* --------------------------------------------------------------------------------------------
* Modify the relative turn speed of the managed vehicle entity.
*/
void SetRelTurnSpeedEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Modify the relative turn speed of the managed vehicle entity.
*/
void AddRelTurnSpeed(const Vector3 & vel) const;
/* --------------------------------------------------------------------------------------------
* Modify the relative turn speed of the managed vehicle entity.
*/
void AddRelTurnSpeedEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the spawn position of the managed vehicle entity.
*/
const Vector4 & GetSpawnPosition() const;
/* --------------------------------------------------------------------------------------------
* Modify the spawn position of the managed vehicle entity.
*/
void SetSpawnPosition(const Vector4 & pos) const;
/* --------------------------------------------------------------------------------------------
* Modify the spawn position of the managed vehicle entity.
*/
void SetSpawnPositionEx(Float32 x, Float32 y, Float32 z, Float32 w) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the spawn rotation of the managed vehicle entity.
*/
const Quaternion & GetSpawnRotation() const;
/* --------------------------------------------------------------------------------------------
* Modify the spawn rotation of the managed vehicle entity.
*/
void SetSpawnRotation(const Quaternion & rot) const;
/* --------------------------------------------------------------------------------------------
* Modify the spawn rotation of the managed vehicle entity.
*/
void SetSpawnRotationEx(Float32 x, Float32 y, Float32 z, Float32 w) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the euler spawn rotation of the managed vehicle entity.
*/
const Vector3 & GetSpawnRotationEuler() const;
/* --------------------------------------------------------------------------------------------
* Modify the euler spawn rotation of the managed vehicle entity.
*/
void SetSpawnRotationEuler(const Vector3 & rot) const;
/* --------------------------------------------------------------------------------------------
* Modify the euler spawn rotation of the managed vehicle entity.
*/
void SetSpawnRotationEulerEx(Float32 x, Float32 y, Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the respawn timer of the managed vehicle entity.
*/
Uint32 GetRespawnTimer() const;
/* --------------------------------------------------------------------------------------------
* Modify the respawn timer of the managed vehicle entity.
*/
void SetRespawnTimer(Uint32 timer) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the health of the managed vehicle entity.
*/
Float32 GetHealth() const;
/* --------------------------------------------------------------------------------------------
* Modify the health of the managed vehicle entity.
*/
void SetHealth(Float32 amount) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the primary color of the managed vehicle entity.
*/
Int32 GetPrimaryColor() const;
/* --------------------------------------------------------------------------------------------
* Modify the primary color of the managed vehicle entity.
*/
void SetPrimaryColor(Int32 col) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the secondary color of the managed vehicle entity.
*/
Int32 GetSecondaryColor() const;
/* --------------------------------------------------------------------------------------------
* Modify the secondary color of the managed vehicle entity.
*/
void SetSecondaryColor(Int32 col) const;
/* --------------------------------------------------------------------------------------------
* Modify the primary and secondary colors of the managed vehicle entity.
*/
void SetColors(Int32 primary, Int32 secondary) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed vehicle entity is locked.
*/
bool GetLocked() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed vehicle entity is locked.
*/
void SetLocked(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the part status of the managed vehicle entity.
*/
Int32 GetPartStatus(Int32 part) const;
/* --------------------------------------------------------------------------------------------
* Modify the part status of the managed vehicle entity.
*/
void SetPartStatus(Int32 part, Int32 status) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the tyre status of the managed vehicle entity.
*/
Int32 GetTyreStatus(Int32 tyre) const;
/* --------------------------------------------------------------------------------------------
* Modify the tyre status of the managed vehicle entity.
*/
void SetTyreStatus(Int32 tyre, Int32 status) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the damage data of the managed vehicle entity.
*/
Uint32 GetDamageData() const;
/* --------------------------------------------------------------------------------------------
* Modify the damage data of the managed vehicle entity.
*/
void SetDamageData(Uint32 data) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed vehicle entity has alarm.
*/
bool GetAlarm() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed vehicle entity has alarm.
*/
void SetAlarm(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed vehicle entity has lights.
*/
bool GetLights() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed vehicle entity has lights.
*/
void SetLights(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the radio of the managed vehicle entity.
*/
Int32 GetRadio() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the radio of the managed vehicle entity.
*/
void SetRadio(Int32 radio) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed vehicle entity has radio locked.
*/
bool GetRadioLocked() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed vehicle entity has radio locked.
*/
void SetRadioLocked(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* See whether the managed vehicle entity is in ghost state.
*/
bool GetGhostState() const;
/* --------------------------------------------------------------------------------------------
* Set whether the managed vehicle entity is in ghost state.
*/
void SetGhostState(bool toggle) const;
/* --------------------------------------------------------------------------------------------
* Reset all the handling rules for the managed vehicle entity.
*/
void ResetHandling() const;
/* --------------------------------------------------------------------------------------------
* Reset the specified handling rule for the managed vehicle entity.
*/
void ResetHandling(Int32 rule) const;
/* --------------------------------------------------------------------------------------------
* See whether the specified handling ruleexists in the managed vehicle entity.
*/
bool ExistsHandling(Int32 rule) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the handling data of the managed vehicle entity.
*/
Float32 GetHandlingData(Int32 rule) const;
/* --------------------------------------------------------------------------------------------
* Modify the handling data of the managed vehicle entity.
*/
void SetHandlingData(Int32 rule, Float32 data) const;
/* --------------------------------------------------------------------------------------------
* Embark the specified player entity into the managed vehicle entity.
*/
void Embark(CPlayer & player) const;
/* --------------------------------------------------------------------------------------------
* Embark the specified player entity into the managed vehicle entity.
*/
void Embark(CPlayer & player, Int32 slot, bool allocate, bool warp) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the x axis of the managed vehicle entity.
*/
Float32 GetPosX() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the y axis of the managed vehicle entity.
*/
Float32 GetPosY() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the position on the z axis of the managed vehicle entity.
*/
Float32 GetPosZ() const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the x axis of the managed vehicle entity.
*/
void SetPosX(Float32 x) const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the y axis of the managed vehicle entity.
*/
void SetPosY(Float32 y) const;
/* --------------------------------------------------------------------------------------------
* Modify the position on the z axis of the managed vehicle entity.
*/
void SetPosZ(Float32 z) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Retrieve the rotation on the x axis of the managed vehicle entity.
*/
Float32 GetRotX() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the rotation on the y axis of the managed vehicle entity.
*/
Float32 GetRotY() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the rotation on the z axis of the managed vehicle entity.
*/
Float32 GetRotZ() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the rotation amount of the managed vehicle entity.
*/
Float32 GetRotW() const;
/* --------------------------------------------------------------------------------------------
* Modify the rotation on the x axis of the managed vehicle entity.
*/
void SetRotX(Float32 x) const;
/* --------------------------------------------------------------------------------------------
* Modify the rotation on the y axis of the managed vehicle entity.
*/
void SetRotY(Float32 y) const;
/* --------------------------------------------------------------------------------------------
* Modify the rotation on the z axis of the managed vehicle entity.
*/
void SetRotZ(Float32 z) const;
/* --------------------------------------------------------------------------------------------
* Modify the rotation amount of the managed vehicle entity.
*/
void SetRotW(Float32 w) const;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* Retrieve the euler rotation on the x axis of the managed vehicle entity.
*/
Float32 GetERotX() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the euler rotation on the y axis of the managed vehicle entity.
*/
Float32 GetERotY() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the euler rotation on the z axis of the managed vehicle entity.
*/
Float32 GetERotZ() const;
/* --------------------------------------------------------------------------------------------
* Modify the euler rotation on the x axis of the managed vehicle entity.
*/
void SetERotX(Float32 x) const;
/* --------------------------------------------------------------------------------------------
* Modify the euler rotation on the y axis of the managed vehicle entity.
*/
void SetERotY(Float32 y) const;
/* --------------------------------------------------------------------------------------------
* Modify the euler rotation on the z axis of the managed vehicle entity.
*/
void SetERotZ(Float32 z) const;
};

View File

@ -36,6 +36,16 @@ static HSQUIRRELVM GetSquirrelVM()
return NULL;
}
// ------------------------------------------------------------------------------------------------
static SQRESULT SqEx_LoadScript(const SQChar * filepath)
{
// Attempt to add the specified script to the load queue
if (_Core->LoadScript(filepath))
return SQ_OK; // The script as added or already existed
// The path was invalied or was unable to pool the script
return SQ_ERROR;
}
// ------------------------------------------------------------------------------------------------
static SQRESULT SqEx_GetSLongValue(HSQUIRRELVM vm, SQInteger idx, Int64 * num)
{
@ -210,11 +220,11 @@ void InitExports()
static HSQEXPORTS sqexports = &g_SqExports;
// Assign the functions that should be exported
g_SqExports.uStructSize = sizeof(SQEXPORTS);
g_SqExports.StructSize = sizeof(SQEXPORTS);
g_SqExports.GetSquirrelAPI = GetSquirrelAPI;
g_SqExports.GetSquirrelVM = GetSquirrelVM;
/*logging utilities*/
//logging utilities
g_SqExports.LogDbg = LogDbg;
g_SqExports.LogUsr = LogUsr;
g_SqExports.LogScs = LogScs;
@ -230,13 +240,16 @@ void InitExports()
g_SqExports.LogSErr = LogSErr;
g_SqExports.LogSFtl = LogSFtl;
/*long numbers*/
//script loading
g_SqExports.LoadScript = SqEx_LoadScript;
//long numbers
g_SqExports.GetSLongValue = SqEx_GetSLongValue;
g_SqExports.PushSLongObject = SqEx_PushSLongObject;
g_SqExports.GetULongValue = SqEx_GetULongValue;
g_SqExports.PushULongObject = SqEx_PushULongObject;
/*time utilities*/
//time utilities
g_SqExports.GetCurrentSysTime = GetCurrentSysTime;
g_SqExports.GetEpochTimeMicro = GetEpochTimeMicro;
g_SqExports.GetEpochTimeMilli = GetEpochTimeMilli;
@ -246,7 +259,7 @@ void InitExports()
// Export them to the server
_Func->ExportFunctions(_Info->nPluginId, (void **)(&sqexports), sizeof(SQEXPORTS));
/*vm*/
//vm
g_SqAPI.open = sq_open;
g_SqAPI.newthread = sq_newthread;
g_SqAPI.seterrorhandler = sq_seterrorhandler;
@ -267,14 +280,14 @@ void InitExports()
g_SqAPI.getvmstate = sq_getvmstate;
g_SqAPI.getversion = sq_getversion;
/*compiler*/
//compiler
g_SqAPI.compile = sq_compile;
g_SqAPI.compilebuffer = sq_compilebuffer;
g_SqAPI.enabledebuginfo = sq_enabledebuginfo;
g_SqAPI.notifyallexceptions = sq_notifyallexceptions;
g_SqAPI.setcompilererrorhandler = sq_setcompilererrorhandler;
/*stack operations*/
//stack operations
g_SqAPI.push = sq_push;
g_SqAPI.pop = sq_pop;
g_SqAPI.poptop = sq_poptop;
@ -285,7 +298,7 @@ void InitExports()
g_SqAPI.cmp = sq_cmp;
g_SqAPI.move = sq_move;
/*object creation handling*/
//object creation handling
g_SqAPI.newuserdata = sq_newuserdata;
g_SqAPI.newtable = sq_newtable;
g_SqAPI.newtableex = sq_newtableex;
@ -340,7 +353,7 @@ void InitExports()
g_SqAPI.getbyhandle = sq_getbyhandle;
g_SqAPI.setbyhandle = sq_setbyhandle;
/*object manipulation*/
//object manipulation
g_SqAPI.pushroottable = sq_pushroottable;
g_SqAPI.pushregistrytable = sq_pushregistrytable;
g_SqAPI.pushconsttable = sq_pushconsttable;
@ -369,7 +382,7 @@ void InitExports()
g_SqAPI.getweakrefval = sq_getweakrefval;
g_SqAPI.clear = sq_clear;
/*calls*/
//calls
g_SqAPI.call = sq_call;
g_SqAPI.resume = sq_resume;
g_SqAPI.getlocal = sq_getlocal;
@ -380,7 +393,7 @@ void InitExports()
g_SqAPI.reseterror = sq_reseterror;
g_SqAPI.getlasterror = sq_getlasterror;
/*raw object handling*/
//raw object handling
g_SqAPI.getstackobj = sq_getstackobj;
g_SqAPI.pushobject = sq_pushobject;
g_SqAPI.addref = sq_addref;
@ -395,35 +408,35 @@ void InitExports()
g_SqAPI.getobjtypetag = sq_getobjtypetag;
g_SqAPI.getvmrefcount = sq_getvmrefcount;
/*GC*/
//GC
g_SqAPI.collectgarbage = sq_collectgarbage;
g_SqAPI.resurrectunreachable = sq_resurrectunreachable;
/*serialization*/
//serialization
g_SqAPI.writeclosure = sq_writeclosure;
g_SqAPI.readclosure = sq_readclosure;
/*mem allocation*/
//mem allocation
g_SqAPI.malloc = sq_malloc;
g_SqAPI.realloc = sq_realloc;
g_SqAPI.free = sq_free;
/*debug*/
//debug
g_SqAPI.stackinfos = sq_stackinfos;
g_SqAPI.setdebughook = sq_setdebughook;
g_SqAPI.setnativedebughook = sq_setnativedebughook;
/*compiler helpers*/
//compiler helpers
g_SqAPI.loadfile = sqstd_loadfile;
g_SqAPI.dofile = sqstd_dofile;
g_SqAPI.writeclosuretofile = sqstd_writeclosuretofile;
/*blob*/
//blob
g_SqAPI.createblob = sqstd_createblob;
g_SqAPI.getblob = sqstd_getblob;
g_SqAPI.getblobsize = sqstd_getblobsize;
/*string*/
//string
g_SqAPI.format = sqstd_format;
}

239
source/Library/Crypt.cpp Normal file
View File

@ -0,0 +1,239 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Crypt.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdlib>
#include <cstring>
// ------------------------------------------------------------------------------------------------
#include <sqstdstring.h>
// ------------------------------------------------------------------------------------------------
#include <crc32.h>
#include <keccak.h>
#include <md5.h>
#include <sha1.h>
#include <sha256.h>
#include <sha3.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQInteger AES256::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("SqAES");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
AES256::AES256()
: m_Context(), m_Buffer()
{
aes256_done(&m_Context);
}
// ------------------------------------------------------------------------------------------------
AES256::AES256(CSStr key)
: m_Context(), m_Buffer()
{
Init(key);
}
// ------------------------------------------------------------------------------------------------
Int32 AES256::Cmp(const AES256 & o) const
{
return memcmp(m_Buffer, o.m_Buffer, sizeof(m_Buffer));
}
// ------------------------------------------------------------------------------------------------
CSStr AES256::ToString() const
{
return ToStrF("%s", m_Buffer);
}
// ------------------------------------------------------------------------------------------------
CSStr AES256::GetKey() const
{
return reinterpret_cast< CSStr >(m_Buffer);
}
// ------------------------------------------------------------------------------------------------
void AES256::Init(CSStr key)
{
// Clear current key, if any
aes256_done(&m_Context);
// Is the specified key empty?
if (!key || *key == 0)
return; // Leave the context with an empty key
// Obtain the specified key size
const Uint32 size = (strlen(key) * sizeof(SQChar));
// See if the key size is accepted
if (size > sizeof(m_Buffer))
SqThrowF("The specified key is out of bounds: %u > %u", size, sizeof(m_Buffer));
// Initialize the key buffer to 0
memset(m_Buffer, 0, sizeof(m_Buffer));
// Copy the key into the key buffer
memcpy(m_Buffer, key, size);
// Initialize the context with the specified key
aes256_init(&m_Context, m_Buffer);
}
// ------------------------------------------------------------------------------------------------
void AES256::Done()
{
aes256_done(&m_Context);
}
// ------------------------------------------------------------------------------------------------
String AES256::Encrypt(CSStr data)
{
// Is there any data to encrypt?
if (!data || *data == 0)
return String();
// Copy the data into an editable string
String str(data);
// Make sure that we have a size with a multiple of 16
if ((str.size() % 16) != 0)
str.resize(str.size() - (str.size() % 16) + 16);
// Encrypt in chunks of 16 characters
for (Uint32 n = 0; n < str.size(); n += 16)
aes256_encrypt_ecb(&m_Context, reinterpret_cast< Uint8 * >(&str[n]));
// Return ownership of the encrypted string
return std::move(str);
}
// ------------------------------------------------------------------------------------------------
String AES256::Decrypt(CSStr data)
{
// Is there any data to decrypt?
if (!data || *data == 0)
return String();
// Copy the data into an editable string
String str(data);
// Make sure that we have a size with a multiple of 16
if ((str.size() % 16) != 0)
str.resize(str.size() - (str.size() % 16) + 16);
// Decrypt inc chunks of 16 characters
for (Uint32 n = 0; n < str.size(); n += 16)
aes256_decrypt_ecb(&m_Context, reinterpret_cast< Uint8 * >(&str[n]));
// Remove null characters in case the string was not a multiple of 16 when encrypted
while (!str.empty() && str.back() == 0)
str.pop_back();
// Return ownership of the encrypted string
return std::move(str);
}
/* ------------------------------------------------------------------------------------------------
* Utility to avoid creating encoder instances for each call.
*/
template < class T > struct BaseHash
{
static T Algo;
};
// ------------------------------------------------------------------------------------------------
template < class T > T BaseHash< T >::Algo;
/* ------------------------------------------------------------------------------------------------
* Hash the specified value or the result of a formatted string.
*/
template < class T > static SQInteger HashF(HSQUIRRELVM vm)
{
const Int32 top = sq_gettop(vm);
// Was the hash value specified?
if (top <= 1)
return sq_throwerror(vm, "Missing hash value");
// Do we have enough values to call the format function?
else if (top > 2)
{
SStr val = NULL;
SQInteger len = 0;
// Attempt to generate the specified string format
SQRESULT ret = sqstd_format(vm, 2, &len, &val);
// Did the format failed?
if (SQ_FAILED(ret))
return ret; // Propagate the exception
// Hash the resulted string
String str(BaseHash< T >::Algo(val));
// Push the string on the stack
sq_pushstring(vm, str.data(), str.size());
}
else
{
// Attempt to retrieve the value from the stack as a string
Var< CSStr > val(vm, 2);
// See if the obtained value is a valid string
if (!val.value)
return sq_throwerror(vm, "Unable to retrieve the value");
// Hash the resulted string
String str(BaseHash< T >::Algo(val.value));
// Push the string on the stack
sq_pushstring(vm, str.data(), str.size());
}
// At this point we have a valid string on the stack
return 1;
}
// ================================================================================================
template < class T > static void RegisterWrapper(Table & hashns, CCStr cname)
{
typedef HashWrapper< T > Hash;
hashns.Bind(cname, Class< Hash >(hashns.GetVM(), cname)
/* Constructors */
.Ctor()
/* Metamethods */
.Func(_SC("_tostring"), &Hash::ToString)
/* Properties */
.Prop(_SC("Hash"), &Hash::GetHash)
/* Functions */
.Func(_SC("Reset"), &Hash::Reset)
.Func(_SC("Compute"), &Hash::Compute)
.Func(_SC("GetHash"), &Hash::GetHash)
.Func(_SC("Add"), &Hash::AddStr)
.Func(_SC("AddStr"), &Hash::AddStr)
);
}
// ================================================================================================
void Register_Crypt(HSQUIRRELVM vm)
{
Table hashns(vm);
RegisterWrapper< CRC32 >(hashns, _SC("CRC32"));
RegisterWrapper< Keccak >(hashns, _SC("Keccak"));
RegisterWrapper< MD5 >(hashns, _SC("MD5"));
RegisterWrapper< SHA1 >(hashns, _SC("SHA1"));
RegisterWrapper< SHA256 >(hashns, _SC("SHA256"));
RegisterWrapper< SHA3 >(hashns, _SC("SHA3"));
hashns.SquirrelFunc(_SC("GetCRC32"), &HashF< CRC32 >);
hashns.SquirrelFunc(_SC("GetKeccak"), &HashF< Keccak >);
hashns.SquirrelFunc(_SC("GetMD5"), &HashF< MD5 >);
hashns.SquirrelFunc(_SC("GetSHA1"), &HashF< SHA1 >);
hashns.SquirrelFunc(_SC("GetSHA256"), &HashF< SHA256 >);
hashns.SquirrelFunc(_SC("GetSHA3"), &HashF< SHA3 >);
RootTable(vm).Bind(_SC("SqHash"), hashns);
RootTable(vm).Bind("SqAES256", Class< AES256 >(vm, "SqAES256")
/* Constructors */
.Ctor()
.Ctor< CSStr >()
/* Metamethods */
.Func(_SC("_cmp"), &AES256::Cmp)
.SquirrelFunc(_SC("_typename"), &AES256::Typename)
.Func(_SC("_tostring"), &AES256::ToString)
/* Properties */
.Prop(_SC("Key"), &AES256::GetKey)
/* Functions */
.Func(_SC("Init"), &AES256::Init)
.Func(_SC("Done"), &AES256::Done)
.Func(_SC("Encrypt"), &AES256::Encrypt)
.Func(_SC("Decrypt"), &AES256::Decrypt)
);
}
} // Namespace:: SqMod

200
source/Library/Crypt.hpp Normal file
View File

@ -0,0 +1,200 @@
#ifndef _LIBRARY_CRYPT_HPP_
#define _LIBRARY_CRYPT_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
#include <aes256.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Simple class to maintain the state of an encoder.
*/
template < class T > class HashWrapper
{
public:
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
HashWrapper()
: m_Encoder()
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy operator.
*/
HashWrapper(const HashWrapper & o)
: m_Encoder(o.m_Encoder)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~HashWrapper()
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
HashWrapper & operator = (const HashWrapper & o)
{
m_Encoder = o.m_Encoder;
return *this;
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
String ToString()
{
return m_Encoder.getHash();
}
/* --------------------------------------------------------------------------------------------
* Reset the encoder state.
*/
void Reset()
{
m_Encoder.reset();
}
/* --------------------------------------------------------------------------------------------
* Compute the hash of the specified string.
*/
String Compute(const String & str)
{
return m_Encoder(str);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the hash value of the data hashed so far.
*/
String GetHash()
{
return m_Encoder.getHash();
}
/* --------------------------------------------------------------------------------------------
* Add a string value to be hashed.
*/
void AddStr(const String & str)
{
m_Encoder.add(str.data(), str.length() * sizeof(String::value_type));
}
private:
/* --------------------------------------------------------------------------------------------
* The managed encoder state.
*/
T m_Encoder;
};
/* ------------------------------------------------------------------------------------------------
* Simple wrapper around a the AES encryption context.
*/
class AES256
{
public:
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
AES256();
/* --------------------------------------------------------------------------------------------
* Construct with an explicit key.
*/
AES256(CSStr key);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
AES256(const AES256 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
AES256(AES256 && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~AES256() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
AES256 & operator = (const AES256 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
AES256 & operator = (AES256 && o) = default;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const AES256 & o) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Retrieve the associated key.
*/
CSStr GetKey() const;
/* --------------------------------------------------------------------------------------------
* Initialize the context key.
*/
void Init(CSStr key);
/* --------------------------------------------------------------------------------------------
* Reset the associated context.
*/
void Done();
/* --------------------------------------------------------------------------------------------
* Encrypt the specified string.
*/
String Encrypt(CSStr data);
/* --------------------------------------------------------------------------------------------
* Decrypt the specified string.
*/
String Decrypt(CSStr data);
private:
/* --------------------------------------------------------------------------------------------
* The managed encryption context.
*/
aes256_context m_Context;
/* --------------------------------------------------------------------------------------------
* The key used to encrypt data.
*/
Uint8 m_Buffer[32]{0};
};
} // Namespace:: SqMod
#endif // _LIBRARY_CRYPT_HPP_

View File

@ -1,103 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Hashing.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
#include <sqstdstring.h>
// ------------------------------------------------------------------------------------------------
#include <crc32.h>
#include <keccak.h>
#include <md5.h>
#include <sha1.h>
#include <sha256.h>
#include <sha3.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* ...
*/
template < class T > struct BaseHash
{
static T Algo;
};
// ------------------------------------------------------------------------------------------------
template < class T > T BaseHash< T >::Algo;
// ------------------------------------------------------------------------------------------------
template < class T > static SQInteger HashF(HSQUIRRELVM vm)
{
const Int32 top = sq_gettop(vm);
Object ret;
if (top <= 1)
SqThrow("Missing the hash string");
else if (top == 2 && ((sq_gettype(vm, -1) == OT_STRING) || !SQ_FAILED(sq_tostring(vm, -1))))
{
CCStr str = 0;
if (SQ_FAILED(sq_getstring(vm, -1, &str)))
SqThrow("Unable to retrieve the string");
else
ret = MakeObject(vm, BaseHash< T >::Algo(str));
sq_settop(vm, top);
}
else if (top > 2)
{
CStr str = NULL;
SQInteger len = 0;
if (SQ_FAILED(sqstd_format(vm, 2, &len, &str)))
SqThrow("Unable to generate the string [%s]", Error::Message(vm).c_str());
else
ret = MakeObject(vm, BaseHash< T >::Algo(str));
}
else
SqThrow("Unable to extract the hash string");
Var< Object >::push(vm, ret);
return 1;
}
// ================================================================================================
template < class T > static void RegisterWrapper(Table & hashns, CCStr cname)
{
typedef HashWrapper< T > Hash;
hashns.Bind(cname, Class< Hash >(hashns.GetVM(), cname)
/* Constructors */
.Ctor()
/* Metamethods */
.Func(_SC("_tostring"), &Hash::ToString)
/* Properties */
.Prop(_SC("Hash"), &Hash::GetHash)
/* Functions */
.Func(_SC("Reset"), &Hash::Reset)
.Func(_SC("Compute"), &Hash::Compute)
.Func(_SC("GetHash"), &Hash::GetHash)
.Func(_SC("Add"), &Hash::AddStr)
.Func(_SC("AddStr"), &Hash::AddStr)
);
}
// ================================================================================================
void Register_Hash(HSQUIRRELVM vm)
{
Table hashns(vm);
RegisterWrapper< CRC32 >(hashns, _SC("CRC32"));
RegisterWrapper< Keccak >(hashns, _SC("Keccak"));
RegisterWrapper< MD5 >(hashns, _SC("MD5"));
RegisterWrapper< SHA1 >(hashns, _SC("SHA1"));
RegisterWrapper< SHA256 >(hashns, _SC("SHA256"));
RegisterWrapper< SHA3 >(hashns, _SC("SHA3"));
hashns.SquirrelFunc(_SC("GetCRC32"), &HashF< CRC32 >);
hashns.SquirrelFunc(_SC("GetKeccak"), &HashF< Keccak >);
hashns.SquirrelFunc(_SC("GetMD5"), &HashF< MD5 >);
hashns.SquirrelFunc(_SC("GetSHA1"), &HashF< SHA1 >);
hashns.SquirrelFunc(_SC("GetSHA256"), &HashF< SHA256 >);
hashns.SquirrelFunc(_SC("GetSHA3"), &HashF< SHA3 >);
RootTable(vm).Bind(_SC("SqHash"), hashns);
}
} // Namespace:: SqMod

View File

@ -1,58 +0,0 @@
#ifndef _LIBRARY_HASHING_HPP_
#define _LIBRARY_HASHING_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
template < class T > class HashWrapper
{
public:
// --------------------------------------------------------------------------------------------
HashWrapper()
: m_Encoder()
{
}
// --------------------------------------------------------------------------------------------
HashWrapper(const HashWrapper & o)
: m_Encoder(o.m_Encoder)
{
}
// --------------------------------------------------------------------------------------------
~HashWrapper()
{
}
// --------------------------------------------------------------------------------------------
HashWrapper & operator = (const HashWrapper & o)
{
m_Encoder = o.m_Encoder;
return *this;
}
// --------------------------------------------------------------------------------------------
String ToString() { return m_Encoder.getHash(); }
void Reset() { m_Encoder.reset(); }
String Compute(const String & str) { return m_Encoder(str); }
String GetHash() { return m_Encoder.getHash(); }
void AddStr(const String & str)
{ m_Encoder.add(str.data(), str.length() * sizeof(String::value_type)); }
private:
// --------------------------------------------------------------------------------------------
T m_Encoder;
};
} // Namespace:: SqMod
#endif // _LIBRARY_HASHING_HPP_

View File

@ -4,11 +4,12 @@
#include "Base/Buffer.hpp"
// ------------------------------------------------------------------------------------------------
#include <RandomLib/Random.hpp>
#include <ctime>
#include <memory>
#include <random>
#include <cstdlib>
// ------------------------------------------------------------------------------------------------
#include <time.h>
#include <stdlib.h>
#ifdef SQMOD_OS_WINDOWS
#include <process.h>
#else
@ -17,10 +18,45 @@
#endif
// ------------------------------------------------------------------------------------------------
RandomLib::Random g_RGen;
namespace SqMod {
// ------------------------------------------------------------------------------------------------
namespace SqMod {
static std::unique_ptr< std::mt19937 > RG32_MT19937 =
std::unique_ptr< std::mt19937 >(new std::mt19937(GenerateSeed()));
static std::unique_ptr< std::mt19937_64 > RG64_MT19937 =
std::unique_ptr< std::mt19937_64 >(new std::mt19937_64(GenerateSeed()));
// ------------------------------------------------------------------------------------------------
static std::uniform_int_distribution< Int8 > Int8_Dist(std::numeric_limits< Int8 >::min(),
std::numeric_limits< Int8 >::max());
static std::uniform_int_distribution< Uint8 > Uint8_Dist(std::numeric_limits< Uint8 >::min(),
std::numeric_limits< Uint8 >::max());
static std::uniform_int_distribution< Int16 > Int16_Dist(std::numeric_limits< Int16 >::min(),
std::numeric_limits< Int16 >::max());
static std::uniform_int_distribution< Uint16 > Uint16_Dist(std::numeric_limits< Uint16 >::min(),
std::numeric_limits< Uint16 >::max());
static std::uniform_int_distribution< Int32 > Int32_Dist(std::numeric_limits< Int32 >::min(),
std::numeric_limits< Int32 >::max());
static std::uniform_int_distribution< Uint32 > Uint32_Dist(std::numeric_limits< Uint32 >::min(),
std::numeric_limits< Uint32 >::max());
static std::uniform_int_distribution< Int64 > Int64_Dist(std::numeric_limits< Int64 >::min(),
std::numeric_limits< Int64 >::max());
static std::uniform_int_distribution< Uint64 > Uint64_Dist(std::numeric_limits< Uint64 >::min(),
std::numeric_limits< Uint64 >::max());
static std::uniform_real_distribution<Float32> Float32_Dist(std::numeric_limits< Float32 >::min(),
std::numeric_limits< Float32 >::max());
static std::uniform_real_distribution<Float64> Float64_Dist(std::numeric_limits< Float64 >::min(),
std::numeric_limits< Float64 >::max());
// ------------------------------------------------------------------------------------------------
static std::uniform_int_distribution< String::value_type >
String_Dist(std::numeric_limits< String::value_type >::min(),
std::numeric_limits< String::value_type >::max());
// ------------------------------------------------------------------------------------------------
SizeT GenerateSeed()
@ -49,309 +85,296 @@ SizeT GenerateSeed()
// ------------------------------------------------------------------------------------------------
void ReseedRandom()
{
g_RGen.Reseed();
RG32_MT19937.reset(new std::mt19937(GenerateSeed()));
RG64_MT19937.reset(new std::mt19937_64(GenerateSeed()));
}
void ReseedRandom(Uint32 n)
{
g_RGen.Reseed(n);
RG32_MT19937.reset(new std::mt19937(n));
RG64_MT19937.reset(new std::mt19937_64(n));
}
// ------------------------------------------------------------------------------------------------
void ReseedRandom32()
{
RG32_MT19937.reset(new std::mt19937(GenerateSeed()));
}
void ReseedRandom32(Uint32 n)
{
RG32_MT19937.reset(new std::mt19937(n));
}
// ------------------------------------------------------------------------------------------------
void ReseedRandom64()
{
RG64_MT19937.reset(new std::mt19937_64(GenerateSeed()));
}
void ReseedRandom64(Uint32 n)
{
RG64_MT19937.reset(new std::mt19937_64(n));
}
// ------------------------------------------------------------------------------------------------
Int8 GetRandomInt8()
{
return g_RGen.Integer< Int8 >();
return Int8_Dist(*RG32_MT19937);
}
Int8 GetRandomInt8(Int8 n)
{
return g_RGen.Integer< Int8 >(n);
return std::uniform_int_distribution< Int8 >(0, n)(*RG32_MT19937);
}
Int8 GetRandomInt8(Int8 m, Int8 n)
{
return g_RGen.IntegerC< Int8 >(m, n);
return std::uniform_int_distribution< Int8 >(m, n)(*RG32_MT19937);
}
// ------------------------------------------------------------------------------------------------
Uint8 GetRandomUint8()
{
return g_RGen.Integer< Uint8 >();
return Uint8_Dist(*RG32_MT19937);
}
Uint8 GetRandomUint8(Uint8 n)
{
return g_RGen.Integer< Uint8 >(n);
return std::uniform_int_distribution< Uint8 >(0, n)(*RG32_MT19937);
}
Uint8 GetRandomUint8(Uint8 m, Uint8 n)
{
return g_RGen.IntegerC< Uint8 >(m, n);
return std::uniform_int_distribution< Uint8 >(m, n)(*RG32_MT19937);
}
// ------------------------------------------------------------------------------------------------
Int16 GetRandomInt16()
{
return g_RGen.Integer< Int16 >();
return Int16_Dist(*RG32_MT19937);
}
Int16 GetRandomInt16(Int16 n)
{
return g_RGen.Integer< Int16 >(n);
return std::uniform_int_distribution< Int16 >(0, n)(*RG32_MT19937);
}
Int16 GetRandomInt16(Int16 m, Int16 n)
{
return g_RGen.IntegerC< Int16 >(m, n);
return std::uniform_int_distribution< Int16 >(m, n)(*RG32_MT19937);
}
// ------------------------------------------------------------------------------------------------
Uint16 GetRandomUint16()
{
return g_RGen.Integer< Uint16 >();
return Uint16_Dist(*RG32_MT19937);
}
Uint16 GetRandomUint16(Uint16 n)
{
return g_RGen.Integer< Uint16 >(n);
return std::uniform_int_distribution< Uint16 >(0, n)(*RG32_MT19937);
}
Uint16 GetRandomUint16(Uint16 m, Uint16 n)
{
return g_RGen.IntegerC< Uint16 >(m, n);
return std::uniform_int_distribution< Uint16 >(m, n)(*RG32_MT19937);
}
// ------------------------------------------------------------------------------------------------
Int32 GetRandomInt32()
{
return g_RGen.Integer< Int32 >();
return Int32_Dist(*RG32_MT19937);
}
Int32 GetRandomInt32(Int32 n)
{
return g_RGen.Integer< Int32 >(n);
return std::uniform_int_distribution< Int32 >(0, n)(*RG32_MT19937);
}
Int32 GetRandomInt32(Int32 m, Int32 n)
{
return g_RGen.IntegerC< Int32 >(m, n);
return std::uniform_int_distribution< Int32 >(m, n)(*RG32_MT19937);
}
// ------------------------------------------------------------------------------------------------
Uint32 GetRandomUint32()
{
return g_RGen.Integer< Uint32 >();
return Int32_Dist(*RG32_MT19937);
}
Uint32 GetRandomUint32(Uint32 n)
{
return g_RGen.Integer< Uint32 >(n);
return std::uniform_int_distribution< Int32 >(0, n)(*RG32_MT19937);
}
Uint32 GetRandomUint32(Uint32 m, Uint32 n)
{
return g_RGen.IntegerC< Uint32 >(m, n);
return std::uniform_int_distribution< Int32 >(m, n)(*RG32_MT19937);
}
// ------------------------------------------------------------------------------------------------
Int64 GetRandomInt64()
{
return g_RGen.Integer< Int64 >();
return Int64_Dist(*RG64_MT19937);
}
Int64 GetRandomInt64(Int64 n)
{
return g_RGen.Integer< Int64 >(n);
return std::uniform_int_distribution< Int64 >(0, n)(*RG64_MT19937);
}
Int64 GetRandomInt64(Int64 m, Int64 n)
{
return g_RGen.IntegerC< Int64 >(m, n);
return std::uniform_int_distribution< Int64 >(m, n)(*RG64_MT19937);
}
// ------------------------------------------------------------------------------------------------
Uint64 GetRandomUint64()
{
return g_RGen.Integer< Uint64 >();
return Uint64_Dist(*RG64_MT19937);
}
Uint64 GetRandomUint64(Uint64 n)
{
return g_RGen.Integer< Uint64 >(n);
return std::uniform_int_distribution< Uint64 >(0, n)(*RG64_MT19937);
}
Uint64 GetRandomUint64(Uint64 m, Uint64 n)
{
return g_RGen.IntegerC< Uint64 >(m, n);
return std::uniform_int_distribution< Uint64 >(m, n)(*RG64_MT19937);
}
// ------------------------------------------------------------------------------------------------
Float32 GetRandomFloat32()
{
static const Float32 m = NumLimit<Int32>::Min, n = NumLimit<Int32>::Max;
return (n - m) * (Float32(g_RGen.IntegerC< Int16 >(0, NumLimit< Int16 >::Max)) / Float32(RAND_MAX)) + m;
return Float32_Dist(*RG32_MT19937);
}
Float32 GetRandomFloat32(Float32 n)
{
return Float32(g_RGen.IntegerC< Int16 >(0, NumLimit< Int16 >::Max)) / Float32(RAND_MAX/n);
return std::uniform_real_distribution< Float32 >(0, n)(*RG32_MT19937);
}
Float32 GetRandomFloat32(Float32 m, Float32 n)
{
return (n - m) * (Float32(g_RGen.IntegerC< Int16 >(0, NumLimit< Int16 >::Max)) / Float32(RAND_MAX)) + m;
return std::uniform_real_distribution< Float32 >(m, n)(*RG32_MT19937);
}
// ------------------------------------------------------------------------------------------------
Float64 GetRandomFloat64()
{
static const Float64 m = NumLimit<Int32>::Min, n = NumLimit<Int32>::Max;
return (n - m) * (Float64(g_RGen.IntegerC< Int16 >(0, NumLimit< Int16 >::Max)) / Float64(RAND_MAX)) + m;
return Float64_Dist(*RG64_MT19937);
}
Float64 GetRandomFloat64(Float64 n)
{
return Float64(g_RGen.IntegerC< Int16 >(0, NumLimit< Int16 >::Max)) / Float64(RAND_MAX/n);
return std::uniform_real_distribution< Float64 >(0, n)(*RG64_MT19937);
}
Float64 GetRandomFloat64(Float64 m, Float64 n)
{
return (n - m) * (Float64(g_RGen.IntegerC< Int16 >(0, NumLimit< Int16 >::Max)) / Float64(RAND_MAX)) + m;
return std::uniform_real_distribution< Float64 >(m, n)(*RG64_MT19937);
}
// ------------------------------------------------------------------------------------------------
void GetRandomString(String & str, String::size_type len)
{
// Reserve the requested size + the null terminator
str.reserve(len+1);
// Resize to the requested size and fill with 0
str.resize(len);
for (String::iterator itr = str.begin(); itr != str.end(); ++itr)
{
*itr = g_RGen.Integer< String::value_type >();
}
// Generate the requested amount of characters
for (auto & c : str)
c = String_Dist(*RG32_MT19937);
// Append the null terminator
str.push_back(0);
}
void GetRandomString(String & str, String::size_type len, String::value_type n)
{
// Reserve the requested size + the null terminator
str.reserve(len+1);
// Resize to the requested size and fill with 0
str.resize(len);
for (String::iterator itr = str.begin(); itr != str.end(); ++itr)
{
*itr = g_RGen.Integer< String::value_type >(n);
}
// Create the custom distribution
std::uniform_int_distribution< String::value_type > dist(1, n);
// Generate the requested amount of characters
for (auto & c : str)
c = dist(*RG32_MT19937);
// Append the null terminator
str.push_back(0);
}
void GetRandomString(String & str, String::size_type len, String::value_type m, String::value_type n)
{
// Reserve the requested size + the null terminator
str.reserve(len+1);
// Resize to the requested size and fill with 0
str.resize(len);
for (String::iterator itr = str.begin(); itr != str.end(); ++itr)
{
*itr = g_RGen.IntegerC< String::value_type >(m, n);
}
// Create the custom distribution
std::uniform_int_distribution< String::value_type > dist(m, n);
// Generate the requested amount of characters
for (auto & c : str)
c = dist(*RG32_MT19937);
// Append the null terminator
str.push_back(0);
}
// ------------------------------------------------------------------------------------------------
bool GetRandomBool()
{
return g_RGen.Boolean();
return std::bernoulli_distribution()(*RG32_MT19937);
}
bool GetRandomBool(SQFloat p)
{
return std::bernoulli_distribution(p)(*RG32_MT19937);
}
// ------------------------------------------------------------------------------------------------
template < typename T > static T RandomValue()
{
return RandomVal< T >::Get();
}
template < typename T > static T RandomValue(T n)
{
return RandomVal< T >::Get(n);
}
template < typename T > static T RandomValue(T m, T n)
{
return RandomVal< T >::Get(m, n);
}
// ------------------------------------------------------------------------------------------------
static CSStr RandomString(Int32 len)
static String RandomString(Int32 len)
{
// Is there anything to generate?
if (len <= 0)
{
return _SC("");
}
Buffer b(len+1);
SStr s = b.Get< SQChar >();
for (Int32 n = 0; n <= len; ++n, ++s)
{
*s = g_RGen.Integer< SQChar >();
}
b.At< SQChar >(len) = 0;
return b.Get< SQChar >();
// Prepare the string buffer
String str;
// Request the random fill
GetRandomString(str, len);
// Return ownership of the string
return std::move(str);
}
// ------------------------------------------------------------------------------------------------
static CSStr RandomString(Int32 len, SQChar n)
static String RandomString(Int32 len, SQChar n)
{
// Is there anything to generate?
if (len <= 0)
{
return _SC("");
}
Buffer b(len+1);
SStr s = b.Get< SQChar >();
for (Int32 i = 0; i <= len; ++i, ++s)
{
*s = g_RGen.Integer< SQChar >(n);
}
b.At< SQChar >(len) = 0;
return b.Get< SQChar >();
// Prepare the string buffer
String str;
// Request the random fill
GetRandomString(str, len, n);
// Return ownership of the string
return std::move(str);
}
// ------------------------------------------------------------------------------------------------
static CSStr RandomString(Int32 len, SQChar m, SQChar n)
static String RandomString(Int32 len, SQChar m, SQChar n)
{
// Is there anything to generate?
if (len <= 0)
{
return _SC("");
}
Buffer b(len+1);
SStr s = b.Get< SQChar >();
for (Int32 i = 0; i <= len; ++i, ++s)
{
*s = g_RGen.IntegerC< SQChar >(m, n);
}
b.At< SQChar >(len) = 0;
return b.Get< SQChar >();
}
// ------------------------------------------------------------------------------------------------
static bool RandomProbI(SQInteger p)
{
return g_RGen.Prob(p);
}
static bool RandomProbI(SQInteger m, SQInteger n)
{
return g_RGen.Prob(m, n);
}
// ------------------------------------------------------------------------------------------------
static bool RandomProbF(SQFloat p)
{
return g_RGen.Prob(p);
}
static bool RandomProbF(SQFloat m, SQFloat n)
{
return g_RGen.Prob(m, n);
// Prepare the string buffer
String str;
// Request the random fill
GetRandomString(str, len, m, n);
// Return ownership of the string
return std::move(str);
}
// ------------------------------------------------------------------------------------------------
@ -361,20 +384,36 @@ void Register_Random(HSQUIRRELVM vm)
.Func(_SC("GenSeed"), &GenerateSeed)
.Overload< void (*)(void) >(_SC("Reseed"), &ReseedRandom)
.Overload< void (*)(Uint32) >(_SC("Reseed"), &ReseedRandom)
.Overload< SQInteger (*)(void) >(_SC("Integer"), &RandomValue< SQInteger >)
.Overload< SQInteger (*)(SQInteger) >(_SC("Integer"), &RandomValue< SQInteger >)
.Overload< SQInteger (*)(SQInteger, SQInteger) >(_SC("Integer"), &RandomValue< SQInteger >)
.Overload< SQFloat (*)(void) >(_SC("Float"), &RandomValue< SQFloat >)
.Overload< SQFloat (*)(SQFloat) >(_SC("Float"), &RandomValue< SQFloat >)
.Overload< SQFloat (*)(SQFloat, SQFloat) >(_SC("Float"), &RandomValue< SQFloat >)
.Overload< CSStr (*)(Int32) >(_SC("String"), &RandomString)
.Overload< CSStr (*)(Int32, SQChar) >(_SC("String"), &RandomString)
.Overload< CSStr (*)(Int32, SQChar, SQChar) >(_SC("String"), &RandomString)
.Func(_SC("Bool"), &GetRandomBool)
.Overload< bool (*)(SQInteger) >(_SC("ProbI"), &RandomProbI)
.Overload< bool (*)(SQInteger, SQInteger) >(_SC("ProbI"), &RandomProbI)
.Overload< bool (*)(SQFloat) >(_SC("ProbF"), &RandomProbF)
.Overload< bool (*)(SQFloat, SQFloat) >(_SC("ProbF"), &RandomProbF)
.Overload< void (*)(void) >(_SC("Reseed32"), &ReseedRandom32)
.Overload< void (*)(Uint32) >(_SC("Reseed32"), &ReseedRandom32)
.Overload< void (*)(void) >(_SC("Reseed64"), &ReseedRandom64)
.Overload< void (*)(Uint32) >(_SC("Reseed64"), &ReseedRandom64)
#ifdef _SQ64
.Overload< SQInteger (*)(void) >(_SC("Integer"), &GetRandomInt64)
.Overload< SQInteger (*)(SQInteger) >(_SC("Integer"), &GetRandomInt64)
.Overload< SQInteger (*)(SQInteger, SQInteger) >(_SC("Integer"), &GetRandomInt64)
#else
.Overload< SQInteger (*)(void) >(_SC("Integer"), &GetRandomInt32)
.Overload< SQInteger (*)(SQInteger) >(_SC("Integer"), &GetRandomInt32)
.Overload< SQInteger (*)(SQInteger, SQInteger) >(_SC("Integer"), &GetRandomInt32)
#endif // _SQ64
#ifdef SQUSEDOUBLE
.Overload< SQFloat (*)(void) >(_SC("Float"), &GetRandomFloat64)
.Overload< SQFloat (*)(SQFloat) >(_SC("Float"), &GetRandomFloat64)
.Overload< SQFloat (*)(SQFloat, SQFloat) >(_SC("Float"), &GetRandomFloat64)
#else
.Overload< SQFloat (*)(void) >(_SC("Float"), &GetRandomFloat32)
.Overload< SQFloat (*)(SQFloat) >(_SC("Float"), &GetRandomFloat32)
.Overload< SQFloat (*)(SQFloat, SQFloat) >(_SC("Float"), &GetRandomFloat32)
#endif // SQUSEDOUBLE
.Overload< String (*)(Int32) >(_SC("String"), &RandomString)
.Overload< String (*)(Int32, SQChar) >(_SC("String"), &RandomString)
.Overload< String (*)(Int32, SQChar, SQChar) >(_SC("String"), &RandomString)
.Overload< bool (*)(void) >(_SC("Bool"), &GetRandomBool)
.Overload< bool (*)(SQFloat) >(_SC("Bool"), &GetRandomBool)
);
}

View File

@ -7,10 +7,10 @@
#include <sqstdstring.h>
// ------------------------------------------------------------------------------------------------
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// ------------------------------------------------------------------------------------------------
#include <algorithm>
@ -21,137 +21,117 @@ namespace SqMod {
// ------------------------------------------------------------------------------------------------
CSStr LeftStr(CSStr t, SQChar f, Uint32 w)
{
// Allocate a buffer with the requested width
Buffer b(w * sizeof(SQChar));
Uint32 n = !t ? 0 : strlen(t);
// Populate the entire buffer with the fill character
memset(b.Data(), f, w * sizeof(SQChar));
// Is the specified width valid?
if (w >= b.Size< SQChar >())
{
SqThrow("Invalid width specified: %d > %d", w, b.Size< SQChar >());
w = 0;
}
if (!w)
return _SC("");
// Allocate a buffer with the requested width
Buffer b(w);
// Is the specified string valid?
else if (n == 0)
{
SqThrow("Invalid string length: %d < 0", n);
}
// Is the specified string within width range?
else if (n > w)
{
SqThrow("String is out of bounds: %d > %d", n, w);
}
// Insert the specified string
if (!t || *t == 0)
// Insert only the fill character
memset(b.Data(), f, w);
else
{
// Calculate the string length
const Uint32 n = strlen(t);
// Insert only the fill character first
memset(b.Data(), f, w);
// Overwrite with the specified string
strncpy(b.Data(), t, n);
}
// End the resulted string
b.At< SQChar >(w) = 0;
b.At(w) = 0;
// Return the resulted string
return b.Get< SQChar >();
}
CSStr LeftStr(CSStr t, SQChar f, Uint32 w, Uint32 o)
{
// Allocate a buffer with the requested width
Buffer b(w * sizeof(SQChar));
Uint32 n = !t ? 0 : strlen(t);
// Populate the entire buffer with the fill character
memset(b.Data(), f, w * sizeof(SQChar));
// Is the specified width valid?
if (w >= b.Size< SQChar >())
{
SqThrow("Invalid width specified: %d > %d", w, b.Size< SQChar >());
w = 0;
}
if (!w)
return _SC("");
// Is the specified offset within width range?
else if (o > w)
SqThrowF("Offset is out of bounds");
// Allocate a buffer with the requested width
Buffer b(w);
// Is the specified string valid?
else if (n == 0)
{
SqThrow("Invalid string length: %d < 0", n);
}
// Is the specified string within width range?
else if ((o+n) > w)
{
SqThrow("String is out of bounds: (%d+%d) > %d", o, n, w);
}
// Insert the specified string
if (!t || *t == 0)
// Insert only the fill character
memset(b.Data(), f, w);
else
{
strncpy(b.Get< SQChar >() + o, t, n);
// Clculate the string length
const Uint32 n = strlen(t);
// Insert the fill character first
memset(b.Data(), f, w);
// Overwrite with the specified string
strncpy(b.Data() + o, t, w - n);
}
// End the resulted string
b.At< SQChar >(w) = 0;
b.At(w) = 0;
// Return the resulted string
return b.Get< SQChar >();
return b.Get();
}
// ------------------------------------------------------------------------------------------------
CSStr RightStr(CSStr t, SQChar f, Uint32 w)
{
// Allocate a buffer with the requested width
Buffer b(w * sizeof(SQChar));
Uint32 n = !t ? 0 : strlen(t);
// Populate the entire buffer with the fill character
memset(b.Data(), f, w * sizeof(SQChar));
// Is the specified width valid?
if (w >= b.Size< SQChar >())
{
SqThrow("Invalid width specified: %d > %d", w, b.Size< SQChar >());
w = 0;
}
if (!w)
return _SC("");
// Allocate a buffer with the requested width
Buffer b(w);
// Is the specified string valid?
else if (n == 0)
{
SqThrow("Invalid string length: %d < 0", n);
}
// Is the specified string within width range?
else if (n > w)
{
SqThrow("String is out of bounds: %d > %d", n, w);
}
// Insert the specified string
if (!t || *t == 0)
// Insert only the fill character
memset(b.Data(), f, w);
else
{
strncpy(b.Get< SQChar >() + (w-n), t, n);
// Calculate the string length
const Uint32 n = strlen(t);
// Insert the fill character first
memset(b.Data(), f, w);
// Overwrite with the specified string
if (n >= w)
strncpy(b.Data(), t, w);
else
strncpy(b.Data() + (w - n), t, n);
}
// End the resulted string
b.At< SQChar >(w) = 0;
b.At(w) = 0;
// Return the resulted string
return b.Get< SQChar >();
}
CSStr RightStr(CSStr t, SQChar f, Uint32 w, Uint32 o)
{
// Allocate a buffer with the requested width
Buffer b(w * sizeof(SQChar));
Uint32 n = !t ? 0 : strlen(t);
// Populate the entire buffer with the fill character
memset(b.Data(), f, w * sizeof(SQChar));
// Is the specified width valid?
if (w >= b.Size< SQChar >())
{
SqThrow("Invalid width specified: %d > %d", w, b.Size< SQChar >());
w = 0;
}
if (!w)
return _SC("");
// Is the specified offset within width range?
else if (o > w)
SqThrowF("Offset is out of bounds");
// Allocate a buffer with the requested width
Buffer b(w);
// Is the specified string valid?
else if (n == 0)
{
SqThrow("Invalid string length: %d < 0", n);
}
// Is the specified string within width range?
else if ((n+o) > w)
{
SqThrow("String is out of bounds: (%d+%d) > %d", n, o, w);
}
// Insert the specified string
if (!t || *t == 0)
// Insert only the fill character
memset(b.Data(), f, w);
else
{
strncpy(b.Get< SQChar >() + ((w-n)-o), t, n);
// Calculate the string length
const Uint32 n = strlen(t);
// Insert the fill character first
memset(b.Data(), f, w);
// Overwrite with the specified string
if (n >= w || (n + o) >= w)
strncpy(b.Data(), t, w - o);
else
strncpy(b.Data() + ((w - n) - o), t, n);
}
// End the resulted string
b.At< SQChar >(w) = 0;
b.At(w) = 0;
// Return the resulted string
return b.Get< SQChar >();
}
@ -159,34 +139,26 @@ CSStr RightStr(CSStr t, SQChar f, Uint32 w, Uint32 o)
// ------------------------------------------------------------------------------------------------
CSStr CenterStr(CSStr t, SQChar f, Uint32 w)
{
// Allocate a buffer with the requested width
Buffer b(w * sizeof(SQChar));
Uint32 n = !t ? 0 : strlen(t);
// Populate the entire buffer with the fill character
memset(b.Data(), f, w * sizeof(SQChar));
// Is the specified width valid?
if (w >= b.Size< SQChar >())
{
SqThrow("Invalid width specified: %d > %d", w, b.Size< SQChar >());
w = 0;
}
if (!w)
return _SC("");
// Allocate a buffer with the requested width
Buffer b(w);
// Is the specified string valid?
else if (n == 0)
{
SqThrow("Invalid string length: %d < 0", n);
}
// Is the specified string within width range?
else if (n > w)
{
SqThrow("String is out of bounds: %d > %d", n, w);
}
// Insert the specified string
if (!t || *t == 0)
// Insert only the fill character
memset(b.Data(), f, w);
else
{
strncpy(b.Get< SQChar >() + (w/2 - n/2), t, n);
// Calculate the string length
const Uint32 n = strlen(t);
// Insert only the fill character first
memset(b.Data(), f, w);
// Overwrite with the specified string
strncpy(b.Data() + ((w/2) - (n/2)), t, n);
}
// End the resulted string
b.At< SQChar >(w) = 0;
b.At(w) = 0;
// Return the resulted string
return b.Get< SQChar >();
}
@ -194,12 +166,11 @@ CSStr CenterStr(CSStr t, SQChar f, Uint32 w)
// ------------------------------------------------------------------------------------------------
CSStr StrJustAlphaNum(CSStr str)
{
Uint32 size = 0;
// See if we actually have something to search for
if(!str || (size = strlen(str)) <= 0)
{
if(!str || *str == 0)
return _SC("");
}
// Calculate the string length
Uint32 size = strlen(str);
// Obtain a temporary buffer
Buffer b(size);
// Resulted string size
@ -211,13 +182,11 @@ CSStr StrJustAlphaNum(CSStr str)
{
// Is this an alpha-numeric character?
if (isalnum(c) != 0)
{
// Save it and move to the next one
b.At< SQChar >(n++) = c;
}
b.At(n++) = c;
}
// End the resulted string
b.At< SQChar >(n) = 0;
b.At(n) = 0;
// Return the string
return b.Get< SQChar >();
}
@ -225,12 +194,11 @@ CSStr StrJustAlphaNum(CSStr str)
// ------------------------------------------------------------------------------------------------
CSStr StrToLowercase(CSStr str)
{
Uint32 size = 0;
// See if we actually have something to search for
if(!str || (size = strlen(str)) <= 0)
{
if(!str || *str == 0)
return _SC("");
}
// Calculate the string length
Uint32 size = strlen(str);
// Obtain a temporary buffer
Buffer b(size);
// Resulted string size
@ -239,12 +207,10 @@ CSStr StrToLowercase(CSStr str)
SQChar c = 0;
// Process characters
while ((c = *(str++)) != 0)
{
// Convert it and move to the next one
b.At< SQChar >(n++) = tolower(c);
}
b.At(n++) = tolower(c);
// End the resulted string
b.At< SQChar >(n) = 0;
b.At(n) = 0;
// Return the string
return b.Get< SQChar >();
}
@ -252,12 +218,11 @@ CSStr StrToLowercase(CSStr str)
// ------------------------------------------------------------------------------------------------
CSStr StrToUppercase(CSStr str)
{
Uint32 size = 0;
// See if we actually have something to search for
if(!str || (size = strlen(str)) <= 0)
{
if(!str || *str == 0)
return _SC("");
}
// Calculate the string length
Uint32 size = strlen(str);
// Obtain a temporary buffer
Buffer b(size);
// Resulted string size
@ -266,12 +231,10 @@ CSStr StrToUppercase(CSStr str)
SQChar c = 0;
// Process characters
while ((c = *(str++)) != 0)
{
// Convert it and move to the next one
b.At< SQChar >(n++) = toupper(c);
}
b.At(n++) = toupper(c);
// End the resulted string
b.At< SQChar >(n) = 0;
b.At(n) = 0;
// Return the string
return b.Get< SQChar >();
}
@ -280,18 +243,16 @@ CSStr StrToUppercase(CSStr str)
static CSStr FromArray(Array & arr)
{
// Determine array size
const Int32 length = (Int32)arr.Length();
const Int32 length = static_cast< Int32 >(arr.Length());
// Obtain a temporary buffer
Buffer b(length * sizeof(Int32));
// Get array elements as integers
arr.GetArray< Int32 >(b.Get< Int32 >(), length);
// Overwrite integers with characters
for (Int32 n = 0; n < length; ++n)
{
b.At< SQChar >(n) = (SQChar)b.At< Int32 >(n);
}
b.At(n) = static_cast< SQChar >(b.At< Int32 >(n));
// Terminate the resulted string
b.At< SQChar >(length) = 0;
b.At(length) = 0;
// Return the string
return b.Get< SQChar >();
}
@ -301,9 +262,14 @@ static SQInteger StdPrintF(HSQUIRRELVM vm)
{
CStr msg = NULL;
SQInteger length = 0;
if(SQ_FAILED(sqstd_format(vm, 2, &length, &msg)))
return -1;
// Attempt to run the specified format and save the result
const SQRESULT res = sqstd_format(vm, 2, &length, &msg);
// Validate the result for errors and propagate them to the VM
if(SQ_FAILED(res))
return res;
// Send the resulted string to console as a user message
LogUsr("%s", msg);
// This function doesn't return anything
return 0;
}

View File

@ -147,22 +147,6 @@ void Logger::Message(Uint8 type, bool sub, CCStr fmt, ...)
va_end(args);
}
// ------------------------------------------------------------------------------------------------
void Logger::Throw(CCStr fmt, ...)
{
va_list args;
va_start(args, fmt);
m_Buffer.WriteF(0, fmt, args);
va_end(args);
Error::Throw(DefaultVM::Get(), m_Buffer.Data());
}
void Logger::Throw(CCStr fmt, va_list args)
{
m_Buffer.WriteF(0, fmt, args);
Error::Throw(DefaultVM::Get(), m_Buffer.Data());
}
// ------------------------------------------------------------------------------------------------
void Logger::Debug(CCStr fmt, ...)
{
@ -382,18 +366,6 @@ SQMOD_CLOG(cLogSWrn, LL_WRN, true)
SQMOD_CLOG(cLogSErr, LL_ERR, true)
SQMOD_CLOG(cLogSFtl, LL_FTL, true)
// ------------------------------------------------------------------------------------------------
void SqThrow(CCStr fmt, ...)
{
va_list args;
va_start(args, fmt);
if (_Log)
_Log->Throw(fmt, args);
else
vprintf(fmt, args);
va_end(args);
}
// --------------------------------------------------------------------------------------------
void OutputMessageImpl(const char * msg, va_list args)
{

View File

@ -74,8 +74,6 @@ public:
void Message(Uint8 type, bool sub, CCStr fmt, ...);
// --------------------------------------------------------------------------------------------
void Throw(CCStr fmt, ...);
void Throw(CCStr fmt, va_list args);
void Debug(CCStr fmt, ...);
void Debug(CCStr fmt, va_list args);

View File

@ -88,29 +88,34 @@ void Register_Core(HSQUIRRELVM vm)
template < Uint8 L, bool S > static SQInteger LogBasicMessage(HSQUIRRELVM vm)
{
const Int32 top = sq_gettop(vm);
// Was the message value specified?
if (top <= 1)
SqThrow("The log message was not specified");
else if (top == 2 && ((sq_gettype(vm, -1) == OT_STRING) || !SQ_FAILED(sq_tostring(vm, -1))))
{
CCStr msg = NULL;
if (SQ_FAILED(sq_getstring(vm, -1, &msg)))
SqThrow("Unable to retrieve the log message");
else
_Log->Message(L, S, "%s", msg);
sq_settop(vm, top);
}
return sq_throwerror(vm, "Missing message value");
// Do we have enough values to call the format function?
else if (top > 2)
{
CStr msg = NULL;
SStr msg = NULL;
SQInteger len = 0;
if (SQ_FAILED(sqstd_format(vm, 2, &len, &msg)))
SqThrow("Unable to generate the log message [%s]", Error::Message(vm).c_str());
else
_Log->Message(L, S, "%s", msg);
// Attempt to generate the specified string format
SQRESULT ret = sqstd_format(vm, 2, &len, &msg);
// Did the format failed?
if (SQ_FAILED(ret))
return ret; // Propagate the exception
// Log the resulted string value
_Log->Message(L, S, "%s", msg);
}
else
SqThrow("Unable to extract the log message");
return 0;
{
// Attempt to retrieve the value from the stack as a string
Var< CSStr > msg(vm, 2);
// See if the obtained value is a valid string
if (!msg.value)
return sq_throwerror(vm, "Unable to retrieve the value");
// Log the resulted string value
_Log->Message(L, S, "%s", msg.value);
}
// This function does not return a value
return 1;
}
// ================================================================================================

View File

@ -739,10 +739,9 @@ Object & FindPlayer(Object & by)
Int32 id = _Func->GetPlayerIDFromName(&str[0]);
if (VALID_ENTITYEX(id, SQMOD_PLAYER_POOL))
_Core->GetPlayer(id).mObj;
}
break;
} break;
default:
SqThrow("Unsupported search identifier");
SqThrowF("Unsupported search identifier");
}
return NullObject();
}

View File

@ -155,14 +155,18 @@ bool EnabledChatTagsByDefault(void)
// ------------------------------------------------------------------------------------------------
void CreateExplosion(Int32 world, Int32 type, const Vector3 & pos, CPlayer & source, Uint32 level)
{
if (source.Validate())
_Func->CreateExplosion(world, type, pos.x, pos.y, pos.z, source.GetID(), level);
// Validate the specified player
source.Validate();
// Perform the requested operation
_Func->CreateExplosion(world, type, pos.x, pos.y, pos.z, source.GetID(), level);
}
void CreateExplosionEx(Int32 world, Int32 type, Float32 x, Float32 y, Float32 z, CPlayer & source, Uint32 level)
{
if (source.Validate())
_Func->CreateExplosion(world, type, x, y, z, source.GetID(), level);
// Validate the specified player
source.Validate();
// Perform the requested operation
_Func->CreateExplosion(world, type, x, y, z, source.GetID(), level);
}
// ------------------------------------------------------------------------------------------------

View File

@ -34,7 +34,7 @@ extern void Register_CVehicle(HSQUIRRELVM vm);
extern void Register_Entity(HSQUIRRELVM vm);
// ------------------------------------------------------------------------------------------------
extern void Register_Hash(HSQUIRRELVM vm);
extern void Register_Crypt(HSQUIRRELVM vm);
extern void Register_Numeric(HSQUIRRELVM vm);
extern void Register_Random(HSQUIRRELVM vm);
extern void Register_String(HSQUIRRELVM vm);
@ -76,7 +76,7 @@ bool RegisterAPI(HSQUIRRELVM vm)
Register_CTextdraw(vm);
Register_CVehicle(vm);
Register_Hash(vm);
Register_Crypt(vm);
Register_Random(vm);
Register_Numeric(vm);
Register_String(vm);

View File

@ -15,60 +15,134 @@ Routine::Time Routine::s_Prev = 0;
Routine::Queue Routine::s_Queue;
Routine::Buckets Routine::s_Buckets;
// ------------------------------------------------------------------------------------------------
void Routine::Attach(Routine * routine, Interval interval)
{
// Do we have a valid routine and interval bucket to attach?
if (!routine || ! interval)
return;
// Attempt to locate the bucket with the specified interval
Buckets::iterator itr = std::find_if(s_Buckets.begin(), s_Buckets.end(), IntrvFunc(interval));
// Does this bucket exist?
if (itr == s_Buckets.end())
{
// Then create it
s_Buckets.emplace_back(interval);
// And attach this routine
s_Buckets.back().mRoutines.push_back(routine);
}
// Is this routine already attached to this bucket?
else if (std::find(itr->mRoutines.begin(), itr->mRoutines.end(), routine) != itr->mRoutines.end())
// Then let's attach it now
itr->mRoutines.push_back(routine);
}
// ------------------------------------------------------------------------------------------------
void Routine::Detach(Routine * routine, Interval interval)
{
// Do we have a valid routine and interval to detach?
if (!routine || ! interval)
return;
// Attempt to locate the bucket with this interval
Buckets::iterator bitr = std::find_if(s_Buckets.begin(), s_Buckets.end(), IntrvFunc(interval));
// Was there a bucket with this interval?
if (bitr == s_Buckets.end())
return; // Nothing to detach from!
// Attempt to find this routine in the associated bucket
Routines::iterator ritr = std::find(bitr->mRoutines.begin(), bitr->mRoutines.end(), routine);
// Was this routine even attached?
if (ritr != bitr->mRoutines.end())
// Then erase it and move on
bitr->mRoutines.erase(ritr);
// Any reason to keep this bucket?
if (bitr->mRoutines.empty())
// Remove the bucket as well
s_Buckets.erase(bitr);
}
// ------------------------------------------------------------------------------------------------
void Routine::ProcQueue()
{
// Do we have any queued commands that must be performed when unlocked?
if (s_Queue.empty() || s_Lock)
return; // We're done here!
// Process all commands in the queue
for (const auto & cmd : s_Queue)
{
// Are we supposed to detach the associated routine?
if (cmd.mCommand == CMD_DETACH)
Detach(cmd.mRoutine, cmd.mInterval);
// Are we supposed to attach the associated routine?
else if (cmd.mCommand == CMD_ATTACH)
Attach(cmd.mRoutine, cmd.mInterval);
}
// Clear processed commands
s_Queue.clear();
}
// ------------------------------------------------------------------------------------------------
void Routine::Process()
{
s_Lock = false; /* In case an exception prevented the unlock last time */
if (!s_Queue.empty())
{
for (Queue::iterator itr = s_Queue.begin(); itr != s_Queue.end(); ++itr)
{
if (itr->first && itr->second)
itr->second->Attach();
else if (itr->second)
itr->second->Terminate();
}
s_Queue.clear();
}
// In case an exception prevented the unlock last time
s_Lock = false;
// Normally there shouldn't be any but just in case the above happened
ProcQueue();
// Is this the first call?
if (s_Last == 0)
{
s_Last = GetCurrentSysTime();
// We'll do it text time
return;
}
// Lock the buckets
s_Lock = true;
// Backup the last known time-stamp
s_Prev = s_Last;
// Get the current time-stamp
s_Last = GetCurrentSysTime();
// Calculate the elapsed time
Int32 delta = Int32((s_Last - s_Prev) / 1000L);
for (Buckets::iterator bucket = s_Buckets.begin(); bucket != s_Buckets.end(); ++bucket)
// Process all available buckets
for (auto & bucket : s_Buckets)
{
bucket->mElapsed += delta;
if (bucket->mElapsed < bucket->mInterval)
continue;
Routines::iterator itr = bucket->mRoutines.begin();
Routines::iterator end = bucket->mRoutines.end();
for (; itr != end; ++itr)
(*itr)->Execute();
bucket->mElapsed = 0;
// Update the bucket elapsed time
bucket.mElapsed += delta;
// Have we completed the bucket interval?
if (bucket.mElapsed < bucket.mInterval)
continue; // Move to the next one
// Attempt to execute bucket routines, if any
for (auto & routine : bucket.mRoutines)
routine->Execute();
// Reset the bucket elapsed time
bucket.mElapsed = 0;
}
// Unlock the buckets
s_Lock = false;
// Process operations that couldn't be performed while buckets were locked
ProcQueue();
}
// ------------------------------------------------------------------------------------------------
void Routine::TerminateAll()
{
for (Buckets::iterator bucket = s_Buckets.begin(); bucket != s_Buckets.end(); ++bucket)
// Let's make sure no pending commands are left
ProcQueue();
// Process all buckets
for (auto & bucket : s_Buckets)
{
Routines::iterator itr = bucket->mRoutines.begin();
Routines::iterator end = bucket->mRoutines.end();
for (; itr != end; ++itr)
// Process all routines in this bucket
for (auto & routine : bucket.mRoutines)
{
(*itr)->Release();
(*itr)->m_Terminated = true;
// Release all resources
routine->Release();
// Mark it as terminated
routine->m_Terminated = true;
}
}
// Clear all references
// Clear all references to routines
s_Buckets.clear();
// Clear the last time-stamp in case of a reload
s_Last = 0;
}
// ------------------------------------------------------------------------------------------------
@ -167,8 +241,13 @@ Routine::Routine(Object & env, Function & func, Interval interval, Iterate itera
// ------------------------------------------------------------------------------------------------
Routine::~Routine()
{
if (!m_Terminated)
Terminate();
// Was the routine already terminated?
if (m_Terminated)
return;
// Detach from the associated bucket
Detach();
// Release script resources
Release();
}
// ------------------------------------------------------------------------------------------------
@ -182,6 +261,7 @@ Int32 Routine::Cmp(const Routine & o) const
return -1;
}
// ------------------------------------------------------------------------------------------------
CSStr Routine::ToString() const
{
return ToStrF(_PRINT_INT_FMT, m_Interval);
@ -190,27 +270,22 @@ CSStr Routine::ToString() const
// ------------------------------------------------------------------------------------------------
void Routine::Terminate()
{
// Was the routine already terminated?
if (m_Terminated)
SqThrow("Routine was already terminated");
else if (s_Lock)
s_Queue.push_back(Queue::value_type(false, this));
else
{
Detach();
Release();
m_Terminated = true;
}
SqThrowF("Routine was already terminated");
// Detach from the associated bucket
Detach();
// Release script resources and mark it as terminated
Release();
}
// ------------------------------------------------------------------------------------------------
void Routine::SetArg(Uint8 num, Object & val)
{
// Was the routine terminated?
if (m_Terminated)
{
SqThrow("Routine was terminated");
return;
}
SqThrowF("Routine was terminated");
// Identify which argument was requested
switch (num)
{
case 1: m_Arg1 = val; break;
@ -227,18 +302,16 @@ void Routine::SetArg(Uint8 num, Object & val)
case 12: m_Arg12 = val; break;
case 13: m_Arg13 = val; break;
case 14: m_Arg14 = val; break;
default: SqThrow("Argument is out of range: %d", num);
default: SqThrowF("Argument is out of range: %d", num);
}
}
Object & Routine::GetArg(Uint8 num)
{
// Was the routine terminated?
if (m_Terminated)
{
SqThrow("Routine was terminated");
return NullObject();
}
SqThrowF("Routine was terminated");
// Identify which argument was requested
switch (num)
{
case 1: return m_Arg1;
@ -255,9 +328,9 @@ Object & Routine::GetArg(Uint8 num)
case 12: return m_Arg12;
case 13: return m_Arg13;
case 14: return m_Arg14;
default: SqThrow("Argument is out of range: %d", num);
default: SqThrowF("Argument is out of range: %d", num);
}
// Shouldn't really reach this point
return NullObject();
}
@ -269,16 +342,18 @@ Routine::Interval Routine::GetInterval() const
void Routine::SetInterval(Interval interval)
{
// Was the routine terminated?
if (m_Terminated)
SqThrow("Routine was terminated");
SqThrowF("Routine was terminated");
// Is the specified interval valid?
else if (!interval)
SqThrow("Invalid routine interval");
else
{
Detach();
m_Interval = interval;
Attach();
}
SqThrowF("Invalid routine interval");
// Detach from the current bucket
Detach();
// Update the interval
m_Interval = interval;
// Attach to the new bucket
Attach();
}
// ------------------------------------------------------------------------------------------------
@ -289,10 +364,11 @@ Routine::Iterate Routine::GetIterations() const
void Routine::SetIterations(Iterate iterations)
{
// Was the routine terminated?
if (m_Terminated)
SqThrow("Routine was terminated");
else
m_Iterations = iterations;
SqThrowF("Routine was terminated");
// Perform the requested operation
m_Iterations = iterations;
}
// ------------------------------------------------------------------------------------------------
@ -303,12 +379,14 @@ Uint8 Routine::GetArguments() const
void Routine::SetArguments(Uint8 num)
{
// Was the routine terminated?
if (m_Terminated)
SqThrow("Routine was terminated");
SqThrowF("Routine was terminated");
// Is the specified argument count valid?
else if (num > 14)
SqThrow("Argument is out of range: %d", num);
else
m_Arguments = num;
SqThrowF("Argument is out of range: %d", num);
// Perform the requested operation
m_Arguments = num;
}
// ------------------------------------------------------------------------------------------------
@ -319,10 +397,11 @@ bool Routine::GetSuspended() const
void Routine::SetSuspended(bool toggle)
{
// Was the routine terminated?
if (m_Terminated)
SqThrow("Routine was terminated");
else
m_Suspended = toggle;
SqThrowF("Routine was terminated");
// Perform the requested operation
m_Suspended = toggle;
}
// ------------------------------------------------------------------------------------------------
@ -334,28 +413,33 @@ bool Routine::GetTerminated() const
// ------------------------------------------------------------------------------------------------
Function & Routine::GetCallback()
{
// Was the routine terminated?
if (m_Terminated)
SqThrow("Routine was terminated");
else
return m_Callback;
return NullFunction();
SqThrowF("Routine was terminated");
// Return the requested information
return m_Callback;
}
void Routine::SetCallback(Object & env, Function & func)
{
// Was the routine terminated?
if (m_Terminated)
SqThrow("Routine was terminated");
else
m_Callback = Function(env.GetVM(), env, func.GetFunc());
SqThrowF("Routine was terminated");
// Perform the requested operation
m_Callback = Function(env.GetVM(), env, func.GetFunc());
}
// ------------------------------------------------------------------------------------------------
void Routine::Release()
{
// Was the routine terminated?
if (m_Terminated)
return;
// Mark it as terminated
m_Terminated = true;
// Release the callback
m_Callback.ReleaseGently();
// Release the arguments
m_Arg1.Release();
m_Arg2.Release();
m_Arg3.Release();
@ -375,64 +459,53 @@ void Routine::Release()
// ------------------------------------------------------------------------------------------------
void Routine::Create()
{
// Was the routine terminated?
if (!m_Interval)
SqThrow("Invalid routine interval");
SqThrowF("Invalid routine interval");
// Is the specified callback valid?
else if (m_Callback.IsNull())
SqThrow("Invalid routine callback");
else
{
Attach();
return;
}
Release();
SqThrowF("Invalid routine callback");
// Attempt to attach the routine
Attach();
}
// ------------------------------------------------------------------------------------------------
void Routine::Attach()
{
// Do we have a valid interval?
if (!m_Interval)
return;
return; // Nothing to attach to!
// Are the buckets locked?
else if (s_Lock)
{
s_Queue.push_back(Queue::value_type(true, this));
return;
}
Buckets::iterator itr = std::find_if(s_Buckets.begin(), s_Buckets.end(), IntrvFunc(m_Interval));
if (itr == s_Buckets.end())
{
s_Buckets.push_back(Buckets::value_type(m_Interval));
s_Buckets.back().mRoutines.push_back(this);
}
else if (std::find(itr->mRoutines.begin(), itr->mRoutines.end(), this) != itr->mRoutines.end())
return;
// Queue a command to attach this routine when the bucket is unlocked
s_Queue.emplace_back(this, m_Interval, CMD_ATTACH);
// Attempt to attach the the routine now
else
itr->mRoutines.push_back(this);
Attach(this, m_Interval);
}
// ------------------------------------------------------------------------------------------------
void Routine::Detach()
{
// Do we have a valid interval?
if (!m_Interval)
return;
return; // Nothing to detach from!
// Are the buckets locked?
else if (s_Lock)
{
s_Queue.push_back(Queue::value_type(false, this));
return;
}
Buckets::iterator bitr = std::find_if(s_Buckets.begin(), s_Buckets.end(), IntrvFunc(m_Interval));
if (bitr == s_Buckets.end())
return;
Routines::iterator ritr = std::find(bitr->mRoutines.begin(), bitr->mRoutines.end(), this);
if (ritr != bitr->mRoutines.end())
bitr->mRoutines.erase(ritr);
// Any reason to keep this bucket?
if (bitr->mRoutines.empty())
s_Buckets.erase(bitr);
// Queue a command to detach this routine when the bucket is unlocked
s_Queue.emplace_back(this, m_Interval, CMD_DETACH);
// Attempt to detach the the routine now
else
Detach(this, m_Interval);
}
// ------------------------------------------------------------------------------------------------
void Routine::Execute()
{
// Is this routine suspended or has nothing to call?
if (m_Suspended || m_Callback.IsNull())
return;
return; // We're done here!
// Attempt to identify how many arguments should be passed
switch (m_Arguments)
{
case 0: m_Callback.Execute();
@ -473,14 +546,11 @@ void Routine::Execute()
m_Arg8, m_Arg9, m_Arg10, m_Arg11, m_Arg12, m_Arg13, m_Arg14);
break;
default:
SqThrow("Unknown argument count: %d", m_Arguments);
SqThrowF("Unknown argument count: %d", m_Arguments);
}
// Decrease the number of iterations if necessary
if (m_Iterations && (--m_Iterations) == 0)
{
Terminate();
}
Terminate(); // This routine reached the end of it's life
}
/* ------------------------------------------------------------------------------------------------

View File

@ -5,8 +5,8 @@
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
#include <set>
#include <vector>
#include <map>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -37,6 +37,16 @@ public:
protected:
/* --------------------------------------------------------------------------------------------
* Commands that can be performed when the buckets are unlocked.
*/
enum
{
CMD_REMOVE = 1,
CMD_DETACH = 2,
CMD_ATTACH = 3
};
/* --------------------------------------------------------------------------------------------
* Group of routines that have the same interval.
*/
@ -48,7 +58,7 @@ protected:
Routines mRoutines; /* Routines to trigger on completion. */
/* ----------------------------------------------------------------------------------------
* Base constructor.
* Default constructor.
*/
Bucket(Interval interval)
: mInterval(interval), mElapsed(0), mRoutines()
@ -59,36 +69,78 @@ protected:
/* ----------------------------------------------------------------------------------------
* Copy constructor.
*/
Bucket(const Bucket & o)
: mInterval(o.mInterval), mElapsed(o.mElapsed), mRoutines(o.mRoutines)
{
/* ... */
}
Bucket(const Bucket & o) = default;
/* ----------------------------------------------------------------------------------------
* Move constructor.
*/
Bucket(Bucket && o) = default;
/* ----------------------------------------------------------------------------------------
* Destructor.
*/
~Bucket()
~Bucket() = default;
/* ----------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Bucket & operator = (const Bucket & o) = default;
/* ----------------------------------------------------------------------------------------
* Move assignment operator.
*/
Bucket & operator = (Bucket && o) = default;
};
/* --------------------------------------------------------------------------------------------
* A command to perform certain actions when the buckets are unlocked.
*/
struct Cmd
{
// ----------------------------------------------------------------------------------------
Routine* mRoutine; /* The routine to which this command applies */
Interval mInterval; /* The bucket where this routine is stored. */
Uint16 mCommand; /* The command that must be performed. */
/* ----------------------------------------------------------------------------------------
* Base constructor.
*/
Cmd(Routine * routine, Interval interval, Uint16 command)
: mRoutine(routine), mInterval(interval), mCommand(command)
{
/* ... */
}
/* ----------------------------------------------------------------------------------------
* Copy constructor.
*/
Cmd(const Cmd & o) = default;
/* ----------------------------------------------------------------------------------------
* Move constructor.
*/
Cmd(Cmd && o) = default;
/* ----------------------------------------------------------------------------------------
* Destructor.
*/
~Cmd() = default;
/* ----------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Bucket & operator = (const Bucket & o)
{
mInterval = o.mInterval;
mElapsed = o.mElapsed;
mRoutines = o.mRoutines;
return *this;
}
Cmd & operator = (const Cmd & o) = default;
/* ----------------------------------------------------------------------------------------
* Move assignment operator.
*/
Cmd & operator = (Cmd && o) = default;
};
// --------------------------------------------------------------------------------------------
typedef Int64 Time;
typedef std::vector< Bucket > Buckets;
typedef std::vector< std::pair< bool, Routine * > > Queue;
typedef Int64 Time;
typedef std::vector< Cmd > Queue;
typedef std::vector< Bucket > Buckets;
/* --------------------------------------------------------------------------------------------
* Functor used to search for buckets with a certain interval.
@ -98,7 +150,7 @@ protected:
private:
// ----------------------------------------------------------------------------------------
Interval m_Interval; /* The interval to be matched. */
const Interval m_Interval; /* The interval to be matched. */
public:
@ -118,6 +170,14 @@ protected:
{
return (elem.mInterval == m_Interval);
}
/* ----------------------------------------------------------------------------------------
* Function call operator.
*/
bool operator () (Buckets::const_reference elem) const
{
return (elem.mInterval == m_Interval);
}
};
// --------------------------------------------------------------------------------------------
@ -127,6 +187,21 @@ protected:
static Queue s_Queue; /* Actions to be performed when the buckets aren't locked */
static Buckets s_Buckets; /* Buckets of routines grouped by similar intervals. */
/* --------------------------------------------------------------------------------------------
* Attach a routine to a certain bucket.
*/
static void Attach(Routine * routine, Interval interval);
/* --------------------------------------------------------------------------------------------
* Detach a routine from a certain bucket.
*/
static void Detach(Routine * routine, Interval interval);
/* --------------------------------------------------------------------------------------------
* Process queue commands.
*/
static void ProcQueue();
private:
/* --------------------------------------------------------------------------------------------
@ -215,7 +290,7 @@ public:
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* Terminate this routine by releasing all resources and sscheduling it for detachment.
* Terminate this routine by releasing all resources and scheduling it for detachment.
*/
void Terminate();

View File

@ -119,6 +119,16 @@
#define SQMOD_VERSION_MINOR 0
#define SQMOD_VERSION_PATCH 1
/* ------------------------------------------------------------------------------------------------
* SQUIRREL FORWARD DECLARATIONS
*/
extern "C" {
typedef struct tagSQObject SQObject;
struct SQVM;
typedef struct SQVM* HSQUIRRELVM;
typedef SQObject HSQOBJECT;
} /*extern "C"*/
/* ------------------------------------------------------------------------------------------------
* SQRAT FORWARD DECLARATIONS
*/
@ -394,19 +404,38 @@ enum CmdArgType
// ------------------------------------------------------------------------------------------------
enum CmdError
{
// The command failed for unknown reasons
CMDERR_UNKNOWN = 0,
// The command failed to execute because there was nothing to execute
CMDERR_EMPTY_COMMAND,
// The command failed to execute because the command name was invalid after processing
CMDERR_INVALID_COMMAND,
// The command failed to execute because there was a syntax error in the arguments
CMDERR_SYNTAX_ERROR,
// The command failed to execute because there was no such command
CMDERR_UNKNOWN_COMMAND,
// The command failed to execute because there was no callback to handle the execution
CMDERR_MISSING_EXECUTER,
// The command failed to execute because the invoker does not have the proper authority
CMDERR_INSUFFICIENT_AUTH,
// The command was unable to execute because the argument limit was not reached
CMDERR_INCOMPLETE_ARGS,
// The command was unable to execute because the argument limit was exceeded
CMDERR_EXTRANEOUS_ARGS,
// Command was unable to execute due to argument type mismatch
CMDERR_UNSUPPORTED_ARG,
CMDERR_EXECUTION_FAILED,
// The command arguments contained more data than the internal buffer can handle
CMDERR_BUFFER_OVERFLOW,
CMDERR_MAX,
// The command failed to complete execution due to a runtime exception
CMDERR_EXECUTION_FAILED,
// The command completed the execution but returned a negative result
CMDERR_EXECUTION_ABORTED,
// The post execution callback failed to execute due to a runtime exception
CMDERR_POST_PROCESSING_FAILED,
// The callback that was supposed to deal with the failure also failed due to a runtime exception
CMDERR_UNRESOLVED_FAILURE,
// Maximum command error identifier
CMDERR_MAX
};
} // Namespace:: SqMod