code:
:: SecExpBackwardCompatibility [nobr]

<<set $nextButton = "Continue", $nextLink = "Main", $returnTo = "Main">>

/* base stats */
<<if ndef $authority>>
	<<set $authority = 0>>
<</if>>
<<if ndef $security>>
	<<set $security = 30>>
<</if>>
<<if ndef $secRestPoint>>
	<<set $secRestPoint = 40>>
<</if>>
<<if ndef $crime>>
	<<set $crime = 30>>
<</if>>
<<if ndef $crimeCap>>
	<<set $crimeCap = 100>>
<</if>>
<<if ndef $readiness>>
	<<set $readiness = 1>>
<</if>>
<<if ndef $recon>>
	<<set $recon = 0>>
<</if>>
<<if ndef $maxUnits>>
	<<set $maxUnits = 6>>
<</if>>
<<if ndef $trade>>
	<<set $trade = 0>>
	<<calcInitialTrade>>
<</if>>
<<if ndef $activeUnits>>
	<<set $activeUnits = 0>>
<</if>>
<<if ndef $attackType>>
	<<set $attackType = "none">>
<</if>>
<<if ndef $attackThisWeek>>
	<<set $attackThisWeek = 0>>
<</if>>
<<if ndef $lastAttackWeeks>>
	<<set $lastAttackWeeks = 0>>
<</if>>
<<set $lastRebellionWeeks = Number($lastRebellionWeeks) || 0>>
<<if ndef $hasFoughtOnce>>
	<<set $hasFoughtOnce = 0>>
<</if>>
<<if ndef $hasFoughtMajorBattleOnce>>
	<<set $hasFoughtMajorBattleOnce = 0>>
<</if>>
<<if ndef $hasRebelledOnce>>
	<<set $hasRebelledOnce = 0>>
<</if>>
<<if ndef $majorBattle>>
	<<set $majorBattle = 0>>
<</if>>
<<if ndef $PCvictoryStreak>>
	<<set $PCvictoryStreak = 0>>
<</if>>
<<if ndef $PClossStreak>>
	<<set $PClossStreak = 0>>
<</if>>
<<if ndef $oldFlux>>
	<<set $oldFlux = 0>>
<</if>>
<<if ndef $wasToggledBefore>>
	<<set $wasToggledBefore = 0>>
<</if>>
<<if ndef $showBattleStatistics>>
	<<set $showBattleStatistics = 0>>
<</if>>
<<if ndef $forceBattle>>
	<<set $forceBattle = 0>>
<</if>>
<<if ndef $forceMajorBattle>>
	<<set $forceMajorBattle = 0>>
<</if>>
<<if ndef $foughtThisWeek>>
	<<set $foughtThisWeek = 0>>
<</if>>
<<if ndef $battlesEnabled>>
	<<set $battlesEnabled = 1>>
<</if>>
<<if ndef $battlesEnabledOceanic>>
	<<set $battlesEnabledOceanic = 0>>
<</if>>
<<if ndef $majorBattlesEnabled>>
	<<set $majorBattlesEnabled = 1>>
<</if>>
<<if ndef $majorBattleGameOver>>
	<<set $majorBattleGameOver = 1>>
<</if>>
<<if ndef $rebellionsEnabled>>
	<<set $rebellionsEnabled = 1>>
<</if>>
<<if ndef $forceRebellion>>
	<<set $forceRebellion = 0>>
<</if>>
<<if ndef $rebellionGameOver>>
	<<set $rebellionGameOver = 1>>
<</if>>
<<if ndef $allowPrestigeFromBattles>>
	<<set $allowPrestigeFromBattles = 1>>
<</if>>
<<if ndef $battleFrequency>>
	<<set $battleFrequency = 1>>
<</if>>
<<if ndef $rebellionSpeed>>
	<<set $rebellionSpeed = 1>>
<</if>>
<<if ndef $majorBattleMult>>
	<<set $majorBattleMult = 1>>
<</if>>

/* edicts */
<<if ndef $edictsUpkeep>>
	<<set $edictsUpkeep = 0>>
<</if>>
<<if ndef $edictsAuthUpkeep>>
	<<set $edictsAuthUpkeep = 0>>
<</if>>
<<if ndef $alternativeRents>>
	<<set $alternativeRents = 0>>
<</if>>
<<if ndef $enslavementRights>>
	<<set $enslavementRights = 0>>
<</if>>
<<if ndef $securityExemption>>
	<<set $securityExemption = 0>>
<</if>>
<<if ndef $sellData>>
	<<set $sellData = 0>>
<</if>>
<<if ndef $propCampaignBoost>>
	<<set $propCampaignBoost = 0>>
<</if>>
<<if ndef $slaveWatch>>
	<<set $slaveWatch = 0>>
<</if>>
<<if ndef $subsidyChurch>>
	<<set $subsidyChurch = 0>>
<</if>>
<<if ndef $limitImmigration>>
	<<set $limitImmigration = 0>>
<</if>>
<<if ndef $openBorders>>
	<<set $openBorders = 0>>
<</if>>
<<if ndef $limitSubhumans>>
	<<set $limitSubhumans = 0>>
<</if>>
<<if ndef $slavesOfficers>>
	<<set $slavesOfficers = 0>>
<</if>>
<<if ndef $martialSchool>>
	<<set $martialSchool = 0>>
<</if>>
<<if ndef $discountMercenaries>>
	<<set $discountMercenaries = 0>>
<</if>>
<<if ndef $militiaFounded>>
	<<set $militiaFounded = 0>>
<</if>>
<<if ndef $recruitVolunteers>>
	<<set $recruitVolunteers = 0>>
<</if>>
<<if ndef $conscription>>
	<<set $conscription = 0>>
<</if>>
<<if ndef $militaryService>>
	<<set $militaryService = 0>>
<</if>>
<<if ndef $militarizedSociety>>
	<<set $militarizedSociety = 0>>
<</if>>
<<if ndef $militaryExemption>>
	<<set $militaryExemption = 0>>
<</if>>
<<if ndef $lowerRquirements>>
	<<set $lowerRquirements = 0>>
<</if>>
<<if ndef $noSubhumansInArmy>>
	<<set $noSubhumansInArmy = 0>>
<</if>>
<<if ndef $pregExemption>>
	<<set $pregExemption = 0>>
<</if>>
<<if ndef $eliteOfficers>>
	<<set $eliteOfficers = 0>>
<</if>>
<<if ndef $liveTargets>>
	<<set $liveTargets = 0>>
<</if>>
<<if ndef $legionTradition>>
	<<set $legionTradition = 0>>
<</if>>
<<if ndef $eagleWarriors>>
	<<set $eagleWarriors = 0>>
<</if>>
<<if ndef $ronin>>
	<<set $ronin = 0>>
<</if>>
<<if ndef $sunTzu>>
	<<set $sunTzu = 0>>
<</if>>
<<if ndef $mamluks>>
	<<set $mamluks = 0>>
<</if>>
<<if ndef $pharaonTradition>>
	<<set $pharaonTradition = 0>>
<</if>>
<<if ndef $weaponsLaw>>
	<<set $weaponsLaw = 3>>
<</if>>
<<if ndef $soldierWages>>
	<<set $soldierWages = 1>>
<</if>>
<<if ndef $militiaSoldierPrivilege>>
	<<set $militiaSoldierPrivilege = 0>>
<</if>>
<<if ndef $slaveSoldierPrivilege>>
	<<set $slaveSoldierPrivilege = 0>>
<</if>>
<<if ndef $mercSoldierPrivilege>>
	<<set $mercSoldierPrivilege = 0>>
<</if>>
<<if ndef $tradeLegalAid>>
	<<set $tradeLegalAid = 0>>
<</if>>
<<if ndef $taxTrade>>
	<<set $taxTrade = 0>>
<</if>>

/* proclamations */
<<if ndef $proclamationsCooldown>>
	<<set $proclamationsCooldown = 0>>
<</if>>
<<if ndef $proclamationCurrency>>
	<<set $proclamationCurrency = "authority">>
<</if>>
<<if ndef $proclamationType>>
	<<set $proclamationType = "none">>
<</if>>

/* buildings */
<<if ndef $secHQ>>
	<<set $secHQ = 0>>
<</if>>
<<if ndef $secBarracks>>
	<<set $secBarracks = 0>>
<</if>>
<<if ndef $secBarracksUpgrades>>
	<<set $secBarracksUpgrades ={
	size: 0,
	luxury: 0,
	training: 0,
	loyaltyMod:0 }>>
<</if>>
<<if ndef $secBarracksUpgrades.loyaltyMod>>
	<<set $secBarracksUpgrades.loyaltyMod = 0>>
<</if>>
<<if ndef $secMenials>>
	<<set $secMenials = $secHelots || 0>>
	<<unset $secHelots>>
<</if>>
<<if ndef $reqMenials>>
	<<set $reqMenials = $reqHelots || 20>>
	<<unset $reqHelots>>
<</if>>
<<if ndef $secUpgrades >>
	<<set $secUpgrades = {
	nanoCams: 0,
	cyberBots: 0,
	eyeScan: 0,
	cryptoAnalyzer: 0,
	coldstorage: 0}>>
<</if>>
<<if ndef $crimeUpgrades>>
	<<set $crimeUpgrades = {
	autoTrial: 0,
	autoArchive: 0,
	worldProfiler: 0,
	advForensic: 0}>>
<</if>>
<<if ndef $intelUpgrades>>
	<<set $intelUpgrades = {
	sensors: 0,
	radar: 0,
	signalIntercept: 0}>>
<</if>>
<<if ndef $readinessUpgrades>>
	<<set $readinessUpgrades = {
	earlyWarn: 0,
	rapidPlatforms: 0,
	pathways: 0,
	rapidVehicles: 0}>>
<</if>>
<<if ndef $propHub>>
	<<set $propHub = 0>>
<</if>>
<<if ndef $propCampaign>>
	<<set $propCampaign = 0>>
<</if>>
<<if ndef $propFocus>>
	<<set $propFocus = "none">>
<</if>>
<<if ndef $miniTruth>>
	<<set $miniTruth = 0>>
<</if>>
<<if ndef $fakeNews>>
	<<set $fakeNews = 0>>
<</if>>
<<if ndef $controlLeaks>>
	<<set $controlLeaks = 0>>
<</if>>
<<if ndef $secretService>>
	<<set $secretService = 0>>
<</if>>
<<if ndef $blackOps>>
	<<set $blackOps = 0>>
<</if>>
<<if ndef $marketInfiltration>>
	<<set $marketInfiltration = 0>>
<</if>>
<<if ndef $propHubUpkeep>>
	<<set $propHubUpkeep = $facilityCost * 5>>
<</if>>
<<if ndef $secHQUpkeep>>
	<<set $secHQUpkeep = $facilityCost * 5>>
<</if>>
<<if ndef $secBarracksUpkeep>>
	<<set $secBarracksUpkeep = $facilityCost * 5>>
<</if>>
<<if ndef $upgradeUpkeep>>
	<<set $upgradeUpkeep = 50>>
<</if>>
<<if ndef $riotCenter>>
	<<set $riotCenter = 0>>
<</if>>
<<if ndef $riotUpkeep>>
	<<set $riotUpkeep = $facilityCost * 5>>
<</if>>
<<if ndef $riotUpgrades>>
	<<set $riotUpgrades = {
	freeMedia: 0,
	rapidUnit: 0,
	rapidUnitSpeed: 0}>>
<</if>>
<<if ndef $fort>>
	<<set $fort = {
	reactor: 0,
	waterway: 0,
	assistant: 0}>>
<</if>>
<<if ndef $sentUnitCooldown>>
	<<set $sentUnitCooldown = 0>>
<</if>>
<<if ndef $advancedRiotEquip>>
	<<set $advancedRiotEquip = 0>>
<</if>>
<<if ndef $brainImplant>>
	<<set $brainImplant = -1>>
<</if>>
<<if ndef $brainImplantProject>>
	<<set $brainImplantProject = 0>>
<</if>>
<<if def $weapHelots>>
	<<set $weapMenials = $weapHelots || 0>>
	<<unset $weapHelots>>
<</if>>
<<if ndef $weapManu>>
	<<set $weapManu = 0>>
<</if>>
<<if ndef $weapProductivity>>
	<<set $weapProductivity = 1>>
<</if>>
<<if ndef $weapLab>>
	<<set $weapLab = 1>>
<</if>>
<<if ndef $baseUpgradeTime>>
	<<set $baseUpgradeTime = 10>>
