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>>	/* bribery check */
	<<if $showBattleStatistics == 1>>bribery chosen<</if>>
	<<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 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>>

/*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 $securityForceCreate == 1>>
		<<if $securityForceArcologyUpgrades >= 7>>
			<<set _atkMod += ($securityForceArcologyUpgrades - 6) * 0.05>>
		<</if>>
		<<if $securityForceFortressZeppelin >= 1>>
			<<set _defMod += $securityForceFortressZeppelin * 0.05>>
		<</if>>
		<<if $securityForceSatellitePower >= 11>>
			<<set _atkMod += ($securityForceSatellitePower - 10) * 0.05>>
		<</if>>
		<<if $securityForceGiantRobot >= 6>>
			<<set _defMod += ($securityForceGiantRobot - 5) * 0.05>>
		<</if>>
		<<if $securityForceMissileSilo >= 1>>
			<<set _atkMod += $securityForceMissileSilo * 0.05>>
			<<set _defMod += $securityForceMissileSilo * 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 >= 50>>
		<<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>>
		<<set $PC.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)) && $Bodyguard.intelligence == 3>>
		<<set _atkMod += 0.25>>
		<<set _defMod += 0.25>>
		<<set _tacChance += 0.50>>
	<<elseif $Bodyguard.intelligence == 3>>
		<<set _atkMod += 0.20>>
		<<set _defMod += 0.15>>
		<<set _tacChance += 0.35>>
	<<elseif (setup.bodyguardCareers.includes($Bodyguard.career) || setup.HGCareers.includes($Bodyguard.career)) && $Bodyguard.intelligence == 2>>
		<<set _atkMod += 0.15>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.25>>
	<<elseif $Bodyguard.intelligence == 2>>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.20>>
	<<elseif setup.bodyguardCareers.includes($Bodyguard.career) || setup.HGCareers.includes($Bodyguard.career) && $Bodyguard.intelligence >= 1>>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.05>>
		<<set _tacChance += 0.15>>
	<<elseif !(setup.bodyguardCareers.includes($Bodyguard.career) && setup.HGCareers.includes($Bodyguard.career)) || $Bodyguard.intelligence <= -2>>
		<<set _atkMod -= 0.15>>
		<<set _defMod -= 0.15>>
		<<set _tacChance -= 0.30>>
	<<elseif $Bodyguard.intelligence <= -2>>
		<<set _atkMod -= 0.15>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.25>>
	<<elseif !(setup.bodyguardCareers.includes($Bodyguard.career) && setup.HGCareers.includes($Bodyguard.career)) || $Bodyguard.intelligence <= -1>>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.20>>
	<<elseif $Bodyguard.intelligence <= -1>>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.05>>
		<<set _tacChance -= 0.15>>
	<</if>>
		/* does she get wounded? */
	<<if $Bodyguard.combatSkill == 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.preg >= 30>>
		<<set _woundChance += 1>>
	<</if>>
	<<if $Bodyguard.dick >= 8>>
		<<set _woundChance += 1>>
	<</if>>
	<<if $Bodyguard.balls >= 8>>
		<<set _woundChance += 1>>
	<</if>>
	<<if $Bodyguard.intelligence <= -3>>
		<<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)>>
		<<for _i = 0; _i < $slaves.length; _i++>>
			<<if $slaves[_i].ID == $Bodyguard.ID>>
				<<if $woundType == 1>>
					<<set $slaves[_i].voice = 0>>
				<<elseif $woundType == 2>>
					<<set $slaves[_i].eyes = -2>>
				<<elseif $woundType == 3>>
					<<set $slaves[_i].amp = 1>>
				<<elseif $woundType >= 4>>
					<<if $slaves[_i].health >= -60>>
						<<set $slaves[_i].health -= 30>>
					<<else>>
						<<set $slaves[_i].health -= Math.abs(90 - $slaves[_i].health)>>
					<</if>>
				<</if>>
			<</if>>
		<</for>>
	<</if>>
	/* 60% chance of getting combat skill if not already have it */
	<<if $Bodyguard.combatSkill == 0 && random(1,100) <= 60>>
		<<set $gainedCombat = 1>>
		<<set $Bodyguard.combatSkill = 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)) && $HeadGirl.intelligence == 3>>
		<<set _atkMod += 0.25>>
		<<set _defMod += 0.25>>
		<<set _tacChance += 0.50>>
	<<elseif $HeadGirl.intelligence == 3>>
		<<set _atkMod += 0.20>>
		<<set _defMod += 0.15>>
		<<set _tacChance += 0.35>>
	<<elseif (setup.bodyguardCareers.includes($HeadGirl.career) || setup.HGCareers.includes($HeadGirl.career)) && $HeadGirl.intelligence == 2>>
		<<set _atkMod += 0.15>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.25>>
	<<elseif $HeadGirl.intelligence == 2>>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.20>>
	<<elseif setup.bodyguardCareers.includes($HeadGirl.career) || setup.HGCareers.includes($HeadGirl.career) && $HeadGirl.intelligence >= 1>>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.05>>
		<<set _tacChance += 0.15>>
	<<elseif !(setup.bodyguardCareers.includes($HeadGirl.career) && setup.HGCareers.includes($HeadGirl.career)) || $HeadGirl.intelligence <= -2>>
		<<set _atkMod -= 0.15>>
		<<set _defMod -= 0.15>>
		<<set _tacChance -= 0.30>>
	<<elseif $HeadGirl.intelligence <= -2>>
		<<set _atkMod -= 0.15>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.25>>
	<<elseif !(setup.bodyguardCareers.includes($HeadGirl.career) && setup.HGCareers.includes($HeadGirl.career)) || $HeadGirl.intelligence <= -1>>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.20>>
	<<elseif $HeadGirl.intelligence <= -1>>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.05>>
		<<set _tacChance -= 0.15>>
	<</if>>
	/* does she get wounded? */
	<<if $HeadGirl.combatSkill == 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.preg >= 30>>
		<<set _woundChance += 1>>
	<</if>>
	<<if $HeadGirl.dick >= 8>>
		<<set _woundChance += 1>>
	<</if>>
	<<if $HeadGirl.balls >= 8>>
		<<set _woundChance += 1>>
	<</if>>
	<<if $HeadGirl.intelligence <= -3>>
		<<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)>>
		<<for _i = 0; _i < $slaves.length; _i++>>
			<<if $slaves[_i].ID == $HeadGirl.ID>>
				<<if $woundType == 1>>
					<<set $slaves[_i].voice = 0>>
				<<elseif $woundType == 2>>
					<<set $slaves[_i].eyes = -2>>
				<<elseif $woundType == 3>>
					<<set $slaves[_i].amp = 1>>
				<<elseif $woundType >= 4>>
					<<if $slaves[_i].health >= -60>>
						<<set $slaves[_i].health -= 30>>
					<<else>>
						<<set $slaves[_i].health -= Math.abs(90 - $slaves[_i].health)>>
					<</if>>
				<</if>>
			<</if>>
		<</for>>
	<</if>>
	/* 60% chance of getting combat skill if nto already have it */
	<<if $HeadGirl.combatSkill == 0 && random(1,100) <= 60>>
		<<set $gainedCombat = 1>>
		<<set $HeadGirl.combatSkill = 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 Manouver">>
		<<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 Manouver">>
		<<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 Manouver">>
		<<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 Manouver">>
		<<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 Manouver">>
		<<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 Manouver">>
		<<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 Manouver">>
		<<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 Manouver">>
	<<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 $SFIntervention == 1>>
	<<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 $troopCount>>

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

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

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

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

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

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

