示例#1
0
        public override void UpdateBeforeSimulation100()
        {
            if (_cargoTeleporter == null)
            {
                return;
            }
            if (!((IMyFunctionalBlock)_cargoTeleporter).Enabled)
            {
                return;
            }

            try
            {
                if (MyAPIGateway.Session == null)
                {
                    Write("MyAPIGateway.Session is null");
                    return;
                }

                Write("MainRun");

                var name     = "";
                var gridName = "";
                var toMode   = true;
                ParseName(ref name, ref gridName, ref toMode);

                if (name.Length < 2)
                {
                    Write("Name too small");
                    UpdateStatus("Status: No filters\n\nPlease add [T:Block Name] or [F:Block Name].\nPlease add [G:Grid Name] if using antennas.");
                    return;
                }

                if (_inventory == null)
                {
                    _inventory = _cargoTeleporter.GetInventory();
                }
                if (toMode && _inventory.Empty())
                {
                    UpdateStatus("Status: Source Empty");
                    return;
                }

                Write("GetBlocks");
                var cubeBlocks = _cargoTeleporter.CubeGrid.GetFatBlocks();

                //We use true, if it goes through the ref; (which I treat like an out) it will be changed.
                //If it goes through the linq; then we are unchanged and true is the correct value. (we have found the grid, it is this one)
                bool gridFound = true;
                var  target    = gridName.Length > 2 && gridName != _cargoTeleporter.CubeGrid.Name ?
                                 GetTarget(gridName, name, _cargoTeleporter.CubeGrid, ref gridFound) :
                                 cubeBlocks.First(x => x?.DisplayNameText == name && OwnershipUtils.isSameFactionOrOwner(_cargoTeleporter, x));


                var disonnectedStatus = "Status: Disconnected";
                //If grid not found,
                if (!gridFound)
                {
                    Write("Grid Not Found");
                }
                disonnectedStatus += $"\nGrid: {(gridFound ? "" : "Not ")}Found";
                if (target == null)
                {
                    Write("Target Not Found");
                }
                //This message should only ever say T/S Not Found, but in case I flubbed it; it can say found
                disonnectedStatus += $"\n{(toMode ? "Target" : "Source")}: {(target != null ? "" : "Not ")}Found";

                if (target == null || !gridFound)
                {
                    UpdateStatus(disonnectedStatus);
                    return;
                }

                var targetInventory = target.GetInventory();
                var status          = "Status: Connected\nTarget: ";
                var targetStatus    = targetInventory.IsFull ? "Full" : targetInventory.Empty() ? "Empty" : "Some";
                var inventoryStatus = _inventory.IsFull ? "Full" : _inventory.Empty() ? "Empty" : "Some";
                status += toMode ? targetStatus : inventoryStatus;
                status += "\nSource: ";
                status += !toMode ? targetStatus : inventoryStatus;

                UpdateStatus(status);

                if (!targetInventory.IsFull && toMode)
                {
                    targetInventory.TransferItemFrom(_inventory, 0, null, true, null, false);
                }
                else if (!targetInventory.Empty() && !_inventory.IsFull && !toMode)
                {
                    _inventory.TransferItemFrom(targetInventory, 0, null, true, null, false);
                }
            }
            catch (Exception ex)
            {
                Logging.WriteLine(ex.ToString());
            }
        }
示例#2
0
        private IMyEntity GetTarget(string gridName, string name, IMyCubeGrid startingGrid, ref bool foundGrid)
        {
            var       gridsToProcess = new List <IMyCubeGrid>();
            var       gridsProcessed = new HashSet <IMyCubeGrid>();
            IMyEntity target         = null;

            foundGrid = false;

            gridsToProcess.Add(startingGrid);

            while (target == null && gridsToProcess.Count > 0)
            {
                var processing = gridsToProcess.Pop();
                Write("Processing Grid: " + processing.DisplayName);

                var fatBlocks = new List <IMySlimBlock>();
                processing.GetBlocks(fatBlocks, x => x?.FatBlock != null);
                var gridBlocks = fatBlocks.ConvertAll(x => x.FatBlock);

                if (processing.DisplayName == gridName)
                {
                    target    = gridBlocks.First(x => x?.DisplayNameText == name);
                    foundGrid = true;
                    break;
                }

                foreach (var antenna in gridBlocks.Where(x => x is IMyRadioAntenna && OwnershipUtils.isSameFactionOrOwner(processing, x)).Cast <IMyRadioAntenna>())
                {
                    var sphere = new BoundingSphereD(antenna.GetPosition(), antenna.Radius);
                    gridsToProcess.AddRange(MyAPIGateway.Entities.GetEntitiesInSphere(ref sphere).Where(x => x is IMyCubeGrid).Cast <IMyCubeGrid>().Where(x => !gridsProcessed.Contains(x) && !gridsToProcess.Contains(x)));
                }

                foreach (var antenna in gridBlocks.Where(x => x is IMyLaserAntenna && OwnershipUtils.isSameFactionOrOwner(processing, x)).Cast <IMyLaserAntenna>().Where(x => x.Status == MyLaserAntennaStatus.Connected))
                {
                    var sphere = new BoundingSphereD(antenna.TargetCoords, 1);
                    gridsToProcess.AddRange(MyAPIGateway.Entities.GetEntitiesInSphere(ref sphere).Where(x => x is IMyCubeGrid).Cast <IMyCubeGrid>().Where(x => !gridsProcessed.Contains(x) && !gridsToProcess.Contains(x)));
                }

                gridsProcessed.Add(processing);
            }

            return(target);
        }