ID:159239
 
Can any one direct me to a tutorial or give some advice? I've been looking for a tutorial for awhile now.

I want to be able to set up an area that makes it so any NPCs in this area cannot leave. This has to apply to mobs created in the area in the first place also.

I need it so that NPC's stay in houses and monsters stay in certain areas and don't drift off.
You could do a number of things there, set an area like this:
area/NPC
Exit(mob/M)
if(ismob(M)&&M.NPC) return
..()

Or setup a turf that can't be entered by an NPC, and just make a box around where you want the area to be.
turf/dense
Enter(mob/M)
if(ismob(M)&&M.NPC) return
..()


(Note: Neither are tested, though should work)
In response to Bakasensei
Well, you came close. :P Enter() and Exit()'s job is to determine whether or not to allow the movement by returning true or false; the default action of them doesn't actually affect anything, but only decides whether or not the movement should be allowed according to density and returns 1 or 0 accordingly. So if you don't pass on the return value their parent proc returns, calling it has no effect whatsoever, same thing as if you put a line of just rand(1,2) - it calculates a value and returns it, but it isn't used and is lost. The overrides you've posted would always return null, which is false, so all applicable movement attempts are denied. The way to work with these should be to return 0/false if you want the movement to be denied, and otherwise return the parent proc so any other parent/default movement rules are 'applied' and will determine if the movement succeeds or not.
Misc note: I wouldn't go in favor of using vars like mob.NPC to determine if a mob is an NPC one; that seems a fairly extraneous var. I usually have it set up so I can do something like if(istype(Mob,/mob/NPC)), but otherwise you can still check the key var to see if the mob is a player mob (or of course the client to see if a player is currently playing as that mob).
Misc note #2: As one may want to have a specific turf/area (instead of the NPC-blocking one) in that spot and you can't have 2 in the same spot, one may wish to use a different method of flagging a turf/area as NPC-blocking, such as with a simple variable on that atom.

Also, while this method would work to keep the NPCs in their designated area, the best way to go about such a thing is to remove the "problem" at the source: modify the NPCs AI code to never even attempt to move if the destination is a disallowed one, and that way Move() calls are spared and the NPC can decide to do something else instead, which is probably more productive than wasting time attempting a movement that will never succeed. For example, if the NPC's current target has moved beyond the NPC's allowed perimeter, switch to attacking it with ranged attacks (if applicable) instead of chasing, otherwise give up chasing (and try to find and attack a different target in the allowed area, when applicable).
This way of thinking can apply to mostly everything the AI can be denied from doing; if you don't root it out at the source, the result will be that instead of doing more desirable things (like in the example above), the NPC will try (usually over and over) to do the forbidden action and fail (unlike say a smart person or player that would realize he can't do that and try doing something else). This can lead to weaker and exploitable AI, so it should be avoided, as it is still easy to do anyway.

EDIT: I'll include a small example for the sake of the OP, to make this wall of text somewhat easier to grasp. Let's say that 'before', your AI code looked something like this (psuedo-code, illustration-only):
mob/monster/proc/AI()
...
if(src.target && src.target in oview(src))
if(get_dist(src,src.target) <= 1)
src.Attack(src.target)
else
step_to(src,src.target)
else
//get new target in visible range here
...

'After' [you change it so the NPC doesn't try to go into a forbidden area], it'd look like this:
mob/monster/proc/AI()
...
if(src.target && src.target in oview(src))
if(get_dist(src,src.target) <= 1)
src.Attack(src.target)
else
var/T = get_step_to(src,src.target)
if(istype(T,/turf/no_monster)) //if the path to the target is a forbidden turf
src.target = null //abandon chasing this target (again, may wish to see if using ranged attack is possible first, if available)
else src.Move(T) //if it's not forbidden, attempt to move there
if(!src.target) //if we had no target or we abandoned it
//get new target in visible range here
...
In response to Kaioken
Ah that is what I was missing, I knew something was wrong with it. Anyways, I do prefer variables as it is easier to hold a group of mobs instead of multiple uses of istype().
http://www.byond.com/developer/FlameSage/FS_Walkback

i think you might be able to use this in some smart way.. making it so if it doesnt have a target it walks back to its original position(created position)
Alright.....I finally had time to come back and go over this. I relooked at all you guys wrote and well...its not really what I'm looking to do. Maybe it would be easier if I just showed the code I have for areas?

area
cavearea
opacity=1
cavebosscontain
Exited(mob/M)
if(ismob(M))
if(!M.client)
var/lx = M.x
M.x=lx+1


I want to just set it up so the area handles if the NPCs can enter or not. Not have it so the AI handles it

EDIT:
Also, other NPCs keep using portals only for clients. I can't figure out why. Here's the code.
    entercave
Entered(mob/M)
if(ismob(M))
if(M.client)
M.loc=locate(90,1,2)
else
var/ly=M.y
M.y=ly-1
exitcave
Entered(mob/M)
if(ismob(M))
if(M.client)
M.loc=locate(90,4,1)
else
var/ly=M.y
M.y=ly-1
In response to 8BitGeek
8BitGeek wrote:
I want to be able to set up an area that makes it so any NPCs in this area cannot leave.

Although Bakasensei's code had an error in it, it was still (aside from mentioned error) exactly what you asked for.
area/NPCRestrictedExit
Exit(mob/M)
if(ismob(M) && !M.client)
return 0
return ..()

Taking what you said strictly and exactly as you put it, it is as simple as that code snippet. Any mob without an attached client controlling it will be denied exit, and everything else will be treated normally.

If you want it to be smarter, then what Kaioken said will benefit you, as it was basically just a suggestion to do the same thing but handled from the mob's point of view so that the mob can take its own special action on entry-failure.
In response to Loduwijk
ahhh i see now, thank you =)