average base HP: <<print 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 $SFIntervention == 1>>

security 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 Math.round(_tacChance * 100)>>%

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

__Enemy__:

enemy troops: <<print $attackTroops>>

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

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

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

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

enemy base Hp: <<print 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 Math.round(_damage)>><</if>>
	<<set _enemyHp -= _damage>>

	<<if $showBattleStatistics == 1>> remaining enemy Hp: <<print 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 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 Math.round(_damage)>><</if>>
	<<set _hp -= _damage>>

	<<if $showBattleStatistics == 1>> remaining hp: <<print 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 Math.trunc($losses)>>

Enemy losses: <<print 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 to "major battle defeat">>
		<<goto "Gameover">>
	<</link>>
<<elseif $majorBattle == 1 && $majorBattleGameOver == 1 && $battleResult == -3>>
	<<set $gameover to "major battle defeat">>
	<<goto "Gameover">>
<<elseif $showBattleStatistics == 1>>

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

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


code:
:: attackOptions [nobr]

<<set $nextButton = " ", $nextLink = "attackOptions", $showEncyclopedia = 1, $encyclopedia = "Battles">>

<<if $majorBattle == 0>>
	Attack Imminent
<<else>>
	Major Attack Imminent
<</if>>

<<if $majorBattle == 0>>
	<<if $hasFoughtOnce == 1>>
		The ominous message dominates the screens of your office, <<print $assistantName>> quickly gathers all information available to prepare for battle.
		<<if $attackType == "raiders">>
			<<if $recon >= 1>>
				A disorganized horde of raiders is coming to your city. To such jackals your arcology surely looks like an appetizing morsel.
				Fortunately you knew of their coming, thanks to your recon systems.
			<<else>>
				Some of your citizens saw the disorganized horde of raiders coming towards the city and quickly reported it. To such jackals your arcology surely looks like an appetizing morsel.
			<</if>>

			Raiders are roaming gangs of bandits, preying on the vulnerable supply lines of free cities and old world nations. They are rarely equipped with decent armaments and even more rarely have any formal military training, but they make up for that with high mobility and numbers.
		<<elseif $attackType == "free city">>
			<<if $recon >= 1>>
				A menacing column of consumed mercenaries and slavers is coming to your city. Another free city is ready to use their best tools to hit a dangerous competitor where it hurts.
				Fortunately you knew of their coming, thanks to your recon systems.
			<<else>>
				Some of your citizens saw the menacing column of consumed mercenaries and slavers and rushed to your office to bring the grim news. Another free city is ready to use their best tools to bring down a dangerous competitor.
			<</if>>

			Free City expeditions are usually composed of mercenaries hired to take down sensible supplies or infrastructure in order to damage the enemies of their contractor. They have on average good equipment and training, together with decent mobility, making them a formidable force. Their biggest weakness however is their relatively low numbers.
		<<elseif $attackType == "freedom fighters">>
			<<if $recon >= 1>>
				A dangerous looking army of guerrillas is gathering just outside the arcology. Fanatics and idealists armed with dead men's words and hope, set on erasing your fledgling empire.
				Fortunately you knew of their coming, thanks to your recon systems.
			<<else>>
				Some of your citizens saw the dangerous looking army of guerrillas is gathering just outside the arcology. Fanatics and idealists armed with dead men's words and hope, set on erasing your fledgling empire.
			<</if>>

			Freedom Fighters are groups of individuals fighting to rid the planet of "evils" such as the free cities and their way of life. Lacking the strenght to assault one directly they fight guerrilla style slowly starving to death their enemies. They are rarely well equipped, but with good training and mobility they are not a threat that can be taken lightly.
		<<elseif $attackType == "old world">>
			<<if $recon >= 1>>
				A diciplined yet dusty, scruffy old world army is approaching the confines of your arcology. There's nothing better than a good war to unite the electorate and your arcology is just the perfect target.
				Fortunately you knew of their coming, thanks to your recon systems.
			<<else>>
				Some of your citizens saw the diciplined yet dusty, scruffy old world army is approaching the confines of your arcology. There's nothing better than a good war to unite the electorate and your arcology is just the perfect target.
			<</if>>

			Old world expeditions are usually sent to secure resources and trade routs for their nation or, more often, to provide their citizens with a boogey man to be scared of. They are usually decently equipped and trained, which together with their generous numbers make them a tough nut to crack. They however often lack in mobility.
		<</if>>
	<<else>>
		Your assistant interrupted your rest to bring the grim news. You quickly rush to your console, where you can see one of the convoys supplying your arcology has been attacked and looted. It seems a group of desperate looking bandits decided it was a good idea to steal from you.
		Due to their great wealth, free cities inevitably become tasty morsels for anyone able to field armed men. Considering the particular needs of arcologies their supply lines tend to be delicate lifelines, often preyed upon by those who stand to gain from the free city downfall.
	<</if>>
