Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ZG_Bot extends UDKBot;
- // This is a reference to the archetype that holds information about the properties of the bot.
- var() const ZG_Bot_Properties BotProperties;
- // True if the bot has just landed
- var ProtectedWrite bool bHasLanded;
- // This is the current enemy. This is acquired by either being hit by this pawn or having seen or heard this pawn
- var ProtectedWrite Pawn CurrentEnemy;
- // The deltatime for the AI main decision loop
- var const float UpdateDeltaTime;
- /**
- * This function is used to initialize the AIController upon the spawning of their pawn
- */
- function Initialize()
- {
- if(Pawn != none)
- {
- if(Pawn.Physics == PHYS_None)
- {
- Pawn.SetMovementPhysics();
- }
- if(Pawn.Physics == PHYS_Falling && DoWaitForLanding())
- {
- return;
- }
- }
- }
- event Possess(Pawn InPawn, bool bVehicleTransition)
- {
- Super.Possess(InPawn, bVehicleTransition);
- if(Pawn != none)
- {
- SetTimer(UpdateDeltaTime, true, NameOf(WhatToDo));
- }
- }
- function NotifyTakeHit(Controller InstigatedBy, vector HitLocation, int Damage, class<DamageType> damageType, vector Momentum)
- {
- if(InstigatedBy != none && InstigatedBy != self)
- {
- if( ShouldSetNewEnemy() && InstigatedBy.Pawn != none )
- {
- CurrentEnemy = InstigatedBy.Pawn;
- }
- }
- }
- /**
- * This function returns a vector which causes the pawn to walk towards the targetposition
- */
- function Vector Seek(Vector TargetPos)
- {
- local vector DesiredVelocity;
- if(Pawn != none)
- {
- DesiredVelocity = Normal(TargetPos - Pawn.Location) * 1000;
- return (DesiredVelocity - Pawn.Velocity);
- }
- return vect(0, 0, 0);
- }
- /**
- * This function returns a vector that causes the pawn to walk away from the target position
- */
- function vector Flee(Vector TargetPos)
- {
- local Vector DesiredVelocity;
- if(Pawn != none)
- {
- DesiredVelocity = Normal(Pawn.Location - TargetPos) * Pawn.GroundSpeed;
- return (DesiredVelocity - Pawn.Velocity);
- }
- return vect(0, 0, 0);
- }
- /**
- * Calculate the velocity needed to arrive at the target. Note that this makes the bot decelerate as it nears the target.
- */
- function vector Arrive(Vector TargetPos)
- {
- local Vector ToTarget, DesiredVelocity;
- local float Distance;
- local float Speed;
- if(Pawn != none)
- {
- ToTarget = TargetPos - Pawn.Location;
- Distance = vsize(ToTarget);
- if(Distance > 0)
- {
- // calculate speed required to reach the target given the deceleration
- Speed = Distance / (float(BotProperties.DecelerationType) + 1) * BotProperties.TweakDeceleration;
- // Make sure the velocity does not exceed the max
- Speed *= 8;
- Speed = Min(Speed, Pawn.GroundSpeed);
- DesiredVelocity = ToTarget * Speed / Distance;
- return (DesiredVelocity - Pawn.Velocity);
- }
- }
- return vect(0, 0, 0);
- }
- /**
- * This function evaluates whether this bot should set a new enemy.
- */
- function bool ShouldSetNewEnemy()
- {
- return true;
- }
- /**
- * If our pawn is falling, lets wait for it to land before doing anything else
- */
- function bool DoWaitForLanding()
- {
- GoToState('WaitingForLanding');
- return true;
- }
- /**
- * This is the entry point for AI decision making.
- */
- function WhatToDo()
- {
- if(Pawn == none)
- {
- return;
- }
- if(CurrentEnemy != none)
- {
- // If we are falling, wait until we are done
- if(Pawn.Physics == PHYS_Falling && DoWaitForLanding())
- {
- return;
- }
- else
- {
- Pawn.Velocity = Seek(CurrentEnemy.Location);
- Pawn.SetDesiredRotation( Rotator(Pawn.Velocity), false, true, BotProperties.RotationInterpTime );
- }
- }
- }
- /**
- * Display general debug information about the game
- */
- simulated function DisplayDebug(HUD HUD, out float out_YL, out float out_YPos)
- {
- local Canvas Canvas;
- Super.DisplayDebug(HUD, out_YL, out_YPos);
- Canvas = HUD.Canvas;
- Canvas.DrawText("Controller:" $Self);
- out_YPos += out_YL;
- Canvas.SetPos(4, out_YPos);
- Canvas.DrawText("Pawn:" $Pawn);
- out_YPos += out_YL;
- Canvas.SetPos(4, out_YPos);
- if(Pawn != none)
- {
- Canvas.DrawText("Velocity:" $Pawn.Velocity);
- out_YPos += out_YL;
- Canvas.SetPos(4, out_YPos);
- }
- Canvas.DrawText("State:" $GetStateName());
- out_YPos += out_YL;
- Canvas.SetPos(4, out_YPos);
- }
- /**
- * Notify the AIController that the pawn has been respawned. Called from ZG_GameInfo.
- */
- function NotifyRespawn();
- /**
- * In this state the pawn is falling. Wait for it's landing.
- */
- State WaitingForLanding
- {
- event LongFall()
- {
- RecoverFromLongFall();
- }
- event bool NotifyLanded(vector HitNormal, Actor FloorActor)
- {
- bHasLanded = true;
- return false;
- }
- function RecoverFromLongFall()
- {
- // Recover from a long fall here
- }
- event BeginState(Name PreviousStateName)
- {
- bHasLanded = false;
- }
- Begin:
- WaitForLanding();
- Sleep(0.5);
- if(bHasLanded)
- {
- // Just exit out of this state for now
- GoToState('');
- }
- GoTo('Begin');
- }
- defaultproperties
- {
- UpdateDeltaTime=0.05
- BotProperties=ZG_Bot_Properties'ZombieGameAssets.Bots.BotProperties'
- bIsPlayer=true
- }
Add Comment
Please, Sign In to add comment