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