]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/weapon/hook.qc
Merge branch 'master' into Lyberta/RandomItems
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / hook.qc
1 #include "hook.qh"
2
3 #ifdef SVQC
4
5 void W_Hook_ExplodeThink(entity this)
6 {
7         float dt, dmg_remaining_next, f;
8
9         dt = time - this.teleport_time;
10         dmg_remaining_next = (bound(0, 1 - dt / this.dmg_duration, 1) ** this.dmg_power);
11
12         f = this.dmg_last - dmg_remaining_next;
13         this.dmg_last = dmg_remaining_next;
14
15         RadiusDamage(this, this.realowner, this.dmg * f, this.dmg_edge * f, this.dmg_radius, this.realowner, NULL, this.dmg_force * f, this.projectiledeathtype, NULL);
16         this.projectiledeathtype |= HITTYPE_BOUNCE;
17         //RadiusDamage(this, NULL, this.dmg * f, this.dmg_edge * f, this.dmg_radius, NULL, NULL, this.dmg_force * f, this.projectiledeathtype, NULL);
18
19         if(dt < this.dmg_duration)
20                 this.nextthink = time + 0.05; // soon
21         else
22                 delete(this);
23 }
24
25 void W_Hook_Explode2(entity this)
26 {
27         this.event_damage = func_null;
28         settouch(this, func_null);
29         this.effects |= EF_NODRAW;
30
31         setthink(this, W_Hook_ExplodeThink);
32         this.nextthink = time;
33         this.dmg = WEP_CVAR_SEC(hook, damage);
34         this.dmg_edge = WEP_CVAR_SEC(hook, edgedamage);
35         this.dmg_radius = WEP_CVAR_SEC(hook, radius);
36         this.dmg_force = WEP_CVAR_SEC(hook, force);
37         this.dmg_power = WEP_CVAR_SEC(hook, power);
38         this.dmg_duration = WEP_CVAR_SEC(hook, duration);
39         this.teleport_time = time;
40         this.dmg_last = 1;
41         set_movetype(this, MOVETYPE_NONE);
42 }
43
44 void W_Hook_Explode2_use(entity this, entity actor, entity trigger)
45 {
46         W_Hook_Explode2(this);
47 }
48
49 void W_Hook_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
50 {
51         if(this.health <= 0)
52                 return;
53
54         if(!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
55                 return; // g_projectiles_damage says to halt
56
57         this.health = this.health - damage;
58
59         if(this.health <= 0)
60                 W_PrepareExplosionByDamage(this, this.realowner, W_Hook_Explode2);
61 }
62
63 void W_Hook_Touch2(entity this, entity toucher)
64 {
65         PROJECTILE_TOUCH(this, toucher);
66         this.use(this, NULL, NULL);
67 }
68
69 void W_Hook_Attack2(Weapon thiswep, entity actor, .entity weaponentity)
70 {
71         //W_DecreaseAmmo(thiswep, actor, WEP_CVAR_SEC(hook, ammo)); // WEAPONTODO: Figure out how to handle ammo with hook secondary (gravitybomb)
72         W_SetupShot(actor, weaponentity, false, 4, SND_HOOKBOMB_FIRE, CH_WEAPON_A, WEP_CVAR_SEC(hook, damage));
73
74         entity gren = new(hookbomb);
75         gren.owner = gren.realowner = actor;
76         gren.bot_dodge = true;
77         gren.bot_dodgerating = WEP_CVAR_SEC(hook, damage);
78         set_movetype(gren, MOVETYPE_TOSS);
79         PROJECTILE_MAKETRIGGER(gren);
80         gren.projectiledeathtype = WEP_HOOK.m_id | HITTYPE_SECONDARY;
81         setorigin(gren, w_shotorg);
82         setsize(gren, '0 0 0', '0 0 0');
83
84         gren.nextthink = time + WEP_CVAR_SEC(hook, lifetime);
85         setthink(gren, adaptor_think2use_hittype_splash);
86         gren.use = W_Hook_Explode2_use;
87         settouch(gren, W_Hook_Touch2);
88
89         gren.takedamage = DAMAGE_YES;
90         gren.health = WEP_CVAR_SEC(hook, health);
91         gren.damageforcescale = WEP_CVAR_SEC(hook, damageforcescale);
92         gren.event_damage = W_Hook_Damage;
93         gren.damagedbycontents = true;
94         IL_PUSH(g_damagedbycontents, gren);
95         gren.missile_flags = MIF_SPLASH | MIF_ARC;
96
97         gren.velocity = '0 0 1' * WEP_CVAR_SEC(hook, speed);
98         if (autocvar_g_projectiles_newton_style)
99                 gren.velocity = gren.velocity + actor.velocity;
100
101         gren.gravity = WEP_CVAR_SEC(hook, gravity);
102         //W_SetupProjVelocity_Basic(gren); // just falling down!
103
104         gren.angles = '0 0 0';
105         gren.flags = FL_PROJECTILE;
106         IL_PUSH(g_projectiles, gren);
107         IL_PUSH(g_bot_dodge, gren);
108
109         CSQCProjectile(gren, true, PROJECTILE_HOOKBOMB, true);
110
111         MUTATOR_CALLHOOK(EditProjectile, actor, gren);
112 }
113
114 METHOD(Hook, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
115 {
116     if (fire & 1) {
117         if(!actor.(weaponentity).hook)
118         if(!(actor.(weaponentity).hook_state & HOOK_WAITING_FOR_RELEASE))
119         if(time > actor.(weaponentity).hook_refire)
120         if(weapon_prepareattack(thiswep, actor, weaponentity, false, -1))
121         {
122             W_DecreaseAmmo(thiswep, actor, thiswep.ammo_factor * WEP_CVAR_PRI(hook, ammo), weaponentity);
123             actor.(weaponentity).hook_state |= HOOK_FIRING;
124             actor.(weaponentity).hook_state |= HOOK_WAITING_FOR_RELEASE;
125             weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(hook, animtime), w_ready);
126         }
127     } else {
128         actor.(weaponentity).hook_state |= HOOK_REMOVING;
129         actor.(weaponentity).hook_state &= ~HOOK_WAITING_FOR_RELEASE;
130     }
131
132     if(fire & 2)
133     {
134         if(weapon_prepareattack(thiswep, actor, weaponentity, true, WEP_CVAR_SEC(hook, refire)))
135         {
136             W_Hook_Attack2(thiswep, actor, weaponentity);
137             weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(hook, animtime), w_ready);
138         }
139     }
140
141     if(actor.(weaponentity).hook)
142     {
143         // if hooked, no bombs, and increase the timer
144         actor.(weaponentity).hook_refire = max(actor.(weaponentity).hook_refire, time + WEP_CVAR_PRI(hook, refire) * W_WeaponRateFactor(actor));
145
146         // hook also inhibits health regeneration, but only for 1 second
147         if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
148             actor.pauseregen_finished = max(actor.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
149     }
150
151     if(actor.(weaponentity).hook && actor.(weaponentity).hook.state == 1)
152     {
153         float hooked_time_max = WEP_CVAR_PRI(hook, hooked_time_max);
154         if(hooked_time_max > 0)
155         {
156             if( time > actor.(weaponentity).hook_time_hooked + hooked_time_max )
157                 actor.(weaponentity).hook_state |= HOOK_REMOVING;
158         }
159
160         float hooked_fuel = thiswep.ammo_factor * WEP_CVAR_PRI(hook, hooked_ammo);
161         if(hooked_fuel > 0)
162         {
163             if( time > actor.(weaponentity).hook_time_fueldecrease )
164             {
165                 if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
166                 {
167                     if( actor.ammo_fuel >= (time - actor.(weaponentity).hook_time_fueldecrease) * hooked_fuel )
168                     {
169                         W_DecreaseAmmo(thiswep, actor, (time - actor.(weaponentity).hook_time_fueldecrease) * hooked_fuel, weaponentity);
170                         actor.(weaponentity).hook_time_fueldecrease = time;
171                         // decrease next frame again
172                     }
173                     else
174                     {
175                         actor.ammo_fuel = 0;
176                         actor.(weaponentity).hook_state |= HOOK_REMOVING;
177                         if(actor.(weaponentity).m_weapon != WEP_Null) // offhand
178                                 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
179                     }
180                 }
181             }
182         }
183     }
184     else
185     {
186         actor.(weaponentity).hook_time_hooked = time;
187         actor.(weaponentity).hook_time_fueldecrease = time + WEP_CVAR_PRI(hook, hooked_time_free);
188     }
189
190     actor.(weaponentity).hook_state = BITSET(actor.(weaponentity).hook_state, HOOK_PULLING, (!PHYS_INPUT_BUTTON_CROUCH(actor) || !autocvar_g_balance_grapplehook_crouchslide));
191
192     if (actor.(weaponentity).hook_state & HOOK_FIRING)
193     {
194         if (actor.(weaponentity).hook)
195             RemoveHook(actor.(weaponentity).hook);
196         FireGrapplingHook(actor, weaponentity);
197         actor.(weaponentity).hook_state &= ~HOOK_FIRING;
198         actor.(weaponentity).hook_refire = max(actor.(weaponentity).hook_refire, time + autocvar_g_balance_grapplehook_refire * W_WeaponRateFactor(actor));
199     }
200     else if (actor.(weaponentity).hook_state & HOOK_REMOVING)
201     {
202         if (actor.(weaponentity).hook)
203             RemoveHook(actor.(weaponentity).hook);
204         actor.(weaponentity).hook_state &= ~HOOK_REMOVING;
205     }
206 }
207 METHOD(Hook, wr_setup, void(entity thiswep, entity actor, .entity weaponentity))
208 {
209         actor.(weaponentity).hook_state &= ~HOOK_WAITING_FOR_RELEASE;
210 }
211 METHOD(Hook, wr_checkammo1, bool(Hook thiswep, entity actor, .entity weaponentity))
212 {
213     if (!thiswep.ammo_factor) return true;
214
215     if(actor.(weaponentity).hook)
216         return actor.ammo_fuel > 0;
217
218     return actor.ammo_fuel >= WEP_CVAR_PRI(hook, ammo);
219 }
220 METHOD(Hook, wr_checkammo2, bool(Hook thiswep, entity actor, .entity weaponentity))
221 {
222     // infinite ammo for now
223     return true; // actor.ammo_cells >= WEP_CVAR_SEC(hook, ammo); // WEAPONTODO: see above
224 }
225 METHOD(Hook, wr_resetplayer, void(entity thiswep, entity actor))
226 {
227     RemoveGrapplingHooks(actor);
228     for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
229     {
230         .entity weaponentity = weaponentities[slot];
231         actor.(weaponentity).hook_time = 0;
232         actor.(weaponentity).hook_refire = time;
233     }
234 }
235 METHOD(Hook, wr_killmessage, Notification(entity thiswep))
236 {
237     return WEAPON_HOOK_MURDER;
238 }
239
240 #endif
241 #ifdef CSQC
242
243 METHOD(Hook, wr_impacteffect, void(entity thiswep, entity actor))
244 {
245     vector org2;
246     org2 = w_org + w_backoff * 2;
247     pointparticles(EFFECT_HOOK_EXPLODE, org2, '0 0 0', 1);
248     if(!w_issilent)
249         sound(actor, CH_SHOTS, SND_HOOKBOMB_IMPACT, VOL_BASE, ATTN_NORM);
250 }
251
252 #endif
253
254 #ifdef CSQC
255 #include <lib/csqcmodel/interpolate.qh>
256 #include <lib/warpzone/common.qh>
257
258 float autocvar_cl_grapplehook_alpha = 1;
259
260 void Draw_CylindricLine(vector from, vector to, float thickness, string texture, float aspect, float shift, vector rgb, float theAlpha, float drawflag, vector vieworg);
261
262 entityclass(Hook);
263 class(Hook) .entity HookType; // ENT_CLIENT_*
264 class(Hook) .vector origin;
265 class(Hook) .vector velocity;
266 class(Hook) .float HookSilent;
267 class(Hook) .float HookRange;
268
269 string Draw_GrapplingHook_trace_callback_tex;
270 float Draw_GrapplingHook_trace_callback_rnd;
271 vector Draw_GrapplingHook_trace_callback_rgb;
272 float Draw_GrapplingHook_trace_callback_a;
273 void Draw_GrapplingHook_trace_callback(vector start, vector hit, vector end)
274 {
275         float i;
276         vector vorg;
277         vorg = WarpZone_TransformOrigin(WarpZone_trace_transform, view_origin);
278         for(i = 0; i < Draw_GrapplingHook_trace_callback_a; ++i)
279                 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);
280         Draw_GrapplingHook_trace_callback_rnd += 0.25 * vlen(hit - start) / 8;
281 }
282
283 class(Hook) .float teleport_time;
284 void Draw_GrapplingHook(entity this)
285 {
286         vector a, b, atrans;
287         string tex;
288         vector rgb;
289         float t;
290         vector vs;
291         float intensity, offset;
292
293         if(this.teleport_time)
294         if(time > this.teleport_time)
295         {
296                 sound (this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, ATTEN_NORM); // safeguard
297                 this.teleport_time = 0;
298         }
299
300         InterpolateOrigin_Do(this);
301
302         int s = W_GunAlign(viewmodels[this.cnt], STAT(GUNALIGN)) - 1;
303
304         switch(this.HookType)
305         {
306                 default:
307                 case NET_ENT_CLIENT_HOOK:
308                         vs = hook_shotorigin[s];
309                         break;
310                 case NET_ENT_CLIENT_ARC_BEAM:
311                         vs = lightning_shotorigin[s];
312                         break;
313         }
314
315         if((this.owner.sv_entnum == player_localentnum - 1))
316         {
317                 switch(this.HookType)
318                 {
319                         default:
320                         case NET_ENT_CLIENT_HOOK:
321                                 if(autocvar_chase_active > 0)
322                                         a = csqcplayer.origin;
323                                 else
324                                         a = view_origin + view_forward * vs.x + view_right * -vs.y + view_up * vs.z;
325                                 b = this.origin;
326                                 break;
327                         case NET_ENT_CLIENT_ARC_BEAM:
328                                 if(this.HookRange)
329                                         b = view_origin + view_forward * this.HookRange;
330                                 else
331                                         b = view_origin + view_forward * vlen(this.velocity - this.origin); // honor original length of beam!
332                                 WarpZone_TraceLine(view_origin, b, MOVE_NORMAL, NULL);
333                                 b = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos);
334                                 a = view_origin + view_forward * vs.x + view_right * -vs.y + view_up * vs.z;
335                                 break;
336                 }
337         }
338         else
339         {
340                 switch(this.HookType)
341                 {
342                         default:
343                         case NET_ENT_CLIENT_HOOK:
344                                 a = this.velocity;
345                                 b = this.origin;
346                                 break;
347                         case NET_ENT_CLIENT_ARC_BEAM:
348                                 a = this.origin;
349                                 b = this.velocity;
350                                 break;
351                 }
352         }
353
354         t = entcs_GetTeamColor(this.owner.sv_entnum);
355
356         switch(this.HookType)
357         {
358                 default:
359                 case NET_ENT_CLIENT_HOOK:
360                         intensity = autocvar_cl_grapplehook_alpha;
361                         offset = 0;
362                         switch(t)
363                         {
364                                 case NUM_TEAM_1: tex = "particles/hook_red"; rgb = '1 0.3 0.3'; break;
365                                 case NUM_TEAM_2: tex = "particles/hook_blue"; rgb = '0.3 0.3 1'; break;
366                                 case NUM_TEAM_3: tex = "particles/hook_yellow"; rgb = '1 1 0.3'; break;
367                                 case NUM_TEAM_4: tex = "particles/hook_pink"; rgb = '1 0.3 1'; break;
368                                 default: tex = "particles/hook_white"; rgb = entcs_GetColor(this.sv_entnum - 1); break;
369                         }
370                         break;
371                 case NET_ENT_CLIENT_ARC_BEAM: // todo
372                         intensity = bound(0.2, 1 + Noise_Pink(this, frametime) * 1 + Noise_Burst(this, frametime, 0.03) * 0.3, 2);
373                         offset = Noise_Brown(this, frametime) * 10;
374                         tex = "particles/lgbeam";
375                         rgb = '1 1 1';
376                         break;
377         }
378
379         MUTATOR_CALLHOOK(DrawGrapplingHook, this, tex, rgb, t);
380         tex = M_ARGV(1, string);
381         rgb = M_ARGV(2, vector);
382
383         Draw_GrapplingHook_trace_callback_tex = tex;
384         Draw_GrapplingHook_trace_callback_rnd = offset;
385         Draw_GrapplingHook_trace_callback_rgb = rgb;
386         Draw_GrapplingHook_trace_callback_a = intensity;
387         WarpZone_TraceBox_ThroughZone(a, '0 0 0', '0 0 0', b, ((this.HookType == NET_ENT_CLIENT_HOOK) ? MOVE_NOTHING : MOVE_NORMAL), NULL, NULL, Draw_GrapplingHook_trace_callback);
388         Draw_GrapplingHook_trace_callback_tex = string_null;
389
390         atrans = WarpZone_TransformOrigin(WarpZone_trace_transform, a);
391
392         switch(this.HookType)
393         {
394                 default:
395                 case NET_ENT_CLIENT_HOOK:
396                         if(vdist(trace_endpos - atrans, >, 0.5))
397                         {
398                                 setorigin(this, trace_endpos); // hook endpoint!
399                                 this.angles = vectoangles(trace_endpos - atrans);
400                                 this.drawmask = MASK_NORMAL;
401                         }
402                         else
403                         {
404                                 this.drawmask = 0;
405                         }
406                         break;
407                 case NET_ENT_CLIENT_ARC_BEAM:
408                         setorigin(this, a); // beam origin!
409                         break;
410         }
411
412         switch(this.HookType)
413         {
414                 default:
415                 case NET_ENT_CLIENT_HOOK:
416                         break;
417                 case NET_ENT_CLIENT_ARC_BEAM:
418                         pointparticles(EFFECT_ARC_LIGHTNING2, trace_endpos, normalize(atrans - trace_endpos), frametime * intensity); // todo: new effect
419                         break;
420         }
421 }
422
423 void Remove_GrapplingHook(entity this)
424 {
425         sound (this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, ATTEN_NORM);
426
427         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
428         {
429                 entity wep = viewmodels[slot];
430                 if(wep.hook == this)
431                         wep.hook = NULL;
432         }
433 }
434
435 NET_HANDLE(ENT_CLIENT_HOOK, bool bIsNew)
436 {
437         this.HookType = NET_ENT_CLIENT_HOOK;
438
439         int sf = ReadByte();
440
441         this.HookSilent = (sf & 0x80);
442         this.iflags = IFLAG_VELOCITY | IFLAG_ORIGIN;
443
444         InterpolateOrigin_Undo(this);
445
446         if(sf & 1)
447         {
448                 int myowner = ReadByte();
449                 int slot = ReadByte();
450                 this.owner = playerslots[myowner - 1];
451                 this.sv_entnum = myowner;
452                 if(myowner == player_localentnum)
453                         viewmodels[slot].hook = this;
454                 this.cnt = slot;
455                 switch(this.HookType)
456                 {
457                         default:
458                         case NET_ENT_CLIENT_HOOK:
459                                 this.HookRange = 0;
460                                 break;
461                         case NET_ENT_CLIENT_ARC_BEAM:
462                                 this.HookRange = ReadCoord();
463                                 break;
464                 }
465         }
466         if(sf & 2)
467         {
468                 this.origin_x = ReadCoord();
469                 this.origin_y = ReadCoord();
470                 this.origin_z = ReadCoord();
471                 setorigin(this, this.origin);
472         }
473         if(sf & 4)
474         {
475                 this.velocity_x = ReadCoord();
476                 this.velocity_y = ReadCoord();
477                 this.velocity_z = ReadCoord();
478         }
479
480         InterpolateOrigin_Note(this);
481
482         if(bIsNew || !this.teleport_time)
483         {
484                 this.draw = Draw_GrapplingHook;
485                 IL_PUSH(g_drawables, this);
486                 this.entremove = Remove_GrapplingHook;
487
488                 switch(this.HookType)
489                 {
490                         default:
491                         case NET_ENT_CLIENT_HOOK:
492                                 // for the model
493                                 setmodel(this, MDL_HOOK);
494                                 this.drawmask = MASK_NORMAL;
495                                 break;
496                         case NET_ENT_CLIENT_ARC_BEAM:
497                                 sound (this, CH_SHOTS_SINGLE, SND_LGBEAM_FLY, VOL_BASE, ATTEN_NORM);
498                                 break;
499                 }
500         }
501
502         this.teleport_time = time + 10;
503         return true;
504 }
505
506 // TODO: hook: temporarily transform this.origin for drawing the model along warpzones!
507 #endif