code:
:: bodyswap widgets [nobr widget]

/* It's too fucking big jesus christ */

/* first arg is slave getting swapped, second is body she is being swapped to. The second body's physical traits overwrite the originals */
<<widget "BodySwap">>

<<set WombInit($args[1])>> /*Just to be sure.*/
<<set $args[0].genes = $args[1].genes>>
<<set $args[0].prestige = $args[1].prestige>>
<<set $args[0].pornFame = $args[1].pornFame>>
<<set $args[0].pornPrestige = $args[1].pornPrestige>>
<<set $args[0].pornPrestigeDesc = $args[1].pornPrestigeDesc>>
<<set $args[0].pornFameType = $args[1].pornFameType>>
<<set $args[0].pornFocus = "none">>
<<set $args[0].pornTypeGeneral = $args[1].pornTypeGeneral>>
<<set $args[0].pornTypeFuckdoll = $args[1].pornTypeFuckdoll>>
<<set $args[0].pornTypeRape = $args[1].pornTypeRape>>
<<set $args[0].pornTypePreggo = $args[1].pornTypePreggo>>
<<set $args[0].pornTypeBBW = $args[1].pornTypeBBW>>
<<set $args[0].pornTypeGainer = $args[1].pornTypeGainer>>
<<set $args[0].pornTypeStud = $args[1].pornTypeStud>>
<<set $args[0].pornTypeLoli = $args[1].pornTypeLoli>>
<<set $args[0].pornTypeDeepThroat = $args[1].pornTypeDeepThroat>>
<<set $args[0].pornTypeStruggleFuck = $args[1].pornTypeStruggleFuck>>
<<set $args[0].pornTypePainal = $args[1].pornTypePainal>>
<<set $args[0].pornTypeTease = $args[1].pornTypeTease>>
<<set $args[0].pornTypeRomantic = $args[1].pornTypeRomantic>>
<<set $args[0].pornTypePervert = $args[1].pornTypePervert>>
<<set $args[0].pornTypeCaring = $args[1].pornTypeCaring>>
<<set $args[0].pornTypeUnflinching = $args[1].pornTypeUnflinching>>
<<set $args[0].pornTypeSizeQueen = $args[1].pornTypeSizeQueen>>
<<set $args[0].pornTypeNeglectful = $args[1].pornTypeNeglectful>>
<<set $args[0].pornTypeCumAddict = $args[1].pornTypeCumAddict>>
<<set $args[0].pornTypeAnalAddict = $args[1].pornTypeAnalAddict>>
<<set $args[0].pornTypeAttentionWhore = $args[1].pornTypeAttentionWhore>>
<<set $args[0].pornTypeBreastGrowth = $args[1].pornTypeBreastGrowth>>
<<set $args[0].pornTypeAbusive = $args[1].pornTypeAbusive>>
<<set $args[0].pornTypeMalicious = $args[1].pornTypeMalicious>>
<<set $args[0].pornTypeSelfHating = $args[1].pornTypeSelfHating>>
<<set $args[0].pornTypeBreeder = $args[1].pornTypeBreeder>>
<<set $args[0].pornTypeSub = $args[1].pornTypeSub>>
<<set $args[0].pornTypeCumSlut = $args[1].pornTypeCumSlut>>
<<set $args[0].pornTypeAnal = $args[1].pornTypeAnal>>
<<set $args[0].pornTypeHumiliation = $args[1].pornTypeHumiliation>>
<<set $args[0].pornTypeBoobs = $args[1].pornTypeBoobs>>
<<set $args[0].pornTypeDom = $args[1].pornTypeDom>>
<<set $args[0].pornTypeSadist = $args[1].pornTypeSadist>>
<<set $args[0].pornTypeMasochist = $args[1].pornTypeMasochist>>
<<set $args[0].pornTypePregnancy = $args[1].pornTypePregnancy>>
<<set $args[0].prestigeDesc = $args[1].prestigeDesc>>
<<set $args[0].physicalAge = $args[1].physicalAge>>
<<set $args[0].visualAge = $args[1].visualAge>>
<<set $args[0].ageImplant = $args[1].ageImplant>>
<<set $args[0].health = $args[1].health>>
<<set $args[0].minorInjury = $args[1].minorInjury>>
<<set $args[0].weight = $args[1].weight>>
<<set $args[0].muscles = $args[1].muscles>>
<<set $args[0].height = $args[1].height>>
<<set $args[0].heightImplant = $args[1].heightImplant>>
<<set $args[0].race = $args[1].race>>
<<set $args[0].origRace = $args[1].origRace>>
<<set $args[0].pubicHColor = $args[1].pubicHColor>>
<<set $args[0].skin = $args[1].skin>>
<<set $args[0].origSkin = $args[1].origSkin>>
<<set $args[0].markings = $args[1].markings>>
<<set $args[0].eyes = $args[1].eyes>>
<<set $args[0].origEye = $args[1].origEye>>
<<set $args[0].eyeColor = $args[1].eyeColor>>
<<set $args[0].eyeWear = $args[1].eyeWear>>
<<set $args[0].hears = $args[1].hears>>
<<set $args[0].earwear = $args[1].earwear>>
<<set $args[0].earImplant = $args[1].earImplant>>
<<set $args[0].origHColor = $args[1].origHColor>>
<<set $args[0].hColor = $args[1].hColor>>
<<set $args[0].hLength = $args[1].hLength>>
<<set $args[0].hStyle = $args[1].hStyle>>
<<set $args[0].pubicHStyle = $args[1].pubicHStyle>>
<<set $args[0].waist = $args[1].waist>>
<<set $args[0].corsetPiercing = $args[1].corsetPiercing>>
<<set $args[0].amp = $args[1].amp>>
<<set $args[0].PLimb = $args[1].PLimb>>
<<set $args[0].heels = $args[1].heels>>
<<set $args[0].voice = $args[1].voice>>
<<set $args[0].voiceImplant = $args[1].voiceImplant>>
<<set $args[0].shoulders = $args[1].shoulders>>
<<set $args[0].shouldersImplant = $args[1].shouldersImplant>>
<<set $args[0].boobs = $args[1].boobs>>
<<set $args[0].boobsImplant = $args[1].boobsImplant>>
<<set $args[0].boobsImplantType = $args[1].boobsImplantType>>
<<set $args[0].boobShape = $args[1].boobShape>>
<<set $args[0].nipples = $args[1].nipples>>
<<set $args[0].nipplesPiercing = $args[1].nipplesPiercing>>
<<set $args[0].nipplesAccessory = $args[1].nipplesAccessory>>
<<set $args[0].areolae = $args[1].areolae>>
<<set $args[0].areolaePiercing = $args[1].areolaePiercing>>
<<set $args[0].areolaeShape = $args[1].areolaeShape>>
<<set $args[0].boobsTat = $args[1].boobsTat>>
<<set $args[0].lactation = $args[1].lactation>>
<<set $args[0].lactationDuration = $args[1].lactationDuration>>
<<set $args[0].induceLactation = $args[1].induceLactation>>
<<set $args[0].boobsMilk = $args[1].boobsMilk>>
<<set $args[0].lactationAdaptation = $args[1].lactationAdaptation>>
<<set $args[0].hips = $args[1].hips>>
<<set $args[0].hipsImplant = $args[1].hipsImplant>>
<<set $args[0].butt = $args[1].butt>>
<<set $args[0].buttImplant = $args[1].buttImplant>>
<<set $args[0].buttImplantType = $args[1].buttImplantType>>
<<set $args[0].buttTat = $args[1].buttTat>>
<<set $args[0].face = $args[1].face>>
<<set $args[0].faceImplant = $args[1].faceImplant>>
<<set $args[0].faceShape = $args[1].faceShape>>
<<set $args[0].lips = $args[1].lips>>
<<set $args[0].lipsImplant = $args[1].lipsImplant>>
<<set $args[0].lipsPiercing = $args[1].lipsPiercing>>
<<set $args[0].lipsTat = $args[1].lipsTat>>
<<set $args[0].teeth = $args[1].teeth>>
<<set $args[0].tonguePiercing = $args[1].tonguePiercing>>
<<set $args[0].vagina = $args[1].vagina>>
<<set $args[0].vaginaLube = $args[1].vaginaLube>>
<<set $args[0].vaginaPiercing = $args[1].vaginaPiercing>>
<<set $args[0].vaginaTat = $args[1].vaginaTat>>
<<set $args[0].preg = $args[1].preg>>
<<set $args[0].pregSource = $args[1].pregSource>>
<<set $args[0].pregType = $args[1].pregType>>
<<set $args[0].broodmother = $args[1].broodmother>>
<<set $args[0].broodmotherFetuses = $args[1].broodmotherFetuses>>
<<set $args[0].broodmotherOnHold = $args[1].broodmotherOnHold>>
<<set $args[0].broodmotherCountDown = $args[1].broodmotherCountDown>>
<<set $args[0].labor = $args[1].labor>>
<<set $args[0].csec = $args[1].csec>>
<<set $args[0].bellyAccessory = $args[1].bellyAccessory>>
<<set $args[0].labia = $args[1].labia>>
<<set $args[0].clit = $args[1].clit>>
<<set $args[0].clitPiercing = $args[1].clitPiercing>>
<<set $args[0].clitSetting = $args[1].clitSetting>>
<<set $args[0].dick = $args[1].dick>>
<<set $args[0].foreskin = $args[1].foreskin>>
<<set $args[0].anus = $args[1].anus>>
<<set $args[0].analArea = $args[1].analArea>>
<<set $args[0].dickPiercing = $args[1].dickPiercing>>
<<set $args[0].dickTat = $args[1].dickTat>>
<<set $args[0].prostate = $args[1].prostate>>
<<set $args[0].balls = $args[1].balls>>
<<set $args[0].scrotum = $args[1].scrotum>>
<<set $args[0].ovaries = $args[1].ovaries>>
<<set $args[0].anusPiercing = $args[1].anusPiercing>>
<<set $args[0].anusTat = $args[1].anusTat>>
<<set $args[0].makeup = $args[1].makeup>>
<<set $args[0].nails = $args[1].nails>>
<<set $args[0].brand = $args[1].brand>>
<<set $args[0].brandLocation = $args[1].brandLocation>>
<<set $args[0].earsPiercing = $args[1].earsPiercing>>
<<set $args[0].nosePiercing = $args[1].nosePiercing>>
<<set $args[0].eyebrowPiercing = $args[1].eyebrowPiercing>>
<<set $args[0].navelPiercing = $args[1].navelPiercing>>
<<set $args[0].shouldersTat = $args[1].shouldersTat>>
<<set $args[0].armsTat = $args[1].armsTat>>
<<set $args[0].legsTat = $args[1].legsTat>>
<<set $args[0].backTat = $args[1].backTat>>
<<set $args[0].stampTat = $args[1].stampTat>>
<<set $args[0].diet = $args[1].diet>>
<<set $args[0].dietCum = $args[1].dietCum>>
<<set $args[0].dietMilk = $args[1].dietMilk>>
<<set $args[0].hormones = $args[1].hormones>>
<<set $args[0].drugs = $args[1].drugs>>
<<set $args[0].aphrodisiacs = $args[1].aphrodisiacs>>
<<set $args[0].curatives = $args[1].curatives>>
<<set $args[0].chem = $args[1].chem>>
<<set $args[0].clothes = $args[1].clothes>>
<<set $args[0].collar = $args[1].collar>>
<<set $args[0].shoes = $args[1].shoes>>
<<set $args[0].vaginalChastity = $args[1].vaginalChastity>>
<<set $args[0].dickAccessory = $args[1].dickAccessory>>
<<set $args[0].buttplug = $args[1].buttplug>>
<<set $args[0].customTat = $args[1].customTat>>
<<set $args[0].bellyTat = $args[1].bellyTat>>
<<set $args[0].bellySag = $args[1].bellySag>>
<<set $args[0].bellySagPreg = $args[1].bellySagPreg>>
<<set $args[0].induce = $args[1].induce>>
<<set $args[0].mpreg = $args[1].mpreg>>
<<set $args[0].inflation = 0, $args[0].inflationType = "none", $args[0].inflationMethod = 0, $args[0].milkSource = 0, $args[0].cumSource = 0>>
<<set $args[0].bellyImplant = $args[1].bellyImplant>>
<<set $args[0].bellyPain = $args[1].bellyPain>>
<<set $args[0].cervixImplant = $args[1].cervixImplant>>
<<set $args[0].pubertyAgeXX = $args[1].pubertyAgeXX>>
<<set $args[0].pubertyXX = $args[1].pubertyXX>>
<<set $args[0].pubertyAgeXY = $args[1].pubertyAgeXY>>
<<set $args[0].pubertyXY = $args[1].pubertyXY>>
<<set $args[0].scars = $args[1].scars>>
<<set $args[0].breedingMark = $args[1].breedingMark>>
<<set $args[0].underArmHColor = $args[1].underArmHColor>>
<<set $args[0].underArmHStyle = $args[1].underArmHStyle>>
<<set $args[0].publicCount = $args[1].publicCount>>
<<set $args[0].ballType = $args[1].ballType>>
<<set $args[0].eggType = $args[1].eggType>>
<<set $args[0].pregControl = $args[1].pregControl>>
<<set $args[0].readyLimbs = $args[1].readyLimbs>>
<<set $args[0].bald = $args[1].bald>>
<<set $args[0].hormoneBalance = $args[1].hormoneBalance>>
<<set $args[0].breastMesh = $args[1].breastMesh>>
<<set $args[0].buttplugAttachment = $args[1].buttplugAttachment>>
<<set $args[0].vasectomy = $args[1].vasectomy>>
<<set $args[0].haircuts = $args[1].haircuts>>
<<set $args[0].pregKnown = $args[1].pregKnown>>
<<set $args[0].pregWeek = $args[1].pregWeek>>
<<set $args[0].ovaryAge = $args[1].ovaryAge>>
<<set $args[0].belly = $args[1].belly>>
<<set $args[0].bellyPreg = $args[1].bellyPreg>>
<<set $args[0].bellyFluid = $args[1].bellyFluid>>
<<set $args[0].readyOva = $args[1].readyOva>>
<<set $args[0].reservedChildren = $args[1].reservedChildren>>
<<set $args[0].reservedChildrenNursery = $args[1].reservedChildrenNursery>>
<<set $args[0].womb = $args[1].womb>> /* this is array assigned by reference, if slave body that is $args[1] will be stil used anywhere in code (not discarded) - it's WRONG (they now technically share one womb object). Please tell me about it then. But if old body $args[1] just discarded - it's no problem then.*/
<<set $args[0].laborCount = $args[1].laborCount>>
<<set $args[0].geneMods = $args[1].geneMods>>
<<set $args[0].NCSyouthening = $args[1].NCSyouthening>>
<<set $args[0].eyebrowHColor = $args[1].eyebrowHColor>>
<<set $args[0].eyebrowHStyle = $args[1].eyebrowHStyle>>
<<set $args[0].eyebrowFullness = $args[1].eyebrowFullness>>
<<set $args[0].wombImplant = $args[1].wombImplant>>
<<set $args[0].ovaImplant = $args[1].ovaImplant>>

