hi, i'm trying to make a game where NPCs roam aimlessly in the playing area until an enemy is within its range. right now i'm trying to prevent them from going out of bounds and that's where i'm stumped. i already have a walking and turning controller for them, i'll put them after the code that i need help with, which is this:
`if current point will eventually go out of bounds
if OutOfBounds(newxvalue(NPCdata#(x,1),NPCdata#(x,7)-180,3),newzvalue(NPCdata#(x,3),NPCdata#(x,7)-180,3)) = 1
if NPCdata#(x,7)< 90 and NPCdata#(x,7) > 0
testAngle# = NPCdata#(x,7)
NPCdata#(x,7) = wrapvalue(NPCdata#(x,7) - (testAngle#*2))
goto _fix
endif
if NPCdata#(x,7)< 180 and NPCdata#(x,7) > 90
testAngle# = 180 - NPCdata#(x,7)
NPCdata#(x,7) = wrapvalue(NPCdata#(x,7) + (testAngle#*2))
goto _fix
endif
if NPCdata#(x,7)< 270 and NPCdata#(x,7) > 180
testAngle# = NPCdata#(x,7) - 180
NPCdata#(x,7) = wrapvalue(NPCdata#(x,7) - (testAngle#*2))
goto _fix
endif
if NPCdata#(x,7)< 360 and NPCdata#(x,7) > 270
testAngle# = 360 - NPCdata#(x,7)
NPCdata#(x,7) = wrapvalue(NPCdata#(x,7) + (testAngle#*2))
goto _fix
endif
if NPCdata#(x,7) = 0 or NPCdata#(x,7) = 90 or NPCdata#(x,7) = 180 or NPCdata#(x,7) = 270 or NPCdata#(x,7) = 360
NPCdata#(x,7) = wrapvalue(NPCdata#(x,7) + 180)
endif
_fix:
NPCdata#(x,2) = 0 - rnd(30) - 120
NPCdata#(x,8) = 30 + rnd(30)
endif
this is the turning and walking controller:
`if distance to target less than 2, choose another target
if NPCdata#(x,8) < 2
`get distance and angle
NPCdata#(x,8) = 30 + rnd(30)
NPCdata#(x,7) = wrapvalue(oaY# + (rnd(90) - 45))
endif
`walking controller
if NPCdata#(x,2) > 0
set object frame (10 * x + 100),0
NPCdata#(x,2) = rnd(50)
endif
`if he is allowed to walk
if NPCdata#(x,2) = 0
`set duration for walking (a negative value so it would be a different case)
NPCdata#(x,2) = 0 - rnd(30) - 120
dec NPCdata#(x,8),0.5
endif
if NPCdata#(x,2) < 0
if NPCdata#(x,8) > 0
inc NPCdata#(x,5)
if NPCdata#(x,5) > 39 then NPCdata#(x,5) = 1
set object frame 10 * x + 100,NPCdata#(x,5)
xTestN# = newxvalue(NPCdata#(x,1),NPCdata#(x,7)-180,1.7)
zTestN# = newzvalue(NPCdata#(x,3),NPCdata#(x,7)-180,1.7)
position object 10 * x + 100,xTestN#,get ground height(1,xTestN#,zTestN#)+24,zTestN#
yrotate object 10 * x + 100, curveangle(NPCdata#(x,7),oaY#,5)
endif
dec NPCdata#(x,8),0.5
inc NPCdata#(x,2),2
endif
i have no problems with the walking and turning controller, except that it lets the NPCs go out of the playing area. here's a legend on what the various variables mean:
OutOfBounds(x#,z#) = this function checks if the point is out of bounds
NPCdata#(x,2) = walking controller for the current NPC
NPCdata#(x,7) = angle of the current NPC
NPCdata#(x,8) = turning controller for the current NPC
NPCdata#(x,9) = x position of target
NPCdata#(x,10) = z position of target
NPCdata# is an array. i use 5 NPCs and control them with the values stored in the array.
in the first code, if the current position will go out of bounds, it will try to find the how far from the horizontal is the current angle in. then it adds twice of the result to the original angle and makes it walk. it should look like it bounced off the edge of the playing area but the NPCs just go straight through.
what should i do? i'm really out of ideas