Here are the directions:
//Directions
#define N 1 //North
#define S 2 //South
#define E 4 //East
#define W 8 //West
#define NE 16 //Northeast
#define SE 32 //Southeast
#define SW 64 //Southwest
#define NW 128 //Northwest
And here's the autojoining code itself:
//This is the flag for the cardinal directions (NORTH|SOUTH|EAST|WEST).
#define CARDINAL 15
//This is the list of directions to be considered. The text values are
//the default BYOND flag, and the numeric values are my values.
var/list/joins = list("1" = 1, "5" = 16, "4" = 4, "6" = 32, "2" = 2, "10" = 64, "8" = 8, "9" = 128)
turf
var
//Autojoin is a boolean variable to determine if the turf should
//autojoin or not. autojoin_state is the state that it should
//join as. Its default is 47 for 47-state joining, but it can
//also be set to 16 to work for 16-state autojoining.
autojoin = 0
autojoin_state = 47
//join_flag is the variable to hold the flag for the state.
//state_a and state_b are for preceding and following text for
//the icon states (the icon state is, in the end, set to
//"[state_a][join_flag][state_b]").
join_flag = 0
state_a
state_b
//This is a boolean variable to determine if nonexistant turfs
//(i.e. turfs outside of the map) should be considered to be
//connected or not. If the variable is false, they won't be
//connected; if it's true, they will.
nonexistant_connect = 0
New()
if(autojoin) autojoin()
..()
proc/autojoin()
//This is the counter for the directions list. By default it is
//set to eight, so it counts the whole list. The reason for its
//existence is explained below.
var/c = 8
//This is the amount to increment by. If autojoin_state is 47,
//it increments by one. If it's 16, it increments by 2.
//Otherwise it is set to zero.
var/i = (autojoin_state == 47 ? (1) : (autojoin_state == 16 ? (2) : (0)))
for(var/a = 1, i && a <= c , a += i)
var/turf/t = get_step(src, text2num(joins[a]))
if(t && istype(t, type) || nonexistant_connect && !t) join_flag |= joins[joins[a]]
else if(autojoin_state == 47 && text2num(joins[joins[a]]) & CARDINAL)
//For 47-state joining, filled inside corners are only
//made if the cardinal values of the diagonal state are
//both turned on. If the cardinal value isn't filled,
//then the join flag for the previous state (the
//diagonal state) is turned off. For NORTH, this can't
//work, because it is the first in the list. Instead,
//one is subtracted from c so the NORTHWEST flag is
//never reached.
if(a - 1 > 0) join_flag &= ~joins[joins[a - 1]]
else c --
//This allows a skip ahead to the next cardinal. The
//following diagonal must be skipped so it won't be
//turned on because one of its cardinals has already
//been shown to not exist.
a ++
icon_state = "[state_a][join_flag][state_b]"