<<set $args[0].canRecruit = 0>>
/*
<<if def $args[2]>>
	<<= assignJob($args[0], "rest")>>
<</if>>
*/

<</widget>>

<<widget "BodySwapName">>

<<if $args[1].bodySwap == 0>>
	<<if $args[1].birthSurname>>
		<<if $surnameOrder != 1>>
			<<switch $args[1].nationality>>
				<<case "Cambodian" "Chinese" "Hungarian" "Japanese" "Korean" "Mongolian" "Taiwanese" "Vietnamese">>
					<<if $args[1].birthName !== "">>
						<<set $args[0].origBodyOwner = $args[1].birthSurname + " " + $args[1].birthName>>
					<<else>>
						<<set $args[0].origBodyOwner = $args[1].birthSurname + " " + $args[1].slaveName>>
					<</if>>
				<<default>>
					<<if $args[1].birthName !== "">>
						<<set $args[0].origBodyOwner = $args[1].birthName + " " + $args[1].birthSurname>>
					<<else>>
						<<set $args[0].origBodyOwner = $args[1].slaveName + " " + $args[1].birthSurname>>
					<</if>>
			<</switch>>
		<<else>>
			<<if $args[1].birthName !== "">>
				<<set $args[0].origBodyOwner = $args[1].birthName + " " + $args[1].birthSurname>>
			<<else>>
				<<set $args[0].origBodyOwner = $args[1].slaveName + " " + $args[1].birthSurname>>
			<</if>>
		<</if>>
	<<elseif $args[1].birthName>>
		<<if $args[1].slaveSurname>>
			<<if $surnameOrder != 1>>
				<<switch $args[1].nationality>>
					<<case "Cambodian" "Chinese" "Hungarian" "Japanese" "Korean" "Mongolian" "Taiwanese" "Vietnamese">>
						<<set $args[0].origBodyOwner = $args[1].slaveSurname + " " + $args[1].birthName>>
					<<default>>
						<<set $args[0].origBodyOwner = $args[1].birthName + " " + $args[1].slaveSurname>>
				<</switch>>
			<<else>>
				<<set $args[0].origBodyOwner = $args[1].birthName + " " + $args[1].slaveSurname>>
			<</if>>
		<<else>>
			<<set $args[0].origBodyOwner = $args[1].birthName>>
		<</if>>
	<<elseif $args[1].slaveSurname>>
		<<if $surnameOrder != 1>>
			<<switch $args[1].nationality>>
				<<case "Cambodian" "Chinese" "Hungarian" "Japanese" "Korean" "Mongolian" "Taiwanese" "Vietnamese">>
					<<set $args[0].origBodyOwner = $args[1].slaveSurname + " " + $args[1].slaveName>>
				<<default>>
					<<set $args[0].origBodyOwner = $args[1].slaveName + " " + $args[1].slaveSurname>>
			<</switch>>
		<<else>>
			<<set $args[0].origBodyOwner = $args[1].slaveName + " " + $args[1].slaveSurname>>
		<</if>>
	<<else>>
		<<set $args[0].origBodyOwner = $args[1].slaveName>>
	<</if>>
