fc52287ddd4fc75b266231b6b58ea82f294fe497
[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, message, string, _("Grappling Hook"));
17 ENDCLASS(Hook)
18 REGISTER_WEAPON(HOOK, NEW(Hook));
19
20 CLASS(OffhandHook, OffhandWeapon)
21     METHOD(OffhandHook, offhand_think, void(OffhandHook this, entity player, bool key_pressed))
22     {
23         Weapon wep = WEP_HOOK;
24         WITH(entity, self, player, wep.wr_think(wep, key_pressed, false));
25     }
26 ENDCLASS(OffhandHook)
27 OffhandHook OFFHAND_HOOK; STATIC_INIT(OFFHAND_HOOK) { OFFHAND_HOOK = NEW(OffhandHook); }
28
29 #define HOOK_SETTINGS(w_cvar,w_prop) HOOK_SETTINGS_LIST(w_cvar, w_prop, HOOK, hook)
30 #define HOOK_SETTINGS_LIST(w_cvar,w_prop,id,sn) \
31         w_cvar(id, sn, BOTH, animtime) \
32         w_cvar(id, sn, BOTH, refire) \
33         w_cvar(id, sn, PRI,  ammo) \
34         w_cvar(id, sn, PRI,  hooked_ammo) \
35         w_cvar(id, sn, PRI,  hooked_time_free) \
36         w_cvar(id, sn, PRI,  hooked_time_max) \
37         w_cvar(id, sn, SEC,  damage) \
38         w_cvar(id, sn, SEC,  duration) \
39         w_cvar(id, sn, SEC,  edgedamage) \
40         w_cvar(id, sn, SEC,  force) \
41         w_cvar(id, sn, SEC,  gravity) \
42         w_cvar(id, sn, SEC,  lifetime) \
43         w_cvar(id, sn, SEC,  power) \
44         w_cvar(id, sn, SEC,  radius) \
45         w_cvar(id, sn, SEC,  speed) \
46         w_cvar(id, sn, SEC,  health) \
47         w_cvar(id, sn, SEC,  damageforcescale) \
48         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
49         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
50         w_prop(id, sn, string, weaponreplace, weaponreplace) \
51         w_prop(id, sn, float,  weaponstart, weaponstart) \
52         w_prop(id, sn, float,  weaponstartoverride, weaponstartoverride) \
53         w_prop(id, sn, float,  weaponthrowable, weaponthrowable)
54
55 #ifdef SVQC
56 HOOK_SETTINGS(WEP_ADD_CVAR, WEP_ADD_PROP)
57
58 .float dmg;
59 .float dmg_edge;
60 .float dmg_radius;
61 .float dmg_force;
62 .float dmg_power;
63 .float dmg_duration;
64 .float dmg_last;
65 .float hook_refire;
66 .float hook_time_hooked;
67 .float hook_time_fueldecrease;
68 #endif
69 #endif
70 #ifdef IMPLEMENTATION
71 #ifdef SVQC
72
73 void spawnfunc_weapon_hook() { weapon_defaultspawnfunc(WEP_HOOK.m_id); }
74
75 void W_Hook_ExplodeThink(void)
76 {SELFPARAM();
77         float dt, dmg_remaining_next, f;
78
79         dt = time - self.teleport_time;
80         dmg_remaining_next = pow(bound(0, 1 - dt / self.dmg_duration, 1), self.dmg_power);
81
82         f = self.dmg_last - dmg_remaining_next;
83         self.dmg_last = dmg_remaining_next;
84
85         RadiusDamage(self, self.realowner, self.dmg * f, self.dmg_edge * f, self.dmg_radius, self.realowner, world, self.dmg_force * f, self.projectiledeathtype, world);
86         self.projectiledeathtype |= HITTYPE_BOUNCE;
87         //RadiusDamage(self, world, self.dmg * f, self.dmg_edge * f, self.dmg_radius, world, world, self.dmg_force * f, self.projectiledeathtype, world);
88
89         if(dt < self.dmg_duration)
90                 self.nextthink = time + 0.05; // soon
91         else
92                 remove(self);
93 }
94
95 void W_Hook_Explode2(void)
96 {SELFPARAM();
97         self.event_damage = func_null;
98         self.touch = func_null;
99         self.effects |= EF_NODRAW;
100
101         self.think = W_Hook_ExplodeThink;
102         self.nextthink = time;
103         self.dmg = WEP_CVAR_SEC(hook, damage);
104         self.dmg_edge = WEP_CVAR_SEC(hook, edgedamage);
105         self.dmg_radius = WEP_CVAR_SEC(hook, radius);
106         self.dmg_force = WEP_CVAR_SEC(hook, force);
107         self.dmg_power = WEP_CVAR_SEC(hook, power);
108         self.dmg_duration = WEP_CVAR_SEC(hook, duration);
109         self.teleport_time = time;
110         self.dmg_last = 1;
111         self.movetype = MOVETYPE_NONE;
112 }
113
114 void W_Hook_Damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
115 {SELFPARAM();
116         if(self.health <= 0)
117                 return;
118
119         if(!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
120                 return; // g_projectiles_damage says to halt
121
122         self.health = self.health - damage;
123
124         if(self.health <= 0)
125                 W_PrepareExplosionByDamage(self.realowner, W_Hook_Explode2);
126 }
127
128 void W_Hook_Touch2(void)
129 {SELFPARAM();
130         PROJECTILE_TOUCH;
131         self.use();
132 }
133
134 void W_Hook_Attack2(Weapon thiswep)
135 {SELFPARAM();
136         entity gren;
137
138         //W_DecreaseAmmo(thiswep, WEP_CVAR_SEC(hook, ammo)); // WEAPONTODO: Figure out how to handle ammo with hook secondary (gravitybomb)
139         W_SetupShot(self, false, 4, SND(HOOKBOMB_FIRE), CH_WEAPON_A, WEP_CVAR_SEC(hook, damage));
140
141         gren = spawn();
142         gren.owner = gren.realowner = self;
143         gren.classname = "hookbomb";
144         gren.bot_dodge = true;
145         gren.bot_dodgerating = WEP_CVAR_SEC(hook, damage);
146         gren.movetype = MOVETYPE_TOSS;
147         PROJECTILE_MAKETRIGGER(gren);
148         gren.projectiledeathtype = WEP_HOOK.m_id | HITTYPE_SECONDARY;
149         setorigin(gren, w_shotorg);
150         setsize(gren, '0 0 0', '0 0 0');
151
152         gren.nextthink = time + WEP_CVAR_SEC(hook, lifetime);
153         gren.think = adaptor_think2use_hittype_splash;
154         gren.use = W_Hook_Explode2;
155         gren.touch = W_Hook_Touch2;
156
157         gren.takedamage = DAMAGE_YES;
158         gren.health = WEP_CVAR_SEC(hook, health);
159         gren.damageforcescale = WEP_CVAR_SEC(hook, damageforcescale);
160         gren.event_damage = W_Hook_Damage;
161         gren.damagedbycontents = true;
162         gren.missile_flags = MIF_SPLASH | MIF_ARC;
163
164         gren.velocity = '0 0 1' * WEP_CVAR_SEC(hook, speed);
165         if(autocvar_g_projectiles_newton_style)
166                 gren.velocity = gren.velocity + self.velocity;
167
168         gren.gravity = WEP_CVAR_SEC(hook, gravity);
169         //W_SetupProjVelocity_Basic(gren); // just falling down!
170
171         gren.angles = '0 0 0';
172         gren.flags = FL_PROJECTILE;
173
174         CSQCProjectile(gren, true, PROJECTILE_HOOKBOMB, true);
175
176         MUTATOR_CALLHOOK(EditProjectile, self, gren);
177 }
178
179                 METHOD(Hook, wr_aim, bool(entity thiswep))
180                 {
181                         // no bot AI for hook (yet?)
182                         return true;
183                 }
184                 METHOD(Hook, wr_think, bool(entity thiswep, bool fire1, bool fire2))
185                 {
186                         if(fire1 || self.BUTTON_HOOK)
187                         {
188                                 if(!self.hook)
189                                 if(!(self.hook_state & HOOK_WAITING_FOR_RELEASE))
190                                 if(!(self.hook_state & HOOK_FIRING))
191                                 if(time > self.hook_refire)
192                                 if(weapon_prepareattack(false, -1))
193                                 {
194                                         W_DecreaseAmmo(thiswep, WEP_CVAR_PRI(hook, ammo));
195                                         self.hook_state |= HOOK_FIRING;
196                                         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_PRI(hook, animtime), w_ready);
197                                 }
198                         }
199
200                         if(fire2)
201                         {
202                                 if(weapon_prepareattack(true, WEP_CVAR_SEC(hook, refire)))
203                                 {
204                                         W_Hook_Attack2(thiswep);
205                                         weapon_thinkf(WFRAME_FIRE2, WEP_CVAR_SEC(hook, animtime), w_ready);
206                                 }
207                         }
208
209                         if(self.hook)
210                         {
211                                 // if hooked, no bombs, and increase the timer
212                                 self.hook_refire = max(self.hook_refire, time + WEP_CVAR_PRI(hook, refire) * W_WeaponRateFactor());
213
214                                 // hook also inhibits health regeneration, but only for 1 second
215                                 if(!(self.items & IT_UNLIMITED_WEAPON_AMMO))
216                                         self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
217                         }
218
219                         if(self.hook && self.hook.state == 1)
220                         {
221                                 float hooked_time_max = WEP_CVAR_PRI(hook, hooked_time_max);
222                                 if(hooked_time_max > 0)
223                                 {
224                                         if( time > self.hook_time_hooked + hooked_time_max )
225                                                 self.hook_state |= HOOK_REMOVING;
226                                 }
227
228                                 float hooked_fuel = WEP_CVAR_PRI(hook, hooked_ammo);
229                                 if(hooked_fuel > 0)
230                                 {
231                                         if( time > self.hook_time_fueldecrease )
232                                         {
233                                                 if(!(self.items & IT_UNLIMITED_WEAPON_AMMO))
234                                                 {
235                                                         if( self.ammo_fuel >= (time - self.hook_time_fueldecrease) * hooked_fuel )
236                                                         {
237                                                                 W_DecreaseAmmo(thiswep, (time - self.hook_time_fueldecrease) * hooked_fuel);
238                                                                 self.hook_time_fueldecrease = time;
239                                                                 // decrease next frame again
240                                                         }
241                                                         else
242                                                         {
243                                                                 self.ammo_fuel = 0;
244                                                                 self.hook_state |= HOOK_REMOVING;
245                                                                 W_SwitchWeapon_Force(self, w_getbestweapon(self));
246                                                         }
247                                                 }
248                                         }
249                                 }
250                         }
251                         else
252                         {
253                                 self.hook_time_hooked = time;
254                                 self.hook_time_fueldecrease = time + WEP_CVAR_PRI(hook, hooked_time_free);
255                         }
256
257                         if(self.BUTTON_CROUCH)
258                         {
259                                 self.hook_state &= ~HOOK_PULLING;
260                                 if(fire1 || self.BUTTON_HOOK)
261                                         self.hook_state &= ~HOOK_RELEASING;
262                                 else
263                                         self.hook_state |= HOOK_RELEASING;
264                         }
265                         else
266                         {
267                                 self.hook_state |= HOOK_PULLING;
268                                 self.hook_state &= ~HOOK_RELEASING;
269
270                                 if(fire1 || self.BUTTON_HOOK)
271                                 {
272                                         // already fired
273                                         if(self.hook)
274                                                 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
275                                 }
276                                 else
277                                 {
278                                         self.hook_state |= HOOK_REMOVING;
279                                         self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
280                                 }
281                         }
282
283                         _GrapplingHookFrame();
284
285                         return true;
286                 }
287                 METHOD(Hook, wr_init, bool(entity thiswep))
288                 {
289                         HOOK_SETTINGS(WEP_SKIP_CVAR, WEP_SET_PROP);
290                         return true;
291                 }
292                 METHOD(Hook, wr_setup, bool(entity thiswep))
293                 {
294                         self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
295                         return true;
296                 }
297                 METHOD(Hook, wr_checkammo1, bool(entity thiswep))
298                 {
299                         if(self.hook)
300                                 return self.ammo_fuel > 0;
301                         else
302                                 return self.ammo_fuel >= WEP_CVAR_PRI(hook, ammo);
303                 }
304                 METHOD(Hook, wr_checkammo2, bool(entity thiswep))
305                 {
306                         // infinite ammo for now
307                         return true; // self.ammo_cells >= WEP_CVAR_SEC(hook, ammo); // WEAPONTODO: see above
308                 }
309                 METHOD(Hook, wr_config, bool(entity thiswep))
310                 {
311                         HOOK_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS);
312                         return true;
313                 }
314                 METHOD(Hook, wr_resetplayer, bool(entity thiswep))
315                 {
316                         self.hook_refire = time;
317                         return true;
318                 }
319                 METHOD(Hook, wr_suicidemessage, bool(entity thiswep))
320                 {
321                         return false;
322                 }
323                 METHOD(Hook, wr_killmessage, bool(entity thiswep))
324                 {
325                         return WEAPON_HOOK_MURDER;
326                 }
327
328 #endif
329 #ifdef CSQC
330
331                 METHOD(Hook, wr_impacteffect, bool(entity thiswep))
332                 {
333                         vector org2;
334                         org2 = w_org + w_backoff * 2;
335                         pointparticles(particleeffectnum(EFFECT_HOOK_EXPLODE), org2, '0 0 0', 1);
336                         if(!w_issilent)
337                                 sound(self, CH_SHOTS, SND_HOOKBOMB_IMPACT, VOL_BASE, ATTN_NORM);
338
339                         return true;
340                 }
341                 METHOD(Hook, wr_init, bool(entity thiswep))
342                 {
343                         return true;
344                 }
345                 METHOD(Hook, wr_zoomreticle, bool(entity thiswep))
346                 {
347                         // no weapon specific image for this weapon
348                         return false;
349                 }
350
351 #endif
352 #endif