]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/debug.qh
Standardize command usage messages:
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / debug.qh
1 #pragma once
2
3 #ifdef CSQC
4 #include <client/draw.qh>
5 #include <client/resources.qh>
6 #include <client/view.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_INFOF("%i (%s)", it, it.classname);
350                                 ++entcnt;
351                         });
352                         if(entcnt)
353                                 LOG_INFOF("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 match = stov(argv(1));
376                     FOREACH_ENTITY_ORDERED(it.origin == match, LOG_INFOF("%i (%s)", it, it.classname));
377                         return;
378                 }
379
380                 default:
381                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
382                 case CMD_REQUEST_USAGE:
383                 {
384                         LOG_HELP("Usage:^3 " GetProgramCommandPrefix() " findat \"x y z\"");
385                         return;
386                 }
387         }
388 }
389
390
391 // debug_test() allows drawing text from server on the client anywhere in world coordinates.
392
393 #ifdef GAMEQC
394 REGISTER_NET_TEMP(debug_text_3d);
395 #endif
396
397 #ifdef CSQC
398
399 CLASS(DebugText3d, Object)
400         // reusing existing fields
401         ATTRIB(DebugText3d, origin, vector);
402         ATTRIB(DebugText3d, message, string); // the text (i wanted to use the .text field but then this whole macro-based-inheritance thing shat itself)
403         ATTRIB(DebugText3d, health, float); // text alignment (recycled field)
404         ATTRIB(DebugText3d, hit_time, float); // when it was created
405         ATTRIB(DebugText3d, fade_rate, float); // how fast it should disappear
406         ATTRIB(DebugText3d, velocity, vector);
407
408         CONSTRUCTOR(DebugText3d, vector pos, string msg, float align, float fade_rate_, vector vel) {
409                 CONSTRUCT(DebugText3d);
410                 this.origin = pos;
411                 this.message = strzone(msg);
412                 SetResource(this, RES_HEALTH, align);
413                 this.hit_time = time;
414                 this.fade_rate = fade_rate_;
415                 this.velocity = vel;
416                 IL_PUSH(g_drawables_2d, this);
417         }
418
419         DESTRUCTOR(DebugText3d) {
420                 strfree(this.message);
421         }
422
423         void DebugText3d_draw2d(DebugText3d this) {
424                 float since_created = time - this.hit_time;
425                 float alpha_ = 1 - since_created * this.fade_rate;
426
427                 if (alpha_ < 0) {
428                         delete(this);
429                         return;
430                 }
431
432                 int size = 11;
433                 vector screen_pos = project_3d_to_2d(this.origin) + since_created * this.velocity;
434                 if (screen_pos.z < 0) return; // behind camera
435
436                 screen_pos.z = 0;
437                 float align = GetResource(this, RES_HEALTH);
438                 string msg;
439                 vector msg_pos;
440
441                 int n = tokenizebyseparator(this.message, "\n");
442                 for(int k = 0; k < n; ++k)
443                 {
444                         msg = argv(k);
445                         msg_pos = screen_pos + k * 1.25 * size * eY;
446                         if (align > 0)
447                                 msg_pos.x -= stringwidth(msg, true, size * '1 1 0') * min(1, align);
448
449                         drawcolorcodedstring_builtin(msg_pos, msg, size * '1 1 0', alpha_, DRAWFLAG_NORMAL);
450                 }
451         }
452         ATTRIB(DebugText3d, draw2d, void(DebugText3d), DebugText3d_draw2d);
453 ENDCLASS(DebugText3d)
454
455 NET_HANDLE(debug_text_3d, bool is_new) {
456         vector pos = ReadVector();
457         string msg = ReadString();
458         float align = ReadFloat();
459         float duration = ReadFloat();
460         vector vel = ReadVector();
461         make_impure(NEW(DebugText3d, pos, msg, align, 1 / duration, vel));
462         return true;
463 }
464
465 #endif // CSQC
466
467 #ifdef SVQC
468
469 // can't use autocvars because they give unused warning unless the macros are expanded
470 #define debug_text_3d(...) EVAL(OVERLOAD(debug_text_3d, __VA_ARGS__))
471 #define debug_text_3d_2(pos, msg) debug_text_3d_3(pos, msg, cvar("debug_text_3d_default_align"))
472 #define debug_text_3d_3(pos, msg, align) debug_text_3d_4(pos, msg, align, cvar("debug_text_3d_default_duration"))
473 #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")))
474 #define debug_text_3d_5(pos, msg, align, dur, vel) debug_text_3d_fn(pos, msg, align, dur, vel)
475
476 ERASEABLE
477 void debug_text_3d_fn(vector pos, string msg, float align, float duration, vector vel) {
478         WriteHeader(MSG_BROADCAST, debug_text_3d);
479         WriteVector(MSG_BROADCAST, pos);
480         WriteString(MSG_BROADCAST, msg);
481         WriteFloat(MSG_BROADCAST, align);
482         WriteFloat(MSG_BROADCAST, duration);
483         WriteVector(MSG_BROADCAST, vel);
484 }
485
486 #endif // SVQC