ID:166700
 
I have an AI system setup for my mob, but I dont want the mob to walk all over the world. I want the mob to stay in an area i set up. How can i set it up so that the mob can walk randomly in an area without going out of bounds?

Example:

10
9
8 ________
7 |...........|
6 |.....M....|
5 |.M........|
4 |...........|
3
2
1
1 2 3 4 5 6 7 8 9 10

10 x 10 is the world, the square box is the area I want the mob to move randomly in, M is the mob. Please help me get started on this code. Thanks in advance.
Add a monstor block o.o
Without testing this, I couldn't be sure if it would work but you could create a literal Area for him and have it deny exit when he tries to wander out.
area/Mob_Wander
Exit(atom/movable/Obj)
if(Obj.type in typesof([your mob's type..]))
return 0
else
return ..()

Hell, you could even define a list of mob types for each area so you don't have to hardcode it each time.
I'm not sure what you mean when you say the word 'area'. Do you mean the atomic object /area? Or do you mean area in the general sense, like region? From the content of your post I'm guessing you mean the later. Keeping an /atom/movable (like a mob or obj) from leaving a region can be done with /area objects by returning FALSE when they ask to exit the area:
area
Exit(var/atom/movable/what)
if(mob cannot exit)
return FALSE
else
.=..()

Or, we could modify the /atom/movable's movment proc so as to keep it in the region:
region
var
min_x as num
max_x as num
min_y as num
min_y as num
New(var/mob/center, var/radius as num)
min_x = center.x - radius
max_x = center.x + radius
min_y = center.y - radius
max_y = center.y + radius
proc
is_contained(var/turf/test_loc)
. = TRUE
if(test_loc.x > max_x || test_loc.x < min_x)
return FALSE
if(test_loc.y > max_y || test_loc.y < min_y)
return FALSE

monster
var
radius = 3
region/region
New()
.=..()
region = new(src, radius)
Move(var/atom/new_loc)
if(region.is_contained(new_loc))
.=..()

Personally, I prefer the second method, as it leaves /area objects open for better uses.
In response to IainPeregrine
this is an interesing problem as I will eventually attack the same type of problem, my npc must stay in their respective areas but the other types of mobs may pass.
ie the banker stays in his bank, the toyshop keeper stays in the toyshop, but other mobs and npcs may go anywhere they like.
In response to IainPeregrine
IainPeregrine wrote:
> monster
> var
> radius = 3
> region/region
> New()
> .=..()
> region = new(src, radius)
> Move(var/atom/new_loc)
> if(region.is_contained(new_loc))
> .=..()
>


I keep getting an error that is_contained is a undefined proc. on the line "if(region.is_contained(new_loc))" I've tryed (region/is_contained(new_loc)) and (region/is_contained()) if you have any ideas please let me know. Thanks
In response to Ssdave
To be honest, that whole region object was a very poor choice on my part. It would have been easier to save a central turf and check if the new location was in range(center_turf, radius).
monster
var
radius = 3
turf/center
New()
.=..()
center = loc
Move(var/atom/new_loc)
if(new_loc in range(center, radius))
.=..()
In response to IainPeregrine
I got the mob to stop when they reach the limit of there area but they dont move again after that. How can I make it so they mob goes back to the center then walk rand again from there?