]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/weapon/electro.qc
f7ac37287d9d7c6af4d97df5fa2f05f976b8084c
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / electro.qc
1 #include "electro.qh"
2
3 #ifdef SVQC
4
5 void W_Electro_TriggerCombo(vector org, float rad, entity own)
6 {
7         entity e = WarpZone_FindRadius(org, rad, !WEP_CVAR(electro, combo_comboradius_thruwall));
8         while(e)
9         {
10                 if(e.classname == "electro_orb")
11                 {
12                         // do we allow thruwall triggering?
13                         if(WEP_CVAR(electro, combo_comboradius_thruwall))
14                         {
15                                 // if distance is greater than thruwall distance, check to make sure it's not through a wall
16                                 if(vdist(e.WarpZone_findradius_dist, >, WEP_CVAR(electro, combo_comboradius_thruwall)))
17                                 {
18                                         WarpZone_TraceLine(org, e.origin, MOVE_NOMONSTERS, e);
19                                         if(trace_fraction != 1)
20                                         {
21                                                 // trigger is through a wall and outside of thruwall range, abort
22                                                 e = e.chain;
23                                                 continue;
24                                         }
25                                 }
26                         }
27
28                         // change owner to whoever caused the combo explosion
29                         e.realowner = own;
30                         e.takedamage = DAMAGE_NO;
31                         e.classname = "electro_orb_chain";
32
33                         // now set the next one to trigger as well
34                         setthink(e, W_Electro_ExplodeCombo);
35
36                         // delay combo chains, looks cooler
37                         e.nextthink =
38                                 (
39                                         time
40                                         +
41                                         (WEP_CVAR(electro, combo_speed) ?
42                                                 (vlen(e.WarpZone_findradius_dist) / WEP_CVAR(electro, combo_speed))
43                                                 :
44                                                 0
45                                         )
46                                 );
47                 }
48                 e = e.chain;
49         }
50 }
51
52 void W_Electro_ExplodeCombo(entity this)
53 {
54         W_Electro_TriggerCombo(this.origin, WEP_CVAR(electro, combo_comboradius), this.realowner);
55
56         this.event_damage = func_null;
57
58         RadiusDamage(
59                 this,
60                 this.realowner,
61                 WEP_CVAR(electro, combo_damage),
62                 WEP_CVAR(electro, combo_edgedamage),
63                 WEP_CVAR(electro, combo_radius),
64                 NULL,
65                 NULL,
66                 WEP_CVAR(electro, combo_force),
67                 WEP_ELECTRO.m_id | HITTYPE_BOUNCE, // use THIS type for a combo because primary can't bounce
68                 this.weaponentity_fld,
69                 NULL
70         );
71
72         delete(this);
73 }
74
75 void W_Electro_Explode(entity this, entity directhitentity)
76 {
77         if(directhitentity.takedamage == DAMAGE_AIM)
78                 if(IS_PLAYER(directhitentity))
79                         if(DIFF_TEAM(this.realowner, directhitentity))
80                                 if(!IS_DEAD(directhitentity))
81                                         if(IsFlying(directhitentity))
82                                                 Send_Notification(NOTIF_ONE, this.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_ELECTROBITCH);
83
84         this.event_damage = func_null;
85         this.takedamage = DAMAGE_NO;
86
87         if(this.move_movetype == MOVETYPE_BOUNCE || this.classname == "electro_orb") // TODO: classname is more reliable anyway?
88         {
89                 RadiusDamage(
90                         this,
91                         this.realowner,
92                         WEP_CVAR_SEC(electro, damage),
93                         WEP_CVAR_SEC(electro, edgedamage),
94                         WEP_CVAR_SEC(electro, radius),
95                         NULL,
96                         NULL,
97                         WEP_CVAR_SEC(electro, force),
98                         this.projectiledeathtype,
99                         this.weaponentity_fld,
100                         directhitentity
101                 );
102         }
103         else
104         {
105                 W_Electro_TriggerCombo(this.origin, WEP_CVAR_PRI(electro, comboradius), this.realowner);
106                 RadiusDamage(
107                         this,
108                         this.realowner,
109                         WEP_CVAR_PRI(electro, damage),
110                         WEP_CVAR_PRI(electro, edgedamage),
111                         WEP_CVAR_PRI(electro, radius),
112                         NULL,
113                         NULL,
114                         WEP_CVAR_PRI(electro, force),
115                         this.projectiledeathtype,
116                         this.weaponentity_fld,
117                         directhitentity
118                 );
119         }
120
121         delete(this);
122 }
123
124 void W_Electro_Explode_use(entity this, entity actor, entity trigger)
125 {
126         W_Electro_Explode(this, trigger);
127 }
128
129 void W_Electro_TouchExplode(entity this, entity toucher)
130 {
131         PROJECTILE_TOUCH(this, toucher);
132         W_Electro_Explode(this, toucher);
133 }
134
135
136 void sys_phys_update_single(entity this);
137
138 void W_Electro_Bolt_Think(entity this)
139 {
140         // sys_phys_update_single(this);
141         if(time >= this.ltime)
142         {
143                 this.use(this, NULL, NULL);
144                 return;
145         }
146
147         if(WEP_CVAR_PRI(electro, midaircombo_radius))
148         {
149                 float found = 0;
150                 entity e = WarpZone_FindRadius(this.origin, WEP_CVAR_PRI(electro, midaircombo_radius), true);
151
152                 // loop through nearby orbs and trigger them
153                 while(e)
154                 {
155                         if(e.classname == "electro_orb" && (WEP_CVAR_PRI(electro, midaircombo_own) || DIFF_TEAM(this.owner, e.owner)))
156                         {
157                                 // change owner to whoever caused the combo explosion
158                                 e.realowner = this.realowner;
159                                 e.takedamage = DAMAGE_NO;
160                                 e.classname = "electro_orb_chain";
161
162                                 // explode first orb immediately, other orbs will chain with delay
163                                 setthink(e, W_Electro_ExplodeCombo);
164                                 e.nextthink = time;
165
166                                 ++found;
167                         }
168                         e = e.chain;
169                 }
170
171                 // if we triggered an orb, should we explode? if not, lets try again next time
172                 if(found && WEP_CVAR_PRI(electro, midaircombo_explode))
173                         { this.use(this, NULL, NULL); }
174                 else
175                         { this.nextthink = min(time + WEP_CVAR_PRI(electro, midaircombo_interval), this.ltime); }
176         }
177         else { this.nextthink = this.ltime; }
178         // this.nextthink = time;
179 }
180
181 void W_Electro_Attack_Bolt(Weapon thiswep, entity actor, .entity weaponentity)
182 {
183         entity proj;
184
185         W_DecreaseAmmo(thiswep, actor, WEP_CVAR_PRI(electro, ammo), weaponentity);
186
187         W_SetupShot_ProjectileSize(
188                 actor,
189                 weaponentity,
190                 '0 0 -3',
191                 '0 0 -3',
192                 false,
193                 2,
194                 SND_ELECTRO_FIRE,
195                 CH_WEAPON_A,
196                 WEP_CVAR_PRI(electro, damage),
197                 thiswep.m_id
198         );
199
200         Send_Effect(EFFECT_ELECTRO_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
201
202         proj = new(electro_bolt);
203         proj.owner = proj.realowner = actor;
204         proj.bot_dodge = true;
205         proj.bot_dodgerating = WEP_CVAR_PRI(electro, damage);
206         proj.use = W_Electro_Explode_use;
207         setthink(proj, W_Electro_Bolt_Think);
208         proj.nextthink = time;
209         proj.ltime = time + WEP_CVAR_PRI(electro, lifetime);
210         PROJECTILE_MAKETRIGGER(proj);
211         proj.projectiledeathtype = thiswep.m_id;
212         proj.weaponentity_fld = weaponentity;
213         setorigin(proj, w_shotorg);
214
215         // if (IS_CSQC)
216         set_movetype(proj, MOVETYPE_FLY);
217         W_SetupProjVelocity_PRI(proj, electro);
218         proj.angles = vectoangles(proj.velocity);
219         settouch(proj, W_Electro_TouchExplode);
220         setsize(proj, '0 0 -3', '0 0 -3');
221         proj.flags = FL_PROJECTILE;
222         IL_PUSH(g_projectiles, proj);
223         IL_PUSH(g_bot_dodge, proj);
224         proj.missile_flags = MIF_SPLASH;
225
226         CSQCProjectile(proj, true, PROJECTILE_ELECTRO_BEAM, true);
227
228         MUTATOR_CALLHOOK(EditProjectile, actor, proj);
229         // proj.com_phys_pos = proj.origin;
230         // proj.com_phys_vel = proj.velocity;
231 }
232
233 void W_Electro_Orb_Stick(entity this, entity to)
234 {
235         entity newproj = spawn();
236         newproj.classname = this.classname;
237
238         newproj.bot_dodge = this.bot_dodge;
239         newproj.bot_dodgerating = this.bot_dodgerating;
240
241         newproj.owner = this.owner;
242         newproj.realowner = this.realowner;
243         setsize(newproj, this.mins, this.maxs);
244         setorigin(newproj, this.origin);
245         setmodel(newproj, MDL_PROJECTILE_ELECTRO);
246         newproj.angles = vectoangles(-trace_plane_normal); // face against the surface
247
248         newproj.takedamage = this.takedamage;
249         newproj.damageforcescale = this.damageforcescale;
250         SetResourceExplicit(newproj, RES_HEALTH, GetResource(this, RES_HEALTH));
251         newproj.event_damage = this.event_damage;
252         newproj.spawnshieldtime = this.spawnshieldtime;
253         newproj.damagedbycontents = true;
254         IL_PUSH(g_damagedbycontents, newproj);
255
256         set_movetype(newproj, MOVETYPE_NONE); // lock the orb in place
257         newproj.projectiledeathtype = this.projectiledeathtype;
258         newproj.weaponentity_fld = this.weaponentity_fld;
259
260         settouch(newproj, func_null);
261         setthink(newproj, getthink(this));
262         newproj.nextthink = this.nextthink;
263         newproj.use = this.use;
264         newproj.flags = this.flags;
265         IL_PUSH(g_projectiles, newproj);
266         IL_PUSH(g_bot_dodge, newproj);
267
268         delete(this);
269
270         if(to)
271                 SetMovetypeFollow(newproj, to);
272 }
273
274 void W_Electro_Orb_Touch(entity this, entity toucher)
275 {
276         PROJECTILE_TOUCH(this, toucher);
277         if(toucher.takedamage == DAMAGE_AIM && WEP_CVAR_SEC(electro, touchexplode))
278                 { W_Electro_Explode(this, toucher); }
279         else if(toucher.owner != this.owner && toucher.classname != this.classname) // don't stick to player's other projectiles!
280         {
281                 //UpdateCSQCProjectile(this);
282                 spamsound(this, CH_SHOTS, SND_ELECTRO_BOUNCE, VOL_BASE, ATTEN_NORM);
283                 this.projectiledeathtype |= HITTYPE_BOUNCE;
284
285                 if(WEP_CVAR_SEC(electro, stick))
286                         W_Electro_Orb_Stick(this, toucher);
287         }
288 }
289
290 void W_Electro_Orb_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
291 {
292         if(GetResource(this, RES_HEALTH) <= 0)
293                 return;
294
295         // note: combos are usually triggered by W_Electro_TriggerCombo, not damage
296         float is_combo = (inflictor.classname == "electro_orb_chain" || inflictor.classname == "electro_bolt");
297
298         if(!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, (is_combo ? 1 : -1)))
299                 return; // g_projectiles_damage says to halt
300
301         TakeResource(this, RES_HEALTH, damage);
302         if(GetResource(this, RES_HEALTH) <= 0)
303         {
304                 this.takedamage = DAMAGE_NO;
305                 this.nextthink = time;
306                 if(is_combo)
307                 {
308                         // change owner to whoever caused the combo explosion
309                         this.realowner = inflictor.realowner;
310                         this.classname = "electro_orb_chain";
311                         setthink(this, W_Electro_ExplodeCombo);
312                         this.nextthink = time +
313                                 (
314                                         // bound the length, inflictor may be in a galaxy far far away (warpzones)
315                                         min(
316                                                 WEP_CVAR(electro, combo_radius),
317                                                 vlen(this.origin - inflictor.origin)
318                                         )
319                                         /
320                                         // delay combo chains, looks cooler
321                                         WEP_CVAR(electro, combo_speed)
322                                 );
323                 }
324                 else
325                 {
326                         this.use = W_Electro_Explode_use;
327                         setthink(this, adaptor_think2use); // not _hittype_splash, as this runs "immediately"
328                 }
329         }
330 }
331
332 void W_Electro_Attack_Orb(Weapon thiswep, entity actor, .entity weaponentity)
333 {
334         W_DecreaseAmmo(thiswep, actor, WEP_CVAR_SEC(electro, ammo), weaponentity);
335
336         W_SetupShot_ProjectileSize(
337                 actor,
338                 weaponentity,
339                 '-4 -4 -4',
340                 '4 4 4',
341                 false,
342                 2,
343                 SND_ELECTRO_FIRE2,
344                 CH_WEAPON_A,
345                 WEP_CVAR_SEC(electro, damage),
346                 thiswep.m_id | HITTYPE_SECONDARY
347         );
348
349         w_shotdir = v_forward; // no TrueAim for grenades please
350
351         Send_Effect(EFFECT_ELECTRO_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
352
353         entity proj = new(electro_orb);
354         proj.owner = proj.realowner = actor;
355         proj.use = W_Electro_Explode_use;
356         setthink(proj, adaptor_think2use_hittype_splash);
357         proj.bot_dodge = true;
358         proj.bot_dodgerating = WEP_CVAR_SEC(electro, damage);
359         proj.nextthink = time + WEP_CVAR_SEC(electro, lifetime);
360         PROJECTILE_MAKETRIGGER(proj);
361         proj.projectiledeathtype = thiswep.m_id | HITTYPE_SECONDARY;
362         proj.weaponentity_fld = weaponentity;
363         setorigin(proj, w_shotorg);
364
365         //proj.glow_size = 50;
366         //proj.glow_color = 45;
367         set_movetype(proj, MOVETYPE_BOUNCE);
368         W_SetupProjVelocity_UP_SEC(proj, electro);
369         settouch(proj, W_Electro_Orb_Touch);
370         setsize(proj, '-4 -4 -4', '4 4 4');
371         proj.takedamage = DAMAGE_YES;
372         proj.damageforcescale = WEP_CVAR_SEC(electro, damageforcescale);
373         SetResourceExplicit(proj, RES_HEALTH, WEP_CVAR_SEC(electro, health));
374         proj.event_damage = W_Electro_Orb_Damage;
375         proj.flags = FL_PROJECTILE;
376         IL_PUSH(g_projectiles, proj);
377         IL_PUSH(g_bot_dodge, proj);
378         proj.damagedbycontents = (WEP_CVAR_SEC(electro, damagedbycontents));
379         if(proj.damagedbycontents)
380                 IL_PUSH(g_damagedbycontents, proj);
381
382         proj.bouncefactor = WEP_CVAR_SEC(electro, bouncefactor);
383         proj.bouncestop = WEP_CVAR_SEC(electro, bouncestop);
384         proj.missile_flags = MIF_SPLASH | MIF_ARC;
385
386         CSQCProjectile(proj, true, PROJECTILE_ELECTRO, false); // no culling, it has sound
387
388         MUTATOR_CALLHOOK(EditProjectile, actor, proj);
389 }
390
391 void W_Electro_CheckAttack(Weapon thiswep, entity actor, .entity weaponentity, int fire)
392 {
393         if(actor.(weaponentity).electro_count > 1)
394         if(PHYS_INPUT_BUTTON_ATCK2(actor))
395         if(weapon_prepareattack(thiswep, actor, weaponentity, true, -1))
396         {
397                 W_Electro_Attack_Orb(thiswep, actor, weaponentity);
398                 actor.(weaponentity).electro_count -= 1;
399                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(electro, animtime), W_Electro_CheckAttack);
400                 return;
401         }
402         // WEAPONTODO: when the player releases the button, cut down the length of refire2?
403         w_ready(thiswep, actor, weaponentity, fire);
404 }
405
406 .float bot_secondary_electromooth;
407
408 METHOD(Electro, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
409 {
410     PHYS_INPUT_BUTTON_ATCK(actor) = PHYS_INPUT_BUTTON_ATCK2(actor) = false;
411     if(vdist(actor.origin - actor.enemy.origin, >, 1000)) { actor.bot_secondary_electromooth = 0; }
412     if(actor.bot_secondary_electromooth == 0)
413     {
414         float shoot;
415
416         if(WEP_CVAR_PRI(electro, speed))
417             shoot = bot_aim(actor, weaponentity, WEP_CVAR_PRI(electro, speed), 0, WEP_CVAR_PRI(electro, lifetime), false);
418         else
419             shoot = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
420
421         if(shoot)
422         {
423             PHYS_INPUT_BUTTON_ATCK(actor) = true;
424             if(random() < 0.01) actor.bot_secondary_electromooth = 1;
425         }
426     }
427     else
428     {
429         if(bot_aim(actor, weaponentity, WEP_CVAR_SEC(electro, speed), WEP_CVAR_SEC(electro, speed_up), WEP_CVAR_SEC(electro, lifetime), true))
430         {
431             PHYS_INPUT_BUTTON_ATCK2(actor) = true;
432             if(random() < 0.03) actor.bot_secondary_electromooth = 0;
433         }
434     }
435 }
436 METHOD(Electro, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
437 {
438     if(autocvar_g_balance_electro_reload_ammo) // forced reload // WEAPONTODO
439     {
440         float ammo_amount = 0;
441         if(actor.(weaponentity).clip_load >= WEP_CVAR_PRI(electro, ammo))
442             ammo_amount = 1;
443         if(actor.(weaponentity).clip_load >= WEP_CVAR_SEC(electro, ammo))
444             ammo_amount += 1;
445
446         if(!ammo_amount)
447         {
448             thiswep.wr_reload(thiswep, actor, weaponentity);
449             return;
450         }
451     }
452
453     if(fire & 1)
454     {
455         if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_PRI(electro, refire)))
456         {
457                 W_Electro_Attack_Bolt(thiswep, actor, weaponentity);
458                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(electro, animtime), w_ready);
459         }
460     }
461     else if(fire & 2)
462     {
463         if(time >= actor.(weaponentity).electro_secondarytime)
464         if(weapon_prepareattack(thiswep, actor, weaponentity, true, WEP_CVAR_SEC(electro, refire)))
465         {
466             W_Electro_Attack_Orb(thiswep, actor, weaponentity);
467             actor.(weaponentity).electro_count = WEP_CVAR_SEC(electro, count);
468             weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(electro, animtime), W_Electro_CheckAttack);
469             actor.(weaponentity).electro_secondarytime = time + WEP_CVAR_SEC(electro, refire2) * W_WeaponRateFactor(actor);
470         }
471     }
472 }
473 METHOD(Electro, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
474 {
475     float ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR_PRI(electro, ammo);
476     ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR_PRI(electro, ammo);
477     return ammo_amount;
478 }
479 METHOD(Electro, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
480 {
481     float ammo_amount;
482     if(WEP_CVAR(electro, combo_safeammocheck)) // true if you can fire at least one secondary blob AND one primary shot after it, otherwise false.
483     {
484         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR_SEC(electro, ammo) + WEP_CVAR_PRI(electro, ammo);
485         ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR_SEC(electro, ammo) + WEP_CVAR_PRI(electro, ammo);
486     }
487     else
488     {
489         ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR_SEC(electro, ammo);
490         ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR_SEC(electro, ammo);
491     }
492     return ammo_amount;
493 }
494 METHOD(Electro, wr_resetplayer, void(entity thiswep, entity actor))
495 {
496     for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
497     {
498         .entity weaponentity = weaponentities[slot];
499         actor.(weaponentity).electro_secondarytime = time;
500     }
501 }
502 METHOD(Electro, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
503 {
504     W_Reload(actor, weaponentity, min(WEP_CVAR_PRI(electro, ammo), WEP_CVAR_SEC(electro, ammo)), SND_RELOAD);
505 }
506 METHOD(Electro, wr_suicidemessage, Notification(entity thiswep))
507 {
508     if(w_deathtype & HITTYPE_SECONDARY)
509         return WEAPON_ELECTRO_SUICIDE_ORBS;
510     else
511         return WEAPON_ELECTRO_SUICIDE_BOLT;
512 }
513 METHOD(Electro, wr_killmessage, Notification(entity thiswep))
514 {
515     if(w_deathtype & HITTYPE_SECONDARY)
516     {
517         return WEAPON_ELECTRO_MURDER_ORBS;
518     }
519     else
520     {
521         if(w_deathtype & HITTYPE_BOUNCE)
522             return WEAPON_ELECTRO_MURDER_COMBO;
523         else
524             return WEAPON_ELECTRO_MURDER_BOLT;
525     }
526 }
527
528 #endif
529 #ifdef CSQC
530
531 METHOD(Electro, wr_impacteffect, void(entity thiswep, entity actor))
532 {
533     vector org2;
534     org2 = w_org + w_backoff * 6;
535     if(w_deathtype & HITTYPE_SECONDARY)
536     {
537         pointparticles(EFFECT_ELECTRO_BALLEXPLODE, org2, '0 0 0', 1);
538         if(!w_issilent)
539             sound(actor, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_NORM);
540     }
541     else
542     {
543         if(w_deathtype & HITTYPE_BOUNCE)
544         {
545             // this is sent as "primary (w_deathtype & HITTYPE_BOUNCE)" to distinguish it from (w_deathtype & HITTYPE_SECONDARY) bounced balls
546             pointparticles(EFFECT_ELECTRO_COMBO, org2, '0 0 0', 1);
547             if(!w_issilent)
548                 sound(actor, CH_SHOTS, SND_ELECTRO_IMPACT_COMBO, VOL_BASE, ATTEN_NORM);
549         }
550         else
551         {
552             pointparticles(EFFECT_ELECTRO_IMPACT, org2, '0 0 0', 1);
553             if(!w_issilent)
554                 sound(actor, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_NORM);
555         }
556     }
557 }
558
559 #endif