Example #1
0
 private static void Close()
 {
     if (!IsServer)
     {
         return;
     }
     GeneralLog.WriteToLog("Core", "Unloading...");
     CubeProcessing.Close();
     Drones.Drones.Close();
     PostProcessing.Close();
     Definitions.Close();
     Messaging.Close();
     ProfilerLog.Close();
     GeneralLog.Close();
 }
Example #2
0
        public static void SpawnPrefab(string prefabToSpawn, MatrixD spawnOrigin, Options options = null)
        {
            if (options == null)
            {
                options = new Options();
            }

            Core.GeneralLog.WriteToLog("SpawnPrefab", $"Spawning... {prefabToSpawn} {options.PreservePrograms}");

            List <MyObjectBuilder_EntityBase> tempList = new List <MyObjectBuilder_EntityBase>();

            try
            {
                MyAPIGateway.Parallel.Start(delegate
                {
                    List <MyObjectBuilder_Cockpit> myCockpitList             = new List <MyObjectBuilder_Cockpit>();
                    List <MyObjectBuilder_RemoteControl> myRemoteControlList = new List <MyObjectBuilder_RemoteControl>();

                    //WeaponSwapper.ProcessPrefab(prefab, options);

                    bool cubeGridZero             = true;
                    MatrixD mainGridInverseMatrix = MatrixD.Identity;
                    MatrixD mainGridSpawnMatrix   = MatrixD.Identity;

                    MyPrefabDefinition prefabs = MyDefinitionManager.Static.GetPrefabDefinition(prefabToSpawn);
                    if (prefabs.CubeGrids[0] == null)
                    {
                        return;
                    }


                    MyAPIGateway.Entities.RemapObjectBuilderCollection(prefabs.CubeGrids);

                    foreach (MyObjectBuilder_CubeGrid gridBuilderOrig in prefabs.CubeGrids)
                    {
                        MyObjectBuilder_CubeGrid gridBuilder = (MyObjectBuilder_CubeGrid)gridBuilderOrig.Clone();                          // TODO adds lag, need to fix?  problem is all cube blocks need to be cloned.

                        //MyObjectBuilderSerializer

                        tempList.Add(gridBuilder);

                        options.EntityId = gridBuilder.EntityId;
                        CubeProcessing.GeneralGridSettings(gridBuilder, options);

                        foreach (MyObjectBuilder_CubeBlock block in gridBuilder.CubeBlocks)
                        {
                            if (block == null)
                            {
                                continue;
                            }
                            CubeProcessing.GeneralBlockSettings(block, options);

                            Action <MyObjectBuilder_CubeBlock, Options, MyCubeSize> action;
                            CubeProcessing.CubeBlockProcessing.TryGetValue(block.GetType(), out action);
                            action?.Invoke(block, options, gridBuilder.GridSizeEnum);

                            if (!cubeGridZero)
                            {
                                continue;
                            }
                            if (block.GetType() == typeof(MyObjectBuilder_Cockpit))
                            {
                                myCockpitList.Add(block as MyObjectBuilder_Cockpit);
                            }
                            if (block.GetType() == typeof(MyObjectBuilder_RemoteControl))
                            {
                                myRemoteControlList.Add(block as MyObjectBuilder_RemoteControl);
                            }
                        }

                        MyPositionAndOrientation gridPositionAndOrientation = gridBuilder.PositionAndOrientation ?? MyPositionAndOrientation.Default;
                        MatrixD gridTransform = gridPositionAndOrientation.GetMatrix();
                        MatrixD subGridOffset = MatrixD.Identity;

                        if (cubeGridZero)
                        {
                            mainGridInverseMatrix = MatrixD.Invert(ref gridTransform);
                            mainGridSpawnMatrix   = spawnOrigin;

                            MyObjectBuilder_CubeBlock mainOrientationBlock = GetMainOrientationBlock(myCockpitList, myRemoteControlList);

                            if (mainOrientationBlock != null)
                            {
                                Matrix blockMatrix;
                                ((MyBlockOrientation)mainOrientationBlock.BlockOrientation).GetMatrix(out blockMatrix);
                                blockMatrix.Translation = ((Vector3I)mainOrientationBlock.Min) * (gridBuilder.GridSizeEnum == MyCubeSize.Large ? 2.5f : 0.5f);

                                Matrix inverseBlockMatrix = Matrix.Invert(ref blockMatrix);
                                mainGridSpawnMatrix       = inverseBlockMatrix * mainGridSpawnMatrix;
                            }
                        }
                        else
                        {
                            subGridOffset = gridTransform * mainGridInverseMatrix;
                        }

                        if (gridBuilder.PositionAndOrientation != null)
                        {
                            MatrixD gridSpawnMatrix            = subGridOffset * mainGridSpawnMatrix;
                            gridBuilder.PositionAndOrientation = new MyPositionAndOrientation(ref gridSpawnMatrix);
                        }
                        cubeGridZero = false;
                    }
                    tempList.ForEach(item => MyAPIGateway.Entities.CreateFromObjectBuilderParallel(item, true));
                    //foreach (MyObjectBuilder_EntityBase item in tempList) MyAPIGateway.Entities.CreateFromObjectBuilderParallel(item, true);
                    //MyAPIGateway.Multiplayer.SendEntitiesCreated(tempList); // may need to uncomment this if entities aren't syncing
                    tempList.Clear();
                    myCockpitList.Clear();
                    myRemoteControlList.Clear();
                });
            }
            catch (Exception e)
            {
                Core.GeneralLog.WriteToLog("prefabToSpawn", $"Prefab Processing Exception! {e}");
            }
        }