1 #include "devastator.qh"
4 spawnfunc(weapon_devastator) { weapon_defaultspawnfunc(this, WEP_DEVASTATOR); }
5 spawnfunc(weapon_rocketlauncher) { spawnfunc_weapon_devastator(this); }
9 void W_Devastator_Unregister(entity this)
11 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
13 .entity weaponentity = weaponentities[slot];
14 if(this.realowner.(weaponentity).lastrocket == this)
15 this.realowner.(weaponentity).lastrocket = NULL;
19 void W_Devastator_Explode(entity this, entity directhitentity)
21 W_Devastator_Unregister(this);
23 if(directhitentity.takedamage == DAMAGE_AIM)
24 if(IS_PLAYER(directhitentity))
25 if(DIFF_TEAM(this.realowner, directhitentity))
26 if(!IS_DEAD(directhitentity))
27 if(IsFlying(directhitentity))
28 Send_Notification(NOTIF_ONE, this.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_AIRSHOT);
30 this.event_damage = func_null;
31 this.takedamage = DAMAGE_NO;
36 WEP_CVAR(devastator, damage),
37 WEP_CVAR(devastator, edgedamage),
38 WEP_CVAR(devastator, radius),
41 WEP_CVAR(devastator, force),
42 this.projectiledeathtype,
46 Weapon thiswep = WEP_DEVASTATOR;
47 .entity weaponentity = this.weaponentity_fld;
48 if(this.realowner.(weaponentity).m_weapon == thiswep)
50 if(this.realowner.(thiswep.ammo_field) < WEP_CVAR(devastator, ammo))
51 if(!(this.realowner.items & IT_UNLIMITED_WEAPON_AMMO))
53 this.realowner.cnt = WEP_DEVASTATOR.m_id;
54 int slot = weaponslot(weaponentity);
55 ATTACK_FINISHED(this.realowner, slot) = time;
56 this.realowner.(weaponentity).m_switchweapon = w_getbestweapon(this.realowner, weaponentity);
62 void W_Devastator_Explode_think(entity this)
64 W_Devastator_Explode(this, NULL);
67 void W_Devastator_DoRemoteExplode(entity this, .entity weaponentity)
69 W_Devastator_Unregister(this);
71 this.event_damage = func_null;
72 this.takedamage = DAMAGE_NO;
74 bool handled_as_rocketjump = false;
77 if(WEP_CVAR(devastator, remote_jump_radius))
79 head = WarpZone_FindRadius(
81 WEP_CVAR(devastator, remote_jump_radius),
87 if(head.takedamage && (head == this.realowner))
89 if(vdist(this.origin - head.WarpZone_findradius_nearest, <=, WEP_CVAR(devastator, remote_jump_radius)))
91 // we handled this as a rocketjump :)
92 handled_as_rocketjump = true;
95 if(WEP_CVAR(devastator, remote_jump_velocity_z_add))
97 head.velocity_x *= 0.9;
98 head.velocity_y *= 0.9;
99 head.velocity_z = bound(
100 WEP_CVAR(devastator, remote_jump_velocity_z_min),
101 head.velocity.z + WEP_CVAR(devastator, remote_jump_velocity_z_add),
102 WEP_CVAR(devastator, remote_jump_velocity_z_max)
110 WEP_CVAR(devastator, remote_jump_damage),
111 WEP_CVAR(devastator, remote_jump_damage),
112 WEP_CVAR(devastator, remote_jump_radius),
115 (WEP_CVAR(devastator, remote_jump_force) ? WEP_CVAR(devastator, remote_jump_force) : 0),
116 this.projectiledeathtype | HITTYPE_BOUNCE,
129 WEP_CVAR(devastator, remote_damage),
130 WEP_CVAR(devastator, remote_edgedamage),
131 WEP_CVAR(devastator, remote_radius),
132 (handled_as_rocketjump ? head : NULL),
134 WEP_CVAR(devastator, remote_force),
135 this.projectiledeathtype | HITTYPE_BOUNCE,
139 Weapon thiswep = WEP_DEVASTATOR;
140 if(this.realowner.(weaponentity).m_weapon == thiswep)
142 if(this.realowner.(thiswep.ammo_field) < WEP_CVAR(devastator, ammo))
143 if(!(this.realowner.items & IT_UNLIMITED_WEAPON_AMMO))
145 this.realowner.cnt = WEP_DEVASTATOR.m_id;
146 int slot = weaponslot(weaponentity);
147 ATTACK_FINISHED(this.realowner, slot) = time;
148 this.realowner.(weaponentity).m_switchweapon = w_getbestweapon(this.realowner, weaponentity);
154 void W_Devastator_RemoteExplode(entity this, .entity weaponentity)
156 if(!IS_DEAD(this.realowner))
157 if(this.realowner.(weaponentity).lastrocket)
159 if((this.spawnshieldtime >= 0)
160 ? (time >= this.spawnshieldtime) // timer
161 : (vdist(NearestPointOnBox(this.realowner, this.origin) - this.origin, >, WEP_CVAR(devastator, remote_radius))) // safety device
164 W_Devastator_DoRemoteExplode(this, weaponentity);
169 vector W_Devastator_SteerTo(vector thisdir, vector goaldir, float maxturn_cos)
171 if(thisdir * goaldir > maxturn_cos)
173 if(thisdir * goaldir < -0.9998) // less than 1 degree and opposite
174 return thisdir; // refuse to guide (better than letting a numerical error happen)
178 // g = normalize(thisdir + goaldir * X)
179 // thisdir * g = maxturn
181 // gg = thisdir + goaldir * X
182 // (thisdir * gg)^2 = maxturn^2 * (gg * gg)
184 // (1 + (thisdir * goaldir) * X)^2 = maxturn^2 * (1 + X*X + 2 * X * thisdir * goaldir)
185 f = thisdir * goaldir;
186 // (1 + f * X)^2 = maxturn^2 * (1 + X*X + 2 * X * f)
187 // 0 = (m^2 - f^2) * x^2 + (2 * f * (m^2 - 1)) * x + (m^2 - 1)
188 m2 = maxturn_cos * maxturn_cos;
189 v = solve_quadratic(m2 - f * f, 2 * f * (m2 - 1), m2 - 1);
190 return normalize(thisdir + goaldir * v.y); // the larger solution!
192 // assume thisdir == -goaldir:
194 // v = solve_qadratic(m2 - 1, -2 * (m2 - 1), m2 - 1)
195 // (m2 - 1) x^2 - 2 * (m2 - 1) * x + (m2 - 1) = 0
196 // x^2 - 2 * x + 1 = 0
199 // normalize(thisdir + goaldir)
202 void W_Devastator_Think(entity this)
204 vector desireddir, olddir, newdir, desiredorigin, goal;
206 this.nextthink = time;
209 this.projectiledeathtype |= HITTYPE_BOUNCE;
210 W_Devastator_Explode(this, NULL);
215 makevectors(this.angles.x * '-1 0 0' + this.angles.y * '0 1 0');
216 velspeed = WEP_CVAR(devastator, speed) * W_WeaponSpeedFactor(this.realowner) - (this.velocity * v_forward);
218 this.velocity = this.velocity + v_forward * min(WEP_CVAR(devastator, speedaccel) * W_WeaponSpeedFactor(this.realowner) * frametime, velspeed);
220 // laser guided, or remote detonation
221 .entity weaponentity = this.weaponentity_fld;
222 if(this.realowner.(weaponentity).m_weapon == WEP_DEVASTATOR)
224 if(this == this.realowner.(weaponentity).lastrocket)
225 if(!this.realowner.(weaponentity).rl_release)
226 if(!PHYS_INPUT_BUTTON_ATCK2(this))
227 if(WEP_CVAR(devastator, guiderate))
228 if(time > this.pushltime)
229 if(!IS_DEAD(this.realowner))
231 f = WEP_CVAR(devastator, guideratedelay);
233 f = bound(0, (time - this.pushltime) / f, 1);
237 velspeed = vlen(this.velocity);
239 makevectors(this.realowner.v_angle);
240 desireddir = WarpZone_RefSys_TransformVelocity(this.realowner, this, v_forward);
241 desiredorigin = WarpZone_RefSys_TransformOrigin(this.realowner, this, this.realowner.origin + this.realowner.view_ofs);
242 olddir = normalize(this.velocity);
244 // now it gets tricky... we want to move like some curve to approximate the target direction
245 // but we are limiting the rate at which we can turn!
246 goal = desiredorigin + ((this.origin - desiredorigin) * desireddir + WEP_CVAR(devastator, guidegoal)) * desireddir;
247 newdir = W_Devastator_SteerTo(olddir, normalize(goal - this.origin), cos(WEP_CVAR(devastator, guiderate) * f * frametime * DEG2RAD));
249 this.velocity = newdir * velspeed;
250 this.angles = vectoangles(this.velocity);
254 Send_Effect(EFFECT_ROCKET_GUIDE, this.origin, this.velocity, 1);
255 // TODO add a better sound here
256 sound(this.realowner, CH_WEAPON_B, SND_ROCKET_MODE, VOL_BASE, ATTN_NORM);
261 if(this.rl_detonate_later)
262 W_Devastator_RemoteExplode(this, weaponentity);
265 if(this.csqcprojectile_clientanimate == 0)
266 UpdateCSQCProjectile(this);
269 void W_Devastator_Touch(entity this, entity toucher)
271 if(WarpZone_Projectile_Touch(this, toucher))
274 W_Devastator_Unregister(this);
277 W_Devastator_Unregister(this);
278 W_Devastator_Explode(this, toucher);
281 void W_Devastator_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
286 if(!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
287 return; // g_projectiles_damage says to halt
289 this.health = this.health - damage;
290 this.angles = vectoangles(this.velocity);
293 W_PrepareExplosionByDamage(this, attacker, W_Devastator_Explode_think);
296 void W_Devastator_Attack(Weapon thiswep, entity actor, .entity weaponentity)
298 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(devastator, ammo), weaponentity);
300 W_SetupShot_ProjectileSize(actor, weaponentity, '-3 -3 -3', '3 3 3', false, 5, SND_ROCKET_FIRE, CH_WEAPON_A, WEP_CVAR(devastator, damage));
301 Send_Effect(EFFECT_ROCKET_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
303 entity missile = WarpZone_RefSys_SpawnSameRefSys(actor);
304 missile.weaponentity_fld = weaponentity;
305 missile.owner = missile.realowner = actor;
306 actor.(weaponentity).lastrocket = missile;
307 if(WEP_CVAR(devastator, detonatedelay) >= 0)
308 missile.spawnshieldtime = time + WEP_CVAR(devastator, detonatedelay);
310 missile.spawnshieldtime = -1;
311 missile.pushltime = time + WEP_CVAR(devastator, guidedelay);
312 missile.classname = "rocket";
313 missile.bot_dodge = true;
314 missile.bot_dodgerating = WEP_CVAR(devastator, damage) * 2; // * 2 because it can be detonated inflight which makes it even more dangerous
316 missile.takedamage = DAMAGE_YES;
317 missile.damageforcescale = WEP_CVAR(devastator, damageforcescale);
318 missile.health = WEP_CVAR(devastator, health);
319 missile.event_damage = W_Devastator_Damage;
320 missile.damagedbycontents = true;
321 IL_PUSH(g_damagedbycontents, missile);
323 set_movetype(missile, MOVETYPE_FLY);
324 PROJECTILE_MAKETRIGGER(missile);
325 missile.projectiledeathtype = WEP_DEVASTATOR.m_id;
326 setsize(missile, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot
328 setorigin(missile, w_shotorg - v_forward * 3); // move it back so it hits the wall at the right point
329 W_SetupProjVelocity_Basic(missile, WEP_CVAR(devastator, speedstart), 0);
330 missile.angles = vectoangles(missile.velocity);
332 settouch(missile, W_Devastator_Touch);
333 setthink(missile, W_Devastator_Think);
334 missile.nextthink = time;
335 missile.cnt = time + WEP_CVAR(devastator, lifetime);
336 missile.flags = FL_PROJECTILE;
337 IL_PUSH(g_projectiles, missile);
338 IL_PUSH(g_bot_dodge, missile);
339 missile.missile_flags = MIF_SPLASH;
341 CSQCProjectile(missile, WEP_CVAR(devastator, guiderate) == 0 && WEP_CVAR(devastator, speedaccel) == 0, PROJECTILE_ROCKET, false); // because of fly sound
343 // muzzle flash for 1st person view
344 entity flash = spawn();
345 setmodel(flash, MDL_DEVASTATOR_MUZZLEFLASH); // precision set below
346 SUB_SetFade(flash, time, 0.1);
347 flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
348 W_AttachToShotorg(actor, weaponentity, flash, '5 0 0');
351 MUTATOR_CALLHOOK(EditProjectile, actor, missile);
354 METHOD(Devastator, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
356 // aim and decide to fire if appropriate
357 PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, WEP_CVAR(devastator, speed), 0, WEP_CVAR(devastator, lifetime), false);
358 if(skill >= 2) // skill 0 and 1 bots won't detonate rockets!
360 // decide whether to detonate rockets
361 float edgedamage, coredamage, edgeradius, recipricoledgeradius;
362 float selfdamage, teamdamage, enemydamage;
363 edgedamage = WEP_CVAR(devastator, edgedamage);
364 coredamage = WEP_CVAR(devastator, damage);
365 edgeradius = WEP_CVAR(devastator, radius);
366 recipricoledgeradius = 1 / edgeradius;
370 IL_EACH(g_projectiles, it.realowner == actor && it.classname == "rocket",
373 IL_EACH(g_bot_targets, it.bot_attack,
375 float d = vlen(it.origin + (it.mins + it.maxs) * 0.5 - rocket.origin);
376 d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
377 // count potential damage according to type of target
379 selfdamage = selfdamage + d;
380 else if(SAME_TEAM(it, actor))
381 teamdamage = teamdamage + d;
382 else if(bot_shouldattack(actor, it))
383 enemydamage = enemydamage + d;
386 float desirabledamage;
387 desirabledamage = enemydamage;
388 if(time > actor.invincible_finished && time > actor.spawnshieldtime)
389 desirabledamage = desirabledamage - selfdamage * autocvar_g_balance_selfdamagepercent;
390 if(teamplay && actor.team)
391 desirabledamage = desirabledamage - teamdamage;
393 makevectors(actor.v_angle);
394 IL_EACH(g_projectiles, it.realowner == actor && it.classname == "rocket",
396 if(skill > 9) // normal players only do this for the target they are tracking
399 IL_EACH(g_bot_targets, it.bot_attack,
401 if((v_forward * normalize(rocket.origin - it.origin) < 0.1)
402 && desirabledamage > 0.1 * coredamage
403 ) PHYS_INPUT_BUTTON_ATCK2(actor) = true;
408 //As the distance gets larger, a correct detonation gets near imposible
409 //Bots are assumed to use the rocket spawnfunc_light to see if the rocket gets near a player
410 if((v_forward * normalize(it.origin - actor.enemy.origin) < 0.1)
411 && IS_PLAYER(actor.enemy)
412 && (desirabledamage >= 0.1 * coredamage)
415 float distance = bound(300, vlen(actor.origin - actor.enemy.origin), 30000);
416 if(random() / distance * 300 > frametime * bound(0, (10 - skill) * 0.2, 1))
417 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
421 // if we would be doing at X percent of the core damage, detonate it
422 // but don't fire a new shot at the same time!
423 if(desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
424 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
425 if((skill > 6.5) && (selfdamage > actor.health))
426 PHYS_INPUT_BUTTON_ATCK2(actor) = false;
427 //if(PHYS_INPUT_BUTTON_ATCK2(actor) == true)
428 // dprint(ftos(desirabledamage),"\n");
429 if(PHYS_INPUT_BUTTON_ATCK2(actor)) PHYS_INPUT_BUTTON_ATCK(actor) = false;
433 METHOD(Devastator, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
435 if(WEP_CVAR(devastator, reload_ammo) && actor.(weaponentity).clip_load < WEP_CVAR(devastator, ammo)) { // forced reload
436 thiswep.wr_reload(thiswep, actor, weaponentity);
440 if(actor.(weaponentity).rl_release || WEP_CVAR(devastator, guidestop))
441 if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR(devastator, refire)))
443 W_Devastator_Attack(thiswep, actor, weaponentity);
444 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(devastator, animtime), w_ready);
445 actor.(weaponentity).rl_release = 0;
449 actor.(weaponentity).rl_release = 1;
452 if(actor.(weaponentity).m_switchweapon == WEP_DEVASTATOR)
454 bool rockfound = false;
455 IL_EACH(g_projectiles, it.realowner == actor && it.classname == "rocket",
457 if(!it.rl_detonate_later)
459 it.rl_detonate_later = true;
464 sound(actor, CH_WEAPON_B, SND_ROCKET_DET, VOL_BASE, ATTN_NORM);
468 METHOD(Devastator, wr_setup, void(entity thiswep, entity actor, .entity weaponentity))
470 actor.(weaponentity).rl_release = 1;
472 METHOD(Devastator, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
475 // don't switch while guiding a missile
476 if(ATTACK_FINISHED(actor, slot) <= time || PS(actor).m_weapon != WEP_DEVASTATOR)
479 if(WEP_CVAR(devastator, reload_ammo))
481 if(actor.(thiswep.ammo_field) < WEP_CVAR(devastator, ammo) && actor.(weaponentity).(weapon_load[WEP_DEVASTATOR.m_id]) < WEP_CVAR(devastator, ammo))
484 else if(actor.(thiswep.ammo_field) < WEP_CVAR(devastator, ammo))
490 if(actor.rl_release == 0)
492 LOG_INFOF("W_Devastator(WR_CHECKAMMO1): %d, %.2f, %d: TRUE\n", actor.rl_release, actor.(thiswep.ammo_field), WEP_CVAR(devastator, ammo));
497 ammo_amount = actor.(thiswep.ammo_field) >= WEP_CVAR(devastator, ammo);
498 ammo_amount += actor.(weaponentity).(weapon_load[WEP_DEVASTATOR.m_id]) >= WEP_CVAR(devastator, ammo);
499 LOG_INFOF("W_Devastator(WR_CHECKAMMO1): %d, %.2f, %d: %s\n", actor.rl_release, actor.(thiswep.ammo_field), WEP_CVAR(devastator, ammo), (ammo_amount ? "TRUE" : "FALSE"));
503 float ammo_amount = actor.(thiswep.ammo_field) >= WEP_CVAR(devastator, ammo);
504 ammo_amount += actor.(weaponentity).(weapon_load[WEP_DEVASTATOR.m_id]) >= WEP_CVAR(devastator, ammo);
508 METHOD(Devastator, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
512 METHOD(Devastator, wr_resetplayer, void(entity thiswep, entity actor))
514 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
516 .entity weaponentity = weaponentities[slot];
517 actor.(weaponentity).lastrocket = NULL; // stop rocket guiding, no revenge from the grave!
518 actor.(weaponentity).rl_release = 0;
521 METHOD(Devastator, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
523 W_Reload(actor, weaponentity, WEP_CVAR(devastator, ammo), SND_RELOAD);
525 METHOD(Devastator, wr_suicidemessage, Notification(entity thiswep))
527 return WEAPON_DEVASTATOR_SUICIDE;
529 METHOD(Devastator, wr_killmessage, Notification(entity thiswep))
531 if((w_deathtype & HITTYPE_BOUNCE) || (w_deathtype & HITTYPE_SPLASH))
532 return WEAPON_DEVASTATOR_MURDER_SPLASH;
534 return WEAPON_DEVASTATOR_MURDER_DIRECT;
540 METHOD(Devastator, wr_impacteffect, void(entity thiswep, entity actor))
543 org2 = w_org + w_backoff * 12;
544 pointparticles(EFFECT_ROCKET_EXPLODE, org2, '0 0 0', 1);
546 sound(actor, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTN_NORM);