combat.qc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* Copyright (C) 1996-2022 id Software LLC
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  13. See file, 'COPYING', for details.
  14. */
  15. void() T_MissileTouch;
  16. void() info_player_start;
  17. void(entity targ, entity attacker) ClientObituary;
  18. void(entity who) ResistanceSound;
  19. //============================================================================
  20. /*
  21. ============
  22. CanDamage
  23. Returns true if the inflictor can directly damage the target. Used for
  24. explosions and melee attacks.
  25. ============
  26. */
  27. float(entity targ, entity inflictor) CanDamage =
  28. {
  29. // bmodels need special checking because their origin is 0,0,0
  30. if (targ.movetype == MOVETYPE_PUSH)
  31. {
  32. traceline(inflictor.origin, 0.5 * (targ.absmin + targ.absmax), TRUE, self);
  33. if (trace_fraction == 1)
  34. return TRUE;
  35. if (trace_ent == targ)
  36. return TRUE;
  37. return FALSE;
  38. }
  39. traceline(inflictor.origin, targ.origin, TRUE, self);
  40. if (trace_fraction == 1)
  41. return TRUE;
  42. traceline(inflictor.origin, targ.origin + '15 15 0', TRUE, self);
  43. if (trace_fraction == 1)
  44. return TRUE;
  45. traceline(inflictor.origin, targ.origin + '-15 -15 0', TRUE, self);
  46. if (trace_fraction == 1)
  47. return TRUE;
  48. traceline(inflictor.origin, targ.origin + '-15 15 0', TRUE, self);
  49. if (trace_fraction == 1)
  50. return TRUE;
  51. traceline(inflictor.origin, targ.origin + '15 -15 0', TRUE, self);
  52. if (trace_fraction == 1)
  53. return TRUE;
  54. return FALSE;
  55. };
  56. /*
  57. ============
  58. Killed
  59. ============
  60. */
  61. void(entity targ, entity attacker) Killed =
  62. {
  63. local entity oself;
  64. oself = self;
  65. self = targ;
  66. if (self.health < -99)
  67. self.health = -99; // don't let sbar look bad if a player
  68. if (self.movetype == MOVETYPE_PUSH || self.movetype == MOVETYPE_NONE)
  69. { // doors, triggers, etc
  70. self.th_die ();
  71. self = oself;
  72. return;
  73. }
  74. self.enemy = attacker;
  75. // bump the monster counter
  76. if (self.flags & FL_MONSTER)
  77. {
  78. killed_monsters = killed_monsters + 1;
  79. WriteByte (MSG_ALL, SVC_KILLEDMONSTER);
  80. }
  81. ClientObituary(self, attacker);
  82. self.takedamage = DAMAGE_NO;
  83. self.touch = SUB_Null;
  84. // removed monsters
  85. // monster_death_use();
  86. self.th_die ();
  87. self = oself;
  88. };
  89. // *TEAMPLAY*
  90. // Prototypes
  91. float(entity targ, entity inflictor, entity attacker, float damage) TeamArmorDam;
  92. float(entity targ, entity inflictor, entity attacker, float damage) TeamHealthDam;
  93. /*
  94. ============
  95. T_Damage
  96. The damage is coming from inflictor, but get mad at attacker
  97. This should be the only function that ever reduces health.
  98. ============
  99. */
  100. void(entity targ, entity inflictor, entity attacker, float damage) T_Damage=
  101. {
  102. local vector dir;
  103. local entity oldself;
  104. local float save;
  105. local float take;
  106. if (!targ.takedamage)
  107. return;
  108. // used by buttons and triggers to set activator for target firing
  109. damage_attacker = attacker;
  110. // check for quad damage powerup on the attacker
  111. if (attacker.super_damage_finished > time)
  112. damage = damage * 4;
  113. // RUNE: check for double damage for rune of Black Magic powerup
  114. if (attacker.player_flag & ITEM_RUNE2_FLAG)
  115. damage = damage * 2;
  116. // RUNE
  117. //RUNE check if target has rune of Earth Magic (half damage)
  118. if (targ.player_flag & ITEM_RUNE1_FLAG) {
  119. damage = damage / 2;
  120. ResistanceSound(targ);
  121. }
  122. //RUNE
  123. // *XXX* EXPERT CTF mark players who hurt the flag carrier, so they
  124. // are worth more points for a while.
  125. if ( (attacker.classname == "player") && // attacker must be a player
  126. (targ.player_flag & ITEM_ENEMY_FLAG) && // target is a flag carrier
  127. (attacker.lastteam != targ.lastteam) && // target and attacker on diff teams
  128. (targ.lastteam > 0) ) // unconnected check?
  129. attacker.last_hurt_carrier = time;
  130. // save damage based on the target's armor level
  131. // *TEAMPLAY*
  132. // TeamArmorDam returns true iff the attacker can damage the target's armor
  133. if (TeamArmorDam(targ, inflictor, attacker, damage))
  134. save = ceil(targ.armortype*damage);
  135. else
  136. save = 0;
  137. if (save >= targ.armorvalue)
  138. {
  139. save = targ.armorvalue;
  140. targ.armortype = 0; // lost all armor
  141. targ.items = targ.items - (targ.items & (IT_ARMOR1 | IT_ARMOR2 | IT_ARMOR3));
  142. }
  143. targ.armorvalue = targ.armorvalue - save;
  144. take = ceil(damage-save);
  145. // add to the damage total for clients, which will be sent as a single
  146. // message at the end of the frame
  147. // FIXME: remove after combining shotgun blasts?
  148. if (targ.flags & FL_CLIENT)
  149. {
  150. targ.dmg_take = targ.dmg_take + take;
  151. targ.dmg_save = targ.dmg_save + save;
  152. targ.dmg_inflictor = inflictor;
  153. }
  154. // figure momentum add
  155. if ( (inflictor != world) && (targ.movetype == MOVETYPE_WALK) )
  156. {
  157. dir = targ.origin - (inflictor.absmin + inflictor.absmax) * 0.5;
  158. dir = normalize(dir);
  159. targ.velocity = targ.velocity + dir*damage*8;
  160. }
  161. // check for godmode or invincibility
  162. if (targ.team == targ.lastteam) { // 666 or god mode doesn't save ya
  163. if (targ.flags & FL_GODMODE)
  164. return;
  165. if (targ.invincible_finished >= time)
  166. {
  167. if (self.invincible_sound < time)
  168. {
  169. sound (targ, CHAN_ITEM, "items/protect3.wav", 1, ATTN_NORM);
  170. self.invincible_sound = time + 2;
  171. }
  172. return;
  173. }
  174. }
  175. // team play damage avoidance
  176. if ( (teamplay == 1) && (targ.team > 0)&&(targ.team == attacker.team) )
  177. return;
  178. // *TEAMPLAY*
  179. // TeamHealthDam will return true if the attacker can damage the target's
  180. // health
  181. if (!TeamHealthDam(targ, inflictor, attacker, damage))
  182. return;
  183. // do the damage
  184. targ.health = targ.health - take;
  185. if (targ.health <= 0)
  186. {
  187. Killed (targ, attacker);
  188. return;
  189. }
  190. // react to the damage
  191. oldself = self;
  192. self = targ;
  193. if (self.th_pain)
  194. self.th_pain (attacker, take);
  195. self = oldself;
  196. };
  197. /*
  198. ============
  199. T_RadiusDamage
  200. ============
  201. */
  202. void(entity inflictor, entity attacker, float damage, entity ignore) T_RadiusDamage =
  203. {
  204. local float points;
  205. local entity head;
  206. local vector org;
  207. head = findradius(inflictor.origin, damage+40);
  208. while (head)
  209. {
  210. if (head != ignore)
  211. {
  212. if (head.takedamage)
  213. {
  214. org = head.origin + (head.mins + head.maxs)*0.5;
  215. points = 0.5*vlen (inflictor.origin - org);
  216. if (points < 0)
  217. points = 0;
  218. points = damage - points;
  219. if (head == attacker)
  220. points = points * 0.5;
  221. if (points > 0)
  222. {
  223. if (CanDamage (head, inflictor))
  224. { // shambler takes half damage from all explosions
  225. if (head.classname == "monster_shambler")
  226. T_Damage (head, inflictor, attacker, points*0.5);
  227. else
  228. T_Damage (head, inflictor, attacker, points);
  229. }
  230. }
  231. }
  232. }
  233. head = head.chain;
  234. }
  235. };
  236. /*
  237. ============
  238. T_BeamDamage
  239. ============
  240. */
  241. void(entity attacker, float damage) T_BeamDamage =
  242. {
  243. local float points;
  244. local entity head;
  245. head = findradius(attacker.origin, damage+40);
  246. while (head)
  247. {
  248. if (head.takedamage)
  249. {
  250. points = 0.5*vlen (attacker.origin - head.origin);
  251. if (points < 0)
  252. points = 0;
  253. points = damage - points;
  254. if (head == attacker)
  255. points = points * 0.5;
  256. if (points > 0)
  257. {
  258. if (CanDamage (head, attacker))
  259. {
  260. if (head.classname == "monster_shambler")
  261. T_Damage (head, attacker, attacker, points*0.5);
  262. else
  263. T_Damage (head, attacker, attacker, points);
  264. }
  265. }
  266. }
  267. head = head.chain;
  268. }
  269. };