Merge branch 'master' into Lyberta/StandaloneOverkillWeapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / overkill / oknex.qc
1 #include "oknex.qh"
2
3 #ifdef SVQC
4
5 .float oknex_lasthit;
6 #endif
7
8 #if defined(GAMEQC)
9
10 METHOD(OverkillNex, wr_glow, vector(OverkillNex this, entity actor, entity wepent))
11 {
12         if (!WEP_CVAR(oknex, charge)) return '0 0 0';
13         float charge = wepent.oknex_charge;
14         float animlimit = WEP_CVAR(oknex, charge_animlimit);
15         vector g;
16         g.x = autocvar_g_weapon_charge_colormod_hdrmultiplier * autocvar_g_weapon_charge_colormod_red_half * min(1, charge / animlimit);
17         g.y = autocvar_g_weapon_charge_colormod_hdrmultiplier * autocvar_g_weapon_charge_colormod_green_half * min(1, charge / animlimit);
18         g.z = autocvar_g_weapon_charge_colormod_hdrmultiplier * autocvar_g_weapon_charge_colormod_blue_half * min(1, charge / animlimit);
19         if (charge > animlimit)
20         {
21                 g.x += autocvar_g_weapon_charge_colormod_hdrmultiplier * autocvar_g_weapon_charge_colormod_red_full * (charge - animlimit) / (1 - animlimit);
22                 g.y += autocvar_g_weapon_charge_colormod_hdrmultiplier * autocvar_g_weapon_charge_colormod_green_full * (charge - animlimit) / (1 - animlimit);
23                 g.z += autocvar_g_weapon_charge_colormod_hdrmultiplier * autocvar_g_weapon_charge_colormod_blue_full * (charge - animlimit) / (1 - animlimit);
24         }
25         return g;
26 }
27 #endif
28
29 #ifdef SVQC
30 REGISTER_MUTATOR(oknex_charge, true);
31
32 MUTATOR_HOOKFUNCTION(oknex_charge, GetPressedKeys)
33 {
34         entity player = M_ARGV(0, entity);
35
36         // WEAPONTODO
37         if(!WEP_CVAR(oknex, charge) || !WEP_CVAR(oknex, charge_velocity_rate))
38                 return;
39
40         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
41         {
42                 .entity weaponentity = weaponentities[slot];
43
44                 if (player.(weaponentity).m_weapon == WEP_OVERKILL_NEX && WEP_CVAR(oknex, charge) && WEP_CVAR(oknex, charge_velocity_rate) && vdist(vec2(player.velocity), >, WEP_CVAR(oknex, charge_minspeed)))
45                 {
46                         float xyspeed = vlen(vec2(player.velocity));
47                         // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
48                                 xyspeed = min(xyspeed, WEP_CVAR(oknex, charge_maxspeed));
49                         float f = (xyspeed - WEP_CVAR(oknex, charge_minspeed)) / (WEP_CVAR(oknex, charge_maxspeed) - WEP_CVAR(oknex, charge_minspeed));
50                         // add the extra charge
51                         player.(weaponentity).oknex_charge = min(1, player.(weaponentity).oknex_charge + WEP_CVAR(oknex, charge_velocity_rate) * f * PHYS_INPUT_TIMELENGTH);
52                 }
53         }
54 }
55
56 void W_OverkillNex_Attack(Weapon thiswep, entity actor, .entity weaponentity, float issecondary)
57 {
58         float mydmg, myforce, mymindist, mymaxdist, myhalflife, myforcehalflife, myammo, charge;
59
60         mydmg = WEP_CVAR_BOTH(oknex, !issecondary, damage);
61         myforce = WEP_CVAR_BOTH(oknex, !issecondary, force);
62         mymindist = WEP_CVAR_BOTH(oknex, !issecondary, damagefalloff_mindist);
63         mymaxdist = WEP_CVAR_BOTH(oknex, !issecondary, damagefalloff_maxdist);
64         myhalflife = WEP_CVAR_BOTH(oknex, !issecondary, damagefalloff_halflife);
65         myforcehalflife = WEP_CVAR_BOTH(oknex, !issecondary, damagefalloff_forcehalflife);
66         myammo = WEP_CVAR_BOTH(oknex, !issecondary, ammo);
67
68         float flying;
69         flying = IsFlying(actor); // do this BEFORE to make the trace values from FireRailgunBullet last
70
71         if (WEP_CVAR(oknex, charge))
72         {
73                 charge = WEP_CVAR(oknex, charge_mindmg) / mydmg + (1 - WEP_CVAR(oknex, charge_mindmg) / mydmg) * actor.(weaponentity).oknex_charge;
74                 actor.(weaponentity).oknex_charge *= WEP_CVAR(oknex, charge_shot_multiplier); // do this AFTER setting mydmg/myforce
75                 // O RLY? -- divVerent
76                 // YA RLY -- FruitieX
77         }
78         else
79         {
80                 charge = 1;
81         }
82         mydmg *= charge;
83         myforce *= charge;
84
85         W_SetupShot(actor, weaponentity, true, 5, SND_NEXFIRE, CH_WEAPON_A, mydmg, WEP_OVERKILL_NEX.m_id);
86         if(charge > WEP_CVAR(oknex, charge_animlimit) && WEP_CVAR(oknex, charge_animlimit)) // if the OverkillNex is overcharged, we play an extra sound
87         {
88                 sound(actor, CH_WEAPON_B, SND_NEXCHARGE, VOL_BASE * (charge - 0.5 * WEP_CVAR(oknex, charge_animlimit)) / (1 - 0.5 * WEP_CVAR(oknex, charge_animlimit)), ATTN_NORM);
89         }
90
91         yoda = 0;
92         damage_goodhits = 0;
93         FireRailgunBullet(actor, weaponentity, w_shotorg, w_shotorg + w_shotdir * max_shot_distance, mydmg, myforce, mymindist, mymaxdist, myhalflife, myforcehalflife, WEP_OVERKILL_NEX.m_id);
94
95         if(yoda && flying)
96                 Send_Notification(NOTIF_ONE, actor, MSG_ANNCE, ANNCE_ACHIEVEMENT_YODA);
97         if(damage_goodhits && actor.oknex_lasthit)
98         {
99                 Send_Notification(NOTIF_ONE, actor, MSG_ANNCE, ANNCE_ACHIEVEMENT_IMPRESSIVE);
100                 damage_goodhits = 0; // only every second time
101         }
102
103         actor.oknex_lasthit = damage_goodhits;
104
105         //beam and muzzle flash done on client
106         SendCSQCVortexBeamParticle(charge);
107
108         W_DecreaseAmmo(thiswep, actor, myammo, weaponentity);
109 }
110
111 .float oknex_chargepool_pauseregen_finished;
112
113 METHOD(OverkillNex, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
114 {
115         if(bot_aim(actor, weaponentity, 1000000, 0, 1, false))
116                 PHYS_INPUT_BUTTON_ATCK(actor) = true;
117         else
118         {
119                 if(WEP_CVAR(oknex, charge))
120                         PHYS_INPUT_BUTTON_ATCK2(actor) = true;
121         }
122 }
123
124 METHOD(OverkillNex, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
125 {
126         if (WEP_CVAR(oknex, charge) && actor.(weaponentity).oknex_charge < WEP_CVAR(oknex, charge_limit))
127         {
128                 actor.(weaponentity).oknex_charge = min(1, actor.(weaponentity).oknex_charge + WEP_CVAR(oknex, charge_rate) * frametime / W_TICSPERFRAME);
129         }
130
131         if (WEP_CVAR_SEC(oknex, chargepool))
132                 if (actor.(weaponentity).oknex_chargepool_ammo < 1)
133                 {
134                         if (actor.oknex_chargepool_pauseregen_finished < time)
135                                 actor.(weaponentity).oknex_chargepool_ammo = min(1, actor.(weaponentity).oknex_chargepool_ammo + WEP_CVAR_SEC(oknex, chargepool_regen) * frametime / W_TICSPERFRAME);
136                         actor.pauseregen_finished = max(actor.pauseregen_finished, time + WEP_CVAR_SEC(oknex, chargepool_pause_regen));
137                 }
138
139         if ((WEP_CVAR_SEC(oknex, refire_type) == 1) && (fire & 2) && (time >= actor.jump_interval))
140         {
141                 // Secondary uses it's own refire timer if refire_type is 1.
142                 actor.jump_interval = time + WEP_CVAR_SEC(oknex, refire) * W_WeaponRateFactor(actor);
143                 BLASTER_SECONDARY_ATTACK(oknex, actor, weaponentity);
144                 if ((actor.(weaponentity).wframe == WFRAME_IDLE) ||
145                         (actor.(weaponentity).wframe == WFRAME_FIRE2))
146                 {
147                         // Set secondary fire animation.
148                         vector a = '0 0 0';
149                         actor.(weaponentity).wframe = WFRAME_FIRE2;
150                         a = actor.(weaponentity).anim_fire2;
151                         a.z *= g_weaponratefactor;
152                         FOREACH_CLIENT(true, LAMBDA(
153                                 if (it == actor || (IS_SPEC(it) && it.enemy == actor))
154                                 {
155                                         wframe_send(it, actor.(weaponentity), a, true);
156                                 }
157                         ));
158                         animdecide_setaction(actor, ANIMACTION_SHOOT, true);
159                 }
160         }
161
162         if (autocvar_g_balance_oknex_reload_ammo && actor.(weaponentity).clip_load < WEP_CVAR_PRI(oknex, ammo))
163         {
164                 // Rorced reload
165                 thiswep.wr_reload(thiswep, actor, weaponentity);
166                 return;
167         }
168         if (fire & 1) // Primary attack
169         {
170                 if (!weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_PRI(oknex, refire)))
171                 {
172                         return;
173                 }
174                 W_OverkillNex_Attack(thiswep, actor, weaponentity, 0);
175                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(oknex, animtime), w_ready);
176                 return;
177         }
178         if ((fire & 2) && (WEP_CVAR(oknex, secondary) == 2) && (WEP_CVAR_SEC(oknex, refire_type) == 0))
179         {
180                 // Secondary attack
181                 if (!weapon_prepareattack(thiswep, actor, weaponentity, true, WEP_CVAR_SEC(oknex, refire)))
182                 {
183                         return;
184                 }
185                 BLASTER_SECONDARY_ATTACK(oknex, actor, weaponentity);
186                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(oknex, animtime), w_ready);
187                 return;
188         }
189         //if ((WEP_CVAR(oknex, charge) && (WEP_CVAR(oknex, secondary) == 1)) ? (PHYS_INPUT_BUTTON_ZOOM(actor) | PHYS_INPUT_BUTTON_ZOOMSCRIPT(actor)) : (fire & 2))
190         //{
191         //      if(WEP_CVAR(oknex, charge))
192         //      {
193         //              actor.(weaponentity).oknex_charge_rottime = time + WEP_CVAR(oknex, charge_rot_pause);
194         //              float dt = frametime / W_TICSPERFRAME;
195         //
196         //              if(actor.(weaponentity).oknex_charge < 1)
197         //              {
198         //                      if(WEP_CVAR_SEC(oknex, chargepool))
199         //                      {
200         //                              if(WEP_CVAR_SEC(oknex, ammo))
201         //                              {
202         //                                      // always deplete if secondary is held
203         //                                      actor.(weaponentity).oknex_chargepool_ammo = max(0, actor.(weaponentity).oknex_chargepool_ammo - WEP_CVAR_SEC(oknex, ammo) * dt);
204
205         //                                      dt = min(dt, (1 - actor.(weaponentity).oknex_charge) / WEP_CVAR(oknex, charge_rate));
206         //                                      actor.oknex_chargepool_pauseregen_finished = time + WEP_CVAR_SEC(oknex, chargepool_pause_regen);
207         //                                      dt = min(dt, actor.(weaponentity).oknex_chargepool_ammo);
208         //                                      dt = max(0, dt);
209
210         //                                      actor.(weaponentity).oknex_charge += dt * WEP_CVAR(oknex, charge_rate);
211         //                              }
212         //                      }
213
214         //                      else if(WEP_CVAR_SEC(oknex, ammo))
215         //                      {
216         //                              if(fire & 2) // only eat ammo when the button is pressed
217         //                              {
218         //                                      dt = min(dt, (1 - actor.(weaponentity).oknex_charge) / WEP_CVAR(oknex, charge_rate));
219         //                                      if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
220         //                                      {
221         //                                              // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
222         //                                              if(autocvar_g_balance_vortex_reload_ammo)
223         //                                              {
224         //                                                      dt = min(dt, (actor.(weaponentity).clip_load - WEP_CVAR_PRI(oknex, ammo)) / WEP_CVAR_SEC(oknex, ammo));
225         //                                                      dt = max(0, dt);
226         //                                                      if(dt > 0)
227         //                                                      {
228         //                                                              actor.(weaponentity).clip_load = max(WEP_CVAR_SEC(oknex, ammo), actor.(weaponentity).clip_load - WEP_CVAR_SEC(oknex, ammo) * dt);
229         //                                                      }
230         //                                                      actor.(weaponentity).(weapon_load[WEP_OVERKILL_NEX.m_id]) = actor.(weaponentity).clip_load;
231         //                                              }
232         //                                              else
233         //                                              {
234         //                                                      dt = min(dt, (actor.(thiswep.ammo_field) - WEP_CVAR_PRI(oknex, ammo)) / WEP_CVAR_SEC(oknex, ammo));
235         //                                                      dt = max(0, dt);
236         //                                                      if(dt > 0)
237         //                                                      {
238         //                                                              actor.(thiswep.ammo_field) = max(WEP_CVAR_SEC(oknex, ammo), actor.(thiswep.ammo_field) - WEP_CVAR_SEC(oknex, ammo) * dt);
239         //                                                      }
240         //                                              }
241         //                                      }
242         //                                      actor.(weaponentity).oknex_charge += dt * WEP_CVAR(oknex, charge_rate);
243         //                              }
244         //                      }
245
246         //                      else
247         //                      {
248         //                              dt = min(dt, (1 - actor.(weaponentity).oknex_charge) / WEP_CVAR(oknex, charge_rate));
249         //                              actor.(weaponentity).oknex_charge += dt * WEP_CVAR(oknex, charge_rate);
250         //                      }
251         //              }
252         //      }
253         //      else if(WEP_CVAR(oknex, secondary))
254         //      {
255         //              if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_SEC(oknex, refire)))
256         //              {
257         //                      W_OverkillNex_Attack(thiswep, actor, weaponentity, 1);
258         //                      weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_SEC(oknex, animtime), w_ready);
259         //              }
260         //      }
261         //}
262 }
263
264 METHOD(OverkillNex, wr_setup, void(entity thiswep, entity actor, .entity weaponentity))
265 {
266         actor.oknex_lasthit = 0;
267 }
268
269 METHOD(OverkillNex, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
270 {
271         float ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR_PRI(oknex, ammo);
272         ammo_amount += (autocvar_g_balance_oknex_reload_ammo && actor.(weaponentity).(weapon_load[WEP_OVERKILL_NEX.m_id]) >= WEP_CVAR_PRI(oknex, ammo));
273         return ammo_amount;
274 }
275
276 METHOD(OverkillNex, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
277 {
278         if (WEP_CVAR(oknex, secondary))
279         {
280                 // don't allow charging if we don't have enough ammo
281                 float ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR_SEC(oknex, ammo);
282                 ammo_amount += actor.(weaponentity).(weapon_load[WEP_OVERKILL_NEX.m_id]) >= WEP_CVAR_SEC(oknex, ammo);
283                 return ammo_amount;
284         }
285         else
286         {
287                 return false; // zoom is not a fire mode
288         }
289 }
290
291 METHOD(OverkillNex, wr_resetplayer, void(entity thiswep, entity actor))
292 {
293         if (WEP_CVAR(oknex, charge)) {
294                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
295                 {
296                         .entity weaponentity = weaponentities[slot];
297                         actor.(weaponentity).oknex_charge = WEP_CVAR(oknex, charge_start);
298                 }
299         }
300         actor.oknex_lasthit = 0;
301 }
302
303 METHOD(OverkillNex, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
304 {
305         W_Reload(actor, weaponentity, WEP_CVAR_PRI(oknex, ammo), SND_RELOAD);
306 }
307
308 METHOD(OverkillNex, wr_suicidemessage, Notification(entity thiswep))
309 {
310         return WEAPON_THINKING_WITH_PORTALS;
311 }
312
313 METHOD(OverkillNex, wr_killmessage, Notification(entity thiswep))
314 {
315         return WEAPON_OVERKILL_NEX_MURDER;
316 }
317
318 METHOD(OverkillNex, wr_zoom, bool(entity thiswep, entity actor))
319 {
320         return PHYS_INPUT_BUTTON_ATCK2(actor) && !WEP_CVAR(oknex, secondary);
321 }
322
323 #endif
324 #ifdef CSQC
325
326 METHOD(OverkillNex, wr_impacteffect, void(entity thiswep, entity actor))
327 {
328         entity this = actor;
329         vector org2 = w_org + w_backoff * 6;
330         pointparticles(EFFECT_VORTEX_IMPACT, org2, '0 0 0', 1);
331         if(!w_issilent)
332                 sound(this, CH_SHOTS, SND_NEXIMPACT, VOL_BASE, ATTN_NORM);
333 }
334
335 METHOD(OverkillNex, wr_init, void(entity thiswep))
336 {
337         if(autocvar_cl_reticle && autocvar_cl_reticle_weapon)
338         {
339                 precache_pic("gfx/reticle_nex");
340         }
341 }
342
343 METHOD(OverkillNex, wr_zoom, bool(entity thiswep, entity actor))
344 {
345         if(button_zoom || zoomscript_caught || (!WEP_CVAR(oknex, secondary) && button_attack2))
346         {
347                 return true;
348         }
349         else
350         {
351                 // no weapon specific image for this weapon
352                 return false;
353         }
354 }
355
356 METHOD(OverkillNex, wr_zoomdir, bool(entity thiswep))
357 {
358     return button_attack2 && !WEP_CVAR(oknex, secondary);
359 }
360
361 #endif