<<else>>
	<<set $args[0].origBodyOwner = $args[1].origBodyOwner>>
<</if>>

<</widget>>


code:
:: death widgets [widget nobr]

<<widget "pop">>

<br>

As $args[0].slaveName is going about $his business with $his overfilled $args[0].inflationType belly, $he can't help but feel exhausted. $His health has been poor lately and the pressure in $his organs is not helping things. $He immediately knows something is wrong when an intense pain runs through $his middle and $his load shifts threateningly. Blood and $args[0].inflationType leak from $his rear as $his body cavity fills with the contents of $his digestive tract. The skin of $his taut belly reddens as the pressure against it builds. As $he takes $his last breath, $he falls forward, $his weight landing upon $his straining stomach. With a gush, $he ruptures, flooding the area around $his with blood, guts and $args[0].inflationType. Word of the late slave and $his gruesome fate spread fast, @@.gold;terrifying@@ your untrusting slaves.
<<for _dw = 0; _dw < $slaves.length; _dw++>>
	<<if ($slaves[_dw].trust <= 50)>>
	<<if ($slaves[_dw].inflation >= 0)>>
		<<set $slaves[_dw].trust -= ((($slaves[_dw].inflation)^3)*5)>>
	<<else>>
		<<set $slaves[_dw].trust -= 10>>
	<</if>>
	<</if>>
<</for>>

<</widget>>

<<widget "DeathOldAge">>
	<<run SlavePronouns($args[0])>>
	<<if $args[0].assignment == "be confined in the arcade">>
		You are notified by $arcadeName staff that one of the cabinets has broken down and will need to be replaced. It would seem ''@@.pink;$args[0].slaveName@@'', the fucktoy encased in it, died <<if $args[0].physicalAge >= 70>>naturally of old age despite<<else>>suddenly, unrelated to<</if>> $his living conditions. $He was a good unit; logs show $he was taking dick up until the very end.
	<<elseif $args[0].assignment == "work in the dairy" && $dairyRestraintsSetting > 1>>
		You are notified by $dairyName staff that one of the occupied milkers has ceased producing. Upon inspection, it would seem ''@@.pink;$args[0].slaveName@@'', the cow restrained in it, died <<if $args[0].physicalAge >= 70>>naturally of old age despite<<else>>suddenly, unrelated to<</if>> $his living conditions. $He was a good cow; $he gave milk up until $his death.
	<<elseif $args[0].fuckdoll > 0>>
		One of your fuckdoll's monitoring systems alerts you that the slave contained within has died. It would seem ''@@.pink;$args[0].slaveName@@'' has died <<if $args[0].physicalAge >= 70>>naturally of old age despite<<else>>suddenly, unrelated to<</if>> $his living conditions. Thankfully the suit notifies its owner of such things; especially with the rumors of earlier models and necrophilia you hear occasionally.
	<<else>>
		''@@.pink;$args[0].slaveName@@'' failed to report in for a routine inspection, something that rarely occurs under your watch. It doesn't take long to track down the wayward slave.
		<<set _deathSeed = random(1,100)>>
		<<if _deathSeed > 75>>
			$He is found dead in $his bed, having died sometime during the previous night.
		<<elseif _deathSeed > 50>>
			$He is found dead in a stairwell, having had a heart attack while trying to climb it.
		<<elseif _deathSeed > 25>>
			$He is found dead in the showers, having slipped sometime earlier.
		<<else>>
			<<if $args[0].releaseRules != "restrictive" && $args[0].releaseRules != "masturbation" && $args[0].releaseRules != "chastity">>
				$He is found dead in the bed of another slave, having died during intercourse. $His lover is not taking it well.
			<<else>>
				$He is found dead in the cafeteria, having died during breakfast; $he ruined the day for a number of your slaves.
			<</if>>
		<</if>>
		$args[0].slaveName died at the age of $args[0].actualAge;
		<<if $args[0].actualAge < 10>>
			$he lived a tragically short life.
		<<elseif $args[0].actualAge < 20>>
			$he died far too young.
		<<elseif $args[0].actualAge < 30>>
			$he died in what would be a sex slave's prime.
		<<elseif $args[0].actualAge < 50>>
			$he died in $his prime.
		<<elseif $args[0].actualAge < 65>>
			$he lived a fair life, good or bad.
		<<elseif $args[0].actualAge < 90>>
			$he lived a long life and experienced much during it.
		<<else>>
			$he lived a very long life that few get to see.
		<</if>>
	<</if>>
<</widget>>

