CodeBase - ai codes
Return to the CodeBase listing
Category: Miscellaneous
Version: 1.0
Information
Uploaded: 20th Nov 2004 05:57
Modified: 20th Nov 2004 06:00
Author: dared
Summary
you can use this code to make co-op games if you use it correct
Full Description
rem shoot back<br /> Make Object Sphere 102,2<br /> texture object 102,2<br /> Hide Object 102<br /> <br /> This code is added before the main loop, creating the sphere for the Monsters projectile. Notice we are using texture 2 for the object texture. This is the same texture we used for the player projectile and effects. Re-using textures will economize the amount of memory your program uses.<br /> <br /> rem Make Monster Explosion<br /> Make Object Sphere 130,20<br /> texture object 130,2<br /> ghost object on 130<br /> Hide Object 130<br /> Make Object Sphere 131,20<br /> texture object 131,2<br /> ghost object on 131<br /> Hide Object 131<br /> <br /> This code is also added before the main loop. It creates the explosion objects for the monsters projectile.<br /> <br /> rem load Monsterparticles<br /> Load Image "fire.bmp",2<br /> For x =100 to 110<br /> Make object plain x+10,5,5<br /> Texture object x+10,2<br /> Set object x+10,1,0,0<br /> Ghost object on x+10<br /> Hide object x+10<br /> Next x<br /> MPn=110<br /> <br /> You may recognize this code snippet from the players particle effect. The code is the same only the object numbers have been changed. We have initialized the monster particle counter, "MPn" to 110, the number of the first object in the particle series.<br /> <br /> Load 3Dsound "fireball2.wav",102<br /> Load 3Dsound "Explode.wav",103<br /> <br /> This code was added to load the sound effects for the monster projectile. Notice, we did not re-use the sounds for the player effects. We created new sounds because the player sounds and the monster sounds may be playing in the same instance.<br /> <br /> If MonsterBulletLife > 0 then Gosub MonsterShootBullet<br /> If MonsterExplode > 0<br /> Gosub MonsterExplodeRocket<br /> else<br /> Gosub MonsterAI<br /> endif<br /> <br /> This code is in our main loop. Here we check for the monster bullet life and the monster explosion life, and run the respective subroutines much like those used by the player. The addition is the "MonsterAI" subroutine. We don't want the monster trying to shoot a new bullet while the old one is exploding. We are using the "If Then Else" comparison to make sure this doesn't happen.<br /> <br /> Rem Simple AI for guided monster missile<br /> MonsterAI:<br /> <br /> Point object 3,X#,Y#,Z#<br /> PDist=Sqrt((mX# - X#)^2 + (mY#+25 - Y#)^2 + (mZ# - Z#)^2)<br /> if PDist<1500<br /> if MonsterBulletLife=0 or MonsterBulletLife < 500-PDist/10<br /> Point object 3,X#,Y#-25,Z#<br /> Position object 102,mX#,mY#+25,mZ#<br /> Set object to object orientation 102,3<br /> MonsterBulletLife =500<br /> show object 102<br /> Loop sound 102<br /> Endif<br /> <br /> endif<br /> Return<br /> <br /> This is the "MonsterAI" subroutine. When this subroutine is called the monster object is pointed directly at the players. The distance between the monster and the player is calculated. If the distance is less than 1500 then the code continues. If the player is out of range, nothing happens and the subroutine returns to the main loop. If the player is in range the code checks to see if the bullet is still alive or if the bullet has missed the player. The way we check to see if the bullet has missed the player is through the use of this piece of code "500-PDist/10". The monsters bullet travels 12 world units each time it moves. It has a life of 500, so the bullet can tavel a distance of 6000 units. If we divide the distance, or the value of the "PDist" variable by 12 we get the number of life units the monsters bullet has traveled towards the player.<br /> <br /> If the distance were 1200 the life units from the monster would be 100. If we subtract this value from the total number of life units we get 400 if this number is greater than the value in the "MonsterBulletLife" variable then we know the bullet has missed the target. Instead of 10 we use 12 to divide the distance, this allows the bullet to travel a little further than it should before another bullet can be shot. It adds more to the effect of the bullet missing the player. If the "MonsterBulletLife" value passes the test for another shot to be fired we point the monster directly at the players position. We position and orient the bullet object to the monster. We set the bullet life to 500, show the projectile and begin the bullet sound.<br /> <br /> Rem Shoot Monster bullet<br /> MonsterShootBullet:<br /> <br /> Dec MonsterBulletLife<br /> Move object 102,12<br /> MbX#=Object position X(102)<br /> MbY#=Object position Y(102)<br /> MbZ#=Object position Z(102)<br /> inc MPn<br /> if MPn=121 then MPn=110<br /> Scale object MPn,100,100,100<br /> Position object MPn,MbX#,MbY#,MbZ#<br /> Position sound 102,MbX#,MbY#,MbZ#<br /> point object MPn,X#,Y#,Z#<br /> Zrotate object MPn,rnd(180)<br /> Show object MPn<br /> for x = 1 to 10<br /> scale object int((Wrapvalue((MPn-9+x)*36))/36)+110,100+x*25,100+x*25,100+x*25<br /> next x<br /> if MbY# < Get Ground height(1,MbX#,MbZ#) then MonsterBulletLife=0<br /> Pdist=Sqrt((X# - MbX#)^2 + (Y#+25 - MbY#)^2 + (Z# - MbZ#)^2)<br /> if Pdist<50<br /> GoSub PlacePlayer<br /> MonsterBulletLife = 0<br /> endif<br /> <br /> Rem guided missile<br /> if Pdist <500 and Pdist>250 then Point object 102,X#,Y#,Z#<br /> if Pdist < 100 then point object 102,X#,Y#,Z#<br /> if MonsterBulletLife = 0<br /> Hide object 102<br /> stop sound 102<br /> for x=110 to 120<br /> hide object x<br /> next x<br /> MonsterExplode = 20<br /> endif<br /> <br /> Return<br /> <br /> The "MonsterShootBullet" subroutine is much like our "ShootBullet" Subroutine, but we've added a few lines to make the monster bullet a guided missile. This creates game play and a greater challenge for the player when dodging the monsters projectile.<br /> <br /> Rem guided missile<br /> if Pdist <500 and Pdist>250 then Point object 102,X#,Y#,Z#<br /> if Pdist < 100 then point object 102,X#,Y#,Z#<br /> <br /> This is the code for the guided missile. If the bullet gets in a range between 500 and 250 world units of the player, the bullet points itself at the player. If the bullet is in the range of 100 units the bullet again points itself at the player. This gives the player a little time to try and dodge the bullet.<br /> <br /> Rem Explode monster bullet<br /> MonsterExplodeRocket:<br /> Position object 130,MbX#,MbY#,MbZ#<br /> Show object 130<br /> Position object 131,MbX#,MbY#,MbZ#<br /> Show object 131<br /> EScale=20*(30-MonsterExplode)<br /> Scale object 130,EScale,EScale,EScale<br /> Yrotate object 130,WrapValue(MonsterExplode*37)<br /> Scale object 131,EScale/1.5,EScale/1.5,EScale/1.5<br /> Yrotate object 131,WrapValue(359-MonsterExplode*37)<br /> Dec MonsterExplode<br /> If MonsterExplode = 0 then hide object 130: Hide object 131<br /> If MonsterExplode=18<br /> position sound 103,X#,Y#,Z#<br /> play sound 103<br /> endif<br /> If MonsterExplode < 15 then position sound 103,X#,Y#,Z#<br /> Return<br /> <br /> <br /> rem hunt<br /> <br /> Rem Load Target<br /> Load object "idle.x",3<br /> append Object "Walk.x",3,21<br /> if DecoCollide(MbX#,MbY#,MbZ#) = 1<br /> MonsterBulletLife = 0<br /> BulletAvoidDeco = BulletAvoidDeco + 2<br /> Endif<br /> <br /> if MbY# < Get Ground height(1,MbX#,MbZ#)<br /> MonsterBulletLife=0<br /> ShootUp=3<br /> endif<br /> Rem Simple AI for guided monster missile<br /> MonsterAI:<br /> Point object 3,X#,Y#,Z#<br /> If AvoidDeco > 0<br /> mA# = Object Angle Y(3)<br /> Dec AvoidDeco<br /> Yrotate object 3,WrapValue(mA#+AvoidDeco*60)<br /> endif<br /> <br /> Rem Position Monster at new location<br /> Position Object 3,mX#,mY#,mZ#<br /> <br /> Rem check distance from player<br /> PDist=Sqrt((mX# - X#)^2 + (mY#+25 - Y#)^2 + (mZ# - Z#)^2)<br /> <br /> Rem If the player is within range shoot bullet<br /> if PDist<1500<br /> if MonsterBulletLife=0 or MonsterBulletLife < 500-Pdist/10<br /> Point object 3,X#,Y#-25,Z#<br /> If BulletAvoidDeco > 0<br /> CornerAim = Rnd(1)<br /> mA# = object angle Y(3)<br /> if CornerAim = 0 then Yrotate Object 3,WrapValue(mA#+BulletAvoidDeco*10)<br /> if CornerAim = 1 then Yrotate Object 3,WrapValue(mA#+BulletAvoidDeco*-16)<br /> Dec BulletAvoidDeco<br /> Endif<br /> <br /> If ShootUp > 0<br /> mA# = object angle X(3)<br /> XRotate Object 3,WrapValue(mA#+ShootUp*-8)<br /> Dec ShootUp<br /> Endif<br /> <br /> Position object 102,mX#,mY#+25,mZ#<br /> Set object to object orientation 102,3<br /> MonsterBulletLife =500<br /> show object 102<br /> Loop sound 102<br /> <br /> Rem Play idle animation<br /> Loop Object 3,0,20<br /> Endif<br /> Endif<br /> <br /> if PDist>1000<br /> Rem Store old location<br /> OldmX# = mX#<br /> OldmZ# = mZ#<br /> OldmY# = mY#<br /> <br /> Rem Play walking animation<br /> Loop Object 3,21,46<br /> <br /> Rem Move monster<br /> Move Object 3,7<br /> <br /> Rem Get new position<br /> mX# = Object Position X(3)<br /> mZ# = Object Position Z(3)<br /> mY# = Get Ground Height(1,mX#,mZ#)<br /> <br /> Rem Check for Decoration collision<br /> If DecoCollide(mX#,mY#,mZ#) = 1 and AvoidDeco = 0<br /> mX# = OldmX#<br /> mZ# = OldmZ#<br /> mY# = OldmY#<br /> AvoidDeco = 3<br /> Endif<br /> Endif<br /> <br /> Return<br /> <br /> <br />
Comments
No comments yet.