Esempio n. 1
0
        private void handleVesselUpdate(KLFVesselUpdate vessel_update)
        {
            //Debug.Log("*** Handling update!");

            //Build the key for the vessel
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(vessel_update.ownerName);
            sb.Append(vessel_update.id.ToString());

            String vessel_key = sb.ToString();

            KLFVessel vessel = null;

            //Try to find the key in the vessel dictionary
            VesselEntry entry;
            if (vessels.TryGetValue(vessel_key, out entry))
            {
                vessel = entry.vessel;

                if (vessel == null || vessel.gameObj == null || vessel.vesselName != vessel_update.vesselName)
                {
                    //Delete the vessel if it's null or needs to be renamed
                    vessels.Remove(vessel_key);

                    if (vessel != null && vessel.gameObj != null)
                        GameObject.Destroy(vessel.gameObj);

                    vessel = null;
                }
                else
                {
                    //Update the entry's timestamp
                    VesselEntry new_entry = new VesselEntry();
                    new_entry.vessel = entry.vessel;
                    new_entry.lastUpdateTime = UnityEngine.Time.fixedTime;

                    vessels[vessel_key] = new_entry;
                }
            }

            if (vessel == null) {
                //Add the vessel to the dictionary
                vessel = new KLFVessel(vessel_update.vesselName, vessel_update.ownerName, vessel_update.id);
                entry = new VesselEntry();
                entry.vessel = vessel;
                entry.lastUpdateTime = UnityEngine.Time.fixedTime;

                if (vessels.ContainsKey(vessel_key))
                    vessels[vessel_key] = entry;
                else
                    vessels.Add(vessel_key, entry);

                /*Queue this update for the next update call because updating a vessel on the same step as
                 * creating it usually causes problems for some reason */
                vesselUpdateQueue.Enqueue(vessel_update);
            }
            else
                applyVesselUpdate(vessel_update, vessel); //Apply the vessel update to the existing vessel

            //Debug.Log("*** Updated handled");
        }
Esempio n. 2
0
        private void writeVesselUpdateToFile(KSP.IO.FileStream out_stream, Vessel vessel)
        {
            if (!vessel || !vessel.mainBody)
                return;

            //Create a KLFVesselUpdate from the vessel data
            KLFVesselUpdate update = new KLFVesselUpdate();

            update.vesselName = vessel.vesselName;
            update.ownerName = playerName;
            update.id = vessel.id;

            Vector3 pos = vessel.mainBody.transform.InverseTransformPoint(vessel.GetWorldPos3D());
            Vector3 dir = vessel.mainBody.transform.InverseTransformDirection(vessel.transform.up);
            Vector3 vel = vessel.mainBody.transform.InverseTransformDirection(vessel.GetObtVelocity());

            for (int i = 0; i < 3; i++)
            {
                update.localPosition[i] = pos[i];
                update.localDirection[i] = dir[i];
                update.localVelocity[i] = vel[i];
            }

            update.situation = vessel.situation;

            if (vessel == FlightGlobals.ActiveVessel)
                update.state = Vessel.State.ACTIVE;
            else if (vessel.isCommandable)
                update.state = Vessel.State.INACTIVE;
            else
                update.state = Vessel.State.DEAD;

            update.timeScale = Planetarium.TimeScale;
            update.bodyName = vessel.mainBody.bodyName;

            //Serialize the update
            byte[] update_bytes = KSP.IO.IOUtils.SerializeToBinary(update);

            //Write the length of the serialized to the stream
            writeIntToStream(out_stream, update_bytes.Length);

            //Write the serialized update to the stream
            out_stream.Write(update_bytes, 0, update_bytes.Length);
        }
Esempio n. 3
0
        private void applyVesselUpdate(KLFVesselUpdate vessel_update, KLFVessel vessel)
        {
            //Debug.Log("*** Handling vessel update!");

            //Find the CelestialBody that matches the one in the update
            CelestialBody update_body = null;

            if (vessel.mainBody != null && vessel.mainBody.bodyName == vessel_update.bodyName)
                update_body = vessel.mainBody; //Vessel already has the correct body
            else
            {

                //Find the celestial body in the list of bodies
                foreach (CelestialBody body in FlightGlobals.Bodies)
                {
                    if (body.bodyName == vessel_update.bodyName)
                    {
                        update_body = body;
                        break;
                    }
                }

            }

            if (update_body != null)
            {

                //Convert float arrays to Vector3s
                Vector3 pos = new Vector3(vessel_update.localPosition[0], vessel_update.localPosition[1], vessel_update.localPosition[2]);
                Vector3 dir = new Vector3(vessel_update.localDirection[0], vessel_update.localDirection[1], vessel_update.localDirection[2]);
                Vector3 vel = new Vector3(vessel_update.localVelocity[0], vessel_update.localVelocity[1], vessel_update.localVelocity[2]);

                vessel.vesselName = vessel_update.vesselName;

                vessel.setOrbitalData(update_body, pos, vel, dir);

                vessel.situation = vessel_update.situation;
                vessel.state = vessel_update.state;
                vessel.timeScale = vessel_update.timeScale;

            }
        }