ID:1155095
 
(See the best response by Cody123100.)
I tried using rand but it didn't work with view nor the M. Shown below

var/Z=rand(1,8)
M.loc= locate(M.Z,M.Z,M.z)


That dont work at all. This shown below work with the map view.

var/Z=rand(1,8)
M.loc= locate(Z,Z,M.z)


So how do i go about making M teleport to random spot thats in M view.

Best response
The preliminary information you need is the view distance. You can retrieve this from the client's view variable for player mobs and the world view variable for non-player mobs (or hardcode it in if your view distance isn't variable). First generate two random integers, A and B, from a range of -view distance to view distance. Next use the Move proc in conjunction with locate to move the mobs as so:

M.Move(locate(M.x+A, M.y+B, M.z))


The mob will move to the new location if movement is possible (i.e. the random location isn't dense or nonexistent or otherwise blocked). Otherwise, it will fail and no teleportation will occur. The simple workaround to this is to utilize Move's return value within a loop and keep generating A and B and then trying to Move until Move returns a value of 1. Here is a sample of the final product.

Teleport()
var/X = rand(-world.view,world.view)
var/Y = rand(-world.view,world.view)
while(!src.Move(locate(src.x+X, src.y+Y, src.z)))
X = rand(-world.view,world.view)
Y = rand(-world.view,world.view)
world << "Teleported to [src.x], [src.y]"
In response to Cody123100
Cody123100 wrote:
The mob will move to the new location if movement is possible (i.e. the random location isn't dense or nonexistent or otherwise blocked). Otherwise, it will fail and no teleportation will occur. The simple workaround to this is to utilize Move's return value within a loop and keep generating A and B and then trying to Move until Move returns a value of 1. Here is a sample of the final product.

> Teleport()
> var/X = rand(-world.view,world.view)
> var/Y = rand(-world.view,world.view)
> while(!src.Move(locate(src.x+X, src.y+Y, src.z)))
> X = rand(-world.view,world.view)
> Y = rand(-world.view,world.view)
> world << "Teleported to [src.x], [src.y]"
>


Okay, I didnt understand so i do what i always do and search DM Ref of what you showed me. So i can understand. Then i tried it out. For moment it had my eyes crazy because I use it as proc and it spam it and had me teleporting all over the map. Then just add few adjustment added a return so it wouldn't continue and its perfect. Thank you for help.