<</if>>
<<if ndef $weapUpgrades>>
	<<set $weapUpgrades = []>>
<</if>>
<<if ndef $currentUpgrade>>
	<<set $currentUpgrade = {
	ID: 0,
	name: " ",
	unit: 0,
	type: 0,
	time: 0,
	value: 0}>>
<<else>>
	<<if ndef $currentUpgrade.ID>>
		<<if $currentUpgrade.name == "magnetic based ballistic weaponry">>
			<<set $currentUpgrade.ID = 0>>
		<<elseif $currentUpgrade.name == "ceramo-metallic alloys">>
			<<set $currentUpgrade.ID = 1>>
		<<elseif $currentUpgrade.name == "rapid action stimulants">>
			<<set $currentUpgrade.ID = 2>>
		<<elseif $currentUpgrade.name == "fast response neural stimulant">>
			<<set $currentUpgrade.ID = 3>>
		<<elseif $currentUpgrade.name == "universal cyber enhancements">>
			<<set $currentUpgrade.ID = 4>>
		<<elseif $currentUpgrade.name == "remote neural links">>
			<<set $currentUpgrade.ID = 5>>
		<<elseif $currentUpgrade.name == "combined training regimens with the special force">>
			<<set $currentUpgrade.ID = 6>>
		<<elseif $currentUpgrade.name == "a variant of the stimulant cocktail that the special force created">>
			<<set $currentUpgrade.ID = 7>>
		<<elseif $currentUpgrade.name == "a mesh network based off the custom network of the special force">>
			<<set $currentUpgrade.ID = 8>>
		<<elseif $currentUpgrade.name == "dynamic battle aware AI">>
			<<set $currentUpgrade.ID = -1>>
		<<elseif $currentUpgrade.name == "adaptive armored frames">>
			<<set $currentUpgrade.ID = -2>>
		<<elseif $currentUpgrade.name == "advanced synthetic alloys">>
			<<set $currentUpgrade.ID = -3>>
		<</if>>
	<</if>>
<</if>>
<<if ndef $droneUpgrades>>
	<<set $droneUpgrades = {
	attack: 0,
	defense: 0,
	hp:0}>>
<<elseif $droneUpgrades == null>>
	<<set $droneUpgrades = {
	attack: 0,
	defense: 0,
	hp:0}>>
<<else>>
	<<if Number.isInteger($droneUpgrades)>>
		<<unset $droneUpgrades>>
		<<set $droneUpgrades = {
		attack: 0,
		defense: 0,
		hp:0}>>
	<</if>>
	<<if ndef $droneUpgrades.attack || !Number.isInteger($droneUpgrades.attack)>>
		<<set $droneUpgrades.attack = 0>>
	<</if>>
	<<if ndef $droneUpgrades.defense || !Number.isInteger($droneUpgrades.defense)>>
		<<set $droneUpgrades.defense = 0>>
	<</if>>
	<<if ndef $droneUpgrades.hp || !Number.isInteger($droneUpgrades.hp)>>
		<<set $droneUpgrades.hp = 0>>
	<</if>>
<</if>>
<<if ndef $humanUpgrade>>
	<<set $humanUpgrade = {
	attack: 0,
	defense: 0,
	hp:0,
	morale: 0}>>
<<elseif $humanUpgrade == null>>
	<<set $humanUpgrade = {
	attack: 0,
	defense: 0,
	hp:0,
	morale: 0}>>
<<else>>
	<<if Number.isInteger($humanUpgrade)>>
		<<unset $humanUpgrade>>
		<<set $humanUpgrade = {
		attack: 0,
		defense: 0,
		hp:0,
		morale: 0}>>
	<</if>>
	<<if ndef $humanUpgrade.attack || !Number.isInteger($humanUpgrade.attack)>>
		<<set $humanUpgrade.attack = 0>>
	<</if>>
	<<if ndef $humanUpgrade.defense || !Number.isInteger($humanUpgrade.defense)>>
		<<set $humanUpgrade.defense = 0>>
	<</if>>
	<<if ndef $humanUpgrade.hp || !Number.isInteger($humanUpgrade.hp)>>
		<<set $humanUpgrade.hp = 0>>
	<</if>>
	<<if ndef $humanUpgrade.morale || !Number.isInteger($humanUpgrade.morale)>>
		<<set $humanUpgrade.morale = 0>>
	<</if>>
<</if>>
<<if ndef $sellTo>>
	<<set $sellTo = {
	citizen: 1,
	raiders: 1,
	oldWorld: 1,
	FC: 1}>>
<</if>>
<<if ndef $completedUpgrades>>
	<<set $completedUpgrades = []>>
<</if>>
<<if ndef $transportHub>>
	<<set $transportHub = 0>>
<</if>>
<<if ndef $airport>>
	<<set $airport = 1>>
<</if>>
<<if ndef $railway>>
	<<set $railway = 1>>
<</if>>
<<if ndef $docks>>
	<<set $docks = 1>>
<</if>>
<<if ndef $hubSecurity>>
	<<set $hubSecurity = 1>>
<</if>>

/* events */
<<if ndef $smilingManProgress>>
	<<set $smilingManProgress = 0>>
<</if>>
<<if ndef $investedFunds>>
	<<set $investedFunds = 0>>
<</if>>
<<if ndef $relationshipLM>>
	<<set $relationshipLM = 0>>
<</if>>
<<if ndef $captureRoute>>
	<<set $captureRoute = 0>>
<</if>>
<<if ndef $collaborationRoute>>
	<<set $collaborationRoute = 0>>
<</if>>
<<if ndef $smilingManWeek>>
	<<set $smilingManWeek = 0>>
<</if>>
<<if ndef $globalCrisisWeeks>>
	<<set $globalCrisisWeeks = 0>>
<</if>>
<<if ndef $smilingManFate>>
	<<set $smilingManFate = 4>>
<</if>>

/* rebellions */
<<if ndef $tension>>
	<<set $tension = 0>>
<</if>>
<<if ndef $slaveProgress>>
	<<set $slaveProgress = 0>>
<</if>>
<<if ndef $citizenProgress>>
	<<set $citizenProgress = 0>>
<</if>>
<<if ndef $slaveRebellionEventFires>>
	<<set $slaveRebellionEventFires = 0>>
<</if>>
<<if ndef $citizenRebellionEventFires>>
	<<set $citizenRebellionEventFires = 0>>
<</if>>
<<if ndef $slaveRebellion>>
	<<set $slaveRebellion = 0>>
<</if>>
<<if ndef $citizenRebellion>>
	<<set $citizenRebellion = 0>>
<</if>>
<<if ndef $rebellingUnits>>
	<<set $rebellingUnits = []>>
<</if>>
<<if ndef $notInvolved>>
	<<set $notInvolved = 0>>
<</if>>
<<if ndef $engageRule>>
	<<set $engageRule = 0>>
<</if>>
<<if ndef $irregulars>>
	<<set $irregulars = 0>>
<</if>>
<<if ndef $rebellingMilitia>>
	<<set $rebellingMilitia = 0>>
<</if>>
<<if ndef $rebellingSlaves>>
	<<set $rebellingSlaves = 0>>
<</if>>
<<if ndef $rebellingMercs>>
	<<set $rebellingMercs = 0>>
<</if>>
<<if ndef $repairTime>>
	<<set $repairTime = 3>>
<</if>>
<<if ndef $arcRepairTime>>
	<<set $arcRepairTime = 0>>
<</if>>
<<if ndef $rebelDefeatAftermath>>
	<<set $rebelDefeatAftermath = 0>>
<</if>>
<<if ndef $garrison>>
	<<set $garrison = {
	penthouse: 0,
	reactor: 0,
	assistant: 0,
	waterway: 0,
	reactorTime: 0,
	assistantTime: 0,
	waterwayTime: 0}>>
<<else>>
	<<if ndef $garrison.penthouse>>
		<<set $garrison.penthouse = 0>>
	<</if>>
	<<if ndef $garrison.reactor>>
		<<set $garrison.reactor = 0>>
	<</if>>
	<<if ndef $garrison.assistant>>
		<<set $garrison.assistant = 0>>
	<</if>>
	<<if ndef $garrison.waterway>>
		<<set $garrison.waterway = 0>>
	<</if>>
	<<if ndef $garrison.reactorTime>>
		<<set $garrison.reactorTime = 0>>
	<</if>>
	<<if ndef $garrison.assistantTime>>
		<<set $garrison.assistantTime = 0>>
	<</if>>
	<<if ndef $garrison.waterwayTime>>
		<<set $garrison.waterwayTime = 0>>
	<</if>>
<</if>>
<<if ndef $rebellionsCount>>
	<<set $rebellionsCount = 0>>
<</if>>
<<if ndef $PCrebWon>>
	<<set $PCrebWon = 0>>
<</if>>
<<if ndef $PCrebLoss>>
	<<set $PCrebLoss = 0>>
<</if>>

/* armed forces stats */
<<if ndef $difficulty>>
	<<set $difficulty = 1>>
<</if>>
<<if ndef $targetUnit>>
	<<set $targetUnit = 0>>
<</if>>
<<if ndef $targetIndex>>
	<<set $targetIndex = 0>>
<</if>>
<<if ndef $secBotsCost>>
	<<set $secBotsCost = 500>>
<</if>>
<<if ndef $secBotsUpgradeCost>>
	<<set $secBotsUpgradeCost = 250>>
<</if>>
<<if ndef $equipUpgradeCost>>
	<<set $equipUpgradeCost = 250>>
<</if>>
<<if ndef $maxTroops>>
	<<set $maxTroops = 30>>
<</if>>
<<if ndef $militiaFounded>>
	<<set $militiaFounded = 0>>
<</if>>
<<if ndef $militiaTotalManpower>>
	<<set $militiaTotalManpower = 0>>
<</if>>
<<if ndef $militiaFreeManpower>>
	<<set $militiaFreeManpower = 0>>
<</if>>
<<if ndef $militiaEmployedManpower>>
	<<set $militiaEmployedManpower = 0>>
<</if>>
<<if ndef $militiaTotalCasualties>>
	<<set $militiaTotalCasualties = 0>>
<</if>>
<<if ndef $slavesOfficers>>
	<<set $slavesOfficers = 0>>
<</if>>
<<if ndef $slavesEmployedManpower>>
	<<set $slavesEmployedManpower = 0>>
<</if>>
<<if ndef $slavesTotalCasualties>>
	<<set $slavesTotalCasualties = 0>>
<</if>>
<<if ndef $slavesMaxTroops>>
	<<set $slavesMaxTroops = 30>>
<</if>>
<<if ndef $mercTotalManpower>>
	<<set $mercTotalManpower = 0>>
<</if>>
<<if ndef $mercFreeManpower>>
	<<set $mercFreeManpower = 0>>
<</if>>
<<if ndef mercEmployedManpower>>
	<<set $mercEmployedManpower = 0>>
<</if>>
<<if ndef $mercTotalCasualties>>
	<<set $mercTotalCasualties = 0>>
<</if>>
<<if ndef $militiaLoyalty>>
	<<set $militiaLoyalty = 0>>
<</if>>
<<if ndef $slaveArmyLoyalty>>
	<<set $slaveArmyLoyalty = 0>>
<</if>>
<<if ndef $mercLoyalty>>
	<<set $mercLoyalty = 0>>
<</if>>
<<if ndef $soldierUpkeep>>
	<<set $soldierUpkeep = 10>>
<</if>>
<<if ndef $createdSlavesUnits>>
	<<set $createdSlavesUnits = 0>>
<</if>>
<<if ndef $createdMilitiaUnits>>
	<<set $createdMilitiaUnits = 0>>
<</if>>
<<if ndef $createdMercUnits>>
	<<set $createdMercUnits = 0>>
<</if>>

/* battle relevant vars */
<<if ndef $troopCount>>
	<<set $troopCount = 0>>
<</if>>
<<if ndef $slaveVictories>>
	<<set $slaveVictories = []>>
<</if>>
<<if ndef $slaveIncreasedPrestige>>
	<<set $slaveIncreasedPrestige = 0>>
<</if>>
<<if ndef $totalKills>>
	<<set $totalKills = 0>>
<</if>>
<<if ndef $battlesCount>>
	<<set $battlesCount = 0>>
<</if>>
<<if ndef $majorBattlesCount>>
	<<set $majorBattlesCount = 0>>
<</if>>
<<if ndef $chosenTactic>>
	<<set $chosenTactic = "none">>
<</if>>
<<if ndef $leadingTroops>>
	<<set $leadingTroops = "none">>
<</if>>
<<if ndef $attackTroops>>
	<<set $attackTroops = 0>>
