예제 #1
0
        private IEnumerator PlaceSpawnedVessel(Vessel v, bool moveVessel)
        {
            loadingCraft   = true;
            v.isPersistent = true;
            v.Landed       = false;
            v.situation    = Vessel.Situations.FLYING;
            while (v.packed)
            {
                yield return(null);
            }
            v.SetWorldVelocity(Vector3d.zero);

            //      yield return null;
            //      FlightGlobals.ForceSetActiveVessel(v);
            yield return(null);

            v.Landed    = true;
            v.situation = Vessel.Situations.LANDED;
            v.GoOffRails();
            v.IgnoreGForces(240);

            //Staging.beginFlight();
            StageManager.BeginFlight();
            loadingCraft = false;
            if (count <= 4)
            {
                CheckSpawnTimer(); /// loop back to beginning
            }
            else
            {
                count = 0;
            }
        }
예제 #2
0
    private IEnumerator PlaceSpawnedVessel(Vessel v, bool moveVessel)
    {
      loadingCraft = true;
      v.isPersistent = true;
      v.Landed = false;
      v.situation = Vessel.Situations.FLYING;
      while (v.packed)
      {
        yield return null;
      }
      v.SetWorldVelocity(Vector3d.zero);

      yield return null;
      FlightGlobals.ForceSetActiveVessel(v);
      yield return null;
      v.Landed = true;
      v.situation = Vessel.Situations.PRELAUNCH;
      v.GoOffRails();
      v.IgnoreGForces(240);

      //Staging.beginFlight();
      StageManager.BeginFlight();

      if (moveVessel)
      {
        VesselMove.Instance.StartMove(v, false);
        VesselMove.Instance.MoveHeight = 35;
        yield return null;
        if (VesselMove.Instance.MovingVessel == v)
        {
          v.Landed = false;
        }
      }
      loadingCraft = false;
    }
예제 #3
0
        private IEnumerator PlaceSpawnedVessel(Vessel v, bool moveVessel)
        {
            loadingCraft   = true;
            v.isPersistent = true;
            v.Landed       = false;
            v.situation    = Vessel.Situations.FLYING;
            while (v.packed)
            {
                yield return(null);
            }
            v.SetWorldVelocity(Vector3d.zero);

            yield return(null);

            //FlightGlobals.ForceSetActiveVessel(v);
            spawnedVessel = v;
            yield return(null);

            v.Landed    = true;
            v.situation = Vessel.Situations.PRELAUNCH;
            v.GoOffRails();
            v.IgnoreGForces(240);


            //Staging.beginFlight();
            StageManager.BeginFlight();

            KeoCacheDriver.activeKeoCacheData.CacheVessel = spawnedVessel;
            loadingCraft = false;
            Log.Info("PlaceSpawnedVessel: Altitude/depth: " + TerrainHeightAt(v.latitude, v.longitude, v.mainBody));
        }
예제 #4
0
        private IEnumerator PlaceSpawnedVessel(Vessel v, bool moveVessel)
        {
            loadingCraft   = true;
            v.isPersistent = true;
            v.Landed       = false;
            v.situation    = Vessel.Situations.FLYING;
            while (v.packed)
            {
                yield return(null);
            }
            v.SetWorldVelocity(Vector3d.zero);

            //      yield return null;
            //      FlightGlobals.ForceSetActiveVessel(v);
            yield return(null);

            v.Landed    = true;
            v.situation = Vessel.Situations.LANDED;
            v.GoOffRails();
            v.IgnoreGForces(240);
            var hc = v.FindPartModuleImplementing <ModuleOrXHoloCache>();

            if (hc != null)
            {
                hc.powerDown = false;

                if (!hc.getGPS)
                {
                    hc.spawned = true;
                }

                if (hc.sth)
                {
                    hc.spawned = true;
                    hc.spawn   = true;
                }
            }
            else
            {
                v.rootPart.AddModule("ModuleOrXEnemySetup", true);
            }
            StageManager.BeginFlight();
            loadingCraft = false;
            holo         = false;
            OrXHoloCache.instance.spawnHoloCache = false;
        }
