예제 #1
0
 public void Load()
 {
     try
     {
         if (MyAPIGateway.Multiplayer.MultiplayerActive && !MyAPIGateway.Multiplayer.IsServer)
         {
             SyncHarvestEfficiency.Ask();
             return;
         }
         string Storage = "";
         //if (Tool.Storage.ContainsKey(SessionCore.StorageGuid))
         if (Drill.Storage?.TryGetValue(SessionCore.StorageGuid, out Storage) == true)
         {
             try
             {
                 Persistent persistent = MyAPIGateway.Utilities.SerializeFromBinary <Persistent>(Convert.FromBase64String(Storage));
                 SyncHarvestEfficiency.Set(persistent.HarvestMultiplier);
             }
             catch (Exception Scrap)
             {
                 SessionCore.LogError($"{Drill.CustomName}.Load()", Scrap);
             }
         }
         else
         {
             SessionCore.DebugWrite($"{Drill.CustomName}.Load()", "Storage access failed.");
         }
     }
     catch (Exception Scrap)
     {
         SessionCore.LogError($"{Drill.CustomName}.Load().AccessStorage", Scrap);
     }
 }
예제 #2
0
        public void Load()
        {
            try
            {
                if (MyAPIGateway.Multiplayer.MultiplayerActive && !MyAPIGateway.Multiplayer.IsServer)
                {
                    ToolComp.TermModule.LoadSyncersFromServer();
                    return;
                }
                string Storage = "";

                if (Tool.Storage == null)
                {
                    SessionCore.DebugWrite($"{Tool.CustomName}.Load()", "Tool's Storage doesn't exist.");
                    ToolComp.TermModule.BeamLength = ToolComp.BeamCtlModule.MaxBeamLengthBlocks;
                    return;
                }

                if (!Tool.Storage.ContainsKey(SessionCore.StorageGuid))
                {
                    SessionCore.DebugWrite($"{Tool.CustomName}.Load()", "Tool's Storage is empty.");
                    ToolComp.TermModule.BeamLength = ToolComp.BeamCtlModule.MaxBeamLengthBlocks;
                    return;
                }

                if (Tool.Storage.TryGetValue(SessionCore.StorageGuid, out Storage) == true /* ||
                                                                                            * MyAPIGateway.Utilities.GetVariable($"settings_{Tool.EntityId}", out Storage)*/)
                {
                    try
                    {
                        SessionCore.DebugWrite($"{Tool.CustomName}.Load()", $"Accessing storage. Raw data: {Storage}");
                        PersistentStruct persistent = MyAPIGateway.Utilities.SerializeFromBinary <PersistentStruct>(Convert.FromBase64String(Storage));
                        ToolComp.TermModule.BeamLength = (int)persistent.BeamLength;
                        if (ToolComp.TermModule.BeamLength > ToolComp.BeamCtlModule.MaxBeamLengthBlocks)
                        {
                            ToolComp.TermModule.BeamLength = ToolComp.BeamCtlModule.MaxBeamLengthBlocks;
                        }
                        ToolComp.TermModule.DistanceMode    = persistent.DistanceBased;
                        ToolComp.TermModule.SpeedMultiplier = (int)persistent.SpeedMultiplier;
                        SessionCore.DebugWrite($"{Tool.CustomName}.Load()", $"Loaded from storage. Persistent Beamlength: {persistent.BeamLength}; Sync Beamlength: {ToolComp.TermModule.BeamLength}");
                    }
                    catch (Exception Scrap)
                    {
                        SessionCore.LogError($"{Tool.CustomName}.Load()", Scrap);
                    }
                }
                else
                {
                    ToolComp.TermModule.BeamLength = ToolComp.BeamCtlModule.MaxBeamLengthBlocks;
                    SessionCore.DebugWrite($"{Tool.CustomName}.Load()", "Storage access failed.");
                }
            }
            catch (Exception Scrap)
            {
                SessionCore.LogError($"{Tool.CustomName}.Load().AccessStorage", Scrap);
            }
        }
