code:
/obj/item/organ/bone
	name = "bone"
	desc = "Bone apple tea."
	icon_state = "bone"
	base_icon_state = "bone"
	organ_flags = ORGAN_NOINFECTION|ORGAN_LIMB_SUPPORTER|ORGAN_INDESTRUCTIBLE|ORGAN_NO_VIOLENT_DAMAGE // you can't just eat a bone
	organ_efficiency = list(ORGAN_SLOT_BONE = 100)

	maxHealth = BONE_MAX_HEALTH
	//compound fracture
	high_threshold = BONE_MAX_HEALTH * 0.8
	//fracture
	medium_threshold = BONE_MAX_HEALTH * 0.5
	//dislocation
	low_threshold = BONE_MAX_HEALTH * 0.2
	pain_multiplier = 0.35

	organ_volume = 0.5
	nutriment_req = 0.15
	hydration_req = 0.15

	force = 5
	throwforce = 5

	attaching_items = list(/obj/item/stack/medical/bone_gel)
	healing_items = list(/obj/item/stack/medical/bone_gel)

	/// BONE_JOINTED, BONE_ENCASING
	var/bone_flags = BONE_JOINTED
	/// Descriptive string for dislocations
	var/joint_name = "joint"
	/// Did this bone get reinforced? Mostly used to prevent armor stacking
	var/reinforced = FALSE
	/// Bones sometimes provide wound resistance depending on efficiency
	var/wound_resistance = 0

	// Variables below only matter for dislocations/fractures
	/// If we suffer severe head booboos, we can get brain traumas tied to them
	var/datum/brain_trauma/active_trauma
	/// Have we been taped?
	var/taped = FALSE
	/// Have we been bone gel'd?
	var/gelled = FALSE
	/// If we deal brain traumas, when is the next one due?
	COOLDOWN_DECLARE(next_trauma_cycle)

/obj/item/organ/bone/can_heal(delta_time, times_fired)
	return FALSE

/obj/item/organ/bone/update_icon_state()
	. = ..()
	if(reinforced && (status == ORGAN_ORGANIC))
		icon_state = "[base_icon_state]-reinforced"

/obj/item/organ/bone/get_visible_state()
	return

/obj/item/organ/bone/handle_organ_attack(obj/item/tool, mob/living/user, params)
	if(owner && DOING_INTERACTION_WITH_TARGET(user, owner))
		return TRUE
	else if(DOING_INTERACTION_WITH_TARGET(user, src))
		return TRUE
	if(owner && CHECK_BITFIELD(organ_flags, ORGAN_CUT_AWAY))
		for(var/thing in attaching_items)
			if(istype(tool, thing))
				handle_attaching_item(tool, user, params)
				return TRUE
	for(var/thing in healing_items)
		if(istype(tool, thing))
			handle_healing_item(tool, user, params)
			return TRUE
	for(var/thing in healing_tools)
		if(tool.tool_behaviour == thing)
			handle_healing_item(tool, user, params)
			return TRUE
	if(owner && (tool.tool_behaviour == TOOL_SAW) && !CHECK_BITFIELD(organ_flags, ORGAN_CUT_AWAY))
		handle_cutting_away(tool, user, params)
		return TRUE

/obj/item/organ/bone/handle_cutting_away(obj/item/tool, mob/living/user, params)
	user.visible_message(span_notice("[user] starts sawing \the [src] from \the [owner]..."), \
					span_notice("I start sawing \the [src] from \the [owner]..."), \
					vision_distance = COMBAT_MESSAGE_RANGE)
	owner.custom_pain("OH GOD! My [src] is being BROKEN AWAY!", 30, FALSE, owner.get_bodypart(current_zone))
	if(!do_mob(user, owner, 8 SECONDS))
		to_chat(user, span_warning("I must stand still!"))
		return TRUE
	user.visible_message(span_notice("[user] saws \the [src] away."), \
					span_notice("I saw \the [src] away."), \
					vision_distance = COMBAT_MESSAGE_RANGE)
	organ_flags |= ORGAN_CUT_AWAY
	return TRUE

/obj/item/organ/bone/applyOrganDamage(amount, maximum, silent)
	. = ..()
	if(!owner)
		return
	var/obj/item/bodypart/limb = owner.get_bodypart(current_zone)
	if((amount > 0) && (damage >= low_threshold))
		if(damage >= medium_threshold)
			if(limb.body_zone == BODY_ZONE_PRECISE_NECK)
				if(!HAS_TRAIT_FROM(owner, TRAIT_PARALYSIS_R_LEG, "neck_fracture") || !HAS_TRAIT_FROM(owner, TRAIT_PARALYSIS_L_LEG, "neck_fractureneck_fracture"))
					if(!HAS_TRAIT(owner, TRAIT_PARALYSIS_R_LEG) && !HAS_TRAIT(owner, TRAIT_PARALYSIS_L_LEG))
						to_chat(owner, span_flashinguserdanger("I become PARAPLEGIC!"))
					ADD_TRAIT(owner, TRAIT_PARALYSIS_R_LEG, "neck_fracture")
					ADD_TRAIT(owner, TRAIT_PARALYSIS_L_LEG, "neck_fracture")
			if(limb.body_zone in list(BODY_ZONE_PRECISE_FACE, BODY_ZONE_HEAD))
				if(!active_trauma)
					active_trauma = owner.gain_trauma_type((damage >= maxHealth ? BRAIN_TRAUMA_SEVERE : BRAIN_TRAUMA_MILD), TRAUMA_RESILIENCE_WOUND)
					COOLDOWN_START(src, next_trauma_cycle, (rand(100-WOUND_BONE_HEAD_TIME_VARIANCE, 100+WOUND_BONE_HEAD_TIME_VARIANCE) * 0.01 * 1.5 MINUTES * (damage/maxHealth)))
				if(!HAS_TRAIT_FROM(owner, TRAIT_DISFIGURED, BRUTE))
					owner.visible_message(span_danger("[owner]'s face turns into an unrecognizable, mangled mess!"), \
								span_userdanger("MY FACE IS HORRIBLY MANGLED!"))
					ADD_TRAIT(owner, TRAIT_DISFIGURED, BRUTE)
					var/obj/item/bodypart/face/face = owner.get_bodypart_nostump(BODY_ZONE_PRECISE_FACE)
					if(face)
						ADD_TRAIT(face, TRAIT_DISFIGURED, BRUTE)
			jostle(owner)
		var/obj/item/bodypart/child
		if(length(limb.children_zones))
			child = owner.get_bodypart(limb.children_zones[1])
		if( (limb.held_index && owner.get_item_for_held_index(limb.held_index) && prob(70 * (damage/maxHealth))) || \
			(child?.held_index && owner.get_item_for_held_index(child.held_index) && prob(55 * (damage/maxHealth))) )
			var/obj/item/held_item = owner.get_item_for_held_index(limb.held_index || child?.held_index)
			if(istype(held_item, /obj/item/offhand))
				held_item = owner.get_active_held_item()
			if(held_item && owner.dropItemToGround(held_item))
				owner.visible_message(span_danger("[owner] drops [held_item] in shock!"), \
					span_userdanger("The shock on my [name] causes me to drop [held_item]!"), \
				vision_distance=COMBAT_MESSAGE_RANGE)
		if(owner.stat < UNCONSCIOUS)
			if((damage >= medium_threshold) && (prev_damage < medium_threshold))
				owner.agony_scream()
			else if((damage >= low_threshold) && (prev_damage < low_threshold))
				owner.death_scream()
	else if(amount < 0)
		if(damage < medium_threshold)
			if(limb.body_zone == BODY_ZONE_PRECISE_NECK)
				if(HAS_TRAIT_FROM(owner, TRAIT_PARALYSIS_R_LEG, "neck_fracutre") || HAS_TRAIT_FROM(owner, TRAIT_PARALYSIS_L_LEG, "neck_fracutre"))
					REMOVE_TRAIT(owner, TRAIT_PARALYSIS_R_LEG, "neck_fracture")
					REMOVE_TRAIT(owner, TRAIT_PARALYSIS_L_LEG, "neck_fracture")
					if(!HAS_TRAIT(owner, TRAIT_PARALYSIS_R_LEG) && !HAS_TRAIT(owner, TRAIT_PARALYSIS_L_LEG))
						to_chat(owner, span_green("I am no longer paraplegic!"))
			if(active_trauma)
				QDEL_NULL(active_trauma)

/obj/item/organ/bone/on_life(delta_time, times_fired)
	. = ..()
	if(damage < medium_threshold)
		gelled = FALSE
		taped = FALSE
		if(active_trauma)
			QDEL_NULL(active_trauma)
		return

	if(istype(src, BONE_HEAD) && COOLDOWN_FINISHED(src, next_trauma_cycle))
		if(active_trauma)
			QDEL_NULL(active_trauma)
		else
			active_trauma = owner.gain_trauma_type((damage >= maxHealth ? BRAIN_TRAUMA_SEVERE : BRAIN_TRAUMA_MILD), TRAUMA_RESILIENCE_WOUND)
		COOLDOWN_START(src, next_trauma_cycle, (rand(100-WOUND_BONE_HEAD_TIME_VARIANCE, 100+WOUND_BONE_HEAD_TIME_VARIANCE) * 0.01 * 1.5 MINUTES * (damage/maxHealth)))

	if(gelled && taped)
		applyOrganDamage(-0.1 * delta_time)
		if(DT_PROB((damage/maxHealth) * 3, delta_time))
			var/obj/item/bodypart/limb = owner.get_bodypart(current_zone)
			limb.add_pain(10)
			if(prob(40))
				to_chat(owner, span_userdanger("You feel a sharp pain in your body as your bones are reforming!"))

/obj/item/organ/bone/proc/reinforce(added_resistance = 5)
	if(reinforced || (status != ORGAN_ORGANIC))
		return
	wound_resistance += added_resistance
	force += 5
	throwforce += 5
	reinforced = TRUE
	name = "reinforced [name]"
	update_appearance()

/obj/item/organ/bone/proc/get_wound_resistance()
	if(damage < low_threshold)
		return CEILING(wound_resistance * (get_slot_efficiency(ORGAN_SLOT_BONE)/100), 1)
	else if(damage < medium_threshold)
		return (wound_resistance - 15)
	else if(damage < maxHealth)
		return (wound_resistance - 25)
	else
		return (wound_resistance - 35)

/// If we're a human who's punching something with a broken hand, we might hurt ourselves doing so
/obj/item/organ/bone/proc/attack_with_hurt_hand(mob/living/carbon/owner, obj/item/bodypart/limb, atom/target)
	if(damage < low_threshold)
		return
	var/punch_verb = "punch"
	if(IS_HELP_INTENT(owner, null))
		punch_verb = "touch"
	// With a bone wound, you have up to a 60% chance to proc pain on hit
	if(prob(60 * (damage/maxHealth)))
		if(prob(75 - (35 * damage/maxHealth)))
			to_chat(owner, span_userdanger("The [damage >= medium_threshold ? "fracture" : "dislocation"] in your [limb.name] shoots with pain as you [punch_verb] [target]!"))
			limb.add_pain(10)
		else
			owner.visible_message(span_danger("[owner] weakly [punch_verb]es [target] with [owner.p_their()] broken [limb.name], recoiling from pain!"), \
				span_userdanger("You fail to [punch_verb] [target] as the [damage >= medium_threshold ? "fracture" : "dislocation"] in your [limb.name] lights up in unbearable pain!"), \
				vision_distance=COMBAT_MESSAGE_RANGE)
			owner.agony_scream()
			owner.Stun(0.5 SECONDS)
			limb.add_pain(rand(10, 15))
			return COMPONENT_CANCEL_ATTACK_CHAIN

/// If we're a human who's kicking something with a broken foot, we might hurt ourselves doing so
/obj/item/organ/bone/proc/attack_with_hurt_foot(mob/living/carbon/owner, obj/item/bodypart/limb, atom/target)
	if(damage < low_threshold)
		return
	// With a bone wound, you have up to a 60% chance to proc pain on hit
	if(prob(60 * (damage/maxHealth)))
		if(prob(75 - (35 * damage/maxHealth)))
			to_chat(owner, span_userdanger("The [damage >= medium_threshold ? "fracture" : "dislocation"] in your [limb.name] shoots with pain as you kick [target]!"))
			limb.add_pain(10)
		else
			owner.visible_message(span_danger("[owner] weakly kicks [target] with [owner.p_their()] broken [limb.name], recoiling from pain!"), \
				span_userdanger("You fail to kick [target] as the [damage >= medium_threshold ? "fracture" : "dislocation"] in your [limb.name] lights up in unbearable pain!"), \
				vision_distance=COMBAT_MESSAGE_RANGE)
			owner.agony_scream()
			owner.Stun(0.5 SECONDS)
			limb.add_pain(rand(10, 15))
			return COMPONENT_CANCEL_ATTACK_CHAIN

/// If we're a human who's biting something with a broken jaw, we might hurt ourselves doing so
/obj/item/organ/bone/proc/attack_with_hurt_jaw(mob/living/carbon/owner, obj/item/bodypart/limb, atom/target)
	if(damage < low_threshold)
		return
	// With a bone wound, you have up to a 60% chance to proc pain on hit
	if(prob(60 * (damage/maxHealth)))
		if(prob(75 - (35 * damage/maxHealth)))
			to_chat(owner, span_userdanger("The [damage >= medium_threshold ? "fracture" : "dislocation"] in your [limb.name] shoots with pain as you bite [target]!"))
			limb.add_pain(10)
		else
			owner.visible_message(span_danger("[owner] weakly bites [target] with [owner.p_their()] broken [limb.name], recoiling from pain!"), \
				span_userdanger("You fail to bite [target] as the [damage >= medium_threshold ? "fracture" : "dislocation"] in your [limb.name] lights up in unbearable pain!"), \
				vision_distance=COMBAT_MESSAGE_RANGE)
			owner.agony_scream()
			owner.Stun(0.5 SECONDS)
			limb.add_pain(rand(10, 15))
			return COMPONENT_CANCEL_ATTACK_CHAIN