예제 #5
0
 public IEnumerator <YieldInstruction> SpawnShipConstructToGround(ShipConstruct construct,
                                                                  Transform spawn_transform,
                                                                  Vector3 spawn_offset,
                                                                  Callback <Vessel> on_vessel_positioned = null,
                                                                  Callback <Vessel> on_vessel_loaded     = null,
                                                                  Callback <Vessel> on_vessel_off_rails  = null,
                                                                  Callback <Vessel> on_vessel_launched   = null,
                                                                  int easing_frames = 0)
 {
     begin_launch(spawn_transform);
     PutShipToGround(construct, spawn_transform, spawn_offset);
     ShipConstruction.AssembleForLaunch(construct,
                                        vessel.landedAt, vessel.displaylandedAt, part.flagURL,
                                        FlightDriver.FlightStateCache,
                                        new VesselCrewManifest());
     launched_vessel = FlightGlobals.Vessels[FlightGlobals.Vessels.Count - 1];
     on_vessel_positioned?.Invoke(launched_vessel);
     while (!launched_vessel.loaded)
     {
         FlightCameraOverride.UpdateDurationSeconds(1);
         yield return(new WaitForFixedUpdate());
     }
     on_vessel_loaded?.Invoke(launched_vessel);
     while (launched_vessel.packed)
     {
         launched_vessel.precalc.isEasingGravity = true;
         launched_vessel.situation = Vessel.Situations.PRELAUNCH;
         stabilize_launched_vessel(0);
         FlightCameraOverride.UpdateDurationSeconds(1);
         yield return(new WaitForFixedUpdate());
     }
     on_vessel_off_rails?.Invoke(launched_vessel);
     if (easing_frames > 0)
     {
         foreach (var n in stabilize_launched_vessel(easing_frames))
         {
             FlightCameraOverride.UpdateDurationSeconds(1);
             yield return(new WaitForFixedUpdate());
         }
     }
     on_vessel_launched?.Invoke(launched_vessel);
     StageManager.BeginFlight();
     end_launch();
 }
예제 #6
0
        private IEnumerator PlaceSpawnedVessel(Vessel v, bool moveVessel)
        {
            loadingCraft   = true;
            v.isPersistent = true;
            v.Landed       = false;
            v.situation    = Vessel.Situations.FLYING;
            while (v.packed)
            {
                yield return(null);
            }
            v.SetWorldVelocity(Vector3d.zero);

            yield return(null);

            v.GoOffRails();

            StageManager.BeginFlight();

            loadingCraft = false;
        }
예제 #7
0
        public void LaunchVessel(Action spawnHandler)
        {
            spawnHandler.Invoke();
            var spawnedVessel = FlightGlobals.Vessels.Last();

            foreach (var part in spawnedVessel.parts)
            {
                foreach (var resource in part.Resources)
                {
                    if (resource.resourceName != "ElectricCharge")
                    {
                        resource.amount = 0d;
                    }
                }
            }
            FlightDriver.CanRevertToPostInit  = false;
            FlightDriver.CanRevertToPrelaunch = false;
            FlightGlobals.SetActiveVessel(spawnedVessel);
            spawnedVessel.currentStage = -1;
            StageManager.BeginFlight();
        }
예제 #8
0
        private IEnumerator PlaceSpawnedVessel(Vessel v, bool moveVessel)
        {
            loadingCraft = true;

            v.isPersistent = true;
            v.Landed       = false;
            v.situation    = Vessel.Situations.FLYING;
            while (v.packed)
            {
                yield return(null);
            }
            v.SetWorldVelocity(Vector3d.zero);

            //      yield return null;
            //      FlightGlobals.ForceSetActiveVessel(v);
            yield return(null);

            v.Landed    = true;
            v.situation = Vessel.Situations.LANDED;
            v.GoOffRails();
            v.IgnoreGForces(240);

            //Staging.beginFlight();
            StageManager.BeginFlight();

            /*
             *    if (moveVessel)
             *    {
             *      MoveOrXHole.Instance.StartMove(v, false);
             *      MoveOrXHole.Instance.MoveHeight = 2;
             *
             *      yield return null;
             *      if (MoveOrXHole.Instance.MovingVessel == v)
             *      {
             *        v.Landed = false;
             *      }
             *    }*/
            loadingCraft = false;
            spawning     = false;
        }
