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