m_rogue_turret.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. // Copyright (c) ZeniMax Media Inc.
  2. // Licensed under the GNU General Public License 2.0.
  3. /*
  4. ==============================================================================
  5. TURRET
  6. ==============================================================================
  7. */
  8. #include "../g_local.h"
  9. #include "m_rogue_turret.h"
  10. constexpr spawnflags_t SPAWNFLAG_TURRET_BLASTER = 0x0008_spawnflag;
  11. constexpr spawnflags_t SPAWNFLAG_TURRET_MACHINEGUN = 0x0010_spawnflag;
  12. constexpr spawnflags_t SPAWNFLAG_TURRET_ROCKET = 0x0020_spawnflag;
  13. constexpr spawnflags_t SPAWNFLAG_TURRET_HEATBEAM = 0x0040_spawnflag;
  14. constexpr spawnflags_t SPAWNFLAG_TURRET_WEAPONCHOICE = SPAWNFLAG_TURRET_HEATBEAM | SPAWNFLAG_TURRET_ROCKET | SPAWNFLAG_TURRET_MACHINEGUN | SPAWNFLAG_TURRET_BLASTER;
  15. constexpr spawnflags_t SPAWNFLAG_TURRET_WALL_UNIT = 0x0080_spawnflag;
  16. constexpr spawnflags_t SPAWNFLAG_TURRET_NO_LASERSIGHT = 18_spawnflag_bit;
  17. bool FindTarget(edict_t *self);
  18. void TurretAim(edict_t *self);
  19. void turret_ready_gun(edict_t *self);
  20. void turret_run(edict_t *self);
  21. extern const mmove_t turret_move_fire;
  22. extern const mmove_t turret_move_fire_blind;
  23. static cached_soundindex sound_moved, sound_moving;
  24. void TurretAim(edict_t *self)
  25. {
  26. vec3_t end, dir;
  27. vec3_t ang;
  28. float move, idealPitch, idealYaw, current, speed;
  29. int orientation;
  30. if (!self->enemy || self->enemy == world)
  31. {
  32. if (!FindTarget(self))
  33. return;
  34. }
  35. // if turret is still in inactive mode, ready the gun, but don't aim
  36. if (self->s.frame < FRAME_active01)
  37. {
  38. turret_ready_gun(self);
  39. return;
  40. }
  41. // if turret is still readying, don't aim.
  42. if (self->s.frame < FRAME_run01)
  43. return;
  44. // PMM - blindfire aiming here
  45. if (self->monsterinfo.active_move == &turret_move_fire_blind)
  46. {
  47. end = self->monsterinfo.blind_fire_target;
  48. if (self->enemy->s.origin[2] < self->monsterinfo.blind_fire_target[2])
  49. end[2] += self->enemy->viewheight + 10;
  50. else
  51. end[2] += self->enemy->mins[2] - 10;
  52. }
  53. else
  54. {
  55. end = self->enemy->s.origin;
  56. if (self->enemy->client)
  57. end[2] += self->enemy->viewheight;
  58. }
  59. dir = end - self->s.origin;
  60. ang = vectoangles(dir);
  61. //
  62. // Clamp first
  63. //
  64. idealPitch = ang[PITCH];
  65. idealYaw = ang[YAW];
  66. orientation = (int) self->offset[1];
  67. switch (orientation)
  68. {
  69. case -1: // up pitch: 0 to 90
  70. if (idealPitch < -90)
  71. idealPitch += 360;
  72. if (idealPitch > -5)
  73. idealPitch = -5;
  74. break;
  75. case -2: // down pitch: -180 to -360
  76. if (idealPitch > -90)
  77. idealPitch -= 360;
  78. if (idealPitch < -355)
  79. idealPitch = -355;
  80. else if (idealPitch > -185)
  81. idealPitch = -185;
  82. break;
  83. case 0: // +X pitch: 0 to -90, -270 to -360 (or 0 to 90)
  84. if (idealPitch < -180)
  85. idealPitch += 360;
  86. if (idealPitch > 85)
  87. idealPitch = 85;
  88. else if (idealPitch < -85)
  89. idealPitch = -85;
  90. // yaw: 270 to 360, 0 to 90
  91. // yaw: -90 to 90 (270-360 == -90-0)
  92. if (idealYaw > 180)
  93. idealYaw -= 360;
  94. if (idealYaw > 85)
  95. idealYaw = 85;
  96. else if (idealYaw < -85)
  97. idealYaw = -85;
  98. break;
  99. case 90: // +Y pitch: 0 to 90, -270 to -360 (or 0 to 90)
  100. if (idealPitch < -180)
  101. idealPitch += 360;
  102. if (idealPitch > 85)
  103. idealPitch = 85;
  104. else if (idealPitch < -85)
  105. idealPitch = -85;
  106. // yaw: 0 to 180
  107. if (idealYaw > 270)
  108. idealYaw -= 360;
  109. if (idealYaw > 175)
  110. idealYaw = 175;
  111. else if (idealYaw < 5)
  112. idealYaw = 5;
  113. break;
  114. case 180: // -X pitch: 0 to 90, -270 to -360 (or 0 to 90)
  115. if (idealPitch < -180)
  116. idealPitch += 360;
  117. if (idealPitch > 85)
  118. idealPitch = 85;
  119. else if (idealPitch < -85)
  120. idealPitch = -85;
  121. // yaw: 90 to 270
  122. if (idealYaw > 265)
  123. idealYaw = 265;
  124. else if (idealYaw < 95)
  125. idealYaw = 95;
  126. break;
  127. case 270: // -Y pitch: 0 to 90, -270 to -360 (or 0 to 90)
  128. if (idealPitch < -180)
  129. idealPitch += 360;
  130. if (idealPitch > 85)
  131. idealPitch = 85;
  132. else if (idealPitch < -85)
  133. idealPitch = -85;
  134. // yaw: 180 to 360
  135. if (idealYaw < 90)
  136. idealYaw += 360;
  137. if (idealYaw > 355)
  138. idealYaw = 355;
  139. else if (idealYaw < 185)
  140. idealYaw = 185;
  141. break;
  142. }
  143. //
  144. // adjust pitch
  145. //
  146. current = self->s.angles[PITCH];
  147. speed = self->yaw_speed / (gi.tick_rate / 10);
  148. if (idealPitch != current)
  149. {
  150. move = idealPitch - current;
  151. while (move >= 360)
  152. move -= 360;
  153. if (move >= 90)
  154. {
  155. move = move - 360;
  156. }
  157. while (move <= -360)
  158. move += 360;
  159. if (move <= -90)
  160. {
  161. move = move + 360;
  162. }
  163. if (move > 0)
  164. {
  165. if (move > speed)
  166. move = speed;
  167. }
  168. else
  169. {
  170. if (move < -speed)
  171. move = -speed;
  172. }
  173. self->s.angles[PITCH] = anglemod(current + move);
  174. }
  175. //
  176. // adjust yaw
  177. //
  178. current = self->s.angles[YAW];
  179. if (idealYaw != current)
  180. {
  181. move = idealYaw - current;
  182. // while(move >= 360)
  183. // move -= 360;
  184. if (move >= 180)
  185. {
  186. move = move - 360;
  187. }
  188. // while(move <= -360)
  189. // move += 360;
  190. if (move <= -180)
  191. {
  192. move = move + 360;
  193. }
  194. if (move > 0)
  195. {
  196. if (move > speed)
  197. move = speed;
  198. }
  199. else
  200. {
  201. if (move < -speed)
  202. move = -speed;
  203. }
  204. self->s.angles[YAW] = anglemod(current + move);
  205. }
  206. if (self->spawnflags.has(SPAWNFLAG_TURRET_NO_LASERSIGHT))
  207. return;
  208. // Paril: improved turrets; draw lasersight
  209. if (!self->target_ent)
  210. {
  211. self->target_ent = G_Spawn();
  212. self->target_ent->s.modelindex = MODELINDEX_WORLD;
  213. self->target_ent->s.renderfx = RF_BEAM;
  214. self->target_ent->s.frame = 1;
  215. self->target_ent->s.skinnum = 0xf0f0f0f0;
  216. self->target_ent->classname = "turret_lasersight";
  217. self->target_ent->s.origin = self->s.origin;
  218. }
  219. vec3_t forward;
  220. AngleVectors(self->s.angles, forward, nullptr, nullptr);
  221. end = self->s.origin + (forward * 8192);
  222. trace_t tr = gi.traceline(self->s.origin, end, self, MASK_SOLID);
  223. float scan_range = 64.f;
  224. if (visible(self, self->enemy))
  225. scan_range = 12.f;
  226. tr.endpos[0] += sinf(level.time.seconds() + self->s.number) * scan_range;
  227. tr.endpos[1] += cosf((level.time.seconds() - self->s.number) * 3.f) * scan_range;
  228. tr.endpos[2] += sinf((level.time.seconds() - self->s.number) * 2.5f) * scan_range;
  229. forward = tr.endpos - self->s.origin;
  230. forward.normalize();
  231. end = self->s.origin + (forward * 8192);
  232. tr = gi.traceline(self->s.origin, end, self, MASK_SOLID);
  233. self->target_ent->s.old_origin = tr.endpos;
  234. gi.linkentity(self->target_ent);
  235. }
  236. MONSTERINFO_SIGHT(turret_sight) (edict_t *self, edict_t *other) -> void
  237. {
  238. }
  239. MONSTERINFO_SEARCH(turret_search) (edict_t *self) -> void
  240. {
  241. }
  242. mframe_t turret_frames_stand[] = {
  243. { ai_stand },
  244. { ai_stand }
  245. };
  246. MMOVE_T(turret_move_stand) = { FRAME_stand01, FRAME_stand02, turret_frames_stand, nullptr };
  247. MONSTERINFO_STAND(turret_stand) (edict_t *self) -> void
  248. {
  249. M_SetAnimation(self, &turret_move_stand);
  250. if (self->target_ent)
  251. {
  252. G_FreeEdict(self->target_ent);
  253. self->target_ent = nullptr;
  254. }
  255. }
  256. mframe_t turret_frames_ready_gun[] = {
  257. { ai_stand },
  258. { ai_stand },
  259. { ai_stand },
  260. { ai_stand },
  261. { ai_stand },
  262. { ai_stand },
  263. { ai_stand }
  264. };
  265. MMOVE_T(turret_move_ready_gun) = { FRAME_active01, FRAME_run01, turret_frames_ready_gun, turret_run };
  266. void turret_ready_gun(edict_t *self)
  267. {
  268. if (self->monsterinfo.active_move != &turret_move_ready_gun)
  269. {
  270. M_SetAnimation(self, &turret_move_ready_gun);
  271. self->monsterinfo.weapon_sound = sound_moving;
  272. }
  273. }
  274. mframe_t turret_frames_seek[] = {
  275. { ai_walk, 0, TurretAim },
  276. { ai_walk, 0, TurretAim }
  277. };
  278. MMOVE_T(turret_move_seek) = { FRAME_run01, FRAME_run02, turret_frames_seek, nullptr };
  279. MONSTERINFO_WALK(turret_walk) (edict_t *self) -> void
  280. {
  281. if (self->s.frame < FRAME_run01)
  282. turret_ready_gun(self);
  283. else
  284. M_SetAnimation(self, &turret_move_seek);
  285. }
  286. mframe_t turret_frames_run[] = {
  287. { ai_run, 0, TurretAim },
  288. { ai_run, 0, TurretAim }
  289. };
  290. MMOVE_T(turret_move_run) = { FRAME_run01, FRAME_run02, turret_frames_run, turret_run };
  291. MONSTERINFO_RUN(turret_run) (edict_t *self) -> void
  292. {
  293. if (self->s.frame < FRAME_run01)
  294. turret_ready_gun(self);
  295. else
  296. {
  297. self->monsterinfo.aiflags |= AI_HIGH_TICK_RATE;
  298. M_SetAnimation(self, &turret_move_run);
  299. if (self->monsterinfo.weapon_sound)
  300. {
  301. self->monsterinfo.weapon_sound = 0;
  302. gi.sound(self, CHAN_WEAPON, sound_moved, 1.0f, ATTN_NORM, 0.f);
  303. }
  304. }
  305. }
  306. // **********************
  307. // ATTACK
  308. // **********************
  309. constexpr int32_t TURRET_BLASTER_DAMAGE = 8;
  310. constexpr int32_t TURRET_BULLET_DAMAGE = 2;
  311. // unused
  312. // constexpr int32_t TURRET_HEAT_DAMAGE = 4;
  313. void TurretFire(edict_t *self)
  314. {
  315. vec3_t forward;
  316. vec3_t start, end, dir;
  317. float dist, chance;
  318. trace_t trace;
  319. int rocketSpeed;
  320. TurretAim(self);
  321. if (!self->enemy || !self->enemy->inuse)
  322. return;
  323. if (self->monsterinfo.aiflags & AI_LOST_SIGHT)
  324. end = self->monsterinfo.blind_fire_target;
  325. else
  326. end = self->enemy->s.origin;
  327. dir = end - self->s.origin;
  328. dir.normalize();
  329. AngleVectors(self->s.angles, forward, nullptr, nullptr);
  330. chance = dir.dot(forward);
  331. if (chance < 0.98f)
  332. return;
  333. chance = frandom();
  334. if (self->spawnflags.has(SPAWNFLAG_TURRET_ROCKET))
  335. rocketSpeed = 650;
  336. else if (self->spawnflags.has(SPAWNFLAG_TURRET_BLASTER))
  337. rocketSpeed = 800;
  338. else
  339. rocketSpeed = 0;
  340. if (self->spawnflags.has(SPAWNFLAG_TURRET_MACHINEGUN) || visible(self, self->enemy))
  341. {
  342. start = self->s.origin;
  343. // aim for the head.
  344. if (!(self->monsterinfo.aiflags & AI_LOST_SIGHT))
  345. {
  346. if ((self->enemy) && (self->enemy->client))
  347. end[2] += self->enemy->viewheight;
  348. else
  349. end[2] += 22;
  350. }
  351. dir = end - start;
  352. dist = dir.length();
  353. // check for predictive fire
  354. // Paril: adjusted to be a bit more fair
  355. if (!(self->monsterinfo.aiflags & AI_LOST_SIGHT))
  356. {
  357. // on harder difficulties, randomly fire directly at enemy
  358. // more often; makes them more unpredictable
  359. if (self->spawnflags.has(SPAWNFLAG_TURRET_MACHINEGUN))
  360. PredictAim(self, self->enemy, start, 0, true, 0.3f, &dir, nullptr);
  361. else if (frandom() < skill->integer / 5.f)
  362. PredictAim(self, self->enemy, start, (float) rocketSpeed, true, (frandom(3.f - skill->integer) / 3.f) - frandom(0.05f * (3.f - skill->integer)), &dir, nullptr);
  363. }
  364. dir.normalize();
  365. trace = gi.traceline(start, end, self, MASK_PROJECTILE);
  366. if (trace.ent == self->enemy || trace.ent == world)
  367. {
  368. if (self->spawnflags.has(SPAWNFLAG_TURRET_BLASTER))
  369. monster_fire_blaster(self, start, dir, TURRET_BLASTER_DAMAGE, rocketSpeed, MZ2_TURRET_BLASTER, EF_BLASTER);
  370. else if (self->spawnflags.has(SPAWNFLAG_TURRET_MACHINEGUN))
  371. {
  372. if (!(self->monsterinfo.aiflags & AI_HOLD_FRAME))
  373. {
  374. self->monsterinfo.aiflags |= AI_HOLD_FRAME;
  375. self->monsterinfo.duck_wait_time = level.time + 2_sec + gtime_t::from_sec(frandom(skill->value));
  376. self->monsterinfo.next_duck_time = level.time + 1_sec;
  377. gi.sound(self, CHAN_VOICE, gi.soundindex("weapons/chngnu1a.wav"), 1, ATTN_NORM, 0);
  378. }
  379. else
  380. {
  381. if (self->monsterinfo.next_duck_time < level.time &&
  382. self->monsterinfo.melee_debounce_time <= level.time)
  383. {
  384. monster_fire_bullet(self, start, dir, TURRET_BULLET_DAMAGE, 0, DEFAULT_BULLET_HSPREAD, DEFAULT_BULLET_VSPREAD, MZ2_TURRET_MACHINEGUN);
  385. self->monsterinfo.melee_debounce_time = level.time + 10_hz;
  386. }
  387. if (self->monsterinfo.duck_wait_time < level.time)
  388. self->monsterinfo.aiflags &= ~AI_HOLD_FRAME;
  389. }
  390. }
  391. else if (self->spawnflags.has(SPAWNFLAG_TURRET_ROCKET))
  392. {
  393. if (dist * trace.fraction > 72)
  394. monster_fire_rocket(self, start, dir, 40, rocketSpeed, MZ2_TURRET_ROCKET);
  395. }
  396. }
  397. }
  398. }
  399. // PMM
  400. void TurretFireBlind(edict_t *self)
  401. {
  402. vec3_t forward;
  403. vec3_t start, end, dir;
  404. float chance;
  405. int rocketSpeed = 550;
  406. TurretAim(self);
  407. if (!self->enemy || !self->enemy->inuse)
  408. return;
  409. dir = self->monsterinfo.blind_fire_target - self->s.origin;
  410. dir.normalize();
  411. AngleVectors(self->s.angles, forward, nullptr, nullptr);
  412. chance = dir.dot(forward);
  413. if (chance < 0.98f)
  414. return;
  415. if (self->spawnflags.has(SPAWNFLAG_TURRET_ROCKET))
  416. rocketSpeed = 650;
  417. else if (self->spawnflags.has(SPAWNFLAG_TURRET_BLASTER))
  418. rocketSpeed = 800;
  419. else
  420. rocketSpeed = 0;
  421. start = self->s.origin;
  422. end = self->monsterinfo.blind_fire_target;
  423. if (self->enemy->s.origin[2] < self->monsterinfo.blind_fire_target[2])
  424. end[2] += self->enemy->viewheight + 10;
  425. else
  426. end[2] += self->enemy->mins[2] - 10;
  427. dir = end - start;
  428. dir.normalize();
  429. if (self->spawnflags.has(SPAWNFLAG_TURRET_BLASTER))
  430. monster_fire_blaster(self, start, dir, TURRET_BLASTER_DAMAGE, rocketSpeed, MZ2_TURRET_BLASTER, EF_BLASTER);
  431. else if (self->spawnflags.has(SPAWNFLAG_TURRET_ROCKET))
  432. monster_fire_rocket(self, start, dir, 40, rocketSpeed, MZ2_TURRET_ROCKET);
  433. }
  434. // pmm
  435. mframe_t turret_frames_fire[] = {
  436. { ai_run, 0, TurretFire },
  437. { ai_run, 0, TurretAim },
  438. { ai_run, 0, TurretAim },
  439. { ai_run, 0, TurretAim }
  440. };
  441. MMOVE_T(turret_move_fire) = { FRAME_pow01, FRAME_pow04, turret_frames_fire, turret_run };
  442. // PMM
  443. // the blind frames need to aim first
  444. mframe_t turret_frames_fire_blind[] = {
  445. { ai_run, 0, TurretAim },
  446. { ai_run, 0, TurretAim },
  447. { ai_run, 0, TurretAim },
  448. { ai_run, 0, TurretFireBlind }
  449. };
  450. MMOVE_T(turret_move_fire_blind) = { FRAME_pow01, FRAME_pow04, turret_frames_fire_blind, turret_run };
  451. // pmm
  452. MONSTERINFO_ATTACK(turret_attack) (edict_t *self) -> void
  453. {
  454. float r, chance;
  455. if (self->s.frame < FRAME_run01)
  456. turret_ready_gun(self);
  457. // PMM
  458. else if (self->monsterinfo.attack_state != AS_BLIND)
  459. {
  460. M_SetAnimation(self, &turret_move_fire);
  461. }
  462. else
  463. {
  464. // setup shot probabilities
  465. if (self->monsterinfo.blind_fire_delay < 1_sec)
  466. chance = 1.0;
  467. else if (self->monsterinfo.blind_fire_delay < 7.5_sec)
  468. chance = 0.4f;
  469. else
  470. chance = 0.1f;
  471. r = frandom();
  472. // minimum of 3 seconds, plus 0-4, after the shots are done - total time should be max less than 7.5
  473. self->monsterinfo.blind_fire_delay += random_time(3.4_sec, 7.4_sec);
  474. // don't shoot at the origin
  475. if (!self->monsterinfo.blind_fire_target)
  476. return;
  477. // don't shoot if the dice say not to
  478. if (r > chance)
  479. return;
  480. M_SetAnimation(self, &turret_move_fire_blind);
  481. }
  482. // pmm
  483. }
  484. // **********************
  485. // PAIN
  486. // **********************
  487. PAIN(turret_pain) (edict_t *self, edict_t *other, float kick, int damage, const mod_t &mod) -> void
  488. {
  489. }
  490. // **********************
  491. // DEATH
  492. // **********************
  493. DIE(turret_die) (edict_t *self, edict_t *inflictor, edict_t *attacker, int damage, const vec3_t &point, const mod_t &mod) -> void
  494. {
  495. vec3_t forward;
  496. edict_t *base;
  497. AngleVectors(self->s.angles, forward, nullptr, nullptr);
  498. self->s.origin += (forward * 1);
  499. ThrowGibs(self, 2, {
  500. { 2, "models/objects/debris1/tris.md2", GIB_METALLIC | GIB_DEBRIS }
  501. });
  502. ThrowGibs(self, 1, {
  503. { 2, "models/objects/debris1/tris.md2", GIB_METALLIC | GIB_DEBRIS }
  504. });
  505. gi.WriteByte(svc_temp_entity);
  506. gi.WriteByte(TE_PLAIN_EXPLOSION);
  507. gi.WritePosition(self->s.origin);
  508. gi.multicast(self->s.origin, MULTICAST_PHS, false);
  509. if (self->teamchain)
  510. {
  511. base = self->teamchain;
  512. base->solid = SOLID_NOT;
  513. base->takedamage = false;
  514. base->movetype = MOVETYPE_NONE;
  515. base->teammaster = base;
  516. base->teamchain = nullptr;
  517. base->flags &= ~FL_TEAMSLAVE;
  518. base->flags |= FL_TEAMMASTER;
  519. gi.linkentity(base);
  520. self->teammaster = self->teamchain = nullptr;
  521. self->flags &= ~(FL_TEAMSLAVE | FL_TEAMMASTER);
  522. }
  523. if (self->target)
  524. {
  525. if (self->enemy && self->enemy->inuse)
  526. G_UseTargets(self, self->enemy);
  527. else
  528. G_UseTargets(self, self);
  529. }
  530. if (self->target_ent)
  531. {
  532. G_FreeEdict(self->target_ent);
  533. self->target_ent = nullptr;
  534. }
  535. edict_t *gib = ThrowGib(self, "models/monsters/turret/tris.md2", damage, GIB_SKINNED | GIB_METALLIC | GIB_HEAD | GIB_DEBRIS, self->s.scale);
  536. gib->s.frame = 14;
  537. }
  538. // **********************
  539. // WALL SPAWN
  540. // **********************
  541. void turret_wall_spawn(edict_t *turret)
  542. {
  543. edict_t *ent;
  544. int angle;
  545. ent = G_Spawn();
  546. ent->s.origin = turret->s.origin;
  547. ent->s.angles = turret->s.angles;
  548. angle = (int) ent->s.angles[1];
  549. if (ent->s.angles[0] == 90)
  550. angle = -1;
  551. else if (ent->s.angles[0] == 270)
  552. angle = -2;
  553. switch (angle)
  554. {
  555. case -1:
  556. ent->mins = { -16, -16, -8 };
  557. ent->maxs = { 16, 16, 0 };
  558. break;
  559. case -2:
  560. ent->mins = { -16, -16, 0 };
  561. ent->maxs = { 16, 16, 8 };
  562. break;
  563. case 0:
  564. ent->mins = { -8, -16, -16 };
  565. ent->maxs = { 0, 16, 16 };
  566. break;
  567. case 90:
  568. ent->mins = { -16, -8, -16 };
  569. ent->maxs = { 16, 0, 16 };
  570. break;
  571. case 180:
  572. ent->mins = { 0, -16, -16 };
  573. ent->maxs = { 8, 16, 16 };
  574. break;
  575. case 270:
  576. ent->mins = { -16, 0, -16 };
  577. ent->maxs = { 16, 8, 16 };
  578. break;
  579. }
  580. ent->movetype = MOVETYPE_PUSH;
  581. ent->solid = SOLID_NOT;
  582. ent->teammaster = turret;
  583. turret->flags |= FL_TEAMMASTER;
  584. turret->teammaster = turret;
  585. turret->teamchain = ent;
  586. ent->teamchain = nullptr;
  587. ent->flags |= FL_TEAMSLAVE;
  588. ent->owner = turret;
  589. ent->s.modelindex = gi.modelindex("models/monsters/turretbase/tris.md2");
  590. gi.linkentity(ent);
  591. }
  592. MOVEINFO_ENDFUNC(turret_wake) (edict_t *ent) -> void
  593. {
  594. // the wall section will call this when it stops moving.
  595. // just return without doing anything. easiest way to have a null function.
  596. if (ent->flags & FL_TEAMSLAVE)
  597. {
  598. ent->s.sound = 0;
  599. return;
  600. }
  601. ent->monsterinfo.stand = turret_stand;
  602. ent->monsterinfo.walk = turret_walk;
  603. ent->monsterinfo.run = turret_run;
  604. ent->monsterinfo.dodge = nullptr;
  605. ent->monsterinfo.attack = turret_attack;
  606. ent->monsterinfo.melee = nullptr;
  607. ent->monsterinfo.sight = turret_sight;
  608. ent->monsterinfo.search = turret_search;
  609. M_SetAnimation(ent, &turret_move_stand);
  610. ent->takedamage = true;
  611. ent->movetype = MOVETYPE_NONE;
  612. // prevent counting twice
  613. ent->monsterinfo.aiflags |= AI_DO_NOT_COUNT;
  614. gi.linkentity(ent);
  615. stationarymonster_start(ent);
  616. if (ent->spawnflags.has(SPAWNFLAG_TURRET_MACHINEGUN))
  617. {
  618. ent->s.skinnum = 1;
  619. }
  620. else if (ent->spawnflags.has(SPAWNFLAG_TURRET_ROCKET))
  621. {
  622. ent->s.skinnum = 2;
  623. }
  624. // but we do want the death to count
  625. ent->monsterinfo.aiflags &= ~AI_DO_NOT_COUNT;
  626. }
  627. USE(turret_activate) (edict_t *self, edict_t *other, edict_t *activator) -> void
  628. {
  629. vec3_t endpos;
  630. vec3_t forward;
  631. edict_t *base;
  632. self->movetype = MOVETYPE_PUSH;
  633. if (!self->speed)
  634. self->speed = 15;
  635. self->moveinfo.speed = self->speed;
  636. self->moveinfo.accel = self->speed;
  637. self->moveinfo.decel = self->speed;
  638. if (self->s.angles[0] == 270)
  639. {
  640. forward = { 0, 0, 1 };
  641. }
  642. else if (self->s.angles[0] == 90)
  643. {
  644. forward = { 0, 0, -1 };
  645. }
  646. else if (self->s.angles[1] == 0)
  647. {
  648. forward = { 1, 0, 0 };
  649. }
  650. else if (self->s.angles[1] == 90)
  651. {
  652. forward = { 0, 1, 0 };
  653. }
  654. else if (self->s.angles[1] == 180)
  655. {
  656. forward = { -1, 0, 0 };
  657. }
  658. else if (self->s.angles[1] == 270)
  659. {
  660. forward = { 0, -1, 0 };
  661. }
  662. // start up the turret
  663. endpos = self->s.origin + (forward * 32);
  664. Move_Calc(self, endpos, turret_wake);
  665. base = self->teamchain;
  666. if (base)
  667. {
  668. base->movetype = MOVETYPE_PUSH;
  669. base->speed = self->speed;
  670. base->moveinfo.speed = base->speed;
  671. base->moveinfo.accel = base->speed;
  672. base->moveinfo.decel = base->speed;
  673. // start up the wall section
  674. endpos = self->teamchain->s.origin + (forward * 32);
  675. Move_Calc(self->teamchain, endpos, turret_wake);
  676. base->s.sound = sound_moving;
  677. base->s.loop_attenuation = ATTN_NORM;
  678. }
  679. }
  680. // PMM
  681. // checkattack .. ignore range, just attack if available
  682. MONSTERINFO_CHECKATTACK(turret_checkattack) (edict_t *self) -> bool
  683. {
  684. vec3_t spot1, spot2;
  685. float chance;
  686. trace_t tr;
  687. if (self->enemy->health > 0)
  688. {
  689. // see if any entities are in the way of the shot
  690. spot1 = self->s.origin;
  691. spot1[2] += self->viewheight;
  692. spot2 = self->enemy->s.origin;
  693. spot2[2] += self->enemy->viewheight;
  694. tr = gi.traceline(spot1, spot2, self, CONTENTS_SOLID | CONTENTS_PLAYER | CONTENTS_MONSTER | CONTENTS_SLIME | CONTENTS_LAVA | CONTENTS_WINDOW);
  695. // do we have a clear shot?
  696. if (tr.ent != self->enemy && !(tr.ent->svflags & SVF_PLAYER))
  697. {
  698. // PGM - we want them to go ahead and shoot at info_notnulls if they can.
  699. if (self->enemy->solid != SOLID_NOT || tr.fraction < 1.0f) // PGM
  700. {
  701. // PMM - if we can't see our target, and we're not blocked by a monster, go into blind fire if available
  702. if ((!(tr.ent->svflags & SVF_MONSTER)) && (!visible(self, self->enemy)))
  703. {
  704. if ((self->monsterinfo.blindfire) && (self->monsterinfo.blind_fire_delay <= 10_sec))
  705. {
  706. if (level.time < self->monsterinfo.attack_finished)
  707. {
  708. return false;
  709. }
  710. if (level.time < (self->monsterinfo.trail_time + self->monsterinfo.blind_fire_delay))
  711. {
  712. // wait for our time
  713. return false;
  714. }
  715. else
  716. {
  717. // make sure we're not going to shoot something we don't want to shoot
  718. tr = gi.traceline(spot1, self->monsterinfo.blind_fire_target, self, CONTENTS_MONSTER | CONTENTS_PLAYER);
  719. if (tr.allsolid || tr.startsolid || ((tr.fraction < 1.0f) && (tr.ent != self->enemy && !(tr.ent->svflags & SVF_PLAYER))))
  720. {
  721. return false;
  722. }
  723. self->monsterinfo.attack_state = AS_BLIND;
  724. self->monsterinfo.attack_finished = level.time + random_time(500_ms, 2.5_sec);
  725. return true;
  726. }
  727. }
  728. }
  729. // pmm
  730. return false;
  731. }
  732. }
  733. }
  734. if (level.time < self->monsterinfo.attack_finished)
  735. return false;
  736. gtime_t nexttime;
  737. if (self->spawnflags.has(SPAWNFLAG_TURRET_ROCKET))
  738. {
  739. chance = 0.10f;
  740. nexttime = (1.8_sec - (0.2_sec * skill->integer));
  741. }
  742. else if (self->spawnflags.has(SPAWNFLAG_TURRET_BLASTER))
  743. {
  744. chance = 0.35f;
  745. nexttime = (1.2_sec - (0.2_sec * skill->integer));
  746. }
  747. else
  748. {
  749. chance = 0.50f;
  750. nexttime = (0.8_sec - (0.1_sec * skill->integer));
  751. }
  752. if (skill->integer == 0)
  753. chance *= 0.5f;
  754. else if (skill->integer > 1)
  755. chance *= 2;
  756. // PGM - go ahead and shoot every time if it's a info_notnull
  757. // PMM - added visibility check
  758. if (((frandom() < chance) && (visible(self, self->enemy))) || (self->enemy->solid == SOLID_NOT))
  759. {
  760. self->monsterinfo.attack_state = AS_MISSILE;
  761. self->monsterinfo.attack_finished = level.time + nexttime;
  762. return true;
  763. }
  764. self->monsterinfo.attack_state = AS_STRAIGHT;
  765. return false;
  766. }
  767. // **********************
  768. // SPAWN
  769. // **********************
  770. /*QUAKED monster_turret (1 .5 0) (-16 -16 -16) (16 16 16) Ambush Trigger_Spawn Sight Blaster MachineGun Rocket Heatbeam WallUnit
  771. The automated defense turret that mounts on walls.
  772. Check the weapon you want it to use: blaster, machinegun, rocket, heatbeam.
  773. Default weapon is blaster.
  774. When activated, wall units move 32 units in the direction they're facing.
  775. */
  776. void SP_monster_turret(edict_t *self)
  777. {
  778. int angle;
  779. if ( !M_AllowSpawn( self ) ) {
  780. G_FreeEdict( self );
  781. return;
  782. }
  783. // pre-caches
  784. sound_moved.assign("turret/moved.wav");
  785. sound_moving.assign("turret/moving.wav");
  786. gi.modelindex("models/objects/debris1/tris.md2");
  787. self->s.modelindex = gi.modelindex("models/monsters/turret/tris.md2");
  788. self->mins = { -12, -12, -12 };
  789. self->maxs = { 12, 12, 12 };
  790. self->movetype = MOVETYPE_NONE;
  791. self->solid = SOLID_BBOX;
  792. self->health = 50 * st.health_multiplier;
  793. self->gib_health = -100;
  794. self->mass = 250;
  795. self->yaw_speed = 10 * skill->integer;
  796. self->monsterinfo.armor_type = IT_ARMOR_COMBAT;
  797. self->monsterinfo.armor_power = 50;
  798. self->flags |= FL_MECHANICAL;
  799. self->pain = turret_pain;
  800. self->die = turret_die;
  801. // map designer didn't specify weapon type. set it now.
  802. if (!self->spawnflags.has(SPAWNFLAG_TURRET_WEAPONCHOICE))
  803. self->spawnflags |= SPAWNFLAG_TURRET_BLASTER;
  804. if (self->spawnflags.has(SPAWNFLAG_TURRET_HEATBEAM))
  805. {
  806. self->spawnflags &= ~SPAWNFLAG_TURRET_HEATBEAM;
  807. self->spawnflags |= SPAWNFLAG_TURRET_BLASTER;
  808. }
  809. if (!self->spawnflags.has(SPAWNFLAG_TURRET_WALL_UNIT))
  810. {
  811. self->monsterinfo.stand = turret_stand;
  812. self->monsterinfo.walk = turret_walk;
  813. self->monsterinfo.run = turret_run;
  814. self->monsterinfo.dodge = nullptr;
  815. self->monsterinfo.attack = turret_attack;
  816. self->monsterinfo.melee = nullptr;
  817. self->monsterinfo.sight = turret_sight;
  818. self->monsterinfo.search = turret_search;
  819. M_SetAnimation(self, &turret_move_stand);
  820. }
  821. // PMM
  822. self->monsterinfo.checkattack = turret_checkattack;
  823. self->monsterinfo.aiflags |= AI_MANUAL_STEERING;
  824. self->monsterinfo.scale = MODEL_SCALE;
  825. self->gravity = 0;
  826. self->offset = self->s.angles;
  827. angle = (int) self->s.angles[1];
  828. switch (angle)
  829. {
  830. case -1: // up
  831. self->s.angles[0] = 270;
  832. self->s.angles[1] = 0;
  833. self->s.origin[2] += 2;
  834. break;
  835. case -2: // down
  836. self->s.angles[0] = 90;
  837. self->s.angles[1] = 0;
  838. self->s.origin[2] -= 2;
  839. break;
  840. case 0:
  841. self->s.origin[0] += 2;
  842. break;
  843. case 90:
  844. self->s.origin[1] += 2;
  845. break;
  846. case 180:
  847. self->s.origin[0] -= 2;
  848. break;
  849. case 270:
  850. self->s.origin[1] -= 2;
  851. break;
  852. default:
  853. break;
  854. }
  855. gi.linkentity(self);
  856. if (self->spawnflags.has(SPAWNFLAG_TURRET_WALL_UNIT))
  857. {
  858. if (!self->targetname)
  859. {
  860. G_FreeEdict(self);
  861. return;
  862. }
  863. self->takedamage = false;
  864. self->use = turret_activate;
  865. turret_wall_spawn(self);
  866. if (!(self->monsterinfo.aiflags & AI_DO_NOT_COUNT))
  867. {
  868. if (g_debug_monster_kills->integer)
  869. level.monsters_registered[level.total_monsters] = self;
  870. level.total_monsters++;
  871. }
  872. }
  873. else
  874. {
  875. stationarymonster_start(self);
  876. }
  877. if (self->spawnflags.has(SPAWNFLAG_TURRET_MACHINEGUN))
  878. {
  879. gi.soundindex("infantry/infatck1.wav");
  880. gi.soundindex("weapons/chngnu1a.wav");
  881. self->s.skinnum = 1;
  882. self->spawnflags &= ~SPAWNFLAG_TURRET_WEAPONCHOICE;
  883. self->spawnflags |= SPAWNFLAG_TURRET_MACHINEGUN;
  884. }
  885. else if (self->spawnflags.has(SPAWNFLAG_TURRET_ROCKET))
  886. {
  887. gi.soundindex("weapons/rockfly.wav");
  888. gi.modelindex("models/objects/rocket/tris.md2");
  889. gi.soundindex("chick/chkatck2.wav");
  890. self->s.skinnum = 2;
  891. self->spawnflags &= ~SPAWNFLAG_TURRET_WEAPONCHOICE;
  892. self->spawnflags |= SPAWNFLAG_TURRET_ROCKET;
  893. }
  894. else
  895. {
  896. gi.modelindex("models/objects/laser/tris.md2");
  897. gi.soundindex("misc/lasfly.wav");
  898. gi.soundindex("soldier/solatck2.wav");
  899. self->spawnflags &= ~SPAWNFLAG_TURRET_WEAPONCHOICE;
  900. self->spawnflags |= SPAWNFLAG_TURRET_BLASTER;
  901. }
  902. // PMM - turrets don't get mad at monsters, and visa versa
  903. self->monsterinfo.aiflags |= AI_IGNORE_SHOTS;
  904. // PMM - blindfire
  905. if (self->spawnflags.has(SPAWNFLAG_TURRET_ROCKET | SPAWNFLAG_TURRET_BLASTER))
  906. self->monsterinfo.blindfire = true;
  907. }