예제 #9
0
        public IEnumerator <YieldInstruction> SpawnShipConstruct(ShipConstruct construct,
                                                                 Transform spawn_transform,
                                                                 Vector3 spawn_offset,
                                                                 Vector3 dV,
                                                                 Callback <Vessel> on_vessel_positioned = null,
                                                                 Callback <Vessel> on_vessel_loaded     = null,
                                                                 Callback <Vessel> on_vessel_off_rails  = null,
                                                                 Callback <Vessel> on_vessel_launched   = null)
        {
            begin_launch(spawn_transform);
            float   angle;
            Vector3 axis;

            spawn_transform.rotation.ToAngleAxis(out angle, out axis);
            var root = construct.parts[0].localRoot.transform;

            root.Translate(spawn_transform.TransformPointUnscaled(spawn_offset), Space.World);
            root.RotateAround(spawn_transform.position, axis, angle);
            //initialize new vessel
            AssembleForLaunchUnlanded(construct, new Orbit(vessel.orbit),
                                      part.flagURL, FlightDriver.FlightStateCache);
            launched_vessel = FlightGlobals.Vessels[FlightGlobals.Vessels.Count - 1];
            on_vessel_positioned?.Invoke(launched_vessel);
            //launch the vessel
            foreach (var i in launch_moving_vessel(spawn_transform,
                                                   spawn_offset,
                                                   spawn_transform.rotation.Inverse() * root.rotation,
                                                   dV,
                                                   on_vessel_loaded,
                                                   on_vessel_off_rails,
                                                   on_vessel_launched))
            {
                yield return(i);
            }
            StageManager.BeginFlight();
            end_launch();
        }