<<else>>
	<<if $hasFoughtMajorBattleOnce == 1>>
		The ominous message dominates the screens of your office, <<print $assistantName>> quickly gathers all information available to prepare for battle.
		<<if $attackType == "raiders">>
			<<if $recon >= 1>>
				A massive, disorganized horde of raiders is coming to your city. It seems a warlord of the wastelands massed enough men to try and obtain a slice of territory of his own, if he's not defeated there won't be a tomorrow for the arcology.
				Fortunately you knew of their coming, thanks to your recon systems.
			<<else>>
				Some of your citizens saw the massive, disorganized horde of raiders coming towards the city and quickly reported it. It seems a warlord of the wastelands massed enough men to try and obtain a slice of territory of his own, if he's not defeated there won't be a tomorrow for the arcology.
			<</if>>

			Raiders are roaming gangs of bandits, preying on the vulnerable supply lines of free cities and old world nations. They are rarely equipped with decent armaments and even more rarely have any formal military training, but they make up for that with high mobility and numbers.
		<<elseif $attackType == "free city">>
			<<if $recon >= 1>>
				A massive, menacing column of consumed mercenaries and slavers is coming to your city. The quantity of money invested in this assault is staggering, it seems you made some very powerful enemies. If they're not defeated your story will end this day.
				Fortunately you knew of their coming, thanks to your recon systems.
			<<else>>
				Some of your citizens saw the massive, menacing column of consumed mercenaries and slavers and rushed to your office to bring the grim news. The quantity of money invested in this assault is staggering, it seems you made some very powerful enemies. If they're not defeated your story will end this day.
			<</if>>

			Free City expeditions are usually composed of mercenaries hired to take down sensible supplies or infrastructure in order to damage the enemies of their contractor. They have on average good equipment and training, together with decent mobility, making them a formidable force. Their biggest weakness however is their low numbers.
		<<elseif $attackType == "freedom fighters">>
			<<if $recon >= 1>>
				A massive, dangerous army of guerrillas is gathering just outside the arcology. A huge ocean of fanatics and idealists armed with dead men's words and hope, set on erasing your fledgling empire once and for all. And this time they won't stop until your body is burnt to a crisp.
				Fortunately you knew of their coming, thanks to your recon systems.
			<<else>>
				Some of your citizens saw the massive, dangerous army of guerrillas is gathering just outside the arcology. A huge ocean of fanatics and idealists armed with dead men's words and hope, set on erasing your fledgling empire once and for all. And this time they won't stop until your body is burnt to a crisp.
			<</if>>

			Freedom Fighters are groups of individuals fighting to rid the planet of "evils" such as the free cities and their way of life. Lacking the strenght to assault one directly they fight guerrilla style slowly starving to death their enemies. They are rarely well equipped, but with good training and mobility they are not a threat that can be taken lightly.
		<<elseif $attackType == "old world">>
			<<if $recon >= 1>>
				A massive, diciplined ld world army is approaching the confines of your arcology. It seems one of the nations of the old world is determined to put your arcology to rest once and for all or die trying.
				Fortunately you knew of their coming, thanks to your recon systems.
			<<else>>
				Some of your citizens saw the massive, diciplined old world army is approaching the confines of your arcology. It seems one of the nations of the old world is determined to put your arcology to rest once and for all or die trying.
			<</if>>

			Old world expeditions are usually sent to secure resources and trade routs for their nation or, more often, to provide their citizens with a boogey man to be scared of. They are usually decently equipped and trained, which together with their generous numbers make them a tough nut to crack. They however often lack in mobility.
		<</if>>
	<<else>>
		Your assistant interrupted your rest to bring the grim news. You quickly rush to your console, where you can see the satellite images coming in of the force about to crash against your arcology. It's not the first time your armies fought for the survival of your empire, but this time it seems it will be a fight for life or death.

		<<if $attackType == "raiders">>
			<<if $recon >= 1>>
				A massive, disorganized horde of raiders is coming to your city. It seems a warlord of the wastelands massed enough men to try and obtain a slice of territory of his own, if he's not defeated there won't be a tomorrow for the arcology.
				Fortunately you knew of their coming, thanks to your recon systems.
			<<else>>
				Some of your citizens saw the massive, disorganized horde of raiders coming towards the city and quickly reported it. It seems a warlord of the wastelands massed enough men to try and obtain a slice of territory of his own, if he's not defeated there won't be a tomorrow for the arcology.
			<</if>>

			Raiders are roaming gangs of bandits, preying on the vulnerable supply lines of free cities and old world nations. They are rarely equipped with decent armaments and even more rarely have any formal military training, but they make up for that with high mobility and numbers.
		<<elseif $attackType == "free city">>
			<<if $recon >= 1>>
				A massive, menacing column of consumed mercenaries and slavers is coming to your city. The quantity of money invested in this assault is staggering, it seems you made some very powerful enemies. If they're not defeated your story will end this day.
				Fortunately you knew of their coming, thanks to your recon systems.
			<<else>>
				Some of your citizens saw the massive, menacing column of consumed mercenaries and slavers and rushed to your office to bring the grim news. The quantity of money invested in this assault is staggering, it seems you made some very powerful enemies. If they're not defeated your story will end this day.
			<</if>>

			Free City expeditions are usually composed of mercenaries hired to take down sensible supplies or infrastructure in order to damage the enemies of their contractor. They have on average good equipment and training, together with decent mobility, making them a formidable force. Their biggest weakness however is their relatively low numbers.
		<<elseif $attackType == "freedom fighters">>
			<<if $recon >= 1>>
				A massive, dangerous army of guerrillas is gathering just outside the arcology. A huge ocean of fanatics and idealists armed with dead men's words and hope, set on erasing your fledgling empire once and for all. And this time they won't stop until your body is burnt to a crisp.
				Fortunately you knew of their coming, thanks to your recon systems.
			<<else>>
				Some of your citizens saw the massive, dangerous army of guerrillas is gathering just outside the arcology. A huge ocean of fanatics and idealists armed with dead men's words and hope, set on erasing your fledgling empire once and for all. And this time they won't stop until your body is burnt to a crisp.
			<</if>>

			Freedom Fighters are groups of individuals fighting to rid the planet of "evils" such as the free cities and their way of life. Lacking the strenght to assault one directly they fight guerrilla style slowly starving to death their enemies. They are rarely well equipped, but with good training and mobility they are not a threat that can be taken lightly.
		<<elseif $attackType == "old world">>
			<<if $recon >= 1>>
				A massive, diciplined ld world army is approaching the confines of your arcology. It seems one of the nations of the old world is determined to put your arcology to rest once and for all or die trying.
				Fortunately you knew of their coming, thanks to your recon systems.
			<<else>>
				Some of your citizens saw the massive, diciplined old world army is approaching the confines of your arcology. It seems one of the nations of the old world is determined to put your arcology to rest once and for all or die trying.
			<</if>>

			Old world expeditions are usually sent to secure resources and trade routs for their nation or, more often, to provide their citizens with a boogey man to be scared of. They are usually decently equipped and trained, which together with their generous numbers make them a tough nut to crack. They however often lack in mobility.
		<</if>>
	<</if>>
