Exemple #1
0
        public bool AreConditionsMets()
        {
            if (!_gotWatchedBlocks)
            {
                SetupWatchedBlocks();
            }

            if (UseConditions == false)
            {
                return(true);
            }

            int usedConditions      = 0;
            int satisfiedConditions = 0;

            if (_behavior == null)
            {
                _behavior = BehaviorManager.GetBehavior(_remoteControl);

                if (_behavior == null)
                {
                    return(false);
                }
            }

            if (CheckAllLoadedModIDs == true)
            {
                usedConditions++;
                bool missingMod = false;

                foreach (var mod in AllModIDsToCheck)
                {
                    if (Utilities.ModIDs.Contains(mod) == false)
                    {
                        Logger.MsgDebug(ProfileSubtypeId + ": Mod ID Not Present", DebugTypeEnum.Condition);
                        missingMod = true;
                        break;
                    }
                }

                if (!missingMod)
                {
                    satisfiedConditions++;
                }
            }

            if (CheckAnyLoadedModIDs == true)
            {
                usedConditions++;

                foreach (var mod in AllModIDsToCheck)
                {
                    if (Utilities.ModIDs.Contains(mod))
                    {
                        Logger.MsgDebug(ProfileSubtypeId + ": A Mod ID was Found: " + mod.ToString(), DebugTypeEnum.Condition);
                        satisfiedConditions++;
                        break;
                    }
                }
            }

            if (CheckTrueBooleans == true)
            {
                usedConditions++;
                bool failedCheck = false;

                foreach (var boolName in TrueBooleans)
                {
                    if (!_settings.GetCustomBoolResult(boolName))
                    {
                        Logger.MsgDebug(ProfileSubtypeId + ": Boolean Not True: " + boolName, DebugTypeEnum.Condition);
                        failedCheck = true;
                        break;
                    }
                }

                if (!failedCheck)
                {
                    satisfiedConditions++;
                }
            }

            if (CheckCustomCounters == true)
            {
                usedConditions++;
                bool failedCheck = false;

                if (CustomCounters.Count == CustomCountersTargets.Count)
                {
                    for (int i = 0; i < CustomCounters.Count; i++)
                    {
                        try {
                            var compareType = CounterCompareEnum.GreaterOrEqual;

                            if (i <= CounterCompareTypes.Count - 1)
                            {
                                compareType = CounterCompareTypes[i];
                            }

                            if (_settings.GetCustomCounterResult(CustomCounters[i], CustomCountersTargets[i], compareType) == false)
                            {
                                Logger.MsgDebug(ProfileSubtypeId + ": Counter Amount Condition Not Satisfied: " + CustomCounters[i], DebugTypeEnum.Condition);
                                failedCheck = true;
                                break;
                            }
                        } catch (Exception e) {
                            Logger.MsgDebug("Exception: ", DebugTypeEnum.Condition);
                            Logger.MsgDebug(e.ToString(), DebugTypeEnum.Condition);
                        }
                    }
                }
                else
                {
                    Logger.MsgDebug(ProfileSubtypeId + ": Counter Names and Targets List Counts Don't Match. Check Your Condition Profile", DebugTypeEnum.Condition);
                    failedCheck = true;
                }

                if (!failedCheck)
                {
                    satisfiedConditions++;
                }
            }

            if (CheckTrueSandboxBooleans == true)
            {
                usedConditions++;
                bool failedCheck = false;

                for (int i = 0; i < TrueSandboxBooleans.Count; i++)
                {
                    try {
                        bool output = false;
                        var  result = MyAPIGateway.Utilities.GetVariable(TrueSandboxBooleans[i], out output);

                        if (!result || !output)
                        {
                            Logger.MsgDebug(ProfileSubtypeId + ": Sandbox Boolean False: " + TrueSandboxBooleans[i], DebugTypeEnum.Condition);
                            failedCheck = true;
                            break;
                        }
                    } catch (Exception e) {
                        Logger.MsgDebug("Exception: ", DebugTypeEnum.Condition);
                        Logger.MsgDebug(e.ToString(), DebugTypeEnum.Condition);
                    }
                }

                if (!failedCheck)
                {
                    satisfiedConditions++;
                }
            }

            if (CheckCustomSandboxCounters == true)
            {
                usedConditions++;
                bool failedCheck = false;

                if (CustomSandboxCounters.Count == CustomSandboxCountersTargets.Count)
                {
                    for (int i = 0; i < CustomSandboxCounters.Count; i++)
                    {
                        try {
                            int counter = 0;
                            var result  = MyAPIGateway.Utilities.GetVariable(CustomSandboxCounters[i], out counter);

                            var compareType = CounterCompareEnum.GreaterOrEqual;

                            if (i <= SandboxCounterCompareTypes.Count - 1)
                            {
                                compareType = SandboxCounterCompareTypes[i];
                            }

                            bool counterResult = false;

                            if (compareType == CounterCompareEnum.GreaterOrEqual)
                            {
                                counterResult = (counter >= CustomSandboxCountersTargets[i]);
                            }

                            if (compareType == CounterCompareEnum.Greater)
                            {
                                counterResult = (counter > CustomSandboxCountersTargets[i]);
                            }

                            if (compareType == CounterCompareEnum.Equal)
                            {
                                counterResult = (counter == CustomSandboxCountersTargets[i]);
                            }

                            if (compareType == CounterCompareEnum.NotEqual)
                            {
                                counterResult = (counter != CustomSandboxCountersTargets[i]);
                            }

                            if (compareType == CounterCompareEnum.Less)
                            {
                                counterResult = (counter < CustomSandboxCountersTargets[i]);
                            }

                            if (compareType == CounterCompareEnum.LessOrEqual)
                            {
                                counterResult = (counter <= CustomSandboxCountersTargets[i]);
                            }

                            if (!result || !counterResult)
                            {
                                Logger.MsgDebug(ProfileSubtypeId + ": Sandbox Counter Amount Condition Not Satisfied: " + CustomSandboxCounters[i], DebugTypeEnum.Condition);
                                failedCheck = true;
                                break;
                            }
                        } catch (Exception e) {
                            Logger.MsgDebug("Exception: ", DebugTypeEnum.Condition);
                            Logger.MsgDebug(e.ToString(), DebugTypeEnum.Condition);
                        }
                    }
                }
                else
                {
                    Logger.MsgDebug(ProfileSubtypeId + ": Sandbox Counter Names and Targets List Counts Don't Match. Check Your Condition Profile", DebugTypeEnum.Condition);
                    failedCheck = true;
                }

                if (!failedCheck)
                {
                    satisfiedConditions++;
                }
            }

            if (CheckGridSpeed == true)
            {
                usedConditions++;
                float speed = (float)_remoteControl.GetShipSpeed();

                if ((MinGridSpeed == -1 || speed >= MinGridSpeed) && (MaxGridSpeed == -1 || speed <= MaxGridSpeed))
                {
                    Logger.MsgDebug(ProfileSubtypeId + ": Grid Speed High Enough", DebugTypeEnum.Condition);
                    satisfiedConditions++;
                }
                else
                {
                    Logger.MsgDebug(ProfileSubtypeId + ": Grid Speed Not High Enough", DebugTypeEnum.Condition);
                }
            }

            if (MESApi.MESApiReady && CheckMESBlacklistedSpawnGroups)
            {
                var blackList = MESApi.GetSpawnGroupBlackList();

                if (SpawnGroupBlacklistContainsAll.Count > 0)
                {
                    usedConditions++;
                    bool failedCheck = false;

                    foreach (var group in SpawnGroupBlacklistContainsAll)
                    {
                        if (blackList.Contains(group) == false)
                        {
                            Logger.MsgDebug(ProfileSubtypeId + ": A Spawngroup was not on MES BlackList: " + group, DebugTypeEnum.Condition);
                            failedCheck = true;
                            break;
                        }
                    }

                    if (!failedCheck)
                    {
                        satisfiedConditions++;
                    }
                }

                if (SpawnGroupBlacklistContainsAny.Count > 0)
                {
                    usedConditions++;
                    foreach (var group in SpawnGroupBlacklistContainsAll)
                    {
                        if (blackList.Contains(group))
                        {
                            Logger.MsgDebug(ProfileSubtypeId + ": A Spawngroup was on MES BlackList: " + group, DebugTypeEnum.Condition);
                            satisfiedConditions++;
                            break;
                        }
                    }
                }
            }

            if (UseAccumulatedDamageWatcher)
            {
                usedConditions++;
                bool failedCheck = false;

                if (MinAccumulatedDamage >= 0 && MinAccumulatedDamage < _settings.TotalDamageAccumulated)
                {
                    failedCheck = true;
                }

                if (MaxAccumulatedDamage >= 0 && MaxAccumulatedDamage > _settings.TotalDamageAccumulated)
                {
                    failedCheck = true;
                }

                if (!failedCheck)
                {
                    satisfiedConditions++;
                }
            }

            if (UseRequiredFunctionalBlocks)
            {
                if (_watchingAllBlocks)
                {
                    usedConditions++;

                    if (_watchedAllBlocksResult)
                    {
                        satisfiedConditions++;
                    }
                }

                if (_watchingAnyBlocks)
                {
                    usedConditions++;

                    if (_watchedAnyBlocksResult)
                    {
                        satisfiedConditions++;
                    }
                }

                if (_watchingNoneBlocks)
                {
                    usedConditions++;

                    if (_watchedNoneBlocksResult)
                    {
                        satisfiedConditions++;
                    }
                }
            }

            if (CheckTargetAltitudeDifference)
            {
                usedConditions++;

                if (_behavior.AutoPilot.Targeting.HasTarget() && _behavior.AutoPilot.InGravity())
                {
                    var planetPos      = _behavior.AutoPilot.CurrentPlanet.PositionComp.WorldAABB.Center;
                    var targetCoreDist = _behavior.AutoPilot.Targeting.Target.Distance(planetPos);
                    var myCoreDist     = Vector3D.Distance(planetPos, _remoteControl.GetPosition());
                    var difference     = targetCoreDist - myCoreDist;

                    if (difference >= this.MinTargetAltitudeDifference && difference <= this.MaxTargetAltitudeDifference)
                    {
                        satisfiedConditions++;
                    }
                }
            }

            if (CheckTargetDistance)
            {
                usedConditions++;

                if (_behavior.AutoPilot.Targeting.HasTarget())
                {
                    var dist = _behavior.AutoPilot.Targeting.Target.Distance(_remoteControl.GetPosition());

                    if ((this.MinTargetDistance == -1 || dist >= this.MinTargetDistance) && (this.MaxTargetDistance == -1 || dist <= this.MaxTargetDistance))
                    {
                        satisfiedConditions++;
                    }
                }
            }

            if (CheckTargetAngleFromForward)
            {
                usedConditions++;

                if (_behavior.AutoPilot.Targeting.HasTarget())
                {
                    var dirToTarget = Vector3D.Normalize(_behavior.AutoPilot.Targeting.GetTargetCoords() - _remoteControl.GetPosition());
                    var myForward   = _behavior.AutoPilot.RefBlockMatrixRotation.Forward;
                    var angle       = VectorHelper.GetAngleBetweenDirections(dirToTarget, myForward);

                    if ((this.MinTargetAngle == -1 || angle >= this.MinTargetAngle) && (this.MaxTargetAngle == -1 || angle <= this.MaxTargetAngle))
                    {
                        satisfiedConditions++;
                    }
                }
            }

            if (CheckIfTargetIsChasing)
            {
                usedConditions++;

                if (_behavior.AutoPilot.Targeting.HasTarget())
                {
                    var dirFromTarget  = Vector3D.Normalize(_remoteControl.GetPosition() - _behavior.AutoPilot.Targeting.GetTargetCoords());
                    var targetVelocity = Vector3D.Normalize(_behavior.AutoPilot.Targeting.Target.CurrentVelocity());

                    if (targetVelocity.IsValid() && targetVelocity.Length() > 0)
                    {
                        var angle = VectorHelper.GetAngleBetweenDirections(dirFromTarget, targetVelocity);

                        if ((this.MinTargetChaseAngle == -1 || angle >= this.MinTargetChaseAngle) && (this.MaxTargetChaseAngle == -1 || angle <= this.MaxTargetChaseAngle))
                        {
                            satisfiedConditions++;
                        }
                    }
                }
            }

            if (CheckIfGridNameMatches)
            {
                usedConditions++;

                if (!string.IsNullOrWhiteSpace(_remoteControl.SlimBlock.CubeGrid.CustomName))
                {
                    bool pass = false;

                    foreach (var name in GridNamesToCheck)
                    {
                        if (AllowPartialGridNameMatches)
                        {
                            if (_remoteControl.SlimBlock.CubeGrid.CustomName.Contains(name))
                            {
                                pass = true;
                            }
                        }
                        else
                        {
                            if (_remoteControl.SlimBlock.CubeGrid.CustomName == name)
                            {
                                pass = true;
                            }
                        }
                    }

                    if (pass)
                    {
                        satisfiedConditions++;
                    }
                }
            }

            if (UnderwaterCheck)
            {
                usedConditions++;

                if (WaterHelper.UnderwaterAndDepthCheck(_remoteControl.GetPosition(), _behavior.AutoPilot.CurrentWater, IsUnderwater, MinDistanceUnderwater, MaxDistanceUnderwater))
                {
                    satisfiedConditions++;
                }
            }

            if (TargetUnderwaterCheck)
            {
                usedConditions++;

                if (WaterHelper.UnderwaterAndDepthCheck(_remoteControl.GetPosition(), _behavior.AutoPilot.CurrentWater, TargetIsUnderwater, MinTargetDistanceUnderwater, MaxTargetDistanceUnderwater))
                {
                    satisfiedConditions++;
                }
            }

            if (BehaviorModeCheck)
            {
                usedConditions++;

                if (_behavior.Mode == CurrentBehaviorMode)
                {
                    satisfiedConditions++;
                }
            }

            if (MatchAnyCondition == false)
            {
                bool result = satisfiedConditions >= usedConditions;
                Logger.MsgDebug(ProfileSubtypeId + ": All Condition Satisfied: " + result.ToString(), DebugTypeEnum.Condition);
                Logger.MsgDebug(string.Format("Used Conditions: {0} // Satisfied Conditions: {1}", usedConditions, satisfiedConditions), DebugTypeEnum.Condition);
                return(result);
            }
            else
            {
                bool result = satisfiedConditions > 0;
                Logger.MsgDebug(ProfileSubtypeId + ": Any Condition(s) Satisfied: " + result.ToString(), DebugTypeEnum.Condition);
                Logger.MsgDebug(string.Format("Used Conditions: {0} // Satisfied Conditions: {1}", usedConditions, satisfiedConditions), DebugTypeEnum.Condition);
                return(result);
            }
        }
        public bool AreConditionsMets()
        {
            if (!_gotWatchedBlocks)
            {
                SetupWatchedBlocks();
            }

            if (this.UseConditions == false)
            {
                return(true);
            }

            int usedConditions      = 0;
            int satisfiedConditions = 0;

            if (this.CheckAllLoadedModIDs == true)
            {
                usedConditions++;
                bool missingMod = false;

                foreach (var mod in this.AllModIDsToCheck)
                {
                    if (Utilities.ModIDs.Contains(mod) == false)
                    {
                        Logger.MsgDebug(this.ProfileSubtypeId + ": Mod ID Not Present", DebugTypeEnum.Condition);
                        missingMod = true;
                        break;
                    }
                }

                if (!missingMod)
                {
                    satisfiedConditions++;
                }
            }

            if (this.CheckAnyLoadedModIDs == true)
            {
                usedConditions++;

                foreach (var mod in this.AllModIDsToCheck)
                {
                    if (Utilities.ModIDs.Contains(mod))
                    {
                        Logger.MsgDebug(this.ProfileSubtypeId + ": A Mod ID was Found: " + mod.ToString(), DebugTypeEnum.Condition);
                        satisfiedConditions++;
                        break;
                    }
                }
            }

            if (this.CheckTrueBooleans == true)
            {
                usedConditions++;
                bool failedCheck = false;

                foreach (var boolName in this.TrueBooleans)
                {
                    if (!_settings.GetCustomBoolResult(boolName))
                    {
                        Logger.MsgDebug(this.ProfileSubtypeId + ": Boolean Not True: " + boolName, DebugTypeEnum.Condition);
                        failedCheck = true;
                        break;
                    }
                }

                if (!failedCheck)
                {
                    satisfiedConditions++;
                }
            }

            if (this.CheckCustomCounters == true)
            {
                usedConditions++;
                bool failedCheck = false;

                if (this.CustomCounters.Count == this.CustomCountersTargets.Count)
                {
                    for (int i = 0; i < this.CustomCounters.Count; i++)
                    {
                        try {
                            if (_settings.GetCustomCounterResult(this.CustomCounters[i], this.CustomCountersTargets[i]) == false)
                            {
                                Logger.MsgDebug(this.ProfileSubtypeId + ": Counter Amount Not High Enough: " + this.CustomCounters[i], DebugTypeEnum.Condition);
                                failedCheck = true;
                                break;
                            }
                        } catch (Exception e) {
                            Logger.MsgDebug("Exception: ", DebugTypeEnum.Condition);
                            Logger.MsgDebug(e.ToString(), DebugTypeEnum.Condition);
                        }
                    }
                }
                else
                {
                    Logger.MsgDebug(this.ProfileSubtypeId + ": Counter Names and Targets List Counts Don't Match. Check Your Condition Profile", DebugTypeEnum.Condition);
                    failedCheck = true;
                }

                if (!failedCheck)
                {
                    satisfiedConditions++;
                }
            }

            if (this.CheckTrueSandboxBooleans == true)
            {
                usedConditions++;
                bool failedCheck = false;

                for (int i = 0; i < this.TrueSandboxBooleans.Count; i++)
                {
                    try {
                        bool output = false;
                        var  result = MyAPIGateway.Utilities.GetVariable <bool>(this.TrueSandboxBooleans[i], out output);

                        if (!result || !output)
                        {
                            Logger.MsgDebug(this.ProfileSubtypeId + ": Sandbox Boolean False: " + this.TrueSandboxBooleans[i], DebugTypeEnum.Condition);
                            failedCheck = true;
                            break;
                        }
                    } catch (Exception e) {
                        Logger.MsgDebug("Exception: ", DebugTypeEnum.Condition);
                        Logger.MsgDebug(e.ToString(), DebugTypeEnum.Condition);
                    }
                }

                if (!failedCheck)
                {
                    satisfiedConditions++;
                }
            }

            if (this.CheckCustomSandboxCounters == true)
            {
                usedConditions++;
                bool failedCheck = false;

                if (this.CustomCounters.Count == this.CustomCountersTargets.Count)
                {
                    for (int i = 0; i < this.CustomCounters.Count; i++)
                    {
                        try {
                            int counter = 0;
                            var result  = MyAPIGateway.Utilities.GetVariable <int>(this.CustomCounters[i], out counter);

                            if (!result || counter < this.CustomCountersTargets[i])
                            {
                                Logger.MsgDebug(this.ProfileSubtypeId + ": Sandbox Counter Amount Not High Enough: " + this.CustomSandboxCounters[i], DebugTypeEnum.Condition);
                                failedCheck = true;
                                break;
                            }
                        } catch (Exception e) {
                            Logger.MsgDebug("Exception: ", DebugTypeEnum.Condition);
                            Logger.MsgDebug(e.ToString(), DebugTypeEnum.Condition);
                        }
                    }
                }
                else
                {
                    Logger.MsgDebug(this.ProfileSubtypeId + ": Sandbox Counter Names and Targets List Counts Don't Match. Check Your Condition Profile", DebugTypeEnum.Condition);
                    failedCheck = true;
                }

                if (!failedCheck)
                {
                    satisfiedConditions++;
                }
            }

            if (this.CheckGridSpeed == true)
            {
                usedConditions++;
                float speed = (float)_remoteControl.GetShipSpeed();

                if ((this.MinGridSpeed == -1 || speed >= this.MinGridSpeed) && (this.MaxGridSpeed == -1 || speed <= this.MaxGridSpeed))
                {
                    Logger.MsgDebug(this.ProfileSubtypeId + ": Grid Speed High Enough", DebugTypeEnum.Condition);
                    satisfiedConditions++;
                }
                else
                {
                    Logger.MsgDebug(this.ProfileSubtypeId + ": Grid Speed Not High Enough", DebugTypeEnum.Condition);
                }
            }

            if (MESApi.MESApiReady && this.CheckMESBlacklistedSpawnGroups)
            {
                var blackList = MESApi.GetSpawnGroupBlackList();

                if (this.SpawnGroupBlacklistContainsAll.Count > 0)
                {
                    usedConditions++;
                    bool failedCheck = false;

                    foreach (var group in this.SpawnGroupBlacklistContainsAll)
                    {
                        if (blackList.Contains(group) == false)
                        {
                            Logger.MsgDebug(this.ProfileSubtypeId + ": A Spawngroup was not on MES BlackList: " + group, DebugTypeEnum.Condition);
                            failedCheck = true;
                            break;
                        }
                    }

                    if (!failedCheck)
                    {
                        satisfiedConditions++;
                    }
                }

                if (this.SpawnGroupBlacklistContainsAny.Count > 0)
                {
                    usedConditions++;
                    foreach (var group in this.SpawnGroupBlacklistContainsAll)
                    {
                        if (blackList.Contains(group))
                        {
                            Logger.MsgDebug(this.ProfileSubtypeId + ": A Spawngroup was on MES BlackList: " + group, DebugTypeEnum.Condition);
                            satisfiedConditions++;
                            break;
                        }
                    }
                }
            }

            if (UseAccumulatedDamageWatcher)
            {
                usedConditions++;
                bool failedCheck = false;

                if (this.MinAccumulatedDamage >= 0 && this.MinAccumulatedDamage < _settings.TotalDamageAccumulated)
                {
                    failedCheck = true;
                }

                if (this.MaxAccumulatedDamage >= 0 && this.MaxAccumulatedDamage > _settings.TotalDamageAccumulated)
                {
                    failedCheck = true;
                }

                if (!failedCheck)
                {
                    satisfiedConditions++;
                }
            }

            if (this.MatchAnyCondition == false)
            {
                bool result = (satisfiedConditions >= usedConditions);
                Logger.MsgDebug(this.ProfileSubtypeId + ": All Condition Satisfied: " + result.ToString(), DebugTypeEnum.Condition);
                Logger.MsgDebug(string.Format("Used Conditions: {0} // Satisfied Conditions: {1}", usedConditions, satisfiedConditions), DebugTypeEnum.Condition);
                return(result);
            }
            else
            {
                bool result = (satisfiedConditions > 0);
                Logger.MsgDebug(this.ProfileSubtypeId + ": Any Condition(s) Satisfied: " + result.ToString(), DebugTypeEnum.Condition);
                Logger.MsgDebug(string.Format("Used Conditions: {0} // Satisfied Conditions: {1}", usedConditions, satisfiedConditions), DebugTypeEnum.Condition);
                return(result);
            }
        }