예제 #3
0
 public static void Report(this System.Diagnostics.Stopwatch Watch, string Source, string WatchedProcessName, bool UseAsync = false)
 {
     if (!SessionCore.Settings.DebugPerformance)
     {
         return;
     }
     if (!UseAsync)
     {
         SessionCore.DebugWrite(Source, $"{WatchedProcessName} took {Math.Round(Watch.ElapsedTicks * 1000f / System.Diagnostics.Stopwatch.Frequency, 2)} ms to run", WriteOnlyIfDebug: true);
     }
     else
     {
         SessionCore.DebugAsync(Source, $"{WatchedProcessName} took {Math.Round(Watch.ElapsedTicks * 1000f / System.Diagnostics.Stopwatch.Frequency, 2)} ms to run", WriteOnlyIfDebug: true);
     }
 }
예제 #4
0
 public override void Init(MyObjectBuilder_EntityBase objectBuilder)
 {
     NeedsUpdate |= MyEntityUpdateEnum.BEFORE_NEXT_FRAME;
     //Tool.ResourceSink.SetRequiredInputFuncByType(Electricity, () => PowerConsumptionFunc());
     try
     {
         if (!Drill.HasComponent <MyModStorageComponent>())
         {
             Drill.Storage = new MyModStorageComponent();
             Drill.Components.Add(Drill.Storage);
             SessionCore.DebugWrite($"{Drill.CustomName}.Init()", "Block doesn't have a Storage component!", IsExcessive: false);
         }
     }
     catch { }
 }
예제 #5
0
 public void Save()
 {
     try
     {
         Persistent persistent;
         persistent.BeamLength      = SyncBeamLength.Get();
         persistent.DistanceBased   = SyncDistanceMode.Get();
         persistent.SpeedMultiplier = SyncSpeedMultiplier.Get();
         string Raw = Convert.ToBase64String(MyAPIGateway.Utilities.SerializeToBinary(persistent));
         MyAPIGateway.Utilities.SetVariable($"settings_{Tool.EntityId}", Raw);
         Tool.Storage?.AddOrUpdate(SessionCore.StorageGuid, Raw);
         SessionCore.DebugWrite($"{Tool.CustomName}.Load()", $"Set settings to storage. Saved beamlength: {persistent.BeamLength}");
     }
     catch (Exception Scrap)
     {
         SessionCore.LogError($"{Tool.CustomName}.Save()", Scrap);
     }
 }
예제 #6
0
        void Place(IMySlimBlock Block)
        {
            if (!Block.IsProjectable())
            {
                return;
            }
            var FirstItem = ((MyCubeBlockDefinition)Block.BlockDefinition).Components[0].Definition.Id;

            if (MyAPIGateway.Session.CreativeMode || ToolCargo.PullAny(OnboardInventoryOwners, FirstItem.SubtypeName, 1))
            {
                var Projector = ((Block.CubeGrid as MyCubeGrid).Projector as IMyProjector);
                Projector.Build(Block, 0, Tool.EntityId, false);
                ToolCargo.RemoveItemsOfType(1, FirstItem);
            }
            else
            {
                UnbuiltBlocks.Add(Block);
                SessionCore.DebugWrite($"{Tool.CustomName}.Place()", $"Tool can't pull the component {FirstItem.SubtypeName}!", IsExcessive: false);
            }
        }
예제 #7
0
 public override void Init(MyObjectBuilder_EntityBase objectBuilder)
 {
     NeedsUpdate      |= MyEntityUpdateEnum.BEFORE_NEXT_FRAME;
     Tool              = Entity as IMyShipToolBase;
     TermModule        = new ToolTermCtl(this);
     BeamCtlModule     = new BeamController(this);
     PersistenceModule = new ToolPersistence(this);
     HUDModule         = new WelderHUDModule(this);
     PowerModule       = new ToolPowerModule(this);
     //Tool.ResourceSink.SetRequiredInputFuncByType(Electricity, () => PowerConsumptionFunc());
     try
     {
         if (!Tool.HasComponent <MyModStorageComponent>())
         {
             Tool.Storage = new MyModStorageComponent();
             Tool.Components.Add(Tool.Storage);
             SessionCore.DebugWrite($"{Tool.CustomName}.Init()", "Block doesn't have a Storage component!", IsExcessive: false);
         }
     }
     catch { }
 }
