:: unitsBattleReport [nobr]

<<if $losses == 0>>
	<<if App.SecExp.conflict.deployedUnits('bots')>>
		<br>
		Security Drones: no casualties.
	<</if>>
	<<if $SF.Toggle && $SF.Active >= 1 && $SFIntervention>>
		<br>
		<<print num($SF.ArmySize)>> soldiers from $SF.Lower joined the battle: no casualties suffered.
	<</if>>
	<<if App.SecExp.conflict.deployedUnits('militia') >= 1>>
		<<for _j = 0; _j < $militiaUnits.length; _j++>>
			<<if $militiaUnits[_j].isDeployed == 1>>
				<br>
				$militiaUnits[_j].platoonName: no casualties.
				<<if $militiaUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Experience has increased.
						<<set $militiaUnits[_j].training += random(5,15) + $majorBattle * random(5,15)>>
					<</if>>
				<</if>>
			<</if>>
		<</for>>
	<</if>>
	<<if App.SecExp.conflict.deployedUnits('slaves') >= 1>>
		<<for _j = 0; _j < $slaveUnits.length; _j++>>
			<<if $slaveUnits[_j].isDeployed == 1>>
				<br>
				$slaveUnits[_j].platoonName: no casualties.
				<<if $slaveUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Experience has increased.
						<<set $slaveUnits[_j].training += random(5,15) + $majorBattle * random(5,15)>>
					<</if>>
				<</if>>
			<</if>>
		<</for>>
	<</if>>
	<<if App.SecExp.conflict.deployedUnits('mercs') >= 1>>
		<<for _j = 0; _j < $mercUnits.length; _j++>>
			<<if $mercUnits[_j].isDeployed == 1>>
				<br>
				$mercUnits[_j].platoonName: no casualties.
				<<if $mercUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Experience has increased.
						<<set $mercUnits[_j].training += random(5,15) + $majorBattle * random(5,15)>>
					<</if>>
				<</if>>
			<</if>>
		<</for>>
	<</if>>

