code:
#!/bin/bash
if [ ! -d ".git" ]; then
	#not running in git repo, so can't use git commands :-)
	echo "No .git repo found - skipping sanity checks"
	exit 0
fi

WARNING='\033[93m'
WARNING='\033[93m'
ENDC='\033[0m'

myprint() {
	while read data; do
		echo -n -e "[$1]$WARNING"
		echo "$data"
	done
}

GREP="git grep -n --color"
# Check for missing right angle bracket: <</if>
$GREP "<</[^>]*>[^>]" -- 'src/*'  | myprint "MissingClosingAngleBracket"
$GREP "<<[^>()]*>[^()<>"$'\r]*\r'"\?$" -- 'src/*' | myprint "MissingClosingAngleBracket"
# Check for missing left angle bracket: </if>>
$GREP "\([^<]\|^\)</\?\(if\|else\|case\|set\|print\|elseif\)" -- 'src/*' | myprint "MissingOpeningAngleBracket2"
# Check for accidental assignment.  e.g.:   <<if $foo = "hello">>
$GREP "<<[ ]*if[^>=]*[^><\!=]=[^=][^>]*>>" -- 'src/*' | myprint "AccidentalAssignmentInIf"
# Check for accidental assignment.  e.g.:   <<elseif $foo = "hello">>
$GREP "<<[ ]*elseif[^>=]*[^><\!=]=[^=][^>]*>>" -- 'src/*' | myprint "AccidentalAssignmentInElseIf"
# Check for missing ".  e.g.:   <<if $foo == "hello>>
$GREP "<<[^\"<>]*\"[^\"<>]*>>" -- 'src/*' | myprint "MissingSpeechMark"
# Check for missing ".  e.g.:   <<if $foo = "hello)
$GREP -e "<<[^\"<>]*\([^\"<>]*\"[^><\"]*\"\| [<>] \)*\"\([^\"<>]*\"[^><\"]*\"\| [<>] \)*\([^\"<>]\| [<>] \)*>>" --and --not -e "*[^']*" -- 'src/*' | myprint "MissingSpeechMark2"
# Check for colors like: @@color:red   - should be @@.red
$GREP -e "@@color:" --and --not -e  "@@color:rgb([0-9 ]\+,[0-9 ]\+,[0-9 ]\+)" -- "src/*" | myprint "UseCssColors"
# Check for missing $ in activeSlave or PC
$GREP "<<[ ]*[^\$><_\[]*\(activeSlave\|PC\)[.]"  -- "src/*" | myprint "MissingDollar"
# Check for closing bracket without opening bracket.  e.g.:  <<if foo)>>      (but  <<case "foo")>>   is valid, so ignore those
$GREP -e "<<[ a-zA-Z]\+\([^()<>]\|[^()<>][<>][^()<>]\)*)" --and --not -e "<< *case"  -- "src/*" | myprint "MissingOpeningBracket"
# Check for opening bracket without closing bracket.  e.g.:  <<if (foo>>
$GREP -e "<<[ a-zA-Z]\([^<>]\|[^<>][<>][^<>]\)\+(\([^()<>]\|[^<>()][<>][^<>()]\|([^<>()]*])\)*>>" -- "src/*" | myprint "MissingClosingBracket"
# Check for two closing brackets but one opening bracket.  e.g.:  <<if (foo))>>
$GREP -e "<<[ a-zA-Z]\+[^()<>]*([^()]*)[^()]*)[^()<>]*>>"  -- "src/*" | myprint "MissingOpeningBracket2"
# Check for one closing bracket but two opening brackets.  e.g.:  <<if ((foo)>>
$GREP -e "<<[ a-zA-Z]\+[^()<>]*([^()]*([^()]*)[^()<>]*>>"  -- "src/*" | myprint "MissingClosingBracket2"
$GREP -e "<<.*[(][^<>)]*[(][^<>)]*)\?[^<>)]*>>" -- "src/*" | myprint "MissingClosingBracket3"
# Check for missing >>.  e.g.:   <<if $foo
$GREP "<<[^<>]*[^,\"\[{"$'\r]\r'"\?$" -- 'src/*' | myprint "MissingClosingAngleBrackets"
# Check for too many >>>.  e.g.: <</if>>>
$GREP "<<[^<>]*[<>]\?[^<>]*>>>" -- "src/*.tw" | myprint "TooManyAngleBrackets"
# Check for too many <<<.  e.g.: <<</if>>
$GREP "<<<[^<>]*[<>]\?[^<>]*>>" -- "src/*.tw" | myprint "TooManyAngleBrackets"
# Check for wrong capitalization on 'activeslave' and other common typos
$GREP -e "\$act" --and --not -e "\$\(activeSlave\|activeArcology\|activeStandard\|activeOrgan\|activeLimbs\|activeUnits\)" -- "src/*" | myprint "WrongCapitilization"
$GREP  "\(csae\|[a-z] She \|attepmts\|youreslf\|advnaces\|canAcheive\|setBellySize\|SetbellySize\|setbellySize\|bellypreg\|pregBelly\|bellyimplant\|bellyfluid\|pronounCaps\)" -- 'src/*' | myprint "SpellCheck"
$GREP "\$slave\[" -- 'src/*' | myprint "ShouldBeSlaves"
# Check for strange spaces e.g.  $slaves[$i]. lips
$GREP "\$slaves\[\$i\]\. " -- 'src/*' | myprint "MissingPropertyAfterSlaves"
# Check using refreshmentType instead of refreshment
$GREP "\$PC.refreshmentType[^ =]" -- 'src/*' | myprint "ShouldBeRefreshment"
# Check, e.g., <<//if>>
$GREP "<</[a-zA-Z]*[^a-zA-Z<>]\+[a-zA-Z]*>>" -- 'src/*' | myprint "DoubleSlash"
# Check, e.g.  <<else $foo==4
$GREP "<<else >\?[^>]" -- 'src/*' | myprint "ShouldBeElseIf"
# Check, e.g., =to
$GREP "=to" -- 'src/*' | myprint "EqualAndTo"
# Check doing  $slaves.foo instead of $slaves[i].foo
$GREP -e "[$]slaves[.]"  --and --not -e '[$]slaves[.]\(length\|random\|map\|filter\|deleteAt\|push\|find\|includes\|delete\|forEach\)' -- 'src/*' | myprint "MissingSlavesIndex"
# Try to check for accidentally mixing slaves[] and activeSlave.  This can have a lot of false matches, but has caught a lot of bugs so it's worth the pain
$GREP -e "activeSlave[.]" --and -e "slaves\[..\?\][.]" --and --not -e '[.]ID' --and --not -e 'slaves\[..\?\][.]\(slaveName\|slaveSurname\|actualAge\|relation\|assignment\|age\|devotion\|trust\|vagina\|mother\|father\)' -- 'src/*' | myprint "MaybeAccidentalMixingOfSlavesAndActiveSlave"
# Check, e.g.  <<set foo == 4>>
$GREP "<<set[^{>=]*==" -- 'src/*' | myprint "DoubleEqualsInSet"
# Check for, e.g   <<if slaves[foo]>>
$GREP "<<\([^>]\|[^>]>[^>]\)*[^$]slaves\[" -- 'src/*' | myprint "MissingDollar"
# Check for missing $ or _ in variable name:
$GREP -e "<<[a-zA-Z]\([^>\"]\|[^>]>[^>]\|\"[^\"]*\"\)* [a-zA-Z]\+ * =" -- src/*.tw | myprint "MissingDollar2"
# Check for missing command, e.g.  <<foo =
$GREP -e "<<[a-zA-Z]* = *" -- src/*.tw | myprint "BadCommand"
# Check for duplicate words, e.g. with with
$GREP -e  " \(\b[a-zA-Z][a-zA-Z]\+\) \1\b " --and --not -e " her her " --and --not -e " you you " --and --not -e " New New " --and --not -e " that that " --and --not -e " in in " --and --not -e " is is " -- 'src/*' | myprint "Duplicate words"
# Check for obsolete SugarCube macros
$GREP -E "<<display|<<click|<<.*\.contains" -- src/*.tw | myprint "ObsoleteMacro"
# Check for double articles
$GREP -E "\Wa an\W" -- src/*.tw | myprint "DoubleArticle"
# Check for incorrect articles
$GREP -i -E "\Wa (a|e|i|o|u)." -- src/*.tw | grep -i -vE "\Wa (un|eu|us|ut|on|ur|in)." | grep -i -vE "(&|<<s>>|UM)." | myprint "IncorrectArticle"
$GREP -i -E "\Wan (b|c|d|f|g|j|k|l|m|n|p|q|r|s|t|v|w|x|y|z)\w." -- src/*.tw | grep -i -vE "[A-Z]{3}" | myprint "IncorrectArticle"
# Check for $ sign mid-word
$GREP -i "\w$\w" -- src/*.tw | myprint "VarSignMidWord"
# check for $ sign at beginning of macro
$GREP '<<\s*\$' -- 'src/*'  | myprint "VarSignAtMacroStart"
# check for missing ; before statement
$GREP 'if $ ' -- 'src/*'  | myprint "missing ; before statement"
$GREP 'elseif $ ' -- 'src/*'  | myprint "missing ; before statement"
# Check for a . inside a <<>>
$GREP "<<[a-zA-Z]\([^\"'>]\|[^\"'>]>[^\"'>]\)*[a-zA-Z][.][^a-zA-Z]" | myprint "StrangeCharacterAfterDot"

# Check that we do not have any variables that we use only once.   e.g.     $onlyUsedOnce
# Ignore  *Nationalities
(
cd src/
cat $(find . -name "*.tw" ) | tr -c '$a-zA-Z' '\n'  | sed -n '/^[$]/p' | grep -v "Nationalities" | sort | uniq -u | sed 's/^[$]/-e[$]/' | sed 's/$/\\\\W/' | xargs -r  git grep -n --color | myprint "OnlyUsedOnce"
cat $(find . -name "*.tw" ) | tr -c '.$a-zA-Z[]_' '\n' | sed 's/SugarCube\.State\.variables\./$/g' | sed -n -e 's/^[$]\(PC\|activeSlave\|\(slaves\|tanks\)\[[^]]*\]*\)[.]\([a-zA-Z]\+\).*$/[.]\3/p' | sort | uniq -u |sed 's/^\(.*\)$/-e\1\\\\\b/'  | xargs -r git grep -n --color | myprint "SlaveAttributeUsedOnce"
$GREP "\$\(PC\|activeSlave\|slaves\|tanks\)[.][^a-zA-Z]" | myprint "UnexpectedCharAfterDot"

)

# Check that all the tags are properly opened and closed
git ls-files "src/*.tw" | xargs -d '\n' ./devTools/check.py

path: sanityCheck


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>>
<<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>>

/* 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 $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 $weaponsLaw || $wasToggledBefore == 1>>
<<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 $secHelots>>
<<set $secHelots = 0>>
<</if>>
<<if ndef $reqHelots>>
<<set $reqHelots = 20>>
<</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 ndef $weapHelots>>
<<set $weapHelots = 0>>
<</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 isInt($droneUpgrades)>>
		<<unset $droneUpgrades>>
		<<set $droneUpgrades = {
			attack: 0,
			defense: 0,
			hp:0}>>
	<</if>>
	<<if ndef $droneUpgrades.attack || !isInt($droneUpgrades.attack)>>
		<<set $droneUpgrades.attack = 0>>
	<</if>>
	<<if ndef $droneUpgrades.defense || !isInt($droneUpgrades.defense)>>
		<<set $droneUpgrades.defense = 0>>
	<</if>>
	<<if ndef $droneUpgrades.hp || !isInt($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 isInt($humanUpgrade)>>
		<<unset $humanUpgrade>>
		<<set $humanUpgrade = {
			attack: 0,
			defense: 0,
			hp:0,
			morale: 0}>>
	<</if>>
	<<if ndef $humanUpgrade.attack || !isInt($humanUpgrade.attack)>>
		<<set $humanUpgrade.attack = 0>>
	<</if>>
	<<if ndef $humanUpgrade.defense || !isInt($humanUpgrade.defense)>>
		<<set $humanUpgrade.defense = 0>>
	<</if>>
	<<if ndef $humanUpgrade.hp || !isInt($humanUpgrade.hp)>>
		<<set $humanUpgrade.hp = 0>>
	<</if>>
	<<if ndef $humanUpgrade.morale || !isInt($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 $hostileRoute>>
<<set $hostileRoute = 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 $militiaRecruitment>>
<<set $militiaRecruitment = 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 $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>>

/* 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,
		isDeployed: 0,
		troops: 0,
		maxTroops: 0,
		equip: 0} >>
	<<elseif $arcologyUpgrade.drones == 1>>
		<<set $secBots = {
		active: 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>>

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

<<set $wasToggledBefore = 1>>

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

license: mit


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 $lastAttackWeeks >= 15>>
		<<set _attackChance += 5>>
	<</if>>
	<<if $hasFoughtOnce == 1>>
		<<set _attackChance = 25>>
	<</if>>
<</if>>

<<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", "Guerrilla", "Choke Points", "Interior Lines", "Pincer Manouver", "Defense In Depth", "Blitzkrieg", "Human Wave")>>
	/* _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("mountains","outskirts","hills","wasteland")>>
	<<elseif $terrain == "marine">>
		<<set $battleTerrain = either("coast","outskirts","hills","wasteland")>>
	<<elseif $terrain == "oceanic">>
		<<set $battleTerrain = either("coast","outskirts","hills","wasteland")>>
	<<else>>
		<<set $battleTerrain = "error">>
	<</if>>

	<<if $attackType == "raiders">>
		<<set $attackTroops = random(40,80)>>
		<<if $week < 30>>
			<<set $attackTroops *= random(1,2)>>
		<<elseif $week < 60>>
			<<set $attackTroops *= random(1,3)>>
		<<elseif $week < 90>>
			<<set $attackTroops *= random(2,3)>>
		<<elseif $week < 120>>
			<<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,2)>>
		<<elseif $week < 120>>
			<<set $attackEquip = random(0,3)>>
		<<else>>
			<<set $attackEquip = random(1,3)>>
		<</if>>
	<<elseif $attackType == "free city">>
		<<set $attackTroops = random(20,40)>>
		<<if $week < 30>>
			<<set $attackTroops *= random(1,2)>>
		<<elseif $week < 60>>
			<<set $attackTroops *= random(1,3)>>
		<<elseif $week < 90>>
			<<set $attackTroops *= random(2,3)>>
		<<elseif $week < 120>>
			<<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)>>
		<<elseif $week < 120>>
			<<set $attackEquip = random(1,3)>>
		<<else>>
			<<set $attackEquip = random(2,4)>>
		<</if>>
	<<elseif $attackType == "old world">>
		<<set $attackTroops = random(25,50)>>
		<<if $week < 30>>
			<<set $attackTroops *= random(1,2)>>
		<<elseif $week < 60>>
			<<set $attackTroops *= random(1,3)>>
		<<elseif $week < 90>>
			<<set $attackTroops *= random(2,3)>>
		<<elseif $week < 120>>
			<<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)>>
		<<elseif $week < 120>>
			<<set $attackEquip = random(1,3)>>
		<<else>>
			<<set $attackEquip = random(2,4)>>
		<</if>>
	<<elseif $attackType == "freedom fighters">>
		<<set $attackTroops = random(30,60)>>
		<<if $week < 30>>
			<<set $attackTroops *= random(1,2)>>
		<<elseif $week < 60>>
			<<set $attackTroops *= random(1,3)>>
		<<elseif $week < 90>>
			<<set $attackTroops *= random(2,3)>>
		<<elseif $week < 120>>
			<<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)>>
		<<elseif $week < 120>>
			<<set $attackEquip = random(1,3)>>
		<<else>>
			<<set $attackEquip = random(2,4)>>
		<</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>>

/* 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>>
			<<set $attackTroops *= random(4,6)>>
			<<set $attackEquip = either(3,4)>>
			<<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>>
	<</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>>	/* 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 $securityForceSatalitePower >= 11>>
			<<set _atkMod += ($securityForceSatalitePower - 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)>>
		<<if $woundType == 1>>
			<<set $Bodyguard.voice = 0>>
		<<elseif $woundType == 2>>
			<<set $Bodyguard.eyes = -2>>
		<<elseif $woundType == 3>>
			<<set $Bodyguard.amp = 1>>
		<<elseif $woundType >= 4>>
			<<if $Bodyguard.health >= -60>>
				<<set $Bodyguard.health -= 30>>
			<<else>>
				<<set $Bodyguard.health -= Math.abs(90 - $Bodyguard.health)>>
			<</if>>
		<</if>>
	<</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)>>
		<<if $woundType == 1>>
			<<set $HeadGirl.voice = 0>>
		<<elseif $woundType == 2>>
			<<set $HeadGirl.eyes = -2>>
		<<elseif $woundType == 3>>
			<<set $HeadGirl.amp = 1>>
		<<elseif $woundType >= 4>>
			<<set $HeadGirl.health -= 40>>
		<</if>>
	<</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) * _atkMod>>
		<<set _defense += ($militiaBaseDefense + $militiaBaseDefense * $militiaUnits[_i].equip * $equipMod + $militiaBaseDefense * _expBonus + $militiaBaseDefense * _loyaltyBonus + $militiaBaseDefense * $militiaUnits[_i].SF * 0.20) * _defMod>>
		<<set _hp += ($militiaBaseHp + $militiaBaseHp * $militiaUnits[_i].medics * $equipMod) * $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) * _atkMod>>
		<<set _defense += ($slaveBaseDefense + $slaveBaseDefense * $slaveUnits[_i].equip * $equipMod + $slaveBaseDefense * _expBonus + $slaveBaseDefense * _loyaltyBonus + $slaveBaseDefense * $slaveUnits[_i].SF * 0.20) * _defMod>>
		<<set _hp += ($slaveBaseHp + $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) * _atkMod>>
		<<set _defense += ($mercBaseDefense + $mercBaseDefense * $mercUnits[_i].equip * $equipMod + $mercBaseDefense * _expBonus + $mercBaseDefense * _loyaltyBonus + $mercBaseDefense * $mercUnits[_i].SF * 0.20) * _defMod>>
		<<set _hp += ($mercBaseHp + $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 / 200,1,10)>>

<<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 / 400,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 */