]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/debug.qh
Merge branch 'TimePath/intrusive' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / debug.qh
1 #pragma once
2
3 #ifdef CSQC
4 .entity tag_entity;
5 #endif
6
7 #ifndef MENUQC
8 .bool debug;
9 .int sv_entnum;
10 REGISTER_NET_TEMP(net_debug)
11 #endif
12
13 #ifdef CSQC
14         NET_HANDLE(net_debug, bool isNew)
15         {
16                 Net_Accept(net_debug);
17                 this.sv_entnum = ReadShort();
18                 if (ReadByte()) make_pure(this);
19                 this.origin_x = ReadCoord();
20                 this.origin_y = ReadCoord();
21                 this.origin_z = ReadCoord();
22                 setorigin(this, this.origin);
23                 this.debug = true;  // identify server entities by this
24                 this.classname = strzone(ReadString());
25                 this.sourceLoc = strzone(ReadString());
26                 return true;
27         }
28 #endif
29
30 #ifdef SVQC
31         bool debug_send(entity this, entity to, int sf)
32         {
33                 int channel = MSG_ONE;
34                 msg_entity = to;
35                 WriteHeader(channel, net_debug);
36                 WriteShort(channel, etof(this));
37                 WriteByte(channel, is_pure(this));
38                 vector o = this.origin;
39                 if (o == '0 0 0') // brushes
40                         o = (this.absmin + this.absmax) / 2;
41                 if (this.tag_entity)
42                         o += this.tag_entity.origin;
43                 WriteCoord(channel, o.x); WriteCoord(channel, o.y); WriteCoord(channel, o.z);
44                 WriteString(channel, this.classname);
45                 WriteString(channel, this.sourceLoc);
46                 return true;
47         }
48 #endif
49
50 #ifndef MENUQC
51 /**
52  * 0: off
53  * 1: on
54  * 2: on (pure)
55  * 3: on (.entnum != 0)
56  * 4: on (.origin == '0 0 0')
57  * 5: on (.debug != 0), server only
58  * 6: on (.solid != 0)
59  */
60 bool autocvar_debugdraw;
61 #endif
62
63 #ifdef CSQC
64         string autocvar_debugdraw_filter, autocvar_debugdraw_filterout;
65         .int debugdraw_last;
66         vector project_3d_to_2d(vector vec);
67         void Debug_Draw()
68         {
69                 if (!autocvar_debugdraw) return;
70                 static int debugdraw_frame;
71                 ++debugdraw_frame;
72                 const int sz = 8;
73                 FOREACH_ENTITY(true, {
74                         if (it.debugdraw_last == debugdraw_frame) continue;
75                         int ofs = 0;
76                         FOREACH_ENTITY_RADIUS(it.origin, 100, it.debugdraw_last != debugdraw_frame, {
77                                 it.debugdraw_last = debugdraw_frame;
78                                 vector rgb = (it.debug) ? '0 0 1' : '1 0 0';
79                                 if (autocvar_debugdraw_filterout != "" && strhasword(autocvar_debugdraw_filterout, it.classname)) continue;
80                                 if (autocvar_debugdraw_filter != "" && !strhasword(autocvar_debugdraw_filter, it.classname)) continue;
81                                 if (autocvar_debugdraw == 3)
82                                 {
83                                         if (!it.entnum) continue;
84                                 }
85                                 if (autocvar_debugdraw == 4)
86                                 {
87                                         if (it.origin) continue;
88                                 }
89                                 if (autocvar_debugdraw == 5)
90                                 {
91                                         if (!it.debug) continue;
92                                 }
93                                 else if (autocvar_debugdraw > 5)
94                                 {
95                                         bool flag = true;
96                                         do {
97 //                                              if (it.modelindex) break;
98 //                                              if (it.absmin) break;
99 //                                              if (it.absmax) break;
100 //                                              if (it.entnum) break;
101 //                                              if (it.drawmask) break;
102 //                                              if (it.predraw) break;
103 //                                              if (it.move_movetype) break;
104                                                 if (it.solid) break;
105 //                                              if (it.origin) break;
106 //                                              if (it.oldorigin) break;
107 //                                              if (it.velocity) break;
108 //                                              if (it.angles) break;
109 //                                              if (it.avelocity) break;
110 //                                              if (it.classname) break;
111 //                                              if (it.model) break;
112 //                                              if (it.frame) break;
113 //                                              if (it.skin) break;
114 //                                              if (it.effects) break;
115 //                                              if (it.mins) break;
116 //                                              if (it.maxs) break;
117 //                                              if (it.size) break;
118 //                                              if (it.touch) break;
119 //                                              if (it.use) break;
120 //                                              if (it.think) break;
121 //                                              if (it.blocked) break;
122 //                                              if (it.nextthink) break;
123 //                                              if (it.chain) break;
124 //                                              if (it.netname) break;
125 //                                              if (it.enemy) break;
126 //                                              if (it.flags) break;
127 //                                              if (it.colormap) break;
128 //                                              if (it.owner) break;
129                                                 flag = false;
130                                         } while (0);
131                                         if (!flag) continue;
132                                 }
133                                 else if (is_pure(it))
134                                 {
135                                         if (autocvar_debugdraw < 2) continue;
136                                         rgb.y = 1;
137                                 }
138                                 vector o = it.origin;
139                                 if (it.tag_entity)
140                                         o += it.tag_entity.origin;
141                                 vector pos = project_3d_to_2d(o);
142                                 if (pos.z < 0) continue;
143                                 pos.z = 0;
144                                 pos.y += ofs * sz;
145                                 drawcolorcodedstring2_builtin(pos,
146                                         sprintf("%d: '%s'@%s", (it.debug ? it.sv_entnum : etof(it)),
147                                         it.classname, it.sourceLoc),
148                                         sz * '1 1 0', rgb, 0.5, DRAWFLAG_NORMAL);
149                                 ++ofs;
150             });
151                 });
152         }
153 #endif
154
155 #ifdef SVQC
156         COMMON_COMMAND(debugdraw_sv, "Dump all server entities")
157         {
158                 switch (request)
159                 {
160                         case CMD_REQUEST_COMMAND:
161                         {
162                                 if (!autocvar_debugdraw) return;
163                                 int n = 1000;
164                                 int rem = n;
165                                 for (entity e = NULL; (e = findfloat(e, debug, 0)) && rem > 0; )
166                                 {
167                                         if (autocvar_debugdraw < 2 && is_pure(e)) continue;
168                                         debug_send(e, caller, 0);
169                                         e.debug = true;
170                                         --rem;
171                                 }
172                                 LOG_INFOF("%d server entities sent\n", n - rem);
173                                 return;
174                         }
175
176                         default:
177                         case CMD_REQUEST_USAGE:
178                         {
179                                 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " debugdraw_sv"));
180                                 return;
181                         }
182                 }
183         }
184 #endif
185
186 GENERIC_COMMAND(bufstr_get, "Examine a string buffer object")
187 {
188         switch (request)
189         {
190                 case CMD_REQUEST_COMMAND:
191                 {
192                         int bufhandle = stof(argv(1));
193                         int string_index = stof(argv(2));
194                         string s = bufstr_get(bufhandle, string_index);
195                         LOG_INFOF("%s\n", s);
196                         return;
197                 }
198
199                 default:
200                 case CMD_REQUEST_USAGE:
201                 {
202                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " bufstr_get bufhandle string_index"));
203                         return;
204                 }
205         }
206 }
207
208 GENERIC_COMMAND(version, "Print the current version")
209 {
210         switch (request)
211         {
212                 case CMD_REQUEST_COMMAND:
213                 {
214                         LOG_INFO(WATERMARK "\n");
215                         return;
216                 }
217                 default:
218                 case CMD_REQUEST_USAGE:
219                 {
220                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " version"));
221                         return;
222                 }
223         }
224 }
225
226 #ifdef CSQC
227 void(float bufhandle, string pattern, string antipattern) buf_cvarlist = #517;
228 #endif
229 GENERIC_COMMAND(cvar_localchanges, "Print locally changed cvars")
230 {
231         switch (request)
232         {
233                 case CMD_REQUEST_COMMAND:
234                 {
235                         string s = "";
236                         int h = buf_create();
237                         buf_cvarlist(h, "", "_"); // exclude all _ cvars as they are temporary
238                         int n = buf_getsize(h);
239                         for (int i = 0; i < n; ++i) {
240                                 string k = bufstr_get(h, i);
241                                 string v = cvar_string(k);
242                                 string d = cvar_defstring(k);
243                                 if (v == d)
244                                         continue;
245                                 s = strcat(s, k, " \"", v, "\" // \"", d, "\"\n");
246                         }
247                         buf_del(h);
248                         LOG_INFO(s);
249                         return;
250                 }
251                 default:
252                 case CMD_REQUEST_USAGE:
253                 {
254                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " cvar_localchanges"));
255                         return;
256                 }
257         }
258 }
259
260 REGISTER_STAT(TRACE_ENT, int)
261 #ifdef SVQC
262 bool autocvar_debugtrace;
263
264 REGISTER_MUTATOR(trace, autocvar_debugtrace);
265
266 .bool debug_trace_button;
267 .int solid_prev;
268 MUTATOR_HOOKFUNCTION(trace, SV_StartFrame)
269 {
270         FOREACH_CLIENT(true, {
271                 bool skip = false;
272                 bool btn = PHYS_INPUT_BUTTON_HOOK(it);
273                 if (btn == it.debug_trace_button) skip = true;
274                 it.debug_trace_button = btn;
275                 if (!btn || skip) continue;
276                 FOREACH_ENTITY(true, {
277                     it.solid_prev = it.solid;
278                         it.solid = SOLID_BBOX;
279                 });
280                 vector forward = '0 0 0'; vector right = '0 0 0'; vector up = '0 0 0';
281                 MAKEVECTORS(makevectors, it.v_angle, forward, right, up);
282                 vector pos = it.origin + it.view_ofs;
283                 traceline(pos, pos + forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, it);
284                 FOREACH_ENTITY(true, {
285                     it.solid = it.solid_prev;
286             it.solid_prev = 0;
287                 });
288                 entity e = trace_ent;
289                 int i = etof(e);
290                 STAT(TRACE_ENT, it) = i;
291                 if (!e) continue;
292                 setorigin(e, e.origin + '0 0 100');
293                 stuffcmd(it, sprintf("prvm_edict server %d\n", i));
294         });
295 }
296 #endif
297 #ifdef CSQC
298 entity TRACE_ENT;
299 void Trace_draw2d(entity this)
300 {
301         int e = STAT(TRACE_ENT);
302         if (!e) return;
303         vector pos = '0 0 0';
304         pos.y += vid_conheight / 2;
305         drawstring(pos, sprintf("prvm_edict server %d", e), '10 10 0', '1 1 1', 1, DRAWFLAG_NORMAL);
306 }
307
308 STATIC_INIT(TRACE_ENT)
309 {
310         entity e = TRACE_ENT = new_pure(TRACE_ENT);
311         e.draw2d = Trace_draw2d;
312         IL_PUSH(g_drawables_2d, e);
313 }
314 #endif
315
316 GENERIC_COMMAND(find, "Search through entities for matching classname")
317 {
318         switch (request)
319         {
320                 case CMD_REQUEST_COMMAND:
321                 {
322                         FOREACH_ENTITY_CLASS_ORDERED(argv(1), true, LOG_INFOF("%i (%s)\n", it, it.classname));
323                         return;
324                 }
325
326                 default:
327                 {
328                         LOG_INFO("Incorrect parameters for ^2find^7\n");
329         }
330                 case CMD_REQUEST_USAGE:
331                 {
332                         LOG_INFO("\nUsage:^3 " GetProgramCommandPrefix() " find classname\n");
333                         LOG_INFO("  Where 'classname' is the classname to search for.\n");
334                         return;
335                 }
336         }
337 }
338
339 GENERIC_COMMAND(findat, "Search through entities for matching origin")
340 {
341         switch (request)
342         {
343                 case CMD_REQUEST_COMMAND:
344                 {
345                     vector match = stov(argv(1));
346                     FOREACH_ENTITY_ORDERED(it.origin == match, LOG_INFOF("%i (%s)\n", it, it.classname));
347                         return;
348                 }
349
350                 default:
351                         LOG_INFO("Incorrect parameters for ^2findat^7\n");
352                 case CMD_REQUEST_USAGE:
353                 {
354                         LOG_INFO("\nUsage:^3 " GetProgramCommandPrefix() " findat \"0 0 0\"\n");
355                         return;
356                 }
357         }
358 }