News:

One Minute Game Review by The Happy Friar: https://ugetube.com/@OneMinteGameReviews
Also on Rumble: https://rumble.com/c/c-1115371

idTech 4 (aka Doom 3 tech) Discord Server! https://discord.gg/9wtCGHa

Main Menu

Simple Character That Attacks baddies

Started by VGames, June 08, 2015, 08:52:06 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

VGames

Get the latest on Perfected Doom 3 here - http://www.moddb.com/mods/perfected-doom-3-version-500

The Happy Friar


VGames

Get the latest on Perfected Doom 3 here - http://www.moddb.com/mods/perfected-doom-3-version-500

Radegast

#18
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 (cpp) Select
/*
* Inherit from monster_zombie_security_pistol object which includes all the necessary
* animations and behaviours like crouching and changing positions.
*/
object ai_ally : monster_zombie_security_pistol
{
boolean checkForEnemy( float use_fov );

// States
void state_Begin();
};

/*
* Override parent checkForEnemy and adapt it to target monsters instead of the player
* My enemy is your enemy.
*/
boolean ai_ally::checkForEnemy( float use_fov ) {
entity enemy;

if($player1.hasEnemies())
{
enemy = closestReachableEnemyOfEntity($player1);

if(enemy.isHidden() || !canSee(enemy))
{
enemy = $null_entity;
clearEnemy();
}
else
{
setEnemy(enemy);
return true;
}
}

return false;
}

/*
* Copy state_Begin of the parent class but remove a call to monster initialization
*/
void ai_ally::state_Begin() {
fire = false;
crouch_fire = false;
animState( ANIMCHANNEL_TORSO, "Torso_Idle", 0 );
animState( ANIMCHANNEL_LEGS, "Legs_Idle", 0 );

setMoveType( MOVETYPE_ANIM );
setState( "state_Idle" );
}


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

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 (cpp) Select
boolean ai_ally::checkForEnemy( float use_fov ) {
entity enemy;

enemy = findEnemyAI(use_fov);

if(enemy)
{
setEnemy(enemy);
return true;
}

return false;
}


findEnemyAI function already has a check for hidden enemies, so there is no need for one inside the script

Radegast