Пример #1
0
        public static void RemoveMechLab(string id, ComponentType type, MechLabHelper mechLab, ChassisLocations location)
        {
            var widget = mechLab.GetLocationWidget(location);

            if (widget == null)
            {
                Control.LogDebug(DType.DefaultHandle, $"DefaultHelper: Cannot remove {id} from {location} - wrong location ");
                return;
            }
            var helper = new LocationHelper(widget);

            var remove = helper.LocalInventory.FirstOrDefault(e => e.ComponentRef.ComponentDefID == id);

            if (remove == null)
            {
                Control.LogDebug(DType.DefaultHandle, $"- not found");
            }
            else if (!remove.ComponentRef.IsDefault())
            {
                Control.LogDebug(DType.DefaultHandle, $"- not default");
            }
            else
            {
                widget.OnRemoveItem(remove, true);
                Control.LogDebug(DType.DefaultHandle, $"- removed");
                remove.thisCanvasGroup.blocksRaycasts = true;
                mechLab.MechLab.dataManager.PoolGameObject(MechLabPanel.MECHCOMPONENT_ITEM_PREFAB, remove.GameObject);
            }
        }
Пример #2
0
        public override void DoChange(MechLabHelper mechLab, LocationHelper loc)
        {
            Control.LogDebug(DType.ComponentInstall, $"-- AddFromInventoryChange: {item.ComponentRef.ComponentDefID} to {location}");

            var widget = location == loc.widget.loadout.Location ? loc.widget : mechLab.GetLocationWidget(location);

            if (widget == null)
            {
                return;
            }

            widget.OnAddItem(item, false);
            Control.LogDebug(DType.ComponentInstall, "--- added");


            if (mechLab.MechLab.IsSimGame)
            {
                Control.LogDebug(DType.ComponentInstall, "--- not default: create work order");
                WorkOrderEntry_InstallComponent subEntry = mechLab.MechLab.Sim.CreateComponentInstallWorkOrder(
                    mechLab.MechLab.baseWorkOrder.MechID,
                    item.ComponentRef, widget.loadout.Location, item.MountedLocation);
                mechLab.MechLab.baseWorkOrder.AddSubEntry(subEntry);
            }
            item.MountedLocation = location;
        }
Пример #3
0
        public void RemoveLinked(IMechLabDraggableItem item, MechLabPanel mechLab)
        {
            var helper = new MechLabHelper(mechLab);

            foreach (var r_link in Links)
            {
                Control.LogDebug(DType.ComponentInstall, $"{r_link.ComponentDefId} from {r_link.Location}");
                DefaultHelper.RemoveMechLab(r_link.ComponentDefId, r_link.ComponentDefType ?? Def.ComponentType, helper, r_link.Location);
            }
        }
Пример #4
0
        public override void DoChange(MechLabHelper mechLab, LocationHelper loc)
        {
            Control.LogDebug(DType.ComponentInstall, $"-- AddFromInventoryChange: {item.ComponentRef.ComponentDefID} to {location}");

            var widget = location == loc.widget.loadout.Location ? loc.widget : mechLab.GetLocationWidget(location);

            if (widget == null)
            {
                return;
            }

            widget.OnAddItem(item, false);
            Control.LogDebug(DType.ComponentInstall, "--- added");
            item.MountedLocation = location;
        }
Пример #5
0
        private static MechLabItemSlotElement search_item(MechLabHelper mechlab,
                                                          Predicate <MechComponentDef> SearchTerms)
        {
            Control.LogDebug(DType.ComponentInstall, $"- bin search");
            if (mechlab.MechLab.sim != null)
            {
                foreach (var slot in mechlab.DismountWidget.localInventory)
                {
                    if (slot.ComponentRef?.Def == null)
                    {
                        continue;
                    }

                    if (SearchTerms(slot.ComponentRef.Def))
                    {
                        Control.LogDebug(DType.ComponentInstall, $"-- found {slot.ComponentRef.ComponentDefID}");
                        slot.RemoveFromParent();
                        return(slot);
                    }
                }
            }
            else
            {
                Control.LogDebug(DType.ComponentInstall, $"-- not sim game, skipped");
            }

            Control.LogDebug(DType.ComponentInstall, $"- inventory search");
            foreach (var inventoryItem in mechlab.InventoryWidget.localInventory)
            {
                if (inventoryItem.ComponentRef?.Def == null)
                {
                    continue;
                }

                if (SearchTerms(inventoryItem.ComponentRef.Def))
                {
                    inventoryItem.RemoveFromParent();
                    var slot = mechlab.MechLab.CreateMechComponentItem(inventoryItem.ComponentRef, true,
                                                                       inventoryItem.MountedLocation, inventoryItem.DropParent, inventoryItem);
                    Control.LogDebug(DType.ComponentInstall, $"-- found {slot.ComponentRef.ComponentDefID}");
                    return(slot);
                }
            }

            Control.LogDebug(DType.ComponentInstall, $"- not found");

            return(null);
        }
