I got a basic AI script from here:
http://forum.thegamecreators.com/?m=forum_view&t=63411&b=7
It works and all that, but the only problem is collision. I have set up a collision system for the player, but when I attempt my current collision code I use for the player, it simply locks up the game.
`Setup for performance
AI_Setup:
`Amount of enemies - change to how many enemies you want
enemies=10
`*Arrays* - you need to know these!!!!
`-----------------------------------------
`-------------------------------------------
Dim enemy_state(enemies) :`This makes an array for the state of every enemy
`Say you wanted enemy No 5 to attack you - this is what you'd do:
`Enemy_state(5)=2
`If you want enemy number 10 to attack you:
`Enemy_state(10)=2
`Then say enemy 10 goes back to wandering around
`Enemy_state(10)=1
`Here's how it goes: Enemy_state(enemy No.)=current state
`Initial state for the enemies
for e=1 to enemies
enemy_state(e)=1
next e
`!!!^^READ ALL THAT STUFF UP THERE^^!!!
Dim enemy_dist#(enemies) :`Here, just like above, we make an array for the distance between
`you and every enemy. The array doesn't know yet that it gets the distance between you and the
`enemy, so we'll do that later
`Types: I'll explain after I've done it
Type enemy
x as float
y as float
z as float
endtype
Dim enemy_position(enemies) as enemy
`Randomise positions
for e=1 to enemies
enemy_position(e).x=rnd(100)
enemy_position(e).z=rnd(100)
next e
Dim enemy_angle(enemies) :`Array for the enemies' angles
`Create the enemies
for e=1 to enemies
make object cube 300+e,10
color object 300+e,rgb(255,0,0)
next e
return
`Main Loop
AI_loop:
`**********AI**********
`Select all enemies
for e=1 to enemies
`Check the distance between you and the enmies
enemy_dist#(e)=sqrt((x#-enemy_position(e).x)^2+(y#-enemy_position(e).y)^2+(z#-enemy_position(e).z)^2)
`If enemy state is 1 then make the angle change (so the enemy can wander)
if enemy_state(e)=1
enemy_angle(e)=wrapvalue(enemy_angle(e)+rnd(3))
endif
`If the enemy state is 2 then point to player
if enemy_state(e)=2
enemy_angle(e)=atanfull(x#-enemy_position(e).x,z#-enemy_position(e).z)
endif
`Always move enemy
enemy_position(e).x=newxvalue(enemy_position(e).x,enemy_angle(e),1.0)
enemy_position(e).z=newzvalue(enemy_position(e).z,enemy_angle(e),1.0)
`Change state depending on distance
if enemy_dist#(e)<1500 then enemy_state(e)=2 else enemy_state(e)=1
<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>
!!!<<<I INSERT THE COLLISION CODE HERE>>>!!!
<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>
`Update enemies
enemy_position(e).y=250
position object 300+e,enemy_position(e).x,enemy_position(e).y,enemy_position(e).z
yrotate object 300+e,enemy_angle(e)
`If you get hit then respawn
if enemy_dist#(e)<140 then x#=rnd(10000) : z#=rnd(10000)
`deselect enemies
next e
return
And here's the collision code I use for the point where it says I insert it.
enemy_oldx# = object position x(e)
enemy_oldy# = object position y(e)
enemy_oldz# = object position z(e)
rem apply gravity, and user changes to movement
enemy_angy# = object angle y(e)
enemy_angx# = object angle x(e)
enemy_angz# = object angle z(e)
enemy_vx# = 0
enemy_vz# = 0
enemy_x# = enemy_oldx#+enemy_vx#
enemy_y# = enemy_oldy#+enemy_vy#
enemy_z# = enemy_oldz#+enemy_vz#
if enemy_collide>0
enemy_ny# = sc_getCollisionNormalY()
if abs(enemy_ny#)>slope#
rem FLAT, stick
enemy_oldy# = sc_getStaticCollisionY()
else
rem STEEP, slide
enemy_x# = enemy_x# - enemy_oldx# : enemy_z# = enemy_z# - enemy_oldz#
enemy_oldx# = sc_getCollisionSlideX()
enemy_oldy# = sc_getCollisionSlideY()
enemy_oldz# = sc_getCollisionSlideZ()
enemy_x# = enemy_x# + enemy_oldx# : enemy_z# = enemy_z# + enemy_oldz#
endif
if enemy_ny#>slope#
rem only on ground if standing on flat ground
enemy_ground = 1
enemy_vy# = 0
else
ground = 0
rem if player has hit a flat ceiling then stop vy# movement
if enemy_ny#<-slope# then enemy_vy# = gravity#
endif
else
rem nothing below player, not on ground, add vertical speed to player
oldy# = oldy# + vy#
enemy_ground = 0
endif
enemy_collide = sc_SphereSlideGroup(1,enemy_oldx#,enemy_oldy#,enemy_oldz#,enemy_x#,enemy_oldy#,enemy_z#,radius#/0.5,0)
if enemy_collide>0
rem if hit, reposition player, halt movement vector
enemy_x# = sc_getCollisionSlideX()
enemy_oldy# = sc_getCollisionSlideY()
enemy_z# = sc_getCollisionSlideZ()
enemy_vx# = 0
enemy_vz# = 0
endif
rem position the player
position object 300+e,enemy_x#,enemy_oldy#,enemy_z#
sc_updateObject 300+e
remend
I've been great working on the game, but the AI and collision never really ceased to confuse me.
I'm probably using a wrong collision system for the AI, fixes are welcome.
Also, yeah, I'm using Sparky's Collision DLL. That information might help.