Exemple #1
0
 public override void Apply()
 {
     if (RandomRadius == 0f)
     {
         MyPrefabManager.Static.AddShipPrefab(PrefabFile, Transform.GetMatrix());
     }
     else
     {
         MyPrefabManager.Static.AddShipPrefabRandomPosition(PrefabFile, Transform.Position, RandomRadius);
     }
 }
Exemple #2
0
        private void Create(List <MyObjectBuilder_CubeGrid> grids)
        {
            Vector3D centerSum = new Vector3D();

            MatrixD mRef   = reference.WorldMatrix;
            MatrixD mRefNI = MatrixD.Normalize(MatrixD.Invert(mRef));

            obbs        = new List <MyOrientedBoundingBoxD>(grids.Count);
            orientation = new GridOrientation(reference);
            entityIds   = new HashSet <long>();
            foreach (MyObjectBuilder_CubeGrid grid in grids)
            {
                entityIds.Add(grid.EntityId);
                Vector3I min = Vector3I.MaxValue;
                Vector3I max = Vector3I.MinValue;
                foreach (MyObjectBuilder_CubeBlock cube in grid.CubeBlocks)
                {
                    min = Vector3I.Min(min, cube.Min);
                    max = Vector3I.Max(max, ComputeMax(cube));
                }

                double   cubeGridSize = grid.GridSizeEnum == MyCubeSize.Large ? 2.5 : 0.5;
                Vector3D pMin         = min * cubeGridSize - (cubeGridSize * 0.5);
                Vector3D pMax         = max * cubeGridSize + (cubeGridSize * 0.5);

                MyPositionAndOrientation pos = grid.PositionAndOrientation.Value;
                Vector3D center = Vector3D.Transform((pMin + pMax) * 0.5, pos.GetMatrix());
                centerSum += center;
                MyOrientedBoundingBoxD box = new MyOrientedBoundingBoxD(center, (pMax - pMin) * 0.5, pos.Orientation);
                obbs.Add(box);

                orientation.Include(MatrixD.CreateWorld(center, pos.Forward, pos.Up));
            }


            centerSum     /= obbs.Count;
            relativeCenter = Vector3D.TransformNormal(centerSum - mRef.Translation, MatrixD.Transpose(mRef));

            double radius2 = 0;

            foreach (MyOrientedBoundingBoxD obb in obbs)
            {
                double dist2 = Vector3D.DistanceSquared(centerSum, obb.Center) + obb.HalfExtent.LengthSquared();
                if (dist2 > radius2)
                {
                    radius2 = dist2;
                }
            }

            worldVolume = new BoundingSphereD(centerSum, Math.Sqrt(radius2));
        }
        protected void InternalUpdateOrientation( )
        {
            try
            {
                HkRigidBody havokBody = PhysicsBody;
                if (havokBody == null)
                {
                    return;
                }

                Matrix     orientationMatrix = m_positionOrientation.GetMatrix( ).GetOrientation( );
                Quaternion orientation       = Quaternion.CreateFromRotationMatrix(orientationMatrix);
                havokBody.Rotation = orientation;
            }
            catch (Exception ex)
            {
                ApplicationLog.BaseLog.Error(ex);
            }
        }
Exemple #4
0
            public override void Apply()
            {
                MyFaction faction = null;

                if (FactionTag != null)
                {
                    faction = MySession.Static.Factions.TryGetOrCreateFactionByTag(FactionTag);
                }

                long factionId = faction != null ? faction.FactionId : 0;

                if (RandomRadius == 0f)
                {
                    MyPrefabManager.Static.AddShipPrefab(PrefabFile, Transform.GetMatrix(), factionId, spawnAtOrigin: UseFirstGridOrigin);
                }
                else
                {
                    MyPrefabManager.Static.AddShipPrefabRandomPosition(PrefabFile, Transform.Position, RandomRadius, factionId);
                }
            }
        public static void SendEntityCreatedRelative(MyObjectBuilder_EntityBase entity, IMyCubeGrid grid, Vector3 relativeVelocity, ulong userId)
        {
            // Extract Type and Field Info
            Type sendCreateClassType = SandboxGameAssemblyWrapper.Instance.GetAssemblyType(MultiplayerNamespace, SendCreateClass);
            Type sendCreateRelativeCompressedMsgType = sendCreateClassType.GetNestedType(SendCreateRelativeCompressedMsg, BindingFlags.NonPublic);
            Type sendCreateCompressedMsgType         = sendCreateClassType.GetNestedType(SendCreateCompressedMsg, BindingFlags.NonPublic);

            FieldInfo createMessageField     = sendCreateRelativeCompressedMsgType.GetField(SendCreateRelativeCompressedMsgCreateMessage);
            FieldInfo createBaseEntity       = sendCreateRelativeCompressedMsgType.GetField(SendCreateRelativeCompressedMsgBaseEntity);
            FieldInfo createRelativeVelocity = sendCreateRelativeCompressedMsgType.GetField(SendCreateRelativeCompressedMsgRelativeVelocity);

            FieldInfo createObjectBuilders = sendCreateCompressedMsgType.GetField(SendCreateCompressedMsgObjectBuilders);
            FieldInfo createBuilderLengths = sendCreateCompressedMsgType.GetField(SendCreateCompressedMsgBuilderLengths);

            // Run logic
            MemoryStream             memoryStream = new MemoryStream( );
            MyPositionAndOrientation value        = entity.PositionAndOrientation.Value;
            Matrix matrix = value.GetMatrix( ) * grid.PositionComp.WorldMatrixNormalizedInv;

            entity.PositionAndOrientation = new MyPositionAndOrientation?(new MyPositionAndOrientation(matrix));
            MyObjectBuilderSerializer.SerializeXML(memoryStream, entity, MyObjectBuilderSerializer.XmlCompression.Gzip, typeof(MyObjectBuilder_EntityBase));
            if (memoryStream.Length > (long)2147483647)
            {
                return;
            }

            // SetValues
            object relativeMessage = Activator.CreateInstance(sendCreateRelativeCompressedMsgType);
            object createMessage   = createMessageField.GetValue(relativeMessage);

            createObjectBuilders.SetValue(createMessage, memoryStream.ToArray( ));
            createBuilderLengths.SetValue(createMessage, new int[] { (int)memoryStream.Length });

            createBaseEntity.SetValue(relativeMessage, entity.EntityId);
            createRelativeVelocity.SetValue(relativeMessage, relativeVelocity);

            SendMessage(relativeMessage, userId, sendCreateCompressedMsgType, 1);
        }
Exemple #6
0
 protected override void sync_PilotRelativeEntryUpdated(MyPositionAndOrientation relativeEntry)
 {
     base.sync_PilotRelativeEntryUpdated(relativeEntry);
     m_pilotRelativeWorld = relativeEntry.GetMatrix();
 }
Exemple #7
0
 protected override void sync_PilotRelativeEntryUpdated(MyPositionAndOrientation relativeEntry)
 {
     base.sync_PilotRelativeEntryUpdated(relativeEntry);
     m_pilotRelativeWorld = relativeEntry.GetMatrix();
 }
        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}");
            }
        }