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