Example #1
0
        private static IEnumerator CallBack(SkirmishMechBayPanel panel)
        {
            yield return(new WaitForEndOfFrame());

            try
            {
                if (mechDefsIterator == null)
                {
                    mechDefsIterator = panel.allMechs.GetEnumerator();
                }
                while (mechDefsIterator.MoveNext())
                {
                    var mechDef        = mechDefsIterator.Current;
                    var screenshotPath = DebugScreenshotMechsFeature.Shared.ScreenshotPath(mechDef);
                    if (File.Exists(screenshotPath))
                    {
                        continue;
                    }
                    if (DebugScreenshotMechsFeature.Shared.Settings.OnlyInvalidMechs &&
                        MechValidationRules.ValidateMechDef(MechValidationLevel.Full, panel.dataManager, mechDef, null).All(x => !x.Value.Any()))
                    {
                        continue;
                    }
                    panel.SelectMech(mechDef);
                    panel.OnEditMechClicked();
                    break;
                }
            }
            catch (Exception e)
            {
                Control.mod.Logger.LogError(e);
            }
        }
 internal static void Postfix(MechValidationLevel validationLevel, DataManager dataManager, MechDef mechDef, ref Dictionary <MechValidationType, List <Text> > __result)
 {
     if (validationLevel == MechValidationLevel.MechLab)
     {
         MechValidationRules.ValidateMechInventorySlots(dataManager, mechDef, ref __result);
     }
 }
            public static bool Prefix(SimGameState __instance)
            {
                try
                {
                    Logger.Debug($"[SimGameState_ShowMechRepairsNeededNotif_PREFIX] Called");

                    List <MechDef> fieldableActiveMechs = new List <MechDef>();
                    foreach (MechDef mechDef in __instance.ActiveMechs.Values)
                    {
                        if (MechValidationRules.ValidateMechCanBeFielded(__instance, mechDef))
                        {
                            fieldableActiveMechs.Add(mechDef);
                        }
                    }

                    if (fieldableActiveMechs.Count > 4)
                    {
                        Logger.Debug($"[SimGameState_ShowMechRepairsNeededNotif_PREFIX] Suppress repair notification as we have more than 4 fieldable Mechs");
                        return(false);
                    }
                    return(true);
                }
                catch (Exception e)
                {
                    Logger.Error(e);
                    return(true);
                }
            }
 public static List <string> GetValidationErrors(Dictionary <MechValidationType, List <string> > errorMessages, List <MechValidationType> validationTypes)
 {
     if (_isSimGame)
     {
         validationTypes.Add(MechValidationType.InvalidInventorySlots);
     }
     return(MechValidationRules.GetValidationErrors(errorMessages, validationTypes));
 }
Example #5
0
        public static bool ValidateMech(MechDef m, SimGameState s)
        {
            bool r = true;

            foreach (KeyValuePair <MechValidationType, List <Text> > kv in MechValidationRules.ValidateMechDef(MechValidationLevel.Full, s.DataManager, m, null))
            {
                foreach (Text t in kv.Value)
                {
                    BTRandomMechComponentUpgrader_Init.Log.Log($"validation error: {t}");
                    r = false; // dont return here, to log all errors
                }
            }
            return(r);
        }
        public static void Prefix(SkirmishSettings_Beta __instance, UIManager ___uiManager, ref List <MechDef> ___stockMechs)
        {
            try
            {
                var mechDefs = ___uiManager.dataManager.MechDefs.Select(pair => pair.Value).ToList();
                AutoFixer.Shared.FixMechDef(mechDefs);

                ___stockMechs = mechDefs.Where(x => MechValidationRules.MechIsValidForSkirmish(x, false)).ToList();
            }
            catch (Exception e)
            {
                Control.LogError(e);
            }
        }
