g_weapon.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "g_local.h"
  16. /*
  17. =================
  18. check_dodge
  19. This is a support routine used when a client is firing
  20. a non-instant attack weapon. It checks to see if a
  21. monster's dodge function should be called.
  22. =================
  23. */
  24. static void check_dodge (edict_t *self, vec3_t start, vec3_t dir, int speed)
  25. {
  26. vec3_t end;
  27. vec3_t v;
  28. trace_t tr;
  29. float eta;
  30. // easy mode only ducks one quarter the time
  31. if (skill->value == 0)
  32. {
  33. if (random() > 0.25)
  34. return;
  35. }
  36. VectorMA (start, 8192, dir, end);
  37. tr = gi.trace (start, NULL, NULL, end, self, MASK_SHOT);
  38. if ((tr.ent) && (tr.ent->svflags & SVF_MONSTER) && (tr.ent->health > 0) && (tr.ent->monsterinfo.dodge) && infront(tr.ent, self))
  39. {
  40. VectorSubtract (tr.endpos, start, v);
  41. eta = (VectorLength(v) - tr.ent->maxs[0]) / speed;
  42. tr.ent->monsterinfo.dodge (tr.ent, self, eta);
  43. }
  44. }
  45. /*
  46. =================
  47. fire_hit
  48. Used for all impact (hit/punch/slash) attacks
  49. =================
  50. */
  51. qboolean fire_hit (edict_t *self, vec3_t aim, int damage, int kick)
  52. {
  53. trace_t tr;
  54. vec3_t forward, right, up;
  55. vec3_t v;
  56. vec3_t point;
  57. float range;
  58. vec3_t dir;
  59. //see if enemy is in range
  60. VectorSubtract (self->enemy->s.origin, self->s.origin, dir);
  61. range = VectorLength(dir);
  62. if (range > aim[0])
  63. return false;
  64. if (aim[1] > self->mins[0] && aim[1] < self->maxs[0])
  65. {
  66. // the hit is straight on so back the range up to the edge of their bbox
  67. range -= self->enemy->maxs[0];
  68. }
  69. else
  70. {
  71. // this is a side hit so adjust the "right" value out to the edge of their bbox
  72. if (aim[1] < 0)
  73. aim[1] = self->enemy->mins[0];
  74. else
  75. aim[1] = self->enemy->maxs[0];
  76. }
  77. VectorMA (self->s.origin, range, dir, point);
  78. tr = gi.trace (self->s.origin, NULL, NULL, point, self, MASK_SHOT);
  79. if (tr.fraction < 1)
  80. {
  81. if (!tr.ent->takedamage)
  82. return false;
  83. // if it will hit any client/monster then hit the one we wanted to hit
  84. if ((tr.ent->svflags & SVF_MONSTER) || (tr.ent->client))
  85. tr.ent = self->enemy;
  86. }
  87. AngleVectors(self->s.angles, forward, right, up);
  88. VectorMA (self->s.origin, range, forward, point);
  89. VectorMA (point, aim[1], right, point);
  90. VectorMA (point, aim[2], up, point);
  91. VectorSubtract (point, self->enemy->s.origin, dir);
  92. // do the damage
  93. T_Damage (tr.ent, self, self, dir, point, vec3_origin, damage, kick/2, DAMAGE_NO_KNOCKBACK, MOD_HIT);
  94. if (!(tr.ent->svflags & SVF_MONSTER) && (!tr.ent->client))
  95. return false;
  96. // do our special form of knockback here
  97. VectorMA (self->enemy->absmin, 0.5, self->enemy->size, v);
  98. VectorSubtract (v, point, v);
  99. VectorNormalize (v);
  100. VectorMA (self->enemy->velocity, kick, v, self->enemy->velocity);
  101. if (self->enemy->velocity[2] > 0)
  102. self->enemy->groundentity = NULL;
  103. return true;
  104. }
  105. /*
  106. =================
  107. fire_lead
  108. This is an internal support routine used for bullet/pellet based weapons.
  109. =================
  110. */
  111. static void fire_lead (edict_t *self, vec3_t start, vec3_t aimdir, int damage, int kick, int te_impact, int hspread, int vspread, int mod)
  112. {
  113. trace_t tr;
  114. vec3_t dir;
  115. vec3_t forward, right, up;
  116. vec3_t end;
  117. float r;
  118. float u;
  119. vec3_t water_start;
  120. qboolean water = false;
  121. int content_mask = MASK_SHOT | MASK_WATER;
  122. tr = gi.trace (self->s.origin, NULL, NULL, start, self, MASK_SHOT);
  123. if (!(tr.fraction < 1.0))
  124. {
  125. vectoangles (aimdir, dir);
  126. AngleVectors (dir, forward, right, up);
  127. r = crandom()*hspread;
  128. u = crandom()*vspread;
  129. VectorMA (start, 8192, forward, end);
  130. VectorMA (end, r, right, end);
  131. VectorMA (end, u, up, end);
  132. if (gi.pointcontents (start) & MASK_WATER)
  133. {
  134. water = true;
  135. VectorCopy (start, water_start);
  136. content_mask &= ~MASK_WATER;
  137. }
  138. tr = gi.trace (start, NULL, NULL, end, self, content_mask);
  139. // see if we hit water
  140. if (tr.contents & MASK_WATER)
  141. {
  142. int color;
  143. water = true;
  144. VectorCopy (tr.endpos, water_start);
  145. if (!VectorCompare (start, tr.endpos))
  146. {
  147. if (tr.contents & CONTENTS_WATER)
  148. {
  149. if (strcmp(tr.surface->name, "*brwater") == 0)
  150. color = SPLASH_BROWN_WATER;
  151. else
  152. color = SPLASH_BLUE_WATER;
  153. }
  154. else if (tr.contents & CONTENTS_SLIME)
  155. color = SPLASH_SLIME;
  156. else if (tr.contents & CONTENTS_LAVA)
  157. color = SPLASH_LAVA;
  158. else
  159. color = SPLASH_UNKNOWN;
  160. if (color != SPLASH_UNKNOWN)
  161. {
  162. gi.WriteByte (svc_temp_entity);
  163. gi.WriteByte (TE_SPLASH);
  164. gi.WriteByte (8);
  165. gi.WritePosition (tr.endpos);
  166. gi.WriteDir (tr.plane.normal);
  167. gi.WriteByte (color);
  168. gi.multicast (tr.endpos, MULTICAST_PVS);
  169. }
  170. // change bullet's course when it enters water
  171. VectorSubtract (end, start, dir);
  172. vectoangles (dir, dir);
  173. AngleVectors (dir, forward, right, up);
  174. r = crandom()*hspread*2;
  175. u = crandom()*vspread*2;
  176. VectorMA (water_start, 8192, forward, end);
  177. VectorMA (end, r, right, end);
  178. VectorMA (end, u, up, end);
  179. }
  180. // re-trace ignoring water this time
  181. tr = gi.trace (water_start, NULL, NULL, end, self, MASK_SHOT);
  182. }
  183. }
  184. // send gun puff / flash
  185. if (!((tr.surface) && (tr.surface->flags & SURF_SKY)))
  186. {
  187. if (tr.fraction < 1.0)
  188. {
  189. if (tr.ent->takedamage)
  190. {
  191. T_Damage (tr.ent, self, self, aimdir, tr.endpos, tr.plane.normal, damage, kick, DAMAGE_BULLET, mod);
  192. }
  193. else
  194. {
  195. if (strncmp (tr.surface->name, "sky", 3) != 0)
  196. {
  197. gi.WriteByte (svc_temp_entity);
  198. gi.WriteByte (te_impact);
  199. gi.WritePosition (tr.endpos);
  200. gi.WriteDir (tr.plane.normal);
  201. gi.multicast (tr.endpos, MULTICAST_PVS);
  202. if (self->client)
  203. PlayerNoise(self, tr.endpos, PNOISE_IMPACT);
  204. }
  205. }
  206. }
  207. }
  208. // if went through water, determine where the end and make a bubble trail
  209. if (water)
  210. {
  211. vec3_t pos;
  212. VectorSubtract (tr.endpos, water_start, dir);
  213. VectorNormalize (dir);
  214. VectorMA (tr.endpos, -2, dir, pos);
  215. if (gi.pointcontents (pos) & MASK_WATER)
  216. VectorCopy (pos, tr.endpos);
  217. else
  218. tr = gi.trace (pos, NULL, NULL, water_start, tr.ent, MASK_WATER);
  219. VectorAdd (water_start, tr.endpos, pos);
  220. VectorScale (pos, 0.5, pos);
  221. gi.WriteByte (svc_temp_entity);
  222. gi.WriteByte (TE_BUBBLETRAIL);
  223. gi.WritePosition (water_start);
  224. gi.WritePosition (tr.endpos);
  225. gi.multicast (pos, MULTICAST_PVS);
  226. }
  227. }
  228. /*
  229. =================
  230. fire_bullet
  231. Fires a single round. Used for machinegun and chaingun. Would be fine for
  232. pistols, rifles, etc....
  233. =================
  234. */
  235. void fire_bullet (edict_t *self, vec3_t start, vec3_t aimdir, int damage, int kick, int hspread, int vspread, int mod)
  236. {
  237. fire_lead (self, start, aimdir, damage, kick, TE_GUNSHOT, hspread, vspread, mod);
  238. }
  239. /*
  240. =================
  241. fire_shotgun
  242. Shoots shotgun pellets. Used by shotgun and super shotgun.
  243. =================
  244. */
  245. void fire_shotgun (edict_t *self, vec3_t start, vec3_t aimdir, int damage, int kick, int hspread, int vspread, int count, int mod)
  246. {
  247. int i;
  248. for (i = 0; i < count; i++)
  249. fire_lead (self, start, aimdir, damage, kick, TE_SHOTGUN, hspread, vspread, mod);
  250. }
  251. /*
  252. =================
  253. fire_blaster
  254. Fires a single blaster bolt. Used by the blaster and hyper blaster.
  255. =================
  256. */
  257. void blaster_touch (edict_t *self, edict_t *other, cplane_t *plane, csurface_t *surf)
  258. {
  259. int mod;
  260. if (other == self->owner)
  261. return;
  262. if (surf && (surf->flags & SURF_SKY))
  263. {
  264. G_FreeEdict (self);
  265. return;
  266. }
  267. if (self->owner->client)
  268. PlayerNoise(self->owner, self->s.origin, PNOISE_IMPACT);
  269. if (other->takedamage)
  270. {
  271. if (self->spawnflags & 1)
  272. mod = MOD_HYPERBLASTER;
  273. else
  274. mod = MOD_BLASTER;
  275. T_Damage (other, self, self->owner, self->velocity, self->s.origin, plane->normal, self->dmg, 1, DAMAGE_ENERGY, mod);
  276. }
  277. else
  278. {
  279. gi.WriteByte (svc_temp_entity);
  280. gi.WriteByte (TE_BLASTER);
  281. gi.WritePosition (self->s.origin);
  282. if (!plane)
  283. gi.WriteDir (vec3_origin);
  284. else
  285. gi.WriteDir (plane->normal);
  286. gi.multicast (self->s.origin, MULTICAST_PVS);
  287. }
  288. G_FreeEdict (self);
  289. }
  290. void fire_blaster (edict_t *self, vec3_t start, vec3_t dir, int damage, int speed, int effect, qboolean hyper)
  291. {
  292. edict_t *bolt;
  293. trace_t tr;
  294. VectorNormalize (dir);
  295. bolt = G_Spawn();
  296. bolt->svflags = SVF_DEADMONSTER;
  297. // yes, I know it looks weird that projectiles are deadmonsters
  298. // what this means is that when prediction is used against the object
  299. // (blaster/hyperblaster shots), the player won't be solid clipped against
  300. // the object. Right now trying to run into a firing hyperblaster
  301. // is very jerky since you are predicted 'against' the shots.
  302. VectorCopy (start, bolt->s.origin);
  303. VectorCopy (start, bolt->s.old_origin);
  304. vectoangles (dir, bolt->s.angles);
  305. VectorScale (dir, speed, bolt->velocity);
  306. bolt->movetype = MOVETYPE_FLYMISSILE;
  307. bolt->clipmask = MASK_SHOT;
  308. bolt->solid = SOLID_BBOX;
  309. bolt->s.effects |= effect;
  310. VectorClear (bolt->mins);
  311. VectorClear (bolt->maxs);
  312. bolt->s.modelindex = gi.modelindex ("models/objects/laser/tris.md2");
  313. bolt->s.sound = gi.soundindex ("misc/lasfly.wav");
  314. bolt->owner = self;
  315. bolt->touch = blaster_touch;
  316. bolt->nextthink = level.time + 2;
  317. bolt->think = G_FreeEdict;
  318. bolt->dmg = damage;
  319. bolt->classname = "bolt";
  320. if (hyper)
  321. bolt->spawnflags = 1;
  322. gi.linkentity (bolt);
  323. if (self->client)
  324. check_dodge (self, bolt->s.origin, dir, speed);
  325. tr = gi.trace (self->s.origin, NULL, NULL, bolt->s.origin, bolt, MASK_SHOT);
  326. if (tr.fraction < 1.0)
  327. {
  328. VectorMA (bolt->s.origin, -10, dir, bolt->s.origin);
  329. bolt->touch (bolt, tr.ent, NULL, NULL);
  330. }
  331. }
  332. /*
  333. =================
  334. fire_grenade
  335. =================
  336. */
  337. static void Grenade_Explode (edict_t *ent)
  338. {
  339. vec3_t origin;
  340. int mod;
  341. if (ent->owner->client)
  342. PlayerNoise(ent->owner, ent->s.origin, PNOISE_IMPACT);
  343. //FIXME: if we are onground then raise our Z just a bit since we are a point?
  344. if (ent->enemy)
  345. {
  346. float points;
  347. vec3_t v;
  348. vec3_t dir;
  349. VectorAdd (ent->enemy->mins, ent->enemy->maxs, v);
  350. VectorMA (ent->enemy->s.origin, 0.5, v, v);
  351. VectorSubtract (ent->s.origin, v, v);
  352. points = ent->dmg - 0.5 * VectorLength (v);
  353. VectorSubtract (ent->enemy->s.origin, ent->s.origin, dir);
  354. if (ent->spawnflags & 1)
  355. mod = MOD_HANDGRENADE;
  356. else
  357. mod = MOD_GRENADE;
  358. T_Damage (ent->enemy, ent, ent->owner, dir, ent->s.origin, vec3_origin, (int)points, (int)points, DAMAGE_RADIUS, mod);
  359. }
  360. if (ent->spawnflags & 2)
  361. mod = MOD_HELD_GRENADE;
  362. else if (ent->spawnflags & 1)
  363. mod = MOD_HG_SPLASH;
  364. else
  365. mod = MOD_G_SPLASH;
  366. T_RadiusDamage(ent, ent->owner, ent->dmg, ent->enemy, ent->dmg_radius, mod);
  367. VectorMA (ent->s.origin, -0.02, ent->velocity, origin);
  368. gi.WriteByte (svc_temp_entity);
  369. if (ent->waterlevel)
  370. {
  371. if (ent->groundentity)
  372. gi.WriteByte (TE_GRENADE_EXPLOSION_WATER);
  373. else
  374. gi.WriteByte (TE_ROCKET_EXPLOSION_WATER);
  375. }
  376. else
  377. {
  378. if (ent->groundentity)
  379. gi.WriteByte (TE_GRENADE_EXPLOSION);
  380. else
  381. gi.WriteByte (TE_ROCKET_EXPLOSION);
  382. }
  383. gi.WritePosition (origin);
  384. gi.multicast (ent->s.origin, MULTICAST_PHS);
  385. G_FreeEdict (ent);
  386. }
  387. static void Grenade_Touch (edict_t *ent, edict_t *other, cplane_t *plane, csurface_t *surf)
  388. {
  389. if (other == ent->owner)
  390. return;
  391. if (surf && (surf->flags & SURF_SKY))
  392. {
  393. G_FreeEdict (ent);
  394. return;
  395. }
  396. if (!other->takedamage)
  397. {
  398. if (ent->spawnflags & 1)
  399. {
  400. if (random() > 0.5)
  401. gi.sound (ent, CHAN_VOICE, gi.soundindex ("weapons/hgrenb1a.wav"), 1, ATTN_NORM, 0);
  402. else
  403. gi.sound (ent, CHAN_VOICE, gi.soundindex ("weapons/hgrenb2a.wav"), 1, ATTN_NORM, 0);
  404. }
  405. else
  406. {
  407. gi.sound (ent, CHAN_VOICE, gi.soundindex ("weapons/grenlb1b.wav"), 1, ATTN_NORM, 0);
  408. }
  409. return;
  410. }
  411. ent->enemy = other;
  412. Grenade_Explode (ent);
  413. }
  414. void fire_grenade (edict_t *self, vec3_t start, vec3_t aimdir, int damage, int speed, float timer, float damage_radius)
  415. {
  416. edict_t *grenade;
  417. vec3_t dir;
  418. vec3_t forward, right, up;
  419. vectoangles (aimdir, dir);
  420. AngleVectors (dir, forward, right, up);
  421. grenade = G_Spawn();
  422. VectorCopy (start, grenade->s.origin);
  423. VectorScale (aimdir, speed, grenade->velocity);
  424. VectorMA (grenade->velocity, 200 + crandom() * 10.0, up, grenade->velocity);
  425. VectorMA (grenade->velocity, crandom() * 10.0, right, grenade->velocity);
  426. VectorSet (grenade->avelocity, 300, 300, 300);
  427. grenade->movetype = MOVETYPE_BOUNCE;
  428. grenade->clipmask = MASK_SHOT;
  429. grenade->solid = SOLID_BBOX;
  430. grenade->s.effects |= EF_GRENADE;
  431. VectorClear (grenade->mins);
  432. VectorClear (grenade->maxs);
  433. grenade->s.modelindex = gi.modelindex ("models/objects/grenade/tris.md2");
  434. grenade->owner = self;
  435. grenade->touch = Grenade_Touch;
  436. grenade->nextthink = level.time + timer;
  437. grenade->think = Grenade_Explode;
  438. grenade->dmg = damage;
  439. grenade->dmg_radius = damage_radius;
  440. grenade->classname = "grenade";
  441. gi.linkentity (grenade);
  442. }
  443. void fire_grenade2 (edict_t *self, vec3_t start, vec3_t aimdir, int damage, int speed, float timer, float damage_radius, qboolean held)
  444. {
  445. edict_t *grenade;
  446. vec3_t dir;
  447. vec3_t forward, right, up;
  448. vectoangles (aimdir, dir);
  449. AngleVectors (dir, forward, right, up);
  450. grenade = G_Spawn();
  451. VectorCopy (start, grenade->s.origin);
  452. VectorScale (aimdir, speed, grenade->velocity);
  453. VectorMA (grenade->velocity, 200 + crandom() * 10.0, up, grenade->velocity);
  454. VectorMA (grenade->velocity, crandom() * 10.0, right, grenade->velocity);
  455. VectorSet (grenade->avelocity, 300, 300, 300);
  456. grenade->movetype = MOVETYPE_BOUNCE;
  457. grenade->clipmask = MASK_SHOT;
  458. grenade->solid = SOLID_BBOX;
  459. grenade->s.effects |= EF_GRENADE;
  460. VectorClear (grenade->mins);
  461. VectorClear (grenade->maxs);
  462. grenade->s.modelindex = gi.modelindex ("models/objects/grenade2/tris.md2");
  463. grenade->owner = self;
  464. grenade->touch = Grenade_Touch;
  465. grenade->nextthink = level.time + timer;
  466. grenade->think = Grenade_Explode;
  467. grenade->dmg = damage;
  468. grenade->dmg_radius = damage_radius;
  469. grenade->classname = "hgrenade";
  470. if (held)
  471. grenade->spawnflags = 3;
  472. else
  473. grenade->spawnflags = 1;
  474. grenade->s.sound = gi.soundindex("weapons/hgrenc1b.wav");
  475. if (timer <= 0.0)
  476. Grenade_Explode (grenade);
  477. else
  478. {
  479. gi.sound (self, CHAN_WEAPON, gi.soundindex ("weapons/hgrent1a.wav"), 1, ATTN_NORM, 0);
  480. gi.linkentity (grenade);
  481. }
  482. }
  483. /*
  484. =================
  485. fire_rocket
  486. =================
  487. */
  488. void rocket_touch (edict_t *ent, edict_t *other, cplane_t *plane, csurface_t *surf)
  489. {
  490. vec3_t origin;
  491. int n;
  492. if (other == ent->owner)
  493. return;
  494. if (surf && (surf->flags & SURF_SKY))
  495. {
  496. G_FreeEdict (ent);
  497. return;
  498. }
  499. if (ent->owner->client)
  500. PlayerNoise(ent->owner, ent->s.origin, PNOISE_IMPACT);
  501. // calculate position for the explosion entity
  502. VectorMA (ent->s.origin, -0.02, ent->velocity, origin);
  503. if (other->takedamage)
  504. {
  505. T_Damage (other, ent, ent->owner, ent->velocity, ent->s.origin, plane->normal, ent->dmg, 0, 0, MOD_ROCKET);
  506. }
  507. else
  508. {
  509. // don't throw any debris in net games
  510. if (!deathmatch->value && !coop->value)
  511. {
  512. if ((surf) && !(surf->flags & (SURF_WARP|SURF_TRANS33|SURF_TRANS66|SURF_FLOWING)))
  513. {
  514. n = rand() % 5;
  515. while(n--)
  516. ThrowDebris (ent, "models/objects/debris2/tris.md2", 2, ent->s.origin);
  517. }
  518. }
  519. }
  520. T_RadiusDamage(ent, ent->owner, ent->radius_dmg, other, ent->dmg_radius, MOD_R_SPLASH);
  521. gi.WriteByte (svc_temp_entity);
  522. if (ent->waterlevel)
  523. gi.WriteByte (TE_ROCKET_EXPLOSION_WATER);
  524. else
  525. gi.WriteByte (TE_ROCKET_EXPLOSION);
  526. gi.WritePosition (origin);
  527. gi.multicast (ent->s.origin, MULTICAST_PHS);
  528. G_FreeEdict (ent);
  529. }
  530. void fire_rocket (edict_t *self, vec3_t start, vec3_t dir, int damage, int speed, float damage_radius, int radius_damage)
  531. {
  532. edict_t *rocket;
  533. rocket = G_Spawn();
  534. VectorCopy (start, rocket->s.origin);
  535. VectorCopy (dir, rocket->movedir);
  536. vectoangles (dir, rocket->s.angles);
  537. VectorScale (dir, speed, rocket->velocity);
  538. rocket->movetype = MOVETYPE_FLYMISSILE;
  539. rocket->clipmask = MASK_SHOT;
  540. rocket->solid = SOLID_BBOX;
  541. rocket->s.effects |= EF_ROCKET;
  542. VectorClear (rocket->mins);
  543. VectorClear (rocket->maxs);
  544. rocket->s.modelindex = gi.modelindex ("models/objects/rocket/tris.md2");
  545. rocket->owner = self;
  546. rocket->touch = rocket_touch;
  547. rocket->nextthink = level.time + 8000/speed;
  548. rocket->think = G_FreeEdict;
  549. rocket->dmg = damage;
  550. rocket->radius_dmg = radius_damage;
  551. rocket->dmg_radius = damage_radius;
  552. rocket->s.sound = gi.soundindex ("weapons/rockfly.wav");
  553. rocket->classname = "rocket";
  554. if (self->client)
  555. check_dodge (self, rocket->s.origin, dir, speed);
  556. gi.linkentity (rocket);
  557. }
  558. /*
  559. =================
  560. fire_rail
  561. =================
  562. */
  563. void fire_rail (edict_t *self, vec3_t start, vec3_t aimdir, int damage, int kick)
  564. {
  565. vec3_t from;
  566. vec3_t end;
  567. trace_t tr;
  568. edict_t *ignore;
  569. int mask;
  570. qboolean water;
  571. VectorMA (start, 8192, aimdir, end);
  572. VectorCopy (start, from);
  573. ignore = self;
  574. water = false;
  575. mask = MASK_SHOT|CONTENTS_SLIME|CONTENTS_LAVA;
  576. while (ignore)
  577. {
  578. tr = gi.trace (from, NULL, NULL, end, ignore, mask);
  579. if (tr.contents & (CONTENTS_SLIME|CONTENTS_LAVA))
  580. {
  581. mask &= ~(CONTENTS_SLIME|CONTENTS_LAVA);
  582. water = true;
  583. }
  584. else
  585. {
  586. if ((tr.ent->svflags & SVF_MONSTER) || (tr.ent->client))
  587. ignore = tr.ent;
  588. else
  589. ignore = NULL;
  590. if ((tr.ent != self) && (tr.ent->takedamage))
  591. T_Damage (tr.ent, self, self, aimdir, tr.endpos, tr.plane.normal, damage, kick, 0, MOD_RAILGUN);
  592. }
  593. VectorCopy (tr.endpos, from);
  594. }
  595. // send gun puff / flash
  596. gi.WriteByte (svc_temp_entity);
  597. gi.WriteByte (TE_RAILTRAIL);
  598. gi.WritePosition (start);
  599. gi.WritePosition (tr.endpos);
  600. gi.multicast (self->s.origin, MULTICAST_PHS);
  601. // gi.multicast (start, MULTICAST_PHS);
  602. if (water)
  603. {
  604. gi.WriteByte (svc_temp_entity);
  605. gi.WriteByte (TE_RAILTRAIL);
  606. gi.WritePosition (start);
  607. gi.WritePosition (tr.endpos);
  608. gi.multicast (tr.endpos, MULTICAST_PHS);
  609. }
  610. if (self->client)
  611. PlayerNoise(self, tr.endpos, PNOISE_IMPACT);
  612. }
  613. /*
  614. =================
  615. fire_bfg
  616. =================
  617. */
  618. void bfg_explode (edict_t *self)
  619. {
  620. edict_t *ent;
  621. float points;
  622. vec3_t v;
  623. float dist;
  624. if (self->s.frame == 0)
  625. {
  626. // the BFG effect
  627. ent = NULL;
  628. while ((ent = findradius(ent, self->s.origin, self->dmg_radius)) != NULL)
  629. {
  630. if (!ent->takedamage)
  631. continue;
  632. if (ent == self->owner)
  633. continue;
  634. if (!CanDamage (ent, self))
  635. continue;
  636. if (!CanDamage (ent, self->owner))
  637. continue;
  638. VectorAdd (ent->mins, ent->maxs, v);
  639. VectorMA (ent->s.origin, 0.5, v, v);
  640. VectorSubtract (self->s.origin, v, v);
  641. dist = VectorLength(v);
  642. points = self->radius_dmg * (1.0 - sqrt(dist/self->dmg_radius));
  643. if (ent == self->owner)
  644. points = points * 0.5;
  645. gi.WriteByte (svc_temp_entity);
  646. gi.WriteByte (TE_BFG_EXPLOSION);
  647. gi.WritePosition (ent->s.origin);
  648. gi.multicast (ent->s.origin, MULTICAST_PHS);
  649. T_Damage (ent, self, self->owner, self->velocity, ent->s.origin, vec3_origin, (int)points, 0, DAMAGE_ENERGY, MOD_BFG_EFFECT);
  650. }
  651. }
  652. self->nextthink = level.time + FRAMETIME;
  653. self->s.frame++;
  654. if (self->s.frame == 5)
  655. self->think = G_FreeEdict;
  656. }
  657. void bfg_touch (edict_t *self, edict_t *other, cplane_t *plane, csurface_t *surf)
  658. {
  659. if (other == self->owner)
  660. return;
  661. if (surf && (surf->flags & SURF_SKY))
  662. {
  663. G_FreeEdict (self);
  664. return;
  665. }
  666. if (self->owner->client)
  667. PlayerNoise(self->owner, self->s.origin, PNOISE_IMPACT);
  668. // core explosion - prevents firing it into the wall/floor
  669. if (other->takedamage)
  670. T_Damage (other, self, self->owner, self->velocity, self->s.origin, plane->normal, 200, 0, 0, MOD_BFG_BLAST);
  671. T_RadiusDamage(self, self->owner, 200, other, 100, MOD_BFG_BLAST);
  672. gi.sound (self, CHAN_VOICE, gi.soundindex ("weapons/bfg__x1b.wav"), 1, ATTN_NORM, 0);
  673. self->solid = SOLID_NOT;
  674. self->touch = NULL;
  675. VectorMA (self->s.origin, -1 * FRAMETIME, self->velocity, self->s.origin);
  676. VectorClear (self->velocity);
  677. self->s.modelindex = gi.modelindex ("sprites/s_bfg3.sp2");
  678. self->s.frame = 0;
  679. self->s.sound = 0;
  680. self->s.effects &= ~EF_ANIM_ALLFAST;
  681. self->think = bfg_explode;
  682. self->nextthink = level.time + FRAMETIME;
  683. self->enemy = other;
  684. gi.WriteByte (svc_temp_entity);
  685. gi.WriteByte (TE_BFG_BIGEXPLOSION);
  686. gi.WritePosition (self->s.origin);
  687. gi.multicast (self->s.origin, MULTICAST_PVS);
  688. }
  689. void bfg_think (edict_t *self)
  690. {
  691. edict_t *ent;
  692. edict_t *ignore;
  693. vec3_t point;
  694. vec3_t dir;
  695. vec3_t start;
  696. vec3_t end;
  697. int dmg;
  698. trace_t tr;
  699. if (deathmatch->value)
  700. dmg = 5;
  701. else
  702. dmg = 10;
  703. ent = NULL;
  704. while ((ent = findradius(ent, self->s.origin, 256)) != NULL)
  705. {
  706. if (ent == self)
  707. continue;
  708. if (ent == self->owner)
  709. continue;
  710. if (!ent->takedamage)
  711. continue;
  712. if (!(ent->svflags & SVF_MONSTER) && (!ent->client) && (strcmp(ent->classname, "misc_explobox") != 0))
  713. continue;
  714. VectorMA (ent->absmin, 0.5, ent->size, point);
  715. VectorSubtract (point, self->s.origin, dir);
  716. VectorNormalize (dir);
  717. ignore = self;
  718. VectorCopy (self->s.origin, start);
  719. VectorMA (start, 2048, dir, end);
  720. while(1)
  721. {
  722. tr = gi.trace (start, NULL, NULL, end, ignore, CONTENTS_SOLID|CONTENTS_MONSTER|CONTENTS_DEADMONSTER);
  723. if (!tr.ent)
  724. break;
  725. // hurt it if we can
  726. if ((tr.ent->takedamage) && !(tr.ent->flags & FL_IMMUNE_LASER) && (tr.ent != self->owner))
  727. T_Damage (tr.ent, self, self->owner, dir, tr.endpos, vec3_origin, dmg, 1, DAMAGE_ENERGY, MOD_BFG_LASER);
  728. // if we hit something that's not a monster or player we're done
  729. if (!(tr.ent->svflags & SVF_MONSTER) && (!tr.ent->client))
  730. {
  731. gi.WriteByte (svc_temp_entity);
  732. gi.WriteByte (TE_LASER_SPARKS);
  733. gi.WriteByte (4);
  734. gi.WritePosition (tr.endpos);
  735. gi.WriteDir (tr.plane.normal);
  736. gi.WriteByte (self->s.skinnum);
  737. gi.multicast (tr.endpos, MULTICAST_PVS);
  738. break;
  739. }
  740. ignore = tr.ent;
  741. VectorCopy (tr.endpos, start);
  742. }
  743. gi.WriteByte (svc_temp_entity);
  744. gi.WriteByte (TE_BFG_LASER);
  745. gi.WritePosition (self->s.origin);
  746. gi.WritePosition (tr.endpos);
  747. gi.multicast (self->s.origin, MULTICAST_PHS);
  748. }
  749. self->nextthink = level.time + FRAMETIME;
  750. }
  751. void fire_bfg (edict_t *self, vec3_t start, vec3_t dir, int damage, int speed, float damage_radius)
  752. {
  753. edict_t *bfg;
  754. bfg = G_Spawn();
  755. VectorCopy (start, bfg->s.origin);
  756. VectorCopy (dir, bfg->movedir);
  757. vectoangles (dir, bfg->s.angles);
  758. VectorScale (dir, speed, bfg->velocity);
  759. bfg->movetype = MOVETYPE_FLYMISSILE;
  760. bfg->clipmask = MASK_SHOT;
  761. bfg->solid = SOLID_BBOX;
  762. bfg->s.effects |= EF_BFG | EF_ANIM_ALLFAST;
  763. VectorClear (bfg->mins);
  764. VectorClear (bfg->maxs);
  765. bfg->s.modelindex = gi.modelindex ("sprites/s_bfg1.sp2");
  766. bfg->owner = self;
  767. bfg->touch = bfg_touch;
  768. bfg->nextthink = level.time + 8000/speed;
  769. bfg->think = G_FreeEdict;
  770. bfg->radius_dmg = damage;
  771. bfg->dmg_radius = damage_radius;
  772. bfg->classname = "bfg blast";
  773. bfg->s.sound = gi.soundindex ("weapons/bfg__l1a.wav");
  774. bfg->think = bfg_think;
  775. bfg->nextthink = level.time + FRAMETIME;
  776. bfg->teammaster = bfg;
  777. bfg->teamchain = NULL;
  778. if (self->client)
  779. check_dodge (self, bfg->s.origin, dir, speed);
  780. gi.linkentity (bfg);
  781. }