<</if>>

__Recon__:

It seems your troops and your adversary will fight
<<if $battleTerrain == "rural">>
	in the rural land surrounding the free city.
<<elseif $battleTerrain == "urban">>
	in the old abandoned city surrounding the free city.
<<elseif $battleTerrain == "hills">>
	on the hills around the free city.
<<elseif $battleTerrain == "coast">>
	along the coast just outside the free city.
<<elseif $battleTerrain == "outskirts">>
	right against the walls of the arcology.
<<elseif $battleTerrain == "mountains">>
	in the mountains overlooking the arcology.
<<elseif $battleTerrain == "wasteland">>
	in the wastelands outside the free city territory.
<<elseif $battleTerrain == "error">>

@@.red;Error: failed to assign terrain@@. battleTerrain reads "<<print $battleTerrain>>".
<<else>>

@@.red;Error: failed to read terrain@@. battleTerrain reads "<<print $battleTerrain>>".
<</if>>
<<if $recon == 3>>
	Your recon capabilities are top notch. The information collected will be most likely correct or very close to be so:
<<elseif $recon == 2>>
	Your recon capabilities are decent. The information collected will be mostly close to the truth:
<<elseif $recon == 1>>
	Your recon capabilities are fairly low. The information collected will be quite inaccurate:
<<else>>
	Your recon capabilities are almost non-existent. The information collected will be wild guesses at best:
<</if>>
approximately <<print $estimatedMen>> men are coming, they seems to be
<<if $expectedEquip <= 0>>
	poorly armed. Old rusty small arms are the norm with just a few barely working civilian vehicles.
<<elseif $expectedEquip == 1>>
	lightly armed. Mostly small arms with some repurposed civilian vehicles and a scattered few machine guns. There's no sign of heavy vehicles, artillery or aircrafts.
<<elseif $expectedEquip == 2>>
	decently armed. Good quality small arms, machine guns a few mortars. There seems to be some heavy military vehicles coming as well.
<<elseif $expectedEquip == 3>>
	well armed. High quality small arms, snipers, demolitions teams, heavy duty machine guns and mortars. Heavy military vehicles are numerous and a few artillery piecies are accompaning the detachment.
<<elseif $expectedEquip >= 4>>
	extremely well armed. Excellent small arms and specialized teams with heavy duty infantry support weapons. Heavy presence of armored military vehicles, artillery pieces and even some attack helicopters.
<</if>>

__Battle Plan__:

/* leader assignment */
<<print $assistantName>> will lead your troops.

	<<link "Personally join the battle">>
		<<set $leadingTroops = "PC">>
		<<replace "#leader">><<print $PC.name>><</replace>>
	<</link>>
	|
	<<link "Let $assistantName lead the troops">>
		<<set $leadingTroops = "assistant">>
		<<replace "#leader">><<print $assistantName>><</replace>>
	<</link>>
	<<if $Bodyguard != 0 && $slavesOfficers == 1>>
	|
	<<link "Let your bodyguard lead your troops">>
		<<set $leadingTroops = "bodyguard">>
		<<replace "#leader">><<print $Bodyguard.slaveName>><</replace>>
	<</link>>
	<</if>>
	<<if $HeadGirl != 0 && $slavesOfficers == 1>>
	|
	<<link "Let your head girl lead your troops">>
		<<set $leadingTroops = "headGirl">>
		<<replace "#leader">><<print $HeadGirl.slaveName>><</replace>>
	<</link>>
	<</if>>
	<<if $militiaFounded == 1>>
	|
	<<link "Let the citizens' militia officers lead the troops">>
		<<set $leadingTroops = "citizen">>
		<<replace "#leader">>The citizens' militia commander<</replace>>
	<</link>>
	<</if>>
	<<if $mercenaries > 0>>
	|
	<<link "Let the mercenary officers lead the troops">>
		<<set $leadingTroops = "mercenary">>
		<<replace "#leader">>The mercenary commander<</replace>>
	<</link>>
	<</if>>
	<<if $securityForceCreate == 1>>
	|
	<<link "Let the colonel lead the troops">>
		<<set $leadingTroops = "colonel">>
		<<replace "#leader">>$securityForceName's colonel<</replace>>
	<</link>>
	<</if>>