Example #7
0
        public static List <Text> GetValidationErrors(Dictionary <MechValidationType, List <Text> > errorMessages, List <MechValidationType> validationTypes)
        {
            try
            {
                if (_isSimGame)
                {
                    validationTypes.Remove(MechValidationType.InvalidInventorySlots);
                }
            }
            catch (Exception e)
            {
                Control.Logger.Error.Log(e);
            }

            return(MechValidationRules.GetValidationErrors(errorMessages, validationTypes));
        }
            public static void Postfix(LanceConfiguratorPanel __instance, LanceLoadoutSlot[] ___loadoutSlots)
            {
                ModState.IsSimulatorMission = false;
                if (ModInit.Settings.enableSimulationHotKey)
                {
                    var hk = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
                    if (hk)
                    {
                        ModState.IsSimulatorMission = true;
                    }
                }

                bool flag = false;

                foreach (LanceLoadoutSlot lanceLoadoutSlot in ___loadoutSlots)
                {
                    if (lanceLoadoutSlot.SelectedMech != null)
                    {
                        List <Text> mechFieldableWarnings =
                            MechValidationRules.GetMechFieldableWarnings(__instance.dataManager,
                                                                         lanceLoadoutSlot.SelectedMech.MechDef);
                        if (mechFieldableWarnings.Count > 0)
                        {
                            flag = true;
                        }
                    }
                }
                if (!ModState.runContinueConfirmClickedPost && flag)
                {
                    return;
                }


//                ModState.IsTrainingMission = false;
                ModState.AIGetsPlayerMechs = false;
                ModState.PlayerGetsAIMechs = false;
//                ModState.successReq = "";
                ModState.playerMechs   = new List <ModState.playerMechStore>();
                ModState.AIMechs       = new List <ModState.playerMechStore>();
                ModState.deployedMechs = new List <MechDef>();
                ModState.contractID    = __instance.activeContract.Override.ID;

                if (ModInit.Settings.enableSimulationHotKey)
                {
                    var hk = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
                    if (hk)
                    {
                        ModState.IsSimulatorMission = true;
                    }
                }

                var nonNullSlots = ___loadoutSlots.Where(x => x.SelectedMech != null);

                MechDef newMech;
                var     lanceLoadoutSlots = nonNullSlots as LanceLoadoutSlot[] ?? nonNullSlots.ToArray();

                if (ModInit.Settings.SwapUnitsWithAIContractIDs.ContainsKey(contractID))
                {
                    ModState.AIGetsPlayerMechs = true;
                    ModState.PlayerGetsAIMechs = true;
                    foreach (var slot in lanceLoadoutSlots)
                    {
                        if (slot.SelectedMech?.MechDef == null)
                        {
                            continue;
                        }
                        var newGUID = Guid.NewGuid().ToString();
                        newMech = new MechDef(slot.SelectedMech.MechDef, newGUID, true);

                        ModState.playerMechs.Add(new ModState.playerMechStore(newMech, 0));
                        ModInit.modLog.LogMessage($"Added {slot.SelectedMech.MechDef.Description.Name} to player mechs for AI use.");
                        ModState.deployedMechs.Add(slot.SelectedMech.MechDef);
                    }
                }

                else if (ModInit.Settings.DoppelgangerContractIDs.ContainsKey(contractID))
                {
                    ModState.AIGetsPlayerMechs = true;
                    foreach (var slot in lanceLoadoutSlots)
                    {
                        if (slot.SelectedMech?.MechDef == null)
                        {
                            continue;
                        }
                        var newGUID = Guid.NewGuid().ToString();
                        newMech = new MechDef(slot.SelectedMech.MechDef, newGUID, true);

                        ModState.playerMechs.Add(new ModState.playerMechStore(newMech, 0));
                        ModInit.modLog.LogMessage($"Added {slot.SelectedMech.MechDef.Description.Name} to player mechs for AI use.");
                    }
                }

                if (ModInit.Settings.TrainingContractIDs.ContainsKey(contractID) || ModState.IsSimulatorMission || ModState.DynamicTrainingMissionsDict.ContainsKey(__instance.activeContract.GenerateID()))
                {
//                    ModState.successReq = ModInit.Settings.TrainingContractIDs[contractID];
//                    ModState.IsTrainingMission = true;
                    foreach (var slot in lanceLoadoutSlots)
                    {
                        ModState.deployedMechs.Add(slot.SelectedMech.MechDef);
                        ModInit.modLog.LogMessage($"Adding {slot.SelectedMech.MechDef.Name} to deployedMechs for restore");
                    }
                }
            }
