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