Beispiel #1
0
        internal void RemoveCoreToolbarWeapons(MyCubeGrid grid)
        {
            foreach (var cube in grid.GetFatBlocks())
            {
                if (cube is MyShipController)
                {
                    var ob     = (MyObjectBuilder_ShipController)cube.GetObjectBuilderCubeBlock();
                    var reinit = false;
                    for (int i = 0; i < ob.Toolbar.Slots.Count; i++)
                    {
                        var toolbarItem = ob.Toolbar.Slots[i].Data as MyObjectBuilder_ToolbarItemWeapon;
                        if (toolbarItem != null)
                        {
                            var defId = (MyDefinitionId)toolbarItem.defId;
                            if ((ReplaceVanilla && VanillaIds.ContainsKey(defId)) || WeaponPlatforms.ContainsKey(defId.SubtypeId))
                            {
                                var index = ob.Toolbar.Slots[i].Index;
                                var item  = ob.Toolbar.Slots[i].Item;
                                ob.Toolbar.Slots[i] = new MyObjectBuilder_Toolbar.Slot {
                                    Index = index, Item = item
                                };
                                reinit = true;
                            }
                        }
                    }

                    if (reinit)
                    {
                        cube.Init(ob, grid);
                    }
                }
            }
        }
Beispiel #2
0
        private void DrawDisabledGuns()
        {
            if (Tick600 || Tick60 && QuickDisableGunsCheck)
            {
                QuickDisableGunsCheck         = false;
                _nearbyGridsTestSphere.Center = CameraPos;
                _gridsNearCamera.Clear();
                _uninitializedBlocks.Clear();

                MyGamePruningStructure.GetAllTopMostEntitiesInSphere(ref _nearbyGridsTestSphere, _gridsNearCamera);
                for (int i = _gridsNearCamera.Count - 1; i >= 0; i--)
                {
                    var grid = _gridsNearCamera[i] as MyCubeGrid;
                    if (grid?.Physics != null && !grid.MarkedForClose && !grid.IsPreview && !grid.Physics.IsPhantom)
                    {
                        var fatBlocks = grid.GetFatBlocks();
                        for (int j = 0; j < fatBlocks.Count; j++)
                        {
                            var block = fatBlocks[j];
                            if (block.IsFunctional && WeaponPlatforms.ContainsKey(block.BlockDefinition.Id))
                            {
                                GridAi gridAi;
                                if (!GridTargetingAIs.TryGetValue(block.CubeGrid, out gridAi) || !gridAi.WeaponBase.ContainsKey(block))
                                {
                                    _uninitializedBlocks.Add(block);
                                }
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < _uninitializedBlocks.Count; i++)
            {
                var badBlock = _uninitializedBlocks[i];
                if (badBlock.InScene)
                {
                    var lookSphere = new BoundingSphereD(badBlock.PositionComp.WorldAABB.Center, 30f);
                    if (Camera.IsInFrustum(ref lookSphere))
                    {
                        MyOrientedBoundingBoxD blockBox;
                        SUtils.GetBlockOrientedBoundingBox(badBlock, out blockBox);
                        DsDebugDraw.DrawBox(blockBox, _uninitializedColor);
                    }
                }
            }
        }
Beispiel #3
0
        private void UpdateGrids()
        {
            DeferedUpBlockTypeCleanUp();

            DirtyGridsTmp.Clear();
            DirtyGridsTmp.AddRange(DirtyGrids);
            DirtyGrids.Clear();
            for (int i = 0; i < DirtyGridsTmp.Count; i++)
            {
                var grid       = DirtyGridsTmp[i];
                var newTypeMap = BlockTypePool.Get();
                newTypeMap[Offense]    = ConcurrentListPool.Get();
                newTypeMap[Utility]    = ConcurrentListPool.Get();
                newTypeMap[Thrust]     = ConcurrentListPool.Get();
                newTypeMap[Steering]   = ConcurrentListPool.Get();
                newTypeMap[Jumping]    = ConcurrentListPool.Get();
                newTypeMap[Power]      = ConcurrentListPool.Get();
                newTypeMap[Production] = ConcurrentListPool.Get();

                ConcurrentDictionary <WeaponDefinition.TargetingDef.BlockTypes, ConcurrentCachingList <MyCubeBlock> > noFatTypeMap;

                FatMap fatMap;
                if (GridToFatMap.TryGetValue(grid, out fatMap))
                {
                    var allFat    = fatMap.MyCubeBocks;
                    var terminals = 0;
                    var tStatus   = fatMap.Targeting == null || fatMap.Targeting.AllowScanning;
                    for (int j = 0; j < allFat.Count; j++)
                    {
                        var fat = allFat[j];
                        if (!(fat is IMyTerminalBlock))
                        {
                            continue;
                        }
                        terminals++;

                        using (fat.Pin()) {
                            if (fat.MarkedForClose)
                            {
                                continue;
                            }
                            if (fat is IMyProductionBlock)
                            {
                                newTypeMap[Production].Add(fat);
                            }
                            else if (fat is IMyPowerProducer)
                            {
                                newTypeMap[Power].Add(fat);
                            }
                            else if (fat is IMyGunBaseUser || fat is IMyWarhead || fat is MyConveyorSorter && WeaponPlatforms.ContainsKey(fat.BlockDefinition.Id.SubtypeId))
                            {
                                if (!tStatus && fat is IMyGunBaseUser && !WeaponPlatforms.ContainsKey(fat.BlockDefinition.Id.SubtypeId))
                                {
                                    tStatus = fatMap.Targeting.AllowScanning = true;
                                }

                                newTypeMap[Offense].Add(fat);
                            }
                            else if (fat is IMyUpgradeModule || fat is IMyRadioAntenna)
                            {
                                newTypeMap[Utility].Add(fat);
                            }
                            else if (fat is MyThrust)
                            {
                                newTypeMap[Thrust].Add(fat);
                            }
                            else if (fat is MyGyro)
                            {
                                newTypeMap[Steering].Add(fat);
                            }
                            else if (fat is MyJumpDrive)
                            {
                                newTypeMap[Jumping].Add(fat);
                            }
                        }
                    }

                    foreach (var type in newTypeMap)
                    {
                        type.Value.ApplyAdditions();
                    }

                    fatMap.MyCubeBocks.ApplyAdditions();

                    fatMap.Trash = terminals == 0;
                    var gridBlocks = grid.BlocksCount;
                    if (gridBlocks > fatMap.MostBlocks)
                    {
                        fatMap.MostBlocks = gridBlocks;
                    }
                    ConcurrentDictionary <WeaponDefinition.TargetingDef.BlockTypes, ConcurrentCachingList <MyCubeBlock> > oldTypeMap;
                    if (GridToBlockTypeMap.TryGetValue(grid, out oldTypeMap))
                    {
                        GridToBlockTypeMap[grid] = newTypeMap;
                        BlockTypeCleanUp.Enqueue(new DeferedTypeCleaning {
                            Collection = oldTypeMap, RequestTick = Tick
                        });
                    }
                    else
                    {
                        GridToBlockTypeMap[grid] = newTypeMap;
                    }
                }
                else if (GridToBlockTypeMap.TryRemove(grid, out noFatTypeMap))
                {
                    BlockTypeCleanUp.Enqueue(new DeferedTypeCleaning {
                        Collection = noFatTypeMap, RequestTick = Tick
                    });
                }
            }
            DirtyGridsTmp.Clear();
        }
Beispiel #4
0
        internal void OnEntityCreate(MyEntity myEntity)
        {
            try
            {
                if (!Inited)
                {
                    lock (InitObj) Init();
                }
                var grid = myEntity as MyCubeGrid;
                if (grid != null)
                {
                    grid.AddedToScene += GridAddedToScene;
                }
                if (!PbApiInited && myEntity is IMyProgrammableBlock)
                {
                    PbActivate = true;
                }

                var placer = myEntity as IMyBlockPlacerBase;
                if (placer != null && Placer == null)
                {
                    Placer = placer;
                }

                var cube            = myEntity as MyCubeBlock;
                var sorter          = cube as MyConveyorSorter;
                var turret          = cube as IMyLargeTurretBase;
                var controllableGun = cube as IMyUserControllableGun;

                if (sorter != null || turret != null || controllableGun != null)
                {
                    if (!(ReplaceVanilla && VanillaIds.ContainsKey(cube.BlockDefinition.Id)) && !WeaponPlatforms.ContainsKey(cube.BlockDefinition.Id))
                    {
                        return;
                    }

                    lock (InitObj)
                    {
                        if (!SorterControls && myEntity is MyConveyorSorter)
                        {
                            MyAPIGateway.Utilities.InvokeOnGameThread(() => CreateTerminalUi <IMyConveyorSorter>(this));
                            SorterControls = true;
                        }
                        else if (!TurretControls && turret != null)
                        {
                            MyAPIGateway.Utilities.InvokeOnGameThread(() => CreateTerminalUi <IMyLargeTurretBase>(this));
                            TurretControls = true;
                        }
                        else if (!FixedMissileReloadControls && controllableGun is IMySmallMissileLauncherReload)
                        {
                            MyAPIGateway.Utilities.InvokeOnGameThread(() => CreateTerminalUi <IMySmallMissileLauncherReload>(this));
                            FixedMissileReloadControls = true;
                        }
                        else if (!FixedMissileControls && controllableGun is IMySmallMissileLauncher)
                        {
                            MyAPIGateway.Utilities.InvokeOnGameThread(() => CreateTerminalUi <IMySmallMissileLauncher>(this));
                            FixedMissileControls = true;
                        }
                        else if (!FixedGunControls && controllableGun is IMySmallGatlingGun)
                        {
                            MyAPIGateway.Utilities.InvokeOnGameThread(() => CreateTerminalUi <IMySmallGatlingGun>(this));
                            FixedGunControls = true;
                        }
                    }
                    InitComp(cube);
                }
            }
            catch (Exception ex) { Log.Line($"Exception in OnEntityCreate: {ex}"); }
        }
Beispiel #5
0
        /*
         * IEnumerable<Vector3I> NearLine(Vector3I start, LineD line)
         * {
         *  MinHeap blocks;
         *  HashSet<Vector3I> seen = new HashSet<Vector3I> {start};
         *  blocks.Add(dist(line, start), start);
         *  while (!blocks.Empty)
         *  {
         *      var next = blocks.RemoveMin();
         *      yield return next;
         *      foreach (var neighbor in Neighbors(next))
         *      {
         *          if (seen.add(neighbor))
         *              blocks.Add(dist(line, neighbor), neighbor);
         *      }
         *  }
         * }
         */

        private void UpdateGrids()
        {
            DeferedUpBlockTypeCleanUp();

            DirtyGridsTmp.Clear();
            DirtyGridsTmp.AddRange(DirtyGridInfos);
            DirtyGridInfos.Clear();
            for (int i = 0; i < DirtyGridsTmp.Count; i++)
            {
                var grid       = DirtyGridsTmp[i];
                var newTypeMap = BlockTypePool.Get();
                newTypeMap[Offense]    = ConcurrentListPool.Get();
                newTypeMap[Utility]    = ConcurrentListPool.Get();
                newTypeMap[Thrust]     = ConcurrentListPool.Get();
                newTypeMap[Steering]   = ConcurrentListPool.Get();
                newTypeMap[Jumping]    = ConcurrentListPool.Get();
                newTypeMap[Power]      = ConcurrentListPool.Get();
                newTypeMap[Production] = ConcurrentListPool.Get();

                ConcurrentDictionary <WeaponDefinition.TargetingDef.BlockTypes, ConcurrentCachingList <MyCubeBlock> > noFatTypeMap;

                GridMap gridMap;
                if (GridToInfoMap.TryGetValue(grid, out gridMap))
                {
                    var allFat         = gridMap.MyCubeBocks;
                    var terminals      = 0;
                    var tStatus        = gridMap.Targeting == null || gridMap.Targeting.AllowScanning;
                    var thrusters      = 0;
                    var gyros          = 0;
                    var powerProducers = 0;
                    var warHead        = 0;
                    var working        = 0;
                    for (int j = 0; j < allFat.Count; j++)
                    {
                        var fat = allFat[j];
                        if (!(fat is IMyTerminalBlock))
                        {
                            continue;
                        }
                        terminals++;
                        using (fat.Pin()) {
                            if (fat.MarkedForClose)
                            {
                                continue;
                            }
                            if (fat.IsWorking && ++working == 1)
                            {
                                var oldCube = (gridMap.FakeController.SlimBlock as IMySlimBlock)?.FatBlock as MyCubeBlock;
                                if (oldCube == null || oldCube.MarkedForClose || oldCube.CubeGrid != grid)
                                {
                                    gridMap.FakeController.SlimBlock = fat.SlimBlock;
                                    GridDistributors[grid]           = gridMap;
                                }
                            }

                            var cockpit = fat as MyCockpit;
                            var decoy   = fat as IMyDecoy;
                            var bomb    = fat as IMyWarhead;

                            if (decoy != null)
                            {
                                WeaponDefinition.TargetingDef.BlockTypes type;
                                if (DecoyMap.TryGetValue(fat, out type))
                                {
                                    newTypeMap[type].Add(fat);
                                }
                                else
                                {
                                    newTypeMap[Utility].Add(fat);
                                    DecoyMap[fat] = Utility;
                                }
                                continue;
                            }

                            if (fat is IMyProductionBlock)
                            {
                                newTypeMap[Production].Add(fat);
                            }
                            else if (fat is IMyPowerProducer)
                            {
                                newTypeMap[Power].Add(fat);
                                powerProducers++;
                            }
                            else if (fat is IMyGunBaseUser || bomb != null || fat is MyConveyorSorter && WeaponPlatforms.ContainsKey(fat.BlockDefinition.Id))
                            {
                                if (bomb != null)
                                {
                                    warHead++;
                                }

                                if (!tStatus && fat is IMyGunBaseUser && !WeaponPlatforms.ContainsKey(fat.BlockDefinition.Id))
                                {
                                    tStatus = gridMap.Targeting.AllowScanning = true;
                                }

                                newTypeMap[Offense].Add(fat);
                            }
                            else if (fat is IMyUpgradeModule || fat is IMyRadioAntenna || cockpit != null && cockpit.EnableShipControl || fat is MyRemoteControl || fat is IMyShipGrinder || fat is IMyShipDrill)
                            {
                                newTypeMap[Utility].Add(fat);
                            }
                            else if (fat is MyThrust)
                            {
                                newTypeMap[Thrust].Add(fat);
                                thrusters++;
                            }
                            else if (fat is MyGyro)
                            {
                                newTypeMap[Steering].Add(fat);
                                gyros++;
                            }

                            else if (fat is MyJumpDrive)
                            {
                                newTypeMap[Jumping].Add(fat);
                            }
                        }
                    }

                    foreach (var type in newTypeMap)
                    {
                        type.Value.ApplyAdditions();
                    }

                    GridMap oldMap;
                    if (terminals == 0 && !gridMap.Trash && GridDistributors.TryRemove(grid, out oldMap))
                    {
                        oldMap.FakeController.SlimBlock = null;
                    }

                    gridMap.MyCubeBocks.ApplyAdditions();
                    gridMap.SuspectedDrone = warHead > 0 || powerProducers > 0 && thrusters > 0 && gyros > 0;
                    gridMap.Trash          = terminals == 0;
                    gridMap.Powered        = working > 0;
                    gridMap.PowerCheckTick = Tick;

                    var gridBlocks = grid.BlocksCount;

                    if (gridBlocks > gridMap.MostBlocks)
                    {
                        gridMap.MostBlocks = gridBlocks;
                    }

                    ConcurrentDictionary <WeaponDefinition.TargetingDef.BlockTypes, ConcurrentCachingList <MyCubeBlock> > oldTypeMap;
                    if (GridToBlockTypeMap.TryGetValue(grid, out oldTypeMap))
                    {
                        GridToBlockTypeMap[grid] = newTypeMap;
                        BlockTypeCleanUp.Enqueue(new DeferedTypeCleaning {
                            Collection = oldTypeMap, RequestTick = Tick
                        });
                    }
                    else
                    {
                        GridToBlockTypeMap[grid] = newTypeMap;
                    }
                }
                else if (GridToBlockTypeMap.TryRemove(grid, out noFatTypeMap))
                {
                    BlockTypeCleanUp.Enqueue(new DeferedTypeCleaning {
                        Collection = noFatTypeMap, RequestTick = Tick
                    });
                }
            }
            DirtyGridsTmp.Clear();
        }
        private void DrawDisabledGuns()
        {
            if (Tick600 || Tick60 && QuickDisableGunsCheck)
            {
                QuickDisableGunsCheck         = false;
                _nearbyGridsTestSphere.Center = CameraPos;
                _gridsNearCamera.Clear();
                _uninitializedBlocks.Clear();
                _debugBlocks.Clear();

                MyGamePruningStructure.GetAllTopMostEntitiesInSphere(ref _nearbyGridsTestSphere, _gridsNearCamera);
                for (int i = _gridsNearCamera.Count - 1; i >= 0; i--)
                {
                    var grid = _gridsNearCamera[i] as MyCubeGrid;
                    if (grid?.Physics != null && !grid.MarkedForClose && !grid.IsPreview && !grid.Physics.IsPhantom)
                    {
                        var fatBlocks = grid.GetFatBlocks();
                        for (int j = 0; j < fatBlocks.Count; j++)
                        {
                            var block = fatBlocks[j];
                            if (block.IsFunctional && WeaponPlatforms.ContainsKey(block.BlockDefinition.Id))
                            {
                                GridAi          gridAi;
                                WeaponComponent comp;
                                if (!GridTargetingAIs.TryGetValue(block.CubeGrid, out gridAi) || !gridAi.WeaponBase.TryGetValue(block, out comp))
                                {
                                    _uninitializedBlocks.Add(block);
                                }
                                else if (comp != null && comp.Data.Repo.Base.Set.Overrides.Debug)
                                {
                                    _debugBlocks.Add(comp);
                                }
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < _uninitializedBlocks.Count; i++)
            {
                var badBlock = _uninitializedBlocks[i];
                if (badBlock.InScene)
                {
                    var lookSphere = new BoundingSphereD(badBlock.PositionComp.WorldAABB.Center, 30f);
                    if (Camera.IsInFrustum(ref lookSphere))
                    {
                        MyOrientedBoundingBoxD blockBox;
                        SUtils.GetBlockOrientedBoundingBox(badBlock, out blockBox);
                        DsDebugDraw.DrawBox(blockBox, _uninitializedColor);
                    }
                }
            }

            for (int i = 0; i < _debugBlocks.Count; i++)
            {
                var comp = _debugBlocks[i];
                if (comp.MyCube.InScene)
                {
                    var lookSphere = new BoundingSphereD(comp.MyCube.PositionComp.WorldAABB.Center, 100f);

                    if (Camera.IsInFrustum(ref lookSphere))
                    {
                        foreach (var w in comp.Platform.Weapons)
                        {
                            if (!w.AiEnabled && w.ActiveAmmoDef.AmmoDef.Trajectory.Guidance == WeaponDefinition.AmmoDef.TrajectoryDef.GuidanceType.Smart)
                            {
                                w.SmartLosDebug();
                            }
                        }
                    }
                }
            }
        }
Beispiel #7
0
        internal void OnEntityCreate(MyEntity myEntity)
        {
            try
            {
                if (!Inited)
                {
                    lock (InitObj) Init();
                }

                var planet = myEntity as MyPlanet;
                if (planet != null)
                {
                    PlanetMap.TryAdd(planet.EntityId, planet);
                }

                var grid = myEntity as MyCubeGrid;
                if (grid != null)
                {
                    grid.AddedToScene += GridAddedToScene;
                }
                if (!PbApiInited && myEntity is IMyProgrammableBlock)
                {
                    PbActivate = true;
                }

                var placer = myEntity as IMyBlockPlacerBase;
                if (placer != null && Placer == null)
                {
                    Placer = placer;
                }

                var cube            = myEntity as MyCubeBlock;
                var sorter          = cube as MyConveyorSorter;
                var turret          = cube as IMyLargeTurretBase;
                var controllableGun = cube as IMyUserControllableGun;
                var decoy           = cube as IMyDecoy;
                var camera          = cube as MyCameraBlock;

                if (sorter != null || turret != null || controllableGun != null)
                {
                    if (!(ReplaceVanilla && VanillaIds.ContainsKey(cube.BlockDefinition.Id)) && !WeaponPlatforms.ContainsKey(cube.BlockDefinition.Id))
                    {
                        return;
                    }

                    lock (InitObj)
                    {
                        if (!SorterDetected && myEntity is MyConveyorSorter)
                        {
                            MyAPIGateway.Utilities.InvokeOnGameThread(() => CreateTerminalUi <IMyConveyorSorter>(this));
                            if (!EarlyInitOver)
                            {
                                ControlQueue.Enqueue(typeof(IMyConveyorSorter));
                            }
                            SorterDetected = true;
                        }
                        else if (!TurretDetected && turret != null)
                        {
                            MyAPIGateway.Utilities.InvokeOnGameThread(() => CreateTerminalUi <IMyLargeTurretBase>(this));
                            if (!EarlyInitOver)
                            {
                                ControlQueue.Enqueue(typeof(IMyLargeTurretBase));
                            }
                            TurretDetected = true;
                        }
                        else if (!FixedMissileReloadDetected && controllableGun is IMySmallMissileLauncherReload)
                        {
                            MyAPIGateway.Utilities.InvokeOnGameThread(() => CreateTerminalUi <IMySmallMissileLauncherReload>(this));
                            if (!EarlyInitOver)
                            {
                                ControlQueue.Enqueue(typeof(IMySmallMissileLauncherReload));
                            }

                            FixedMissileReloadDetected = true;
                        }
                        else if (!FixedMissileDetected && controllableGun is IMySmallMissileLauncher)
                        {
                            MyAPIGateway.Utilities.InvokeOnGameThread(() => CreateTerminalUi <IMySmallMissileLauncher>(this));
                            if (!EarlyInitOver)
                            {
                                ControlQueue.Enqueue(typeof(IMySmallMissileLauncher));
                            }
                            FixedMissileDetected = true;
                        }
                        else if (!FixedGunDetected && controllableGun is IMySmallGatlingGun)
                        {
                            MyAPIGateway.Utilities.InvokeOnGameThread(() => CreateTerminalUi <IMySmallGatlingGun>(this));
                            if (!EarlyInitOver)
                            {
                                ControlQueue.Enqueue(typeof(IMySmallGatlingGun));
                            }
                            FixedGunDetected = true;
                        }
                    }
                    InitComp(cube);
                }
                else if (decoy != null)
                {
                    if (!DecoyDetected)
                    {
                        MyAPIGateway.Utilities.InvokeOnGameThread(() => CreateDecoyTerminalUi <IMyDecoy>(this));
                        DecoyDetected = true;
                    }

                    cube.AddedToScene += DecoyAddedToScene;
                }
                else if (camera != null)
                {
                    if (!CameraDetected)
                    {
                        MyAPIGateway.Utilities.InvokeOnGameThread(() => CreateCameraTerminalUi <IMyCameraBlock>(this));
                        CameraDetected = true;
                    }

                    cube.AddedToScene += CameraAddedToScene;
                }
            }
            catch (Exception ex) { Log.Line($"Exception in OnEntityCreate: {ex}"); }
        }