code:
:: Multiple Organ Implant [nobr]

You head down to your
<<if $surgeryUpgrade == 1>>
	heavily upgraded and customized
<</if>>
remote surgery and start having the slaves with
<<if ($completedOrgans.length > 0) && ($adjustProstheticsCompleted > 0)>>
	organs or prosthetics
<<elseif ($completedOrgans.length > 1)>>
	organs
<<elseif ($adjustProstheticsCompleted > 1)>>
	prosthetics
<</if>>
that are ready be sent down.

Implant Organs

<<set _F = App.Medicine.OrganFarm>>
<<for _i = 0; _i < $slaves.length; _i++>>
	<<set $AS = $slaves[_i].ID>>
	<<set _sortedOrgans = _F.getSortedOrgans(getSlave($AS))>>
	<<if _sortedOrgans.length === 0>><<continue>><</if>>

<<= getSlave($AS).slaveName>>

	<<for _k = 0; _k < _sortedOrgans.length; _k++>>

<<print _F.Organs[_sortedOrgans[_k]].name>>

		<<set _actions = _F.Organs[_sortedOrgans[_k]].implantActions>>
		<<set _manual = false, _success = false, _break = false>>
		<<for _l = 0; _l < _actions.length; _l++>>
			<<if !_actions[_l].autoImplant>>
				<<if _actions[_l].canImplant(getSlave($AS))>>
					<<set _manual = true>>
				<</if>>
				<<continue>>
			<</if>>
			<<if !_actions[_l].canImplant(getSlave($AS))>>
				<<set _error = _actions[_l].implantError(getSlave($AS))>>
				<<if _error !== "">>
					@@.red;ERROR: <<print _error>>@@

				<</if>>
			<<elseif getSlave($AS).health.health - _actions[_l].healthImpact < -75>>
				Estimated health impact too high, skipping further surgeries.
				<<set _break = true>>
				<<break>>
			<<else>>
				<<run App.Medicine.OrganFarm.implant(getSlave($AS), _sortedOrgans[_k], _l)>>
				<<include "Surgery Degradation">>

				<<set _success = true>>
			<</if>>
		<</for>>
		<<if _break>><<break>><</if>>
		<<if !_success && _manual>>
			//Cannot implant <<print _F.Organs[_sortedOrgans[_k]].name.toLowerCase()>> automatically, try implanting manually in the remote surgery.//
		<</if>>
	<</for>>
<</for>>

Implant prosthetics

