Remove SELFPARAM() from .think and .touch
[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(entity this)
83 {
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(entity this)
103 {
104         self.event_damage = func_null;
105         settouch(self, func_null);
106         self.effects |= EF_NODRAW;
107
108         setthink(self, 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_Explode2_use(entity this, entity actor, entity trigger)
122 {
123         WITHSELF(this, W_Hook_Explode2(this));
124 }
125
126 void W_Hook_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
127 {
128         if(this.health <= 0)
129                 return;
130
131         if(!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
132                 return; // g_projectiles_damage says to halt
133
134         this.health = this.health - damage;
135
136         if(this.health <= 0)
137                 W_PrepareExplosionByDamage(this, this.realowner, W_Hook_Explode2);
138 }
139
140 void W_Hook_Touch2(entity this)
141 {
142         PROJECTILE_TOUCH;
143         this.use(this, NULL, NULL);
144 }
145
146 void W_Hook_Attack2(Weapon thiswep, entity actor)
147 {
148         //W_DecreaseAmmo(thiswep, actor, WEP_CVAR_SEC(hook, ammo)); // WEAPONTODO: Figure out how to handle ammo with hook secondary (gravitybomb)
149         W_SetupShot(actor, false, 4, SND_HOOKBOMB_FIRE, CH_WEAPON_A, WEP_CVAR_SEC(hook, damage));
150
151         entity gren = new(hookbomb);
152         gren.owner = gren.realowner = actor;
153         gren.bot_dodge = true;
154         gren.bot_dodgerating = WEP_CVAR_SEC(hook, damage);
155         gren.movetype = MOVETYPE_TOSS;
156         PROJECTILE_MAKETRIGGER(gren);
157         gren.projectiledeathtype = WEP_HOOK.m_id | HITTYPE_SECONDARY;
158         setorigin(gren, w_shotorg);
159         setsize(gren, '0 0 0', '0 0 0');
160
161         gren.nextthink = time + WEP_CVAR_SEC(hook, lifetime);
162         setthink(gren, adaptor_think2use_hittype_splash);
163         gren.use = W_Hook_Explode2_use;
164         settouch(gren, W_Hook_Touch2);
165
166         gren.takedamage = DAMAGE_YES;
167         gren.health = WEP_CVAR_SEC(hook, health);
168         gren.damageforcescale = WEP_CVAR_SEC(hook, damageforcescale);
169         gren.event_damage = W_Hook_Damage;
170         gren.damagedbycontents = true;
171         gren.missile_flags = MIF_SPLASH | MIF_ARC;
172
173         gren.velocity = '0 0 1' * WEP_CVAR_SEC(hook, speed);
174         if (autocvar_g_projectiles_newton_style)
175                 gren.velocity = gren.velocity + actor.velocity;
176
177         gren.gravity = WEP_CVAR_SEC(hook, gravity);
178         //W_SetupProjVelocity_Basic(gren); // just falling down!
179
180         gren.angles = '0 0 0';
181         gren.flags = FL_PROJECTILE;
182
183         CSQCProjectile(gren, true, PROJECTILE_HOOKBOMB, true);
184
185         MUTATOR_CALLHOOK(EditProjectile, actor, gren);
186 }
187
188 METHOD(Hook, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
189 {
190     if (fire & 1) {
191         if(!actor.hook)
192         if(!(actor.hook_state & HOOK_WAITING_FOR_RELEASE))
193         if(time > actor.hook_refire)
194         if(weapon_prepareattack(thiswep, actor, weaponentity, false, -1))
195         {
196             W_DecreaseAmmo(thiswep, actor, thiswep.ammo_factor * WEP_CVAR_PRI(hook, ammo));
197             actor.hook_state |= HOOK_FIRING;
198             actor.hook_state |= HOOK_WAITING_FOR_RELEASE;
199             weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(hook, animtime), w_ready);
200         }
201     } else {
202         actor.hook_state |= HOOK_REMOVING;
203         actor.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
204     }
205
206     if(fire & 2)
207     {
208         if(weapon_prepareattack(thiswep, actor, weaponentity, true, WEP_CVAR_SEC(hook, refire)))
209         {
210             W_Hook_Attack2(thiswep, actor);
211             weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(hook, animtime), w_ready);
212         }
213     }
214
215     if(actor.hook)
216     {
217         // if hooked, no bombs, and increase the timer
218         actor.hook_refire = max(actor.hook_refire, time + WEP_CVAR_PRI(hook, refire) * W_WeaponRateFactor());
219
220         // hook also inhibits health regeneration, but only for 1 second
221         if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
222             actor.pauseregen_finished = max(actor.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
223     }
224
225     if(actor.hook && actor.hook.state == 1)
226     {
227         float hooked_time_max = WEP_CVAR_PRI(hook, hooked_time_max);
228         if(hooked_time_max > 0)
229         {
230             if( time > actor.hook_time_hooked + hooked_time_max )
231                 actor.hook_state |= HOOK_REMOVING;
232         }
233
234         float hooked_fuel = thiswep.ammo_factor * WEP_CVAR_PRI(hook, hooked_ammo);
235         if(hooked_fuel > 0)
236         {
237             if( time > actor.hook_time_fueldecrease )
238             {
239                 if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
240                 {
241                     if( actor.ammo_fuel >= (time - actor.hook_time_fueldecrease) * hooked_fuel )
242                     {
243                         W_DecreaseAmmo(thiswep, actor, (time - actor.hook_time_fueldecrease) * hooked_fuel);
244                         actor.hook_time_fueldecrease = time;
245                         // decrease next frame again
246                     }
247                     else
248                     {
249                         actor.ammo_fuel = 0;
250                         actor.hook_state |= HOOK_REMOVING;
251                         W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
252                     }
253                 }
254             }
255         }
256     }
257     else
258     {
259         actor.hook_time_hooked = time;
260         actor.hook_time_fueldecrease = time + WEP_CVAR_PRI(hook, hooked_time_free);
261     }
262
263     actor.hook_state = BITSET(actor.hook_state, HOOK_PULLING, (!PHYS_INPUT_BUTTON_CROUCH(actor) || !autocvar_g_balance_grapplehook_crouchslide));
264
265     if (actor.hook_state & HOOK_FIRING)
266     {
267         if (actor.hook)
268             RemoveGrapplingHook(actor);
269         WITHSELF(actor, FireGrapplingHook());
270         actor.hook_state &= ~HOOK_FIRING;
271         actor.hook_refire = max(actor.hook_refire, time + autocvar_g_balance_grapplehook_refire * W_WeaponRateFactor());
272     }
273     else if (actor.hook_state & HOOK_REMOVING)
274     {
275         if (actor.hook)
276             RemoveGrapplingHook(actor);
277         actor.hook_state &= ~HOOK_REMOVING;
278     }
279 }
280 METHOD(Hook, wr_setup, void(entity thiswep))
281 {
282     SELFPARAM();
283     self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
284 }
285 METHOD(Hook, wr_checkammo1, bool(Hook thiswep, entity actor))
286 {
287     if (!thiswep.ammo_factor) return true;
288     if(actor.hook)
289         return actor.ammo_fuel > 0;
290     else
291         return actor.ammo_fuel >= WEP_CVAR_PRI(hook, ammo);
292 }
293 METHOD(Hook, wr_checkammo2, bool(Hook thiswep, entity actor))
294 {
295     // infinite ammo for now
296     return true; // actor.ammo_cells >= WEP_CVAR_SEC(hook, ammo); // WEAPONTODO: see above
297 }
298 METHOD(Hook, wr_resetplayer, void(entity thiswep))
299 {
300     SELFPARAM();
301     RemoveGrapplingHook(self);
302     self.hook_time = 0;
303     self.hook_refire = time;
304 }
305 METHOD(Hook, wr_killmessage, Notification(entity thiswep))
306 {
307     return WEAPON_HOOK_MURDER;
308 }
309
310 #endif
311 #ifdef CSQC
312
313 METHOD(Hook, wr_impacteffect, void(entity thiswep))
314 {
315     SELFPARAM();
316     vector org2;
317     org2 = w_org + w_backoff * 2;
318     pointparticles(EFFECT_HOOK_EXPLODE, org2, '0 0 0', 1);
319     if(!w_issilent)
320         sound(self, CH_SHOTS, SND_HOOKBOMB_IMPACT, VOL_BASE, ATTN_NORM);
321 }
322
323 #endif
324
325 #ifdef CSQC
326 #include <lib/csqcmodel/interpolate.qh>
327 #include <lib/warpzone/common.qh>
328
329 float autocvar_cl_grapplehook_alpha = 1;
330
331 void Draw_CylindricLine(vector from, vector to, float thickness, string texture, float aspect, float shift, vector rgb, float theAlpha, float drawflag, vector vieworg);
332
333 entityclass(Hook);
334 class(Hook) .entity HookType; // ENT_CLIENT_*
335 class(Hook) .vector origin;
336 class(Hook) .vector velocity;
337 class(Hook) .float HookSilent;
338 class(Hook) .float HookRange;
339
340 string Draw_GrapplingHook_trace_callback_tex;
341 float Draw_GrapplingHook_trace_callback_rnd;
342 vector Draw_GrapplingHook_trace_callback_rgb;
343 float Draw_GrapplingHook_trace_callback_a;
344 void Draw_GrapplingHook_trace_callback(vector start, vector hit, vector end)
345 {
346         float i;
347         vector vorg;
348         vorg = WarpZone_TransformOrigin(WarpZone_trace_transform, view_origin);
349         for(i = 0; i < Draw_GrapplingHook_trace_callback_a; ++i)
350                 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);
351         Draw_GrapplingHook_trace_callback_rnd += 0.25 * vlen(hit - start) / 8;
352 }
353
354 class(Hook) .float teleport_time;
355 void Draw_GrapplingHook(entity this)
356 {
357         vector a, b, atrans;
358         string tex;
359         vector rgb;
360         float t;
361         vector vs;
362         float intensity, offset;
363
364         if(this.teleport_time)
365         if(time > this.teleport_time)
366         {
367                 sound (this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, ATTEN_NORM); // safeguard
368                 this.teleport_time = 0;
369         }
370
371         InterpolateOrigin_Do(this);
372
373         int s = W_GetGunAlignment(world);
374
375         switch(this.HookType)
376         {
377                 default:
378                 case NET_ENT_CLIENT_HOOK:
379                         vs = hook_shotorigin[s];
380                         break;
381                 case NET_ENT_CLIENT_ARC_BEAM:
382                         vs = lightning_shotorigin[s];
383                         break;
384         }
385
386         if((this.owner.sv_entnum == player_localentnum - 1) && autocvar_chase_active <= 0)
387         {
388                 switch(this.HookType)
389                 {
390                         default:
391                         case NET_ENT_CLIENT_HOOK:
392                                 a = view_origin + view_forward * vs.x + view_right * -vs.y + view_up * vs.z;
393                                 b = this.origin;
394                                 break;
395                         case NET_ENT_CLIENT_ARC_BEAM:
396                                 if(this.HookRange)
397                                         b = view_origin + view_forward * this.HookRange;
398                                 else
399                                         b = view_origin + view_forward * vlen(this.velocity - this.origin); // honor original length of beam!
400                                 WarpZone_TraceLine(view_origin, b, MOVE_NORMAL, world);
401                                 b = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos);
402                                 a = view_origin + view_forward * vs.x + view_right * -vs.y + view_up * vs.z;
403                                 break;
404                 }
405         }
406         else
407         {
408                 switch(this.HookType)
409                 {
410                         default:
411                         case NET_ENT_CLIENT_HOOK:
412                                 a = this.velocity;
413                                 b = this.origin;
414                                 break;
415                         case NET_ENT_CLIENT_ARC_BEAM:
416                                 a = this.origin;
417                                 b = this.velocity;
418                                 break;
419                 }
420         }
421
422         t = entcs_GetTeamColor(this.owner.sv_entnum);
423
424         switch(this.HookType)
425         {
426                 default:
427                 case NET_ENT_CLIENT_HOOK:
428                         intensity = autocvar_cl_grapplehook_alpha;
429                         offset = 0;
430                         switch(t)
431                         {
432                                 case NUM_TEAM_1: tex = "particles/hook_red"; rgb = '1 0.3 0.3'; break;
433                                 case NUM_TEAM_2: tex = "particles/hook_blue"; rgb = '0.3 0.3 1'; break;
434                                 case NUM_TEAM_3: tex = "particles/hook_yellow"; rgb = '1 1 0.3'; break;
435                                 case NUM_TEAM_4: tex = "particles/hook_pink"; rgb = '1 0.3 1'; break;
436                                 default: tex = "particles/hook_white"; rgb = entcs_GetColor(this.sv_entnum - 1); break;
437                         }
438                         break;
439                 case NET_ENT_CLIENT_ARC_BEAM: // todo
440                         intensity = bound(0.2, 1 + Noise_Pink(this, frametime) * 1 + Noise_Burst(this, frametime, 0.03) * 0.3, 2);
441                         offset = Noise_Brown(this, frametime) * 10;
442                         tex = "particles/lgbeam";
443                         rgb = '1 1 1';
444                         break;
445         }
446
447         Draw_GrapplingHook_trace_callback_tex = tex;
448         Draw_GrapplingHook_trace_callback_rnd = offset;
449         Draw_GrapplingHook_trace_callback_rgb = rgb;
450         Draw_GrapplingHook_trace_callback_a = intensity;
451         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);
452         Draw_GrapplingHook_trace_callback_tex = string_null;
453
454         atrans = WarpZone_TransformOrigin(WarpZone_trace_transform, a);
455
456         switch(this.HookType)
457         {
458                 default:
459                 case NET_ENT_CLIENT_HOOK:
460                         if(vdist(trace_endpos - atrans, >, 0.5))
461                         {
462                                 setorigin(this, trace_endpos); // hook endpoint!
463                                 this.angles = vectoangles(trace_endpos - atrans);
464                                 this.drawmask = MASK_NORMAL;
465                         }
466                         else
467                         {
468                                 this.drawmask = 0;
469                         }
470                         break;
471                 case NET_ENT_CLIENT_ARC_BEAM:
472                         setorigin(this, a); // beam origin!
473                         break;
474         }
475
476         switch(this.HookType)
477         {
478                 default:
479                 case NET_ENT_CLIENT_HOOK:
480                         break;
481                 case NET_ENT_CLIENT_ARC_BEAM:
482                         pointparticles(EFFECT_ARC_LIGHTNING2, trace_endpos, normalize(atrans - trace_endpos), frametime * intensity); // todo: new effect
483                         break;
484         }
485 }
486
487 void Remove_GrapplingHook(entity this)
488 {
489         sound (this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, ATTEN_NORM);
490
491         if(csqcplayer && csqcplayer.hook == this)
492                 csqcplayer.hook = NULL;
493 }
494
495 NET_HANDLE(ENT_CLIENT_HOOK, bool bIsNew)
496 {
497         this.HookType = NET_ENT_CLIENT_HOOK;
498
499         int sf = ReadByte();
500
501         this.HookSilent = (sf & 0x80);
502         this.iflags = IFLAG_VELOCITY | IFLAG_ORIGIN;
503
504         InterpolateOrigin_Undo(this);
505
506         if(sf & 1)
507         {
508                 int myowner = ReadByte();
509                 this.owner = playerslots[myowner - 1];
510                 this.sv_entnum = myowner;
511                 if(csqcplayer && myowner == player_localentnum)
512                         csqcplayer.hook = this;
513                 switch(this.HookType)
514                 {
515                         default:
516                         case NET_ENT_CLIENT_HOOK:
517                                 this.HookRange = 0;
518                                 break;
519                         case NET_ENT_CLIENT_ARC_BEAM:
520                                 this.HookRange = ReadCoord();
521                                 break;
522                 }
523         }
524         if(sf & 2)
525         {
526                 this.origin_x = ReadCoord();
527                 this.origin_y = ReadCoord();
528                 this.origin_z = ReadCoord();
529                 setorigin(this, this.origin);
530         }
531         if(sf & 4)
532         {
533                 this.velocity_x = ReadCoord();
534                 this.velocity_y = ReadCoord();
535                 this.velocity_z = ReadCoord();
536         }
537
538         InterpolateOrigin_Note(this);
539
540         if(bIsNew || !this.teleport_time)
541         {
542                 this.draw = Draw_GrapplingHook;
543                 this.entremove = Remove_GrapplingHook;
544
545                 switch(this.HookType)
546                 {
547                         default:
548                         case NET_ENT_CLIENT_HOOK:
549                                 // for the model
550                                 setmodel(this, MDL_HOOK);
551                                 this.drawmask = MASK_NORMAL;
552                                 break;
553                         case NET_ENT_CLIENT_ARC_BEAM:
554                                 sound (this, CH_SHOTS_SINGLE, SND_LGBEAM_FLY, VOL_BASE, ATTEN_NORM);
555                                 break;
556                 }
557         }
558
559         this.teleport_time = time + 10;
560         return true;
561 }
562
563 // TODO: hook: temporarily transform this.origin for drawing the model along warpzones!
564 #endif
565
566 #endif