Add the constants loader, to allow entityDefs and other scripts to reference constants defined within scripts/constants.txt
This commit is contained in:
parent
b847316ec0
commit
f877b1620f
|
@ -0,0 +1,24 @@
|
||||||
|
# Constants
|
||||||
|
|
||||||
|
Often you want to be able to use aliases for values inside your **EntityDef** files.
|
||||||
|
|
||||||
|
For that, you can have a name/value pair mapping inside a text file named `scripts/constants.txt`.
|
||||||
|
|
||||||
|
```
|
||||||
|
// some comments here
|
||||||
|
WEAPON_NONE 0
|
||||||
|
WEAPON_CROWBAR 1
|
||||||
|
WEAPON_GLOCK 2
|
||||||
|
[...]
|
||||||
|
DEFAULT_NAME "Max"
|
||||||
|
```
|
||||||
|
|
||||||
|
And then you use the identifers in place of those constants. Similar to environment variables in the **UNIX** shell.
|
||||||
|
|
||||||
|
```
|
||||||
|
entityDef weapon_glock {
|
||||||
|
"spawnclass" "NSItem"
|
||||||
|
"inv_item" "$WEAPON_GLOCK"
|
||||||
|
[...]
|
||||||
|
}
|
||||||
|
```
|
1
Doxyfile
1
Doxyfile
|
@ -888,6 +888,7 @@ INPUT = src/ \
|
||||||
Documentation/Materials/MatShaders.md \
|
Documentation/Materials/MatShaders.md \
|
||||||
Documentation/Materials/MatGoldSrc.md \
|
Documentation/Materials/MatGoldSrc.md \
|
||||||
Documentation/Shaders/ \
|
Documentation/Shaders/ \
|
||||||
|
Documentation/Constants.md \
|
||||||
Documentation/EntityDef.md \
|
Documentation/EntityDef.md \
|
||||||
Documentation/Surf_data.md \
|
Documentation/Surf_data.md \
|
||||||
Documentation/Prop_data.md \
|
Documentation/Prop_data.md \
|
||||||
|
|
|
@ -52,7 +52,7 @@ CSQC_Init(float apilevel, string enginename, float engineversion)
|
||||||
|
|
||||||
Cmd_Init();
|
Cmd_Init();
|
||||||
|
|
||||||
/* Sound shaders */
|
Constants_Init();
|
||||||
Sound_Init();
|
Sound_Init();
|
||||||
SurfData_Init();
|
SurfData_Init();
|
||||||
PropData_Init();
|
PropData_Init();
|
||||||
|
|
|
@ -338,6 +338,7 @@ init(float prevprogs)
|
||||||
|
|
||||||
Plugin_Init();
|
Plugin_Init();
|
||||||
|
|
||||||
|
Constants_Init();
|
||||||
Sound_Init();
|
Sound_Init();
|
||||||
PropData_Init();
|
PropData_Init();
|
||||||
SurfData_Init();
|
SurfData_Init();
|
||||||
|
|
|
@ -241,35 +241,35 @@ bool
|
||||||
NSIO::ReadBool(string strValue)
|
NSIO::ReadBool(string strValue)
|
||||||
{
|
{
|
||||||
if (strValue && strValue != "")
|
if (strValue && strValue != "")
|
||||||
return stof(strValue);
|
return stof(Constants_LookUp(strValue, strValue));
|
||||||
return __NULL__;
|
return __NULL__;
|
||||||
}
|
}
|
||||||
float
|
float
|
||||||
NSIO::ReadFloat(string strValue)
|
NSIO::ReadFloat(string strValue)
|
||||||
{
|
{
|
||||||
if (strValue && strValue != "")
|
if (strValue && strValue != "")
|
||||||
return stof(strValue);
|
return stof(Constants_LookUp(strValue, strValue));
|
||||||
return __NULL__;
|
return __NULL__;
|
||||||
}
|
}
|
||||||
int
|
int
|
||||||
NSIO::ReadInt(string strValue)
|
NSIO::ReadInt(string strValue)
|
||||||
{
|
{
|
||||||
if (strValue && strValue != "")
|
if (strValue && strValue != "")
|
||||||
return stoi(strValue);
|
return stoi(Constants_LookUp(strValue, strValue));
|
||||||
return __NULL__;
|
return __NULL__;
|
||||||
}
|
}
|
||||||
string
|
string
|
||||||
NSIO::ReadString(string strValue)
|
NSIO::ReadString(string strValue)
|
||||||
{
|
{
|
||||||
if (strValue && strValue != "")
|
if (strValue && strValue != "")
|
||||||
return strValue;
|
return Constants_LookUp(strValue, strValue);
|
||||||
return __NULL__;
|
return __NULL__;
|
||||||
}
|
}
|
||||||
vector
|
vector
|
||||||
NSIO::ReadVector(string strValue)
|
NSIO::ReadVector(string strValue)
|
||||||
{
|
{
|
||||||
if (strValue && strValue != "")
|
if (strValue && strValue != "")
|
||||||
return stov(strValue);
|
return stov(Constants_LookUp(strValue, strValue));
|
||||||
return __NULL__;
|
return __NULL__;
|
||||||
}
|
}
|
||||||
entity
|
entity
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023 Vera Visions LLC.
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
||||||
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
void Constants_Init(void);
|
||||||
|
string Constants_LookUp(string, string);
|
||||||
|
|
||||||
|
var hashtable g_hashConstants;
|
|
@ -0,0 +1,71 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023 Vera Visions LLC.
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
||||||
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MAX_CONSTANTS_ID
|
||||||
|
#define MAX_CONSTANTS_ID 128
|
||||||
|
#endif
|
||||||
|
|
||||||
|
string g_constIndex[MAX_CONSTANTS_ID];
|
||||||
|
|
||||||
|
void
|
||||||
|
Constants_Init(void)
|
||||||
|
{
|
||||||
|
filestream constFile;
|
||||||
|
string tempString;
|
||||||
|
int arguments = 0i;
|
||||||
|
int indexCount = 0i;
|
||||||
|
|
||||||
|
if (!g_hashConstants) {
|
||||||
|
g_hashConstants = hash_createtab(2, HASH_ADD);
|
||||||
|
}
|
||||||
|
|
||||||
|
constFile = fopen("scripts/constants.txt", FILE_READ);
|
||||||
|
|
||||||
|
if (constFile < 0) {
|
||||||
|
print("Unable to load scripts/constants.txt");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((tempString = fgets(constFile))) {
|
||||||
|
arguments = (int)tokenize_console(tempString);
|
||||||
|
|
||||||
|
if (arguments != 2) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* sanity bounds check */
|
||||||
|
if (indexCount == MAX_CONSTANTS_ID) {
|
||||||
|
print("Constants_Init: Hit maximum number of constants.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_constIndex[indexCount] = argv(1);
|
||||||
|
hash_add(g_hashConstants, argv(0), indexCount);
|
||||||
|
indexCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
string
|
||||||
|
Constants_LookUp(string constName, string returnValue)
|
||||||
|
{
|
||||||
|
int constIndex = -1i;
|
||||||
|
|
||||||
|
/* only if we're prefixed */
|
||||||
|
if (substring(constName, 0, 1) == "$")
|
||||||
|
constIndex = hash_get(g_hashConstants, substring(constName, 1, -1), -1i);
|
||||||
|
|
||||||
|
return (constIndex == -1i) ? returnValue : g_constIndex[constIndex];
|
||||||
|
}
|
|
@ -40,6 +40,7 @@ string __fullspawndata;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "global.h"
|
#include "global.h"
|
||||||
|
#include "cloader.h"
|
||||||
#include "sound.h"
|
#include "sound.h"
|
||||||
|
|
||||||
#ifdef CLIENT
|
#ifdef CLIENT
|
||||||
|
|
|
@ -23,6 +23,7 @@ NSClient.qc
|
||||||
NSClientSpectator.qc
|
NSClientSpectator.qc
|
||||||
pmove.qc
|
pmove.qc
|
||||||
pmove_custom.qc
|
pmove_custom.qc
|
||||||
|
cloader.qc
|
||||||
sound.qc
|
sound.qc
|
||||||
math.qc
|
math.qc
|
||||||
NSClientPlayer.qc
|
NSClientPlayer.qc
|
||||||
|
|
Loading…
Reference in New Issue