By defining the value of boundary (default 10) you can choose how close to the boundary the player can get.
/*
Invisible Boundary
Author: Zecronious
Terms: Public Resource
*/
#define boundary 10
mob
var/turningBack = 0
Move()
// Is currently turning back
if(turningBack) return ..()
// In bounds
if(x <= boundary || y <= boundary)
turnAroundAndGoBack()
return 0
if(x >= world.maxx - boundary || y >= world.maxy - boundary)
turnAroundAndGoBack()
return 0
// Return the value of the original Move()
return ..()
proc
turnAroundAndGoBack()
dir = turn(dir, 180)
turningBack = 1
step(src, dir)
turningBack = 0
best to not modify what default procs return unless you have good reason.