anchor.diff:  		..()
   
   
  -
  -
   /obj/machinery/porta_turret/stationary
   	emagged = 1
   
   	New()
  -		installation = new/obj/item/weapon/gun/energy/laser(src.loc)
  -		..()
  +		installation = new/obj/item/weapon/gun/energy/laser(loc)
  +		..()
positive.code: 						targets += C
  						continue
  
  				if (istype(C, /mob/living/carbon/human)) // if the target is a human, analyze threat level
  					if(src.assess_perp(C)<4)
  						continue // if threat level < 4, keep going
  
  				else if (istype(C, /mob/living/carbon/monkey))
  					continue // Don't target monkeys or borgs/AIs you dumb shit
  
  				if (C.lying) // if the perp is lying down, it's still a target but a less-important target
  					secondarytargets += C
  					continue
  
  				targets += C // if the perp has passed all previous tests, congrats, it is now a "shoot-me!" nominee
  
  	if (targets.len>0) // if there are targets to shoot
  
  		var/atom/t = pick(targets) // pick a perp from the list of targets. Targets go first because they are the most important
  
  		if (istype(t, /mob/living)) // if a mob
  			var/mob/living/M = t // simple typecasting
  			if (M.stat!=2) // if the target is not dead
  				spawn() popUp() // pop the turret up if it's not already up.
  				dir=get_dir(src,M) // even if you can't shoot, follow the target
  				spawn() shootAt(M) // shoot the target, finally
  
  	else
  		if(secondarytargets.len>0) // if there are no primary targets, go for secondary targets
  			var/mob/t = pick(secondarytargets)
  			if (istype(t, /mob/living))
  				if (t.stat!=2)
  					spawn() popUp()
  					dir=get_dir(src,t)
  					shootAt(t)
  		else
  			spawn() popDown()
  
  /obj/machinery/porta_turret/proc
  	popUp() // pops the turret up
  		if(disabled)
  			return
  		if(raising || raised) return
  		if(stat & BROKEN) return
  		invisibility=0
  		raising=1
  		flick("popup",cover)
  		sleep(5)
  		sleep(5)
  		raising=0
  		cover.icon_state="openTurretCover"
  		raised=1
  		layer=4
  
  	popDown() // pops the turret down
  		if(disabled)
  			return
  		if(raising || !raised) return
  		if(stat & BROKEN) return
  		layer=3
  		raising=1
  		flick("popdown",cover)
  		sleep(10)
  		raising=0
  		cover.icon_state="turretCover"
  		raised=0
  		invisibility=2
  		icon_state="[lasercolor]grey_target_prism"
  
  
  /obj/machinery/porta_turret/proc/assess_perp(mob/living/carbon/human/perp as mob)
  	var/threatcount = 0 // the integer returned
  
  	if(src.emagged) return 10 // if emagged, always return 10.
  
  	if((stun_all && !src.allowed(perp)) || attacked && !src.allowed(perp))
  		// if the turret has been attacked or is angry, target all non-sec people
  		if(!src.allowed(perp))
  			return 10
  
  	if(auth_weapons) // check for weapon authorization
  		if((isnull(perp.wear_id)) || (istype(perp.wear_id.GetID(), /obj/item/weapon/card/id/syndicate)))
  
  			if((src.allowed(perp)) && !(src.lasercolor)) // if the perp has security access, return 0
  				return 0
  
  			if((istype(perp.l_hand, /obj/item/weapon/gun) && !istype(perp.l_hand, /obj/item/weapon/gun/projectile/shotgun)) || istype(perp.l_hand, /obj/item/weapon/melee/baton))
  				threatcount += 4
  
  			if((istype(perp.r_hand, /obj/item/weapon/gun) && !istype(perp.r_hand, /obj/item/weapon/gun/projectile/shotgun)) || istype(perp.r_hand, /obj/item/weapon/melee/baton))
  				threatcount += 4
  
  			if(istype(perp.belt, /obj/item/weapon/gun) || istype(perp.belt, /obj/item/weapon/melee/baton))
  				threatcount += 2
  
  	if((src.lasercolor) == "b")//Lasertag turrets target the opposing team, how great is that? -Sieve
  		threatcount = 0//But does not target anyone else
  		if(istype(perp.wear_suit, /obj/item/clothing/suit/redtag))
  			threatcount += 4
  		if((istype(perp.r_hand,/obj/item/weapon/gun/energy/laser/redtag)) || (istype(perp.l_hand,/obj/item/weapon/gun/energy/laser/redtag)))
  			threatcount += 4
  		if(istype(perp.belt, /obj/item/weapon/gun/energy/laser/redtag))
  			threatcount += 2
  
  	if((src.lasercolor) == "r")
  		threatcount = 0
  		if(istype(perp.wear_suit, /obj/item/clothing/suit/bluetag))
  			threatcount += 4
  
negative.code: 	icon = 'icons/obj/turrets.dmi'
  	icon_state = "turret_frame"
  	density=1
  	var/build_step = 0			//the current step in the building process
  	var/finish_name="turret"	//the name applied to the product turret
  	var/installation = null		//the gun type installed
  	var/gun_charge = 0			//the gun charge of the gun type installed
  
  
  /obj/machinery/porta_turret_construct/attackby(obj/item/I, mob/user)
  	//this is a bit unwieldy but self-explanatory
  	switch(build_step)
  		if(0)	//first step
  			if(istype(I, /obj/item/weapon/wrench) && !anchored)
  				playsound(loc, 'sound/items/Ratchet.ogg', 100, 1)
  				user << "<span class='notice'>You secure the external bolts.</span>"
  				anchored = 1
  				build_step = 1
  				return
  
  			else if(istype(I, /obj/item/weapon/crowbar) && !anchored)
  				playsound(loc, 'sound/items/Crowbar.ogg', 75, 1)
  				user << "<span class='notice'>You dismantle the turret construction.</span>"
  				new /obj/item/stack/sheet/metal( loc, 5)
  				del(src)
  				return
  
  		if(1)
  			if(istype(I, /obj/item/stack/sheet/metal))
  				if(I:amount>=2) //requires 2 metal sheets
  					user << "<span class='notice'>You add some metal armor to the interior frame.</span>"
  					build_step = 2
  					I:amount -= 2
  					icon_state = "turret_frame2"
  					if(I:amount <= 0)
  						del(I)
  					return
  
  			else if(istype(I, /obj/item/weapon/wrench))
  				playsound(loc, 'sound/items/Ratchet.ogg', 75, 1)
  				user << "<span class='notice'>You unfasten the external bolts.</span>"
  				anchored = 0
  				build_step = 0
  				return
  
  
  		if(2)
  			if(istype(I, /obj/item/weapon/wrench))
  				playsound(loc, 'sound/items/Ratchet.ogg', 100, 1)
  				user << "<span class='notice'>You bolt the metal armor into place.</span>"
  				build_step = 3
  				return
  
  			else if(istype(I, /obj/item/weapon/weldingtool))
  				var/obj/item/weapon/weldingtool/WT = I
  				if(!WT.isOn())
  					return
  				if(WT.get_fuel() < 5) //uses up 5 fuel.
  					user << "<span class='notice'>You need more fuel to complete this task.</span>"
  					return
  
  				playsound(loc, pick('sound/items/Welder.ogg', 'sound/items/Welder2.ogg'), 50, 1)
  				if(do_after(user, 20))
  					if(!src || !WT.remove_fuel(5, user)) return
  					build_step = 1
  