/obj/item/organ/bone/proc/try_jostle(mob/living/carbon/source)
	if(damage < medium_threshold)
		return
	var/obj/item/bodypart/limb = source?.get_bodypart(current_zone)
	if((1 - limb.current_splint?.splint_factor) >= 0.5)
		return
	if(source.stat < UNCONSCIOUS && prob(8 * (damage/maxHealth)))
		jostle(source)

/obj/item/organ/bone/proc/jostle(mob/living/carbon/source)
	if(!istype(source))
		source = owner
	if(!istype(source))
		return
	var/obj/item/bodypart/limb = source?.get_bodypart(current_zone)
	if(!limb)
		return
	source.custom_pain("Staggering pain jolts through my [name]!", 30, affecting = limb)
	source.Stumble(3 SECONDS)
	source.Stun(3 SECONDS)
	limb.damage_internal_organs(amount = (rand(5, 10) * (damage/maxHealth)), wounding_type = WOUND_PIERCE, forced = TRUE)
	source.sound_hint()
	if((source.get_active_hand() == limb) && source.can_feel_pain() && (source.get_chem_effect(CE_PAINKILLER) < 50))
		source.dropItemToGround(source.get_active_held_item())

/obj/item/organ/bone/proc/dislocate()
	if(!owner)
		return
	if(damage >= low_threshold)
		return
	setOrganDamage(low_threshold * 1.5)
	return TRUE

/obj/item/organ/bone/proc/fracture()
	if(!owner)
		return
	if(damage >= medium_threshold)
		return
	setOrganDamage(medium_threshold * 1.5)
	return TRUE

/obj/item/organ/bone/proc/compound_fracture()
	if(!owner)
		return
	if(damage >= high_threshold)
		return
	setOrganDamage(high_threshold * 1.5)
	return TRUE

/obj/item/organ/bone/proc/relocate()
	if(!owner)
		return
	if(damage >= medium_threshold)
		return
	setOrganDamage(0)
	return TRUE

/obj/item/organ/bone/proc/mend_fracture()
	if(!owner)
		return
	if(damage < medium_threshold)
		return
	if(damage >= high_threshold)
		return
	if(bone_flags & BONE_JOINTED)
		setOrganDamage(low_threshold * 1.5)
	else
		setOrganDamage(0)
	return TRUE

/obj/item/organ/bone/proc/mend_compound_fracture()
	if(!owner)
		return
	if(damage < high_threshold)
		return
	setOrganDamage(medium_threshold * 1.5)
	return TRUE


code:
/obj/item/organ/bone/chest
	name = "ribcage"
	desc = "Despite all my rage, i'm just a rib in a cage."
	icon_state = "ribcage"
	base_icon_state = "ribcage"
	zone = BODY_ZONE_CHEST
	joint_name = "ribs"
	bone_flags = BONE_JOINTED|BONE_ENCASING
	wound_resistance = 5

repo_name: AnalWerewolf/sundown

license: mit


code:
/obj/item/organ/bone/groin
	name = "pelvis"
	desc = "How do women get a fetus to fit through this?"
	icon_state = "pelvis"
	base_icon_state = "pelvis"
	zone = BODY_ZONE_PRECISE_GROIN
	joint_name = "hip"
	bone_flags = BONE_JOINTED
	wound_resistance = 5


code:
/obj/item/organ/bone/head
	name = "skull"
	desc = "To be or not to be: That is the question."
	icon_state = "skull"
	base_icon_state = "skull"
	zone = BODY_ZONE_HEAD
	joint_name = "sutures" // that is the actual name i kid you not
	bone_flags = BONE_ENCASING // if you dislocate a skull - well, that's pretty much death...
	wound_resistance = 5


code:
/obj/item/organ/bone/l_arm
	name = "left humerus"
	desc = "Not so funny for the one who lost it."
	icon_state = "left_humerus"
	base_icon_state = "left_humerus"
	zone = BODY_ZONE_L_ARM
	joint_name = "left elbow"
	bone_flags = BONE_JOINTED


code:
/obj/item/organ/bone/l_foot
	name = "left caucaneus"
	desc = "If having a bigger foot meant having a bigger penis, amputees everywhere would be in a bad spot."
	icon_state = "left_caucaneus"
	base_icon_state = "left_caucaneus"
	zone = BODY_ZONE_PRECISE_L_FOOT
	joint_name = "left ankle"
	bone_flags = BONE_JOINTED


code:
/obj/item/organ/bone/l_hand
	name = "left carpals"
	desc = "For most people, losing the left hand is better than losing the right hand - But by far, losing none is the best option."
	icon_state = "left_carpals"
	base_icon_state = "left_carpals"
	zone = BODY_ZONE_PRECISE_L_HAND
	joint_name = "left wrist"
	bone_flags = BONE_JOINTED
	gender = PLURAL


code:
/obj/item/organ/bone/l_leg
	name = "left femur"
	desc = "A femur fracture is said to be painful enough to attract the attention of demons - That is no longer a problem."
	icon_state = "left_femur"
	base_icon_state = "left_femur"
	zone = BODY_ZONE_L_LEG
	joint_name = "left knee"
	bone_flags = BONE_JOINTED


code:
/obj/item/organ/bone/mouth
	name = "mandible"
	desc = "Just how deep do you believe? Will you bite the hand that feeds?"
	icon_state = "mandible"
	base_icon_state = "mandible"
	zone = BODY_ZONE_PRECISE_MOUTH
	joint_name = "mandible"
	bone_flags = BONE_JOINTED


code:
/obj/item/organ/bone/neck
	name = "cervical spine"
	desc = "Woe to the spineless bastard that lost this."
	icon_state = "spine"
	base_icon_state = "spine"
	zone = BODY_ZONE_PRECISE_NECK
	joint_name = "cervical spine"
	bone_flags = NONE // dislocating the spine is also death


code:
/obj/item/organ/bone/r_arm
	name = "right humerus"
	desc = "The second funniest bone in the human body, only surpassed by the funny bone."
	icon_state = "right_humerus"
	base_icon_state = "right_humerus"
	zone = BODY_ZONE_R_ARM
	joint_name = "right elbow"
	bone_flags = BONE_JOINTED


code:
/obj/item/organ/bone/r_foot
	name = "right caucaneus"
	desc = "Having a fractured caucaneus significantly impairs movement - But it is better than having no caucaneus."
	icon_state = "right_caucaneus"
	base_icon_state = "right_caucaneus"
	zone = BODY_ZONE_PRECISE_R_FOOT
	joint_name = "right ankle"
	bone_flags = BONE_JOINTED


code:
/obj/item/organ/bone/r_hand
	name = "right carpals"
	desc = "Carpal tunnel syndrome can't happen if you have no carpal bones."
	icon_state = "right_carpals"
	base_icon_state = "right_carpals"
	zone = BODY_ZONE_PRECISE_R_HAND
	joint_name = "right wrist"
	bone_flags = BONE_JOINTED
	gender = PLURAL


code:
/obj/item/organ/bone/r_leg
	name = "right femur"
	desc = "The femur is the largest, strongest bone of the human body. Until you lose it."
	icon_state = "right_femur"
	base_icon_state = "right_femur"
	zone = BODY_ZONE_R_LEG
	joint_name = "right knee"
	bone_flags = BONE_JOINTED


code:
/obj/item/organ/nerve
	name = "nerve"
	desc = "An unnerving sight."
	icon_state = "nerve"
	organ_flags = ORGAN_EDIBLE|ORGAN_LIMB_SUPPORTER|ORGAN_INDESTRUCTIBLE|ORGAN_NO_VIOLENT_DAMAGE
	organ_efficiency = list(ORGAN_SLOT_NERVE = 100)

	maxHealth = NERVE_MAX_HEALTH
	high_threshold = NERVE_MAX_HEALTH * 0.8
	low_threshold = NERVE_MAX_HEALTH * 0.2
	pain_multiplier = 0.5 // it's a fucking nerve, of course it hurts like hell

	organ_volume = 0.5
	max_blood_storage = 1
	current_blood = 1
	blood_req = 0.2
	oxygen_req = 0.1
	nutriment_req = 0.2
	hydration_req = 0.25

	healing_tools = null
	healing_items = list(/obj/item/stack/medical/nervemend)

/obj/item/organ/nerve/can_heal(delta_time, times_fired)
	return FALSE

/obj/item/organ/nerve/proc/tear()
	if(!owner)
		return
	if(owner.stat < UNCONSCIOUS)
		owner.death_scream()
	applyOrganDamage(maxHealth * 0.5)

/obj/item/organ/nerve/proc/mend()
	if(!owner)
		return
	setOrganDamage(0)


code:
/obj/item/organ/nerve/chest
	name = "thoracoabdominal nerves"
	zone = BODY_ZONE_CHEST


code:
/obj/item/organ/nerve/groin
	name = "gluteal nerves"
	zone = BODY_ZONE_PRECISE_GROIN


code:
/obj/item/organ/nerve/head
	name = "trigeminal nerve"
	zone = BODY_ZONE_HEAD


code:
/obj/item/organ/nerve/l_arm
	name = "radial nerve"
	zone = BODY_ZONE_L_ARM


code:
/obj/item/organ/nerve/l_eye
	name = "optic nerve"
	zone = BODY_ZONE_PRECISE_L_EYE


code:
/obj/item/organ/nerve/l_foot
	name = "deep peroneal nerve"
	zone = BODY_ZONE_PRECISE_L_FOOT


code:
/obj/item/organ/nerve/l_hand
	name = "median nerve"
	zone = BODY_ZONE_PRECISE_L_HAND


code:
/obj/item/organ/nerve/l_leg
	name = "sciatic nerve"
	zone = BODY_ZONE_L_LEG


code:
/obj/item/organ/nerve/mouth
	name = "mandibular nerve"
	zone = BODY_ZONE_PRECISE_MOUTH


code:
/obj/item/organ/nerve/neck
	name = "spinal nerves"
	zone = BODY_ZONE_PRECISE_NECK


code:
/obj/item/organ/nerve/r_arm
	name = "radial nerve"
	zone = BODY_ZONE_R_ARM


code:
/obj/item/organ/nerve/r_eye
	name = "optic nerve"
	zone = BODY_ZONE_PRECISE_R_EYE


code:
/obj/item/organ/nerve/r_foot
	name = "deep peroneal nerve"
	zone = BODY_ZONE_PRECISE_R_FOOT


code:
/obj/item/organ/nerve/r_hand
	name = "median nerve"
	zone = BODY_ZONE_PRECISE_R_HAND


code:
/obj/item/organ/nerve/r_leg
	name = "sciatic nerve"
	zone = BODY_ZONE_R_LEG


code:
// important note: Tendons in medical terminology connect muscles to bone - Here,
// any sort of muscle tissue could be made as a tendon
/obj/item/organ/tendon
	name = "tendon"
	desc = "Not the song made by experimental musician Igorrr."
	icon_state = "tendon"
	organ_flags = ORGAN_EDIBLE|ORGAN_LIMB_SUPPORTER|ORGAN_INDESTRUCTIBLE|ORGAN_NO_VIOLENT_DAMAGE
	organ_efficiency = list(ORGAN_SLOT_TENDON = 100)

	maxHealth = TENDON_MAX_HEALTH
	high_threshold = TENDON_MAX_HEALTH * 0.8
	low_threshold = TENDON_MAX_HEALTH * 0.2
	pain_multiplier = 0.15

	organ_volume = 0.5
	max_blood_storage = 1.5
	current_blood = 1.5
	blood_req = 0.25
	nutriment_req = 0.2
	hydration_req = 0.25

/obj/item/organ/tendon/can_heal(delta_time, times_fired)
	return FALSE

/obj/item/organ/tendon/proc/tear()
	if(!owner)
		return
	if(owner.stat < UNCONSCIOUS)
		if(ORGAN_SLOT_VOICE in organ_efficiency)
			owner.agony_gargle()
		else
			owner.death_scream()
	applyOrganDamage(maxHealth * 0.5)

/obj/item/organ/tendon/proc/mend()
	if(!owner)
		return
	setOrganDamage(0)


code:
/obj/item/organ/tendon/chest
	name = "rectus abdominis"
	zone = BODY_ZONE_CHEST


code:
/obj/item/organ/tendon/groin
	name = "gluteus maximus"
	desc = "The most treasured muscle of the human body."
	icon_state = "gluteus"
	zone = BODY_ZONE_PRECISE_GROIN


code:
/obj/item/organ/tendon/head
	name = "frontalis muscle"
	zone = BODY_ZONE_HEAD


code:
/obj/item/organ/tendon/l_arm
	name = "distal biceps tendon"
	zone = BODY_ZONE_L_ARM


code:
/obj/item/organ/tendon/l_eye
	name = "medial rectus"
	zone = BODY_ZONE_PRECISE_L_EYE


code:
/obj/item/organ/tendon/l_foot
	name = "achilles tendon"
	desc = "Achilles was brought down with a hit to the heel. So was the owner of this tendon."
	zone = BODY_ZONE_PRECISE_L_FOOT


code:
/obj/item/organ/tendon/l_hand
	name = "carpal ligament"
	desc = "Many people live fine without a palmaris longus tendon. Having no carpal ligament is more worrying, however."
	zone = BODY_ZONE_PRECISE_L_HAND


code:
/obj/item/organ/tendon/l_leg
	name = "cruciate ligament"
	zone = BODY_ZONE_L_LEG


code:
/obj/item/organ/tendon/mouth
	name = "lateral ligament"
	zone = BODY_ZONE_PRECISE_MOUTH


code:
/obj/item/organ/tendon/neck
	name = "vocal cords"
	desc = "Well... Some smoker must have gotten a tracheotomy."
	icon_state = "vocal-cords"
	zone = BODY_ZONE_PRECISE_NECK
	organ_efficiency = list(ORGAN_SLOT_TENDON = 100, ORGAN_SLOT_VOICE = 100)