예제 #8
0
 public void Load()
 {
     try
     {
         if (MyAPIGateway.Multiplayer.MultiplayerActive && !MyAPIGateway.Multiplayer.IsServer)
         {
             SyncBeamLength.Ask();
             SyncDistanceMode.Ask();
             SyncSpeedMultiplier.Ask();
             return;
         }
         string Storage = "";
         //if (Tool.Storage.ContainsKey(SessionCore.StorageGuid))
         if (Tool.Storage?.TryGetValue(SessionCore.StorageGuid, out Storage) == true ||
             MyAPIGateway.Utilities.GetVariable($"settings_{Tool.EntityId}", out Storage))
         {
             try
             {
                 Persistent persistent = MyAPIGateway.Utilities.SerializeFromBinary <Persistent>(Convert.FromBase64String(Storage));
                 SyncBeamLength.Set(persistent.BeamLength);
                 SyncDistanceMode.Set(persistent.DistanceBased);
                 SyncSpeedMultiplier.Set(persistent.SpeedMultiplier);
                 SessionCore.DebugWrite($"{Tool.CustomName}.Load()", $"Loaded from storage. Persistent Beamlength: {persistent.BeamLength}; Sync Beamlength: {SyncBeamLength.Get()}");
             }
             catch (Exception Scrap)
             {
                 SessionCore.LogError($"{Tool.CustomName}.Load()", Scrap);
             }
         }
         else
         {
             SessionCore.DebugWrite($"{Tool.CustomName}.Load()", "Storage access failed.");
         }
     }
     catch (Exception Scrap)
     {
         SessionCore.LogError($"{Tool.CustomName}.Load().AccessStorage", Scrap);
     }
 }
예제 #9
0
        public void Save()
        {
            if (Tool.Storage == null)
            {
                SessionCore.DebugWrite($"{Tool.CustomName}.Save()", "Tool's Storage doesn't exist.");
                return;
            }

            if (!Tool.Storage.ContainsKey(SessionCore.StorageGuid))
            {
                SessionCore.DebugWrite($"{Tool.CustomName}.Save()", "Tool's Storage is empty.");
                Tool.Storage[SessionCore.StorageGuid] = "";
            }

            try
            {
                PersistentStruct persistent;
                persistent.BeamLength      = ToolComp.TermModule.BeamLength;
                persistent.DistanceBased   = ToolComp.TermModule.DistanceMode;
                persistent.SpeedMultiplier = ToolComp.TermModule.SpeedMultiplier;
                string Raw = Convert.ToBase64String(MyAPIGateway.Utilities.SerializeToBinary(persistent));
                //MyAPIGateway.Utilities.SetVariable($"settings_{Tool.EntityId}", Raw);
                if (!Tool.Storage.ContainsKey(SessionCore.StorageGuid))
                {
                    Tool.Storage.Add(SessionCore.StorageGuid, Raw);
                }
                else
                {
                    Tool.Storage[SessionCore.StorageGuid] = Raw;
                }
                SessionCore.DebugWrite($"{Tool.CustomName}.Save()", $"Saved to storage. Raw data: {Raw}");
                SessionCore.DebugWrite($"{Tool.CustomName}.Save()", $"Checked raw data: {Tool.Storage[SessionCore.StorageGuid]}");
            }
            catch (Exception Scrap)
            {
                SessionCore.LogError($"{Tool.CustomName}.Save()", Scrap);
            }
        }
