Kevin Giang - Technical Designer, Programmer
  • Projects
    • Justice League - Battle for Metropolis
    • Plants vs Zombies: 3Z Arena
    • Benno's Great Race
    • Lightmare
    • Bored Space Cops
    • Project Nova
    • OcuBlocks
    • CandyMakers
    • Cubedly
    • Unfolding Tale
    • Ancient Forest & Grumpy Monsters
    • Analog Games >
      • Treasure Hunting
      • Treasure Hunting: Kraken
      • Treasure Hunting: One for Self, All for Self
      • Treasure Hunting: Houdini
      • Shadow Peacekeepers
  • Who I Am
Sample Code - Custom Flying AI

This AI is designed to extend off of UDK's bots to use their pathfinding code. They will be looking for any light orbs within their sight radius to chase after, and will chase players if there is no light orb around.
Code template credit to: Lucas William's tutorial at http://lucas-williams.com/2010/12/udk-building-better-bots/

class Adv_CreatureAIController extends UTBot;

var Adv_Proj_MedusaBounceSpawner theGrenade; // Variable to hold the light orb object
var Adv_Pawn thePlayer;
var Adv_CreaturePawn thePawn;
var Actor Destination;

var float grenadeSightDistance; // How far the creature will look for the light orb
var float grenadeFarAway; // Variable to hold distance between pawn and object // Variable speed that will be passed into a function in Adv_CreaturePawn to calculate modified speed
var SkeletalMesh grenadeChaseMesh; // Holds the skeletal mesh the pawn will change to in the state

var float pawnSightDistance;
var float pawnFarAway;

var SkeletalMesh pawnChaseMesh;
var SkeletalMesh roamingMesh;

var float searchInterval; // Time in seconds for a search loop.

protected event ExecuteWhatToDoNext()
{
   //Go to the roaming state
   GotoState('Roaming');
}

function Adv_Proj_MedusaBounceSpawner FindGrenade()
{
            local Adv_Proj_MedusaBounceSpawner G;

            ForEach AllActors(class'Adv_Proj_MedusaBounceSpawner', G)
            {
                        if (G != None)
                        {
                                    return G;
                        }
            }
            Return None;
}

function Adv_Pawn FindEnemy()
{
            local Adv_Pawn P;

            ForEach AllActors(class'Adv_Pawn', P)
            {
                        if (Adv_PlayerController(P.Controller) != None)
                        return P;
            }
            Return None;

}

state Roaming
{
Begin:
            if(theGrenade == none)
            {
                        theGrenade = FindGrenade();
                        grenadeFarAway = vsize(Pawn.Location - theGrenade.Location);
                        if (grenadeFarAway > grenadeSightDistance)
                        {
                                    theGrenade = None;
                        }
            }

            if(Enemy == None)
            {
                        //If the bot has no enemy, find one
                        Enemy = FindEnemy();
                        pawnFarAway = vsize(Pawn.Location - Enemy.Location);
                        if (pawnFarAway > pawnSightDistance)
                        {
                                    Enemy = None;
                        }
            }

            if (theGrenade != none)
            {
                        grenadefarAway = vsize(Pawn.Location - theGrenade.Location);
                        GoToState('chaseLight');
            }
            else if(Enemy != None)
            {
                        pawnFarAway = vsize(Pawn.Location - Enemy.Location);
                        GoToState('pawnAttack');
            }
            else
            {
                        thePawn = Adv_CreaturePawn(Pawn);
                        if(thePawn != none)
                        {
                                    thePawn.setRoaming();
                        }
                        moveto(findrandomdest().location);
            }

            Sleep(searchInterval);
            Goto('Begin');
}

state chaseLight
{
Begin:
            if (!theGrenade.isAlive)  // Check first to see if Grenade light is dead
            {
                        theGrenade = FindGrenade();
                        grenadefarAway = vsize(Pawn.Location - theGrenade.Location);
                        if (grenadefarAway > grenadeSightDistance)
                        {
                                    theGrenade = None;
                        }
            }
            else
            {
                        if (grenadefarAway > grenadeSightDistance)
                        {
                                    theGrenade = None;
                        }
                        else
                        {
                                    thePawn = Adv_CreaturePawn(Pawn);
                                    if(thePawn != none)
                                    {
                                                thePawn.setLightChase();
                                    }

                                    if(FindBestPathToward(theGrenade, false, false))
                                    {
                                                //Move along the path that was just calculated
                                                MoveToward(MoveTarget,,,False);
                                    }
                                    else
                                    {
                                                MoveToward(theGrenade, , ,False);
                                    }
                        }
            }
             
            GoToState('Roaming');
}

state pawnAttack
{
Begin:
            if (pawnFarAway > pawnSightDistance)
            {
                        Enemy = None;
            }
            else
            {
                        if (theGrenade != none && theGrenade.isAlive == true)
                        {
                                    thePawn = Adv_CreaturePawn(Pawn);
                                    if(thePawn != none)
                                    {
                                                thePawn.setLightChase();
                                    }

                                    grenadeFarAway = vsize(Pawn.Location - theGrenade.Location);

                                    if (grenadeFarAway < grenadeSightDistance)
                                    {
                                                GoToState('chaseLight');
                                    }
                                    else
                                    {
                                                thePawn = Adv_CreaturePawn(Pawn);
                                                if(thePawn != none)
                                                {
                                                            thePawn.setPawnChase();
                                                }
                                                
                                                if(FindBestPathToward(Enemy, false, false))
                                                {
                                                            //Move along the path that was just calculated
                                                            MoveToward(MoveTarget,,,False);
                                                }
                                                else
                                                {
                                                            MoveToward(Enemy, , ,False);
                                                }
                                    }
                        }
                        else
                        {
                                    thePawn = Adv_CreaturePawn(Pawn);
                                    if(thePawn != none)
                                    {
                                                thePawn.setPawnChase();
                                    }
                                     
                                    if(FindBestPathToward(Enemy, false, false))
                                    {
                                                MoveToward(MoveTarget,,,False);
                                    }
                                    else
                                    {
                                                MoveToward(Enemy, , ,False);
                                    }
                        }
            }

            GoToState('Roaming');
}

defaultproperties
{
            searchInterval=0.3 //time before ai recalculates location
            roamingMesh=SkeletalMesh'adv_BatPackage.SkeletalMeshes_states.creature_roaming_SkelMesh'
         
            grenadeSightDistance=1500
            grenadeChaseMesh=SkeletalMesh'adv_BatPackage.SkeletalMeshes_states.creature_chaseGrenade_SkelMesh'
            
            pawnSightDistance=800
            pawnChasemesh=SkeletalMesh'adv_BatPackage.SkeletalMeshes_states.creature_chasePlayer_SkelMesh'

}
Powered by Create your own unique website with customizable templates.