]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/debug.qh
Merge branch 'master' into terencehill/menu_hudskin_selector
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / debug.qh
1 #ifndef DEBUG_H
2 #define DEBUG_H
3
4 .bool debug;
5 .int sv_entnum;
6 REGISTER_NET_TEMP(net_debug)
7 #ifdef CSQC
8         NET_HANDLE(net_debug, bool isNew)
9         {
10                 Net_Accept(net_debug);
11                 this.sv_entnum = ReadShort();
12                 if (ReadByte()) make_pure(this);
13                 this.origin_x = ReadCoord();
14                 this.origin_y = ReadCoord();
15                 this.origin_z = ReadCoord();
16                 setorigin(this, this.origin);
17                 this.debug = true;  // identify server entities by this
18                 this.classname = strzone(ReadString());
19                 this.sourceLocFile = strzone(ReadString());
20                 this.sourceLocLine = ReadInt24_t();
21                 return true;
22         }
23 #endif
24
25 #ifdef SVQC
26         bool debug_send(entity this, entity to, int sf)
27         {
28                 int channel = MSG_ONE;
29                 msg_entity = to;
30                 WriteHeader(channel, net_debug);
31                 WriteShort(channel, num_for_edict(this));
32                 WriteByte(channel, is_pure(this));
33                 WriteCoord(channel, this.origin.x);
34                 WriteCoord(channel, this.origin.y);
35                 WriteCoord(channel, this.origin.z);
36                 WriteString(channel, this.classname);
37                 WriteString(channel, this.sourceLocFile);
38                 WriteInt24_t(channel, this.sourceLocLine);
39                 return true;
40         }
41 #endif
42
43 bool autocvar_debugdraw;
44
45 #ifdef CSQC
46         string autocvar_debugdraw_filter;
47         .int debugdraw_last;
48         vector project_3d_to_2d(vector vec);
49         void Debug_Draw()
50         {
51                 if (!autocvar_debugdraw) return;
52                 static int debugdraw_frame;
53                 ++debugdraw_frame;
54                 const int size = 8;
55                 FOREACH_ENTITY(true, LAMBDA(
56                         if (it.debugdraw_last == debugdraw_frame) continue;
57                         int ofs = 0;
58                         for (entity e = findradius(it.origin, 100); e; e = e.chain)
59                         {
60                                 if (e.debugdraw_last == debugdraw_frame) continue;
61                                 e.debugdraw_last = debugdraw_frame;
62                                 vector rgb = (e.debug) ? '0 0 1' : '1 0 0';
63                                 if (autocvar_debugdraw_filter != "" && !strhasword(autocvar_debugdraw_filter, e.classname)) continue;
64                                 if (is_pure(e))
65                                 {
66                                         if (autocvar_debugdraw < 2) continue;
67                                         rgb.y = 1;
68                                 }
69                                 vector pos = project_3d_to_2d(e.origin);
70                                 if (pos.z < 0) continue;
71                                 pos.z = 0;
72                                 pos.y += ofs * size;
73                                 drawcolorcodedstring2(pos,
74                                         sprintf("%d: '%s'@%s:%d", (e.debug ? e.sv_entnum : num_for_edict(e)),
75                                         e.classname, e.sourceLocFile, e.sourceLocLine),
76                                         size * '1 1 0', rgb, 0.5, DRAWFLAG_NORMAL);
77                                 ++ofs;
78                         }
79                 ));
80         }
81 #endif
82
83 #ifdef SVQC
84         COMMON_COMMAND(debugdraw_sv, "Dump all server entities")
85         {
86                 switch (request)
87                 {
88                         case CMD_REQUEST_COMMAND:
89                         {
90                                 if (!autocvar_debugdraw) return;
91                                 int n = 1000;
92                                 int rem = n;
93                                 for (entity e = NULL; (e = findfloat(e, debug, 0)) && rem > 0; )
94                                 {
95                                         if (autocvar_debugdraw < 2 && is_pure(e)) continue;
96                                         debug_send(e, nextent(NULL), 0);
97                                         e.debug = true;
98                                         --rem;
99                                 }
100                                 LOG_INFOF("%d server entities sent\n", n - rem);
101                                 return;
102                         }
103
104                         default:
105                         case CMD_REQUEST_USAGE:
106                         {
107                                 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " debugdraw_sv"));
108                                 return;
109                         }
110                 }
111         }
112 #endif
113
114 GENERIC_COMMAND(bufstr_get, "Examine a string buffer object")
115 {
116         switch (request)
117         {
118                 case CMD_REQUEST_COMMAND:
119                 {
120                     int bufhandle = stof(argv(1));
121                     int string_index = stof(argv(2));
122                         string s = bufstr_get(bufhandle, string_index);
123                         LOG_INFOF("%s\n", s);
124                         return;
125                 }
126
127                 default:
128                 case CMD_REQUEST_USAGE:
129                 {
130                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " bufstr_get bufhandle string_index"));
131                         return;
132                 }
133         }
134 }
135
136 #endif