electro/hook: add a safeguard to stop the sound 10 seconds after last update
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hook.qc
1 .float HookType; // ENT_CLIENT_*
2 .vector origin;
3 .vector velocity;
4 .float HookSound;
5 .float HookSilent;
6 .float HookRange;
7
8 void Draw_CylindricLine(vector from, vector to, float thickness, string texture, float aspect, float shift, vector rgb, float alpha, float drawflag, vector vieworg)
9 {
10         // I want to draw a quad...
11         // from and to are MIDPOINTS.
12         
13         vector axis, thickdir, A, B, C, D;
14         float length_tex;
15
16         axis = normalize(to - from);
17         length_tex = aspect * vlen(to - from) / thickness;
18
19         // direction is perpendicular to the view normal, and perpendicular to the axis
20         thickdir = normalize(cross(axis, vieworg - from));
21
22 /*
23         print("from ", vtos(from), "\n");
24         print("to ", vtos(to), "\n");
25         print("org ", vtos(view_origin), "\n");
26         print("dir ", vtos(thickdir), "\n");
27 */
28
29         A = from - thickdir * (thickness / 2);
30         B = from + thickdir * (thickness / 2);
31         C = to + thickdir * (thickness / 2);
32         D = to - thickdir * (thickness / 2);
33
34         R_BeginPolygon(texture, drawflag);
35         R_PolygonVertex(A, '0 0 0' + shift * '1 0 0', rgb, alpha);
36         R_PolygonVertex(B, '0 1 0' + shift * '1 0 0', rgb, alpha);
37         R_PolygonVertex(C, '0 1 0' + (shift + length_tex) * '1 0 0', rgb, alpha);
38         R_PolygonVertex(D, '0 0 0' + (shift + length_tex) * '1 0 0', rgb, alpha);
39         R_EndPolygon();
40 }
41
42 string Draw_GrapplingHook_trace_callback_tex;
43 float Draw_GrapplingHook_trace_callback_rnd;
44 vector Draw_GrapplingHook_trace_callback_rgb;
45 float Draw_GrapplingHook_trace_callback_a;
46 void Draw_GrapplingHook_trace_callback(vector start, vector hit, vector end)
47 {
48         float i;
49         vector vorg;
50         vorg = WarpZone_TransformOrigin(WarpZone_trace_transform, view_origin);
51         for(i = 0; i < Draw_GrapplingHook_trace_callback_a; ++i)
52                 Draw_CylindricLine(hit, start, 8, Draw_GrapplingHook_trace_callback_tex, 0.25, Draw_GrapplingHook_trace_callback_rnd, Draw_GrapplingHook_trace_callback_rgb, min(1, Draw_GrapplingHook_trace_callback_a - i), DRAWFLAG_NORMAL, vorg);
53         Draw_GrapplingHook_trace_callback_rnd += 0.25 * vlen(hit - start) / 8;
54 }
55
56 void Draw_GrapplingHook()
57 {
58         vector a, b, atrans;
59         string tex, snd;
60         vector rgb;
61         float t;
62         float s;
63         vector vs;
64         float intensity, offset;
65
66         if(self.teleport_time)
67         if(time > self.teleport_time)
68         {
69                 sound (self, CHAN_PROJECTILE, "misc/null.wav", VOL_BASE, ATTN_NORM); // safeguard
70                 self.teleport_time = 0;
71         }
72
73         InterpolateOrigin_Do();
74
75         s = cvar("cl_gunalign");
76         if(s != 1 && s != 2 && s != 4)
77                 s = 3; // default value
78         --s;
79         switch(self.HookType)
80         {
81                 default:
82                 case ENT_CLIENT_HOOK:
83                         vs = hook_shotorigin[s];
84                         break;
85                 case ENT_CLIENT_LGBEAM:
86                         vs = electro_shotorigin[s];
87                         break;
88                 case ENT_CLIENT_GAUNTLET:
89                         vs = gauntlet_shotorigin[s];
90                         break;
91         }
92
93         if((self.owner.sv_entnum == player_localentnum - 1))
94         {
95                 switch(self.HookType)
96                 {
97                         default:
98                         case ENT_CLIENT_HOOK:
99                                 a = view_origin + view_forward * vs_x + view_right * -vs_y + view_up * vs_z;
100                                 b = self.origin;
101                                 break;
102                         case ENT_CLIENT_LGBEAM:
103                         case ENT_CLIENT_GAUNTLET:
104                                 if(self.HookRange)
105                                         b = view_origin + view_forward * self.HookRange;
106                                 else
107                                         b = view_origin + view_forward * vlen(self.velocity - self.origin); // honor original length of beam!
108                                 WarpZone_TraceLine(view_origin, b, MOVE_NORMAL, world);
109                                 b = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos);
110                                 a = view_origin + view_forward * vs_x + view_right * -vs_y + view_up * vs_z;
111                                 break;
112                 }
113         }
114         else
115         {
116                 switch(self.HookType)
117                 {
118                         default:
119                         case ENT_CLIENT_HOOK:
120                                 a = self.velocity;
121                                 b = self.origin;
122                                 break;
123                         case ENT_CLIENT_LGBEAM:
124                         case ENT_CLIENT_GAUNTLET:
125                                 a = self.origin;
126                                 b = self.velocity;
127                                 break;
128                 }
129         }
130
131         t = GetPlayerColorForce(self.owner.sv_entnum);
132
133         switch(self.HookType)
134         {
135                 default:
136                 case ENT_CLIENT_HOOK:
137                         intensity = 1;
138                         offset = 0;
139                         if(t == COLOR_TEAM1)
140                         {
141                                 tex = "particles/hook_red";
142                                 rgb = '1 .3 .3';
143                         }
144                         else if(t == COLOR_TEAM2)
145                         {
146                                 tex = "particles/hook_blue";
147                                 rgb = '.3 .3 1';
148                         }
149                         else if(t == COLOR_TEAM3)
150                         {
151                                 tex = "particles/hook_yellow";
152                                 rgb = '1 1 .3';
153                         }
154                         else if(t == COLOR_TEAM4)
155                         {
156                                 tex = "particles/hook_pink";
157                                 rgb = '1 .3 1';
158                         }
159                         else
160                         {
161                                 tex = "particles/hook_green";
162                                 rgb = '.3 1 .3';
163                         }
164                         break;
165                 case ENT_CLIENT_LGBEAM:
166                         intensity = bound(0.2, 1 + Noise_Pink(self, frametime) * 1 + Noise_Burst(self, frametime, 0.03) * 0.3, 2);
167                         offset = Noise_Brown(self, frametime) * 10;
168                         tex = "particles/lgbeam";
169                         rgb = '1 1 1';
170                         break;
171                 case ENT_CLIENT_GAUNTLET:
172                         intensity = 1;
173                         offset = Noise_White(self, frametime);
174                         tex = "particles/gauntletbeam";
175                         rgb = '1 1 1';
176                         break;
177         }
178
179         Draw_GrapplingHook_trace_callback_tex = tex;
180         Draw_GrapplingHook_trace_callback_rnd = offset;
181         Draw_GrapplingHook_trace_callback_rgb = rgb;
182         Draw_GrapplingHook_trace_callback_a = intensity;
183         WarpZone_TraceBox_ThroughZone(a, '0 0 0', '0 0 0', b, ((self.HookType == ENT_CLIENT_HOOK) ? MOVE_NOTHING : MOVE_NORMAL), world, world, Draw_GrapplingHook_trace_callback);
184         Draw_GrapplingHook_trace_callback_tex = string_null;
185
186         atrans = WarpZone_TransformOrigin(WarpZone_trace_transform, a);
187
188         switch(self.HookType)
189         {
190                 default:
191                 case ENT_CLIENT_HOOK:
192                         setorigin(self, trace_endpos); // hook endpoint!
193                         self.angles = vectoangles(trace_endpos - atrans);
194                         break;
195                 case ENT_CLIENT_LGBEAM:
196                 case ENT_CLIENT_GAUNTLET:
197                         setorigin(self, a); // beam origin!
198                         break;
199         }
200
201         switch(self.HookType)
202         {
203                 default:
204                 case ENT_CLIENT_HOOK:
205                         break;
206                 case ENT_CLIENT_LGBEAM:
207                         pointparticles(particleeffectnum("electro_lightning"), trace_endpos, normalize(atrans - trace_endpos), frametime * intensity);
208                         break;
209                 case ENT_CLIENT_GAUNTLET:
210                         pointparticles(particleeffectnum("gauntlet_lightning"), b, normalize(a - b), frametime * intensity);
211                         break;
212         }
213 }
214
215 void Remove_GrapplingHook()
216 {
217         sound (self, CHAN_PROJECTILE, "misc/null.wav", VOL_BASE, ATTN_NORM);
218 }
219
220 void Ent_ReadHook(float bIsNew, float type)
221 {
222         self.HookType = type;
223
224         float sf;
225         sf = ReadByte();
226
227         self.HookSilent = (sf & 0x80);
228         self.iflags = IFLAG_VELOCITY;
229
230         InterpolateOrigin_Undo();
231
232         if(sf & 1)
233         {
234                 self.owner = playerslots[ReadByte() - 1];
235                 switch(self.HookType)
236                 {
237                         default:
238                         case ENT_CLIENT_HOOK:
239                         case ENT_CLIENT_GAUNTLET:
240                                 self.HookRange = 0;
241                                 break;
242                         case ENT_CLIENT_LGBEAM:
243                                 self.HookRange = ReadCoord();
244                                 break;
245                 }
246         }
247         if(sf & 2)
248         {
249                 self.origin_x = ReadCoord();
250                 self.origin_y = ReadCoord();
251                 self.origin_z = ReadCoord();
252                 setorigin(self, self.origin);
253         }
254         if(sf & 4)
255         {
256                 self.velocity_x = ReadCoord();
257                 self.velocity_y = ReadCoord();
258                 self.velocity_z = ReadCoord();
259         }
260
261         InterpolateOrigin_Note();
262
263         if(bIsNew)
264         {
265                 self.draw = Draw_GrapplingHook;
266                 self.entremove = Remove_GrapplingHook;
267
268                 switch(self.HookType)
269                 {
270                         default:
271                         case ENT_CLIENT_HOOK:
272                                 // for the model
273                                 setmodel(self, "models/hook.md3");
274                                 self.drawmask = MASK_NORMAL;
275                                 break;
276                         case ENT_CLIENT_LGBEAM:
277                                 sound (self, CHAN_PROJECTILE, "weapons/lgbeam_fly.wav", VOL_BASE, ATTN_NORM);
278                                 break;
279                         case ENT_CLIENT_GAUNTLET:
280                                 sound (self, CHAN_PROJECTILE, "weapons/gauntletbeam_fly.wav", VOL_BASE, ATTN_NORM);
281                                 break;
282                 }
283         }
284
285         self.teleport_time = time + 10;
286 }
287
288 void Hook_Precache()
289 {
290         precache_sound("weapons/lgbeam_fly.wav");
291         precache_sound("weapons/gauntletbeam_fly.wav");
292         precache_model("models/hook.md3");
293 }
294
295 // TODO: hook: temporarily transform self.origin for drawing the model along warpzones!