/* troop deployment */

With your current readiness level you can <<if $deployedUnits > 0>>still send <<print $deployableUnits>> more units.<<else>>send <<print $deployableUnits>> units.<</if>>

Deployable units:

<<set _mL = $militiaUnits.length>>
<<set _sL = $slaveUnits.length>>
<<set _meL = $mercUnits.length>>
<<if $deployableUnits > 0>>
		<<if $secBots.active == 1 && $secBots.isDeployed == 0 && $secBots.troops > 0>>

			<<secBotsDescription>>

			<<link "Deploy the unit">>
				<<set $secBots.isDeployed = 1>>
				<<set $deployableUnits-->>
				<<set $deployedUnits++>>
				<<set $deployingBots = 1>>
				<<goto "attackOptions">>
			<</link>>
		<</if>>
	<<for _i = 0; _i < _mL; _i++>>
		<<capture _i>>
		<<if $militiaUnits[_i].active == 1 && $militiaUnits[_i].isDeployed == 0 && $militiaUnits[_i].troops > 0>>

			<<militiaUnitsDescription $militiaUnits[_i]>>

			<<link "Deploy the unit">>
				<<set $militiaUnits[_i].isDeployed = 1>>
				<<set $deployableUnits-->>
				<<set $deployedUnits++>>
				<<goto "attackOptions">>
			<</link>>
		<</if>>
		<</capture>>
	<</for>>
	<<for _i = 0; _i < _sL; _i++>>
		<<capture _i>>
		<<if $slaveUnits[_i].active == 1 && $slaveUnits[_i].isDeployed == 0 && $slaveUnits[_i].troops > 0>>

			<<slaveUnitsDescription $slaveUnits[_i]>>

			<<link "Deploy the unit">>
				<<set $slaveUnits[_i].isDeployed = 1>>
				<<set $deployableUnits-->>
				<<set $deployedUnits++>>
				<<goto "attackOptions">>
			<</link>>
		<</if>>
		<</capture>>
	<</for>>
	<<for _i = 0; _i < _meL; _i++>>
		<<capture _i>>
		<<if $mercUnits[_i].active == 1 && $mercUnits[_i].isDeployed == 0 && $mercUnits[_i].troops > 0>>

			<<mercUnitsDescription $mercUnits[_i]>>

			<<link "Deploy the unit">>
				<<set $mercUnits[_i].isDeployed = 1>>
				<<set $deployableUnits-->>
				<<set $deployedUnits++>>
				<<goto "attackOptions">>
			<</link>>
		<</if>>
		<</capture>>
	<</for>>
