public json getCelestialState(CelestialBody celestial)
        {
            Debug.Log ("Collecting: " + celestial.GetName ());
            json buffer = new json ();
            buffer.Add ("type", "celestial");

            buffer.Add ("name", celestial.GetName ());
            buffer.Add ("ref", celestial.referenceBody.GetName ());
            if (celestial.orbitDriver != null) {
                Orbit orbit = celestial.GetOrbit ();

                Vector3d r = orbit.getRelativePositionAtUT (0);
                Vector3d v = orbit.getOrbitalVelocityAtUT (0);

                List<double> RV = new List<double> ();

                // Swap coordinate system
                RV.Add (r.y);
                RV.Add (r.x);
                RV.Add (r.z);

                RV.Add (v.y);
                RV.Add (v.x);
                RV.Add (v.z);

                buffer.Add ("rv", RV);

            } else {
                List<double> RV = new List<double> ();
                RV.Add (0.0);
                RV.Add (0.0);
                RV.Add (0.0);
                RV.Add (0.0);
                RV.Add (0.0);
                RV.Add (0.0);
                buffer.Add ("rv", RV);
            }

            buffer.Add ("mu", celestial.gravParameter);
            buffer.Add ("radius", celestial.Radius);
            buffer.Add ("soi", celestial.sphereOfInfluence);

            if (celestial.atmosphere == true) {
                buffer.Add ("alt_atm", celestial.maxAtmosphereAltitude);
            } else {
                buffer.Add ("alt_atm", 0);
            }

            // Angular velocity data
            buffer.Add ("ang_v", celestial.zUpAngularVelocity.magnitude);

            buffer.Add ("initial_rotation", celestial.initialRotation);
            buffer.Add ("rotation_angle", celestial.rotationAngle);
            buffer.Add ("rotation_t0", Planetarium.GetUniversalTime ());

            return buffer;
        }
 public json GameState()
 {
     json buffer = new json ();
     buffer.Add ("ut", Planetarium.GetUniversalTime());
     //buffer.Add ("rotating", Planetarium.FrameIsRotating());
     buffer.Add ("frame_angle", Planetarium.InverseRotAngle);
     buffer.Add ("active_vessel", FlightGlobals.ActiveVessel.id.ToString());
     buffer.Add ("throttle", FlightInputHandler.state.mainThrottle);
     return buffer;
 }
        public void UpdateClients()
        {
            json state = GameState ();
            json buffer = new json ();
            buffer.Add ("state", state);

            List<json> vessel_buffer = new List<json> ();
            vessel_buffer.Add (utilities.getVesselState (FlightGlobals.ActiveVessel));
            buffer.Add ("vessels", vessel_buffer);

            string msg = buffer.dumps ();

            foreach (MissionControlService mcs in subscribed_clients) {
                mcs.send (msg);
            }

            /*
            if (RemoteTech.RTGlobals.coreList.ActiveCore != null) {
                Debug.Log ("Remote connection:" + RemoteTech.RTGlobals.coreList.ActiveCore.InContact.ToString ());
                if (RemoteTech.RTGlobals.coreList.ActiveCore.InContact == true && FlightGlobals.ActiveVessel != null) {
                    if (RMConn == false) {
                        RMConn = true;
                        server.SendAll ("RMCONN TRUE");
                    }
                    Vessel ActiveVessel = FlightGlobals.ActiveVessel;
                    //string pid = ActiveVessel.id.ToString ();
                    server.SendAll (utils.getStateLine (ActiveVessel));
                }
                else {
                    if (RMConn == true) {
                        RMConn = false;
                        server.SendAll ("RMCONN FALSE");
                    }
                }
            }
            */

            // Check for new vessels..
            /*
            foreach (Vessel vessel in FlightGlobals.Vessels) {
                if (!all_vessels.Contains (vessel)) {
                    all_vessels.Add (vessel);
                    server.SendAll (utils.getStateLine (vessel));
                }
            }

            // Check for changed active vessel
            if (active_vessel != FlightGlobals.ActiveVessel) {
                active_vessel = FlightGlobals.ActiveVessel;
                server.SendAll("AV\t" + active_vessel.id.ToString ());
            }
            */
            /*
            Vessel ActiveVessel = FlightGlobals.ActiveVessel;
            double UT = Planetarium.GetUniversalTime ();
            bool frame_rotating = Planetarium.FrameIsRotating ();
            double frame_angle = Planetarium.InverseRotAngle;
            string rotating;
            if (frame_rotating) {
                rotating = "1";
            }
            else {
                rotating = "0";
            }
            json buffer = new json ();
            buffer.Add ("type", "update");
            buffer.Add ("ut", UT);
            buffer.Add ("rotating", rotating);
            buffer.Add ("frame_angle", frame_angle);
            buffer.Add ("active_vessel", ActiveVessel.id.ToString());
            */

            //Debug.Log ("Forward: " + active_vessel.GetTransform ().eulerAngles.ToString ());
            //Debug.Log ("Forward: " + active_vessel.GetTransform ().eulerAngles.ToString ());
            //Debug.Log ("Surface rotation: " + active_vessel.srfRelRotation.ToString ());
            //Debug.Log ("Surface eotation: " + active_vessel.srfRelRotation.eulerAngles);
            //Transform test = active_vessel.GetTransform ();
            //Debug.Log ("Rotation1: " + active_vessel.GetTransform().eulerAngles);
            //Debug.Log ("Rotation2: " + active_vessel.GetTransform().rotation.eulerAngles);
            //Debug.Log ("Rotation3: " + active_vessel.srfRelRotation.eulerAngles);

            // REF: FAR
            /*
            Vector3 tmpVec = active_vessel.transform.up * Vector3.Dot(active_vessel.transform.up, active_vessel.srf_velocity.normalized) + active_vessel.transform.forward * Vector3.Dot(active_vessel.transform.forward, active_vessel.srf_velocity.normalized);   //velocity vector projected onto a plane that divides the airplane into left and right halves
            float AoA = Vector3.Dot(tmpVec.normalized, active_vessel.transform.forward);
            AoA = Mathf.Rad2Deg * Mathf.Asin(AoA);
            if (float.IsNaN(AoA))
                AoA = 0;
            */
            /*
            tmpVec = active_vessel.transform.up * Vector3.Dot(active_vessel.transform.up, active_vessel.srf_velocity.normalized) + active_vessel.transform.right * Vector3.Dot(active_vessel.transform.right, active_vessel.srf_velocity.normalized);     //velocity vector projected onto the vehicle-horizontal plane
            float yaw = Vector3.Dot(tmpVec.normalized, active_vessel.transform.right);
            yaw = Mathf.Rad2Deg * Mathf.Asin(yaw);
            if (float.IsNaN(yaw))
                yaw = 0;
            */
            /*
            float pitch = 0;
            float yaw = 0;
            float roll = 0;

            NavBall ball = FlightUIController.fetch.GetComponentInChildren<NavBall>();
            if (ball)
            {
                Quaternion vesselRot = Quaternion.Inverse(ball.relativeGymbal);

                float heading = vesselRot.eulerAngles.y;
                //vesselRot *= Quaternion.Euler(0, -heading, 0);
                //heading = 360 - heading;
                pitch = (vesselRot.eulerAngles.x > 180) ? (360 - vesselRot.eulerAngles.x) : -vesselRot.eulerAngles.x;
                yaw = vesselRot.eulerAngles.y;
                roll = (vesselRot.eulerAngles.z > 180) ? (360 - vesselRot.eulerAngles.z) : -vesselRot.eulerAngles.z;
            }

            Debug.Log ("AoA: " + AoA);
            Debug.Log ("PCH: " + pitch);
            Debug.Log ("YAW: " + yaw);
            Debug.Log ("ROL: " + roll);

            //server.SendAll (buffer.dumps());
            //server.SendAll (utils.getStateLine (ActiveVessel));
            */
        }
        public void Synchronize(MissionControlService client)
        {
            Debug.Log ("STEP1");
            // Retrieve celestial information
            List<json> celestial_buffer = new List<json>();
            foreach (CelestialBody celestial in FlightGlobals.Bodies) {
                celestial_buffer.Add (utilities.getCelestialState (celestial));
            }
            Debug.Log ("STEP2");
            // Retrieve vessel information and add the vessel to known vessels
            List<json> vessel_buffer = new List<json>();
            foreach (Vessel vessel in FlightGlobals.Vessels) {
                vessel_buffer.Add (utilities.getVesselState (vessel));
                Debug.Log ("STEP2X");
                client.known_vessels.Add (vessel);
            }

            Debug.Log ("STEP3");
            // Compile it into a single json packet
            json buffer = new json();
            buffer.Add ("state", GameState ());
            buffer.Add ("celestials", celestial_buffer);
            buffer.Add ("vessels", vessel_buffer);
            Debug.Log ("STEP4");
            client.send (buffer.dumps ());
            Debug.Log ("STEP5");
            UpdateClients (); // TODO: not necessary the right way to do it.
        }
