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;
75 if(WEP_CVAR(devastator, remote_jump_radius))
77 head = WarpZone_FindRadius(
79 WEP_CVAR(devastator, remote_jump_radius),
85 if(head.takedamage && (head == this.realowner))
87 if(vdist(this.origin - head.WarpZone_findradius_nearest, <=, WEP_CVAR(devastator, remote_jump_radius)))
89 // we handled this as a rocketjump :)
90 handled_as_rocketjump = true;
93 if(WEP_CVAR(devastator, remote_jump_velocity_z_add))
95 head.velocity_x *= 0.9;
96 head.velocity_y *= 0.9;
97 head.velocity_z = bound(
98 WEP_CVAR(devastator, remote_jump_velocity_z_min),
99 head.velocity.z + WEP_CVAR(devastator, remote_jump_velocity_z_add),
100 WEP_CVAR(devastator, remote_jump_velocity_z_max)
108 WEP_CVAR(devastator, remote_jump_damage),
109 WEP_CVAR(devastator, remote_jump_damage),
110 WEP_CVAR(devastator, remote_jump_radius),
113 (WEP_CVAR(devastator, remote_jump_force) ? WEP_CVAR(devastator, remote_jump_force) : 0),
114 this.projectiledeathtype | HITTYPE_BOUNCE,
115 this.weaponentity_fld,
128 WEP_CVAR(devastator, remote_damage),
129 WEP_CVAR(devastator, remote_edgedamage),
130 WEP_CVAR(devastator, remote_radius),
131 (handled_as_rocketjump ? head : NULL),
133 WEP_CVAR(devastator, remote_force),
134 this.projectiledeathtype | HITTYPE_BOUNCE,
135 this.weaponentity_fld,
139 Weapon thiswep = WEP_DEVASTATOR;
140 if(this.realowner.(weaponentity).m_weapon == thiswep)
142 if(GetResource(this.realowner, thiswep.ammo_type) < WEP_CVAR(devastator, ammo))
143 if(!(this.realowner.items & IT_UNLIMITED_AMMO))
145 this.realowner.cnt = thiswep.m_id;
146 ATTACK_FINISHED(this.realowner, weaponentity) = time;
147 this.realowner.(weaponentity).m_switchweapon = w_getbestweapon(this.realowner, weaponentity);
153 void W_Devastator_RemoteExplode(entity this, .entity weaponentity)
155 if(!IS_DEAD(this.realowner))
156 if(this.realowner.(weaponentity).lastrocket)
158 if((this.spawnshieldtime >= 0)
159 ? (time >= this.spawnshieldtime) // timer
160 : (vdist(NearestPointOnBox(this.realowner, this.origin) - this.origin, >, WEP_CVAR(devastator, remote_radius))) // safety device
163 W_Devastator_DoRemoteExplode(this, weaponentity);
168 vector W_Devastator_SteerTo(vector thisdir, vector goaldir, float maxturn_cos)
170 if(thisdir * goaldir > maxturn_cos)
172 if(thisdir * goaldir < -0.9998) // less than 1 degree and opposite
173 return thisdir; // refuse to guide (better than letting a numerical error happen)
177 // g = normalize(thisdir + goaldir * X)
178 // thisdir * g = maxturn
180 // gg = thisdir + goaldir * X
181 // (thisdir * gg)^2 = maxturn^2 * (gg * gg)
183 // (1 + (thisdir * goaldir) * X)^2 = maxturn^2 * (1 + X*X + 2 * X * thisdir * goaldir)
184 f = thisdir * goaldir;
185 // (1 + f * X)^2 = maxturn^2 * (1 + X*X + 2 * X * f)
186 // 0 = (m^2 - f^2) * x^2 + (2 * f * (m^2 - 1)) * x + (m^2 - 1)
187 m2 = maxturn_cos * maxturn_cos;
188 v = solve_quadratic(m2 - f * f, 2 * f * (m2 - 1), m2 - 1);
189 return normalize(thisdir + goaldir * v.y); // the larger solution!
191 // assume thisdir == -goaldir:
193 // v = solve_qadratic(m2 - 1, -2 * (m2 - 1), m2 - 1)
194 // (m2 - 1) x^2 - 2 * (m2 - 1) * x + (m2 - 1) = 0
195 // x^2 - 2 * x + 1 = 0
198 // normalize(thisdir + goaldir)
201 void W_Devastator_Think(entity this)
203 vector desireddir, olddir, newdir, desiredorigin, goal;
205 this.nextthink = time;
208 this.projectiledeathtype |= HITTYPE_BOUNCE;
209 W_Devastator_Explode(this, NULL);
214 makevectors(this.angles.x * '-1 0 0' + this.angles.y * '0 1 0');
215 velspeed = WEP_CVAR(devastator, speed) * W_WeaponSpeedFactor(this.realowner) - (this.velocity * v_forward);
217 this.velocity = this.velocity + v_forward * min(WEP_CVAR(devastator, speedaccel) * W_WeaponSpeedFactor(this.realowner) * frametime, velspeed);
219 // laser guided, or remote detonation
220 .entity weaponentity = this.weaponentity_fld;
221 if(this.realowner.(weaponentity).m_weapon == WEP_DEVASTATOR)
223 if(this == this.realowner.(weaponentity).lastrocket)
224 if(!this.realowner.(weaponentity).rl_release)
225 if(!PHYS_INPUT_BUTTON_ATCK2(this))
226 if(WEP_CVAR(devastator, guiderate))
227 if(time > this.pushltime)
228 if(!IS_DEAD(this.realowner))
230 f = WEP_CVAR(devastator, guideratedelay);
232 f = bound(0, (time - this.pushltime) / f, 1);
236 vector md = this.realowner.(weaponentity).movedir;
237 vector vecs = ((md.x > 0) ? md : '0 0 0');
239 vector dv = v_right * -vecs.y + v_up * vecs.z;
241 if(!W_DualWielding(this.realowner))
242 dv = '0 0 0'; // don't override!
244 velspeed = vlen(this.velocity);
246 makevectors(this.realowner.v_angle);
247 desireddir = WarpZone_RefSys_TransformVelocity(this.realowner, this, v_forward);
248 desiredorigin = WarpZone_RefSys_TransformOrigin(this.realowner, this, this.realowner.origin + this.realowner.view_ofs + dv);
249 olddir = normalize(this.velocity);
251 // now it gets tricky... we want to move like some curve to approximate the target direction
252 // but we are limiting the rate at which we can turn!
253 goal = desiredorigin + ((this.origin - desiredorigin) * desireddir + WEP_CVAR(devastator, guidegoal)) * desireddir;
254 newdir = W_Devastator_SteerTo(olddir, normalize(goal - this.origin), cos(WEP_CVAR(devastator, guiderate) * f * frametime * DEG2RAD));
256 this.velocity = newdir * velspeed;
257 this.angles = vectoangles(this.velocity);
261 Send_Effect(EFFECT_ROCKET_GUIDE, this.origin, this.velocity, 1);
262 // TODO add a better sound here
263 sound(this.realowner, CH_WEAPON_B, SND_ROCKET_MODE, VOL_BASE, ATTN_NORM);
268 if(this.rl_detonate_later)
269 W_Devastator_RemoteExplode(this, weaponentity);
272 if(this.csqcprojectile_clientanimate == 0)
273 UpdateCSQCProjectile(this);
276 void W_Devastator_Touch(entity this, entity toucher)
278 if(WarpZone_Projectile_Touch(this, toucher))
281 W_Devastator_Unregister(this);
284 W_Devastator_Unregister(this);
285 W_Devastator_Explode(this, toucher);
288 void W_Devastator_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
290 if(GetResource(this, RES_HEALTH) <= 0)
293 if(!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
294 return; // g_projectiles_damage says to halt
296 TakeResource(this, RES_HEALTH, damage);
297 this.angles = vectoangles(this.velocity);
299 if(GetResource(this, RES_HEALTH) <= 0)
300 W_PrepareExplosionByDamage(this, attacker, W_Devastator_Explode_think);
303 void W_Devastator_Attack(Weapon thiswep, entity actor, .entity weaponentity, int fire)
305 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(devastator, ammo), weaponentity);
307 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);
308 Send_Effect(EFFECT_ROCKET_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
310 entity missile = WarpZone_RefSys_SpawnSameRefSys(actor);
311 missile.weaponentity_fld = weaponentity;
312 missile.owner = missile.realowner = actor;
313 actor.(weaponentity).lastrocket = missile;
314 if(WEP_CVAR(devastator, detonatedelay) >= 0)
315 missile.spawnshieldtime = time + WEP_CVAR(devastator, detonatedelay);
317 missile.spawnshieldtime = -1;
318 missile.pushltime = time + WEP_CVAR(devastator, guidedelay);
319 missile.classname = "rocket";
320 missile.bot_dodge = true;
321 missile.bot_dodgerating = WEP_CVAR(devastator, damage) * 2; // * 2 because it can be detonated inflight which makes it even more dangerous
323 missile.takedamage = DAMAGE_YES;
324 missile.damageforcescale = WEP_CVAR(devastator, damageforcescale);
325 SetResourceExplicit(missile, RES_HEALTH, WEP_CVAR(devastator, health));
326 missile.event_damage = W_Devastator_Damage;
327 missile.damagedbycontents = true;
328 IL_PUSH(g_damagedbycontents, missile);
330 set_movetype(missile, MOVETYPE_FLY);
331 PROJECTILE_MAKETRIGGER(missile);
332 missile.projectiledeathtype = thiswep.m_id;
333 setsize(missile, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot
335 setorigin(missile, w_shotorg - v_forward * 3); // move it back so it hits the wall at the right point
336 W_SetupProjVelocity_Basic(missile, WEP_CVAR(devastator, speedstart), 0);
337 missile.angles = vectoangles(missile.velocity);
339 settouch(missile, W_Devastator_Touch);
340 setthink(missile, W_Devastator_Think);
341 missile.nextthink = time;
342 missile.cnt = time + WEP_CVAR(devastator, lifetime);
343 missile.rl_detonate_later = (fire & 2); // allow instant detonation
344 missile.flags = FL_PROJECTILE;
345 IL_PUSH(g_projectiles, missile);
346 IL_PUSH(g_bot_dodge, missile);
347 missile.missile_flags = MIF_SPLASH;
349 CSQCProjectile(missile, WEP_CVAR(devastator, guiderate) == 0 && WEP_CVAR(devastator, speedaccel) == 0, PROJECTILE_ROCKET, false); // because of fly sound
351 // muzzle flash for 1st person view
352 entity flash = spawn();
353 setmodel(flash, MDL_DEVASTATOR_MUZZLEFLASH); // precision set below
354 SUB_SetFade(flash, time, 0.1);
355 flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
356 W_AttachToShotorg(actor, weaponentity, flash, '5 0 0');
359 MUTATOR_CALLHOOK(EditProjectile, actor, missile);
361 if (time >= missile.nextthink)
363 getthink(missile)(missile);
367 METHOD(Devastator, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
369 // aim and decide to fire if appropriate
370 PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, WEP_CVAR(devastator, speed), 0, WEP_CVAR(devastator, lifetime), false);
371 if(skill >= 2) // skill 0 and 1 bots won't detonate rockets!
373 // decide whether to detonate rockets
374 float edgedamage, coredamage, edgeradius, recipricoledgeradius;
375 float selfdamage, teamdamage, enemydamage;
376 edgedamage = WEP_CVAR(devastator, edgedamage);
377 coredamage = WEP_CVAR(devastator, damage);
378 edgeradius = WEP_CVAR(devastator, radius);
379 recipricoledgeradius = 1 / edgeradius;
383 IL_EACH(g_projectiles, it.realowner == actor && it.classname == "rocket",
386 IL_EACH(g_bot_targets, it.bot_attack,
388 float d = vlen(it.origin + (it.mins + it.maxs) * 0.5 - rocket.origin);
389 d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
390 // count potential damage according to type of target
392 selfdamage = selfdamage + d;
393 else if(SAME_TEAM(it, actor))
394 teamdamage = teamdamage + d;
395 else if(bot_shouldattack(actor, it))
396 enemydamage = enemydamage + d;
399 float desirabledamage;
400 desirabledamage = enemydamage;
401 if(time > actor.invincible_finished && time > actor.spawnshieldtime)
402 desirabledamage = desirabledamage - selfdamage * autocvar_g_balance_selfdamagepercent;
403 if(teamplay && actor.team)
404 desirabledamage = desirabledamage - teamdamage;
406 makevectors(actor.v_angle);
407 IL_EACH(g_projectiles, it.realowner == actor && it.classname == "rocket",
409 if(skill > 9) // normal players only do this for the target they are tracking
412 IL_EACH(g_bot_targets, it.bot_attack,
414 if((v_forward * normalize(rocket.origin - it.origin) < 0.1)
415 && desirabledamage > 0.1 * coredamage
416 ) PHYS_INPUT_BUTTON_ATCK2(actor) = true;
421 //As the distance gets larger, a correct detonation gets near imposible
422 //Bots are assumed to use the rocket spawnfunc_light to see if the rocket gets near a player
423 if((v_forward * normalize(it.origin - actor.enemy.origin) < 0.1)
424 && IS_PLAYER(actor.enemy)
425 && (desirabledamage >= 0.1 * coredamage)
428 float distance = bound(300, vlen(actor.origin - actor.enemy.origin), 30000);
429 if(random() / distance * 300 > frametime * bound(0, (10 - skill) * 0.2, 1))
430 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
434 // if we would be doing at X percent of the core damage, detonate it
435 // but don't fire a new shot at the same time!
436 if(desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
437 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
438 if((skill > 6.5) && (selfdamage > GetResource(actor, RES_HEALTH)))
439 PHYS_INPUT_BUTTON_ATCK2(actor) = false;
440 //if(PHYS_INPUT_BUTTON_ATCK2(actor) == true)
441 // dprint(ftos(desirabledamage),"\n");
442 if(PHYS_INPUT_BUTTON_ATCK2(actor)) PHYS_INPUT_BUTTON_ATCK(actor) = false;
446 METHOD(Devastator, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
448 if(WEP_CVAR(devastator, reload_ammo) && actor.(weaponentity).clip_load < WEP_CVAR(devastator, ammo)) { // forced reload
449 thiswep.wr_reload(thiswep, actor, weaponentity);
453 if(actor.(weaponentity).rl_release || WEP_CVAR(devastator, guidestop))
454 if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR(devastator, refire)))
456 W_Devastator_Attack(thiswep, actor, weaponentity, fire);
457 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(devastator, animtime), w_ready);
458 actor.(weaponentity).rl_release = 0;
462 actor.(weaponentity).rl_release = 1;
465 if(actor.(weaponentity).m_switchweapon == thiswep)
467 bool rockfound = false;
468 IL_EACH(g_projectiles, it.realowner == actor && it.classname == "rocket",
470 if(!it.rl_detonate_later)
472 it.rl_detonate_later = true;
477 sound(actor, CH_WEAPON_B, SND_ROCKET_DET, VOL_BASE, ATTN_NORM);
481 METHOD(Devastator, wr_setup, void(entity thiswep, entity actor, .entity weaponentity))
483 actor.(weaponentity).rl_release = 1;
485 METHOD(Devastator, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
488 // don't switch while guiding a missile
489 if(ATTACK_FINISHED(actor, weaponentity) <= time || PS(actor).m_weapon != WEP_DEVASTATOR)
492 if(WEP_CVAR(devastator, reload_ammo))
494 if(GetResource(actor, thiswep.ammo_type) < WEP_CVAR(devastator, ammo) && actor.(weaponentity).(weapon_load[WEP_DEVASTATOR.m_id]) < WEP_CVAR(devastator, ammo))
497 else if(GetResource(actor, thiswep.ammo_type) < WEP_CVAR(devastator, ammo))
503 if(actor.rl_release == 0)
505 LOG_INFOF("W_Devastator(WR_CHECKAMMO1): %d, %.2f, %d: TRUE", actor.rl_release, GetResource(actor, thiswep.ammo_type), WEP_CVAR(devastator, ammo));
510 ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(devastator, ammo);
511 ammo_amount += actor.(weaponentity).(weapon_load[WEP_DEVASTATOR.m_id]) >= WEP_CVAR(devastator, ammo);
512 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"));
516 float ammo_amount = GetResource(actor, thiswep.ammo_type) >= WEP_CVAR(devastator, ammo);
517 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(devastator, ammo);
521 METHOD(Devastator, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
525 METHOD(Devastator, wr_resetplayer, void(entity thiswep, entity actor))
527 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
529 .entity weaponentity = weaponentities[slot];
530 actor.(weaponentity).lastrocket = NULL; // stop rocket guiding, no revenge from the grave!
531 actor.(weaponentity).rl_release = 0;
534 METHOD(Devastator, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
536 W_Reload(actor, weaponentity, WEP_CVAR(devastator, ammo), SND_RELOAD);
538 METHOD(Devastator, wr_suicidemessage, Notification(entity thiswep))
540 return WEAPON_DEVASTATOR_SUICIDE;
542 METHOD(Devastator, wr_killmessage, Notification(entity thiswep))
544 if((w_deathtype & HITTYPE_BOUNCE) || (w_deathtype & HITTYPE_SPLASH))
545 return WEAPON_DEVASTATOR_MURDER_SPLASH;
547 return WEAPON_DEVASTATOR_MURDER_DIRECT;
553 METHOD(Devastator, wr_impacteffect, void(entity thiswep, entity actor))
556 org2 = w_org + w_backoff * 12;
557 pointparticles(EFFECT_ROCKET_EXPLODE, org2, '0 0 0', 1);
559 sound(actor, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTN_NORM);