<<widget "DeathOverdose">>
	<<run SlavePronouns($args[0])>>
	<<if $args[0].assignment == "be confined in the arcade">>
		You are notified by $arcadeName staff that one of the cabinets has broken down and will need to be replaced. It would seem ''@@.pink;$args[0].slaveName@@'', the fucktoy encased in it, died of an aphrodisiac overdose from the constant aphrodisiac injections. $He was a good unit; logs show $he was taking dick up until the very end.
	<<elseif $args[0].assignment == "work in the dairy" && $dairyRestraintsSetting > 1>>
		You are notified by $dairyName staff that one of the occupied milkers has ceased producing. Upon inspection, it would seem ''@@.pink;$args[0].slaveName@@'', the cow restrained in it, died of an aphrodisiac overdose. How $he managed to get them is unknown, but $he was a good cow; $he gave milk up until $his death.
	<<elseif $args[0].fuckdoll > 0>>
		One of your fuckdoll's monitoring systems alerts you that the slave contained within has died. It would seem ''@@.pink;$args[0].slaveName@@'' has died of an aphrodisiac overdose. Thankfully the suit notifies its owner of such things; especially with the rumors of earlier models and necrophilia you hear occasionally. It does little to deal with the resulting mess of the orgasm $he died during, however.
	<<else>>
		''@@.pink;$args[0].slaveName@@'' failed to report in for a routine inspection, something that rarely occurs under your watch. It doesn't take long to track down the wayward slave.
		$He is found dead in $his bed, having died sometime earlier. Judging by the mess and the expression on $his face, $he died of a heart attack during a particularly intense orgasm bought about by the massive amount of aphrodisiacs in $his system. $args[0].slaveName died at the age of $args[0].actualAge;
		<<if $args[0].actualAge < 10>>
			$he lived a tragically short life.
		<<elseif $args[0].actualAge < 20>>
			$he died far too young.
		<<elseif $args[0].actualAge < 30>>
			$he died in what would be a sex slave's prime.
		<<elseif $args[0].actualAge < 50>>
			$he died in $his prime.
		<<elseif $args[0].actualAge < 65>>
			$he lived a fair life, good or bad.
		<<elseif $args[0].actualAge < 90>>
			$he lived a long life and experienced much during it.
		<<else>>
			$he lived a very long life that few get to see.
		<</if>>
	<</if>>
<</widget>>

<<widget "DeathUnhealthy">>
	<<run SlavePronouns($args[0])>>
	<<if $args[0].assignment == "be confined in the arcade">>
		You are notified by $arcadeName staff that one of the cabinets has broken down and will need to be replaced. It would seem ''@@.pink;$args[0].slaveName@@'', the fucktoy encased in it, died to poor health caused by $his living conditions. $He was a good unit; logs show $he was taking dick up until the very end.
	<<elseif $args[0].assignment == "work in the dairy" && $dairyRestraintsSetting > 1>>
		You are notified by $dairyName staff that one of the occupied milkers has ceased producing. Upon inspection, it would seem ''@@.pink;$args[0].slaveName@@'', the cow restrained in it, died to poor health caused by $his living conditions. $He was a good cow; $he gave milk up until $his death.
	<<elseif $args[0].fuckdoll > 0>>
		One of your fuckdoll's monitoring systems alerts you that the slave contained within has died. It would seem ''@@.pink;$args[0].slaveName@@'' has died of general poor health. Thankfully the suit notifies its owner of such things; especially with the rumors of earlier models and necrophilia you hear occasionally. Clean up is easy enough, however.
	<<else>>
		''@@.pink;$args[0].slaveName@@'' failed to report in for a routine inspection, something that rarely occurs under your watch. It doesn't take long to track down the wayward slave.
		$He is found dead in $his bed, having died sometime during the night. $He has been in very poor health lately, so you knew this was a possibility. $args[0].slaveName died at the age of $args[0].actualAge;
		<<if $args[0].actualAge < 10>>
			$he lived a tragically short life.
		<<elseif $args[0].actualAge < 20>>
			$he died far too young.
		<<elseif $args[0].actualAge < 30>>
			$he died in what would be a sex slave's prime.
		<<elseif $args[0].actualAge < 50>>
			$he died in $his prime.
		<<elseif $args[0].actualAge < 65>>
			$he lived a fair life, good or bad.
		<<elseif $args[0].actualAge < 90>>
			$he lived a long life and experienced much during it.
		<<else>>
			$he lived a very long life that few get to see.
		<</if>>
		<<if ($arcologies[0].FSPaternalist != "unset") && ($args[0].actualAge < 75)>>
			Allowing a slave to die under your care @@.red;severely damages@@ your image as a caring slaveowner and @@.red;calls into question@@ your paternalistic resolve.
			<<= FSChange("Paternalist", -10)>>
		<</if>>
	<</if>>
<</widget>>

<<widget "DeathPregnancyBurst">>
<</widget>>


code:
:: economy widgets [widget nobr]

<<widget "BrothelStatistics">>
	<<if ($showEconomicDetails)>>
	<<if (!$facility || !$facility.brothel)>>

- No statistics for $brothelName gathered this week -

	<<else>>
		<<set _details = $args[0]>>
		<<set _b = $facility.brothel>>
		<<set _b.whoreIncome = (def _b.whoreIncome) ? _b.whoreIncome : 0>>
		<<set _b.rep = (def _b.rep) ? _b.rep : 0>>
		<<set _b.whoreCosts = (def _b.whoreCosts) ? _b.whoreCosts : 0>>
		<<set _b.adsIncome = (def _b.adsIncome) ? _b.adsIncome : 0>>
		<<set _b.maintenance = (def _b.maintenance) ? _b.maintenance : 0>>
		<<set _b.adsCosts = (def _b.adsCosts) ? _b.adsCosts : 0>>
		<<set _b.totalIncome = (def _b.totalIncome) ? _b.totalIncome : 0>>
		<<set _b.totalExpenses = (def _b.totalExpenses) ? _b.totalExpenses : 0>>
		<<set _b.profit = (def _b.profit) ? _b.profit : 0>>

			Items
			Revenue
			Expenses
			Net Income
			Rep. Change

			Total whoring income
			@@.yellowgreen;¤<<= _b.whoreIncome.toFixedHTML(2)>>@@

			@@.yellowgreen;¤<<= _b.whoreIncome.toFixedHTML(2)>>@@
			<<if (_b.rep > 0)>>@@.green;+<<= _b.rep.toFixedHTML(1)>>@@<<elseif (_b.rep < 0)>>@@.red;<<= _b.rep.toFixedHTML(1)>>@@<<else>>±<<= Number(0).toFixedHTML(1)>><</if>>

			Total whore living costs

			@@.red;¤<<= _b.whoreCosts.toFixedHTML(2)>>@@
			@@.red;¤-<<= _b.whoreCosts.toFixedHTML(2)>>@@

		<<if _details>>

			Whore details

				Whore
				Customers
				Revenue
				Expenses
				Net Income
				Rep. Change

			<<set _slaveDetails = _b.income.values()>>
			<<set _slaveInfo = _slaveDetails.next()>>
			<<for !_slaveInfo.done>>
				<<set _revenue = _slaveInfo.value.income + _slaveInfo.value.adsIncome, _netIncome = _revenue - _slaveInfo.value.cost>>

					<<if (_slaveInfo.value.customLabel)>>(@@.yellow;''_slaveInfo.value.customLabel''@@) <</if>>_slaveInfo.value.slaveName
					<<if (_slaveInfo.value.customers <= 0)>>@@.red;none@@<<else>>_slaveInfo.value.customers<</if>>
					@@.yellowgreen;¤<<= _revenue.toFixedHTML(2)>>@@<<if (_slaveInfo.value.adsIncome > 0)>> (@@.yellowgreen;¤<<= _slaveInfo.value.adsIncome.toFixedHTML(2)>>@@ due to advertising)<</if>>
					@@.red;¤<<= _slaveInfo.value.cost.toFixedHTML(2)>>@@
					<<if (_netIncome > 0)>>@@.yellowgreen;¤<<= _netIncome.toFixedHTML(2)>>@@<<elseif (_netIncome < 0)>>@@.red;¤<<= _netIncome.toFixedHTML(2)>>@@<<else>>¤<<= _netIncome.toFixedHTML(2)>><</if>>
					<<if (_slaveInfo.value.rep > 0)>>@@.green;+<<= _slaveInfo.value.rep.toFixedHTML(1)>>@@<<elseif (_slaveInfo.value.rep < 0)>>@@.red;<<= _slaveInfo.value.rep.toFixedHTML(1)>>@@<<else>><</if>>

				<<set _slaveInfo = _slaveDetails.next()>>
			<</for>>

		<</if>>
		<<if (_b.adsIncome > 0)>>

			Additional income
			@@.yellowgreen;¤<<= _b.adsIncome.toFixedHTML(2)>>@@

			@@.yellowgreen;¤<<= _b.adsIncome.toFixedHTML(2)>>@@

		<</if>>

			Brothel maintenance

			@@.red;¤<<= _b.maintenance.toFixedHTML(2)>>@@
			@@.red;¤-<<= _b.maintenance.toFixedHTML(2)>>@@

		<<if (_b.adsCosts > 0)>>

			Advertising program

			@@.red;¤<<= _b.adsCosts.toFixedHTML(2)>>@@
			@@.red;¤-<<= _b.adsCosts.toFixedHTML(2)>>@@

		<</if>>

			Total
			@@.yellowgreen;¤<<= _b.totalIncome.toFixedHTML(2)>>@@
			@@.red;¤<<= _b.totalExpenses.toFixedHTML(2)>>@@
			<<if (_b.profit > 0)>>@@.yellowgreen;¤<<= _b.profit.toFixedHTML(2)>>@@<<elseif (_b.profit < 0)>>@@.red;¤<<= _b.profit.toFixedHTML(2)>>@@<<else>>¤<<= _b.profit.toFixedHTML(2)>><</if>>
			<<if (_b.rep > 0)>>@@.green;+<<= _b.rep.toFixedHTML(1)>>@@<<elseif (_b.rep < 0)>>@@.red;<<= _b.rep.toFixedHTML(1)>>@@<<else>>±<<= Number(0).toFixedHTML(1)>><</if>>

	<</if>>
	<</if>>
