ai.qc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. .hunt_time
  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. //
  44. // when a monster becomes angry at a player, that monster will be used
  45. // as the sight target the next frame so that monsters near that one
  46. // will wake up even if they wouldn't have noticed the player
  47. //
  48. entity sight_entity;
  49. float sight_entity_time;
  50. void makevectorsfixed(vector ang) {
  51. ang_x *= -1;
  52. makevectors(ang);
  53. };
  54. float(float v) anglemod =
  55. {
  56. while (v >= 360)
  57. v = v - 360;
  58. while (v < 0)
  59. v = v + 360;
  60. return v;
  61. };
  62. /*
  63. ==============================================================================
  64. MOVETARGET CODE
  65. The angle of the movetarget effects standing and bowing direction, but has no effect on movement, which allways heads to the next target.
  66. targetname
  67. must be present. The name of this movetarget.
  68. target
  69. the next spot to move to. If not present, stop here for good.
  70. pausetime
  71. The number of seconds to spend standing or bowing for path_stand or path_bow
  72. ==============================================================================
  73. */
  74. void() movetarget_f =
  75. {
  76. if (!self.targetname)
  77. objerror ("monster_movetarget: no targetname");
  78. self.solid = SOLID_TRIGGER;
  79. self.touch = t_movetarget;
  80. setsize (self, '-8 -8 -8', '8 8 8');
  81. };
  82. /*QUAKED path_corner (0.5 0.3 0) (-8 -8 -8) (8 8 8)
  83. Monsters will continue walking towards the next target corner.
  84. */
  85. void() path_corner =
  86. {
  87. movetarget_f ();
  88. };
  89. /*
  90. =============
  91. t_movetarget
  92. Something has bumped into a movetarget. If it is a monster
  93. moving towards it, change the next destination and continue.
  94. ==============
  95. */
  96. void() t_movetarget =
  97. {
  98. local entity temp;
  99. if (other.movetarget != self)
  100. return;
  101. if (other.enemy)
  102. return; // fighting, not following a path
  103. temp = self;
  104. self = other;
  105. other = temp;
  106. if (self.classname == "monster_ogre")
  107. sound (self, CHAN_VOICE, "ogre/ogdrag.wav", 1, ATTN_IDLE);// play chainsaw drag sound
  108. //dprint ("t_movetarget\n");
  109. self.goalentity = self.movetarget = find (world, targetname, other.target);
  110. self.ideal_yaw = vectoyaw(self.goalentity.origin - self.origin);
  111. if (!self.movetarget)
  112. {
  113. self.pausetime = time + 999999;
  114. self.th_stand ();
  115. return;
  116. }
  117. };
  118. //============================================================================
  119. /*
  120. =============
  121. range
  122. returns the range catagorization of an entity reletive to self
  123. 0 melee range, will become hostile even if back is turned
  124. 1 visibility and infront, or visibility and show hostile
  125. 2 infront and show hostile
  126. 3 only triggered by damage
  127. =============
  128. */
  129. float(entity targ) range =
  130. {
  131. local vector spot1, spot2;
  132. local float r;
  133. spot1 = self.origin + self.view_ofs;
  134. spot2 = targ.origin + targ.view_ofs;
  135. r = vlen (spot1 - spot2);
  136. if (r < 120)
  137. return RANGE_MELEE;
  138. if (r < 500)
  139. return RANGE_NEAR;
  140. if (r < 1000)
  141. return RANGE_MID;
  142. return RANGE_FAR;
  143. };
  144. /*
  145. =============
  146. visible
  147. returns 1 if the entity is visible to self, even if not infront ()
  148. =============
  149. */
  150. float (entity targ) visible =
  151. {
  152. local vector spot1, spot2;
  153. spot1 = self.origin + self.view_ofs;
  154. spot2 = targ.origin + targ.view_ofs;
  155. traceline (spot1, spot2, TRUE, self); // see through other monsters
  156. if (trace_inopen && trace_inwater)
  157. return FALSE; // sight line crossed contents
  158. if (trace_fraction == 1)
  159. return TRUE;
  160. return FALSE;
  161. };
  162. /*
  163. =============
  164. infront
  165. returns 1 if the entity is in front (in sight) of self
  166. =============
  167. */
  168. float(entity targ) infront =
  169. {
  170. local vector vec;
  171. local float dot;
  172. makevectorsfixed(self.angles);
  173. vec = normalize (targ.origin - self.origin);
  174. dot = vec * v_forward;
  175. if ( dot > 0.3)
  176. {
  177. return TRUE;
  178. }
  179. return FALSE;
  180. };
  181. //============================================================================
  182. void() HuntTarget =
  183. {
  184. self.goalentity = self.enemy;
  185. self.think = self.th_run;
  186. self.ideal_yaw = vectoyaw(self.enemy.origin - self.origin);
  187. self.nextthink = time + 0.1;
  188. SUB_AttackFinished (1); // wait a while before first attack
  189. };
  190. void SightSound() {
  191. if ( self.classname == "enforcer" ) {
  192. local float rsnd;
  193. rsnd = rint( random() * 3 );
  194. if (rsnd == 1)
  195. sound (self, CHAN_VOICE, self.noise, 1, ATTN_NORM);
  196. else if (rsnd == 2)
  197. sound (self, CHAN_VOICE, self.noise1, 1, ATTN_NORM);
  198. else if (rsnd == 0)
  199. sound (self, CHAN_VOICE, self.noise2, 1, ATTN_NORM);
  200. else
  201. sound (self, CHAN_VOICE, self.noise4, 1, ATTN_NORM);
  202. } else {
  203. sound (self, CHAN_VOICE, self.noise, 1, ATTN_NORM);
  204. }
  205. };
  206. void() FoundTarget =
  207. {
  208. if (self.enemy.classname == "player")
  209. { // let other monsters see this monster for a while
  210. sight_entity = self;
  211. sight_entity_time = time;
  212. }
  213. self.show_hostile = time + 1; // wake up other monsters
  214. SightSound ();
  215. HuntTarget ();
  216. };
  217. /*
  218. ===========
  219. FindTarget
  220. Self is currently not attacking anything, so try to find a target
  221. Returns TRUE if an enemy was sighted
  222. When a player fires a missile, the point of impact becomes a fakeplayer so
  223. that monsters that see the impact will respond as if they had seen the
  224. player.
  225. To avoid spending too much time, only a single client (or fakeclient) is
  226. checked each frame. This means multi player games will have slightly
  227. slower noticing monsters.
  228. ============
  229. */
  230. float() FindTarget =
  231. {
  232. local entity client;
  233. local float r;
  234. // if the first spawnflag bit is set, the monster will only wake up on
  235. // really seeing the player, not another monster getting angry
  236. // spawnflags & 3 is a big hack, because zombie crucified used the first
  237. // spawn flag prior to the ambush flag, and I forgot about it, so the second
  238. // spawn flag works as well
  239. if (sight_entity_time >= time - 0.1 && !(self.spawnflags & 3) )
  240. {
  241. client = sight_entity;
  242. if (client.enemy == self.enemy)
  243. return TRUE;
  244. }
  245. else
  246. {
  247. client = checkclient ();
  248. if (!client)
  249. return FALSE; // current check entity isn't in PVS
  250. }
  251. if (client == self.enemy)
  252. return FALSE;
  253. if (client.flags & FL_NOTARGET)
  254. return FALSE;
  255. if (client.items & IT_INVISIBILITY)
  256. return FALSE;
  257. r = range (client);
  258. if (r == RANGE_FAR)
  259. return FALSE;
  260. if (!visible (client))
  261. return FALSE;
  262. if (r == RANGE_NEAR)
  263. {
  264. if (client.show_hostile < time && !infront (client))
  265. return FALSE;
  266. }
  267. else if (r == RANGE_MID)
  268. {
  269. if ( !infront (client))
  270. return FALSE;
  271. }
  272. //
  273. // got one
  274. //
  275. self.enemy = client;
  276. if (self.enemy.classname != "player")
  277. {
  278. self.enemy = self.enemy.enemy;
  279. if (self.enemy.classname != "player")
  280. {
  281. self.enemy = world;
  282. return FALSE;
  283. }
  284. }
  285. FoundTarget ();
  286. return TRUE;
  287. };
  288. //=============================================================================
  289. void(float dist) ai_forward =
  290. {
  291. walkmove (self.angles_y, dist);
  292. };
  293. void(float dist) ai_back =
  294. {
  295. walkmove ( (self.angles_y+180), dist);
  296. };
  297. /*
  298. =============
  299. ai_pain
  300. stagger back a bit
  301. =============
  302. */
  303. void(float dist) ai_pain =
  304. {
  305. ai_back (dist);
  306. };
  307. /*
  308. =============
  309. ai_painforward
  310. stagger back a bit
  311. =============
  312. */
  313. void(float dist) ai_painforward =
  314. {
  315. walkmove (self.ideal_yaw, dist);
  316. };
  317. /*
  318. =============
  319. ai_walk
  320. The monster is walking it's beat
  321. =============
  322. */
  323. void(float dist) ai_walk =
  324. {
  325. movedist = dist;
  326. // check for noticing a player
  327. if (FindTarget ())
  328. return;
  329. movetogoal (dist);
  330. };
  331. /*
  332. =============
  333. ai_stand
  334. The monster is staying in one place for a while, with slight angle turns
  335. =============
  336. */
  337. void() ai_stand =
  338. {
  339. if (FindTarget ())
  340. return;
  341. if (time > self.pausetime)
  342. {
  343. self.th_walk ();
  344. return;
  345. }
  346. };
  347. /*
  348. =============
  349. ai_turn
  350. don't move, but turn towards ideal_yaw
  351. =============
  352. */
  353. void() ai_turn =
  354. {
  355. if (FindTarget ())
  356. return;
  357. ChangeYaw ();
  358. };
  359. //=============================================================================
  360. /*
  361. =============
  362. ChooseTurn
  363. =============
  364. */
  365. void(vector dest3) ChooseTurn =
  366. {
  367. local vector dir, newdir;
  368. dir = self.origin - dest3;
  369. newdir_x = trace_plane_normal_y;
  370. newdir_y = 0 - trace_plane_normal_x;
  371. newdir_z = 0;
  372. if (dir * newdir > 0)
  373. {
  374. dir_x = 0 - trace_plane_normal_y;
  375. dir_y = trace_plane_normal_x;
  376. }
  377. else
  378. {
  379. dir_x = trace_plane_normal_y;
  380. dir_y = 0 - trace_plane_normal_x;
  381. }
  382. dir_z = 0;
  383. self.ideal_yaw = vectoyaw(dir);
  384. };
  385. /*
  386. ============
  387. FacingIdeal
  388. ============
  389. */
  390. float() FacingIdeal =
  391. {
  392. local float delta;
  393. delta = anglemod(self.angles_y - self.ideal_yaw);
  394. if (delta > 45 && delta < 315)
  395. return FALSE;
  396. return TRUE;
  397. };
  398. //=============================================================================
  399. float() WizardCheckAttack;
  400. float() DogCheckAttack;
  401. float() CheckAnyAttack =
  402. {
  403. if (!enemy_visible)
  404. return FALSE;
  405. if (self.classname == "monster_army")
  406. return SoldierCheckAttack ();
  407. if (self.classname == "monster_ogre")
  408. return OgreCheckAttack ();
  409. if (self.classname == "monster_shambler")
  410. return ShamCheckAttack ();
  411. if (self.classname == "monster_demon1")
  412. return DemonCheckAttack ();
  413. if (self.classname == "monster_dog")
  414. return DogCheckAttack ();
  415. if (self.classname == "monster_wizard")
  416. return WizardCheckAttack ();
  417. return CheckAttack ();
  418. };
  419. /*
  420. =============
  421. ai_run_melee
  422. Turn and close until within an angle to launch a melee attack
  423. =============
  424. */
  425. void() ai_run_melee =
  426. {
  427. self.ideal_yaw = enemy_yaw;
  428. ChangeYaw ();
  429. if (FacingIdeal())
  430. {
  431. self.th_melee ();
  432. self.attack_state = AS_STRAIGHT;
  433. }
  434. };
  435. /*
  436. =============
  437. ai_run_missile
  438. Turn in place until within an angle to launch a missile attack
  439. =============
  440. */
  441. void() ai_run_missile =
  442. {
  443. self.ideal_yaw = enemy_yaw;
  444. ChangeYaw ();
  445. if (FacingIdeal())
  446. {
  447. self.th_missile ();
  448. self.attack_state = AS_STRAIGHT;
  449. }
  450. };
  451. /*
  452. =============
  453. ai_run_slide
  454. Strafe sideways, but stay at aproximately the same range
  455. =============
  456. */
  457. void() ai_run_slide =
  458. {
  459. local float ofs;
  460. self.ideal_yaw = enemy_yaw;
  461. ChangeYaw ();
  462. if (self.lefty)
  463. ofs = 90;
  464. else
  465. ofs = -90;
  466. if (walkmove (self.ideal_yaw + ofs, movedist))
  467. return;
  468. self.lefty = 1 - self.lefty;
  469. walkmove (self.ideal_yaw - ofs, movedist);
  470. };
  471. /*
  472. =============
  473. ai_pathtogoal
  474. Advanced movement code that use the bots pathfinder if allowed and conditions are right.
  475. Feel free to add any other conditions needed.
  476. =============
  477. */
  478. void ai_pathtogoal( float dist ) {
  479. if ( self.allowPathFind == FALSE ) {
  480. movetogoal( dist ); // can't use pathfinding, so use normal Quake movement behavior.
  481. return;
  482. }
  483. if ( enemy_visible ) {
  484. if ( self.combat_style == CS_RANGED ) {
  485. // do the normal "shoot, walk, shoot" behavior...
  486. movetogoal( dist );
  487. return;
  488. } else if ( self.combat_style == CS_MELEE ) {
  489. // path pretty close to the enemy, then let normal Quake movement take over.
  490. if ( enemy_range > RANGE_NEAR ) {
  491. if ( walkpathtogoal( dist, self.enemy.origin ) == PATH_IN_PROGRESS ) {
  492. return;
  493. }
  494. }
  495. } else if ( self.combat_style == CS_MIXED ) {
  496. // most mixed combat AI have fairly short range attacks, so try to path within mid range.
  497. if ( enemy_range > RANGE_MID ) {
  498. if ( walkpathtogoal( dist, self.enemy.origin ) == PATH_IN_PROGRESS ) {
  499. return;
  500. }
  501. }
  502. }
  503. } else {
  504. // we can't see our enemy, let's see if we can path to them
  505. if ( walkpathtogoal( dist, self.enemy.origin ) == PATH_IN_PROGRESS ) {
  506. return;
  507. }
  508. }
  509. movetogoal( dist ); // fall back to normal Quake movement behavior.
  510. }
  511. /*
  512. =============
  513. ai_run
  514. The monster has an enemy it is trying to kill
  515. =============
  516. */
  517. void(float dist) ai_run =
  518. {
  519. movedist = dist;
  520. // see if the enemy is dead
  521. if (self.enemy.health <= 0)
  522. {
  523. self.enemy = world;
  524. // FIXME: look all around for other targets
  525. if (self.oldenemy.health > 0)
  526. {
  527. self.enemy = self.oldenemy;
  528. HuntTarget ();
  529. }
  530. else
  531. {
  532. if (self.movetarget)
  533. self.th_walk ();
  534. else
  535. self.th_stand ();
  536. return;
  537. }
  538. }
  539. self.show_hostile = time + 1; // wake up other monsters
  540. // check knowledge of enemy
  541. enemy_visible = visible(self.enemy);
  542. if (enemy_visible)
  543. self.search_time = time + 5;
  544. // look for other coop players
  545. if (coop && self.search_time < time)
  546. {
  547. if (FindTarget ())
  548. return;
  549. }
  550. enemy_infront = infront(self.enemy);
  551. enemy_range = range(self.enemy);
  552. enemy_yaw = vectoyaw(self.enemy.origin - self.origin);
  553. if (self.attack_state == AS_MISSILE)
  554. {
  555. ai_run_missile ();
  556. return;
  557. }
  558. if (self.attack_state == AS_MELEE)
  559. {
  560. ai_run_melee ();
  561. return;
  562. }
  563. if (CheckAnyAttack ())
  564. return; // beginning an attack
  565. if (self.attack_state == AS_SLIDING)
  566. {
  567. ai_run_slide ();
  568. return;
  569. }
  570. // mal: added here for demonstration purposes for mods, but disabled by default in id1...
  571. #ifdef USE_ADVANCED_PATHING
  572. ai_pathtogoal( dist );
  573. #else
  574. movetogoal( dist ); // fall back to normal Quake movement behavior.
  575. #endif
  576. };