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