<</widget>>

<<widget "ClubStatistics">>
	<<if ($showEconomicDetails)>>
	<<if (!$facility || !$facility.club)>>

- No statistics for $clubName gathered this week -

	<<else>>
		<<set _details = $args[0]>>
		<<set _b = $facility.club>>
		<<set _b.whoreIncome = (def _b.whoreIncome) ? _b.whoreIncome : 0>>
		<<set _b.rep = (def _b.rep) ? _b.rep : 0>>
		<<set _b.whoreCosts = (def _b.whoreCosts) ? _b.whoreCosts : 0>>
		<<set _b.adsIncome = (def _b.adsIncome) ? _b.adsIncome : 0>>
		<<set _b.maintenance = (def _b.maintenance) ? _b.maintenance : 0>>
		<<set _b.adsCosts = (def _b.adsCosts) ? _b.adsCosts : 0>>
		<<set _b.totalIncome = (def _b.totalIncome) ? _b.totalIncome : 0>>
		<<set _b.totalExpenses = (def _b.totalExpenses) ? _b.totalExpenses : 0>>
		<<set _b.profit = (def _b.profit) ? _b.profit : 0>>

			Items
			Rep. Gain
			Expenses
			Rep/Expenses
			Extra Income

			Total whoring income
			@@.green;+<<= _b.whoreIncome.toFixedHTML(2)>>@@

			<<if (_b.rep > 0)>>@@.green;¤<<= _b.rep.toFixedHTML(1)>>@@<<elseif (_b.rep < 0)>>@@.red;¤<<= -_b.rep.toFixedHTML(1)>>@@<</if>>

			Total whore living costs

			@@.red;¤<<= _b.whoreCosts.toFixedHTML(2)>>@@

		<<if _details>>

			Whore details

				Whore
				Customers
				Rep. Gain
				Expenses
				Rep/Expenses
				Extra Income

			<<set _slaveDetails = _b.income.values()>>
			<<set _slaveInfo = _slaveDetails.next()>>
			<<for !_slaveInfo.done>>
				<<set _netIncome = _slaveInfo.value.income / _slaveInfo.value.cost>>

					<<if (_slaveInfo.value.customLabel)>>(@@.yellow;''_slaveInfo.value.customLabel''@@) <</if>>_slaveInfo.value.slaveName
					<<if (_slaveInfo.value.customers <= 0)>>@@.red;none@@<<else>>_slaveInfo.value.customers<</if>>
					@@.green;+<<= _slaveInfo.value.income.toFixedHTML(2)>>@@
					@@.red;¤<<= _slaveInfo.value.cost.toFixedHTML(2)>>@@
					@@.green;<<= _netIncome.toFixedHTML(2)>>@@ rep/¤
					@@.yellowgreen;¤<<= _slaveInfo.value.rep.toFixedHTML(1)>>@@

				<<set _slaveInfo = _slaveDetails.next()>>
			<</for>>

		<</if>>
		<<if (_b.adsIncome > 0)>>

			Additional rep gain
			@@.yellowgreen;¤<<= _b.adsIncome.toFixedHTML(2)>>@@

		<</if>>

			Club maintenance

			@@.red;¤<<= _b.maintenance.toFixedHTML(2)>>@@

		<<if (_b.adsCosts > 0)>>

			Advertising program

			@@.red;¤<<= _b.adsCosts.toFixedHTML(2)>>@@

		<</if>>

			Total
			@@.green;+<<= _b.totalIncome.toFixedHTML(2)>>@@
			@@.red;¤<<= _b.totalExpenses.toFixedHTML(2)>>@@
			@@.green;<<= _b.profit.toFixedHTML(2)>>@@ rep/¤
			@@.green;+<<= _b.rep.toFixedHTML(1)>>@@

	<</if>>
	<</if>>
<</widget>>

<<widget "DairyStatistics">>
	<<if ($showEconomicDetails)>>
	<<if (!$facility || !$facility.dairy)>>

- No statistics for $dairyName gathered this week -

	<<else>>
		<<set _details = $args[0]>>
		<<set _b = $facility.dairy>>
		<<set _b.whoreIncome = (def _b.whoreIncome) ? _b.whoreIncome : 0>>
		<<set _b.whoreCosts = (def _b.whoreCosts) ? _b.whoreCosts : 0>>
		<<set _b.maintenance = (def _b.maintenance) ? _b.maintenance : 0>>
		<<set _b.totalIncome = (def _b.totalIncome) ? _b.totalIncome : 0>>
		<<set _b.totalExpenses = (def _b.totalExpenses) ? _b.totalExpenses : 0>>
		<<set _b.profit = (def _b.profit) ? _b.profit : 0>>

			Items
			Revenue
			Expenses
			Net Income
			Rep. Change

			Total cow income
			@@.yellowgreen;¤<<= _b.whoreIncome.toFixedHTML(2)>>@@

			@@.yellowgreen;¤<<= _b.whoreIncome.toFixedHTML(2)>>@@

			Total cow living costs

			@@.red;¤<<= _b.whoreCosts.toFixedHTML(2)>>@@
			@@.red;¤-<<= _b.whoreCosts.toFixedHTML(2)>>@@

		<<if _details>>

			Cow details

				Cow
				Milk/Cum/Fluids
				Revenue
				Expenses
				Net Income
				Rep. Change

			<<set _slaveDetails = _b.income.values()>>
			<<set _slaveInfo = _slaveDetails.next()>>
			<<for !_slaveInfo.done>>
				<<set _revenue = _slaveInfo.value.income, _netIncome = _revenue - _slaveInfo.value.cost>>

					<<if (_slaveInfo.value.customLabel)>>(@@.yellow;''_slaveInfo.value.customLabel''@@) <</if>>_slaveInfo.value.slaveName
					<<= _slaveInfo.value.milk>>/<<= _slaveInfo.value.cum>>/<<= _slaveInfo.value.fluid>>
					@@.yellowgreen;¤<<= _revenue.toFixedHTML(2)>>@@<<if (_slaveInfo.value.adsIncome > 0)>> (@@.yellowgreen;¤<<= _slaveInfo.value.adsIncome.toFixedHTML(2)>>@@ due to advertising)<</if>>
					@@.red;¤<<= _slaveInfo.value.cost.toFixedHTML(2)>>@@
					<<if (_netIncome > 0)>>@@.yellowgreen;¤<<= _netIncome.toFixedHTML(2)>>@@<<elseif (_netIncome < 0)>>@@.red;¤<<= _netIncome.toFixedHTML(2)>>@@<<else>>¤<<= _netIncome.toFixedHTML(2)>><</if>>

				<<set _slaveInfo = _slaveDetails.next()>>
			<</for>>

		<</if>>

			Dairy maintenance

			@@.red;¤<<= _b.maintenance.toFixedHTML(2)>>@@
			@@.red;¤-<<= _b.maintenance.toFixedHTML(2)>>@@

			Total
			@@.yellowgreen;¤<<= _b.totalIncome.toFixedHTML(2)>>@@
			@@.red;¤<<= _b.totalExpenses.toFixedHTML(2)>>@@
			<<if (_b.profit > 0)>>@@.yellowgreen;¤<<= _b.profit.toFixedHTML(2)>>@@<<elseif (_b.profit < 0)>>@@.red;¤<<= _b.profit.toFixedHTML(2)>>@@<<else>>¤<<= _b.profit.toFixedHTML(2)>><</if>>

	<</if>>
	<</if>>