code:
/obj/item/organ/tendon/r_arm
	name = "distal biceps tendon"
	zone = BODY_ZONE_R_ARM


code:
/obj/item/organ/tendon/r_eye
	name = "medial rectus"
	zone = BODY_ZONE_PRECISE_R_EYE


code:
/obj/item/organ/tendon/r_foot
	name = "achilles tendon"
	desc = "Achilles was brought down with a hit to the heel. So was the owner of this tendon."
	zone = BODY_ZONE_PRECISE_R_FOOT


code:
/obj/item/organ/tendon/r_hand
	name = "carpal ligament"
	desc = "Many people live fine without a palmaris longus tendon. Having no carpal ligament is more worrying, however."
	zone = BODY_ZONE_PRECISE_R_HAND


code:
/obj/item/organ/tendon/r_leg
	name = "cruciate ligament"
	zone = BODY_ZONE_R_LEG


code:
/obj/item/organ/tongue
	name = "tongue"
	desc = "Cat got your tongue?"
	icon_state = "tongue"
	zone = BODY_ZONE_PRECISE_MOUTH
	organ_efficiency = list(ORGAN_SLOT_TONGUE = 100)

	organ_volume = 0.5
	max_blood_storage = 5
	current_blood = 5
	blood_req = 1
	oxygen_req = 0.5
	nutriment_req = 0.5
	hydration_req = 2

	attack_verb_continuous = list("licks", "slobbers", "slaps", "frenches", "tongues")
	attack_verb_simple = list("lick", "slobber", "slap", "french", "tongue")
	var/list/languages_possible
	var/say_mod = null

	/// Whether the owner of this tongue can taste anything. Being set to FALSE will mean no taste feedback will be provided.
	var/sense_of_taste = TRUE

	var/taste_sensitivity = 15 // lower is more sensitive.
	var/modifies_speech = FALSE
	var/static/list/languages_possible_base = typecacheof(list(
		/datum/language/common,
		/datum/language/uncommon,
		/datum/language/newspeak,
		/datum/language/cyrillic,
		/datum/language/yoinky,
		/datum/language/draconic,
		/datum/language/codespeak,
		/datum/language/monkey,
		/datum/language/narsie,
		/datum/language/beachbum,
		/datum/language/aphasia,
		/datum/language/piratespeak,
		/datum/language/moffic,
		/datum/language/sylvan,
		/datum/language/shadowtongue,
		/datum/language/terrum,
		/datum/language/nekomimetic,
	))

/obj/item/organ/tongue/Initialize(mapload)
	. = ..()
	languages_possible = languages_possible_base

/obj/item/organ/tongue/proc/handle_speech(datum/source, list/speech_args)
	return

/obj/item/organ/tongue/Insert(mob/living/carbon/M, special = 0)
	. = ..()
	if(say_mod && M.dna && M.dna.species)
		M.dna.species.say_mod = say_mod
	if (modifies_speech)
		RegisterSignal(M, COMSIG_MOB_SAY, .proc/handle_speech)
	M.UnregisterSignal(M, COMSIG_MOB_SAY)

	/* This could be slightly simpler, by making the removal of the
	* NO_TONGUE_TRAIT conditional on the tongue's `sense_of_taste`, but
	* then you can distinguish between ageusia from no tongue, and
	* ageusia from having a non-tasting tongue.
	*/
	REMOVE_TRAIT(M, TRAIT_AGEUSIA, NO_TONGUE_TRAIT)
	if(!sense_of_taste)
		ADD_TRAIT(M, TRAIT_AGEUSIA, ORGAN_TRAIT)

/obj/item/organ/tongue/Remove(mob/living/carbon/M, special = 0)
	. = ..()
	if(say_mod && M.dna && M.dna.species)
		M.dna.species.say_mod = initial(M.dna.species.say_mod)
	UnregisterSignal(M, COMSIG_MOB_SAY, .proc/handle_speech)
	M.RegisterSignal(M, COMSIG_MOB_SAY, /mob/living/carbon/.proc/handle_tongueless_speech)
	REMOVE_TRAIT(M, TRAIT_AGEUSIA, ORGAN_TRAIT)
	// Carbons by default start with NO_TONGUE_TRAIT caused TRAIT_AGEUSIA
	ADD_TRAIT(M, TRAIT_AGEUSIA, NO_TONGUE_TRAIT)

/obj/item/organ/tongue/could_speak_language(language)
	return is_type_in_typecache(language, languages_possible)


code:
/obj/item/organ/tongue/abductor
	name = "superlingual matrix"
	desc = "A mysterious structure that allows for instant communication between users. Pretty impressive until you need to eat something."
	icon_state = "tongue-ayylmao"
	say_mod = "gibbers"
	sense_of_taste = FALSE
	modifies_speech = TRUE
	var/mothership

/obj/item/organ/tongue/abductor/attack_self(mob/living/carbon/human/H)
	if(!istype(H))
		return

	var/obj/item/organ/tongue/abductor/T = H.getorganslot(ORGAN_SLOT_TONGUE)
	if(!istype(T))
		return

	if(T.mothership == mothership)
		to_chat(H, span_notice("[src] is already attuned to the same channel as your own."))

	H.visible_message(span_notice("[H] holds [src] in their hands, and concentrates for a moment."), \
					span_notice("You attempt to modify the attenuation of [src]."))
	if(do_after(H, delay=15, target=src))
		to_chat(H, span_notice("You attune [src] to your own channel."))
		mothership = T.mothership

/obj/item/organ/tongue/abductor/examine(mob/M)
	. = ..()
	if(HAS_TRAIT(M, TRAIT_ABDUCTOR_TRAINING) || (M.mind && HAS_TRAIT(M.mind, TRAIT_ABDUCTOR_TRAINING)) || isobserver(M))
		. += span_notice("It can be attuned to a different channel by using it inhand.")
		if(!mothership)
			. += span_notice("It is not attuned to a specific mothership.")
		else
			. += span_notice("It is attuned to [mothership].")

/obj/item/organ/tongue/abductor/handle_speech(datum/source, list/speech_args)
	//Hacks
	var/message = speech_args[SPEECH_MESSAGE]
	var/mob/living/carbon/human/user = source
	var/rendered = span_abductor("<b>[user.real_name]:</b> [message]")
	user.log_talk(message, LOG_SAY, tag="abductor")
	for(var/mob/living/carbon/human/H in GLOB.alive_mob_list)
		var/obj/item/organ/tongue/abductor/T = H.getorganslot(ORGAN_SLOT_TONGUE)
		if(!istype(T))
			continue
		if(mothership == T.mothership)
			to_chat(H, rendered)

	for(var/mob/M in GLOB.dead_mob_list)
		var/link = FOLLOW_LINK(M, user)
		to_chat(M, "[link] [rendered]")

	speech_args[SPEECH_MESSAGE] = ""


code:
/obj/item/organ/tongue/alien
	name = "alien tongue"
	desc = "According to leading xenobiologists the evolutionary benefit of having a second mouth in your mouth is \"that it looks badass\"."
	icon_state = "tongue-x"
	say_mod = "hisses"
	taste_sensitivity = 10 // LIZARDS ARE ALIENS CONFIRMED
	modifies_speech = TRUE // not really, they just hiss
	var/static/list/languages_possible_alien = typecacheof(list(
		/datum/language/xenocommon,
		/datum/language/common,
		/datum/language/draconic,
		/datum/language/monkey))

/obj/item/organ/tongue/alien/Initialize(mapload)
	. = ..()
	languages_possible = languages_possible_alien

/obj/item/organ/tongue/alien/handle_speech(datum/source, list/speech_args)
	playsound(owner, "hiss", 25, TRUE, TRUE)


code:
/obj/item/organ/tongue/bone
	name = "bone \"tongue\""
	desc = "Apparently skeletons alter the sounds they produce through oscillation of their teeth, hence their characteristic rattling."
	icon_state = "tongue-bone"
	say_mod = "rattles"
	attack_verb_continuous = list("bites", "chatters", "chomps", "enamelles", "bones")
	attack_verb_simple = list("bite", "chatter", "chomp", "enamel", "bone")
	sense_of_taste = FALSE
	modifies_speech = TRUE
	var/chattering = FALSE
	var/phomeme_type = "sans"
	var/list/phomeme_types = list("sans", "papyrus")
	var/static/list/languages_possible_skeleton = typecacheof(list(
		/datum/language/common,
		/datum/language/draconic,
		/datum/language/codespeak,
		/datum/language/monkey,
		/datum/language/narsie,
		/datum/language/beachbum,
		/datum/language/aphasia,
		/datum/language/piratespeak,
		/datum/language/moffic,
		/datum/language/sylvan,
		/datum/language/shadowtongue,
		/datum/language/terrum,
		/datum/language/nekomimetic,
		/datum/language/calcic
	))

/obj/item/organ/tongue/bone/Initialize()
	. = ..()
	phomeme_type = pick(phomeme_types)
	languages_possible = languages_possible_skeleton

/obj/item/organ/tongue/bone/handle_speech(datum/source, list/speech_args)
	if (chattering)
		chatter(speech_args[SPEECH_MESSAGE], phomeme_type, source)
	switch(phomeme_type)
		if("sans")
			speech_args[SPEECH_SPANS] |= SPAN_SANS
		if("papyrus")
			speech_args[SPEECH_SPANS] |= SPAN_PAPYRUS


code:
/obj/item/organ/tongue/robot
	name = "robotic tongue"
	desc = "A voice synthesizer that can interface with organic lifeforms."
	status = ORGAN_ROBOTIC
	organ_flags = NONE
	icon_state = "tongue-c"
	say_mod = "states"
	attack_verb_continuous = list("beeps", "boops")
	attack_verb_simple = list("beep", "boop")
	modifies_speech = TRUE
	taste_sensitivity = 25 // not as good as an organic tongue

/obj/item/organ/tongue/robot/can_speak_language(language)
	return TRUE // THE MAGIC OF ELECTRONICS

/obj/item/organ/tongue/robot/handle_speech(datum/source, list/speech_args)
	speech_args[SPEECH_SPANS] |= SPAN_ROBOT


code:
/obj/item/organ/tongue/ethereal
	name = "electric discharger"
	desc = "A sophisticated ethereal organ, capable of synthesising speech via electrical discharge."
	icon_state = "tongue-electro"
	say_mod = "crackles"
	attack_verb_continuous = list("shocks", "jolts", "zaps")
	attack_verb_simple = list("shock", "jolt", "zap")
	sense_of_taste = FALSE
	var/static/list/languages_possible_ethereal = typecacheof(list(
		/datum/language/common,
		/datum/language/draconic,
		/datum/language/codespeak,
		/datum/language/monkey,
		/datum/language/narsie,
		/datum/language/beachbum,
		/datum/language/aphasia,
		/datum/language/piratespeak,
		/datum/language/moffic,
		/datum/language/sylvan,
		/datum/language/shadowtongue,
		/datum/language/terrum,
		/datum/language/nekomimetic,
		/datum/language/voltaic
	))

/obj/item/organ/tongue/ethereal/Initialize(mapload)
	. = ..()
	languages_possible = languages_possible_ethereal


code:
/obj/item/organ/tongue/fly
	name = "proboscis"
	desc = "A freakish looking meat tube that apparently can take in liquids."
	icon_state = "tongue-fly"
	say_mod = "buzzes"
	taste_sensitivity = 25 // you eat vomit, this is a mercy
	modifies_speech = TRUE
	var/static/list/languages_possible_fly = typecacheof(list(
		/datum/language/common,
		/datum/language/draconic,
		/datum/language/codespeak,
		/datum/language/monkey,
		/datum/language/narsie,
		/datum/language/beachbum,
		/datum/language/aphasia,
		/datum/language/piratespeak,
		/datum/language/moffic,
		/datum/language/sylvan,
		/datum/language/shadowtongue,
		/datum/language/terrum,
		/datum/language/nekomimetic,
		/datum/language/buzzwords
	))

/obj/item/organ/tongue/fly/handle_speech(datum/source, list/speech_args)
	var/static/regex/fly_buzz = new("z+", "g")
	var/static/regex/fly_buZZ = new("Z+", "g")
	var/message = speech_args[SPEECH_MESSAGE]
	if(message[1] != "*")
		message = fly_buzz.Replace(message, "zzz")
		message = fly_buZZ.Replace(message, "ZZZ")
		message = replacetext(message, "s", "z")
		message = replacetext(message, "S", "Z")
	speech_args[SPEECH_MESSAGE] = message

/obj/item/organ/tongue/fly/Initialize(mapload)
	. = ..()
	languages_possible = languages_possible_fly


code:
/obj/item/organ/tongue/lizard
	name = "forked tongue"
	desc = "A thin and long muscle typically found in reptilian races, apparently moonlights as a nose."
	icon_state = "tongue-lizard"
	say_mod = "hisses"
	taste_sensitivity = 10 // combined nose + tongue, extra sensitive
	modifies_speech = TRUE

/obj/item/organ/tongue/lizard/handle_speech(datum/source, list/speech_args)
	var/static/regex/lizard_hiss = new("s+", "g")
	var/static/regex/lizard_hiSS = new("S+", "g")
	var/static/regex/lizard_kss = new(@"(\w)x", "g")
	var/static/regex/lizard_kSS = new(@"(\w)X", "g")
	var/static/regex/lizard_ecks = new(@"\bx([\-|r|R]|\b)", "g")
	var/static/regex/lizard_eckS = new(@"\bX([\-|r|R]|\b)", "g")
	var/message = speech_args[SPEECH_MESSAGE]
	if(message[1] != "*")
		message = lizard_hiss.Replace(message, "sss")
		message = lizard_hiSS.Replace(message, "SSS")
		message = lizard_kss.Replace(message, "$1kss")
		message = lizard_kSS.Replace(message, "$1KSS")
		message = lizard_ecks.Replace(message, "ecks$1")
		message = lizard_eckS.Replace(message, "ECKS$1")
	speech_args[SPEECH_MESSAGE] = message


