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 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()); } }
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); } } }