2 CLASS(Devastator, Weapon)
3 /* ammotype */ ATTRIB(Devastator, ammo_field, .int, ammo_rockets)
4 /* impulse */ ATTRIB(Devastator, impulse, int, 9)
5 /* flags */ ATTRIB(Devastator, spawnflags, int, WEP_FLAG_NORMAL | WEP_FLAG_RELOADABLE | WEP_FLAG_CANCLIMB | WEP_TYPE_SPLASH);
6 /* rating */ ATTRIB(Devastator, bot_pickupbasevalue, float, BOT_PICKUP_RATING_HIGH);
7 /* color */ ATTRIB(Devastator, wpcolor, vector, '1 1 0');
8 /* modelname */ ATTRIB(Devastator, mdl, string, "rl");
10 /* model */ ATTRIB(Devastator, m_model, Model, MDL_DEVASTATOR_ITEM);
12 /* crosshair */ ATTRIB(Devastator, w_crosshair, string, "gfx/crosshairrocketlauncher");
13 /* crosshair */ ATTRIB(Devastator, w_crosshair_size, float, 0.7);
14 /* wepimg */ ATTRIB(Devastator, model2, string, "weaponrocketlauncher");
15 /* refname */ ATTRIB(Devastator, netname, string, "devastator");
16 /* wepname */ ATTRIB(Devastator, m_name, string, _("Devastator"));
18 #define X(BEGIN, P, END, class, prefix) \
20 P(class, prefix, ammo, float, NONE) \
21 P(class, prefix, animtime, float, NONE) \
22 P(class, prefix, damageforcescale, float, NONE) \
23 P(class, prefix, damage, float, NONE) \
24 P(class, prefix, detonatedelay, float, NONE) \
25 P(class, prefix, edgedamage, float, NONE) \
26 P(class, prefix, force, float, NONE) \
27 P(class, prefix, guidedelay, float, NONE) \
28 P(class, prefix, guidegoal, float, NONE) \
29 P(class, prefix, guideratedelay, float, NONE) \
30 P(class, prefix, guiderate, float, NONE) \
31 P(class, prefix, guidestop, float, NONE) \
32 P(class, prefix, health, float, NONE) \
33 P(class, prefix, lifetime, float, NONE) \
34 P(class, prefix, radius, float, NONE) \
35 P(class, prefix, refire, float, NONE) \
36 P(class, prefix, reload_ammo, float, NONE) \
37 P(class, prefix, reload_time, float, NONE) \
38 P(class, prefix, remote_damage, float, NONE) \
39 P(class, prefix, remote_edgedamage, float, NONE) \
40 P(class, prefix, remote_force, float, NONE) \
41 P(class, prefix, remote_jump_damage, float, NONE) \
42 P(class, prefix, remote_jump_radius, float, NONE) \
43 P(class, prefix, remote_jump_velocity_z_add, float, NONE) \
44 P(class, prefix, remote_jump_velocity_z_max, float, NONE) \
45 P(class, prefix, remote_jump_velocity_z_min, float, NONE) \
46 P(class, prefix, remote_radius, float, NONE) \
47 P(class, prefix, speedaccel, float, NONE) \
48 P(class, prefix, speedstart, float, NONE) \
49 P(class, prefix, speed, float, NONE) \
50 P(class, prefix, switchdelay_drop, float, NONE) \
51 P(class, prefix, switchdelay_raise, float, NONE) \
52 P(class, prefix, weaponreplace, string,NONE) \
53 P(class, prefix, weaponstartoverride, float, NONE) \
54 P(class, prefix, weaponstart, float, NONE) \
55 P(class, prefix, weaponthrowable, float, NONE) \
57 W_PROPS(X, Devastator, devastator)
61 REGISTER_WEAPON(DEVASTATOR, devastator, NEW(Devastator));
65 .float rl_detonate_later;
70 spawnfunc(weapon_devastator) { weapon_defaultspawnfunc(this, WEP_DEVASTATOR); }
71 spawnfunc(weapon_rocketlauncher) { spawnfunc_weapon_devastator(this); }
75 void W_Devastator_Unregister()
77 if(self.realowner && self.realowner.lastrocket == self)
79 self.realowner.lastrocket = world;
80 // self.realowner.rl_release = 1;
84 void W_Devastator_Explode()
86 W_Devastator_Unregister();
88 if(other.takedamage == DAMAGE_AIM)
90 if(DIFF_TEAM(self.realowner, other))
93 Send_Notification(NOTIF_ONE, self.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_AIRSHOT);
95 self.event_damage = func_null;
96 self.takedamage = DAMAGE_NO;
101 WEP_CVAR(devastator, damage),
102 WEP_CVAR(devastator, edgedamage),
103 WEP_CVAR(devastator, radius),
106 WEP_CVAR(devastator, force),
107 self.projectiledeathtype,
111 Weapon thiswep = WEP_DEVASTATOR;
112 if(PS(self.realowner).m_weapon == thiswep)
114 if(self.realowner.(thiswep.ammo_field) < WEP_CVAR(devastator, ammo))
115 if(!(self.realowner.items & IT_UNLIMITED_WEAPON_AMMO))
117 self.realowner.cnt = WEP_DEVASTATOR.m_id;
118 int slot = 0; // TODO: unhardcode
119 ATTACK_FINISHED(self.realowner, slot) = time;
120 PS(self.realowner).m_switchweapon = w_getbestweapon(self.realowner);
126 void W_Devastator_DoRemoteExplode(.entity weaponentity)
128 W_Devastator_Unregister();
130 self.event_damage = func_null;
131 self.takedamage = DAMAGE_NO;
133 float handled_as_rocketjump = false;
135 entity head = WarpZone_FindRadius(
137 WEP_CVAR(devastator, remote_jump_radius),
143 if(head.takedamage && (head == self.realowner))
145 float distance_to_head = vlen(self.origin - head.WarpZone_findradius_nearest);
146 if(distance_to_head <= WEP_CVAR(devastator, remote_jump_radius))
148 // we handled this as a rocketjump :)
149 handled_as_rocketjump = true;
152 head.velocity_x *= 0.9;
153 head.velocity_y *= 0.9;
154 head.velocity_z = bound(
155 WEP_CVAR(devastator, remote_jump_velocity_z_min),
156 head.velocity.z + WEP_CVAR(devastator, remote_jump_velocity_z_add),
157 WEP_CVAR(devastator, remote_jump_velocity_z_max)
164 WEP_CVAR(devastator, remote_jump_damage),
165 WEP_CVAR(devastator, remote_jump_damage),
166 WEP_CVAR(devastator, remote_jump_radius),
170 self.projectiledeathtype | HITTYPE_BOUNCE,
182 WEP_CVAR(devastator, remote_damage),
183 WEP_CVAR(devastator, remote_edgedamage),
184 WEP_CVAR(devastator, remote_radius),
185 (handled_as_rocketjump ? head : world),
187 WEP_CVAR(devastator, remote_force),
188 self.projectiledeathtype | HITTYPE_BOUNCE,
192 Weapon thiswep = WEP_DEVASTATOR;
193 if(PS(self.realowner).m_weapon == thiswep)
195 if(self.realowner.(thiswep.ammo_field) < WEP_CVAR(devastator, ammo))
196 if(!(self.realowner.items & IT_UNLIMITED_WEAPON_AMMO))
198 self.realowner.cnt = WEP_DEVASTATOR.m_id;
199 int slot = weaponslot(weaponentity);
200 ATTACK_FINISHED(self.realowner, slot) = time;
201 PS(self.realowner).m_switchweapon = w_getbestweapon(self.realowner);
207 void W_Devastator_RemoteExplode(.entity weaponentity)
209 if(!IS_DEAD(self.realowner))
210 if(self.realowner.lastrocket)
212 if((self.spawnshieldtime >= 0)
213 ? (time >= self.spawnshieldtime) // timer
214 : (vdist(NearestPointOnBox(self.realowner, self.origin) - self.origin, >, WEP_CVAR(devastator, remote_radius))) // safety device
217 W_Devastator_DoRemoteExplode(weaponentity);
222 vector W_Devastator_SteerTo(vector thisdir, vector goaldir, float maxturn_cos)
224 if(thisdir * goaldir > maxturn_cos)
226 if(thisdir * goaldir < -0.9998) // less than 1 degree and opposite
227 return thisdir; // refuse to guide (better than letting a numerical error happen)
231 // g = normalize(thisdir + goaldir * X)
232 // thisdir * g = maxturn
234 // gg = thisdir + goaldir * X
235 // (thisdir * gg)^2 = maxturn^2 * (gg * gg)
237 // (1 + (thisdir * goaldir) * X)^2 = maxturn^2 * (1 + X*X + 2 * X * thisdir * goaldir)
238 f = thisdir * goaldir;
239 // (1 + f * X)^2 = maxturn^2 * (1 + X*X + 2 * X * f)
240 // 0 = (m^2 - f^2) * x^2 + (2 * f * (m^2 - 1)) * x + (m^2 - 1)
241 m2 = maxturn_cos * maxturn_cos;
242 v = solve_quadratic(m2 - f * f, 2 * f * (m2 - 1), m2 - 1);
243 return normalize(thisdir + goaldir * v.y); // the larger solution!
245 // assume thisdir == -goaldir:
247 // v = solve_qadratic(m2 - 1, -2 * (m2 - 1), m2 - 1)
248 // (m2 - 1) x^2 - 2 * (m2 - 1) * x + (m2 - 1) = 0
249 // x^2 - 2 * x + 1 = 0
252 // normalize(thisdir + goaldir)
255 void W_Devastator_Think()
257 vector desireddir, olddir, newdir, desiredorigin, goal;
259 self.nextthink = time;
263 self.projectiledeathtype |= HITTYPE_BOUNCE;
264 W_Devastator_Explode();
269 makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0');
270 velspeed = WEP_CVAR(devastator, speed) * W_WeaponSpeedFactor() - (self.velocity * v_forward);
272 self.velocity = self.velocity + v_forward * min(WEP_CVAR(devastator, speedaccel) * W_WeaponSpeedFactor() * frametime, velspeed);
274 // laser guided, or remote detonation
275 if(PS(self.realowner).m_weapon == WEP_DEVASTATOR)
277 if(self == self.realowner.lastrocket)
278 if(!self.realowner.rl_release)
279 if(!PHYS_INPUT_BUTTON_ATCK2(self))
280 if(WEP_CVAR(devastator, guiderate))
281 if(time > self.pushltime)
282 if(!IS_DEAD(self.realowner))
284 f = WEP_CVAR(devastator, guideratedelay);
286 f = bound(0, (time - self.pushltime) / f, 1);
290 velspeed = vlen(self.velocity);
292 makevectors(self.realowner.v_angle);
293 desireddir = WarpZone_RefSys_TransformVelocity(self.realowner, self, v_forward);
294 desiredorigin = WarpZone_RefSys_TransformOrigin(self.realowner, self, self.realowner.origin + self.realowner.view_ofs);
295 olddir = normalize(self.velocity);
297 // now it gets tricky... we want to move like some curve to approximate the target direction
298 // but we are limiting the rate at which we can turn!
299 goal = desiredorigin + ((self.origin - desiredorigin) * desireddir + WEP_CVAR(devastator, guidegoal)) * desireddir;
300 newdir = W_Devastator_SteerTo(olddir, normalize(goal - self.origin), cos(WEP_CVAR(devastator, guiderate) * f * frametime * DEG2RAD));
302 self.velocity = newdir * velspeed;
303 self.angles = vectoangles(self.velocity);
307 Send_Effect(EFFECT_ROCKET_GUIDE, self.origin, self.velocity, 1);
308 // TODO add a better sound here
309 sound(self.realowner, CH_WEAPON_B, SND_ROCKET_MODE, VOL_BASE, ATTN_NORM);
314 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
315 if(self.rl_detonate_later)
316 W_Devastator_RemoteExplode(weaponentity);
319 if(self.csqcprojectile_clientanimate == 0)
320 UpdateCSQCProjectile(self);
323 void W_Devastator_Touch()
325 if(WarpZone_Projectile_Touch())
328 W_Devastator_Unregister();
331 W_Devastator_Unregister();
332 W_Devastator_Explode();
335 void W_Devastator_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
340 if(!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
341 return; // g_projectiles_damage says to halt
343 this.health = this.health - damage;
344 this.angles = vectoangles(this.velocity);
347 WITH(entity, self, this, W_PrepareExplosionByDamage(attacker, W_Devastator_Explode));
350 void W_Devastator_Attack(Weapon thiswep)
355 W_DecreaseAmmo(thiswep, self, WEP_CVAR(devastator, ammo));
357 W_SetupShot_ProjectileSize(self, '-3 -3 -3', '3 3 3', false, 5, SND_ROCKET_FIRE, CH_WEAPON_A, WEP_CVAR(devastator, damage));
358 Send_Effect(EFFECT_ROCKET_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
360 missile = WarpZone_RefSys_SpawnSameRefSys(self);
361 missile.owner = missile.realowner = self;
362 self.lastrocket = missile;
363 if(WEP_CVAR(devastator, detonatedelay) >= 0)
364 missile.spawnshieldtime = time + WEP_CVAR(devastator, detonatedelay);
366 missile.spawnshieldtime = -1;
367 missile.pushltime = time + WEP_CVAR(devastator, guidedelay);
368 missile.classname = "rocket";
369 missile.bot_dodge = true;
370 missile.bot_dodgerating = WEP_CVAR(devastator, damage) * 2; // * 2 because it can be detonated inflight which makes it even more dangerous
372 missile.takedamage = DAMAGE_YES;
373 missile.damageforcescale = WEP_CVAR(devastator, damageforcescale);
374 missile.health = WEP_CVAR(devastator, health);
375 missile.event_damage = W_Devastator_Damage;
376 missile.damagedbycontents = true;
378 missile.movetype = MOVETYPE_FLY;
379 PROJECTILE_MAKETRIGGER(missile);
380 missile.projectiledeathtype = WEP_DEVASTATOR.m_id;
381 setsize(missile, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot
383 setorigin(missile, w_shotorg - v_forward * 3); // move it back so it hits the wall at the right point
384 W_SetupProjVelocity_Basic(missile, WEP_CVAR(devastator, speedstart), 0);
385 missile.angles = vectoangles(missile.velocity);
387 missile.touch = W_Devastator_Touch;
388 missile.think = W_Devastator_Think;
389 missile.nextthink = time;
390 missile.cnt = time + WEP_CVAR(devastator, lifetime);
391 missile.flags = FL_PROJECTILE;
392 missile.missile_flags = MIF_SPLASH;
394 CSQCProjectile(missile, WEP_CVAR(devastator, guiderate) == 0 && WEP_CVAR(devastator, speedaccel) == 0, PROJECTILE_ROCKET, false); // because of fly sound
396 // muzzle flash for 1st person view
398 setmodel(flash, MDL_DEVASTATOR_MUZZLEFLASH); // precision set below
399 SUB_SetFade(flash, time, 0.1);
400 flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
401 W_AttachToShotorg(self, flash, '5 0 0');
404 MUTATOR_CALLHOOK(EditProjectile, self, missile);
408 METHOD(Devastator, wr_aim, void(entity thiswep))
410 // aim and decide to fire if appropriate
411 PHYS_INPUT_BUTTON_ATCK(self) = bot_aim(WEP_CVAR(devastator, speed), 0, WEP_CVAR(devastator, lifetime), false);
412 if(skill >= 2) // skill 0 and 1 bots won't detonate rockets!
414 // decide whether to detonate rockets
415 entity missile, targetlist, targ;
416 targetlist = findchainfloat(bot_attack, true);
417 for(missile = world; (missile = find(missile, classname, "rocket")); ) if(missile.realowner == self)
422 if(targ != missile.realowner && vlen(targ.origin - missile.origin) < WEP_CVAR(devastator, radius))
424 PHYS_INPUT_BUTTON_ATCK2(self) = true;
431 if(PHYS_INPUT_BUTTON_ATCK2(self)) PHYS_INPUT_BUTTON_ATCK(self) = false;
435 METHOD(Devastator, wr_aim, void(entity thiswep))
438 // aim and decide to fire if appropriate
439 PHYS_INPUT_BUTTON_ATCK(self) = bot_aim(WEP_CVAR(devastator, speed), 0, WEP_CVAR(devastator, lifetime), false);
440 if(skill >= 2) // skill 0 and 1 bots won't detonate rockets!
442 // decide whether to detonate rockets
443 entity targetlist, targ;
444 float edgedamage, coredamage, edgeradius, recipricoledgeradius, d;
445 float selfdamage, teamdamage, enemydamage;
446 edgedamage = WEP_CVAR(devastator, edgedamage);
447 coredamage = WEP_CVAR(devastator, damage);
448 edgeradius = WEP_CVAR(devastator, radius);
449 recipricoledgeradius = 1 / edgeradius;
453 targetlist = findchainfloat(bot_attack, true);
454 FOREACH_ENTITY_ENT(realowner, self,
456 if(it.classname != "rocket") continue;
461 d = vlen(targ.origin + (targ.mins + targ.maxs) * 0.5 - it.origin);
462 d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
463 // count potential damage according to type of target
465 selfdamage = selfdamage + d;
466 else if(targ.team == self.team && teamplay)
467 teamdamage = teamdamage + d;
468 else if(bot_shouldattack(targ))
469 enemydamage = enemydamage + d;
473 float desirabledamage;
474 desirabledamage = enemydamage;
475 if(time > self.invincible_finished && time > self.spawnshieldtime)
476 desirabledamage = desirabledamage - selfdamage * autocvar_g_balance_selfdamagepercent;
477 if(teamplay && self.team)
478 desirabledamage = desirabledamage - teamdamage;
480 FOREACH_ENTITY_ENT(realowner, self,
482 if(it.classname != "rocket") continue;
484 makevectors(it.v_angle);
486 if(skill > 9) // normal players only do this for the target they are tracking
492 (v_forward * normalize(it.origin - targ.origin)< 0.1)
493 && desirabledamage > 0.1*coredamage
494 ) PHYS_INPUT_BUTTON_ATCK2(self) = true;
500 float distance; distance= bound(300,vlen(self.origin-self.enemy.origin),30000);
501 //As the distance gets larger, a correct detonation gets near imposible
502 //Bots are assumed to use the rocket spawnfunc_light to see if the rocket gets near a player
503 if(v_forward * normalize(it.origin - self.enemy.origin)< 0.1)
504 if(IS_PLAYER(self.enemy))
505 if(desirabledamage >= 0.1*coredamage)
506 if(random()/distance*300 > frametime*bound(0,(10-skill)*0.2,1))
507 PHYS_INPUT_BUTTON_ATCK2(self) = true;
508 // dprint(ftos(random()/distance*300),">");dprint(ftos(frametime*bound(0,(10-skill)*0.2,1)),"\n");
511 // if we would be doing at X percent of the core damage, detonate it
512 // but don't fire a new shot at the same time!
513 if(desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
514 PHYS_INPUT_BUTTON_ATCK2(self) = true;
515 if((skill > 6.5) && (selfdamage > self.health))
516 PHYS_INPUT_BUTTON_ATCK2(self) = false;
517 //if(PHYS_INPUT_BUTTON_ATCK2(self) == true)
518 // dprint(ftos(desirabledamage),"\n");
519 if(PHYS_INPUT_BUTTON_ATCK2(self)) PHYS_INPUT_BUTTON_ATCK(self) = false;
523 METHOD(Devastator, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
525 if(WEP_CVAR(devastator, reload_ammo) && actor.clip_load < WEP_CVAR(devastator, ammo)) { // forced reload
526 thiswep.wr_reload(thiswep, actor, weaponentity);
530 if(actor.rl_release || WEP_CVAR(devastator, guidestop))
531 if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR(devastator, refire)))
533 W_Devastator_Attack(thiswep);
534 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(devastator, animtime), w_ready);
535 actor.rl_release = 0;
539 actor.rl_release = 1;
542 if(PS(actor).m_switchweapon == WEP_DEVASTATOR)
545 bool rockfound = false;
546 for(rock = world; (rock = find(rock, classname, "rocket")); ) if(rock.realowner == actor)
548 if(!rock.rl_detonate_later)
550 rock.rl_detonate_later = true;
555 sound(actor, CH_WEAPON_B, SND_ROCKET_DET, VOL_BASE, ATTN_NORM);
559 METHOD(Devastator, wr_setup, void(entity thiswep))
564 METHOD(Devastator, wr_checkammo1, bool(entity thiswep))
568 // don't switch while guiding a missile
569 if(ATTACK_FINISHED(self, slot) <= time || PS(self).m_weapon != WEP_DEVASTATOR)
572 if(WEP_CVAR(devastator, reload_ammo))
574 if(self.(thiswep.ammo_field) < WEP_CVAR(devastator, ammo) && self.(weapon_load[WEP_DEVASTATOR.m_id]) < WEP_CVAR(devastator, ammo))
577 else if(self.(thiswep.ammo_field) < WEP_CVAR(devastator, ammo))
583 if(self.rl_release == 0)
585 LOG_INFOF("W_Devastator(WR_CHECKAMMO1): %d, %.2f, %d: TRUE\n", self.rl_release, self.(thiswep.ammo_field), WEP_CVAR(devastator, ammo));
590 ammo_amount = self.(thiswep.ammo_field) >= WEP_CVAR(devastator, ammo);
591 ammo_amount += self.(weapon_load[WEP_DEVASTATOR.m_id]) >= WEP_CVAR(devastator, ammo);
592 LOG_INFOF("W_Devastator(WR_CHECKAMMO1): %d, %.2f, %d: %s\n", self.rl_release, self.(thiswep.ammo_field), WEP_CVAR(devastator, ammo), (ammo_amount ? "TRUE" : "FALSE"));
596 float ammo_amount = self.(thiswep.ammo_field) >= WEP_CVAR(devastator, ammo);
597 ammo_amount += self.(weapon_load[WEP_DEVASTATOR.m_id]) >= WEP_CVAR(devastator, ammo);
601 METHOD(Devastator, wr_checkammo2, bool(entity thiswep))
605 METHOD(Devastator, wr_resetplayer, void(entity thiswep))
608 self.lastrocket = NULL; // stop rocket guiding, no revenge from the grave!
611 METHOD(Devastator, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
614 W_Reload(self, WEP_CVAR(devastator, ammo), SND_RELOAD);
616 METHOD(Devastator, wr_suicidemessage, Notification(entity thiswep))
618 return WEAPON_DEVASTATOR_SUICIDE;
620 METHOD(Devastator, wr_killmessage, Notification(entity thiswep))
622 if((w_deathtype & HITTYPE_BOUNCE) || (w_deathtype & HITTYPE_SPLASH))
623 return WEAPON_DEVASTATOR_MURDER_SPLASH;
625 return WEAPON_DEVASTATOR_MURDER_DIRECT;
631 METHOD(Devastator, wr_impacteffect, void(entity thiswep))
635 org2 = w_org + w_backoff * 12;
636 pointparticles(EFFECT_ROCKET_EXPLODE, org2, '0 0 0', 1);
638 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTN_NORM);