<</if>>
<<if ndef $attackEquip>>
	<<set $attackEquip = 0>>
<</if>>
<<if ndef $deployableUnits>>
	<<set $deployableUnits = 0>>
<</if>>
<<if ndef $deployedUnits>>
	<<set $deployedUnits = 0>>
<</if>>
<<if ndef $deployingBots>>
	<<set $deployingBots = 0>>
<</if>>
<<if ndef $deployingMilitia>>
	<<set $deployingMilitia = 0>>
<</if>>
<<if ndef $deployingSlaves>>
	<<set $deployingSlaves = 0>>
<</if>>
<<if ndef $deployingMercs>>
	<<set $deployingMercs = 0>>
<</if>>
<<if ndef $battleTerrain>>
	<<set $battleTerrain = "none">>
<</if>>
<<if ndef $maxTurns>>
	<<set $maxTurns = 10>>
<</if>>
<<if ndef $battleResult>>
	<<set $battleResult = 4>>				/* sets $battleResult value outside accepted range (-3,3) to avoid evaluation problems */
<</if>>
<<if ndef $losses>>
	<<set $losses = 0>>
<</if>>
<<if ndef $enemyLosses>>
	<<set $enemyLosses = 0>>
<</if>>
<<if ndef $battleTurns>>
	<<set $battleTurns = 0>>
<</if>>
<<if ndef $bribeCost>>
	<<set $bribeCost = 0>>
<</if>>
<<if ndef $tacticsSuccessful>>
	<<set $tacticsSuccessful = 0>>
<</if>>
<<if ndef $leaderWounded>>
	<<set $leaderWounded = 0>>				/* 0=no wound, 1=mute, 2=blind, 3=amputee, 4=health */
<</if>>
<<if ndef $gainedCombat>>
	<<set $gainedCombat = 0>>
<</if>>
<<if ndef $gainedWarfare>>
	<<set $gainedWarfare = 0>>
<</if>>
<<if ndef $PCWounded>>
	<<set $PCWounded = 0>>
<</if>>
<<if ndef $PCWoundCooldown>>
	<<set $PCWoundCooldown = 0>>
<</if>>
<<if ndef $expectedEquip>>
	<<set $expectedEquip = 0>>
<</if>>
<<if ndef $estimatedMen>>
	<<set $estimatedMen = 0>>
<</if>>
<<if ndef $SFIntervention>>
	<<set $SFIntervention = 0>>
<</if>>
<<if ndef $carriableSoldiers>>
	<<set $carriableSoldiers = 0>>
<</if>>
<<if ndef $SFatk>>
	<<set $SFatk = 0>>
<</if>>
<<if ndef $SFdef>>
	<<set $SFdef = 0>>
<</if>>
<<if ndef $SFhp>>
	<<set $SFhp = 0>>
<</if>>
<<if ndef $rebellingID>>
	<<set $rebellingID = []>>
<</if>>
<<if ndef $saveValid>>
	<<set $saveValid = 0>>
<</if>>
<<if ndef $lastSelection>>
	<<set $lastSelection = []>>
<</if>>

/* statistics */
<<if ndef $baseBribePerAttacker>>
	<<set $baseBribePerAttacker = 5>>
<</if>>
<<if ndef $equipMod>>
	<<set $equipMod = 0.15>>
<</if>>
<<if ndef $secBotsBaseAttack>>
	<<set $secBotsBaseAttack = 7 + $droneUpgrades.attack>>
<</if>>
<<if ndef $secBotsBaseDefense>>
	<<set $secBotsBaseDefense = 3 + $droneUpgrades.defense>>
<</if>>
<<if ndef $secBotsMorale>>
	<<set $secBotsMorale = 200>>
<</if>>
<<if ndef $secBotsBaseHp>>
	<<set $secBotsBaseHp = 3 + $droneUpgrades.hp>>
<</if>>
<<if ndef $militiaBaseAttack>>
	<<set $militiaBaseAttack = 7 + $humanUpgrade.attack>>
<</if>>
<<if ndef $militiaBaseDefense>>
	<<set $militiaBaseDefense = 5 + $humanUpgrade.defense>>
<</if>>
<<if ndef $militiaBaseMorale>>
	<<set $militiaBaseMorale = 140 + $humanUpgrade.morale>>
<</if>>
<<if ndef $militiaBaseHp>>
	<<set $militiaBaseHp = 3 + $humanUpgrade.hp>>
<</if>>
<<if ndef $slaveBaseAttack>>
	<<set $slaveBaseAttack = 8 + $humanUpgrade.attack>>
<</if>>
<<if ndef $slaveBaseDefense>>
	<<set $slaveBaseDefense = 3 + $humanUpgrade.defense>>
<</if>>
<<if ndef $slaveBaseMorale>>
	<<set $slaveBaseMorale = 110 + $humanUpgrade.morale>>
<</if>>
<<if ndef $slaveBaseHp>>
	<<set $slaveBaseHp = 3 + $humanUpgrade.hp>>
<</if>>
<<if ndef $mercBaseAttack>>
	<<set $mercBaseAttack = 8 + $humanUpgrade.attack>>
<</if>>
<<if ndef $mercBaseDefense>>
	<<set $mercBaseDefense = 4 + $humanUpgrade.defense>>
<</if>>
<<if ndef $mercBaseMorale>>
	<<set $mercBaseMorale = 125 + $humanUpgrade.morale>>
<</if>>
<<if ndef $mercBaseHp>>
	<<set $mercBaseHp = 4 + $humanUpgrade.hp>>
<</if>>
<<if ndef $SFBaseAttack>>
	<<set $SFBaseAttack = 8 + $humanUpgrade.attack>>
<</if>>
<<if ndef $SFBaseDefense>>
	<<set $SFBaseDefense = 4 + $humanUpgrade.defense>>
<</if>>
<<if ndef $SFBaseMorale>>
	<<set $SFBaseMorale = 140 + $humanUpgrade.morale>>
<</if>>
<<if ndef $SFBaseHp>>
	<<set $SFBaseHp = 4 + $humanUpgrade.hp>>
<</if>>

<<if ndef $raBaseAttack>>
	<<set $raBaseAttack = 7>>
<</if>>
<<if ndef $raBaseDefense>>
	<<set $raBaseDefense = 2>>
<</if>>
<<if ndef $raBaseMorale>>
	<<set $raBaseMorale = 100>>
<</if>>
<<if ndef $raBaseHp>>
	<<set $raBaseHp = 2>>
<</if>>
<<if ndef $fcBaseAttack>>
	<<set $fcBaseAttack = 6>>
<</if>>
<<if ndef $fcBaseDefense>>
	<<set $fcBaseDefense = 4>>
<</if>>
<<if ndef $fcBaseMorale>>
	<<set $fcBaseMorale = 130>>
<</if>>
<<if ndef $fcBaseHp>>
	<<set $fcBaseHp = 3>>
<</if>>
<<if ndef $owBaseAttack>>
	<<set $owBaseAttack = 8>>
<</if>>
<<if ndef $owBaseDefense>>
	<<set $owBaseDefense = 4>>
<</if>>
<<if ndef $owBaseMorale>>
	<<set $owBaseMorale = 110>>
<</if>>
<<if ndef $owBaseHp>>
	<<set $owBaseHp = 2>>
<</if>>
<<if ndef $ffBaseAttack>>
	<<set $ffBaseAttack = 9>>
<</if>>
<<if ndef $ffBaseDefense>>
	<<set $ffBaseDefense = 2>>
<</if>>
<<if ndef $ffBaseMorale>>
	<<set $ffBaseMorale = 160>>
<</if>>
<<if ndef $ffBaseHp>>
	<<set $ffBaseHp = 2>>
<</if>>

/* units */
<<if ndef $secBots>>
	<<if $arcologyUpgrade.drones == 0>>
		<<set $secBots = {
		active: 0,
		ID: -1,
		isDeployed: 0,
		troops: 0,
		maxTroops: 0,
		equip: 0} >>
	<<elseif $arcologyUpgrade.drones == 1>>
		<<set $secBots = {
		active: 1,
		ID: -1,
		isDeployed: 0,
		troops: 30,
		maxTroops: 30,
		equip: 0} >>
	<</if>>
<</if>>

<<if ndef $militiaUnits>>
	<<set $militiaUnits = []>>
<</if>>
<<if ndef $slaveUnits>>
	<<set $slaveUnits = []>>
<</if>>
<<if ndef $mercUnits>>
	<<set $mercUnits = []>>
<</if>>

/* SFanon additions */
<<if ndef $SFSupportLevel>>
	<<set $SFSupportLevel = 0>>
<</if>>
<<if ndef $SFSupportUpkeep>>
	<<set $SFSupportUpkeep = 0>>
<</if>>
<<if ndef $secUpgrades.coldstorage>>
	<<set $secUpgrades.coldstorage = 0>>
<</if>>
<<if ndef $SFGear>>
	<<set $SFGear = 0>>
<</if>>
<<if ndef $SavedLeader>>
	<<set $SavedLeader = $leadingTroops>>
<</if>>
<<if ndef $SavedSFI>>
	<<set $SavedSFI = $SFIntervention>>
<</if>>

/* recalculation widgets */
<<fixBrokenUnits>>
<<fixBrokenStats>>
<<fixBrokenFS>>
<<recalcSecRestPoint>>
<<recalcCrimeCap>>
<<recalcReqMenials>>
<<recalcManpower>>
<<recalcEdictsUpkeep>>
<<recalcBaseStats>>
<<recalcUnits>>

<<set $wasToggledBefore = 1>>

<br>Missing Security Expansion variables set. All done!


code:
:: attackGenerator [nobr]

/* _attackChance value is the chance out of 100 of an attack happening this week */
/* attacks are deactivated if battles are not enabled, the arcology is in the middle of the ocean, security drones are not around yet, there is not a rebellion this week or the last attack/rebellion happened within 3 weeks */
<<if $battlesEnabled == 0 || ($terrain == "oceanic" && $battlesEnabledOceanic == 0) || $arcologyUpgrade.drones != 1 || $lastAttackWeeks <= 3 || $citizenRebellion == 1 || $slaveRebellion == 1 || $lastRebellionWeeks <= 3>>
	<<set _attackChance = 0>>
<<else>>
	<<if $week < 30>>
		<<set _attackChance = 5>>
	<<elseif $week < 60>>
		<<set _attackChance = 8>>
	<<elseif $week < 90>>
		<<set _attackChance = 12>>
	<<elseif $week < 120>>
		<<set _attackChance = 16>>
	<<else>>
		<<set _attackChance = 20>>
	<</if>>
	<<if $hasFoughtOnce == 1>>
		<<set _attackChance = 25>>
	<</if>>
	<<if $lastAttackWeeks >= 10>>
		<<set _attackChance += 5>>
	<</if>>
<</if>>
/* battle frequency */
<<set _attackChance *= $battleFrequency>>

<<if $forceBattle == 1 && $forceRebellion != 1 && $foughtThisWeek == 0>>
	<<set _attackChance = 100>>
<</if>>