Beispiel #5
0
 public void Add(string key, json value)
 {
     keys.Add ('"' + key + '"');
     values.Add (value.dumps());
 }
        public json getVesselState(Vessel vessel)
        {
            Debug.Log ("STEP_XXX_1");
            Orbit orbit = vessel.GetOrbit ();
            //string referenceBody = FlightGlobals.Bodies.IndexOf (orbit.referenceBody).ToString ();
            string referenceBody = orbit.referenceBody.GetName ();

            //List<string> buffer = new List<string>();
            json buffer = new json ();
            buffer.Add ("type", "vessel");

            if (vessel.situation == Vessel.Situations.LANDED) {
                buffer.Add ("state", "landed");
            }
            else if (vessel.situation == Vessel.Situations.SPLASHED) {
                buffer.Add ("state", "splashed");
            }
            else if (vessel.situation == Vessel.Situations.PRELAUNCH) {
                buffer.Add ("state", "prelaunch");
            }
            else if (vessel.situation == Vessel.Situations.FLYING) {
                buffer.Add ("state", "flying");
            }
            else if (vessel.situation == Vessel.Situations.ORBITING) {
                buffer.Add ("state", "orbiting");
            }
            else if (vessel.situation == Vessel.Situations.DOCKED) {
                buffer.Add ("state", "docked");
            }
            else if (vessel.situation == Vessel.Situations.SUB_ORBITAL) {
                buffer.Add ("state", "suborbital");
            }
            else if (vessel.situation == Vessel.Situations.ESCAPING) {
                buffer.Add ("state", "escaping");
            }

            else {
                Debug.Log ("Unknown vessel situation");
                buffer.Add ("state", "unknown");
            } // # 1
            Debug.Log ("STEP_XXX_2");
            buffer.Add ("uid", vessel.id.ToString ());
            buffer.Add ("name", vessel.vesselName); // # 3
            buffer.Add ("ut", Planetarium.GetUniversalTime ()); // # 4
            buffer.Add ("ref", referenceBody); // # 5

            buffer.Add ("lon", vessel.longitude); // # 6
            buffer.Add ("lat", vessel.latitude); // # 7

            buffer.Add ("apo_t", vessel.orbit.timeToAp);
            buffer.Add ("per_t", vessel.orbit.timeToPe);

            // Why was this zero?
            Vector3d r = orbit.getRelativePositionAtUT (Planetarium.GetUniversalTime ());
            Vector3d v = orbit.getOrbitalVelocityAtUT (Planetarium.GetUniversalTime ());

            //Vector3d r = orbit.pos.xzy;
            //Vector3d v = orbit.vel.xzy;
            List<double> RV = new List<double> ();
            Debug.Log ("STEP_XXX_3");
            // TODO explain the coordinate system further
            RV.Add(r.y);
            RV.Add(r.x);
               	RV.Add(r.z);

               	RV.Add(v.y);
               	RV.Add(v.x);
               	RV.Add(v.z);

            buffer.Add ("rv", RV);	// # 8

            Vector3d forward = vessel.ReferenceTransform.forward;
            Vector3d up = vessel.ReferenceTransform.up;
            List<double> fwup = new List<double> ();
            fwup.Add (forward.y);
            fwup.Add (forward.x);
            fwup.Add (forward.z);
            fwup.Add (up.y);
            fwup.Add (up.x);
            fwup.Add (up.z);
            Debug.Log ("STEP_XXX_4");
            buffer.Add ("fwup", fwup);
            //Debug.Log ("SPEED1: " + orbit.getOrbitalVelocityAtUT (Planetarium.GetUniversalTime ()).ToString ()); // Tis is currently used velocity
            //Debug.Log ("SPEED2: " + orbit.GetFrameVelAtUT (Planetarium.GetUniversalTime ()).ToString ());
            //Debug.Log ("SPEED3: " + orbit.GetRelativeVel ().ToString () );
            // GetRelativeVel seems to be OK to get the correct position

            //Debug.Log ("SPEED4: " + orbit.GetRotFrameVel ( ().ToString () );

            //Debug.Log ("POSIT1: " + orbit.getPositionAtUT(Planetarium.GetUniversalTime ()).ToString ());
            //Debug.Log ("POSIT2: " + orbit.getRelativePositionAtUT(Planetarium.GetUniversalTime ()).ToString ()); // Tis is currently used pos
            //Debug.Log ("POSIT3: " + orbit.getTruePositionAtUT (Planetarium.GetUniversalTime ()).ToString ());
            //Debug.Log ("POSIT4: " + orbit.pos.xzy.ToString ());

            //Debug.Log ("FRAMEROT: " + Planetarium.FrameIsRotating ().ToString ());
            //Debug.Log ("FRAMEROT: " + Planetarium.ZupRotation.ToString ());
            //Debug.Log ("FRAMEROT: " + Planetarium.Rotation.ToString ());

            //Debug.Log ("Framerot: " + Planetarium.InverseRotAngle.ToString ());

            //buffer.Add (orbit.epoch.ToString ());
            List<double> elements = new List<double> ();

            elements.Add (orbit.semiMajorAxis);
            elements.Add (orbit.eccentricity);
            elements.Add (orbit.inclination);
            elements.Add (orbit.LAN);
            elements.Add (orbit.argumentOfPeriapsis); // # 9

            //buffer.Add (orbit.meanAnomalyAtEpoch.ToString ());

            buffer.Add ("elements", elements);

            buffer.Add ("mt", vessel.missionTime); // # 10
            buffer.Add ("atm_density", vessel.atmDensity); // # 11
            buffer.Add ("geeforce", vessel.geeForce); // # 12
            buffer.Add ("obt_v", vessel.obt_velocity.magnitude); // # 13
            buffer.Add ("srf_v", vessel.srf_velocity.magnitude); // # 14
            buffer.Add ("vrt_v", vessel.verticalSpeed);   // # 15
            buffer.Add ("pressure_s", vessel.staticPressure); // # 16
            Debug.Log ("STEP_XXX_5");
            if (vessel.Parts.Count () > 0) {
                Part part = vessel.Parts [0];
                buffer.Add ("pressure_d", part.dynamicPressureAtm); // # 17
                buffer.Add ("temperature", part.temperature); // #  18
            } else {
                buffer.Add ("pressure_d", 0);
                buffer.Add ("temperature", 0);
            }

            buffer.Add ("alt", vessel.altitude); // # 19
            buffer.Add ("alt_srf", vessel.heightFromSurface); // # 20
            buffer.Add ("alt_ter", vessel.heightFromTerrain); // # 21

            buffer.Add ("alt_per", orbit.PeA);
            buffer.Add ("alt_apo", orbit.ApA);
            //vessel.GetTransform().rotation
            Vector3 roteul = vessel.GetTransform ().rotation.eulerAngles;
            Vector3 rotinv = Quaternion.Inverse(vessel.GetTransform ().rotation).eulerAngles;
            List<double> rot = new List<double> ();
            rot.Add (roteul.y);
            rot.Add (roteul.x);
            rot.Add (roteul.z);
            rot.Add (rotinv.y);
            rot.Add (rotinv.x);
            rot.Add (rotinv.z);
            buffer.Add ("rot", rot);
            //buffer.Add (vessel.acceleration.magnitude.ToString ());
            //buffer.Add (vessel.angularMomentum.magnitude.ToString ());
            //buffer.Add (vessel.angularVelocity.magnitude.ToString ());
            //buffer.Add (vessel.geeForce_immediate.ToString ());
            //buffer.Add (vessel.horizontalSrfSpeed.ToString ());
            //buffer.Add (vessel.pqsAltitude.ToString ());
            //buffer.Add (vessel.rb_velocity.magnitude.ToString ());
            //buffer.Add (vessel.specificAcceleration.ToString ());
            //buffer.Add (vessel.terrainAltitude.ToString ());

            Debug.Log ("STEP_XXX_6");
            return buffer;
        }