again, fields work better when declared
[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 .float teleport_time;
57 void Draw_GrapplingHook()
58 {
59         vector a, b, atrans;
60         string tex, snd;
61         vector rgb;
62         float t;
63         float s;
64         vector vs;
65         float intensity, offset;
66
67         if(self.teleport_time)
68         if(time > self.teleport_time)
69         {
70                 sound (self, CHAN_PROJECTILE, "misc/null.wav", VOL_BASE, ATTN_NORM); // safeguard
71                 self.teleport_time = 0;
72         }
73
74         InterpolateOrigin_Do();
75
76         s = cvar("cl_gunalign");
77         if(s != 1 && s != 2 && s != 4)
78                 s = 3; // default value
79         --s;
80         switch(self.HookType)
81         {
82                 default:
83                 case ENT_CLIENT_HOOK:
84                         vs = hook_shotorigin[s];
85                         break;
86                 case ENT_CLIENT_LGBEAM:
87                         vs = electro_shotorigin[s];
88                         break;
89                 case ENT_CLIENT_GAUNTLET:
90                         vs = gauntlet_shotorigin[s];
91                         break;
92         }
93
94         if((self.owner.sv_entnum == player_localentnum - 1))
95         {
96                 switch(self.HookType)
97                 {
98                         default:
99                         case ENT_CLIENT_HOOK:
100                                 a = view_origin + view_forward * vs_x + view_right * -vs_y + view_up * vs_z;
101                                 b = self.origin;
102                                 break;
103                         case ENT_CLIENT_LGBEAM:
104                         case ENT_CLIENT_GAUNTLET:
105                                 if(self.HookRange)
106                                         b = view_origin + view_forward * self.HookRange;
107                                 else
108                                         b = view_origin + view_forward * vlen(self.velocity - self.origin); // honor original length of beam!
109                                 WarpZone_TraceLine(view_origin, b, MOVE_NORMAL, world);
110                                 b = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos);
111                                 a = view_origin + view_forward * vs_x + view_right * -vs_y + view_up * vs_z;
112                                 break;
113                 }
114         }
115         else
116         {
117                 switch(self.HookType)
118                 {
119                         default:
120                         case ENT_CLIENT_HOOK:
121                                 a = self.velocity;
122                                 b = self.origin;
123                                 break;
124                         case ENT_CLIENT_LGBEAM:
125                         case ENT_CLIENT_GAUNTLET:
126                                 a = self.origin;
127                                 b = self.velocity;
128                                 break;
129                 }
130         }
131
132         t = GetPlayerColorForce(self.owner.sv_entnum);
133
134         switch(self.HookType)
135         {
136                 default:
137                 case ENT_CLIENT_HOOK:
138                         intensity = 1;
139                         offset = 0;
140                         if(t == COLOR_TEAM1)
141                         {
142                                 tex = "particles/hook_red";
143                                 rgb = '1 .3 .3';
144                         }
145                         else if(t == COLOR_TEAM2)
146                         {
147                                 tex = "particles/hook_blue";
148                                 rgb = '.3 .3 1';
149                         }
150                         else if(t == COLOR_TEAM3)
151                         {
152                                 tex = "particles/hook_yellow";
153                                 rgb = '1 1 .3';
154                         }
155                         else if(t == COLOR_TEAM4)
156                         {
157                                 tex = "particles/hook_pink";
158                                 rgb = '1 .3 1';
159                         }
160                         else
161                         {
162                                 tex = "particles/hook_green";
163                                 rgb = '.3 1 .3';
164                         }
165                         break;
166                 case ENT_CLIENT_LGBEAM:
167                         intensity = bound(0.2, 1 + Noise_Pink(self, frametime) * 1 + Noise_Burst(self, frametime, 0.03) * 0.3, 2);
168                         offset = Noise_Brown(self, frametime) * 10;
169                         tex = "particles/lgbeam";
170                         rgb = '1 1 1';
171                         break;
172                 case ENT_CLIENT_GAUNTLET:
173                         intensity = 1;
174                         offset = Noise_White(self, frametime);
175                         tex = "particles/gauntletbeam";
176                         rgb = '1 1 1';
177                         break;
178         }
179
180         Draw_GrapplingHook_trace_callback_tex = tex;
181         Draw_GrapplingHook_trace_callback_rnd = offset;
182         Draw_GrapplingHook_trace_callback_rgb = rgb;
183         Draw_GrapplingHook_trace_callback_a = intensity;
184         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);
185         Draw_GrapplingHook_trace_callback_tex = string_null;
186
187         atrans = WarpZone_TransformOrigin(WarpZone_trace_transform, a);
188
189         switch(self.HookType)
190         {
191                 default:
192                 case ENT_CLIENT_HOOK:
193                         setorigin(self, trace_endpos); // hook endpoint!
194                         self.angles = vectoangles(trace_endpos - atrans);
195                         break;
196                 case ENT_CLIENT_LGBEAM:
197                 case ENT_CLIENT_GAUNTLET:
198                         setorigin(self, a); // beam origin!
199                         break;
200         }
201
202         switch(self.HookType)
203         {
204                 default:
205                 case ENT_CLIENT_HOOK:
206                         break;
207                 case ENT_CLIENT_LGBEAM:
208                         pointparticles(particleeffectnum("electro_lightning"), trace_endpos, normalize(atrans - trace_endpos), frametime * intensity);
209                         break;
210                 case ENT_CLIENT_GAUNTLET:
211                         pointparticles(particleeffectnum("gauntlet_lightning"), b, normalize(a - b), frametime * intensity);
212                         break;
213         }
214 }
215
216 void Remove_GrapplingHook()
217 {
218         sound (self, CHAN_PROJECTILE, "misc/null.wav", VOL_BASE, ATTN_NORM);
219 }
220
221 void Ent_ReadHook(float bIsNew, float type)
222 {
223         self.HookType = type;
224
225         float sf;
226         sf = ReadByte();
227
228         self.HookSilent = (sf & 0x80);
229         self.iflags = IFLAG_VELOCITY;
230
231         InterpolateOrigin_Undo();
232
233         if(sf & 1)
234         {
235                 self.owner = playerslots[ReadByte() - 1];
236                 switch(self.HookType)
237                 {
238                         default:
239                         case ENT_CLIENT_HOOK:
240                         case ENT_CLIENT_GAUNTLET:
241                                 self.HookRange = 0;
242                                 break;
243                         case ENT_CLIENT_LGBEAM:
244                                 self.HookRange = ReadCoord();
245                                 break;
246                 }
247         }
248         if(sf & 2)
249         {
250                 self.origin_x = ReadCoord();
251                 self.origin_y = ReadCoord();
252                 self.origin_z = ReadCoord();
253                 setorigin(self, self.origin);
254         }
255         if(sf & 4)
256         {
257                 self.velocity_x = ReadCoord();
258                 self.velocity_y = ReadCoord();
259                 self.velocity_z = ReadCoord();
260         }
261
262         InterpolateOrigin_Note();
263
264         if(bIsNew)
265         {
266                 self.draw = Draw_GrapplingHook;
267                 self.entremove = Remove_GrapplingHook;
268
269                 switch(self.HookType)
270                 {
271                         default:
272                         case ENT_CLIENT_HOOK:
273                                 // for the model
274                                 setmodel(self, "models/hook.md3");
275                                 self.drawmask = MASK_NORMAL;
276                                 break;
277                         case ENT_CLIENT_LGBEAM:
278                                 sound (self, CHAN_PROJECTILE, "weapons/lgbeam_fly.wav", VOL_BASE, ATTN_NORM);
279                                 break;
280                         case ENT_CLIENT_GAUNTLET:
281                                 sound (self, CHAN_PROJECTILE, "weapons/gauntletbeam_fly.wav", VOL_BASE, ATTN_NORM);
282                                 break;
283                 }
284         }
285
286         self.teleport_time = time + 10;
287 }
288
289 void Hook_Precache()
290 {
291         precache_sound("weapons/lgbeam_fly.wav");
292         precache_sound("weapons/gauntletbeam_fly.wav");
293         precache_model("models/hook.md3");
294 }
295
296 // TODO: hook: temporarily transform self.origin for drawing the model along warpzones!