2b639024488cce4a96a914739c3fa779e29c632b
[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
98 void spawnfunc_weapon_hook (void)
99 {
100         if(g_grappling_hook) // offhand hook
101         {
102                 startitem_failed = TRUE;
103                 remove(self);
104                 return;
105         }
106         weapon_defaultspawnfunc(WEP_HOOK);
107 }
108
109 float w_hook(float req)
110 {
111         float hooked_time_max, hooked_fuel;
112                 
113         if (req == WR_AIM)
114         {
115                 // ... sorry ...
116         }
117         else if (req == WR_THINK)
118         {
119                 if (self.BUTTON_ATCK || (!(self.items & IT_JETPACK) && self.BUTTON_HOOK))
120                 {
121                         if(!self.hook)
122                         if not(self.hook_state & HOOK_WAITING_FOR_RELEASE)
123                         if not(self.hook_state & HOOK_FIRING)
124                         if (time > self.hook_refire)
125                         if (weapon_prepareattack(0, -1))
126                         {
127                                 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
128                                         self.ammo_fuel = self.ammo_fuel - cvar("g_balance_hook_primary_fuel");
129                                 self.hook_state |= HOOK_FIRING;
130                                 weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_hook_primary_animtime"), w_ready);                          
131                         }
132                 }
133
134                 if (self.BUTTON_ATCK2)
135                 {
136                         if (weapon_prepareattack(1, cvar("g_balance_hook_secondary_refire")))
137                         {
138                                 W_Hook_Attack2();
139                                 weapon_thinkf(WFRAME_FIRE2, cvar("g_balance_hook_secondary_animtime"), w_ready);
140                         }
141                 }
142
143                 if(self.hook)
144                 {
145                         // if hooked, no bombs, and increase the timer
146                         self.hook_refire = max(self.hook_refire, time + cvar("g_balance_hook_primary_refire"));
147
148                         // hook also inhibits health regeneration, but only for 1 second
149                         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
150                                 self.pauseregen_finished = max(self.pauseregen_finished, time + cvar("g_balance_pause_fuel_regen"));
151                 }
152
153                 if(self.hook && self.hook.state == 1)
154                 {
155                         hooked_time_max = cvar("g_balance_hook_primary_hooked_time_max");                       
156                         if (hooked_time_max > 0)
157                         {
158                                 if ( time > self.hook_time_hooked + hooked_time_max )
159                                         self.hook_state |= HOOK_REMOVING;
160                         }
161                         
162                         hooked_fuel = cvar("g_balance_hook_primary_hooked_fuel");
163                         if (hooked_fuel > 0)
164                         {
165                                 if ( time > self.hook_time_fueldecrease )
166                                 {
167                                         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
168                                         {
169                                                 if ( self.ammo_fuel >= (time - self.hook_time_fueldecrease) * hooked_fuel )
170                                                 {
171                                                         self.ammo_fuel -= (time - self.hook_time_fueldecrease) * hooked_fuel;
172                                                         self.hook_time_fueldecrease = time;
173                                                         // decrease next frame again
174                                                 }
175                                                 else
176                                                 {
177                                                         self.ammo_fuel = 0;
178                                                         self.hook_state |= HOOK_REMOVING;
179                                                         W_SwitchWeapon_Force(self, w_getbestweapon(self));
180                                                 }
181                                         }
182                                 }
183                         }
184                 }
185                 else
186                 {
187                         self.hook_time_hooked = time;                           
188                         self.hook_time_fueldecrease = time + cvar("g_balance_hook_primary_hooked_time_free");
189                 }
190
191                 if (self.BUTTON_CROUCH)
192                 {
193                         self.hook_state &~= HOOK_PULLING;
194                         if (self.BUTTON_ATCK || (!(self.items & IT_JETPACK) && self.BUTTON_HOOK))
195                                 self.hook_state &~= HOOK_RELEASING;
196                         else
197                                 self.hook_state |= HOOK_RELEASING;
198                 }
199                 else
200                 {
201                         self.hook_state |= HOOK_PULLING;
202                         self.hook_state &~= HOOK_RELEASING;
203
204                         if (self.BUTTON_ATCK || (!(self.items & IT_JETPACK) && self.BUTTON_HOOK))
205                         {
206                                 // already fired
207                                 if(self.hook)
208                                         self.hook_state |= HOOK_WAITING_FOR_RELEASE;
209                         }
210                         else
211                         {
212                                 self.hook_state |= HOOK_REMOVING;
213                                 self.hook_state &~= HOOK_WAITING_FOR_RELEASE;
214                         }
215                 }
216         }
217         else if (req == WR_PRECACHE)
218         {
219                 precache_model ("models/weapons/g_hookgun.md3");
220                 precache_model ("models/weapons/v_hookgun.md3");
221                 precache_model ("models/weapons/h_hookgun.iqm");
222                 precache_sound ("weapons/hook_impact.wav"); // done by g_hook.qc
223                 precache_sound ("weapons/hook_fire.wav");
224                 precache_sound ("weapons/hookbomb_fire.wav");
225         }
226         else if (req == WR_SETUP)
227         {
228                 weapon_setup(WEP_HOOK);
229                 self.hook_state &~= HOOK_WAITING_FOR_RELEASE;
230         }
231         else if (req == WR_CHECKAMMO1)
232         {
233                 if(self.hook)
234                         return self.ammo_fuel > 0;
235                 else
236                         return self.ammo_fuel >= cvar("g_balance_hook_primary_fuel");
237         }
238         else if (req == WR_CHECKAMMO2)
239         {
240                 return self.ammo_cells >= cvar("g_balance_hook_secondary_ammo");
241         }
242         else if (req == WR_SUICIDEMESSAGE)
243                 w_deathtypestring = "did the impossible";
244         else if (req == WR_KILLMESSAGE)
245                 w_deathtypestring = "has run into #'s gravity bomb";
246         else if (req == WR_RESETPLAYER)
247         {
248                 self.hook_refire = time;
249         }
250         return TRUE;
251 };
252 #endif
253 #ifdef CSQC
254 float w_hook(float req)
255 {
256         return TRUE;
257 }
258 #endif
259 #endif