]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/debug.qh
Cleanup
[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         .int debugdraw_last;
47         vector project_3d_to_2d(vector vec);
48         void Debug_Draw()
49         {
50                 if (!autocvar_debugdraw) return;
51                 static int debugdraw_frame;
52                 ++debugdraw_frame;
53                 const int size = 8;
54                 for (entity e1 = NULL; (e1 = nextent(e1)); )
55                 {
56                         if (e1.debugdraw_last == debugdraw_frame) continue;
57                         int ofs = 0;
58                         for (entity e = findradius(e1.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 (is_pure(e))
64                                 {
65                                         if (autocvar_debugdraw < 2) continue;
66                                         rgb.y = 1;
67                                 }
68                                 vector pos = project_3d_to_2d(e.origin);
69                                 if (pos.z < 0) continue;
70                                 pos.z = 0;
71                                 pos.y += ofs * size;
72                                 drawcolorcodedstring2(pos,
73                                         sprintf("%d: '%s'@%s:%d", (e.debug ? e.sv_entnum : num_for_edict(e)),
74                                         e.classname, e.sourceLocFile, e.sourceLocLine),
75                                         size * '1 1 0', rgb, 0.5, DRAWFLAG_NORMAL);
76                                 ++ofs;
77                         }
78                 }
79         }
80 #endif
81
82 #ifdef SVQC
83         GENERIC_COMMAND(debugdraw_sv, "Dump all server entities")
84         {
85                 switch (request)
86                 {
87                         case CMD_REQUEST_COMMAND:
88                         {
89                                 if (!autocvar_debugdraw) return;
90                                 int n = 1000;
91                                 int rem = n;
92                                 for (entity e = NULL; (e = findfloat(e, debug, 0)) && rem > 0; )
93                                 {
94                                         if (autocvar_debugdraw < 2 && is_pure(e)) continue;
95                                         debug_send(e, nextent(NULL), 0);
96                                         e.debug = true;
97                                         --rem;
98                                 }
99                                 LOG_INFOF("%d server entities sent\n", n - rem);
100                                 return;
101                         }
102
103                         default:
104                         case CMD_REQUEST_USAGE:
105                         {
106                                 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " debugdraw_sv"));
107                                 return;
108                         }
109                 }
110         }
111 #endif
112
113 #endif