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).
17 REGISTER_NET_TEMP(net_debug)
21 NET_HANDLE(net_debug, bool isNew)
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());
36 bool debug_send(entity this, entity to, int sf)
38 int channel = MSG_ONE;
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;
47 o += this.tag_entity.origin;
48 WriteVector(channel, o);
49 WriteString(channel, this.classname);
50 WriteString(channel, this.sourceLoc);
62 * 3: on (.entnum != 0)
63 * 4: on (.origin == '0 0 0')
64 * 5: on (.debug != 0), server only
67 bool autocvar_debugdraw;
71 string autocvar_debugdraw_filter, autocvar_debugdraw_filterout;
73 vector project_3d_to_2d(vector vec);
76 if (!autocvar_debugdraw) return;
77 static int debugdraw_frame;
80 FOREACH_ENTITY(true, {
81 if (it.debugdraw_last == debugdraw_frame) continue;
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)
90 if (!it.entnum) continue;
92 if (autocvar_debugdraw == 4)
94 if (it.origin) continue;
96 if (autocvar_debugdraw == 5)
98 if (!it.debug) continue;
100 else if (autocvar_debugdraw > 5)
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;
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;
140 else if (is_pure(it))
142 if (autocvar_debugdraw < 2) continue;
145 vector o = it.origin;
147 o += it.tag_entity.origin;
148 vector pos = project_3d_to_2d(o);
149 if (pos.z < 0) continue;
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);
164 COMMON_COMMAND(debugdraw_sv, "Dump all server entities")
168 case CMD_REQUEST_COMMAND:
170 if (!autocvar_debugdraw) return;
173 for (entity e = NULL; (e = findfloat(e, debug, 0)) && rem > 0; )
175 if (autocvar_debugdraw < 2 && is_pure(e)) continue;
176 debug_send(e, caller, 0);
180 LOG_INFOF("%d server entities sent", n - rem);
185 case CMD_REQUEST_USAGE:
187 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " debugdraw_sv");
196 GENERIC_COMMAND(bufstr_get, "Examine a string buffer object")
200 case CMD_REQUEST_COMMAND:
202 int bufhandle = stof(argv(1));
203 int string_index = stof(argv(2));
204 string s = bufstr_get(bufhandle, string_index);
210 case CMD_REQUEST_USAGE:
212 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " bufstr_get bufhandle string_index");
219 GENERIC_COMMAND(version, "Print the current version")
223 case CMD_REQUEST_COMMAND:
229 case CMD_REQUEST_USAGE:
231 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " version");
239 void(float bufhandle, string pattern, string antipattern) buf_cvarlist = #517;
241 GENERIC_COMMAND(cvar_localchanges, "Print locally changed cvars")
245 case CMD_REQUEST_COMMAND:
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);
257 s = strcat(s, k, " \"", v, "\" // \"", d, "\"\n");
264 case CMD_REQUEST_USAGE:
266 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " cvar_localchanges");
273 #if ENABLE_DEBUGTRACE
274 REGISTER_STAT(TRACE_ENT, int)
276 bool autocvar_debugtrace;
278 REGISTER_MUTATOR(trace, autocvar_debugtrace);
280 .bool debug_trace_button;
282 MUTATOR_HOOKFUNCTION(trace, SV_StartFrame)
284 FOREACH_CLIENT(true, {
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;
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;
302 entity e = trace_ent;
304 STAT(TRACE_ENT, it) = i;
306 setorigin(e, e.origin + '0 0 100');
307 stuffcmd(it, sprintf("prvm_edict server %d\n", i));
313 void Trace_draw2d(entity this)
315 int e = STAT(TRACE_ENT);
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);
322 STATIC_INIT(TRACE_ENT)
324 entity e = TRACE_ENT = new_pure(TRACE_ENT);
325 e.draw2d = Trace_draw2d;
326 IL_PUSH(g_drawables_2d, e);
332 GENERIC_COMMAND(find, "Search through entities for matching classname")
336 case CMD_REQUEST_COMMAND:
339 FOREACH_ENTITY_CLASS_ORDERED(argv(1), true,
341 LOG_INFOF("%i (%s)", it, it.classname);
345 LOG_INFOF("Found %d entities", entcnt);
351 LOG_INFO("Incorrect parameters for ^2find^7");
353 case CMD_REQUEST_USAGE:
355 LOG_INFO("Usage:^3 " GetProgramCommandPrefix() " find classname");
356 LOG_INFO(" Where 'classname' is the classname to search for.");
363 GENERIC_COMMAND(findat, "Search through entities for matching origin")
367 case CMD_REQUEST_COMMAND:
369 vector match = stov(argv(1));
370 FOREACH_ENTITY_ORDERED(it.origin == match, LOG_INFOF("%i (%s)", it, it.classname));
375 LOG_INFO("Incorrect parameters for ^2findat^7");
376 case CMD_REQUEST_USAGE:
378 LOG_INFO("Usage:^3 " GetProgramCommandPrefix() " findat \"0 0 0\"");
385 // debug_test() allows drawing text from server on the client anywhere in world coordinates.
388 REGISTER_NET_TEMP(debug_text_3d);
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);
402 CONSTRUCTOR(DebugText3d, vector pos, string msg, float align, float fade_rate_, vector vel) {
403 CONSTRUCT(DebugText3d);
405 this.message = strzone(msg);
407 this.hit_time = time;
408 this.fade_rate = fade_rate_;
410 IL_PUSH(g_drawables_2d, this);
413 DESTRUCTOR(DebugText3d) {
414 strfree(this.message);
417 void DebugText3d_draw2d(DebugText3d this) {
418 float since_created = time - this.hit_time;
419 float alpha_ = 1 - since_created * this.fade_rate;
427 vector screen_pos = project_3d_to_2d(this.origin) + since_created * this.velocity;
428 float align = this.health;
430 screen_pos.x -= stringwidth(this.message, true, size * '1 1 0') * min(1, align);
431 if (screen_pos.z < 0) return; // behind camera
434 vector rgb = '1 1 0';
435 drawcolorcodedstring2_builtin(screen_pos, this.message, size * '1 1 0', rgb, alpha_, DRAWFLAG_NORMAL);
437 ATTRIB(DebugText3d, draw2d, void(DebugText3d), DebugText3d_draw2d);
438 ENDCLASS(DebugText3d)
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));
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)
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);