centerprints now are managed independently per line
[divverent/nexuiz.git] / data / qcsrc / server / w_rocketlauncher.qc
1
2 .float rl_sound;
3
4 void W_Rocket_Explode (void)
5 {
6         vector  org2;
7         sound (self, CHAN_BODY, "weapons/rocket_impact.ogg", 1, ATTN_NORM);
8         org2 = findbetterlocation (self.origin, 16);
9
10         //te_explosion (org2);
11         // LordHavoc: TE_TEI_BIGEXPLOSION
12         WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
13         WriteByte (MSG_BROADCAST, 78);
14         WriteCoord (MSG_BROADCAST, org2_x);
15         WriteCoord (MSG_BROADCAST, org2_y);
16         WriteCoord (MSG_BROADCAST, org2_z);
17
18         //effect (org2, "models/sprites/rockexpl.spr", 0, 12, 35);
19         self.event_damage = SUB_Null;
20         RadiusDamage (self, self.owner, cvar("g_balance_rocketlauncher_damage"), cvar("g_balance_rocketlauncher_edgedamage"), cvar("g_balance_rocketlauncher_radius"), world, cvar("g_balance_rocketlauncher_force"), IT_ROCKET_LAUNCHER);
21
22         if (self.owner.weapon == WEP_ROCKET_LAUNCHER)
23         {
24                 if(self.owner.ammo_rockets < cvar("g_balance_rocketlauncher_ammo"))
25                 {
26                         self.owner.cnt = WEP_ROCKET_LAUNCHER;
27                         self.owner.attack_finished = time;
28                         self.owner.switchweapon = w_getbestweapon(self.owner);
29                 }
30                 if(cvar("g_laserguided_missile"))
31                                   self.owner.attack_finished = time + cvar("g_balance_rocketlauncher_refire");
32         }
33         remove (self);
34 }
35
36 entity FindLaserTarget(entity e, float dist_variance, float dot_variance)
37 {
38         entity head, selected;
39         vector dir;
40         float dist, maxdist,// bestdist,
41                 dot,// bestdot,
42                 points, bestpoints;
43         //bestdist = 9999;
44         //bestdot = -2;
45         bestpoints = 0;
46         maxdist = 800;
47         selected = world;
48
49         makevectors(e.angles);
50
51         head = find(world, classname, "laser_target");
52         while(head)
53         {
54                 points = 0;
55                 dir = normalize(head.origin - self.origin);
56                 dot = dir * v_forward;
57                 dist = vlen(head.origin - self.origin);
58                 if(dist > maxdist)
59                         dist = maxdist;
60
61                 // gain points for being in front
62                 points = points + ((dot+1)*0.5) * 500
63                         * (1 + crandom()*dot_variance);
64                 // gain points for being close away
65                 points = points + (1 - dist/maxdist) * 1000
66                         * (1 + crandom()*dot_variance);
67
68                 traceline(e.origin, head.origin, TRUE, self);
69                 if(trace_fraction < 1)
70                 {
71                         points = 0;
72                 }
73
74                 if(points > bestpoints)//random() > 0.5)//
75                 {
76                         bestpoints = points;
77                         selected = head;
78                 }
79
80                 head = find(head, classname, "laser_target");
81         }
82
83         //bprint(selected.owner.netname);
84         //bprint("\n");
85         return selected;
86 }
87
88 void W_Rocket_Think (void)
89 {
90         entity e;
91         vector desireddir, olddir, newdir;
92         float turnrate;
93         self.nextthink = time;
94         if (time > self.cnt)
95         {
96                 W_Rocket_Explode ();
97                 return;
98         }
99         if (self.owner.weapon == WEP_ROCKET_LAUNCHER)
100         {
101                 if(cvar("g_laserguided_missile"))
102                 {
103                         if(!self.owner.button0)
104                                 self.ltime = -1; // indicate that the player has let go of the button
105
106
107                         if (self.owner.button0 && self.ltime < 0) // if the player let go of the button and then pushed it again
108                         {
109                                   W_Rocket_Explode ();
110                                   return;
111                         }
112
113                         if(cvar("g_balance_rocketlauncher_laserguided_allow_steal"))
114                         {
115                                 if(self.owner.laser_on)
116                                 {
117                                         if(self.attack_finished < time)
118                                         {
119                                                 self.attack_finished = time + 0.2 + random()*0.3;
120                                                 self.enemy = FindLaserTarget(self, 0.7, 0.7);
121                                         }
122
123                                         if(!self.enemy)
124                                                 self.enemy = self.owner.weaponentity.lasertarget;
125                                 }
126                                 else self.enemy = world;
127                         }
128                         else // don't allow stealing: always target my owner's laser (if it exists)
129                                 self.enemy = self.owner.weaponentity.lasertarget;
130
131                         if(self.enemy != world)
132                         {
133                                 //bprint(strcat("Targeting ", self.enemy.owner.netname, "'s laser\n"));
134                                 if(!self.speed)
135                                         self.speed = vlen(self.velocity);
136                                 e = self.enemy;//self.owner.weaponentity.lasertarget;
137                                 turnrate = cvar("g_balance_rocketlauncher_laserguided_turnrate");//0.65;                                                // how fast to turn
138                                 desireddir = normalize(e.origin - self.origin);         // get direction from my position to the laser target
139                                 olddir = normalize(self.velocity);                              // get my current direction
140                                 newdir = normalize((olddir + desireddir * turnrate) * 0.5);     // take the average of the 2 directions; not the best method but simple & easy
141                                 self.velocity = newdir * self.speed;                    // make me fly in the new direction at my flight speed
142                                 self.angles = vectoangles(self.velocity);                       // turn model in the new flight direction
143
144                                 self.owner.attack_finished = time + 0.2;
145                         }
146                 }
147                 else
148                 {
149                         if (self.owner.button3)
150                                   W_Rocket_Explode ();
151                 }
152         }
153 }
154
155 void W_Rocket_Touch (void)
156 {
157         if(self.owner && self.owner.lastrocket == self)
158                 self.owner.lastrocket = world;
159         if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
160         {
161                 sound (self, CHAN_BODY, "misc/null.wav", 1, ATTN_NORM);
162                 remove(self);
163                 return;
164         }
165         W_Rocket_Explode ();
166 }
167
168 void W_Rocket_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
169 {
170         self.health = self.health - damage;
171         if (self.health <= 0)
172         {
173                 self.owner = attacker;
174                 W_Rocket_Explode();
175         }
176 }
177
178 void W_Rocket_Attack (void)
179 {
180         local entity missile;
181         local entity flash;
182
183         if (cvar("g_use_ammunition") && !cvar("g_rocketarena"))
184                 self.ammo_rockets = self.ammo_rockets - cvar("g_balance_rocketlauncher_ammo");
185
186         W_SetupShot (self, '15 3 -8', FALSE, 5, "weapons/rocket_fire.ogg");
187         te_smallflash(w_shotorg);
188
189         missile = spawn ();
190         missile.owner = self;
191         self.lastrocket = missile;
192         missile.classname = "missile";
193         missile.bot_dodge = TRUE;
194         missile.bot_dodgerating = cvar("g_balance_rocketlauncher_primary_damage") * 2; // * 2 because it can be detonated inflight which makes it even more dangerous
195
196         missile.takedamage = DAMAGE_YES;
197         missile.damageforcescale = 4;
198         missile.health = 30;
199         missile.event_damage = W_Rocket_Damage;
200
201         missile.movetype = MOVETYPE_FLY;
202         missile.solid = SOLID_BBOX;
203         setmodel (missile, "models/rocket.md3");
204         setsize (missile, '0 0 0', '0 0 0');
205
206         setorigin (missile, w_shotorg);
207         if(cvar("g_laserguided_missile") && self.laser_on)
208                 missile.velocity = w_shotdir * cvar("g_balance_rocketlauncher_laserguided_speed");
209         else
210                 missile.velocity = w_shotdir * cvar("g_balance_rocketlauncher_speed");
211         missile.angles = vectoangles (missile.velocity);
212
213         missile.touch = W_Rocket_Touch;
214         missile.think = W_Rocket_Think;
215         missile.nextthink = time;
216         missile.cnt = time + cvar("g_balance_rocketlauncher_lifetime");
217         missile.effects = EF_NOSHADOW;
218         sound (missile, CHAN_BODY, "weapons/rocket_fly.wav", 0.4, ATTN_NORM);
219         missile.flags = FL_PROJECTILE;
220
221         flash = spawn ();
222         setorigin (flash, w_shotorg);
223         setmodel (flash, "models/flash.md3");
224         flash.angles = vectoangles (w_shotdir);
225         SUB_SetFade (flash, time, 0.4);
226         flash.effects = flash.effects | EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
227 }
228
229 float(float req) w_rlauncher =
230 {
231         if (req == WR_AIM)
232         {
233                 // aim and decide to fire if appropriate
234                 self.button0 = bot_aim(cvar("g_balance_rocketlauncher_speed"), 0, cvar("g_balance_rocketlauncher_lifetime"), FALSE);
235                 if(skill >= 2) // skill 0 and 1 bots won't detonate rockets!
236                 {
237                         // decide whether to detonate rockets
238                         local entity missile, targetlist, targ;
239                         local float edgedamage, coredamage, edgeradius, recipricoledgeradius, d;
240                         local float selfdamage, teamdamage, enemydamage;
241                         edgedamage = cvar("g_balance_rocketlauncher_edgedamage");
242                         coredamage = cvar("g_balance_rocketlauncher_damage");
243                         edgeradius = cvar("g_balance_rocketlauncher_radius");
244                         recipricoledgeradius = 1 / edgeradius;
245                         selfdamage = 0;
246                         teamdamage = 0;
247                         enemydamage = 0;
248                         targetlist = findchainfloat(bot_attack, TRUE);
249                         missile = find(world, classname, "missile");
250                         while (missile)
251                         {
252                                 targ = targetlist;
253                                 while (targ)
254                                 {
255                                         d = vlen(targ.origin + (targ.mins + targ.maxs) * 0.5 - missile.origin);
256                                         d = edgedamage + (coredamage - edgedamage) * (1 - d * recipricoledgeradius);
257                                         // count potential damage according to type of target
258                                         if (targ == self)
259                                                 selfdamage = selfdamage + d;
260                                         else if (targ.team == self.team && teamplay)
261                                                 teamdamage = teamdamage + d;
262                                         else if (bot_shouldattack(targ))
263                                                 enemydamage = enemydamage + d;
264                                         targ = targ.chain;
265                                 }
266                                 missile = find(missile, classname, "missile");
267                         }
268                         local float desirabledamage;
269                         desirabledamage = enemydamage;
270                         if (teamplay != 1 && time > self.invincible_finished && time > self.spawnshieldtime)
271                                 desirabledamage = desirabledamage - selfdamage * cvar("g_balance_selfdamagepercent");
272                         if (self.team && teamplay == 2)
273                                 desirabledamage = desirabledamage - teamdamage;
274                         // if we would be doing at least half of the core damage, detonate it
275                         // but don't fire a new shot at the same time!
276                         if (desirabledamage >= 0.5 * coredamage)
277                         {
278                                 self.button3 = TRUE;
279                                 self.button0 = FALSE;
280                         }
281                 }
282         }
283         else if (req == WR_THINK)
284         {
285                 if (self.button0)
286                 if (weapon_prepareattack(0, cvar("g_balance_rocketlauncher_refire")))
287                 {
288                         W_Rocket_Attack();
289                         weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_rocketlauncher_animtime"), w_ready);
290                 }
291                 if (self.button3)
292                 if(time > self.rl_sound)
293                 {
294                         self.rl_sound = time + 1;
295                         sound (self, CHAN_BODY, "weapons/rocket_det.ogg", 0.5, ATTN_NORM);
296                 }
297                 if (self.button3)
298                 if(cvar("g_laserguided_missile"))
299                 if(self.exteriorweaponentity.attack_finished < time)
300                 {
301                         self.exteriorweaponentity.attack_finished = time + 0.4;
302                         self.laser_on = !self.laser_on;
303                         sound (self, CHAN_AUTO, "weapons/tink1.ogg", 1, ATTN_NORM);
304                 }
305         }
306         else if (req == WR_SETUP)
307                 weapon_setup(WEP_ROCKET_LAUNCHER, "rl", IT_ROCKETS);
308         else if (req == WR_CHECKAMMO1)
309         {
310                 // don't switch while guiding a missile
311                 if ((self.attack_finished <= time || self.weapon != WEP_ROCKET_LAUNCHER)
312                         && self.ammo_rockets < cvar("g_balance_rocketlauncher_ammo"))
313                         return FALSE;
314         }
315         else if (req == WR_CHECKAMMO2)
316                 return FALSE;
317         return TRUE;
318 };