private static void RescindClaim(IMyMotorStator stator, MotorTurret turret) { lock (claimedStator) { List <MotorTurret> users; if (claimedStator.TryGetValue(stator, out users)) { int index = users.IndexOf(turret); if (index < 0) { Logger.DebugLog("Cannot remove claim on " + stator.nameWithId() + " by " + turret.FaceBlock.nameWithId() + ", not in users list"); return; } SetClaim(stator, turret, false); if (users.Count == 1) { Logger.DebugLog("removing only claim: " + stator.nameWithId() + "/" + turret.FaceBlock.nameWithId()); claimedStator.Remove(stator); return; } users.RemoveAtFast(index); UpdateClaim(stator); } else { Logger.DebugLog("Cannot remove claim on " + stator.nameWithId() + " by " + turret.FaceBlock.nameWithId() + ", no users list"); return; } } }
private static void UpdateClaim(IMyMotorStator stator) { List <MotorTurret> users = claimedStator[stator]; MotorTurret currentClaimant = users[0]; if (currentClaimant.StatorEl == stator) { if (!currentClaimant.m_claimedElevation) { currentClaimant = null; } } else if (currentClaimant.StatorAz == stator) { if (!currentClaimant.m_claimedAzimuth) { currentClaimant = null; } } else { throw new Exception(currentClaimant.FaceBlock.nameWithId() + " does not have " + stator.nameWithId()); } int middle = GetMiddle(users); MotorTurret middleClaimant = users[middle]; if (currentClaimant == middleClaimant) { Logger.DebugLog("Claim for " + stator.nameWithId() + " is up-to-date. Claimed by " + currentClaimant.FaceBlock.nameWithId()); return; } Logger.DebugLog("claim of " + stator.nameWithId() + " changed from " + currentClaimant?.FaceBlock.nameWithId() + " to " + middleClaimant.FaceBlock.nameWithId()); if (middle != 0) { users.Swap(0, middle); } if (currentClaimant != null) { SetClaim(stator, currentClaimant, false); } SetClaim(stator, middleClaimant, true); }
private static void RegisterClaim(IMyMotorStator stator, MotorTurret turret) { lock (claimedStator) { List <MotorTurret> users; if (claimedStator.TryGetValue(stator, out users)) { Debug.Assert(!users.Contains(turret), turret.FaceBlock.nameWithId() + " is already in users list"); Logger.DebugLog("registered claim: " + stator.nameWithId() + "/" + turret.FaceBlock.nameWithId()); users.Add(turret); UpdateClaim(stator); } else { Logger.DebugLog("first claim: " + stator.nameWithId() + "/" + turret.FaceBlock.nameWithId()); users = new List <MotorTurret>(); claimedStator.Add(stator, users); users.Add(turret); SetClaim(stator, turret, true); } } }
private bool GetStatorRotor(IMyCubeGrid grid, out IMyMotorStator Stator, IMyMotorStator IgnoreStator = null) { CubeGridCache cache = CubeGridCache.GetFor(grid); // first stator that are found that are not working // returned if a working set is not found IMyMotorStator offStator = null; foreach (MyObjectBuilderType type in types_Rotor) { foreach (IMyMotorRotor rotor in cache.BlocksOfType(type)) { if (!FaceBlock.canControlBlock(rotor)) { Log.DebugLog("Cannot control: " + rotor.nameWithId()); continue; } Stator = (IMyMotorStator)rotor.Base; if (Stator == null || Stator == IgnoreStator) { continue; } if (IgnoreStator != null && !ArePerpendicular(IgnoreStator, Stator)) { Log.DebugLog("Not perpendicular: " + IgnoreStator.nameWithId() + " & " + Stator.nameWithId()); continue; } if (Stator.IsWorking) { return(true); } else if (offStator == null) { offStator = Stator; } } } if (offStator != null) { Stator = offStator; return(true); } Stator = null; return(false); }
private static void SetClaim(IMyMotorStator stator, MotorTurret turret, bool claimed) { Logger.DebugLog(nameof(stator) + ": " + stator.nameWithId() + ", " + nameof(turret) + ": " + turret.FaceBlock.nameWithId() + ", " + nameof(claimed) + ": " + claimed); if (turret.StatorEl == stator) { turret.m_claimedElevation = claimed; } else if (turret.StatorAz == stator) { turret.m_claimedAzimuth = claimed; } else { throw new Exception(turret.FaceBlock.nameWithId() + " does not have " + stator.nameWithId()); } }
public void SetTarget(float targetAngle) { if (Flipped) { targetAngle = -targetAngle; } float targetVelocity = (targetAngle - Stator.Angle) * 60f; float currentVelocity = ((MyMotorStator)Stator).TargetVelocity; if (((targetVelocity == 0f) != (currentVelocity == 0f)) || Math.Abs(targetVelocity - currentVelocity) > 0.1f) { VelocityProperty.SetValue(Stator, targetVelocity); } Logger.TraceLog("target acceleration: " + targetAngle + ", target angle: " + targetAngle + ", stator angle: " + Stator.Angle + ", target velocity: " + targetVelocity + ", current velocity: " + currentVelocity, context: Stator.CubeGrid.nameWithId(), primaryState: Stator.nameWithId()); }
private static bool ArePerpendicular(IMyMotorStator statorOne, IMyMotorStator statorTwo) { Logger.TraceLog(nameof(statorOne) + ": " + statorOne.nameWithId() + ", Up: " + statorOne.WorldMatrix.Up + ", " + nameof(statorTwo) + ": " + statorTwo.nameWithId() + ", Up: " + statorTwo.WorldMatrix.Up + ", dot: " + statorOne.WorldMatrix.Up.Dot(statorTwo.WorldMatrix.Up)); return(Math.Abs(statorOne.WorldMatrix.Up.Dot(statorTwo.WorldMatrix.Up)) < 0.1f); }