public void CalcVelocity()
 {
     foreach (KeyValuePair <string, LCARS_PhotonTorpedoType> pair in TorpedoList)
     {
         LCARS_PhotonTorpedoType PTT = pair.Value;
         try // in case the target ship explodes..
         {
             string     ID           = pair.Key;
             GameObject TorpedoClone = PTT.TorpedoClone;
             PTT.savedVel = (prevPos - TorpedoClone.transform.position) / Time.deltaTime;
             PTT.prevPos  = TorpedoClone.transform.position;
         }
         catch // in case the target ship explodes..
         {
             //DestroyPhotonTorpedo(PTT.TorpedoClone.name);
             continue;
         }
     }
 }
        public void TorpedoUpdate()
        {
            CalcVelocity();

            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate begin ");
            int i = 0;

            foreach (KeyValuePair <string, LCARS_PhotonTorpedoType> pair in TorpedoList)
            {
                LCARS_PhotonTorpedoType PTT = pair.Value;
                string ID = pair.Key;
                Target t  = PTT.Target;

                GameObject TorpedoClone    = PTT.TorpedoClone;
                float      homing_distance = Vector3.Distance(FlightGlobals.ActiveVessel.GetWorldPos3D(), TorpedoClone.transform.position);
                float      target_distance = Vector3.Distance(t.ship.transform.localPosition, TorpedoClone.transform.position);
                UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate  ID=" + ID + " homing_distance=" + homing_distance + " target_distance=" + target_distance);

                if (homing_distance > 2000)
                {
                    Destroy(TorpedoClone, 0.1f);
                    TorpedoList.Remove(ID);
                    continue;
                }

                try // in case the target ship explodes..
                {
                    TorpedoClone.SetActive(true);
                    TorpedoClone.rigidbody.collider.enabled   = true;
                    TorpedoClone.rigidbody.collider.isTrigger = true;
                    TorpedoClone.rigidbody.isKinematic        = false;
                    //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate 5 ");



                    t.ship.GoOffRails();
                    //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate 7 ");
                    t.ship.rigidbody.isKinematic = false;
                    //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate 8 ");
                    t.ship.rigidbody.detectCollisions = true;
                    //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate 9 ");
                    //t.ship.rigidbody.collider.isTrigger = true;
                    //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate 10 ");

                    //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate 6 ");

                    if (TorpedoClone.rigidbody != null && t.ship.transform != null)
                    {
                        if (target_distance > 5F)
                        {
                            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate 7 ");


                            Vector3 geeVector = FlightGlobals.getGeeForceAtPosition(TorpedoClone.transform.position);
                            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate 8 geeVector=" + geeVector);
                            CancelG2(TorpedoClone, geeVector);

                            //TorpedoClone.transform.LookAt(t.ship.transform, TorpedoClone.transform.forward);
                            //TorpedoClone.transform.LookAt(t.ship.transform.localPosition, Vector3.up);
                            //TorpedoClone.transform.LookAt(t.ship.transform, Vector3.up);
                            //TorpedoClone.transform.LookAt(t.ship.transform, t.ship.transform.forward);

                            TorpedoClone.transform.LookAt(t.ship.transform, t.ship.upAxis);



                            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate 10 ");

                            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate PTT.savedVel.sqrMagnitude=" + PTT.savedVel.sqrMagnitude);
                            this.AddNewForce_ffwd_back(1200f, TorpedoClone);
                            if (PTT.savedVel.sqrMagnitude < (2f * 2f))
                            {
                            }
                            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate 11 ");

                            //PilotMode(TorpedoClone, PTT.savedVel, true);

                            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate 12 ");
                        }
                        else
                        {
                            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate proximity alert!!!! check_distance=" + check_distance);
                            DestroyPhotonTorpedo(PTT.TorpedoClone.name);
                        }
                    }
                    else
                    {
                        //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate 13 ");
                        DestroyPhotonTorpedo(PTT.TorpedoClone.name);
                    }


                    i++;
                }
                catch // in case the target ship explodes..
                {
                    DestroyPhotonTorpedo(PTT.TorpedoClone.name);
                    //UnityEngine.Debug.Log("ImpulseDrive WeaponSystems: Draw_TargetList 7 ");
                }
            }
            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo TorpedoUpdate end ");
        }
        public void SpawnPhotonTorpedo(Vector3 location, Target t)
        {
            try
            {
                TorpedoSound = new FXGroup("TorpedoSound");
                GameObject audioObj = new GameObject();
                audioObj.transform.position     = FlightGlobals.ActiveVessel.transform.position;
                audioObj.transform.parent       = FlightGlobals.ActiveVessel.transform; // add to parent
                TorpedoSound.audio              = audioObj.AddComponent <AudioSource>();
                TorpedoSound.audio.dopplerLevel = 0f;
                TorpedoSound.audio.Stop();
                TorpedoSound.audio.clip = GameDatabase.Instance.GetAudioClip(TorpedoSoundFile);
                TorpedoSound.audio.loop = false;
                TorpedoSound.audio.Play();
                TorpedoSound.audio.enabled = false;
                if (TorpedoSound != null && TorpedoSound.audio != null)
                {
                    float soundVolume = GameSettings.SHIP_VOLUME * TorpedoSoundVolume;
                    TorpedoSound.audio.enabled = true;
                    TorpedoSound.audio.volume  = soundVolume;
                }
            }
            catch (Exception ex)
            {
                Debug.LogError("ExplosionSound Error Explode: " + ex.Message);
            }



            float TorpedoPower = t.TorpedoYield;

            TorpedoClone = null;

            Collider[] myColliders = FlightGlobals.ActiveVessel.GetComponentsInChildren <Collider>();
            Bounds     myBounds    = new Bounds(FlightGlobals.ActiveVessel.transform.position, Vector3.zero);

            foreach (Collider nextCollider in myColliders)
            {
                myBounds.Encapsulate(nextCollider.bounds);
            }

            _LCARS_PhotonTorpedo bar = new _LCARS_PhotonTorpedo();

            TorpedoClone = bar.get_PhotonTorpedo(myBounds, FlightGlobals.ActiveVessel, FlightGlobals.ActiveVessel.GetWorldPos3D());
            Vector3 TargetVector = TorpedoClone.transform.position - t.ship.transform.position;

            TorpedoClone.transform.rotation = Quaternion.LookRotation(TargetVector.normalized);
            LCARS_PhotonTorpedoType PTT = new LCARS_PhotonTorpedoType();

            PTT.ID           = TorpedoCounter++;
            PTT.Target       = t;
            PTT.TorpedoClone = TorpedoClone;
            PTT.TorpedoYield = PTT.Target.TorpedoYield;
            PTT.prevPos      = TorpedoClone.transform.position;
            PTT.savedVel     = new Vector3(0, 0, 0);

            TorpedoList.Add(TorpedoClone.name, PTT);


            float power = PTT.Target.TorpedoYield * PT1.L2_usage;

            this.PowSys.draw(PT1.takerName, power);
        }
        public void DestroyPhotonTorpedo(string ID)
        {
            if (expl != null)
            {
                expl.stopSound();
            }

            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo DestroyPhotonTorpedo begin ID=" + ID);

            //Transform expl = Instantiate(explosion, transform.position, Quaternion.identity) as Transform;
            //Instantiate(explosionPrefab, pos, rot);

            LCARS_PhotonTorpedoType PTT = this.TorpedoList[ID];

            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo DestroyPhotonTorpedo 1 ");

            expl = PTT.TorpedoClone.AddComponent <LCARS_Explosion>() as LCARS_Explosion;
            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo DestroyPhotonTorpedo 2 ");

            expl.Initialize(PTT.TorpedoClone.transform, 10 * PTT.TorpedoYield);
            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo DestroyPhotonTorpedo 3 PTT.TorpedoYield=" + PTT.TorpedoYield);

            expl.Explode(PTT.TorpedoClone.transform.position);
            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo DestroyPhotonTorpedo 4 ");

            vesselList     = FlightGlobals.Vessels;
            visibleVessels = new List <string>()
            {
            };
            foreach (Vessel v in vesselList)
            {
                float check_distance = Vector3.Distance(PTT.TorpedoClone.transform.position, v.findWorldCenterOfMass());
                if (check_distance < (PTT.TorpedoYield * 2))
                {
                    v.GoOffRails();
                    float mass = v.GetTotalMass();
                    float temperatur_damage = PTT.TorpedoYield * 2000 / mass / check_distance;
                    //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo DestroyPhotonTorpedo temperatur_damage=" + temperatur_damage + " PTT.TorpedoYield=" + PTT.TorpedoYield + " mass=" + mass);
                    foreach (var vesselPart in v.parts.Where(p => p.rigidbody != null))
                    {
                        //vesselPart.force_activate();
                        vesselPart.rigidbody.WakeUp();

                        vesselPart.rigidbody.AddExplosionForce(PTT.TorpedoYield * 40000f, PTT.TorpedoClone.transform.position, PTT.TorpedoYield, 0.01F);
                        if (check_distance < PTT.TorpedoYield)
                        {
                            vesselPart.temperature += temperatur_damage;
                        }
                    }
                }
            }



            Destroy(PTT.TorpedoClone, 0.1f);
            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo DestroyPhotonTorpedo 5 ");

            Destroy(expl, 1f);
            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo DestroyPhotonTorpedo 6 ");


            TorpedoList.Remove(ID);
            //UnityEngine.Debug.Log("ImpulseDrive: PhotonTorpedo DestroyPhotonTorpedo end ");
        }