<<elseif $losses > 0>>
	/* if the losses are more than zero */
	/* generates a list of randomized losses, from which each unit picks one at random */
	<<set _losses = $losses>>
	<<set _averageLosses = Math.trunc(_losses / App.SecExp.conflict.deployedUnits())>>
	<<set _lossesList = []>>
	<<set _validityCount = 0>>
	<<for _i = 0; _i < App.SecExp.conflict.deployedUnits(); _i++>>
		<<set _assignedLosses = Math.trunc(Math.clamp(_averageLosses + random(-5,5), 0, 100))>>
		<<if _assignedLosses > _losses>>
			<<set _assignedLosses = _losses>>
			<<set _losses = 0>>
		<<else>>
			<<set _losses -= _assignedLosses>>
		<</if>>
		<<set _lossesList.push(_assignedLosses)>>
		<<set _validityCount += _assignedLosses>>
	<</for>>
	<<if _losses > 0>>
		<<set _lossesList[random(_lossesList.length - 1)] += _losses>>
	<</if>>
	<<set _lossesList.shuffle()>>

	/* sanity check for losses */
	<<set _count = 0>>
	<<for _i = 0; _i < _lossesList.length; _i++>>
		<<if !Number.isInteger(_lossesList[_i])>>
			<<set _lossesList[_i] = 0>>
		<</if>>
		<<set _count += _lossesList[_i]>>
	<</for>>
	<<if _count < $losses>>
		<<set _rand = random(_lossesList.length - 1)>>
		<<set _lossesList[_rand] += $losses - _count>>
	<<elseif _count > $losses>>
		<<set _diff = _count - $losses>>
		<<set _rand = random(_lossesList.length - 1)>>
		<<set _lossesList[_rand] = Math.trunc(_lossesList[_rand]-_diff,0,100)>>
	<</if>>

	/* assigns the losses and notify the player */
	<<if App.SecExp.conflict.deployedUnits('bots')>>
		<br>
		<<set _loss = _lossesList.pluck()>>
		<<set _loss = Math.clamp(_loss,0,$secBots.troops)>>
		<<set $secBots.troops -= _loss>>
		Security drones:
		<<if _loss <= 0>>
			no casualties
		<<elseif _loss <= $secBots.troops * 0.2>>
			light casualties
		<<elseif _loss <= $secBots.troops * 0.4>>
			moderate casualties
		<<elseif _loss <= $secBots.troops * 0.6>>
			heavy casualties
		<<else>>
			catastrophic casualties
		<</if>>
		suffered.
		<<if $secBots.troops <= 5>>
			<<set $secBots.active = 0>>
			Unfortunately the losses they took were simply too great, their effective combatants are in so small number you can no longer call them a deployable unit. It will take quite the investment to rebuild them.
		<<elseif $secBots.troops <= 10>>
			The unit has very few operatives left, it risks complete annihilation if deployed again.
		<</if>>
		<br>
	<</if>>
	<<if $SF.Toggle && $SF.Active >= 1 && $SFIntervention>>
		<br>
		<<set _loss = _lossesList.pluck()>>
		<<set _loss = Math.clamp(_loss,0,$SF.ArmySize)>>
		<<print num($SF.ArmySize)>> soldiers from $SF.Lower joined the battle:
		<<if _loss <= 0>>
			no casualties
		<<elseif _loss <= 10>>
			light casualties
		<<elseif _loss <= 30>>
			moderate casualties
		<<elseif _loss <= 60>>
			heavy casualties
		<<else>>
			catastrophic casualties
		<</if>>
		suffered.
		<<set $SF.ArmySize -= _loss>>
		<br>
	<</if>>
	<<if App.SecExp.conflict.deployedUnits('militia') >= 1>>
		<<for _j = 0; _j < $militiaUnits.length; _j++>>
			<<if $militiaUnits[_j].isDeployed == 1>>
				<br>
				<<set $militiaUnits[_j].battlesFought++>>
				<<set _loss = _lossesList.pluck()>>
				<<set _loss = Math.clamp(_loss,0,$militiaUnits[_j].troops)>>
				$militiaUnits[_j].platoonName:
				<<if _loss <= 0>>
					no casualties
				<<elseif _loss <= $militiaUnits[_j].troops * 0.2>>
					light casualties
				<<elseif _loss <= $militiaUnits[_j].troops * 0.4>>
					moderate casualties
				<<elseif _loss <= $militiaUnits[_j].troops * 0.6>>
					heavy casualties
				<<else>>
					catastrophic casualties
				<</if>>
				suffered.
				<<set _med = Math.round(Math.clamp(_loss * $militiaUnits[_j].medics * 0.25,1,_loss))>>
				<<if $militiaUnits[_j].medics == 1 && _loss > 0>>
					Some men were saved by their medics.
				<</if>>
				<<set $militiaUnits[_j].troops -= Math.trunc(Math.clamp(_loss - _med,0,$militiaUnits[_j].maxTroops))>>
				<<set $militiaEmployedManpower -= Math.trunc(_loss - _med)>>
				<<set $militiaTotalCasualties += Math.trunc(_loss - _med)>>
				<<set $militiaTotalManpower -= Math.trunc(_loss - _med)>>
				<<if $militiaUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Experience has increased.
						<<set $militiaUnits[_j].training += random(5,15) + $majorBattle * random(5,15)>>
					<</if>>
				<</if>>
				<<if $militiaUnits[_j].troops <= 5>>
					<<set $militiaUnits[_j].active = 0>>
					<br>Unfortunately the losses they took were simply too great, their effective combatants are in so small number you can no longer call them a deployable unit. The remnants will be sent home honored as veterans or reorganized in a new unit.
				<<elseif $militiaUnits[_j].troops <= 10>>
					<br>The unit has very few operatives left, it risks complete annihilation if deployed again.
				<</if>>
			<</if>>
		<</for>>
	<</if>>
	<<if App.SecExp.conflict.deployedUnits('slaves') >= 1>>
		<<for _j = 0; _j < $slaveUnits.length; _j++>>
			<<if $slaveUnits[_j].isDeployed == 1>>
				<br>
				<<set $slaveUnits[_j].battlesFought++>>
				<<set _loss = _lossesList.pluck()>>
				<<set _loss = Math.clamp(_loss,0,$slaveUnits[_j].troops)>>
				$slaveUnits[_j].platoonName:
				<<if _loss <= 0>>
					no casualties
				<<elseif _loss <= $slaveUnits[_j].troops * 0.2>>
					light casualties
				<<elseif _loss <= $slaveUnits[_j].troops * 0.4>>
					moderate casualties
				<<elseif _loss <= $slaveUnits[_j].troops * 0.6>>
					heavy casualties
				<<else>>
					catastrophic casualties
				<</if>>
				suffered.
				<<set _med = Math.round(Math.clamp(_loss * $slaveUnits[_j].medics * 0.25,1,_loss))>>
				<<if $slaveUnits[_j].medics == 1 && _loss > 0>>
					Some men were saved by their medics.
				<</if>>
				<<set $slaveUnits[_j].troops -= Math.trunc(Math.clamp(_loss - _med,0,$slaveUnits[_j].maxTroops))>>
				<<set $slavesEmployedManpower -= Math.trunc(_loss - _med)>>
				<<set $slavesTotalCasualties += Math.trunc(_loss - _med)>>
				<<set $menials -= Math.clamp($menials, 0, Math.trunc(_loss - _med))>>
				<<if $slaveUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Experience has increased.
						<<set $slaveUnits[_j].training += random(5,15) + $majorBattle * random(5,15)>>
					<</if>>
				<</if>>
				<<if $slaveUnits[_j].troops <= 5>>
					<<set $slaveUnits[_j].active = 0>>
					<br>Unfortunately the losses they took were simply too great, their effective combatants are in so small number you can no longer call them a deployable unit. The survivors will be sent home honored as veterans or reorganized in a new unit.
				<<elseif $slaveUnits[_j].troops <= 10>>
					<br>The unit has very few operatives left, it risks complete annihilation if deployed again.
				<</if>>
			<</if>>
		<</for>>
	<</if>>
	<<if App.SecExp.conflict.deployedUnits('mercs') >= 1>>
		<<for _j = 0; _j < $mercUnits.length; _j++>>
			<<if $mercUnits[_j].isDeployed == 1>>
				<br>
				<<set $mercUnits[_j].battlesFought++>>
				<<set _loss = _lossesList.pluck()>>
				<<set _loss = Math.clamp(_loss,0,$mercUnits[_j].troops)>>
				$mercUnits[_j].platoonName:
				<<if _loss <= 0>>
					no casualties
				<<elseif _loss <= $mercUnits[_j].troops * 0.2>>
					light casualties
				<<elseif _loss <= $mercUnits[_j].troops * 0.4>>
					moderate casualties
				<<elseif _loss <= $mercUnits[_j].troops * 0.6>>
					heavy casualties
				<<else>>
					catastrophic casualties
				<</if>>
				suffered.
				<<set _med = Math.round(Math.clamp(_loss * $mercUnits[_j].medics * 0.25,1,_loss))>>
				<<if $mercUnits[_j].medics == 1 && _loss > 0>>
					Some men were saved by their medics.
				<</if>>
				<<set $mercUnits[_j].troops -= Math.trunc(Math.clamp(_loss - _med,0,$mercUnits[_j].maxTroops))>>
				<<set $mercEmployedManpower -= Math.trunc(_loss - _med)>>
				<<set $mercTotalCasualties += Math.trunc(_loss - _med)>>
				<<set $mercTotalManpower -= Math.trunc(_loss - _med)>>
				<<if $mercUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Experience has increased.
						<<set $mercUnits[_j].training += random(5,15) + $majorBattle * random(5,15)>>
					<</if>>
				<</if>>
				<<if $mercUnits[_j].troops <= 5>>
					<<set $mercUnits[_j].active = 0>>
					<br>Unfortunately the losses they took were simply too great, their effective combatants are in so small number you can no longer call them a deployable unit. The remnants will be sent home honored as veterans or reorganized in a new unit.
				<<elseif $mercUnits[_j].troops <= 10>>
					<br>The unit has very few operatives left, it risks complete annihilation if deployed again.
				<</if>>
			<</if>>
		<</for>>
	<</if>>
<<else>>
	<br>@@.red;Error: losses are a negative number or NaN@@
<</if>> /* closes check for more than zero casualties */

src/Mods/SecExp/unitsBattleReport.tw