Example #9
0
            public static bool Prefix(MechBayPanel __instance, MechBayMechUnitElement mechElement)
            {
                Logger.LogDebug("We are repairing, yes?");
                Logger.Log("We are repairing, yes?");
                if (!Core.Settings.RepairRearm)
                {
                    return(true);
                }

                var     sim     = UnityGameInstance.BattleTechGame.Simulation;
                MechDef mechDef = mechElement.MechDef;
                WorkOrderEntry_MechLab workOrderEntry_MechLab = __instance.Sim.GetWorkOrderEntryForMech(mechDef);
                bool flag = false;

                for (int i = 0; i < mechDef.Inventory.Length; i++)
                {
                    MechComponentRef mechComponentRef = mechDef.Inventory[i];
                    if (mechComponentRef.DamageLevel != ComponentDamageLevel.Functional && mechComponentRef.DamageLevel
                        != ComponentDamageLevel.Installing && !MechValidationRules.MechComponentUnderMaintenance(mechComponentRef, MechValidationLevel.MechLab, workOrderEntry_MechLab))
                    {
                        flag = true;
                        break;
                    }
                }
                if (!mechDef.IsDamaged && !flag)
                {
                    return(false);
                }
                List <ChassisLocations> list = new List <ChassisLocations>();

                __instance.pendingWorkOrderNew = false;
                __instance.pendingWorkOrderEntriesToAdd.Clear();
                if (workOrderEntry_MechLab == null)
                {
                    workOrderEntry_MechLab = new WorkOrderEntry_MechLab(WorkOrderType.MechLabGeneric, "MechLab-BaseWorkOrder", Strings.T("Modify 'Mech - {0}", new object[]
                    {
                        mechDef.Description.Name
                    }), mechDef.GUID, 0, Strings.T(__instance.Sim.Constants.Story.GeneralMechWorkOrderCompletedText, new object[]
                    {
                        mechDef.Description.Name
                    }));
                    workOrderEntry_MechLab.SetMechDef(mechDef);
                    __instance.pendingWorkOrderNew = true;
                }
                __instance.pendingWorkOrder = workOrderEntry_MechLab;
                if (!MechValidationRules.MechStructureUnderMaintenance(ChassisLocations.Head, MechValidationLevel.MechLab, workOrderEntry_MechLab) && mechDef.Head.CurrentInternalStructure < mechDef.Chassis.Head.InternalStructure)
                {
                    list.Add(ChassisLocations.Head);
                }
                if (!MechValidationRules.MechStructureUnderMaintenance(ChassisLocations.CenterTorso, MechValidationLevel.MechLab, workOrderEntry_MechLab) && mechDef.CenterTorso.CurrentInternalStructure < mechDef.Chassis.CenterTorso.InternalStructure)
                {
                    list.Add(ChassisLocations.CenterTorso);
                }
                if (!MechValidationRules.MechStructureUnderMaintenance(ChassisLocations.LeftTorso, MechValidationLevel.MechLab, workOrderEntry_MechLab) && mechDef.LeftTorso.CurrentInternalStructure < mechDef.Chassis.LeftTorso.InternalStructure)
                {
                    list.Add(ChassisLocations.LeftTorso);
                }
                if (!MechValidationRules.MechStructureUnderMaintenance(ChassisLocations.RightTorso, MechValidationLevel.MechLab, workOrderEntry_MechLab) && mechDef.RightTorso.CurrentInternalStructure < mechDef.Chassis.RightTorso.InternalStructure)
                {
                    list.Add(ChassisLocations.RightTorso);
                }
                if (!MechValidationRules.MechStructureUnderMaintenance(ChassisLocations.LeftLeg, MechValidationLevel.MechLab, workOrderEntry_MechLab) && mechDef.LeftLeg.CurrentInternalStructure < mechDef.Chassis.LeftLeg.InternalStructure)
                {
                    list.Add(ChassisLocations.LeftLeg);
                }
                if (!MechValidationRules.MechStructureUnderMaintenance(ChassisLocations.RightLeg, MechValidationLevel.MechLab, workOrderEntry_MechLab) && mechDef.RightLeg.CurrentInternalStructure < mechDef.Chassis.RightLeg.InternalStructure)
                {
                    list.Add(ChassisLocations.RightLeg);
                }
                if (!MechValidationRules.MechStructureUnderMaintenance(ChassisLocations.LeftArm, MechValidationLevel.MechLab, workOrderEntry_MechLab) && mechDef.LeftArm.CurrentInternalStructure < mechDef.Chassis.LeftArm.InternalStructure)
                {
                    list.Add(ChassisLocations.LeftArm);
                }
                if (!MechValidationRules.MechStructureUnderMaintenance(ChassisLocations.RightArm, MechValidationLevel.MechLab, workOrderEntry_MechLab) && mechDef.RightArm.CurrentInternalStructure < mechDef.Chassis.RightArm.InternalStructure)
                {
                    list.Add(ChassisLocations.RightArm);
                }
                if (list.Count < 1 && !flag)
                {
                    GenericPopupBuilder.Create("Repair Already Ordered", string.Format("A repair order has already been queued for " +
                                                                                       "{0}", mechDef.Name)).AddFader(new UIColorRef?(LazySingletonBehavior <UIManager> .Instance.UILookAndColorConstants.PopupBackfill), 0f, true).Render();
                    __instance.OnRepairAllCancelled();
                    return(false);
                }
                int num  = 0;
                int num2 = 0;

                for (int j = 0; j < list.Count; j++)
                {
                    LocationDef        chassisLocationDef = mechDef.GetChassisLocationDef(list[j]);
                    LocationLoadoutDef locationLoadoutDef = mechDef.GetLocationLoadoutDef(list[j]);
                    int structureCount = Mathf.RoundToInt(Mathf.Max(0f, chassisLocationDef.InternalStructure - locationLoadoutDef.CurrentInternalStructure));
                    WorkOrderEntry_RepairMechStructure workOrderEntry_RepairMechStructure = __instance.Sim.CreateMechRepairWorkOrder(mechDef.GUID, list[j], structureCount);
                    __instance.pendingWorkOrderEntriesToAdd.Add(workOrderEntry_RepairMechStructure);
                    num  += workOrderEntry_RepairMechStructure.GetCost();
                    num2 += workOrderEntry_RepairMechStructure.GetCBillCost();
                }
                StringBuilder stringBuilder = new StringBuilder();
                int           num3          = 0;

                for (int k = 0; k < mechDef.Inventory.Length; k++)
                {
                    MechComponentRef mechComponentRef2 = mechDef.Inventory[k];
                    if (string.IsNullOrEmpty(mechComponentRef2.SimGameUID))
                    {
                        mechComponentRef2.SetSimGameUID(__instance.Sim.GenerateSimGameUID());
                    }
                    if (mechComponentRef2.DamageLevel == ComponentDamageLevel.Destroyed)
                    {
                        if (num3 < 1)
                        {
                            stringBuilder.Append("\n\nThe following components have been Destroyed. If you continue with the Repair, " +
                                                 "replacement Components will NOT be installed. If you want to replace them with identical or " +
                                                 "different Components, you must Refit the 'Mech.\n\n");
                        }
                        if (num3 < 5)
                        {
                            stringBuilder.Append(mechComponentRef2.MountedLocation.ToString());
                            stringBuilder.Append(": ");
                            stringBuilder.Append(mechComponentRef2.Def.Description.Name);
                            stringBuilder.Append("\n");
                        }
                        num3++;
                        WorkOrderEntry_InstallComponent workOrderEntry_InstallComponent = sim.CreateComponentInstallWorkOrder(__instance.selectedMech.MechDef.GUID,
                                                                                                                              mechComponentRef2, ChassisLocations.None, mechComponentRef2.MountedLocation);
                        __instance.pendingWorkOrderEntriesToAdd.Insert(0, workOrderEntry_InstallComponent);
                        num  += workOrderEntry_InstallComponent.GetCost();
                        num2 += workOrderEntry_InstallComponent.GetCBillCost();
                    }
                    else if (mechComponentRef2.DamageLevel != ComponentDamageLevel.Functional && mechComponentRef2.DamageLevel != ComponentDamageLevel.Installing)
                    {
                        WorkOrderEntry_RepairComponent workOrderEntry_RepairComponent = __instance.Sim.CreateComponentRepairWorkOrder(mechComponentRef2, true);
                        __instance.pendingWorkOrderEntriesToAdd.Add(workOrderEntry_RepairComponent);
                        num  += workOrderEntry_RepairComponent.GetCost();
                        num2 += workOrderEntry_RepairComponent.GetCBillCost();
                    }
                }

                foreach (var foo in __instance.pendingWorkOrderEntriesToAdd)
                {
                    Logger.LogDebug(foo.ID);
                }
                Logger.LogDebug("Armor Repair Section");
                float armorLoss = 1;
                bool  armorTag  = false;

                foreach (var tag in mechDef.MechTags)
                {
                    Logger.LogDebug(tag);
                    if (tag.StartsWith("XLRPArmor"))
                    {
                        armorTag = true;
                        string[] parsedString = tag.Split('_');
                        armorLoss = float.Parse(parsedString[1]);
                    }
                    Logger.LogDebug(armorLoss.ToString());
                }
                if (armorTag)
                {
                    if (!mechDef.MechTags.Contains("XLRP_Armor_Repairing"))
                    {
                        mechDef.MechTags.Add("XLRP_Armor_Repairing");
                    }
                    int brokenArmor = (int)((1 - armorLoss) * mechDef.MechDefAssignedArmor);
                    int frontArmor  = (int)(mechDef.MechDefAssignedArmor - mechDef.CenterTorso.AssignedRearArmor -
                                            mechDef.LeftTorso.AssignedRearArmor - mechDef.RightTorso.AssignedRearArmor);
                    int rearArmor = (int)(mechDef.CenterTorso.AssignedRearArmor +
                                          mechDef.LeftTorso.AssignedRearArmor + mechDef.RightTorso.AssignedRearArmor);
                    Logger.LogDebug($"brokenAmor: {brokenArmor}, frontArmor: {frontArmor}, rearArmor: {rearArmor}");
                    WorkOrderEntry_ModifyMechArmor subEntry = sim.CreateMechArmorModifyWorkOrder(__instance.selectedMech.MechDef.GUID,
                                                                                                 ChassisLocations.All, brokenArmor, frontArmor, rearArmor);

                    __instance.pendingWorkOrderEntriesToAdd.Add(subEntry);
                    num  += subEntry.GetCost();
                    num2 += subEntry.GetCBillCost();
                }

                num = Mathf.Max(1, Mathf.CeilToInt((float)num / (float)__instance.Sim.MechTechSkill));
                if (num3 > 5)
                {
                    stringBuilder.Append(Strings.T("...\nAnd {0} additional destroyed components.\n", new object[]
                    {
                        num3 - 5
                    }));
                }
                string body = Strings.T("Repairing {0} will cost {1:n0} C-Bills and take {2} Days.{3}\n\nProceed?", new object[]
                {
                    mechDef.Name,
                    num2,
                    num,
                    stringBuilder.ToString()
                });

                GenericPopupBuilder.Create("Repair 'Mech?", body).AddButton("Cancel", new Action(__instance.OnRepairAllCancelled), true, null).
                AddButton("Repair", new Action(__instance.OnRepairAllAccepted), true, null).AddFader(new UIColorRef?(LazySingletonBehavior <UIManager> .Instance.UILookAndColorConstants.PopupBackfill), 0f, true).Render();
                return(false);
            }
