code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" version="1.1" x="0px" y="0px" viewBox="0 0 1000 1000" style="enable-background:new 0 0 1000 1000;" xml:space="preserve" id="svg4356" sodipodi:docname="vector source.svg" inkscape:version="0.91 r13725">
  <metadata id="metadata4362">
    <rdf:RDF>
      <cc:Work rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
        <dc:title/>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <defs id="defs4360"/>
  <sodipodi:namedview pagecolor="#ffffff" bordercolor="#666666" borderopacity="1" objecttolerance="10" gridtolerance="10" guidetolerance="10" inkscape:pageopacity="0" inkscape:pageshadow="2" inkscape:window-width="1280" inkscape:window-height="952" id="namedview4358" showgrid="false" inkscape:zoom="4.808326" inkscape:cx="531.76051" inkscape:cy="751.71421" inkscape:window-x="0" inkscape:window-y="0" inkscape:window-maximized="1" inkscape:current-layer="Boob_Large_3_" inkscape:object-nodes="true" inkscape:object-paths="false" inkscape:snap-smooth-nodes="true" inkscape:snap-object-midpoints="true"/>
  <style type="text/css" id="style3680">
	.st0{fill:#FFFFFF;}
	.st1{fill:#76C043;}
	.st2{fill:#2680BC;}
	.st3{fill:#F6E0E8;}
	.st4{fill:#515351;}
	.st5{fill:#F4F1A3;}
	.st6{fill:#8E4F21;}
	.st7{fill:#BC2027;}
	.st8{fill:#60BB46;}
	.st9{fill:#4686C6;}
	.st10{fill:#D28DBD;}
	.st11{fill:#3F403F;}
	.st12{fill:#010101;}
	.st13{fill:#8F8F8E;}
	.st14{fill:#515251;}
	.st15{fill:#4F6AB2;}
	.st16{fill:#3E65B0;}
	.st17{fill:#15406D;}
	.st18{fill:#65AD45;}
	.st19{fill:#787878;}
	.st20{fill:#D76B93;}
	.st21{fill:#4DB748;}
	.st22{opacity:0.24;fill:#BF2126;enable-background:new    ;}
	.st23{fill:#070505;}
	.st24{fill:#BF2126;}
</style>
  <svg:g inkscape:groupmode="layer" id="Torso_Hourglass_straps" inkscape:label="Torso_Hourglass_straps" style="display:inline">
    <svg:path style="display:inline;fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" d="m 500.97796,204.16551 c -0.71264,4.69149 3.72511,8.09539 9.22204,11.23449 12.56584,-0.1687 25.13169,-3.25605 37.69753,-9.69857" id="path9218" inkscape:connector-curvature="0" sodipodi:nodetypes="ccc"/>
    <svg:path id="path9222" style="display:inline;fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:3;stroke-dasharray:none;stroke-opacity:1" d="m 495.8,467.5 -14.5431,-61.0527 4.91522,-55.28426 5.08461,-71.0825 L 510.2,215.4 530.35082,279.04329 523.449,317.775 508.76009,353.35994 481.2569,406.4473 471.9861,352.02373 461.91384,309.87756 468.81988,275.6 510.2,215.4" inkscape:connector-curvature="0" sodipodi:nodetypes="ccccccccccccc"/>
    <svg:path style="display:inline;fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" d="m 463.2324,352.85095 8.7537,-0.82722 14.18602,-0.86069 22.58797,2.1969 37.46362,7.77538" id="path9224" inkscape:connector-curvature="0" sodipodi:nodetypes="ccccc"/>
    <svg:path style="display:inline;fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" d="m 460.59329,375.71105 c 8.26177,10.3193 15.11821,15.52324 20.66361,30.73625 14.56337,-16.56115 48.05529,-16.49869 68.44366,-26.50672" id="path9226" inkscape:connector-curvature="0" sodipodi:nodetypes="ccc"/>
    <svg:path id="path4809" style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" d="m 587.45053,268.9835 -27.76453,10.0025 -29.33518,0.0573 -39.09409,1.03724 -22.43685,-4.48053 -8.85992,-2.64861" inkscape:connector-curvature="0" sodipodi:nodetypes="cccccc"/>
  </svg:g>
</svg>


code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" version="1.1" x="0px" y="0px" viewBox="0 0 1000 1000" style="enable-background:new 0 0 1000 1000;" xml:space="preserve" id="svg4356" sodipodi:docname="vector source.svg" inkscape:version="0.91 r13725">
  <metadata id="metadata4362">
    <rdf:RDF>
      <cc:Work rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
        <dc:title/>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <defs id="defs4360"/>
  <sodipodi:namedview pagecolor="#ffffff" bordercolor="#666666" borderopacity="1" objecttolerance="10" gridtolerance="10" guidetolerance="10" inkscape:pageopacity="0" inkscape:pageshadow="2" inkscape:window-width="1280" inkscape:window-height="952" id="namedview4358" showgrid="false" inkscape:zoom="4.808326" inkscape:cx="531.76051" inkscape:cy="751.71421" inkscape:window-x="0" inkscape:window-y="0" inkscape:window-maximized="1" inkscape:current-layer="Boob_Large_3_" inkscape:object-nodes="true" inkscape:object-paths="false" inkscape:snap-smooth-nodes="true" inkscape:snap-object-midpoints="true"/>
  <style type="text/css" id="style3680">
	.st0{fill:#FFFFFF;}
	.st1{fill:#76C043;}
	.st2{fill:#2680BC;}
	.st3{fill:#F6E0E8;}
	.st4{fill:#515351;}
	.st5{fill:#F4F1A3;}
	.st6{fill:#8E4F21;}
	.st7{fill:#BC2027;}
	.st8{fill:#60BB46;}
	.st9{fill:#4686C6;}
	.st10{fill:#D28DBD;}
	.st11{fill:#3F403F;}
	.st12{fill:#010101;}
	.st13{fill:#8F8F8E;}
	.st14{fill:#515251;}
	.st15{fill:#4F6AB2;}
	.st16{fill:#3E65B0;}
	.st17{fill:#15406D;}
	.st18{fill:#65AD45;}
	.st19{fill:#787878;}
	.st20{fill:#D76B93;}
	.st21{fill:#4DB748;}
	.st22{opacity:0.24;fill:#BF2126;enable-background:new    ;}
	.st23{fill:#070505;}
	.st24{fill:#BF2126;}
</style>
  <svg:g inkscape:groupmode="layer" id="Torso_Normal_straps" inkscape:label="Torso_Normal_straps" style="display:inline">
    <svg:path style="display:inline;fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" d="M 500.903,204.256 C 500.191,208.948 504.628,212.352 510.125,215.491 C 522.07,215.33 533.649,211.074 545.594,204.95" id="path9218" inkscape:connector-curvature="0" sodipodi:nodetypes="ccc"/>
    <svg:path id="path9222" style="display:inline;fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:3;stroke-dasharray:none;stroke-opacity:1" d="M 492.779,466.2 L 479.154,404.326 L 481.969,348.317 L 491.442,278.56 L 510.125,215.491 L 531.396,274.432 L 535.475,313.426 L 525.004,352.471 L 479.154,404.326 L 467.783,349.178 L 461.718,308.021 L 469.006,274.079 L 510.125,215.491" inkscape:connector-curvature="0" sodipodi:nodetypes="ccccccccccccc"/>
    <svg:path style="display:inline;fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" d="M 459.029,350.005 L 467.783,349.178 L 481.969,348.317 L 525.004,352.471 L 562.468,360.247" id="path9224" inkscape:connector-curvature="0" sodipodi:nodetypes="ccccc"/>
    <svg:path style="display:inline;fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" d="M 458.065,372.805 C 466.525,383.372 473.476,388.748 479.154,404.326 C 495.782,385.418 536.776,393.898 560.054,382.472" id="path9226" inkscape:connector-curvature="0" sodipodi:nodetypes="ccc"/>
    <svg:path id="path4809" style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" d="M 587.622,264.803 L 560.344,274.43 L 531.396,274.432 L 491.442,278.56 L 469.006,274.079 L 460.146,271.43" inkscape:connector-curvature="0" sodipodi:nodetypes="cccccc"/>
  </svg:g>
</svg>


code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" version="1.1" x="0px" y="0px" viewBox="0 0 1000 1000" style="enable-background:new 0 0 1000 1000;" xml:space="preserve" id="svg4356" sodipodi:docname="vector source.svg" inkscape:version="0.91 r13725">
  <metadata id="metadata4362">
    <rdf:RDF>
      <cc:Work rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
        <dc:title/>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <defs id="defs4360"/>
  <sodipodi:namedview pagecolor="#ffffff" bordercolor="#666666" borderopacity="1" objecttolerance="10" gridtolerance="10" guidetolerance="10" inkscape:pageopacity="0" inkscape:pageshadow="2" inkscape:window-width="1280" inkscape:window-height="952" id="namedview4358" showgrid="false" inkscape:zoom="4.808326" inkscape:cx="531.76051" inkscape:cy="751.71421" inkscape:window-x="0" inkscape:window-y="0" inkscape:window-maximized="1" inkscape:current-layer="Boob_Large_3_" inkscape:object-nodes="true" inkscape:object-paths="false" inkscape:snap-smooth-nodes="true" inkscape:snap-object-midpoints="true"/>
  <style type="text/css" id="style3680">
	.st0{fill:#FFFFFF;}
	.st1{fill:#76C043;}
	.st2{fill:#2680BC;}
	.st3{fill:#F6E0E8;}
	.st4{fill:#515351;}
	.st5{fill:#F4F1A3;}
	.st6{fill:#8E4F21;}
	.st7{fill:#BC2027;}
	.st8{fill:#60BB46;}
	.st9{fill:#4686C6;}
	.st10{fill:#D28DBD;}
	.st11{fill:#3F403F;}
	.st12{fill:#010101;}
	.st13{fill:#8F8F8E;}
	.st14{fill:#515251;}
	.st15{fill:#4F6AB2;}
	.st16{fill:#3E65B0;}
	.st17{fill:#15406D;}
	.st18{fill:#65AD45;}
	.st19{fill:#787878;}
	.st20{fill:#D76B93;}
	.st21{fill:#4DB748;}
	.st22{opacity:0.24;fill:#BF2126;enable-background:new    ;}
	.st23{fill:#070505;}
	.st24{fill:#BF2126;}
</style>
  <svg:g inkscape:groupmode="layer" id="Torso_Unnatural_straps" inkscape:label="Torso_Unnatural_straps" style="display:inline">
    <svg:path style="display:inline;fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" d="M 501.152,204.07 C 500.44,208.762 504.877,212.166 510.374,215.305 C 523.664,215.126 537.25,213.091 550.54,206.278" id="path9218" inkscape:connector-curvature="0" sodipodi:nodetypes="ccc"/>
    <svg:path id="path9222" style="display:inline;fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:3;stroke-dasharray:none;stroke-opacity:1" d="M 496.678,465.98 L 482.188,407.3 L 491.521,353.918 L 491.181,281.745 L 510.374,215.305 L 528.877,283.779 L 512.809,320.263 L 498.229,354.208 L 482.188,407.3 L 477.335,354.779 L 462.265,311.869 L 468.744,277.264 L 510.374,215.305" inkscape:connector-curvature="0" sodipodi:nodetypes="ccccccccccccc"/>
    <svg:path style="display:inline;fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" d="M 468.581,355.606 L 477.335,354.779 L 491.521,353.918 L 498.229,354.208 L 535.693,361.984" id="path9224" inkscape:connector-curvature="0" sodipodi:nodetypes="ccccc"/>
    <svg:path style="display:inline;fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" d="M 465.026,377.931 C 472.614,387.409 477.095,393.328 482.188,407.3 C 495.417,392.256 523.809,387.614 542.329,378.524" id="path9226" inkscape:connector-curvature="0" sodipodi:nodetypes="ccc"/>
    <svg:path id="path4809" style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" d="M 586.895,273.468 L 558.608,283.791 L 528.877,283.779 L 491.181,281.745 L 468.744,277.264 L 459.884,274.615" inkscape:connector-curvature="0" sodipodi:nodetypes="cccccc"/>
  </svg:g>
</svg>


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\|activeCanine\|activeHooved\|activeFeline\)" -- "src/*" | myprint "WrongCapitilization"
$GREP  "\(csae\|[a-z] She \|attepmts\|youreslf\|advnaces\|canAcheive\|setBellySize\|SetbellySize\|setbellySize\|bellypreg\|pregBelly\|bellyimplant\|bellyfluid\|pronounCaps\|carress\|hormonebalance\)" -- 'src/*' | myprint "SpellCheck"
$GREP  "\(recieve\|recieves\)" -- 'src/*' | myprint "PregmodderCannotSpellReceive"
$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\|training\)' -- '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 "Slave Slave " --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 -a -i -vE "\Wa (un|eu|us|ut|on|ur|in)." | grep -a -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 -a -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:
:: Dinner Party Preparations [nobr]

<<set $nextButton = "Cancel The Event", $nextLink = "Main">>

Hosting of high society dinner parties will increase your prestige significantly and it is expected for someone of your station. Since there is a lack of animal meat, human meat is served at these events to illustrate the wealth and power of the host. The success of the evening is judged by how well the human offering is prepared. Guests to these events are encouraged to rate the dishes and special dishes are expected. If the host receives 5 stars on all the dishes, they will receive the coveted title "<<if $PC.title == 1>>Master<<else>>Mistress<</if>> of The Culinary Arts". You ask $assistantName to show you a list of cooking instructions for those dishes.

Cooking Instructions and Recipes:

''Roast Long Pig'' – Made with the meat of a human roasted on a spit. Young meat is better than old meat. There should be some amount of fat to make the meat juicy, but not too much fat that makes the meat greasy. Muscles affect the texture of the meat. Meat without muscles lacks texture while an excess will be tough and hard to chew. Remember you can't eat silicone, and virgin meat is highly sought after. If the human is pregnant a veal dish can also be made from the fetus.

''Dicky Roll'' – An erect penis made into a spring roll. For best results harvest the penis at the moment of ejaculation. The size of the penis and the amount of accumulated ejaculate all affect the quality of the dish. Too big of a dick and too thin of semen can both ruin a dish.

''Testy Meat Balls'' – Testicles made into meat balls served with a sauce. Size matters.

''Titty Tartare'' – Made from finely chopped breasts, mixed with onions, capers and seasonings, served raw. Big breasts make the best quality meat for this dish. To add a hint of creamy taste, make sure the breasts are lactating. Remember you can't eat silicone.

''Camel Toe à l'Orange'' – Pussy grilled like a steak. A big clit and big labia will improve the quality of the dish.

Your assistant will take care of the invitations and all the arrangements; all you need to do is pick the meat.

__Select Your Meat:__

<<assignmentFilter>>

	<<showallAssignmentFilter>>
	<<include "Slave Summary">>
	<<resetAssignmentFilter>>


code:
:: SecExpBackwardCompatibility [nobr]

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

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

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

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

/* buildings */
<<if ndef $secHQ>>
	<<set $secHQ = 0>>
<</if>>
<<if ndef $secBarracks>>
	<<set $secBarracks = 0>>
<</if>>
<<if ndef $secBarracksUpgrades>>
	<<set $secBarracksUpgrades ={
	size: 0,
	luxury: 0,
	training: 0,
	loyaltyMod:0 }>>
<</if>>
<<if ndef $secBarracksUpgrades.loyaltyMod>>
	<<set $secBarracksUpgrades.loyaltyMod = 0>>
<</if>>
<<if ndef $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 $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 $slaveVictories>>
	<<set $slaveVictories = []>>
<</if>>
<<if ndef $slaveIncreasedPrestige>>
	<<set $slaveIncreasedPrestige = 0>>
<</if>>
<<if ndef $totalKills>>
	<<set $totalKills = 0>>
<</if>>
<<if ndef $battlesCount>>
	<<set $battlesCount = 0>>
<</if>>
<<if ndef $majorBattlesCount>>
	<<set $majorBattlesCount = 0>>
<</if>>
<<if ndef $chosenTactic>>
	<<set $chosenTactic = "none">>
<</if>>
<<if ndef $leadingTroops>>
	<<set $leadingTroops = "none">>
<</if>>
<<if ndef $attackTroops>>
	<<set $attackTroops = 0>>
<</if>>
<<if ndef $attackEquip>>
	<<set $attackEquip = 0>>
<</if>>
<<if ndef $deployableUnits>>
	<<set $deployableUnits = 0>>
<</if>>
<<if ndef $deployedUnits>>
	<<set $deployedUnits = 0>>
<</if>>
<<if ndef $deployingBots>>
	<<set $deployingBots = 0>>
<</if>>
<<if ndef $deployingMilitia>>
	<<set $deployingMilitia = 0>>
<</if>>
<<if ndef $deployingSlaves>>
	<<set $deployingSlaves = 0>>
<</if>>
<<if ndef $deployingMercs>>
	<<set $deployingMercs = 0>>
<</if>>
<<if ndef $battleTerrain>>
	<<set $battleTerrain = "none">>
<</if>>
<<if ndef $maxTurns>>
	<<set $maxTurns = 10>>
<</if>>
<<if ndef $battleResult>>
	<<set $battleResult = 4>>				/* sets $battleResult value outside accepted range (-3,3) to avoid evaluation problems */
<</if>>
<<if ndef $losses>>
	<<set $losses = 0>>
<</if>>
<<if ndef $enemyLosses>>
	<<set $enemyLosses = 0>>
<</if>>
<<if ndef $battleTurns>>
	<<set $battleTurns = 0>>
<</if>>
<<if ndef $bribeCost>>
	<<set $bribeCost = 0>>
<</if>>
<<if ndef $tacticsSuccessful>>
	<<set $tacticsSuccessful = 0>>
<</if>>
<<if ndef $leaderWounded>>
	<<set $leaderWounded = 0>>				/* 0=no wound, 1=mute, 2=blind, 3=amputee, 4=health */
<</if>>
<<if ndef $gainedCombat>>
	<<set $gainedCombat = 0>>
<</if>>
<<if ndef $gainedWarfare>>
	<<set $gainedWarfare = 0>>
<</if>>
<<if ndef $PCWounded>>
	<<set $PCWounded = 0>>
<</if>>
<<if ndef $PCWoundCooldown>>
	<<set $PCWoundCooldown = 0>>
<</if>>
<<if ndef $expectedEquip>>
	<<set $expectedEquip = 0>>
<</if>>
<<if ndef $estimatedMen>>
	<<set $estimatedMen = 0>>
<</if>>
<<if ndef $SFIntervention>>
	<<set $SFIntervention = 0>>
<</if>>
<<if ndef $carriableSoldiers>>
	<<set $carriableSoldiers = 0>>
<</if>>
<<if ndef $SFatk>>
	<<set $SFatk = 0>>
<</if>>
<<if ndef $SFdef>>
	<<set $SFdef = 0>>
<</if>>
<<if ndef $SFhp>>
	<<set $SFhp = 0>>
<</if>>
<<if ndef $rebellingID>>
	<<set $rebellingID = []>>
<</if>>
<<if ndef $saveValid>>
	<<set $saveValid = 0>>
<</if>>
<<if ndef $lastSelection>>
	<<set $lastSelection = []>>
<</if>>

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

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

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

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

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

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

<<set $wasToggledBefore = 1>>

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


code:
:: attackGenerator [nobr]

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

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

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

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

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

	/* terrain */
	<<if $terrain == "urban">>
		<<set $battleTerrain = either("outskirts","urban","wasteland")>>
	<<elseif $terrain == "rural">>
		<<set $battleTerrain = either("hills","outskirts","rural","wasteland")>>
	<<elseif $terrain == "ravine">>
		<<set $battleTerrain = either("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>>
			<<if $SF.Toggle && $SF.Active >= 1>>
				<<set $attackTroops = Math.trunc($attackTroops * random(4,6) * $majorBattleMult)>>
				<<set $attackEquip = either(3,4)>>
			<<else>>
				<<set $attackTroops = Math.trunc($attackTroops * random(2,3) * $majorBattleMult)>>
				<<set $attackEquip = either(2,3,4)>>
			<</if>>
			<<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 || $battleResult == -1>>	/* bribery/surrender check */
	<<if $showBattleStatistics == 1>>
		bribery chosen
	<<else>>
		Surrender chosen
	<</if>>
	<<if $battleResult == 1>>
		<<if $cash >= $bribeCost>>						/* if there's enough cash there's a 10% chance bribery fails. If there isn't there's instead a 50% chance it fails */
			<<if $attackType == "freedom fighters">>
				<<if random(1,100) <= 50>>
					<<set $battleResult = 0>>
				<</if>>
			<<elseif random(1,100) <= 10>>
				<<set $battleResult = 0>>
			<</if>>
		<<else>>
			<<if random(1,100) <= 50>>
				<<set $battleResult = 0>>
			<</if>>
		<</if>>
		<<if $showBattleStatistics == 1>>
			<<if $battleResult == 0>>

Bribery failed!
			<<else>>

Bribery Successful!
			<</if>>

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

<<else>>

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

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

/* Leaders */
<<if $leadingTroops == "PC">>
	<<if $authority <= 2500 && $authority > 1000>>
		<<set _slaveMod -= 0.10>>
	<<elseif $authority <= 1000>>
		<<set _slaveMod -= 0.25>>
	<<elseif $authority >= 5000 && $authority < 15000>>
		<<set _slaveMod += 0.10>>
	<<elseif $authority >= 15000>>
		<<set _slaveMod += 0.25>>
	<</if>>
	<<if $PC.career == "escort" || $PC.career == "servant">>
		<<set _slaveMod += 0.10>>
	<<elseif $PC.career == "slaver">>
		<<set _slaveMod -= 0.10>>
	<</if>>
	<<if $rep <= 2500 && $rep > 1000>>
		<<set _militiaMod -= 0.10>>
	<<elseif $rep <= 1000>>
		<<set _militiaMod -= 0.25>>
	<<elseif $rep >= 5000 && $rep < 15000>>
		<<set _militiaMod += 0.10>>
	<<elseif $rep >= 15000>>
		<<set _militiaMod += 0.25>>
	<</if>>
	<<if $PC.career == "celebrity" || $PC.career == "capitalist">>
		<<set _militiaMod += 0.10>>
	<<elseif $PC.career == "gang" || $PC.career == "escort">>
		<<set _militiaMod -= 0.10>>
	<</if>>
	<<if $PC.career == "mercenary" || $PC.warfare > 75>>
		<<set _mercMod += 0.10>>
		<<set _SFMod += 0.10>>
	<<elseif $PC.career == "wealth" || $PC.career == "servant">>
		<<set _mercMod -= 0.10>>
		<<set _SFMod -= 0.10>>
	<</if>>
	<<if $rep >= 15000>>
		<<set _enemyMod -= 0.10>>
	<</if>>
	<<if $PC.warfare <= 25 && $PC.warfare > 10>>
		<<set _atkMod -= 0.15>>
		<<set _tacChance -= 0.15>>
	<<elseif $PC.warfare <= 10>>
		<<set _atkMod -= 0.20>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.30>>
	<<elseif $PC.warfare >= 50 && $PC.warfare >= 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+$Bodyguard.intelligenceImplant > 95>>
		<<set _atkMod += 0.25>>
		<<set _defMod += 0.25>>
		<<set _tacChance += 0.50>>
	<<elseif $Bodyguard.intelligence+$Bodyguard.intelligenceImplant > 95>>
		<<set _atkMod += 0.20>>
		<<set _defMod += 0.15>>
		<<set _tacChance += 0.35>>
	<<elseif (setup.bodyguardCareers.includes($Bodyguard.career) || setup.HGCareers.includes($Bodyguard.career)) && $Bodyguard.intelligence+$Bodyguard.intelligenceImplant > 50>>
		<<set _atkMod += 0.15>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.25>>
	<<elseif $Bodyguard.intelligence+$Bodyguard.intelligenceImplant > 50>>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.20>>
	<<elseif (setup.bodyguardCareers.includes($Bodyguard.career) || setup.HGCareers.includes($Bodyguard.career)) && $Bodyguard.intelligence+$Bodyguard.intelligenceImplant > 15>>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.05>>
		<<set _tacChance += 0.15>>
	<<elseif $Bodyguard.intelligence+$Bodyguard.intelligenceImplant > 15>>
		<<set _atkMod += 0.5>>
		<<set _defMod += 0.05>>
		<<set _tacChance += 0.10>>
	<<elseif !(setup.bodyguardCareers.includes($Bodyguard.career) && setup.HGCareers.includes($Bodyguard.career)) || $Bodyguard.intelligence+$Bodyguard.intelligenceImplant < -50>>
		<<set _atkMod -= 0.15>>
		<<set _defMod -= 0.15>>
		<<set _tacChance -= 0.30>>
	<<elseif $Bodyguard.intelligence+$Bodyguard.intelligenceImplant < -50>>
		<<set _atkMod -= 0.15>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.25>>
	<<elseif !(setup.bodyguardCareers.includes($Bodyguard.career) && setup.HGCareers.includes($Bodyguard.career)) || $Bodyguard.intelligence+$Bodyguard.intelligenceImplant < -15>>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.20>>
	<<elseif $Bodyguard.intelligence+$Bodyguard.intelligenceImplant < -15>>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.05>>
		<<set _tacChance -= 0.15>>
	<</if>>
		/* does she get wounded? */
	<<if $Bodyguard.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.belly >= 10000>>
		<<set _woundChance += 1>>
	<</if>>
	<<if $Bodyguard.dick >= 8>>
		<<set _woundChance += 1>>
	<</if>>
	<<if $Bodyguard.balls >= 8>>
		<<set _woundChance += 1>>
	<</if>>
	<<if $Bodyguard.intelligence+$Bodyguard.intelligenceImplant < -95>>
		<<set _woundChance += 1>>
	<</if>>
	<<if random(1,100) <= _woundChance>>
		<<set $leaderWounded = 1>>
		<<set _militiaMod -= 0.2>>
		<<set _slaveMod -= 0.2>>
		<<set _mercMod -= 0.2>>
		<<set _SFMod -= 0.2>>
		<<set _enemyMod += 0.2>>
		<<set $woundType = random(1,10)>>
		<<set _i = $slaveIndices[$Bodyguard.ID]>>
		<<if $woundType == 1>>
			<<set $slaves[_i].voice = 0>>
			<<set $slaves[_i].health -= 60>>
		<<elseif $woundType == 2>>
			<<set $slaves[_i].eyes = -2>>
			<<set $slaves[_i].health -= 30>>
		<<elseif $woundType == 3>>
			<<set $slaves[_i].amp = 1>>
			<<set $slaves[_i].health = -80>>
		<<elseif $woundType >= 4>>
			<<if $slaves[_i].health >= -60>>
				<<set $slaves[_i].health -= 30>>
			<<else>>
				<<set $slaves[_i].health = -90>>
			<</if>>
		<</if>>
	<</if>>
	/* 60% chance of getting combat skill if not already have it */
	<<if $Bodyguard.combatSkill == 0 && random(1,100) <= 60>>
		<<set $gainedCombat = 1>>
		<<set _i = $slaveIndices[$Bodyguard.ID]>>
		<<set $slaves[_i].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+$HeadGirl.intelligenceImplant > 95>>
		<<set _atkMod += 0.25>>
		<<set _defMod += 0.25>>
		<<set _tacChance += 0.50>>
	<<elseif $HeadGirl.intelligence+$HeadGirl.intelligenceImplant > 95>>
		<<set _atkMod += 0.20>>
		<<set _defMod += 0.15>>
		<<set _tacChance += 0.35>>
	<<elseif (setup.bodyguardCareers.includes($HeadGirl.career) || setup.HGCareers.includes($HeadGirl.career)) && $HeadGirl.intelligence+$HeadGirl.intelligenceImplant > 50>>
		<<set _atkMod += 0.15>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.25>>
	<<elseif $HeadGirl.intelligence+$HeadGirl.intelligenceImplant > 50>>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.20>>
	<<elseif (setup.bodyguardCareers.includes($HeadGirl.career) || setup.HGCareers.includes($HeadGirl.career)) && $HeadGirl.intelligence+$HeadGirl.intelligenceImplant > 15>>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.05>>
		<<set _tacChance += 0.15>>
	<<elseif $HeadGirl.intelligence+$HeadGirl.intelligenceImplant > 15>>
		<<set _atkMod += 0.05>>
		<<set _defMod += 0.05>>
		<<set _tacChance += 0.10>>
	<<elseif !(setup.bodyguardCareers.includes($HeadGirl.career) && setup.HGCareers.includes($HeadGirl.career)) || $HeadGirl.intelligence+$HeadGirl.intelligenceImplant < -50>>
		<<set _atkMod -= 0.15>>
		<<set _defMod -= 0.15>>
		<<set _tacChance -= 0.30>>
	<<elseif $HeadGirl.intelligence+$HeadGirl.intelligenceImplant < -50>>
		<<set _atkMod -= 0.15>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.25>>
	<<elseif !(setup.bodyguardCareers.includes($HeadGirl.career) && setup.HGCareers.includes($HeadGirl.career)) || $HeadGirl.intelligence+$HeadGirl.intelligenceImplant < -15>>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.20>>
	<<elseif $HeadGirl.intelligence+$HeadGirl.intelligenceImplant < -15>>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.05>>
		<<set _tacChance -= 0.15>>
	<</if>>
	/* does she get wounded? */
	<<if $HeadGirl.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.belly >= 10000>>
		<<set _woundChance += 1>>
	<</if>>
	<<if $HeadGirl.dick >= 8>>
		<<set _woundChance += 1>>
	<</if>>
	<<if $HeadGirl.balls >= 8>>
		<<set _woundChance += 1>>
	<</if>>
	<<if $HeadGirl.intelligence+$HeadGirl.intelligenceImplant < -95>>
		<<set _woundChance += 1>>
	<</if>>
	<<if random(1,100) <= _woundChance>>
		<<set $leaderWounded = 1>>
		<<set _militiaMod -= 0.2>>
		<<set _slaveMod -= 0.2>>
		<<set _mercMod -= 0.2>>
		<<set _SFMod -= 0.2>>
		<<set _enemyMod += 0.2>>
		<<set $woundType = random(1,10)>>
		<<set _i = $slaveIndices[$HeadGirl.ID]>>
		<<if $woundType == 1>>
			<<set $slaves[_i].voice = 0>>
			<<set $slaves[_i].health -= 60>>
		<<elseif $woundType == 2>>
			<<set $slaves[_i].eyes = -2>>
			<<set $slaves[_i].health -= 30>>
		<<elseif $woundType == 3>>
			<<set $slaves[_i].amp = 1>>
			<<set $slaves[_i].health = -80>>
		<<elseif $woundType >= 4>>
			<<if $slaves[_i].health >= -60>>
				<<set $slaves[_i].health -= 30>>
			<<else>>
				<<set $slaves[_i].health = -90>>
			<</if>>
		<</if>>
	<</if>>
	/* 60% chance of getting combat skill if not already have it */
	<<if $HeadGirl.combatSkill == 0 && random(1,100) <= 60>>
		<<set $gainedCombat = 1>>
		<<set _i = $slaveIndices[$HeadGirl.ID]>>
		<<set $slaves[_i].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 Maneuver">>
		<<set _atkMod -= 0.05>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.15>>
	<<elseif $chosenTactic == "Defense In Depth">>
		<<set _atkMod -= 0.05>>
		<<set _defMod -= 0.05>>
		<<set _tacChance -= 0.10>>
	<<elseif $chosenTactic == "Blitzkrieg">>
		<<set _atkMod -= 0.15>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.25>>
	<<elseif $chosenTactic == "Human Wave">>
		<<set _atkMod -= 0.15>>
		<<set _defMod -= 0.15>>
		<<set _tacChance -= 0.30>>
	<</if>>
<<elseif $battleTerrain == "rural">>
	<<if $chosenTactic == "Bait and Bleed">>
		<<set _atkMod -= 0.05>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.15>>
	<<elseif $chosenTactic == "Guerrilla">>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.20>>
	<<elseif $chosenTactic == "Choke Points">>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.15>>
		<<set _tacChance -= 0.25>>
	<<elseif $chosenTactic == "Interior Lines">>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.15>>
		<<set _tacChance += 0.25>>
	<<elseif $chosenTactic == "Pincer Maneuver">>
		<<set _atkMod += 0.15>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.25>>
	<<elseif $chosenTactic == "Defense In Depth">>
		<<set _atkMod += 0.15>>
		<<set _defMod += 0.15>>
		<<set _tacChance += 0.30>>
	<<elseif $chosenTactic == "Blitzkrieg">>
		<<set _atkMod += 0.15>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.25>>
	<<elseif $chosenTactic == "Human Wave">>
		<<set _atkMod += 0.20>>
		<<set _defMod += 0.05>>
		<<set _tacChance += 0.25>>
	<</if>>
<<elseif $battleTerrain == "hills">>
	<<if $chosenTactic == "Bait and Bleed">>
		<<set _atkMod += 0.05>>
		<<set _defMod += 0.05>>
		<<set _tacChance += 0.10>>
	<<elseif $chosenTactic == "Guerrilla">>
		<<set _atkMod += 0.05>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.15>>
	<<elseif $chosenTactic == "Choke Points">>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.20>>
	<<elseif $chosenTactic == "Interior Lines">>
		<<set _atkMod -= 0.05>>
		<<set _defMod -= 0.05>>
		<<set _tacChance -= 0.10>>
	<<elseif $chosenTactic == "Pincer Maneuver">>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.05>>
		<<set _tacChance += 0.15>>
	<<elseif $chosenTactic == "Defense In Depth">>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.05>>
		<<set _tacChance -= 0.15>>
	<<elseif $chosenTactic == "Blitzkrieg">>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.15>>
		<<set _tacChance += 0.25>>
	<<elseif $chosenTactic == "Human Wave">>
		<<set _atkMod -= 0.15>>
		<<set _defMod -= 0.15>>
		<<set _tacChance -= 0.30>>
	<</if>>
<<elseif $battleTerrain == "coast">>
	<<if $chosenTactic == "Bait and Bleed">>
		<<set _atkMod -= 0.05>>
		<<set _defMod -= 0.05>>
		<<set _tacChance -= 0.10>>
	<<elseif $chosenTactic == "Guerrilla">>
		<<set _atkMod += 0.05>>
		<<set _defMod += 0.05>>
		<<set _tacChance += 0.10>>
	<<elseif $chosenTactic == "Choke Points">>
		<<set _atkMod += 0.05>>
		<<set _defMod += 0.15>>
		<<set _tacChance += 0.15>>
	<<elseif $chosenTactic == "Interior Lines">>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.20>>
	<<elseif $chosenTactic == "Pincer Maneuver">>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.20>>
	<<elseif $chosenTactic == "Defense In Depth">>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.20>>
	<<elseif $chosenTactic == "Blitzkrieg">>
		<<set _atkMod -= 0.05>>
		<<set _defMod -= 0.05>>
		<<set _tacChance -= 0.10>>
	<<elseif $chosenTactic == "Human Wave">>
		<<set _atkMod += 0.05>>
		<<set _defMod -= 0.05>>
	<</if>>
<<elseif $battleTerrain == "outskirts">>
	<<if $chosenTactic == "Bait and Bleed">>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.15>>
		<<set _tacChance -= 0.25>>
	<<elseif $chosenTactic == "Guerrilla">>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.05>>
		<<set _tacChance -= 0.15>>
	<<elseif $chosenTactic == "Choke Points">>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.15>>
		<<set _tacChance += 0.25>>
	<<elseif $chosenTactic == "Interior Lines">>
		<<set _atkMod += 0.15>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.25>>
	<<elseif $chosenTactic == "Pincer Maneuver">>
		<<set _atkMod += 0.05>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.15>>
	<<elseif $chosenTactic == "Defense In Depth">>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.15>>
		<<set _tacChance += 0.25>>
	<<elseif $chosenTactic == "Blitzkrieg">>
		<<set _atkMod += 0.10>>
		<<set _defMod -= 0.05>>
		<<set _tacChance += 0.05>>
	<<elseif $chosenTactic == "Human Wave">>
		<<set _atkMod += 0.10>>
		<<set _defMod -= 0.10>>
	<</if>>
<<elseif $battleTerrain == "mountains">>
	<<if $chosenTactic == "Bait and Bleed">>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.15>>
		<<set _tacChance += 0.25>>
	<<elseif $chosenTactic == "Guerrilla">>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.15>>
		<<set _tacChance += 0.25>>
	<<elseif $chosenTactic == "Choke Points">>
		<<set _atkMod += 0.05>>
		<<set _defMod += 0.20>>
		<<set _tacChance += 0.25>>
	<<elseif $chosenTactic == "Interior Lines">>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.20>>
	<<elseif $chosenTactic == "Pincer Maneuver">>
		<<set _atkMod += 0.10>>
		<<set _defMod -= 0.05>>
		<<set _tacChance += 0.05>>
	<<elseif $chosenTactic == "Defense In Depth">>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.20>>
	<<elseif $chosenTactic == "Blitzkrieg">>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.20>>
	<<elseif $chosenTactic == "Human Wave">>
		<<set _atkMod -= 0.10>>
		<<set _defMod -= 0.10>>
		<<set _tacChance -= 0.20>>
	<</if>>
<<elseif $battleTerrain == "wasteland">>
	<<if $chosenTactic == "Bait and Bleed">>
		<<set _atkMod += 0.05>>
		<<set _defMod += 0.05>>
		<<set _tacChance += 0.10>>
	<<elseif $chosenTactic == "Guerrilla">>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.05>>
		<<set _tacChance += 0.15>>
	<<elseif $chosenTactic == "Choke Points">>
		<<set _atkMod -= 0.10>>
		<<set _defMod += 0.05>>
		<<set _tacChance -= 0.05>>
	<<elseif $chosenTactic == "Interior Lines">>
		<<set _atkMod += 0.10>>
		<<set _defMod += 0.15>>
		<<set _tacChance += 0.25>>
	<<elseif $chosenTactic == "Pincer Maneuver">>
		<<set _atkMod += 0.15>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.25>>
	<<elseif $chosenTactic == "Defense In Depth">>
		<<set _atkMod += 0.05>>
		<<set _defMod += 0.10>>
		<<set _tacChance += 0.15>>
	<<elseif $chosenTactic == "Blitzkrieg">>
		<<set _atkMod += 0.15>>
		<<set _defMod += 0.15>>
		<<set _tacChance += 0.30>>
	<<elseif $chosenTactic == "Human Wave">>
		<<set _atkMod += 0.20>>
		<<set _defMod += 0.05>>
		<<set _tacChance += 0.25>>
	<</if>>
<</if>>

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

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

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

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

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

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

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

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

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

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

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

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

<<if isNaN(_attack)>>

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

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

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

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

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

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

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

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

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

__Difficulty__:

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

__Army__:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

__Tactics__:

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

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

__Enemy__:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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