]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/teamradar.qc
cl_teamradar_position
[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 a)
47 {
48         if(a <= 0)
49                 return;
50
51         R_BeginPolygon("", 0);
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', a);
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', a);
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', a);
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', a);
56         R_EndPolygon();
57
58         R_BeginPolygon(minimapname, DRAWFLAG_ADDITIVE | DRAWFLAG_MIPMAP);
59         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord0), yinvert(mi_pictexcoord0), '1 1 1', 1);
60         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord1), yinvert(mi_pictexcoord1), '1 1 1', 1);
61         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord2), yinvert(mi_pictexcoord2), '1 1 1', 1);
62         R_PolygonVertex(teamradar_texcoord_to_2dcoord(mi_pictexcoord3), yinvert(mi_pictexcoord3), '1 1 1', 1);
63         R_EndPolygon();
64 }
65
66 void(vector coord3d, vector pangles, vector rgb) draw_teamradar_player =
67 {
68         vector coord, rgb2;
69
70         coord = teamradar_texcoord_to_2dcoord(teamradar_3dcoord_to_texcoord(coord3d));
71
72         makevectors(pangles - '0 1 0' * teamradar_angle);
73         v_forward_z = 0;
74         v_forward = normalize(v_forward);
75         v_forward_y *= -1.0;
76         v_right_x = -v_forward_y;
77         v_right_y = v_forward_x;
78
79         if(rgb == '1 1 1')
80                 rgb2 = '0 0 0';
81         else
82                 rgb2 = '1 1 1';
83
84         R_BeginPolygon("", 0);
85         R_PolygonVertex(coord+v_forward*3, '0 0 0', rgb2, 1);
86         R_PolygonVertex(coord+v_right*4-v_forward*2.5, '0 1 0', rgb2, 1);
87         R_PolygonVertex(coord-v_forward*2, '1 0 0', rgb2, 1);
88         R_PolygonVertex(coord-v_right*4-v_forward*2.5, '1 1 0', rgb2, 1);
89         R_EndPolygon();
90
91         R_BeginPolygon("", 0);
92         R_PolygonVertex(coord+v_forward*2, '0 0 0', rgb, 1);
93         R_PolygonVertex(coord+v_right*3-v_forward*2, '0 1 0', rgb, 1);
94         R_PolygonVertex(coord-v_forward, '1 0 0', rgb, 1);
95         R_PolygonVertex(coord-v_right*3-v_forward*2, '1 1 0', rgb, 1);
96         R_EndPolygon();
97 };
98
99 void draw_teamradar_icon(vector coord, float icon, float pingtime, vector rgb, float a)
100 {
101         float dt;
102         vector v;
103
104         coord = teamradar_texcoord_to_2dcoord(teamradar_3dcoord_to_texcoord(coord));
105         drawpic(coord - '4 4 0', strcat("gfx/teamradar_icon_", ftos(icon)), '8 8 0', rgb, a, 0);
106         if(pingtime != 0)
107         {
108                 dt = time - pingtime;
109                 if(dt > 1)
110                         return;
111                 v = '2 2 0' * teamradar_size * dt;
112                 drawpic(coord - 0.5 * v, "gfx/teamradar_ping", v, '1 1 1', 1 - dt, DRAWFLAG_ADDITIVE);
113         }
114 }
115
116 float cl_teamradar_scale;
117 float cl_teamradar_background_alpha;
118 float cl_teamradar_rotation;
119 vector cl_teamradar_size;
120 vector cl_teamradar_position;
121 float cl_teamradar_zoommode;
122
123 void teamradar_loadcvars()
124 {
125         cl_teamradar_scale = cvar("cl_teamradar_scale");
126         cl_teamradar_background_alpha = cvar("cl_teamradar_background_alpha");
127         cl_teamradar_rotation = cvar("cl_teamradar_rotation");
128         cl_teamradar_size = stov(cvar_string("cl_teamradar_size"));
129         cl_teamradar_position = stov(cvar_string("cl_teamradar_position"));
130         cl_teamradar_zoommode = cvar("cl_teamradar_zoommode");
131
132         // others default to 0
133         if(!cl_teamradar_scale) cl_teamradar_scale = 4096;
134         if(!cl_teamradar_background_alpha) cl_teamradar_background_alpha = 0.75;
135         if(!cl_teamradar_size_x) cl_teamradar_size_x = 128;
136         if(!cl_teamradar_size_y) cl_teamradar_size_y = cl_teamradar_size_x;
137
138         cl_teamradar_size_z = 0;
139         cl_teamradar_position_z = 0;
140 }
141
142 void() teamradar_view =
143 {
144         local float color;
145         local vector rgb;
146         local entity tm;
147         float scale2d, normalsize, bigsize;
148         float f;
149
150         teamradar_loadcvars();
151
152         switch(cl_teamradar_zoommode)
153         {
154                 default:
155                 case 0:
156                         f = current_zoomfraction;
157                         break;
158                 case 1:
159                         f = 1 - current_zoomfraction;
160                         break;
161                 case 2:
162                         f = 0;
163                         break;
164                 case 3:
165                         f = 1;
166                         break;
167         }
168
169         switch(cl_teamradar_rotation)
170         {
171                 case 0:
172                         teamradar_angle = input_angles_y - 90;
173                         break;
174                 default:
175                         teamradar_angle = 90 * cl_teamradar_rotation;
176                         break;
177         }
178
179         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
180         teamradar_size2d = cl_teamradar_size;
181         teamradar_origin2d =
182                   '1 0 0' * (0.5 * teamradar_size2d_x + cl_teamradar_position_x * (vid_conwidth - teamradar_size2d_x))
183                 + '0 1 0' * (0.5 * teamradar_size2d_y + cl_teamradar_position_y * (vid_conheight - teamradar_size2d_y));
184
185         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
186         if(cl_teamradar_rotation == 0)
187         {
188                 // max-min distance must fit the radar in any rotation
189                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen(mi_max - mi_min));
190         }
191         else
192         {
193                 vector c0, c1, c2, c3, span;
194                 c0 = rotate(mi_min, teamradar_angle * DEG2RAD);
195                 c1 = rotate(mi_max, teamradar_angle * DEG2RAD);
196                 c2 = rotate('1 0 0' * mi_min_x + '0 1 0' * mi_max_y, teamradar_angle * DEG2RAD);
197                 c3 = rotate('1 0 0' * mi_max_x + '0 1 0' * mi_min_y, teamradar_angle * DEG2RAD);
198                 span = '0 0 0';
199                 span_x = max4(c0_x, c1_x, c2_x, c3_x) - min4(c0_x, c1_x, c2_x, c3_x);
200                 span_y = max4(c0_y, c1_y, c2_y, c3_y) - min4(c0_y, c1_y, c2_y, c3_y);
201
202                 // max-min distance must fit the radar in x=x, y=y
203                 bigsize = min(
204                         teamradar_size2d_x * scale2d / (1.05 * span_x),
205                         teamradar_size2d_y * scale2d / (1.05 * span_y)
206                 );
207         }
208
209         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / cl_teamradar_scale;
210         if(bigsize > normalsize)
211                 normalsize = bigsize;
212
213         teamradar_size =
214                   f * bigsize
215                 + (1 - f) * normalsize;
216         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
217                   f * (mi_min + mi_max) * 0.5
218                 + (1 - f) * pmove_org);
219
220         color = GetPlayerColor(player_localentnum-1);
221         rgb = GetTeamRGB(color);
222
223         drawsetcliparea(
224                 teamradar_origin2d_x - teamradar_size2d_x * 0.5,
225                 teamradar_origin2d_y - teamradar_size2d_y * 0.5,
226                 teamradar_size2d_x,
227                 teamradar_size2d_y
228         );
229
230         draw_teamradar_background(cl_teamradar_background_alpha);
231         for(tm = world; (tm = findflags(tm, teamradar_icon, 0xFFFFFF)); )
232                 draw_teamradar_icon(tm.origin, tm.teamradar_icon, tm.teamradar_time, tm.teamradar_color, tm.alpha);
233         for(tm = gps_start; tm != world; tm = tm.chain)
234                 draw_teamradar_player(tm.origin, tm.angles, rgb);
235         draw_teamradar_player(pmove_org, input_angles, '1 1 1');
236
237         drawresetcliparea();
238 };