예제 #10
0
        void Weld(ICollection <IMySlimBlock> Blocks, int ticks = 1)
        {
            UnbuiltBlocks.Clear();
            if (Blocks.Count == 0)
            {
                return;
            }
            float SpeedRatio   = (VanillaToolConstants.WelderSpeed / Blocks.Count) * ticks * TermModule.SpeedMultiplier;
            float BoneFixSpeed = VanillaToolConstants.WelderBoneRepairSpeed * ticks;
            Dictionary <IMySlimBlock, int> UniqueBlocks = new Dictionary <IMySlimBlock, int>();

            if (!TermModule.DistanceMode)
            {
                UniqueBlocks = Blocks.CollapseDuplicates();
            }
            else
            {
                IMySlimBlock Block = Blocks.OrderByDescending(x => Vector3D.DistanceSquared(x.GetPosition(), Tool.GetPosition())).ToList().First();
                UniqueBlocks.Add(Block, 1);
                SpeedRatio = VanillaToolConstants.WelderSpeed * ticks * TermModule.SpeedMultiplier;
            }
            HashSet <IMySlimBlock> unbuilt     = new HashSet <IMySlimBlock>();
            List <IMySlimBlock>    CanBuild    = new List <IMySlimBlock>();
            List <IMySlimBlock>    CannotBuild = new List <IMySlimBlock>();

            if (MyAPIGateway.Session.CreativeMode)
            {
                foreach (IMySlimBlock Block in UniqueBlocks.Keys)
                {
                    float blockRatio = SpeedRatio * UniqueBlocks.GetData(Block);
                    Block.MoveItemsToConstructionStockpile(ToolCargo);
                    Block.IncreaseMountLevel(blockRatio, Welder.OwnerId, ToolCargo, BoneFixSpeed, Welder.HelpOthers);
                }
                return;
            }

            foreach (IMySlimBlock Block in UniqueBlocks.Keys)
            {
                if (Block.CanContinueBuild(ToolCargo))
                {
                    CanBuild.Add(Block);
                }
                else
                {
                    CannotBuild.Add(Block);
                }
            }

            SessionCore.DebugWrite(Tool.CustomName, $"Can build {CanBuild.Count} blocks", WriteOnlyIfDebug: true);
            SessionCore.DebugWrite(Tool.CustomName, $"Can't build {CannotBuild.Count} blocks", WriteOnlyIfDebug: true);

            bool Pull = false;

            var Components = Reduce(CannotBuild.ReadMissingComponents());
            Dictionary <string, int> Pulled = new Dictionary <string, int>();

            lock (GridInventoryModule.InventoryLock)
            {
                if (Welder.UseConveyorSystem)
                {
                    GridInventoryModule.PullIn(Welder, Components, "Component");
                }
                Pull = Pulled.Count > 0;
            }
            SessionCore.DebugWrite(Tool.CustomName, $"Pulled {Pulled.Values.Sum()} components", WriteOnlyIfDebug: true);

            System.Diagnostics.Stopwatch WelderWatch = new System.Diagnostics.Stopwatch();
            WelderWatch.Start();
            foreach (IMySlimBlock Block in UniqueBlocks.Keys)
            {
                if (!Pull && !CanBuild.Contains(Block))
                {
                    unbuilt.Add(Block);
                    continue;
                }
                float blockRatio = SpeedRatio * UniqueBlocks.GetData(Block);
                if (!Weld(Block, blockRatio, BoneFixSpeed))
                {
                    unbuilt.Add(Block);
                }
            }
            WelderWatch.Stop();
            WelderWatch.Report(Tool.CustomName, "Welding", UseAsync: true);

            System.Diagnostics.Stopwatch ReadMissingWatch = new System.Diagnostics.Stopwatch();
            ReadMissingWatch.Start();
            if (unbuilt.Count > 0)
            {
                Dictionary <string, int> Missing = new Dictionary <string, int>();
                unbuilt.ReadMissingComponents(Missing);
                Missing = Reduce(Missing);
                if (Missing.Count > 0)
                {
                    UnbuiltBlocks.UnionWith(unbuilt);
                }
            }
            ComplainUnbuilt();
            ReadMissingWatch.Stop();
            ReadMissingWatch.Report(Tool.CustomName, "ReadMissing", true);
            SessionCore.DebugAsync(Tool.CustomName, $"Unbuilt: {unbuilt.Count} blocks", WriteOnlyIfDebug: true);
        }