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