示例#1
0
 public void addForce(Vector3 force, ForceMode mode)
 {
     if (forceableType == ForceableType.part)
     {
         ((Part)forceTarget).Rigidbody.AddForce(force, mode);
     }
     else if (forceableType == ForceableType.vessel)
     {
         if (!((Vessel)forceTarget).loaded)
         {
             ((Vessel)forceTarget).Load();
         }
         if (!((Vessel)forceTarget).rootPart.Modules.Contains <ModuleRangeReverter>())
         {
             ModuleRangeReverter.SetVesselRanges(((Vessel)forceTarget), ranges + 2000, ranges + 10000, ranges + 5000, ranges, ranges);
         }
         ((Vessel)forceTarget).addForce(force, mode);
     }
 }
        public void FixedUpdate()
        {
            if (HighLogic.LoadedSceneIsFlight)
            {
                double EEAvailable;
                double EEMax;
                double maxEEUpdate = maxEESec * TimeWarp.fixedDeltaTime;
                part.GetConnectedResourceTotals(Constants.EEDefinition.id, out EEAvailable, out EEMax);
                if (EEAvailable < maxEEUpdate)
                {
                    maxEEUpdate = EEAvailable;
                }

                double remainingEEUpdate = maxEEUpdate;

                if (applyTorque && (vessel.ctrlState.yaw != 0 || vessel.ctrlState.pitch != 0 || vessel.ctrlState.roll != 0))
                {
                    double EETorqueCost = Math.Max(Math.Max(vessel.ctrlState.yaw, vessel.ctrlState.pitch), vessel.ctrlState.roll) * maxTorqueEE * TimeWarp.fixedDeltaTime;
                    double throttleLimit;

                    if (remainingEEUpdate < EETorqueCost)
                    {
                        throttleLimit = remainingEEUpdate / EETorqueCost;
                        torqueStatus  = "Power Limited " + throttleLimit.ToString("P2");
                    }
                    else
                    {
                        throttleLimit = 1;
                        torqueStatus  = "Full power";
                    }

                    part.Rigidbody.AddTorque(transform.forward * (float)maxTorque * -vessel.ctrlState.yaw * (float)throttleLimit * TimeWarp.fixedDeltaTime, ForceMode.Impulse);
                    part.Rigidbody.AddTorque(transform.right * (float)maxTorque * -vessel.ctrlState.pitch * (float)throttleLimit * TimeWarp.fixedDeltaTime, ForceMode.Impulse);
                    part.Rigidbody.AddTorque(transform.up * (float)maxTorque * -vessel.ctrlState.roll * (float)throttleLimit * TimeWarp.fixedDeltaTime, ForceMode.Impulse);
                }
                else
                {
                    torqueStatus = "Inactive";
                }

                updateForceables();

                //Test to make sure we have a valid sink
                if (!sinkValid)
                {
                    thrustStatus        = "No Sink";
                    this.sourceDistance = 0f;
                }
                else if (!sourceValid)
                {
                    thrustStatus        = "No Source";
                    this.sourceDistance = 0f;
                }
                else if (sinkForceable.Equals(sourceForceable))
                {
                    thrustStatus = "Source & Sink Same";
                }
                else if (active)
                {
                    if (!part.vessel.rootPart.Modules.Contains <ModuleRangeReverter>())
                    {
                        ModuleRangeReverter.SetVesselRanges(vessel, (float)minForceloadRange + 2000, (float)minForceloadRange + 10000, (float)minForceloadRange + 5000, (float)minForceloadRange, (float)minForceloadRange);
                    }
                    sourceDistance = sourceForceable.distanceFrom(part);
                    sinkDistance   = sinkForceable.distanceFrom(part);

                    double EEDistanceCost = (EECostPerMeterCurve.Evaluate(sourceForceable.distanceFrom(part)) + EECostPerMeterCurve.Evaluate(sinkForceable.distanceFrom(part))) * TimeWarp.fixedDeltaTime;
                    remainingEEUpdate -= EEDistanceCost;

                    if (remainingEEUpdate > 0)
                    {
                        if (shuntMode == ShuntMode.ManualControl && vessel.ctrlState.mainThrottle > 0)
                        {
                            double EEThrustCost = vessel.ctrlState.mainThrottle * maxThrustEE * TimeWarp.fixedDeltaTime;
                            double throttleLimit;

                            if (remainingEEUpdate < EEThrustCost)
                            {
                                throttleLimit = remainingEEUpdate / EEThrustCost;
                                thrustStatus  = "Power Limited " + throttleLimit.ToString("P2");
                            }
                            else
                            {
                                throttleLimit = 1;
                                thrustStatus  = "Full power";
                            }

                            remainingEEUpdate -= EEThrustCost * throttleLimit;
                            part.RequestResource(Constants.EEDefinition.id, EEDistanceCost + EEThrustCost * throttleLimit);

                            //Accelerate in part's forward direction
                            Vector3 force = transform.up * (float)maxThrust * vessel.ctrlState.mainThrottle * (float)throttleLimit;
                            force *= TimeWarp.fixedDeltaTime;

                            transferKineticEnergy(sourceForceable, sinkForceable, force);
                        }
                        else if (shuntMode == ShuntMode.KillSurfaceVelocity)
                        {
                            Vector3 force = (sourceForceable.getSrfVelocity() + sourceForceable.getGraviticAcceleration() + sourceForceable.getCentrifugalAcceleration()) * (float)sourceForceable.getVesselMass() * -1;
                            if (force.magnitude > maxThrust)
                            {
                                force = force.normalized * (float)maxThrust;
                            }
                            force *= TimeWarp.fixedDeltaTime;

                            double EEThrustCost = (force.magnitude / maxThrust) * maxThrustEE;
                            double throttleLimit;

                            if (remainingEEUpdate < EEThrustCost)
                            {
                                throttleLimit = remainingEEUpdate / EEThrustCost;
                                thrustStatus  = "Power Limited " + throttleLimit.ToString("P2");
                            }
                            else
                            {
                                throttleLimit = 1;
                                thrustStatus  = "Full power";
                            }
                            force *= (float)throttleLimit;

                            remainingEEUpdate -= EEThrustCost * throttleLimit;
                            part.RequestResource(Constants.EEDefinition.id, EEDistanceCost + EEThrustCost * throttleLimit);

                            transferKineticEnergy(sourceForceable, sinkForceable, force);
                        }
                        else if (shuntMode == ShuntMode.KillOrbitalVelocity)
                        {
                            Vector3 force = (sourceForceable.getObtVelocity() + sourceForceable.getGraviticAcceleration() + sourceForceable.getCentrifugalAcceleration()) * (float)sourceForceable.getVesselMass() * -1;
                            if (force.magnitude > maxThrust)
                            {
                                force = force.normalized * (float)maxThrust;
                            }
                            force *= TimeWarp.fixedDeltaTime;

                            double EEThrustCost = (force.magnitude / maxThrust) * maxThrustEE;
                            double throttleLimit;

                            if (remainingEEUpdate < EEThrustCost)
                            {
                                throttleLimit = remainingEEUpdate / EEThrustCost;
                                thrustStatus  = "Power Limited " + throttleLimit.ToString("P2");
                            }
                            else
                            {
                                throttleLimit = 1;
                                thrustStatus  = "Full power";
                            }
                            force *= (float)throttleLimit;

                            remainingEEUpdate -= EEThrustCost * throttleLimit;
                            part.RequestResource(Constants.EEDefinition.id, EEDistanceCost + EEThrustCost * throttleLimit);

                            transferKineticEnergy(sourceForceable, sinkForceable, force);
                        }
                        else if (shuntMode == ShuntMode.KillRelativeVelocity)
                        {
                            if (sourceTarget == ShuntTarget.Self)
                            {
                                thrustStatus = "Invalid source";
                            }
                            else
                            {
                                Vector3 force;
                                if (sinkTarget != ShuntTarget.Self)
                                {
                                    force = (sourceForceable.getObtVelocity() - vessel.GetObtVelocity()) * (float)sourceForceable.getVesselMass() * -1;
                                }
                                else
                                {
                                    force = (sourceForceable.getObtVelocity() - vessel.GetObtVelocity()) * ((float)sourceForceable.getVesselMass() + vessel.GetTotalMass()) / 2 * -1;
                                }

                                if (force.magnitude > maxThrust)
                                {
                                    force = force.normalized * (float)maxThrust;
                                }
                                force *= TimeWarp.fixedDeltaTime;

                                double EEThrustCost = (force.magnitude / maxThrust) * maxThrustEE;
                                double throttleLimit;

                                if (remainingEEUpdate < EEThrustCost)
                                {
                                    throttleLimit = remainingEEUpdate / EEThrustCost;
                                    thrustStatus  = "Power Limited " + throttleLimit.ToString("P2");
                                }
                                else
                                {
                                    throttleLimit = 1;
                                    thrustStatus  = "Full power";
                                }
                                force *= (float)throttleLimit;

                                remainingEEUpdate -= EEThrustCost * throttleLimit;
                                part.RequestResource(Constants.EEDefinition.id, EEDistanceCost + EEThrustCost * throttleLimit);

                                transferKineticEnergy(sourceForceable, sinkForceable, force);
                            }
                        }
                        else
                        {
                            thrustStatus = "Inactive";
                        }
                    }
                    else
                    {
                        thrustStatus       = "Max Distance Exceeded";
                        remainingEEUpdate += EEDistanceCost;
                    }
                }
                else
                {
                    thrustStatus = "Inactive";
                }
            }
        }