1e8d6b2a9da0066c05464261504fcc6721f00c6e
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / turrets / turret / tesla_weapon.qc
1 #ifndef TURRET_TESLA_WEAPON_H
2 #define TURRET_TESLA_WEAPON_H
3
4 CLASS(TeslaCoilTurretAttack, PortoLaunch)
5 /* flags     */ ATTRIB(TeslaCoilTurretAttack, spawnflags, int, WEP_TYPE_OTHER | WEP_FLAG_HIDDEN);
6 /* impulse   */ ATTRIB(TeslaCoilTurretAttack, impulse, int, 9);
7 /* refname   */ ATTRIB(TeslaCoilTurretAttack, netname, string, "turret_tesla");
8 /* wepname   */ ATTRIB(TeslaCoilTurretAttack, message, string, _("Tesla Coil"));
9 ENDCLASS(TeslaCoilTurretAttack)
10 REGISTER_WEAPON(TESLA, NEW(TeslaCoilTurretAttack));
11
12 #endif
13
14 #ifdef IMPLEMENTATION
15
16 #ifdef SVQC
17
18 entity toast(entity from, float range, float damage);
19 METHOD(TeslaCoilTurretAttack, wr_think, bool(entity thiswep, bool fire1, bool fire2)) {
20     SELFPARAM();
21     bool isPlayer = IS_PLAYER(self);
22     if (fire1)
23     if (!isPlayer || weapon_prepareattack(false, WEP_CVAR_PRI(electro, refire))) {
24         if (isPlayer) {
25             turret_initparams(self);
26             W_SetupShot_Dir(self, v_forward, false, 0, W_Sound("electro_fire"), CH_WEAPON_B, 0);
27             self.tur_shotdir_updated = w_shotdir;
28             self.tur_shotorg = w_shotorg;
29             self.tur_head = self;
30             weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_PRI(electro, animtime), w_ready);
31         }
32
33         float d = self.shot_dmg;
34         float r = self.target_range;
35         entity e = spawn();
36         setorigin(e,self.tur_shotorg);
37
38         self.target_validate_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_MISSILES | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_TEAMCHECK;
39
40         entity t = toast(e,r,d);
41         remove(e);
42
43         if (t == NULL) return true;
44
45         self.target_validate_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_MISSILES | TFL_TARGETSELECT_TEAMCHECK;
46
47         self.attack_finished_single = time + self.shot_refire;
48         for (int i = 0; i < 10; ++i) {
49             d *= 0.75;
50             r *= 0.85;
51             t = toast(t, r, d);
52             if (t == world) break;
53
54         }
55
56         e = findchainfloat(railgunhit, 1);
57         while (e) {
58             e.railgunhit = 0;
59             e = e.chain;
60         }
61
62     }
63     return true;
64 }
65
66 entity toast(entity from, float range, float damage)
67 {SELFPARAM();
68     entity e;
69     entity etarget = world;
70     float d,dd;
71     float r;
72
73     dd = range + 1;
74
75     e = findradius(from.origin,range);
76     while (e)
77     {
78         if ((e.railgunhit != 1) && (e != from))
79         {
80             r = turret_validate_target(self,e,self.target_validate_flags);
81             if (r > 0)
82             {
83                 traceline(from.origin,0.5 * (e.absmin + e.absmax),MOVE_WORLDONLY,from);
84                 if (trace_fraction == 1.0)
85                 {
86                     d = vlen(e.origin - from.origin);
87                     if (d < dd)
88                     {
89                         dd = d;
90                         etarget = e;
91                     }
92                 }
93             }
94         }
95         e = e.chain;
96     }
97
98     if (etarget)
99     {
100         te_csqc_lightningarc(from.origin,etarget.origin);
101         Damage(etarget, self, self, damage, DEATH_TURRET_TESLA, etarget.origin, '0 0 0');
102         etarget.railgunhit = 1;
103     }
104
105     return etarget;
106 }
107
108 #endif
109
110 #endif