/// <summary>
        /// returns amount spent on guardians
        /// </summary>
        private FInt Helper_SeedGuardians(ArcenSimContext Context, Planet ThisPlanet, ArcenPoint centerPoint, GameEntity entityToGuard, List <GameEntityTypeData> guardianTypes, FInt budget, FInt minDistanceFactor, FInt maxDistanceFactor, EntityBehaviorType behavior, Boolean isMobilePatrol, int MaxCountToSeed)
        {
            int minDistance = (ExternalConstants.Instance.Balance_AverageGravWellRadius * minDistanceFactor).IntValue;
            int maxDistance = (ExternalConstants.Instance.Balance_AverageGravWellRadius * maxDistanceFactor).IntValue;

            FInt result = FInt.Zero;

            while (budget > FInt.Zero)
            {
                GameEntityTypeData guardianData = guardianTypes[Context.QualityRandom.Next(0, guardianTypes.Count)];

                budget -= guardianData.BalanceStats.StrengthPerSquad;

                ArcenPoint point = ThisPlanet.Combat.GetSafePlacementPoint(Context, guardianData, centerPoint, minDistance, maxDistance);
                if (point == ArcenPoint.ZeroZeroPoint)
                {
                    continue;
                }

                result += guardianData.BalanceStats.StrengthPerSquad;

                GameEntity newEntity = GameEntity.CreateNew(entityToGuard.Side, guardianData, point, Context);
                newEntity.EntitySpecificOrders.Behavior = behavior;
                newEntity.GuardedObject = entityToGuard;
                switch (behavior)
                {
                case EntityBehaviorType.Guard_Guardian_Anchored:
                    break;

                case EntityBehaviorType.Guard_Guardian_Patrolling:
                    newEntity.GuardingOffsets.Add(newEntity.WorldLocation - newEntity.GuardedObject.WorldLocation);
                    if (isMobilePatrol)
                    {
                        AngleDegrees initialAngle    = centerPoint.GetAngleToDegrees(newEntity.WorldLocation);
                        int          initialDistance = centerPoint.GetDistanceTo(newEntity.WorldLocation, false);
                        int          step            = (AngleDegrees.MaxValue / 6).IntValue;
                        for (int i = step; i < AngleDegrees.MaxValue; i += step)
                        {
                            AngleDegrees angleToThisPoint = initialAngle.Add(AngleDegrees.Create((FInt)i));
                            ArcenPoint   thisPoint        = centerPoint.GetPointAtAngleAndDistance(angleToThisPoint, initialDistance);
                            newEntity.GuardingOffsets.Add(thisPoint - newEntity.GuardedObject.WorldLocation);
                        }
                    }
                    break;
                }

                if (MaxCountToSeed > 0)
                {
                    MaxCountToSeed--;
                    if (MaxCountToSeed == 0)
                    {
                        break;
                    }
                }
            }

            return(result);
        }
        private void Helper_SeedDireGuardians(ArcenSimContext Context, Planet ThisPlanet, ArcenPoint centerPoint, GameEntity controller, List <GameEntityTypeData> guardianTypes, int Count, FInt minDistanceFactor, FInt maxDistanceFactor, EntityBehaviorType behavior, Boolean isMobilePatrol)
        {
            int minDistance = (ExternalConstants.Instance.Balance_AverageGravWellRadius * minDistanceFactor).IntValue;
            int maxDistance = (ExternalConstants.Instance.Balance_AverageGravWellRadius * maxDistanceFactor).IntValue;

            while (Count > 0)
            {
                Count--;

                GameEntityTypeData guardianData = guardianTypes[Context.QualityRandom.Next(0, guardianTypes.Count)];

                ArcenPoint point = ThisPlanet.Combat.GetSafePlacementPoint(Context, guardianData, centerPoint, minDistance, maxDistance);
                if (point == ArcenPoint.ZeroZeroPoint)
                {
                    continue;
                }

                GameEntity newEntity = GameEntity.CreateNew(controller.Side, guardianData, point, Context);
                newEntity.EntitySpecificOrders.Behavior = behavior;
                newEntity.GuardedObject = controller;
                switch (behavior)
                {
                case EntityBehaviorType.Guard_Guardian_Anchored:
                    break;

                case EntityBehaviorType.Guard_Guardian_Patrolling:
                    newEntity.GuardingOffsets.Add(newEntity.WorldLocation - newEntity.GuardedObject.WorldLocation);
                    if (isMobilePatrol)
                    {
                        AngleDegrees initialAngle    = newEntity.GuardedObject.WorldLocation.GetAngleToDegrees(newEntity.WorldLocation);
                        int          initialDistance = newEntity.GuardedObject.WorldLocation.GetDistanceTo(newEntity.WorldLocation, false);
                        int          step            = (AngleDegrees.MaxValue / 6).IntValue;
                        for (int i = step; i < AngleDegrees.MaxValue; i += step)
                        {
                            AngleDegrees angleToThisPoint = initialAngle.Add(AngleDegrees.Create((FInt)i));
                            ArcenPoint   thisPoint        = newEntity.GuardedObject.WorldLocation.GetPointAtAngleAndDistance(angleToThisPoint, initialDistance);
                            newEntity.GuardingOffsets.Add(thisPoint - newEntity.GuardedObject.WorldLocation);
                        }
                    }
                    break;
                }
            }
        }
        private static int SpendBudgetOnItemsInList(ArcenSimContext Context, CombatSide Side, ArcenPoint CenterLocation, int MinimumDistanceFromCenter, List <GameEntityTypeData> listToBuyFrom, int budget)
        {
            if (listToBuyFrom.Count <= 0)
            {
                return(0);
            }
            if (budget <= 0)
            {
                return(0);
            }

            int remainingBudget = budget;

            int budgetPerType = remainingBudget / listToBuyFrom.Count;

            ArcenPoint formationCenterPoint = CenterLocation;

            listToBuyFrom.Sort(delegate(GameEntityTypeData Left, GameEntityTypeData Right)
            {
                int leftValue = 0;
                if (Left.SystemEntries.Count > 0)
                {
                    SystemEntry systemEntry = Left.SystemEntries[0];
                    if (systemEntry.SubEntries.Count > 0)
                    {
                        leftValue = systemEntry.SubEntries[0].BalanceStats.Range;
                    }
                }
                int rightValue = 0;
                if (Right.SystemEntries.Count > 0)
                {
                    SystemEntry systemEntry = Right.SystemEntries[0];
                    if (systemEntry.SubEntries.Count > 0)
                    {
                        rightValue = systemEntry.SubEntries[0].BalanceStats.Range;
                    }
                }
                return(rightValue.CompareTo(leftValue));
            });

            int innerRingDistance = MinimumDistanceFromCenter;
            int outerRingDistance = (ExternalConstants.Instance.Balance_AverageGravWellRadius * FInt.FromParts(0, 100)).IntValue;

            int distanceBetweenRings = (outerRingDistance - innerRingDistance) / listToBuyFrom.Count;

            for (int i = 0; i < listToBuyFrom.Count; i++)
            {
                GameEntityTypeData entityType = listToBuyFrom[i];
                int numberToPlace             = budgetPerType / entityType.BalanceStats.SquadPowerConsumption;
                numberToPlace = Math.Min(Side.GetRemainingCap(entityType), numberToPlace);
                if (numberToPlace <= 0)
                {
                    continue;
                }
                int          ringDistance       = innerRingDistance + (distanceBetweenRings * i);
                AngleDegrees startingAngle      = AngleDegrees.Create((FInt)Context.QualityRandom.Next(0, AngleDegrees.MaxValue.IntValue));
                AngleDegrees angleChangePerItem = AngleDegrees.MaxAngle / numberToPlace;
                for (int j = 0; j < numberToPlace; j++)
                {
                    if (Side.GetCanBuildAnother(entityType) != ArcenRejectionReason.Unknown)
                    {
                        break;
                    }
                    AngleDegrees angle = startingAngle + (angleChangePerItem * j);
                    ArcenPoint   point = formationCenterPoint.GetPointAtAngleAndDistance(angle, ringDistance);
                    point = Side.Combat.GetSafePlacementPoint(Context, entityType, point, 0, distanceBetweenRings / 2);
                    if (point == ArcenPoint.ZeroZeroPoint)
                    {
                        continue;
                    }
                    GameEntity newEntity = GameEntity.CreateNew(Side, entityType, point, Context);
                    newEntity.SelfBuildingMetalRemaining = (FInt)entityType.BalanceStats.SquadMetalCost;
                    remainingBudget -= entityType.BalanceStats.SquadPowerConsumption;
                }
            }

            // fill in the corners of the budget
            listToBuyFrom.Sort(delegate(GameEntityTypeData Left, GameEntityTypeData Right)
            {
                return(Right.BalanceStats.SquadPowerConsumption.CompareTo(Left.BalanceStats.SquadPowerConsumption));
            });

            bool checkAgain = true;

            while (remainingBudget > 0 && checkAgain)
            {
                checkAgain = false;
                for (int i = 0; i < listToBuyFrom.Count; i++)
                {
                    GameEntityTypeData entityType = listToBuyFrom[i];
                    if (remainingBudget < entityType.BalanceStats.SquadPowerConsumption)
                    {
                        continue;
                    }
                    if (Side.GetCanBuildAnother(entityType) != ArcenRejectionReason.Unknown)
                    {
                        continue;
                    }
                    ArcenPoint point = Side.Combat.GetSafePlacementPoint(Context, entityType, formationCenterPoint, innerRingDistance, outerRingDistance);
                    if (point == ArcenPoint.ZeroZeroPoint)
                    {
                        continue;
                    }
                    GameEntity newEntity = GameEntity.CreateNew(Side, entityType, point, Context);
                    newEntity.SelfBuildingMetalRemaining = (FInt)entityType.BalanceStats.SquadMetalCost;
                    remainingBudget -= entityType.BalanceStats.SquadPowerConsumption;
                    checkAgain       = true;
                }
            }

            return(budget - remainingBudget);
        }
        public void Execute(ArcenSimContext Context, GameEntity BuildingEntity)
        {
            List <GameEntityTypeData> turretTypes = new List <GameEntityTypeData>();

            for (int menuIndex = 0; menuIndex < BuildingEntity.TypeData.BuildMenus.Count; menuIndex++)
            {
                BuildMenu menu = BuildingEntity.TypeData.BuildMenus[menuIndex];
                for (int i = 0; i < menu.List.Count; i++)
                {
                    GameEntityTypeData entityData = menu.List[i];
                    if (entityData.Balance_FuelCost.FuelMultiplier > 0)
                    {
                        continue;
                    }
                    if (entityData.Balance_PowerCost.PowerMultiplier <= 0)
                    {
                        continue;
                    }
                    if (!entityData.CapIsPerPlanet)
                    {
                        continue;
                    }
                    if (!EntityRollupType.Combatants.Matches(entityData))     //&& !EntityRollupType.ProjectsShield.Matches( entityData )
                    {
                        continue;
                    }
                    if (BuildingEntity.Side.GetCanBuildAnother(entityData) != ArcenRejectionReason.Unknown)
                    {
                        continue;
                    }
                    turretTypes.Add(entityData);
                }
            }

            if (turretTypes.Count <= 0)
            {
                return;
            }

            int powerBudget = BuildingEntity.Side.NetPower;

            int budgetPerType = powerBudget / turretTypes.Count;

            ArcenPoint formationCenterPoint = BuildingEntity.WorldLocation;

            turretTypes.Sort(delegate(GameEntityTypeData Left, GameEntityTypeData Right)
            {
                int leftValue = 0;
                if (Left.SystemEntries.Count > 0)
                {
                    SystemEntry systemEntry = Left.SystemEntries[0];
                    if (systemEntry.SubEntries.Count > 0)
                    {
                        leftValue = systemEntry.SubEntries[0].BalanceStats.Range;
                    }
                }
                int rightValue = 0;
                if (Right.SystemEntries.Count > 0)
                {
                    SystemEntry systemEntry = Right.SystemEntries[0];
                    if (systemEntry.SubEntries.Count > 0)
                    {
                        rightValue = systemEntry.SubEntries[0].BalanceStats.Range;
                    }
                }
                return(rightValue.CompareTo(leftValue));
            });

            int innerRingDistance = BuildingEntity.TypeData.Radius * 2;
            int outerRingDistance = (ExternalConstants.Instance.Balance_AverageGravWellRadius * FInt.FromParts(0, 100)).IntValue;

            int distanceBetweenRings = (outerRingDistance - innerRingDistance) / turretTypes.Count;

            for (int i = 0; i < turretTypes.Count; i++)
            {
                GameEntityTypeData turretType = turretTypes[i];
                int numberToPlace             = budgetPerType / turretType.BalanceStats.SquadPowerConsumption;
                numberToPlace = Math.Min(BuildingEntity.Side.GetRemainingCap(turretType), numberToPlace);
                if (numberToPlace <= 0)
                {
                    continue;
                }
                int          ringDistance       = innerRingDistance + (distanceBetweenRings * i);
                AngleDegrees startingAngle      = AngleDegrees.Create((FInt)Context.QualityRandom.Next(0, AngleDegrees.MaxValue.IntValue));
                AngleDegrees angleChangePerItem = AngleDegrees.MaxAngle / numberToPlace;
                for (int j = 0; j < numberToPlace; j++)
                {
                    if (BuildingEntity.Side.GetCanBuildAnother(turretType) != ArcenRejectionReason.Unknown)
                    {
                        break;
                    }
                    AngleDegrees angle = startingAngle + (angleChangePerItem * j);
                    ArcenPoint   point = formationCenterPoint.GetPointAtAngleAndDistance(angle, ringDistance);
                    point = BuildingEntity.Combat.GetSafePlacementPoint(Context, turretType, point, 0, distanceBetweenRings / 2);
                    if (point == ArcenPoint.ZeroZeroPoint)
                    {
                        continue;
                    }
                    GameEntity newEntity = GameEntity.CreateNew(BuildingEntity.Side, turretType, point, Context);
                    newEntity.SelfBuildingMetalRemaining = (FInt)turretType.BalanceStats.SquadMetalCost;
                    powerBudget -= turretType.BalanceStats.SquadPowerConsumption;
                }
            }

            // fill in the corners of the budget
            turretTypes.Sort(delegate(GameEntityTypeData Left, GameEntityTypeData Right)
            {
                return(Right.BalanceStats.SquadPowerConsumption.CompareTo(Left.BalanceStats.SquadPowerConsumption));
            });

            bool checkAgain = true;

            while (powerBudget > 0 && checkAgain)
            {
                checkAgain = false;
                for (int i = 0; i < turretTypes.Count; i++)
                {
                    GameEntityTypeData turretType = turretTypes[i];
                    if (powerBudget < turretType.BalanceStats.SquadPowerConsumption)
                    {
                        continue;
                    }
                    if (BuildingEntity.Side.GetCanBuildAnother(turretType) != ArcenRejectionReason.Unknown)
                    {
                        continue;
                    }
                    ArcenPoint point = BuildingEntity.Combat.GetSafePlacementPoint(Context, turretType, formationCenterPoint, innerRingDistance, outerRingDistance);
                    if (point == ArcenPoint.ZeroZeroPoint)
                    {
                        continue;
                    }
                    GameEntity newEntity = GameEntity.CreateNew(BuildingEntity.Side, turretType, point, Context);
                    newEntity.SelfBuildingMetalRemaining = (FInt)turretType.BalanceStats.SquadMetalCost;
                    powerBudget -= turretType.BalanceStats.SquadPowerConsumption;
                    checkAgain   = true;
                }
            }
        }