openmohaa/code/script/scriptvariable.h

322 lines
8.9 KiB
C
Raw Normal View History

2016-03-27 11:49:47 +02:00
/*
===========================================================================
Copyright (C) 2015 the OpenMoHAA team
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// scriptvariable.h: Dynamic variables for scripts.
#ifndef __SCRIPTVARIABLE_H__
#define __SCRIPTVARIABLE_H__
#include "../qcommon/listener.h"
#include "../qcommon/short3.h"
2016-03-27 11:49:47 +02:00
#ifdef GAME_DLL
2023-07-05 21:23:39 +02:00
# include "../fgame/misc.h"
2016-03-27 11:49:47 +02:00
#endif
2023-07-05 21:23:39 +02:00
enum variabletype {
VARIABLE_NONE,
VARIABLE_STRING,
VARIABLE_INTEGER,
VARIABLE_FLOAT,
VARIABLE_CHAR,
VARIABLE_CONSTSTRING,
VARIABLE_LISTENER,
VARIABLE_REF,
VARIABLE_ARRAY,
VARIABLE_CONSTARRAY,
VARIABLE_CONTAINER,
VARIABLE_SAFECONTAINER,
VARIABLE_POINTER,
VARIABLE_VECTOR,
VARIABLE_MAX
2016-03-27 11:49:47 +02:00
};
2023-07-05 21:23:39 +02:00
static const char *typenames[] = {
"none",
"string",
"int",
"float",
"char",
"const string",
"listener",
"ref",
"array",
"const array",
"array",
"array",
"pointer",
"vector",
"double"};
2016-03-27 11:49:47 +02:00
2023-02-06 19:24:01 +01:00
class ScriptArrayHolder;
class ScriptConstArrayHolder;
class ScriptPointer;
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
class ScriptVariable
{
2016-03-27 11:49:47 +02:00
public:
2023-01-29 20:59:31 +01:00
#ifdef GAME_DLL
2023-07-05 21:23:39 +02:00
short3 key; // variable name
2023-01-29 20:59:31 +01:00
#endif
2023-07-05 21:23:39 +02:00
unsigned char type; // variable type
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
union anon393 {
public:
char charValue;
float floatValue;
int intValue;
SafePtr<Listener> *listenerValue;
str *stringValue;
float *vectorValue;
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
ScriptVariable *refValue;
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
ScriptArrayHolder *arrayValue;
ScriptConstArrayHolder *constArrayValue;
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
Container<SafePtr<Listener>> *containerValue;
SafePtr<ContainerClass<SafePtr<Listener>>> *safeContainerValue;
ScriptPointer *pointerValue;
} m_data;
2016-03-27 11:49:47 +02:00
private:
2023-07-05 21:23:39 +02:00
void ClearInternal();
void ClearPointerInternal() const;
2016-03-27 11:49:47 +02:00
public:
2023-07-05 21:23:39 +02:00
ScriptVariable();
ScriptVariable(const ScriptVariable& variable);
ScriptVariable(ScriptVariable&& variable);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
~ScriptVariable();
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
void Archive(Archiver &arc);
static void Archive(Archiver& arc, ScriptVariable **obj);
void ArchiveInternal(Archiver &arc);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
void CastBoolean(void);
void CastConstArrayValue(void);
void CastEntity(void);
void CastFloat(void);
void CastInteger(void);
void CastString(void);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
void Clear();
void ClearPointer();
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
const char *GetTypeName(void) const;
variabletype GetType(void) const;
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
qboolean IsEntity(void);
qboolean IsListener(void);
qboolean IsNumeric(void);
qboolean IsConstArray() const;
#ifdef WITH_SCRIPT_ENGINE
2023-07-05 21:23:39 +02:00
qboolean IsSimpleEntity(void);
2016-03-27 11:49:47 +02:00
#endif
2023-07-05 21:23:39 +02:00
qboolean IsString(void);
qboolean IsVector(void);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
void PrintValue(void);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
void SetFalse(void);
void SetTrue(void);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
int arraysize(void) const;
size_t size(void) const;
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
bool booleanNumericValue(void);
bool booleanValue(void) const;
2016-03-27 11:49:47 +02:00
#ifdef WITH_SCRIPT_ENGINE
2023-07-05 21:23:39 +02:00
str& getName(void);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
short3& GetKey();
void SetKey(const short3 &key);
2016-03-27 11:49:47 +02:00
#endif
2023-07-05 21:23:39 +02:00
Entity *entityValue(void);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
void evalArrayAt(ScriptVariable& var);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
void setArrayAt(ScriptVariable& index, ScriptVariable& value);
void setArrayAtRef(ScriptVariable& index, ScriptVariable& value);
void setArrayRefValue(ScriptVariable& var);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
char charValue(void) const;
void setCharValue(char newvalue);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
ScriptVariable *constArrayValue(void);
void setConstArrayValue(ScriptVariable *pVar, unsigned int size);
2016-03-27 11:49:47 +02:00
#ifdef WITH_SCRIPT_ENGINE
2023-07-05 21:23:39 +02:00
const_str constStringValue(void) const;
void setConstStringValue(const_str s);
2016-03-27 11:49:47 +02:00
#endif
2023-07-05 21:23:39 +02:00
void setContainerValue(Container<SafePtr<Listener>> *newvalue);
void setSafeContainerValue(ContainerClass<SafePtr<Listener>> *newvalue);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
float floatValue(void) const;
void setFloatValue(float newvalue);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
int intValue(void) const;
void setIntValue(int newvalue);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
Listener *listenerValue(void) const;
Listener *listenerAt(uintptr_t index) const;
void setListenerValue(Listener *newvalue);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
void newPointer(void);
void setPointer(const ScriptVariable& newvalue);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
void setRefValue(ScriptVariable *ref);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
//const char *stringValue( void );
str stringValue(void) const;
void setStringValue(str newvalue);
2016-03-27 11:49:47 +02:00
#ifdef WITH_SCRIPT_ENGINE
2023-07-05 21:23:39 +02:00
SimpleEntity *simpleEntityValue(void) const;
2016-03-27 11:49:47 +02:00
#endif
2023-07-05 21:23:39 +02:00
Vector vectorValue(void) const;
void setVectorValue(const Vector &newvector);
class PathNode *pathNodeValue(void) const;
class Waypoint *waypointValue(void) const;
void greaterthan(ScriptVariable& variable);
void greaterthanorequal(ScriptVariable& variable);
void lessthan(ScriptVariable& variable);
void lessthanorequal(ScriptVariable& variable);
void complement(void);
void minus(void);
ScriptVariable& operator=(const ScriptVariable& variable);
ScriptVariable& operator=(ScriptVariable&& variable);
ScriptVariable& operator[](ScriptVariable& index);
ScriptVariable *operator[](unsigned index) const;
ScriptVariable *operator*();
void operator+=(const ScriptVariable &value);
void operator-=(const ScriptVariable &value);
void operator*=(const ScriptVariable &value);
void operator/=(const ScriptVariable &value);
void operator%=(const ScriptVariable &value);
void operator&=(const ScriptVariable &value);
void operator^=(const ScriptVariable &value);
void operator|=(const ScriptVariable &value);
void operator<<=(const ScriptVariable &value);
void operator>>=(const ScriptVariable &value);
bool operator!=(const ScriptVariable& value);
bool operator==(const ScriptVariable& value);
ScriptVariable operator++(int);
ScriptVariable operator--(int);
2016-03-27 11:49:47 +02:00
};
2023-07-05 21:23:39 +02:00
class ScriptArrayHolder
{
2023-02-06 19:24:01 +01:00
public:
2023-07-05 21:23:39 +02:00
con_map<ScriptVariable, ScriptVariable> arrayValue;
unsigned int refCount;
2023-02-06 19:24:01 +01:00
public:
2023-08-18 01:42:10 +02:00
ScriptArrayHolder();
2023-07-05 21:23:39 +02:00
void Archive(Archiver &arc);
static void Archive(Archiver& arc, ScriptArrayHolder *& arrayValue);
2023-02-06 19:24:01 +01:00
};
2023-07-05 21:23:39 +02:00
class ScriptConstArrayHolder
{
2023-02-06 19:24:01 +01:00
public:
2023-07-05 21:23:39 +02:00
ScriptVariable *constArrayValue;
unsigned int refCount;
unsigned int size;
2023-02-06 19:24:01 +01:00
public:
2023-07-05 21:23:39 +02:00
void Archive(Archiver &arc);
static void Archive(Archiver& arc, ScriptConstArrayHolder *& constArrayValue);
2023-02-06 19:24:01 +01:00
2023-07-05 21:23:39 +02:00
ScriptConstArrayHolder(ScriptVariable *pVar, unsigned int size);
ScriptConstArrayHolder(unsigned int size);
ScriptConstArrayHolder();
~ScriptConstArrayHolder();
2023-02-06 19:24:01 +01:00
};
2023-07-05 21:23:39 +02:00
class ScriptPointer
{
2023-02-06 19:24:01 +01:00
public:
2023-07-05 21:23:39 +02:00
Container<ScriptVariable *> list;
2023-02-06 19:24:01 +01:00
public:
2023-07-05 21:23:39 +02:00
void Archive(Archiver &arc);
static void Archive(Archiver& arc, ScriptPointer *& pointerValue);
2023-02-06 19:24:01 +01:00
2023-07-05 21:23:39 +02:00
void Clear();
2023-02-06 19:24:01 +01:00
2023-07-05 21:23:39 +02:00
void add(ScriptVariable *var);
void remove(ScriptVariable *var);
void setValue(const ScriptVariable& var);
2023-02-06 19:24:01 +01:00
};
#ifdef WITH_SCRIPT_ENGINE
2016-03-27 11:49:47 +02:00
class ScriptVariableList : public Class
{
private:
2023-07-05 21:23:39 +02:00
con_set<short3, ScriptVariable> list;
2016-03-27 11:49:47 +02:00
public:
2023-07-05 21:23:39 +02:00
CLASS_PROTOTYPE(ScriptVariableList);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
ScriptVariableList();
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
void Archive(Archiver& arc) override;
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
void ClearList(void);
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
ScriptVariable *GetOrCreateVariable(str name);
ScriptVariable *GetOrCreateVariable(unsigned int name);
2016-03-27 11:49:47 +02:00
ScriptVariable *GetVariable(str name) const;
ScriptVariable *GetVariable(unsigned int name) const;
bool VariableExists(str name) const;
2016-03-27 11:49:47 +02:00
2023-07-05 21:23:39 +02:00
ScriptVariable *SetVariable(const char *name, int value);
ScriptVariable *SetVariable(const char *name, float value);
ScriptVariable *SetVariable(const char *name, const char *value);
ScriptVariable *SetVariable(const char *name, Entity *value);
ScriptVariable *SetVariable(const char *name, Listener *value);
ScriptVariable *SetVariable(const char *name, Vector& value);
ScriptVariable *SetVariable(const char *name, ScriptVariable& value);
ScriptVariable *SetVariable(unsigned int name, ScriptVariable& value);
ScriptVariable *SetVariable(unsigned int name, ScriptVariable&& value);
2016-03-27 11:49:47 +02:00
};
#endif
#endif /* __SCRIPTVARIABLE_H__ */