Merge branch 'master' into Mario/wepent_experimental
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / vortex.qc
1 #include "vortex.qh"
2 #ifndef IMPLEMENTATION
3 CLASS(Vortex, Weapon)
4 /* ammotype  */ ATTRIB(Vortex, ammo_field, .int, ammo_cells);
5 /* impulse   */ ATTRIB(Vortex, impulse, int, 7);
6 /* flags     */ ATTRIB(Vortex, spawnflags, int, WEP_FLAG_NORMAL | WEP_FLAG_RELOADABLE | WEP_TYPE_HITSCAN);
7 /* rating    */ ATTRIB(Vortex, bot_pickupbasevalue, float, BOT_PICKUP_RATING_HIGH);
8 /* color     */ ATTRIB(Vortex, wpcolor, vector, '0.5 1 1');
9 /* modelname */ ATTRIB(Vortex, mdl, string, "nex");
10 #ifdef GAMEQC
11 /* model     */ ATTRIB(Vortex, m_model, Model, MDL_VORTEX_ITEM);
12 #endif
13 /* crosshair */ ATTRIB(Vortex, w_crosshair, string, "gfx/crosshairnex");
14 /* crosshair */ ATTRIB(Vortex, w_crosshair_size, float, 0.65);
15 /* reticle   */ ATTRIB(Vortex, w_reticle, string, "gfx/reticle_nex");
16 /* wepimg    */ ATTRIB(Vortex, model2, string, "weaponnex");
17 /* refname   */ ATTRIB(Vortex, netname, string, "vortex");
18 /* wepname   */ ATTRIB(Vortex, m_name, string, _("Vortex"));
19
20 #define X(BEGIN, P, END, class, prefix) \
21         BEGIN(class) \
22                 P(class, prefix, ammo, float, BOTH) \
23                 P(class, prefix, animtime, float, BOTH) \
24                 P(class, prefix, chargepool, float, SEC) \
25                 P(class, prefix, chargepool_pause_regen, float, SEC) \
26                 P(class, prefix, chargepool_regen, float, SEC) \
27                 P(class, prefix, charge, float, NONE) \
28                 P(class, prefix, charge_animlimit, float, NONE) \
29                 P(class, prefix, charge_limit, float, NONE) \
30                 P(class, prefix, charge_maxspeed, float, NONE) \
31                 P(class, prefix, charge_mindmg, float, NONE) \
32                 P(class, prefix, charge_minspeed, float, NONE) \
33                 P(class, prefix, charge_rate, float, NONE) \
34                 P(class, prefix, charge_rot_pause, float, NONE) \
35                 P(class, prefix, charge_rot_rate, float, NONE) \
36                 P(class, prefix, charge_shot_multiplier, float, NONE) \
37                 P(class, prefix, charge_start, float, NONE) \
38                 P(class, prefix, charge_velocity_rate, float, NONE) \
39                 P(class, prefix, damagefalloff_forcehalflife, float, BOTH) \
40                 P(class, prefix, damagefalloff_halflife, float, BOTH) \
41                 P(class, prefix, damagefalloff_maxdist, float, BOTH) \
42                 P(class, prefix, damagefalloff_mindist, float, BOTH) \
43                 P(class, prefix, damage, float, BOTH) \
44                 P(class, prefix, force, float, BOTH) \
45                 P(class, prefix, refire, float, BOTH) \
46                 P(class, prefix, secondary, float, NONE) \
47                 P(class, prefix, reload_ammo, float, NONE) \
48         P(class, prefix, reload_time, float, NONE) \
49         P(class, prefix, switchdelay_raise, float, NONE) \
50         P(class, prefix, switchdelay_drop, float, NONE) \
51         P(class, prefix, weaponreplace, string, NONE) \
52         P(class, prefix, weaponstart, float, NONE) \
53         P(class, prefix, weaponstartoverride, float, NONE) \
54         P(class, prefix, weaponthrowable, float, NONE) \
55         END()
56     W_PROPS(X, Vortex, vortex)
57 #undef X
58
59 ENDCLASS(Vortex)
60 REGISTER_WEAPON(VORTEX, vortex, NEW(Vortex));
61
62
63 #ifdef SVQC
64
65 .float vortex_lasthit;
66 #endif
67 #endif
68 #ifdef IMPLEMENTATION
69
70 REGISTER_STAT(WEP_CVAR_vortex_charge, bool, WEP_CVAR(vortex, charge))
71 REGISTER_STAT(WEP_CVAR_vortex_charge_animlimit, float, WEP_CVAR(vortex, charge_animlimit))
72
73 #if defined(GAMEQC)
74 float autocvar_g_weapon_charge_colormod_red_full;
75 float autocvar_g_weapon_charge_colormod_red_half;
76 float autocvar_g_weapon_charge_colormod_green_full;
77 float autocvar_g_weapon_charge_colormod_blue_full;
78 float autocvar_g_weapon_charge_colormod_blue_half;
79 float autocvar_g_weapon_charge_colormod_green_half;
80 float autocvar_g_weapon_charge_colormod_hdrmultiplier;
81
82 METHOD(Vortex, wr_glow, vector(Vortex this, entity actor, entity wepent))
83 {
84         if (!STAT(WEP_CVAR_vortex_charge, actor)) return '0 0 0';
85         float charge = wepent.vortex_charge;
86         float animlimit = STAT(WEP_CVAR_vortex_charge_animlimit, actor);
87         vector g;
88         g.x = autocvar_g_weapon_charge_colormod_hdrmultiplier * autocvar_g_weapon_charge_colormod_red_half * min(1, charge / animlimit);
89         g.y = autocvar_g_weapon_charge_colormod_hdrmultiplier * autocvar_g_weapon_charge_colormod_green_half * min(1, charge / animlimit);
90         g.z = autocvar_g_weapon_charge_colormod_hdrmultiplier * autocvar_g_weapon_charge_colormod_blue_half * min(1, charge / animlimit);
91         if (charge > animlimit)
92         {
93                 g.x += autocvar_g_weapon_charge_colormod_hdrmultiplier * autocvar_g_weapon_charge_colormod_red_full * (charge - animlimit) / (1 - animlimit);
94                 g.y += autocvar_g_weapon_charge_colormod_hdrmultiplier * autocvar_g_weapon_charge_colormod_green_full * (charge - animlimit) / (1 - animlimit);
95                 g.z += autocvar_g_weapon_charge_colormod_hdrmultiplier * autocvar_g_weapon_charge_colormod_blue_full * (charge - animlimit) / (1 - animlimit);
96         }
97         return g;
98 }
99 #endif
100
101 REGISTER_NET_TEMP(TE_CSQC_VORTEXBEAMPARTICLE)
102
103 #if defined(SVQC)
104 void SendCSQCVortexBeamParticle(float charge) {
105         vector v;
106         v = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos);
107         WriteHeader(MSG_BROADCAST, TE_CSQC_VORTEXBEAMPARTICLE);
108         WriteCoord(MSG_BROADCAST, w_shotorg.x);
109         WriteCoord(MSG_BROADCAST, w_shotorg.y);
110         WriteCoord(MSG_BROADCAST, w_shotorg.z);
111         WriteCoord(MSG_BROADCAST, v.x);
112         WriteCoord(MSG_BROADCAST, v.y);
113         WriteCoord(MSG_BROADCAST, v.z);
114         WriteByte(MSG_BROADCAST, bound(0, 255 * charge, 255));
115 }
116 #elif defined(CSQC)
117 NET_HANDLE(TE_CSQC_VORTEXBEAMPARTICLE, bool isNew)
118 {
119         vector shotorg, endpos;
120         float charge;
121         shotorg.x = ReadCoord(); shotorg.y = ReadCoord(); shotorg.z = ReadCoord();
122         endpos.x = ReadCoord(); endpos.y = ReadCoord(); endpos.z = ReadCoord();
123         charge = ReadByte() / 255.0;
124
125         pointparticles(EFFECT_VORTEX_MUZZLEFLASH, shotorg, normalize(endpos - shotorg) * 1000, 1);
126
127         //draw either the old v2.3 beam or the new beam
128         charge = sqrt(charge); // divide evenly among trail spacing and alpha
129         particles_alphamin = particles_alphamax = particles_fade = charge;
130
131         if(!MUTATOR_CALLHOOK(Particles_VortexBeam, shotorg, endpos))
132         if(autocvar_cl_particles_oldvortexbeam && (STAT(ALLOW_OLDVORTEXBEAM) || isdemo()))
133                 WarpZone_TrailParticles_WithMultiplier(NULL, particleeffectnum(EFFECT_VORTEX_BEAM_OLD), shotorg, endpos, 1, PARTICLES_USEALPHA | PARTICLES_USEFADE);
134         else
135                 WarpZone_TrailParticles_WithMultiplier(NULL, particleeffectnum(EFFECT_VORTEX_BEAM), shotorg, endpos, 1, PARTICLES_USEALPHA | PARTICLES_USEFADE);
136         return true;
137 }
138 #endif
139
140 #ifdef SVQC
141 spawnfunc(weapon_vortex) { weapon_defaultspawnfunc(this, WEP_VORTEX); }
142 spawnfunc(weapon_nex) { spawnfunc_weapon_vortex(this); }
143
144 REGISTER_MUTATOR(vortex_charge, true);
145
146 MUTATOR_HOOKFUNCTION(vortex_charge, GetPressedKeys)
147 {
148     entity player = M_ARGV(0, entity);
149
150         // WEAPONTODO
151     if(!WEP_CVAR(vortex, charge) || !WEP_CVAR(vortex, charge_velocity_rate))
152         return;
153
154     for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
155     {
156         .entity weaponentity = weaponentities[slot];
157
158         if (player.(weaponentity).m_weapon == WEP_VORTEX && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && vdist(vec2(player.velocity), >, WEP_CVAR(vortex, charge_minspeed)))
159         {
160             float xyspeed = vlen(vec2(player.velocity));
161                 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
162                 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
163                 float f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
164                 // add the extra charge
165                 player.(weaponentity).vortex_charge = min(1, player.(weaponentity).vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * PHYS_INPUT_TIMELENGTH);
166         }
167     }
168 }
169
170 void W_Vortex_Attack(Weapon thiswep, entity actor, .entity weaponentity, float issecondary)
171 {
172         float mydmg, myforce, mymindist, mymaxdist, myhalflife, myforcehalflife, myammo, charge;
173
174         mydmg = WEP_CVAR_BOTH(vortex, !issecondary, damage);
175         myforce = WEP_CVAR_BOTH(vortex, !issecondary, force);
176         mymindist = WEP_CVAR_BOTH(vortex, !issecondary, damagefalloff_mindist);
177         mymaxdist = WEP_CVAR_BOTH(vortex, !issecondary, damagefalloff_maxdist);
178         myhalflife = WEP_CVAR_BOTH(vortex, !issecondary, damagefalloff_halflife);
179         myforcehalflife = WEP_CVAR_BOTH(vortex, !issecondary, damagefalloff_forcehalflife);
180         myammo = WEP_CVAR_BOTH(vortex, !issecondary, ammo);
181
182         float flying;
183         flying = IsFlying(actor); // do this BEFORE to make the trace values from FireRailgunBullet last
184
185         if(WEP_CVAR(vortex, charge))
186         {
187                 charge = WEP_CVAR(vortex, charge_mindmg) / mydmg + (1 - WEP_CVAR(vortex, charge_mindmg) / mydmg) * actor.(weaponentity).vortex_charge;
188                 actor.(weaponentity).vortex_charge *= WEP_CVAR(vortex, charge_shot_multiplier); // do this AFTER setting mydmg/myforce
189                 // O RLY? -- divVerent
190                 // YA RLY -- FruitieX
191         }
192         else
193                 charge = 1;
194         mydmg *= charge;
195         myforce *= charge;
196
197         W_SetupShot(actor, weaponentity, true, 5, SND_NEXFIRE, CH_WEAPON_A, mydmg);
198         if(charge > WEP_CVAR(vortex, charge_animlimit) && WEP_CVAR(vortex, charge_animlimit)) // if the Vortex is overcharged, we play an extra sound
199         {
200                 sound(actor, CH_WEAPON_B, SND_NEXCHARGE, VOL_BASE * (charge - 0.5 * WEP_CVAR(vortex, charge_animlimit)) / (1 - 0.5 * WEP_CVAR(vortex, charge_animlimit)), ATTN_NORM);
201         }
202
203         yoda = 0;
204         damage_goodhits = 0;
205         FireRailgunBullet(actor, weaponentity, w_shotorg, w_shotorg + w_shotdir * max_shot_distance, mydmg, myforce, mymindist, mymaxdist, myhalflife, myforcehalflife, WEP_VORTEX.m_id);
206
207         if(yoda && flying)
208                 Send_Notification(NOTIF_ONE, actor, MSG_ANNCE, ANNCE_ACHIEVEMENT_YODA);
209         if(damage_goodhits && actor.vortex_lasthit)
210         {
211                 Send_Notification(NOTIF_ONE, actor, MSG_ANNCE, ANNCE_ACHIEVEMENT_IMPRESSIVE);
212                 damage_goodhits = 0; // only every second time
213         }
214
215         actor.vortex_lasthit = damage_goodhits;
216
217         //beam and muzzle flash done on client
218         SendCSQCVortexBeamParticle(charge);
219
220         W_DecreaseAmmo(thiswep, actor, myammo, weaponentity);
221 }
222
223 .float vortex_chargepool_pauseregen_finished;
224
225 METHOD(Vortex, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
226 {
227     if(bot_aim(actor, weaponentity, 1000000, 0, 1, false))
228         PHYS_INPUT_BUTTON_ATCK(actor) = true;
229     else
230     {
231         if(WEP_CVAR(vortex, charge))
232             PHYS_INPUT_BUTTON_ATCK2(actor) = true;
233     }
234 }
235 METHOD(Vortex, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
236 {
237     if(WEP_CVAR(vortex, charge) && actor.(weaponentity).vortex_charge < WEP_CVAR(vortex, charge_limit))
238         actor.(weaponentity).vortex_charge = min(1, actor.(weaponentity).vortex_charge + WEP_CVAR(vortex, charge_rate) * frametime / W_TICSPERFRAME);
239
240     if(weaponslot(weaponentity) == 0)
241         actor.vortex_charge = actor.(weaponentity).vortex_charge;
242
243     if(WEP_CVAR_SEC(vortex, chargepool))
244         if(actor.(weaponentity).vortex_chargepool_ammo < 1)
245         {
246             if(actor.vortex_chargepool_pauseregen_finished < time)
247                 actor.(weaponentity).vortex_chargepool_ammo = min(1, actor.(weaponentity).vortex_chargepool_ammo + WEP_CVAR_SEC(vortex, chargepool_regen) * frametime / W_TICSPERFRAME);
248             actor.pauseregen_finished = max(actor.pauseregen_finished, time + WEP_CVAR_SEC(vortex, chargepool_pause_regen));
249         }
250
251     if(weaponslot(weaponentity) == 0)
252         actor.vortex_chargepool_ammo = actor.(weaponentity).vortex_chargepool_ammo;
253
254     if(autocvar_g_balance_vortex_reload_ammo && actor.(weaponentity).clip_load < min(WEP_CVAR_PRI(vortex, ammo), WEP_CVAR_SEC(vortex, ammo))) { // forced reload
255         thiswep.wr_reload(thiswep, actor, weaponentity);
256     } else
257     {
258         if(fire & 1)
259         {
260             if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_PRI(vortex, refire)))
261             {
262                 W_Vortex_Attack(thiswep, actor, weaponentity, 0);
263                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(vortex, animtime), w_ready);
264             }
265         }
266         if((WEP_CVAR(vortex, charge) && !WEP_CVAR(vortex, secondary)) ? (PHYS_INPUT_BUTTON_ZOOM(actor) | PHYS_INPUT_BUTTON_ZOOMSCRIPT(actor)) : (fire & 2))
267         {
268             if(WEP_CVAR(vortex, charge))
269             {
270                 actor.(weaponentity).vortex_charge_rottime = time + WEP_CVAR(vortex, charge_rot_pause);
271                 float dt = frametime / W_TICSPERFRAME;
272
273                 if(actor.(weaponentity).vortex_charge < 1)
274                 {
275                     if(WEP_CVAR_SEC(vortex, chargepool))
276                     {
277                         if(WEP_CVAR_SEC(vortex, ammo))
278                         {
279                             // always deplete if secondary is held
280                             actor.vortex_chargepool_ammo = max(0, actor.vortex_chargepool_ammo - WEP_CVAR_SEC(vortex, ammo) * dt);
281
282                             dt = min(dt, (1 - actor.(weaponentity).vortex_charge) / WEP_CVAR(vortex, charge_rate));
283                             actor.vortex_chargepool_pauseregen_finished = time + WEP_CVAR_SEC(vortex, chargepool_pause_regen);
284                             dt = min(dt, actor.vortex_chargepool_ammo);
285                             dt = max(0, dt);
286
287                             actor.(weaponentity).vortex_charge += dt * WEP_CVAR(vortex, charge_rate);
288                         }
289                     }
290
291                     else if(WEP_CVAR_SEC(vortex, ammo))
292                     {
293                         if(fire & 2) // only eat ammo when the button is pressed
294                         {
295                             dt = min(dt, (1 - actor.(weaponentity).vortex_charge) / WEP_CVAR(vortex, charge_rate));
296                             if(!(actor.items & IT_UNLIMITED_WEAPON_AMMO))
297                             {
298                                 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
299                                 if(autocvar_g_balance_vortex_reload_ammo)
300                                 {
301                                     dt = min(dt, (actor.(weaponentity).clip_load - WEP_CVAR_PRI(vortex, ammo)) / WEP_CVAR_SEC(vortex, ammo));
302                                     dt = max(0, dt);
303                                     if(dt > 0)
304                                     {
305                                         actor.(weaponentity).clip_load = max(WEP_CVAR_SEC(vortex, ammo), actor.(weaponentity).clip_load - WEP_CVAR_SEC(vortex, ammo) * dt);
306                                     }
307                                     actor.(weaponentity).(weapon_load[WEP_VORTEX.m_id]) = actor.(weaponentity).clip_load;
308                                 }
309                                 else
310                                 {
311                                     dt = min(dt, (actor.(thiswep.ammo_field) - WEP_CVAR_PRI(vortex, ammo)) / WEP_CVAR_SEC(vortex, ammo));
312                                     dt = max(0, dt);
313                                     if(dt > 0)
314                                     {
315                                         actor.(thiswep.ammo_field) = max(WEP_CVAR_SEC(vortex, ammo), actor.(thiswep.ammo_field) - WEP_CVAR_SEC(vortex, ammo) * dt);
316                                     }
317                                 }
318                             }
319                             actor.(weaponentity).vortex_charge += dt * WEP_CVAR(vortex, charge_rate);
320                         }
321                     }
322
323                     else
324                     {
325                         dt = min(dt, (1 - actor.(weaponentity).vortex_charge) / WEP_CVAR(vortex, charge_rate));
326                         actor.(weaponentity).vortex_charge += dt * WEP_CVAR(vortex, charge_rate);
327                     }
328                 }
329             }
330             else if(WEP_CVAR(vortex, secondary))
331             {
332                 if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_SEC(vortex, refire)))
333                 {
334                     W_Vortex_Attack(thiswep, actor, weaponentity, 1);
335                     weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_SEC(vortex, animtime), w_ready);
336                 }
337             }
338         }
339     }
340 }
341 METHOD(Vortex, wr_setup, void(entity thiswep, entity actor, .entity weaponentity))
342 {
343     actor.vortex_lasthit = 0;
344 }
345 METHOD(Vortex, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
346 {
347     float ammo_amount = actor.(thiswep.ammo_field) >= WEP_CVAR_PRI(vortex, ammo);
348     ammo_amount += (autocvar_g_balance_vortex_reload_ammo && actor.(weaponentity).(weapon_load[WEP_VORTEX.m_id]) >= WEP_CVAR_PRI(vortex, ammo));
349     return ammo_amount;
350 }
351 METHOD(Vortex, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
352 {
353     if(WEP_CVAR(vortex, secondary))
354     {
355         // don't allow charging if we don't have enough ammo
356         float ammo_amount = actor.(thiswep.ammo_field) >= WEP_CVAR_SEC(vortex, ammo);
357         ammo_amount += actor.(weaponentity).(weapon_load[WEP_VORTEX.m_id]) >= WEP_CVAR_SEC(vortex, ammo);
358         return ammo_amount;
359     }
360     else
361     {
362         return false; // zoom is not a fire mode
363     }
364 }
365 METHOD(Vortex, wr_resetplayer, void(entity thiswep, entity actor))
366 {
367     if (WEP_CVAR(vortex, charge)) {
368         if (WEP_CVAR_SEC(vortex, chargepool)) {
369             actor.vortex_chargepool_ammo = 1;
370         }
371         actor.vortex_charge = WEP_CVAR(vortex, charge_start);
372         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
373         {
374             .entity weaponentity = weaponentities[slot];
375             actor.(weaponentity).vortex_charge = WEP_CVAR(vortex, charge_start);
376
377             if (WEP_CVAR_SEC(vortex, chargepool))
378                 actor.(weaponentity).vortex_chargepool_ammo = 1;
379         }
380     }
381     actor.vortex_lasthit = 0;
382 }
383 METHOD(Vortex, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
384 {
385     W_Reload(actor, weaponentity, min(WEP_CVAR_PRI(vortex, ammo), WEP_CVAR_SEC(vortex, ammo)), SND_RELOAD);
386 }
387 METHOD(Vortex, wr_suicidemessage, Notification(entity thiswep))
388 {
389     return WEAPON_THINKING_WITH_PORTALS;
390 }
391 METHOD(Vortex, wr_killmessage, Notification(entity thiswep))
392 {
393     return WEAPON_VORTEX_MURDER;
394 }
395 METHOD(Vortex, wr_zoom, bool(entity thiswep, entity actor))
396 {
397     return PHYS_INPUT_BUTTON_ATCK2(actor) && !WEP_CVAR(vortex, secondary);
398 }
399
400 #endif
401 #ifdef CSQC
402
403 METHOD(Vortex, wr_impacteffect, void(entity thiswep, entity actor))
404 {
405     entity this = actor;
406     vector org2 = w_org + w_backoff * 6;
407     pointparticles(EFFECT_VORTEX_IMPACT, org2, '0 0 0', 1);
408     if(!w_issilent)
409         sound(this, CH_SHOTS, SND_NEXIMPACT, VOL_BASE, ATTN_NORM);
410 }
411 METHOD(Vortex, wr_init, void(entity thiswep))
412 {
413     if(autocvar_cl_reticle && autocvar_cl_reticle_weapon)
414     {
415         precache_pic("gfx/reticle_nex");
416     }
417 }
418 METHOD(Vortex, wr_zoom, bool(entity thiswep, entity actor))
419 {
420     if(button_zoom || zoomscript_caught || (!WEP_CVAR(vortex, secondary) && button_attack2))
421     {
422         return true;
423     }
424     else
425     {
426         // no weapon specific image for this weapon
427         return false;
428     }
429 }
430
431 #endif
432 #endif