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