Example #1
0
        internal void OnAddedToSceneTasks()
        {
            try {
                Ai.UpdatePowerSources = true;
                if (!Ai.GridInit)
                {
                    Ai.GridInit = true;
                    Ai.InitFakeShipController();
                    Ai.ScanBlockGroups = true;
                    var fatList = Session.GridToFatMap[MyCube.CubeGrid].MyCubeBocks;

                    for (int i = 0; i < fatList.Count; i++)
                    {
                        var cubeBlock = fatList[i];
                        if (cubeBlock is MyBatteryBlock || cubeBlock is IMyCargoContainer || cubeBlock is IMyAssembler || cubeBlock is IMyShipConnector)
                        {
                            Ai.FatBlockAdded(cubeBlock);
                        }
                    }
                }

                var maxTrajectory = 0d;
                var ob            = MyCube.BlockDefinition as MyLargeTurretBaseDefinition;

                for (int i = 0; i < Platform.Weapons.Length; i++)
                {
                    var weapon = Platform.Weapons[i];
                    weapon.InitTracking();

                    double weaponMaxRange;
                    DpsAndHeatInit(weapon, ob, out weaponMaxRange);

                    if (maxTrajectory < weaponMaxRange)
                    {
                        maxTrajectory = weaponMaxRange;
                    }
                }

                if (maxTrajectory + Ai.MyGrid.PositionComp.LocalVolume.Radius > Ai.MaxTargetingRange)
                {
                    Ai.MaxTargetingRange    = maxTrajectory + Ai.MyGrid.PositionComp.LocalVolume.Radius;
                    Ai.MaxTargetingRangeSqr = Ai.MaxTargetingRange * Ai.MaxTargetingRange;
                }

                Ai.OptimalDps += PeakDps;
                Ai.UpdateConstruct();

                if (!FunctionalBlock.Enabled)
                {
                    for (int i = 0; i < Platform.Weapons.Length; i++)
                    {
                        Platform.Weapons[i].EventTriggerStateChanged(EventTriggers.TurnOff, true);
                    }
                }

                Status = !IsWorking ? Start.Starting : Start.ReInit;
            }
            catch (Exception ex) { Log.Line($"Exception in OnAddedToSceneTasks: {ex} AiNull:{Ai == null} - SessionNull:{Session == null} EntNull{Entity == null} MyCubeNull:{MyCube?.CubeGrid == null}"); }
        }
Example #2
0
        internal void OnAddedToSceneTasks()
        {
            try {
                if (Ai.MarkedForClose)
                {
                    Log.Line($"OnAddedToSceneTasks and AI MarkedForClose - Subtype:{MyCube.BlockDefinition.Id.SubtypeName} - grid:{MyCube.CubeGrid.DebugName} - CubeMarked:{MyCube.MarkedForClose}({Entity?.MarkedForClose}) - GridMarked:{MyCube.CubeGrid.MarkedForClose}({Entity?.GetTopMostParent()?.MarkedForClose}) - GridMatch:{MyCube.CubeGrid == Ai.MyGrid} - AiContainsMe:{Ai.WeaponBase.ContainsKey(MyCube)} - MyGridInAi:{Ai.Session.GridToMasterAi.ContainsKey(MyCube.CubeGrid)}[{Ai.Session.GridTargetingAIs.ContainsKey(MyCube.CubeGrid)}]");
                }
                Ai.UpdatePowerSources = true;
                RegisterEvents();
                if (!Ai.GridInit)
                {
                    Ai.GridInit = true;
                    var fatList = Session.GridToInfoMap[MyCube.CubeGrid].MyCubeBocks;

                    for (int i = 0; i < fatList.Count; i++)
                    {
                        var cubeBlock = fatList[i];
                        if (cubeBlock is MyBatteryBlock || cubeBlock.HasInventory)
                        {
                            Ai.FatBlockAdded(cubeBlock);
                        }
                    }

                    SubGridInit();
                }

                var maxTrajectory = 0d;

                for (int i = 0; i < Platform.Weapons.Length; i++)
                {
                    var weapon = Platform.Weapons[i];
                    weapon.InitTracking();

                    double weaponMaxRange;
                    DpsAndHeatInit(weapon, out weaponMaxRange);

                    if (maxTrajectory < weaponMaxRange)
                    {
                        maxTrajectory = weaponMaxRange;
                    }

                    if (weapon.Ammo.CurrentAmmo > weapon.ActiveAmmoDef.AmmoDef.Const.MagazineSize)
                    {
                        weapon.Ammo.CurrentAmmo = weapon.ActiveAmmoDef.AmmoDef.Const.MagazineSize;
                    }

                    if (Session.IsServer && weapon.TrackTarget)
                    {
                        Session.AcqManager.Monitor(weapon.Acquire);
                    }
                }

                if (maxTrajectory + Ai.MyGrid.PositionComp.LocalVolume.Radius > Ai.MaxTargetingRange)
                {
                    Ai.MaxTargetingRange    = maxTrajectory + Ai.MyGrid.PositionComp.LocalVolume.Radius;
                    Ai.MaxTargetingRangeSqr = Ai.MaxTargetingRange * Ai.MaxTargetingRange;
                }

                Ai.OptimalDps   += PeakDps;
                Ai.EffectiveDps += EffectiveDps;


                if (!Ai.WeaponBase.TryAdd(MyCube, this))
                {
                    Log.Line($"failed to add cube to gridAi");
                }

                Ai.CompChange(true, this);

                Ai.IsStatic = Ai.MyGrid.Physics?.IsStatic ?? false;
                Ai.Construct.Refresh(Ai, Constructs.RefreshCaller.Init);

                if (!FunctionalBlock.Enabled)
                {
                    for (int i = 0; i < Platform.Weapons.Length; i++)
                    {
                        Session.FutureEvents.Schedule(Platform.Weapons[i].DelayedStart, null, 1);
                    }
                }

                TurretBase?.SetTarget(Vector3D.MaxValue);

                Status = !IsWorking ? Start.Starting : Start.ReInit;
            }
            catch (Exception ex) { Log.Line($"Exception in OnAddedToSceneTasks: {ex} AiNull:{Ai == null} - SessionNull:{Session == null} EntNull{Entity == null} MyCubeNull:{MyCube?.CubeGrid == null}"); }
        }