anchor.diff:  		((hi3 >= 65 ? hi3-55 : hi3-48)<<4) | (lo3 >= 65 ? lo3-55 : lo3-48),
   		((hi4 >= 65 ? hi4-55 : hi4-48)<<4) | (lo4 >= 65 ? lo4-55 : lo4-48))
   
  -
  -/*
  -Get flat icon by DarkCampainger. As it says on the tin, will return an icon with all the overlays
  -as a single icon. Useful for when you want to manipulate an icon via the above as overlays are not normally included.
  -The _flatIcons list is a cache for generated icon files.
  -*/
  -
   // Creates a single icon from a given /atom or /image.  Only the first argument is required.
   /proc/getFlatIcon(image/A, defdir, deficon, defstate, defblend, start = TRUE, no_anim = FALSE)
  -	// We start with a blank canvas, otherwise some icon procs crash silently
  -	var/icon/flat = icon('icons/effects/effects.dmi', "nothing") // Final flattened icon
  -	if(!A)
  -		return flat
  -	if(A.alpha <= 0)
  -		return flat
  +	//Define... defines.
  +	var/static/icon/flat_template = icon('icons/effects/effects.dmi', "nothing")
  +
  +	#define BLANK icon(flat_template)
  +	#define SET_SELF(SETVAR) var/icon/SELF_ICON=icon(icon(curicon, curstate, base_icon_dir),"",SOUTH,no_anim?1:null);if(A.alpha<255)SELF_ICON.Blend(rgb(255,255,255,A.alpha),ICON_MULTIPLY);if(A.color)SELF_ICON.Blend(A.color,ICON_MULTIPLY);;##SETVAR=SELF_ICON;
  +
  +	#define INDEX_X_LOW 1
  +	#define INDEX_X_HIGH 2
  +	#define INDEX_Y_LOW 3
  +	#define INDEX_Y_HIGH 4
  +
  +	#define flatX1 flat_size[INDEX_X_LOW]
  +	#define flatX2 flat_size[INDEX_X_HIGH]
  +	#define flatY1 flat_size[INDEX_Y_LOW]
  +	#define flatY2 flat_size[INDEX_Y_HIGH]
  +	#define addX1 add_size[INDEX_X_LOW]
  +	#define addX2 add_size[INDEX_X_HIGH]
  +	#define addY1 add_size[INDEX_Y_LOW]
  +	#define addY2 add_size[INDEX_Y_HIGH]
  +
  +	if(!A || A.alpha <= 0)
  +		return BLANK
  +
   	var/noIcon = FALSE
  -
   	if(start)
   		if(!defdir)
   			defdir = A.dir
  
