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