code:
/obj/item/organ/tongue/bone/plasmaman
	name = "plasma bone \"tongue\""
	desc = "Like animated skeletons, Plasmamen vibrate their teeth in order to produce speech."
	icon_state = "tongue-plasma"
	modifies_speech = FALSE


code:
/obj/item/organ/tongue/lizard/silver
	name = "silver tongue"
	desc = "A genetic branch of the high society Silver Scales that gives them their silverizing properties. To them, it is everything, and society traitors have their tongue forcibly revoked. Oddly enough, it itself is just blue."
	icon_state = "tongue-silver"
	actions_types = list(/datum/action/item_action/organ_action/statue)

/datum/action/item_action/organ_action/statue
	name = "Become Statue"
	desc = "Become an elegant silver statue. Its durability and yours are directly tied together, so make sure you're careful."
	COOLDOWN_DECLARE(ability_cooldown)

	var/obj/structure/statue/custom/statue

/datum/action/item_action/organ_action/statue/New(Target)
	. = ..()
	statue = new
	RegisterSignal(statue, COMSIG_PARENT_QDELETING, .proc/statue_destroyed)

/datum/action/item_action/organ_action/statue/Destroy()
	UnregisterSignal(statue, COMSIG_PARENT_QDELETING)
	QDEL_NULL(statue)
	return ..()

/datum/action/item_action/organ_action/statue/Trigger()
	. = ..()
	if(!iscarbon(owner))
		to_chat(owner, span_warning("Your body rejects the powers of the tongue!"))
		return
	var/mob/living/carbon/becoming_statue = owner
	if(becoming_statue.health < 1)
		to_chat(becoming_statue, span_danger("You are too weak to become a statue!"))
		return
	if(!COOLDOWN_FINISHED(src, ability_cooldown))
		to_chat(becoming_statue, span_warning("You just used the ability, wait a little bit!"))
		return
	var/is_statue = becoming_statue.loc == statue
	to_chat(becoming_statue, span_notice("You begin to [is_statue ? "break free from the statue" : "make a glorious pose as you become a statue"]!"))
	if(!do_after(becoming_statue, (is_statue ? 5 : 30), target = get_turf(becoming_statue)))
		to_chat(becoming_statue, span_warning("Your transformation is interrupted!"))
		COOLDOWN_START(src, ability_cooldown, 3 SECONDS)
		return
	COOLDOWN_START(src, ability_cooldown, 10 SECONDS)

	if(statue.name == initial(statue.name)) //statue has not been set up
		statue.name = "statue of [becoming_statue.real_name]"
		statue.desc = "statue depicting [becoming_statue.real_name]"
		statue.set_custom_materials(list(/datum/material/silver=MINERAL_MATERIAL_AMOUNT*5))

	if(is_statue)
		statue.visible_message(span_danger("[statue] becomes animated!"))
		becoming_statue.forceMove(get_turf(statue))
		statue.moveToNullspace()
		UnregisterSignal(becoming_statue, COMSIG_MOVABLE_MOVED)
	else
		becoming_statue.visible_message(span_notice("[becoming_statue] hardens into a silver statue."), \
										span_notice("You have become a silver statue!"))
		statue.set_visuals(becoming_statue.appearance)
		statue.forceMove(get_turf(becoming_statue))
		becoming_statue.forceMove(statue)
		statue.max_integrity = becoming_statue.health
		statue.repair_damage(statue.max_integrity)
		RegisterSignal(becoming_statue, COMSIG_MOVABLE_MOVED, .proc/human_left_statue)

//somehow they used an exploit/teleportation to leave statue, lets clean up
/datum/action/item_action/organ_action/statue/proc/human_left_statue(atom/movable/mover, atom/oldloc, direction)
	SIGNAL_HANDLER

	statue.moveToNullspace()
	UnregisterSignal(mover, COMSIG_MOVABLE_MOVED)

/datum/action/item_action/organ_action/statue/proc/statue_destroyed(datum/source)
	to_chat(owner, span_userdanger("Your existence as a living creature snaps as your statue form crumbles!"))
	if(iscarbon(owner))
		//drop everything, just in case
		var/mob/living/carbon/dying_carbon = owner
		for(var/obj/item/dropped in dying_carbon)
			if(!dying_carbon.dropItemToGround(dropped))
				qdel(dropped)
	qdel(owner)


code:
/obj/item/organ/tongue/snail
	name = "radula"
	color = "#96DB00" // TODO proper sprite, rather than recoloured pink tongue
	desc = "A minutely toothed, chitious ribbon, which as a side effect, makes all snails talk IINNCCRREEDDIIBBLLYY SSLLOOWWLLYY."
	modifies_speech = TRUE

/obj/item/organ/tongue/snail/handle_speech(datum/source, list/speech_args)
	var/new_message
	var/message = speech_args[SPEECH_MESSAGE]
	for(var/i in 1 to length(message))
		if(findtext("ABCDEFGHIJKLMNOPWRSTUVWXYZabcdefghijklmnopqrstuvwxyz", message[i])) //Im open to suggestions
			new_message += message[i] + message[i] + message[i] //aaalllsssooo ooopppeeennn tttooo sssuuuggggggeeessstttiiiooonsss
		else
			new_message += message[i]
	speech_args[SPEECH_MESSAGE] = new_message


code:
//Sign Language Tongue - yep, that's how you speak sign language.
/obj/item/organ/tongue/tied
	name = "tied tongue"
	desc = "If only one had a sword so we may finally untie this knot."
	say_mod = "signs"
	icon_state = "tongue-tied"
	modifies_speech = TRUE
	organ_flags = ORGAN_EDIBLE | ORGAN_UNREMOVABLE

/obj/item/organ/tongue/tied/Insert(mob/living/carbon/M)
	. = ..()
	M.verb_ask = "signs"
	M.verb_exclaim = "signs"
	M.verb_whisper = "subtly signs"
	M.verb_sing = "rythmically signs"
	M.verb_yell = "emphatically signs"
	ADD_TRAIT(M, TRAIT_SIGN_LANG, ORGAN_TRAIT)
	REMOVE_TRAIT(M, TRAIT_MUTE, ORGAN_TRAIT)

/obj/item/organ/tongue/tied/Remove(mob/living/carbon/M, special = 0)
	. = ..()
	M.verb_ask = initial(verb_ask)
	M.verb_exclaim = initial(verb_exclaim)
	M.verb_whisper = initial(verb_whisper)
	M.verb_sing = initial(verb_sing)
	M.verb_yell = initial(verb_yell)
	//People who are Ahealed get "cured" of their sign language-having ways. If I knew how to make the tied tongue persist through aheals, I'd do that.
	REMOVE_TRAIT(M, TRAIT_SIGN_LANG, ORGAN_TRAIT)

//Thank you Jwapplephobia for helping me with the literal hellcode below

/obj/item/organ/tongue/tied/handle_speech(datum/source, list/speech_args)
	var/new_message
	var/message = speech_args[SPEECH_MESSAGE]
	var/exclamation_found = findtext(message, "!")
	var/question_found = findtext(message, "?")
	var/mob/living/carbon/M = owner
	new_message = message
	if(exclamation_found)
		new_message = replacetext(new_message, "!", ".")
	if(question_found)
		new_message = replacetext(new_message, "?", ".")
	speech_args[SPEECH_MESSAGE] = new_message

	if(exclamation_found && question_found)
		M.visible_message(span_notice("[M] lowers one of [M.p_their()] eyebrows, raising the other."))
	else if(exclamation_found)
		M.visible_message(span_notice("[M] raises [M.p_their()] eyebrows."))
	else if(question_found)
		M.visible_message(span_notice("[M] lowers [M.p_their()] eyebrows."))


code:
/obj/item/organ/tongue/zombie
	name = "rotting tongue"
	desc = "Between the decay and the fact that it's just lying there you doubt a tongue has ever seemed less sexy."
	icon_state = "tongue-zombie"
	say_mod = "moans"
	modifies_speech = TRUE
	taste_sensitivity = 32

/obj/item/organ/tongue/zombie/handle_speech(datum/source, list/speech_args)
	var/list/message_list = splittext(speech_args[SPEECH_MESSAGE], " ")
	var/maxchanges = max(round(message_list.len / 1.5), 2)

	for(var/i = rand(maxchanges / 2, maxchanges), i > 0, i--)
		var/insertpos = rand(1, message_list.len - 1)
		var/inserttext = message_list[insertpos]

		if(!(copytext(inserttext, -3) == "..."))//3 == length("...")
			message_list[insertpos] = inserttext + "..."

		if(prob(20) && message_list.len > 3)
			message_list.Insert(insertpos, "[pick("BRAINS", "Brains", "Braaaiinnnsss", "BRAAAIIINNSSS")]...")

	speech_args[SPEECH_MESSAGE] = jointext(message_list, " ")


code:
/obj/item/organ/vocal_cords //organs that are activated through speech with the :x/MODE_KEY_VOCALCORDS channel
	name = "vocal cords"
	icon_state = "appendix"
	zone = BODY_ZONE_PRECISE_MOUTH
	organ_efficiency = list(ORGAN_SLOT_VOICE = 100)
	gender = PLURAL
	min_decay_factor = 0
	max_decay_factor = 0
	healing_factor = 0

	// a small price to pay for the powers these give
	organ_volume = 1
	max_blood_storage = 10
	current_blood = 10
	blood_req = 2
	oxygen_req = 2.5
	nutriment_req = 2.5

	var/list/spans = null

/obj/item/organ/vocal_cords/proc/can_speak_with() //if there is any limitation to speaking with these cords
	return TRUE

/obj/item/organ/vocal_cords/proc/speak_with(message) //do what the organ does
	return

/obj/item/organ/vocal_cords/proc/handle_speech(message) //actually say the message
	owner.say(message, spans = spans, sanitize = FALSE)

/obj/item/organ/adamantine_resonator
	name = "adamantine resonator"
	desc = "Fragments of adamantine exist in all golems, stemming from their origins as purely magical constructs. These are used to \"hear\" messages from their leaders."
	zone = BODY_ZONE_HEAD
	organ_efficiency = list(ORGAN_SLOT_ADAMANTINE_RESONATOR = 100)
	icon_state = "adamantine_resonator"

/obj/item/organ/vocal_cords/adamantine
	name = "adamantine vocal cords"
	desc = "When adamantine resonates, it causes all nearby pieces of adamantine to resonate as well. Adamantine golems use this to broadcast messages to nearby golems."
	actions_types = list(/datum/action/item_action/organ_action/use/adamantine_vocal_cords)
	icon_state = "adamantine_cords"

/datum/action/item_action/organ_action/use/adamantine_vocal_cords/Trigger()
	if(!IsAvailable())
		return
	var/message = input(owner, "Resonate a message to all nearby golems.", "Resonate")
	if(QDELETED(src) || QDELETED(owner) || !message)
		return
	owner.say(".x[message]")

/obj/item/organ/vocal_cords/adamantine/handle_speech(message)
	var/msg = span_resonate("[owner.real_name] resonates, \"[message]\"")
	for(var/m in GLOB.player_list)
		if(iscarbon(m))
			var/mob/living/carbon/C = m
			if(C.getorganslot(ORGAN_SLOT_ADAMANTINE_RESONATOR))
				to_chat(C, msg)
		if(isobserver(m))
			var/link = FOLLOW_LINK(m, owner)
			to_chat(m, "[link] [msg]")

//Colossus drop, forces the listeners to obey certain commands
/obj/item/organ/vocal_cords/colossus
	name = "divine vocal cords"
	desc = "They carry the voice of an ancient god."
	icon_state = "voice_of_god"
	actions_types = list(/datum/action/item_action/organ_action/colossus)
	var/next_command = 0
	var/cooldown_mod = 1
	var/base_multiplier = 1
	spans = list("colossus","yell")

/datum/action/item_action/organ_action/colossus
	name = "Voice of God"
	var/obj/item/organ/vocal_cords/colossus/cords = null

/datum/action/item_action/organ_action/colossus/New()
	. = ..()
	cords = target

/datum/action/item_action/organ_action/colossus/IsAvailable()
	if(world.time < cords.next_command)
		return FALSE
	if(!owner)
		return FALSE
	if(isliving(owner))
		var/mob/living/L = owner
		if(!L.can_speak_vocal())
			return FALSE
	if(check_flags & AB_CHECK_CONSCIOUS)
		if(owner.stat)
			return FALSE
	return TRUE

/datum/action/item_action/organ_action/colossus/Trigger()
	. = ..()
	if(!IsAvailable())
		if(world.time < cords.next_command)
			to_chat(owner, span_notice("You must wait [DisplayTimeText(cords.next_command - world.time)] before Speaking again."))
		return
	var/command = input(owner, "Speak with the Voice of God", "Command")
	if(QDELETED(src) || QDELETED(owner))
		return
	if(!command)
		return
	owner.say(".x[command]")

/obj/item/organ/vocal_cords/colossus/can_speak_with()
	if(world.time < next_command)
		to_chat(owner, span_notice("You must wait [DisplayTimeText(next_command - world.time)] before Speaking again."))
		return FALSE
	if(!owner)
		return FALSE
	if(!owner.can_speak_vocal())
		to_chat(owner, span_warning("You are unable to speak!"))
		return FALSE
	return TRUE

/obj/item/organ/vocal_cords/colossus/handle_speech(message)
	playsound(get_turf(owner), 'sound/magic/clockwork/invoke_general.ogg', 300, TRUE, 5)
	return //voice of god speaks for us

/obj/item/organ/vocal_cords/colossus/speak_with(message)
	var/cooldown = voice_of_god(uppertext(message), owner, spans, base_multiplier)
	next_command = world.time + (cooldown * cooldown_mod)


