Give W_SetupShot a deathtype parameter, fixes some ugly hacks
[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")
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                                 // now set the next one to trigger as well
163                                 setthink(e, W_Electro_ExplodeCombo);
164
165                                 // delay combo chains, looks cooler
166                                 e.nextthink =
167                                         (
168                                                 time
169                                                 +
170                                                 (WEP_CVAR(electro, combo_speed) ?
171                                                         (vlen(e.WarpZone_findradius_dist) / WEP_CVAR(electro, combo_speed))
172                                                         :
173                                                         0
174                                                 )
175                                         );
176
177                                 ++found;
178                         }
179                         e = e.chain;
180                 }
181
182                 // if we triggered an orb, should we explode? if not, lets try again next time
183                 if(found && WEP_CVAR_PRI(electro, midaircombo_explode))
184                         { this.use(this, NULL, NULL); }
185                 else
186                         { this.nextthink = min(time + WEP_CVAR_PRI(electro, midaircombo_interval), this.ltime); }
187         }
188         else { this.nextthink = this.ltime; }
189         // this.nextthink = time;
190 }
191
192 void W_Electro_Attack_Bolt(Weapon thiswep, entity actor, .entity weaponentity)
193 {
194         entity proj;
195
196         W_DecreaseAmmo(thiswep, actor, WEP_CVAR_PRI(electro, ammo), weaponentity);
197
198         W_SetupShot_ProjectileSize(
199                 actor,
200                 weaponentity,
201                 '0 0 -3',
202                 '0 0 -3',
203                 false,
204                 2,
205                 SND_ELECTRO_FIRE,
206                 CH_WEAPON_A,
207                 WEP_CVAR_PRI(electro, damage),
208                 WEP_ELECTRO.m_id
209         );
210
211         Send_Effect(EFFECT_ELECTRO_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
212
213         proj = new(electro_bolt);
214         proj.owner = proj.realowner = actor;
215         proj.bot_dodge = true;
216         proj.bot_dodgerating = WEP_CVAR_PRI(electro, damage);
217         proj.use = W_Electro_Explode_use;
218         setthink(proj, W_Electro_Bolt_Think);
219         proj.nextthink = time;
220         proj.ltime = time + WEP_CVAR_PRI(electro, lifetime);
221         PROJECTILE_MAKETRIGGER(proj);
222         proj.projectiledeathtype = WEP_ELECTRO.m_id;
223         proj.weaponentity_fld = weaponentity;
224         setorigin(proj, w_shotorg);
225
226         // if (IS_CSQC)
227         set_movetype(proj, MOVETYPE_FLY);
228         W_SetupProjVelocity_PRI(proj, electro);
229         proj.angles = vectoangles(proj.velocity);
230         settouch(proj, W_Electro_TouchExplode);
231         setsize(proj, '0 0 -3', '0 0 -3');
232         proj.flags = FL_PROJECTILE;
233         IL_PUSH(g_projectiles, proj);
234         IL_PUSH(g_bot_dodge, proj);
235         proj.missile_flags = MIF_SPLASH;
236
237         CSQCProjectile(proj, true, PROJECTILE_ELECTRO_BEAM, true);
238
239         MUTATOR_CALLHOOK(EditProjectile, actor, proj);
240         // proj.com_phys_pos = proj.origin;
241         // proj.com_phys_vel = proj.velocity;
242 }
243
244 void W_Electro_Orb_Stick(entity this, entity to)
245 {
246         entity newproj = spawn();
247         newproj.classname = this.classname;
248
249         newproj.bot_dodge = this.bot_dodge;
250         newproj.bot_dodgerating = this.bot_dodgerating;
251
252         newproj.owner = this.owner;
253         newproj.realowner = this.realowner;
254         setsize(newproj, this.mins, this.maxs);
255         setorigin(newproj, this.origin);
256         setmodel(newproj, MDL_PROJECTILE_ELECTRO);
257         newproj.angles = vectoangles(-trace_plane_normal); // face against the surface
258
259         newproj.takedamage = this.takedamage;
260         newproj.damageforcescale = this.damageforcescale;
261         newproj.health = this.health;
262         newproj.event_damage = this.event_damage;
263         newproj.spawnshieldtime = this.spawnshieldtime;
264         newproj.damagedbycontents = true;
265         IL_PUSH(g_damagedbycontents, newproj);
266
267         set_movetype(newproj, MOVETYPE_NONE); // lock the orb in place
268         newproj.projectiledeathtype = this.projectiledeathtype;
269         newproj.weaponentity_fld = this.weaponentity_fld;
270
271         settouch(newproj, func_null);
272         setthink(newproj, getthink(this));
273         newproj.nextthink = this.nextthink;
274         newproj.use = this.use;
275         newproj.flags = this.flags;
276         IL_PUSH(g_projectiles, newproj);
277         IL_PUSH(g_bot_dodge, newproj);
278
279         delete(this);
280
281         if(to)
282                 SetMovetypeFollow(this, to);
283 }
284
285 void W_Electro_Orb_Touch(entity this, entity toucher)
286 {
287         PROJECTILE_TOUCH(this, toucher);
288         if(toucher.takedamage == DAMAGE_AIM)
289                 { if(WEP_CVAR_SEC(electro, touchexplode)) { W_Electro_Explode(this, toucher); } }
290         else if(toucher.owner != this.owner && toucher.classname != this.classname) // don't stick to player's other projectiles!
291         {
292                 //UpdateCSQCProjectile(this);
293                 spamsound(this, CH_SHOTS, SND_ELECTRO_BOUNCE, VOL_BASE, ATTEN_NORM);
294                 this.projectiledeathtype |= HITTYPE_BOUNCE;
295
296                 if(WEP_CVAR_SEC(electro, stick))
297                         W_Electro_Orb_Stick(this, toucher);
298         }
299 }
300
301 void W_Electro_Orb_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
302 {
303         if(this.health <= 0)
304                 return;
305
306         // note: combos are usually triggered by W_Electro_TriggerCombo, not damage
307         float is_combo = (inflictor.classname == "electro_orb_chain" || inflictor.classname == "electro_bolt");
308
309         if(!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, (is_combo ? 1 : -1)))
310                 return; // g_projectiles_damage says to halt
311
312         this.health = this.health - damage;
313         if(this.health <= 0)
314         {
315                 this.takedamage = DAMAGE_NO;
316                 this.nextthink = time;
317                 if(is_combo)
318                 {
319                         // change owner to whoever caused the combo explosion
320                         this.realowner = inflictor.realowner;
321                         this.classname = "electro_orb_chain";
322                         setthink(this, W_Electro_ExplodeCombo);
323                         this.nextthink = time +
324                                 (
325                                         // bound the length, inflictor may be in a galaxy far far away (warpzones)
326                                         min(
327                                                 WEP_CVAR(electro, combo_radius),
328                                                 vlen(this.origin - inflictor.origin)
329                                         )
330                                         /
331                                         // delay combo chains, looks cooler
332                                         WEP_CVAR(electro, combo_speed)
333                                 );
334                 }
335                 else
336                 {
337                         this.use = W_Electro_Explode_use;
338                         setthink(this, adaptor_think2use); // not _hittype_splash, as this runs "immediately"
339                 }
340         }
341 }
342
343 void W_Electro_Attack_Orb(Weapon thiswep, entity actor, .entity weaponentity)
344 {
345         W_DecreaseAmmo(thiswep, actor, WEP_CVAR_SEC(electro, ammo), weaponentity);
346
347         W_SetupShot_ProjectileSize(
348                 actor,
349                 weaponentity,
350                 '-4 -4 -4',
351                 '4 4 4',
352                 false,
353                 2,
354                 SND_ELECTRO_FIRE2,
355                 CH_WEAPON_A,
356                 WEP_CVAR_SEC(electro, damage),
357                 WEP_ELECTRO.m_id | HITTYPE_SECONDARY
358         );
359
360         w_shotdir = v_forward; // no TrueAim for grenades please
361
362         Send_Effect(EFFECT_ELECTRO_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
363
364         entity proj = new(electro_orb);
365         proj.owner = proj.realowner = actor;
366         proj.use = W_Electro_Explode_use;
367         setthink(proj, adaptor_think2use_hittype_splash);
368         proj.bot_dodge = true;
369         proj.bot_dodgerating = WEP_CVAR_SEC(electro, damage);
370         proj.nextthink = time + WEP_CVAR_SEC(electro, lifetime);
371         PROJECTILE_MAKETRIGGER(proj);
372         proj.projectiledeathtype = WEP_ELECTRO.m_id | HITTYPE_SECONDARY;
373         proj.weaponentity_fld = weaponentity;
374         setorigin(proj, w_shotorg);
375
376         //proj.glow_size = 50;
377         //proj.glow_color = 45;
378         set_movetype(proj, MOVETYPE_BOUNCE);
379         W_SetupProjVelocity_UP_SEC(proj, electro);
380         settouch(proj, W_Electro_Orb_Touch);
381         setsize(proj, '-4 -4 -4', '4 4 4');
382         proj.takedamage = DAMAGE_YES;
383         proj.damageforcescale = WEP_CVAR_SEC(electro, damageforcescale);
384         proj.health = WEP_CVAR_SEC(electro, health);
385         proj.event_damage = W_Electro_Orb_Damage;
386         proj.flags = FL_PROJECTILE;
387         IL_PUSH(g_projectiles, proj);
388         IL_PUSH(g_bot_dodge, proj);
389         proj.damagedbycontents = (WEP_CVAR_SEC(electro, damagedbycontents));
390         if(proj.damagedbycontents)
391                 IL_PUSH(g_damagedbycontents, proj);
392
393         proj.bouncefactor = WEP_CVAR_SEC(electro, bouncefactor);
394         proj.bouncestop = WEP_CVAR_SEC(electro, bouncestop);
395         proj.missile_flags = MIF_SPLASH | MIF_ARC;
396
397 #if 0
398         entity p2;
399         p2 = spawn();
400         copyentity(proj, p2);
401         setmodel(p2, MDL_PROJECTILE_ELECTRO);
402         setsize(p2, proj.mins, proj.maxs);
403 #endif
404
405         CSQCProjectile(proj, true, PROJECTILE_ELECTRO, false); // no culling, it has sound
406
407         MUTATOR_CALLHOOK(EditProjectile, actor, proj);
408 }
409
410 void W_Electro_CheckAttack(Weapon thiswep, entity actor, .entity weaponentity, int fire)
411 {
412         if(actor.(weaponentity).electro_count > 1)
413         if(PHYS_INPUT_BUTTON_ATCK2(actor))
414         if(weapon_prepareattack(thiswep, actor, weaponentity, true, -1))
415         {
416                 W_Electro_Attack_Orb(WEP_ELECTRO, actor, weaponentity);
417                 actor.(weaponentity).electro_count -= 1;
418                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(electro, animtime), W_Electro_CheckAttack);
419                 return;
420         }
421         // WEAPONTODO: when the player releases the button, cut down the length of refire2?
422         w_ready(thiswep, actor, weaponentity, fire);
423 }
424
425 .float bot_secondary_electromooth;
426
427 METHOD(Electro, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
428 {
429     PHYS_INPUT_BUTTON_ATCK(actor) = PHYS_INPUT_BUTTON_ATCK2(actor) = false;
430     if(vdist(actor.origin - actor.enemy.origin, >, 1000)) { actor.bot_secondary_electromooth = 0; }
431     if(actor.bot_secondary_electromooth == 0)
432     {
433         float shoot;
434
435         if(WEP_CVAR_PRI(electro, speed))
436             shoot = bot_aim(actor, weaponentity, WEP_CVAR_PRI(electro, speed), 0, WEP_CVAR_PRI(electro, lifetime), false);
437         else
438             shoot = bot_aim(actor, weaponentity, 1000000, 0, 0.001, false);
439
440         if(shoot)
441         {
442             PHYS_INPUT_BUTTON_ATCK(actor) = true;
443             if(random() < 0.01) actor.bot_secondary_electromooth = 1;
444         }
445     }
446     else
447     {
448         if(bot_aim(actor, weaponentity, WEP_CVAR_SEC(electro, speed), WEP_CVAR_SEC(electro, speed_up), WEP_CVAR_SEC(electro, lifetime), true))
449         {
450             PHYS_INPUT_BUTTON_ATCK2(actor) = true;
451             if(random() < 0.03) actor.bot_secondary_electromooth = 0;
452         }
453     }
454 }
455 METHOD(Electro, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
456 {
457     if(autocvar_g_balance_electro_reload_ammo) // forced reload // WEAPONTODO
458     {
459         float ammo_amount = 0;
460         if(actor.(weaponentity).clip_load >= WEP_CVAR_PRI(electro, ammo))
461             ammo_amount = 1;
462         if(actor.(weaponentity).clip_load >= WEP_CVAR_SEC(electro, ammo))
463             ammo_amount += 1;
464
465         if(!ammo_amount)
466         {
467             thiswep.wr_reload(thiswep, actor, weaponentity);
468             return;
469         }
470     }
471
472     if(fire & 1)
473     {
474         if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_PRI(electro, refire)))
475         {
476                 W_Electro_Attack_Bolt(thiswep, actor, weaponentity);
477                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(electro, animtime), w_ready);
478         }
479     }
480     else if(fire & 2)
481     {
482         if(time >= actor.(weaponentity).electro_secondarytime)
483         if(weapon_prepareattack(thiswep, actor, weaponentity, true, WEP_CVAR_SEC(electro, refire)))
484         {
485             W_Electro_Attack_Orb(thiswep, actor, weaponentity);
486             actor.(weaponentity).electro_count = WEP_CVAR_SEC(electro, count);
487             weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(electro, animtime), W_Electro_CheckAttack);
488             actor.(weaponentity).electro_secondarytime = time + WEP_CVAR_SEC(electro, refire2) * W_WeaponRateFactor(actor);
489         }
490     }
491 }
492 METHOD(Electro, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
493 {
494     float ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR_PRI(electro, ammo);
495     ammo_amount += actor.(weaponentity).(weapon_load[WEP_ELECTRO.m_id]) >= WEP_CVAR_PRI(electro, ammo);
496     return ammo_amount;
497 }
498 METHOD(Electro, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
499 {
500     float ammo_amount;
501     if(WEP_CVAR(electro, combo_safeammocheck)) // true if you can fire at least one secondary blob AND one primary shot after it, otherwise false.
502     {
503         ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR_SEC(electro, ammo) + WEP_CVAR_PRI(electro, ammo);
504         ammo_amount += actor.(weaponentity).(weapon_load[WEP_ELECTRO.m_id]) >= WEP_CVAR_SEC(electro, ammo) + WEP_CVAR_PRI(electro, ammo);
505     }
506     else
507     {
508         ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR_SEC(electro, ammo);
509         ammo_amount += actor.(weaponentity).(weapon_load[WEP_ELECTRO.m_id]) >= WEP_CVAR_SEC(electro, ammo);
510     }
511     return ammo_amount;
512 }
513 METHOD(Electro, wr_resetplayer, void(entity thiswep, entity actor))
514 {
515     for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
516     {
517         .entity weaponentity = weaponentities[slot];
518         actor.(weaponentity).electro_secondarytime = time;
519     }
520 }
521 METHOD(Electro, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
522 {
523     W_Reload(actor, weaponentity, min(WEP_CVAR_PRI(electro, ammo), WEP_CVAR_SEC(electro, ammo)), SND_RELOAD);
524 }
525 METHOD(Electro, wr_suicidemessage, Notification(entity thiswep))
526 {
527     if(w_deathtype & HITTYPE_SECONDARY)
528         return WEAPON_ELECTRO_SUICIDE_ORBS;
529     else
530         return WEAPON_ELECTRO_SUICIDE_BOLT;
531 }
532 METHOD(Electro, wr_killmessage, Notification(entity thiswep))
533 {
534     if(w_deathtype & HITTYPE_SECONDARY)
535     {
536         return WEAPON_ELECTRO_MURDER_ORBS;
537     }
538     else
539     {
540         if(w_deathtype & HITTYPE_BOUNCE)
541             return WEAPON_ELECTRO_MURDER_COMBO;
542         else
543             return WEAPON_ELECTRO_MURDER_BOLT;
544     }
545 }
546
547 #endif
548 #ifdef CSQC
549
550 METHOD(Electro, wr_impacteffect, void(entity thiswep, entity actor))
551 {
552     vector org2;
553     org2 = w_org + w_backoff * 6;
554     if(w_deathtype & HITTYPE_SECONDARY)
555     {
556         pointparticles(EFFECT_ELECTRO_BALLEXPLODE, org2, '0 0 0', 1);
557         if(!w_issilent)
558             sound(actor, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_NORM);
559     }
560     else
561     {
562         if(w_deathtype & HITTYPE_BOUNCE)
563         {
564             // this is sent as "primary (w_deathtype & HITTYPE_BOUNCE)" to distinguish it from (w_deathtype & HITTYPE_SECONDARY) bounced balls
565             pointparticles(EFFECT_ELECTRO_COMBO, org2, '0 0 0', 1);
566             if(!w_issilent)
567                 sound(actor, CH_SHOTS, SND_ELECTRO_IMPACT_COMBO, VOL_BASE, ATTEN_NORM);
568         }
569         else
570         {
571             pointparticles(EFFECT_ELECTRO_IMPACT, org2, '0 0 0', 1);
572             if(!w_issilent)
573                 sound(actor, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_NORM);
574         }
575     }
576 }
577
578 #endif