public static void LinkageActivate(this MechComponent component, bool isInital)
        {
            ActivatableComponent activatable = component.componentDef.GetComponent <ActivatableComponent>();

            if (activatable == null)
            {
                return;
            }
            Log.Debug?.TWL(0, "LinkageActivate " + component.defId);
            foreach (string toActivateBtn in activatable.Linkage.OnActivate.Activate)
            {
                Log.Debug?.WL(1, "searching " + toActivateBtn);
                foreach (MechComponent CompToActivate in component.parent.allComponents)
                {
                    ActivatableComponent tactivatable = CompToActivate.componentDef.GetComponent <ActivatableComponent>();
                    if (tactivatable == null)
                    {
                        continue;
                    }
                    //if (tactivatable.CanBeactivatedManualy == false) { continue; };
                    if (tactivatable.ButtonName != toActivateBtn)
                    {
                        continue;
                    }
                    if (ActivatableComponent.isComponentActivated(CompToActivate) == true)
                    {
                        continue;
                    }
                    ;
                    ActivatableComponent.activateComponent(CompToActivate, true, isInital);
                }
            }
            foreach (string toActivateBtn in activatable.Linkage.OnActivate.Deactivate)
            {
                foreach (MechComponent CompToDeactivate in component.parent.allComponents)
                {
                    ActivatableComponent tactivatable = CompToDeactivate.componentDef.GetComponent <ActivatableComponent>();
                    if (tactivatable == null)
                    {
                        continue;
                    }
                    //if (tactivatable.CanBeactivatedManualy == false) { continue; };
                    if (tactivatable.ButtonName != toActivateBtn)
                    {
                        continue;
                    }
                    if (ActivatableComponent.isComponentActivated(CompToDeactivate) == false)
                    {
                        continue;
                    }
                    ;
                    ActivatableComponent.deactivateComponent(CompToDeactivate);
                }
            }
        }
