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");
10 /* model */ ATTRIB(Hook, m_model, Model, MDL_HOOK_ITEM);
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)
19 #define X(BEGIN, P, END, class, prefix) \
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) \
45 W_PROPS(X, Hook, hook)
49 REGISTER_WEAPON(HOOK, hook, NEW(Hook));
51 CLASS(OffhandHook, OffhandWeapon)
53 METHOD(OffhandHook, offhand_think, void(OffhandHook this, entity actor, bool key_pressed))
55 Weapon wep = WEP_HOOK;
56 .entity weaponentity = weaponentities[1];
57 wep.wr_think(wep, actor, weaponentity, key_pressed ? 1 : 0);
61 OffhandHook OFFHAND_HOOK; STATIC_INIT(OFFHAND_HOOK) { OFFHAND_HOOK = NEW(OffhandHook); }
73 .float hook_time_hooked;
74 .float hook_time_fueldecrease;
80 spawnfunc(weapon_hook) { weapon_defaultspawnfunc(this, WEP_HOOK); }
82 void W_Hook_ExplodeThink(entity this)
84 float dt, dmg_remaining_next, f;
86 dt = time - this.teleport_time;
87 dmg_remaining_next = pow(bound(0, 1 - dt / this.dmg_duration, 1), this.dmg_power);
89 f = this.dmg_last - dmg_remaining_next;
90 this.dmg_last = dmg_remaining_next;
92 RadiusDamage(this, this.realowner, this.dmg * f, this.dmg_edge * f, this.dmg_radius, this.realowner, NULL, this.dmg_force * f, this.projectiledeathtype, NULL);
93 this.projectiledeathtype |= HITTYPE_BOUNCE;
94 //RadiusDamage(this, NULL, this.dmg * f, this.dmg_edge * f, this.dmg_radius, NULL, NULL, this.dmg_force * f, this.projectiledeathtype, NULL);
96 if(dt < this.dmg_duration)
97 this.nextthink = time + 0.05; // soon
102 void W_Hook_Explode2(entity this)
104 this.event_damage = func_null;
105 settouch(this, func_null);
106 this.effects |= EF_NODRAW;
108 setthink(this, W_Hook_ExplodeThink);
109 this.nextthink = time;
110 this.dmg = WEP_CVAR_SEC(hook, damage);
111 this.dmg_edge = WEP_CVAR_SEC(hook, edgedamage);
112 this.dmg_radius = WEP_CVAR_SEC(hook, radius);
113 this.dmg_force = WEP_CVAR_SEC(hook, force);
114 this.dmg_power = WEP_CVAR_SEC(hook, power);
115 this.dmg_duration = WEP_CVAR_SEC(hook, duration);
116 this.teleport_time = time;
118 set_movetype(this, MOVETYPE_NONE);
121 void W_Hook_Explode2_use(entity this, entity actor, entity trigger)
123 W_Hook_Explode2(this);
126 void W_Hook_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
131 if(!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
132 return; // g_projectiles_damage says to halt
134 this.health = this.health - damage;
137 W_PrepareExplosionByDamage(this, this.realowner, W_Hook_Explode2);
140 void W_Hook_Touch2(entity this, entity toucher)
142 PROJECTILE_TOUCH(this, toucher);
143 this.use(this, NULL, NULL);
146 void W_Hook_Attack2(Weapon thiswep, entity actor, .entity weaponentity)
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, weaponentity, false, 4, SND_HOOKBOMB_FIRE, CH_WEAPON_A, WEP_CVAR_SEC(hook, damage));
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 set_movetype(gren, 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');
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);
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;
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;
177 gren.gravity = WEP_CVAR_SEC(hook, gravity);
178 //W_SetupProjVelocity_Basic(gren); // just falling down!
180 gren.angles = '0 0 0';
181 gren.flags = FL_PROJECTILE;
182 IL_PUSH(g_projectiles, gren);
184 CSQCProjectile(gren, true, PROJECTILE_HOOKBOMB, true);
186 MUTATOR_CALLHOOK(EditProjectile, actor, gren);
189 METHOD(Hook, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
193 if(!(actor.hook_state & HOOK_WAITING_FOR_RELEASE))
194 if(time > actor.hook_refire)
195 if(weapon_prepareattack(thiswep, actor, weaponentity, false, -1))
197 W_DecreaseAmmo(thiswep, actor, thiswep.ammo_factor * WEP_CVAR_PRI(hook, ammo));
198 actor.hook_state |= HOOK_FIRING;
199 actor.hook_state |= HOOK_WAITING_FOR_RELEASE;
200 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(hook, animtime), w_ready);
203 actor.hook_state |= HOOK_REMOVING;
204 actor.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
209 if(weapon_prepareattack(thiswep, actor, weaponentity, true, WEP_CVAR_SEC(hook, refire)))
211 W_Hook_Attack2(thiswep, actor, weaponentity);
212 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(hook, animtime), w_ready);
218 // if hooked, no bombs, and increase the timer
219 actor.hook_refire = max(actor.hook_refire, time + WEP_CVAR_PRI(hook, refire) * W_WeaponRateFactor(actor));
221 // hook also inhibits health regeneration, but only for 1 second
222 if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
223 actor.pauseregen_finished = max(actor.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
226 if(actor.hook && actor.hook.state == 1)
228 float hooked_time_max = WEP_CVAR_PRI(hook, hooked_time_max);
229 if(hooked_time_max > 0)
231 if( time > actor.hook_time_hooked + hooked_time_max )
232 actor.hook_state |= HOOK_REMOVING;
235 float hooked_fuel = thiswep.ammo_factor * WEP_CVAR_PRI(hook, hooked_ammo);
238 if( time > actor.hook_time_fueldecrease )
240 if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
242 if( actor.ammo_fuel >= (time - actor.hook_time_fueldecrease) * hooked_fuel )
244 W_DecreaseAmmo(thiswep, actor, (time - actor.hook_time_fueldecrease) * hooked_fuel);
245 actor.hook_time_fueldecrease = time;
246 // decrease next frame again
251 actor.hook_state |= HOOK_REMOVING;
252 W_SwitchWeapon_Force(actor, w_getbestweapon(actor));
260 actor.hook_time_hooked = time;
261 actor.hook_time_fueldecrease = time + WEP_CVAR_PRI(hook, hooked_time_free);
264 actor.hook_state = BITSET(actor.hook_state, HOOK_PULLING, (!PHYS_INPUT_BUTTON_CROUCH(actor) || !autocvar_g_balance_grapplehook_crouchslide));
266 if (actor.hook_state & HOOK_FIRING)
269 RemoveGrapplingHook(actor);
270 FireGrapplingHook(actor);
271 actor.hook_state &= ~HOOK_FIRING;
272 actor.hook_refire = max(actor.hook_refire, time + autocvar_g_balance_grapplehook_refire * W_WeaponRateFactor(actor));
274 else if (actor.hook_state & HOOK_REMOVING)
277 RemoveGrapplingHook(actor);
278 actor.hook_state &= ~HOOK_REMOVING;
281 METHOD(Hook, wr_setup, void(entity thiswep, entity actor))
283 actor.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
285 METHOD(Hook, wr_checkammo1, bool(Hook thiswep, entity actor))
287 if (!thiswep.ammo_factor) return true;
289 return actor.ammo_fuel > 0;
291 return actor.ammo_fuel >= WEP_CVAR_PRI(hook, ammo);
293 METHOD(Hook, wr_checkammo2, bool(Hook thiswep, entity actor))
295 // infinite ammo for now
296 return true; // actor.ammo_cells >= WEP_CVAR_SEC(hook, ammo); // WEAPONTODO: see above
298 METHOD(Hook, wr_resetplayer, void(entity thiswep, entity actor))
300 RemoveGrapplingHook(actor);
302 actor.hook_refire = time;
304 METHOD(Hook, wr_killmessage, Notification(entity thiswep))
306 return WEAPON_HOOK_MURDER;
312 METHOD(Hook, wr_impacteffect, void(entity thiswep, entity actor))
315 org2 = w_org + w_backoff * 2;
316 pointparticles(EFFECT_HOOK_EXPLODE, org2, '0 0 0', 1);
318 sound(actor, CH_SHOTS, SND_HOOKBOMB_IMPACT, VOL_BASE, ATTN_NORM);
324 #include <lib/csqcmodel/interpolate.qh>
325 #include <lib/warpzone/common.qh>
327 float autocvar_cl_grapplehook_alpha = 1;
329 void Draw_CylindricLine(vector from, vector to, float thickness, string texture, float aspect, float shift, vector rgb, float theAlpha, float drawflag, vector vieworg);
332 class(Hook) .entity HookType; // ENT_CLIENT_*
333 class(Hook) .vector origin;
334 class(Hook) .vector velocity;
335 class(Hook) .float HookSilent;
336 class(Hook) .float HookRange;
338 string Draw_GrapplingHook_trace_callback_tex;
339 float Draw_GrapplingHook_trace_callback_rnd;
340 vector Draw_GrapplingHook_trace_callback_rgb;
341 float Draw_GrapplingHook_trace_callback_a;
342 void Draw_GrapplingHook_trace_callback(vector start, vector hit, vector end)
346 vorg = WarpZone_TransformOrigin(WarpZone_trace_transform, view_origin);
347 for(i = 0; i < Draw_GrapplingHook_trace_callback_a; ++i)
348 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);
349 Draw_GrapplingHook_trace_callback_rnd += 0.25 * vlen(hit - start) / 8;
352 class(Hook) .float teleport_time;
353 void Draw_GrapplingHook(entity this)
360 float intensity, offset;
362 if(this.teleport_time)
363 if(time > this.teleport_time)
365 sound (this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, ATTEN_NORM); // safeguard
366 this.teleport_time = 0;
369 InterpolateOrigin_Do(this);
371 int s = W_GetGunAlignment(NULL);
373 switch(this.HookType)
376 case NET_ENT_CLIENT_HOOK:
377 vs = hook_shotorigin[s];
379 case NET_ENT_CLIENT_ARC_BEAM:
380 vs = lightning_shotorigin[s];
384 if((this.owner.sv_entnum == player_localentnum - 1) && autocvar_chase_active <= 0)
386 switch(this.HookType)
389 case NET_ENT_CLIENT_HOOK:
390 a = view_origin + view_forward * vs.x + view_right * -vs.y + view_up * vs.z;
393 case NET_ENT_CLIENT_ARC_BEAM:
395 b = view_origin + view_forward * this.HookRange;
397 b = view_origin + view_forward * vlen(this.velocity - this.origin); // honor original length of beam!
398 WarpZone_TraceLine(view_origin, b, MOVE_NORMAL, NULL);
399 b = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos);
400 a = view_origin + view_forward * vs.x + view_right * -vs.y + view_up * vs.z;
406 switch(this.HookType)
409 case NET_ENT_CLIENT_HOOK:
413 case NET_ENT_CLIENT_ARC_BEAM:
420 t = entcs_GetTeamColor(this.owner.sv_entnum);
422 switch(this.HookType)
425 case NET_ENT_CLIENT_HOOK:
426 intensity = autocvar_cl_grapplehook_alpha;
430 case NUM_TEAM_1: tex = "particles/hook_red"; rgb = '1 0.3 0.3'; break;
431 case NUM_TEAM_2: tex = "particles/hook_blue"; rgb = '0.3 0.3 1'; break;
432 case NUM_TEAM_3: tex = "particles/hook_yellow"; rgb = '1 1 0.3'; break;
433 case NUM_TEAM_4: tex = "particles/hook_pink"; rgb = '1 0.3 1'; break;
434 default: tex = "particles/hook_white"; rgb = entcs_GetColor(this.sv_entnum - 1); break;
437 case NET_ENT_CLIENT_ARC_BEAM: // todo
438 intensity = bound(0.2, 1 + Noise_Pink(this, frametime) * 1 + Noise_Burst(this, frametime, 0.03) * 0.3, 2);
439 offset = Noise_Brown(this, frametime) * 10;
440 tex = "particles/lgbeam";
445 MUTATOR_CALLHOOK(DrawGrapplingHook, this, tex, rgb, t);
446 tex = M_ARGV(1, string);
447 rgb = M_ARGV(2, vector);
449 Draw_GrapplingHook_trace_callback_tex = tex;
450 Draw_GrapplingHook_trace_callback_rnd = offset;
451 Draw_GrapplingHook_trace_callback_rgb = rgb;
452 Draw_GrapplingHook_trace_callback_a = intensity;
453 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);
454 Draw_GrapplingHook_trace_callback_tex = string_null;
456 atrans = WarpZone_TransformOrigin(WarpZone_trace_transform, a);
458 switch(this.HookType)
461 case NET_ENT_CLIENT_HOOK:
462 if(vdist(trace_endpos - atrans, >, 0.5))
464 setorigin(this, trace_endpos); // hook endpoint!
465 this.angles = vectoangles(trace_endpos - atrans);
466 this.drawmask = MASK_NORMAL;
473 case NET_ENT_CLIENT_ARC_BEAM:
474 setorigin(this, a); // beam origin!
478 switch(this.HookType)
481 case NET_ENT_CLIENT_HOOK:
483 case NET_ENT_CLIENT_ARC_BEAM:
484 pointparticles(EFFECT_ARC_LIGHTNING2, trace_endpos, normalize(atrans - trace_endpos), frametime * intensity); // todo: new effect
489 void Remove_GrapplingHook(entity this)
491 sound (this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, ATTEN_NORM);
493 if(csqcplayer && csqcplayer.hook == this)
494 csqcplayer.hook = NULL;
497 NET_HANDLE(ENT_CLIENT_HOOK, bool bIsNew)
499 this.HookType = NET_ENT_CLIENT_HOOK;
503 this.HookSilent = (sf & 0x80);
504 this.iflags = IFLAG_VELOCITY | IFLAG_ORIGIN;
506 InterpolateOrigin_Undo(this);
510 int myowner = ReadByte();
511 this.owner = playerslots[myowner - 1];
512 this.sv_entnum = myowner;
513 if(csqcplayer && myowner == player_localentnum)
514 csqcplayer.hook = this;
515 switch(this.HookType)
518 case NET_ENT_CLIENT_HOOK:
521 case NET_ENT_CLIENT_ARC_BEAM:
522 this.HookRange = ReadCoord();
528 this.origin_x = ReadCoord();
529 this.origin_y = ReadCoord();
530 this.origin_z = ReadCoord();
531 setorigin(this, this.origin);
535 this.velocity_x = ReadCoord();
536 this.velocity_y = ReadCoord();
537 this.velocity_z = ReadCoord();
540 InterpolateOrigin_Note(this);
542 if(bIsNew || !this.teleport_time)
544 this.draw = Draw_GrapplingHook;
545 IL_PUSH(g_drawables, this);
546 this.entremove = Remove_GrapplingHook;
548 switch(this.HookType)
551 case NET_ENT_CLIENT_HOOK:
553 setmodel(this, MDL_HOOK);
554 this.drawmask = MASK_NORMAL;
556 case NET_ENT_CLIENT_ARC_BEAM:
557 sound (this, CH_SHOTS_SINGLE, SND_LGBEAM_FLY, VOL_BASE, ATTEN_NORM);
562 this.teleport_time = time + 10;
566 // TODO: hook: temporarily transform this.origin for drawing the model along warpzones!