Merge branch 'master' into terencehill/crosshair_fix
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hook.qc
1 .float HookType; // ENT_CLIENT_*
2 .vector origin;
3 .vector velocity;
4 .float HookSilent;
5 .float HookRange;
6
7 void Draw_CylindricLine(vector from, vector to, float thickness, string texture, float aspect, float shift, vector rgb, float alpha, float drawflag, vector vieworg)
8 {
9         // I want to draw a quad...
10         // from and to are MIDPOINTS.
11         
12         vector axis, thickdir, A, B, C, D;
13         float length_tex;
14
15         axis = normalize(to - from);
16         length_tex = aspect * vlen(to - from) / thickness;
17
18         // direction is perpendicular to the view normal, and perpendicular to the axis
19         thickdir = normalize(cross(axis, vieworg - from));
20
21 /*
22         print("from ", vtos(from), "\n");
23         print("to ", vtos(to), "\n");
24         print("org ", vtos(view_origin), "\n");
25         print("dir ", vtos(thickdir), "\n");
26 */
27
28         A = from - thickdir * (thickness / 2);
29         B = from + thickdir * (thickness / 2);
30         C = to + thickdir * (thickness / 2);
31         D = to - thickdir * (thickness / 2);
32
33         R_BeginPolygon(texture, drawflag);
34         R_PolygonVertex(A, '0 0 0' + shift * '1 0 0', rgb, alpha);
35         R_PolygonVertex(B, '0 1 0' + shift * '1 0 0', rgb, alpha);
36         R_PolygonVertex(C, '0 1 0' + (shift + length_tex) * '1 0 0', rgb, alpha);
37         R_PolygonVertex(D, '0 0 0' + (shift + length_tex) * '1 0 0', rgb, alpha);
38         R_EndPolygon();
39 }
40
41 string Draw_GrapplingHook_trace_callback_tex;
42 float Draw_GrapplingHook_trace_callback_rnd;
43 vector Draw_GrapplingHook_trace_callback_rgb;
44 float Draw_GrapplingHook_trace_callback_a;
45 void Draw_GrapplingHook_trace_callback(vector start, vector hit, vector end)
46 {
47         float i;
48         vector vorg;
49         vorg = WarpZone_TransformOrigin(WarpZone_trace_transform, view_origin);
50         for(i = 0; i < Draw_GrapplingHook_trace_callback_a; ++i)
51                 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);
52         Draw_GrapplingHook_trace_callback_rnd += 0.25 * vlen(hit - start) / 8;
53 }
54
55 .float teleport_time;
56 void Draw_GrapplingHook()
57 {
58         vector a, b, atrans;
59         string tex;
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                         if(vlen(trace_endpos - atrans) > 0.5)
193                         {
194                                 setorigin(self, trace_endpos); // hook endpoint!
195                                 self.angles = vectoangles(trace_endpos - atrans);
196                                 self.drawmask = MASK_NORMAL;
197                         }
198                         else
199                         {
200                                 self.drawmask = 0;
201                         }
202                         break;
203                 case ENT_CLIENT_LGBEAM:
204                 case ENT_CLIENT_GAUNTLET:
205                         setorigin(self, a); // beam origin!
206                         break;
207         }
208
209         switch(self.HookType)
210         {
211                 default:
212                 case ENT_CLIENT_HOOK:
213                         break;
214                 case ENT_CLIENT_LGBEAM:
215                         pointparticles(particleeffectnum("electro_lightning"), trace_endpos, normalize(atrans - trace_endpos), frametime * intensity);
216                         break;
217                 case ENT_CLIENT_GAUNTLET:
218                         pointparticles(particleeffectnum("gauntlet_lightning"), b, normalize(a - b), frametime * intensity);
219                         break;
220         }
221 }
222
223 void Remove_GrapplingHook()
224 {
225         sound (self, CHAN_PROJECTILE, "misc/null.wav", VOL_BASE, ATTN_NORM);
226 }
227
228 void Ent_ReadHook(float bIsNew, float type)
229 {
230         self.HookType = type;
231
232         float sf;
233         sf = ReadByte();
234
235         self.HookSilent = (sf & 0x80);
236         self.iflags = IFLAG_VELOCITY;
237
238         InterpolateOrigin_Undo();
239
240         if(sf & 1)
241         {
242                 self.owner = playerslots[ReadByte() - 1];
243                 switch(self.HookType)
244                 {
245                         default:
246                         case ENT_CLIENT_HOOK:
247                         case ENT_CLIENT_GAUNTLET:
248                                 self.HookRange = 0;
249                                 break;
250                         case ENT_CLIENT_LGBEAM:
251                                 self.HookRange = ReadCoord();
252                                 break;
253                 }
254         }
255         if(sf & 2)
256         {
257                 self.origin_x = ReadCoord();
258                 self.origin_y = ReadCoord();
259                 self.origin_z = ReadCoord();
260                 setorigin(self, self.origin);
261         }
262         if(sf & 4)
263         {
264                 self.velocity_x = ReadCoord();
265                 self.velocity_y = ReadCoord();
266                 self.velocity_z = ReadCoord();
267         }
268
269         InterpolateOrigin_Note();
270
271         if(bIsNew || !self.teleport_time)
272         {
273                 self.draw = Draw_GrapplingHook;
274                 self.entremove = Remove_GrapplingHook;
275
276                 switch(self.HookType)
277                 {
278                         default:
279                         case ENT_CLIENT_HOOK:
280                                 // for the model
281                                 setmodel(self, "models/hook.md3");
282                                 self.drawmask = MASK_NORMAL;
283                                 break;
284                         case ENT_CLIENT_LGBEAM:
285                                 sound (self, CHAN_PROJECTILE, "weapons/lgbeam_fly.wav", VOL_BASE, ATTN_NORM);
286                                 break;
287                         case ENT_CLIENT_GAUNTLET:
288                                 sound (self, CHAN_PROJECTILE, "weapons/gauntletbeam_fly.wav", VOL_BASE, ATTN_NORM);
289                                 break;
290                 }
291         }
292
293         self.teleport_time = time + 10;
294 }
295
296 void Hook_Precache()
297 {
298         precache_sound("weapons/lgbeam_fly.wav");
299         precache_sound("weapons/gauntletbeam_fly.wav");
300         precache_model("models/hook.md3");
301 }
302
303 // TODO: hook: temporarily transform self.origin for drawing the model along warpzones!