Merge branch 'master' into Mario/weapons
[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 theAlpha, 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         A = from - thickdir * (thickness / 2);
22         B = from + thickdir * (thickness / 2);
23         C = to + thickdir * (thickness / 2);
24         D = to - thickdir * (thickness / 2);
25
26         R_BeginPolygon(texture, drawflag);
27         R_PolygonVertex(A, '0 0 0' + shift * '1 0 0', rgb, theAlpha);
28         R_PolygonVertex(B, '0 1 0' + shift * '1 0 0', rgb, theAlpha);
29         R_PolygonVertex(C, '0 1 0' + (shift + length_tex) * '1 0 0', rgb, theAlpha);
30         R_PolygonVertex(D, '0 0 0' + (shift + length_tex) * '1 0 0', rgb, theAlpha);
31         R_EndPolygon();
32 }
33
34 string Draw_GrapplingHook_trace_callback_tex;
35 float Draw_GrapplingHook_trace_callback_rnd;
36 vector Draw_GrapplingHook_trace_callback_rgb;
37 float Draw_GrapplingHook_trace_callback_a;
38 void Draw_GrapplingHook_trace_callback(vector start, vector hit, vector end)
39 {
40         float i;
41         vector vorg;
42         vorg = WarpZone_TransformOrigin(WarpZone_trace_transform, view_origin);
43         for(i = 0; i < Draw_GrapplingHook_trace_callback_a; ++i)
44                 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);
45         Draw_GrapplingHook_trace_callback_rnd += 0.25 * vlen(hit - start) / 8;
46 }
47
48 .float teleport_time;
49 void Draw_GrapplingHook()
50 {
51         vector a, b, atrans;
52         string tex;
53         vector rgb;
54         float t;
55         float s;
56         vector vs;
57         float intensity, offset;
58
59         if(self.teleport_time)
60         if(time > self.teleport_time)
61         {
62                 sound (self, CH_SHOTS_SINGLE, "misc/null.wav", VOL_BASE, ATTEN_NORM); // safeguard
63                 self.teleport_time = 0;
64         }
65
66         InterpolateOrigin_Do();
67
68         s = autocvar_cl_gunalign;
69         if(s != 1 && s != 2 && s != 4)
70                 s = 3; // default value
71         --s;
72         switch(self.HookType)
73         {
74                 default:
75                 case ENT_CLIENT_HOOK:
76                         vs = hook_shotorigin[s];
77                         break;
78                 case ENT_CLIENT_ARC_BEAM:
79                         vs = lightning_shotorigin[s];
80                         break;
81         }
82
83         if((self.owner.sv_entnum == player_localentnum - 1) && autocvar_chase_active <= 0)
84         {
85                 switch(self.HookType)
86                 {
87                         default:
88                         case ENT_CLIENT_HOOK:
89                                 a = view_origin + view_forward * vs_x + view_right * -vs_y + view_up * vs_z;
90                                 b = self.origin;
91                                 break;
92                         case ENT_CLIENT_ARC_BEAM:
93                                 if(self.HookRange)
94                                         b = view_origin + view_forward * self.HookRange;
95                                 else
96                                         b = view_origin + view_forward * vlen(self.velocity - self.origin); // honor original length of beam!
97                                 WarpZone_TraceLine(view_origin, b, MOVE_NORMAL, world);
98                                 b = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos);
99                                 a = view_origin + view_forward * vs_x + view_right * -vs_y + view_up * vs_z;
100                                 break;
101                 }
102         }
103         else
104         {
105                 switch(self.HookType)
106                 {
107                         default:
108                         case ENT_CLIENT_HOOK:
109                                 a = self.velocity;
110                                 b = self.origin;
111                                 break;
112                         case ENT_CLIENT_ARC_BEAM:
113                                 a = self.origin;
114                                 b = self.velocity;
115                                 break;
116                 }
117         }
118
119         t = GetPlayerColorForce(self.owner.sv_entnum);
120
121         switch(self.HookType)
122         {
123                 default:
124                 case ENT_CLIENT_HOOK:
125                         intensity = 1;
126                         offset = 0;
127                         switch(t)
128                         {
129                                 case NUM_TEAM_1: tex = "particles/hook_red"; rgb = '1 0.3 0.3'; break;
130                                 case NUM_TEAM_2: tex = "particles/hook_blue"; rgb = '0.3 0.3 1'; break;
131                                 case NUM_TEAM_3: tex = "particles/hook_yellow"; rgb = '1 1 0.3'; break;
132                                 case NUM_TEAM_4: tex = "particles/hook_pink"; rgb = '1 0.3 1'; break;
133                                 default: tex = "particles/hook_white"; rgb = getcsqcplayercolor(self.sv_entnum); break;
134                         }
135                         break;
136                 case ENT_CLIENT_ARC_BEAM: // todo
137                         intensity = bound(0.2, 1 + Noise_Pink(self, frametime) * 1 + Noise_Burst(self, frametime, 0.03) * 0.3, 2);
138                         offset = Noise_Brown(self, frametime) * 10;
139                         tex = "particles/lgbeam";
140                         rgb = '1 1 1';
141                         break;
142         }
143
144         Draw_GrapplingHook_trace_callback_tex = tex;
145         Draw_GrapplingHook_trace_callback_rnd = offset;
146         Draw_GrapplingHook_trace_callback_rgb = rgb;
147         Draw_GrapplingHook_trace_callback_a = intensity;
148         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);
149         Draw_GrapplingHook_trace_callback_tex = string_null;
150
151         atrans = WarpZone_TransformOrigin(WarpZone_trace_transform, a);
152
153         switch(self.HookType)
154         {
155                 default:
156                 case ENT_CLIENT_HOOK:
157                         if(vlen(trace_endpos - atrans) > 0.5)
158                         {
159                                 setorigin(self, trace_endpos); // hook endpoint!
160                                 self.angles = vectoangles(trace_endpos - atrans);
161                                 self.drawmask = MASK_NORMAL;
162                         }
163                         else
164                         {
165                                 self.drawmask = 0;
166                         }
167                         break;
168                 case ENT_CLIENT_ARC_BEAM:
169                         setorigin(self, a); // beam origin!
170                         break;
171         }
172
173         switch(self.HookType)
174         {
175                 default:
176                 case ENT_CLIENT_HOOK:
177                         break;
178                 case ENT_CLIENT_ARC_BEAM:
179                         pointparticles(particleeffectnum("electro_lightning"), trace_endpos, normalize(atrans - trace_endpos), frametime * intensity); // todo: new effect
180                         break;
181         }
182 }
183
184 void Remove_GrapplingHook()
185 {
186         sound (self, CH_SHOTS_SINGLE, "misc/null.wav", VOL_BASE, ATTEN_NORM);
187 }
188
189 void Ent_ReadHook(float bIsNew, float type)
190 {
191         self.HookType = type;
192
193         float sf;
194         sf = ReadByte();
195
196         self.HookSilent = (sf & 0x80);
197         self.iflags = IFLAG_VELOCITY | IFLAG_ORIGIN;
198
199         InterpolateOrigin_Undo();
200
201         if(sf & 1)
202         {
203                 float myowner = ReadByte();
204                 self.owner = playerslots[myowner - 1];
205                 self.sv_entnum = myowner;
206                 switch(self.HookType)
207                 {
208                         default:
209                         case ENT_CLIENT_HOOK:
210                                 self.HookRange = 0;
211                                 break;
212                         case ENT_CLIENT_ARC_BEAM:
213                                 self.HookRange = ReadCoord();
214                                 break;
215                 }
216         }
217         if(sf & 2)
218         {
219                 self.origin_x = ReadCoord();
220                 self.origin_y = ReadCoord();
221                 self.origin_z = ReadCoord();
222                 setorigin(self, self.origin);
223         }
224         if(sf & 4)
225         {
226                 self.velocity_x = ReadCoord();
227                 self.velocity_y = ReadCoord();
228                 self.velocity_z = ReadCoord();
229         }
230
231         InterpolateOrigin_Note();
232
233         if(bIsNew || !self.teleport_time)
234         {
235                 self.draw = Draw_GrapplingHook;
236                 self.entremove = Remove_GrapplingHook;
237
238                 switch(self.HookType)
239                 {
240                         default:
241                         case ENT_CLIENT_HOOK:
242                                 // for the model
243                                 setmodel(self, "models/hook.md3");
244                                 self.drawmask = MASK_NORMAL;
245                                 break;
246                         case ENT_CLIENT_ARC_BEAM:
247                                 sound (self, CH_SHOTS_SINGLE, "weapons/lgbeam_fly.wav", VOL_BASE, ATTEN_NORM);
248                                 break;
249                 }
250         }
251
252         self.teleport_time = time + 10;
253 }
254
255 void Hook_Precache()
256 {
257         precache_sound("weapons/lgbeam_fly.wav");
258         precache_model("models/hook.md3");
259 }
260
261 // TODO: hook: temporarily transform self.origin for drawing the model along warpzones!