Announcement

Collapse
No announcement yet.

[Code] Flying Fighting AI + GameType + Stuff

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [Code] Flying Fighting AI + GameType + Stuff

    ey up

    Heres another simple(ish) AI for you to play with.
    its for a helicopter game ive started making, so its meant for a flying enemy, but it should work with any pawn.
    it tries to go behind the player pawn while shooting at it, if the player gets too close.

    im going a different route with my ai as it gets more complex, using states (thanks everyone on that thread)
    so instead of letting this one dissapear into my hard drive i thaught id give it away.
    replace 'PlPawn' with whatever class your player pawn is using.
    ere
    Code:
    class AIHeli1 extends GameAIController;
    
    var vector MyTarget;
    
    simulated event PostBeginPlay()
    {
    	super.PostBeginPlay();
    
       SetTimer(0.5, true, 'BrainTimer');
    }
    
    function BrainTimer()
    {
       local PlPawn P; //your player pawn class
       local float Distance;
       local vector X,Y,Z;
       local Actor	HitActor;
       local vector HitLoc, HitNormal;
    
        foreach WorldInfo.AllPawns(class'PlPawn', P)
    	{
              if (P != None)   //if theres a player pawn
              {
                //get the distance
                Distance = VSize2D(Pawn.Location - P.Location);
    
                  //if its close
                if (Distance <= 2000)
                {
                   if (CanSee(P))
                   {
                      //look at it and shoot it
                      Pawn.SetDesiredRotation(Rotator(Normal(P.Location)));
                        Pawn.StartFire(0);
                   }
                   else
                   {
                      Pawn.StopFire(0);
                   }
                   //
                   P.GetAxes(Rotation, X, Y, Z);
                    //go behind the player
                   MyTarget = P.Location - 500 * X;
                   //trace to see if theres an obstruction
                   HitActor = Trace(HitLoc, HitNormal, MyTarget, Pawn.Location);
    
                   if (HitActor != None)  // if we will hit something
                   {
                       if (PlPawn(HitActor) != None)  //if its plpawn we hit (your player pawn)
                       {
    
                       GoToState('GetIt');
                       }
                       else     //something else we hit
                       {
                         Return;
                       }
                   }
                   else   //nothing in the way
                   {
    
                    GoToState('GetIt');
                   }
    
                }
                else  //stay where you are
                {
                 Pawn.StopFire(0);
                 MyTarget = Pawn.Location;
                 MyTarget.Z = Pawn.Location.Z;
    
                 GoToState('GetIt');
                }
              }
            }
    }
    
    state GetIt
    {
    
    Begin:
    
    
        MoveTo(MyTarget);
    
    }
    
    defaultproperties
    {
    
    }
    have fun

    edit:
    other stuff below

    #2
    Nice. Will use.

    Comment


      #3
      heres a gametype and enemy pawn to help some of you out.
      this will spawn bots inside my Plug n Play Helicopter at special spawnpoints you place on your map (HeliSpawnPoint).
      please let me know if i missed anything and/or you get errors.

      HeliSpawnPoint.uc
      Code:
      // Spawn actors without using navigation points
      //=============================================================================
      class HeliSpawnPoint extends Actor
      	hidecategories(Lighting,LightColor,Force)
      	placeable;
      
      var	CylinderComponent		CylinderComponent;
      
      /** Normal editor sprite */
      var const transient SpriteComponent GoodSprite;
      /** Used to draw bad collision intersection in editor */
      var const transient SpriteComponent BadSprite;
      
      defaultproperties
      {
      	Begin Object Class=SpriteComponent Name=Sprite
      		Sprite=Texture2D'EditorResources.S_KVehFact'
      		HiddenGame=true
      		HiddenEditor=false
      		AlwaysLoadOnClient=False
      		AlwaysLoadOnServer=False
      	End Object
      	Components.Add(Sprite)
      	GoodSprite=Sprite
      
      	Begin Object Class=SpriteComponent Name=Sprite2
      		Sprite=Texture2D'EditorResources.Bad'
      		HiddenGame=true
      		HiddenEditor=true
      		AlwaysLoadOnClient=False
      		AlwaysLoadOnServer=False
      		Scale=0.25
      	End Object
      	Components.Add(Sprite2)
      	BadSprite=Sprite2
      
      	Begin Object Class=ArrowComponent Name=Arrow
      		ArrowColor=(R=150,G=200,B=255)
      		ArrowSize=1.5
      		bTreatAsASprite=True
      		HiddenGame=true
      		AlwaysLoadOnClient=False
      		AlwaysLoadOnServer=False
      	End Object
      	Components.Add(Arrow)
      
      	Begin Object Class=CylinderComponent Name=CollisionCylinder //LegacyClassName=NavigationPoint_NavigationPointCylinderComponent_Class
      		CollisionRadius=+0050.000000
      		CollisionHeight=+0050.000000
      	End Object
      	CollisionComponent=CollisionCylinder
      	CylinderComponent=CollisionCylinder
      	Components.Add(CollisionCylinder)
      
      
      	bCollideWhenPlacing=true
      
      	bCollideActors=false
      }
      HeliGame.uc
      Code:
      class HeliGame extends UTGame;
      
      /**
        * Allows overriding of which gameinfo class to use.
        * Called on the DefaultGameType from the ini, or the one specified on the command line (?game=xxx)
        */
      static event class<GameInfo> SetGameType(string MapName, string Options, string Portal)
      {
      
      	return default.class;
      }
      
      simulated function PostBeginPlay()
      {
                Super.PostBeginPlay();
      
                SpawnTheBots();
      }
      
      exec function SpawnTheBots()
      {
         local vector L;
         local Rotator R;
         local HeliSpawnPoint S;
         local HeliPawn P;
         local AIHeli1 HC;
      
         foreach WorldInfo.AllActors(class'HeliSpawnPoint', S)
      	{
              L = S.Location;
              R = S.Rotation;
              //spawn the heli
              P = Spawn(class'HeliPawn',,,L, R);
              //spawn the controller and possess pawn
              HC = Spawn(class'AIHeli1',,,L, R);
              HC.Possess(P, false);
          }
      
      }
      defaultproperties
      {
        DefaultPawnClass=class'PlPawn'
      }
      HeliPawn.uc
      Code:
      class HeliPawn extends TegVehicle_Heli_Content
         placeable;
      
      simulated function PostBeginPlay()
      {
      	super.PostBeginPlay();
      }
      
      function bool DriverEnter(Pawn P);
      function DriverLeft();
      function bool FindAutoExit(Pawn ExitingDriver);
      function DriverRadiusDamage( float DamageAmount, float DamageRadius, Controller EventInstigator, class<DamageType> DamageType, float Momentum, vector HitLocation, Actor DamageCauser, optional float DamageFalloffExponent=1.f);
      simulated function bool DisableVehicle();
      simulated function EnableVehicle();
      simulated event StartDriving(Vehicle V);
      function AdjustDriverDamage(out int Damage, Controller InstigatedBy, Vector HitLocation, out Vector Momentum, class<DamageType> DamageType);
      function DriverDied(class<DamageType> DamageType);
      function NotifyDriverTakeHit(Controller InstigatedBy, vector HitLocation, int Damage, class<DamageType> DamageType, vector Momentum);
      function float GetDamageScaling()
      {
      	return Super(Pawn).GetDamageScaling();
      }
      
      function SpawnDefaultController()
      {
          super.SpawnDefaultController();
      }
      
      function bool Died(Controller Killer, class<DamageType> DamageType, vector HitLocation)
      {
      	return Super(Pawn).Died(Killer, DamageType, HitLocation);
      }
      
      function bool AnySeatAvailable()
      {
      	return false;
      }
      
      function bool HasOccupiedTurret()
      {
      	return true;
      }
      
      simulated function Destroyed()
      {
      	Super(Pawn).Destroyed();
      }
      
      function bool IsDriverSeat(Vehicle TestSeatPawn)
      {
      	return true;
      }
      
      simulated function SetInputs(float InForward, float InStrafe, float InUp)
      {
      	Throttle = InForward;
      	Steering = InStrafe;
      	Rise = InUp;
      	//ClientMessage("Throttle:"@Throttle@" Steering:"@Steering@" Rise:"@Rise);
      }
      
      simulated event TakeDamage(int Damage, Controller EventInstigator, vector HitLocation, vector Momentum, class<DamageType> DamageType, optional TraceHitInfo HitInfo, optional Actor DamageCauser)
      {
      	Super(Pawn).TakeDamage(Damage, EventInstigator, HitLocation, Momentum, DamageType, HitInfo, DamageCauser);
      }
      
      simulated function TakeRadiusDamage(Controller InstigatedBy, float BaseDamage, float DamageRadius, class<DamageType> DamageType, float Momentum, vector HurtOrigin, bool	 bFullDamage, Actor DamageCauser, optional float DamageFalloffExponent=1.f)
      {
      	Super(Pawn).TakeRadiusDamage(InstigatedBy, BaseDamage, DamageRadius, DamageType, Momentum, HurtOrigin, bFullDamage, DamageCauser, DamageFalloffExponent);
      }
      
      function PossessedBy(Controller C, bool bVehicleTransition)
      {
      	//ClientMessage("PossessedBy()  Controller ="@C);
      	Driver = self;
      	SetDriving(true);
      	Super.PossessedBy(C, bVehicleTransition);
      }
      
      simulated event Tick( float DeltaTime )
      {
      	//ClientMessage("CurrentState = "$GetStateName()$", Controller.State = "$Controller.GetStateName());
      	Super.Tick(DeltaTime);
      }
      
      defaultproperties
      {
      	bAttachDriver=false
      	bDriverIsVisible=false
      	InventoryManagerClass=class'HeliInventoryManager'
      	ControllerClass=class'AIHeli1'
      }
      HeliInventoryManager.uc
      Code:
         class HeliInventoryManager extends UTInventoryManager;
      
      simulated function DiscardInventory()
      {
      	local Vehicle V;
      
      	if (Role == ROLE_Authority)
      	{
      		Super(InventoryManager).DiscardInventory();
      
      		V = Vehicle(Owner);
      		// HACK to fix ininite loop - Driver will = V for default player pawn!!
      		if (V != None && V.Driver != None && V.Driver.InvManager != None && V.Driver != V)
      		{
      			V.Driver.InvManager.DiscardInventory();
      		}
      	}
      }
      EDIT:
      added the player pawn below and changed HeliGame to use it by default.

      PlPawn.uc
      Code:
      class PlPawn extends UTPawn;

      Comment


        #4
        Wow, I don't have use for this but I read over it anyways, and just wanted to say thanx for sharing this with everybody, I'm sure many will learn from it, as well as the other things you have shared with everybody.

        Comment


          #5
          MUCH NEEDED! I've been looking for an aircraft AI and class for quite some time now for two different projects and this may be exactly what I've been looking for. I'll definitely let you know how it works out for me as I test it out over the next several weeks. Thanks!!

          Comment


            #6
            This is awesome!!!!!!!!!!!! I was just asking around about how to do this. Does it chase the player as well? I was wondering how, if it's even possible, you get a bot to chase the player.

            Comment


              #7
              yes it chases the player.
              it uses a ForEach to go through the pawns in the level, if it finds one it looks at it, shoots it and tries to move to a position behind the player.

              local PlPawn P; //your player pawn class
              foreach WorldInfo.AllPawns(class'PlPawn', P)

              these lines dafine the player pawn it will look for, my player pawn at the time was called PlPawn.

              edit:
              ive added PlPawn to the code above so you can use the scripts without having to modify anything.

              Comment


                #8
                Nice one Tegleg thanks for sharing.

                Comment


                  #9
                  I have a Airplane mesh that I would like to add the AI script above to. How do I do this? I am having trouble finding clear tutorials on how to create custom AI to custom meshes.

                  Comment


                    #10
                    this is the ai script
                    Code:
                    class AIHeli1 extends GameAIController;
                    this is the vehicle
                    Code:
                    class HeliPawn extends TegVehicle_Heli_Content
                    its mesh is defined in TegVehicle_Heli_Content from the plug and play helicopter

                    the controller and pawn are Spawned() in the gameinfo, HeliGame
                    Code:
                    exec function SpawnTheBots()
                    basicaly, the AI or Controller, posseses a Pawn. a vehicle is also a pawn.
                    here the pawn is given a vehicle in its inventory
                    there are several ways of possesing a pawn or vehicle, look at the race starter gem on udn for another.

                    let me know if you need anything explaining more

                    Comment


                      #11
                      Thank you. I think I fallow. I will give it a shot and let you know. I am familiar with Kismet but not UnrealScript.

                      Comment


                        #12
                        I got it working somewhat. The Helicopter is shooting at me but not flying or moving for that matter. Just like you did with the Plug n Play Helicopter can you upload it again but with the AI setup as well? Good work by the way!

                        Comment


                          #13
                          thanks

                          it is all set up by combining the plug and play content with the scripts on here, i made it that way.
                          parhaps the bot is unable to move to its target vector for some reason.

                          this bit makes it go 500 uu's behind the player pawn.
                          Code:
                          //go behind the player
                                         MyTarget = P.Location - 500 * X;
                          maybe try changing the number to something smaller.

                          Comment


                            #14
                            Thanks man gonna check this out when I got the time.

                            Comment


                              #15
                              I'm trying to get the AI to attack each other. They can't seem to take off normally.

                              Code:
                              foreach WorldInfo.AllPawns(class'Light_Interceptor_Virus_AI_Pawn', P)
                              I also have a few questions as I can't seem to find it inside the scripts
                              1. Is there a var that sets the max height the chopper can go ?
                              2. Is there a var that sets the height when the chopper goes idle ?
                              3. I can't seem to find any indications of path finding ?


                              Hope someone can help me out with the above questions, other than that great work and thanks for sharing tegleg

                              Comment

                              Working...
                              X