code:
//amputate limb
/datum/surgery_step/sever_limb
	name = "Sever limb"
	implements = list(TOOL_SAW = 80, \
			/obj/item/melee/arm_blade = 75, \
			/obj/item/chainsaw = 75, \
			/obj/item/mounted_chainsaw = 75, \
			/obj/item/fireaxe = 50, \
			/obj/item/hatchet = 40, \
			/obj/item/knife/butcher = 25)
	minimum_time = 32
	maximum_time = 96
	target_mobtypes = list(/mob/living/carbon/human)
	possible_locs = AMPUTATABLE_BODYPARTS
	surgery_flags = (STEP_NEEDS_INCISED|STEP_NEEDS_BROKEN)

/datum/surgery_step/sever_limb/validate_target(mob/living/carbon/target, mob/user)
	. = ..()
	var/obj/item/bodypart/part = target.get_bodypart(user.zone_selected)
	if(part?.is_stump())
		return FALSE

/datum/surgery_step/sever_limb/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	var/obj/item/bodypart/BP = target.get_bodypart(target_zone)
	display_results(user, target, \
		span_notice("You begin to sever [target]'s [parse_zone(target_zone)] by \the [BP.amputation_point_name]..."),
		span_notice("[user] begins to sever [target]'s [parse_zone(target_zone)]!"), \
		span_notice("[user] begins to sever [target]'s [parse_zone(target_zone)]!"))

/datum/surgery_step/sever_limb/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	var/mob/living/carbon/human/L = target
	var/obj/item/bodypart/BP = target.get_bodypart(target_zone)
	display_results(user, target, \
		span_notice("You sever [L]'s [parse_zone(target_zone)] from \the [BP.amputation_point_name]."), \
		span_notice("[user] severs [L]'s [parse_zone(target_zone)]!"), \
		span_notice("[user] severs [L]'s [parse_zone(target_zone)]!"))
	var/obj/item/bodypart/target_limb = target.get_bodypart(target_zone)
	target_limb.drop_limb()
	return SURGERY_ADVANCE

//amputate stump
/datum/surgery_step/sever_stump
	name = "Sever stump"
	implements = list(TOOL_SAW = 80, \
			/obj/item/melee/arm_blade = 75, \
			/obj/item/chainsaw = 75, \
			/obj/item/mounted_chainsaw = 75, \
			/obj/item/fireaxe = 50, \
			/obj/item/hatchet = 40, \
			/obj/item/knife/butcher = 25)
	minimum_time = 16
	maximum_time = 56
	target_mobtypes = list(/mob/living/carbon/human)
	possible_locs = AMPUTATABLE_BODYPARTS
	surgery_flags = (STEP_NEEDS_INCISED)

/datum/surgery_step/sever_stump/validate_target(mob/living/carbon/target, mob/user)
	. = ..()
	var/obj/item/bodypart/stump = target.get_bodypart(user.zone_selected)
	if(!stump?.is_stump())
		return FALSE

/datum/surgery_step/sever_stump/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	var/obj/item/bodypart/BP = target.get_bodypart(target_zone)
	display_results(user, target, \
		span_notice("You begin to sever [target]'s [parse_zone(target_zone)] by \the [BP.amputation_point_name]..."),
		span_notice("[user] begins to sever [target]'s [parse_zone(target_zone)]!"), \
		span_notice("[user] begins to sever [target]'s [parse_zone(target_zone)]!"))

/datum/surgery_step/sever_limb/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	var/mob/living/carbon/human/L = target
	var/obj/item/bodypart/BP = target.get_bodypart(target_zone)
	display_results(user, target, \
		span_notice("You sever [L]'s [parse_zone(target_zone)] from \the [BP.amputation_point_name]."), \
		span_notice("[user] severs [L]'s [parse_zone(target_zone)]!"), \
		span_notice("[user] severs [L]'s [parse_zone(target_zone)]!"))
	var/obj/item/bodypart/target_limb = target.get_bodypart(target_zone)
	target_limb.drop_limb()
	return SURGERY_ADVANCE


code:
//amputate limb
//(but robotic)
/datum/surgery_step/mechanic_sever_limb
	name = "Sever limb"
	implements = list(TOOL_WIRECUTTER = 80, \
			TOOL_SAW = 60)
	minimum_time = 32
	maximum_time = 96
	target_mobtypes = list(/mob/living/carbon/human)
	possible_locs = AMPUTATABLE_BODYPARTS
	requires_bodypart_type = BODYPART_ROBOTIC
	surgery_flags = (STEP_NEEDS_INCISED|STEP_NEEDS_BROKEN)
	skill_used = SKILL_ELECTRONICS

/datum/surgery_step/mechanic_sever_limb/validate_target(mob/living/carbon/target, mob/user)
	. = ..()
	var/obj/item/bodypart/part = target.get_bodypart(user.zone_selected)
	if(part?.is_stump())
		return FALSE

/datum/surgery_step/mechanic_sever_limb/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	var/obj/item/bodypart/BP = target.get_bodypart(target_zone)
	display_results(user, target, \
		span_notice("I begin to sever [target]'s [parse_zone(target_zone)] by \the [BP.amputation_point_name]..."), \
		span_notice("[user] begins to sever [target]'s [parse_zone(target_zone)]!"), \
		span_notice("[user] begins to sever [target]'s [parse_zone(target_zone)]!"))

/datum/surgery_step/mechanic_sever_limb/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	var/mob/living/carbon/human/L = target
	var/obj/item/bodypart/BP = target.get_bodypart(target_zone)
	display_results(user, target, \
		span_notice("I sever [L]'s [parse_zone(target_zone)] from \the [BP.amputation_point_name]."), \
		span_notice("[user] severs [L]'s [parse_zone(target_zone)]!"), \
		span_notice("[user] severs [L]'s [parse_zone(target_zone)]!"))
	var/obj/item/bodypart/target_limb = target.get_bodypart(target_zone)
	target_limb.drop_limb()
	return TRUE

//amputate stump
/datum/surgery_step/mechanic_sever_stump
	name = "Sever stump"
	implements = list(TOOL_SAW = 80, \
			/obj/item/melee/arm_blade = 75, \
			/obj/item/chainsaw = 75, \
			/obj/item/mounted_chainsaw = 75, \
			/obj/item/fireaxe = 50, \
			/obj/item/hatchet = 40, \
			/obj/item/knife/butcher = 25)
	minimum_time = 16
	maximum_time = 56
	target_mobtypes = list(/mob/living/carbon/human)
	possible_locs = AMPUTATABLE_BODYPARTS
	requires_bodypart_type = BODYPART_ROBOTIC
	surgery_flags = (STEP_NEEDS_INCISED)
	skill_used = SKILL_ELECTRONICS

/datum/surgery_step/mechanic_sever_stump/validate_target(mob/living/carbon/target, mob/user)
	. = ..()
	var/obj/item/bodypart/stump = target.get_bodypart(user.zone_selected)
	if(!stump?.is_stump())
		return FALSE

/datum/surgery_step/mechanic_sever_stump/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	var/obj/item/bodypart/BP = target.get_bodypart(target_zone)
	display_results(user, target, \
		span_notice("You begin to sever [target]'s [parse_zone(target_zone)] by \the [BP.amputation_point_name]..."),
		span_notice("[user] begins to sever [target]'s [parse_zone(target_zone)]!"), \
		span_notice("[user] begins to sever [target]'s [parse_zone(target_zone)]!"))

/datum/surgery_step/mechanic_sever_stump/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	var/mob/living/carbon/human/L = target
	var/obj/item/bodypart/BP = target.get_bodypart(target_zone)
	display_results(user, target, \
		span_notice("You sever [L]'s [parse_zone(target_zone)] from \the [BP.amputation_point_name]."), \
		span_notice("[user] severs [L]'s [parse_zone(target_zone)]!"), \
		span_notice("[user] severs [L]'s [parse_zone(target_zone)]!"))
	var/obj/item/bodypart/target_limb = target.get_bodypart(target_zone)
	target_limb.drop_limb()
	return SURGERY_ADVANCE


code:
//dumbasses
/datum/surgery_step/replace_limb
	name = "Replace limb"
	implements = list(/obj/item/storage/organbox = 90,
					/obj/item/bodypart = 80)
	minimum_time = 32
	maximum_time = 96
	requires_bodypart_type = null
	surgery_flags = (STEP_NEEDS_INCISED|STEP_NEEDS_RETRACTED|STEP_NEEDS_BROKEN)

/datum/surgery_step/replace_limb/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	if(istype(tool, /obj/item/storage/organbox) && istype(tool.contents[1], /obj/item/bodypart))
		tool = tool.contents[1]
	var/obj/item/bodypart/aug = tool
	if(aug.body_zone != target_zone)
		to_chat(user, span_warning("[tool] isn't the right type for [parse_zone(target_zone)]."))
		return -1
	var/obj/item/bodypart/L = target.get_bodypart(target_zone)
	if(L)
		display_results(user, target, \
			span_notice("You begin to augment [target]'s [parse_zone(user.zone_selected)]..."), \
			span_notice("[user] begins to augment [target]'s [parse_zone(user.zone_selected)] with [aug]."), \
			span_notice("[user] begins to augment [target]'s [parse_zone(user.zone_selected)]."))

/datum/surgery_step/replace_limb/success(mob/user, mob/living/carbon/target, target_zone, obj/item/bodypart/tool)
	var/obj/item/bodypart/L = target.get_bodypart(target_zone)
	if(L)
		if(istype(tool, /obj/item/storage/organbox))
			tool.icon_state = initial(tool.icon_state)
			tool.desc = initial(tool.desc)
			tool.cut_overlays()
			tool = tool.contents[1]
		if(istype(tool) && user.temporarilyRemoveItemFromInventory(tool))
			if(tool.body_zone == target_zone)
				tool.replace_limb(target, TRUE)
			else if(target_zone in tool.children_zones)
				var/obj/item/bodypart/BP
				for(var/obj/item/bodypart/candidate in tool)
					if(target_zone == candidate.body_zone)
						BP = candidate
				if(BP)
					BP.replace_limb(target, TRUE)
				else
					return FALSE
		display_results(user, target, \
			span_notice("You successfully augment [target]'s [parse_zone(target_zone)]."), \
			span_notice("[user] successfully augments [target]'s [parse_zone(target_zone)] with [tool]!"), \
			span_notice("[user] successfully augments [target]'s [parse_zone(target_zone)]!"))
		log_combat(user, target, "augmented", addition="by giving him new [parse_zone(target_zone)]")
	else
		to_chat(user, span_warning("[target] has no [parse_zone(target_zone)] there!"))
	return TRUE


code:
//Relocate bones
/datum/surgery_step/relocate_bones
	name = "Relocate bones"
	implements = list(TOOL_BONESET = 85)
	minimum_time = 25
	maximum_time = 75
	ignore_clothes = TRUE
	surgery_flags = (STEP_NEEDS_NOT_INCISED|STEP_NEEDS_DISLOCATED)
	skill_used = SKILL_MEDICINE

/datum/surgery_step/relocate_bones/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to relocate [target]'s [parse_zone(user.zone_selected)]..."), \
		span_notice("[user] begins to relocate [target]'s [parse_zone(user.zone_selected)] with [tool]."), \
		span_notice("[user] begins to relocate [target]'s [parse_zone(user.zone_selected)]."))

/datum/surgery_step/relocate_bones/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	if(istype(tool, /obj/item/stack))
		var/obj/item/stack/used_stack = tool
		used_stack.use(1)
	display_results(user, target, \
		span_notice("I successfully relocate [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] successfully relocates in [target]'s [parse_zone(target_zone)] with [tool]!"), \
		span_notice("[user] successfully relocates in [target]'s [parse_zone(target_zone)]!"))
	log_combat(user, target, "relocated bones in")
	var/obj/item/bodypart/borked = target.get_bodypart(target_zone)
	for(var/obj/item/organ/bone/bone as anything in borked?.getorganslotlist(ORGAN_SLOT_BONE))
		if(bone.organ_flags & ORGAN_SYNTHETIC)
			continue
		if(bone.bone_flags & BONE_JOINTED)
			bone.relocate()
	return ..()

// Set bones
/datum/surgery_step/set_bones
	name = "Set bones"
	implements = list(TOOL_BONESET = 80, \
			/obj/item/stack/sticky_tape/surgical = 70, \
			/obj/item/stack/sticky_tape/super = 50, \
			/obj/item/stack/sticky_tape = 30)
	minimum_time = 25
	maximum_time = 75
	surgery_flags = (STEP_NEEDS_INCISED|STEP_NEEDS_DISLOCATED) //i hate black people

/datum/surgery_step/set_bones/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to set the bones in [target]'s [parse_zone(user.zone_selected)]..."), \
		span_notice("[user] begins to set the bones in [target]'s [parse_zone(user.zone_selected)] with [tool]."), \
		span_notice("[user] begins to set the bones in [target]'s [parse_zone(user.zone_selected)]."))

/datum/surgery_step/set_bones/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	if(istype(tool, /obj/item/stack))
		var/obj/item/stack/used_stack = tool
		used_stack.use(1)
	display_results(user, target, \
		span_notice("I successfully set the bones in [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] successfully sets the bones in [target]'s [parse_zone(target_zone)] with [tool]!"), \
		span_notice("[user] successfully sets the bones in [target]'s [parse_zone(target_zone)]!"))
	log_combat(user, target, "set bones in")
	var/obj/item/bodypart/borked = target.get_bodypart(target_zone)
	for(var/obj/item/organ/bone/bone in borked?.getorganslotlist(ORGAN_SLOT_BONE))
		if(bone.organ_flags & ORGAN_SYNTHETIC)
			continue
		if(bone.bone_flags & BONE_JOINTED)
			bone.relocate()
	return ..()

/datum/surgery_step/set_bones/failure(mob/user, mob/living/target, target_zone, obj/item/tool, fail_prob = 0)
	. = ..()
	if(istype(tool, /obj/item/stack))
		var/obj/item/stack/used_stack = tool
		used_stack.use(1)