예제 #10
0
        private void SpawnVessel(VesselData VesselData, List <ProtoCrewMember> crewData = null)
        {
            //      string gameDataDir = KSPUtil.ApplicationRootPath;

            // Set additional info for landed vessels
            bool landed = false;

            if (!landed)
            {
                landed = true;
                if (VesselData.altitude == null || VesselData.altitude < 0)
                {
                    VesselData.altitude = SpawnCoords.z;      //LocationUtil.TerrainHeight(VesselData.latitude, VesselData.longitude, VesselData.body);
                }

                //Vector3d pos = VesselData.body.GetWorldSurfacePosition(VesselData.latitude, VesselData.longitude, VesselData.altitude.Value);
                Vector3d pos = VesselData.body.GetRelSurfacePosition(VesselData.latitude, VesselData.longitude, VesselData.altitude.Value);

                VesselData.orbit = new Orbit(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, VesselData.body);
                VesselData.orbit.UpdateFromStateVectors(pos, VesselData.body.getRFrmVel(pos), VesselData.body, Planetarium.GetUniversalTime());
            }

            ConfigNode[]  partNodes;
            ShipConstruct shipConstruct = null;
            bool          hasClamp      = false;
            float         lcHeight      = 0;
            ConfigNode    craftNode;
            Quaternion    craftRotation = Quaternion.identity;

            if (!string.IsNullOrEmpty(VesselData.craftURL))
            {
                // Save the current ShipConstruction ship, otherwise the player will see the spawned ship next time they enter the VAB!
                ConfigNode currentShip = ShipConstruction.ShipConfig;

                shipConstruct = ShipConstruction.LoadShip(VesselData.craftURL);
                if (shipConstruct == null)
                {
                    return;//continue;
                }

                craftNode     = ConfigNode.Load(VesselData.craftURL);
                lcHeight      = ConfigNode.ParseVector3(craftNode.GetNode("PART").GetValue("pos")).y;
                craftRotation = ConfigNode.ParseQuaternion(craftNode.GetNode("PART").GetValue("rot"));

                // Restore ShipConstruction ship
                ShipConstruction.ShipConfig = currentShip;

                // Set the name
                if (string.IsNullOrEmpty(VesselData.name))
                {
                    VesselData.name = "";
                    ;
                }

                // Set some parameters that need to be at the part level
                uint missionID = (uint)Guid.NewGuid().GetHashCode();
                uint launchID  = HighLogic.CurrentGame.launchID++;
                foreach (Part p in shipConstruct.parts)
                {
                    p.flightID  = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState);
                    p.missionID = missionID;
                    p.launchID  = launchID;
                    p.flagURL   = flagURL;

                    // Had some issues with this being set to -1 for some ships - can't figure out
                    // why.  End result is the vessel exploding, so let's just set it to a positive
                    // value.
                    p.temperature = 1.0;
                }

                // Create a Blank ProtoVessel, we will use this to dump the parts to a config node.
                // We can't use the config nodes from the .craft file, because they are in a
                // slightly different format than those required for a ProtoVessel (seriously
                // Squad?!?).
                ConfigNode  empty       = new ConfigNode();
                ProtoVessel BlankProto  = new ProtoVessel(empty, null);
                Vessel      BlankVessel = new Vessel();
                BlankVessel.parts    = shipConstruct.parts;
                BlankProto.vesselRef = BlankVessel;

                // Create the ProtoPartSnapshot objects and then initialize them
                foreach (Part p in shipConstruct.parts)
                {
                    BlankProto.protoPartSnapshots.Add(new ProtoPartSnapshot(p, BlankProto));
                }
                foreach (ProtoPartSnapshot p in BlankProto.protoPartSnapshots)
                {
                    p.storePartRefs();
                }

                // Create the ship's parts

                List <ConfigNode> partNodesL = new List <ConfigNode>();
                foreach (ProtoPartSnapshot snapShot in BlankProto.protoPartSnapshots)
                {
                    ConfigNode node = new ConfigNode("PART");
                    snapShot.Save(node);
                    partNodesL.Add(node);
                }
                partNodes = partNodesL.ToArray();
            }
            else
            {
                // Create crew member array
                ProtoCrewMember[] crewArray = new ProtoCrewMember[VesselData.crew.Count];

                /*
                 *      int i = 0;
                 *      foreach (CrewData cd in VesselData.crew)
                 *      {
                 * /*
                 *        // Create the ProtoCrewMember
                 *        ProtoCrewMember crewMember = HighLogic.CurrentGame.CrewRoster.GetNewKerbal(ProtoCrewMember.KerbalType.Crew);
                 *        if (cd.name != null)
                 *        {
                 *          crewMember.KerbalRef.name = cd.name;
                 *        }
                 *
                 *        crewArray[i++] = crewMember;
                 *
                 *      }
                 */
                // Create part nodes
                uint flightId = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState);
                partNodes    = new ConfigNode[1];
                partNodes[0] = ProtoVessel.CreatePartNode(VesselData.craftPart.name, flightId, crewArray);

                // Default the size class
                //sizeClass = UntrackedObjectClass.A;

                // Set the name
                if (string.IsNullOrEmpty(VesselData.name))
                {
                    VesselData.name = VesselData.craftPart.name;
                }
            }

            // Create additional nodes
            ConfigNode[] additionalNodes = new ConfigNode[0];
            //DiscoveryLevels discoveryLevel = VesselData.owned ? DiscoveryLevels.Owned : DiscoveryLevels.Unowned;
            //additionalNodes[0] = ProtoVessel.CreateDiscoveryNode(discoveryLevel, sizeClass, contract.TimeDeadline, contract.TimeDeadline);

            // Create the config node representation of the ProtoVessel
            ConfigNode protoVesselNode = ProtoVessel.CreateVesselNode(VesselData.name, VesselData.vesselType, VesselData.orbit, 0, partNodes, additionalNodes);

            // Additional seetings for a landed vessel
            if (!VesselData.orbiting)
            {
                Vector3d norm = VesselData.body.GetRelSurfaceNVector(VesselData.latitude, VesselData.longitude);

                double terrainHeight = 0.0;
                if (VesselData.body.pqsController != null)
                {
                    terrainHeight = VesselData.body.pqsController.GetSurfaceHeight(norm) - VesselData.body.pqsController.radius;
                }
                bool splashed = false;// = landed && terrainHeight < 0.001;

                // Create the config node representation of the ProtoVessel
                // Note - flying is experimental, and so far doesn't worx
                protoVesselNode.SetValue("sit", (splashed ? Vessel.Situations.SPLASHED : landed ?
                                                 Vessel.Situations.LANDED : Vessel.Situations.FLYING).ToString());
                protoVesselNode.SetValue("landed", (landed && !splashed).ToString());
                protoVesselNode.SetValue("splashed", splashed.ToString());
                protoVesselNode.SetValue("lat", VesselData.latitude.ToString());
                protoVesselNode.SetValue("lon", VesselData.longitude.ToString());
                protoVesselNode.SetValue("alt", VesselData.altitude.ToString());
                protoVesselNode.SetValue("landedAt", VesselData.body.name);

                // Figure out the additional height to subtract
                float lowest = float.MaxValue;
                if (shipConstruct != null)
                {
                    foreach (Part p in shipConstruct.parts)
                    {
                        foreach (Collider collider in p.GetComponentsInChildren <Collider>())
                        {
                            if (collider.gameObject.layer != 21 && collider.enabled)
                            {
                                lowest = Mathf.Min(lowest, collider.bounds.min.y);
                            }
                        }
                    }
                }
                else
                {
                    foreach (Collider collider in VesselData.craftPart.partPrefab.GetComponentsInChildren <Collider>())
                    {
                        if (collider.gameObject.layer != 21 && collider.enabled)
                        {
                            lowest = Mathf.Min(lowest, collider.bounds.min.y);
                        }
                    }
                }

                if (lowest == float.MaxValue)
                {
                    lowest = 0;
                }

                // Figure out the surface height and rotation
                Quaternion normal   = Quaternion.LookRotation((Vector3)norm);// new Vector3((float)norm.x, (float)norm.y, (float)norm.z));
                Quaternion rotation = Quaternion.identity;
                float      heading  = VesselData.heading;
                rotation = rotation * Quaternion.FromToRotation(Vector3.up, Vector3.back);
                rotation = rotation * Quaternion.AngleAxis(heading, Vector3.back);
                rotation = rotation * Quaternion.AngleAxis(VesselData.roll, Vector3.down);
                rotation = rotation * Quaternion.AngleAxis(VesselData.pitch, Vector3.left);

                // Set the height and rotation
                if (landed || splashed)
                {
                    float hgt = (shipConstruct != null ? shipConstruct.parts[0] : VesselData.craftPart.partPrefab).localRoot.attPos0.y - lowest;
                    hgt += VesselData.height;
                    protoVesselNode.SetValue("hgt", hgt.ToString(), true);
                }
                protoVesselNode.SetValue("rot", KSPUtil.WriteQuaternion(normal * rotation), true);

                // Set the normal vector relative to the surface
                Vector3 nrm = (rotation * Vector3.forward);
                protoVesselNode.SetValue("nrm", nrm.x + "," + nrm.y + "," + nrm.z, true);

                protoVesselNode.SetValue("prst", false.ToString(), true);
            }

            // Add vessel to the game
            ProtoVessel protoVessel = HighLogic.CurrentGame.AddVessel(protoVesselNode);

            //protoVessel.vesselRef.transform.rotation = protoVessel.rotation;


            // Store the id for later use
            VesselData.id = protoVessel.vesselRef.id;
            protoVessel.vesselRef.isPersistent = true;
            protoVessel.vesselRef.Landed       = false;
            protoVessel.vesselRef.situation    = Vessel.Situations.FLYING;
            protoVessel.vesselRef.GoOffRails();
            StageManager.BeginFlight();

            //StartCoroutine(PlaceSpawnedVessel(protoVessel.vesselRef, !hasClamp));

            //destroy prefabs
            foreach (Part p in FindObjectsOfType <Part>())
            {
                if (!p.vessel)
                {
                    Destroy(p.gameObject);
                }
            }
        }
