ai.qc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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() movetarget_f;
  16. void() t_movetarget;
  17. void() knight_walk1;
  18. void() knight_bow6;
  19. void() knight_bow1;
  20. void(entity etemp, entity stemp, entity stemp, float dmg) T_Damage;
  21. /*
  22. .enemy
  23. Will be world if not currently angry at anyone.
  24. .movetarget
  25. The next path spot to walk toward. If .enemy, ignore .movetarget.
  26. When an enemy is killed, the monster will try to return to it's path.
  27. .huntt_ime
  28. Set to time + something when the player is in sight, but movement straight for
  29. him is blocked. This causes the monster to use wall following code for
  30. movement direction instead of sighting on the player.
  31. .ideal_yaw
  32. A yaw angle of the intended direction, which will be turned towards at up
  33. to 45 deg / state. If the enemy is in view and hunt_time is not active,
  34. this will be the exact line towards the enemy.
  35. .pausetime
  36. A monster will leave it's stand state and head towards it's .movetarget when
  37. time > .pausetime.
  38. walkmove(angle, speed) primitive is all or nothing
  39. */
  40. //
  41. // globals
  42. //
  43. float current_yaw;
  44. //
  45. // when a monster becomes angry at a player, that monster will be used
  46. // as the sight target the next frame so that monsters near that one
  47. // will wake up even if they wouldn't have noticed the player
  48. //
  49. entity sight_entity;
  50. float sight_entity_time;
  51. float(float v) anglemod =
  52. {
  53. while (v >= 360)
  54. v = v - 360;
  55. while (v < 0)
  56. v = v + 360;
  57. return v;
  58. };
  59. /*
  60. ==============================================================================
  61. MOVETARGET CODE
  62. The angle of the movetarget effects standing and bowing direction, but has no effect on movement, which allways heads to the next target.
  63. targetname
  64. must be present. The name of this movetarget.
  65. target
  66. the next spot to move to. If not present, stop here for good.
  67. pausetime
  68. The number of seconds to spend standing or bowing for path_stand or path_bow
  69. ==============================================================================
  70. */
  71. void() movetarget_f =
  72. {
  73. if (!self.targetname)
  74. objerror ("monster_movetarget: no targetname");
  75. self.solid = SOLID_TRIGGER;
  76. self.touch = t_movetarget;
  77. setsize (self, '-8 -8 -8', '8 8 8');
  78. };
  79. /*QUAKED path_corner (0.5 0.3 0) (-8 -8 -8) (8 8 8)
  80. Monsters will continue walking towards the next target corner.
  81. */
  82. void() path_corner =
  83. {
  84. movetarget_f ();
  85. };
  86. /*
  87. =============
  88. t_movetarget
  89. Something has bumped into a movetarget. If it is a monster
  90. moving towards it, change the next destination and continue.
  91. ==============
  92. */
  93. void() t_movetarget =
  94. {
  95. local entity temp;
  96. if (other.movetarget != self)
  97. return;
  98. if (other.enemy)
  99. return; // fighting, not following a path
  100. temp = self;
  101. self = other;
  102. other = temp;
  103. if (self.classname == "monster_ogre")
  104. sound (self, CHAN_VOICE, "ogre/ogdrag.wav", 1, ATTN_IDLE);// play chainsaw drag sound
  105. //dprint ("t_movetarget\n");
  106. self.goalentity = self.movetarget = find (world, targetname, other.target);
  107. self.ideal_yaw = vectoyaw(self.goalentity.origin - self.origin);
  108. if (!self.movetarget)
  109. {
  110. self.pausetime = time + 999999;
  111. self.th_stand ();
  112. return;
  113. }
  114. };
  115. //============================================================================
  116. /*
  117. =============
  118. range
  119. returns the range catagorization of an entity reletive to self
  120. 0 melee range, will become hostile even if back is turned
  121. 1 visibility and infront, or visibility and show hostile
  122. 2 infront and show hostile
  123. 3 only triggered by damage
  124. =============
  125. */
  126. float(entity targ) range =
  127. {
  128. local vector spot1, spot2;
  129. local float r;
  130. spot1 = self.origin + self.view_ofs;
  131. spot2 = targ.origin + targ.view_ofs;
  132. r = vlen (spot1 - spot2);
  133. if (r < 120)
  134. return RANGE_MELEE;
  135. if (r < 500)
  136. return RANGE_NEAR;
  137. if (r < 1000)
  138. return RANGE_MID;
  139. return RANGE_FAR;
  140. };
  141. /*
  142. =============
  143. visible
  144. returns 1 if the entity is visible to self, even if not infront ()
  145. =============
  146. */
  147. float (entity targ) visible =
  148. {
  149. local vector spot1, spot2;
  150. spot1 = self.origin + self.view_ofs;
  151. spot2 = targ.origin + targ.view_ofs;
  152. traceline (spot1, spot2, TRUE, self); // see through other monsters
  153. if (trace_inopen && trace_inwater)
  154. return FALSE; // sight line crossed contents
  155. if (trace_fraction == 1)
  156. return TRUE;
  157. return FALSE;
  158. };
  159. /*
  160. =============
  161. infront
  162. returns 1 if the entity is in front (in sight) of self
  163. =============
  164. */
  165. float(entity targ) infront =
  166. {
  167. local vector vec;
  168. local float dot;
  169. makevectors (self.angles);
  170. vec = normalize (targ.origin - self.origin);
  171. dot = vec * v_forward;
  172. if ( dot > 0.3)
  173. {
  174. return TRUE;
  175. }
  176. return FALSE;
  177. };
  178. //============================================================================
  179. /*
  180. ===========
  181. ChangeYaw
  182. Turns towards self.ideal_yaw at self.yaw_speed
  183. Sets the global variable current_yaw
  184. Called every 0.1 sec by monsters
  185. ============
  186. */
  187. /*
  188. void() ChangeYaw =
  189. {
  190. local float ideal, move;
  191. //current_yaw = self.ideal_yaw;
  192. // mod down the current angle
  193. current_yaw = anglemod( self.angles_y );
  194. ideal = self.ideal_yaw;
  195. if (current_yaw == ideal)
  196. return;
  197. move = ideal - current_yaw;
  198. if (ideal > current_yaw)
  199. {
  200. if (move > 180)
  201. move = move - 360;
  202. }
  203. else
  204. {
  205. if (move < -180)
  206. move = move + 360;
  207. }
  208. if (move > 0)
  209. {
  210. if (move > self.yaw_speed)
  211. move = self.yaw_speed;
  212. }
  213. else
  214. {
  215. if (move < 0-self.yaw_speed )
  216. move = 0-self.yaw_speed;
  217. }
  218. current_yaw = anglemod (current_yaw + move);
  219. self.angles_y = current_yaw;
  220. };
  221. */
  222. //============================================================================
  223. void() HuntTarget =
  224. {
  225. self.goalentity = self.enemy;
  226. self.think = self.th_run;
  227. self.ideal_yaw = vectoyaw(self.enemy.origin - self.origin);
  228. self.nextthink = time + 0.1;
  229. SUB_AttackFinished (1); // wait a while before first attack
  230. };
  231. void() SightSound =
  232. {
  233. local float rsnd;
  234. if (self.classname == "monster_ogre")
  235. sound (self, CHAN_VOICE, "ogre/ogwake.wav", 1, ATTN_NORM);
  236. else if (self.classname == "monster_knight")
  237. {
  238. if (self.skin == 0)
  239. sound (self, CHAN_VOICE, "knight/ksight.wav", 1, ATTN_NORM);
  240. }
  241. else if (self.classname == "monster_shambler")
  242. sound (self, CHAN_VOICE, "shambler/ssight.wav", 1, ATTN_NORM);
  243. else if (self.classname == "monster_demon1")
  244. sound (self, CHAN_VOICE, "demon/sight2.wav", 1, ATTN_NORM);
  245. else if (self.classname == "monster_wizard")
  246. sound (self, CHAN_VOICE, "wizard/wsight.wav", 1, ATTN_NORM);
  247. else if (self.classname == "monster_zombie")
  248. sound (self, CHAN_VOICE, "zombie/z_idle.wav", 1, ATTN_NORM);
  249. else if (self.classname == "monster_dog")
  250. sound (self, CHAN_VOICE, "dog/dsight.wav", 1, ATTN_NORM);
  251. else if (self.classname == "monster_hell_knight")
  252. {
  253. if (self.skin == 0)
  254. sound (self, CHAN_VOICE, "hknight/sight1.wav", 1, ATTN_NORM);
  255. }
  256. else if (self.classname == "monster_tarbaby")
  257. sound (self, CHAN_VOICE, "blob/sight1.wav", 1, ATTN_NORM);
  258. else if (self.classname == "monster_vomit")
  259. sound (self, CHAN_VOICE, "vomitus/v_sight1.wav", 1, ATTN_NORM);
  260. else if (self.classname == "monster_eel")
  261. sound (self, CHAN_VOICE, "eel/eelc5.wav", 1, ATTN_NORM);
  262. else if (self.classname == "monster_wrath")
  263. sound (self, CHAN_VOICE, "wrath/wsee.wav", 1, ATTN_NORM);
  264. else if (self.classname == "monster_enforcer")
  265. {
  266. rsnd = rint(random() * 3);
  267. if (rsnd == 1)
  268. sound (self, CHAN_VOICE, "enforcer/sight1.wav", 1, ATTN_NORM);
  269. else if (rsnd == 2)
  270. sound (self, CHAN_VOICE, "enforcer/sight2.wav", 1, ATTN_NORM);
  271. else if (rsnd == 0)
  272. sound (self, CHAN_VOICE, "enforcer/sight3.wav", 1, ATTN_NORM);
  273. else
  274. sound (self, CHAN_VOICE, "enforcer/sight4.wav", 1, ATTN_NORM);
  275. }
  276. else if (self.classname == "monster_army")
  277. sound (self, CHAN_VOICE, "soldier/sight1.wav", 1, ATTN_NORM);
  278. else if (self.classname == "monster_shalrath")
  279. sound (self, CHAN_VOICE, "shalrath/sight.wav", 1, ATTN_NORM);
  280. else if (self.classname == "monster_dragon")
  281. sound (self, CHAN_VOICE, "dragon/see.wav", 1, ATTN_NORM);
  282. };
  283. void() FoundTarget =
  284. {
  285. if (self.enemy.classname == "player")
  286. { // let other monsters see this monster for a while
  287. sight_entity = self;
  288. sight_entity_time = time;
  289. }
  290. self.show_hostile = time + 1; // wake up other monsters
  291. SightSound ();
  292. HuntTarget ();
  293. };
  294. /*
  295. ===========
  296. FindTarget
  297. Self is currently not attacking anything, so try to find a target
  298. Returns TRUE if an enemy was sighted
  299. When a player fires a missile, the point of impact becomes a fakeplayer so
  300. that monsters that see the impact will respond as if they had seen the
  301. player.
  302. To avoid spending too much time, only a single client (or fakeclient) is
  303. checked each frame. This means multi player games will have slightly
  304. slower noticing monsters.
  305. ============
  306. */
  307. float() FindTarget =
  308. {
  309. local entity client;
  310. local float r;
  311. // if the first spawnflag bit is set, the monster will only wake up on
  312. // really seeing the player, not another monster getting angry
  313. // spawnflags & 3 is a big hack, because zombie crucified used the first
  314. // spawn flag prior to the ambush flag, and I forgot about it, so the second
  315. // spawn flag works as well
  316. if (sight_entity_time >= time - 0.1 && !(self.spawnflags & 3) )
  317. {
  318. client = sight_entity;
  319. if (client.enemy == self.enemy)
  320. return;
  321. }
  322. else
  323. {
  324. client = checkclient ();
  325. if (!client)
  326. return FALSE; // current check entity isn't in PVS
  327. }
  328. if (client == self.enemy)
  329. return FALSE;
  330. if (client.flags & FL_NOTARGET)
  331. return FALSE;
  332. if (client.items & IT_INVISIBILITY)
  333. return FALSE;
  334. r = range (client);
  335. if (r == RANGE_FAR)
  336. return FALSE;
  337. if (!visible (client))
  338. return FALSE;
  339. if (r == RANGE_NEAR)
  340. {
  341. if (client.show_hostile < time && !infront (client))
  342. return FALSE;
  343. }
  344. else if (r == RANGE_MID)
  345. {
  346. if ( /* client.show_hostile < time || */ !infront (client))
  347. return FALSE;
  348. }
  349. //
  350. // got one
  351. //
  352. self.enemy = client;
  353. if (self.enemy.classname != "player")
  354. {
  355. self.enemy = self.enemy.enemy;
  356. if (self.enemy.classname != "player")
  357. {
  358. self.enemy = world;
  359. return FALSE;
  360. }
  361. }
  362. // PGM hack
  363. if (self.classname != "dragon")
  364. FoundTarget ();
  365. return TRUE;
  366. };
  367. //=============================================================================
  368. void(float dist) ai_forward =
  369. {
  370. walkmove (self.angles_y, dist);
  371. };
  372. void(float dist) ai_back =
  373. {
  374. walkmove ( (self.angles_y+180), dist);
  375. };
  376. /*
  377. =============
  378. ai_pain
  379. stagger back a bit
  380. =============
  381. */
  382. void(float dist) ai_pain =
  383. {
  384. ai_back (dist);
  385. /*
  386. local float away;
  387. away = anglemod (vectoyaw (self.origin - self.enemy.origin)
  388. + 180*(random()- 0.5) );
  389. walkmove (away, dist);
  390. */
  391. };
  392. /*
  393. =============
  394. ai_painforward
  395. stagger back a bit
  396. =============
  397. */
  398. void(float dist) ai_painforward =
  399. {
  400. walkmove (self.ideal_yaw, dist);
  401. };
  402. /*
  403. =============
  404. ai_walk
  405. The monster is walking it's beat
  406. =============
  407. */
  408. void(float dist) ai_walk =
  409. {
  410. local vector mtemp;
  411. movedist = dist;
  412. /* Dragon movement handled in dragon.qc.... this is legacy.
  413. if (self.classname == "monster_dragon")
  414. {
  415. movetogoal (dist);
  416. return;
  417. }
  418. */
  419. // check for noticing a player
  420. if (FindTarget ())
  421. return;
  422. movetogoal (dist);
  423. };
  424. /*
  425. =============
  426. ai_stand
  427. The monster is staying in one place for a while, with slight angle turns
  428. =============
  429. */
  430. void() ai_stand =
  431. {
  432. if (FindTarget ())
  433. return;
  434. if (time > self.pausetime)
  435. {
  436. self.th_walk ();
  437. return;
  438. }
  439. // change angle slightly
  440. };
  441. /*
  442. =============
  443. ai_turn
  444. don't move, but turn towards ideal_yaw
  445. =============
  446. */
  447. void() ai_turn =
  448. {
  449. if (FindTarget ())
  450. return;
  451. ChangeYaw ();
  452. };
  453. //=============================================================================
  454. /*
  455. =============
  456. ChooseTurn
  457. =============
  458. */
  459. void(vector dest3) ChooseTurn =
  460. {
  461. local vector dir, newdir;
  462. dir = self.origin - dest3;
  463. newdir_x = trace_plane_normal_y;
  464. newdir_y = 0 - trace_plane_normal_x;
  465. newdir_z = 0;
  466. if (dir * newdir > 0)
  467. {
  468. dir_x = 0 - trace_plane_normal_y;
  469. dir_y = trace_plane_normal_x;
  470. }
  471. else
  472. {
  473. dir_x = trace_plane_normal_y;
  474. dir_y = 0 - trace_plane_normal_x;
  475. }
  476. dir_z = 0;
  477. self.ideal_yaw = vectoyaw(dir);
  478. };
  479. /*
  480. ============
  481. FacingIdeal
  482. ============
  483. */
  484. float() FacingIdeal =
  485. {
  486. local float delta;
  487. delta = anglemod(self.angles_y - self.ideal_yaw);
  488. if (delta > 45 && delta < 315)
  489. return FALSE;
  490. return TRUE;
  491. };
  492. //=============================================================================
  493. float() WizardCheckAttack;
  494. float() DogCheckAttack;
  495. float() CheckAnyAttack =
  496. {
  497. if (!enemy_vis)
  498. return;
  499. if (self.classname == "monster_army")
  500. return SoldierCheckAttack ();
  501. if (self.classname == "monster_ogre")
  502. return OgreCheckAttack ();
  503. if (self.classname == "monster_shambler")
  504. return ShamCheckAttack ();
  505. if (self.classname == "monster_demon1")
  506. return DemonCheckAttack ();
  507. if (self.classname == "monster_dog")
  508. return DogCheckAttack ();
  509. if (self.classname == "monster_wizard")
  510. return WizardCheckAttack ();
  511. return CheckAttack ();
  512. };
  513. /*
  514. =============
  515. ai_run_melee
  516. Turn and close until within an angle to launch a melee attack
  517. =============
  518. */
  519. void() ai_run_melee =
  520. {
  521. self.ideal_yaw = enemy_yaw;
  522. ChangeYaw ();
  523. if (FacingIdeal())
  524. {
  525. self.th_melee ();
  526. self.attack_state = AS_STRAIGHT;
  527. }
  528. };
  529. /*
  530. =============
  531. ai_run_missile
  532. Turn in place until within an angle to launch a missile attack
  533. =============
  534. */
  535. void() ai_run_missile =
  536. {
  537. self.ideal_yaw = enemy_yaw;
  538. ChangeYaw ();
  539. if (FacingIdeal())
  540. {
  541. self.th_missile ();
  542. self.attack_state = AS_STRAIGHT;
  543. }
  544. };
  545. /*
  546. =============
  547. ai_run_slide
  548. Strafe sideways, but stay at aproximately the same range
  549. =============
  550. */
  551. void() ai_run_slide =
  552. {
  553. local float ofs;
  554. self.ideal_yaw = enemy_yaw;
  555. ChangeYaw ();
  556. if (self.lefty)
  557. ofs = 90;
  558. else
  559. ofs = -90;
  560. if (walkmove (self.ideal_yaw + ofs, movedist))
  561. return;
  562. self.lefty = 1 - self.lefty;
  563. walkmove (self.ideal_yaw - ofs, movedist);
  564. };
  565. /*
  566. =============
  567. ai_run
  568. The monster has an enemy it is trying to kill
  569. =============
  570. */
  571. void(float dist) ai_run =
  572. {
  573. local vector delta;
  574. local float axis;
  575. local float direct, ang_rint, ang_floor, ang_ceil;
  576. movedist = dist;
  577. // see if the enemy is dead
  578. if (self.enemy.health <= 0)
  579. {
  580. self.enemy = world;
  581. // FIXME: look all around for other targets
  582. if (self.oldenemy.health > 0)
  583. {
  584. self.enemy = self.oldenemy;
  585. HuntTarget ();
  586. }
  587. else
  588. {
  589. if (self.movetarget)
  590. self.th_walk ();
  591. else
  592. self.th_stand ();
  593. return;
  594. }
  595. }
  596. self.show_hostile = time + 1; // wake up other monsters
  597. // check knowledge of enemy
  598. enemy_vis = visible(self.enemy);
  599. if (enemy_vis)
  600. self.search_time = time + 5;
  601. // look for other coop players
  602. if (coop && self.search_time < time)
  603. {
  604. if (FindTarget ())
  605. return;
  606. }
  607. enemy_infront = infront(self.enemy);
  608. enemy_range = range(self.enemy);
  609. enemy_yaw = vectoyaw(self.enemy.origin - self.origin);
  610. if (self.attack_state == AS_MISSILE)
  611. {
  612. //dprint ("ai_run_missile\n");
  613. ai_run_missile ();
  614. return;
  615. }
  616. if (self.attack_state == AS_MELEE)
  617. {
  618. //dprint ("ai_run_melee\n");
  619. ai_run_melee ();
  620. return;
  621. }
  622. if (CheckAnyAttack ())
  623. return; // beginning an attack
  624. if (self.attack_state == AS_SLIDING)
  625. {
  626. ai_run_slide ();
  627. return;
  628. }
  629. // head straight in
  630. movetogoal (dist); // done in C code...
  631. };