// Gel le bone
/datum/surgery_step/gel_bones
	name = "Gel bones"
	implements = list(/obj/item/stack/medical/bone_gel = 80, \
			/obj/item/stack/sticky_tape/surgical = 70, \
			/obj/item/stack/sticky_tape/super = 50, \
			/obj/item/stack/sticky_tape = 30)
	minimum_time = 25
	maximum_time = 75
	surgery_flags = (STEP_NEEDS_INCISED|STEP_NEEDS_BROKEN)

/datum/surgery_step/gel_bones/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin applying [tool] on the fracture in [target]'s [parse_zone(user.zone_selected)]..."), \
		span_notice("[user] begins applying [tool] on the fracture in [target]'s [parse_zone(user.zone_selected)] with [tool]."), \
		span_notice("[user] begins applying [tool] on the fracture in [target]'s [parse_zone(user.zone_selected)]."))

/datum/surgery_step/gel_bones/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool, default_display_results = FALSE)
	if(istype(tool, /obj/item/stack))
		var/obj/item/stack/used_stack = tool
		used_stack.use(1)
	display_results(user, target, \
		span_notice("I successfully apply [tool] on the fracture on [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] successfully applies [tool] on the fracture on [target]'s [parse_zone(target_zone)] with [tool]!"), \
		span_notice("[user] successfully applies [tool] on the fracture on [target]'s [parse_zone(target_zone)]!"))
	log_combat(user, target, "mended fracture")
	var/obj/item/bodypart/unfracture_me = target.get_bodypart(target_zone)
	for(var/obj/item/organ/bone/bone in unfracture_me.getorganslotlist(ORGAN_SLOT_BONE))
		if(bone.organ_flags & ORGAN_SYNTHETIC)
			continue
		bone.mend_compound_fracture()
		bone.mend_fracture()
	return ..()

/datum/surgery_step/gel_bones/failure(mob/user, mob/living/target, target_zone, obj/item/tool)
	. = ..()
	if(istype(tool, /obj/item/stack))
		var/obj/item/stack/used_stack = tool
		used_stack.use(1)


code:
//Relocate bones
/datum/surgery_step/relocate_bones_mechanic
	name = "Relocate bones"
	implements = list(TOOL_WRENCH = 85)
	minimum_time = 25
	maximum_time = 75
	ignore_clothes = TRUE
	surgery_flags = (STEP_NEEDS_NOT_INCISED|STEP_NEEDS_DISLOCATED)
	requires_bodypart_type = BODYPART_ROBOTIC
	skill_used = SKILL_ELECTRONICS

/datum/surgery_step/relocate_bones_mechanic/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to relocate [target]'s [parse_zone(user.zone_selected)]..."), \
		span_notice("[user] begins to relocate [target]'s [parse_zone(user.zone_selected)] with [tool]."), \
		span_notice("[user] begins to relocate [target]'s [parse_zone(user.zone_selected)]."))

/datum/surgery_step/relocate_bones_mechanic/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	if(istype(tool, /obj/item/stack))
		var/obj/item/stack/used_stack = tool
		used_stack.use(1)
	display_results(user, target, \
		span_notice("I successfully relocate [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] successfully relocates in [target]'s [parse_zone(target_zone)] with [tool]!"), \
		span_notice("[user] successfully relocates in [target]'s [parse_zone(target_zone)]!"))
	log_combat(user, target, "relocated bones in")
	var/obj/item/bodypart/borked = target.get_bodypart(target_zone)
	for(var/obj/item/organ/bone/bone as anything in borked?.getorganslotlist(ORGAN_SLOT_BONE))
		if(!(bone.organ_flags & ORGAN_SYNTHETIC))
			continue
		if(bone.bone_flags & BONE_JOINTED)
			bone.relocate()
	return ..()

// Set bones
/datum/surgery_step/mechanic_set_bones
	name = "Set bones"
	implements = list(TOOL_WRENCH = 80, \
			/obj/item/stack/sticky_tape = 30)
	minimum_time = 25
	maximum_time = 75
	surgery_flags = (STEP_NEEDS_INCISED|STEP_NEEDS_DISLOCATED) //i hate black people
	requires_bodypart_type = BODYPART_ROBOTIC
	skill_used = SKILL_ELECTRONICS

/datum/surgery_step/mechanic_set_bones/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to set the endoskeleton in [target]'s [parse_zone(user.zone_selected)]..."), \
		span_notice("[user] begins to set the endoskeleton in [target]'s [parse_zone(user.zone_selected)] with [tool]."), \
		span_notice("[user] begins to set the endoskeleton in [target]'s [parse_zone(user.zone_selected)]."))

/datum/surgery_step/mechanic_set_bones/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	if(istype(tool, /obj/item/stack))
		var/obj/item/stack/used_stack = tool
		used_stack.use(1)
	display_results(user, target, \
		span_notice("I successfully set the endoskeleton in [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] successfully sets the endoskeleton in [target]'s [parse_zone(target_zone)] with [tool]!"), \
		span_notice("[user] successfully sets the endoskeleton in [target]'s [parse_zone(target_zone)]!"))
	log_combat(user, target, "set bones in")
	var/obj/item/bodypart/borked = target.get_bodypart(target_zone)
	for(var/obj/item/organ/bone/bone in borked?.getorganslotlist(ORGAN_SLOT_BONE))
		if(!(bone.organ_flags & ORGAN_SYNTHETIC))
			continue
		if(bone.bone_flags & BONE_JOINTED)
			bone.relocate()
	return ..()

/datum/surgery_step/mechanic_set_bones/failure(mob/user, mob/living/target, target_zone, obj/item/tool, fail_prob = 0)
	. = ..()
	if(istype(tool, /obj/item/stack))
		var/obj/item/stack/used_stack = tool
		used_stack.use(1)

// Weld endoskeleton
//(mechanic gel bones)
/datum/surgery_step/mechanic_gel_bones
	name = "Weld endoskeleton"
	implements = list(TOOL_WELDER = 80)
	minimum_time = 25
	maximum_time = 75
	surgery_flags = (STEP_NEEDS_INCISED|STEP_NEEDS_BROKEN)
	requires_bodypart_type = BODYPART_ROBOTIC
	skill_used = SKILL_ELECTRONICS

/datum/surgery_step/mechanic_gel_bones/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin welding the fracture in [target]'s [parse_zone(user.zone_selected)]..."), \
		span_notice("[user] begins welding the fracture in [target]'s [parse_zone(user.zone_selected)] with [tool]."), \
		span_notice("[user] begins welding the fracture in [target]'s [parse_zone(user.zone_selected)]."))

/datum/surgery_step/mechanic_gel_bones/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool, default_display_results = FALSE)
	display_results(user, target, \
		span_notice("I successfully weld the fracture on [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] successfully weld the fracture on [target]'s [parse_zone(target_zone)] with [tool]!"), \
		span_notice("[user] successfully weld the fracture on [target]'s [parse_zone(target_zone)]!"))
	log_combat(user, target, "mended fracture")
	var/obj/item/bodypart/unfracture_me = target.get_bodypart(target_zone)
	for(var/obj/item/organ/bone/bone in unfracture_me.getorganslotlist(ORGAN_SLOT_BONE))
		if(!(bone.organ_flags & ORGAN_SYNTHETIC))
			continue
		bone.mend_compound_fracture()
		bone.mend_fracture()
	return ..()


code:
//REDPILL
/datum/surgery_step/insert_pill
	name = "Insert pill"
	implements = list(/obj/item/reagent_containers/pill = 80)
	minimum_time = 25
	maximum_time = 50
	requires_bodypart_type = null
	possible_locs = list(BODY_ZONE_PRECISE_MOUTH)
	surgery_flags = (STEP_NEEDS_INCISED|STEP_NEEDS_RETRACTED|STEP_NEEDS_DRILLED)

/datum/surgery_step/insert_pill/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	user.visible_message(span_notice("[user] begins to wedge \the [tool] in [target]'s [parse_zone(target_zone)]."), \
		span_notice("I begin to wedge [tool] in [target]'s [parse_zone(target_zone)]..."))

/datum/surgery_step/insert_pill/success(mob/user, mob/living/carbon/target, target_zone, var/obj/item/reagent_containers/pill/tool)
	if(!istype(tool))
		return FALSE

	user.transferItemToLoc(tool, target, TRUE)

	var/datum/action/item_action/hands_free/activate_pill/pill_action = new(tool)
	pill_action.button.name = "Activate [tool.name]"
	pill_action.target = tool
	pill_action.Grant(target)	//The pill never actually goes in an inventory slot, so the owner doesn't inherit actions from it

	user.visible_message(span_notice("[user] wedges \the [tool] into [target]'s [parse_zone(target_zone)]!"), \
		span_notice("I wedge [tool] into [target]'s [parse_zone(target_zone)]."))
	return TRUE

//LE ACTION!
/datum/action/item_action/hands_free/activate_pill
	name = "Activate Pill"

/datum/action/item_action/hands_free/activate_pill/Trigger()
	if(!..())
		return FALSE
	var/obj/item/item_target = target
	to_chat(owner, span_userdanger("You grit your teeth and burst the implanted [item_target.name]!"))
	log_combat(owner, null, "swallowed an implanted pill", item_target)
	if(item_target.reagents?.total_volume)
		item_target.reagents.trans_to(owner, item_target.reagents.total_volume)
	qdel(target)
	return TRUE


code:
/datum/surgery_step/dissect
	name = "Dissect"
	implements = list(TOOL_SCALPEL = 80, \
			/obj/item/melee/energy/sword = 75, \
			/obj/item/knife = 65, \
			/obj/item/shard = 45, \
			/obj/item = 45) // 45% success with any sharp item.
	minimum_time = 20
	maximum_time = 40
	surgery_flags = 0 //fucking FAGS

/datum/surgery_step/dissect/tool_check(mob/user, obj/item/tool, mob/living/carbon/target)
	. = ..()
	if((implement_type == /obj/item) && !(tool.get_sharpness() & SHARP_EDGED))
		return FALSE
	if(istype(tool, /obj/item/reagent_containers))
		return FALSE

/datum/surgery_step/dissect/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.) //nah nigga lol
		return FALSE
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/BP = C.get_bodypart(user.zone_selected)
	if(!BP.get_incision(TRUE))
		return FALSE

/datum/surgery_step/dissect/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to dissect [target]'s [parse_zone(target_zone)]..."), \
		span_danger("[user] begins to dissect [target]'s [parse_zone(target_zone)]."), \
		span_danger("[user] begins to dissect [target]'s [parse_zone(target_zone)]."))

/datum/surgery_step/dissect/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	. = ..()
	if(ishuman(target))
		var/obj/item/bodypart/BP = target.get_bodypart(target_zone)
		if(istype(BP))
			if(target.stat < UNCONSCIOUS)
				target.death_scream()
			for(var/obj/item/organ/bone/bone in BP.getorganslotlist(ORGAN_SLOT_BONE))
				bone.compound_fracture()
			if(BP.body_zone in (LIMB_BODYPARTS|BODY_ZONE_PRECISE_NECK))
				BP.apply_dismember(WOUND_SLASH, FALSE, FALSE)
			SEND_SIGNAL(target, COMSIG_CARBON_CLEAR_WOUND_MESSAGE)
			playsound(target, 'modular_septic/sound/gore/dissection.ogg', 80, 0)


code:
//Generic steps used in a lot of surgeries

//make incision
/datum/surgery_step/incise
	name = "Make incision"
	implements = list(TOOL_SCALPEL = 80, \
			/obj/item/melee/energy/sword = 75, \
			/obj/item/knife = 65, \
			/obj/item/shard = 45, \
			/obj/item = 45) // 45% success with any sharp item.
	minimum_time = 16
	maximum_time = 32
	surgery_flags = 0 //fucking FAGS

/datum/surgery_step/incise/tool_check(mob/user, obj/item/tool, mob/living/carbon/target)
	. = ..()
	if((implement_type == /obj/item) && !(tool.get_sharpness() & SHARP_EDGED))
		return FALSE
	if(istype(tool, /obj/item/reagent_containers))
		return FALSE

/datum/surgery_step/incise/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.) //nah nigga lol
		return FALSE
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/BP = C.get_bodypart(user.zone_selected)
	if(BP.get_incision(TRUE))
		return FALSE

/datum/surgery_step/incise/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to make an incision in [target]'s [parse_zone(target_zone)]..."), \
		span_notice("[user] begins to make an incision in [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] begins to make an incision in [target]'s [parse_zone(target_zone)]."))

/datum/surgery_step/incise/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	. = ..()
	if(ishuman(target))
		var/mob/living/carbon/human/H = target
		if(!(NOBLOOD in H.dna.species.species_traits))
			display_results(user, target, \
				span_notice("Blood pools around the incision in [H]'s [parse_zone(target_zone)]."), \
				span_notice("Blood pools around the incision in [H]'s [parse_zone(target_zone)]."))
		var/obj/item/bodypart/BP = target.get_bodypart(target_zone)
		if(istype(BP))
			var/datum/injury/ouchie = BP.create_injury(WOUND_SLASH, BP.max_damage * 0.3, TRUE)
			if(!ouchie)
				return
			ouchie.apply_injury(BP.max_damage * 0.3, BP)
			ouchie.injury_flags |= INJURY_SURGICAL
			SEND_SIGNAL(target, COMSIG_CARBON_CLEAR_WOUND_MESSAGE)
			playsound(target, 'modular_septic/sound/gore/flesh1.ogg', 75, 0)
			if(BP.current_splint)
				BP.remove_splint()
			if(BP.current_gauze)
				BP.remove_gauze()

//clamp bleeders
//Not a hard requirement, just needed if you don't want your patient to bleed out
/datum/surgery_step/clamp_bleeders
	name = "Clamp bleeders"
	implements = list(TOOL_HEMOSTAT = 80, \
			TOOL_WIRECUTTER = 60, \
			/obj/item/stack/package_wrap = 35, \
			/obj/item/stack/cable_coil = 15)
	minimum_time = 24
	maximum_time = 48

/datum/surgery_step/clamp_bleeders/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.)
		return
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/limb = C.get_bodypart(user.zone_selected)
	if(limb.is_clamped())
		return FALSE

