]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/w_devastator.qc
Merge remote-tracking branch 'origin/master' into samual/weapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / w_devastator.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(
3 /* WEP_##id  */ DEVASTATOR,
4 /* function  */ W_Devastator,
5 /* ammotype  */ ammo_rockets,
6 /* impulse   */ 9,
7 /* flags     */ WEP_FLAG_NORMAL | WEP_FLAG_RELOADABLE | WEP_FLAG_CANCLIMB | WEP_TYPE_SPLASH,
8 /* rating    */ BOT_PICKUP_RATING_HIGH,
9 /* color     */ '1 1 0',
10 /* modelname */ "rl",
11 /* simplemdl */ "foobar",
12 /* crosshair */ "gfx/crosshairrocketlauncher 0.5875",
13 /* wepimg    */ "weaponrocketlauncher",
14 /* refname   */ "devastator",
15 /* wepname   */ _("Devastator")
16 );
17
18 #define DEVASTATOR_SETTINGS(w_cvar,w_prop) DEVASTATOR_SETTINGS_LIST(w_cvar, w_prop, DEVASTATOR, devastator)
19 #define DEVASTATOR_SETTINGS_LIST(w_cvar,w_prop,id,sn) \
20         w_cvar(id, sn, NONE, ammo) \
21         w_cvar(id, sn, NONE, animtime) \
22         w_cvar(id, sn, NONE, damage) \
23         w_cvar(id, sn, NONE, damageforcescale) \
24         w_cvar(id, sn, NONE, detonatedelay) \
25         w_cvar(id, sn, NONE, edgedamage) \
26         w_cvar(id, sn, NONE, force) \
27         w_cvar(id, sn, NONE, guidedelay) \
28         w_cvar(id, sn, NONE, guidegoal) \
29         w_cvar(id, sn, NONE, guiderate) \
30         w_cvar(id, sn, NONE, guideratedelay) \
31         w_cvar(id, sn, NONE, guidestop) \
32         w_cvar(id, sn, NONE, health) \
33         w_cvar(id, sn, NONE, lifetime) \
34         w_cvar(id, sn, NONE, radius) \
35         w_cvar(id, sn, NONE, refire) \
36         w_cvar(id, sn, NONE, remote_damage) \
37         w_cvar(id, sn, NONE, remote_edgedamage) \
38         w_cvar(id, sn, NONE, remote_force) \
39         w_cvar(id, sn, NONE, remote_radius) \
40         w_cvar(id, sn, NONE, speed) \
41         w_cvar(id, sn, NONE, speedaccel) \
42         w_cvar(id, sn, NONE, speedstart) \
43         w_prop(id, sn, float,  reloading_ammo, reload_ammo) \
44         w_prop(id, sn, float,  reloading_time, reload_time) \
45         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
46         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
47         w_prop(id, sn, string, weaponreplace, weaponreplace) \
48         w_prop(id, sn, float,  weaponstart, weaponstart) \
49         w_prop(id, sn, float,  weaponstartoverride, weaponstartoverride)
50
51 #ifdef SVQC
52 DEVASTATOR_SETTINGS(WEP_ADD_CVAR, WEP_ADD_PROP)
53 .float rl_release;
54 .float rl_detonate_later;
55 #endif
56 #else
57 #ifdef SVQC
58 void spawnfunc_weapon_devastator() { weapon_defaultspawnfunc(WEP_DEVASTATOR); }
59 void spawnfunc_weapon_rocketlauncher() { spawnfunc_weapon_devastator(); }
60
61 void W_Devastator_Unregister()
62 {
63         if(self.realowner && self.realowner.lastrocket == self)
64         {
65                 self.realowner.lastrocket = world;
66                 // self.realowner.rl_release = 1;
67         }
68 }
69
70 void W_Devastator_Explode()
71 {
72         W_Devastator_Unregister();
73
74         if(other.takedamage == DAMAGE_AIM)
75                 if(IS_PLAYER(other))
76                         if(DIFF_TEAM(self.realowner, other))
77                                 if(other.deadflag == DEAD_NO)
78                                         if(IsFlying(other))
79                                                 Send_Notification(NOTIF_ONE, self.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_AIRSHOT);
80
81         self.event_damage = func_null;
82         self.takedamage = DAMAGE_NO;
83
84         RadiusDamage (self, self.realowner, WEP_CVAR(devastator, damage), WEP_CVAR(devastator, edgedamage), WEP_CVAR(devastator, radius), world, world, WEP_CVAR(devastator, force), self.projectiledeathtype, other);
85
86         if (self.realowner.weapon == WEP_DEVASTATOR)
87         {
88                 if(self.realowner.WEP_AMMO(DEVASTATOR) < WEP_CVAR(devastator, ammo))
89                 {
90                         self.realowner.cnt = WEP_DEVASTATOR;
91                         ATTACK_FINISHED(self.realowner) = time;
92                         self.realowner.switchweapon = w_getbestweapon(self.realowner);
93                 }
94         }
95         remove (self);
96 }
97
98 void W_Devastator_DoRemoteExplode()
99 {
100         W_Devastator_Unregister();
101
102         self.event_damage = func_null;
103         self.takedamage = DAMAGE_NO;
104
105         RadiusDamage (self, self.realowner, WEP_CVAR(devastator, remote_damage), WEP_CVAR(devastator, remote_edgedamage), WEP_CVAR(devastator, remote_radius), world, world, WEP_CVAR(devastator, remote_force), self.projectiledeathtype | HITTYPE_BOUNCE, world);
106
107         if (self.realowner.weapon == WEP_DEVASTATOR)
108         {
109                 if(self.realowner.WEP_AMMO(DEVASTATOR) < WEP_CVAR(devastator, ammo))
110                 {
111                         self.realowner.cnt = WEP_DEVASTATOR;
112                         ATTACK_FINISHED(self.realowner) = time;
113                         self.realowner.switchweapon = w_getbestweapon(self.realowner);
114                 }
115         }
116         remove (self);
117 }
118
119 void W_Devastator_RemoteExplode()
120 {
121         if(self.realowner.deadflag == DEAD_NO)
122         if(self.realowner.lastrocket)
123         {
124                 if((self.spawnshieldtime >= 0)
125                         ? (time >= self.spawnshieldtime) // timer
126                         : (vlen(NearestPointOnBox(self.realowner, self.origin) - self.origin) > WEP_CVAR(devastator, remote_radius)) // safety device
127                 )
128                 {
129                         W_Devastator_DoRemoteExplode();
130                 }
131         }
132 }
133
134 vector W_Devastator_SteerTo(vector thisdir, vector goaldir, float maxturn_cos)
135 {
136         if(thisdir * goaldir > maxturn_cos)
137                 return goaldir;
138         if(thisdir * goaldir < -0.9998) // less than 1 degree and opposite
139                 return thisdir; // refuse to guide (better than letting a numerical error happen)
140         float f, m2;
141         vector v;
142         // solve:
143         //   g = normalize(thisdir + goaldir * X)
144         //   thisdir * g = maxturn
145         //
146         //   gg = thisdir + goaldir * X
147         //   (thisdir * gg)^2 = maxturn^2 * (gg * gg)
148         //
149         //   (1 + (thisdir * goaldir) * X)^2 = maxturn^2 * (1 + X*X + 2 * X * thisdir * goaldir)
150         f = thisdir * goaldir;
151         //   (1 + f * X)^2 = maxturn^2 * (1 + X*X + 2 * X * f)
152         //   0 = (m^2 - f^2) * x^2 + (2 * f * (m^2 - 1)) * x + (m^2 - 1)
153         m2 = maxturn_cos * maxturn_cos;
154         v = solve_quadratic(m2 - f * f, 2 * f * (m2 - 1), m2 - 1);
155         return normalize(thisdir + goaldir * v_y); // the larger solution!
156 }
157 // assume thisdir == -goaldir:
158 //   f == -1
159 //   v = solve_qadratic(m2 - 1, -2 * (m2 - 1), m2 - 1)
160 //   (m2 - 1) x^2 - 2 * (m2 - 1) * x + (m2 - 1) = 0
161 //   x^2 - 2 * x + 1 = 0
162 //   (x - 1)^2 = 0
163 //   x = 1
164 //   normalize(thisdir + goaldir)
165 //   normalize(0)
166
167 void W_Devastator_Think (void)
168 {
169         vector desireddir, olddir, newdir, desiredorigin, goal;
170 #if 0
171         float cosminang, cosmaxang, cosang;
172 #endif
173         float velspeed, f;
174         self.nextthink = time;
175         if (time > self.cnt)
176         {
177                 other = world;
178                 self.projectiledeathtype |= HITTYPE_BOUNCE;
179                 W_Devastator_Explode ();
180                 return;
181         }
182
183         // accelerate
184         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
185         velspeed = WEP_CVAR(devastator, speed) * g_weaponspeedfactor - (self.velocity * v_forward);
186         if (velspeed > 0)
187                 self.velocity = self.velocity + v_forward * min(WEP_CVAR(devastator, speedaccel) * g_weaponspeedfactor * frametime, velspeed);
188
189         // laser guided, or remote detonation
190         if (self.realowner.weapon == WEP_DEVASTATOR)
191         {
192                 if(self == self.realowner.lastrocket)
193                 if(!self.realowner.rl_release)
194                 if(!self.BUTTON_ATCK2)
195                 if(WEP_CVAR(devastator, guiderate))
196                 if(time > self.pushltime)
197                 if(self.realowner.deadflag == DEAD_NO)
198                 {
199                         f = WEP_CVAR(devastator, guideratedelay);
200                         if(f)
201                                 f = bound(0, (time - self.pushltime) / f, 1);
202                         else
203                                 f = 1;
204
205                         velspeed = vlen(self.velocity);
206
207                         makevectors(self.realowner.v_angle);
208                         desireddir = WarpZone_RefSys_TransformVelocity(self.realowner, self, v_forward);
209                         desiredorigin = WarpZone_RefSys_TransformOrigin(self.realowner, self, self.realowner.origin + self.realowner.view_ofs);
210                         olddir = normalize(self.velocity);
211
212                         // now it gets tricky... we want to move like some curve to approximate the target direction
213                         // but we are limiting the rate at which we can turn!
214                         goal = desiredorigin + ((self.origin - desiredorigin) * desireddir + WEP_CVAR(devastator, guidegoal)) * desireddir;
215                         newdir = W_Devastator_SteerTo(olddir, normalize(goal - self.origin), cos(WEP_CVAR(devastator, guiderate) * f * frametime * DEG2RAD));
216
217                         self.velocity = newdir * velspeed;
218                         self.angles = vectoangles(self.velocity);
219
220                         if(!self.count)
221                         {
222                                 pointparticles(particleeffectnum("rocket_guide"), self.origin, self.velocity, 1);
223                                 // TODO add a better sound here
224                                 sound (self.realowner, CH_WEAPON_B, "weapons/rocket_mode.wav", VOL_BASE, ATTN_NORM);
225                                 self.count = 1;
226                         }
227                 }
228
229                 if(self.rl_detonate_later)
230                         W_Devastator_RemoteExplode();
231         }
232
233         if(self.csqcprojectile_clientanimate == 0)
234                 UpdateCSQCProjectile(self);
235 }
236
237 void W_Devastator_Touch (void)
238 {
239         if(WarpZone_Projectile_Touch())
240         {
241                 if(wasfreed(self))
242                         W_Devastator_Unregister();
243                 return;
244         }
245         W_Devastator_Unregister();
246         W_Devastator_Explode ();
247 }
248
249 void W_Devastator_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
250 {
251         if (self.health <= 0)
252                 return;
253         
254         if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
255                 return; // g_projectiles_damage says to halt
256                 
257         self.health = self.health - damage;
258         self.angles = vectoangles(self.velocity);
259         
260         if (self.health <= 0)
261                 W_PrepareExplosionByDamage(attacker, W_Devastator_Explode);
262 }
263
264 void W_Devastator_Attack (void)
265 {
266         entity missile;
267         entity flash;
268
269         W_DecreaseAmmo(WEP_CVAR(devastator, ammo));
270
271         W_SetupShot_ProjectileSize (self, '-3 -3 -3', '3 3 3', FALSE, 5, "weapons/rocket_fire.wav", CH_WEAPON_A, WEP_CVAR(devastator, damage));
272         pointparticles(particleeffectnum("rocketlauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
273
274         missile = WarpZone_RefSys_SpawnSameRefSys(self);
275         missile.owner = missile.realowner = self;
276         self.lastrocket = missile;
277         if(WEP_CVAR(devastator, detonatedelay) >= 0)
278                 missile.spawnshieldtime = time + WEP_CVAR(devastator, detonatedelay);
279         else
280                 missile.spawnshieldtime = -1;
281         missile.pushltime = time + WEP_CVAR(devastator, guidedelay);
282         missile.classname = "rocket";
283         missile.bot_dodge = TRUE;
284         missile.bot_dodgerating = WEP_CVAR(devastator, damage) * 2; // * 2 because it can be detonated inflight which makes it even more dangerous
285
286         missile.takedamage = DAMAGE_YES;
287         missile.damageforcescale = WEP_CVAR(devastator, damageforcescale);
288         missile.health = WEP_CVAR(devastator, health);
289         missile.event_damage = W_Devastator_Damage;
290         missile.damagedbycontents = TRUE;
291
292         missile.movetype = MOVETYPE_FLY;
293         PROJECTILE_MAKETRIGGER(missile);
294         missile.projectiledeathtype = WEP_DEVASTATOR;
295         setsize (missile, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot
296
297         setorigin (missile, w_shotorg - v_forward * 3); // move it back so it hits the wall at the right point
298         W_SetupProjVelocity_Basic(missile, WEP_CVAR(devastator, speedstart), 0);
299         missile.angles = vectoangles (missile.velocity);
300
301         missile.touch = W_Devastator_Touch;
302         missile.think = W_Devastator_Think;
303         missile.nextthink = time;
304         missile.cnt = time + WEP_CVAR(devastator, lifetime);
305         missile.flags = FL_PROJECTILE;
306         missile.missile_flags = MIF_SPLASH; 
307
308         CSQCProjectile(missile, WEP_CVAR(devastator, guiderate) == 0 && WEP_CVAR(devastator, speedaccel) == 0, PROJECTILE_ROCKET, FALSE); // because of fly sound
309
310         // muzzle flash for 1st person view
311         flash = spawn ();
312         setmodel (flash, "models/flash.md3"); // precision set below
313         SUB_SetFade (flash, time, 0.1);
314         flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
315         W_AttachToShotorg(flash, '5 0 0');
316
317         // common properties
318         other = missile; MUTATOR_CALLHOOK(EditProjectile);
319 }
320
321 float W_Devastator(float req)
322 {
323         entity rock;
324         float rockfound;
325         float ammo_amount;
326         switch(req)
327         {
328                 case WR_AIM: // WEAPONTODO: rewrite this, it's WAY too complicated for what it should be
329                 {
330                         // aim and decide to fire if appropriate
331                         self.BUTTON_ATCK = bot_aim(WEP_CVAR(devastator, speed), 0, WEP_CVAR(devastator, lifetime), FALSE);
332                         if(skill >= 2) // skill 0 and 1 bots won't detonate rockets!
333                         {
334                                 // decide whether to detonate rockets
335                                 entity missile, targetlist, targ;
336                                 targetlist = findchainfloat(bot_attack, TRUE);
337                                 for(missile = world; (missile = find(missile, classname, "rocket")); ) if(missile.realowner == self)
338                                 {
339                                         targ = targetlist;
340                                         while(targ)
341                                         {
342                                                 if(targ != missile.realowner && vlen(targ.origin - missile.origin) < WEP_CVAR(devastator, radius))
343                                                 {
344                                                         self.BUTTON_ATCK2 = TRUE;
345                                                         break;
346                                                 }
347                                                 targ = targ.chain;
348                                         }
349                                 }
350                                 
351                                 if(self.BUTTON_ATCK2) self.BUTTON_ATCK = FALSE;
352                         }
353                         
354                         return TRUE;
355                 }
356                 case WR_THINK:
357                 {
358                         if(WEP_CVAR(devastator, reload_ammo) && self.clip_load < WEP_CVAR(devastator, ammo)) // forced reload
359                                 WEP_ACTION(self.weapon, WR_RELOAD);
360                         else
361                         {
362                                 if(self.BUTTON_ATCK)
363                                 {
364                                         if(self.rl_release || WEP_CVAR(devastator, guidestop))
365                                         if(weapon_prepareattack(0, WEP_CVAR(devastator, refire)))
366                                         {
367                                                 W_Devastator_Attack();
368                                                 weapon_thinkf(WFRAME_FIRE1, WEP_CVAR(devastator, animtime), w_ready);
369                                                 self.rl_release = 0;
370                                         }
371                                 }
372                                 else
373                                         self.rl_release = 1;
374
375                                 if(self.BUTTON_ATCK2)
376                                 {
377                                         rockfound = 0;
378                                         for(rock = world; (rock = find(rock, classname, "rocket")); ) if(rock.realowner == self)
379                                         {
380                                                 if(!rock.rl_detonate_later)
381                                                 {
382                                                         rock.rl_detonate_later = TRUE;
383                                                         rockfound = 1;
384                                                 }
385                                         }
386                                         if(rockfound)
387                                                 sound (self, CH_WEAPON_B, "weapons/rocket_det.wav", VOL_BASE, ATTN_NORM);
388                                 }
389                         }
390                         
391                         return TRUE;
392                 }
393                 case WR_INIT:
394                 {
395                         if(autocvar_sv_precacheweapons)
396                         {
397                                 precache_model("models/flash.md3");
398                                 precache_model("models/weapons/g_rl.md3");
399                                 precache_model("models/weapons/v_rl.md3");
400                                 precache_model("models/weapons/h_rl.iqm");
401                                 precache_sound("weapons/rocket_det.wav");
402                                 precache_sound("weapons/rocket_fire.wav");
403                                 precache_sound("weapons/rocket_mode.wav");
404                         }
405                         DEVASTATOR_SETTINGS(WEP_SKIP_CVAR, WEP_SET_PROP)
406                         return TRUE;
407                 }
408                 case WR_SETUP:
409                 {
410                         self.rl_release = 1;
411                         return TRUE;
412                 }
413                 case WR_CHECKAMMO1:
414                 {
415                         // don't switch while guiding a missile
416                         if (ATTACK_FINISHED(self) <= time || self.weapon != WEP_DEVASTATOR)
417                         {
418                                 ammo_amount = FALSE;
419                                 if(WEP_CVAR(devastator, reload_ammo))
420                                 {
421                                         if(self.WEP_AMMO(DEVASTATOR) < WEP_CVAR(devastator, ammo) && self.(weapon_load[WEP_DEVASTATOR]) < WEP_CVAR(devastator, ammo))
422                                                 ammo_amount = TRUE;
423                                 }
424                                 else if(self.WEP_AMMO(DEVASTATOR) < WEP_CVAR(devastator, ammo))
425                                         ammo_amount = TRUE;
426                                 return !ammo_amount;
427                         }
428                         
429                         return TRUE;
430                 }
431                 case WR_CHECKAMMO2:
432                 {
433                         return FALSE;
434                 }
435                 case WR_CONFIG:
436                 {
437                         DEVASTATOR_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS)
438                         return TRUE;
439                 }
440                 case WR_RESETPLAYER:
441                 {
442                         self.rl_release = 0;
443                         return TRUE;
444                 }
445                 case WR_RELOAD:
446                 {
447                         W_Reload(WEP_CVAR(devastator, ammo), "weapons/reload.wav");
448                         return TRUE;
449                 }
450                 case WR_SUICIDEMESSAGE:
451                 {
452                         return WEAPON_DEVASTATOR_SUICIDE;
453                 }
454                 case WR_KILLMESSAGE:
455                 {
456                         if((w_deathtype & HITTYPE_BOUNCE) || (w_deathtype & HITTYPE_SPLASH))
457                                 return WEAPON_DEVASTATOR_MURDER_SPLASH;
458                         else
459                                 return WEAPON_DEVASTATOR_MURDER_DIRECT;
460                 }
461         }
462         return TRUE;
463 }
464 #endif
465 #ifdef CSQC
466 float W_Devastator(float req)
467 {
468         switch(req)
469         {
470                 case WR_IMPACTEFFECT:
471                 {
472                         vector org2;
473                         org2 = w_org + w_backoff * 12;
474                         pointparticles(particleeffectnum("rocket_explode"), org2, '0 0 0', 1);
475                         if(!w_issilent)
476                                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
477                                 
478                         return TRUE;
479                 }
480                 case WR_INIT:
481                 {
482                         precache_sound("weapons/rocket_impact.wav");
483                         return TRUE;
484                 }
485                 case WR_ZOOMRETICLE:
486                 {
487                         // no weapon specific image for this weapon
488                         return FALSE;
489                 }
490         }
491         return TRUE;
492 }
493 #endif
494 #endif