4 #include <client/draw.qh>
5 #include <client/resources.qh>
6 #include <client/view.qh>
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).
23 REGISTER_NET_TEMP(net_debug)
27 NET_HANDLE(net_debug, bool isNew)
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());
42 bool debug_send(entity this, entity to, int sf)
44 int channel = MSG_ONE;
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;
53 o += this.tag_entity.origin;
54 WriteVector(channel, o);
55 WriteString(channel, this.classname);
56 WriteString(channel, this.sourceLoc);
68 * 3: on (.entnum != 0)
69 * 4: on (.origin == '0 0 0')
70 * 5: on (.debug != 0), server only
73 bool autocvar_debugdraw;
77 string autocvar_debugdraw_filter, autocvar_debugdraw_filterout;
79 vector project_3d_to_2d(vector vec);
82 if (!autocvar_debugdraw) return;
83 static int debugdraw_frame;
86 FOREACH_ENTITY(true, {
87 if (it.debugdraw_last == debugdraw_frame) continue;
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)
96 if (!it.entnum) continue;
98 if (autocvar_debugdraw == 4)
100 if (it.origin) continue;
102 if (autocvar_debugdraw == 5)
104 if (!it.debug) continue;
106 else if (autocvar_debugdraw > 5)
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;
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;
146 else if (is_pure(it))
148 if (autocvar_debugdraw < 2) continue;
151 vector o = it.origin;
153 o += it.tag_entity.origin;
154 vector pos = project_3d_to_2d(o);
155 if (pos.z < 0) continue;
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);
169 COMMON_COMMAND(debugdraw_sv, "Dump all server entities")
173 case CMD_REQUEST_COMMAND:
175 if (!autocvar_debugdraw) return;
178 for (entity e = NULL; (e = findfloat(e, debug, 0)) && rem > 0; )
180 if (autocvar_debugdraw < 2 && is_pure(e)) continue;
181 debug_send(e, caller, 0);
185 LOG_INFOF("%d server entities sent", n - rem);
190 case CMD_REQUEST_USAGE:
192 LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " debugdraw_sv");
198 #endif // ENABLE_DEBUGDRAW
201 GENERIC_COMMAND(bufstr_get, "Examine a string buffer object", false)
205 case CMD_REQUEST_COMMAND:
207 int bufhandle = stof(argv(1));
208 int string_index = stof(argv(2));
209 LOG_INFO(bufstr_get(bufhandle, string_index));
214 case CMD_REQUEST_USAGE:
216 LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " bufstr_get <bufhandle> <string_index>");
223 GENERIC_COMMAND(version, "Print the current version", false)
227 case CMD_REQUEST_COMMAND:
229 LOG_INFO(PROGNAME, " version: ", WATERMARK);
233 case CMD_REQUEST_USAGE:
235 LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " version");
243 void(float bufhandle, string pattern, string antipattern) buf_cvarlist = #517;
246 GENERIC_COMMAND(cvar_localchanges, "Print locally changed cvars", false)
250 case CMD_REQUEST_COMMAND:
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);
262 s = strcat(s, k, " \"", v, "\" // \"", d, "\"\n");
269 case CMD_REQUEST_USAGE:
271 LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " cvar_localchanges");
278 #if ENABLE_DEBUGTRACE
279 REGISTER_STAT(TRACE_ENT, int)
282 bool autocvar_debugtrace;
284 REGISTER_MUTATOR(trace, autocvar_debugtrace);
286 .bool debug_trace_button;
288 MUTATOR_HOOKFUNCTION(trace, SV_StartFrame)
290 FOREACH_CLIENT(true, {
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;
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;
308 entity e = trace_ent;
310 STAT(TRACE_ENT, it) = i;
312 setorigin(e, e.origin + '0 0 100');
313 stuffcmd(it, sprintf("prvm_edict server %d\n", i));
320 void Trace_draw2d(entity this)
322 int e = STAT(TRACE_ENT);
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);
329 STATIC_INIT(TRACE_ENT)
331 entity e = TRACE_ENT = new_pure(TRACE_ENT);
332 e.draw2d = Trace_draw2d;
333 IL_PUSH(g_drawables_2d, e);
340 GENERIC_COMMAND(findent, "Search through entities for matching classname", false)
344 case CMD_REQUEST_COMMAND:
347 FOREACH_ENTITY_CLASS_ORDERED(argv(1), true,
349 LOG_HELPF("%i (%s)", it, it.classname);
353 LOG_HELPF("Found %d entities", entcnt);
358 LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
359 case CMD_REQUEST_USAGE:
361 LOG_HELP("Usage:^3 " GetProgramCommandPrefix() " find <classname>");
362 LOG_HELP(" Where <classname> is the classname to search for.");
369 GENERIC_COMMAND(findat, "Search through entities for matching origin", false)
373 case CMD_REQUEST_COMMAND:
375 vector org = stov(argv(1));
376 float dist = stof(argv(2));
378 FOREACH_ENTITY_ORDERED(true,
382 if (!vdist(it.origin - org, <, dist))
385 else if (it.origin != org)
387 LOG_HELPF("%i (%s)", it, it.classname);
391 LOG_HELPF("Found %d entities", entcnt);
396 LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
397 case CMD_REQUEST_USAGE:
399 LOG_HELP("Usage:^3 " GetProgramCommandPrefix() " findat <position> [<dist>]");
400 LOG_HELP(" Where <position> is a vector \"x y z\"");
407 // debug_test() allows drawing text from server on the client anywhere in world coordinates.
410 REGISTER_NET_TEMP(debug_text_3d);
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);
424 CONSTRUCTOR(DebugText3d, vector pos, string msg, float align, float fade_rate_, vector vel) {
425 CONSTRUCT(DebugText3d);
427 this.message = strzone(msg);
428 SetResource(this, RES_HEALTH, align);
429 this.hit_time = time;
430 this.fade_rate = fade_rate_;
432 IL_PUSH(g_drawables_2d, this);
435 DESTRUCTOR(DebugText3d) {
436 strfree(this.message);
439 void DebugText3d_draw2d(DebugText3d this) {
440 float since_created = time - this.hit_time;
441 float alpha_ = 1 - since_created * this.fade_rate;
449 vector screen_pos = project_3d_to_2d(this.origin) + since_created * this.velocity;
450 if (screen_pos.z < 0) return; // behind camera
453 float align = GetResource(this, RES_HEALTH);
457 int n = tokenizebyseparator(this.message, "\n");
458 for(int k = 0; k < n; ++k)
461 msg_pos = screen_pos + k * 1.25 * size * eY;
463 msg_pos.x -= stringwidth(msg, true, size * '1 1 0') * min(1, align);
465 drawcolorcodedstring_builtin(msg_pos, msg, size * '1 1 0', alpha_, DRAWFLAG_NORMAL);
468 ATTRIB(DebugText3d, draw2d, void(DebugText3d), DebugText3d_draw2d);
469 ENDCLASS(DebugText3d)
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));
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)
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);