home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / quake / programs / asmoqc11 / ai.qc next >
Encoding:
Text File  |  1996-07-28  |  16.1 KB  |  804 lines

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