public SolverTask(RCSSolverKey key, Vector3 direction, Vector3 rotation) { this.key = key; this.direction = direction; this.rotation = rotation; this.timeSubmitted = DateTime.Now; }
// The list of throttles is ordered under the assumption that you iterate // over the vessel as follows: // foreach part in vessel.parts: // foreach rcsModule in part.Modules.OfType<ModuleRCS>: // ... // Note that rotation balancing is not supported at the moment. public void GetThrottles(Vessel vessel, VesselState state, Vector3 direction, out double[] throttles, out List <RCSSolver.Thruster> thrustersOut) { thrustersOut = callerThrusters; Vector3 rotation = Vector3.zero; var rcsBalancer = VesselExtensions.GetMasterMechJeb(vessel).rcsbal; // Update vessel info if needed. CheckVessel(vessel, state); Vector3 dir = direction.normalized; RCSSolverKey key = new RCSSolverKey(ref dir, rotation); if (thrusters.Count == 0) { throttles = double0; } else if (direction == Vector3.zero) { throttles = originalThrottles; } else if (results.TryGetValue(key, out throttles)) { cacheHits++; } else { // This task hasn't been calculated. We'll handle that here. // Meanwhile, TryGetValue() will have set 'throttles' to null, but // we'll make it a 0-element array instead to avoid null checks. cacheMisses++; throttles = double0; if (pending.Contains(key)) { // We've submitted this key before, so we need to check the // results queue. while (resultsQueue.Count > 0) { SolverResult sr = (SolverResult)resultsQueue.Dequeue(); results[sr.key] = sr.throttles; pending.Remove(sr.key); if (sr.key == key) { throttles = sr.throttles; } } } else { // This task was neither calculated nor pending, so we've never // submitted it. Do so! pending.Add(key); tasks.Enqueue(new SolverTask(key, dir, rotation)); workEvent.Set(); } } // Return a copy of the array to make sure ours isn't modified. throttles = (double[])throttles.Clone(); }
public SolverResult(RCSSolverKey key, double[] throttles) { this.key = key; this.throttles = throttles; }
// The list of throttles is ordered under the assumption that you iterate // over the vessel as follows: // foreach part in vessel.parts: // foreach rcsModule in part.Modules.OfType<ModuleRCS>: // ... // Note that rotation balancing is not supported at the moment. public void GetThrottles(Vessel vessel, VesselState state, Vector3 direction, out double[] throttles, out List<RCSSolver.Thruster> thrustersOut) { thrustersOut = callerThrusters; Vector3 rotation = Vector3.zero; var rcsBalancer = VesselExtensions.GetMasterMechJeb(vessel).rcsbal; // Update vessel info if needed. CheckVessel(vessel, state); Vector3 dir = direction.normalized; RCSSolverKey key = new RCSSolverKey(ref dir, rotation); if (thrusters.Count == 0) { throttles = double0; } else if (direction == Vector3.zero) { throttles = originalThrottles; } else if (results.TryGetValue(key, out throttles)) { cacheHits++; } else { // This task hasn't been calculated. We'll handle that here. // Meanwhile, TryGetValue() will have set 'throttles' to null, but // we'll make it a 0-element array instead to avoid null checks. cacheMisses++; throttles = double0; if (pending.Contains(key)) { // We've submitted this key before, so we need to check the // results queue. while (resultsQueue.Count > 0) { SolverResult sr = (SolverResult)resultsQueue.Dequeue(); results[sr.key] = sr.throttles; pending.Remove(sr.key); if (sr.key == key) { throttles = sr.throttles; } } } else { // This task was neither calculated nor pending, so we've never // submitted it. Do so! pending.Add(key); tasks.Enqueue(new SolverTask(key, dir, rotation)); workEvent.Set(); } } // Return a copy of the array to make sure ours isn't modified. throttles = (double[])throttles.Clone(); }