<</widget>>

<<widget "FarmyardStatistics">>
	<<if ($showEconomicDetails)>>
	<<if (!$facility || !$facility.farmyard)>>

- No statistics for $farmyardName gathered this week -

	<<else>>
		<<set _details = $args[0]>>
		<<set _b = $facility.farmyard>>
		<<set _b.whoreIncome = (def _b.whoreIncome) ? _b.whoreIncome : 0>>
		<<set _b.whoreCosts = (def _b.whoreCosts) ? _b.whoreCosts : 0>>
		<<set _b.maintenance = (def _b.maintenance) ? _b.maintenance : 0>>
		<<set _b.totalIncome = (def _b.totalIncome) ? _b.totalIncome : 0>>
		<<set _b.totalExpenses = (def _b.totalExpenses) ? _b.totalExpenses : 0>>
		<<set _b.profit = (def _b.profit) ? _b.profit : 0>>

			Items
			Revenue
			Expenses
			Net Income
			Rep. Change

			Total farmhand income
			@@.yellowgreen;¤<<= _b.whoreIncome.toFixedHTML(2)>>@@

			@@.yellowgreen;¤<<= _b.whoreIncome.toFixedHTML(2)>>@@

			Total farmhand living costs

			@@.red;¤<<= _b.whoreCosts.toFixedHTML(2)>>@@
			@@.red;¤-<<= _b.whoreCosts.toFixedHTML(2)>>@@

		<<if _details>>

			Farmhand details

				Farmhand
				Milk/Cum/Fluids
				Revenue
				Expenses
				Net Income
				Rep. Change

			<<set _slaveDetails = _b.income.values()>>
			<<set _slaveInfo = _slaveDetails.next()>>
			<<for !_slaveInfo.done>>
				<<set _revenue = _slaveInfo.value.income, _netIncome = _revenue - _slaveInfo.value.cost>>

					<<if (_slaveInfo.value.customLabel)>>(@@.yellow;''_slaveInfo.value.customLabel''@@) <</if>>_slaveInfo.value.slaveName
					<<= _slaveInfo.value.milk>>/<<= _slaveInfo.value.cum>>/<<= _slaveInfo.value.fluid>>
					@@.yellowgreen;¤<<= _revenue.toFixedHTML(2)>>@@<<if (_slaveInfo.value.adsIncome > 0)>> (@@.yellowgreen;¤<<= _slaveInfo.value.adsIncome.toFixedHTML(2)>>@@ due to advertising)<</if>>
					@@.red;¤<<= _slaveInfo.value.cost.toFixedHTML(2)>>@@
					<<if (_netIncome > 0)>>@@.yellowgreen;¤<<= _netIncome.toFixedHTML(2)>>@@<<elseif (_netIncome < 0)>>@@.red;¤<<= _netIncome.toFixedHTML(2)>>@@<<else>>¤<<= _netIncome.toFixedHTML(2)>><</if>>

				<<set _slaveInfo = _slaveDetails.next()>>
			<</for>>

		<</if>>

			Farmyard maintenance

			@@.red;¤<<= _b.maintenance.toFixedHTML(2)>>@@
			@@.red;¤-<<= _b.maintenance.toFixedHTML(2)>>@@

			Total
			@@.yellowgreen;¤<<= _b.totalIncome.toFixedHTML(2)>>@@
			@@.red;¤<<= _b.totalExpenses.toFixedHTML(2)>>@@
			<<if (_b.profit > 0)>>@@.yellowgreen;¤<<= _b.profit.toFixedHTML(2)>>@@<<elseif (_b.profit < 0)>>@@.red;¤<<= _b.profit.toFixedHTML(2)>>@@<<else>>¤<<= _b.profit.toFixedHTML(2)>><</if>>

	<</if>>
	<</if>>
<</widget>>

<<widget "ArcadeStatistics">>
	<<if ($showEconomicDetails)>>
	<<if (!$facility || !$facility.arcade)>>

- No statistics for $arcadeName gathered this week -

	<<else>>
		<<set _details = $args[0]>>
		<<set _b = $facility.arcade>>
		<<set _b.whoreIncome = (def _b.whoreIncome) ? _b.whoreIncome : 0>>
		<<set _b.rep = (def _b.rep) ? _b.rep : 0>>
		<<set _b.whoreCosts = (def _b.whoreCosts) ? _b.whoreCosts : 0>>
		<<set _b.maintenance = (def _b.maintenance) ? _b.maintenance : 0>>
		<<set _b.totalIncome = (def _b.totalIncome) ? _b.totalIncome : 0>>
		<<set _b.totalExpenses = (def _b.totalExpenses) ? _b.totalExpenses : 0>>
		<<set _b.profit = (def _b.profit) ? _b.profit : 0>>

			Items
			Revenue
			Expenses
			Net Income
			Rep. Change

			Total income
			@@.yellowgreen;¤<<= _b.whoreIncome.toFixedHTML(2)>>@@

			@@.yellowgreen;¤<<= _b.whoreIncome.toFixedHTML(2)>>@@
			<<if (_b.rep > 0)>>@@.green;+<<= _b.rep.toFixedHTML(1)>>@@<<elseif (_b.rep < 0)>>@@.red;<<= _b.rep.toFixedHTML(1)>>@@<<else>>±<<= Number(0).toFixedHTML(1)>><</if>>

			Total fuckmeat living costs

			@@.red;¤<<= _b.whoreCosts.toFixedHTML(2)>>@@
			@@.red;¤-<<= _b.whoreCosts.toFixedHTML(2)>>@@

		<<if _details>>

			Fuckmeat details

				Fuckmeat
				Customers
				Revenue
				Expenses
				Net Income
				Rep. Change

			<<set _slaveDetails = _b.income.values()>>
			<<set _slaveInfo = _slaveDetails.next()>>
			<<for !_slaveInfo.done>>
				<<set _revenue = _slaveInfo.value.income + _slaveInfo.value.adsIncome, _netIncome = _revenue - _slaveInfo.value.cost>>

					<<if (_slaveInfo.value.customLabel)>>(@@.yellow;''_slaveInfo.value.customLabel''@@) <</if>>_slaveInfo.value.slaveName
					<<if (_slaveInfo.value.customers <= 0)>>@@.red;none@@<<else>>_slaveInfo.value.customers<</if>>
					@@.yellowgreen;¤<<= _revenue.toFixedHTML(2)>>@@<<if (_slaveInfo.value.adsIncome > 0)>> (@@.yellowgreen;¤<<= _slaveInfo.value.adsIncome.toFixedHTML(2)>>@@ due to advertising)<</if>>
					@@.red;¤<<= _slaveInfo.value.cost.toFixedHTML(2)>>@@
					<<if (_netIncome > 0)>>@@.yellowgreen;¤<<= _netIncome.toFixedHTML(2)>>@@<<elseif (_netIncome < 0)>>@@.red;¤<<= _netIncome.toFixedHTML(2)>>@@<<else>>¤<<= _netIncome.toFixedHTML(2)>><</if>>
					<<if (_slaveInfo.value.rep > 0)>>@@.green;+<<= _slaveInfo.value.rep.toFixedHTML(1)>>@@<<elseif (_slaveInfo.value.rep < 0)>>@@.red;<<= _slaveInfo.value.rep.toFixedHTML(1)>>@@<<else>><</if>>

				<<set _slaveInfo = _slaveDetails.next()>>
			<</for>>

		<</if>>

			Arcade maintenance

			@@.red;¤<<= _b.maintenance.toFixedHTML(2)>>@@
			@@.red;¤-<<= _b.maintenance.toFixedHTML(2)>>@@

			Total
			@@.yellowgreen;¤<<= _b.totalIncome.toFixedHTML(2)>>@@
			@@.red;¤<<= _b.totalExpenses.toFixedHTML(2)>>@@
			<<if (_b.profit > 0)>>@@.yellowgreen;¤<<= _b.profit.toFixedHTML(2)>>@@<<elseif (_b.profit < 0)>>@@.red;¤<<= _b.profit.toFixedHTML(2)>>@@<<else>>¤<<= _b.profit.toFixedHTML(2)>><</if>>
			<<if (_b.rep > 0)>>@@.green;+<<= _b.rep.toFixedHTML(1)>>@@<<elseif (_b.rep < 0)>>@@.red;<<= _b.rep.toFixedHTML(1)>>@@<<else>>±<<= Number(0).toFixedHTML(1)>><</if>>

	<</if>>
	<</if>>