/* Rolls to see if attack happens this week */
/* raiders are attracted by low security */
/* the old world by "degenerate" future societies */
/* free Cities by high prosperity */
/* freedom fighters by high slave/citizen ratio */
<<if random(1,100) <= _attackChance>>
	<<set $attackThisWeek = 1>>
	<<set $lastAttackWeeks = 0>>
	<<set $leadingTroops = "assistant">>
	<<set $deployableUnits = 2 * $readiness>>
	<<set $chosenTactic = either("Bait and Bleed", "Blitzkrieg", "Choke Points", "Defense In Depth", "Guerrilla", "Human Wave", "Interior Lines", "Pincer Maneuver")>>
	/* _type is the chance out of 100 of an attack of that type happening */
	<<set _raider = 25>>
	<<set _oldWorld = 25>>
	<<set _freeCity = 25>>
	<<set _free = 25>>
	/* old world */
	<<if $arcologies[0].FSRomanRevivalist != "unset" || $arcologies[0].FSEdoRevivalist != "unset" || $arcologies[0].FSArabianRevivalist != "unset" || $arcologies[0].FSChineseRevivalist != "unset" || $arcologies[0].FSEgyptianRevivalist != "unset" || $arcologies[0].FSAztecRevivalist != "unset" || $arcologies[0].FSRepopulationFocus != "unset" || $arcologies[0].FSGenderRadicalist != "unset" || $arcologies[0].FSPastoralist != "unset" || $arcologies[0].FSChattelReligionist != "unset">>
		<<set _oldWorld += 15>>
		<<set _raider -= 5>>
		<<set _freeCity -= 5>>
		<<set _free -= 5>>
	<<elseif ($arcologies[0].FSRomanRevivalist != "unset" || $arcologies[0].FSEdoRevivalist != "unset" || $arcologies[0].FSArabianRevivalist != "unset" || $arcologies[0].FSChineseRevivalist != "unset" || $arcologies[0].FSEgyptianRevivalist != "unset" || $arcologies[0].FSAztecRevivalist != "unset") && ($arcologies[0].FSRepopulationFocus != "unset" || $arcologies[0].FSGenderRadicalist != "unset" || $arcologies[0].FSPastoralist != "unset" || $arcologies[0].FSChattelReligionist != "unset")>>
		<<set _oldWorld += 24>>
		<<set _raider -= 8>>
		<<set _freeCity -= 8>>
		<<set _free -= 8>>
	<</if>>
	/* freedom fighter */
	<<if $ASlaves > $ACitizens * 2>>
		<<set _oldWorld -= 8>>
		<<set _raider -= 8>>
		<<set _freeCity -= 8>>
		<<set _free += 24>>
	<<elseif $ASlaves > $ACitizens * 1.2 || $arcologies[0].FSDegradationist != "unset">>
		<<set _oldWorld -= 5>>
		<<set _raider -= 5>>
		<<set _freeCity -= 5>>
		<<set _free += 15>>
	<</if>>
	/* free cities */
	<<if $arcologies[0].prosperity >= 10 && $arcologies[0].prosperity < 20>>
		<<set _oldWorld -= 5>>
		<<set _raider -= 5>>
		<<set _freeCity += 15>>
		<<set _free -= 5>>
	<<elseif $arcologies[0].prosperity >= 20>>
		<<set _oldWorld -= 8>>
		<<set _raider -= 8>>
		<<set _freeCity += 24>>
		<<set _free -= 8>>
	<</if>>
	/* raiders */
	<<if $security <= 50>>
		<<set _oldWorld -= 5>>
		<<set _raider += 15>>
		<<set _freeCity -= 5>>
		<<set _free -= 5>>
	<<elseif $security <= 25>>
		<<set _oldWorld -= 8>>
		<<set _raider += 24>>
		<<set _freeCity -= 8>>
		<<set _free -= 8>>
	<</if>>

	/* makes the actual roll */
	<<set _roll = random(1,100)>>
	<<if _roll <= _raider>>
		<<set $attackType = "raiders">>
	<<elseif _roll <= _raider + _oldWorld>>
		<<set $attackType = "old world">>
	<<elseif _roll <= _raider + _oldWorld + _freeCity>>
		<<set $attackType = "free city">>
	<<elseif _roll <= _raider + _oldWorld + _freeCity + _free>>
		<<set $attackType = "freedom fighters">>
	<</if>>
<<else>>
	<<set $lastAttackWeeks++>>
<</if>>

/* if an attack happens */
<<if $attackThisWeek == 1>>

	/* terrain */
	<<if $terrain == "urban">>
		<<set $battleTerrain = either("outskirts", "urban", "wasteland")>>
	<<elseif $terrain == "rural">>
		<<set $battleTerrain = either("hills", "outskirts", "rural", "wasteland")>>
	<<elseif $terrain == "ravine">>
		<<set $battleTerrain = either("hills", "mountains", "outskirts", "wasteland")>>
	<<elseif $terrain == "marine">>
		<<set $battleTerrain = either("coast", "hills", "outskirts", "wasteland")>>
	<<elseif $terrain == "oceanic">>
		<<set $battleTerrain = either("coast", "hills", "outskirts", "wasteland")>>
	<<else>>
		<<set $battleTerrain = "error">>
	<</if>>

	<<set _L=0>>
	<<if $attackType == "raiders">> <<set $attackTroops = random(40,80),_L=1>>
	<<elseif $attackType == "free city">> <<set $attackTroops = random(20,40)>>
	<<elseif $attackType == "old world">> <<set $attackTroops = random(25,50)>>
	<<elseif $attackType == "freedom fighters">> <<set $attackTroops = random(30,60)>>
	<</if>>
	<<if $week < 30>>
		/*<<set $attackTroops *= Math.trunc(random( (1*(1.01+($week/100))), (2*(1.01+($week/100))) ))>>*/ <<set $attackTroops *= random(1,2)>>
	<<elseif $week < 60>>
		/*<<set $attackTroops *= Math.trunc(random( (1*(1.01+($week/200))), (3*(1.01+($week/200))) ))>>*/ <<set $attackTroops *= random(1,3)>>
	<<elseif $week < 90>>
		/*<<set $attackTroops *= Math.trunc(random( (2*(1.01+($week/300))), (3*(1.01+($week/300))) ))>>*/ <<set $attackTroops *= random(2,3)>>
	<<elseif $week < 120>>
		/*<<set $attackTroops *= Math.trunc(random( (2*(1.01+($week/400))), (4*(1.01+($week/400))) ))>>*/ <<set $attackTroops *= random(2,4)>>
	<<else>>
		<<set $attackTroops *= random(3,5)>>
	<</if>>
	<<if $week < 60>>
		<<set $attackEquip = random(0,1)>>
	<<elseif $week < 90>>
		<<set $attackEquip = random(0,3-_L)>> /*"raiders" <<set $attackEquip = random(0,2)>>*/
	<<elseif $week < 120>>
		<<set $attackEquip = random(1-_L,3)>> /*"raiders" <<set $attackEquip = random(0,3)>>*/
	<<else>>
		<<set $attackEquip = random(2-_L,4-_L)>> /*"raiders" <<set $attackEquip = random(1,3)>>*/
	<</if>>

	/* major battles have a 50% chance of firing after week 120 */
	<<if $majorBattlesEnabled == 1>>
		<<if ($week >= 120 && $attackType != "none") || ($forceMajorBattle == 1 && $foughtThisWeek == 0)>>
			<<if random(1,100) >= 50 || $forceMajorBattle == 1>>
				<<set $majorBattle = 1>>
				<<if $SF.Toggle && $SF.Active >= 1>>
					<<set $attackTroops = Math.trunc($attackTroops * random(4,6) * $majorBattleMult)>>
					<<set $attackEquip = either(3,4)>>
				<<else>>
					<<set $attackTroops = Math.trunc($attackTroops * random(2,3) * $majorBattleMult)>>
					<<set $attackEquip = either(2,3,4)>>
				<</if>>
			<</if>>
		<</if>>
	<</if>>

	<<set $estimatedMen = Math.round($attackTroops * (1 + either(-1,1) * (random(3,4) - $recon) * 0.1))>>
	<<if $recon == 3>>
		<<set $expectedEquip = $attackEquip + random(-1,1)>>
	<<elseif $recon == 2>>
		<<set $expectedEquip = $attackEquip + random(-1,2)>>
	<<elseif $recon == 1>>
		<<set $expectedEquip = $attackEquip + random(-2,2)>>
	<<else>>
		<<set $expectedEquip = $attackEquip + random(-2,3)>>
	<</if>>
<</if>>


code:
:: attackHandler [nobr]

<<set $nextButton = " ", $nextLink = "attackReport", $showEncyclopedia = 0>>

/* updates deployed flags */
<<for _i = 0; _i < $militiaUnits.length; _i++>>
	<<if $militiaUnits[_i].isDeployed == 1>>
		<<set $deployingMilitia = 1>>
		<<break>>
	<</if>>
<</for>>
<<for _i = 0; _i < $slaveUnits.length; _i++>>
	<<if $slaveUnits[_i].isDeployed == 1>>
		<<set $deployingSlaves = 1>>
		<<break>>
	<</if>>
<</for>>
<<for _i = 0; _i < $mercUnits.length; _i++>>
	<<if $mercUnits[_i].isDeployed == 1>>
		<<set $deployingMercs = 1>>
		<<break>>
	<</if>>
<</for>>

<<if $battleResult == 1 || $battleResult == -1>>	/* bribery/surrender check */
	<<if $showBattleStatistics == 1>>
		bribery chosen
	<<else>>
		Surrender chosen
	<</if>>
	<<if $battleResult == 1>>
		<<if $cash >= $bribeCost>>						/* if there's enough cash there's a 10% chance bribery fails. If there isn't there's instead a 50% chance it fails */
			<<if $attackType == "freedom fighters">>
				<<if random(1,100) <= 50>>
					<<set $battleResult = 0>>
				<</if>>
			<<elseif random(1,100) <= 10>>
				<<set $battleResult = 0>>
			<</if>>
		<<else>>
			<<if random(1,100) <= 50>>
				<<set $battleResult = 0>>
			<</if>>
		<</if>>
		<<if $showBattleStatistics == 1>>
			<<if $battleResult == 0>>

Bribery failed!
			<<else>>

Bribery Successful!
			<</if>>

			<<link "proceed">>
				<<goto "attackReport">>
			<</link>>
		<<else>>
			<<goto "attackReport">>
		<</if>>
	<<else>>
		<<goto "attackReport">>
	<</if>>