Example #10
0
        internal static void Validate(List <MechDef> mechdefs)
        {
            foreach (var mechDef in mechdefs)
            {
                try
                {
                    var dm   = UnityGameInstance.BattleTechGame.DataManager;
                    var work = new WorkOrderEntry_MechLab(WorkOrderType.MechLabGeneric, "test", "test", "test", 0);

                    if (mechDef == null)
                    {
                        Control.LogError("NullMECHDEF!");
                        continue;
                    }
                    if (Control.Settings.IgnoreValidationTags != null && Control.Settings.IgnoreValidationTags.Length > 0)
                    {
                        bool skip = false;
                        foreach (var tag in Control.Settings.IgnoreValidationTags)
                        {
                            if ((mechDef.Chassis.ChassisTags != null && mechDef.Chassis.ChassisTags.Contains(tag)) ||
                                (mechDef.MechTags != null && mechDef.MechTags.Contains(tag)))
                            {
                                Control.LogDebug(DType.AutofixValidate, $"{mechDef.Description.Id} Ignored by {tag}");
                                skip = true;
                                break;
                            }
                        }
                        if (skip)
                        {
                            continue;
                        }
                    }

                    var error = MechValidationRules.ValidateMechDef(MechValidationLevel.Full, dm,
                                                                    mechDef, work);
                    foreach (var component in mechDef.Inventory)
                    {
                        foreach (var validator in component.GetComponents <IMechValidate>())
                        {
                            validator.ValidateMech(error, MechValidationLevel.MechLab, mechDef, component);
                        }
                    }

                    bool bad_mech = false;
                    foreach (var pair in error)
                    {
                        if (pair.Value.Count > 0)
                        {
                            if (!bad_mech)
                            {
                                bad_mech = true;
                                Control.LogDebug(DType.AutofixValidate, $"{mechDef.Description.Id} has errors:");
                            }
                            foreach (var text in pair.Value)
                            {
                                Control.LogDebug(DType.AutofixValidate, $"[{pair.Key}]:{text}");
                            }
                        }
                    }
                    if (!bad_mech && !Control.Settings.DEBUG_ShowOnlyErrors)
                    {
                        Control.LogDebug(DType.AutofixValidate, $"{mechDef.Description.Id} no errors");
                    }
                }
                catch (Exception e)
                {
                    Control.LogError($"{mechDef.Description.Id} throwed exception on validation", e);
                }
            }
        }