]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/w_hook.qc
Rename the macros so that they're a little easier to understand
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / w_hook.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(
3 /* WEP_##id */ HOOK,
4 /* function */ W_Hook,
5 /* ammotype */ ammo_fuel,
6 /* impulse  */ 0,
7 /* flags    */ WEP_FLAG_CANCLIMB | WEP_TYPE_SPLASH,
8 /* rating   */ 0,
9 /* color        */ '0 0.5 0',
10 /* model    */ "hookgun",
11 /* netname  */ "hook",
12 /* fullname */ _("Grappling Hook")
13 );
14
15 #define HOOK_SETTINGS(w_cvar,w_prop) HOOK_SETTINGS_LIST(w_cvar, w_prop, HOOK, hook)
16 #define HOOK_SETTINGS_LIST(w_cvar,w_prop,id,sn) \
17         w_cvar(id, sn, BOTH, animtime) \
18         w_cvar(id, sn, BOTH, refire) \
19         w_cvar(id, sn, PRI,  ammo) \
20         w_cvar(id, sn, PRI,  hooked_ammo) \
21         w_cvar(id, sn, PRI,  hooked_time_free) \
22         w_cvar(id, sn, PRI,  hooked_time_max) \
23         w_cvar(id, sn, SEC,  damage) \
24         w_cvar(id, sn, SEC,  duration) \
25         w_cvar(id, sn, SEC,  edgedamage) \
26         w_cvar(id, sn, SEC,  force) \
27         w_cvar(id, sn, SEC,  gravity) \
28         w_cvar(id, sn, SEC,  lifetime) \
29         w_cvar(id, sn, SEC,  power) \
30         w_cvar(id, sn, SEC,  radius) \
31         w_cvar(id, sn, SEC,  speed) \
32         w_cvar(id, sn, SEC,  health) \
33         w_cvar(id, sn, SEC,  damageforcescale) \
34         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
35         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
36         w_prop(id, sn, string, weaponreplace, weaponreplace) \
37         w_prop(id, sn, float,  weaponstart, weaponstart) \
38         w_prop(id, sn, float,  weaponstartoverride, weaponstartoverride)
39
40 #ifdef SVQC
41 HOOK_SETTINGS(WEP_ADD_CVAR, WEP_ADD_PROP)
42
43 .float dmg;
44 .float dmg_edge;
45 .float dmg_radius;
46 .float dmg_force;
47 .float dmg_power;
48 .float dmg_duration;
49 .float dmg_last;
50 .float hook_refire;
51 .float hook_time_hooked;
52 .float hook_time_fueldecrease;
53 #endif
54 #else
55 #ifdef SVQC
56
57 void spawnfunc_weapon_hook()
58 {
59         if(g_grappling_hook) // offhand hook
60         {
61                 startitem_failed = TRUE;
62                 remove(self);
63                 return;
64         }
65         weapon_defaultspawnfunc(WEP_HOOK);
66 }
67
68 void W_Hook_ExplodeThink (void)
69 {
70         float dt, dmg_remaining_next, f;
71
72         dt = time - self.teleport_time;
73         dmg_remaining_next = pow(bound(0, 1 - dt / self.dmg_duration, 1), self.dmg_power);
74
75         f = self.dmg_last - dmg_remaining_next;
76         self.dmg_last = dmg_remaining_next;
77
78         RadiusDamage (self, self.realowner, self.dmg * f, self.dmg_edge * f, self.dmg_radius, self.realowner, world, self.dmg_force * f, self.projectiledeathtype, world);
79         self.projectiledeathtype |= HITTYPE_BOUNCE;
80         //RadiusDamage (self, world, self.dmg * f, self.dmg_edge * f, self.dmg_radius, world, world, self.dmg_force * f, self.projectiledeathtype, world);
81
82         if(dt < self.dmg_duration)
83                 self.nextthink = time + 0.05; // soon
84         else
85                 remove(self);
86 }
87
88 void W_Hook_Explode2 (void)
89 {
90         self.event_damage = func_null;
91         self.touch = func_null;
92         self.effects |= EF_NODRAW;
93
94         self.think = W_Hook_ExplodeThink;
95         self.nextthink = time;
96         self.dmg = WEP_CVAR_SEC(hook, damage);
97         self.dmg_edge = WEP_CVAR_SEC(hook, edgedamage);
98         self.dmg_radius = WEP_CVAR_SEC(hook, radius);
99         self.dmg_force = WEP_CVAR_SEC(hook, force);
100         self.dmg_power = WEP_CVAR_SEC(hook, power);
101         self.dmg_duration = WEP_CVAR_SEC(hook, duration);
102         self.teleport_time = time;
103         self.dmg_last = 1;
104         self.movetype = MOVETYPE_NONE;
105 }
106
107 void W_Hook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
108 {
109         if (self.health <= 0)
110                 return;
111                 
112         if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
113                 return; // g_projectiles_damage says to halt    
114         
115         self.health = self.health - damage;
116         
117         if (self.health <= 0)
118                 W_PrepareExplosionByDamage(self.realowner, W_Hook_Explode2);
119 }
120
121 void W_Hook_Touch2 (void)
122 {
123         PROJECTILE_TOUCH;
124         self.use();
125 }
126
127 void W_Hook_Attack2()
128 {
129         entity gren;
130
131         //W_DecreaseAmmo(WEP_CVAR_SEC(hook, ammo)); // WEAPONTODO: Figure out how to handle ammo with hook secondary (gravitybomb)
132         W_SetupShot (self, FALSE, 4, "weapons/hookbomb_fire.wav", CH_WEAPON_A, WEP_CVAR_SEC(hook, damage));
133
134         gren = spawn ();
135         gren.owner = gren.realowner = self;
136         gren.classname = "hookbomb";
137         gren.bot_dodge = TRUE;
138         gren.bot_dodgerating = WEP_CVAR_SEC(hook, damage);
139         gren.movetype = MOVETYPE_TOSS;
140         PROJECTILE_MAKETRIGGER(gren);
141         gren.projectiledeathtype = WEP_HOOK | HITTYPE_SECONDARY;
142         setorigin(gren, w_shotorg);
143         setsize(gren, '0 0 0', '0 0 0');
144
145         gren.nextthink = time + WEP_CVAR_SEC(hook, lifetime);
146         gren.think = adaptor_think2use_hittype_splash;
147         gren.use = W_Hook_Explode2;
148         gren.touch = W_Hook_Touch2;
149         
150         gren.takedamage = DAMAGE_YES;
151         gren.health = WEP_CVAR_SEC(hook, health);
152         gren.damageforcescale = WEP_CVAR_SEC(hook, damageforcescale);
153         gren.event_damage = W_Hook_Damage;
154         gren.damagedbycontents = TRUE;
155         gren.missile_flags = MIF_SPLASH | MIF_ARC;
156
157         gren.velocity = '0 0 1' * WEP_CVAR_SEC(hook, speed);
158         if(autocvar_g_projectiles_newton_style)
159                 gren.velocity = gren.velocity + self.velocity;
160
161         gren.gravity = WEP_CVAR_SEC(hook, gravity);
162         //W_SetupVelocity_Explicit(gren); // just falling down!
163
164         gren.angles = '0 0 0';
165         gren.flags = FL_PROJECTILE;
166
167         CSQCProjectile(gren, TRUE, PROJECTILE_HOOKBOMB, TRUE);
168
169         other = gren; MUTATOR_CALLHOOK(EditProjectile);
170 }
171
172 float W_Hook(float req)
173 {
174         float hooked_time_max, hooked_fuel;
175                 
176         switch(req)
177         {
178                 case WR_AIM:
179                 {
180                         // no bot AI for hook (yet?)
181                         return TRUE;
182                 }
183                 case WR_THINK:
184                 {
185                         if (self.BUTTON_ATCK || (!(self.items & IT_JETPACK) && self.BUTTON_HOOK))
186                         {
187                                 if(!self.hook)
188                                 if(!(self.hook_state & HOOK_WAITING_FOR_RELEASE))
189                                 if(!(self.hook_state & HOOK_FIRING))
190                                 if (time > self.hook_refire)
191                                 if (weapon_prepareattack(0, -1))
192                                 {
193                                         W_DecreaseAmmo(WEP_CVAR_PRI(hook, ammo));
194                                         self.hook_state |= HOOK_FIRING;
195                                         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_PRI(hook, animtime), w_ready);                             
196                                 }
197                         }
198
199                         if (self.BUTTON_ATCK2)
200                         {
201                                 if (weapon_prepareattack(1, WEP_CVAR_SEC(hook, refire)))
202                                 {
203                                         W_Hook_Attack2();
204                                         weapon_thinkf(WFRAME_FIRE2, WEP_CVAR_SEC(hook, animtime), w_ready);
205                                 }
206                         }
207
208                         if(self.hook)
209                         {
210                                 // if hooked, no bombs, and increase the timer
211                                 self.hook_refire = max(self.hook_refire, time + WEP_CVAR_PRI(hook, refire) * W_WeaponRateFactor());
212
213                                 // hook also inhibits health regeneration, but only for 1 second
214                                 if(!(self.items & IT_UNLIMITED_WEAPON_AMMO))
215                                         self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
216                         }
217
218                         if(self.hook && self.hook.state == 1)
219                         {
220                                 hooked_time_max = WEP_CVAR_PRI(hook, hooked_time_max);                  
221                                 if (hooked_time_max > 0)
222                                 {
223                                         if ( time > self.hook_time_hooked + hooked_time_max )
224                                                 self.hook_state |= HOOK_REMOVING;
225                                 }
226                                 
227                                 hooked_fuel = WEP_CVAR_PRI(hook, hooked_ammo);
228                                 if (hooked_fuel > 0)
229                                 {
230                                         if ( time > self.hook_time_fueldecrease )
231                                         {
232                                                 if(!(self.items & IT_UNLIMITED_WEAPON_AMMO))
233                                                 {
234                                                         if ( self.ammo_fuel >= (time - self.hook_time_fueldecrease) * hooked_fuel )
235                                                         {
236                                                                 W_DecreaseAmmo((time - self.hook_time_fueldecrease) * hooked_fuel);
237                                                                 self.hook_time_fueldecrease = time;
238                                                                 // decrease next frame again
239                                                         }
240                                                         else
241                                                         {
242                                                                 self.ammo_fuel = 0;
243                                                                 self.hook_state |= HOOK_REMOVING;
244                                                                 W_SwitchWeapon_Force(self, w_getbestweapon(self));
245                                                         }
246                                                 }
247                                         }
248                                 }
249                         }
250                         else
251                         {
252                                 self.hook_time_hooked = time;                           
253                                 self.hook_time_fueldecrease = time + WEP_CVAR_PRI(hook, hooked_time_free);
254                         }
255
256                         if (self.BUTTON_CROUCH)
257                         {
258                                 self.hook_state &= ~HOOK_PULLING;
259                                 if (self.BUTTON_ATCK || (!(self.items & IT_JETPACK) && self.BUTTON_HOOK))
260                                         self.hook_state &= ~HOOK_RELEASING;
261                                 else
262                                         self.hook_state |= HOOK_RELEASING;
263                         }
264                         else
265                         {
266                                 self.hook_state |= HOOK_PULLING;
267                                 self.hook_state &= ~HOOK_RELEASING;
268
269                                 if (self.BUTTON_ATCK || (!(self.items & IT_JETPACK) && self.BUTTON_HOOK))
270                                 {
271                                         // already fired
272                                         if(self.hook)
273                                                 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
274                                 }
275                                 else
276                                 {
277                                         self.hook_state |= HOOK_REMOVING;
278                                         self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
279                                 }
280                         }
281                         
282                         return TRUE;
283                 }
284                 case WR_INIT:
285                 {
286                         precache_model ("models/weapons/g_hookgun.md3");
287                         precache_model ("models/weapons/v_hookgun.md3");
288                         precache_model ("models/weapons/h_hookgun.iqm");
289                         precache_sound ("weapons/hook_impact.wav"); // done by g_hook.qc
290                         precache_sound ("weapons/hook_fire.wav");
291                         precache_sound ("weapons/hookbomb_fire.wav");
292                         HOOK_SETTINGS(WEP_SKIPCVAR, WEP_SET_PROP)
293                         return TRUE;
294                 }
295                 case WR_SETUP:
296                 {
297                         self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
298                         return TRUE;
299                 }
300                 case WR_CHECKAMMO1:
301                 {
302                         if(self.hook)
303                                 return self.ammo_fuel > 0;
304                         else
305                                 return self.ammo_fuel >= WEP_CVAR_PRI(hook, ammo);
306                 }
307                 case WR_CHECKAMMO2:
308                 {
309                         // infinite ammo for now
310                         return TRUE; // self.ammo_cells >= WEP_CVAR_SEC(hook, ammo); // WEAPONTODO: see above
311                 }
312                 case WR_CONFIG:
313                 {
314                         HOOK_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS)
315                         return TRUE;
316                 }
317                 case WR_RESETPLAYER:
318                 {
319                         self.hook_refire = time;
320                         return TRUE;
321                 }
322                 case WR_SUICIDEMESSAGE:
323                 {
324                         return FALSE;
325                 }
326                 case WR_KILLMESSAGE:
327                 {
328                         return WEAPON_HOOK_MURDER;
329                 }
330         }
331         return TRUE;
332 }
333 #endif
334 #ifdef CSQC
335 float W_Hook(float req)
336 {
337         switch(req)
338         {
339                 case WR_IMPACTEFFECT:
340                 {
341                         vector org2;
342                         org2 = w_org + w_backoff * 2;
343                         pointparticles(particleeffectnum("hookbomb_explode"), org2, '0 0 0', 1);
344                         if(!w_issilent)
345                                 sound(self, CH_SHOTS, "weapons/hookbomb_impact.wav", VOL_BASE, ATTN_NORM);
346                                 
347                         return TRUE;
348                 }
349                 case WR_INIT:
350                 {
351                         precache_sound("weapons/hookbomb_impact.wav");
352                         return TRUE;
353                 }
354         }
355         return TRUE;
356 }
357 #endif
358 #endif