Example #1
0
        //Here we check to see if the current vessel or target vessel has an asteroid attached
        private bool asteroidInRange()
        {
            if (vessel.FindPartModulesImplementing <ModuleAsteroid>().Count > 0)
            {
                return(true);
            }

            Vessel           targetVessel = FlightGlobals.fetch.VesselTarget as Vessel;
            FlightCoMTracker targetObj    = FlightGlobals.fetch.VesselTarget as FlightCoMTracker;

            if (targetVessel != null)
            {
                List <ModuleAsteroid> asteroids = targetVessel.FindPartModulesImplementing <ModuleAsteroid>();
                if (asteroids.Count > 0)
                {
                    foreach (ModuleAsteroid astMod in asteroids)
                    {
                        Vector3d astPos = astMod.part.transform.position;
                        Vector3d pPos   = part.transform.position;
                        if ((astPos - pPos).magnitude < 200)
                        {
                            return(true);
                        }
                    }
                }
            }
            else if (targetObj != null)
            {
                List <ModuleAsteroid> asteroids = targetObj.GetOrbitDriver().vessel.FindPartModulesImplementing <ModuleAsteroid>();
                if (asteroids.Count > 0)
                {
                    foreach (ModuleAsteroid astMod in asteroids)
                    {
                        Vector3d astPos = astMod.part.transform.position;
                        Vector3d pPos   = part.transform.position;
                        if ((astPos - pPos).magnitude < 200)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
        private void Update()
        {
            if (HighLogic.LoadedSceneIsFlight)
            {
                EventsCheck();
                string s = "Deactivated";
                if (fullyDeployed && resourceOn)
                {
                    s = "Searching...";
                    Vessel           target    = FlightGlobals.fetch.VesselTarget as Vessel;
                    FlightCoMTracker targetObj = FlightGlobals.fetch.VesselTarget as FlightCoMTracker;

                    if (target != null)
                    {
                        targetModule = target.FindPartModulesImplementing <DMAsteroidScanner>().FirstOrDefault();
                    }
                    else if (targetObj != null)
                    {
                        var targets = targetObj.GetOrbitDriver().vessel.FindPartModulesImplementing <DMAsteroidScanner>();
                        if (targets.Count > 0)
                        {
                            if (targets.Count > 1)
                            {
                                foreach (DMAsteroidScanner t in targets)
                                {
                                    if (t == this)
                                    {
                                        continue;
                                    }

                                    float d = (t.part.transform.position - dishBase.position).sqrMagnitude;
                                    if (d > 4f)
                                    {
                                        targetModule = t;
                                        break;
                                    }
                                    else
                                    {
                                        targetModule    = null;
                                        targetDistance  = 0f;
                                        receiverInRange = asteroidInSight = false;
                                    }
                                }
                            }
                            else if (targets[0] != this)
                            {
                                targetModule = targets[0];
                            }
                        }
                        else
                        {
                            targetModule    = null;
                            targetDistance  = 0f;
                            receiverInRange = asteroidInSight = false;
                        }
                    }
                    else
                    {
                        var targets = vessel.FindPartModulesImplementing <DMAsteroidScanner>();
                        if (targets.Count > 1)
                        {
                            foreach (DMAsteroidScanner t in targets)
                            {
                                if (t == this)
                                {
                                    continue;
                                }

                                float d = (t.part.transform.position - dishBase.position).sqrMagnitude;
                                if (d > 4f)
                                {
                                    targetModule = t;
                                    break;
                                }
                                else
                                {
                                    targetDistance  = 0f;
                                    targetModule    = null;
                                    receiverInRange = asteroidInSight = false;
                                }
                            }
                        }
                        else
                        {
                            targetDistance  = 0f;
                            targetModule    = null;
                            receiverInRange = asteroidInSight = false;
                        }
                    }

                    if (targetModule != null)
                    {
                        s = "Receiver Located";
                        targetDistance = (targetModule.part.transform.position - dishBase.position).sqrMagnitude;
                        if (targetDistance < (2000 * 2000) && targetDistance > 4)
                        {
                            s = "Receiver In Range";
                            receiverInRange = true;
                            if (simpleRayHit())
                            {
                                s = "Experiment Ready";
                                asteroidInSight = true;
                            }
                            else
                            {
                                asteroidInSight = false;
                            }
                        }
                        else
                        {
                            receiverInRange = asteroidInSight = false;
                            targetDistance  = 0f;
                        }
                    }
                    else
                    {
                        receiverInRange = asteroidInSight = false;
                        targetDistance  = 0f;
                    }

                    if (receiverInRange && targetModule != null)
                    {
                        lookAtTarget();
                    }
                    else
                    {
                        searchForTarget();
                    }
                    rotating = true;
                }
                else if (fullyDeployed && !resourceOn && resourceCost > 0f)
                {
                    s = "No Power";
                    targetDistance  = 0f;
                    targetModule    = null;
                    receiverInRange = asteroidInSight = false;
                }
                else if (rotating)
                {
                    spinDownDish();
                    targetDistance  = 0f;
                    targetModule    = null;
                    receiverInRange = asteroidInSight = false;
                }
                else
                {
                    targetDistance  = 0f;
                    targetModule    = null;
                    receiverInRange = asteroidInSight = false;
                }

                lightSwitch(receiverInRange && asteroidInSight, !IsDeployed);

                status = s;
            }
        }