예제 #11
0
        public static Part CreatePart(AvailablePart avPart, Vector3 position, Quaternion rotation,
                                      Part flagFromPart)
        {
            UnityEngine.Object obj = UnityEngine.Object.Instantiate(avPart.partPrefab);
            if (!obj)
            {
                KAS_Shared.DebugError("CreatePart(Crate) Failed to instantiate " + avPart.partPrefab.name);
                return(null);
            }

            Part newPart = (Part)obj;

            newPart.gameObject.SetActive(true);
            newPart.gameObject.name = avPart.name;
            newPart.partInfo        = avPart;
            //newPart.highlightRecurse = true;
            newPart.SetMirror(Vector3.one);

            ShipConstruct newShip = new ShipConstruct();

            newShip.Add(newPart);
            newShip.SaveShip();
            newShip.shipName = avPart.title;
            //newShip.ty = 1;

            VesselCrewManifest vessCrewManifest = new VesselCrewManifest();
            Vessel             currentVessel    = FlightGlobals.ActiveVessel;

            Vessel v = newShip.parts[0].localRoot.gameObject.AddComponent <Vessel>();

            v.id         = Guid.NewGuid();
            v.vesselName = newShip.shipName;
            v.Initialize(false);
            v.Landed             = true;
            v.rootPart.flightID  = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState);
            v.rootPart.missionID = flagFromPart.missionID;
            v.rootPart.flagURL   = flagFromPart.flagURL;

            //v.rootPart.collider.isTrigger = true;

            //v.landedAt = "somewhere";

            StageManager.BeginFlight();
            newShip.parts[0].vessel.ResumeStaging();
            StageManager.GenerateStagingSequence(newShip.parts[0].localRoot);
            StageManager.RecalculateVesselStaging(newShip.parts[0].vessel);

            FlightGlobals.SetActiveVessel(currentVessel);

            v.SetPosition(position);
            v.SetRotation(rotation);

            // Solar panels from containers don't work otherwise
            for (int i = 0; i < newPart.Modules.Count; i++)
            {
                ConfigNode node = new ConfigNode();
                node.AddValue("name", newPart.Modules[i].moduleName);
                newPart.LoadModule(node, ref i);
            }

            return(newPart);
        }
