combat.qc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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() monster_death_use;
  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. if (attacker.classname == "player")
  81. {
  82. attacker.frags = attacker.frags + 1;
  83. }
  84. if (attacker != self && attacker.flags & FL_MONSTER)
  85. {
  86. WriteByte (MSG_ALL, SVC_ACHIEVEMENT);
  87. WriteString(MSG_ALL, "ACH_FRIENDLY_FIRE");
  88. }
  89. }
  90. ClientObituary(self, attacker);
  91. self.takedamage = DAMAGE_NO;
  92. self.touch = SUB_Null;
  93. monster_death_use();
  94. self.th_die ();
  95. self = oself;
  96. };
  97. /*
  98. ============
  99. T_Damage
  100. The damage is coming from inflictor, but get mad at attacker
  101. This should be the only function that ever reduces health.
  102. ============
  103. */
  104. void(entity targ, entity inflictor, entity attacker, float damage) T_Damage=
  105. {
  106. local vector dir;
  107. local entity oldself;
  108. local float save;
  109. local float take;
  110. if (!targ.takedamage)
  111. return;
  112. // mal: in Coop, don't let bots hurt human teammates - that would be REALLY annoying...
  113. if ( coop && targ != attacker && targ.classname == "player" && attacker.classname == "player" ) {
  114. if ( attacker.flags & FL_ISBOT && !( targ.flags & FL_ISBOT ) ) {
  115. return;
  116. }
  117. }
  118. // shub kill crash fix.
  119. if (targ.classname == "monster_oldone" && damage < 9999)
  120. return;
  121. // used by buttons and triggers to set activator for target firing
  122. damage_attacker = attacker;
  123. // check for quad damage powerup on the attacker
  124. if (attacker.super_damage_finished > time)
  125. damage = damage * 4;
  126. // save damage based on the target's armor level
  127. save = ceil(targ.armortype*damage);
  128. if (save >= targ.armorvalue)
  129. {
  130. save = targ.armorvalue;
  131. targ.armortype = 0; // lost all armor
  132. targ.items = targ.items - (targ.items & (IT_ARMOR1 | IT_ARMOR2 | IT_ARMOR3));
  133. }
  134. targ.armorvalue = targ.armorvalue - save;
  135. take = ceil(damage-save);
  136. // add to the damage total for clients, which will be sent as a single
  137. // message at the end of the frame
  138. // FIXME: remove after combining shotgun blasts?
  139. if (targ.flags & FL_CLIENT)
  140. {
  141. targ.dmg_take = targ.dmg_take + take;
  142. targ.dmg_save = targ.dmg_save + save;
  143. targ.dmg_inflictor = inflictor;
  144. }
  145. // figure momentum add
  146. if ( (inflictor != world) && (targ.movetype == MOVETYPE_WALK) )
  147. {
  148. dir = targ.origin - (inflictor.absmin + inflictor.absmax) * 0.5;
  149. dir = normalize(dir);
  150. targ.velocity = targ.velocity + dir*damage*8;
  151. }
  152. // check for godmode or invincibility
  153. if (targ.flags & FL_GODMODE)
  154. return;
  155. if (targ.invincible_finished >= time)
  156. {
  157. if (self.invincible_sound < time)
  158. {
  159. sound (targ, CHAN_ITEM, "items/protect3.wav", 1, ATTN_NORM);
  160. self.invincible_sound = time + 2;
  161. }
  162. return;
  163. }
  164. // team play damage avoidance
  165. if ( (teamplay == 1 && targ != attacker) && (targ.team > 0)&&(targ.team == attacker.team) )
  166. return;
  167. // [NDS] haleyjd: if player and actual health damage is inflicted, flag it here
  168. if (targ.classname == "player" && take != 0)
  169. {
  170. targ.took_damage = 1;
  171. }
  172. // do the damage
  173. targ.health = targ.health - take;
  174. if (targ.health <= 0)
  175. {
  176. Killed (targ, attacker);
  177. return;
  178. }
  179. //react to the damage
  180. oldself = self;
  181. self = targ;
  182. if ( (self.flags & FL_MONSTER) && attacker != world)
  183. {
  184. // get mad unless of the same class (except for soldiers)
  185. if (self != attacker && attacker != self.enemy)
  186. {
  187. if ( (self.classname != attacker.classname) || (self.classname == "monster_army" ) )
  188. {
  189. if (self.enemy.classname == "player")
  190. self.oldenemy = self.enemy;
  191. self.enemy = attacker;
  192. FoundTarget ();
  193. }
  194. }
  195. }
  196. if (self.th_pain)
  197. {
  198. self.th_pain (attacker, take);
  199. // nightmare mode monsters don't go into pain frames often
  200. if (skill == 3)
  201. self.pain_finished = time + 5;
  202. }
  203. self = oldself;
  204. };
  205. /*
  206. ============
  207. T_RadiusDamage
  208. ============
  209. */
  210. void(entity inflictor, entity attacker, float damage, entity ignore) T_RadiusDamage =
  211. {
  212. local float points;
  213. local entity head;
  214. local vector org;
  215. head = findradius(inflictor.origin, damage+40);
  216. while (head)
  217. {
  218. if (head != ignore)
  219. {
  220. if (head.takedamage)
  221. {
  222. org = head.origin + (head.mins + head.maxs)*0.5;
  223. points = 0.5*vlen (inflictor.origin - org);
  224. if (points < 0)
  225. points = 0;
  226. points = damage - points;
  227. if (head == attacker)
  228. points = points * 0.5;
  229. if (points > 0)
  230. {
  231. if (CanDamage (head, inflictor))
  232. { // shambler takes half damage from all explosions
  233. if (head.classname == "monster_shambler")
  234. T_Damage (head, inflictor, attacker, points*0.5);
  235. else
  236. T_Damage (head, inflictor, attacker, points);
  237. }
  238. }
  239. }
  240. }
  241. head = head.chain;
  242. }
  243. };
  244. /*
  245. ============
  246. T_BeamDamage
  247. ============
  248. */
  249. void(entity attacker, float damage) T_BeamDamage =
  250. {
  251. local float points;
  252. local entity head;
  253. head = findradius(attacker.origin, damage+40);
  254. while (head)
  255. {
  256. if (head.takedamage)
  257. {
  258. points = 0.5*vlen (attacker.origin - head.origin);
  259. if (points < 0)
  260. points = 0;
  261. points = damage - points;
  262. if (head == attacker)
  263. points = points * 0.5;
  264. if (points > 0)
  265. {
  266. if (CanDamage (head, attacker))
  267. {
  268. if (head.classname == "monster_shambler")
  269. T_Damage (head, attacker, attacker, points*0.5);
  270. else
  271. T_Damage (head, attacker, attacker, points);
  272. }
  273. }
  274. }
  275. head = head.chain;
  276. }
  277. };