public static bool RemoveEncounter(BoundingBoxD boundingVolume, int seed)
        {
            bool wasFound = false;
            for (int i = 0; i < 2; ++i)
            {
                MyEncounterId encounter = new MyEncounterId(boundingVolume, seed,i);
                if (true == m_savedEncounters.Contains(encounter))
                {
                    wasFound = false;
                    continue;
                }

                List<IMyEntity> entitiesToRemove = new List<IMyEntity>();

                foreach (var entity in m_entityToEncounterConversion)
                {
                    if (entity.Value.BoundingBox == encounter.BoundingBox && entity.Value.Seed == encounter.Seed && entity.Value.EncounterId == encounter.EncounterId)
                    {
                        entity.Key.Close();
                        entitiesToRemove.Add(entity.Key);
                        wasFound = true;
                    }
                }

                foreach (var entity in entitiesToRemove)
                {
                    m_entityToEncounterConversion.Remove(entity);
                }
            }
            return wasFound;
        }
        public static bool PlaceEncounterToWorld(BoundingBoxD boundingVolume, int seed, MyAsteroidCellGenerator.MyObjectSeedType seedType)
        {
            if (MySession.Static.Settings.EnableEncounters == false)
            {
                return false;
            }

            Vector3D placePosition = boundingVolume.Center;
            m_random.SetSeed(seed);

            if (m_spawnGroups.Count == 0)
            {
                m_spawnGroupsNoVoxels.Clear();
                var allSpawnGroups = MyDefinitionManager.Static.GetSpawnGroupDefinitions();
                foreach (var spawnGroup in allSpawnGroups)
                {
                    if (spawnGroup.IsEncounter)
                    {
                        m_spawnGroups.Add(spawnGroup);
                        if (spawnGroup.Voxels.Count == 0)
                        {
                            m_spawnGroupsNoVoxels.Add(spawnGroup);
                        }
                    }
                }
            }

            if (m_spawnGroups.Count > 0)
            {
                m_randomEncounters.Clear();
                m_placePositions.Clear();
                m_encountersId.Clear();
                int numEncoutersToPlace = seedType == MyAsteroidCellGenerator.MyObjectSeedType.EncounterMulti ? 2 : 1;
                List<MySpawnGroupDefinition> currentSpawnGroup = seedType == MyAsteroidCellGenerator.MyObjectSeedType.EncounterMulti ? m_spawnGroupsNoVoxels : m_spawnGroups;

                for (int i = 0; i < numEncoutersToPlace; ++i)
                {
                    MyEncounterId encounterPosition = new MyEncounterId(boundingVolume, seed, i);
                    if (true == m_savedEncounters.Contains(encounterPosition))
                    {
                        continue;
                    }
                    m_randomEncounters.Add(PickRandomEncounter(currentSpawnGroup));
                    Vector3D newPosition = placePosition + (i == 0 ? -1 : 1) * GetEncounterBoundingBox(currentSpawnGroup[m_randomEncounters[m_randomEncounters.Count - 1]]).HalfExtents;
                    Vector3D savedPosition = Vector3D.Zero;
                    if (true == m_movedOnlyEncounters.Dictionary.TryGetValue(encounterPosition, out savedPosition))
                    {
                        newPosition = savedPosition;
                    }
                    encounterPosition.PlacePosition = newPosition;

                    m_encountersId.Add(encounterPosition);

                    m_placePositions.Add(newPosition);
                }

                //first place voxels becaose voxel needs to be created even on client and if grids were created first
                //entity ids woudn't match
                for (int i = 0; i < m_randomEncounters.Count; ++i)
                {
                    foreach (var selectedVoxel in currentSpawnGroup[m_randomEncounters[i]].Voxels)
                    {
                        var filePath = MyWorldGenerator.GetVoxelPrefabPath(selectedVoxel.StorageName);

                        var storage = MyStorageBase.LoadFromFile(filePath);
                        storage.DataProvider = MyCompositeShapeProvider.CreateAsteroidShape(0, 1.0f, MySession.Static.Settings.VoxelGeneratorVersion);
                        IMyEntity voxel = MyWorldGenerator.AddVoxelMap(String.Format("Asteroid_{0}_{1}_{2}", m_entityToEncounterConversion.Count, seed, m_random.Next()), storage, m_placePositions[i] + selectedVoxel.Offset);
                        voxel.Save = false;
                        voxel.OnPhysicsChanged += OnCreatedEntityChanged;
                        m_entityToEncounterConversion[voxel] = m_encountersId[i];
                    }
                }

                if (Sync.IsServer == true)
                {
                    for (int i = 0; i < m_randomEncounters.Count; ++i)
                    {
                        SpawnEncouter(m_encountersId[i], m_placePositions[i], currentSpawnGroup, m_randomEncounters[i]);
                    }
                }
            }

            return true;
        }
        private static void SpawnEncouter(MyEncounterId encounterPosition, Vector3D placePosition, List<MySpawnGroupDefinition> candidates, int selectedEncounter)
        {
            foreach (var selectedPrefab in candidates[selectedEncounter].Prefabs)
            {
                m_createdGrids.Clear();
                Vector3D direction = Vector3D.Forward;
                Vector3D upVector = Vector3D.Up;

                var spawningOptions = Sandbox.ModAPI.SpawningOptions.TurnOffReactors;
                if (selectedPrefab.Speed > 0.0f)
                {
                    spawningOptions = Sandbox.ModAPI.SpawningOptions.RotateFirstCockpitTowardsDirection |
                                     Sandbox.ModAPI.SpawningOptions.SpawnRandomCargo |
                                     Sandbox.ModAPI.SpawningOptions.DisableDampeners;



                    float centerArcRadius = (float)Math.Atan(MyNeutralShipSpawner.NEUTRAL_SHIP_FORBIDDEN_RADIUS / placePosition.Length());
                    direction = -Vector3D.Normalize(placePosition);
                    float theta = m_random.NextFloat(centerArcRadius, centerArcRadius + MyNeutralShipSpawner.NEUTRAL_SHIP_DIRECTION_SPREAD);
                    float phi = m_random.NextFloat(0, 2 * MathHelper.Pi);
                    Vector3D cosVec = Vector3D.CalculatePerpendicularVector(direction);
                    Vector3D sinVec = Vector3D.Cross(direction, cosVec);
                    cosVec *= (Math.Sin(theta) * Math.Cos(phi));
                    sinVec *= (Math.Sin(theta) * Math.Sin(phi));
                    direction = direction * Math.Cos(theta) + cosVec + sinVec;

                    upVector = Vector3D.CalculatePerpendicularVector(direction);
                }
                spawningOptions |= Sandbox.ModAPI.SpawningOptions.DisableSave;

                var prefabDefinition = MyDefinitionManager.Static.GetPrefabDefinition(selectedPrefab.SubtypeId);
                Vector3D prefabPosDeltaValue = Vector3D.Zero;
                if (prefabDefinition.CubeGrids.Length > 0)
                {
                    if (prefabDefinition.CubeGrids[0].PositionAndOrientation.HasValue)
                    {
                        prefabPosDeltaValue = prefabDefinition.CubeGrids[0].PositionAndOrientation.Value.Position;
                    }
                }
                prefabPosDeltaValue -= prefabDefinition.BoundingSphere.Center;

                MyPrefabManager.Static.SpawnPrefab(
                   resultList: m_createdGrids,
                   prefabName: selectedPrefab.SubtypeId,
                   position: placePosition + selectedPrefab.Position - prefabPosDeltaValue,
                   forward: direction,
                   up:upVector,
                   beaconName: selectedPrefab.BeaconText,
                   initialLinearVelocity: direction * selectedPrefab.Speed,
                   spawningOptions: spawningOptions,
                   updateSync: true);

                ProcessCreatedGrids(ref encounterPosition, selectedPrefab.Speed);
            }
        }
 private static void ProcessCreatedGrids(ref MyEncounterId encounterPosition,  float prefabSpeed)
 {
     foreach (var grid in m_createdGrids)
     {              
         grid.OnGridChanged += OnCreatedEntityChanged;
         grid.OnPhysicsChanged += OnCreatedEntityChanged;
         grid.PositionComp.OnPositionChanged += OnCreatedEntityPositionChanged;
         m_entityToEncounterConversion[grid] = encounterPosition;
     }
 }      
