Nuclide
Software Development Kit for id Technology (BETA)
Loading...
Searching...
No Matches
common.h
1/*
2 * Copyright (c) 2016-2024 Vera Visions LLC.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16
17#define bool float
18#define true 1
19#define false 0
20
24
25var bool autocvar_g_logTimestamps = false;
26
27typedef entity id;
28typedef float musictrack_t;
29
30typedef enum
31{
32 LOGLEVEL_NONE,
33 LOGLEVEL_ERRORS,
34 LOGLEVEL_WARNINGS,
35 LOGLEVEL_DEBUG,
36} logLevel_t;
37
38#define LOGLEVEL_DEFAULT LOGLEVEL_WARNINGS
39var logLevel_t autocvar_g_logLevel = LOGLEVEL_DEFAULT;
40
41#define printf(...) print(sprintf(__VA_ARGS__))
42
43#ifdef DOXYGEN
45#define enumflags enum
46#endif
47
48#define ICN_SIZE 8
49
50string
51imageToConsole(string imageName, int imgSize, string toolTip)
52{
53 return sprintf("^[\\img\\%s\\s\\%i\\tip\\%s^]", imageName, imgSize, toolTip);
54}
55
56#define CG_LOG imageToConsole("gfx/icon16/monitor", ICN_SIZE, "Client Game Log")
57#define CG_WARNING imageToConsole("gfx/icon16/error", ICN_SIZE, "Client Game Warning")
58#define CG_ERROR imageToConsole("gfx/icon16/exclamation", ICN_SIZE, "Client Game Error")
59
60#define SV_LOG imageToConsole("gfx/icon16/server", ICN_SIZE, "Server Game Log")
61#define SV_WARNING imageToConsole("gfx/icon16/error", ICN_SIZE, "Server Game Warning")
62#define SV_ERROR imageToConsole("gfx/icon16/exclamation", ICN_SIZE, "Server Game Error")
63
64#define UI_LOG imageToConsole("gfx/icon16/picture", ICN_SIZE, "Menu Game Log")
65#define UI_WARNING imageToConsole("gfx/icon16/error", ICN_SIZE, "Menu Game Warning")
66#define UI_ERROR imageToConsole("gfx/icon16/exclamation", ICN_SIZE, "Menu Game Error")
67
68void
69_ncLog(string msg)
70{
71#ifdef CLIENT
72 if (autocvar_g_logTimestamps)
73 print(sprintf("%s ^9%f ^7%s\n", CG_LOG, time, msg));
74 else
75 print(sprintf("%s ^7%s\n", CG_LOG, msg));
76#endif
77#ifdef SERVER
78 if (autocvar_g_logTimestamps)
79 print(sprintf("%s ^9%f ^7%s\n", SV_LOG, time, msg));
80 else
81 print(sprintf("%s ^7%s\n", SV_LOG, msg));
82#endif
83#ifdef MENU
84 if (autocvar_g_logTimestamps)
85 print(sprintf("%s ^9%f ^7%s\n", UI_LOG, time, msg));
86 else
87 print(sprintf("%s ^7%s\n", UI_LOG, msg));
88#endif
89}
90
91void
92_ncError(string functionName, string msg)
93{
94#ifdef CLIENT
95 if (autocvar_g_logTimestamps)
96 print(sprintf("%s ^9%f ^1%s^1: %s\n", CG_ERROR, time, functionName, msg));
97 else
98 print(sprintf("%s ^1%s^1: %s\n", CG_ERROR, functionName, msg));
99#endif
100#ifdef SERVER
101 if (autocvar_g_logTimestamps)
102 print(sprintf("%s ^9%f ^1%s^1: %s\n", SV_ERROR, time, functionName, msg));
103 else
104 print(sprintf("%s ^1%s^1: %s\n", SV_ERROR, functionName, msg));
105#endif
106#ifdef MENU
107 if (autocvar_g_logTimestamps)
108 print(sprintf("%s ^9%f ^1%s^1: %s\n", UI_ERROR, time, functionName, msg));
109 else
110 print(sprintf("%s ^1%s^1: %s\n", UI_ERROR, functionName, msg));
111#endif
112}
113
114void
115_ncWarning(string functionName, string msg)
116{
117#ifdef CLIENT
118 if (autocvar_g_logTimestamps)
119 print(sprintf("%s ^9%f ^3%s^1: %s\n", CG_WARNING, time, functionName, msg));
120 else
121 print(sprintf("%s ^3%s^1: %s\n", CG_WARNING, functionName, msg));
122#endif
123#ifdef SERVER
124 if (autocvar_g_logTimestamps)
125 print(sprintf("%s ^9%f ^3%s^1: %s\n", SV_WARNING, time, functionName, msg));
126 else
127 print(sprintf("%s ^3%s^1: %s\n", SV_WARNING, functionName, msg));
128#endif
129#ifdef MENU
130 if (autocvar_g_logTimestamps)
131 print(sprintf("%s ^9%f ^3%s^1: %s\n", UI_WARNING, time, functionName, msg));
132 else
133 print(sprintf("%s ^3%s^1: %s\n", UI_WARNING, functionName, msg));
134#endif
135}
136
137void
138_NSAssert(bool condition, string function, string descr)
139{
140#ifdef CLIENT
141 if (!condition) {
142 print(strcat(CG_ERROR, " ^1Assertion failed in ", function, ", reason: ", descr, "\n"));
143 breakpoint();
144 }
145#endif
146#ifdef SERVER
147 if (!condition) {
148 print(strcat(SV_ERROR, " ^1Assertion failed in ", function, ", reason: ", descr, "\n"));
149 breakpoint();
150 }
151#endif
152#ifdef MENU
153 if (!condition) {
154 print(strcat(UI_ERROR, " ^1Assertion failed in ", function, ", reason: ", descr, "\n"));
155 }
156#endif
157}
158
163#define ncLog(...) if (autocvar_g_logLevel >= LOGLEVEL_DEBUG) _ncLog(sprintf(__VA_ARGS__))
164
169#define ncLogAlways(...) _ncLog(sprintf(__VA_ARGS__))
170
175#define ncError(...) if (autocvar_g_logLevel >= LOGLEVEL_ERRORS) _ncError(__FUNC__, sprintf(__VA_ARGS__))
176
181#define ncWarning(...) if (autocvar_g_logLevel >= LOGLEVEL_WARNINGS) _ncWarning(__FUNC__, sprintf(__VA_ARGS__))
182
188
189#define NSAssert(condition, ...) if (autocvar_g_logLevel >= LOGLEVEL_ERRORS) _NSAssert(condition, __FUNC__, sprintf(__VA_ARGS__))
190
191typedef enumflags
192{
193 SEARCH_INSENSITIVE,
194 SEARCH_FULLPACKAGE,
195 SEARCH_ALLOWDUPES,
196 SEARCH_FORCESEARCH,
197 SEARCH_MULTISEARCH,
198 SEARCH_NAMESORT
199} searchFlags_t;
200
201const vector g_vec_null = [0.0f, 0.0f, 0.0f];
202
203/* the console needs some attention too. */
204var float g_initTime;
205
206void
207InitPrint(string functionName)
208{
209 int chars = 51i;
210 int charsLeft;
211 int charExtra;
212 string sideLeft = "";
213 string sideRight = "";
214
215 if (functionName == __NULL__) {
216 ncLog("---------------------------------------------------");
217 return;
218 }
219
220 /* word and padding */
221 chars = chars - (int)strlen(functionName) - 2i;
222 charsLeft = chars / 2i;
223 charExtra = chars % 2i;
224
225 for (int i = 0i; i < charsLeft; i++)
226 sideLeft = strcat(sideLeft,"-");
227
228 for (int i = 0i; i < (charsLeft + charExtra); i++) {
229 sideRight = strcat(sideRight,"-");
230 }
231
232 ncLogAlways( "%s %s %s", sideLeft, functionName, sideRight);
233}
234
235var string g_lastInitFunc;
236void
237_InitStart(string functionName)
238{
239 if (g_initTime != 0)
240 error(sprintf("Called InitStart() without InitEnd()ing %s!", g_lastInitFunc));
241
242 InitPrint(functionName);
243 g_lastInitFunc = functionName;
244 g_initTime = gettime(1);
245}
246
247#define InitStart() _InitStart(__FUNC__)
248
249void
250_InitEnd(void)
251{
252 float endTime = gettime(1);
253 ncLogAlways("loaded in %.1f seconds", (endTime - g_initTime));
254 ncLogAlways("---------------------------------------------------");
255 g_initTime = 0;
256}
257
258#define InitEnd() _InitEnd()
259
261#define entity_def(x, ...) const string x[] = { __VA_ARGS__ }
262
264#define thread(x) if (fork()) { x; abort(); }
265
266#define STRING_SET(x) ((x != __NULL__) && (x != ""))
267
268bool
269fileExists(string filePath)
270{
271 if (filePath != "") /* not empty */
272 if not(whichpack(filePath)) /* not present on disk */
273 return false;
274
275 return true;
276}
277
278string
279fileExtensionFromString(string inputString)
280{
281 int modelNameLength = strlen(inputString);
282 return substring(inputString, modelNameLength - 3, -1);
283}
284
285#define Util_ExtensionFromString fileExtensionFromString
286
287bool
288wordInString(string fullString, string wordToFind)
289{
290 int wordCount = tokenize(fullString);
291
292 for (int i = 0; i < wordCount; i++) {
293 if (wordToFind == argv(i)) {
294 return (true);
295 }
296 }
297
298 return (false);
299}
300
301void
302CallSpawnfuncByName(entity target, string className)
303{
304 entity oldSelf = self;
305 string spawnClass = strcat("spawnfunc_", className);
306 self = target;
307 callfunction(spawnClass);
308 self = oldSelf;
309}
310
311.string spawnclass;
312.float team_info;
313 // end of common