Give W_SetupShot a deathtype parameter, fixes some ugly hacks
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / turrets / turret / tesla_weapon.qc
1 #include "tesla_weapon.qh"
2
3 #ifdef SVQC
4
5 entity toast(entity actor, entity from, float range, float damage);
6 SOUND(TeslaCoilTurretAttack_FIRE, W_Sound("electro_fire"));
7 METHOD(TeslaCoilTurretAttack, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire)) {
8     bool isPlayer = IS_PLAYER(actor);
9     if (fire & 1)
10     if (!isPlayer || weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_PRI(electro, refire))) {
11         if (isPlayer) {
12             turret_initparams(actor);
13             W_SetupShot_Dir(actor, weaponentity, v_forward, false, 0, SND_TeslaCoilTurretAttack_FIRE, CH_WEAPON_B, 0, DEATH_TURRET_TESLA.m_id);
14             actor.tur_shotdir_updated = w_shotdir;
15             actor.tur_shotorg = w_shotorg;
16             actor.tur_head = actor;
17             weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(electro, animtime), w_ready);
18         }
19
20         float d = actor.shot_dmg;
21         float r = actor.target_range;
22         entity e = spawn();
23         setorigin(e, actor.tur_shotorg);
24
25         actor.target_validate_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_MISSILES | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_TEAMCHECK;
26
27         entity t = toast(actor, e,r,d);
28         delete(e);
29
30         if (t == NULL) return;
31
32         actor.target_validate_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_MISSILES | TFL_TARGETSELECT_TEAMCHECK;
33
34         actor.attack_finished_single[0] = time + actor.shot_refire;
35         for (int i = 0; i < 10; ++i) {
36             d *= 0.75;
37             r *= 0.85;
38             t = toast(actor, t, r, d);
39             if (t == NULL) break;
40
41         }
42
43         IL_EACH(g_railgunhit, it.railgunhit,
44         {
45             it.railgunhit = false;
46         });
47         IL_CLEAR(g_railgunhit);
48     }
49 }
50
51 entity toast(entity actor, entity from, float range, float damage)
52 {
53     float dd = range + 1;
54     entity etarget = NULL;
55     FOREACH_ENTITY_RADIUS(from.origin, range, it != from && !it.railgunhit,
56     {
57         float r = turret_validate_target(actor, it, actor.target_validate_flags);
58         if(r > 0)
59         {
60             traceline(from.origin, 0.5 * (it.absmin + it.absmax), MOVE_WORLDONLY, from);
61             if(trace_fraction == 1.0)
62             {
63                 float d = vlen(it.origin - from.origin);
64                 if(d < dd)
65                 {
66                     dd = d;
67                     etarget = it;
68                 }
69             }
70         }
71     });
72
73     if (etarget)
74     {
75         te_csqc_lightningarc(from.origin, etarget.origin);
76         Damage(etarget, actor, actor, damage, DEATH_TURRET_TESLA.m_id, DMG_NOWEP, etarget.origin, '0 0 0');
77         etarget.railgunhit = true;
78         IL_PUSH(g_railgunhit, etarget);
79     }
80
81     return etarget;
82 }
83
84 #endif