Nuclide
Software Development Kit for id Technology (BETA)
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
21#define PROGS_TIME time
22
27var bool autocvar_g_logTimestamps = false;
28
29typedef entity id;
30typedef float musictrack_t;
31
32typedef enum
33{
34 LOGLEVEL_NONE,
35 LOGLEVEL_ERRORS,
36 LOGLEVEL_WARNINGS,
37 LOGLEVEL_DEBUG,
38} logLevel_t;
39
40#define LOGLEVEL_DEFAULT LOGLEVEL_WARNINGS
41var logLevel_t autocvar_g_logLevel = LOGLEVEL_DEFAULT;
42
43#define printf(...) print(sprintf(__VA_ARGS__))
44
45#ifdef DOXYGEN
47#define enumflags enum
48#endif
49
50#define ICN_SIZE 8
51
52string
53imageToConsole(string imageName, int imgSize, string toolTip)
54{
55 return sprintf("^[\\img\\%s\\s\\%i\\tip\\%s^]", imageName, imgSize, toolTip);
56}
57
58#define CG_LOG imageToConsole("gfx/icon16/monitor", ICN_SIZE, "Client Game Log")
59#define CG_WARNING imageToConsole("gfx/icon16/error", ICN_SIZE, "Client Game Warning")
60#define CG_ERROR imageToConsole("gfx/icon16/exclamation", ICN_SIZE, "Client Game Error")
61
62#define SV_LOG imageToConsole("gfx/icon16/server", ICN_SIZE, "Server Game Log")
63#define SV_WARNING imageToConsole("gfx/icon16/error", ICN_SIZE, "Server Game Warning")
64#define SV_ERROR imageToConsole("gfx/icon16/exclamation", ICN_SIZE, "Server Game Error")
65
66#define UI_LOG imageToConsole("gfx/icon16/picture", ICN_SIZE, "Menu Game Log")
67#define UI_WARNING imageToConsole("gfx/icon16/error", ICN_SIZE, "Menu Game Warning")
68#define UI_ERROR imageToConsole("gfx/icon16/exclamation", ICN_SIZE, "Menu Game Error")
69
70#define RULE_LOG imageToConsole("gfx/icon16/script_go", ICN_SIZE, "RuleC Log")
71#define RULE_WARNING imageToConsole("gfx/icon16/error", ICN_SIZE, "RuleC Warning")
72#define RULE_ERROR imageToConsole("gfx/icon16/exclamation", ICN_SIZE, "RuleC Error")
73
74#define MAP_LOG imageToConsole("gfx/icon16/map_go", ICN_SIZE, "MapC Log")
75#define MAP_WARNING imageToConsole("gfx/icon16/error", ICN_SIZE, "MapC Warning")
76#define MAP_ERROR imageToConsole("gfx/icon16/exclamation", ICN_SIZE, "MapC Error")
77
78#define HUD_LOG imageToConsole("gfx/icon16/monitor_go", ICN_SIZE, "HudC Log")
79#define HUD_WARNING imageToConsole("gfx/icon16/error", ICN_SIZE, "HudC Warning")
80#define HUD_ERROR imageToConsole("gfx/icon16/exclamation", ICN_SIZE, "HudC Error")
81
82#define ADDON_LOG imageToConsole("gfx/icon16/plugin_go", ICN_SIZE, "AddonC Log")
83#define ADDON_WARNING imageToConsole("gfx/icon16/error", ICN_SIZE, "AddonC Warning")
84#define ADDON_ERROR imageToConsole("gfx/icon16/exclamation", ICN_SIZE, "AddonC Error")
85
86void
87_ncLog(string msg)
88{
89#ifdef CLIENT
90 if (autocvar_g_logTimestamps)
91 print(sprintf("%s ^9%f ^7%s\n", CG_LOG, PROGS_TIME, msg));
92 else
93 print(sprintf("%s ^7%s\n", CG_LOG, msg));
94#endif
95#ifdef SERVER
96 if (autocvar_g_logTimestamps)
97 print(sprintf("%s ^9%f ^7%s\n", SV_LOG, PROGS_TIME, msg));
98 else
99 print(sprintf("%s ^7%s\n", SV_LOG, msg));
100#endif
101#ifdef MENU
102 if (autocvar_g_logTimestamps)
103 print(sprintf("%s ^9%f ^7%s\n", UI_LOG, PROGS_TIME, msg));
104 else
105 print(sprintf("%s ^7%s\n", UI_LOG, msg));
106#endif
107#ifdef RULEC
108 if (autocvar_g_logTimestamps)
109 print(sprintf("%s ^9%f ^7%s\n", RULE_LOG, PROGS_TIME, msg));
110 else
111 print(sprintf("%s ^7%s\n", RULE_LOG, msg));
112#endif
113#ifdef MAPC
114 if (autocvar_g_logTimestamps)
115 print(sprintf("%s ^9%f ^7%s\n", MAP_LOG, PROGS_TIME, msg));
116 else
117 print(sprintf("%s ^7%s\n", MAP_LOG, msg));
118#endif
119#ifdef HUDC
120 if (autocvar_g_logTimestamps)
121 print(sprintf("%s ^9%f ^7%s\n", HUD_LOG, PROGS_TIME, msg));
122 else
123 print(sprintf("%s ^7%s\n", HUD_LOG, msg));
124#endif
125#ifdef ADDONC
126 if (autocvar_g_logTimestamps)
127 print(sprintf("%s ^9%f ^7%s\n", ADDON_LOG, PROGS_TIME, msg));
128 else
129 print(sprintf("%s ^7%s\n", ADDON_LOG, msg));
130#endif
131}
132
133void
134_ncError(string functionName, string msg)
135{
136#ifdef CLIENT
137 if (autocvar_g_logTimestamps)
138 print(sprintf("%s ^9%f ^1%s^1: %s\n", CG_ERROR, PROGS_TIME, functionName, msg));
139 else
140 print(sprintf("%s ^1%s^1: %s\n", CG_ERROR, functionName, msg));
141#endif
142#ifdef SERVER
143 if (autocvar_g_logTimestamps)
144 print(sprintf("%s ^9%f ^1%s^1: %s\n", SV_ERROR, PROGS_TIME, functionName, msg));
145 else
146 print(sprintf("%s ^1%s^1: %s\n", SV_ERROR, functionName, msg));
147#endif
148#ifdef MENU
149 if (autocvar_g_logTimestamps)
150 print(sprintf("%s ^9%f ^1%s^1: %s\n", UI_ERROR, PROGS_TIME, functionName, msg));
151 else
152 print(sprintf("%s ^1%s^1: %s\n", UI_ERROR, functionName, msg));
153#endif
154#ifdef RULEC
155 if (autocvar_g_logTimestamps)
156 print(sprintf("%s ^9%f ^1%s^1: %s\n", RULE_ERROR, PROGS_TIME, functionName, msg));
157 else
158 print(sprintf("%s ^1%s^1: %s\n", RULE_ERROR, functionName, msg));
159#endif
160#ifdef MAPC
161 if (autocvar_g_logTimestamps)
162 print(sprintf("%s ^9%f ^1%s^1: %s\n", MAP_ERROR, PROGS_TIME, functionName, msg));
163 else
164 print(sprintf("%s ^1%s^1: %s\n", MAP_ERROR, functionName, msg));
165#endif
166#ifdef HUDC
167 if (autocvar_g_logTimestamps)
168 print(sprintf("%s ^9%f ^1%s^1: %s\n", HUD_ERROR, PROGS_TIME, functionName, msg));
169 else
170 print(sprintf("%s ^1%s^1: %s\n", HUD_ERROR, functionName, msg));
171#endif
172#ifdef ADDONC
173 if (autocvar_g_logTimestamps)
174 print(sprintf("%s ^9%f ^1%s^1: %s\n", ADDON_ERROR, PROGS_TIME, functionName, msg));
175 else
176 print(sprintf("%s ^1%s^1: %s\n", ADDON_ERROR, functionName, msg));
177#endif
178}
179
180void
181_ncWarning(string functionName, string msg)
182{
183#ifdef CLIENT
184 if (autocvar_g_logTimestamps)
185 print(sprintf("%s ^9%f ^3%s^1: %s\n", CG_WARNING, PROGS_TIME, functionName, msg));
186 else
187 print(sprintf("%s ^3%s^1: %s\n", CG_WARNING, functionName, msg));
188#endif
189#ifdef SERVER
190 if (autocvar_g_logTimestamps)
191 print(sprintf("%s ^9%f ^3%s^1: %s\n", SV_WARNING, PROGS_TIME, functionName, msg));
192 else
193 print(sprintf("%s ^3%s^1: %s\n", SV_WARNING, functionName, msg));
194#endif
195#ifdef MENU
196 if (autocvar_g_logTimestamps)
197 print(sprintf("%s ^9%f ^3%s^1: %s\n", UI_WARNING, PROGS_TIME, functionName, msg));
198 else
199 print(sprintf("%s ^3%s^1: %s\n", UI_WARNING, functionName, msg));
200#endif
201#ifdef RULEC
202 if (autocvar_g_logTimestamps)
203 print(sprintf("%s ^9%f ^3%s^1: %s\n", RULE_WARNING, PROGS_TIME, functionName, msg));
204 else
205 print(sprintf("%s ^3%s^1: %s\n", RULE_WARNING, functionName, msg));
206#endif
207#ifdef MAPC
208 if (autocvar_g_logTimestamps)
209 print(sprintf("%s ^9%f ^3%s^1: %s\n", MAP_WARNING, PROGS_TIME, functionName, msg));
210 else
211 print(sprintf("%s ^3%s^1: %s\n", MAP_WARNING, functionName, msg));
212#endif
213#ifdef HUDC
214 if (autocvar_g_logTimestamps)
215 print(sprintf("%s ^9%f ^3%s^1: %s\n", HUD_WARNING, PROGS_TIME, functionName, msg));
216 else
217 print(sprintf("%s ^3%s^1: %s\n", HUD_WARNING, functionName, msg));
218#endif
219#ifdef ADDONC
220 if (autocvar_g_logTimestamps)
221 print(sprintf("%s ^9%f ^3%s^1: %s\n", ADDON_WARNING, PROGS_TIME, functionName, msg));
222 else
223 print(sprintf("%s ^3%s^1: %s\n", ADDON_WARNING, functionName, msg));
224#endif
225}
226
227void
228_NSAssert(bool condition, string function, string descr)
229{
230#ifdef CLIENT
231 if (!condition) {
232 print(strcat(CG_ERROR, " ^1Assertion failed in ", function, ", reason: ", descr, "\n"));
233 breakpoint();
234 }
235#endif
236#ifdef SERVER
237 if (!condition) {
238 print(strcat(SV_ERROR, " ^1Assertion failed in ", function, ", reason: ", descr, "\n"));
239 breakpoint();
240 }
241#endif
242#ifdef MENU
243 if (!condition) {
244 print(strcat(UI_ERROR, " ^1Assertion failed in ", function, ", reason: ", descr, "\n"));
245 }
246#endif
247#ifdef RULEC
248 if (!condition) {
249 print(strcat(RULE_ERROR, " ^1Assertion failed in ", function, ", reason: ", descr, "\n"));
250 }
251#endif
252#ifdef MAPC
253 if (!condition) {
254 print(strcat(MAP_ERROR, " ^1Assertion failed in ", function, ", reason: ", descr, "\n"));
255 }
256#endif
257#ifdef HUDC
258 if (!condition) {
259 print(strcat(HUD_ERROR, " ^1Assertion failed in ", function, ", reason: ", descr, "\n"));
260 }
261#endif
262#ifdef ADDONC
263 if (!condition) {
264 print(strcat(ADDON_ERROR, " ^1Assertion failed in ", function, ", reason: ", descr, "\n"));
265 }
266#endif
267}
268
273#define ncLog(...) if (autocvar_g_logLevel >= LOGLEVEL_DEBUG) _ncLog(sprintf(__VA_ARGS__))
274
278#define ncLogAlways(...) _ncLog(sprintf(__VA_ARGS__))
279
284#define ncError(...) if (autocvar_g_logLevel >= LOGLEVEL_ERRORS) _ncError(__FUNC__, sprintf(__VA_ARGS__))
285
290#define ncWarning(...) if (autocvar_g_logLevel >= LOGLEVEL_WARNINGS) _ncWarning(__FUNC__, sprintf(__VA_ARGS__))
291
298#define NSAssert(condition, ...) if (autocvar_g_logLevel >= LOGLEVEL_ERRORS) _NSAssert(condition, __FUNC__, sprintf(__VA_ARGS__))
299
300typedef enumflags
301{
302 SEARCH_INSENSITIVE,
303 SEARCH_FULLPACKAGE,
304 SEARCH_ALLOWDUPES,
305 SEARCH_FORCESEARCH,
306 SEARCH_MULTISEARCH,
307 SEARCH_NAMESORT
308} searchFlags_t;
309
310const entity g_entity_null = __NULL__;
311const float g_float_null = 0.0f;
312const int g_int_null = 0i;
313const string g_string_null = "";
314const vector g_vec_null = [0.0f, 0.0f, 0.0f];
315
316void
317InitPrint(string functionName)
318{
319 static int chars = 51i;
320 int charsLeft;
321 int charExtra;
322 string sideLeft = "";
323 string sideRight = "";
324
325 if (functionName == __NULL__) {
326 ncLog("---------------------------------------------------");
327 return;
328 }
329
330 /* word and padding */
331 chars = chars - (int)strlen(functionName) - 2i;
332 charsLeft = chars / 2i;
333 charExtra = chars % 2i;
334
335 for (int i = 0i; i < charsLeft; i++)
336 sideLeft = strcat(sideLeft,"-");
337
338 for (int i = 0i; i < (charsLeft + charExtra); i++) {
339 sideRight = strcat(sideRight,"-");
340 }
341
342 ncLogAlways( "%s %s %s", sideLeft, functionName, sideRight);
343}
344
345#define InitStart() float local_initTime = gettime(1); InitPrint(__FUNC__)
346
347void
348_InitEnd(float oldTime, string functionName)
349{
350 float endTime = gettime(1);
351 ncLogAlways("%s loaded in %.1f seconds", functionName, (endTime - oldTime));
352 ncLogAlways("---------------------------------------------------");
353}
354
355#define InitEnd() _InitEnd(local_initTime, __FUNC__)
356
358#define entity_def(x, ...) const string x[] = { __VA_ARGS__ }
359
361#define thread(x) if (fork()) { x; abort(); }
362
363#define STRING_SET(x) ((x != __NULL__) && (x != ""))
364
365bool
366fileExists(string filePath)
367{
368 if (!STRING_SET(filePath)) {
369 return (false);
370 }
371
372 /* not present on disk */
373 if not(whichpack(filePath)) {
374 return (false);
375 }
376
377
378 return (true);
379}
380
381string
382fileExtensionFromString(string inputString)
383{
384 int modelNameLength = strlen(inputString);
385 int dotChar = -1;
386
387 for (int i = (modelNameLength - 1); i > 0; i--) {
388 if (str2chr(inputString, i) == '.') {
389 dotChar = i;
390 break;
391 }
392 }
393
394 if (dotChar != -1) {
395 return substring(inputString, dotChar + 1, -1);
396 }
397
398 return (__NULL__);
399}
400
401#define Util_ExtensionFromString fileExtensionFromString
402
403bool
404wordInString(string fullString, string wordToFind)
405{
406 int wordCount = tokenize(fullString);
407
408 for (int i = 0; i < wordCount; i++) {
409 if (wordToFind == argv(i)) {
410 return (true);
411 }
412 }
413
414 return (false);
415}
416
417void
418CallSpawnfuncByName(entity target, string className)
419{
420 entity oldSelf = self;
421 string spawnClass = strcat("spawnfunc_", className);
422 self = target;
423 callfunction(spawnClass);
424 self = oldSelf;
425}
426
427.string spawnclass;
428.float team_info;
429 // end of common
typedef enumflags
Defines the valid alignment flags for text fields.
Definition: font.h:37