ai.qc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /* Copyright (C) 1996-1997 Id Software, Inc.
  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. sound (self, CHAN_VOICE, "knight/ksight.wav", 1, ATTN_NORM);
  238. else if (self.classname == "monster_shambler")
  239. sound (self, CHAN_VOICE, "shambler/ssight.wav", 1, ATTN_NORM);
  240. else if (self.classname == "monster_demon1")
  241. sound (self, CHAN_VOICE, "demon/sight2.wav", 1, ATTN_NORM);
  242. else if (self.classname == "monster_wizard")
  243. sound (self, CHAN_VOICE, "wizard/wsight.wav", 1, ATTN_NORM);
  244. else if (self.classname == "monster_zombie")
  245. sound (self, CHAN_VOICE, "zombie/z_idle.wav", 1, ATTN_NORM);
  246. else if (self.classname == "monster_dog")
  247. sound (self, CHAN_VOICE, "dog/dsight.wav", 1, ATTN_NORM);
  248. else if (self.classname == "monster_hell_knight")
  249. sound (self, CHAN_VOICE, "hknight/sight1.wav", 1, ATTN_NORM);
  250. else if (self.classname == "monster_tarbaby")
  251. sound (self, CHAN_VOICE, "blob/sight1.wav", 1, ATTN_NORM);
  252. else if (self.classname == "monster_vomit")
  253. sound (self, CHAN_VOICE, "vomitus/v_sight1.wav", 1, ATTN_NORM);
  254. else if (self.classname == "monster_enforcer")
  255. {
  256. rsnd = rint(random() * 3);
  257. if (rsnd == 1)
  258. sound (self, CHAN_VOICE, "enforcer/sight1.wav", 1, ATTN_NORM);
  259. else if (rsnd == 2)
  260. sound (self, CHAN_VOICE, "enforcer/sight2.wav", 1, ATTN_NORM);
  261. else if (rsnd == 0)
  262. sound (self, CHAN_VOICE, "enforcer/sight3.wav", 1, ATTN_NORM);
  263. else
  264. sound (self, CHAN_VOICE, "enforcer/sight4.wav", 1, ATTN_NORM);
  265. }
  266. else if (self.classname == "monster_army")
  267. sound (self, CHAN_VOICE, "soldier/sight1.wav", 1, ATTN_NORM);
  268. else if (self.classname == "monster_shalrath")
  269. sound (self, CHAN_VOICE, "shalrath/sight.wav", 1, ATTN_NORM);
  270. };
  271. void() FoundTarget =
  272. {
  273. if (self.enemy.classname == "player")
  274. { // let other monsters see this monster for a while
  275. sight_entity = self;
  276. sight_entity_time = time;
  277. }
  278. self.show_hostile = time + 1; // wake up other monsters
  279. SightSound ();
  280. HuntTarget ();
  281. };
  282. /*
  283. ===========
  284. FindTarget
  285. Self is currently not attacking anything, so try to find a target
  286. Returns TRUE if an enemy was sighted
  287. When a player fires a missile, the point of impact becomes a fakeplayer so
  288. that monsters that see the impact will respond as if they had seen the
  289. player.
  290. To avoid spending too much time, only a single client (or fakeclient) is
  291. checked each frame. This means multi player games will have slightly
  292. slower noticing monsters.
  293. ============
  294. */
  295. float() FindTarget =
  296. {
  297. local entity client;
  298. local float r;
  299. // if the first spawnflag bit is set, the monster will only wake up on
  300. // really seeing the player, not another monster getting angry
  301. // spawnflags & 3 is a big hack, because zombie crucified used the first
  302. // spawn flag prior to the ambush flag, and I forgot about it, so the second
  303. // spawn flag works as well
  304. if (sight_entity_time >= time - 0.1 && !(self.spawnflags & 3) )
  305. {
  306. client = sight_entity;
  307. if (client.enemy == self.enemy)
  308. return;
  309. }
  310. else
  311. {
  312. client = checkclient ();
  313. if (!client)
  314. return FALSE; // current check entity isn't in PVS
  315. }
  316. if (client == self.enemy)
  317. return FALSE;
  318. if (client.flags & FL_NOTARGET)
  319. return FALSE;
  320. if (client.items & IT_INVISIBILITY)
  321. return FALSE;
  322. r = range (client);
  323. if (r == RANGE_FAR)
  324. return FALSE;
  325. if (!visible (client))
  326. return FALSE;
  327. if (r == RANGE_NEAR)
  328. {
  329. if (client.show_hostile < time && !infront (client))
  330. return FALSE;
  331. }
  332. else if (r == RANGE_MID)
  333. {
  334. if ( /* client.show_hostile < time || */ !infront (client))
  335. return FALSE;
  336. }
  337. //
  338. // got one
  339. //
  340. self.enemy = client;
  341. if (self.enemy.classname != "player")
  342. {
  343. self.enemy = self.enemy.enemy;
  344. if (self.enemy.classname != "player")
  345. {
  346. self.enemy = world;
  347. return FALSE;
  348. }
  349. }
  350. FoundTarget ();
  351. return TRUE;
  352. };
  353. //=============================================================================
  354. void(float dist) ai_forward =
  355. {
  356. walkmove (self.angles_y, dist);
  357. };
  358. void(float dist) ai_back =
  359. {
  360. walkmove ( (self.angles_y+180), dist);
  361. };
  362. /*
  363. =============
  364. ai_pain
  365. stagger back a bit
  366. =============
  367. */
  368. void(float dist) ai_pain =
  369. {
  370. ai_back (dist);
  371. /*
  372. local float away;
  373. away = anglemod (vectoyaw (self.origin - self.enemy.origin)
  374. + 180*(random()- 0.5) );
  375. walkmove (away, dist);
  376. */
  377. };
  378. /*
  379. =============
  380. ai_painforward
  381. stagger back a bit
  382. =============
  383. */
  384. void(float dist) ai_painforward =
  385. {
  386. walkmove (self.ideal_yaw, dist);
  387. };
  388. /*
  389. =============
  390. ai_walk
  391. The monster is walking it's beat
  392. =============
  393. */
  394. void(float dist) ai_walk =
  395. {
  396. local vector mtemp;
  397. movedist = dist;
  398. if (self.classname == "monster_dragon")
  399. {
  400. movetogoal (dist);
  401. return;
  402. }
  403. // check for noticing a player
  404. if (FindTarget ())
  405. return;
  406. movetogoal (dist);
  407. };
  408. /*
  409. =============
  410. ai_stand
  411. The monster is staying in one place for a while, with slight angle turns
  412. =============
  413. */
  414. void() ai_stand =
  415. {
  416. if (FindTarget ())
  417. return;
  418. if (time > self.pausetime)
  419. {
  420. self.th_walk ();
  421. return;
  422. }
  423. // change angle slightly
  424. };
  425. /*
  426. =============
  427. ai_turn
  428. don't move, but turn towards ideal_yaw
  429. =============
  430. */
  431. void() ai_turn =
  432. {
  433. if (FindTarget ())
  434. return;
  435. ChangeYaw ();
  436. };
  437. //=============================================================================
  438. /*
  439. =============
  440. ChooseTurn
  441. =============
  442. */
  443. void(vector dest3) ChooseTurn =
  444. {
  445. local vector dir, newdir;
  446. dir = self.origin - dest3;
  447. newdir_x = trace_plane_normal_y;
  448. newdir_y = 0 - trace_plane_normal_x;
  449. newdir_z = 0;
  450. if (dir * newdir > 0)
  451. {
  452. dir_x = 0 - trace_plane_normal_y;
  453. dir_y = trace_plane_normal_x;
  454. }
  455. else
  456. {
  457. dir_x = trace_plane_normal_y;
  458. dir_y = 0 - trace_plane_normal_x;
  459. }
  460. dir_z = 0;
  461. self.ideal_yaw = vectoyaw(dir);
  462. };
  463. /*
  464. ============
  465. FacingIdeal
  466. ============
  467. */
  468. float() FacingIdeal =
  469. {
  470. local float delta;
  471. delta = anglemod(self.angles_y - self.ideal_yaw);
  472. if (delta > 45 && delta < 315)
  473. return FALSE;
  474. return TRUE;
  475. };
  476. //=============================================================================
  477. float() WizardCheckAttack;
  478. float() DogCheckAttack;
  479. float() CheckAnyAttack =
  480. {
  481. if (!enemy_vis)
  482. return;
  483. if (self.classname == "monster_army")
  484. return SoldierCheckAttack ();
  485. if (self.classname == "monster_ogre")
  486. return OgreCheckAttack ();
  487. if (self.classname == "monster_shambler")
  488. return ShamCheckAttack ();
  489. if (self.classname == "monster_demon1")
  490. return DemonCheckAttack ();
  491. if (self.classname == "monster_dog")
  492. return DogCheckAttack ();
  493. if (self.classname == "monster_wizard")
  494. return WizardCheckAttack ();
  495. return CheckAttack ();
  496. };
  497. /*
  498. =============
  499. ai_run_melee
  500. Turn and close until within an angle to launch a melee attack
  501. =============
  502. */
  503. void() ai_run_melee =
  504. {
  505. self.ideal_yaw = enemy_yaw;
  506. ChangeYaw ();
  507. if (FacingIdeal())
  508. {
  509. self.th_melee ();
  510. self.attack_state = AS_STRAIGHT;
  511. }
  512. };
  513. /*
  514. =============
  515. ai_run_missile
  516. Turn in place until within an angle to launch a missile attack
  517. =============
  518. */
  519. void() ai_run_missile =
  520. {
  521. self.ideal_yaw = enemy_yaw;
  522. ChangeYaw ();
  523. if (FacingIdeal())
  524. {
  525. self.th_missile ();
  526. self.attack_state = AS_STRAIGHT;
  527. }
  528. };
  529. /*
  530. =============
  531. ai_run_slide
  532. Strafe sideways, but stay at aproximately the same range
  533. =============
  534. */
  535. void() ai_run_slide =
  536. {
  537. local float ofs;
  538. self.ideal_yaw = enemy_yaw;
  539. ChangeYaw ();
  540. if (self.lefty)
  541. ofs = 90;
  542. else
  543. ofs = -90;
  544. if (walkmove (self.ideal_yaw + ofs, movedist))
  545. return;
  546. self.lefty = 1 - self.lefty;
  547. walkmove (self.ideal_yaw - ofs, movedist);
  548. };
  549. /*
  550. =============
  551. ai_run
  552. The monster has an enemy it is trying to kill
  553. =============
  554. */
  555. void(float dist) ai_run =
  556. {
  557. local vector delta;
  558. local float axis;
  559. local float direct, ang_rint, ang_floor, ang_ceil;
  560. movedist = dist;
  561. // see if the enemy is dead
  562. if (self.enemy.health <= 0)
  563. {
  564. self.enemy = world;
  565. // FIXME: look all around for other targets
  566. if (self.oldenemy.health > 0)
  567. {
  568. self.enemy = self.oldenemy;
  569. HuntTarget ();
  570. }
  571. else
  572. {
  573. if (self.movetarget)
  574. self.th_walk ();
  575. else
  576. self.th_stand ();
  577. return;
  578. }
  579. }
  580. self.show_hostile = time + 1; // wake up other monsters
  581. // check knowledge of enemy
  582. enemy_vis = visible(self.enemy);
  583. if (enemy_vis)
  584. self.search_time = time + 5;
  585. // look for other coop players
  586. if (coop && self.search_time < time)
  587. {
  588. if (FindTarget ())
  589. return;
  590. }
  591. enemy_infront = infront(self.enemy);
  592. enemy_range = range(self.enemy);
  593. enemy_yaw = vectoyaw(self.enemy.origin - self.origin);
  594. if (self.attack_state == AS_MISSILE)
  595. {
  596. //dprint ("ai_run_missile\n");
  597. ai_run_missile ();
  598. return;
  599. }
  600. if (self.attack_state == AS_MELEE)
  601. {
  602. //dprint ("ai_run_melee\n");
  603. ai_run_melee ();
  604. return;
  605. }
  606. if (CheckAnyAttack ())
  607. return; // beginning an attack
  608. if (self.attack_state == AS_SLIDING)
  609. {
  610. ai_run_slide ();
  611. return;
  612. }
  613. // head straight in
  614. movetogoal (dist); // done in C code...
  615. };