onslaught sprite code is back, now using csqc. no radar support yet, but that will...
[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                 d = o - '0.5 0 0' * vid_conwidth - '0 0.5 0' * vid_conheight;
146
147                 f1 = d_x / vid_conwidth;
148                 f2 = d_y / vid_conheight;
149
150                 if(max(f1, -f1) > max(f2, -f2))
151                 {
152                         if(f1 > 0)
153                         {
154                                 // RIGHT edge
155                                 d = d * (0.5 / f1);
156                                 rot = 3;
157                         }
158                         else
159                         {
160                                 // LEFT edge
161                                 d = d * (-0.5 / f1);
162                                 rot = 1;
163                         }
164                 }
165                 else
166                 {
167                         if(f2 > 0)
168                         {
169                                 // BOTTOM edge
170                                 d = d * (0.5 / f2);
171                                 rot = 0;
172                         }
173                         else
174                         {
175                                 // TOP edge
176                                 d = d * (-0.5 / f2);
177                                 rot = 2;
178                         }
179                 }
180
181                 o = d + '0.5 0 0' * vid_conwidth + '0 0.5 0' * vid_conheight;
182         }
183         o_z = 0;
184
185         float vidscale;
186         vidscale = max(vid_conwidth / vid_width, vid_conheight / vid_height);
187
188         t = stof(db_get(tempdb, strcat("/spriteframes/", spriteimage)));
189         if(t == 0)
190                 spriteimage = strcat("models/sprites/", spriteimage);
191         else
192                 spriteimage = strcat("models/sprites/", spriteimage, "_frame", ftos(mod(floor(time * 2), t)));
193
194         drawrotpic(o, rot * 90 * DEG2RAD, spriteimage, SPRITE_SIZE * waypointsprite_scale * vidscale, SPRITE_HOTSPOT * waypointsprite_scale * vidscale, '1 1 1', a, DRAWFLAG_MIPMAP);
195 }
196
197 void Ent_WaypointSprite()
198 {
199         float sendflags, f;
200         sendflags = ReadByte();
201
202         self.draw2d = Draw_WaypointSprite;
203
204         InterpolateOrigin_Undo();
205
206         // unfortunately, this needs to be exact (for the 3D display)
207         self.origin_x = ReadCoord();
208         self.origin_y = ReadCoord();
209         self.origin_z = ReadCoord();
210
211         if(sendflags & 1)
212         {
213                 self.team = ReadByte();
214                 self.rule = ReadByte();
215         }
216
217         if(sendflags & 2)
218         {
219                 if(self.netname)
220                         strunzone(self.netname);
221                 self.netname = strzone(ReadString());
222         }
223
224         if(sendflags & 4)
225         {
226                 if(self.netname2)
227                         strunzone(self.netname2);
228                 self.netname2 = strzone(ReadString());
229         }
230
231         if(sendflags & 8)
232         {
233                 if(self.netname3)
234                         strunzone(self.netname3);
235                 self.netname3 = strzone(ReadString());
236         }
237
238         if(sendflags & 16)
239         {
240                 self.lifetime = ReadCoord();
241                 self.fadetime = ReadCoord();
242                 self.maxdistance = ReadShort();
243                 self.hideflags = ReadByte();
244         }
245
246         if(sendflags & 32)
247         {
248                 f = ReadByte();
249                 self.teamradar_icon = (f & 0x7F);
250                 if(f & 0x80)
251                         self.teamradar_time = time;
252                 self.teamradar_color_x = ReadByte() / 255.0;
253                 self.teamradar_color_y = ReadByte() / 255.0;
254                 self.teamradar_color_z = ReadByte() / 255.0;
255         }
256
257         InterpolateOrigin_Note();
258 }
259
260 void Ent_RemoveWaypointSprite()
261 {
262         if(self.netname)
263                 strunzone(self.netname);
264         if(self.netname2)
265                 strunzone(self.netname2);
266         if(self.netname3)
267                 strunzone(self.netname3);
268 }
269
270 void WaypointSprite_Load()
271 {
272         waypointsprite_fadedistance = vlen(world.maxs - world.mins);
273         waypointsprite_normdistance = cvar("g_waypointsprite_normdistance");
274         waypointsprite_minscale = cvar("g_waypointsprite_minscale");
275         waypointsprite_minalpha = cvar("g_waypointsprite_minalpha");
276         waypointsprite_distancealphaexponent = cvar("g_waypointsprite_distancealphaexponent");
277         waypointsprite_timealphaexponent = cvar("g_waypointsprite_timealphaexponent");
278         waypointsprite_scale = cvar("g_waypointsprite_scale");
279         if(!waypointsprite_scale)
280                 waypointsprite_scale = 1.0;
281
282         if(!waypointsprite_initialized)
283         {
284                 float dh, n, i, o, f;
285                 string s, sname, sframes;
286                 dh = search_begin("models/sprites/*_frame*.tga", FALSE, FALSE);
287                 n = search_getsize(dh);
288                 for(i = 0; i < n; ++i)
289                 {
290                         s = search_getfilename(dh, i);
291                         if(substring(s, 0, 15) != "models/sprites/")
292                                 continue;
293                         if(substring(s, strlen(s) - 4, 4) != ".tga")
294                                 continue;
295                         s = substring(s, 15, strlen(s) - 19);
296
297                         o = strstrofs(s, "_frame", 0);
298                         sname = strcat("/spriteframes/", substring(s, 0, o));
299                         sframes = substring(s, o + 6, strlen(s) - o - 6);
300                         f = stof(sframes) + 1;
301                         db_put(tempdb, sname, ftos(max(f, stof(db_get(tempdb, sname)))));
302                 }
303                 search_end(dh);
304         }
305         waypointsprite_initialized = 1;
306 }