/datum/surgery_step/clamp_bleeders/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to clamp bleeders in [target]'s [parse_zone(target_zone)]..."), \
		span_notice("[user] begins to clamp bleeders in [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] begins to clamp bleeders in [target]'s [parse_zone(target_zone)]."))

/datum/surgery_step/clamp_bleeders/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	. = ..()
	if(iscarbon(target))
		var/mob/living/carbon/C = target
		var/obj/item/bodypart/BP = C.get_bodypart(target_zone)
		if(BP)
			BP.clamp_limb()

//retract skin
/datum/surgery_step/retract_skin
	name = "Retract skin"
	implements = list(TOOL_RETRACTOR = 80, \
			TOOL_SCREWDRIVER = 45, \
			TOOL_WIRECUTTER = 35)
	minimum_time = 32
	maximum_time = 64

/datum/surgery_step/retract_skin/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.) //nah nigga lol
		return FALSE
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/BP = C.get_bodypart(user.zone_selected)
	var/datum/injury/incision = BP.get_incision()
	if(CHECK_BITFIELD(incision?.injury_flags, INJURY_RETRACTED))
		return FALSE

/datum/surgery_step/retract_skin/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to retract the skin in [target]'s [parse_zone(target_zone)]..."), \
		span_notice("[user] begins to retract the skin in [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] begins to retract the skin in [target]'s [parse_zone(target_zone)]."))

/datum/surgery_step/retract_skin/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	. = ..()
	var/obj/item/bodypart/BP = target.get_bodypart(target_zone)
	if(BP)
		BP.open_incision(user)
		if(!length(tool.embedding))
			tool.embedding = EMBED_HARMLESS
			tool.updateEmbedding()
		tool.tryEmbed(target = BP, forced = TRUE, silent = TRUE)
		tool.RegisterSignal(tool, COMSIG_ITEM_UNEMBEDDED, /obj/item/proc/unspeculumize, TRUE)
		playsound(target, 'modular_septic/sound/gore/stuck2.ogg', 60, 0)

//saw bone
/datum/surgery_step/saw
	name = "Saw bone"
	implements = list(TOOL_SAW = 85, \
			/obj/item/melee/arm_blade = 75, \
			/obj/item/fireaxe = 50, \
			/obj/item/hatchet = 35, \
			/obj/item/knife/butcher = 25)
	minimum_time = 32
	maximum_time = 96
	surgery_flags = (STEP_NEEDS_INCISED|STEP_NEEDS_RETRACTED)

/datum/surgery_step/saw/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.) //nah nigga lol
		return FALSE
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/BP = C.get_bodypart(check_zone(user.zone_selected))
	if(BP.is_fractured())
		return FALSE

/datum/surgery_step/saw/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("You begin to saw through the bone in [target]'s [parse_zone(target_zone)]..."), \
		span_notice("[user] begins to saw through the bone in [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] begins to saw through the bone in [target]'s [parse_zone(target_zone)]."))

/datum/surgery_step/saw/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("You saw [target]'s [parse_zone(target_zone)] open."), \
		span_notice("[user] saws [target]'s [parse_zone(target_zone)] open!"), \
		span_notice("[user] saws [target]'s [parse_zone(target_zone)] open!"))
	//oh GOD oh fuck we need to break this nigga's limb to continue surgery
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/BP = C.get_bodypart(target_zone)
	if(!BP.is_fractured())
		var/datum/wound/blunt/severe/fracture = new()
		fracture.apply_wound(BP, TRUE)
		SEND_SIGNAL(C, COMSIG_CARBON_CLEAR_WOUND_MESSAGE)
	return TRUE

//drill bone
/datum/surgery_step/drill
	name = "Drill bone"
	implements = list(TOOL_DRILL = 80, \
			/obj/item/screwdriver/power = 80, \
			/obj/item/pickaxe/drill = 60, \
			TOOL_SCREWDRIVER = 20)
	minimum_time = 32
	maximum_time = 96
	surgery_flags = (STEP_NEEDS_INCISED | STEP_NEEDS_RETRACTED)

/datum/surgery_step/drill/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.) //nah nigga lol
		return FALSE
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/BP = C.get_bodypart(user.zone_selected)
	if(CHECK_BITFIELD(BP?.how_open(), SURGERY_DRILLED))
		return FALSE

/datum/surgery_step/drill/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("You begin to drill into the bone in [target]'s [parse_zone(target_zone)]..."), \
		span_notice("[user] begins to drill into the bone in [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] begins to drill into the bone in [target]'s [parse_zone(target_zone)]."))

/datum/surgery_step/drill/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("You drill into [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] drills into [target]'s [parse_zone(target_zone)]!"), \
		span_notice("[user] drills into [target]'s [parse_zone(target_zone)]!"))
	var/obj/item/bodypart/BP = target.get_bodypart(user.zone_selected)
	var/datum/injury/incision = BP.get_incision()
	incision?.injury_flags |= INJURY_DRILLED
	return TRUE

//Cauterize incision
/datum/surgery_step/close
	name = "Cauterize"
	implements = list(TOOL_CAUTERY = 80, \
			/obj/item/gun/energy/laser = 65, \
			/obj/item = 60) // 65% success with any hot item.
	minimum_time = 16
	maximum_time = 48

/datum/surgery_step/close/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.) //nah nigga lol
		return FALSE
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/BP = C.get_bodypart(user.zone_selected)
	for(var/datum/injury/IN in BP.injuries)
		if(IN.damage_type == WOUND_SLASH || IN.damage_type == WOUND_PIERCE)
			return TRUE
	return FALSE

/datum/surgery_step/close/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("You begin cauterizing the wounds in [target]'s [parse_zone(target_zone)]..."), \
		span_notice("[user] begins to cauterize the wounds in [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] begins to cauterize the wounds in [target]'s [parse_zone(target_zone)]."))

/datum/surgery_step/close/tool_check(mob/user, obj/item/tool, mob/living/carbon/target)
	if(implement_type == TOOL_WELDER || implement_type == /obj/item)
		return tool.get_temperature()
	return TRUE

/datum/surgery_step/close/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	. = ..()
	if(iscarbon(target))
		var/mob/living/carbon/C = target
		var/obj/item/bodypart/BP = C.get_bodypart(target_zone)
		if(istype(BP))
			for(var/datum/injury/inch in BP.injuries)
				if(inch.is_surgical())
					inch.clamp_injury()
					inch.close_injury()
				else
					inch.clamp_injury()
		if(BP.is_clamped())
			BP.unclamp_limb()

//disinfect injuries
/datum/surgery_step/disinfect_injuries
	name = "Disinfect injuries"
	implements = list(/obj/item/reagent_containers = 80)
	minimum_time = 16
	maximum_time = 32
	surgery_flags = 0
	skill_used = SKILL_MEDICINE

/datum/surgery_step/disinfect_injuries/tool_check(mob/user, obj/item/tool, mob/living/carbon/target)
	. = ..()
	if(.)
		var/obj/item/reagent_containers/RC = tool
		if(!istype(RC) || !RC.is_drainable())
			return FALSE
		if(!RC.reagents?.has_reagent(/datum/reagent/space_cleaner/sterilizine, 10) && !RC.reagents.has_reagent(/datum/reagent/consumable/ethanol, 30))
			return FALSE
		if(RC.reagents.has_reagent(/datum/reagent/consumable/ethanol, 30))
			var/datum/reagent/consumable/ethanol/ethanol = RC.reagents.get_reagent(/datum/reagent/consumable/ethanol)
			if(ethanol.boozepwr < 40)
				return FALSE

/datum/surgery_step/disinfect_injuries/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.)
		return FALSE
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/BP = C.get_bodypart(user.zone_selected)
	if(BP.is_disinfected() && BP.is_salved())
		return FALSE

/datum/surgery_step/disinfect_injuries/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to disinfect the injuries on [target]'s [parse_zone(target_zone)]..."), \
		span_notice("[user] begins to sterilize the injuries on [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] begins to sterilize the injuries on [target]'s [parse_zone(target_zone)]."))

/datum/surgery_step/disinfect_injuries/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I sterilize the injuries on [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] sterilizes the injuries on [target]'s [parse_zone(target_zone)]!"), \
		span_notice("[user] sterilizes the injuries on [target]'s [parse_zone(target_zone)]!"))
	var/obj/item/bodypart/BP = target.get_bodypart(target_zone)
	BP?.disinfect_limb()
	BP?.salve_limb()
	return TRUE


code:
//open shell
/datum/surgery_step/mechanic_incise
	name = "Unscrew shell"
	implements = list(TOOL_SCREWDRIVER	= 80, \
			TOOL_DRILL = 70, \
			TOOL_SCALPEL = 70, \
			/obj/item/knife	= 50, \
			/obj/item = 45) // 45% success with any pointy item.
	minimum_time = 16
	maximum_time = 32
	surgery_flags = 0 //fucking FAGS
	requires_bodypart_type = BODYPART_ROBOTIC
	skill_used = SKILL_ELECTRONICS

/datum/surgery_step/mechanic_incise/tool_check(mob/user, obj/item/tool, mob/living/carbon/target)
	. = ..()
	if(implement_type == /obj/item && !(tool.get_sharpness() & SHARP_POINTY))
		return FALSE
	if(istype(tool, /obj/item/reagent_containers))
		return FALSE

/datum/surgery_step/mechanic_incise/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.) //nah nigga lol
		return FALSE
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/BP = C.get_bodypart(user.zone_selected)
	if(BP.get_incision(TRUE))
		return FALSE

/datum/surgery_step/mechanic_incise/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to unscrew the shell of [target]'s [parse_zone(target_zone)]..."), \
		span_notice("[user] begins to unscrew the shell of [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] begins to unscrew the shell of [target]'s [parse_zone(target_zone)]."))

/datum/surgery_step/mechanic_incise/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	. = ..()
	if(ishuman(target))
		var/mob/living/carbon/human/H = target
		if(!(NOBLOOD in H.dna.species.species_traits))
			display_results(user, target, \
				span_notice("Blood pools around the incision in [H]'s [parse_zone(target_zone)]."), \
				span_notice("Blood pools around the incision in [H]'s [parse_zone(target_zone)]."))
		var/obj/item/bodypart/BP = target.get_bodypart(target_zone)
		if(istype(BP))
			var/datum/injury/ouchie = BP.create_injury(WOUND_SLASH, BP.max_damage * 0.3, TRUE)
			if(!ouchie)
				return
			ouchie.apply_injury(BP.max_damage * 0.3, BP)
			ouchie.injury_flags |= INJURY_SURGICAL
			SEND_SIGNAL(target, COMSIG_CARBON_CLEAR_WOUND_MESSAGE)
			if(BP.current_splint)
				BP.remove_splint()
			if(BP.current_gauze)
				BP.remove_gauze()

//short circuits
//(mechanical equivalente of clamp bleeders)
/datum/surgery_step/mechanic_clamp_bleeders
	name = "Short circuits"
	implements = list(TOOL_MULTITOOL = 80, \
			TOOL_WIRECUTTER = 70) // try to reroute blood and electricity conductors
	minimum_time = 24
	maximum_time = 48
	requires_bodypart_type = BODYPART_ROBOTIC
	skill_used = SKILL_ELECTRONICS

/datum/surgery_step/mechanic_clamp_bleeders/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.)
		return
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/limb = C.get_bodypart(user.zone_selected)
	if(limb.is_clamped())
		return FALSE

/datum/surgery_step/mechanic_clamp_bleeders/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to short circuits in [target]'s [parse_zone(target_zone)]..."), \
		span_notice("[user] begins to short circuits in in [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] begins to short circuits in in [target]'s [parse_zone(target_zone)]."))

/datum/surgery_step/mechanic_clamp_bleeders/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	. = ..()
	if(iscarbon(target))
		var/mob/living/carbon/C = target
		var/obj/item/bodypart/BP = C.get_bodypart(target_zone)
		if(BP)
			BP.clamp_limb()

//Pry shell
//(mechanical equivalent of retract skin)
/datum/surgery_step/mechanic_retract_skin
	name = "Pry shell"
	implements = list(TOOL_CROWBAR = 80, \
			/obj/item = 45) //45% with any pointy item
	minimum_time = 32
	maximum_time = 64
	requires_bodypart_type = BODYPART_ROBOTIC
	skill_used = SKILL_ELECTRONICS

/datum/surgery_step/mechanic_retract_skin/tool_check(mob/user, obj/item/tool, mob/living/carbon/target)
	. = ..()
	if(implement_type == /obj/item && !(tool.get_sharpness() & SHARP_POINTY))
		return FALSE
	if(istype(tool, /obj/item/reagent_containers))
		return FALSE

/datum/surgery_step/mechanic_retract_skin/tool_check(mob/user, obj/item/tool, mob/living/carbon/target)
	. = ..()
	if(implement_type == /obj/item && !(tool.get_sharpness() & SHARP_POINTY))
		return FALSE
	if(istype(tool, /obj/item/reagent_containers))
		return FALSE

/datum/surgery_step/mechanic_retract_skin/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.) //nah nigga lol
		return FALSE
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/BP = C.get_bodypart(user.zone_selected)
	var/datum/injury/incision = BP.get_incision()
	if(CHECK_BITFIELD(incision?.injury_flags, INJURY_RETRACTED))
		return FALSE

/datum/surgery_step/mechanic_retract_skin/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to pry the shell from [target]'s [parse_zone(target_zone)]..."), \
		span_notice("[user] begin to pry the shell from [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] begin to pry the shell from [target]'s [parse_zone(target_zone)]."))

/datum/surgery_step/mechanic_retract_skin/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	. = ..()
	var/obj/item/bodypart/BP = target.get_bodypart(check_zone(target_zone))
	if(BP)
		BP.open_incision(user)

