]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/debug.qh
Add new HUD icons for overkill and assault
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / debug.qh
1 #pragma once
2
3
4 // This includes some functions useful for debugging.
5 // Some more bot-specific ones are in server/pathlib/debug.qc.
6 // Look for other useful commands under prvm_* in console (apropos / search).
7
8
9 #ifdef CSQC
10 .entity tag_entity;
11 #endif
12
13
14 #ifdef GAMEQC
15 .bool debug;
16 .int sv_entnum;
17 REGISTER_NET_TEMP(net_debug)
18 #endif
19
20 #ifdef CSQC
21         NET_HANDLE(net_debug, bool isNew)
22         {
23                 Net_Accept(net_debug);
24                 this.sv_entnum = ReadShort();
25                 if (ReadByte()) make_pure(this);
26                 this.origin = ReadVector();
27                 setorigin(this, this.origin);
28                 this.debug = true;  // identify server entities by this
29                 this.classname = strzone(ReadString());
30                 this.sourceLoc = strzone(ReadString());
31                 return true;
32         }
33 #endif
34
35 #ifdef SVQC
36         bool debug_send(entity this, entity to, int sf)
37         {
38                 int channel = MSG_ONE;
39                 msg_entity = to;
40                 WriteHeader(channel, net_debug);
41                 WriteShort(channel, etof(this));
42                 WriteByte(channel, is_pure(this));
43                 vector o = this.origin;
44                 if (o == '0 0 0') // brushes
45                         o = (this.absmin + this.absmax) / 2;
46                 if (this.tag_entity)
47                         o += this.tag_entity.origin;
48                 WriteVector(channel, o);
49                 WriteString(channel, this.classname);
50                 WriteString(channel, this.sourceLoc);
51                 return true;
52         }
53 #endif
54
55
56 #if ENABLE_DEBUGDRAW
57 #ifdef GAMEQC
58 /**
59  * 0: off
60  * 1: on
61  * 2: on (pure)
62  * 3: on (.entnum != 0)
63  * 4: on (.origin == '0 0 0')
64  * 5: on (.debug != 0), server only
65  * 6: on (.solid != 0)
66  */
67 bool autocvar_debugdraw;
68 #endif
69
70 #ifdef CSQC
71         string autocvar_debugdraw_filter, autocvar_debugdraw_filterout;
72         .int debugdraw_last;
73         vector project_3d_to_2d(vector vec);
74         void Debug_Draw()
75         {
76                 if (!autocvar_debugdraw) return;
77                 static int debugdraw_frame;
78                 ++debugdraw_frame;
79                 const int sz = 8;
80                 FOREACH_ENTITY(true, {
81                         if (it.debugdraw_last == debugdraw_frame) continue;
82                         int ofs = 0;
83                         FOREACH_ENTITY_RADIUS(it.origin, 100, it.debugdraw_last != debugdraw_frame, {
84                                 it.debugdraw_last = debugdraw_frame;
85                                 vector rgb = (it.debug) ? '0 0 1' : '1 0 0';
86                                 if (autocvar_debugdraw_filterout != "" && strhasword(autocvar_debugdraw_filterout, it.classname)) continue;
87                                 if (autocvar_debugdraw_filter != "" && !strhasword(autocvar_debugdraw_filter, it.classname)) continue;
88                                 if (autocvar_debugdraw == 3)
89                                 {
90                                         if (!it.entnum) continue;
91                                 }
92                                 if (autocvar_debugdraw == 4)
93                                 {
94                                         if (it.origin) continue;
95                                 }
96                                 if (autocvar_debugdraw == 5)
97                                 {
98                                         if (!it.debug) continue;
99                                 }
100                                 else if (autocvar_debugdraw > 5)
101                                 {
102                                         bool flag = true;
103                                         do {
104 //                                              if (it.modelindex) break;
105 //                                              if (it.absmin) break;
106 //                                              if (it.absmax) break;
107 //                                              if (it.entnum) break;
108 //                                              if (it.drawmask) break;
109 //                                              if (it.predraw) break;
110 //                                              if (it.move_movetype) break;
111                                                 if (it.solid) break;
112 //                                              if (it.origin) break;
113 //                                              if (it.oldorigin) break;
114 //                                              if (it.velocity) break;
115 //                                              if (it.angles) break;
116 //                                              if (it.avelocity) break;
117 //                                              if (it.classname) break;
118 //                                              if (it.model) break;
119 //                                              if (it.frame) break;
120 //                                              if (it.skin) break;
121 //                                              if (it.effects) break;
122 //                                              if (it.mins) break;
123 //                                              if (it.maxs) break;
124 //                                              if (it.size) break;
125 //                                              if (it.touch) break;
126 //                                              if (it.use) break;
127 //                                              if (it.think) break;
128 //                                              if (it.blocked) break;
129 //                                              if (it.nextthink) break;
130 //                                              if (it.chain) break;
131 //                                              if (it.netname) break;
132 //                                              if (it.enemy) break;
133 //                                              if (it.flags) break;
134 //                                              if (it.colormap) break;
135 //                                              if (it.owner) break;
136                                                 flag = false;
137                                         } while (0);
138                                         if (!flag) continue;
139                                 }
140                                 else if (is_pure(it))
141                                 {
142                                         if (autocvar_debugdraw < 2) continue;
143                                         rgb.y = 1;
144                                 }
145                                 vector o = it.origin;
146                                 if (it.tag_entity)
147                                         o += it.tag_entity.origin;
148                                 vector pos = project_3d_to_2d(o);
149                                 if (pos.z < 0) continue;
150                                 pos.z = 0;
151                                 pos.y += ofs * sz;
152                                 drawcolorcodedstring2_builtin(pos,
153                                         sprintf("%d: '%s'@%s", (it.debug ? it.sv_entnum : etof(it)),
154                                         it.classname, it.sourceLoc),
155                                         sz * '1 1 0', rgb, 0.5, DRAWFLAG_NORMAL);
156                                 ++ofs;
157             });
158                 });
159         }
160 #endif
161
162
163 #ifdef SVQC
164         COMMON_COMMAND(debugdraw_sv, "Dump all server entities")
165         {
166                 switch (request)
167                 {
168                         case CMD_REQUEST_COMMAND:
169                         {
170                                 if (!autocvar_debugdraw) return;
171                                 int n = 1000;
172                                 int rem = n;
173                                 for (entity e = NULL; (e = findfloat(e, debug, 0)) && rem > 0; )
174                                 {
175                                         if (autocvar_debugdraw < 2 && is_pure(e)) continue;
176                                         debug_send(e, caller, 0);
177                                         e.debug = true;
178                                         --rem;
179                                 }
180                                 LOG_INFOF("%d server entities sent", n - rem);
181                                 return;
182                         }
183
184                         default:
185                         case CMD_REQUEST_USAGE:
186                         {
187                                 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " debugdraw_sv");
188                                 return;
189                         }
190                 }
191         }
192 #endif
193 #endif
194
195
196 GENERIC_COMMAND(bufstr_get, "Examine a string buffer object")
197 {
198         switch (request)
199         {
200                 case CMD_REQUEST_COMMAND:
201                 {
202                         int bufhandle = stof(argv(1));
203                         int string_index = stof(argv(2));
204                         string s = bufstr_get(bufhandle, string_index);
205                         LOG_INFOF("%s", s);
206                         return;
207                 }
208
209                 default:
210                 case CMD_REQUEST_USAGE:
211                 {
212                         LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " bufstr_get bufhandle string_index");
213                         return;
214                 }
215         }
216 }
217
218
219 GENERIC_COMMAND(version, "Print the current version")
220 {
221         switch (request)
222         {
223                 case CMD_REQUEST_COMMAND:
224                 {
225                         LOG_INFO(WATERMARK);
226                         return;
227                 }
228                 default:
229                 case CMD_REQUEST_USAGE:
230                 {
231                         LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " version");
232                         return;
233                 }
234         }
235 }
236
237
238 #ifdef CSQC
239 void(float bufhandle, string pattern, string antipattern) buf_cvarlist = #517;
240 #endif
241 GENERIC_COMMAND(cvar_localchanges, "Print locally changed cvars")
242 {
243         switch (request)
244         {
245                 case CMD_REQUEST_COMMAND:
246                 {
247                         string s = "";
248                         int h = buf_create();
249                         buf_cvarlist(h, "", "_"); // exclude all _ cvars as they are temporary
250                         int n = buf_getsize(h);
251                         for (int i = 0; i < n; ++i) {
252                                 string k = bufstr_get(h, i);
253                                 string v = cvar_string(k);
254                                 string d = cvar_defstring(k);
255                                 if (v == d)
256                                         continue;
257                                 s = strcat(s, k, " \"", v, "\" // \"", d, "\"\n");
258                         }
259                         buf_del(h);
260                         LOG_INFO(s);
261                         return;
262                 }
263                 default:
264                 case CMD_REQUEST_USAGE:
265                 {
266                         LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " cvar_localchanges");
267                         return;
268                 }
269         }
270 }
271
272
273 #if ENABLE_DEBUGTRACE
274 REGISTER_STAT(TRACE_ENT, int)
275 #ifdef SVQC
276 bool autocvar_debugtrace;
277
278 REGISTER_MUTATOR(trace, autocvar_debugtrace);
279
280 .bool debug_trace_button;
281 .int solid_prev;
282 MUTATOR_HOOKFUNCTION(trace, SV_StartFrame)
283 {
284         FOREACH_CLIENT(true, {
285                 bool skip = false;
286                 bool btn = PHYS_INPUT_BUTTON_HOOK(it);
287                 if (btn == it.debug_trace_button) skip = true;
288                 it.debug_trace_button = btn;
289                 if (!btn || skip) continue;
290                 FOREACH_ENTITY(true, {
291                     it.solid_prev = it.solid;
292                         it.solid = SOLID_BBOX;
293                 });
294                 vector forward = '0 0 0'; vector right = '0 0 0'; vector up = '0 0 0';
295                 MAKEVECTORS(makevectors, it.v_angle, forward, right, up);
296                 vector pos = it.origin + it.view_ofs;
297                 traceline(pos, pos + forward * max_shot_distance, MOVE_NORMAL, it);
298                 FOREACH_ENTITY(true, {
299                     it.solid = it.solid_prev;
300             it.solid_prev = 0;
301                 });
302                 entity e = trace_ent;
303                 int i = etof(e);
304                 STAT(TRACE_ENT, it) = i;
305                 if (!e) continue;
306                 setorigin(e, e.origin + '0 0 100');
307                 stuffcmd(it, sprintf("prvm_edict server %d\n", i));
308         });
309 }
310 #endif
311 #ifdef CSQC
312 entity TRACE_ENT;
313 void Trace_draw2d(entity this)
314 {
315         int e = STAT(TRACE_ENT);
316         if (!e) return;
317         vector pos = '0 0 0';
318         pos.y += vid_conheight / 2;
319         drawstring(pos, sprintf("prvm_edict server %d", e), '10 10 0', '1 1 1', 1, DRAWFLAG_NORMAL);
320 }
321
322 STATIC_INIT(TRACE_ENT)
323 {
324         entity e = TRACE_ENT = new_pure(TRACE_ENT);
325         e.draw2d = Trace_draw2d;
326         IL_PUSH(g_drawables_2d, e);
327 }
328 #endif
329 #endif
330
331
332 GENERIC_COMMAND(find, "Search through entities for matching classname")
333 {
334         switch (request)
335         {
336                 case CMD_REQUEST_COMMAND:
337                 {
338                         int entcnt = 0;
339                         FOREACH_ENTITY_CLASS_ORDERED(argv(1), true,
340                         {
341                                 LOG_INFOF("%i (%s)", it, it.classname);
342                                 ++entcnt;
343                         });
344                         if(entcnt)
345                                 LOG_INFOF("Found %d entities", entcnt);
346                         return;
347                 }
348
349                 default:
350                 {
351                         LOG_INFO("Incorrect parameters for ^2find^7");
352         }
353                 case CMD_REQUEST_USAGE:
354                 {
355                         LOG_INFO("Usage:^3 " GetProgramCommandPrefix() " find classname");
356                         LOG_INFO("  Where 'classname' is the classname to search for.");
357                         return;
358                 }
359         }
360 }
361
362
363 GENERIC_COMMAND(findat, "Search through entities for matching origin")
364 {
365         switch (request)
366         {
367                 case CMD_REQUEST_COMMAND:
368                 {
369                     vector match = stov(argv(1));
370                     FOREACH_ENTITY_ORDERED(it.origin == match, LOG_INFOF("%i (%s)", it, it.classname));
371                         return;
372                 }
373
374                 default:
375                         LOG_INFO("Incorrect parameters for ^2findat^7");
376                 case CMD_REQUEST_USAGE:
377                 {
378                         LOG_INFO("Usage:^3 " GetProgramCommandPrefix() " findat \"0 0 0\"");
379                         return;
380                 }
381         }
382 }
383
384
385 // debug_test() allows drawing text from server on the client anywhere in world coordinates.
386
387 #ifdef GAMEQC
388 REGISTER_NET_TEMP(debug_text_3d);
389 #endif
390
391 #ifdef CSQC
392
393 CLASS(DebugText3d, Object)
394         // reusing existing fields
395         ATTRIB(DebugText3d, origin, vector);
396         ATTRIB(DebugText3d, message, string); // the text (i wanted to use the .text field but then this whole macro-based-inheritance thing shat itself)
397         ATTRIB(DebugText3d, health, float); // text alignment (recycled field)
398         ATTRIB(DebugText3d, hit_time, float); // when it was created
399         ATTRIB(DebugText3d, fade_rate, float); // how fast is should disappear
400         ATTRIB(DebugText3d, velocity, vector);
401
402         CONSTRUCTOR(DebugText3d, vector pos, string msg, float align, float fade_rate_, vector vel) {
403                 CONSTRUCT(DebugText3d);
404                 this.origin = pos;
405                 this.message = strzone(msg);
406                 this.health = align;
407                 this.hit_time = time;
408                 this.fade_rate = fade_rate_;
409                 this.velocity = vel;
410                 IL_PUSH(g_drawables_2d, this);
411         }
412
413         DESTRUCTOR(DebugText3d) {
414                 strfree(this.message);
415         }
416
417         void DebugText3d_draw2d(DebugText3d this) {
418                 float since_created = time - this.hit_time;
419                 float alpha_ = 1 - since_created * this.fade_rate;
420
421                 if (alpha_ < 0) {
422                         delete(this);
423                         return;
424                 }
425
426                 int size = 8;
427                 vector screen_pos = project_3d_to_2d(this.origin) + since_created * this.velocity;
428                 float align = this.health;
429                 if (align > 0)
430                         screen_pos.x -= stringwidth(this.message, true, size * '1 1 0') * min(1, align);
431                 if (screen_pos.z < 0) return; // behind camera
432                 screen_pos.z = 0;
433
434                 vector rgb = '1 1 0';
435                 drawcolorcodedstring2_builtin(screen_pos, this.message, size * '1 1 0', rgb, alpha_, DRAWFLAG_NORMAL);
436         }
437         ATTRIB(DebugText3d, draw2d, void(DebugText3d), DebugText3d_draw2d);
438 ENDCLASS(DebugText3d)
439
440 NET_HANDLE(debug_text_3d, bool is_new) {
441         vector pos = ReadVector();
442         string msg = ReadString();
443         float align = ReadFloat();
444         float duration = ReadFloat();
445         vector vel = ReadVector();
446         make_impure(NEW(DebugText3d, pos, msg, align, 1 / duration, vel));
447         return true;
448 }
449
450 #endif // CSQC
451
452 #ifdef SVQC
453
454 // can't use autocvars because they give unused warning unless the macros are expanded
455 #define debug_text_3d(...) EVAL(OVERLOAD(debug_text_3d, __VA_ARGS__))
456 #define debug_text_3d_2(pos, msg) debug_text_3d_3(pos, msg, cvar("debug_text_3d_default_align"))
457 #define debug_text_3d_3(pos, msg, align) debug_text_3d_4(pos, msg, align, cvar("debug_text_3d_default_duration"))
458 #define debug_text_3d_4(pos, msg, align, dur) debug_text_3d_5(pos, msg, align, dur, stov(cvar_string("debug_text_3d_default_velocity")))
459 #define debug_text_3d_5(pos, msg, align, dur, vel) debug_text_3d_fn(pos, msg, align, dur, vel)
460
461 ERASEABLE
462 void debug_text_3d_fn(vector pos, string msg, float align, float duration, vector vel) {
463         WriteHeader(MSG_BROADCAST, debug_text_3d);
464         WriteVector(MSG_BROADCAST, pos);
465         WriteString(MSG_BROADCAST, msg);
466         WriteFloat(MSG_BROADCAST, align);
467         WriteFloat(MSG_BROADCAST, duration);
468         WriteVector(MSG_BROADCAST, vel);
469 }
470
471 #endif // SVQC