TGC Codebase Backup



Zombie AI Code by Mobiius

12th Feb 2005 17:06
Summary

AI Source code for simple zombie AI. They walk around randomly and if they get near to you, they come for you! Oh yeah, to change the amount of AI's on screen, change the value hel



Description

AI Source code for simple zombie AI. They walk around randomly and if they get near to you, they come for you! Thats about it. Move the cursor to the left / right edge of te screen to turn, LMB shoots!



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    set display mode 800,600,32

sync on
sync rate 30
autocam off
randomize timer()
hide mouse

Backdrop On
Color Backdrop 0
load sound "rifle.wav", 1

Global CrosshairImage = 1
Global CrosshairSprite = 1
Global YourPlayer = 1
Global NumberOfAIs = 10
`=============================================================================================Feel Free To Remove These Lines.
Global MatrixNumber = 1
Global MatrixImage = 2
Dim RandomObjects(NumberOfAIs)
`============================================================================================================================
Dim AIBaseX#(NumberOfAIs)
Dim AIBaseY#(NumberOfAIs)
Dim AIBaseZ#(NumberOfAIs)
Dim AIAlive(NumberOfAIs)
Dim AiDoSomthing#(NumberOfAIs)
Dim AIYAngle(NumberOfAIs)
Dim AIYAngle#(NumberOfAIs)
Dim AIDistanceFromHome#(NumberOfAIs)
Dim AIDistanceFromYou#(NumberOfAIs)

ink rgb(255, 0, 0), 0

line 15, 0, 15, 10
line 15, 20, 15, 32
line 0, 15, 10, 15
line 20, 15, 32, 15
Get Image CrosshairImage, 0, 0, 32, 32, 1


Sprite CrosshairSprite, MouseX(), MouseY(), CrosshairImage
Offset Sprite CrosshairSprite, 16, 16


Make Object Cube YourPlayer, 5
hide object 1
Position Object YourPlayer, 0, 3, 0

