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