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