예제 #12
0
        private IEnumerator PlaceSpawnedVessel(Vessel v, bool moveVessel)
        {
            Debug.Log("[SPAWN OrX] PLACING SPAWNED OrX ...................");
            var wm = v.FindPartModuleImplementing <MissileFire>();

            loadingCraft   = true;
            v.isPersistent = true;
            v.Landed       = false;
            v.situation    = Vessel.Situations.FLYING;
            while (v.packed)
            {
                yield return(null);
            }
            v.SetWorldVelocity(Vector3d.zero);

            yield return(null);

            Debug.Log("[SPAWN OrX] GOING OFF RAILS ...................");

            v.GoOffRails();
            StageManager.BeginFlight();
            float speed = 10;

            foreach (Part p in v.parts)
            {
                var OrXmodule = p.FindModuleImplementing <ModuleOrXBDAc>();

                if (OrXmodule != null)
                {
                    OrXmodule.player = false;

                    if (waldo)
                    {
                        OrXmodule.team      = true;
                        OrXmodule.waldo     = true;
                        OrXmodule.brute     = false;
                        OrXmodule.stayPunkd = false;
                        OrXmodule.orx       = false;
                        waldo = false;
                    }

                    if (brute)
                    {
                        OrXmodule.team      = true;
                        OrXmodule.brute     = true;
                        OrXmodule.waldo     = false;
                        OrXmodule.stayPunkd = false;
                        OrXmodule.orx       = false;
                        brute = false;
                    }

                    if (stayPunkd)
                    {
                        OrXmodule.team      = true;
                        OrXmodule.stayPunkd = true;
                        OrXmodule.waldo     = false;
                        OrXmodule.brute     = false;
                        OrXmodule.orx       = false;
                        stayPunkd           = false;
                    }

                    if (orx)
                    {
                        OrXmodule.team      = true;
                        OrXmodule.orx       = true;
                        OrXmodule.waldo     = false;
                        OrXmodule.stayPunkd = false;
                        OrXmodule.brute     = false;
                        orx = false;
                        v.GetComponent <Rigidbody>().velocity = UnityEngine.Random.onUnitSphere * speed;
                    }
                }
            }

            loadingCraft = false;
        }