<</widget>>

/* Call with <<CorpDevBuySell "asset" "Numasset">>  TODO: replace eval parse with appropriate functions */
<<widget "CorpDevBuySell">>
<<set _textboxMLArg = '_'+$args[1]>>
| Trade Qty
<<textbox `'_Num' + $args[0]` `eval(parse('_Num' + $args[0]))`>><>:
<<link "Buy">>
	<<if !Number.isInteger(Number.parseFloat(eval(parse('_Num' + $args[0]))))>> /* Checks if _Numasset is string */
/* Buy all */
		<<if ["all", "max"].includes(State.temporary[$args[1]].toLowerCase())>>
			<<set State.temporary[$args[1]] = Math.floor($corpCash/(State.variables[$args[0]+"AssetPrice"] * 500))>>
			<<replace `'#'+$args[0]+'AssetsPurchasing'`>>
				Bought <<print commaNum(Math.floor($corpCash/(State.variables[$args[0]+"AssetPrice"] * 500)))>> units for <<print cashFormat(Math.floor($corpCash/(State.variables[$args[0]+"AssetPrice"] * 500))*500*State.variables[$args[0]+"AssetPrice"])>>
			<</replace>>
			<<set State.variables[$args[0]+"Assets"] += (eval(parse('_Num' + $args[0]))) * 500>>
			<<set $corpCash -= (eval(parse('_Num' + $args[0]))) * 500 * State.variables[$args[0]+"AssetPrice"]>>
			<<replace '#cashOnHand'>>
				<<print cashFormat($corpCash)>>
			<</replace>>
			<<replace `'#'+$args[0]+'AssetsDisp'`>>
				<<print cashFormat(State.variables[$args[0]+"AssetPrice"] * State.variables[$args[0]+"Assets"])>> (<<print commaNum(Math.floor(State.variables[$args[0]+"Assets"]/500))>> unit<<if State.variables[$args[0]+"Assets"]/500 > 1>>s<</if>>)
			<</replace>>
			<<replace `'#'+'controls'+$args[0]`>><<CorpDevBuySell $args[0] $args[1]>><</replace>><>
		<<else>>
/* Error if decimal or not "all" or "max" string */
			<<replace `'#'+$args[0]+'AssetsPurchasing'`>> Please input a round number. <</replace>>
		<</if>>
/* Negative number, try again */
	<<elseif Number.parseFloat(eval(parse('_Num' + $args[0]))) < 0>>
		<<replace `'#'+$args[0]+'AssetsPurchasing'`>>Please input a positive number.<</replace>>
/* Buy % */
	<<elseif State.temporary[$args[1]].toString().includes("%")>>
		<<set _DesPerc = State.temporary[$args[1]]>>
		<<set State.temporary[$args[1]] = Math.floor((Math.clamp((Number.parseFloat(State.temporary[$args[1]])/100),0,1)*$corpCash)/(State.variables[$args[0]+"AssetPrice"] * 500))>>
		<<replace `'#'+$args[0]+'AssetsPurchasing'`>>
			Purchased <<print commaNum(State.temporary[$args[1]])>> units (<<print commaNum((State.temporary[$args[1]]*(State.variables[$args[0]+"AssetPrice"] * 50000)/$corpCash).toFixed(2))>>% of cash, desired was _DesPerc)
		<</replace>>
		<<set State.variables[$args[0]+"Assets"] += (eval(parse('_Num' + $args[0]))) * 500>>
		<<set $corpCash -= (eval(parse('_Num' + $args[0]))) * 500 * State.variables[$args[0]+"AssetPrice"]>>
		<<replace '#cashOnHand'>>
			<<print cashFormat($corpCash)>>
		<</replace>>
		<<replace `'#'+$args[0]+'AssetsDisp'`>>
			<<print cashFormat(State.variables[$args[0]+"AssetPrice"] * State.variables[$args[0]+"Assets"])>> (<<print commaNum(Math.floor(State.variables[$args[0]+"Assets"]/500))>> unit<<if State.variables[$args[0]+"Assets"]/500 > 1>>s<</if>>)
		<</replace>>
		<<replace `'#'+'controls'+$args[0]`>><<CorpDevBuySell $args[0] $args[1]>><</replace>><>
/* Successful buy */
	<<elseif (eval(parse('_Num' + $args[0]))) * 500 * State.variables[$args[0]+"AssetPrice"] < $corpCash>>
		<<set State.variables[$args[0]+"Assets"] += (eval(parse('_Num' + $args[0]))) * 500>>
		<<set $corpCash -= (eval(parse('_Num' + $args[0]))) * 500 * State.variables[$args[0]+"AssetPrice"]>>
		<<replace '#cashOnHand'>>
			<<print cashFormat($corpCash)>>
		<</replace>>
		<<replace `'#'+$args[0]+'AssetsDisp'`>>
			<<print cashFormat(State.variables[$args[0]+"AssetPrice"] * State.variables[$args[0]+"Assets"])>> (<<print commaNum(Math.floor(State.variables[$args[0]+"Assets"]/500))>> unit<<if State.variables[$args[0]+"Assets"]/500 > 1>>s<</if>>)
		<</replace>>
		<<replace `'#'+$args[0]+'AssetsPurchasing'`>><</replace>>
/* Unsuccessful buy but have enough cash for more than 1 unit */
	<<elseif $corpCash > 500 * State.variables[$args[0]+"AssetPrice"]>>
		<<replace `'#'+$args[0]+'AssetsPurchasing'`>>
			The corp can only buy <<print commaNum(Math.floor($corpCash/(State.variables[$args[0]+"AssetPrice"] * 500)))>> more unit<<if Math.floor($corpCash/(State.variables[$args[0]+"AssetPrice"] * 500)) > 1 >>s<</if>> of $args[0] assets.
		<</replace>>
		<<set State.temporary[$args[1]] = Math.floor($corpCash/(State.variables[$args[0]+"AssetPrice"] * 500))>>
		<<replace `'#'+'controls'+$args[0]`>><<CorpDevBuySell $args[0] $args[1]>><</replace>><>
