Nuclide
Software Development Kit for id Technology (BETA)
defs.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
25var bool autocvar_g_logTimestamps = false;
26
27typedef entity id;
28
29typedef enum
30{
31 LOGLEVEL_NONE,
32 LOGLEVEL_ERRORS,
33 LOGLEVEL_WARNINGS,
34 LOGLEVEL_DEBUG,
35} logLevel_t;
36
37#define LOGLEVEL_DEFAULT LOGLEVEL_WARNINGS
38var logLevel_t autocvar_g_logLevel = LOGLEVEL_DEFAULT;
39
40#define printf(...) print(sprintf(__VA_ARGS__))
41
42#ifdef DOXYGEN
44#define enumflags enum
45#endif
46
47#define ICN_SIZE 8
48
49string
50imageToConsole(string imageName, int imgSize, string toolTip)
51{
52 return sprintf("^[\\img\\%s\\s\\%i\\tip\\%s^]", imageName, imgSize, toolTip);
53}
54
55#define CG_LOG imageToConsole("gfx/icon16/monitor", ICN_SIZE, "Client Game Log")
56#define CG_WARNING imageToConsole("gfx/icon16/error", ICN_SIZE, "Client Game Warning")
57#define CG_ERROR imageToConsole("gfx/icon16/exclamation", ICN_SIZE, "Client Game Error")
58
59#define SV_LOG imageToConsole("gfx/icon16/server", ICN_SIZE, "Server Game Log")
60#define SV_WARNING imageToConsole("gfx/icon16/error", ICN_SIZE, "Server Game Warning")
61#define SV_ERROR imageToConsole("gfx/icon16/exclamation", ICN_SIZE, "Server Game Error")
62
63#define UI_LOG imageToConsole("gfx/icon16/picture", ICN_SIZE, "Menu Game Log")
64#define UI_WARNING imageToConsole("gfx/icon16/error", ICN_SIZE, "Menu Game Warning")
65#define UI_ERROR imageToConsole("gfx/icon16/exclamation", ICN_SIZE, "Menu Game Error")
66
67void
68_ncLog(string msg)
69{
70#ifdef CLIENT
71 if (autocvar_g_logTimestamps)
72 print(sprintf("%s ^9%f ^7%s\n", CG_LOG, time, msg));
73 else
74 print(sprintf("%s ^7%s\n", CG_LOG, msg));
75#endif
76#ifdef SERVER
77 if (autocvar_g_logTimestamps)
78 print(sprintf("%s ^9%f ^7%s\n", SV_LOG, time, msg));
79 else
80 print(sprintf("%s ^7%s\n", SV_LOG, msg));
81#endif
82#ifdef MENU
83 if (autocvar_g_logTimestamps)
84 print(sprintf("%s ^9%f ^7%s\n", UI_LOG, time, msg));
85 else
86 print(sprintf("%s ^7%s\n", UI_LOG, msg));
87#endif
88}
89
90void
91_ncError(string functionName, string msg)
92{
93#ifdef CLIENT
94 if (autocvar_g_logTimestamps)
95 print(sprintf("%s ^9%f ^1%s^7: %s\n", CG_ERROR, time, functionName, msg));
96 else
97 print(sprintf("%s ^1%s^7: %s\n", CG_ERROR, functionName, msg));
98#endif
99#ifdef SERVER
100 if (autocvar_g_logTimestamps)
101 print(sprintf("%s ^9%f ^1%s^7: %s\n", SV_ERROR, time, functionName, msg));
102 else
103 print(sprintf("%s ^1%s^7: %s\n", SV_ERROR, functionName, msg));
104#endif
105#ifdef MENU
106 if (autocvar_g_logTimestamps)
107 print(sprintf("%s ^9%f ^1%s^7: %s\n", UI_ERROR, time, functionName, msg));
108 else
109 print(sprintf("%s ^1%s^7: %s\n", UI_ERROR, functionName, msg));
110#endif
111}
112
113void
114_ncWarning(string functionName, string msg)
115{
116#ifdef CLIENT
117 if (autocvar_g_logTimestamps)
118 print(sprintf("%s ^9%f ^3%s^7: %s\n", CG_WARNING, time, functionName, msg));
119 else
120 print(sprintf("%s ^3%s^7: %s\n", CG_WARNING, functionName, msg));
121#endif
122#ifdef SERVER
123 if (autocvar_g_logTimestamps)
124 print(sprintf("%s ^9%f ^3%s^7: %s\n", SV_WARNING, time, functionName, msg));
125 else
126 print(sprintf("%s ^3%s^7: %s\n", SV_WARNING, functionName, msg));
127#endif
128#ifdef MENU
129 if (autocvar_g_logTimestamps)
130 print(sprintf("%s ^9%f ^3%s^7: %s\n", UI_WARNING, time, functionName, msg));
131 else
132 print(sprintf("%s ^3%s^7: %s\n", UI_WARNING, functionName, msg));
133#endif
134}
135
136void
137_NSAssert(bool condition, string function, string descr)
138{
139#ifdef CLIENT
140 if (!condition) {
141 print(strcat(CG_ERROR, " ^1Assertion failed in ", function, ", reason: ", descr, "\n"));
142 breakpoint();
143 }
144#endif
145#ifdef SERVER
146 if (!condition) {
147 print(strcat(SV_ERROR, " ^1Assertion failed in ", function, ", reason: ", descr, "\n"));
148 breakpoint();
149 }
150#endif
151#ifdef MENU
152 if (!condition) {
153 print(strcat(UI_ERROR, " ^1Assertion failed in ", function, ", reason: ", descr, "\n"));
154 }
155#endif
156}
157
162#define ncLog(...) if (autocvar_g_logLevel >= LOGLEVEL_DEBUG) _ncLog(sprintf(__VA_ARGS__))
163
168#define ncError(...) if (autocvar_g_logLevel >= LOGLEVEL_ERRORS) _ncError(__FUNC__, sprintf(__VA_ARGS__))
169
174#define ncWarning(...) if (autocvar_g_logLevel >= LOGLEVEL_WARNINGS) _ncWarning(__FUNC__, sprintf(__VA_ARGS__))
175
182#define NSAssert(condition, ...) if (autocvar_g_logLevel >= LOGLEVEL_ERRORS) _NSAssert(condition, __FUNC__, sprintf(__VA_ARGS__))
183
184typedef enumflags
185{
186 SEARCH_INSENSITIVE,
187 SEARCH_FULLPACKAGE,
188 SEARCH_ALLOWDUPES,
189 SEARCH_FORCESEARCH,
190 SEARCH_MULTISEARCH,
191 SEARCH_NAMESORT
192} searchFlags_t;
193
194const vector g_vec_null = [0.0f, 0.0f, 0.0f];
195
196/* the console needs some attention too. */
197var float g_initTime;
198
199void
200InitPrint(string functionName)
201{
202 int chars = 51i;
203 int charsLeft;
204 int charExtra;
205 string sideLeft = "";
206 string sideRight = "";
207
208 if (functionName == __NULL__) {
209 ncLog("---------------------------------------------------");
210 return;
211 }
212
213 /* word and padding */
214 chars = chars - (int)strlen(functionName) - 2i;
215 charsLeft = chars / 2i;
216 charExtra = chars % 2i;
217
218 for (int i = 0i; i < charsLeft; i++)
219 sideLeft = strcat(sideLeft,"-");
220
221 for (int i = 0i; i < (charsLeft + charExtra); i++) {
222 sideRight = strcat(sideRight,"-");
223 }
224
225 ncLog( "%s %s %s", sideLeft, functionName, sideRight);
226}
227
228var string g_lastInitFunc;
229void
230_InitStart(string functionName)
231{
232 if (g_initTime != 0)
233 error(sprintf("Called InitStart() without InitEnd()ing %s!", g_lastInitFunc));
234
235 InitPrint(functionName);
236 g_lastInitFunc = functionName;
237 g_initTime = gettime(1);
238}
239
240#define InitStart() _InitStart(__FUNC__)
241
242void
243_InitEnd(void)
244{
245 float endTime = gettime(1);
246 ncLog("loaded in %.1f seconds", (endTime - g_initTime));
247 ncLog("---------------------------------------------------");
248 g_initTime = 0;
249}
250
251#define InitEnd() _InitEnd()
252
254#define entity_def(x, ...) const string x[] = { __VA_ARGS__ }
255
257#define thread(x) if (fork()) { x; abort(); }
258
259#define STRING_SET(x) ((x != __NULL__) && (x != ""))
260
261bool
262fileExists(string filePath)
263{
264 if (filePath != "") /* not empty */
265 if not(whichpack(filePath)) /* not present on disk */
266 return false;
267
268 return true;
269}
270string
271Util_ExtensionFromString(string inputString)
272{
273 int modelNameLength = strlen(inputString);
274 return substring(inputString, modelNameLength - 3, modelNameLength);
275}
276
277void
278CallSpawnfuncByName(entity target, string className)
279{
280 entity oldSelf = self;
281 string spawnClass = strcat("spawnfunc_", className);
282 self = target;
283 callfunction(spawnClass);
284 self = oldSelf;
285}
286
287.string spawnclass;
288 // end of common
290
291#include "c_math.h"
292#include "c_math_vector.h"
293#include "c_memory.h"
294#include "c_platform.h"
295#include "c_colors.h"
296#include "c_materials.h"
297#include "c_teams.h"
298#include "c_events.h"
299#include "c_flags.h"
typedef enumflags
Defines the valid alignment flags for text fields.
Definition: font.h:37
string Util_ExtensionFromString(string inputString)
Extract the file extension from a given file name string.
Definition: defs.h:271