<<else>>

	Unit roster full.
<</if>>

Units about to be deployed:

<<if $deployedUnits > 0>>
		<<if $secBots.isDeployed == 1>>

			<<secBotsDescription>>

			<<link "Remove the unit">>
				<<set $secBots.isDeployed = 0>>
				<<set $deployableUnits++>>
				<<set $deployedUnits-->>
				<<set $deployingBots = 0>>
				<<goto "attackOptions">>
			<</link>>
		<</if>>
	<<for _i = 0; _i < _mL; _i++>>
		<<capture _i>>
		<<if $militiaUnits[_i].isDeployed == 1>>

			<<militiaUnitsDescription $militiaUnits[_i]>>

			<<link "Remove the unit">>
				<<set $militiaUnits[_i].isDeployed = 0>>
				<<set $deployableUnits++>>
				<<set $deployedUnits-->>
				<<goto "attackOptions">>
			<</link>>
		<</if>>
		<</capture>>
	<</for>>
	<<for _i = 0; _i < _sL; _i++>>
		<<capture _i>>
		<<if $slaveUnits[_i].isDeployed == 1>>

			<<slaveUnitsDescription $slaveUnits[_i]>>

			<<link "Remove the unit">>
				<<set $slaveUnits[_i].isDeployed = 0>>
				<<set $deployableUnits++>>
				<<set $deployedUnits-->>
				<<goto "attackOptions">>
			<</link>>
		<</if>>
		<</capture>>
	<</for>>
	<<for _i = 0; _i < _meL; _i++>>
		<<capture _i>>
		<<if $mercUnits[_i].isDeployed == 1>>

			<<mercUnitsDescription $mercUnits[_i]>>

			<<link "Remove the unit">>
				<<set $mercUnits[_i].isDeployed = 0>>
				<<set $deployableUnits++>>
				<<set $deployedUnits-->>
				<<goto "attackOptions">>
			<</link>>
		<</if>>
		<</capture>>
	<</for>>
<</if>>

