ai.qc 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. void() FoundTarget;
  52. float(float v) anglemod =
  53. {
  54. while (v >= 360)
  55. v = v - 360;
  56. while (v < 0)
  57. v = v + 360;
  58. return v;
  59. };
  60. /*
  61. ==============================================================================
  62. MOVETARGET CODE
  63. The angle of the movetarget effects standing and bowing direction, but has no effect on movement, which allways heads to the next target.
  64. targetname
  65. must be present. The name of this movetarget.
  66. target
  67. the next spot to move to. If not present, stop here for good.
  68. pausetime
  69. The number of seconds to spend standing or bowing for path_stand or path_bow
  70. ==============================================================================
  71. */
  72. void() movetarget_f =
  73. {
  74. if (!self.targetname)
  75. objerror ("monster_movetarget: no targetname");
  76. self.solid = SOLID_TRIGGER;
  77. self.touch = t_movetarget;
  78. setsize (self, '-8 -8 -8', '8 8 8');
  79. };
  80. /*QUAKED path_corner (0.5 0.3 0) (-8 -8 -8) (8 8 8)
  81. Monsters will continue walking towards the next target corner.
  82. "delay" delay to wait before proceeding to next segment;
  83. */
  84. void() path_corner =
  85. {
  86. movetarget_f ();
  87. };
  88. /*
  89. =============
  90. t_movetarget
  91. Something has bumped into a movetarget. If it is a monster
  92. moving towards it, change the next destination and continue.
  93. ==============
  94. */
  95. void() t_movetarget =
  96. {
  97. local entity temp;
  98. if (other.movetarget != self)
  99. return;
  100. if (other.enemy)
  101. return; // fighting, not following a path
  102. temp = self;
  103. self = other;
  104. other = temp;
  105. if (self.classname == "monster_ogre")
  106. sound (self, CHAN_VOICE, "ogre/ogdrag.wav", 1, ATTN_IDLE);// play chainsaw drag sound
  107. //dprint ("t_movetarget\n");
  108. //MED
  109. if (other.target)
  110. {
  111. self.goalentity = self.movetarget = find (world, targetname, other.target);
  112. self.ideal_yaw = vectoyaw(self.goalentity.origin - self.origin);
  113. if (!self.movetarget)
  114. {
  115. self.pausetime = time + 999999;
  116. self.th_stand ();
  117. return;
  118. }
  119. //MED 01/20/97
  120. else if (other.delay)
  121. {
  122. self.pausetime = time + other.delay;
  123. self.th_stand ();
  124. }
  125. }
  126. else
  127. {
  128. self.pausetime = time + 999999;
  129. self.th_stand ();
  130. return;
  131. }
  132. };
  133. //MED 01/20/97
  134. /*
  135. =============
  136. t_followtarget
  137. Something has bumped into a followtarget. If it is a monster
  138. moving towards it, change the next destination and continue.
  139. ==============
  140. */
  141. void() t_followtarget =
  142. {
  143. local entity temp;
  144. local vector spot1, spot2;
  145. local entity targ;
  146. local entity client;
  147. if (!other.flags & FL_MONSTER)
  148. return;
  149. if (other.classname == "monster_decoy")
  150. return;
  151. // if (other.enemy == world)
  152. // return;
  153. if (other.wetsuit_time > time)
  154. return;
  155. targ = other.enemy;
  156. // see if any entities are in the way of the shot
  157. spot1 = other.origin + other.view_ofs;
  158. spot2 = targ.origin + targ.view_ofs;
  159. traceline (spot1, spot2, FALSE, other);
  160. if (trace_fraction == 1)
  161. return;
  162. if (other.enemy)
  163. {
  164. // make the monster tame
  165. other.oldenemy = other.enemy;
  166. other.enemy = world;
  167. other.think = other.th_walk;
  168. }
  169. temp = self;
  170. self = other;
  171. other = temp;
  172. self.goalentity = self.movetarget = find (world, targetname, other.target);
  173. self.ideal_yaw = vectoyaw(self.goalentity.origin - self.origin);
  174. self.wetsuit_time = time + 2;
  175. if (!self.movetarget)
  176. {
  177. if (self.oldenemy != world)
  178. {
  179. self.enemy = self.oldenemy;
  180. FoundTarget();
  181. return;
  182. }
  183. else
  184. {
  185. client = checkclient ();
  186. if (!client)
  187. {
  188. self.enemy = client;
  189. FoundTarget();
  190. return;
  191. }
  192. self.pausetime = time + 999999;
  193. self.th_stand ();
  194. }
  195. }
  196. };
  197. void() followtarget_f =
  198. {
  199. // if (!self.targetname)
  200. // objerror ("monster_followtarget: no targetname");
  201. self.solid = SOLID_TRIGGER;
  202. self.touch = t_followtarget;
  203. setmodel (self, self.model); // set size and link into world
  204. self.movetype = MOVETYPE_NONE;
  205. self.modelindex = 0;
  206. self.model = "";
  207. // setsize (self, '-8 -8 -8', '8 8 8');
  208. };
  209. /*QUAKED path_follow (0.5 0.3 0) ?
  210. Monsters will stop what they are doing and follow to the path
  211. */
  212. void() path_follow =
  213. {
  214. followtarget_f ();
  215. };
  216. /*QUAKED path_follow2 (0.5 0.3 0) (-8 -8 -8) (8 8 8)
  217. Monsters will stop what they are doing and follow to the path
  218. */
  219. void() path_follow2 =
  220. {
  221. self.solid = SOLID_TRIGGER;
  222. self.touch = t_followtarget;
  223. setsize (self, '-8 -8 -8', '8 8 8');
  224. };
  225. //============================================================================
  226. /*
  227. =============
  228. range
  229. returns the range catagorization of an entity reletive to self
  230. 0 melee range, will become hostile even if back is turned
  231. 1 visibility and infront, or visibility and show hostile
  232. 2 infront and show hostile
  233. 3 only triggered by damage
  234. =============
  235. */
  236. float(entity targ) range =
  237. {
  238. local vector spot1, spot2;
  239. local float r;
  240. spot1 = self.origin + self.view_ofs;
  241. spot2 = targ.origin + targ.view_ofs;
  242. r = vlen (spot1 - spot2);
  243. if (r < 120)
  244. return RANGE_MELEE;
  245. if (r < 500)
  246. return RANGE_NEAR;
  247. if (r < 1000)
  248. return RANGE_MID;
  249. return RANGE_FAR;
  250. };
  251. /*
  252. =============
  253. visible
  254. returns 1 if the entity is visible to self, even if not infront ()
  255. =============
  256. */
  257. float (entity targ) visible =
  258. {
  259. local vector spot1, spot2;
  260. spot1 = self.origin + self.view_ofs;
  261. spot2 = targ.origin + targ.view_ofs;
  262. traceline (spot1, spot2, TRUE, self); // see through other monsters
  263. if (trace_inopen && trace_inwater)
  264. return FALSE; // sight line crossed contents
  265. //MED 11/21/96
  266. if (trace_fraction == 1)
  267. {
  268. visible_distance = vlen(spot2-spot1);
  269. return TRUE;
  270. }
  271. return FALSE;
  272. };
  273. /*
  274. =============
  275. infront
  276. returns 1 if the entity is in front (in sight) of self
  277. =============
  278. */
  279. float(entity targ) infront =
  280. {
  281. local vector vec;
  282. local float dot;
  283. makevectors (self.angles);
  284. vec = normalize (targ.origin - self.origin);
  285. dot = vec * v_forward;
  286. if ( dot > 0.3)
  287. {
  288. return TRUE;
  289. }
  290. return FALSE;
  291. };
  292. //============================================================================
  293. /*
  294. ===========
  295. ChangeYaw
  296. Turns towards self.ideal_yaw at self.yaw_speed
  297. Sets the global variable current_yaw
  298. Called every 0.1 sec by monsters
  299. ============
  300. */
  301. /*
  302. void() ChangeYaw =
  303. {
  304. local float ideal, move;
  305. //current_yaw = self.ideal_yaw;
  306. // mod down the current angle
  307. current_yaw = anglemod( self.angles_y );
  308. ideal = self.ideal_yaw;
  309. if (current_yaw == ideal)
  310. return;
  311. move = ideal - current_yaw;
  312. if (ideal > current_yaw)
  313. {
  314. if (move > 180)
  315. move = move - 360;
  316. }
  317. else
  318. {
  319. if (move < -180)
  320. move = move + 360;
  321. }
  322. if (move > 0)
  323. {
  324. if (move > self.yaw_speed)
  325. move = self.yaw_speed;
  326. }
  327. else
  328. {
  329. if (move < 0-self.yaw_speed )
  330. move = 0-self.yaw_speed;
  331. }
  332. current_yaw = anglemod (current_yaw + move);
  333. self.angles_y = current_yaw;
  334. };
  335. */
  336. //MED 10/18/96 added charmed stuff
  337. //============================================================================
  338. void() UpdateCharmerGoal =
  339. {
  340. local entity targ;
  341. local vector d;
  342. d = normalize(self.origin-self.charmer.origin);
  343. if (self.huntingcharmer == 1)
  344. {
  345. targ = spawn();
  346. self.trigger_field = targ;
  347. setorigin(targ,self.charmer.origin);
  348. self.huntingcharmer = 2;
  349. self.goalentity = targ;
  350. }
  351. if (self.huntingcharmer == 2)
  352. {
  353. targ = self.trigger_field;
  354. traceline(self.origin,self.charmer.origin,TRUE,self);
  355. if (trace_fraction == 1.0)
  356. {
  357. // dprint("can see ");
  358. // dprint(vtos(self.charmer.origin));
  359. // dprint("\n");
  360. setorigin(targ,self.charmer.origin);
  361. }
  362. }
  363. else
  364. {
  365. targ = self.trigger_field;
  366. setorigin(targ,self.charmer.origin + (d*300));
  367. }
  368. };
  369. //============================================================================
  370. void() HuntCharmer =
  371. {
  372. self.huntingcharmer = 1;
  373. UpdateCharmerGoal();
  374. // self.goalentity = self.charmer;
  375. self.think = self.th_walk;
  376. self.ideal_yaw = vectoyaw(self.goalentity.origin - self.origin);
  377. self.nextthink = time + 0.1;
  378. };
  379. //============================================================================
  380. void() FleeCharmer =
  381. {
  382. self.huntingcharmer = 1;
  383. UpdateCharmerGoal();
  384. self.huntingcharmer = 3;
  385. // self.goalentity = self.charmer;
  386. self.think = self.th_walk;
  387. // self.ideal_yaw = vectoyaw(self.originself.goalentity.origin - self.origin);
  388. self.nextthink = time + 0.1;
  389. };
  390. //============================================================================
  391. void() StopHuntingCharmer =
  392. {
  393. self.goalentity = world;
  394. if (self.huntingcharmer>1)
  395. remove(self.trigger_field);
  396. self.huntingcharmer = 0;
  397. self.think = self.th_stand;
  398. self.nextthink = time + 0.1;
  399. };
  400. //============================================================================
  401. void() HuntTarget =
  402. {
  403. self.goalentity = self.enemy;
  404. self.think = self.th_run;
  405. self.ideal_yaw = vectoyaw(self.enemy.origin - self.origin);
  406. self.nextthink = time + 0.1;
  407. SUB_AttackFinished (1); // wait a while before first attack
  408. };
  409. void() SightSound =
  410. {
  411. local float rsnd;
  412. if (self.classname == "monster_ogre")
  413. sound (self, CHAN_VOICE, "ogre/ogwake.wav", 1, ATTN_NORM);
  414. else if (self.classname == "monster_knight")
  415. sound (self, CHAN_VOICE, "knight/ksight.wav", 1, ATTN_NORM);
  416. else if (self.classname == "monster_shambler")
  417. sound (self, CHAN_VOICE, "shambler/ssight.wav", 1, ATTN_NORM);
  418. else if (self.classname == "monster_demon1")
  419. sound (self, CHAN_VOICE, "demon/sight2.wav", 1, ATTN_NORM);
  420. else if (self.classname == "monster_wizard")
  421. sound (self, CHAN_VOICE, "wizard/wsight.wav", 1, ATTN_NORM);
  422. else if (self.classname == "monster_zombie")
  423. sound (self, CHAN_VOICE, "zombie/z_idle.wav", 1, ATTN_NORM);
  424. else if (self.classname == "monster_dog")
  425. sound (self, CHAN_VOICE, "dog/dsight.wav", 1, ATTN_NORM);
  426. else if (self.classname == "monster_hell_knight")
  427. sound (self, CHAN_VOICE, "hknight/sight1.wav", 1, ATTN_NORM);
  428. else if (self.classname == "monster_tarbaby")
  429. sound (self, CHAN_VOICE, "blob/sight1.wav", 1, ATTN_NORM);
  430. else if (self.classname == "monster_vomit")
  431. sound (self, CHAN_VOICE, "vomitus/v_sight1.wav", 1, ATTN_NORM);
  432. else if (self.classname == "monster_enforcer")
  433. {
  434. rsnd = rint(random() * 3);
  435. if (rsnd == 1)
  436. sound (self, CHAN_VOICE, "enforcer/sight1.wav", 1, ATTN_NORM);
  437. else if (rsnd == 2)
  438. sound (self, CHAN_VOICE, "enforcer/sight2.wav", 1, ATTN_NORM);
  439. else if (rsnd == 0)
  440. sound (self, CHAN_VOICE, "enforcer/sight3.wav", 1, ATTN_NORM);
  441. else
  442. sound (self, CHAN_VOICE, "enforcer/sight4.wav", 1, ATTN_NORM);
  443. }
  444. else if (self.classname == "monster_army")
  445. sound (self, CHAN_VOICE, "soldier/sight1.wav", 1, ATTN_NORM);
  446. else if (self.classname == "monster_shalrath")
  447. sound (self, CHAN_VOICE, "shalrath/sight.wav", 1, ATTN_NORM);
  448. //MED
  449. else if (self.classname == "monster_gremlin")
  450. {
  451. if (self.stoleweapon == 0)
  452. sound (self, CHAN_VOICE, "grem/sight1.wav", 1, ATTN_NORM);
  453. }
  454. //MED
  455. else if (self.classname == "monster_scourge")
  456. sound (self, CHAN_VOICE, "scourge/sight.wav", 1, ATTN_NORM);
  457. //MED
  458. else if (self.classname == "monster_armagon")
  459. sound (self, CHAN_VOICE, "armagon/sight.wav", 1, 0.1);
  460. };
  461. void() FoundTarget =
  462. {
  463. //MED
  464. if (self.enemy.classname == "player")
  465. {
  466. if (self.charmed)
  467. {
  468. if ((self.charmer == self.enemy) || (self.charmer == self.enemy.charmer))
  469. {
  470. self.enemy = world;
  471. return;
  472. }
  473. }
  474. // let other monsters see this monster for a while
  475. sight_entity = self;
  476. sight_entity_time = time;
  477. }
  478. else if (self.charmed)
  479. {
  480. if ((self.charmer == self.enemy.charmer))
  481. {
  482. self.enemy = world;
  483. return;
  484. }
  485. }
  486. self.show_hostile = time + 1; // wake up other monsters
  487. SightSound ();
  488. HuntTarget ();
  489. };
  490. /*
  491. ===========
  492. FindTarget
  493. Self is currently not attacking anything, so try to find a target
  494. Returns TRUE if an enemy was sighted
  495. When a player fires a missile, the point of impact becomes a fakeplayer so
  496. that monsters that see the impact will respond as if they had seen the
  497. player.
  498. To avoid spending too much time, only a single client (or fakeclient) is
  499. checked each frame. This means multi player games will have slightly
  500. slower noticing monsters.
  501. ============
  502. */
  503. float() FindTarget =
  504. {
  505. local entity client;
  506. local float r;
  507. // if the first spawnflag bit is set, the monster will only wake up on
  508. // really seeing the player, not another monster getting angry
  509. // spawnflags & 3 is a big hack, because zombie crucified used the first
  510. // spawn flag prior to the ambush flag, and I forgot about it, so the second
  511. // spawn flag works as well
  512. //MED 10/17/96 added charmed stuff
  513. if (self.charmed)
  514. {
  515. self.effects = self.effects | EF_DIMLIGHT;
  516. if (self.huntingcharmer > 0)
  517. {
  518. UpdateCharmerGoal();
  519. // self.goalentity = self.charmer;
  520. r = vlen(self.origin - self.goalentity.origin);
  521. if (r < MIN_CHARMER_DISTANCE)
  522. {
  523. // dprint("stopping\n");
  524. if ((self.huntingcharmer == 3) && (r > TOOCLOSE_CHARMER_DISTANCE))
  525. return FALSE;
  526. // self.huntingcharmer = 0;
  527. StopHuntingCharmer();
  528. return TRUE;
  529. }
  530. }
  531. else if (vlen (self.origin - self.charmer.origin) > MAX_CHARMER_DISTANCE)
  532. {
  533. // self.huntingcharmer = 1;
  534. // dprint("hunting\n");
  535. HuntCharmer();
  536. return FALSE;
  537. }
  538. else if (vlen (self.origin - self.charmer.origin) < TOOCLOSE_CHARMER_DISTANCE)
  539. {
  540. // self.huntingcharmer = 1;
  541. // dprint("fleeing\n");
  542. FleeCharmer();
  543. return FALSE;
  544. }
  545. }
  546. if (sight_entity_time >= time - 0.1 && !(self.spawnflags & 3) && !(self.charmed) )
  547. {
  548. client = sight_entity;
  549. if (client.enemy == self.enemy)
  550. return;
  551. }
  552. //MED 10/17/96 added charmed clause
  553. else if (self.charmed)
  554. {
  555. local entity head;
  556. local entity selected;
  557. local float dist;
  558. selected = world;
  559. dist = CHARMED_RADIUS;
  560. head = findradius(self.origin, CHARMED_RADIUS);
  561. while(head)
  562. {
  563. if(!(head.flags & FL_NOTARGET) && (head.flags & FL_MONSTER))
  564. {
  565. if (visible(head) && (visible_distance < dist) && (head.health>0))
  566. {
  567. if ((head !=self) && (head != self.charmer) && (head.charmer != self.charmer))
  568. {
  569. selected = head;
  570. dist = visible_distance;
  571. }
  572. }
  573. }
  574. head = head.chain;
  575. }
  576. if (selected == world)
  577. return FALSE;
  578. client = selected;
  579. }
  580. else
  581. {
  582. client = checkclient ();
  583. if (!client)
  584. return FALSE; // current check entity isn't in PVS
  585. }
  586. if (client == self.enemy)
  587. return FALSE;
  588. //MED 10/17/96 added charmed stuff
  589. if (!self.charmed)
  590. {
  591. if (client.flags & FL_NOTARGET)
  592. return FALSE;
  593. }
  594. if (client.items & IT_INVISIBILITY)
  595. return FALSE;
  596. r = range (client);
  597. if (r == RANGE_FAR)
  598. return FALSE;
  599. if (!visible (client))
  600. return FALSE;
  601. //MED 10/17/96 added charmed stuff
  602. if (!self.charmed)
  603. {
  604. if (r == RANGE_NEAR)
  605. {
  606. if (client.show_hostile < time && !infront (client))
  607. return FALSE;
  608. }
  609. else if (r == RANGE_MID)
  610. {
  611. if ( /* client.show_hostile < time || */ !infront (client))
  612. return FALSE;
  613. }
  614. }
  615. //
  616. // got one
  617. //
  618. self.enemy = client;
  619. //MED 10/17/96 added charmed stuff
  620. if ((!self.charmed) && (!self.enemy.charmed))
  621. {
  622. if (self.enemy.classname != "player")
  623. {
  624. self.enemy = self.enemy.enemy;
  625. if (self.enemy.classname != "player")
  626. {
  627. self.enemy = world;
  628. return FALSE;
  629. }
  630. }
  631. }
  632. FoundTarget ();
  633. return TRUE;
  634. };
  635. //=============================================================================
  636. void(float dist) ai_forward =
  637. {
  638. walkmove (self.angles_y, dist);
  639. };
  640. void(float dist) ai_back =
  641. {
  642. walkmove ( (self.angles_y+180), dist);
  643. };
  644. /*
  645. =============
  646. ai_pain
  647. stagger back a bit
  648. =============
  649. */
  650. void(float dist) ai_pain =
  651. {
  652. ai_back (dist);
  653. /*
  654. local float away;
  655. away = anglemod (vectoyaw (self.origin - self.enemy.origin)
  656. + 180*(random()- 0.5) );
  657. walkmove (away, dist);
  658. */
  659. };
  660. /*
  661. =============
  662. ai_painforward
  663. stagger back a bit
  664. =============
  665. */
  666. void(float dist) ai_painforward =
  667. {
  668. walkmove (self.ideal_yaw, dist);
  669. };
  670. /*
  671. =============
  672. ai_walk
  673. The monster is walking it's beat
  674. =============
  675. */
  676. void(float dist) ai_walk =
  677. {
  678. local vector mtemp;
  679. movedist = dist;
  680. //MED 01/20/97
  681. // check for noticing a player
  682. if (FindTarget ())
  683. return;
  684. //MED 11/02/96
  685. if (self.huntingcharmer)
  686. {
  687. // movetogoal (dist*2);
  688. movetogoal (dist);
  689. self.nextthink = time + ((self.nextthink - time)/2);
  690. }
  691. else
  692. movetogoal (dist);
  693. };
  694. /*
  695. =============
  696. ai_stand
  697. The monster is staying in one place for a while, with slight angle turns
  698. =============
  699. */
  700. void() ai_stand =
  701. {
  702. if (FindTarget ())
  703. return;
  704. if (time > self.pausetime)
  705. {
  706. self.th_walk ();
  707. return;
  708. }
  709. // change angle slightly
  710. };
  711. /*
  712. =============
  713. ai_turn
  714. don't move, but turn towards ideal_yaw
  715. =============
  716. */
  717. void() ai_turn =
  718. {
  719. if (FindTarget ())
  720. return;
  721. ChangeYaw ();
  722. };
  723. //MED 11/10/96 added ai_turn_in_place
  724. /*
  725. =============
  726. ai_turn_in_place
  727. don't move, but turn towards ideal_yaw
  728. =============
  729. */
  730. void() ai_turn_in_place =
  731. {
  732. local float delta;
  733. self.nextthink = time + 0.1;
  734. enemy_yaw = vectoyaw(self.enemy.origin - self.origin);
  735. delta = fabs(self.angles_y - enemy_yaw);
  736. if (delta > MIN_ANGLE_DELTA)
  737. {
  738. self.ideal_yaw = enemy_yaw;
  739. ChangeYaw();
  740. }
  741. else
  742. {
  743. self.think = self.th_run;
  744. }
  745. };
  746. //=============================================================================
  747. /*
  748. =============
  749. ChooseTurn
  750. =============
  751. */
  752. void(vector dest3) ChooseTurn =
  753. {
  754. local vector dir, newdir;
  755. dir = self.origin - dest3;
  756. newdir_x = trace_plane_normal_y;
  757. newdir_y = 0 - trace_plane_normal_x;
  758. newdir_z = 0;
  759. if (dir * newdir > 0)
  760. {
  761. dir_x = 0 - trace_plane_normal_y;
  762. dir_y = trace_plane_normal_x;
  763. }
  764. else
  765. {
  766. dir_x = trace_plane_normal_y;
  767. dir_y = 0 - trace_plane_normal_x;
  768. }
  769. dir_z = 0;
  770. self.ideal_yaw = vectoyaw(dir);
  771. };
  772. /*
  773. ============
  774. FacingIdeal
  775. ============
  776. */
  777. float() FacingIdeal =
  778. {
  779. local float delta;
  780. delta = anglemod(self.angles_y - self.ideal_yaw);
  781. if (delta > 45 && delta < 315)
  782. return FALSE;
  783. return TRUE;
  784. };
  785. //=============================================================================
  786. float() WizardCheckAttack;
  787. float() DogCheckAttack;
  788. //MED
  789. float() GremlinCheckAttack;
  790. float() ScourgeCheckAttack;
  791. float() ArmagonCheckAttack;
  792. float() CheckAnyAttack =
  793. {
  794. if (!enemy_vis)
  795. return;
  796. if (self.classname == "monster_army")
  797. return SoldierCheckAttack ();
  798. if (self.classname == "monster_ogre")
  799. return OgreCheckAttack ();
  800. if (self.classname == "monster_shambler")
  801. return ShamCheckAttack ();
  802. if (self.classname == "monster_demon1")
  803. return DemonCheckAttack ();
  804. if (self.classname == "monster_dog")
  805. return DogCheckAttack ();
  806. if (self.classname == "monster_wizard")
  807. return WizardCheckAttack ();
  808. if (self.classname == "monster_gremlin")
  809. return GremlinCheckAttack ();
  810. if (self.classname == "monster_scourge")
  811. return ScourgeCheckAttack ();
  812. if (self.classname == "monster_armagon")
  813. return ArmagonCheckAttack ();
  814. return CheckAttack ();
  815. };
  816. /*
  817. =============
  818. ai_run_melee
  819. Turn and close until within an angle to launch a melee attack
  820. =============
  821. */
  822. void() ai_run_melee =
  823. {
  824. self.ideal_yaw = enemy_yaw;
  825. ChangeYaw ();
  826. if (FacingIdeal())
  827. {
  828. self.th_melee ();
  829. self.attack_state = AS_STRAIGHT;
  830. }
  831. };
  832. /*
  833. =============
  834. ai_run_missile
  835. Turn in place until within an angle to launch a missile attack
  836. =============
  837. */
  838. void() ai_run_missile =
  839. {
  840. self.ideal_yaw = enemy_yaw;
  841. ChangeYaw ();
  842. if (FacingIdeal())
  843. {
  844. self.th_missile ();
  845. self.attack_state = AS_STRAIGHT;
  846. }
  847. };
  848. /*
  849. =============
  850. ai_run_slide
  851. Strafe sideways, but stay at aproximately the same range
  852. =============
  853. */
  854. void() ai_run_slide =
  855. {
  856. local float ofs;
  857. self.ideal_yaw = enemy_yaw;
  858. ChangeYaw ();
  859. if (self.lefty)
  860. ofs = 90;
  861. else
  862. ofs = -90;
  863. if (walkmove (self.ideal_yaw + ofs, movedist))
  864. return;
  865. self.lefty = 1 - self.lefty;
  866. walkmove (self.ideal_yaw - ofs, movedist);
  867. };
  868. //MED
  869. /*
  870. =============
  871. ai_run_dodge
  872. Strafe sideways, but continue moving towards the enemy
  873. Used by the Scourge.
  874. =============
  875. */
  876. void() ai_run_dodge =
  877. {
  878. local float ofs;
  879. local float newyaw;
  880. /*
  881. // attempt to jump over missiles
  882. if (self.enemy.weaponframe == 1)
  883. {
  884. if (self.flags & FL_ONGROUND)
  885. {
  886. self.origin_z = self.origin_z + 1;
  887. self.velocity = self.velocity + '0 0 500';
  888. self.flags = self.flags - FL_ONGROUND;
  889. }
  890. self.ltime = self.ltime + 1.0;
  891. }
  892. */
  893. self.nextthink = time + 0.1;
  894. if (self.lefty)
  895. ofs = 40;
  896. else
  897. ofs = -40;
  898. if (time > self.ltime)
  899. {
  900. self.lefty = 1 - self.lefty;
  901. self.ltime = time + 0.8;
  902. }
  903. newyaw = enemy_yaw + ofs;
  904. self.ideal_yaw = enemy_yaw;
  905. if (walkmove (newyaw, movedist))
  906. {
  907. ChangeYaw ();
  908. return;
  909. }
  910. self.lefty = 1 - self.lefty;
  911. self.ltime = time + 0.8;
  912. newyaw = enemy_yaw - ofs;
  913. self.ideal_yaw = enemy_yaw;
  914. walkmove (newyaw, movedist);
  915. ChangeYaw ();
  916. };
  917. /*
  918. =============
  919. ai_run
  920. The monster has an enemy it is trying to kill
  921. =============
  922. */
  923. float RUN_STRAIGHT;
  924. void(float dist) ai_run =
  925. {
  926. local vector delta;
  927. local float axis;
  928. local float direct, ang_rint, ang_floor, ang_ceil;
  929. movedist = dist;
  930. // see if the enemy is dead
  931. if (self.enemy.health <= 0 || (self.charmed && (self.charmer == self.enemy)))
  932. {
  933. self.enemy = world;
  934. //MED 10/30/96 added charmed stuff
  935. if (self.charmed)
  936. {
  937. HuntCharmer();
  938. return;
  939. }
  940. // FIXME: look all around for other targets
  941. if (self.oldenemy.health > 0)
  942. {
  943. self.enemy = self.oldenemy;
  944. HuntTarget ();
  945. }
  946. else
  947. {
  948. if (self.movetarget)
  949. self.th_walk ();
  950. else
  951. self.th_stand ();
  952. return;
  953. }
  954. }
  955. self.show_hostile = time + 1; // wake up other monsters
  956. // check knowledge of enemy
  957. enemy_vis = visible(self.enemy);
  958. if (enemy_vis)
  959. self.search_time = time + 5;
  960. // look for other coop players
  961. //MED 10/17/96 added charmed stuff
  962. if (coop && self.search_time < time && !self.charmed)
  963. {
  964. if (FindTarget ())
  965. return;
  966. }
  967. enemy_infront = infront(self.enemy);
  968. enemy_range = range(self.enemy);
  969. enemy_yaw = vectoyaw(self.enemy.origin - self.origin);
  970. //MED
  971. if (self.th_turn)
  972. {
  973. local float angledelta;
  974. angledelta = fabs(self.angles_y - enemy_yaw);
  975. if (angledelta > MIN_ANGLE_DELTA)
  976. {
  977. self.th_turn();
  978. return;
  979. }
  980. }
  981. if (self.attack_state == AS_MISSILE)
  982. {
  983. //dprint ("ai_run_missile\n");
  984. ai_run_missile ();
  985. return;
  986. }
  987. if (self.attack_state == AS_MELEE)
  988. {
  989. //dprint ("ai_run_melee\n");
  990. ai_run_melee ();
  991. return;
  992. }
  993. if (CheckAnyAttack ())
  994. {
  995. return; // beginning an attack
  996. }
  997. if (self.attack_state == AS_SLIDING)
  998. {
  999. ai_run_slide ();
  1000. return;
  1001. }
  1002. if (self.attack_state == AS_DODGING)
  1003. {
  1004. ai_run_dodge ();
  1005. return;
  1006. }
  1007. //MED 11/11/96
  1008. if (RUN_STRAIGHT && time > self.endtime)
  1009. {
  1010. RUN_STRAIGHT = 0;
  1011. axis = walkmove (self.angles_y, movedist);
  1012. if (!axis)
  1013. {
  1014. self.endtime = time + 3;
  1015. movetogoal (dist); // done in C code...
  1016. }
  1017. // else
  1018. // ChangeYaw();
  1019. }
  1020. else
  1021. {
  1022. // head straight in
  1023. movetogoal (dist); // done in C code...
  1024. }
  1025. };