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