csqc: send origin only when needed in WP sprites (shouldn't change much)
[divverent/nexuiz.git] / data / qcsrc / client / waypointsprites.qc
1 vector project_3d_to_2d(vector vec)
2
3         vec = cs_project(vec);
4         if(cs_project_is_b0rked)
5         {
6                 vec_x += vid_width / 2;
7                 vec_y += vid_height / 2;
8         }
9         vec_x *= vid_conwidth / vid_width;
10         vec_y *= vid_conheight / vid_height;
11         return vec;
12 }
13
14 float waypointsprite_initialized;
15 float waypointsprite_fadedistance;
16 float waypointsprite_normdistance;
17 float waypointsprite_minscale;
18 float waypointsprite_minalpha;
19 float waypointsprite_distancealphaexponent;
20 float waypointsprite_timealphaexponent;
21 float waypointsprite_scale;
22
23 .float rule;
24 .string netname; // primary picture
25 .string netname2; // secondary picture
26 .string netname3; // tertiary picture
27 .float team; // team that gets netname2
28 .float lifetime;
29 .float fadetime;
30 .float maxdistance;
31 .float hideflags;
32
33 vector SPRITE_SIZE = '128 32 0';
34 vector SPRITE_HOTSPOT = '64 32 0';
35
36 void drawrotpic(vector org, float rot, string pic, vector sz, vector hotspot, vector rgb, float a, float f)
37 {
38         vector v1, v2, v3, v4;
39
40         hotspot = -1 * hotspot;
41
42         // hotspot-relative coordinates of the corners
43         v1 = hotspot;
44         v2 = hotspot + '1 0 0' * sz_x;
45         v3 = hotspot + '1 0 0' * sz_x + '0 1 0' * sz_y;
46         v4 = hotspot                  + '0 1 0' * sz_y;
47
48         // rotate them, and make them absolute
49         rot = -rot; // rotate by the opposite angle, as our coordinate system is reversed
50         v1 = rotate(v1, rot) + org;
51         v2 = rotate(v2, rot) + org;
52         v3 = rotate(v3, rot) + org;
53         v4 = rotate(v4, rot) + org;
54
55         // draw them
56         R_BeginPolygon(pic, f);
57         R_PolygonVertex(v1, '0 0 0', rgb, a);
58         R_PolygonVertex(v2, '1 0 0', rgb, a);
59         R_PolygonVertex(v3, '1 1 0', rgb, a);
60         R_PolygonVertex(v4, '0 1 0', rgb, a);
61         R_EndPolygon();
62 }
63
64 void Draw_WaypointSprite()
65 {
66         string spriteimage;
67         float t;
68
69         if(self.lifetime)
70                 self.alpha = pow(bound(0, (self.fadetime - time) / self.lifetime, 1), waypointsprite_timealphaexponent);
71         else
72                 self.alpha = 1;
73
74         if(self.hideflags & 2)
75                 return; // radar only
76
77         if(cvar("cl_hidewaypoints") >= 2)
78                 return;
79
80         if(self.hideflags & 1)
81                 if(cvar("cl_hidewaypoints"))
82                         return; // fixed waypoint
83
84         InterpolateOrigin_Do();
85
86         t = GetPlayerColor(player_localentnum - 1) + 1;
87
88         spriteimage = "";
89
90         // choose the sprite
91         switch(self.rule)
92         {
93                 case SPRITERULE_DEFAULT:
94                         if(self.team)
95                         {
96                                 if(self.team == t)
97                                         spriteimage = self.netname;
98                                 else
99                                         spriteimage = "";
100                         }
101                         else
102                                 spriteimage = self.netname;
103                         break;
104                 case SPRITERULE_TEAMPLAY:
105                         if(t == COLOR_SPECTATOR + 1)
106                                 spriteimage = self.netname3;
107                         else if(self.team == t)
108                                 spriteimage = self.netname2;
109                         else
110                                 spriteimage = self.netname;
111                         break;
112                 default:
113                         error("Invalid waypointsprite rule!");
114                         break;
115         }
116
117         if(spriteimage == "")
118                 return;
119         
120         float dist;
121         dist = vlen(self.origin - view_origin);
122         
123         float a;
124         a = self.alpha;
125
126         if(self.maxdistance > waypointsprite_normdistance)
127                 a *= pow(bound(0, (self.maxdistance - dist) / (self.maxdistance - waypointsprite_normdistance), 1), waypointsprite_distancealphaexponent);
128         else if(self.maxdistance > 0)
129                 a *= pow(bound(0, (waypointsprite_fadedistance - dist) / (waypointsprite_fadedistance - waypointsprite_normdistance), 1), waypointsprite_distancealphaexponent) * (1 - waypointsprite_minalpha) + waypointsprite_minalpha;
130
131         if(a <= 0)
132                 return;
133         
134         // draw the sprite image
135         vector o;
136         float rot;
137         o = project_3d_to_2d(self.origin);
138         rot = 0;
139
140         if(o_z < 0 || o_x < 0 || o_y < 0 || o_x > vid_conwidth || o_y > vid_conheight)
141         {
142                 // scale it to be just in view
143                 vector d;
144                 float f1, f2;
145
146                 // get the waypoint angle vector
147                 d_x = view_right * (self.origin - view_origin) * vid_conwidth / vid_width;
148                 d_y = -view_up * (self.origin - view_origin) * vid_conheight / (vid_height * vid_pixelheight);
149                 d_z = 0;
150                 
151                 //d = o - '0.5 0 0' * vid_conwidth - '0 0.5 0' * vid_conheight;
152
153                 f1 = d_x / vid_conwidth;
154                 f2 = d_y / vid_conheight;
155
156                 if(max(f1, -f1) > max(f2, -f2))
157                 {
158                         if(f1 > 0)
159                         {
160                                 // RIGHT edge
161                                 d = d * (0.5 / f1);
162                                 rot = 3;
163                         }
164                         else
165                         {
166                                 // LEFT edge
167                                 d = d * (-0.5 / f1);
168                                 rot = 1;
169                         }
170                 }
171                 else
172                 {
173                         if(f2 > 0)
174                         {
175                                 // BOTTOM edge
176                                 d = d * (0.5 / f2);
177                                 rot = 0;
178                         }
179                         else
180                         {
181                                 // TOP edge
182                                 d = d * (-0.5 / f2);
183                                 rot = 2;
184                         }
185                 }
186
187                 o = d + '0.5 0 0' * vid_conwidth + '0 0.5 0' * vid_conheight;
188         }
189         o_z = 0;
190
191         float vidscale;
192         vidscale = max(vid_conwidth / vid_width, vid_conheight / vid_height);
193
194         t = stof(db_get(tempdb, strcat("/spriteframes/", spriteimage)));
195         if(t == 0)
196                 spriteimage = strcat("models/sprites/", spriteimage);
197         else
198                 spriteimage = strcat("models/sprites/", spriteimage, "_frame", ftos(mod(floor(time * 2), t)));
199
200         drawrotpic(o, rot * 90 * DEG2RAD, spriteimage, SPRITE_SIZE * waypointsprite_scale * vidscale, SPRITE_HOTSPOT * waypointsprite_scale * vidscale, '1 1 1', a, DRAWFLAG_MIPMAP);
201 }
202
203 void Ent_WaypointSprite()
204 {
205         float sendflags, f;
206         sendflags = ReadByte();
207
208         self.draw2d = Draw_WaypointSprite;
209
210         InterpolateOrigin_Undo();
211
212         if(sendflags & 64)
213         {
214                 // unfortunately, this needs to be exact (for the 3D display)
215                 self.origin_x = ReadCoord();
216                 self.origin_y = ReadCoord();
217                 self.origin_z = ReadCoord();
218         }
219
220         if(sendflags & 1)
221         {
222                 self.team = ReadByte();
223                 self.rule = ReadByte();
224         }
225
226         if(sendflags & 2)
227         {
228                 if(self.netname)
229                         strunzone(self.netname);
230                 self.netname = strzone(ReadString());
231         }
232
233         if(sendflags & 4)
234         {
235                 if(self.netname2)
236                         strunzone(self.netname2);
237                 self.netname2 = strzone(ReadString());
238         }
239
240         if(sendflags & 8)
241         {
242                 if(self.netname3)
243                         strunzone(self.netname3);
244                 self.netname3 = strzone(ReadString());
245         }
246
247         if(sendflags & 16)
248         {
249                 self.lifetime = ReadCoord();
250                 self.fadetime = ReadCoord();
251                 self.maxdistance = ReadShort();
252                 self.hideflags = ReadByte();
253         }
254
255         if(sendflags & 32)
256         {
257                 f = ReadByte();
258                 self.teamradar_icon = (f & 0x7F);
259                 if(f & 0x80)
260                 {
261                         self.(teamradar_times[self.teamradar_time_index]) = time;
262                         self.teamradar_time_index = mod(self.teamradar_time_index + 1, MAX_TEAMRADAR_TIMES);
263                 }
264                 self.teamradar_color_x = ReadByte() / 255.0;
265                 self.teamradar_color_y = ReadByte() / 255.0;
266                 self.teamradar_color_z = ReadByte() / 255.0;
267         }
268
269         InterpolateOrigin_Note();
270 }
271
272 void Ent_RemoveWaypointSprite()
273 {
274         if(self.netname)
275                 strunzone(self.netname);
276         if(self.netname2)
277                 strunzone(self.netname2);
278         if(self.netname3)
279                 strunzone(self.netname3);
280 }
281
282 void WaypointSprite_Load()
283 {
284         waypointsprite_fadedistance = vlen(world.maxs - world.mins);
285         waypointsprite_normdistance = cvar("g_waypointsprite_normdistance");
286         waypointsprite_minscale = cvar("g_waypointsprite_minscale");
287         waypointsprite_minalpha = cvar("g_waypointsprite_minalpha");
288         waypointsprite_distancealphaexponent = cvar("g_waypointsprite_distancealphaexponent");
289         waypointsprite_timealphaexponent = cvar("g_waypointsprite_timealphaexponent");
290         waypointsprite_scale = cvar("g_waypointsprite_scale");
291         if(!waypointsprite_scale)
292                 waypointsprite_scale = 1.0;
293
294         if(!waypointsprite_initialized)
295         {
296                 float dh, n, i, o, f;
297                 string s, sname, sframes;
298                 dh = search_begin("models/sprites/*_frame*.tga", FALSE, FALSE);
299                 n = search_getsize(dh);
300                 for(i = 0; i < n; ++i)
301                 {
302                         s = search_getfilename(dh, i);
303                         if(substring(s, 0, 15) != "models/sprites/")
304                                 continue;
305                         if(substring(s, strlen(s) - 4, 4) != ".tga")
306                                 continue;
307                         s = substring(s, 15, strlen(s) - 19);
308
309                         o = strstrofs(s, "_frame", 0);
310                         sname = strcat("/spriteframes/", substring(s, 0, o));
311                         sframes = substring(s, o + 6, strlen(s) - o - 6);
312                         f = stof(sframes) + 1;
313                         db_put(tempdb, sname, ftos(max(f, stof(db_get(tempdb, sname)))));
314                 }
315                 search_end(dh);
316         }
317         waypointsprite_initialized = 1;
318 }