/* prosthetics */
<<for _i = 0; _i < $slaves.length; _i++>>
	<<set $AS = $slaves[_i].ID>>

	/* count for estimating health impact */
	<<set _prostheticCount = $adjustProsthetics.filter(p => p.slaveID == getSlave($AS).ID && p.workLeft <= 0).length>>

	/* since we already have count, skip slaves that don't need work */
	<<if _prostheticCount < 1>>
		<<continue>>
	<</if>>
	/*
	Ensures that a slave can never die during the execution of this passage.
	Calculation based on worst case, so when changing worst case change it here too.
	*/
	<<if getSlave($AS).health.health - (_prostheticCount * 20) < -75>>

		@@.red;Estimated health impact too great; <<= getSlave($AS).slaveName>> skipped.@@
		<<continue>>
	<</if>>

	<<for _k = 0; _k < $adjustProsthetics.length; _k++>>
		<<set _p = $adjustProsthetics[_k]>>
		<<if _p.slaveID == getSlave($AS).ID && _p.workLeft <= 0>>
			<<set $adjustProsthetics.splice(_k, 1), _k-->>
			<<set $adjustProstheticsCompleted-->>
			<<set addProsthetic(getSlave($AS), _p.id)>>

			<<switch _p.id>>
			<<case "ocular">>
				<<if getBestVision(getSlave($AS)) === 0>>
					<<run eyeSurgery(getSlave($AS), "both", "cybernetic"), cashX(forceNeg($surgeryCost), "slaveSurgery", getSlave($AS))>>
					<<set surgeryDamage(getSlave($AS), 20), $surgeryType = "ocular implant">>
					<<include "Surgery Degradation">>
				<<else>>
					//Since $he has working eyes the <<= setup.prosthetics.ocular.name>> will be put into storage.//
				<</if>>
			<<case "cochlear">>
				<<if getSlave($AS).hears != 0>>
					<<set getSlave($AS).earImplant = 1, cashX(forceNeg($surgeryCost), "slaveSurgery", getSlave($AS)), surgeryDamage(getSlave($AS), 20), $surgeryType = "cochlear implant">>
					<<include "Surgery Degradation">>
				<<else>>
					//Since $he has working ears the <<= setup.prosthetics.cochlear.name>> will be put into storage.//
				<</if>>
			<<case "electrolarynx">>
				<<if getSlave($AS).voice <= 0>>
					<<set getSlave($AS).electrolarynx = 1, getSlave($AS).voice = 2, cashX(forceNeg($surgeryCost), "slaveSurgery", getSlave($AS)), surgeryDamage(getSlave($AS), 20), $surgeryType = "electrolarynx">>
					<<include "Surgery Degradation">>
				<<else>>
					//Since $he has a voice the <<= setup.prosthetics.electrolarynx.name>> will be put into storage.//
				<</if>>
			<<case "interfaceP1">>
				<<if hasAnyNaturalLimbs(getSlave($AS))>>
					//Since $he has at least one healthy limb the <<= setup.prosthetics.interfaceP1.name>> will be put into storage.//
				<<elseif getSlave($AS).PLimb == 2>>
					//Since $he already has <<= addA(setup.prosthetics.interfaceP2.name)>> installed the <<= setup.prosthetics.interfaceP1.name>> will be put into storage.//
				<<else>>
					<<set getSlave($AS).PLimb = 1, cashX(forceNeg($surgeryCost), "slaveSurgery", getSlave($AS)), surgeryDamage(getSlave($AS), 20), $surgeryType = "PLimb interface1">>
					<<include "Surgery Degradation">>
				<</if>>
			<<case "interfaceP2">>
				<<if hasAllNaturalLimbs(getSlave($AS))>>
					//Since $he has no amputated limbs the <<= setup.prosthetics.interfaceP2.name>> will be put into storage.//
				<<elseif getSlave($AS).PLimb == 1>>
					<<set getSlave($AS).PLimb = 2, cashX(forceNeg($surgeryCost), "slaveSurgery", getSlave($AS)), surgeryDamage(getSlave($AS), 5), $surgeryType = "PLimb interface3">>
					<<include "Surgery Degradation">>
				<<else>>
					<<set getSlave($AS).PLimb = 2, cashX(forceNeg($surgeryCost), "slaveSurgery", getSlave($AS)), surgeryDamage(getSlave($AS), 20), $surgeryType = "PLimb interface2">>
					<<include "Surgery Degradation">>
				<</if>>
			<<case "basicL" "sexL" "beautyL" "combatL" "cyberneticL">>
				<<if getSlave($AS).fuckdoll != 0>>
					//Since a Fuckdoll can't use prosthetic limbs the <<= setup.prosthetics[_p.id].name>> will be put into storage.//
				<<elseif hasAllNaturalLimbs(getSlave($AS))>>
					//Since $he needs an amputated limb to attach prosthetics the <<= setup.prosthetics[_p.id].name>> will be put into storage.//
				<<elseif getSlave($AS).PLimb == 0>>
					//Since $he must have a prosthetic interface installed to attach prosthetic limbs the <<= setup.prosthetics[_p.id].name>> will be put into storage.//
				<<else>>
					<<if _p.id == "basicL">>
						<<set _state = App.Desc.limbChange().currentLimbs(getSlave($AS)), _change = upgradeLimbs(getSlave($AS), 2)>>
						<<if _change>>
							<<= App.Desc.limbChange().reaction(getSlave($AS), _state)>>
						<<else>>
							//Since $he already has more advanced prosthetic limbs attached the <<= setup.prosthetics.basicL.name>> will be put into storage.//
						<</if>>
					<<elseif _p.id == "sexL">>
						<<set _state = App.Desc.limbChange().currentLimbs(getSlave($AS)), _change = upgradeLimbs(getSlave($AS), 3)>>
						<<if _change>>
							<<= App.Desc.limbChange().reaction(getSlave($AS), _state)>>
						<<else>>
							//Since $he already has advanced prosthetic limbs attached the <<= setup.prosthetics.sexL.name>> will be put into storage.//
						<</if>>
					<<elseif _p.id == "beautyL">>
						<<set _state = App.Desc.limbChange().currentLimbs(getSlave($AS)), _change = upgradeLimbs(getSlave($AS), 4)>>
						<<if _change>>
							<<= App.Desc.limbChange().reaction(getSlave($AS), _state)>>
						<<else>>
							//Since $he already has advanced prosthetic limbs attached the <<= setup.prosthetics.beautyL.name>> will be put into storage.//
						<</if>>
					<<elseif _p.id == "combatL">>
						<<set _state = App.Desc.limbChange().currentLimbs(getSlave($AS)), _change = upgradeLimbs(getSlave($AS), 5)>>
						<<if _change>>
							<<= App.Desc.limbChange().reaction(getSlave($AS), _state)>>
						<<else>>
							//Since $he already has advanced prosthetic limbs attached the <<= setup.prosthetics.combatL.name>> will be put into storage.//
						<</if>>
					<<else>>
						<<if getSlave($AS).PLimb == 2>>
							<<set _state = App.Desc.limbChange().currentLimbs(getSlave($AS)), _change = upgradeLimbs(getSlave($AS), 2)>>
							<<if _change>>
								<<= App.Desc.limbChange().reaction(getSlave($AS), _state)>>
							<</if>>
						<<else>>
							//Since $he must have <<= addA(setup.prosthetics.interfaceP2.name)>> installed to attach cybernetic limbs the <<= setup.prosthetics.cyberneticL.name>> will be put into storage.//
						<</if>>
					<</if>>
				<</if>>
			<<case "interfaceTail">>
				<<set getSlave($AS).PTail = 1, getSlave($AS).tail = "none", getSlave($AS).tailColor = "none", cashX(forceNeg($surgeryCost), "slaveSurgery", getSlave($AS)), surgeryDamage(getSlave($AS), 10), $surgeryType = "tailInterface">>
				<<include "Surgery Degradation">>
			<<case "modT" "sexT" "combatT">>
				<<if getSlave($AS).PTail == 0>>
					//Since $he must have <<= addA(setup.prosthetics.interfaceTail.name)>> installed to attach tails the <<= setup.prosthetics[_p.id].name>> will be put into storage.//
				<<elseif getSlave($AS).tail != "none">>
					//Since $he currently has a tail attached the <<= setup.prosthetics[_p.id].name>> will be put into storage.//
				<<elseif _p.id == "modT">>
					//Since installing <<= addA(setup.prosthetics.modT.name)>> is complicated it can't be automated.//
					/*Reason: there are different designs player can choose from.*/
				<<elseif _p.id == "combatT">>
					<<set $prostheticsConfig = "attachTail", getSlave($AS).tail = "combat", getSlave($AS).tailColor = "jet black">>
					<<include "Prosthetics Configuration">>
				<<elseif _p.id == "sexT">>
					<<set $prostheticsConfig = "attachTail", getSlave($AS).tail = "sex", getSlave($AS).tailColor = "pink">>
					<<include "Prosthetics Configuration">>
				<</if>>
			<<default>>
				//Since there is no automated procedure to implant/attach <<= setup.prosthetics[_p.id].name>> it will be put into storage.//
			<</switch>>

		<</if>>
	<</for>>
<</for>> /* end of the $slaves loop */

/* This needs to be down here to over-ride any Surgery Degradation calls */
<<set $nextButton = "Continue">>
<<set $nextLink = "Main">>