private void SpawnPrefabInternal( List <MyCubeGrid> resultList, String prefabName, Vector3D position, Vector3 forward, Vector3 up, Vector3 initialLinearVelocity, Vector3 initialAngularVelocity, String beaconName, SpawningOptions spawningOptions, long ownerId, bool updateSync, Stack <Action> callbacks) { Debug.Assert(Vector3.IsUnit(ref forward)); Debug.Assert(Vector3.IsUnit(ref up)); Debug.Assert(Vector3.ArePerpendicular(ref forward, ref up)); bool spawnAtOrigin = spawningOptions.HasFlag(SpawningOptions.UseGridOrigin); //CreateGridsFromPrefab(resultList, prefabName, MatrixD.CreateWorld(position, forward, up), spawnAtOrigin); CreateGridsData createGridsData = new CreateGridsData(resultList, prefabName, MatrixD.CreateWorld(position, forward, up), spawnAtOrigin, callbacks: callbacks); Interlocked.Increment(ref PendingGrids); callbacks.Push(delegate() { SpawnPrefabInternalSetProperties(resultList, position, forward, up, initialLinearVelocity, initialAngularVelocity, beaconName, spawningOptions, ownerId, updateSync); }); ParallelTasks.Parallel.Start(createGridsData.CallCreateGridsFromPrefab, createGridsData.OnGridsCreated, createGridsData); }
private void SpawnPrefabInternal( List <MyCubeGrid> resultList, String prefabName, Vector3D position, Vector3 forward, Vector3 up, Vector3 initialLinearVelocity, Vector3 initialAngularVelocity, String beaconName, SpawningOptions spawningOptions, bool updateSync) { Debug.Assert(Vector3.IsUnit(ref forward)); Debug.Assert(Vector3.IsUnit(ref up)); Debug.Assert(Vector3.ArePerpendicular(ref forward, ref up)); int rngSeed = 0; using (updateSync ? MyRandom.Instance.PushSeed(rngSeed = MyRandom.Instance.CreateRandomSeed()) : new MyRandom.StateToken()) { bool spawnAtOrigin = spawningOptions.HasFlag(SpawningOptions.UseGridOrigin); CreateGridsFromPrefab(resultList, prefabName, MatrixD.CreateWorld(position, forward, up), spawnAtOrigin); MyCockpit firstCockpit = null; bool rotateToCockpit = spawningOptions.HasFlag(SpawningOptions.RotateFirstCockpitTowardsDirection); bool spawnCargo = spawningOptions.HasFlag(SpawningOptions.SpawnRandomCargo); bool setNeutralOwner = spawningOptions.HasFlag(SpawningOptions.SetNeutralOwner); bool needsToIterateThroughBlocks = spawnCargo || rotateToCockpit || setNeutralOwner || beaconName != null; long owner = 0; if (updateSync && spawningOptions.HasFlag(SpawningOptions.SetNeutralOwner) && resultList.Count != 0) { string npcName = "NPC " + MyRandom.Instance.Next(1000, 9999); var identity = Sync.Players.CreateNewIdentity(npcName); owner = identity.IdentityId; } foreach (var grid in resultList) { grid.ClearSymmetries(); if (spawningOptions.HasFlag(SpawningOptions.DisableDampeners)) { grid.GridSystems.ThrustSystem.DampenersEnabled = false; } if ((spawningOptions.HasFlag(SpawningOptions.DisableSave))) { grid.Save = false; } if (needsToIterateThroughBlocks || spawningOptions.HasFlag(SpawningOptions.TurnOffReactors)) { ProfilerShort.Begin("Iterate through blocks"); foreach (var block in grid.GetBlocks()) { if (block.FatBlock is MyCockpit && rotateToCockpit && firstCockpit == null) { firstCockpit = (MyCockpit)block.FatBlock; } else if (block.FatBlock is MyCargoContainer && spawnCargo) { MyCargoContainer container = block.FatBlock as MyCargoContainer; container.SpawnRandomCargo(); } else if (block.FatBlock is MyBeacon && beaconName != null) { MyBeacon beacon = block.FatBlock as MyBeacon; beacon.SetCustomName(beaconName); } else if (spawningOptions.HasFlag(SpawningOptions.TurnOffReactors) && block.FatBlock is IMyPowerProducer) { (block.FatBlock as IMyPowerProducer).Enabled = false; } if (setNeutralOwner && block.FatBlock != null && block.BlockDefinition.RatioEnoughForOwnership(block.BuildLevelRatio)) { block.FatBlock.ChangeOwner(owner, MyOwnershipShareModeEnum.None); } } ProfilerShort.End(); } } Matrix transform = default(Matrix); if (rotateToCockpit) { System.Diagnostics.Debug.Assert(firstCockpit != null, "cockpit in prefab ship is missing !"); if (firstCockpit != null) { Matrix cockpitTransform = firstCockpit.WorldMatrix; Matrix cockpitInvertedTransform = Matrix.Invert(cockpitTransform); transform = Matrix.Multiply(cockpitInvertedTransform, Matrix.CreateWorld(firstCockpit.WorldMatrix.Translation, forward, up)); } } foreach (var grid in resultList) { if (firstCockpit != null) { grid.WorldMatrix = grid.WorldMatrix * transform; } if (grid.Physics != null) { grid.Physics.LinearVelocity = initialLinearVelocity; grid.Physics.AngularVelocity = initialAngularVelocity; } ProfilerShort.Begin("Add entity"); MyEntities.Add(grid); ProfilerShort.End(); } if (updateSync == true) { MySyncPrefabManager.SendPrefabSpawned(prefabName, new MyPositionAndOrientation(position, forward, up), initialLinearVelocity, initialAngularVelocity, beaconName, spawningOptions, rngSeed); } } }
private void SpawnPrefabInternal( List<MyCubeGrid> resultList, String prefabName, Vector3D position, Vector3 forward, Vector3 up, Vector3 initialLinearVelocity, Vector3 initialAngularVelocity, String beaconName, SpawningOptions spawningOptions, long ownerId, bool updateSync) { Debug.Assert(Vector3.IsUnit(ref forward)); Debug.Assert(Vector3.IsUnit(ref up)); Debug.Assert(Vector3.ArePerpendicular(ref forward, ref up)); int rngSeed = 0; using (updateSync ? MyRandom.Instance.PushSeed(rngSeed = MyRandom.Instance.CreateRandomSeed()) : new MyRandom.StateToken()) { bool spawnAtOrigin = spawningOptions.HasFlag(SpawningOptions.UseGridOrigin); CreateGridsFromPrefab(resultList, prefabName, MatrixD.CreateWorld(position, forward, up), spawnAtOrigin); MyCockpit firstCockpit = null; bool rotateToCockpit = spawningOptions.HasFlag(SpawningOptions.RotateFirstCockpitTowardsDirection); bool spawnCargo = spawningOptions.HasFlag(SpawningOptions.SpawnRandomCargo); bool setNeutralOwner = spawningOptions.HasFlag(SpawningOptions.SetNeutralOwner); bool needsToIterateThroughBlocks = spawnCargo || rotateToCockpit || setNeutralOwner || beaconName != null; long owner = ownerId; if (updateSync && spawningOptions.HasFlag(SpawningOptions.SetNeutralOwner) && resultList.Count != 0) { string npcName = "NPC " + MyRandom.Instance.Next(1000, 9999); var identity = Sync.Players.CreateNewIdentity(npcName); owner = identity.IdentityId; } bool setOwnership = owner != 0; List<MyCockpit> shipCockpits = new List<MyCockpit>(); foreach (var grid in resultList) { grid.ClearSymmetries(); if (spawningOptions.HasFlag(SpawningOptions.DisableDampeners)) { var thrustComp = grid.Components.Get<MyEntityThrustComponent>(); if(thrustComp != null) thrustComp.DampenersEnabled = false; } if ((spawningOptions.HasFlag(SpawningOptions.DisableSave))) { grid.Save = false; } if (needsToIterateThroughBlocks || spawningOptions.HasFlag(SpawningOptions.TurnOffReactors)) { ProfilerShort.Begin("Iterate through blocks"); foreach (var block in grid.GetBlocks()) { if (block.FatBlock is MyCockpit && block.FatBlock.IsFunctional) { shipCockpits.Add(block.FatBlock as MyCockpit); } else if (block.FatBlock is MyCargoContainer && spawnCargo) { MyCargoContainer container = block.FatBlock as MyCargoContainer; container.SpawnRandomCargo(); } else if (block.FatBlock is MyBeacon && beaconName != null) { MyBeacon beacon = block.FatBlock as MyBeacon; beacon.SetCustomName(beaconName); } else if (spawningOptions.HasFlag(SpawningOptions.TurnOffReactors) && block.FatBlock != null && block.FatBlock.Components.Contains(typeof(MyResourceSourceComponent))) { var sourceComp = block.FatBlock.Components.Get<MyResourceSourceComponent>(); if (sourceComp != null) { if(sourceComp.ResourceTypes.Contains(MyResourceDistributorComponent.ElectricityId)) sourceComp.Enabled = false; } } if (setOwnership && block.FatBlock != null && block.BlockDefinition.RatioEnoughForOwnership(block.BuildLevelRatio)) { block.FatBlock.ChangeOwner(owner, MyOwnershipShareModeEnum.None); } } ProfilerShort.End(); } } // First sort cockpits by order: Ship controlling cockpits set to main, then ship controlling cockpits not set to main, lastly whatever remains, e.g. CryoChambers and Passenger Seats if (shipCockpits.Count > 1) { shipCockpits.Sort(delegate(MyCockpit cockpitA, MyCockpit cockpitB) { int controlCompare = cockpitB.EnableShipControl.CompareTo(cockpitA.EnableShipControl); if (controlCompare != 0) return controlCompare; int mainCompare = cockpitB.IsMainCockpit.CompareTo(cockpitA.IsMainCockpit); if (mainCompare != 0) return mainCompare; return 0; }); } if (shipCockpits.Count > 0) firstCockpit = shipCockpits[0]; // Try to rotate to the first cockpit Matrix transform = Matrix.Identity; if (rotateToCockpit) { System.Diagnostics.Debug.Assert(firstCockpit != null,"cockpit in prefab ship is missing !"); if (firstCockpit != null) { Matrix cockpitTransform = firstCockpit.WorldMatrix; Matrix cockpitInvertedTransform = Matrix.Invert(cockpitTransform); transform = Matrix.Multiply(cockpitInvertedTransform, Matrix.CreateWorld(firstCockpit.WorldMatrix.Translation, forward, up)); } } foreach (var grid in resultList) { if (firstCockpit != null && rotateToCockpit) { grid.WorldMatrix = grid.WorldMatrix * transform; } if (grid.Physics != null) { grid.Physics.LinearVelocity = initialLinearVelocity; grid.Physics.AngularVelocity = initialAngularVelocity; } ProfilerShort.Begin("Add entity"); //MyEntities.Add(grid); ProfilerShort.End(); } } }
private void SpawnPrefabInternalSetProperties( List <MyCubeGrid> resultList, Vector3D position, Vector3 forward, Vector3 up, Vector3 initialLinearVelocity, Vector3 initialAngularVelocity, String beaconName, SpawningOptions spawningOptions, long ownerId, bool updateSync) { int rngSeed = 0; using (updateSync ? MyRandom.Instance.PushSeed(rngSeed = MyRandom.Instance.CreateRandomSeed()) : new MyRandom.StateToken()) { MyCockpit firstCockpit = null; bool rotateToCockpit = spawningOptions.HasFlag(SpawningOptions.RotateFirstCockpitTowardsDirection); bool spawnCargo = spawningOptions.HasFlag(SpawningOptions.SpawnRandomCargo); bool setNeutralOwner = spawningOptions.HasFlag(SpawningOptions.SetNeutralOwner); bool needsToIterateThroughBlocks = spawnCargo || rotateToCockpit || setNeutralOwner || beaconName != null; long owner = ownerId; if (updateSync && spawningOptions.HasFlag(SpawningOptions.SetNeutralOwner) && resultList.Count != 0) { string npcName = "NPC " + MyRandom.Instance.Next(1000, 9999); var identity = Sync.Players.CreateNewIdentity(npcName); owner = identity.IdentityId; } bool setOwnership = owner != 0; List <MyCockpit> shipCockpits = new List <MyCockpit>(); foreach (var grid in resultList) { grid.ClearSymmetries(); if (spawningOptions.HasFlag(SpawningOptions.DisableDampeners)) { var thrustComp = grid.Components.Get <MyEntityThrustComponent>(); if (thrustComp != null) { thrustComp.DampenersEnabled = false; } } if ((spawningOptions.HasFlag(SpawningOptions.DisableSave))) { grid.Save = false; } if (needsToIterateThroughBlocks || spawningOptions.HasFlag(SpawningOptions.TurnOffReactors)) { ProfilerShort.Begin("Iterate through blocks"); foreach (var block in grid.GetBlocks()) { if (block.FatBlock is MyCockpit && block.FatBlock.IsFunctional) { shipCockpits.Add(block.FatBlock as MyCockpit); } else if (block.FatBlock is MyCargoContainer && spawnCargo) { MyCargoContainer container = block.FatBlock as MyCargoContainer; container.SpawnRandomCargo(); } else if (block.FatBlock is MyBeacon && beaconName != null) { MyBeacon beacon = block.FatBlock as MyBeacon; beacon.SetCustomName(beaconName); } else if (spawningOptions.HasFlag(SpawningOptions.TurnOffReactors) && block.FatBlock != null && block.FatBlock.Components.Contains(typeof(MyResourceSourceComponent))) { var sourceComp = block.FatBlock.Components.Get <MyResourceSourceComponent>(); if (sourceComp != null) { if (sourceComp.ResourceTypes.Contains(MyResourceDistributorComponent.ElectricityId)) { sourceComp.Enabled = false; } } } if (setOwnership && block.FatBlock != null && block.BlockDefinition.RatioEnoughForOwnership(block.BuildLevelRatio)) { block.FatBlock.ChangeOwner(owner, MyOwnershipShareModeEnum.None); } } ProfilerShort.End(); } } // First sort cockpits by order: Ship controlling cockpits set to main, then ship controlling cockpits not set to main, lastly whatever remains, e.g. CryoChambers and Passenger Seats if (shipCockpits.Count > 1) { shipCockpits.Sort(delegate(MyCockpit cockpitA, MyCockpit cockpitB) { int controlCompare = cockpitB.EnableShipControl.CompareTo(cockpitA.EnableShipControl); if (controlCompare != 0) { return(controlCompare); } int mainCompare = cockpitB.IsMainCockpit.CompareTo(cockpitA.IsMainCockpit); if (mainCompare != 0) { return(mainCompare); } return(0); }); } if (shipCockpits.Count > 0) { firstCockpit = shipCockpits[0]; } // Try to rotate to the first cockpit Matrix transform = Matrix.Identity; if (rotateToCockpit) { System.Diagnostics.Debug.Assert(firstCockpit != null, "cockpit in prefab ship is missing !"); if (firstCockpit != null) { Matrix cockpitTransform = firstCockpit.WorldMatrix; Matrix cockpitInvertedTransform = Matrix.Invert(cockpitTransform); transform = Matrix.Multiply(cockpitInvertedTransform, Matrix.CreateWorld(firstCockpit.WorldMatrix.Translation, forward, up)); } } foreach (var grid in resultList) { if (firstCockpit != null && rotateToCockpit) { grid.WorldMatrix = grid.WorldMatrix * transform; } if (grid.Physics != null) { grid.Physics.LinearVelocity = initialLinearVelocity; grid.Physics.AngularVelocity = initialAngularVelocity; } ProfilerShort.Begin("Add entity"); //MyEntities.Add(grid); ProfilerShort.End(); } } }
private void SpawnPrefabInternal( List<MyCubeGrid> resultList, String prefabName, Vector3D position, Vector3 forward, Vector3 up, Vector3 initialLinearVelocity, Vector3 initialAngularVelocity, String beaconName, SpawningOptions spawningOptions, long ownerId, bool updateSync, Stack<Action> callbacks) { Debug.Assert(Vector3.IsUnit(ref forward)); Debug.Assert(Vector3.IsUnit(ref up)); Debug.Assert(Vector3.ArePerpendicular(ref forward, ref up)); bool spawnAtOrigin = spawningOptions.HasFlag(SpawningOptions.UseGridOrigin); //CreateGridsFromPrefab(resultList, prefabName, MatrixD.CreateWorld(position, forward, up), spawnAtOrigin); CreateGridsData createGridsData = new CreateGridsData(resultList, prefabName, MatrixD.CreateWorld(position, forward, up), spawnAtOrigin, callbacks: callbacks); Interlocked.Increment(ref PendingGrids); callbacks.Push(delegate() { SpawnPrefabInternalSetProperties(resultList, position, forward, up, initialLinearVelocity, initialAngularVelocity, beaconName, spawningOptions, ownerId, updateSync); }); ParallelTasks.Parallel.Start(createGridsData.CallCreateGridsFromPrefab, createGridsData.OnGridsCreated, createGridsData); }
private void SpawnPrefabInternal( List<MyCubeGrid> resultList, String prefabName, Vector3D position, Vector3 forward, Vector3 up, Vector3 initialLinearVelocity, Vector3 initialAngularVelocity, String beaconName, SpawningOptions spawningOptions, bool updateSync) { Debug.Assert(Vector3.IsUnit(ref forward)); Debug.Assert(Vector3.IsUnit(ref up)); Debug.Assert(Vector3.ArePerpendicular(ref forward, ref up)); int rngSeed = 0; using (updateSync ? MyRandom.Instance.PushSeed(rngSeed = MyRandom.Instance.CreateRandomSeed()) : new MyRandom.StateToken()) { bool spawnAtOrigin = spawningOptions.HasFlag(SpawningOptions.UseGridOrigin); CreateGridsFromPrefab(resultList, prefabName, MatrixD.CreateWorld(position, forward, up), spawnAtOrigin); MyCockpit firstCockpit = null; bool rotateToCockpit = spawningOptions.HasFlag(SpawningOptions.RotateFirstCockpitTowardsDirection); bool spawnCargo = spawningOptions.HasFlag(SpawningOptions.SpawnRandomCargo); bool setNeutralOwner = spawningOptions.HasFlag(SpawningOptions.SetNeutralOwner); bool needsToIterateThroughBlocks = spawnCargo || rotateToCockpit || setNeutralOwner || beaconName != null; long owner = 0; if (updateSync && spawningOptions.HasFlag(SpawningOptions.SetNeutralOwner) && resultList.Count != 0) { string npcName = "NPC " + MyRandom.Instance.Next(1000, 9999); var identity = Sync.Players.CreateNewIdentity(npcName); owner = identity.IdentityId; } foreach (var grid in resultList) { grid.ClearSymmetries(); if (spawningOptions.HasFlag(SpawningOptions.DisableDampeners)) { grid.GridSystems.ThrustSystem.DampenersEnabled = false; } if ((spawningOptions.HasFlag(SpawningOptions.DisableSave))) { grid.Save = false; } if (needsToIterateThroughBlocks || spawningOptions.HasFlag(SpawningOptions.TurnOffReactors)) { ProfilerShort.Begin("Iterate through blocks"); foreach (var block in grid.GetBlocks()) { if (block.FatBlock is MyCockpit && rotateToCockpit && firstCockpit == null) { firstCockpit = (MyCockpit)block.FatBlock; } else if (block.FatBlock is MyCargoContainer && spawnCargo) { MyCargoContainer container = block.FatBlock as MyCargoContainer; container.SpawnRandomCargo(); } else if (block.FatBlock is MyBeacon && beaconName != null) { MyBeacon beacon = block.FatBlock as MyBeacon; beacon.SetCustomName(beaconName); } else if (spawningOptions.HasFlag(SpawningOptions.TurnOffReactors) && block.FatBlock is IMyPowerProducer) { (block.FatBlock as IMyPowerProducer).Enabled = false; } if (setNeutralOwner && block.FatBlock != null && block.BlockDefinition.RatioEnoughForOwnership(block.BuildLevelRatio)) { block.FatBlock.ChangeOwner(owner, MyOwnershipShareModeEnum.None); } } ProfilerShort.End(); } } Matrix transform = default(Matrix); if (rotateToCockpit) { System.Diagnostics.Debug.Assert(firstCockpit != null,"cockpit in prefab ship is missing !"); if (firstCockpit != null) { Matrix cockpitTransform = firstCockpit.WorldMatrix; Matrix cockpitInvertedTransform = Matrix.Invert(cockpitTransform); transform = Matrix.Multiply(cockpitInvertedTransform, Matrix.CreateWorld(firstCockpit.WorldMatrix.Translation, forward, up)); } } foreach (var grid in resultList) { if (firstCockpit != null) { grid.WorldMatrix = grid.WorldMatrix * transform; } if (grid.Physics != null) { grid.Physics.LinearVelocity = initialLinearVelocity; grid.Physics.AngularVelocity = initialAngularVelocity; } ProfilerShort.Begin("Add entity"); MyEntities.Add(grid); ProfilerShort.End(); } if (updateSync == true) { MySyncPrefabManager.SendPrefabSpawned(prefabName, new MyPositionAndOrientation(position, forward, up), initialLinearVelocity, initialAngularVelocity, beaconName, spawningOptions, rngSeed); } } }