//saw endoskeleton
//(mechanical equivalent of sawing through bone)
/datum/surgery_step/mechanic_saw
	name = "Saw endoskeleton"
	implements = list(TOOL_SAW = 85, \
			/obj/item/melee/arm_blade = 75, \
			/obj/item/fireaxe = 50, \
			/obj/item/hatchet = 35, \
			/obj/item/knife/butcher = 25)
	minimum_time = 32
	maximum_time = 96
	surgery_flags = (STEP_NEEDS_INCISED|STEP_NEEDS_RETRACTED)
	requires_bodypart_type = BODYPART_ROBOTIC
	skill_used = SKILL_ELECTRONICS

/datum/surgery_step/mechanic_saw/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.) //nah nigga lol
		return FALSE
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/BP = C.get_bodypart(check_zone(user.zone_selected))
	if(BP.is_fractured())
		return FALSE

/datum/surgery_step/mechanic_saw/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("You begin to saw through the bone in [target]'s [parse_zone(target_zone)]..."), \
		span_notice("[user] begins to saw through the bone in [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] begins to saw through the bone in [target]'s [parse_zone(target_zone)]."))

/datum/surgery_step/mechanic_saw/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("You saw [target]'s [parse_zone(target_zone)] open."), \
		span_notice("[user] saws [target]'s [parse_zone(target_zone)] open!"), \
		span_notice("[user] saws [target]'s [parse_zone(target_zone)] open!"))
	//oh GOD oh fuck we need to break this nigga's limb to continue surgery
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/BP = C.get_bodypart(target_zone)
	if(!BP.is_fractured())
		var/datum/wound/blunt/severe/fracture = new()
		fracture.apply_wound(BP, TRUE)
		SEND_SIGNAL(C, COMSIG_CARBON_CLEAR_WOUND_MESSAGE)
	return TRUE

//close shell
//(mechanical equivalent of mend incision)
/datum/surgery_step/mechanic_close
	name = "Screw shell"
	implements = list(TOOL_SCREWDRIVER = 80, \
			TOOL_DRILL = 70, \
			TOOL_SCALPEL = 70, \
			/obj/item/knife	= 50, \
			/obj/item = 35) // 35% success with any sharp item.
	minimum_time = 16
	maximum_time = 48
	requires_bodypart_type = BODYPART_ROBOTIC
	skill_used = SKILL_ELECTRONICS

/datum/surgery_step/mechanic_close/tool_check(mob/user, obj/item/tool, mob/living/carbon/target)
	. = ..()
	if(implement_type == /obj/item && !(tool.get_sharpness() & SHARP_POINTY))
		return FALSE
	if(istype(tool, /obj/item/reagent_containers))
		return FALSE

/datum/surgery_step/mechanic_close/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.) //nah nigga lol
		return FALSE
	var/mob/living/carbon/C = target
	var/obj/item/bodypart/BP = C.get_bodypart(user.zone_selected)
	for(var/datum/injury/IN as anything in BP.injuries)
		if(IN.damage_type == WOUND_SLASH || IN.damage_type == WOUND_PIERCE)
			return TRUE
	return FALSE

/datum/surgery_step/mechanic_close/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to screw the shell of [target]'s [parse_zone(target_zone)]..."), \
		span_notice("[user] begins to screw the shell of [target]'s [parse_zone(target_zone)]."), \
		span_notice("[user] begins to screw the shell of [target]'s [parse_zone(target_zone)]."))

/datum/surgery_step/mechanic_close/success(mob/user, mob/living/target, target_zone, obj/item/tool)
	. = ..()
	if(iscarbon(target))
		var/mob/living/carbon/C = target
		var/obj/item/bodypart/BP = C.get_bodypart(target_zone)
		if(istype(BP))
			for(var/datum/injury/inch in BP.injuries)
				if(inch.is_surgical())
					inch.clamp_injury()
					inch.close_injury()
				else
					inch.clamp_injury()
		if(BP.is_clamped())
			BP.unclamp_limb()

/datum/surgery_step/mechanic_close/tool_check(mob/user, obj/item/tool, mob/living/carbon/target)
	if(implement_type == /obj/item && !tool.get_sharpness())
		return FALSE
	return TRUE


code:
//reshape_face
/datum/surgery_step/reshape_face
	name = "Reshape face"
	implements = list(/obj/item/stack/medical/mesh = 80)
	possible_locs = list(BODY_ZONE_PRECISE_FACE)
	surgery_flags = (STEP_NEEDS_INCISED|STEP_NEEDS_RETRACTED)
	minimum_time = 48
	maximum_time = 128
	requires_bodypart_type = BODYPART_ORGANIC
	skill_used = SKILL_ELECTRONICS

/datum/surgery_step/reshape_face/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.)
		return
	var/obj/item/bodypart/face/face = target.get_bodypart_nostump(BODY_ZONE_PRECISE_FACE)
	if(!face)
		return FALSE
	if(!HAS_TRAIT_FROM(target, TRAIT_DISFIGURED, TRAIT_GENERIC) && \
	 !HAS_TRAIT_FROM(target, TRAIT_DISFIGURED, GERM_LEVEL) && \
	 !HAS_TRAIT_FROM(target, TRAIT_DISFIGURED, BRUTE) && \
	 !HAS_TRAIT_FROM(target, TRAIT_DISFIGURED, BURN) && \
	 !HAS_TRAIT_FROM(target, TRAIT_DISFIGURED, ACID) && \
	 !HAS_TRAIT_FROM(face, TRAIT_DISFIGURED, TRAIT_GENERIC) && \
	 !HAS_TRAIT_FROM(face, TRAIT_DISFIGURED, GERM_LEVEL) && \
	 !HAS_TRAIT_FROM(face, TRAIT_DISFIGURED, BRUTE) && \
	 !HAS_TRAIT_FROM(face, TRAIT_DISFIGURED, BURN) && \
	 !HAS_TRAIT_FROM(face, TRAIT_DISFIGURED, ACID))
		return FALSE

/datum/surgery_step/reshape_face/tool_check(mob/user, obj/item/tool, mob/living/carbon/target)
	. = ..()
	if(!.)
		return
	var/obj/item/stack/chungus = tool
	if(chungus.amount < 3)
		return FALSE

/datum/surgery_step/reshape_face/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to alter [target]'s appearance..."), \
		span_notice("[user] begins to alter [target]'s appearance."), \
		span_notice("[user] begins to make an incision in [target]'s face."))

/datum/surgery_step/reshape_face/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	var/obj/item/stack/chungus = tool
	if(istype(chungus))
		chungus.use(3)
	var/obj/item/bodypart/face/face = target.get_bodypart_nostump(BODY_ZONE_PRECISE_FACE)
	if(HAS_TRAIT(target, TRAIT_DISFIGURED) || (face && HAS_TRAIT(face, TRAIT_DISFIGURED)))
		REMOVE_TRAIT(target, TRAIT_DISFIGURED, TRAIT_GENERIC)
		REMOVE_TRAIT(target, TRAIT_DISFIGURED, GERM_LEVEL)
		REMOVE_TRAIT(target, TRAIT_DISFIGURED, BRUTE)
		REMOVE_TRAIT(target, TRAIT_DISFIGURED, BURN)
		REMOVE_TRAIT(target, TRAIT_DISFIGURED, ACID)
		if(face)
			REMOVE_TRAIT(face, TRAIT_DISFIGURED, TRAIT_GENERIC)
			REMOVE_TRAIT(face, TRAIT_DISFIGURED, GERM_LEVEL)
			REMOVE_TRAIT(face, TRAIT_DISFIGURED, BRUTE)
			REMOVE_TRAIT(face, TRAIT_DISFIGURED, BURN)
			REMOVE_TRAIT(face, TRAIT_DISFIGURED, ACID)
		display_results(user, target, \
			span_notice("I successfully restore [target]'s appearance."), \
			span_notice("[user] successfully restores [target]'s appearance!"), \
			span_notice("[user] finishes the operation on [target]'s face."))
	else
		var/list/names = list()
		if(!isabductor(user))
			for(var/i in 1 to 10)
				names += target.dna.species.random_name(target.gender, TRUE)
		else
			for(var/_i in 1 to 9)
				names += "Subject [target.gender == MALE ? "i" : "o"]-[pick("a", "b", "c", "d", "e")]-[rand(10000, 99999)]"
			names += target.dna.species.random_name(target.gender, TRUE) //give one normal name in case they want to do regular plastic surgery
		var/chosen_name = input(user, "Choose a new name to assign.", "Plastic Surgery") as null|anything in names
		if(!chosen_name)
			return
		var/oldname = target.real_name
		target.real_name = chosen_name
		var/newname = target.real_name	//something about how the code handles names required that I use this instead of target.real_name
		display_results(user, target, \
			span_notice("I alter [oldname]'s appearance completely, [target.p_they()] is now [newname]."), \
			span_notice("[user] alters [oldname]'s appearance completely, [target.p_they()] is now [newname]!"), \
			span_notice("[user] finishes the operation on [target]'s face."))
	if(ishuman(target))
		var/mob/living/carbon/human/human = target
		human.sec_hud_set_ID()
	return TRUE

/datum/surgery_step/reshape_face/failure(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	var/obj/item/stack/chungus = tool
	if(istype(chungus))
		chungus.use(3)
	display_results(user, target, \
		span_warning("I screw up, leaving [target]'s appearance even more disfigured!"), \
		span_warning("[user] screws up, disfiguring [target]'s appearance even more!"), \
		span_warning("[user] fucks up the operation on [target]'s face."))
	ADD_TRAIT(target, TRAIT_DISFIGURED, BRUTE)
	var/obj/item/bodypart/face/face = target.get_bodypart_nostump(BODY_ZONE_PRECISE_FACE)
	if(face)
		ADD_TRAIT(face, TRAIT_DISFIGURED, BRUTE)
	return FALSE


code:
//reshape_face
/datum/surgery_step/mechanic_reshape_face
	name = "Reshape monitor"
	implements = list(/obj/item/stack/medical/nanopaste = 80)
	possible_locs = list(BODY_ZONE_PRECISE_FACE)
	surgery_flags = (STEP_NEEDS_INCISED|STEP_NEEDS_RETRACTED)
	minimum_time = 48
	maximum_time = 128
	requires_bodypart_type = BODYPART_ROBOTIC
	skill_used = SKILL_ELECTRONICS

/datum/surgery_step/mechanic_reshape_face/validate_target(mob/living/target, mob/user)
	. = ..()
	if(!.)
		return
	if(!HAS_TRAIT_FROM(target, TRAIT_DISFIGURED, TRAIT_GENERIC) && \
	 !HAS_TRAIT_FROM(target, TRAIT_DISFIGURED, GERM_LEVEL) && \
	 !HAS_TRAIT_FROM(target, TRAIT_DISFIGURED, BRUTE) && \
	 !HAS_TRAIT_FROM(target, TRAIT_DISFIGURED, BURN) && \
	 !HAS_TRAIT_FROM(target, TRAIT_DISFIGURED, ACID))
		return FALSE

/datum/surgery_step/mechanic_reshape_face/tool_check(mob/user, obj/item/tool, mob/living/carbon/target)
	. = ..()
	if(!.)
		return
	var/obj/item/stack/chungus = tool
	if(chungus.amount < 3)
		return FALSE

/datum/surgery_step/mechanic_reshape_face/preop(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	display_results(user, target, \
		span_notice("I begin to alter [target]'s appearance..."), \
		span_notice("[user] begins to alter [target]'s appearance."), \
		span_notice("[user] begins to make an incision in [target]'s face."))

/datum/surgery_step/mechanic_reshape_face/success(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	var/obj/item/stack/chungus = tool
	if(istype(chungus))
		chungus.use(3)
	if(HAS_TRAIT(target, TRAIT_DISFIGURED))
		REMOVE_TRAIT(target, TRAIT_DISFIGURED, TRAIT_GENERIC)
		REMOVE_TRAIT(target, TRAIT_DISFIGURED, GERM_LEVEL)
		REMOVE_TRAIT(target, TRAIT_DISFIGURED, BRUTE)
		REMOVE_TRAIT(target, TRAIT_DISFIGURED, BURN)
		REMOVE_TRAIT(target, TRAIT_DISFIGURED, ACID)
		display_results(user, target, \
			span_notice("I successfully restore [target]'s appearance."), \
			span_notice("[user] successfully restores [target]'s appearance!"), \
			span_notice("[user] finishes the operation on [target]'s face."))
	else
		var/list/names = list()
		if(!isabductor(user))
			for(var/i in 1 to 10)
				names += target.dna.species.random_name(target.gender, TRUE)
		else
			for(var/_i in 1 to 9)
				names += "Subject [target.gender == MALE ? "i" : "o"]-[pick("a", "b", "c", "d", "e")]-[rand(10000, 99999)]"
			names += target.dna.species.random_name(target.gender, TRUE) //give one normal name in case they want to do regular plastic surgery
		var/chosen_name = input(user, "Choose a new name to assign.", "Plastic Surgery") as null|anything in names
		if(!chosen_name)
			return
		var/oldname = target.real_name
		target.real_name = chosen_name
		var/newname = target.real_name	//something about how the code handles names required that I use this instead of target.real_name
		display_results(user, target, \
			span_notice("I alter [oldname]'s appearance completely, [target.p_they()] is now [newname]."), \
			span_notice("[user] alters [oldname]'s appearance completely, [target.p_they()] is now [newname]!"), \
			span_notice("[user] finishes the operation on [target]'s face."))
	if(ishuman(target))
		var/mob/living/carbon/human/H = target
		H.sec_hud_set_ID()
	return TRUE

/datum/surgery_step/mechanic_reshape_face/failure(mob/user, mob/living/carbon/target, target_zone, obj/item/tool)
	var/obj/item/stack/chungus = tool
	if(istype(chungus))
		chungus.use(3)
	display_results(user, target, \
		span_warning("I screw up, leaving [target]'s appearance even disfigured!"), \
		span_warning("[user] screws up, disfiguring [target]'s appearance!"), \
		span_warning("[user] fucks up the operation on [target]'s face."))
	ADD_TRAIT(target, TRAIT_DISFIGURED, TRAIT_GENERIC)
	return FALSE