Add the constants loader, to allow entityDefs and other scripts to reference constants defined within scripts/constants.txt

This commit is contained in:
Marco Cawthorne 2023-07-15 09:32:21 -07:00
parent b847316ec0
commit f877b1620f
Signed by: eukara
GPG Key ID: CE2032F0A2882A22
9 changed files with 125 additions and 6 deletions

View File

@ -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"
[...]
}
```

View File

@ -888,6 +888,7 @@ INPUT = src/ \
Documentation/Materials/MatShaders.md \
Documentation/Materials/MatGoldSrc.md \
Documentation/Shaders/ \
Documentation/Constants.md \
Documentation/EntityDef.md \
Documentation/Surf_data.md \
Documentation/Prop_data.md \

View File

@ -52,7 +52,7 @@ CSQC_Init(float apilevel, string enginename, float engineversion)
Cmd_Init();
/* Sound shaders */
Constants_Init();
Sound_Init();
SurfData_Init();
PropData_Init();

View File

@ -338,6 +338,7 @@ init(float prevprogs)
Plugin_Init();
Constants_Init();
Sound_Init();
PropData_Init();
SurfData_Init();

View File

@ -241,35 +241,35 @@ bool
NSIO::ReadBool(string strValue)
{
if (strValue && strValue != "")
return stof(strValue);
return stof(Constants_LookUp(strValue, strValue));
return __NULL__;
}
float
NSIO::ReadFloat(string strValue)
{
if (strValue && strValue != "")
return stof(strValue);
return stof(Constants_LookUp(strValue, strValue));
return __NULL__;
}
int
NSIO::ReadInt(string strValue)
{
if (strValue && strValue != "")
return stoi(strValue);
return stoi(Constants_LookUp(strValue, strValue));
return __NULL__;
}
string
NSIO::ReadString(string strValue)
{
if (strValue && strValue != "")
return strValue;
return Constants_LookUp(strValue, strValue);
return __NULL__;
}
vector
NSIO::ReadVector(string strValue)
{
if (strValue && strValue != "")
return stov(strValue);
return stov(Constants_LookUp(strValue, strValue));
return __NULL__;
}
entity

20
src/shared/cloader.h Normal file
View File

@ -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;

71
src/shared/cloader.qc Normal file
View File

@ -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];
}

View File

@ -40,6 +40,7 @@ string __fullspawndata;
#endif
#include "global.h"
#include "cloader.h"
#include "sound.h"
#ifdef CLIENT

View File

@ -23,6 +23,7 @@ NSClient.qc
NSClientSpectator.qc
pmove.qc
pmove_custom.qc
cloader.qc
sound.qc
math.qc
NSClientPlayer.qc