4 #include <client/resources.qh>
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).
21 REGISTER_NET_TEMP(net_debug)
25 NET_HANDLE(net_debug, bool isNew)
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());
40 bool debug_send(entity this, entity to, int sf)
42 int channel = MSG_ONE;
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;
51 o += this.tag_entity.origin;
52 WriteVector(channel, o);
53 WriteString(channel, this.classname);
54 WriteString(channel, this.sourceLoc);
66 * 3: on (.entnum != 0)
67 * 4: on (.origin == '0 0 0')
68 * 5: on (.debug != 0), server only
71 bool autocvar_debugdraw;
75 string autocvar_debugdraw_filter, autocvar_debugdraw_filterout;
77 vector project_3d_to_2d(vector vec);
80 if (!autocvar_debugdraw) return;
81 static int debugdraw_frame;
84 FOREACH_ENTITY(true, {
85 if (it.debugdraw_last == debugdraw_frame) continue;
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)
94 if (!it.entnum) continue;
96 if (autocvar_debugdraw == 4)
98 if (it.origin) continue;
100 if (autocvar_debugdraw == 5)
102 if (!it.debug) continue;
104 else if (autocvar_debugdraw > 5)
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;
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;
144 else if (is_pure(it))
146 if (autocvar_debugdraw < 2) continue;
149 vector o = it.origin;
151 o += it.tag_entity.origin;
152 vector pos = project_3d_to_2d(o);
153 if (pos.z < 0) continue;
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);
167 COMMON_COMMAND(debugdraw_sv, "Dump all server entities")
171 case CMD_REQUEST_COMMAND:
173 if (!autocvar_debugdraw) return;
176 for (entity e = NULL; (e = findfloat(e, debug, 0)) && rem > 0; )
178 if (autocvar_debugdraw < 2 && is_pure(e)) continue;
179 debug_send(e, caller, 0);
183 LOG_INFOF("%d server entities sent", n - rem);
188 case CMD_REQUEST_USAGE:
190 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " debugdraw_sv");
196 #endif // ENABLE_DEBUGDRAW
199 GENERIC_COMMAND(bufstr_get, "Examine a string buffer object", false)
203 case CMD_REQUEST_COMMAND:
205 int bufhandle = stof(argv(1));
206 int string_index = stof(argv(2));
207 LOG_INFO(bufstr_get(bufhandle, string_index));
212 case CMD_REQUEST_USAGE:
214 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " bufstr_get bufhandle string_index");
221 GENERIC_COMMAND(version, "Print the current version", false)
225 case CMD_REQUEST_COMMAND:
228 LOG_INFO("CSQC version: ", WATERMARK);
230 LOG_INFO("SVQC version: ", WATERMARK);
231 #elif defined(MENUQC)
232 LOG_INFO("MENUQC version: ", WATERMARK);
237 case CMD_REQUEST_USAGE:
239 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " version");
247 void(float bufhandle, string pattern, string antipattern) buf_cvarlist = #517;
250 GENERIC_COMMAND(cvar_localchanges, "Print locally changed cvars", false)
254 case CMD_REQUEST_COMMAND:
257 int h = buf_create();
258 buf_cvarlist(h, "", "_"); // exclude all _ cvars as they are temporary
259 int n = buf_getsize(h);
260 for (int i = 0; i < n; ++i) {
261 string k = bufstr_get(h, i);
262 string v = cvar_string(k);
263 string d = cvar_defstring(k);
266 s = strcat(s, k, " \"", v, "\" // \"", d, "\"\n");
273 case CMD_REQUEST_USAGE:
275 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " cvar_localchanges");
282 #if ENABLE_DEBUGTRACE
283 REGISTER_STAT(TRACE_ENT, int)
286 bool autocvar_debugtrace;
288 REGISTER_MUTATOR(trace, autocvar_debugtrace);
290 .bool debug_trace_button;
292 MUTATOR_HOOKFUNCTION(trace, SV_StartFrame)
294 FOREACH_CLIENT(true, {
296 bool btn = PHYS_INPUT_BUTTON_HOOK(it);
297 if (btn == it.debug_trace_button) skip = true;
298 it.debug_trace_button = btn;
299 if (!btn || skip) continue;
300 FOREACH_ENTITY(true, {
301 it.solid_prev = it.solid;
302 it.solid = SOLID_BBOX;
304 vector forward = '0 0 0'; vector right = '0 0 0'; vector up = '0 0 0';
305 MAKE_VECTORS(it.v_angle, forward, right, up);
306 vector pos = it.origin + it.view_ofs;
307 traceline(pos, pos + forward * max_shot_distance, MOVE_NORMAL, it);
308 FOREACH_ENTITY(true, {
309 it.solid = it.solid_prev;
312 entity e = trace_ent;
314 STAT(TRACE_ENT, it) = i;
316 setorigin(e, e.origin + '0 0 100');
317 stuffcmd(it, sprintf("prvm_edict server %d\n", i));
324 void Trace_draw2d(entity this)
326 int e = STAT(TRACE_ENT);
328 vector pos = '0 0 0';
329 pos.y += vid_conheight / 2;
330 drawstring(pos, sprintf("prvm_edict server %d", e), '10 10 0', '1 1 1', 1, DRAWFLAG_NORMAL);
333 STATIC_INIT(TRACE_ENT)
335 entity e = TRACE_ENT = new_pure(TRACE_ENT);
336 e.draw2d = Trace_draw2d;
337 IL_PUSH(g_drawables_2d, e);
344 GENERIC_COMMAND(find, "Search through entities for matching classname", false)
348 case CMD_REQUEST_COMMAND:
351 FOREACH_ENTITY_CLASS_ORDERED(argv(1), true,
353 LOG_INFOF("%i (%s)", it, it.classname);
357 LOG_INFOF("Found %d entities", entcnt);
363 LOG_INFO("Incorrect parameters for ^2find^7");
365 case CMD_REQUEST_USAGE:
367 LOG_INFO("Usage:^3 " GetProgramCommandPrefix() " find classname");
368 LOG_INFO(" Where 'classname' is the classname to search for.");
375 GENERIC_COMMAND(findat, "Search through entities for matching origin", false)
379 case CMD_REQUEST_COMMAND:
381 vector match = stov(argv(1));
382 FOREACH_ENTITY_ORDERED(it.origin == match, LOG_INFOF("%i (%s)", it, it.classname));
387 LOG_INFO("Incorrect parameters for ^2findat^7");
388 case CMD_REQUEST_USAGE:
390 LOG_INFO("Usage:^3 " GetProgramCommandPrefix() " findat \"0 0 0\"");
397 // debug_test() allows drawing text from server on the client anywhere in world coordinates.
400 REGISTER_NET_TEMP(debug_text_3d);
405 CLASS(DebugText3d, Object)
406 // reusing existing fields
407 ATTRIB(DebugText3d, origin, vector);
408 ATTRIB(DebugText3d, message, string); // the text (i wanted to use the .text field but then this whole macro-based-inheritance thing shat itself)
409 ATTRIB(DebugText3d, health, float); // text alignment (recycled field)
410 ATTRIB(DebugText3d, hit_time, float); // when it was created
411 ATTRIB(DebugText3d, fade_rate, float); // how fast it should disappear
412 ATTRIB(DebugText3d, velocity, vector);
414 CONSTRUCTOR(DebugText3d, vector pos, string msg, float align, float fade_rate_, vector vel) {
415 CONSTRUCT(DebugText3d);
417 this.message = strzone(msg);
418 SetResource(this, RES_HEALTH, align);
419 this.hit_time = time;
420 this.fade_rate = fade_rate_;
422 IL_PUSH(g_drawables_2d, this);
425 DESTRUCTOR(DebugText3d) {
426 strfree(this.message);
429 void DebugText3d_draw2d(DebugText3d this) {
430 float since_created = time - this.hit_time;
431 float alpha_ = 1 - since_created * this.fade_rate;
439 vector screen_pos = project_3d_to_2d(this.origin) + since_created * this.velocity;
440 if (screen_pos.z < 0) return; // behind camera
443 float align = GetResource(this, RES_HEALTH);
447 int n = tokenizebyseparator(this.message, "\n");
448 for(int k = 0; k < n; ++k)
451 msg_pos = screen_pos + k * 1.25 * size * eY;
453 msg_pos.x -= stringwidth(msg, true, size * '1 1 0') * min(1, align);
455 drawcolorcodedstring_builtin(msg_pos, msg, size * '1 1 0', alpha_, DRAWFLAG_NORMAL);
458 ATTRIB(DebugText3d, draw2d, void(DebugText3d), DebugText3d_draw2d);
459 ENDCLASS(DebugText3d)
461 NET_HANDLE(debug_text_3d, bool is_new) {
462 vector pos = ReadVector();
463 string msg = ReadString();
464 float align = ReadFloat();
465 float duration = ReadFloat();
466 vector vel = ReadVector();
467 make_impure(NEW(DebugText3d, pos, msg, align, 1 / duration, vel));
475 // can't use autocvars because they give unused warning unless the macros are expanded
476 #define debug_text_3d(...) EVAL(OVERLOAD(debug_text_3d, __VA_ARGS__))
477 #define debug_text_3d_2(pos, msg) debug_text_3d_3(pos, msg, cvar("debug_text_3d_default_align"))
478 #define debug_text_3d_3(pos, msg, align) debug_text_3d_4(pos, msg, align, cvar("debug_text_3d_default_duration"))
479 #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")))
480 #define debug_text_3d_5(pos, msg, align, dur, vel) debug_text_3d_fn(pos, msg, align, dur, vel)
483 void debug_text_3d_fn(vector pos, string msg, float align, float duration, vector vel) {
484 WriteHeader(MSG_BROADCAST, debug_text_3d);
485 WriteVector(MSG_BROADCAST, pos);
486 WriteString(MSG_BROADCAST, msg);
487 WriteFloat(MSG_BROADCAST, align);
488 WriteFloat(MSG_BROADCAST, duration);
489 WriteVector(MSG_BROADCAST, vel);