I am making a First Person Shooter and I wrote a basic code for the thugs using Pythagoras' theorem to detect if you are within a certain distance from them:
rem -----------------------------thug1 AI---------------------------
if object exist(31)=1
if SQRT(((ABS(ThugPos#(1,1)-X#))^2)+((ABS(ThugPos#(1,3)-Z#))^2))<=(guardlimit(1)+Susp#(1))
Susp#(1)=10
position object 33,ThugPos#(1,1),ThugPos#(1,2),ThugPos#(1,3)
show object 33
hide object 31
point object 33,X#,Y#,Z#
yrotate object 33,wrapvalue(object angle y(33)+180)
if object playing(33)=0
play object 33
endif
if object frame(33)>=10.8 and object frame(33)<=11.2
if ThugBulletLife#(1)=0
Position object 34,ThugPos#(1,1),ThugPos#(1,2)+9,ThugPos#(1,3)
Set object to object orientation 34,33
yrotate object 34,wrapvalue(object angle y(34)+180)
ThugBulletLife#(1) =35
Show object 34
play sound 5
endif
endif
endif
if SQRT(((ABS(ThugPos#(1,1)-X#))^2)+((ABS(ThugPos#(1,3)-Z#))^2))>(guardlimit(1)+Susp#(1))
if object visible(33)=1
hide object 33
show object 31
if object playing(31)=0 then play object 31
stop object 33
endif
If Susp#(1)>0 then Susp#(1)=Susp#(1)-0.01
If Susp#(1)<0 then Susp#(1)=0
endif
endif
rem -----------------------------thug2 AI---------------------------
if object exist(36)=1
if object playing(36)=0 then play object 36
if SQRT(((ABS(169-X#))^2)+((ABS(209-Z#))^2))<=(guardlimit(2)+Susp#(2))
Susp#(2)=10
show object 38
hide object 36
point object 38,X#,Y#,Z#
yrotate object 38,wrapvalue(object angle y(38)+180)
if object playing(38)=0
play object 38
endif
if object frame(38)>=10.8 and object frame(38)<=11.2
if ThugBulletLife#(2)=0
Position object 39,169,9,209
Set object to object orientation 39,38
yrotate object 39,wrapvalue(object angle y(39)+180)
ThugBulletLife#(2) =35
Show object 39
play sound 5
endif
endif
endif
if SQRT(((ABS(169-X#))^2)+((ABS(209-Z#))^2))>(guardlimit(2)+Susp#(2))
if object visible(38)=1
hide object 38
show object 36
stop object 38
endif
If Susp#(2)>0 then Susp#(2)=Susp#(2)-0.01
If Susp#(2)<0 then Susp#(2)=0
endif
endif
rem -----------------------------thug3 AI---------------------------
if object exist(41)=1
if SQRT(((ABS(ThugPos#(3,1)-X#))^2)+((ABS(ThugPos#(3,3)-Z#))^2))<=(guardlimit(3)+Susp#(3))
Susp#(3)=10
position object 43,ThugPos#(3,1),ThugPos#(3,2),ThugPos#(3,3)
show object 43
hide object 41
point object 43,X#,Y#,Z#
yrotate object 43,wrapvalue(object angle y(43)+180)
if object playing(43)=0
play object 43
endif
if object frame(43)>=10.8 and object frame(43)<=11.2
if ThugBulletLife#(3)=0
Position object 44,ThugPos#(3,1),ThugPos#(3,2)+9,ThugPos#(3,3)
Set object to object orientation 44,43
yrotate object 44,wrapvalue(object angle y(44)+180)
ThugBulletLife#(3) =35
Show object 44
play sound 5
endif
endif
endif
if SQRT(((ABS(ThugPos#(3,1)-X#))^2)+((ABS(ThugPos#(3,3)-Z#))^2))>(guardlimit(3)+Susp#(3))
if object visible(43)=1
hide object 43
show object 41
if object playing(41)=0 then play object 41
stop object 43
endif
If Susp#(3)>0 then Susp#(3)=Susp#(3)-0.01
If Susp#(3)<0 then Susp#(3)=0
endif
endif
When I try and create a function for this, however, it doesn't do anything. This is my function:
function thugai(t)
rem -----------------------------thug AI---------------------------
if object exist((t*5)+26)=1
if SQRT(((ABS(ThugPos#(t,1)-X#))^2)+((ABS(ThugPos#(t,3)-Z#))^2))<=(guardlimit(t)+Susp#(t))
Susp#(t)=10
position object (t*5)+28,ThugPos#(t,1),ThugPos#(t,2),ThugPos#(t,3)
show object (t*5)+28
hide object (t*5)+26
point object (t*5)+28,X#,Y#,Z#
yrotate object 33,wrapvalue(object angle y((t*5)+28)+180)
if object playing((t*5)+28)=0
play object (t*5)+28
endif
if object frame((t*5)+28)>=10.8 and object frame((t*5)+28)<=11.2
if ThugBulletLife#(t)=0
Position object (t*5)+29,ThugPos#(t,1),ThugPos#(t,2)+9,ThugPos#(t,3)
Set object to object orientation (t*5)+29,(t*5)+28
yrotate object (t*5)+29,wrapvalue(object angle y((t*5)+29)+180)
ThugBulletLife#(t) =35
Show object (t*5)+29
play sound 5
endif
endif
endif
if SQRT(((ABS(ThugPos#(t,1)-X#))^2)+((ABS(ThugPos#(t,3)-Z#))^2))>(guardlimit(t)+Susp#(t))
if object visible((t*5)+28)=1
hide object (t*5)+28
show object (t*5)+26
if object playing((t*5)+26)=0 then play object (t*5)+26
stop object (t*5)+28
endif
If Susp#(t)>0 then Susp#(t)=Susp#(1)-0.01
If Susp#(t)<0 then Susp#(t)=0
endif
endif
endfunction
I am calling this function using:
for t=1 to thugno
thugai(t)
next t
<!--EDIT-->
object no's are all between 36-45
thugno = 3
Thugpos#(t,1),Thugpos#(t,2) and Thugpos#(t,3) store the x, y and z positions of the object (t*5)+26 respectively
guardlimit(t) is the natural alertness of thug t
susp#(t) is the suspicion of thug t (If he notices you this will go up, but then it will decrease again gradually as he doesnt see you
The models are being rotated by 180 degrees because I need to do that for all the guard models I'm using (DM1 - thug)
Am I the only one here who's really confused?
http://db1games.topcities.com
(used to be demonsbreath)