<<if $majorBattle == 1 && $securityForceCreate == 1>>

	The size of the incoming attack warrants the intervention of the security force in its full force. They will not intervene.

	<<if $SFIntervention == 0>>
		<<link "Let the Security force intervene">>
			<<set $SFIntervention = 1>>
			<<replace "#SFI">>
				intervene
			<</replace>>
		<</link>>
		//The security force will join the battle with all the equipment they can mobilize within a short timeframe//
	<<else>>
		<<link "Do not let the Security force intervene">>
			<<set $SFIntervention = 0>>
			<<replace "#SFI">>
				not intevene
			<</replace>>
		<</link>>
		//The security force will not join the battle//
	<</if>>

//Some upgrades will be able to support the troops even if the security force does not intervene directly in the fight.//
<</if>>

/* tactics */
For this battle you choose to follow <<print $chosenTactic>> tactics.

Defensive tactics

<<link "Bait and Bleed">>
	<<set $chosenTactic = "Bait and Bleed">>
	<<replace "#tactic">><<print $chosenTactic>><</replace>>
<</link>>

    //Combines bait and switch tactics with guerriglia style assaults, with the objective of slowly bleed the enemy.//

<<link "Guerrilla">>
	<<set $chosenTactic = "Guerrilla">>
	<<replace "#tactic">><<print $chosenTactic>><</replace>>
<</link>>

    //Involves using terrain knowledge and small fast attacks to hinder and weaken the enemy.//

<<link "Choke Points">>
	<<set $chosenTactic = "Choke Points">>
	<<replace "#tactic">><<print $chosenTactic>><</replace>>
<</link>>

    //Involves using terrain knowledge and strong fortifications in order to stop the enemy on its track.//

<<link "Interior Lines">>
	<<set $chosenTactic = "Interior Lines">>
	<<replace "#tactic">><<print $chosenTactic>><</replace>>
<</link>>

    //Involves exploiting a defender's shorter logistics lines and redeployment times in order to keep the enemy pressured.//

<<link "Pincer Manouver">>
	<<set $chosenTactic = "Pincer Manouver">>
	<<replace "#tactic">><<print $chosenTactic>><</replace>>
<</link>>

    //Involves letting the enemy push back the centre in order to envelop their formation.//

<<link "Defense In Depth">>
	<<set $chosenTactic = "Defense In Depth">>
	<<replace "#tactic">><<print $chosenTactic>><</replace>>
<</link>>

    //Involves letting the enemy gain terrain to gain tactical superiority by alternating between delaying actions and small counterattacks.//

Offensive Tactics

<<link "Blitzkrieg">>
	<<set $chosenTactic = "Blitzkrieg">>
	<<replace "#tactic">><<print $chosenTactic>><</replace>>
<</link>>

    //Involves breaking the front of the enemy with a fast armored force concentrated into a small area.//

<<link "Human Wave">>
	<<set $chosenTactic = "Human Wave">>
	<<replace "#tactic">><<print $chosenTactic>><</replace>>
<</link>>

    //Involves assaulting the enemy with large numbers of infantry to overwhelm their lines.//

<<if $deployedUnits > 0>>
	<<link "Send your orders">>
		<<set $battleResult = 4>>						/* sets $battleResult value outside accepted range to avoid evaluation problems */
		<<if $hasFoughtOnce == 0>>
			<<set $hasFoughtOnce = 1>>
		<</if>>
		<<set $foughtThisWeek = 1>>
		<<goto "attackHandler">>
	<</link>>
<<else>>
	You need at least a unit in your roster to proceed to battle.
<</if>>

<<link "Surrender">>
	<<set $battleResult = -1>>
	<<if $hasFoughtOnce == 0>>
		<<set $hasFoughtOnce = 1>>
	<</if>>
	<<set $foughtThisWeek = 1>>
	<<goto "attackReport">>
<</link>>

/* calculates bribe cost */
<<calcBribe>>
<<link "Attempt to bribe">>
	<<set $battleResult = 1>>
	<<if $hasFoughtOnce == 0>>
		<<set $hasFoughtOnce = 1>>
	<</if>>
	<<set $foughtThisWeek = 1>>
	<<goto "attackHandler">>
<</link>>

    //Will cost around ¤<<print Math.round($bribeCost * (1 + either(-1,1) * random(2) * 0.1))>> (estimate).//

license: mit