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