public TurretBase(IMyCubeBlock CubeBlock) { try { if (!Settings.boolSettings[Settings.BoolSetName.bAllowTurretControl]) { return; } this.myCubeBlock = CubeBlock; this.myTerminal = CubeBlock as IMyTerminalBlock; this.myTurretBase = CubeBlock as Ingame.IMyLargeTurretBase; this.myLogger = new Logger(myCubeBlock.CubeGrid.DisplayName, "TurretBase", myCubeBlock.DisplayNameText); this.myCubeBlock.throwIfNull_variable("myCubeBlock"); this.myTerminal.throwIfNull_variable("myTerminal"); this.myTurretBase.throwIfNull_variable("myTurretBase"); myLogger.debugLog("created for: " + myCubeBlock.DisplayNameText, "DelayedInit()"); //EnforcedUpdate = Sandbox.Common.MyEntityUpdateEnum.EACH_FRAME; // want as many opportunities to lock main thread as possible if (!(myTurretBase.DisplayNameText.Contains("[") || myTurretBase.DisplayNameText.Contains("]"))) { if (myTurretBase.OwnerId.Is_ID_NPC()) { myTurretBase.SetCustomName(myTurretBase.DisplayNameText + " " + Settings.stringSettings[Settings.StringSetName.sSmartTurretDefaultNPC]); } else { myTurretBase.SetCustomName(myTurretBase.DisplayNameText + " " + Settings.stringSettings[Settings.StringSetName.sSmartTurretDefaultPlayer]); } } TurretBase_CustomNameChanged(null); myTerminal.CustomNameChanged += TurretBase_CustomNameChanged; myTerminal.OwnershipChanged += myTerminal_OwnershipChanged; // definition limits MyLargeTurretBaseDefinition definition = MyDefinitionManager.Static.GetCubeBlockDefinition(myCubeBlock.getSlimObjectBuilder()) as MyLargeTurretBaseDefinition; definition.throwIfNull_variable("definition"); minElevation = (float)Math.Max(definition.MinElevationDegrees / 180 * Math.PI, -0.6); // -0.6 was determined empirically maxElevation = (float)Math.Min(definition.MaxElevationDegrees / 180 * Math.PI, Math.PI / 2); minAzimuth = (float)(definition.MinAzimuthDegrees / 180 * Math.PI); maxAzimuth = (float)(definition.MaxAzimuthDegrees / 180 * Math.PI); myLogger.debugLog("definition limits = " + definition.MinElevationDegrees + ", " + definition.MaxElevationDegrees + ", " + definition.MinAzimuthDegrees + ", " + definition.MaxAzimuthDegrees, "DelayedInit()"); myLogger.debugLog("radian limits = " + minElevation + ", " + maxElevation + ", " + minAzimuth + ", " + maxAzimuth, "DelayedInit()"); CubeBlock.OnClosing += Close; } catch (Exception e) { if (myCubeBlock == null) { myLogger.log("failed to initialize myCubeBlock == null, Exception:" + e, "DelayedInit()", Logger.severity.ERROR); } else { myLogger.log("failed to initialize for " + myCubeBlock.DisplayNameText + ", Exception:" + e, "DelayedInit()", Logger.severity.WARNING); } Close(null); return; } }
public void UpdateAfterSimulation() { if (myCubeBlock == null) { return; } //if (needToRelease) // lock_notMyUpdate.ReleaseExclusive(); //needToRelease = false; try { if (!myCubeBlock.IsWorking || myCubeBlock.Closed) { return; } if (updateCount % 100 == 0) // every 100 updates { if (updateCount == 1000) { myLogger.debugLog("update 1000", "UpdateAfterSimulation()"); reset(); return; } controlEnabled = myCubeBlock.DisplayNameText.Contains("[") && myCubeBlock.DisplayNameText.Contains("]"); TurretBase_CustomNameChanged(null); if (myAntenna == null || myAntenna.CubeBlock == null || !myAntenna.CubeBlock.canSendTo(myCubeBlock, true)) { //myLogger.debugLog("searching for attached antenna", "UpdateAfterSimulation10()"); myAntenna = null; foreach (Receiver antenna in RadioAntenna.registry) { if (antenna.CubeBlock.canSendTo(myCubeBlock, true)) { myLogger.debugLog("found attached antenna: " + antenna.CubeBlock.DisplayNameText, "UpdateAfterSimulation10()", Logger.severity.INFO); myAntenna = antenna; break; } } if (myAntenna == null) { foreach (Receiver antenna in LaserAntenna.registry) { if (antenna.CubeBlock.canSendTo(myCubeBlock, true)) { myLogger.debugLog("found attached antenna: " + antenna.CubeBlock.DisplayNameText, "UpdateAfterSimulation10()", Logger.severity.INFO); myAntenna = antenna; break; } } } //if (myAntenna == null) //myLogger.debugLog("did not find attached antenna", "UpdateAfterSimulation10()"); } MyObjectBuilder_TurretBase builder = myCubeBlock.getSlimObjectBuilder() as MyObjectBuilder_TurretBase; targetMissiles = builder.TargetMissiles; targetMeteors = MeteorsEnabled && builder.TargetMeteors; targetCharacters = builder.TargetCharacters; targetMoving = builder.TargetMoving; targetLargeGrids = builder.TargetLargeGrids; targetSmallGrids = builder.TargetSmallGrids; targetStations = builder.TargetStations; if (possibleTargets()) { if (CurrentState == State.NO_POSSIBLE) { myLogger.debugLog("now possible to target", "UpdateAfterSimulation()"); CurrentState = State.OFF; } } else if (CurrentState != State.NO_POSSIBLE) { myLogger.debugLog("no longer possible to target", "UpdateAfterSimulation()"); setNoTarget(); CurrentState = State.NO_POSSIBLE; } } // end if (updateCount % 100 == 0) if (CurrentState == State.NO_POSSIBLE) { return; } if (!controlEnabled) { if (CurrentState != State.OFF) { CurrentState = State.OFF; //myTurretBase.TrackTarget(null); myTurretBase.ResetTargetingToDefault(); } return; } if (CurrentState == State.OFF) { if (defaultTargetingAcquiredTarget) { setNoTarget(); } else { CurrentState = State.WAIT_DTAT; } } // Wait for default targeting to acquire a target. if (CurrentState == State.WAIT_DTAT) { if (updateCount % 10 == 0) { turretPosition = myCubeBlock.GetPosition(); MyObjectBuilder_TurretBase builder = myCubeBlock.getSlimObjectBuilder() as MyObjectBuilder_TurretBase; IMyEntity target; if (MyAPIGateway.Entities.TryGetEntityById(builder.Target, out target)) { if ((target is IMyCubeBlock || target is IMyMeteor) && canLase(target)) { defaultTargetingAcquiredTarget = true; setNoTarget(); myLogger.debugLog("default targeting acquired a target: " + target.getBestName(), "UpdateAfterSimulation()", Logger.severity.DEBUG); } } } return; } // start thread if (!queued && updateCount % 10 == 0) { queued = true; TurretThread.EnqueueAction(Update); } } catch (Exception e) { myLogger.log("Exception: " + e, "UpdateAfterSimulation10()", Logger.severity.ERROR); } finally { updateCount++; //needToRelease = true; //lock_notMyUpdate.AcquireExclusive(); } }