Author Topic: Simple Character That Attacks baddies  (Read 2124 times)

0 Members and 1 Guest are viewing this topic.

VGames

  • PD3 Beta Testers
  • Hero Member
  • *
  • Posts: 754
  • Karma: +17/-69
    • View Profile
Re: Simple Character That Attacks baddies
« Reply #15 on: June 10, 2015, 11:32:17 PM »
U mean the sentry bot?
Get the latest on Perfected Doom 3 here - http://www.moddb.com/mods/perfected-doom-3-version-500

The Happy Friar

  • Happy Happy Joy Joy
  • Administrator
  • Hero Member
  • *****
  • Posts: 858
  • Karma: +42/-4
  • Semi-Newbie.
    • View Profile
    • EarthQuake
Re: Simple Character That Attacks baddies
« Reply #16 on: June 11, 2015, 11:00:56 AM »
Yeah, that guy.  :)

VGames

  • PD3 Beta Testers
  • Hero Member
  • *
  • Posts: 754
  • Karma: +17/-69
    • View Profile
Re: Simple Character That Attacks baddies
« Reply #17 on: June 11, 2015, 12:11:39 PM »
I'll give it a try. Thanks.
Get the latest on Perfected Doom 3 here - http://www.moddb.com/mods/perfected-doom-3-version-500

Radegast

  • Newbie
  • *
  • Posts: 27
  • Karma: +3/-1
  • Alpha Labs scientist
    • View Profile
    • ET: Legacy
[Tutorial] Simple Character That Attacks baddies
« Reply #18 on: December 29, 2015, 02:31:20 PM »
Simple allied AI [using RBDOOM-3-BFG or one its forks]

This will show you how to spawn a marine NPC holding a machine gun who will attack any approaching enemies.

Step 1: unpack game data

Open the in-game console and execute: exec extract_resources

Step 2: create script basedev/scripts/ai_ally.script with the following content

Code: C++
  1. /*
  2. * Inherit from monster_zombie_security_pistol object which includes all the necessary
  3. * animations and behaviours like crouching and changing positions.
  4. */
  5. object ai_ally : monster_zombie_security_pistol
  6. {      
  7.         boolean         checkForEnemy( float use_fov );
  8.  
  9.         // States
  10.         void            state_Begin();
  11. };
  12.  
  13. /*
  14.  * Override parent checkForEnemy and adapt it to target monsters instead of the player
  15.  * My enemy is your enemy.
  16.  */
  17. boolean ai_ally::checkForEnemy( float use_fov ) {
  18.         entity enemy;
  19.  
  20.         if($player1.hasEnemies())
  21.         {
  22.                 enemy = closestReachableEnemyOfEntity($player1);
  23.  
  24.                 if(enemy.isHidden() || !canSee(enemy))
  25.                 {
  26.                         enemy = $null_entity;
  27.                         clearEnemy();
  28.                 }
  29.                 else
  30.                 {
  31.                         setEnemy(enemy);
  32.                         return true;
  33.                 }
  34.         }
  35.  
  36.         return false;
  37. }
  38.  
  39. /*
  40.  * Copy state_Begin of the parent class but remove a call to monster initialization
  41.  */
  42. void ai_ally::state_Begin() {
  43.         fire = false;
  44.         crouch_fire = false;
  45.         animState( ANIMCHANNEL_TORSO, "Torso_Idle", 0 );
  46.         animState( ANIMCHANNEL_LEGS, "Legs_Idle", 0 );
  47.  
  48.         setMoveType( MOVETYPE_ANIM );  
  49.         setState( "state_Idle" );
  50. }
  51.  

Step 3: open basedev/def/character_soldiers.def and change scriptobject of character_soldier_machinegun to ai_ally, so it looks like this:

Code: [Select]
entityDef character_soldier_machinegun {
...
"scriptobject" "ai_ally"
...
}

Step 4: Open basedev/scripts/doom_main.script and add  #include "script/ai_ally.script" at the bottom of the AI section

Step 5: launch the game with our new basedev mod: "RBDoom3BFG +set fs_game basedev +set fs_resourceLoadPriority 0 "

Finally, load a map and execute command "spawn character_soldier_machinegun" and if there are no enemies nearby, you can spawn a target for him too:  spawn monster_zombie_hazmat

When I return from my vacation I could write more basic AI tutorials like this one if there is interest for it, e.g. make the marine follow you and give him orders.

EDIT: to simplify this even more and make it work not only in the campaign, but also in custom maps, change the content of the checkForEnemy function to this:

Code: C++
  1. boolean ai_ally::checkForEnemy( float use_fov ) {
  2.         entity enemy;
  3.  
  4.         enemy = findEnemyAI(use_fov);
  5.  
  6.         if(enemy)
  7.         {
  8.                 setEnemy(enemy);
  9.                 return true;
  10.         }
  11.  
  12.         return false;
  13. }
  14.  

findEnemyAI function already has a check for hidden enemies, so there is no need for one inside the script
« Last Edit: January 03, 2016, 10:58:50 AM by Radegast »

Radegast

  • Newbie
  • *
  • Posts: 27
  • Karma: +3/-1
  • Alpha Labs scientist
    • View Profile
    • ET: Legacy
Re: Simple Character That Attacks baddies
« Reply #19 on: December 29, 2015, 03:15:50 PM »
How it looks like: