Fix MLRS and Hellion (animations and logic)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / tturrets / units / unit_hellion.qc
1 .float      shot_speed_max;
2 .float      shot_speed_gain;
3
4 void spawnfunc_turret_hellion();
5 void turret_hellion_dinit();
6 void turret_hellion_attack();
7 void turret_hellion_missile_explode();
8 void turret_hellion_missile_think();
9 void turret_hellion_missile_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector vforce)
10
11 void turret_hellion_postthink()
12 {
13     if (cvar("g_turrets_reloadcvars"))
14     {
15         if (!self.shot_speed_max)  self.shot_speed_max  = cvar("g_turrets_unit_hellion_std_shot_speed_max");
16         if (!self.shot_speed_gain) self.shot_speed_gain = cvar("g_turrets_unit_hellion_std_shot_speed_gain");
17     }
18
19     if (self.tur_head.frame != 0)
20         self.tur_head.frame += 1;
21
22     if (self.tur_head.frame >= 7)
23         self.tur_head.frame = 0;
24 }
25
26 void turret_hellion_attack()
27 {
28     entity missile;
29                 
30         if(self.tur_head.frame != 0)
31                 self.tur_shotorg = gettaginfo(self.tur_head, gettagindex(self.tur_head, "tag_fire"));
32         else
33                 self.tur_shotorg = gettaginfo(self.tur_head, gettagindex(self.tur_head, "tag_fire2"));
34     
35     sound (self, CHAN_WEAPON, "weapons/hagar_fire.wav", VOL_BASE, ATTN_NORM);
36
37     missile = spawn ();
38     setorigin(missile, self.tur_shotorg);
39     setsize (missile, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot
40
41     missile.classname          = "hellion_missile";
42     missile.owner              = self;
43     missile.bot_dodge          = TRUE;
44     missile.bot_dodgerating    = self.shot_dmg;
45     missile.takedamage         = DAMAGE_YES;
46     missile.event_damage       = turret_hellion_missile_damage;
47     missile.damageforcescale   = 2;
48     missile.health             = 10;
49     missile.enemy              = self.enemy;
50     missile.think              = turret_hellion_missile_think;
51     missile.nextthink          = time;// + 0.2;
52     missile.solid              = SOLID_BBOX;
53     missile.movetype           = MOVETYPE_FLY;
54     missile.velocity           = normalize(self.tur_shotdir_updated + randomvec() * self.shot_spread) * self.shot_speed;
55     missile.angles             = vectoangles(missile.velocity);
56     missile.touch              = turret_hellion_missile_explode;
57     missile.flags              = FL_PROJECTILE;
58     missile.solid              = SOLID_BBOX;
59     missile.tur_health         = time + 9;
60     missile.tur_aimpos         = randomvec() * 128;
61     te_explosion (missile.origin);
62         CSQCProjectile(missile, FALSE, PROJECTILE_ROCKET, FALSE); // no culling, has fly sound
63
64         self.tur_head.frame += 1;
65 }
66
67 void turret_hellion_missile_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector vforce)
68 {
69     self.health = self.health - damage;
70     self.velocity = self.velocity + vforce;
71     if (self.health <= 0) turret_hellion_missile_explode();
72 }
73
74 void turret_hellion_missile_think()
75 {
76     vector olddir,newdir;
77     vector pre_pos;
78     float itime;
79
80     self.nextthink = time + 0.05;
81
82     olddir = normalize(self.velocity);
83
84     if(self.tur_health < time)
85         turret_hellion_missile_explode();
86
87     // Enemy dead? just keep on the current heading then.
88     if ((self.enemy == world) || (self.enemy.deadflag != DEAD_NO))
89     {
90
91         // Make sure we dont return to tracking a respawned player
92         self.enemy = world;
93
94         // Turn model
95         self.angles = vectoangles(self.velocity);
96
97         if ( (vlen(self.origin - self.owner.origin)) > (self.owner.shot_radius * 5) )
98             turret_hellion_missile_explode();
99
100         // Accelerate
101         self.velocity = olddir * min(vlen(self.velocity) * self.owner.shot_speed_gain,self.owner.shot_speed_max);
102
103         UpdateCSQCProjectile(self);
104
105         return;
106     }
107
108     // Enemy in range?
109     if (vlen(self.origin - self.enemy.origin) < self.owner.shot_radius * 0.2)
110         turret_hellion_missile_explode();
111
112     // Predict enemy position
113     itime = vlen(self.enemy.origin - self.origin) / vlen(self.velocity);
114     pre_pos = self.enemy.origin + self.enemy.velocity * itime;
115
116     pre_pos = (pre_pos + self.enemy.origin) * 0.5;
117
118     // Find out the direction to that place
119     newdir = normalize(pre_pos - self.origin);
120
121     // Turn
122     newdir = normalize(olddir + newdir * 0.35);
123
124     // Turn model
125     self.angles = vectoangles(self.velocity);
126
127     // Accelerate
128     self.velocity = newdir * min(vlen(self.velocity) * self.owner.shot_speed_gain,self.owner.shot_speed_max);
129
130     if (itime < 0.05)
131         self.think = turret_hellion_missile_explode;
132
133     UpdateCSQCProjectile(self);
134 }
135
136 void turret_hellion_missile_explode()
137 {
138     vector org2;
139     float d;
140
141     if(self.event_damage != SUB_Null)
142     {
143         self.event_damage = SUB_Null;
144         self.think = turret_hellion_missile_explode;
145         self.nextthink = time;
146         return;
147     }
148
149     sound (self, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
150     org2 = findbetterlocation (self.origin, 16);
151
152     // LordHavoc: TE_TEI_BIGEXPLOSION
153     WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
154     WriteByte (MSG_BROADCAST, 78);
155     WriteCoord (MSG_BROADCAST, org2_x);
156     WriteCoord (MSG_BROADCAST, org2_y);
157     WriteCoord (MSG_BROADCAST, org2_z);
158
159     //w_deathtypestring = "could not dodge the twin missiles.";
160     self.event_damage = SUB_Null;
161     d = RadiusDamage (self, self.owner, self.owner.shot_dmg, 0, self.owner.shot_radius, world, self.owner.shot_force, DEATH_TURRET, world);
162
163 #ifdef TURRET_DEBUG
164     self.owner.tur_dbg_dmg_t_h = self.owner.tur_dbg_dmg_t_h + d; //self.owner.shot_dmg;
165     self.owner.tur_dbg_dmg_t_f = self.owner.tur_dbg_dmg_t_f + self.owner.shot_dmg;
166 #endif
167
168     // Target dead, get another is still targeting the same.
169     if ((self.enemy.deadflag != DEAD_NO) && (self.enemy == self.owner.enemy))
170         self.owner.enemy = world;
171
172     remove (self);
173 }
174
175 void turret_hellion_dinit()
176 {
177     if (self.netname == "")      self.netname  = "Hellion Missile Turret";
178
179     if not (self.shot_speed_max)
180         self.shot_speed_max  = cvar("g_turrets_unit_hellion_std_shot_speed_max");
181
182     if not (self.shot_speed_gain)
183         self.shot_speed_gain = cvar("g_turrets_unit_hellion_std_shot_speed_gain");
184
185     self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_FASTPROJ | TFL_TURRCAPS_PLAYERKILL | TFL_TURRCAPS_MISSILEKILL;
186     self.aim_flags = TFL_AIM_SIMPLE;
187     self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_TEAMCHECK ;
188     self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_DISTANCES | TFL_FIRECHECK_TEAMCECK | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_AFF | TFL_FIRECHECK_OWM_AMMO;
189     self.ammo_flags = TFL_AMMO_ROCKETS | TFL_AMMO_RECHARGE;
190
191     if (turret_stdproc_init("hellion_std",0,"models/turrets/base.md3","models/turrets/hellion.md3") == 0)
192     {
193         remove(self);
194         return;
195     }
196
197     if (!turret_tag_fire_update())
198         dprint("Warning: Turret ",self.classname, " faild to initialize md3 tags\n");
199
200     self.turret_firefunc  = turret_hellion_attack;
201     self.turret_postthink = turret_hellion_postthink;
202 }
203
204 /*QUAKED turret_hellion (0 .5 .8) ?
205 */
206 void spawnfunc_turret_hellion()
207 {
208     precache_model ("models/turrets/hellion.md3");
209     precache_model ("models/turrets/base.md3");
210
211     self.think = turret_hellion_dinit;
212     self.nextthink = time + 0.5;
213 }
214
215