]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/debug.qh
e20139aacc95acf1c3825e339a45302ad19b1c75
[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.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.use1) 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(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 REGISTER_STAT(TRACE_ENT, int)
227 #ifdef SVQC
228 bool autocvar_debugtrace;
229
230 REGISTER_MUTATOR(trace, autocvar_debugtrace);
231
232 .bool debug_trace_button;
233 .int solid_prev;
234 MUTATOR_HOOKFUNCTION(trace, SV_StartFrame)
235 {
236         FOREACH_CLIENT(true, {
237                 bool skip = false;
238                 bool btn = PHYS_INPUT_BUTTON_HOOK(it);
239                 if (btn == it.debug_trace_button) skip = true;
240                 it.debug_trace_button = btn;
241                 if (!btn || skip) continue;
242                 FOREACH_ENTITY(true, {
243                     it.solid_prev = it.solid;
244                         it.solid = SOLID_BBOX;
245                 });
246                 vector forward = '0 0 0'; vector right = '0 0 0'; vector up = '0 0 0';
247                 MAKEVECTORS(makevectors, it.v_angle, forward, right, up);
248                 vector pos = it.origin + it.view_ofs;
249                 traceline(pos, pos + forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, it);
250                 FOREACH_ENTITY(true, {
251                     it.solid = it.solid_prev;
252             it.solid_prev = 0;
253                 });
254                 entity e = trace_ent;
255                 int i = etof(e);
256                 STAT(TRACE_ENT, it) = i;
257                 if (!e) continue;
258                 setorigin(e, e.origin + '0 0 100');
259                 stuffcmd(it, sprintf("prvm_edict server %d\n", i));
260         });
261 }
262 #endif
263 #ifdef CSQC
264 entity TRACE_ENT;
265 void Trace_draw2d(entity this)
266 {
267         int e = STAT(TRACE_ENT);
268         if (!e) return;
269         vector pos = '0 0 0';
270         pos.y += vid_conheight / 2;
271         drawstring(pos, sprintf("prvm_edict server %d", e), '10 10 0', '1 1 1', 1, DRAWFLAG_NORMAL);
272 }
273
274 STATIC_INIT(TRACE_ENT)
275 {
276         entity e = TRACE_ENT = new_pure(TRACE_ENT);
277         e.draw2d = Trace_draw2d;
278 }
279 #endif
280
281 GENERIC_COMMAND(find, "Search through entities for matching classname")
282 {
283         switch (request)
284         {
285                 case CMD_REQUEST_COMMAND:
286                 {
287                         FOREACH_ENTITY_CLASS_ORDERED(argv(1), true, LOG_INFOF("%i (%s)\n", it, it.classname));
288                         return;
289                 }
290
291                 default:
292                 {
293                         LOG_INFO("Incorrect parameters for ^2find^7\n");
294         }
295                 case CMD_REQUEST_USAGE:
296                 {
297                         LOG_INFO("\nUsage:^3 " GetProgramCommandPrefix() " find classname\n");
298                         LOG_INFO("  Where 'classname' is the classname to search for.\n");
299                         return;
300                 }
301         }
302 }
303
304 GENERIC_COMMAND(findat, "Search through entities for matching origin")
305 {
306         switch (request)
307         {
308                 case CMD_REQUEST_COMMAND:
309                 {
310                     vector match = stov(argv(1));
311                     FOREACH_ENTITY_ORDERED(it.origin == match, LOG_INFOF("%i (%s)\n", it, it.classname));
312                         return;
313                 }
314
315                 default:
316                         LOG_INFO("Incorrect parameters for ^2findat^7\n");
317                 case CMD_REQUEST_USAGE:
318                 {
319                         LOG_INFO("\nUsage:^3 " GetProgramCommandPrefix() " findat \"0 0 0\"\n");
320                         return;
321                 }
322         }
323 }