<<else>>

	/*Init*/
	<<if $majorBattle == 0>>
		<<set _turns = $maxTurns>>
	<</if>>
	<<set _turn = 0>>
	<<set _attack = 0>>
	<<set _defense = 0>>
	<<set _morale = 0>>
	<<set _hp = 0>>
	<<set _baseHp = 0>>
	<<set _enemyAttack = 0>>
	<<set _enemyDefense = 0>>
	<<set _enemyMorale = 0>>
	<<set _enemyHp = 0>>
	<<set _enemyBaseHp = 0>>
	<<set _woundChance = 5>>								/* leader has a base chance of 5% to get wounded */
	<<set _tacChance = 0.5>>								/* by default tactics have a 50% chance of succeeding */
	<<set _atkMod = 1>>
	<<set _defMod = 1>>
	<<set _militiaMod = 1>>
	<<set _slaveMod = 1>>
	<<set _mercMod = 1>>
	<<set _enemyMod = 1>>
	<<set _SFMod = 1>>
	<<set _expBonus = 0>>
	<<set _loyaltyBonus = 0>>
	<<set _armyMod = 0>>

	/* major battle */
	<<if $majorBattle == 1>>
		<<set _militiaMod = 1.5>>
		<<set _slaveMod = 1.5>>
		<<set _mercMod = 1.5>>
		<<set _enemyMod = 1.5>>
		<<set _SFMod = 1.5>>
		<<set _turns = $maxTurns * 2>>
		<<if $SF.Toggle && $SF.Active >= 1>>
			<<if $SF.Squad.Firebase >= 7>>
				<<set _atkMod += ($SF.Squad.Firebase - 6) * 0.05>>
			<</if>>
			<<if $SF.Squad.GunS >= 1>>
				<<set _defMod += $SF.Squad.GunS * 0.05>>
			<</if>>
			<<if $SF.Squad.Satellite.lv >= 5 && $SF.Squad.Satellite.InOrbit > 0>>
				<<set _atkMod += ($SF.Squad.Satellite.lv - 5) * 0.05>>
			<</if>>
			<<if $SF.Squad.GiantRobot >= 6>>
				<<set _defMod += ($SF.Squad.GiantRobot - 5) * 0.05>>
			<</if>>
			<<if $SF.Squad.MissileSilo >= 1>>
				<<set _atkMod += $SF.Squad.MissileSilo * 0.05>>
			<</if>>
		<</if>>
	<</if>>

	/* Leaders */
	<<if $leadingTroops == "PC">>
		<<if $authority <= 2500 && $authority > 1000>>
			<<set _slaveMod -= 0.10>>
		<<elseif $authority <= 1000>>
			<<set _slaveMod -= 0.25>>
		<<elseif $authority >= 5000 && $authority < 15000>>
			<<set _slaveMod += 0.10>>
		<<elseif $authority >= 15000>>
			<<set _slaveMod += 0.25>>
		<</if>>
		<<if $PC.career == "escort" || $PC.career == "servant">>
			<<set _slaveMod += 0.10>>
		<<elseif $PC.career == "slaver">>
			<<set _slaveMod -= 0.10>>
		<</if>>
		<<if $rep <= 2500 && $rep > 1000>>
			<<set _militiaMod -= 0.10>>
		<<elseif $rep <= 1000>>
			<<set _militiaMod -= 0.25>>
		<<elseif $rep >= 5000 && $rep < 15000>>
			<<set _militiaMod += 0.10>>
		<<elseif $rep >= 15000>>
			<<set _militiaMod += 0.25>>
		<</if>>
		<<if $PC.career == "celebrity" || $PC.career == "capitalist">>
			<<set _militiaMod += 0.10>>
		<<elseif $PC.career == "gang" || $PC.career == "escort">>
			<<set _militiaMod -= 0.10>>
		<</if>>
		<<if $PC.career == "mercenary" || $PC.warfare > 75>>
			<<set _mercMod += 0.10>>
			<<set _SFMod += 0.10>>
		<<elseif $PC.career == "wealth" || $PC.career == "servant">>
			<<set _mercMod -= 0.10>>
			<<set _SFMod -= 0.10>>
		<</if>>
		<<if $rep >= 15000>>
			<<set _enemyMod -= 0.10>>
		<</if>>
		<<if $PC.warfare <= 25 && $PC.warfare > 10>>
			<<set _atkMod -= 0.15>>
			<<set _tacChance -= 0.15>>
		<<elseif $PC.warfare <= 10>>
			<<set _atkMod -= 0.20>>
			<<set _defMod -= 0.10>>
			<<set _tacChance -= 0.30>>
		<<elseif $PC.warfare >= 50 && $PC.warfare < 75>>
			<<set _atkMod += 0.15>>
			<<set _tacChance += 0.15>>
		<<elseif $PC.warfare >= 75>>
			<<set _atkMod += 0.20>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.30>>
		<</if>>
		/* 80% chance of increasing warfare */
		<<if $PC.warfare < 100 && random(1,100) <= 80>>
			<<set $gainedWarfare = 1>>
			<<= IncreasePCSkills('warfare',  10)>>
			<<set $PC.warfare = Math.clamp($PC.warfare,-100,100)>>
		<</if>>
			/* does the PC get wounded? */
		<<if $PC.career == "mercenary" || $PC.career == "gang">>
			<<set _woundChance -= 3>>
		<</if>>
		<<if $PC.physicalAge >= 60>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $PC.belly > 5000>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $PC.boobsBonus >= 2>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $PC.butt >= 2>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $PC.preg >= 30>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $PC.balls == 2>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $PC.ballsImplant >= 2>>
			<<set _woundChance += 1>>
		<</if>>
		<<if random(1,100) <= _woundChance>>
			<<set $leaderWounded = 1>>
			<<set _militiaMod -= 0.2>>
			<<set _slaveMod -= 0.2>>
			<<set _mercMod -= 0.2>>
			<<set _SFMod -= 0.2>>
			<<set _enemyMod += 0.2>>
			<<set $PCWounded = 1>>
		<</if>>
	<<elseif $leadingTroops == "assistant">>
		<<if $rep < 10000 && $authority < 10000>>
			<<set _militiaMod -= 0.15>>
			<<set _slaveMod -= 0.15>>
			<<set _mercMod -= 0.15>>
			<<set _SFMod -= 0.15>>
		<</if>>
		<<if $assistantPower == 0>>
			<<set _atkMod -= 0.10>>
			<<set _defMod -= 0.10>>
			<<set _tacChance -= 0.20>>
		<<elseif $assistantPower == 2>>
			<<set _atkMod += 0.15>>
			<<set _defMod += 0.15>>
			<<set _tacChance += 0.30>>
		<</if>>
	<<elseif $leadingTroops == "bodyguard">>
		<<if $Bodyguard.devotion < -20>>
			<<set _slaveMod -= 0.15>>
		<<elseif $Bodyguard.devotion > 50>>
			<<set _slaveMod += 0.15>>
		<</if>>
		<<if ($rep < 10000 && $authority < 10000) || $Bodyguard.prestige < 1>>
			<<set _militiaMod -= 0.15>>
			<<set _mercMod -= 0.15>>
			<<set _SFMod -= 0.15>>
		<<elseif $Bodyguard.prestige >= 2>>
			<<set _militiaMod += 0.10>>
			<<set _mercMod += 0.10>>
			<<set _SFMod += 0.10>>
		<</if>>
		<<if (setup.bodyguardCareers.includes($Bodyguard.career) || setup.HGCareers.includes($Bodyguard.career) || setup.secExCombatPrestige.includes($Bodyguard.prestigeDesc)) && $Bodyguard.intelligence+$Bodyguard.intelligenceImplant > 95>>
			<<set _atkMod += 0.25>>
			<<set _defMod += 0.25>>
			<<set _tacChance += 0.50>>
		<<elseif $Bodyguard.intelligence+$Bodyguard.intelligenceImplant > 95>>
			<<set _atkMod += 0.20>>
			<<set _defMod += 0.15>>
			<<set _tacChance += 0.35>>
		<<elseif (setup.bodyguardCareers.includes($Bodyguard.career) || setup.HGCareers.includes($Bodyguard.career) || setup.secExCombatPrestige.includes($Bodyguard.prestigeDesc)) && $Bodyguard.intelligence+$Bodyguard.intelligenceImplant > 50>>
			<<set _atkMod += 0.15>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.25>>
		<<elseif $Bodyguard.intelligence+$Bodyguard.intelligenceImplant > 50>>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.20>>
		<<elseif (setup.bodyguardCareers.includes($Bodyguard.career) || setup.HGCareers.includes($Bodyguard.career) || setup.secExCombatPrestige.includes($Bodyguard.prestigeDesc)) && $Bodyguard.intelligence+$Bodyguard.intelligenceImplant > 15>>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.05>>
			<<set _tacChance += 0.15>>
		<<elseif $Bodyguard.intelligence+$Bodyguard.intelligenceImplant > 15>>
			<<set _atkMod += 0.5>>
			<<set _defMod += 0.05>>
			<<set _tacChance += 0.10>>
		<<elseif !(setup.bodyguardCareers.includes($Bodyguard.career) && setup.HGCareers.includes($Bodyguard.career) && setup.secExCombatPrestige.includes($Bodyguard.prestigeDesc)) || $Bodyguard.intelligence+$Bodyguard.intelligenceImplant < -50>>
			<<set _atkMod -= 0.15>>
			<<set _defMod -= 0.15>>
			<<set _tacChance -= 0.30>>
		<<elseif $Bodyguard.intelligence+$Bodyguard.intelligenceImplant < -50>>
			<<set _atkMod -= 0.15>>
			<<set _defMod -= 0.10>>
			<<set _tacChance -= 0.25>>
		<<elseif !(setup.bodyguardCareers.includes($Bodyguard.career) && setup.HGCareers.includes($Bodyguard.career) && setup.secExCombatPrestige.includes($Bodyguard.prestigeDesc)) || $Bodyguard.intelligence+$Bodyguard.intelligenceImplant < -15>>
			<<set _atkMod -= 0.10>>
			<<set _defMod -= 0.10>>
			<<set _tacChance -= 0.20>>
		<<elseif $Bodyguard.intelligence+$Bodyguard.intelligenceImplant < -15>>
			<<set _atkMod -= 0.10>>
			<<set _defMod -= 0.05>>
			<<set _tacChance -= 0.15>>
		<</if>>
			/* does she get wounded? */
		<<if $Bodyguard.skill.combat == 1>>
			<<set _woundChance -= 2>>
		<</if>>
		<<if $Bodyguard.amp >= -4>>
			<<set _woundChance -= 1>>
		<</if>>
		<<if $Bodyguard.health >= 50>>
			<<set _woundChance -= 1>>
		<</if>>
		<<if $Bodyguard.weight > 130>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.muscles < -30>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.eyes <= -2>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.heels == 1>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.boobs >= 1400>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.butt >= 6>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.belly >= 10000>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.dick >= 8>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.balls >= 8>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.intelligence+$Bodyguard.intelligenceImplant < -95>>
			<<set _woundChance += 1>>
		<</if>>
		<<if random(1,100) <= _woundChance>>
			<<set $leaderWounded = 1>>
			<<set _militiaMod -= 0.2>>
			<<set _slaveMod -= 0.2>>
			<<set _mercMod -= 0.2>>
			<<set _SFMod -= 0.2>>
			<<set _enemyMod += 0.2>>
			<<set $woundType = random(1,10)>>
			<<set _i = $slaveIndices[$Bodyguard.ID]>>
			<<if $woundType == 1>>
				<<set $slaves[_i].voice = 0>>
				<<set $slaves[_i].health -= 60>>
			<<elseif $woundType == 2>>
				<<set $slaves[_i].eyes = -2>>
				<<set $slaves[_i].health -= 30>>
			<<elseif $woundType == 3>>
				<<set $slaves[_i].amp = 1>>
				<<set $slaves[_i].health = -80>>
			<<elseif $woundType >= 4>>
				<<if $slaves[_i].health >= -60>>
					<<set $slaves[_i].health -= 30>>
				<<else>>
					<<set $slaves[_i].health = -90>>
				<</if>>
			<</if>>
		<</if>>
		/* 60% chance of getting combat skill if not already have it */
		<<if $Bodyguard.skill.combat == 0 && random(1,100) <= 60>>
			<<set $gainedCombat = 1>>
			<<set _i = $slaveIndices[$Bodyguard.ID]>>
			<<set $slaves[_i].skill.combat = 1>>
		<</if>>
	<<elseif $leadingTroops == "headGirl">>
		<<if $HeadGirl.devotion < -20>>
			<<set _slaveMod -= 0.15>>
		<<elseif $HeadGirl.devotion > 51>>
			<<set _slaveMod += 0.15>>
		<</if>>
		<<if ($rep < 10000 && $authority < 10000) || $HeadGirl.prestige < 1>>
			<<set _militiaMod -= 0.15>>
			<<set _mercMod -= 0.15>>
			<<set _SFMod -= 0.15>>
		<<elseif $HeadGirl.prestige >= 2>>
			<<set _militiaMod += 0.10>>
			<<set _mercMod += 0.10>>
			<<set _SFMod += 0.10>>
		<</if>>
		<<if (setup.bodyguardCareers.includes($HeadGirl.career) || setup.HGCareers.includes($HeadGirl.career) || setup.secExCombatPrestige.includes($HeadGirl.prestigeDesc)) && $HeadGirl.intelligence+$HeadGirl.intelligenceImplant > 95>>
			<<set _atkMod += 0.25>>
			<<set _defMod += 0.25>>
			<<set _tacChance += 0.50>>
		<<elseif $HeadGirl.intelligence+$HeadGirl.intelligenceImplant > 95>>
			<<set _atkMod += 0.20>>
			<<set _defMod += 0.15>>
			<<set _tacChance += 0.35>>
		<<elseif (setup.bodyguardCareers.includes($HeadGirl.career) || setup.HGCareers.includes($HeadGirl.career) || setup.secExCombatPrestige.includes($HeadGirl.prestigeDesc)) && $HeadGirl.intelligence+$HeadGirl.intelligenceImplant > 50>>
			<<set _atkMod += 0.15>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.25>>
		<<elseif $HeadGirl.intelligence+$HeadGirl.intelligenceImplant > 50>>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.20>>
		<<elseif (setup.bodyguardCareers.includes($HeadGirl.career) || setup.HGCareers.includes($HeadGirl.career) || setup.secExCombatPrestige.includes($HeadGirl.prestigeDesc)) && $HeadGirl.intelligence+$HeadGirl.intelligenceImplant > 15>>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.05>>
			<<set _tacChance += 0.15>>
		<<elseif $HeadGirl.intelligence+$HeadGirl.intelligenceImplant > 15>>
			<<set _atkMod += 0.05>>
			<<set _defMod += 0.05>>
			<<set _tacChance += 0.10>>
		<<elseif !(setup.bodyguardCareers.includes($HeadGirl.career) && setup.HGCareers.includes($HeadGirl.career) && setup.secExCombatPrestige.includes($HeadGirl.prestigeDesc)) || $HeadGirl.intelligence+$HeadGirl.intelligenceImplant < -50>>
			<<set _atkMod -= 0.15>>
			<<set _defMod -= 0.15>>
			<<set _tacChance -= 0.30>>
		<<elseif $HeadGirl.intelligence+$HeadGirl.intelligenceImplant < -50>>
			<<set _atkMod -= 0.15>>
			<<set _defMod -= 0.10>>
			<<set _tacChance -= 0.25>>
		<<elseif !(setup.bodyguardCareers.includes($HeadGirl.career) && setup.HGCareers.includes($HeadGirl.career) && setup.secExCombatPrestige.includes($HeadGirl.prestigeDesc)) || $HeadGirl.intelligence+$HeadGirl.intelligenceImplant < -15>>
			<<set _atkMod -= 0.10>>
			<<set _defMod -= 0.10>>
			<<set _tacChance -= 0.20>>
		<<elseif $HeadGirl.intelligence+$HeadGirl.intelligenceImplant < -15>>
			<<set _atkMod -= 0.10>>
			<<set _defMod -= 0.05>>
			<<set _tacChance -= 0.15>>
		<</if>>
		/* does she get wounded? */
		<<if $HeadGirl.skill.combat == 1>>
			<<set _woundChance -= 3>>
		<</if>>
		<<if $HeadGirl.amp >= -4>>
			<<set _woundChance -= 1>>
		<</if>>
		<<if $HeadGirl.health >= 50>>
			<<set _woundChance -= 2>>
		<</if>>
		<<if $HeadGirl.weight > 130>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $HeadGirl.muscles < -30>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $HeadGirl.eyes <= -2>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $HeadGirl.heels == 1>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $HeadGirl.boobs >= 1400>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $HeadGirl.butt >= 6>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $HeadGirl.belly >= 10000>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $HeadGirl.dick >= 8>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $HeadGirl.balls >= 8>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $HeadGirl.intelligence+$HeadGirl.intelligenceImplant < -95>>
			<<set _woundChance += 1>>
		<</if>>
		<<if random(1,100) <= _woundChance>>
			<<set $leaderWounded = 1>>
			<<set _militiaMod -= 0.2>>
			<<set _slaveMod -= 0.2>>
			<<set _mercMod -= 0.2>>
			<<set _SFMod -= 0.2>>
			<<set _enemyMod += 0.2>>
			<<set $woundType = random(1,10)>>
			<<set _i = $slaveIndices[$HeadGirl.ID]>>
			<<if $woundType == 1>>
				<<set $slaves[_i].voice = 0>>
				<<set $slaves[_i].health -= 60>>
			<<elseif $woundType == 2>>
				<<set $slaves[_i].eyes = -2>>
				<<set $slaves[_i].health -= 30>>
			<<elseif $woundType == 3>>
				<<set $slaves[_i].amp = 1>>
				<<set $slaves[_i].health = -80>>
			<<elseif $woundType >= 4>>
				<<if $slaves[_i].health >= -60>>
					<<set $slaves[_i].health -= 30>>
				<<else>>
					<<set $slaves[_i].health = -90>>
				<</if>>
			<</if>>
		<</if>>
		/* 60% chance of getting combat skill if not already have it */
		<<if $HeadGirl.skill.combat == 0 && random(1,100) <= 60>>
			<<set $gainedCombat = 1>>
			<<set _i = $slaveIndices[$HeadGirl.ID]>>
			<<set $slaves[_i].skill.combat = 1>>
		<</if>>
	<<elseif $leadingTroops == "citizen">>
		<<if $arcologies[0].FSDegradationist == "unset" && $arcologies[0].FSPaternalist == "unset">>
			<<set _militiaMod += 0.15>>
			<<set _slaveMod -= 0.15>>
		<<elseif $arcologies[0].FSPaternalist != "unset">>
			<<set _militiaMod += 0.15>>
			<<set _slaveMod += 0.10>>
		<<elseif $arcologies[0].FSDegradationist != "unset">>
			<<set _militiaMod += 0.15>>
			<<set _slaveMod -= 0.35>>
		<</if>>
		<<if $arcologies[0].FSRomanRevivalist != "unset">>
			<<set _mercMod += 0.10>>
			<<set _SFMod += 0.10>>
		<<else>>
			<<set _mercMod -= 0.10>>
			<<set _SFMod -= 0.10>>
		<</if>>
		<<set _atkMod += either(-1,1) * random(10) * 0.1>>
		<<set _defMod += either(-1,1) * random(10) * 0.1>>
		<<set _tacChance += either(-1,1) * random(20) * 0.1>>
	<<elseif $leadingTroops == "mercenary">>
		<<set _mercMod += 0.10>>
		<<set _SFMod += 0.10>>
		<<if $arcologies[0].FSRomanRevivalist != "unset">>
			<<set _militiaMod += 0.10>>
		<<else>>
			<<set _militiaMod -= 0.10>>
		<</if>>
		<<if $arcologies[0].FSDegradationist != "unset">>
			<<set _slaveMod -= 0.35>>
		<</if>>
		<<set _atkMod += random(15) * 0.1>>
		<<set _defMod += random(15) * 0.1>>
		<<set _tacChance += random(30) * 0.1>>
	<<elseif $leadingTroops == "colonel">>
		<<set _mercMod += 0.10>>
		<<set _SFMod += 0.15>>
		<<if $arcologies[0].FSRomanRevivalist != "unset">>
			<<set _militiaMod += 0.10>>
		<<else>>
			<<set _militiaMod -= 0.10>>
		<</if>>
		<<if $arcologies[0].FSDegradationist != "unset">>
			<<set _slaveMod -= 0.35>>
		<</if>>
		<<set _atkMod += random(30) * 0.1>>
		<<set _defMod += random(30) * 0.1>>
		<<set _tacChance += random(40) * 0.1>>
	<</if>>
	/* Terrain and Tactics */
	<<if $battleTerrain == "urban">>
		<<if $chosenTactic == "Bait and Bleed">>
			<<set _atkMod += 0.15>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Guerrilla">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.15>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Choke Points">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.15>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Interior Lines">>
			<<set _atkMod += 0.05>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.15>>
		<<elseif $chosenTactic == "Pincer Maneuver">>
			<<set _atkMod -= 0.05>>
			<<set _defMod -= 0.10>>
			<<set _tacChance -= 0.15>>
		<<elseif $chosenTactic == "Defense In Depth">>
			<<set _atkMod -= 0.05>>
			<<set _defMod -= 0.05>>
			<<set _tacChance -= 0.10>>
		<<elseif $chosenTactic == "Blitzkrieg">>
			<<set _atkMod -= 0.15>>
			<<set _defMod -= 0.10>>
			<<set _tacChance -= 0.25>>
		<<elseif $chosenTactic == "Human Wave">>
			<<set _atkMod -= 0.15>>
			<<set _defMod -= 0.15>>
			<<set _tacChance -= 0.30>>
		<</if>>
	<<elseif $battleTerrain == "rural">>
		<<if $chosenTactic == "Bait and Bleed">>
			<<set _atkMod -= 0.05>>
			<<set _defMod -= 0.10>>
			<<set _tacChance -= 0.15>>
		<<elseif $chosenTactic == "Guerrilla">>
			<<set _atkMod -= 0.10>>
			<<set _defMod -= 0.10>>
			<<set _tacChance -= 0.20>>
		<<elseif $chosenTactic == "Choke Points">>
			<<set _atkMod -= 0.10>>
			<<set _defMod -= 0.15>>
			<<set _tacChance -= 0.25>>
		<<elseif $chosenTactic == "Interior Lines">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.15>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Pincer Maneuver">>
			<<set _atkMod += 0.15>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Defense In Depth">>
			<<set _atkMod += 0.15>>
			<<set _defMod += 0.15>>
			<<set _tacChance += 0.30>>
		<<elseif $chosenTactic == "Blitzkrieg">>
			<<set _atkMod += 0.15>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Human Wave">>
			<<set _atkMod += 0.20>>
			<<set _defMod += 0.05>>
			<<set _tacChance += 0.25>>
		<</if>>
	<<elseif $battleTerrain == "hills">>
		<<if $chosenTactic == "Bait and Bleed">>
			<<set _atkMod += 0.05>>
			<<set _defMod += 0.05>>
			<<set _tacChance += 0.10>>
		<<elseif $chosenTactic == "Guerrilla">>
			<<set _atkMod += 0.05>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.15>>
		<<elseif $chosenTactic == "Choke Points">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.20>>
		<<elseif $chosenTactic == "Interior Lines">>
			<<set _atkMod -= 0.05>>
			<<set _defMod -= 0.05>>
			<<set _tacChance -= 0.10>>
		<<elseif $chosenTactic == "Pincer Maneuver">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.05>>
			<<set _tacChance += 0.15>>
		<<elseif $chosenTactic == "Defense In Depth">>
			<<set _atkMod -= 0.10>>
			<<set _defMod -= 0.05>>
			<<set _tacChance -= 0.15>>
		<<elseif $chosenTactic == "Blitzkrieg">>
			<<set _atkMod -= 0.10>>
			<<set _defMod -= 0.15>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Human Wave">>
			<<set _atkMod -= 0.15>>
			<<set _defMod -= 0.15>>
			<<set _tacChance -= 0.30>>
		<</if>>
	<<elseif $battleTerrain == "coast">>
		<<if $chosenTactic == "Bait and Bleed">>
			<<set _atkMod -= 0.05>>
			<<set _defMod -= 0.05>>
			<<set _tacChance -= 0.10>>
		<<elseif $chosenTactic == "Guerrilla">>
			<<set _atkMod += 0.05>>
			<<set _defMod += 0.05>>
			<<set _tacChance += 0.10>>
		<<elseif $chosenTactic == "Choke Points">>
			<<set _atkMod += 0.05>>
			<<set _defMod += 0.15>>
			<<set _tacChance += 0.15>>
		<<elseif $chosenTactic == "Interior Lines">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.20>>
		<<elseif $chosenTactic == "Pincer Maneuver">>
			<<set _atkMod -= 0.10>>
			<<set _defMod -= 0.10>>
			<<set _tacChance -= 0.20>>
		<<elseif $chosenTactic == "Defense In Depth">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.20>>
		<<elseif $chosenTactic == "Blitzkrieg">>
			<<set _atkMod -= 0.05>>
			<<set _defMod -= 0.05>>
			<<set _tacChance -= 0.10>>
		<<elseif $chosenTactic == "Human Wave">>
			<<set _atkMod += 0.05>>
			<<set _defMod -= 0.05>>
		<</if>>
	<<elseif $battleTerrain == "outskirts">>
		<<if $chosenTactic == "Bait and Bleed">>
			<<set _atkMod -= 0.10>>
			<<set _defMod -= 0.15>>
			<<set _tacChance -= 0.25>>
		<<elseif $chosenTactic == "Guerrilla">>
			<<set _atkMod -= 0.10>>
			<<set _defMod -= 0.05>>
			<<set _tacChance -= 0.15>>
		<<elseif $chosenTactic == "Choke Points">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.15>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Interior Lines">>
			<<set _atkMod += 0.15>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Pincer Maneuver">>
			<<set _atkMod += 0.05>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.15>>
		<<elseif $chosenTactic == "Defense In Depth">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.15>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Blitzkrieg">>
			<<set _atkMod += 0.10>>
			<<set _defMod -= 0.05>>
			<<set _tacChance += 0.05>>
		<<elseif $chosenTactic == "Human Wave">>
			<<set _atkMod += 0.10>>
			<<set _defMod -= 0.10>>
		<</if>>
	<<elseif $battleTerrain == "mountains">>
		<<if $chosenTactic == "Bait and Bleed">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.15>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Guerrilla">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.15>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Choke Points">>
			<<set _atkMod += 0.05>>
			<<set _defMod += 0.20>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Interior Lines">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.20>>
		<<elseif $chosenTactic == "Pincer Maneuver">>
			<<set _atkMod += 0.10>>
			<<set _defMod -= 0.05>>
			<<set _tacChance += 0.05>>
		<<elseif $chosenTactic == "Defense In Depth">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.20>>
		<<elseif $chosenTactic == "Blitzkrieg">>
			<<set _atkMod -= 0.10>>
			<<set _defMod -= 0.10>>
			<<set _tacChance -= 0.20>>
		<<elseif $chosenTactic == "Human Wave">>
			<<set _atkMod -= 0.10>>
			<<set _defMod -= 0.10>>
			<<set _tacChance -= 0.20>>
		<</if>>
	<<elseif $battleTerrain == "wasteland">>
		<<if $chosenTactic == "Bait and Bleed">>
			<<set _atkMod += 0.05>>
			<<set _defMod += 0.05>>
			<<set _tacChance += 0.10>>
		<<elseif $chosenTactic == "Guerrilla">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.05>>
			<<set _tacChance += 0.15>>
		<<elseif $chosenTactic == "Choke Points">>
			<<set _atkMod -= 0.10>>
			<<set _defMod += 0.05>>
			<<set _tacChance -= 0.05>>
		<<elseif $chosenTactic == "Interior Lines">>
			<<set _atkMod += 0.10>>
			<<set _defMod += 0.15>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Pincer Maneuver">>
			<<set _atkMod += 0.15>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.25>>
		<<elseif $chosenTactic == "Defense In Depth">>
			<<set _atkMod += 0.05>>
			<<set _defMod += 0.10>>
			<<set _tacChance += 0.15>>
		<<elseif $chosenTactic == "Blitzkrieg">>
			<<set _atkMod += 0.15>>
			<<set _defMod += 0.15>>
			<<set _tacChance += 0.30>>
		<<elseif $chosenTactic == "Human Wave">>
			<<set _atkMod += 0.20>>
			<<set _defMod += 0.05>>
			<<set _tacChance += 0.25>>
		<</if>>
	<</if>>

	<<if $chosenTactic == "Bait and Bleed">>
		<<if $attackType == "raiders">>
			<<set _tacChance -= 0.10>>
		<<elseif $attackType == "free city">>
			<<set _tacChance += 0.10>>
		<<elseif $attackType == "old world">>
			<<set _tacChance += 0.25>>
		<<elseif $attackType == "freedom fighters">>
			<<set _tacChance -= 0.15>>
		<</if>>
	<<elseif $chosenTactic == "Guerrilla">>
		<<if $attackType == "raiders">>
			<<set _tacChance -= 0.20>>
		<<elseif $attackType == "free city">>
			<<set _tacChance += 0.15>>
		<<elseif $attackType == "old world">>
			<<set _tacChance += 0.25>>
		<<elseif $attackType == "freedom fighters">>
			<<set _tacChance -= 0.25>>
		<</if>>
	<<elseif $chosenTactic == "Choke Points">>
		<<if $attackType == "raiders">>
			<<set _tacChance += 0.25>>
		<<elseif $attackType == "free city">>
			<<set _tacChance -= 0.05>>
		<<elseif $attackType == "old world">>
			<<set _tacChance -= 0.10>>
		<<elseif $attackType == "freedom fighters">>
			<<set _tacChance += 0.05>>
		<</if>>
	<<elseif $chosenTactic == "Interior Lines">>
		<<if $attackType == "raiders">>
			<<set _tacChance -= 0.15>>
		<<elseif $attackType == "free city">>
			<<set _tacChance += 0.15>>
		<<elseif $attackType == "old world">>
			<<set _tacChance += 0.20>>
		<<elseif $attackType == "freedom fighters">>
			<<set _tacChance -= 0.10>>
		<</if>>
	<<elseif $chosenTactic == "Pincer Maneuver">>
		<<if $attackType == "raiders">>
			<<set _tacChance += 0.15>>
		<<elseif $attackType == "free city">>
			<<set _tacChance += 0.10>>
		<<elseif $attackType == "old world">>
			<<set _tacChance -= 0.10>>
		<<elseif $attackType == "freedom fighters">>
			<<set _tacChance += 0.15>>
		<</if>>
	<<elseif $chosenTactic == "Defense In Depth">>
		<<if $attackType == "raiders">>
			<<set _tacChance -= 0.20>>
		<<elseif $attackType == "free city">>
			<<set _tacChance += 0.10>>
		<<elseif $attackType == "old world">>
			<<set _tacChance += 0.20>>
		<<elseif $attackType == "freedom fighters">>
			<<set _tacChance -= 0.05>>
		<</if>>
	<<elseif $chosenTactic == "Blitzkrieg">>
		<<if $attackType == "raiders">>
			<<set _tacChance += 0.10>>
		<<elseif $attackType == "free city">>
			<<set _tacChance -= 0.20>>
		<<elseif $attackType == "old world">>
			<<set _tacChance += 0.25>>
		<<elseif $attackType == "freedom fighters">>
			<<set _tacChance -= 0.10>>
		<</if>>
	<<elseif $chosenTactic == "Human Wave">>
		<<if $attackType == "raiders">>
			<<set _tacChance -= 0.10>>
		<<elseif $attackType == "free city">>
			<<set _tacChance += 0.10>>
		<<elseif $attackType == "old world">>
			<<set _tacChance -= 0.15>>
		<<elseif $attackType == "freedom fighters">>
			<<set _tacChance += 0.10>>
		<</if>>
	<</if>>

	/* Calculates if tactics are successful */
	/* minimum chance is 10% */
	<<if _tacChance <= 0>>
		<<set _tacChance = 0.1>>
	<</if>>
	<<if random(1,100) <= _tacChance * 100>>
		<<set _enemyMod -= 0.30>>
		<<set _militiaMod += 0.20>>
		<<set _slaveMod += 0.20>>
		<<set _mercMod += 0.20>>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.10>>
		<<set $tacticsSuccessful = 1>>
	<<else>>
		<<set _enemyMod += 0.20>>
		<<set _militiaMod -= 0.20>>
		<<set _slaveMod -= 0.20>>
		<<set _mercMod -= 0.20>>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.10>>
		<<set $tacticsSuccessful = 0>>
	<</if>>

	/* enemy morale mods */
	<<if $week < 30>>
		<<set _enemyMod += 0.15>>
	<<elseif $week < 60>>
		<<set _enemyMod += 0.30>>
	<<elseif $week < 90>>
		<<set _enemyMod += 0.45>>
	<<elseif $week < 120>>
		<<set _enemyMod += 0.60>>
	<<else>>
		<<set _enemyMod += 0.75>>
	<</if>>

	/* calculates PC army stats */
	<<if $secBots.isDeployed == 1>>
		<<set _attack += ($secBotsBaseAttack + $secBotsBaseAttack * $secBots.equip * $equipMod) * _atkMod>>
		<<set _defense += ($secBotsBaseDefense + $secBotsBaseDefense * $secBots.equip * $equipMod) * _defMod>>
		<<set _hp += $secBotsBaseHp * $secBots.troops>>
	<</if>>
	<<for _i = 0; _i < $militiaUnits.length; _i++>>
		<<if $militiaUnits[_i].isDeployed == 1>>
			<<if $militiaUnits[_i].training <= 10>>
				<<set _expBonus = 0>>
			<<elseif $militiaUnits[_i].training <= 33>>
				<<set _expBonus = 0.10>>
			<<elseif $militiaUnits[_i].training <= 66>>
				<<set _expBonus = 0.25>>
			<<else>>
				<<set _expBonus = 0.50>>
			<</if>>
			<<if $militiaUnits[_i].loyalty <= 10>>
				<<set _loyaltyBonus = 0>>
			<<elseif $militiaUnits[_i].loyalty <= 33>>
				<<set _loyaltyBonus = 0.10>>
			<<elseif $militiaUnits[_i].loyalty <= 66>>
				<<set _loyaltyBonus = 0.20>>
			<<else>>
				<<set _loyaltyBonus = 0.30>>
			<</if>>
			<<set _attack += ($militiaBaseAttack + $militiaBaseAttack * $militiaUnits[_i].equip * $equipMod + $militiaBaseAttack * _expBonus + $militiaBaseAttack * _loyaltyBonus + $militiaBaseAttack * $militiaUnits[_i].SF * 0.20 + $militiaUnits[_i].cyber) * _atkMod>>
			<<set _defense += ($militiaBaseDefense + $militiaBaseDefense * $militiaUnits[_i].equip * $equipMod + $militiaBaseDefense * _expBonus + $militiaBaseDefense * _loyaltyBonus + $militiaBaseDefense * $militiaUnits[_i].SF * 0.20 + $militiaUnits[_i].cyber) * _defMod>>
			<<set _hp += ($militiaBaseHp + $militiaUnits[_i].cyber + $militiaBaseHp * $militiaUnits[_i].medics * 0.25) * $militiaUnits[_i].troops>>
		<</if>>
	<</for>>
	<<for _i = 0; _i < $slaveUnits.length; _i++>>
		<<if $slaveUnits[_i].isDeployed == 1>>
			<<if $slaveUnits[_i].training <= 33>>
				<<set _expBonus = 0>>
			<<elseif $slaveUnits[_i].training <= 66>>
				<<set _expBonus = 0.25>>
			<<else>>
				<<set _expBonus = 0.50>>
			<</if>>
			<<if $slaveUnits[_i].loyalty <= 10>>
				<<set _loyaltyBonus = 0>>
			<<elseif $slaveUnits[_i].loyalty <= 33>>
				<<set _loyaltyBonus = 0.10>>
			<<elseif $slaveUnits[_i].loyalty <= 66>>
				<<set _loyaltyBonus = 0.20>>
			<<else>>
				<<set _loyaltyBonus = 0.30>>
			<</if>>
			<<set _attack += ($slaveBaseAttack + $slaveBaseAttack * $slaveUnits[_i].equip * $equipMod + $slaveBaseAttack * _expBonus + $slaveBaseAttack * _loyaltyBonus + $slaveBaseAttack * $slaveUnits[_i].SF * 0.20 + $slaveUnits[_i].cyber) * _atkMod>>
			<<set _defense += ($slaveBaseDefense + $slaveBaseDefense * $slaveUnits[_i].equip * $equipMod + $slaveBaseDefense * _expBonus + $slaveBaseDefense * _loyaltyBonus + $slaveBaseDefense * $slaveUnits[_i].SF * 0.20 + $slaveUnits[_i].cyber) * _defMod>>
			<<set _hp += ($slaveBaseHp + $slaveUnits[_i].cyber + $slaveBaseHp * $slaveUnits[_i].medics * 0.25) * $slaveUnits[_i].troops>>
		<</if>>
	<</for>>
	<<for _i = 0; _i < $mercUnits.length; _i++>>
		<<if $mercUnits[_i].isDeployed == 1>>
			<<if $mercUnits[_i].training <= 33>>
				<<set _expBonus = 0>>
			<<elseif $mercUnits[_i].training <= 66>>
				<<set _expBonus = 0.25>>
			<<else>>
				<<set _expBonus = 0.50>>
			<</if>>
			<<if $mercUnits[_i].loyalty <= 10>>
				<<set _loyaltyBonus = 0>>
			<<elseif $mercUnits[_i].loyalty <= 33>>
				<<set _loyaltyBonus = 0.10>>
			<<elseif $mercUnits[_i].loyalty <= 66>>
				<<set _loyaltyBonus = 0.20>>
			<<else>>
				<<set _loyaltyBonus = 0.30>>
			<</if>>
			<<set _attack += ($mercBaseAttack + $mercBaseAttack * $mercUnits[_i].equip * $equipMod + $mercBaseAttack * _expBonus + $mercBaseAttack * _loyaltyBonus + $mercBaseAttack * $mercUnits[_i].SF * 0.20 + $mercUnits[_i].cyber) * _atkMod>>
			<<set _defense += ($mercBaseDefense + $mercBaseDefense * $mercUnits[_i].equip * $equipMod + $mercBaseDefense * _expBonus + $mercBaseDefense * _loyaltyBonus + $mercBaseDefense * $mercUnits[_i].SF * 0.20 + $mercUnits[_i].cyber) * _defMod>>
			<<set _hp += ($mercBaseHp + $mercUnits[_i].cyber + $mercBaseHp * $mercUnits[_i].medics * 0.25) * $mercUnits[_i].troops>>
		<</if>>
	<</for>>

	<<if $SF.Toggle && $SF.Active >= 1 && $SFIntervention>>
		<<set $SFatk = 0>>
		<<set $SFdef = 0>>
		<<set $SFhp = 0>>
		<<calcSFStatistics>>
		<<set _attack += $SFatk>>
		<<set _defense += $SFdef>>
		<<set _hp += $SFhp>>
	<</if>>

	/* morale and baseHp calculation */
	/* minimum modifier is -50%, maximum is +50% */
	<<if _militiaMod < 0.5>>
		<<set _militiaMod = 0.5>>
	<<elseif _militiaMod > 1.5>>
		<<set _militiaMod = 1.5>>
	<</if>>
	<<if _slaveMod < 0.5>>
		<<set _slaveMod = 0.5>>
	<<elseif _slaveMod > 1.5>>
		<<set _slaveMod = 1.5>>
	<</if>>
	<<if _mercMod < 0.5>>
		<<set _mercMod = 0.5>>
	<<elseif _mercMod > 1.5>>
		<<set _mercMod = 1.5>>
	<</if>>
	<<if _SFMod < 0.5>>
		<<set _SFMod = 0.5>>
	<<elseif _SFMod > 1.5>>
		<<set _SFMod = 1.5>>
	<</if>>

	<<set $troopCount = 0>>
	<<calcTroopCount>>
	<<set _moraleTroopMod = Math.clamp($troopCount / 100,1,5)>>

	<<set _morale = ($secBotsMorale * $deployingBots + $militiaBaseMorale * _militiaMod * $deployingMilitia + $slaveBaseMorale * _slaveMod * $deployingSlaves + $mercBaseMorale * _mercMod * $deployingMercs + $SFBaseMorale * $SFIntervention * _SFMod) / ($deployingBots + $deployingMilitia +$deployingSlaves + $deployingMercs + $SFIntervention)>>
	<<set _morale = _morale + _morale * $secBarracksUpgrades.luxury * 0.05>>	/* barracks bonus */
	<<set _morale *= _moraleTroopMod>>
	<<set _baseHp = ($secBotsBaseHp * $deployingBots + $militiaBaseHp * $deployingMilitia + $slaveBaseHp * $deployingSlaves + $mercBaseHp * $deployingMercs + $SFBaseHp * $SFIntervention) / ($deployingBots + $deployingMilitia +$deployingSlaves + $deployingMercs + $SFIntervention)>>

	/* calculates enemy army stats */
	<<if $week <= 30>>
		<<set _armyMod = $attackTroops / 80>>
	<<elseif $week <= 60>>
		<<set _armyMod = $attackTroops / 75>>
	<<elseif $week <= 90>>
		<<set _armyMod = $attackTroops / 70>>
	<<elseif $week <= 120>>
		<<set _armyMod = $attackTroops / 65>>
	<<else>>
		<<set _armyMod = $attackTroops / 60>>
	<</if>>
	<<set _armyMod = Math.trunc(_armyMod)>>
	<<if $majorBattle == 1>>
		<<set _armyMod *= 2>>
	<</if>>
	<<if _armyMod <= 0>>
		<<set _armyMod = 1>>
	<</if>>

	<<set _enemyMoraleTroopMod = Math.clamp($attackTroops / 100,1,5)>>

	<<if $attackType == "raiders">>
		<<set _enemyAttack = ($raBaseAttack + $weapManu * $sellTo.raiders + $raBaseAttack * $attackEquip * $equipMod) * _armyMod>>
		<<set _enemyDefense = ($raBaseDefense + $weapManu * $sellTo.raiders + $raBaseDefense * $attackEquip * $equipMod) * _armyMod>>
		<<set _enemyMorale = $raBaseMorale * _enemyMod * _enemyMoraleTroopMod>>
		<<set _enemyHp = $raBaseHp * $attackTroops>>
		<<set _enemyBaseHp = $raBaseHp>>
	<<elseif $attackType == "free city">>
		<<set _enemyAttack = ($fcBaseAttack + $weapManu * $sellTo.FC + $fcBaseAttack * $attackEquip * $equipMod) * _armyMod>>
		<<set _enemyDefense = ($fcBaseDefense + $weapManu * $sellTo.FC + $fcBaseDefense * $attackEquip * $equipMod) * _armyMod>>
		<<set _enemyMorale = $fcBaseMorale * _enemyMod * _enemyMoraleTroopMod>>
		<<set _enemyHp = $fcBaseHp * $attackTroops>>
		<<set _enemyBaseHp = $fcBaseHp>>
	<<elseif $attackType == "old world">>
		<<set _enemyAttack = ($owBaseAttack + $weapManu * $sellTo.oldWorld + $owBaseAttack * $attackEquip * $equipMod) * _armyMod>>
		<<set _enemyDefense = ($owBaseDefense + $weapManu * $sellTo.oldWorld + $owBaseDefense * $attackEquip * $equipMod) * _armyMod>>
		<<set _enemyMorale = $owBaseMorale * _enemyMod * _enemyMoraleTroopMod>>
		<<set _enemyHp = $owBaseHp * $attackTroops>>
		<<set _enemyBaseHp = $owBaseHp>>
	<<elseif $attackType == "freedom fighters">>
		<<set _enemyAttack = ($ffBaseAttack + $weapManu * $sellTo.oldWorld + $ffBaseAttack * $attackEquip * $equipMod) * _armyMod>>
		<<set _enemyDefense = ($ffBaseDefense + $weapManu * $sellTo.oldWorld + $ffBaseDefense * $attackEquip * $equipMod) * _armyMod>>
		<<set _enemyMorale = $ffBaseMorale * _enemyMod * _enemyMoraleTroopMod>>
		<<set _enemyHp = $ffBaseHp * $attackTroops>>
		<<set _enemyBaseHp = $ffBaseHp>>
	<</if>>

	/* difficulty */
	<<set _enemyAttack *= $difficulty>>
	<<set _enemyDefense *= $difficulty>>
	<<set _enemyMorale *= $difficulty>>
	<<set _enemyHp *= $difficulty>>
	<<set _enemyBaseHp *= $difficulty>>

	<<if isNaN(_attack)>>