예제 #5
0
        private static void SpawnEncounter(MyEncounterId encounterPosition, Vector3D placePosition, List<MySpawnGroupDefinition> candidates, int selectedEncounter)
        {
            var spawnGroup = candidates[selectedEncounter];

            long ownerId = 0; // 0 means that the owner won't be changed
            if (spawnGroup.IsPirate)
                ownerId = MyPirateAntennas.GetPiratesId();

            foreach (var selectedPrefab in spawnGroup.Prefabs)
            {
                m_createdGrids.Clear();
                Vector3D direction = Vector3D.Forward;
                Vector3D upVector = Vector3D.Up;

                var spawningOptions = spawnGroup.ReactorsOn ? Sandbox.ModAPI.SpawningOptions.None : Sandbox.ModAPI.SpawningOptions.TurnOffReactors;
                if (selectedPrefab.Speed > 0.0f)
                {
                    spawningOptions = Sandbox.ModAPI.SpawningOptions.RotateFirstCockpitTowardsDirection |
                                     Sandbox.ModAPI.SpawningOptions.SpawnRandomCargo |
                                     Sandbox.ModAPI.SpawningOptions.DisableDampeners;

                    float centerArcRadius = (float)Math.Atan(MyNeutralShipSpawner.NEUTRAL_SHIP_FORBIDDEN_RADIUS / placePosition.Length());
                    direction = -Vector3D.Normalize(placePosition);
                    float theta = m_random.NextFloat(centerArcRadius, centerArcRadius + MyNeutralShipSpawner.NEUTRAL_SHIP_DIRECTION_SPREAD);
                    float phi = m_random.NextFloat(0, 2 * MathHelper.Pi);
                    Vector3D cosVec = Vector3D.CalculatePerpendicularVector(direction);
                    Vector3D sinVec = Vector3D.Cross(direction, cosVec);
                    cosVec *= (Math.Sin(theta) * Math.Cos(phi));
                    sinVec *= (Math.Sin(theta) * Math.Sin(phi));
                    direction = direction * Math.Cos(theta) + cosVec + sinVec;

                    upVector = Vector3D.CalculatePerpendicularVector(direction);
                }
                spawningOptions |= Sandbox.ModAPI.SpawningOptions.DisableSave;

                if (selectedPrefab.PlaceToGridOrigin) spawningOptions |= SpawningOptions.UseGridOrigin;

                MyPrefabManager.Static.SpawnPrefab(
                   resultList: m_createdGrids,
                   prefabName: selectedPrefab.SubtypeId,
                   position: placePosition + selectedPrefab.Position,
                   forward: direction,
                   up:upVector,
                   beaconName: selectedPrefab.BeaconText,
                   initialLinearVelocity: direction * selectedPrefab.Speed,
                   spawningOptions: spawningOptions | SpawningOptions.UseGridOrigin,
                   ownerId: ownerId,
                   updateSync: true);

                ProcessCreatedGrids(ref encounterPosition, selectedPrefab.Speed);
            }
        }