]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/teamradar.qc
support move_avelocity too
[divverent/nexuiz.git] / data / qcsrc / client / teamradar.qc
1 float teamradar_angle; // player yaw angle
2 vector teamradar_origin3d_in_texcoord; // player origin
3 vector teamradar_origin2d; // 2D origin
4 vector teamradar_size2d; // 2D size
5 float teamradar_size; // 2D scale factor
6 float cl_teamradar_scale; // window size = ...qu
7 float v_flipped;
8
9 float vlen2d(vector v)
10 {
11         return sqrt(v_x * v_x + v_y * v_y);
12 }
13
14 float vlen_maxnorm2d(vector v)
15 {
16         return max4(v_x, v_y, -v_x, -v_y);
17 }
18
19 float vlen_minnorm2d(vector v)
20 {
21         return min(max(v_x, -v_x), max(v_y, -v_y));
22 }
23
24 vector teamradar_3dcoord_to_texcoord(vector in)
25 {
26         vector out;
27         out_x = (in_x - mi_picmin_x) / (mi_picmax_x - mi_picmin_x);
28         out_y = (in_y - mi_picmin_y) / (mi_picmax_y - mi_picmin_y);
29         out_z = 0;
30         return out;
31 }
32
33 vector teamradar_texcoord_to_2dcoord(vector in)
34 {
35         vector out;
36         in -= teamradar_origin3d_in_texcoord;
37
38         out = rotate(in, teamradar_angle * DEG2RAD);
39         out_y = - out_y; // screen space is reversed
40
41         out = out * teamradar_size;
42         if(v_flipped)
43                 out_x = -out_x;
44         out += teamradar_origin2d;
45         return out;
46 }
47
48 vector yinvert(vector v)
49 {
50         v_y = 1 - v_y;
51         return v;
52 }
53
54 void draw_teamradar_background(float bg, float fg)
55 {
56         if(bg > 0)
57         {
58                 R_BeginPolygon("", 0);
59                 R_PolygonVertex('1 0 0' * (teamradar_origin2d_x - teamradar_size2d_x * 0.5) + '0 1 0' * (teamradar_origin2d_y - teamradar_size2d_y * 0.5), '0 0 0', '0 0 0', bg);
60                 R_PolygonVertex('1 0 0' * (teamradar_origin2d_x + teamradar_size2d_x * 0.5) + '0 1 0' * (teamradar_origin2d_y - teamradar_size2d_y * 0.5), '0 0 0', '0 0 0', bg);
61                 R_PolygonVertex('1 0 0' * (teamradar_origin2d_x + teamradar_size2d_x * 0.5) + '0 1 0' * (teamradar_origin2d_y + teamradar_size2d_y * 0.5), '0 0 0', '0 0 0', bg);
62                 R_PolygonVertex('1 0 0' * (teamradar_origin2d_x - teamradar_size2d_x * 0.5) + '0 1 0' * (teamradar_origin2d_y + teamradar_size2d_y * 0.5), '0 0 0', '0 0 0', bg);
63                 R_EndPolygon();
64         }
65
66         if(fg > 0 && minimapname != "")
67         {
68                 if(csqc_flags & CSQC_FLAG_READPICTURE) // not 2.4.2
69                         R_BeginPolygon(minimapname, DRAWFLAG_SCREEN | DRAWFLAG_MIPMAP);
70                 else
71                         R_BeginPolygon(minimapname, DRAWFLAG_ADDITIVE);
72                 if(v_flipped)
73                 {
74                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord3), yinvert(mi_pictexcoord3), '1 1 1', fg);
75                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord2), yinvert(mi_pictexcoord2), '1 1 1', fg);
76                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord1), yinvert(mi_pictexcoord1), '1 1 1', fg);
77                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord0), yinvert(mi_pictexcoord0), '1 1 1', fg);
78                 }
79                 else
80                 {
81                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord0), yinvert(mi_pictexcoord0), '1 1 1', fg);
82                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord1), yinvert(mi_pictexcoord1), '1 1 1', fg);
83                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord2), yinvert(mi_pictexcoord2), '1 1 1', fg);
84                         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord3), yinvert(mi_pictexcoord3), '1 1 1', fg);
85                 }
86                 R_EndPolygon();
87         }
88 }
89
90 void(vector coord3d, vector pangles, vector rgb) draw_teamradar_player =
91 {
92         vector coord, rgb2;
93
94         coord = teamradar_texcoord_to_2dcoord(teamradar_3dcoord_to_texcoord(coord3d));
95
96         makevectors(pangles - '0 1 0' * teamradar_angle);
97         if(v_flipped)
98         {
99                 v_forward_x = -v_forward_x;
100                 v_right_x = -v_right_x;
101                 v_up_x = -v_up_x;
102         }
103         v_forward_z = 0;
104         v_forward = normalize(v_forward);
105         v_forward_y *= -1.0;
106         v_right_x = -v_forward_y;
107         v_right_y = v_forward_x;
108
109         if(rgb == '1 1 1')
110                 rgb2 = '0 0 0';
111         else
112                 rgb2 = '1 1 1';
113
114         R_BeginPolygon("", 0);
115         R_PolygonVertex(coord+v_forward*3, '0 0 0', rgb2, 1);
116         R_PolygonVertex(coord+v_right*4-v_forward*2.5, '0 1 0', rgb2, 1);
117         R_PolygonVertex(coord-v_forward*2, '1 0 0', rgb2, 1);
118         R_PolygonVertex(coord-v_right*4-v_forward*2.5, '1 1 0', rgb2, 1);
119         R_EndPolygon();
120
121         R_BeginPolygon("", 0);
122         R_PolygonVertex(coord+v_forward*2, '0 0 0', rgb, 1);
123         R_PolygonVertex(coord+v_right*3-v_forward*2, '0 1 0', rgb, 1);
124         R_PolygonVertex(coord-v_forward, '1 0 0', rgb, 1);
125         R_PolygonVertex(coord-v_right*3-v_forward*2, '1 1 0', rgb, 1);
126         R_EndPolygon();
127 };
128
129 void draw_teamradar_icon(vector coord, float icon, entity pingdata, vector rgb, float a)
130 {
131         float dt;
132         vector v;
133         float i;
134
135         coord = teamradar_texcoord_to_2dcoord(teamradar_3dcoord_to_texcoord(coord));
136         drawpic(coord - '4 4 0', strcat("gfx/teamradar_icon_", ftos(icon)), '8 8 0', rgb, a, 0);
137
138         if(pingdata)
139         {
140                 for(i = 0; i < MAX_TEAMRADAR_TIMES; ++i)
141                 {
142                         dt = pingdata.(teamradar_times[i]);
143                         if(dt == 0)
144                                 continue;
145                         dt = time - dt;
146                         if(dt > 1)
147                                 continue;
148                         v = '2 2 0' * teamradar_size * dt;
149                         drawpic(coord - 0.5 * v, "gfx/teamradar_ping", v, '1 1 1', 1 - dt, DRAWFLAG_ADDITIVE);
150                 }
151         }
152 }
153
154 void draw_teamradar_link(vector start, vector end, float colors)
155 {
156         vector c0, c1, norm;
157
158         start = teamradar_texcoord_to_2dcoord(teamradar_3dcoord_to_texcoord(start));
159         end = teamradar_texcoord_to_2dcoord(teamradar_3dcoord_to_texcoord(end));
160         norm = normalize(start - end);
161         norm_z = norm_x;
162         norm_x = -norm_y;
163         norm_y = norm_z;
164         norm_z = 0;
165
166         c0 = colormapPaletteColor(colors & 0x0F, FALSE);
167         c1 = colormapPaletteColor((colors & 0xF0) / 0x10, FALSE);
168
169         R_BeginPolygon("", 0);
170         R_PolygonVertex(start - norm, '0 0 0', c0, 1);
171         R_PolygonVertex(start + norm, '0 1 0', c0, 1);
172         R_PolygonVertex(end + norm, '1 1 0', c1, 1);
173         R_PolygonVertex(end - norm, '1 0 0', c1, 1);
174         R_EndPolygon();
175 }
176
177 float cl_teamradar_scale;
178 float cl_teamradar_background_alpha;
179 float cl_teamradar_foreground_alpha;
180 float cl_teamradar_rotation;
181 vector cl_teamradar_size;
182 vector cl_teamradar_position;
183 float cl_teamradar_zoommode;
184
185 void teamradar_loadcvars()
186 {
187         v_flipped = cvar("v_flipped");
188         if(ons_showmap)
189         {
190                 cl_teamradar_scale = 42; // dummy, not used (see zoommode)
191                 cl_teamradar_background_alpha = 1;
192                 cl_teamradar_foreground_alpha = 1;
193                 cl_teamradar_rotation = cvar("cl_teamradar_rotation");
194                 if(!cl_teamradar_rotation)
195                         cl_teamradar_rotation = 4;
196                 cl_teamradar_size = '256 256 0'; // TODO make somewhat variable?
197                 cl_teamradar_position = '0.5 0.5 0';
198                 cl_teamradar_zoommode = 3;
199         }
200         else
201         {
202                 cl_teamradar_scale = cvar("cl_teamradar_scale");
203                 cl_teamradar_background_alpha = cvar("cl_teamradar_background_alpha");
204                 cl_teamradar_foreground_alpha = cvar("cl_teamradar_foreground_alpha");
205                 cl_teamradar_rotation = cvar("cl_teamradar_rotation");
206                 cl_teamradar_size = stov(cvar_string("cl_teamradar_size"));
207                 cl_teamradar_position = stov(cvar_string("cl_teamradar_position"));
208                 cl_teamradar_zoommode = cvar("cl_teamradar_zoommode");
209
210                 // others default to 0
211                 // match this to defaultNexuiz.cfg!
212                 if(!cl_teamradar_scale) cl_teamradar_scale = 4096;
213                 if(!cl_teamradar_background_alpha) cl_teamradar_background_alpha = 0.4;
214                 if(!cl_teamradar_foreground_alpha) cl_teamradar_foreground_alpha = 0.8;
215                 if(!cl_teamradar_size_x) cl_teamradar_size_x = 128;
216                 if(!cl_teamradar_size_y) cl_teamradar_size_y = cl_teamradar_size_x;
217
218                 cl_teamradar_size_z = 0;
219                 cl_teamradar_position_z = 0;
220         }
221 }
222
223 void() teamradar_view =
224 {
225         local float color, color2;
226         local vector rgb;
227         local entity tm;
228         float scale2d, normalsize, bigsize;
229         float f;
230
231         if(minimapname == "" && !ons_showmap)
232                 return;
233
234         teamradar_loadcvars();
235
236         switch(cl_teamradar_zoommode)
237         {
238                 default:
239                 case 0:
240                         f = current_zoomfraction;
241                         break;
242                 case 1:
243                         f = 1 - current_zoomfraction;
244                         break;
245                 case 2:
246                         f = 0;
247                         break;
248                 case 3:
249                         f = 1;
250                         break;
251         }
252
253         switch(cl_teamradar_rotation)
254         {
255                 case 0:
256                         teamradar_angle = view_angles_y - 90;
257                         break;
258                 default:
259                         teamradar_angle = 90 * cl_teamradar_rotation;
260                         break;
261         }
262
263         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
264         teamradar_size2d = cl_teamradar_size;
265         teamradar_origin2d =
266                   '1 0 0' * (0.5 * teamradar_size2d_x + cl_teamradar_position_x * (vid_conwidth - teamradar_size2d_x))
267                 + '0 1 0' * (0.5 * teamradar_size2d_y + cl_teamradar_position_y * (vid_conheight - teamradar_size2d_y));
268
269         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
270         if(cl_teamradar_rotation == 0)
271         {
272                 // max-min distance must fit the radar in any rotation
273                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen2d(mi_max - mi_min));
274         }
275         else
276         {
277                 vector c0, c1, c2, c3, span;
278                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
279                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
280                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
281                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
282                 span = '0 0 0';
283                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
284                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
285
286                 // max-min distance must fit the radar in x=x, y=y
287                 bigsize = min(
288                         teamradar_size2d_x * scale2d / (1.05 * span_x),
289                         teamradar_size2d_y * scale2d / (1.05 * span_y)
290                 );
291         }
292
293         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / cl_teamradar_scale;
294         if(bigsize > normalsize)
295                 normalsize = bigsize;
296
297         teamradar_size =
298                   f * bigsize
299                 + (1 - f) * normalsize;
300         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
301                   f * (mi_min + mi_max) * 0.5
302                 + (1 - f) * view_origin);
303
304         color = GetPlayerColor(player_localentnum-1);
305         rgb = GetTeamRGB(color);
306
307         drawsetcliparea(
308                 teamradar_origin2d_x - teamradar_size2d_x * 0.5,
309                 teamradar_origin2d_y - teamradar_size2d_y * 0.5,
310                 teamradar_size2d_x,
311                 teamradar_size2d_y
312         );
313
314         draw_teamradar_background(cl_teamradar_background_alpha, cl_teamradar_foreground_alpha);
315
316         if(ons_showmap)
317         {
318                 drawresetcliparea();
319
320                 vector frame_origin, frame_size;
321                 frame_origin = frame_size = '0 0 0';
322
323                 frame_origin_x = teamradar_origin2d_x - teamradar_size2d_x * 0.55859375;
324                 frame_origin_y = teamradar_origin2d_y - teamradar_size2d_y * 0.55859375;
325                 frame_size_x = teamradar_size2d_x * 1.1171875;
326                 frame_size_y = teamradar_size2d_y * 1.1171875;
327                 drawpic(frame_origin, "gfx/ons-frame.tga", frame_size, '1 1 1', 1, 0);
328                 drawpic(frame_origin, "gfx/ons-frame-team.tga", frame_size, rgb, 1, 0);
329
330                 drawsetcliparea(
331                         teamradar_origin2d_x - teamradar_size2d_x * 0.5,
332                         teamradar_origin2d_y - teamradar_size2d_y * 0.5,
333                         teamradar_size2d_x,
334                         teamradar_size2d_y
335                 );
336         }
337
338         for(tm = world; (tm = find(tm, classname, "radarlink")); )
339                 draw_teamradar_link(tm.origin, tm.velocity, tm.team);
340         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
341                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm, tm.teamradar_color, tm.alpha);
342         for(tm = world; (tm = find(tm, classname, "entcs_receiver")); )
343         {
344                 color2 = GetPlayerColor(tm.sv_entnum);
345                 //if(color == COLOR_SPECTATOR || color == color2)
346                         draw_teamradar_player(tm.origin, tm.angles, GetTeamRGB(color2));
347         }
348         draw_teamradar_player(view_origin, view_angles, '1 1 1');
349
350         drawresetcliparea();
351 };
352
353
354
355 // radar links
356
357 void Ent_RadarLink()
358 {
359         float sendflags;
360         sendflags = ReadByte();
361
362         InterpolateOrigin_Undo();
363
364         self.iflags = IFLAG_VELOCITY;
365         self.classname = "radarlink";
366
367         if(sendflags & 1)
368         {
369                 self.origin_x = ReadCoord();
370                 self.origin_y = ReadCoord();
371                 self.origin_z = ReadCoord();
372         }
373
374         if(sendflags & 2)
375         {
376                 self.velocity_x = ReadCoord();
377                 self.velocity_y = ReadCoord();
378                 self.velocity_z = ReadCoord();
379         }
380
381         if(sendflags & 4)
382         {
383                 self.team = ReadByte();
384         }
385
386         InterpolateOrigin_Note();
387 }