@@.red;Error: attack value reported NaN@@
	<</if>>
	<<if isNaN(_defense)>>

@@.red;Error: defense value reported NaN@@
	<</if>>
	<<if isNaN(_hp)>>

@@.red;Error: hp value reported NaN@@
	<</if>>
	<<if isNaN(_morale)>>

@@.red;Error: morale value reported NaN@@
	<</if>>
	<<if isNaN(_enemyAttack)>>

@@.red;Error: enemy attack value reported NaN@@
	<</if>>
	<<if isNaN(_enemyDefense)>>

@@.red;Error: enemy defense value reported NaN@@
	<</if>>
	<<if isNaN(_enemyHp)>>

@@.red;Error: enemy hp value reported NaN@@
	<</if>>
	<<if isNaN(_enemyMorale)>>

@@.red;Error: enemy morale value reported NaN@@
	<</if>>

	<<if $showBattleStatistics == 1>>
		<<set _atkMod -= 1, _defMod -= 1, _militiaMod -= 1, _mercMod -= 1, _slaveMod -= 1, _SFMod -= 1, _enemyMod -= 1, _moraleTroopMod -= 1, _enemyMoraleTroopMod -= 1, _difficulty = $difficulty -1>>
		<<set _atkMod = Math.round(_atkMod * 100)>>
		<<set _defMod = Math.round(_defMod * 100)>>
		<<set _militiaMod = Math.round(_militiaMod * 100)>>
		<<set _mercMod = Math.round(_mercMod * 100)>>
		<<set _slaveMod = Math.round(_slaveMod * 100)>>
		<<set _SFMod = Math.round(_SFMod * 100)>>
		<<set _enemyMod = Math.round(_enemyMod * 100)>>
		<<set _barracksBonus = $secBarracksUpgrades.luxury * 5>>
		<<set _moraleTroopMod = Math.round(_moraleTroopMod * 100)>>
		<<set _enemyMoraleTroopMod = Math.round(_enemyMoraleTroopMod * 100)>>
		<<set _difficulty *= 100>>

		__Difficulty__:

		<<if $difficulty == 0.5>>
			Very easy
		<<elseif $difficulty == 0.75>>
			Easy
		<<elseif $difficulty == 1>>
			Normal
		<<elseif $difficulty == 1.25>>
			Hard
		<<else>>
			Very hard
		<</if>>

		__Army__:

troops: <<print num(Math.round($troopCount))>>

attack: <<print num(Math.round(_attack))>>

defense: <<print num(Math.round(_defense))>>

hp: <<print num(Math.round(_hp))>>

morale: <<print num(Math.round(_morale))>>

attack modifier: <<if _atkMod > 0>>+<</if>>_atkMod%

defense modifier: <<if _defMod > 0>>+<</if>>_defMod%

average base HP: <<print num(Math.round(_baseHp))>>

militia morale modifier: <<if _militiaMod > 0>>+<</if>>_militiaMod%

slaves morale modifier: <<if _slaveMod > 0>>+<</if>>_slaveMod%

mercenaries morale modifier: <<if _mercMod > 0>>+<</if>>_mercMod%
		<<if $SF.Toggle && $SF.Active >= 1 && $SFIntervention>>

special force morale modifier: <<if _SFMod > 0>>+<</if>>_SFMod%
		<</if>>
		<<if $secBarracksUpgrades.luxury >= 1>>

Barracks bonus morale modifier: +<<print _barracksBonus>>%
		<</if>>
		<<if _moraleTroopMod>>

morale increase due to troop numbers: +<<print _moraleTroopMod>>%
		<</if>>

		__Tactics__:

tactic chance of success: <<print num(Math.round(_tacChance * 100))>>%

