TGC Codebase Backup



ai codes by dared

20th Nov 2004 5:57
Summary

you can use this code to make co-op games if you use it correct



Description

rem shoot back
Make Object Sphere 102,2
texture object 102,2
Hide Object 102

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.

rem Make Monster Explosion
Make Object Sphere 130,20
texture object 130,2
ghost object on 130
Hide Object 130
Make Object Sphere 131,20
texture object 131,2
ghost object on 131
Hide Object 131

This code is also added before the main loop. It creates the explosion objects for the monsters projectile.

rem load Monsterparticles
Load Image "fire.bmp",2
For x =100 to 110
Make object plain x+10,5,5
Texture object x+10,2
Set object x+10,1,0,0
Ghost object on x+10
Hide object x+10
Next x
MPn=110

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.

Load 3Dsound "fireball2.wav",102
Load 3Dsound "Explode.wav",103

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.

If MonsterBulletLife > 0 then Gosub MonsterShootBullet
If MonsterExplode > 0
Gosub MonsterExplodeRocket
else
Gosub MonsterAI
endif

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.

Rem Simple AI for guided monster missile
MonsterAI:

Point object 3,X#,Y#,Z#
PDist=Sqrt((mX# - X#)^2 + (mY#+25 - Y#)^2 + (mZ# - Z#)^2)
if PDist<1500
if MonsterBulletLife=0 or MonsterBulletLife < 500-PDist/10
Point object 3,X#,Y#-25,Z#
Position object 102,mX#,mY#+25,mZ#
Set object to object orientation 102,3
MonsterBulletLife =500
show object 102
Loop sound 102
Endif

endif
Return

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.

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.

Rem Shoot Monster bullet
MonsterShootBullet:

Dec MonsterBulletLife
Move object 102,12
MbX#=Object position X(102)
MbY#=Object position Y(102)
MbZ#=Object position Z(102)
inc MPn
if MPn=121 then MPn=110
Scale object MPn,100,100,100
Position object MPn,MbX#,MbY#,MbZ#
Position sound 102,MbX#,MbY#,MbZ#
point object MPn,X#,Y#,Z#
Zrotate object MPn,rnd(180)
Show object MPn
for x = 1 to 10
scale object int((Wrapvalue((MPn-9+x)*36))/36)+110,100+x*25,100+x*25,100+x*25
next x
if MbY# < Get Ground height(1,MbX#,MbZ#) then MonsterBulletLife=0
Pdist=Sqrt((X# - MbX#)^2 + (Y#+25 - MbY#)^2 + (Z# - MbZ#)^2)
if Pdist<50
GoSub PlacePlayer
MonsterBulletLife = 0
endif

Rem guided missile
if Pdist <500 and Pdist>250 then Point object 102,X#,Y#,Z#
if Pdist < 100 then point object 102,X#,Y#,Z#
if MonsterBulletLife = 0
Hide object 102
stop sound 102
for x=110 to 120
hide object x
next x
MonsterExplode = 20
endif

Return

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.

Rem guided missile
if Pdist <500 and Pdist>250 then Point object 102,X#,Y#,Z#
if Pdist < 100 then point object 102,X#,Y#,Z#

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.

Rem Explode monster bullet
MonsterExplodeRocket:
Position object 130,MbX#,MbY#,MbZ#
Show object 130
Position object 131,MbX#,MbY#,MbZ#
Show object 131
EScale=20*(30-MonsterExplode)
Scale object 130,EScale,EScale,EScale
Yrotate object 130,WrapValue(MonsterExplode*37)
Scale object 131,EScale/1.5,EScale/1.5,EScale/1.5
Yrotate object 131,WrapValue(359-MonsterExplode*37)
Dec MonsterExplode
If MonsterExplode = 0 then hide object 130: Hide object 131
If MonsterExplode=18
position sound 103,X#,Y#,Z#
play sound 103
endif
If MonsterExplode < 15 then position sound 103,X#,Y#,Z#
Return


rem hunt

Rem Load Target
Load object "idle.x",3
append Object "Walk.x",3,21
if DecoCollide(MbX#,MbY#,MbZ#) = 1
MonsterBulletLife = 0
BulletAvoidDeco = BulletAvoidDeco + 2
Endif

if MbY# < Get Ground height(1,MbX#,MbZ#)
MonsterBulletLife=0
ShootUp=3
endif
Rem Simple AI for guided monster missile
MonsterAI:
Point object 3,X#,Y#,Z#
If AvoidDeco > 0
mA# = Object Angle Y(3)
Dec AvoidDeco
Yrotate object 3,WrapValue(mA#+AvoidDeco*60)
endif

Rem Position Monster at new location
Position Object 3,mX#,mY#,mZ#

Rem check distance from player
PDist=Sqrt((mX# - X#)^2 + (mY#+25 - Y#)^2 + (mZ# - Z#)^2)

Rem If the player is within range shoot bullet
if PDist<1500
if MonsterBulletLife=0 or MonsterBulletLife < 500-Pdist/10
Point object 3,X#,Y#-25,Z#
If BulletAvoidDeco > 0
CornerAim = Rnd(1)
mA# = object angle Y(3)
if CornerAim = 0 then Yrotate Object 3,WrapValue(mA#+BulletAvoidDeco*10)
if CornerAim = 1 then Yrotate Object 3,WrapValue(mA#+BulletAvoidDeco*-16)
Dec BulletAvoidDeco
Endif

If ShootUp > 0
mA# = object angle X(3)
XRotate Object 3,WrapValue(mA#+ShootUp*-8)
Dec ShootUp
Endif

Position object 102,mX#,mY#+25,mZ#
Set object to object orientation 102,3
MonsterBulletLife =500
show object 102
Loop sound 102

Rem Play idle animation
Loop Object 3,0,20
Endif
Endif

if PDist>1000
Rem Store old location
OldmX# = mX#
OldmZ# = mZ#
OldmY# = mY#

Rem Play walking animation
Loop Object 3,21,46

Rem Move monster
Move Object 3,7

Rem Get new position
mX# = Object Position X(3)
mZ# = Object Position Z(3)
mY# = Get Ground Height(1,mX#,mZ#)

Rem Check for Decoration collision
If DecoCollide(mX#,mY#,mZ#) = 1 and AvoidDeco = 0
mX# = OldmX#
mZ# = OldmZ#
mY# = OldmY#
AvoidDeco = 3
Endif
Endif

Return




Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    rem shoot back
Make Object Sphere 102,2
texture object 102,2
Hide Object 102

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.

rem Make Monster Explosion
Make Object Sphere 130,20
texture object 130,2
ghost object on 130
Hide Object 130
Make Object Sphere 131,20
texture object 131,2
ghost object on 131
Hide Object 131

This code is also added before the main loop. It creates the explosion objects for the monsters projectile.

rem load Monsterparticles
Load Image "fire.bmp",2
For x =100 to 110
	Make object plain x+10,5,5
	Texture object x+10,2
	Set object x+10,1,0,0
	Ghost object on x+10
	Hide object x+10
Next x
MPn=110

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.

Load 3Dsound "fireball2.wav",102
Load 3Dsound "Explode.wav",103

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.

If MonsterBulletLife > 0 then Gosub MonsterShootBullet
If MonsterExplode > 0
	Gosub MonsterExplodeRocket
else
	Gosub MonsterAI
endif

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.

Rem Simple AI for guided monster missile
MonsterAI:

Point object 3,X#,Y#,Z#
PDist=Sqrt((mX# - X#)^2 + (mY#+25 - Y#)^2 + (mZ# - Z#)^2)
if PDist<1500
if MonsterBulletLife=0 or MonsterBulletLife < 500-PDist/10
	Point object 3,X#,Y#-25,Z#
	Position object 102,mX#,mY#+25,mZ#
	Set object to object orientation 102,3
	MonsterBulletLife =500
	show object 102
	Loop sound 102
Endif

endif
Return

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.

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.

Rem Shoot Monster bullet
MonsterShootBullet:

Dec MonsterBulletLife
Move object 102,12
MbX#=Object position X(102)
MbY#=Object position Y(102)
MbZ#=Object position Z(102)
inc MPn
if MPn=121 then MPn=110
Scale object MPn,100,100,100
Position object MPn,MbX#,MbY#,MbZ#
Position sound 102,MbX#,MbY#,MbZ#
point object MPn,X#,Y#,Z#
Zrotate object MPn,rnd(180)
Show object MPn
for x = 1 to 10
	scale object int((Wrapvalue((MPn-9+x)*36))/36)+110,100+x*25,100+x*25,100+x*25
next x
if MbY# < Get Ground height(1,MbX#,MbZ#) then MonsterBulletLife=0
Pdist=Sqrt((X# - MbX#)^2 + (Y#+25 - MbY#)^2 + (Z# - MbZ#)^2)
if Pdist<50
	GoSub PlacePlayer
	MonsterBulletLife = 0
endif

Rem guided missile
if Pdist <500 and Pdist>250 then Point object 102,X#,Y#,Z#
if Pdist < 100 then point object 102,X#,Y#,Z#
if MonsterBulletLife = 0
	Hide object 102
	stop sound 102
	for x=110 to 120
	hide object x
	next x
	MonsterExplode = 20
endif

Return

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.

Rem guided missile
if Pdist <500 and Pdist>250 then Point object 102,X#,Y#,Z#
if Pdist < 100 then point object 102,X#,Y#,Z#

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.

Rem Explode monster bullet
MonsterExplodeRocket:
Position object 130,MbX#,MbY#,MbZ#
Show object 130
Position object 131,MbX#,MbY#,MbZ#
Show object 131
EScale=20*(30-MonsterExplode)
Scale object 130,EScale,EScale,EScale
Yrotate object 130,WrapValue(MonsterExplode*37)
Scale object 131,EScale/1.5,EScale/1.5,EScale/1.5
Yrotate object 131,WrapValue(359-MonsterExplode*37)
Dec MonsterExplode
If MonsterExplode = 0 then hide object 130: Hide object 131
If MonsterExplode=18
position sound 103,X#,Y#,Z#
play sound 103
endif
If MonsterExplode < 15 then position sound 103,X#,Y#,Z#
Return


rem hunt

Rem Load Target
Load object "idle.x",3
append Object "Walk.x",3,21
if DecoCollide(MbX#,MbY#,MbZ#) = 1
	MonsterBulletLife = 0
	BulletAvoidDeco = BulletAvoidDeco + 2
Endif

if MbY# < Get Ground height(1,MbX#,MbZ#)
	MonsterBulletLife=0
	ShootUp=3
endif
Rem Simple AI for guided monster missile
MonsterAI:
Point object 3,X#,Y#,Z#
If AvoidDeco > 0
	mA# = Object Angle Y(3)
	Dec AvoidDeco
	Yrotate object 3,WrapValue(mA#+AvoidDeco*60)
endif

Rem Position Monster at new location
Position Object 3,mX#,mY#,mZ#

Rem check distance from player
PDist=Sqrt((mX# - X#)^2 + (mY#+25 - Y#)^2 + (mZ# - Z#)^2)

Rem If the player is within range shoot bullet
if PDist<1500
	if MonsterBulletLife=0 or MonsterBulletLife < 500-Pdist/10
		Point object 3,X#,Y#-25,Z#
		If BulletAvoidDeco > 0
			CornerAim = Rnd(1)
			mA# = object angle Y(3)
			if CornerAim = 0 then Yrotate Object 3,WrapValue(mA#+BulletAvoidDeco*10)
			if CornerAim = 1 then Yrotate Object 3,WrapValue(mA#+BulletAvoidDeco*-16)
			Dec BulletAvoidDeco
		Endif
		
		If ShootUp > 0
			mA# = object angle X(3)
			XRotate Object 3,WrapValue(mA#+ShootUp*-8)
			Dec ShootUp
		Endif
		
		Position object 102,mX#,mY#+25,mZ#
		Set object to object orientation 102,3
		MonsterBulletLife =500
		show object 102
		Loop sound 102
		
		Rem Play idle animation
		Loop Object 3,0,20
	Endif
Endif

if PDist>1000
	Rem Store old location
	OldmX# = mX#
	OldmZ# = mZ#
	OldmY# = mY#
	
	Rem Play walking animation
	Loop Object 3,21,46
	
	Rem Move monster
	Move Object 3,7
	
	Rem Get new position
	mX# = Object Position X(3)
	mZ# = Object Position Z(3)
	mY# = Get Ground Height(1,mX#,mZ#)
	
	Rem Check for Decoration collision
	If DecoCollide(mX#,mY#,mZ#) = 1 and AvoidDeco = 0
		mX# = OldmX#
		mZ# = OldmZ#
		mY# = OldmY#
		AvoidDeco = 3
	Endif
Endif

Return


rem tip: you can use this code to make co-op games.