g_combat.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. // g_combat.c
  16. #include "g_local.h"
  17. /*
  18. ============
  19. CanDamage
  20. Returns true if the inflictor can directly damage the target. Used for
  21. explosions and melee attacks.
  22. ============
  23. */
  24. qboolean CanDamage (edict_t *targ, edict_t *inflictor)
  25. {
  26. vec3_t dest;
  27. trace_t trace;
  28. // bmodels need special checking because their origin is 0,0,0
  29. if (targ->movetype == MOVETYPE_PUSH)
  30. {
  31. VectorAdd (targ->absmin, targ->absmax, dest);
  32. VectorScale (dest, 0.5, dest);
  33. trace = gi.trace (inflictor->s.origin, vec3_origin, vec3_origin, dest, inflictor, MASK_SOLID);
  34. if (trace.fraction == 1.0)
  35. return true;
  36. if (trace.ent == targ)
  37. return true;
  38. return false;
  39. }
  40. trace = gi.trace (inflictor->s.origin, vec3_origin, vec3_origin, targ->s.origin, inflictor, MASK_SOLID);
  41. if (trace.fraction == 1.0)
  42. return true;
  43. VectorCopy (targ->s.origin, dest);
  44. dest[0] += 15.0;
  45. dest[1] += 15.0;
  46. trace = gi.trace (inflictor->s.origin, vec3_origin, vec3_origin, dest, inflictor, MASK_SOLID);
  47. if (trace.fraction == 1.0)
  48. return true;
  49. VectorCopy (targ->s.origin, dest);
  50. dest[0] += 15.0;
  51. dest[1] -= 15.0;
  52. trace = gi.trace (inflictor->s.origin, vec3_origin, vec3_origin, dest, inflictor, MASK_SOLID);
  53. if (trace.fraction == 1.0)
  54. return true;
  55. VectorCopy (targ->s.origin, dest);
  56. dest[0] -= 15.0;
  57. dest[1] += 15.0;
  58. trace = gi.trace (inflictor->s.origin, vec3_origin, vec3_origin, dest, inflictor, MASK_SOLID);
  59. if (trace.fraction == 1.0)
  60. return true;
  61. VectorCopy (targ->s.origin, dest);
  62. dest[0] -= 15.0;
  63. dest[1] -= 15.0;
  64. trace = gi.trace (inflictor->s.origin, vec3_origin, vec3_origin, dest, inflictor, MASK_SOLID);
  65. if (trace.fraction == 1.0)
  66. return true;
  67. return false;
  68. }
  69. /*
  70. ============
  71. Killed
  72. ============
  73. */
  74. void Killed (edict_t *targ, edict_t *inflictor, edict_t *attacker, int damage, vec3_t point)
  75. {
  76. if (targ->health < -999)
  77. targ->health = -999;
  78. targ->enemy = attacker;
  79. if ((targ->svflags & SVF_MONSTER) && (targ->deadflag != DEAD_DEAD))
  80. {
  81. // targ->svflags |= SVF_DEADMONSTER; // now treat as a different content type
  82. if (!(targ->monsterinfo.aiflags & AI_GOOD_GUY))
  83. {
  84. level.killed_monsters++;
  85. if (coop->value && attacker->client)
  86. attacker->client->resp.score++;
  87. // medics won't heal monsters that they kill themselves
  88. if (strcmp(attacker->classname, "monster_medic") == 0)
  89. targ->owner = attacker;
  90. }
  91. }
  92. if (targ->movetype == MOVETYPE_PUSH || targ->movetype == MOVETYPE_STOP || targ->movetype == MOVETYPE_NONE)
  93. { // doors, triggers, etc
  94. targ->die (targ, inflictor, attacker, damage, point);
  95. return;
  96. }
  97. if ((targ->svflags & SVF_MONSTER) && (targ->deadflag != DEAD_DEAD))
  98. {
  99. targ->touch = NULL;
  100. monster_death_use (targ);
  101. }
  102. targ->die (targ, inflictor, attacker, damage, point);
  103. }
  104. /*
  105. ================
  106. SpawnDamage
  107. ================
  108. */
  109. void SpawnDamage (int type, vec3_t origin, vec3_t normal, int damage)
  110. {
  111. if (damage > 255)
  112. damage = 255;
  113. gi.WriteByte (svc_temp_entity);
  114. gi.WriteByte (type);
  115. // gi.WriteByte (damage);
  116. gi.WritePosition (origin);
  117. gi.WriteDir (normal);
  118. gi.multicast (origin, MULTICAST_PVS);
  119. }
  120. /*
  121. ============
  122. T_Damage
  123. targ entity that is being damaged
  124. inflictor entity that is causing the damage
  125. attacker entity that caused the inflictor to damage targ
  126. example: targ=monster, inflictor=rocket, attacker=player
  127. dir direction of the attack
  128. point point at which the damage is being inflicted
  129. normal normal vector from that point
  130. damage amount of damage being inflicted
  131. knockback force to be applied against targ as a result of the damage
  132. dflags these flags are used to control how T_Damage works
  133. DAMAGE_RADIUS damage was indirect (from a nearby explosion)
  134. DAMAGE_NO_ARMOR armor does not protect from this damage
  135. DAMAGE_ENERGY damage is from an energy based weapon
  136. DAMAGE_NO_KNOCKBACK do not affect velocity, just view angles
  137. DAMAGE_BULLET damage is from a bullet (used for ricochets)
  138. DAMAGE_NO_PROTECTION kills godmode, armor, everything
  139. ============
  140. */
  141. static int CheckPowerArmor (edict_t *ent, vec3_t point, vec3_t normal, int damage, int dflags)
  142. {
  143. gclient_t *client;
  144. int save;
  145. int power_armor_type;
  146. int index;
  147. int damagePerCell;
  148. int pa_te_type;
  149. int power;
  150. int power_used;
  151. if (!damage)
  152. return 0;
  153. client = ent->client;
  154. if (dflags & DAMAGE_NO_ARMOR)
  155. return 0;
  156. if (client)
  157. {
  158. power_armor_type = PowerArmorType (ent);
  159. if (power_armor_type != POWER_ARMOR_NONE)
  160. {
  161. index = ITEM_INDEX(FindItem("Cells"));
  162. power = client->pers.inventory[index];
  163. }
  164. }
  165. else if (ent->svflags & SVF_MONSTER)
  166. {
  167. power_armor_type = ent->monsterinfo.power_armor_type;
  168. power = ent->monsterinfo.power_armor_power;
  169. }
  170. else
  171. return 0;
  172. if (power_armor_type == POWER_ARMOR_NONE)
  173. return 0;
  174. if (!power)
  175. return 0;
  176. if (power_armor_type == POWER_ARMOR_SCREEN)
  177. {
  178. vec3_t vec;
  179. float dot;
  180. vec3_t forward;
  181. // only works if damage point is in front
  182. AngleVectors (ent->s.angles, forward, NULL, NULL);
  183. VectorSubtract (point, ent->s.origin, vec);
  184. VectorNormalize (vec);
  185. dot = DotProduct (vec, forward);
  186. if (dot <= 0.3)
  187. return 0;
  188. damagePerCell = 1;
  189. pa_te_type = TE_SCREEN_SPARKS;
  190. damage = damage / 3;
  191. }
  192. else
  193. {
  194. damagePerCell = 2;
  195. pa_te_type = TE_SHIELD_SPARKS;
  196. damage = (2 * damage) / 3;
  197. }
  198. save = power * damagePerCell;
  199. if (!save)
  200. return 0;
  201. if (save > damage)
  202. save = damage;
  203. SpawnDamage (pa_te_type, point, normal, save);
  204. ent->powerarmor_time = level.time + 0.2;
  205. power_used = save / damagePerCell;
  206. if (client)
  207. client->pers.inventory[index] -= power_used;
  208. else
  209. ent->monsterinfo.power_armor_power -= power_used;
  210. return save;
  211. }
  212. static int CheckArmor (edict_t *ent, vec3_t point, vec3_t normal, int damage, int te_sparks, int dflags)
  213. {
  214. gclient_t *client;
  215. int save;
  216. int index;
  217. gitem_t *armor;
  218. if (!damage)
  219. return 0;
  220. client = ent->client;
  221. if (!client)
  222. return 0;
  223. if (dflags & DAMAGE_NO_ARMOR)
  224. return 0;
  225. index = ArmorIndex (ent);
  226. if (!index)
  227. return 0;
  228. armor = GetItemByIndex (index);
  229. if (dflags & DAMAGE_ENERGY)
  230. save = ceil(((gitem_armor_t *)armor->info)->energy_protection*damage);
  231. else
  232. save = ceil(((gitem_armor_t *)armor->info)->normal_protection*damage);
  233. if (save >= client->pers.inventory[index])
  234. save = client->pers.inventory[index];
  235. if (!save)
  236. return 0;
  237. client->pers.inventory[index] -= save;
  238. SpawnDamage (te_sparks, point, normal, save);
  239. return save;
  240. }
  241. void M_ReactToDamage (edict_t *targ, edict_t *attacker)
  242. {
  243. if (!(attacker->client) && !(attacker->svflags & SVF_MONSTER))
  244. return;
  245. if (attacker == targ || attacker == targ->enemy)
  246. return;
  247. // if we are a good guy monster and our attacker is a player
  248. // or another good guy, do not get mad at them
  249. if (targ->monsterinfo.aiflags & AI_GOOD_GUY)
  250. {
  251. if (attacker->client || (attacker->monsterinfo.aiflags & AI_GOOD_GUY))
  252. return;
  253. }
  254. // we now know that we are not both good guys
  255. // if attacker is a client, get mad at them because he's good and we're not
  256. if (attacker->client)
  257. {
  258. targ->monsterinfo.aiflags &= ~AI_SOUND_TARGET;
  259. // this can only happen in coop (both new and old enemies are clients)
  260. // only switch if can't see the current enemy
  261. if (targ->enemy && targ->enemy->client)
  262. {
  263. if (visible(targ, targ->enemy))
  264. {
  265. targ->oldenemy = attacker;
  266. return;
  267. }
  268. targ->oldenemy = targ->enemy;
  269. }
  270. targ->enemy = attacker;
  271. if (!(targ->monsterinfo.aiflags & AI_DUCKED))
  272. FoundTarget (targ);
  273. return;
  274. }
  275. // it's the same base (walk/swim/fly) type and a different classname and it's not a tank
  276. // (they spray too much), get mad at them
  277. if (((targ->flags & (FL_FLY|FL_SWIM)) == (attacker->flags & (FL_FLY|FL_SWIM))) &&
  278. (strcmp (targ->classname, attacker->classname) != 0) &&
  279. (strcmp(attacker->classname, "monster_tank") != 0) &&
  280. (strcmp(attacker->classname, "monster_supertank") != 0) &&
  281. (strcmp(attacker->classname, "monster_makron") != 0) &&
  282. (strcmp(attacker->classname, "monster_jorg") != 0) )
  283. {
  284. if (targ->enemy && targ->enemy->client)
  285. targ->oldenemy = targ->enemy;
  286. targ->enemy = attacker;
  287. if (!(targ->monsterinfo.aiflags & AI_DUCKED))
  288. FoundTarget (targ);
  289. }
  290. // if they *meant* to shoot us, then shoot back
  291. else if (attacker->enemy == targ)
  292. {
  293. if (targ->enemy && targ->enemy->client)
  294. targ->oldenemy = targ->enemy;
  295. targ->enemy = attacker;
  296. if (!(targ->monsterinfo.aiflags & AI_DUCKED))
  297. FoundTarget (targ);
  298. }
  299. // otherwise get mad at whoever they are mad at (help our buddy) unless it is us!
  300. else if (attacker->enemy && attacker->enemy != targ)
  301. {
  302. if (targ->enemy && targ->enemy->client)
  303. targ->oldenemy = targ->enemy;
  304. targ->enemy = attacker->enemy;
  305. if (!(targ->monsterinfo.aiflags & AI_DUCKED))
  306. FoundTarget (targ);
  307. }
  308. }
  309. qboolean CheckTeamDamage (edict_t *targ, edict_t *attacker)
  310. {
  311. //FIXME make the next line real and uncomment this block
  312. // if ((ability to damage a teammate == OFF) && (targ's team == attacker's team))
  313. return false;
  314. }
  315. void T_Damage (edict_t *targ, edict_t *inflictor, edict_t *attacker, vec3_t dir, vec3_t point, vec3_t normal, int damage, int knockback, int dflags, int mod)
  316. {
  317. gclient_t *client;
  318. int take;
  319. int save;
  320. int asave;
  321. int psave;
  322. int te_sparks;
  323. if (!targ->takedamage)
  324. return;
  325. // friendly fire avoidance
  326. // if enabled you can't hurt teammates (but you can hurt yourself)
  327. // knockback still occurs
  328. if ((targ != attacker) && ((deathmatch->value && ((int)(dmflags->value) & (DF_MODELTEAMS | DF_SKINTEAMS))) || coop->value))
  329. {
  330. if (OnSameTeam (targ, attacker))
  331. {
  332. if ((int)(dmflags->value) & DF_NO_FRIENDLY_FIRE)
  333. damage = 0;
  334. else
  335. mod |= MOD_FRIENDLY_FIRE;
  336. }
  337. }
  338. meansOfDeath = mod;
  339. // easy mode takes half damage
  340. if (skill->value == 0 && deathmatch->value == 0 && targ->client)
  341. {
  342. damage *= 0.5;
  343. if (!damage)
  344. damage = 1;
  345. }
  346. client = targ->client;
  347. if (dflags & DAMAGE_BULLET)
  348. te_sparks = TE_BULLET_SPARKS;
  349. else
  350. te_sparks = TE_SPARKS;
  351. VectorNormalize(dir);
  352. // bonus damage for suprising a monster
  353. if (!(dflags & DAMAGE_RADIUS) && (targ->svflags & SVF_MONSTER) && (attacker->client) && (!targ->enemy) && (targ->health > 0))
  354. damage *= 2;
  355. if (targ->flags & FL_NO_KNOCKBACK)
  356. knockback = 0;
  357. // figure momentum add
  358. if (!(dflags & DAMAGE_NO_KNOCKBACK))
  359. {
  360. if ((knockback) && (targ->movetype != MOVETYPE_NONE) && (targ->movetype != MOVETYPE_BOUNCE) && (targ->movetype != MOVETYPE_PUSH) && (targ->movetype != MOVETYPE_STOP))
  361. {
  362. vec3_t kvel;
  363. float mass;
  364. if (targ->mass < 50)
  365. mass = 50;
  366. else
  367. mass = targ->mass;
  368. if (targ->client && attacker == targ)
  369. VectorScale (dir, 1600.0 * (float)knockback / mass, kvel); // the rocket jump hack...
  370. else
  371. VectorScale (dir, 500.0 * (float)knockback / mass, kvel);
  372. VectorAdd (targ->velocity, kvel, targ->velocity);
  373. }
  374. }
  375. take = damage;
  376. save = 0;
  377. // check for godmode
  378. if ( (targ->flags & FL_GODMODE) && !(dflags & DAMAGE_NO_PROTECTION) )
  379. {
  380. take = 0;
  381. save = damage;
  382. SpawnDamage (te_sparks, point, normal, save);
  383. }
  384. // check for invincibility
  385. if ((client && client->invincible_framenum > level.framenum ) && !(dflags & DAMAGE_NO_PROTECTION))
  386. {
  387. if (targ->pain_debounce_time < level.time)
  388. {
  389. gi.sound(targ, CHAN_ITEM, gi.soundindex("items/protect4.wav"), 1, ATTN_NORM, 0);
  390. targ->pain_debounce_time = level.time + 2;
  391. }
  392. take = 0;
  393. save = damage;
  394. }
  395. psave = CheckPowerArmor (targ, point, normal, take, dflags);
  396. take -= psave;
  397. asave = CheckArmor (targ, point, normal, take, te_sparks, dflags);
  398. take -= asave;
  399. //treat cheat/powerup savings the same as armor
  400. asave += save;
  401. // team damage avoidance
  402. if (!(dflags & DAMAGE_NO_PROTECTION) && CheckTeamDamage (targ, attacker))
  403. return;
  404. // do the damage
  405. if (take)
  406. {
  407. if ((targ->svflags & SVF_MONSTER) || (client))
  408. SpawnDamage (TE_BLOOD, point, normal, take);
  409. else
  410. SpawnDamage (te_sparks, point, normal, take);
  411. targ->health = targ->health - take;
  412. if (targ->health <= 0)
  413. {
  414. if ((targ->svflags & SVF_MONSTER) || (client))
  415. targ->flags |= FL_NO_KNOCKBACK;
  416. Killed (targ, inflictor, attacker, take, point);
  417. return;
  418. }
  419. }
  420. if (targ->svflags & SVF_MONSTER)
  421. {
  422. M_ReactToDamage (targ, attacker);
  423. if (!(targ->monsterinfo.aiflags & AI_DUCKED) && (take))
  424. {
  425. targ->pain (targ, attacker, knockback, take);
  426. // nightmare mode monsters don't go into pain frames often
  427. if (skill->value == 3)
  428. targ->pain_debounce_time = level.time + 5;
  429. }
  430. }
  431. else if (client)
  432. {
  433. if (!(targ->flags & FL_GODMODE) && (take))
  434. targ->pain (targ, attacker, knockback, take);
  435. }
  436. else if (take)
  437. {
  438. if (targ->pain)
  439. targ->pain (targ, attacker, knockback, take);
  440. }
  441. // add to the damage inflicted on a player this frame
  442. // the total will be turned into screen blends and view angle kicks
  443. // at the end of the frame
  444. if (client)
  445. {
  446. client->damage_parmor += psave;
  447. client->damage_armor += asave;
  448. client->damage_blood += take;
  449. client->damage_knockback += knockback;
  450. VectorCopy (point, client->damage_from);
  451. }
  452. }
  453. /*
  454. ============
  455. T_RadiusDamage
  456. ============
  457. */
  458. void T_RadiusDamage (edict_t *inflictor, edict_t *attacker, float damage, edict_t *ignore, float radius, int mod)
  459. {
  460. float points;
  461. edict_t *ent = NULL;
  462. vec3_t v;
  463. vec3_t dir;
  464. while ((ent = findradius(ent, inflictor->s.origin, radius)) != NULL)
  465. {
  466. if (ent == ignore)
  467. continue;
  468. if (!ent->takedamage)
  469. continue;
  470. VectorAdd (ent->mins, ent->maxs, v);
  471. VectorMA (ent->s.origin, 0.5, v, v);
  472. VectorSubtract (inflictor->s.origin, v, v);
  473. points = damage - 0.5 * VectorLength (v);
  474. if (ent == attacker)
  475. points = points * 0.5;
  476. if (points > 0)
  477. {
  478. if (CanDamage (ent, inflictor))
  479. {
  480. VectorSubtract (ent->s.origin, inflictor->s.origin, dir);
  481. T_Damage (ent, inflictor, attacker, dir, inflictor->s.origin, vec3_origin, (int)points, (int)points, DAMAGE_RADIUS, mod);
  482. }
  483. }
  484. }
  485. }