properly set the HITTYPE_BOUNCE flag on all timeout detonations
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_hook.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(HOOK, w_hook, IT_CELLS|IT_FUEL, 0, WEP_FLAG_CANCLIMB | WEP_TYPE_SPLASH, 0, "hookgun", "hook", "Grappling Hook");
3 #else
4 .float dmg;
5 .float dmg_edge;
6 .float dmg_radius;
7 .float dmg_force;
8 .float dmg_power;
9 .float dmg_duration;
10 .float dmg_last;
11 .float hook_refire;
12 .float hook_time_hooked;
13 .float hook_time_fueldecrease;
14
15 void W_Hook_ExplodeThink (void)
16 {
17         float dt, dmg_remaining_next, f;
18
19         dt = time - self.teleport_time;
20         dmg_remaining_next = pow(bound(0, 1 - dt / self.dmg_duration, 1), self.dmg_power);
21
22         f = self.dmg_last - dmg_remaining_next;
23         self.dmg_last = dmg_remaining_next;
24
25         RadiusDamage (self, self.owner, self.dmg * f, self.dmg_edge * f, self.dmg_radius, self.owner, self.dmg_force * f, self.projectiledeathtype, world);
26         self.projectiledeathtype |= HITTYPE_BOUNCE;
27         //RadiusDamage (self, world, self.dmg * f, self.dmg_edge * f, self.dmg_radius, world, self.dmg_force * f, self.projectiledeathtype, world);
28
29         if(dt < self.dmg_duration)
30                 self.nextthink = time + 0.05; // soon
31         else
32                 remove(self);
33 }
34
35 void W_Hook_Explode2 (void)
36 {
37         self.event_damage = SUB_Null;
38         self.touch = SUB_Null;
39         self.effects |= EF_NODRAW;
40
41         self.think = W_Hook_ExplodeThink;
42         self.nextthink = time;
43         self.dmg = cvar("g_balance_hook_secondary_damage");
44         self.dmg_edge = cvar("g_balance_hook_secondary_edgedamage");
45         self.dmg_radius = cvar("g_balance_hook_secondary_radius");
46         self.dmg_force = cvar("g_balance_hook_secondary_force");
47         self.dmg_power = cvar("g_balance_hook_secondary_power");
48         self.dmg_duration = cvar("g_balance_hook_secondary_duration");
49         self.teleport_time = time;
50         self.dmg_last = 1;
51         self.movetype = MOVETYPE_NONE;
52 }
53
54 void W_Hook_Touch2 (void)
55 {
56         PROJECTILE_TOUCH;
57         self.use();
58 }
59
60 void W_Hook_Attack2()
61 {
62         local entity gren;
63
64         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
65                 self.ammo_cells = self.ammo_cells - cvar("g_balance_hook_secondary_ammo");
66         W_SetupShot (self, FALSE, 4, "weapons/hookbomb_fire.wav", cvar("g_balance_hook_secondary_damage"));
67
68         gren = spawn ();
69         gren.owner = self;
70         gren.classname = "hookbomb";
71         gren.bot_dodge = TRUE;
72         gren.bot_dodgerating = cvar("g_balance_hook_secondary_damage");
73         gren.movetype = MOVETYPE_TOSS;
74         PROJECTILE_MAKETRIGGER(gren);
75         gren.projectiledeathtype = WEP_HOOK | HITTYPE_SECONDARY;
76         setorigin(gren, w_shotorg);
77         setsize(gren, '0 0 0', '0 0 0');
78
79         gren.nextthink = time + cvar("g_balance_hook_secondary_lifetime");
80         gren.think = adaptor_think2use_hittype_splash;
81         gren.use = W_Hook_Explode2;
82         gren.touch = W_Hook_Touch2;
83
84         gren.velocity = '0 0 1' * cvar("g_balance_hook_secondary_speed");
85         if(cvar("g_projectiles_newton_style"))
86                 gren.velocity = gren.velocity + self.velocity;
87
88         gren.gravity = cvar("g_balance_hook_secondary_gravity");
89         //W_SetupProjectileVelocity(gren); // just falling down!
90
91         gren.angles = '0 0 0';
92         gren.flags = FL_PROJECTILE;
93
94         CSQCProjectile(gren, TRUE, PROJECTILE_HOOKBOMB, TRUE);
95 }
96
97 void spawnfunc_weapon_hook (void)
98 {
99         if(g_grappling_hook) // offhand hook
100         {
101                 startitem_failed = TRUE;
102                 remove(self);
103                 return;
104         }
105         weapon_defaultspawnfunc(WEP_HOOK);
106 }
107
108 float w_hook(float req)
109 {
110         float hooked_time_max, hooked_fuel;
111                 
112         if (req == WR_AIM)
113         {
114                 // ... sorry ...
115         }
116         else if (req == WR_THINK)
117         {
118                 if (self.BUTTON_ATCK || (!(self.items & IT_JETPACK) && self.BUTTON_HOOK))
119                 {
120                         if(!self.hook)
121                         if not(self.hook_state & HOOK_WAITING_FOR_RELEASE)
122                         if not(self.hook_state & HOOK_FIRING)
123                         if (time > self.hook_refire)
124                         if (weapon_prepareattack(0, -1))
125                         {
126                                 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
127                                         self.ammo_fuel = self.ammo_fuel - cvar("g_balance_hook_primary_fuel");
128                                 self.hook_state |= HOOK_FIRING;
129                                 weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_hook_primary_animtime"), w_ready);                          
130                         }
131                 }
132
133                 if (self.BUTTON_ATCK2)
134                 {
135                         if (weapon_prepareattack(1, cvar("g_balance_hook_secondary_refire")))
136                         {
137                                 W_Hook_Attack2();
138                                 weapon_thinkf(WFRAME_FIRE2, cvar("g_balance_hook_secondary_animtime"), w_ready);
139                         }
140                 }
141
142                 if(self.hook)
143                 {
144                         // if hooked, no bombs, and increase the timer
145                         self.hook_refire = max(self.hook_refire, time + cvar("g_balance_hook_primary_refire"));
146
147                         // hook also inhibits health regeneration, but only for 1 second
148                         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
149                                 self.pauseregen_finished = max(self.pauseregen_finished, time + cvar("g_balance_pause_fuel_regen"));
150                 }
151
152                 if(self.hook && self.hook.state == 1)
153                 {
154                         hooked_time_max = cvar("g_balance_hook_primary_hooked_time_max");                       
155                         if (hooked_time_max > 0)
156                         {
157                                 if ( time > self.hook_time_hooked + hooked_time_max )
158                                         self.hook_state |= HOOK_REMOVING;
159                         }
160                         
161                         hooked_fuel = cvar("g_balance_hook_primary_hooked_fuel");
162                         if (hooked_fuel > 0)
163                         {
164                                 if ( time > self.hook_time_fueldecrease )
165                                 {
166                                         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
167                                         {
168                                                 if ( self.ammo_fuel >= (time - self.hook_time_fueldecrease) * hooked_fuel )
169                                                 {
170                                                         self.ammo_fuel -= (time - self.hook_time_fueldecrease) * hooked_fuel;
171                                                         self.hook_time_fueldecrease = time;
172                                                         // decrease next frame again
173                                                 }
174                                                 else
175                                                 {
176                                                         self.ammo_fuel = 0;
177                                                         self.hook_state |= HOOK_REMOVING;
178                                                         W_SwitchWeapon_Force(self, w_getbestweapon(self));
179                                                 }
180                                         }
181                                 }
182                         }
183                 }
184                 else
185                 {
186                         self.hook_time_hooked = time;                           
187                         self.hook_time_fueldecrease = time + cvar("g_balance_hook_primary_hooked_time_free");
188                 }
189
190                 if (self.BUTTON_CROUCH)
191                 {
192                         self.hook_state &~= HOOK_PULLING;
193                         if (self.BUTTON_ATCK || (!(self.items & IT_JETPACK) && self.BUTTON_HOOK))
194                                 self.hook_state &~= HOOK_RELEASING;
195                         else
196                                 self.hook_state |= HOOK_RELEASING;
197                 }
198                 else
199                 {
200                         self.hook_state |= HOOK_PULLING;
201                         self.hook_state &~= HOOK_RELEASING;
202
203                         if (self.BUTTON_ATCK || (!(self.items & IT_JETPACK) && self.BUTTON_HOOK))
204                         {
205                                 // already fired
206                                 if(self.hook)
207                                         self.hook_state |= HOOK_WAITING_FOR_RELEASE;
208                         }
209                         else
210                         {
211                                 self.hook_state |= HOOK_REMOVING;
212                                 self.hook_state &~= HOOK_WAITING_FOR_RELEASE;
213                         }
214                 }
215         }
216         else if (req == WR_PRECACHE)
217         {
218                 precache_model ("models/weapons/g_hookgun.md3");
219                 precache_model ("models/weapons/v_hookgun.md3");
220                 precache_model ("models/weapons/h_hookgun.iqm");
221                 precache_sound ("weapons/hook_impact.wav"); // done by g_hook.qc
222                 precache_sound ("weapons/hook_fire.wav");
223                 precache_sound ("weapons/hookbomb_fire.wav");
224         }
225         else if (req == WR_SETUP)
226         {
227                 weapon_setup(WEP_HOOK);
228                 self.hook_state &~= HOOK_WAITING_FOR_RELEASE;
229         }
230         else if (req == WR_CHECKAMMO1)
231         {
232                 if(self.hook)
233                         return self.ammo_fuel > 0;
234                 else
235                         return self.ammo_fuel >= cvar("g_balance_hook_primary_fuel");
236         }
237         else if (req == WR_CHECKAMMO2)
238         {
239                 return self.ammo_cells >= cvar("g_balance_hook_secondary_ammo");
240         }
241         else if (req == WR_SUICIDEMESSAGE)
242                 w_deathtypestring = "did the impossible";
243         else if (req == WR_KILLMESSAGE)
244                 w_deathtypestring = "has run into #'s gravity bomb";
245         else if (req == WR_RESETPLAYER)
246         {
247                 self.hook_refire = time;
248         }
249         return TRUE;
250 };
251 #endif