Пример #6
0
        internal static void AddMechLab(MechComponentRef replace, MechLabHelper mechLab)
        {
            Control.LogDebug(DType.DefaultHandle, $"DefaultHelper.AddMechLab: adding {replace.ComponentDefID} to {replace.MountedLocation}");

            var target = mechLab.GetLocationWidget(replace.MountedLocation);

            if (target == null)
            {
                Control.LogDebug(DType.DefaultHandle, $"DefaultHelper: Cannot add - wrong location ");
                return;
            }

            var slot = CreateSlot(replace, mechLab.MechLab);

            slot.MountedLocation = replace.MountedLocation;
            target.OnAddItem(slot, false);
        }
Пример #7
0
        public static void AddMechLab(string id, ComponentType type, MechLabHelper mechLab, ChassisLocations location)
        {
            Control.LogDebug(DType.DefaultHandle, $"DefaultHelper.AddMechLab: adding {id} to {location}");

            var target = mechLab.GetLocationWidget(location);

            if (target == null)
            {
                Control.LogDebug(DType.DefaultHandle, $"DefaultHelper: Cannot add {id} to {location} - wrong location ");
                return;
            }

            var slot = CreateSlot(id, type, mechLab.MechLab);

            slot.MountedLocation = location;
            target.OnAddItem(slot, false);
        }
