]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/debug.qh
Merge branch 'master' into TimePath/notifications
[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                 WriteCoord(channel, o.x); WriteCoord(channel, o.y); WriteCoord(channel, o.z);
38                 WriteString(channel, this.classname);
39                 WriteString(channel, this.sourceLoc);
40                 return true;
41         }
42 #endif
43
44 #ifndef MENUQC
45 /**
46  * 0: off
47  * 1: on
48  * 2: on (pure)
49  * 3: on (.entnum != 0)
50  * 4: on (.origin == '0 0 0')
51  * 5: on (.debug != 0), server only
52  * 6: on (.solid != 0)
53  */
54 bool autocvar_debugdraw;
55 #endif
56
57 #ifdef CSQC
58         string autocvar_debugdraw_filter, autocvar_debugdraw_filterout;
59         .int debugdraw_last;
60         vector project_3d_to_2d(vector vec);
61         void Debug_Draw()
62         {
63                 if (!autocvar_debugdraw) return;
64                 static int debugdraw_frame;
65                 ++debugdraw_frame;
66                 const int sz = 8;
67                 FOREACH_ENTITY(true, LAMBDA(
68                         if (it.debugdraw_last == debugdraw_frame) continue;
69                         int ofs = 0;
70                         for (entity e = findradius(it.origin, 100); e; e = e.chain)
71                         {
72                                 if (e.debugdraw_last == debugdraw_frame) continue;
73                                 e.debugdraw_last = debugdraw_frame;
74                                 vector rgb = (e.debug) ? '0 0 1' : '1 0 0';
75                                 if (autocvar_debugdraw_filterout != "" && strhasword(autocvar_debugdraw_filterout, e.classname)) continue;
76                                 if (autocvar_debugdraw_filter != "" && !strhasword(autocvar_debugdraw_filter, e.classname)) continue;
77                                 if (autocvar_debugdraw == 3)
78                                 {
79                                         if (!e.entnum) continue;
80                                 }
81                                 if (autocvar_debugdraw == 4)
82                                 {
83                                         if (e.origin) continue;
84                                 }
85                                 if (autocvar_debugdraw == 5)
86                 {
87                     if (!e.debug) continue;
88                 }
89                                 else if (autocvar_debugdraw > 5)
90                                 {
91                                         bool flag = true;
92                                         do {
93 //                                              if (e.modelindex) break;
94 //                                              if (e.absmin) break;
95 //                                              if (e.absmax) break;
96 //                                              if (e.entnum) break;
97 //                                              if (e.drawmask) break;
98 //                                              if (e.predraw) break;
99 //                                              if (e.movetype) break;
100                                                 if (e.solid) break;
101 //                                              if (e.origin) break;
102 //                                              if (e.oldorigin) break;
103 //                                              if (e.velocity) break;
104 //                                              if (e.angles) break;
105 //                                              if (e.avelocity) break;
106 //                                              if (e.classname) break;
107 //                                              if (e.model) break;
108 //                                              if (e.frame) break;
109 //                                              if (e.skin) break;
110 //                                              if (e.effects) break;
111 //                                              if (e.mins) break;
112 //                                              if (e.maxs) break;
113 //                                              if (e.size) break;
114 //                                              if (e.touch) break;
115 //                                              if (e.use) break;
116 //                                              if (e.think) break;
117 //                                              if (e.blocked) break;
118 //                                              if (e.nextthink) break;
119 //                                              if (e.chain) break;
120 //                                              if (e.netname) break;
121 //                                              if (e.enemy) break;
122 //                                              if (e.flags) break;
123 //                                              if (e.colormap) break;
124 //                                              if (e.owner) break;
125                                                 flag = false;
126                                         } while (0);
127                                         if (!flag) continue;
128                                 }
129                                 else if (is_pure(e))
130                                 {
131                                         if (autocvar_debugdraw < 2) continue;
132                                         rgb.y = 1;
133                                 }
134                                 vector pos = project_3d_to_2d(e.origin);
135                                 if (pos.z < 0) continue;
136                                 pos.z = 0;
137                                 pos.y += ofs * sz;
138                                 drawcolorcodedstring2(pos,
139                                         sprintf("%d: '%s'@%s", (e.debug ? e.sv_entnum : etof(e)),
140                                         e.classname, e.sourceLoc),
141                                         sz * '1 1 0', rgb, 0.5, DRAWFLAG_NORMAL);
142                                 ++ofs;
143                         }
144                 ));
145         }
146 #endif
147
148 #ifdef SVQC
149         COMMON_COMMAND(debugdraw_sv, "Dump all server entities")
150         {
151                 switch (request)
152                 {
153                         case CMD_REQUEST_COMMAND:
154                         {
155                                 if (!autocvar_debugdraw) return;
156                                 int n = 1000;
157                                 int rem = n;
158                                 for (entity e = NULL; (e = findfloat(e, debug, 0)) && rem > 0; )
159                                 {
160                                         if (autocvar_debugdraw < 2 && is_pure(e)) continue;
161                                         debug_send(e, nextent(NULL), 0);
162                                         e.debug = true;
163                                         --rem;
164                                 }
165                                 LOG_INFOF("%d server entities sent\n", n - rem);
166                                 return;
167                         }
168
169                         default:
170                         case CMD_REQUEST_USAGE:
171                         {
172                                 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " debugdraw_sv"));
173                                 return;
174                         }
175                 }
176         }
177 #endif
178
179 GENERIC_COMMAND(bufstr_get, "Examine a string buffer object")
180 {
181         switch (request)
182         {
183                 case CMD_REQUEST_COMMAND:
184                 {
185                         int bufhandle = stof(argv(1));
186                         int string_index = stof(argv(2));
187                         string s = bufstr_get(bufhandle, string_index);
188                         LOG_INFOF("%s\n", s);
189                         return;
190                 }
191
192                 default:
193                 case CMD_REQUEST_USAGE:
194                 {
195                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " bufstr_get bufhandle string_index"));
196                         return;
197                 }
198         }
199 }
200
201 GENERIC_COMMAND(version, "Print the current version")
202 {
203         switch (request)
204         {
205                 case CMD_REQUEST_COMMAND:
206                 {
207                         LOG_INFO(WATERMARK "\n");
208                         return;
209                 }
210                 default:
211                 case CMD_REQUEST_USAGE:
212                 {
213                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " version"));
214                         return;
215                 }
216         }
217 }
218
219 REGISTER_STAT(TRACE_ENT, int)
220 #ifdef SVQC
221 bool autocvar_debugtrace;
222
223 REGISTER_MUTATOR(trace, autocvar_debugtrace);
224
225 .bool debug_trace_button;
226 .int solid_prev;
227 MUTATOR_HOOKFUNCTION(trace, SV_StartFrame)
228 {
229         FOREACH_CLIENT(true, {
230                 bool skip = false;
231                 bool btn = PHYS_INPUT_BUTTON_HOOK(it);
232                 if (btn == it.debug_trace_button) skip = true;
233                 it.debug_trace_button = btn;
234                 if (!btn || skip) continue;
235                 FOREACH_ENTITY(true, {
236                     it.solid_prev = it.solid;
237                         it.solid = SOLID_BBOX;
238                 });
239                 vector forward; vector right; vector up;
240                 MAKEVECTORS(makevectors, it.v_angle, forward, right, up);
241                 vector pos = it.origin + it.view_ofs;
242                 traceline(pos, pos + forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, it);
243                 FOREACH_ENTITY(true, {
244                     it.solid = it.solid_prev;
245             it.solid_prev = 0;
246                 });
247                 entity e = trace_ent;
248                 int i = etof(e);
249                 STAT(TRACE_ENT, it) = i;
250                 if (!e) continue;
251                 setorigin(e, e.origin + '0 0 100');
252                 stuffcmd(it, sprintf("prvm_edict server %d\n", i));
253         });
254 }
255 #endif
256 #ifdef CSQC
257 entity TRACE_ENT;
258 void Trace_draw2d(entity this)
259 {
260         int e = STAT(TRACE_ENT);
261         if (!e) return;
262         vector pos = '0 0 0';
263         pos.y += vid_conheight / 2;
264         drawstring(pos, sprintf("prvm_edict server %d", e), '10 10 0', '1 1 1', 1, DRAWFLAG_NORMAL);
265 }
266
267 STATIC_INIT(TRACE_ENT)
268 {
269         entity e = TRACE_ENT = new_pure(TRACE_ENT);
270         e.draw2d = Trace_draw2d;
271 }
272 #endif