Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
debug.qh
Go to the documentation of this file.
1#pragma once
2
3#ifdef CSQC
4#include <client/draw.qh>
5#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;
23REGISTER_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
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) * 0.5;
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
73bool autocvar_debugdraw;
74#endif // GAMEQC
75
76#ifdef CSQC
77 string autocvar_debugdraw_filter, autocvar_debugdraw_filterout;
78 .int debugdraw_last;
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 {
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:
191 {
192 LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " debugdraw_sv");
193 return;
194 }
195 }
196 }
197#endif // SVQC
198#endif // ENABLE_DEBUGDRAW
199
200
201GENERIC_COMMAND(bufstr_get, "Examine a string buffer object", false)
202{
203 switch (request)
204 {
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:
215 {
216 LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " bufstr_get <bufhandle> <string_index>");
217 return;
218 }
219 }
220}
221
222
223GENERIC_COMMAND(version, "Print the current version", false)
224{
225 switch (request)
226 {
228 {
229 LOG_INFO(PROGNAME, " version: ", WATERMARK);
230 return;
231 }
232 default:
234 {
235 LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " version");
236 return;
237 }
238 }
239}
240
241
242#ifdef CSQC
243void(float bufhandle, string pattern, string antipattern) buf_cvarlist = #517;
244#endif
245
246GENERIC_COMMAND(cvar_localchanges, "Print locally changed cvars", false)
247{
248 switch (request)
249 {
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:
270 {
271 LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " cvar_localchanges");
272 return;
273 }
274 }
275}
276
277
278#if ENABLE_DEBUGTRACE
279REGISTER_STAT(TRACE_ENT, INT)
280
281#ifdef SVQC
282bool autocvar_debugtrace;
283
284REGISTER_MUTATOR(trace, autocvar_debugtrace);
285
286.bool debug_trace_button;
287.int solid_prev;
288MUTATOR_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
319entity TRACE_ENT;
320void 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
329STATIC_INIT(TRACE_ENT)
330{
331 entity e = TRACE_ENT = new_pure(TRACE_ENT);
332 e.draw2d = Trace_draw2d;
334}
335#endif // CSQC
336
337#endif
338
339
340GENERIC_COMMAND(findent, "Search through entities for matching classname", false)
341{
342 switch (request)
343 {
345 {
346 int entcnt = 0;
348 {
349 LOG_HELPF("%i (%s)", it, it.classname);
350 ++entcnt;
351 });
352 if(entcnt)
353 LOG_HELPF("Found %d entities", entcnt);
354 return;
355 }
356
357 default:
358 LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
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
369GENERIC_COMMAND(findat, "Search through entities for matching origin", false)
370{
371 switch (request)
372 {
374 {
375 vector org = stov(argv(1));
376 float dist = stof(argv(2));
377 int entcnt = 0;
379 {
380 if (dist > 0)
381 {
382 if (!vdist(it.origin - org, <, dist))
383 continue;
384 }
385 else if (it.origin != org)
386 continue;
387 LOG_HELPF("%i (%s)", it, it.classname);
388 ++entcnt;
389 });
390 if(entcnt)
391 LOG_HELPF("Found %d entities", entcnt);
392 return;
393 }
394
395 default:
396 LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
398 {
399 LOG_HELP("Usage:^3 " GetProgramCommandPrefix() " findat <position> [<dist>]");
400 LOG_HELP(" Where <position> is a vector \"x y z\"");
401 return;
402 }
403 }
404}
405
406
407// debug_test() allows drawing text from server on the client anywhere in world coordinates.
408
409#ifdef GAMEQC
411#endif
412
413#ifdef CSQC
414
416 // reusing existing fields
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
423
424 CONSTRUCTOR(DebugText3d, vector pos, string msg, float align, float fade_rate_, vector vel) {
426 this.origin = pos;
427 this.message = strzone(msg);
428 SetResource(this, RES_HEALTH, align);
429 this.hit_time = time;
430 this.fade_rate = fade_rate_;
431 this.velocity = vel;
432 IL_PUSH(g_drawables_2d, this);
433 }
434
438
440 float since_created = time - this.hit_time;
441 float alpha_ = 1 - since_created * this.fade_rate;
442
443 if (alpha_ < 0) {
444 delete(this);
445 return;
446 }
447
448 int size = 11;
449 vector screen_pos = project_3d_to_2d(this.origin) + since_created * this.velocity;
450 if (screen_pos.z < 0) return; // behind camera
451
452 screen_pos.z = 0;
453 float align = GetResource(this, RES_HEALTH);
454 string msg;
455 vector msg_pos;
456
457 int n = tokenizebyseparator(this.message, "\n");
458 for(int k = 0; k < n; ++k)
459 {
460 msg = argv(k);
461 msg_pos = screen_pos + k * 1.25 * size * eY;
462 if (align > 0)
463 msg_pos.x -= stringwidth(msg, true, size * '1 1 0') * min(1, align);
464
465 drawcolorcodedstring_builtin(msg_pos, msg, size * '1 1 0', alpha_, DRAWFLAG_NORMAL);
466 }
467 }
470
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));
478 return true;
479}
480
481#endif // CSQC
482
483#ifdef SVQC
484
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)
491
493void debug_text_3d_fn(vector pos, string msg, float align, float duration, vector vel) {
495 WriteVector(MSG_BROADCAST, pos);
497 WriteFloat(MSG_BROADCAST, align);
498 WriteFloat(MSG_BROADCAST, duration);
499 WriteVector(MSG_BROADCAST, vel);
500}
501
502#endif // SVQC
#define REGISTER_MUTATOR(...)
Definition base.qh:295
#define MUTATOR_HOOKFUNCTION(...)
Definition base.qh:335
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
#define ReadString
void SetResource(entity e, Resource res_type, float amount)
Sets the current amount of resource the given entity will have.
float GetResource(entity e, Resource res_type)
Returns the current amount of resource the given entity has.
Header file that describes the resource system.
ATTRIB(DebugText3d, origin, vector)
DebugText3d(vector pos, string msg, float align, float fade_rate_, vector vel)
Definition debug.qh:424
void DebugText3d_draw2d(DebugText3d this)
Definition debug.qh:439
#define drawstring(position, text, scale, rgb, alpha, flag)
Definition draw.qh:27
string message
Definition powerups.qc:19
int sv_entnum
Definition main.qh:186
IntrusiveList g_drawables_2d
Definition main.qh:92
const int CMD_REQUEST_COMMAND
Definition command.qh:3
const int CMD_REQUEST_USAGE
Definition command.qh:4
#define GENERIC_COMMAND(id, description, menubased)
Definition reg.qh:12
bool debug
Definition debug.qh:21
bool debug_send(entity this, entity to, int sf)
Definition debug.qh:42
ERASEABLE void debug_text_3d_fn(vector pos, string msg, float align, float duration, vector vel)
Definition debug.qh:493
#define debug_text_3d(...)
Definition debug.qh:486
#define PHYS_INPUT_BUTTON_HOOK(s)
Definition player.qh:155
string classname
const float DRAWFLAG_NORMAL
entity trace_ent
const float MOVE_NORMAL
vector velocity
const float SOLID_BBOX
float time
vector size
vector absmax
vector origin
vector absmin
#define stringwidth
entity tag_entity
#define MAKE_VECTORS(angles, forward, right, up)
Same as the makevectors builtin but uses the provided locals instead of the v_* globals.
#define tokenizebyseparator
#define buf_create
#define GetProgramCommandPrefix()
Definition generic.qh:25
ERASEABLE entity IL_PUSH(IntrusiveList this, entity it)
Push to tail.
#define FOREACH_ENTITY_ORDERED(cond, body)
Definition iter.qh:138
#define FOREACH_ENTITY_CLASS_ORDERED(class, cond, body)
Definition iter.qh:190
#define FOREACH_ENTITY_RADIUS(org, dist, cond, body)
Definition iter.qh:160
#define FOREACH_ENTITY(cond, body)
Definition iter.qh:137
#define ERASEABLE
Definition _all.inc:37
noref float vid_conheight
Definition draw.qh:9
#define ReadFloat()
Definition net.qh:366
#define NET_HANDLE(id, param)
Definition net.qh:15
#define ReadVector()
Definition net.qh:367
#define Net_Accept(classname)
Definition net.qh:201
#define WriteHeader(to, id)
Definition net.qh:221
int ReadByte()
#define REGISTER_NET_TEMP(id)
Definition net.qh:28
#define STAT(...)
Definition stats.qh:82
#define REGISTER_STAT(...)
Definition stats.qh:77
#define LOG_HELP(...)
Definition log.qh:85
#define LOG_INFO(...)
Definition log.qh:65
#define LOG_HELPF(...)
Definition log.qh:86
#define LOG_INFOF(...)
Definition log.qh:66
float MSG_ONE
Definition menudefs.qc:56
float stof(string val,...)
void WriteString(string data, float dest, float desto)
vector stov(string s)
const string cvar_string(string name)
void WriteShort(float data, float dest, float desto)
float min(float f,...)
void WriteByte(float data, float dest, float desto)
const string cvar_defstring(string name)
string strzone(string s)
float MSG_BROADCAST
Definition menudefs.qc:55
string argv(float n)
entity findfloat(entity start,.float field, float match)
#define etof(e)
Definition misc.qh:25
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))
#define NEW(cname,...)
Definition oo.qh:117
#define is_pure(e)
Definition oo.qh:16
#define make_impure(e)
Definition oo.qh:22
#define make_pure(e)
direct use is
Definition oo.qh:13
#define CLASS(...)
Definition oo.qh:145
#define ENDCLASS(cname)
Definition oo.qh:281
#define DESTRUCTOR(cname)
Definition oo.qh:220
#define new_pure(class)
purely logical entities (not linked to the area grid)
Definition oo.qh:67
#define ATTRIB(...)
Definition oo.qh:148
#define CONSTRUCTOR(cname,...)
Definition oo.qh:213
string sourceLoc
Location entity was spawned from in source.
Definition oo.qh:27
#define CONSTRUCT(cname,...)
Definition oo.qh:123
#define NULL
Definition post.qh:14
#define PROGNAME
Definition progname.qh:4
entity msg_entity
Definition progsdefs.qc:63
#define stuffcmd(cl,...)
Definition progsdefs.qh:23
fade_rate
Definition projectile.qh:14
float health
Legacy fields for the resources. To be removed.
Definition resources.qh:9
vector
Definition self.qh:92
vector org
Definition self.qh:92
void
Definition self.qh:72
#define COMMON_COMMAND(id, description)
Definition common.qh:18
#define STATIC_INIT(func)
during worldspawn
Definition static.qh:32
#define strfree(this)
Definition string.qh:59
if(frag_attacker.flagcarried)
Definition sv_ctf.qc:2325
#define FOREACH_CLIENT(cond, body)
Definition utils.qh:50
const vector eY
Definition vector.qh:45
#define vdist(v, cmp, f)
Vector distance comparison, avoids sqrt()
Definition vector.qh:8
vector project_3d_to_2d(vector vec)
Definition view.qc:373
int max_shot_distance
Definition weapon.qh:203