ID:2221844
 
(See the best response by Lummox JR.)

Problem description:How would I code the ability for a mob to walk into a turf only from certain directions and make so a mob can only exit that same turf from certain directions?


You'd need to override Enter() on that turf.

turf/directional
var/list/passable
Enter(mob/M)
if(ismob(M))
if(passable)
var/pass_direction = get_dir(src,M)
if((pass_direction in passable)) return 1
return 0


Then you'd set the passable list to a list of directions like list(NORTH,SOUTH,WEST)
Bitflags would probably be better.
Ah yeah, wow. Toothache is sending my brain back to 2001.
Could you expand about what you mean by Bitflags? Or at least, how you suggest to use bitflags in this situation?
In response to DarkLordZargog
DarkLordZargog wrote:
Could you expand about what you mean by Bitflags? Or at least, how you suggest to use bitflags in this situation?

Instead of a list variable containing the allowed directions, you'd use a variable with key flags toggled on:

// allow /turf/example turfs to only allow entrance from the north side
turf/example
var/allowed_directions = (NORTH | NORTHEAST | NORTHWEST)


Checking would look like:

if(some_movable.dir & allowed_directions)
Could you put that in a format as if you were writing it out for a turf? Just so I can see it put together into a code. I only say that because when I put it together it still didn't register what i'm trying to get it to do, so maybe my formatting was incorrect in this sense.
In response to FKI
Best response
NORTHEAST is NORTH | EAST, so NORTH | NORTHEAST | NORTHWEST is the same as NORTHEAST | WEST.

If you want to include diagonals in the list of allowed entrances, a different way of handling bitflags is called for. That's beyond the scope of this post.

For the purposes of this post, I assume using just the cardinal directions is sufficient. In which case:

turf/directional
var/passable = 255 // all possible dirs by default
Enter(atom/movable/A)
var/d = get_dir(src,A)
if((d & passable) == d) return ..()
return 0

The == d check is because if you allow north but not west, then entry from the northwest should be forbidden.
Thanks for all you help, everyone!
If I wanted to replicate that same effect but for Exit() what would I need to call to get_dir?
In response to DarkLordZargog
Exit() (and Enter()) has a second argument, which refers to the "new loc" that the mover is trying to move to.
turf/directional
var exitable = ~0 // all possible dirs by default

Exit(atom/movable/mover, atom/new_loc)
var d = get_dir(src, new_loc)
return (d & exitable) == d && ..()
Now I have one last question: How would I call it so even if the direction isn't passable, you could still pass through it if your density = 0.
Just include a density check at the beginning. If density is 0 (use the ! operator) return 1.
It would be helpful if you could write out what you mean by that. Code-wise that is. The main reason I say that is because i've tried that before and it didn't do what I need it to do.