diff --git a/Documentation/Constants.md b/Documentation/Constants.md new file mode 100644 index 00000000..c8331203 --- /dev/null +++ b/Documentation/Constants.md @@ -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" + [...] +} +``` \ No newline at end of file diff --git a/Doxyfile b/Doxyfile index 90297f20..a266ea5c 100644 --- a/Doxyfile +++ b/Doxyfile @@ -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 \ diff --git a/src/client/entry.qc b/src/client/entry.qc index c06fc489..2e01fe4c 100644 --- a/src/client/entry.qc +++ b/src/client/entry.qc @@ -52,7 +52,7 @@ CSQC_Init(float apilevel, string enginename, float engineversion) Cmd_Init(); - /* Sound shaders */ + Constants_Init(); Sound_Init(); SurfData_Init(); PropData_Init(); diff --git a/src/server/entry.qc b/src/server/entry.qc index 0c941a47..7825112a 100644 --- a/src/server/entry.qc +++ b/src/server/entry.qc @@ -338,6 +338,7 @@ init(float prevprogs) Plugin_Init(); + Constants_Init(); Sound_Init(); PropData_Init(); SurfData_Init(); diff --git a/src/shared/NSIO.qc b/src/shared/NSIO.qc index a0b852ae..e597bece 100644 --- a/src/shared/NSIO.qc +++ b/src/shared/NSIO.qc @@ -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 diff --git a/src/shared/cloader.h b/src/shared/cloader.h new file mode 100644 index 00000000..5a1ece4f --- /dev/null +++ b/src/shared/cloader.h @@ -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; \ No newline at end of file diff --git a/src/shared/cloader.qc b/src/shared/cloader.qc new file mode 100644 index 00000000..93be55d3 --- /dev/null +++ b/src/shared/cloader.qc @@ -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]; +} \ No newline at end of file diff --git a/src/shared/defs.h b/src/shared/defs.h index 99f6be43..bdc3025b 100644 --- a/src/shared/defs.h +++ b/src/shared/defs.h @@ -40,6 +40,7 @@ string __fullspawndata; #endif #include "global.h" +#include "cloader.h" #include "sound.h" #ifdef CLIENT diff --git a/src/shared/include.src b/src/shared/include.src index 7d6aa77c..c2ffaf74 100644 --- a/src/shared/include.src +++ b/src/shared/include.src @@ -23,6 +23,7 @@ NSClient.qc NSClientSpectator.qc pmove.qc pmove_custom.qc +cloader.qc sound.qc math.qc NSClientPlayer.qc