<?xml version="1.0" ?>
<Equations>
	<!--================== Budgeting for the Empire ==================-->
	<!-- Fairly balanced with emphasis on offense -->
	<!-- Information budget kept low since probe droids are cheap -->

	<!-- Snowball budget - more money for offense as galactic domination comes closer -->
	<BasicEmpireOffensiveBudgetAllocation>
		5.0 
		+ 
		(10.0 * Variable_Self.PlanetsControlled)
		+
		(6.0 * (1.0 - Function_Has_Enough_Space_Force_Globally.Evaluate))
	</BasicEmpireOffensiveBudgetAllocation>

	<!-- Defensive budget
	We only care about land defenses if we have enough space forces
	AND
		SCALE if special structure defenses are needed
		ADD baseline amount for tactical defense spending
	-->
	<BasicEmpireDefensiveBudgetAllocation>
		Function_Has_Enough_Space_Force_Globally.Evaluate
		*
		(
			8.0 * Function_Can_Build_Defensive_Structures.Evaluate * (1.0 - Function_Has_Enough_Defensive_Structures.Evaluate)
			+
			2.0
		)
	</BasicEmpireDefensiveBudgetAllocation>


	<Can_Build_Defensive_Structures>
		(
			Variable_Self.IsFaction {Parameter_Faction = "Empire"} * 
			(Variable_Self.TechLevel >= 2) *
			Variable_Self.HasGroundbaseOfLevel {Parameter_Level = 2, Parameter_Count = 1}
		)
		+
		(
			Variable_Self.IsFaction {Parameter_Faction = "Rebel"} *
			(Variable_Self.TechLevel >= 2) *
			Variable_Self.HasGroundbaseOfLevel {Parameter_Level = 2, Parameter_Count = 1}
		)
		+
		(
			Variable_Self.IsFaction {Parameter_Faction = "Hutts"} *
			(Variable_Self.TechLevel >= 2) *
			Variable_Self.HasGroundbaseOfLevel {Parameter_Level = 2, Parameter_Count = 1}
		)
		>= 1
	</Can_Build_Defensive_Structures>

	<Defensive_Structure_Count>
		Variable_Self.StructureCount {	Parameter_Type = "E_Ground_Base_Shield_740"
													,Parameter_Type = "R_Ground_Base_Shield_740"
													,Parameter_Type = "H_Ground_Base_Shield_740"
													,Parameter_Type = "E_Galactic_Turbolaser_Tower_Defenses"
													,Parameter_Type = "R_Galactic_Turbolaser_Tower_Defenses"
													,Parameter_Type = "H_Galactic_Turbolaser_Tower_Defenses"
													,Parameter_Type = "Ground_Ion_Cannon"
													,Parameter_Type = "Ground_Empire_Hypervelocity_Gun"
													,Parameter_Type = "Ground_Hutts_Hypervelocity_Gun"
													,Parameter_Type = "E_Ground_Magnepulse_Cannon"
													,Parameter_Type = "Ground_Gravity_Generator"
													} 
	</Defensive_Structure_Count>

	<!-- Have we met the desired ratio of defensive structures to planets controlled?
	-->
	<Has_Enough_Defensive_Structures>
		(0.3 - (Function_Defensive_Structure_Count.Evaluate / Variable_Self.PlanetsControlledUnnormalized ))
		> 0.0
	</Has_Enough_Defensive_Structures>

	<!-- Increase with tech level: higher tech units mostly need high level bases.
		Also increase with maintenance: bases prevent maintenance costs 
		Decrease as bases are maxed out.
	 
		Note that The Major Item funds are diverted to Infrastructure if we are unable to tech up but want to.
		Infrastructure money should then be spent on upgrading the highest level starbase, lifting the dependency.
		-->
	<BasicEmpireInfrastructureBudgetAllocation>
		1.1 
		* 
		(2 + 0.5 * (Variable_Self.TechLevel) - Variable_Self.MaxedStarbases - Variable_Self.MaxedGroundbases) 
		* 
		(1.0 + Variable_Self.Maintenance)
		*
		(1.0 + 2.0 * (0.5 > (Variable_Self.StructureCount {Parameter_Type = "Empire_Ground_Mining_Facility_GC", Parameter_Type = "Empire_Ground_Mining_Facility_GC_Unlim", Parameter_Type = "Empire_Mineral_Extractor_GC"} / Variable_Self.PlanetsControlledUnnormalized)))
		+
		6 * (1 - Function_Can_Build_Offensive_Space.Evaluate)
		+
		10 * Function_Need_Tech_Upgrade_Unblocked.Evaluate
		+
		10 * ((Variable_Self.HasStructure {Parameter_Type = "E_Ground_Light_Vehicle_Factory"} == 0) +
				(Variable_Self.HasStructure {Parameter_Type = "E_Ground_Barracks"} == 0) +
				(Variable_Self.HasStructure {Parameter_Type = "Empire_Ground_Mining_Facility_GC"} == 0) +
				(Variable_Self.HasStructure {Parameter_Type = "Empire_Ground_Mining_Facility_GC_Unlim"} == 0) +
				(Variable_Self.HasStructure {Parameter_Type = "Empire_Mineral_Extractor_GC"} == 0))
	</BasicEmpireInfrastructureBudgetAllocation>

	<!-- Based on average age of scouted information -->
	<BasicEmpireInformationBudgetAllocation>
		0.5 * Function_Global_Scout_Need.Evaluate
	</BasicEmpireInformationBudgetAllocation>

	<!-- Receive a trickle of funds so that eventually, a major purchase is likely
		Adjust this to control how soon the AI will spend on tech upgrades.

		Scale this inversely with tech level and posession of the DeathStar (tech 6, in a sense)
		This is needed because during middle and late game, we need more funds for special structures, etc.
		Also, we don't want the AI to rush to max tech.

		Shut off funds if we're trying to upgrade a base in order to tech
		(the tech was already deemed affordable, and the reserve for it remains in the category)

		Shut off funds if we're behind in the space race but not behind in the tech race
		
		2.0 * Variable_Self.CanAdvanceTech
	-->
	<BasicEmpireMajorItemBudgetAllocation>
		0.8 
		* 
		(7.0 	- Variable_Self.TechLevel 
				- Variable_Self.HasUnit{Parameter_Type = "Death_Star_II"}
				- Variable_Self.HasStructure{Parameter_Type = "E_Ground_Research_Facility", Parameter_Count = 1})
		*
		(1.0 - Function_Need_Tech_Upgrade_Unblocked.Evaluate)
		*
		clamp(Function_Has_Enough_Space_Force_Globally.Evaluate + (Game.Timeline > Variable_Self.TechLevel), 0.0, 1.0)
		+
		3.0 * (Variable_Self.TechLevel == 5) * (1 - Variable_Self.HasUnit{Parameter_Type = "Death_Star_II"})
	</BasicEmpireMajorItemBudgetAllocation>

	<!-- Tech Upgrade Purchasing Budgets -->
	<!-- What fraction of the gross budget is required to support building the next tech item
		without pause, plus a small pad to compansate for fluctuations in spending that 
		may occur before this perception can compensate.

		add a buffer large enough to ensure that the upgrade can be afforded, despite cash spikes/drops
	-->
	<TechUpgradeBudgetRequirement>
		(
			(1 - Variable_Self.HasStructure{Parameter_Type = "E_Ground_Research_Facility", Parameter_Count = 1}) *
			Variable_Self.BudgetFractionToBuild{Parameter_Type = "E_Ground_Research_Facility"}
			+
			Variable_Self.HasStructure{Parameter_Type = "E_Ground_Research_Facility", Parameter_Count = 1} *
			(Variable_Self.TechLevel == 1) *
			Variable_Self.BudgetFractionToBuild{Parameter_Type = "DS_Primary_Hyperdrive"} 
			+
			(Variable_Self.TechLevel == 2) *
			Variable_Self.BudgetFractionToBuild{Parameter_Type = "DS_Shield_Gen"} 
			+
			(Variable_Self.TechLevel == 3) *
			Variable_Self.BudgetFractionToBuild{Parameter_Type = "DS_Superlaser_Core"} 
			+
			(Variable_Self.TechLevel == 4) *
			Variable_Self.BudgetFractionToBuild{Parameter_Type = "DS_Durasteel"} 
			+ 
			(Variable_Self.TechLevel == 5) *
			Variable_Self.BudgetFractionToBuild{Parameter_Type = "Death_Star_II"}
			*
			1.25
			+
			0.05
		)
	</TechUpgradeBudgetRequirement>

	<!-- Tech Upgrade Template Budget -->
	<EmpireTechUpgradeOffensiveAllocation>
		0.01
		+
		(1.0 - Function_TechUpgradeBudgetRequirement.Evaluate)
		*
		0.3
	</EmpireTechUpgradeOffensiveAllocation>

	<EmpireTechUpgradeDefensiveAllocation>
		(1.0 - Function_TechUpgradeBudgetRequirement.Evaluate)
		*
		0.3
		*
		Function_Can_Build_Defensive_Structures.Evaluate
	</EmpireTechUpgradeDefensiveAllocation>

	<!-- 
		Variable_Self.CanAdvanceTech 
		* 
	-->
	<EmpireTechUpgradeMajorItemAllocation>
		Function_TechUpgradeBudgetRequirement.Evaluate
	</EmpireTechUpgradeMajorItemAllocation>

	<!-- 
		(1.0 - Variable_Self.CanAdvanceTech) * Function_TechUpgradeBudgetRequirement.Evaluate
		+
	-->
	<EmpireTechUpgradeInfrastructureAllocation>
		(1.0 - Function_TechUpgradeBudgetRequirement.Evaluate)
		*
		0.2
	</EmpireTechUpgradeInfrastructureAllocation>

	<EmpireTechUpgradeInformationAllocation>
		(1.0 - Function_TechUpgradeBudgetRequirement.Evaluate)
		*
		0.0
	</EmpireTechUpgradeInformationAllocation>

	<!--================== Budgeting for the Rebels ==================-->
	<!-- Aggressive stance.  Low defense and information -->

	<!-- Snowball budget - more money for offense as galactic domination comes closer -->
	<BasicRebelOffensiveBudgetAllocation>
		7.2 + Variable_Self.PlanetsControlled
		+
		(6.0 * (1.0 - Function_Has_Enough_Space_Force_Globally.Evaluate))
	</BasicRebelOffensiveBudgetAllocation>
	
	<!-- Defensive budget increases with proportion of planets that are undefended -->
	<BasicRebelDefensiveBudgetAllocation>
		Function_Has_Enough_Space_Force_Globally.Evaluate
		*
		(
			8.0 * Function_Can_Build_Defensive_Structures.Evaluate * (1.0 - Function_Has_Enough_Defensive_Structures.Evaluate)
			+
			2.0
		)
	</BasicRebelDefensiveBudgetAllocation>
	
	<!-- Increase with tech level: higher tech units mostly need high level bases. -->
	<!-- Also increase with low income -->
	<!-- Decrease as bases are maxed out -->
	<BasicRebelInfrastructureBudgetAllocation>
		1.1 
		* 
		(2 + 0.5 * (Variable_Self.TechLevel) - Variable_Self.MaxedStarbases - Variable_Self.MaxedGroundbases) 
		* 
		(1.0 + Variable_Self.Maintenance)
		*
		(1.0 + 2.0 * (0.5 > (Variable_Self.StructureCount {Parameter_Type = "Rebel_Ground_Mining_Facility_GC", Parameter_Type = "Rebel_Ground_Mining_Facility_GC_Unlim", Parameter_Type = "Rebel_Mineral_Extractor_GC"} / Variable_Self.PlanetsControlledUnnormalized)))
		+
		10 * ((Variable_Self.HasStructure {Parameter_Type = "R_Ground_Light_Vehicle_Factory"} == 0) +
				(Variable_Self.HasStructure {Parameter_Type = "R_Ground_Barracks"} == 0) +
				(Variable_Self.HasStructure {Parameter_Type = "Rebel_Ground_Mining_Facility_GC"} == 0) +
				(Variable_Self.HasStructure {Parameter_Type = "Rebel_Ground_Mining_Facility_GC_Unlim"} == 0) +
				(Variable_Self.HasStructure {Parameter_Type = "Rebel_Mineral_Extractor_GC"} == 0))
	</BasicRebelInfrastructureBudgetAllocation>
	
	<!-- Based on average age of scouted information -->
	<BasicRebelInformationBudgetAllocation>
		0.5 * Function_Global_Scout_Need.Evaluate
	</BasicRebelInformationBudgetAllocation>

        <!--================== Budgeting for the Hutt Cartel ==================-->
	<!-- Fairly balanced with emphasis on offense -->
	<!-- Information budget kept minimal -->

	<!-- Snowball budget - more money for offense as galactic domination comes closer -->
	<BasicHuttsOffensiveBudgetAllocation>
		5.0
		+
		(10.0 * Variable_Self.PlanetsControlled)
		+
		(6.0 * (1.0 - Function_Has_Enough_Space_Force_Globally.Evaluate))
	</BasicHuttsOffensiveBudgetAllocation>

	<!-- Defensive budget
	We only care about land defenses if we have enough space forces
	AND
		SCALE if special structure defenses are needed
		ADD baseline amount for tactical defense spending
	-->
	<BasicHuttsDefensiveBudgetAllocation>
		Function_Has_Enough_Space_Force_Globally.Evaluate
		*
		(
			8.0 * Function_Can_Build_Defensive_Structures.Evaluate * (1.0 - Function_Has_Enough_Defensive_Structures.Evaluate)
			+
			3.0
		)
	</BasicHuttsDefensiveBudgetAllocation>
	
		<!-- Increase with tech level: higher tech units mostly need high level bases.
		Also increase with maintenance: bases prevent maintenance costs
		Decrease as bases are maxed out.

		Note that The Major Item funds are diverted to Infrastructure if we are unable to tech up but want to.
		Infrastructure money should then be spent on upgrading the highest level starbase, lifting the dependency.
		-->
	<BasicHuttsInfrastructureBudgetAllocation>
		1.1
		*
		(2 + 0.5 * (Variable_Self.TechLevel) - Variable_Self.MaxedStarbases - Variable_Self.MaxedGroundbases)
		*
		(1.0 + Variable_Self.Maintenance)
		*
		(1.0 + 2.0 * (0.5 > (Variable_Self.StructureCount {Parameter_Type = "Hutts_Ground_Mining_Facility_GC", Parameter_Type = "Hutts_Ground_Mining_Facility_GC_Unlim", Parameter_Type = "Hutts_Mineral_Extractor_GC"} / Variable_Self.PlanetsControlledUnnormalized)))
		+
		6 * (1 - Function_Can_Build_Offensive_Space.Evaluate)
		+
		10 * Function_Need_Tech_Upgrade_Unblocked.Evaluate
		+
		10 * ((Variable_Self.HasStructure {Parameter_Type = "H_Ground_Factory"} == 0) +
				(Variable_Self.HasStructure {Parameter_Type = "H_Ground_Barracks"} == 0) +
				(Variable_Self.HasStructure {Parameter_Type = "Hutts_Ground_Mining_Facility_GC"} == 0) +
				(Variable_Self.HasStructure {Parameter_Type = "Hutts_Ground_Mining_Facility_GC_Unlim"} == 0) +
				(Variable_Self.HasStructure {Parameter_Type = "Hutts_Mineral_Extractor_GC"} == 0))
	</BasicHuttsInfrastructureBudgetAllocation>

	<!-- Based on average age of scouted information -->
	<BasicHuttsInformationBudgetAllocation>
		0.3 * Function_Global_Scout_Need.Evaluate
	</BasicHuttsInformationBudgetAllocation>

	<!-- Receive a trickle of funds so that eventually, a major purchase is likely
		Adjust this to control how soon the AI will spend on tech upgrades.

		Scale this inversely with tech level and posession of the DeathStar (tech 6, in a sense)
		This is needed because during middle and late game, we need more funds for special structures, etc.
		Also, we don't want the AI to rush to max tech.

		Shut off funds if we're trying to upgrade a base in order to tech
		(the tech was already deemed affordable, and the reserve for it remains in the category)

		Shut off funds if we're behind in the space race but not behind in the tech race

		2.0 * Variable_Self.CanAdvanceTech
	-->
	<BasicHuttsMajorItemBudgetAllocation>
		0.7
		*
		(7.0 	- Variable_Self.TechLevel
				- Variable_Self.HasStructure{Parameter_Type = "H_Ground_Advanced_Technologies_Facility", Parameter_Count = 1})
		*
		clamp(Function_Has_Enough_Space_Force_Globally.Evaluate + (Game.Timeline > Variable_Self.TechLevel), 0.0, 1.0)
	</BasicHuttsMajorItemBudgetAllocation>

	<!-- Tech Upgrade Purchasing Budgets -->
	<!-- What fraction of the gross budget is required to support building the next tech item
		without pause, plus a small pad to compansate for fluctuations in spending that
		may occur before this perception can compensate.

		add a buffer large enough to ensure that the upgrade can be afforded, despite cash spikes/drops
	-->
	<HuttsTechUpgradeBudgetRequirement>
		(
			(1 - Variable_Self.HasStructure{Parameter_Type = "H_Ground_Advanced_Technologies_Facility", Parameter_Count = 1}) *
			Variable_Self.BudgetFractionToBuild{Parameter_Type = "H_Ground_Advanced_Technologies_Facility"}
			+
			Variable_Self.HasStructure{Parameter_Type = "H_Ground_Advanced_Technologies_Facility", Parameter_Count = 1} *
			(Variable_Self.TechLevel == 1) *
			Variable_Self.BudgetFractionToBuild{Parameter_Type = "Hutt_Tech_2"}
			+
			(Variable_Self.TechLevel == 2) *
			Variable_Self.BudgetFractionToBuild{Parameter_Type = "Hutt_Tech_3"}
			+
			(Variable_Self.TechLevel == 3) *
			Variable_Self.BudgetFractionToBuild{Parameter_Type = "Hutt_Tech_4"}
			+
			(Variable_Self.TechLevel == 4) *
			Variable_Self.BudgetFractionToBuild{Parameter_Type = "Hutt_Tech_5"}
			*
			1.25
			+
			0.05
		)
	</HuttsTechUpgradeBudgetRequirement>

	<!-- Tech Upgrade Template Budget -->
	<!-- Need to use static values for now. -->
	<!-- HuttsTechUpgradeBudgetRequirement is not a valid perception token and will assert if used! -->
	<HuttsTechUpgradeOffensiveAllocation>
		0.4
	</HuttsTechUpgradeOffensiveAllocation>

	<!-- Zero is used instead -->
	<HuttsTechUpgradeDefensiveAllocation>
		0.3
	</HuttsTechUpgradeDefensiveAllocation>

	<HuttsTechUpgradeMajorItemAllocation>
		0.25
	</HuttsTechUpgradeMajorItemAllocation>

	<HuttsTechUpgradeInfrastructureAllocation>
		0.35
	</HuttsTechUpgradeInfrastructureAllocation>

	<!-- Zero is used instead -->
	<HuttsTechUpgradeInformationAllocation>
		0.1
	</HuttsTechUpgradeInformationAllocation>

	<GenericSpaceTargetedOffensiveBudgetAllocation>
		Game.ForceVisibility
	</GenericSpaceTargetedOffensiveBudgetAllocation>

	<GenericSpaceDefensiveBudgetAllocation>
		0.5
	</GenericSpaceDefensiveBudgetAllocation>

	<GenericSpaceUntargetedOffensiveBudgetAllocation>
		1.0 - Game.ForceVisibility
	</GenericSpaceUntargetedOffensiveBudgetAllocation>

	<!-- Used to determine the funds to be spent from the galactic budget on a land tactical game.
		We like to spend more on planets that are:
			valuable
			have a large ground force investment
			have a large ground infrastructure investment
			scale by a magic number
		NOTE: we're not scaling by the number of open build pads, because if we can't build, the
		over-allocated money simply wont be spent.
	 -->
	<Land_Tactical_Budget>
		(
			1 * Function_GenericPlanetValue.Evaluate + 
			2 * Variable_Target.FriendlyForce.GroundTotal + 
			3 * Variable_Target.GroundbaseLevel
		) / 5
		* 2500
		+
		200
	</Land_Tactical_Budget>

	<Land_Tactical_Budget_Clamped>
		clamp(	Function_Land_Tactical_Budget.Evaluate,
					0,
					Variable_Self.CreditsUnnormalized / (Variable_Self.PlanetsControlled + 1)
				)
	</Land_Tactical_Budget_Clamped>
</Equations>