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