Пример #8
0
        public override void DoChange(MechLabHelper mechLab, LocationHelper loc)
        {
            Control.LogDebug(DType.ComponentInstall, $"-- RemoveChange: {item.ComponentRef.ComponentDefID} from {location}");
            var widget = location == loc.widget.loadout.Location ? loc.widget : mechLab.GetLocationWidget(location);

            if (widget == null)
            {
                return;
            }

            widget.OnRemoveItem(item, true);
            Control.LogDebug(DType.ComponentInstall, $"--- removed");
            if (item.ComponentRef.Is <Flags>(out var f) && f.Default)
            {
                Control.LogDebug(DType.ComponentInstall, $"--- Default: clear");
                item.thisCanvasGroup.blocksRaycasts = true;
                mechLab.MechLab.dataManager.PoolGameObject(MechLabPanel.MECHCOMPONENT_ITEM_PREFAB, item.gameObject);
            }
Пример #9
0
        internal static void EnterMechLab(MechLabPanel mechlab)
        {
            if (Control.Settings.DEBUG_ShowMechUT)
            {
                var ut = mechlab.activeMechDef.GetUnitTypes();
                if (ut == null)
                {
                    Control.Log($"Enter MechLab for {mechlab.activeMechDef.Description.Id}, UT:[ ]");
                }
                else
                {
                    Control.Log($"Enter MechLab for {mechlab.activeMechDef.Description.Id}, UT:[{ut.Join()}]");
                }
            }

            mechlab_instance = new MechLabHelper(mechlab);
            mechlab_instance.MakeLocations();

            mechlab_instance.MakeHardpoints();
            mechlab_instance.RefreshHardpoints();
        }
Пример #10
0
        public IEnumerable <IChange> ValidateDropOnRemove(MechLabItemSlotElement item, LocationHelper location, MechLabHelper mechlab, List <IChange> changes)
        {
            var replace = DefaultFixer.Shared.GetReplaceFor(mechlab.MechLab.activeMechDef, CategoryID, item.MountedLocation, mechlab.MechLab.sim);

            if (replace == null)
            {
                Control.LogDebug(DType.ComponentInstall, $"--- Category {CategoryID} - no replace, return");
                yield break;
            }

            foreach (var addChange in changes.OfType <AddChange>())
            {
                if (addChange.item.ComponentRef.IsCategory(CategoryID))
                {
                    Control.LogDebug(DType.ComponentInstall, $"--- Category {CategoryID} - replace already added");
                    yield break;
                }
            }

            Control.LogDebug(DType.ComponentInstall, $"--- Category {CategoryID} - add replace {replace.ComponentDefID}");

            yield return(new AddDefaultChange(replace.MountedLocation, DefaultHelper.CreateSlot(replace, mechlab.MechLab)));
        }
Пример #11
0
 public IEnumerable <IChange> ValidateDropOnAdd(MechLabItemSlotElement item, LocationHelper location, MechLabHelper mechlab, List <IChange> changes)
 {
     yield break;
 }
Пример #12
0
        public string PreValidateDrop(MechLabItemSlotElement item, LocationHelper location, MechLabHelper mechlab)
        {
            Control.LogDebug(DType.ComponentInstall, $"-- Category {CategoryID}");

            if (!CategoryDescriptor.AllowMixTags && mechlab.MechLab.activeMechDef.Inventory.Any(i => i.Def.IsCategory(CategoryID, out var c) && GetTag() != c.GetTag()))
            {
                return(string.Format(CategoryDescriptor.AddMixed, CategoryDescriptor.DisplayName));
            }

            return(string.Empty);
        }
Пример #13
0
        public string ReplaceValidateDrop(MechLabItemSlotElement drop_item, LocationHelper location, List <IChange> changes)
        {
            Control.LogDebug(DType.ComponentInstall, $"-- Category {CategoryID}");

            if (!CategoryDescriptor.AutoReplace ||
                CategoryDescriptor.MaxEquiped <= 0 && CategoryDescriptor.MaxEquipedPerLocation <= 0)
            {
                Control.LogDebug(DType.ComponentInstall, $"--- no replace needed");
                return(String.Empty);
            }

            if (changes.OfType <RemoveChange>().Select(i => i.item.ComponentRef).Any(i => i.IsCategory(CategoryID)))
            {
                Control.LogDebug(DType.ComponentInstall, $"--- replace already found");
                return(string.Empty);
            }


            var mech = location.mechLab.activeMechDef;

            if (CategoryDescriptor.MaxEquiped > 0)
            {
                var n = location.mechLab.activeMechDef.Inventory.Count(i => i.Def.IsCategory(CategoryID));
                Control.LogDebug(DType.ComponentInstall, $"--- MaxEquiped: {n}/{CategoryDescriptor.MaxEquiped}");

                if (n >= CategoryDescriptor.MaxEquiped)
                {
                    var replace = location.LocalInventory
                                  .FirstOrDefault(i => i.ComponentRef.Def.IsCategory(CategoryID) && !i.ComponentRef.IsModuleFixed(mech));

                    if (CategoryDescriptor.ReplaceAnyLocation && replace == null)
                    {
                        var mechlab = new MechLabHelper(location.mechLab);
                        foreach (var widget in mechlab.GetWidgets())
                        {
                            if (widget.loadout.Location == location.widget.loadout.Location)
                            {
                                continue;
                            }

                            var loc_helper = new LocationHelper(widget);
                            replace = loc_helper.LocalInventory
                                      .FirstOrDefault(i => i.ComponentRef.Def.IsCategory(CategoryID) && !i.ComponentRef.IsModuleFixed(mech));
                            if (replace != null)
                            {
                                break;
                            }
                        }
                    }
                    Control.LogDebug(DType.ComponentInstall, $"--- replace: {(replace == null ? "none" : replace.ComponentRef.ComponentDefID)}");

                    if (replace == null)
                    {
                        Control.LogDebug(DType.ComponentInstall, $"--- return error");

                        if (CategoryDescriptor.MaxEquiped > 1)
                        {
                            return(string.Format(CategoryDescriptor.AddMaximumReached, CategoryDescriptor.displayName,
                                                 n));
                        }
                        else
                        {
                            return(string.Format(CategoryDescriptor.AddAlreadyEquiped, CategoryDescriptor.displayName));
                        }
                    }
                    Control.LogDebug(DType.ComponentInstall, $"--- return replace");

                    changes.Add(new RemoveChange(replace.MountedLocation, replace));

                    return(string.Empty);
                }
            }

            if (CategoryDescriptor.MaxEquipedPerLocation > 0)
            {
                var n = location.LocalInventory.Count(i => i.ComponentRef.Def.IsCategory(CategoryID));
                Control.LogDebug(DType.ComponentInstall, $"--- MaxEquipedPerLocation: {n}/{CategoryDescriptor.MaxEquipedPerLocation}");

                if (n >= CategoryDescriptor.MaxEquipedPerLocation)
                {
                    var replace = location.LocalInventory
                                  .FirstOrDefault(i => i.ComponentRef.Def.IsCategory(CategoryID) && !i.ComponentRef.IsModuleFixed(mech));
                    Control.LogDebug(DType.ComponentInstall, $"--- replace: {(replace == null ? "none" : replace.ComponentRef.ComponentDefID)}");

                    if (replace == null)
                    {
                        Control.LogDebug(DType.ComponentInstall, $"--- return error");

                        if (CategoryDescriptor.MaxEquipedPerLocation > 1)
                        {
                            return(string.Format(CategoryDescriptor.AddMaximumLocationReached,
                                                 CategoryDescriptor.displayName, n, location.LocationName));
                        }
                        else
                        {
                            return(string.Format(CategoryDescriptor.AddAlreadyEquipedLocation,
                                                 CategoryDescriptor.displayName, location.LocationName));
                        }
                    }
                    Control.LogDebug(DType.ComponentInstall, $"--- return replace");
                    changes.Add(new RemoveChange(replace.MountedLocation, replace));
                }
            }
            return(string.Empty);
        }
Пример #14
0
        public IEnumerable <IChange> ValidateDropOnAdd(MechLabItemSlotElement item, LocationHelper location, MechLabHelper mechlab, List <IChange> changes)
        {
            Control.LogDebug(DType.ComponentInstall, "--- AutoLinked Add");

            if (Links == null || Links.Length == 0)
            {
                yield break;
            }

            foreach (var link in Links)
            {
                Control.LogDebug(DType.ComponentInstall, $"---- {link.ComponentDefId} to {link.Location}");
                var slot = DefaultHelper.CreateSlot(link.ComponentDefId, link.ComponentDefType ?? Def.ComponentType, mechlab.MechLab);

                if (slot != null)
                {
                    Control.LogDebug(DType.ComponentInstall, $"----- added");
                    yield return(new AddDefaultChange(link.Location, slot));
                }
                else
                {
                    Control.LogDebug(DType.ComponentInstall, $"----- not found");
                }
            }
        }
Пример #15
0
        public static bool Prefix(MechLabLocationWidget __instance,
                                  MechLabPanel ___mechLab,
                                  PointerEventData eventData)
        {
            try
            {
                var dragItem = ___mechLab.DragItem as MechLabItemSlotElement;


                if (!___mechLab.Initialized)
                {
                    return(false);
                }

                var newComponentDef = dragItem?.ComponentRef?.Def;
                if (newComponentDef == null)
                {
                    return(false);
                }

                Control.LogDebug(DType.ComponentInstall, $"OnMechLabDrop: Adding {newComponentDef.Description.Id}");

                var location_helper = new LocationHelper(__instance);
                var mechlab_helper  = new MechLabHelper(___mechLab);

                bool do_cancel(string error, List <IChange> allchanges)
                {
                    if (string.IsNullOrEmpty(error))
                    {
                        return(false);
                    }

                    Control.LogDebug(DType.ComponentInstall, $"- Canceled: {error}");

                    if (allchanges != null)
                    {
                        foreach (var change in allchanges)
                        {
                            change.CancelChange(mechlab_helper, location_helper);
                        }
                    }

                    ___mechLab.OnDrop(eventData);
                    ___mechLab.ShowDropErrorMessage(new Text(error));
                    return(true);
                }

                Control.LogDebug(DType.ComponentInstall, $"- pre validation");

                foreach (var pre_validator in Validator.GetPre(newComponentDef))
                {
                    if (do_cancel(pre_validator(dragItem, location_helper, mechlab_helper), null))
                    {
                        return(false);
                    }
                }

                Control.LogDebug(DType.ComponentInstall, $"- replace validation");

                List <IChange> changes = new List <IChange>();
                changes.Add(new AddFromInventoryChange(__instance.loadout.Location, dragItem));

                foreach (var rep_validator in Validator.GetReplace(newComponentDef))
                {
                    if (do_cancel(rep_validator(dragItem, location_helper, changes), changes))
                    {
                        return(false);
                    }
                }

#if CCDEBUG
                if (Control.Settings.DebugInfo.HasFlag(DType.ComponentInstall))
                {
                    if (changes.Count == 1)
                    {
                        Control.LogDebug(DType.ComponentInstall, $"-- no replace");
                    }
                    else
                    {
                        foreach (var replace in changes)
                        {
                            if (replace is AddChange add)
                            {
                                Control.LogDebug(DType.ComponentInstall,
                                                 $"-- add {add.item.ComponentRef.ComponentDefID}");
                            }

                            else if (replace is RemoveChange remove)
                            {
                                Control.LogDebug(DType.ComponentInstall,
                                                 $"-- remove {remove.item.ComponentRef.ComponentDefID}");
                            }
                        }
                    }
                }
#endif


                Control.LogDebug(DType.ComponentInstall, $"- adjusting {changes.Count} changes");

                int num = changes.Count;
                for (int i = 0; i < num; i++)
                {
                    if (changes[i] is AddChange add)
                    {
                        Control.LogDebug(DType.ComponentInstall, $"-- add: {add.item.ComponentRef.ComponentDefID}");

                        foreach (var adj_validator in add.item.ComponentRef.GetComponents <IAdjustValidateDrop>())
                        {
                            changes.AddRange(adj_validator.ValidateDropOnAdd(add.item, location_helper, mechlab_helper,
                                                                             changes));
                        }
                    }
                    else if (changes[i] is RemoveChange remove)
                    {
                        Control.LogDebug(DType.ComponentInstall, $"-- add: {remove.item.ComponentRef.ComponentDefID}");

                        foreach (var adj_validator in remove.item.ComponentRef.GetComponents <IAdjustValidateDrop>())
                        {
                            changes.AddRange(adj_validator.ValidateDropOnRemove(remove.item, location_helper,
                                                                                mechlab_helper, changes));
                        }
                    }
                }

                List <InvItem> new_inventory = ___mechLab.activeMechInventory
                                               .Select(i => new InvItem {
                    item = i, location = i.MountedLocation
                }).ToList();

                new_inventory.AddRange(changes.OfType <AddChange>()
                                       .Select(i => new InvItem {
                    item = i.item.ComponentRef, location = i.location
                }));

                foreach (var remove in changes.OfType <RemoveChange>())
                {
                    new_inventory.RemoveAll(i => i.item == remove.item.ComponentRef);
                }
                Control.LogDebug(DType.ComponentInstall, $"- post validation");

                foreach (var pst_validator in Validator.GetPost(newComponentDef))
                {
                    if (do_cancel(pst_validator(dragItem, ___mechLab.activeMechDef, new_inventory, changes), changes))
                    {
                        return(false);
                    }
                }

                Control.LogDebug(DType.ComponentInstall, $"- apply changes");

                foreach (var change in changes)
                {
                    change.DoChange(mechlab_helper, location_helper);
                }

                ___mechLab.ClearDragItem(true);
                __instance.RefreshHardpointData();
                ___mechLab.ValidateLoadout(false);

                return(false);
            }
            catch (Exception e)
            {
                Control.LogError(e);
            }

            return(true);
        }
Пример #16
0
 public override void CancelChange(MechLabHelper mechLab, LocationHelper loc)
 {
     mechLab.MechLab.ForceItemDrop(item);
 }
Пример #17
0
        //internal string ValidateDrop(MechLabItemSlotElement drop_item, MechDef mech, List<InvItem> new_inventory, List<IChange> changes)
        //{
        //    ValidateMech(out var error, mech, drop_item.ComponentRef.Def);
        //    return error;
        //}


        public string ValidateDrop(MechLabItemSlotElement drop_item, LocationHelper location, MechLabHelper mechlab)
        {
            ValidateMech(out var error, mechlab.MechLab.activeMechDef, location.widget.loadout.Location, drop_item.ComponentRef);
            return(error);
        }
Пример #18
0
 public virtual void CancelChange(MechLabHelper mechLab, LocationHelper loc)
 {
 }
Пример #19
0
 public abstract void DoChange(MechLabHelper mechLab, LocationHelper loc);
Пример #20
0
        public string PreValidateDrop(MechLabItemSlotElement item, LocationHelper location, MechLabHelper mechlabf)
        {
            Control.LogDebug(DType.ComponentInstall, "-- TonnageAllowed");
            var tonnage = location.mechLab.activeMechDef.Chassis.Tonnage;

            if (tonnage != Tonnage)
            {
                return($"{Def.Description.Name} designed for {Tonnage}t 'Mech");
            }

            return(string.Empty);
        }
Пример #21
0
        public static AddFromInventoryChange FoundInInventory(ChassisLocations location, MechLabHelper mechlab, Predicate <MechComponentDef> SearchTerms)
        {
            Control.LogDebug(DType.ComponentInstall, $"AddFromInventoryChange.Create() one search");
            var item = search_item(mechlab, SearchTerms);

            return(item == null ? null : new AddFromInventoryChange(location, item));
        }
Пример #22
0
        private static string ValidateBase(MechLabItemSlotElement item, LocationHelper location, MechLabHelper mechlab)
        {
            var component = item.ComponentRef.Def;

            if (location.widget.loadout.CurrentInternalStructure <= 0f)
            {
                return($"Cannot add {component.Description.Name} to {location.LocationName}: The location is Destroyed.");
            }
            if ((component.AllowedLocations & location.widget.loadout.Location) <= ChassisLocations.None)
            {
                return($"Cannot add {component.Description.Name} to {location.LocationName}: Component is not permitted in this location.");
            }
            return(string.Empty);
        }
Пример #23
0
        public IEnumerable <IChange> ValidateDropOnRemove(MechLabItemSlotElement item, LocationHelper location, MechLabHelper mechlab, List <IChange> changes)
        {
            Control.LogDebug(DType.ComponentInstall, "--- AutoLinked Remove");

            if (Links == null || Links.Length == 0)
            {
                yield break;
            }

            foreach (var link in Links)
            {
                var widget = mechlab.GetLocationWidget(link.Location);
                if (widget != null)
                {
                    Control.LogDebug(DType.ComponentInstall, $"---- {link.ComponentDefId} from {link.Location}");
                    var remove = new LocationHelper(widget).LocalInventory.FirstOrDefault(e =>
                                                                                          e?.ComponentRef?.ComponentDefID == link.ComponentDefId);
                    if (remove != null)
                    {
                        Control.LogDebug(DType.ComponentInstall, $"----- removed");
                        yield return(new RemoveChange(link.Location, remove));
                    }
                    else
                    {
                        Control.LogDebug(DType.ComponentInstall, $"----- not found");
                    }
                }
            }
        }
Пример #24
0
        public string PreValidateDrop(MechLabItemSlotElement item, LocationHelper location, MechLabHelper mechlab)
        {
            Control.LogDebug(DType.ComponentInstall, "-- TonnageLimit");
            var tonnage = location.mechLab.activeMechDef.Chassis.Tonnage;


            if (tonnage < Min ||
                tonnage > Max)
            {
                if (Min == Max)
                {
                    return($"{Def.Description.Name} designed for {Min}t 'Mech");
                }
                else
                {
                    return($"{Def.Description.Name} designed for {Min}t-{Max}t 'Mech");
                }
            }

            return(string.Empty);
        }
Пример #25
0
        //private void ReMakeHardpoints(Transform hpLayout)
        //{
        //    HardpointWidgets = new Dictionary<int, MechlabHardpointHelper>();
        //    JJWidget = new JJHardpointHeler(hpLayout.GetChild(0).gameObject);

        //    var list = HardpointController.Instance.HardpointsList.Where(i => i.Visible).ToArray();
        //    for (int i = 1; i < hpLayout.childCount - 1; i++)
        //    {
        //        var obj = hpLayout.GetChild(i).gameObject;

        //        if (i - 1 >= list.Length)
        //        {
        //            GameObject.Destroy(obj);
        //            Control.LogError($"-- Missed hardpoint element for {i}");
        //        }

        //        var hpinfo = list[i - 1];

        //        HardpointWidgets[hpinfo.WeaponCategory.ID] = new MechlabHardpointHelper(obj, hpinfo);
        //    }
        //}



        internal static void CloseMechLab()
        {
            mechlab_instance = null;
        }