ID:164106
 
I wrote an autojoining system using BYOND's default NORTH, SOUTH, EAST, and WEST flags, as well as my own flags for the non-cardinal directions, and it's currently working. What I'm wondering is if anyone could devise a way to optimize the code. I tried to come up with some sort of way to do it via bitwise operators, but couldn't.

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]"
If you could explin a little bit more (im not good with byod tech lingo) i could give it a shot i think
sounds interesting PIF, your making something similar to my map making kit i suppose, although probably more of a library than a game specific demo. Good luck on it.
In response to Masterdan
No. I'm making 47-state autojoining because it's far superior to handpicking states and placing them down. The autojoining, likely, won't be in the final version of the game, though. It can cause too much lag; I'll simply make the map and then save it with pif_MapLoader, and use that as the map, with the states already selected.
In response to Lt. Pain
It's not just BYOND technical terms. It's technical terms for programming in general. If you didn't understand what I was saying (and I was, really, being rather clear), you're probably not going to be able to help.

If that sounded condescending, sorry. It wasn't supposed to be, but as I'm rereading it, it sounds like it in my head.
In response to Popisfizzy
No its alright i get you, sure im crushed inside but ill get over it :'(

XD jk