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