1 #include "devastator.qh"
7 void W_Devastator_Unregister(entity this)
9 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
11 .entity weaponentity = weaponentities[slot];
12 if(this.realowner.(weaponentity).lastrocket == this)
13 this.realowner.(weaponentity).lastrocket = NULL;
17 void W_Devastator_Explode(entity this, entity directhitentity)
19 W_Devastator_Unregister(this);
21 if(directhitentity.takedamage == DAMAGE_AIM)
22 if(IS_PLAYER(directhitentity))
23 if(DIFF_TEAM(this.realowner, directhitentity))
24 if(!IS_DEAD(directhitentity))
25 if(IsFlying(directhitentity))
26 Send_Notification(NOTIF_ONE, this.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_AIRSHOT);
28 this.event_damage = func_null;
29 this.takedamage = DAMAGE_NO;
34 WEP_CVAR(devastator, damage),
35 WEP_CVAR(devastator, edgedamage),
36 WEP_CVAR(devastator, radius),
39 WEP_CVAR(devastator, force),
40 this.projectiledeathtype,
41 this.weaponentity_fld,
45 Weapon thiswep = WEP_DEVASTATOR;
46 .entity weaponentity = this.weaponentity_fld;
47 if(this.realowner.(weaponentity).m_weapon == thiswep)
49 if(GetResource(this.realowner, thiswep.ammo_type) < WEP_CVAR(devastator, ammo))
50 if(!(this.realowner.items & IT_UNLIMITED_AMMO))
52 this.realowner.cnt = thiswep.m_id;
53 ATTACK_FINISHED(this.realowner, weaponentity) = time;
54 this.realowner.(weaponentity).m_switchweapon = w_getbestweapon(this.realowner, weaponentity);
60 void W_Devastator_Explode_think(entity this)
62 W_Devastator_Explode(this, NULL);
65 void W_Devastator_DoRemoteExplode(entity this, .entity weaponentity)
67 W_Devastator_Unregister(this);
69 this.event_damage = func_null;
70 this.takedamage = DAMAGE_NO;
72 bool handled_as_rocketjump = false;
74 bool allow_rocketjump = WEP_CVAR(devastator, remote_jump);
75 MUTATOR_CALLHOOK(AllowRocketJumping, allow_rocketjump);
76 allow_rocketjump = M_ARGV(0, bool);
78 if(allow_rocketjump && WEP_CVAR(devastator, remote_jump_radius))
80 head = WarpZone_FindRadius(
82 WEP_CVAR(devastator, remote_jump_radius),
88 if(head.takedamage && (head == this.realowner))
90 if(vdist(this.origin - head.WarpZone_findradius_nearest, <=, WEP_CVAR(devastator, remote_jump_radius)))
92 // we handled this as a rocketjump :)
93 handled_as_rocketjump = true;
96 if(WEP_CVAR(devastator, remote_jump_velocity_z_add))
98 head.velocity_x *= 0.9;
99 head.velocity_y *= 0.9;
100 head.velocity_z = bound(
101 WEP_CVAR(devastator, remote_jump_velocity_z_min),
102 head.velocity.z + WEP_CVAR(devastator, remote_jump_velocity_z_add),
103 WEP_CVAR(devastator, remote_jump_velocity_z_max)
111 WEP_CVAR(devastator, remote_jump_damage),
112 WEP_CVAR(devastator, remote_jump_damage),
113 WEP_CVAR(devastator, remote_jump_radius),
116 (WEP_CVAR(devastator, remote_jump_force) ? WEP_CVAR(devastator, remote_jump_force) : 0),
117 this.projectiledeathtype | HITTYPE_BOUNCE,
118 this.weaponentity_fld,
131 WEP_CVAR(devastator, remote_damage),
132 WEP_CVAR(devastator, remote_edgedamage),
133 WEP_CVAR(devastator, remote_radius),
134 (handled_as_rocketjump ? head : NULL),
136 WEP_CVAR(devastator, remote_force),
137 this.projectiledeathtype | HITTYPE_BOUNCE,
138 this.weaponentity_fld,
142 Weapon thiswep = WEP_DEVASTATOR;
143 if(this.realowner.(weaponentity).m_weapon == thiswep)
145 if(GetResource(this.realowner, thiswep.ammo_type) < WEP_CVAR(devastator, ammo))
146 if(!(this.realowner.items & IT_UNLIMITED_AMMO))
148 this.realowner.cnt = thiswep.m_id;
149 ATTACK_FINISHED(this.realowner, weaponentity) = time;
150 this.realowner.(weaponentity).m_switchweapon = w_getbestweapon(this.realowner, weaponentity);
156 void W_Devastator_RemoteExplode(entity this, .entity weaponentity)
158 if(!IS_DEAD(this.realowner))
159 if(this.realowner.(weaponentity).lastrocket)
161 if((this.spawnshieldtime >= 0)
162 ? (time >= this.spawnshieldtime) // timer
163 : (vdist(NearestPointOnBox(this.realowner, this.origin) - this.origin, >, WEP_CVAR(devastator, remote_radius))) // safety device
166 W_Devastator_DoRemoteExplode(this, weaponentity);
171 vector W_Devastator_SteerTo(vector thisdir, vector goaldir, float maxturn_cos)
173 if(thisdir * goaldir > maxturn_cos)
175 if(thisdir * goaldir < -0.9998) // less than 1 degree and opposite
176 return thisdir; // refuse to guide (better than letting a numerical error happen)
180 // g = normalize(thisdir + goaldir * X)
181 // thisdir * g = maxturn
183 // gg = thisdir + goaldir * X
184 // (thisdir * gg)^2 = maxturn^2 * (gg * gg)
186 // (1 + (thisdir * goaldir) * X)^2 = maxturn^2 * (1 + X*X + 2 * X * thisdir * goaldir)
187 f = thisdir * goaldir;
188 // (1 + f * X)^2 = maxturn^2 * (1 + X*X + 2 * X * f)
189 // 0 = (m^2 - f^2) * x^2 + (2 * f * (m^2 - 1)) * x + (m^2 - 1)
190 m2 = maxturn_cos * maxturn_cos;
191 v = solve_quadratic(m2 - f * f, 2 * f * (m2 - 1), m2 - 1);
192 return normalize(thisdir + goaldir * v.y); // the larger solution!
194 // assume thisdir == -goaldir:
196 // v = solve_qadratic(m2 - 1, -2 * (m2 - 1), m2 - 1)
197 // (m2 - 1) x^2 - 2 * (m2 - 1) * x + (m2 - 1) = 0
198 // x^2 - 2 * x + 1 = 0
201 // normalize(thisdir + goaldir)
204 void W_Devastator_Think(entity this)
206 vector desireddir, olddir, newdir, desiredorigin, goal;
208 this.nextthink = time;
211 this.projectiledeathtype |= HITTYPE_BOUNCE;
212 W_Devastator_Explode(this, NULL);
217 makevectors(this.angles.x * '-1 0 0' + this.angles.y * '0 1 0');
218 velspeed = WEP_CVAR(devastator, speed) * W_WeaponSpeedFactor(this.realowner) - (this.velocity * v_forward);
220 this.velocity = this.velocity + v_forward * min(WEP_CVAR(devastator, speedaccel) * W_WeaponSpeedFactor(this.realowner) * frametime, velspeed);
222 // laser guided, or remote detonation
223 .entity weaponentity = this.weaponentity_fld;
224 if(this.realowner.(weaponentity).m_weapon == WEP_DEVASTATOR)
226 if(this == this.realowner.(weaponentity).lastrocket)
227 if(!this.realowner.(weaponentity).rl_release)
228 if(!PHYS_INPUT_BUTTON_ATCK2(this))
229 if(WEP_CVAR(devastator, guiderate))
230 if(time > this.pushltime)
231 if(!IS_DEAD(this.realowner))
233 f = WEP_CVAR(devastator, guideratedelay);
235 f = bound(0, (time - this.pushltime) / f, 1);
239 vector md = this.realowner.(weaponentity).movedir;
240 vector vecs = ((md.x > 0) ? md : '0 0 0');
242 vector dv = v_right * -vecs.y + v_up * vecs.z;
244 if(!W_DualWielding(this.realowner))
245 dv = '0 0 0'; // don't override!
247 velspeed = vlen(this.velocity);
249 makevectors(this.realowner.v_angle);
250 desireddir = WarpZone_RefSys_TransformVelocity(this.realowner, this, v_forward);
251 desiredorigin = WarpZone_RefSys_TransformOrigin(this.realowner, this, this.realowner.origin + this.realowner.view_ofs + dv);
252 olddir = normalize(this.velocity);
254 // now it gets tricky... we want to move like some curve to approximate the target direction
255 // but we are limiting the rate at which we can turn!
256 goal = desiredorigin + ((this.origin - desiredorigin) * desireddir + WEP_CVAR(devastator, guidegoal)) * desireddir;
257 newdir = W_Devastator_SteerTo(olddir, normalize(goal - this.origin), cos(WEP_CVAR(devastator, guiderate) * f * frametime * DEG2RAD));
259 this.velocity = newdir * velspeed;
260 this.angles = vectoangles(this.velocity);
264 Send_Effect(EFFECT_ROCKET_GUIDE, this.origin, this.velocity, 1);
265 // TODO add a better sound here
266 sound(this.realowner, CH_WEAPON_B, SND_ROCKET_MODE, VOL_BASE, ATTN_NORM);
271 if(this.rl_detonate_later)
272 W_Devastator_RemoteExplode(this, weaponentity);
275 if(this.csqcprojectile_clientanimate == 0)
276 UpdateCSQCProjectile(this);
279 void W_Devastator_Touch(entity this, entity toucher)
281 if(WarpZone_Projectile_Touch(this, toucher))
284 W_Devastator_Unregister(this);
287 W_Devastator_Unregister(this);
288 W_Devastator_Explode(this, toucher);
291 void W_Devastator_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
293 if(GetResource(this, RES_HEALTH) <= 0)
296 if(!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
297 return; // g_projectiles_damage says to halt
299 TakeResource(this, RES_HEALTH, damage);
300 this.angles = vectoangles(this.velocity);
302 if(GetResource(this, RES_HEALTH) <= 0)
303 W_PrepareExplosionByDamage(this, attacker, W_Devastator_Explode_think);
306 void W_Devastator_Attack(Weapon thiswep, entity actor, .entity weaponentity, int fire)
308 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(devastator, ammo), weaponentity);
310 W_SetupShot_ProjectileSize(actor, weaponentity, '-3 -3 -3', '3 3 3', false, 5, SND_ROCKET_FIRE, CH_WEAPON_A, WEP_CVAR(devastator, damage), thiswep.m_id);
311 W_MuzzleFlash(thiswep, actor, weaponentity, w_shotorg, w_shotdir);
313 entity missile = WarpZone_RefSys_SpawnSameRefSys(actor);
314 missile.weaponentity_fld = weaponentity;
315 missile.owner = missile.realowner = actor;
316 actor.(weaponentity).lastrocket = missile;
317 if(WEP_CVAR(devastator, detonatedelay) >= 0)
318 missile.spawnshieldtime = time + WEP_CVAR(devastator, detonatedelay);
320 missile.spawnshieldtime = -1; // NOTE: proximity based when rocket jumping
321 missile.pushltime = time + WEP_CVAR(devastator, guidedelay);
322 missile.classname = "rocket";
323 missile.bot_dodge = true;
324 missile.bot_dodgerating = WEP_CVAR(devastator, damage) * 2; // * 2 because it can be detonated inflight which makes it even more dangerous
326 missile.takedamage = DAMAGE_YES;
327 missile.damageforcescale = WEP_CVAR(devastator, damageforcescale);
328 SetResourceExplicit(missile, RES_HEALTH, WEP_CVAR(devastator, health));
329 missile.event_damage = W_Devastator_Damage;
330 missile.damagedbycontents = true;
331 IL_PUSH(g_damagedbycontents, missile);
333 set_movetype(missile, MOVETYPE_FLY);
334 PROJECTILE_MAKETRIGGER(missile);
335 missile.projectiledeathtype = thiswep.m_id;
336 setsize(missile, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot
338 setorigin(missile, w_shotorg - v_forward * 3); // move it back so it hits the wall at the right point
339 W_SetupProjVelocity_Basic(missile, WEP_CVAR(devastator, speedstart), 0);
340 missile.angles = vectoangles(missile.velocity);
342 settouch(missile, W_Devastator_Touch);
343 setthink(missile, W_Devastator_Think);
344 missile.nextthink = time;
345 missile.cnt = time + WEP_CVAR(devastator, lifetime);
346 missile.rl_detonate_later = (fire & 2); // allow instant detonation
347 missile.flags = FL_PROJECTILE;
348 IL_PUSH(g_projectiles, missile);
349 IL_PUSH(g_bot_dodge, missile);
350 missile.missile_flags = MIF_SPLASH;
352 CSQCProjectile(missile, WEP_CVAR(devastator, guiderate) == 0 && WEP_CVAR(devastator, speedaccel) == 0, PROJECTILE_ROCKET, false); // because of fly sound
355 MUTATOR_CALLHOOK(EditProjectile, actor, missile);
357 if (time >= missile.nextthink)
359 getthink(missile)(missile);
363 METHOD(Devastator, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
365 if (!WEP_CVAR(devastator, guidestop) && !actor.(weaponentity).rl_release)
367 int fired_rockets = 0;
368 IL_EACH(g_projectiles, it.realowner == actor && it.classname == "rocket",
372 // release PHYS_INPUT_BUTTON_ATCK after all fired rocket exploded otherwise bot can't fire again
377 // aim and decide to fire if appropriate
378 float spd = WEP_CVAR(devastator, speed);
379 // simulate rocket guide by calculating rocket trajectory with higher speed
380 // 20 times faster at 90 degrees guide rate
381 if (WEP_CVAR(devastator, guiderate) > 0)
382 spd *= sqrt(WEP_CVAR(devastator, guiderate)) * (20 / 9.489); // 9.489 ~= sqrt(90)
383 // no need to fire with high accuracy on large distances if rockets can be guided
384 bool shot_accurate = (WEP_CVAR(devastator, guiderate) < 50);
385 PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, spd, 0, WEP_CVAR(devastator, lifetime), false, shot_accurate);
386 float pred_time = bound(0.02, 0.02 + (8 - skill) * 0.01, 0.1);
387 if(skill >= 2) // skill 0 and 1 bots won't detonate rockets!
389 // decide whether to detonate rockets
390 float selfdamage = 0, teamdamage = 0, enemydamage = 0;
391 float pred_selfdamage = 0, pred_teamdamage = 0, pred_enemydamage = 0;
392 float edgedamage = WEP_CVAR(devastator, edgedamage);
393 float coredamage = WEP_CVAR(devastator, damage);
394 float edgeradius = WEP_CVAR(devastator, radius);
395 IL_EACH(g_projectiles, it.realowner == actor && it.classname == "rocket",
398 IL_EACH(g_bot_targets, it.bot_attack,
400 // code to calculate damage is similar to the one used in RadiusDamageForSource with some simplifications
401 vector target_pos = it.origin + (it.maxs - it.mins) * 0.5;
403 float dist = vlen(target_pos - rocket.origin);
405 if (dist <= edgeradius)
407 float f = (edgeradius > 0) ? max(0, 1 - (dist / edgeradius)) : 1;
408 dmg = coredamage * f + edgedamage * (1 - f);
411 float pred_dist = vlen(target_pos + it.velocity * pred_time - (rocket.origin + rocket.velocity * pred_time));
413 if (pred_dist <= edgeradius)
415 float f = (edgeradius > 0) ? max(0, 1 - (pred_dist / edgeradius)) : 1;
416 pred_dmg = coredamage * f + edgedamage * (1 - f);
419 // count potential damage according to type of target
422 if(StatusEffects_active(STATUSEFFECT_Strength, it))
423 dmg *= autocvar_g_balance_powerup_strength_damage;
424 if(StatusEffects_active(STATUSEFFECT_Shield, it))
425 dmg *= autocvar_g_balance_powerup_invincible_takedamage;
426 // self damage reduction factor will be applied later to the total damage
428 pred_selfdamage += pred_dmg;
430 else if(SAME_TEAM(it, actor))
432 if(StatusEffects_active(STATUSEFFECT_Shield, it))
433 dmg *= autocvar_g_balance_powerup_invincible_takedamage;
434 // bot strength factor will be applied later to the total damage
436 pred_teamdamage += pred_dmg;
438 else if(bot_shouldattack(actor, it))
440 if(StatusEffects_active(STATUSEFFECT_Shield, it))
441 dmg *= autocvar_g_balance_powerup_invincible_takedamage;
442 // bot strength factor will be applied later to the total damage
444 pred_enemydamage += pred_dmg;
449 selfdamage *= autocvar_g_balance_selfdamagepercent;
450 pred_selfdamage *= autocvar_g_balance_selfdamagepercent;
451 if(StatusEffects_active(STATUSEFFECT_Strength, actor))
453 // FIXME bots don't know whether team damage is enabled or not
454 teamdamage *= autocvar_g_balance_powerup_strength_damage;
455 pred_teamdamage *= autocvar_g_balance_powerup_strength_damage;
456 enemydamage *= autocvar_g_balance_powerup_strength_damage;
457 pred_enemydamage *= autocvar_g_balance_powerup_strength_damage;
460 float good_damage = enemydamage;
461 float pred_good_damage = pred_enemydamage;
462 float bad_damage = selfdamage + teamdamage;
463 float pred_bad_damage = pred_selfdamage + pred_teamdamage;
465 // detonate if predicted good damage is lower (current good damage is maximum)
466 // or if predicted bad damage is too much
467 if(good_damage > coredamage * 0.1 && good_damage > bad_damage * 1.5
468 && (pred_good_damage < good_damage + 2 || pred_good_damage < pred_bad_damage * 1.5))
470 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
472 if(skill >= 7 && selfdamage > GetResource(actor, RES_HEALTH))
473 PHYS_INPUT_BUTTON_ATCK2(actor) = false;
475 // don't fire a new shot at the same time!
476 if(PHYS_INPUT_BUTTON_ATCK2(actor)) PHYS_INPUT_BUTTON_ATCK(actor) = false;
480 METHOD(Devastator, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
482 if(WEP_CVAR(devastator, reload_ammo) && actor.(weaponentity).clip_load < WEP_CVAR(devastator, ammo)) { // forced reload
483 thiswep.wr_reload(thiswep, actor, weaponentity);
487 if(actor.(weaponentity).rl_release || WEP_CVAR(devastator, guidestop))
488 if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR(devastator, refire)))
490 W_Devastator_Attack(thiswep, actor, weaponentity, fire);
491 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(devastator, animtime), w_ready);
492 actor.(weaponentity).rl_release = 0;
496 actor.(weaponentity).rl_release = 1;
499 if(actor.(weaponentity).m_switchweapon == thiswep)
501 bool rockfound = false;
502 IL_EACH(g_projectiles, it.realowner == actor && it.classname == "rocket",
504 if(!it.rl_detonate_later)
506 it.rl_detonate_later = true;
511 sound(actor, CH_WEAPON_B, SND_ROCKET_DET, VOL_BASE, ATTN_NORM);
515 METHOD(Devastator, wr_setup, void(entity thiswep, entity actor, .entity weaponentity))
517 actor.(weaponentity).rl_release = 1;
518 actor.(weaponentity).damagepush_speedfactor = WEP_CVAR(devastator, damagepush_speedfactor);
520 METHOD(Devastator, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
523 // don't switch while guiding a missile
524 if(ATTACK_FINISHED(actor, weaponentity) <= time || PS(actor).m_weapon != WEP_DEVASTATOR)
527 if(WEP_CVAR(devastator, reload_ammo))
529 if(GetResource(actor, thiswep.ammo_type) < WEP_CVAR(devastator, ammo) && actor.(weaponentity).(weapon_load[WEP_DEVASTATOR.m_id]) < WEP_CVAR(devastator, ammo))
532 else if(GetResource(actor, thiswep.ammo_type) < WEP_CVAR(devastator, ammo))
538 if(actor.rl_release == 0)
540 LOG_INFOF("W_Devastator(WR_CHECKAMMO1): %d, %.2f, %d: TRUE", actor.rl_release, GetResource(actor, thiswep.ammo_type), WEP_CVAR(devastator, ammo));
545 ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(devastator, ammo);
546 ammo_amount += actor.(weaponentity).(weapon_load[WEP_DEVASTATOR.m_id]) >= WEP_CVAR(devastator, ammo);
547 LOG_INFOF("W_Devastator(WR_CHECKAMMO1): %d, %.2f, %d: %s", actor.rl_release, GetResource(actor, thiswep.ammo_type), WEP_CVAR(devastator, ammo), (ammo_amount ? "TRUE" : "FALSE"));
551 float ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(devastator, ammo);
552 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(devastator, ammo);
556 METHOD(Devastator, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
560 METHOD(Devastator, wr_resetplayer, void(entity thiswep, entity actor))
562 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
564 .entity weaponentity = weaponentities[slot];
565 actor.(weaponentity).lastrocket = NULL; // stop rocket guiding, no revenge from the grave!
566 actor.(weaponentity).rl_release = 0;
569 METHOD(Devastator, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
571 W_Reload(actor, weaponentity, WEP_CVAR(devastator, ammo), SND_RELOAD);
573 METHOD(Devastator, wr_suicidemessage, Notification(entity thiswep))
575 return WEAPON_DEVASTATOR_SUICIDE;
577 METHOD(Devastator, wr_killmessage, Notification(entity thiswep))
579 if((w_deathtype & HITTYPE_BOUNCE) || (w_deathtype & HITTYPE_SPLASH))
580 return WEAPON_DEVASTATOR_MURDER_SPLASH;
582 return WEAPON_DEVASTATOR_MURDER_DIRECT;
588 METHOD(Devastator, wr_impacteffect, void(entity thiswep, entity actor))
591 org2 = w_org + w_backoff * 12;
592 pointparticles(EFFECT_ROCKET_EXPLODE, org2, '0 0 0', 1);
594 sound(actor, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTN_NORM);