was tactic chosen successful?: <<if $tacticsSuccessful == 1>> yes <<else>> no<</if>>

		__Enemy__:

enemy troops: <<print num(Math.round($attackTroops))>>

enemy attack: <<print num(Math.round(_enemyAttack))>>

enemy defense: <<print num(Math.round(_enemyDefense))>>

enemy Hp: <<print num(Math.round(_enemyHp))>>

enemy morale: <<print num(Math.round(_enemyMorale))>>

enemy base Hp: <<print num(Math.round(_enemyBaseHp))>>

enemy morale modifier: <<if _enemyMod > 0>>+<</if>>_enemyMod%
		<<if _enemyMoraleTroopMod > 0>>

enemy morale increase due to troop numbers: +<<print _enemyMoraleTroopMod>>%
		<</if>>

Difficulty modifier: <<if _difficulty > 0>>+<</if>><<print _difficulty>>%
	<</if>>

	/* simulates the combat by pitting attk against def */
	<<for _i = 0; _i < _turns; _i++>>

		<<if $showBattleStatistics == 1>> turn: <<print _i + 1>><</if>>
		/* player army attacks */
		<<set _damage = Math.clamp(_attack - _enemyDefense,_attack * 0.1,_attack)>>

		<<if $showBattleStatistics == 1>> player damage: <<print num(Math.round(_damage))>><</if>>
		<<set _enemyHp -= _damage>>

		<<if $showBattleStatistics == 1>> remaining enemy Hp: <<print num(Math.round(_enemyHp))>><</if>>
		<<set $enemyLosses += _damage / _enemyBaseHp>>
		<<set _moraleDamage = Math.clamp(_damage / 2 + _damage / _enemyBaseHp,0,_damage*1.5)>>
		<<set _enemyMorale -= _moraleDamage>>

		<<if $showBattleStatistics == 1>> remaining enemy morale: <<print num(Math.round(_enemyMorale))>><</if>>
		<<if _enemyHp <= 0 || _enemyMorale <= 0>>

			<<if $showBattleStatistics == 1>>
Victory!<</if>>
			<<set $battleResult = 3>>
			<<set $battleTurns = _i>>
			<<break>>
		<</if>>

		/* attacker army attacks */
		<<set _damage = _enemyAttack - _defense>>
		<<if _damage < _enemyAttack * 0.1>>
			<<set _damage = _enemyAttack * 0.1>>
		<</if>>

		<<if $showBattleStatistics == 1>> enemy damage: <<print num(Math.round(_damage))>><</if>>
		<<set _hp -= _damage>>

		<<if $showBattleStatistics == 1>> remaining hp: <<print num(Math.round(_hp))>><</if>>
		<<set $losses += _damage / _baseHp>>
		<<set _moraleDamage = Math.clamp(_damage / 2 + _damage / _baseHp,0,_damage*1.5)>>
		<<set _morale -= _moraleDamage>>

		<<if $showBattleStatistics == 1>> remaining morale: <<print Math.round(_morale)>><</if>>
		<<if _hp <= 0 || _morale <= 0>>

			<<if $showBattleStatistics == 1>>
Defeat!<</if>>
			<<set $battleResult = -3>>
			<<set $battleTurns = _i>>
			<<break>>
		<</if>>
	<</for>>
	<<if $battleResult != 3 && $battleResult != -3>>

		<<if _morale > _enemyMorale>>
			<<if $showBattleStatistics == 1>>
Partial victory!<</if>>
			<<set $battleResult = 2>>
		<<elseif _morale < _enemyMorale>>
			<<if $showBattleStatistics == 1>>
Partial defeat!<</if>>
			<<set $battleResult = -2>>
		<</if>>
	<</if>>

	<<if $showBattleStatistics == 1>>

Losses: <<print num(Math.trunc($losses))>>

Enemy losses: <<print num(Math.trunc($enemyLosses))>>
	<</if>>

	<<if $battleResult > 3 || $battleResult < -3>>

@@.red;Error: failed to determine battle result@@
	<</if>>

	<<if $showBattleStatistics == 1 && $majorBattle == 1 && $majorBattleGameOver == 1 && $battleResult == -3>>

		<<link "Proceed">>
			<<set $gameover = "major battle defeat">>
			<<goto "Gameover">>
		<</link>>
	<<elseif $majorBattle == 1 && $majorBattleGameOver == 1 && $battleResult == -3>>
		<<set $gameover = "major battle defeat">>
		<<goto "Gameover">>
	<<elseif $showBattleStatistics == 1>>

		<<link "Proceed">>
			<<goto "attackReport">>
		<</link>>
	<<else>>
		<<goto "attackReport">>
	<</if>>

<</if>> /* closes check for bribery */