`=============================================================================================Feel Free To Remove These Lines.

Create Bitmap 1, 64, 64
Set Current Bitmap 1
Ink Rgb(64, 255, 64), Rgb(64, 255, 64)
Box 0, 0, 64, 64
For A = 1 to 1000
   Dot Rnd(64), Rnd(64), Rgb(Int(Rnd(192)), Int(Rnd(192)), Int(Rnd(192)))
Next A
Blur Bitmap 1, 10
Get Image MatrixImage, 0, 0, 64, 64
Delete Bitmap 1

Make Matrix MatrixNumber, 2000, 2000, 60, 60
Position Matrix MatrixNumber, -1000, 0, -1000

Prepare Matrix Texture MatrixNumber, MatrixImage, 1, 1




Randomize Timer()
For Id = 1 To NumberOfAIs
   RandomObjects(Id) = Id + 1
   Load Object "pong.x" ,RandomObjects(Id)
   Scale Object RandomObjects(Id),1000,1000,1000
   YRotate Object RandomObjects(Id), 180
   Fix Object Pivot RandomObjects(Id)
   AIBaseX#(Id) = Int(Rnd(200) - 100)
   AIBaseY#(Id) = 5
   AIBaseZ#(Id) = Int(Rnd(200) - 100)
   Position Object RandomObjects(Id), AIBaseX#(Id), AIBaseY#(Id), AIBaseZ#(Id)

   AIYAngle(Id) = Int(Rnd(359))
   AIYAngle#(Id) = AIYAngle(Id)
   YRotate Object RandomObjects(Id), AIYAngle(Id)
   AIAlive(Id) = 1
Next Id

ShotFlag = 0

Do
   P# = Pick Object(MouseX(), MouseY(), 2, NumberOfAIs + 1)

`---------------------------------------------------------------------------------------------------------------------------
`------------------------------------------------------------------------------------------------ Start Of The Enemy AI Code
`---------------------------------------------------------------------------------------------------------------------------
   For Object = 1 To NumberOfAIs
      AI_X# = Object Position X(RandomObjects(Object)):` Gets The Ememys X Position
      AI_Y# = Object Position Y(RandomObjects(Object)):` Gets The Ememys Y Position
      AI_Z# = Object Position Z(RandomObjects(Object)):` Gets The Ememys Z Position
      Dist_X# = AIBaseX#(Object) - AI_X#:` Gets The X Distance From The Enemys Start Position To Its Current Position
      Dist_Y# = AIBaseY#(Object) - AI_Y#:` Gets The Y Distance From The Enemys Start Position To Its Current Position
      Dist_Z# = AIBaseZ#(Object) - AI_Z#:` Gets The Z Distance From The Enemys Start Position To Its Current Position
      `Gets The Actual Distance From The Enemys Start Position To Its Current Position
      AIDistanceFromHome#(Object) = Sqrt(Dist_X# * Dist_X# + Dist_Y# * Dist_Y# + Dist_Z# * Dist_Z#)

      Your_X# = Object Position X(1):` Gets Your X Position
      Your_Y# = Object Position Y(1):` Gets Your Y Position
      Your_Z# = Object Position Z(1):` Gets Your Z Position
      Dist_X# = Your_X# - AI_X#:` Gets The X Distance From The Enemys Current Position To Your Current Position
      Dist_Y# = Your_Y# - AI_Y#:` Gets The Y Distance From The Enemys Current Position To Your Current Position
      Dist_Z# = Your_Z# - AI_Z#:` Gets The Z Distance From The Enemys Current Position To Your Current Position
      `Gets The Actual Distance From The Enemys Start Position To Your Current Position
      AIDistanceFromYou#(Object) = Sqrt(Dist_X# * Dist_X# + Dist_Y# * Dist_Y# + Dist_Z# * Dist_Z#)

      If AIDistanceFromYou#(Object) >= 10
         If AIAlive(Object) = 1
            Move Object RandomObjects(Object), 0.5
            Loop Object RandomObjects(Object),2,18
         EndIf
      Else
         Move Object RandomObjects(Object), 0
         If AIAlive(Object) = 1 Then Stop Object RandomObjects(Object)
         Make Object Plain 1000, 1, 1:` Make A Dummy Object
         Position Object 1000, Object Position X(RandomObjects(Object)), Object Position Y(RandomObjects(Object)), Object Position Z(RandomObjects(Object)):` Place It Where The Enemy Is
         Point Object 1000, Object Position X(1), Object Position Y(1), Object Position Z(1):` Point The Enemy At You

         AIYAngle(Object) = WrapValue(Object Angle Y(1000)):` Make The Enemy Turn Towards You Slowly
         Delete Object 1000:` Deletes The Dummy Object
      EndIf


      If AIDistanceFromHome#(Object) >= 100:`If That Distance Is Too Big, Then Make The Enemy Look At The Start Position.
                             `                  (Stops The Enemy Wandering Too Far Away!)
         Make Object Plain 1000, 1, 1:` Make A Dummy Object
         Position Object 1000, Object Position X(RandomObjects(Object)), Object Position Y(RandomObjects(Object)), Object Position Z(RandomObjects(Object)):` Place It Where he Enemy Is
         Point Object 1000, AIBaseX#(Object), AIBaseY#(Object), AIBaseZ#(Object):` Point It At The Start Position

         AIYAngle(Object) = WrapValue(Object Angle Y(1000)):` Make The Enemy Turn Towards The Start Position Slowly
         Delete Object 1000:` Deletes The Dummy Object
      EndIf


      If AIDistanceFromHome#(Object) >= 150:`If That Distance Is Too Big, Then Make The Enemy Look At The Start Position.
                             `                  (Stops The Enemy Wandering Too Far Away!)
         Make Object Plain 1000, 1, 1:` Make A Dummy Object
         Position Object 1000, Object Position X(RandomObjects(Object)), Object Position Y(RandomObjects(Object)), Object Position Z(RandomObjects(Object)):` Place It Where he Enemy Is
         Point Object 1000, AIBaseX#(Object), AIBaseY#(Object), AIBaseZ#(Object):` Point It At The Start Position

         AIYAngle(Object) = WrapValue(Object Angle Y(1000)):` Make The Enemy Turn Towards The Start Position Slowly
         AIYAngle#(Object) = WrapValue(Object Angle Y(1000)):` Make The Enemy Turn Towards The Start Position Slowly
         Delete Object 1000:` Deletes The Dummy Object
      EndIf


      If AiDoSomthing#(Object) <= Timer():` If A Certain Time Has Elapsed, Then Tell The Enemy To Do Somthing.
         Randomize Timer():` Makes The Randum Number More Random
         AIYAngle(Object) = WrapValue(Object Angle Y(RandomObjects(Object)) + (Rnd(180)-90)):` Picks A Random Direction For The Enemy
         AiDoSomthing#(Object) = (Timer() + 2500):` Resets The Timer
      EndIf


      AIYAngle#(Object) = CurveValue(AIYAngle(Object), AIYAngle#(Object), 40)
      If AIAlive(Object) = 1 Then YRotate Object RandomObjects(Object), AIYAngle#(Object)

      Text 0, 10 * Object, Str$(AIDistanceFromHome#(Object))
   Next Object

`---------------------------------------------------------------------------------------------------------------------------
`-------------------------------------------------------------------------------------------------- End Of The Enemy AI Code
`---------------------------------------------------------------------------------------------------------------------------

` Hit the fricken dudet code
````````````````````````````````````````````````````````````````````
   If P# <> 0
      P = Int(P#) - 1
      If MouseClick() <> 0 And AIAlive(P) = 1
         play object P#, 59,75: AIAlive(P) = 0
         Make Object Plain 1000, 1, 1:` Make A Dummy Object
         Position Object 1000, Object Position X(P#), Object Position Y(P#), Object Position Z(P#):` Place It Where The Enemy Is
         Point Object 1000, Camera Position X(), Camera Position Y(), Camera Position Z():` Point The Enemy At You

         P = int(P#)
         AIYAngle(P - 1) = WrapValue(Object Angle Y(1000)):` Make The Enemy Turn Towards You Slowly
         YRotate Object P#, AIYAngle(P - 1)
         Delete Object 1000:` Deletes The Dummy Object
      EndIf
   EndIf
   If MouseClick() <> 0 And ShotFlag = 0
      Play Sound 1
      ShotFlag = 2
   EndIf
   If MouseClick() = 0 And ShotFlag = 2 Then ShotFlag = 0

````````````````````````````````````````````````````````````````````


   If P# = 0
      Text 0, 0, "No Object Pointed At"
   Else
      Text 0, 0, "Object " + Str$(P#) + " Pointed At"
   EndIf

   If UpKey() = 1 Then Move Object YourPlayer, 1
   If DownKey() = 1 Then Move Object YourPlayer, -1

   If MouseX() < 20 Then YRotate Object YourPlayer, WrapValue(Object Angle Y(YourPlayer) - .75)
   If MouseX() > (Screen Width() - 20) Then YRotate Object YourPlayer, WrapValue(Object Angle Y(YourPlayer) + .75)
   If MouseX() < 10 Then YRotate Object YourPlayer, WrapValue(Object Angle Y(YourPlayer) - 1.5)
   If MouseX() > (Screen Width() - 10) Then YRotate Object YourPlayer, WrapValue(Object Angle Y(YourPlayer) + 1.5)

   Set Camera To Follow Object Position X(YourPlayer), Object Position Y(YourPlayer), Object Position Z(YourPlayer), Object Angle Y(YourPlayer), 30, 10, 1, 0
   Point Camera Object Position X(YourPlayer), Object Position Y(YourPlayer), Object Position Z(YourPlayer)

   Sprite CrosshairSprite, MouseX(), MouseY(), CrosshairImage
   Sync
Loop