<?xml version="1.0" ?>

<Equations>

	<Can_Corrupt_Planet>
		Variable_Target.RetainsResidualInfluence
		*
		(Variable_Target.IsCorrupted == 0.0)
		*
		(Variable_Target.IsCorruptionTransitionActive == 0.0)
		*
		((Variable_Target.IsConnectedToCorruption + (Game.Age > Variable_Target.TimeSinceCorruptionChange)) > 0)
	</Can_Corrupt_Planet>

	<Should_Corrupt_Planet>
		Function_Can_Corrupt_Planet.Evaluate
		*
		(3.0
		+
		2.0 * Variable_Target.IsConnectedToCorruption
		+
		1.0 * Variable_Target.IncomeUnnormalized / 100)
	</Should_Corrupt_Planet>

	<Can_Make_Black_Market_Purchase>
		Variable_Target.IsCorrupted
		*
		Variable_Target.BlackMarketAbilitiesAvailable {Parameter_Faction = "Underworld"}
	</Can_Make_Black_Market_Purchase>

	<Should_Make_Black_Market_Purchase>
		Function_Can_Make_Black_Market_Purchase.Evaluate
		*
		(Variable_Self.CreditsUnnormalized > Variable_Target.BlackMarketMinimumAbilityPrice {Parameter_Faction = "Underworld"})
		*
		(10.0 - Variable_Target.BlackMarketPriceModifier / 300.0)
	</Should_Make_Black_Market_Purchase>

	<Should_Hack_Super_Weapon>
		Variable_Target.IsCorrupted
		*
		(Variable_Target.EnemyForce.HasSpaceUnitsBitfield {Parameter_Type = "Death_Star_II"} > 0.0)
		*
		(0.1 * (0.5 > Variable_Self.PlanetsControlled) + 
		10.0 * (Variable_Target.EnemyForce.HasGroundUnitsBitfield {Parameter_Type = "Emperor_Palpatine_Team"} > 0.0) +
		10.0 * (Variable_Target.EnemyForce.HasSpaceUnitsBitfield {Parameter_Type = "Emperor_Palpatine_Team"} > 0.0))
	</Should_Hack_Super_Weapon>

	<Can_Sabotage_Planet>
		Variable_Target.IsCorrupted
		*
		(Variable_Target.GroundbaseLevelUnnormalized > 0)
	</Can_Sabotage_Planet>

	<Should_Sabotage_Planet>
		Function_Can_Sabotage_Planet.Evaluate
		*
		(Function_Should_Save_For_Corruption.Evaluate == 0.0)
		*
		(25.0
		+
		5.0 * Function_GenericPlanetValue.Evaluate
		+
		5.0 * Variable_Target.HasStructure {Parameter_Type = "E_Ground_Research_Facility",
												Parameter_Type = "Ground_Empire_Hypervelocity_Gun",
												Parameter_Type = "Ground_Ion_Cannon",
												Parameter_Type = "E_Galactic_Turbolaser_Tower_Defenses",
												Parameter_Type = "R_Galactic_Turbolaser_Tower_Defenses"})
	</Should_Sabotage_Planet>

	<Should_Build_Underworld_Barracks>
		18 
		* 
		(1.0 - clamp((Variable_Self.StructureCount {Parameter_Type = "U_Ground_Barracks", Parameter_Only_Consider_Complete = 1.0} /
														Variable_Self.PlanetsControlledUnnormalized), 0.0, 0.5))
		*
		(
			Function_Needs_Groundbase_Upgrade.Evaluate
			+
			(Variable_Target.StructureCount {Parameter_Type = "U_Ground_Barracks", Parameter_Only_Consider_Complete = 1.0})
			+
			(2.0 * (3.0 > Variable_Self.StructureCount {Parameter_Type = "U_Ground_Barracks", Parameter_Only_Consider_Complete = 1.0}))
		)
		*
		(2 > Variable_Target.StructureCount {Parameter_Type = "U_Ground_Barracks", Parameter_Only_Consider_Complete = 1.0})
		*
		(Function_Should_Underworld_Save_For_Desirable_Structures.Evaluate == 0.0)
		*
		(Function_Should_Save_For_Corruption.Evaluate == 0.0)
	</Should_Build_Underworld_Barracks>	
	
	<Should_Build_Underworld_Palace>
		23 
		* 
		(Variable_Target.StructureCount {Parameter_Type = "U_Ground_Palace", Parameter_Only_Consider_Complete = 1.0} == 0)
		*
		(1.0 - clamp((Variable_Self.StructureCount {Parameter_Type = "U_Ground_Palace", Parameter_Only_Consider_Complete = 1.0} /
														Variable_Self.PlanetsControlledUnnormalized), 0.0, 0.5))
		*
		(
			Function_Needs_Groundbase_Upgrade.Evaluate
			+
			(4.0 * (3.0 > Variable_Self.StructureCount {Parameter_Type = "U_Ground_Palace", Parameter_Only_Consider_Complete = 1.0}))
		)
		*
		(1.0 - Game.PlanetsCorrupted + Variable_Self.PlanetsControlled)
		*
		(Function_Should_Save_For_Corruption.Evaluate == 0.0)
		+
		50.0 
		* 
		(Variable_Self.HasStructure {Parameter_Type = "U_Ground_Palace", Parameter_Only_Consider_Complete = 0.0} == 0.0)
		+
		50.0
		*
		(Variable_Self.HasStructure {Parameter_Type = "U_Ground_Palace", Parameter_Only_Consider_Complete = 1.0} == 0.0)
		*
		(Variable_Target.HasStructure {Parameter_Type = "U_Ground_Palace", Parameter_Only_Consider_Complete = 0.0})
	</Should_Build_Underworld_Palace>
	
	<Should_Build_Underworld_Vehicle_Factory>
		18 
		* 
		(1.0 - clamp((Variable_Self.StructureCount {Parameter_Type = "U_Ground_Vehicle_Factory", Parameter_Only_Consider_Complete = 1.0} /
														Variable_Self.PlanetsControlledUnnormalized), 0.0, 0.5))
		*
		(
			Function_Needs_Groundbase_Upgrade.Evaluate
			+
			(Variable_Target.StructureCount {Parameter_Type = "U_Ground_Vehicle_Factory", Parameter_Only_Consider_Complete = 1.0})
			+
			(2.0 * (3.0 > Variable_Self.StructureCount {Parameter_Type = "U_Ground_Vehicle_Factory", Parameter_Only_Consider_Complete = 1.0}))
		)
		*
		(3 > Variable_Target.StructureCount {Parameter_Type = "U_Ground_Vehicle_Factory", Parameter_Only_Consider_Complete = 1.0})			
		*
		(Function_Should_Save_For_Corruption.Evaluate == 0.0)
	</Should_Build_Underworld_Vehicle_Factory>
	
	<Should_Build_Underworld_Droid_Works>
		18 
		* 
		(1.0 - clamp((Variable_Self.StructureCount {Parameter_Type = "U_Ground_Droid_Works", Parameter_Only_Consider_Complete = 1.0} /
														Variable_Self.PlanetsControlledUnnormalized), 0.0, 0.5))
		*
		(
			Function_Needs_Groundbase_Upgrade.Evaluate
			+
			(Variable_Target.StructureCount {Parameter_Type = "U_Ground_Droid_Works", Parameter_Only_Consider_Complete = 1.0})
			+
			(3.0 * (3.0 > Variable_Self.StructureCount {Parameter_Type = "U_Ground_Droid_Works", Parameter_Only_Consider_Complete = 1.0}))
		)
		*
		(2 > Variable_Target.StructureCount {Parameter_Type = "U_Ground_Droid_Works", Parameter_Only_Consider_Complete = 1.0})	
		*
		(Function_Should_Underworld_Save_For_Desirable_Structures.Evaluate == 0.0)
		*
		(Function_Should_Save_For_Corruption.Evaluate == 0.0)
	</Should_Build_Underworld_Droid_Works>
	
	<Should_Underworld_Save_For_Desirable_Structures>
		(Variable_Target.GroundBaseLevelUnnormalized > 1.0)
		*
		(
			(Function_Should_Build_Underworld_Vehicle_Factory.Evaluate > 15.0)
			+
			(Function_Should_Build_Underworld_Palace.Evaluate > 15.0)
		)
	</Should_Underworld_Save_For_Desirable_Structures>	
	
	<Should_Save_For_Corruption>
		(Variable_Self.HasUnit {Parameter_Type = "Underworld_Saboteur"} == 0.0)
		*
		(0.67 > Game.PlanetsCorrupted)
		*
		(Variable_Self.StructureCount {Parameter_Type = "U_Ground_Palace", Parameter_Only_Consider_Complete = 1.0} > 1.0)
	</Should_Save_For_Corruption>
	
	<Should_Underworld_Build_Starbase>
		(0.75 * Function_Needs_Starbase_Upgrade.Evaluate)
		*
		(Function_Should_Save_For_Corruption.Evaluate == 0.0) 
	</Should_Underworld_Build_Starbase>
</Equations>