/* Unsuccessful buy */
	<<else>>
		<<replace `'#'+$args[0]+'AssetsPurchasing'`>>There are insufficient funds for additional purchases.<</replace>>
	<</if>>
<</link>>
| <<link "Sell">>
	<<if !Number.isInteger(Number.parseFloat(eval(parse('_Num' + $args[0]))))>>
/* Sell all */
		<<if ["all", "max"].includes(State.temporary[$args[1]].toLowerCase())>>
			<<replace `'#'+$args[0]+'AssetsPurchasing'`>>
				Sold <<print commaNum(Math.ceil((State.variables[$args[0]+"Assets"]-500)/500))>> units for <<print cashFormat(Math.ceil((State.variables[$args[0]+"Assets"]-500)/500)*500*State.variables[$args[0]+"AssetPrice"])>>
			<</replace>>
			<<set State.temporary[$args[1]] = Math.ceil((State.variables[$args[0]+"Assets"]-500)/500)>>
			<<set State.variables[$args[0]+"Assets"] -= eval(parse('_Num' + $args[0])) * 500>>
			<<set $corpCash += eval(parse('_Num' + $args[0])) * 500 * State.variables[$args[0]+"AssetPrice"]>>
			<<replace '#cashOnHand'>>
				<<print cashFormat($corpCash)>>
			<</replace>>
			<<replace `'#'+$args[0]+'AssetsDisp'`>>
				<<print cashFormat(State.variables[$args[0]+"AssetPrice"] * State.variables[$args[0]+"Assets"])>> (<<if Math.ceil(State.variables[$args[0]+"Assets"]/500) <1>> <1 <<else>><<print commaNum(Math.ceil(State.variables[$args[0]+"Assets"]/500))>> <</if>>unit<<if State.variables[$args[0]+"Assets"]/500 > 1>>s<</if>>)
			<</replace>>
			<<replace `'#'+'controls'+$args[0]`>><<CorpDevBuySell $args[0] $args[1]>><</replace>><>
/* Error if decimal or not "all" or "max" string */
		<<else>>
			<<replace `'#'+$args[0]+'AssetsPurchasing'`>> Please input a round number. <</replace>>
		<</if>>
/* Negative number, try again */
	<<elseif Number.parseFloat(eval(parse('_Num' + $args[0]))) < 0>>
		<<replace `'#'+$args[0]+'AssetsPurchasing'`>>Please input a positive number.<</replace>>
/* Sell % */
	<<elseif State.temporary[$args[1]].toString().includes("%")>>
		<<set _DesPerc = State.temporary[$args[1]]>>
		<<set State.temporary[$args[1]] = Math.ceil((State.variables[$args[0]+"Assets"]-500)/500*(Math.clamp((Number.parseFloat(State.temporary[$args[1]])/100),0,1)))>>
		<<replace `'#'+$args[0]+'AssetsPurchasing'`>>
			Sold <<print commaNum(State.temporary[$args[1]])>> units (<<print commaNum((State.temporary[$args[1]]*100/((State.variables[$args[0]+"Assets"]-500)/500)).toFixed(2))>>% of $args[0] assets, desired was _DesPerc)
		<</replace>>
		<<set State.variables[$args[0]+"Assets"] -= eval(parse('_Num' + $args[0])) * 500>>
		<<set $corpCash += eval(parse('_Num' + $args[0])) * 500 * State.variables[$args[0]+"AssetPrice"]>>
		<<replace '#cashOnHand'>>
			<<print cashFormat($corpCash)>>
		<</replace>>
		<<replace `'#'+$args[0]+'AssetsDisp'`>>
			<<print cashFormat(State.variables[$args[0]+"AssetPrice"] * State.variables[$args[0]+"Assets"])>> (<<if Math.ceil(State.variables[$args[0]+"Assets"]/500) <1>> <1 <<else>><<print commaNum(Math.ceil(State.variables[$args[0]+"Assets"]/500))>> <</if>>unit<<if State.variables[$args[0]+"Assets"]/500 > 1>>s<</if>>)
		<</replace>>
		<<replace `'#'+'controls'+$args[0]`>><<CorpDevBuySell $args[0] $args[1]>><</replace>><>
/* Successful sell */
	<<elseif State.variables[$args[0]+"Assets"] > 500 && eval(parse('_Num' + $args[0])) * 500 < State.variables[$args[0]+"Assets"]>>
		<<set State.variables[$args[0]+"Assets"] -= eval(parse('_Num' + $args[0])) * 500>>
		<<set $corpCash += eval(parse('_Num' + $args[0])) * 500 * State.variables[$args[0]+"AssetPrice"]>>
		<<replace '#cashOnHand'>>
			<<print cashFormat($corpCash)>>
		<</replace>>
		<<replace `'#'+$args[0]+'AssetsDisp'`>>
			<<print cashFormat(State.variables[$args[0]+"AssetPrice"] * State.variables[$args[0]+"Assets"])>> (<<if (State.variables[$args[0]+"Assets"]/500) <1>> <1 <<else>><<print commaNum(Math.ceil(State.variables[$args[0]+"Assets"]/500))>> <</if>>unit<<if State.variables[$args[0]+"Assets"]/500 > 1>>s<</if>>)
		<</replace>>
		<<replace `'#'+$args[0]+'AssetsPurchasing'`>><</replace>>
/* Unsuccessful sell but have enough assets for more than 1 unit */
	<<elseif State.variables[$args[0]+"Assets"] > 500>>
		<<replace `'#'+$args[0]+'AssetsPurchasing'`>>
			The corp can only sell <<print commaNum(Math.ceil((State.variables[$args[0]+"Assets"]-500)/500))>> more unit<<if Math.ceil((State.variables[$args[0]+"Assets"]-500)/500) > 1 >>s<</if>> of $args[0] assets.
		<</replace>>
		<<set State.temporary[$args[1]] = Math.ceil((State.variables[$args[0]+"Assets"]-500)/500)>>
		<<replace `'#'+'controls'+$args[0]`>><<CorpDevBuySell $args[0] $args[1]>><</replace>><>
/* Unsuccessful sell */
	<<else>>
		<<replace `'#'+$args[0]+'AssetsPurchasing'`>>The corp cannot sell any more $args[0] assets.<</replace>>
	<</if>>
<</link>>
<</widget>>


code:
:: Market Widgets [widget nobr]

/*
 * Look for widgets in passages tagged with "market:marketname" which
 * end in "Populate" and calls them all with the second argument
 * (an "origins" array) as argument.
 *
 * Call like <<AddMarketOrigins "marketname" _possibleOrigins>>
 */

<<widget "AddMarketOrigins">>
<<if _.isString($args[0]) && _.isArray($args[1])>>
	<<set _sanityCheck = '>>', /* to appease sanityCheck */
		_widgets = _(Story.widgets)
		.filter(function(wp) { return wp.tags.includes('market:' + $args[0]); })
		.flatMap(function(wp) {
			var re = RegExp('<<widget\\s+"([^"]+Populate)"\\s*>>', 'g');
			var match, result = [];
			while(match = re.exec(wp.text)) { result.push(match[1]); }
			return result; })
		.value()>>
	<<silently>><<foreach _widget of _widgets>>
		<<= "<<" + _widget + " $args[1]>>">>
	<</foreach>><</silently>>
<</if>>
<</widget>>

<<widget "CreateSlaveByOrigin">>
<<silently>>
	<<if _.isObject($args[0]) && _.isString($args[0].widget)>>
		<<= "<<" + $args[0].widget + " $args[0]>>">>
	<<elseif _.isString($args[0])>>
		<<= "<<" + $args[0] + " $args[0]>>">>
	<<else>>
		<<include "Generate Random Slave">>
	<</if>>
<</silently>>
<</widget>>