Пример #2
0
 public static void AIActivatableProc(AbstractActor unit)
 {
     try {
         if (unit.IsDead)
         {
             return;
         }
         if (unit.TeamId == unit.Combat.LocalPlayerTeamGuid)
         {
             Log.Debug?.Write(" not AI\n");
             return;
         }
         HashSet <MechComponent> extreamlyUsefull = new HashSet <MechComponent>();
         HashSet <MechComponent> Usefull          = new HashSet <MechComponent>();
         var AITime = System.Diagnostics.Stopwatch.StartNew();
         // the code that you want to measure comes here
         var   elapsedMs        = AITime.ElapsedMilliseconds;
         int   isVisibleTargets = 0;
         float offenceCoeff     = 0f;
         float deffenceCoeff    = 0f;
         unit.TargetsCalc(ref isVisibleTargets, ref offenceCoeff, ref deffenceCoeff);
         Mech  mech          = unit as Mech;
         float heatCoeff     = 0f;
         float overheatCoeff = 0f;
         if (mech != null)
         {
             heatCoeff     = mech.CurrentHeat / mech.OverheatLevel;
             overheatCoeff = mech.CurrentHeatAsRatio;
         }
         Log.Debug?.Write("AI activatable coeffs:" + unit.DisplayName + ":" + unit.GUID + "\n");
         Log.Debug?.Write(" visible targets:" + isVisibleTargets + "\n");
         Log.Debug?.Write(" offenceCoeff:" + offenceCoeff + "\n");
         Log.Debug?.Write(" deffenceCoeff:" + deffenceCoeff + "\n");
         Log.Debug?.Write(" heatCoeff:" + heatCoeff + "\n");
         Log.Debug?.Write(" overheatCoeff:" + overheatCoeff + "\n");
         bool isAnyLocationExposed = unit.IsAnyStructureExposed;
         Log.Debug?.Write(" isAnyLocationExposed:" + isAnyLocationExposed + "\n");
         Log.Debug?.Write("Components:\n");
         foreach (MechComponent component in unit.allComponents)
         {
             Log.Debug?.Write(" " + component.defId + "\n");
             if (component.CanBeActivated() == false)
             {
                 Log.Debug?.Write("  can't be activated\n");
                 continue;
             }
             ;
             if (component.isSensors())
             {
                 Log.Debug?.Write("  sensors\n");
                 if (isVisibleTargets == 0)
                 {
                     if (isAnyLocationExposed == false)
                     {
                         Log.Debug?.Write("  no visible targets. Sensors are extreamly usefull to detect them\n");
                         extreamlyUsefull.Add(component);
                     }
                 }
                 else
                 if (isVisibleTargets < 2)
                 {
                     if (isAnyLocationExposed == false)
                     {
                         Log.Debug?.Write("  low visible visible targets count. Sensors are usefull to detect additional targets\n");
                         Usefull.Add(component);
                     }
                 }
             }
             if (component.isCool())
             {
                 if (heatCoeff > Core.Settings.AIHeatCoeffCoeff)
                 {
                     Log.Debug?.Write("  cooling component and i'm heated. Usefull\n");
                     Usefull.Add(component);
                 }
                 if (overheatCoeff > Core.Settings.AIOverheatCoeffCoeff)
                 {
                     Log.Debug?.Write("  cooling component and i'm close to overheat. Very usefull\n");
                     extreamlyUsefull.Add(component);
                 }
             }
             if (component.isSpeed())
             {
                 if (isAnyLocationExposed)
                 {
                     Log.Debug?.Write("  speed and i'm damaged. Need to flee. Very usefull\n");
                     extreamlyUsefull.Add(component);
                 }
                 else
                 if (isVisibleTargets == 0)
                 {
                     Log.Debug?.Write("  speed and no visible targets. Need to find usefull\n");
                     Usefull.Add(component);
                 }
             }
             if (component.isOffence())
             {
                 if ((isVisibleTargets > 1) && (isAnyLocationExposed))
                 {
                     Log.Debug?.Write("  offence and i see target and damaged. Very usefull\n");
                     extreamlyUsefull.Add(component);
                 }
                 else if (offenceCoeff > Core.Settings.AIOffenceUsefullCoeff)
                 {
                     Log.Debug?.Write("  offence and i can make much damage. Very usefull\n");
                     extreamlyUsefull.Add(component);
                 }
                 else if (offenceCoeff > Core.Epsilon)
                 {
                     Log.Debug?.Write("  offence and i can make some damage. Usefull\n");
                     Usefull.Add(component);
                 }
             }
             if (component.isDefence())
             {
                 if ((isVisibleTargets > 1) && (isAnyLocationExposed))
                 {
                     Log.Debug?.Write("  defence and i see target and damaged. Very usefull\n");
                     extreamlyUsefull.Add(component);
                 }
                 else if (deffenceCoeff > Core.Settings.AIDefenceUsefullCoeff)
                 {
                     Log.Debug?.Write("  defence and i can suffer much damage. Very usefull\n");
                     extreamlyUsefull.Add(component);
                 }
                 else if (deffenceCoeff > Core.Epsilon)
                 {
                     Log.Debug?.Write("  defence and i suffer make some damage. Usefull\n");
                     Usefull.Add(component);
                 }
             }
         }
         HashSet <MechComponent> stepDownComponents = new HashSet <MechComponent>();
         foreach (MechComponent component in extreamlyUsefull)
         {
             if (component.isHeat())
             {
                 if (heatCoeff > Core.Settings.AIHeatCoeffCoeff)
                 {
                     Log.Debug?.Write(component.defId + " very usefull. But i'm heated\n");
                     stepDownComponents.Add(component);
                 }
                 else
                 if (overheatCoeff > Core.Settings.AIOverheatCoeffCoeff)
                 {
                     Log.Debug?.Write(component.defId + " very usefull. But i'm overheated\n");
                     stepDownComponents.Add(component);
                 }
             }
         }
         foreach (MechComponent component in stepDownComponents)
         {
             extreamlyUsefull.Remove(component);
             Usefull.Add(component);
         }
         Log.Debug?.Write("Activation:\n");
         foreach (MechComponent component in unit.allComponents)
         {
             Log.Debug?.Write(" " + component.defId + "\n");
             bool needToBeActivated = false;
             if (extreamlyUsefull.Contains(component))
             {
                 Log.Debug?.Write("  very usefull\n");
                 if (component.isFailDanger() == false)
                 {
                     Log.Debug?.Write("  not danger\n");
                     needToBeActivated = true;
                 }
                 else
                 {
                     Log.Debug?.Write("  FailChance:" + component.FailChance() + "\n");
                     if ((component.isDamaged() == false) || (isAnyLocationExposed == true))
                     {
                         if (component.FailChance() < Core.Settings.AIComponentExtreamlyUsefulModifyer)
                         {
                             needToBeActivated = true;
                             Log.Debug?.Write("  not big danger. Can be activated\n");
                         }
                     }
                     else
                     {
                         Log.Debug?.Write("  component is damaged and have no exposed locations. Danger level increase\n");
                         if (component.FailChance() < Core.Settings.AIComponentUsefullModifyer)
                         {
                             needToBeActivated = true;
                             Log.Debug?.Write("  not big danger. Can be activated\n");
                         }
                     }
                 }
             }
             if (Usefull.Contains(component))
             {
                 Log.Debug?.Write("  usefull\n");
                 if (component.isFailDanger() == false)
                 {
                     Log.Debug?.Write("  not danger\n");
                     needToBeActivated = true;
                 }
                 else
                 {
                     Log.Debug?.Write("  FailChance:" + component.FailChance() + "\n");
                     if ((component.isDamaged() == false) || (isAnyLocationExposed == true))
                     {
                         if (component.FailChance() < Core.Settings.AIComponentUsefullModifyer)
                         {
                             needToBeActivated = true;
                             Log.Debug?.Write("  not big danger. Can be activated\n");
                         }
                     }
                     else
                     {
                         Log.Debug?.Write("  component is damaged and have no exposed locations. Danger level increase will not activate.\n");
                     }
                 }
             }
             if (needToBeActivated)
             {
                 if (ActivatableComponent.isComponentActivated(component))
                 {
                     Log.Debug?.Write("  already active\n");
                     if (unit.AICheatRoll())
                     {
                         bool isSuccess = ActivatableComponent.rollFail(component, false, true);
                         if (isSuccess == false)
                         {
                             Log.Debug?.Write("  deactivating due to high possible fail\n");
                             ActivatableComponent.deactivateComponent(component);
                         }
                         else
                         {
                             component.setAIRollPassed(true);
                         }
                     }
                 }
                 else
                 {
                     Log.Debug?.Write("  activating\n");
                     if (unit.AICheatRoll())
                     {
                         bool isSuccess = ActivatableComponent.rollFail(component, false, true);
                         if (isSuccess == false)
                         {
                             Log.Debug?.Write("  not activate due to high possible fail\n");
                         }
                         else
                         {
                             component.setAIRollPassed(true);
                             ActivatableComponent.activateComponent(component, false, false);
                         }
                     }
                     else
                     {
                         ActivatableComponent.activateComponent(component, false, false);
                     }
                 }
             }
             else
             {
                 if (ActivatableComponent.isComponentActivated(component))
                 {
                     Log.Debug?.Write("  not active\n");
                 }
                 else
                 {
                     Log.Debug?.Write("  deactivating\n");
                     ActivatableComponent.deactivateComponent(component);
                 }
             }
         }
         AITime.Stop();
         Log.Debug?.Write("AI activatable time:" + AITime.ElapsedMilliseconds + " msec\n");
     } catch (Exception e) {
         Log.WriteCritical("AI activatable exception:" + e.ToString() + "\n");
     }
 }