positive.code: 		if(!defblend)
  			defblend = A.blend_mode
  
  	var/curicon
  	if(A.icon)
  		curicon = A.icon
  	else
  		curicon = deficon
  
  	if(!curicon)
  		noIcon = TRUE // Do not render this object.
  
  	var/curstate
  	if(A.icon_state)
  		curstate = A.icon_state
  	else
  		curstate = defstate
  
  	if(!noIcon && !(curstate in icon_states(curicon)))
  		if("" in icon_states(curicon))
  			curstate = ""
  		else
  			noIcon = TRUE // Do not render this object.
  
  	var/curdir
  	var/base_icon_dir	//We'll use this to get the icon state to display if not null BUT NOT pass it to overlays as the dir we have
  	
  	//These should use the parent's direction (most likely)
  	if(!A.dir || A.dir == SOUTH)
  		curdir = defdir
  	else
  		curdir = A.dir
  
  	//Let's check if the icon actually contains any diagonals, just skip if it's south to save (lot of) time
  	if(curdir != SOUTH)
  		var/icon/test_icon 
  		var/directionals_exist = FALSE
  		var/list/dirs_to_check = GLOB.cardinals - SOUTH
  		outer:
  			for(var/possible_dir in dirs_to_check)
  				test_icon = icon(curicon,curstate,possible_dir,frame=1)
  				for(var/x in 1 to world.icon_size)
  					for(var/y in 1 to world.icon_size)
  						if(!isnull(test_icon.GetPixel(x,y)))
  							directionals_exist = TRUE
  							break outer
  		if(!directionals_exist)
  			base_icon_dir = SOUTH
  	if(!base_icon_dir)
  		base_icon_dir = curdir
  
  	var/curblend
  	if(A.blend_mode == BLEND_DEFAULT)
  		curblend = defblend
  	else
  		curblend = A.blend_mode
  
  	// Layers will be a sorted list of icons/overlays, based on the order in which they are displayed
  	var/list/layers = list()
  	var/image/copy
  	// Add the atom's icon itself, without pixel_x/y offsets.
  	if(!noIcon)
  		copy = image(icon=curicon, icon_state=curstate, layer=A.layer, dir=base_icon_dir)
  		copy.color = A.color
  		copy.alpha = A.alpha
  		copy.blend_mode = curblend
  		layers[copy] = A.layer
  
  	// Loop through the underlays, then overlays, sorting them into the layers list
  	var/list/process = A.underlays // Current list being processed
  	var/pSet=0 // Which list is being processed: 0 = underlays, 1 = overlays
  	var/curIndex=1 // index of 'current' in list being processed
  	var/current // Current overlay being sorted
  	var/currentLayer // Calculated layer that overlay appears on (special case for FLOAT_LAYER)
  	var/compare // The overlay 'add' is being compared against
  	var/cmpIndex // The index in the layers list of 'compare'
  	while(TRUE)
  		if(curIndex<=process.len)
  			current = process[curIndex]
  			if(!current)
  				curIndex++ //Try the next layer
  				continue
  			var/image/I = current
  			if(I.plane != FLOAT_PLANE && I.plane != A.plane)
  				curIndex++
  				continue
  			currentLayer = I.layer
  			if(currentLayer<0) // Special case for FLY_LAYER
  				if(currentLayer <= -1000)
  					return flat
  				if(pSet == 0) // Underlay
  					currentLayer = A.layer+currentLayer/1000
  				else // Overlay
  					currentLayer = A.layer+(1000+currentLayer)/1000
  
  			// Sort add into layers list
  			for(cmpIndex=1,cmpIndex<=layers.len,cmpIndex++)
  				compare = layers[cmpIndex]
  				if(currentLayer < layers[compare]) // Associated value is the calculated layer
  					layers.Insert(cmpIndex,current)
  					layers[current] = currentLayer
  					break
  			if(cmpIndex>layers.len) // Reached end of list without inserting
  				layers[current]=currentLayer // Place at end
  
  			curIndex++
  
  		if(curIndex>process.len)
  			if(pSet == 0) // Switch to overlays
  				curIndex = 1
  				pSet = 1
  				process = A.overlays
  			else // All done
  				break
  
  	var/icon/add // Icon of overlay being added
  
  	// Current dimensions of flattened icon
  	var/flatX1=1
  	var/flatX2=flat.Width()
  	var/flatY1=1
  	var/flatY2=flat.Height()
  	// Dimensions of overlay being added
  	var/addX1
  	var/addX2
  	var/addY1
  	var/addY2
  
  	for(var/V in layers)
  		var/image/I = V
  		if(I.alpha == 0)
  			continue
  
  		if(I == copy) // 'I' is an /image based on the object being flattened.
  			curblend = BLEND_OVERLAY
  			add = icon(I.icon, I.icon_state, base_icon_dir)
  		else // 'I' is an appearance object.
  			add = getFlatIcon(new/image(I), curdir, curicon, curstate, curblend, FALSE, no_anim)
  
  		// Find the new dimensions of the flat icon to fit the added overlay
  		addX1 = min(flatX1, I.pixel_x+1)
  		addX2 = max(flatX2, I.pixel_x+add.Width())
  		addY1 = min(flatY1, I.pixel_y+1)
  		addY2 = max(flatY2, I.pixel_y+add.Height())
  
  		if(addX1!=flatX1 || addX2!=flatX2 || addY1!=flatY1 || addY2!=flatY2)
  			// Resize the flattened icon so the new icon fits
  			flat.Crop(addX1-flatX1+1, addY1-flatY1+1, addX2-flatX1+1, addY2-flatY1+1)
  			flatX1=addX1;flatX2=addX2
  			flatY1=addY1;flatY2=addY2
  
  		// Blend the overlay into the flattened icon
  		flat.Blend(add, blendMode2iconMode(curblend), I.pixel_x + 2 - flatX1, I.pixel_y + 2 - flatY1)
  
  	if(A.color)
  		flat.Blend(A.color, ICON_MULTIPLY)
  	if(A.alpha < 255)
  		flat.Blend(rgb(255, 255, 255, A.alpha), ICON_MULTIPLY)
  
  	if(no_anim)
  		//Clean up repeated frames
  		var/icon/cleaned = new /icon()
  		cleaned.Insert(flat, "", SOUTH, 1, 0)
  		return cleaned
  	else
  		return icon(flat, "", SOUTH)
  
  /proc/getIconMask(atom/A)//By yours truly. Creates a dynamic mask for a mob/whatever. /N
  	var/icon/alpha_mask = new(A.icon,A.icon_state)//So we want the default icon and icon state of A.
  
negative.code: 
  //What the mob looks like as animated static
  //By vg's ComicIronic
  /proc/getStaticIcon(icon/A, safety = TRUE)
  	var/icon/flat_icon = safety ? A : new(A)
  	flat_icon.Blend(rgb(255,255,255))
  	flat_icon.BecomeAlphaMask()
  	var/icon/static_icon = icon('icons/effects/effects.dmi', "static_base")
  	static_icon.AddAlphaMask(flat_icon)
  	return static_icon
  
  //What the mob looks like as a pitch black outline
  //By vg's ComicIronic
  