cee2a0cd83a1d6e64c8a9eb2b68807bc1bbf3c88
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hook.qc
1 #include "hook.qh"
2
3 #include "../lib/csqcmodel/interpolate.qh"
4 #include "../lib/warpzone/common.qh"
5
6 entityclass(Hook);
7 class(Hook) .entity HookType; // ENT_CLIENT_*
8 class(Hook) .vector origin;
9 class(Hook) .vector velocity;
10 class(Hook) .float HookSilent;
11 class(Hook) .float HookRange;
12
13 string Draw_GrapplingHook_trace_callback_tex;
14 float Draw_GrapplingHook_trace_callback_rnd;
15 vector Draw_GrapplingHook_trace_callback_rgb;
16 float Draw_GrapplingHook_trace_callback_a;
17 void Draw_GrapplingHook_trace_callback(vector start, vector hit, vector end)
18 {
19         float i;
20         vector vorg;
21         vorg = WarpZone_TransformOrigin(WarpZone_trace_transform, view_origin);
22         for(i = 0; i < Draw_GrapplingHook_trace_callback_a; ++i)
23                 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);
24         Draw_GrapplingHook_trace_callback_rnd += 0.25 * vlen(hit - start) / 8;
25 }
26
27 class(Hook) .float teleport_time;
28 void Draw_GrapplingHook(entity this)
29 {
30         vector a, b, atrans;
31         string tex;
32         vector rgb;
33         float t;
34         vector vs;
35         float intensity, offset;
36
37         if(self.teleport_time)
38         if(time > self.teleport_time)
39         {
40                 sound (self, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, ATTEN_NORM); // safeguard
41                 self.teleport_time = 0;
42         }
43
44         InterpolateOrigin_Do();
45
46         int s = W_GetGunAlignment(world);
47
48         switch(self.HookType)
49         {
50                 default:
51                 case NET_ENT_CLIENT_HOOK:
52                         vs = hook_shotorigin[s];
53                         break;
54                 case NET_ENT_CLIENT_ARC_BEAM:
55                         vs = lightning_shotorigin[s];
56                         break;
57         }
58
59         if((self.owner.sv_entnum == player_localentnum - 1) && autocvar_chase_active <= 0)
60         {
61                 switch(self.HookType)
62                 {
63                         default:
64                         case NET_ENT_CLIENT_HOOK:
65                                 a = view_origin + view_forward * vs.x + view_right * -vs.y + view_up * vs.z;
66                                 b = self.origin;
67                                 break;
68                         case NET_ENT_CLIENT_ARC_BEAM:
69                                 if(self.HookRange)
70                                         b = view_origin + view_forward * self.HookRange;
71                                 else
72                                         b = view_origin + view_forward * vlen(self.velocity - self.origin); // honor original length of beam!
73                                 WarpZone_TraceLine(view_origin, b, MOVE_NORMAL, world);
74                                 b = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos);
75                                 a = view_origin + view_forward * vs.x + view_right * -vs.y + view_up * vs.z;
76                                 break;
77                 }
78         }
79         else
80         {
81                 switch(self.HookType)
82                 {
83                         default:
84                         case NET_ENT_CLIENT_HOOK:
85                                 a = self.velocity;
86                                 b = self.origin;
87                                 break;
88                         case NET_ENT_CLIENT_ARC_BEAM:
89                                 a = self.origin;
90                                 b = self.velocity;
91                                 break;
92                 }
93         }
94
95         t = GetPlayerColorForce(self.owner.sv_entnum);
96
97         switch(self.HookType)
98         {
99                 default:
100                 case NET_ENT_CLIENT_HOOK:
101                         intensity = 1;
102                         offset = 0;
103                         switch(t)
104                         {
105                                 case NUM_TEAM_1: tex = "particles/hook_red"; rgb = '1 0.3 0.3'; break;
106                                 case NUM_TEAM_2: tex = "particles/hook_blue"; rgb = '0.3 0.3 1'; break;
107                                 case NUM_TEAM_3: tex = "particles/hook_yellow"; rgb = '1 1 0.3'; break;
108                                 case NUM_TEAM_4: tex = "particles/hook_pink"; rgb = '1 0.3 1'; break;
109                                 default: tex = "particles/hook_white"; rgb = getcsqcplayercolor(self.sv_entnum); break;
110                         }
111                         break;
112                 case NET_ENT_CLIENT_ARC_BEAM: // todo
113                         intensity = bound(0.2, 1 + Noise_Pink(self, frametime) * 1 + Noise_Burst(self, frametime, 0.03) * 0.3, 2);
114                         offset = Noise_Brown(self, frametime) * 10;
115                         tex = "particles/lgbeam";
116                         rgb = '1 1 1';
117                         break;
118         }
119
120         Draw_GrapplingHook_trace_callback_tex = tex;
121         Draw_GrapplingHook_trace_callback_rnd = offset;
122         Draw_GrapplingHook_trace_callback_rgb = rgb;
123         Draw_GrapplingHook_trace_callback_a = intensity;
124         WarpZone_TraceBox_ThroughZone(a, '0 0 0', '0 0 0', b, ((self.HookType == NET_ENT_CLIENT_HOOK) ? MOVE_NOTHING : MOVE_NORMAL), world, world, Draw_GrapplingHook_trace_callback);
125         Draw_GrapplingHook_trace_callback_tex = string_null;
126
127         atrans = WarpZone_TransformOrigin(WarpZone_trace_transform, a);
128
129         switch(self.HookType)
130         {
131                 default:
132                 case NET_ENT_CLIENT_HOOK:
133                         if(vlen(trace_endpos - atrans) > 0.5)
134                         {
135                                 setorigin(self, trace_endpos); // hook endpoint!
136                                 self.angles = vectoangles(trace_endpos - atrans);
137                                 self.drawmask = MASK_NORMAL;
138                         }
139                         else
140                         {
141                                 self.drawmask = 0;
142                         }
143                         break;
144                 case NET_ENT_CLIENT_ARC_BEAM:
145                         setorigin(self, a); // beam origin!
146                         break;
147         }
148
149         switch(self.HookType)
150         {
151                 default:
152                 case NET_ENT_CLIENT_HOOK:
153                         break;
154                 case NET_ENT_CLIENT_ARC_BEAM:
155                         pointparticles(EFFECT_ARC_LIGHTNING2, trace_endpos, normalize(atrans - trace_endpos), frametime * intensity); // todo: new effect
156                         break;
157         }
158 }
159
160 void Remove_GrapplingHook()
161 {SELFPARAM();
162         sound (self, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, ATTEN_NORM);
163 }
164
165 NET_HANDLE(ENT_CLIENT_HOOK, bool bIsNew)
166 {
167         self.HookType = NET_ENT_CLIENT_HOOK;
168
169         int sf = ReadByte();
170
171         self.HookSilent = (sf & 0x80);
172         self.iflags = IFLAG_VELOCITY | IFLAG_ORIGIN;
173
174         InterpolateOrigin_Undo();
175
176         if(sf & 1)
177         {
178                 int myowner = ReadByte();
179                 self.owner = playerslots[myowner - 1];
180                 self.sv_entnum = myowner;
181                 switch(self.HookType)
182                 {
183                         default:
184                         case NET_ENT_CLIENT_HOOK:
185                                 self.HookRange = 0;
186                                 break;
187                         case NET_ENT_CLIENT_ARC_BEAM:
188                                 self.HookRange = ReadCoord();
189                                 break;
190                 }
191         }
192         if(sf & 2)
193         {
194                 self.origin_x = ReadCoord();
195                 self.origin_y = ReadCoord();
196                 self.origin_z = ReadCoord();
197                 setorigin(self, self.origin);
198         }
199         if(sf & 4)
200         {
201                 self.velocity_x = ReadCoord();
202                 self.velocity_y = ReadCoord();
203                 self.velocity_z = ReadCoord();
204         }
205
206         InterpolateOrigin_Note();
207
208         if(bIsNew || !self.teleport_time)
209         {
210                 self.draw = Draw_GrapplingHook;
211                 self.entremove = Remove_GrapplingHook;
212
213                 switch(self.HookType)
214                 {
215                         default:
216                         case NET_ENT_CLIENT_HOOK:
217                                 // for the model
218                                 setmodel(self, MDL_HOOK);
219                                 self.drawmask = MASK_NORMAL;
220                                 break;
221                         case NET_ENT_CLIENT_ARC_BEAM:
222                                 sound (self, CH_SHOTS_SINGLE, SND_LGBEAM_FLY, VOL_BASE, ATTEN_NORM);
223                                 break;
224                 }
225         }
226
227         self.teleport_time = time + 10;
228         return true;
229 }
230
231 // TODO: hook: temporarily transform self.origin for drawing the model along warpzones!