예제 #1
0
        protected override void OnLoad(BitStream stream, Action <MyVoxelBase> loadingDoneHandler)
        {
            MyVoxelBase voxelMap;

            bool isUserCreated = VRage.Serialization.MySerializer.CreateAndRead <bool>(stream);
            bool isFromPrefab  = VRage.Serialization.MySerializer.CreateAndRead <bool>(stream);
            bool rangeChanged  = VRage.Serialization.MySerializer.CreateAndRead <bool>(stream);

            byte[] data         = null;
            string asteroidName = null;

            if (rangeChanged)
            {
                data = VRage.Serialization.MySerializer.CreateAndRead <byte[]>(stream);
            }
            else if (isUserCreated)
            {
                asteroidName = VRage.Serialization.MySerializer.CreateAndRead <string>(stream);
            }

            if (isFromPrefab)
            {
                var builder = VRage.Serialization.MySerializer.CreateAndRead <MyObjectBuilder_EntityBase>(stream, MyObjectBuilderSerializer.Dynamic);

                if (rangeChanged && data != null)
                {
                    IMyStorage storage = MyStorageBase.Load(data);
                    if (MyEntities.TryGetEntityById <MyVoxelBase>(builder.EntityId, out voxelMap))
                    {
                        if (voxelMap is MyVoxelMap)
                        {
                            ((MyVoxelMap)voxelMap).Storage = storage;
                        }
                        else if (voxelMap is MyPlanet)
                        {
                            ((MyPlanet)voxelMap).Storage = storage;
                        }
                        else
                        {
                            Debug.Fail("Unknown voxel kind");
                        }
                    }
                    else
                    {
                        voxelMap = (MyVoxelBase)MyEntities.CreateFromObjectBuilderNoinit(builder);
                        if (voxelMap is MyVoxelMap)
                        {
                            ((MyVoxelMap)voxelMap).Init(builder, storage);
                        }
                        else if (voxelMap is MyPlanet)
                        {
                            ((MyPlanet)voxelMap).Init(builder, storage);
                        }
                        else
                        {
                            Debug.Fail("Unknown voxel kind");
                        }
                        if (voxelMap != null)
                        {
                            MyEntities.Add(voxelMap);
                        }
                    }
                }
                else if (isUserCreated)
                {
                    TryRemoveExistingEntity(builder.EntityId);

                    IMyStorage storage = MyGuiScreenDebugSpawnMenu.CreateAsteroidStorage(asteroidName, 0);
                    voxelMap = (MyVoxelBase)MyEntities.CreateFromObjectBuilderNoinit(builder);
                    if (voxelMap is MyVoxelMap)
                    {
                        ((MyVoxelMap)voxelMap).Init(builder, storage);
                    }
                    if (voxelMap != null)
                    {
                        MyEntities.Add(voxelMap);
                    }
                }
                else
                {
                    TryRemoveExistingEntity(builder.EntityId);

                    voxelMap = (MyVoxelBase)MyEntities.CreateFromObjectBuilderNoinit(builder);
                    if (voxelMap != null)
                    {
                        voxelMap.Init(builder);
                        MyEntities.Add(voxelMap);
                    }
                }
            }
            else
            {
                long voxelMapId = VRage.Serialization.MySerializer.CreateAndRead <long>(stream);
                MyEntities.TryGetEntityById <MyVoxelBase>(voxelMapId, out voxelMap);
            }

            if (voxelMap != null)
            {
                BoundingBoxD voxelBox = new BoundingBoxD(voxelMap.PositionLeftBottomCorner, voxelMap.PositionLeftBottomCorner + voxelMap.SizeInMetres);
                m_entities = MyEntities.GetEntitiesInAABB(ref voxelBox);
                foreach (var entity in m_entities)
                {
                    MyVoxelBase voxel = entity as MyVoxelBase;
                    if (voxel != null)
                    {
                        if (voxel.Save == false && voxel != voxelMap)
                        {
                            voxel.Close();
                            break;
                        }
                    }
                }
                m_entities.Clear();
            }

            loadingDoneHandler(voxelMap);
        }
        protected override void OnLoad(BitStream stream, Action <MyVoxelBase> loadingDoneHandler)
        {
            MyVoxelBase voxelMap;

            bool isUserCreated = VRage.Serialization.MySerializer.CreateAndRead <bool>(stream);
            bool isFromPrefab  = VRage.Serialization.MySerializer.CreateAndRead <bool>(stream);
            bool rangeChanged  = VRage.Serialization.MySerializer.CreateAndRead <bool>(stream);

            byte[] data         = null;
            string asteroidName = null;

            if (rangeChanged)
            {
                data = VRage.Serialization.MySerializer.CreateAndRead <byte[]>(stream);
            }
            else if (isUserCreated)
            {
                asteroidName = VRage.Serialization.MySerializer.CreateAndRead <string>(stream);
            }

            if (isFromPrefab)
            {
                var builder = VRage.Serialization.MySerializer.CreateAndRead <MyObjectBuilder_EntityBase>(stream, MyObjectBuilderSerializer.Dynamic);

                if (rangeChanged && data != null)
                {
                    IMyStorage storage = MyStorageBase.Load(data);
                    if (MyEntities.TryGetEntityById <MyVoxelBase>(builder.EntityId, out voxelMap))
                    {
                        if (voxelMap is MyVoxelMap)
                        {
                            ((MyVoxelMap)voxelMap).Storage = storage;
                        }
                        else if (voxelMap is MyPlanet)
                        {
                            ((MyPlanet)voxelMap).Storage = storage;
                        }
                        else
                        {
                            Debug.Fail("Unknown voxel kind");
                        }
                    }
                    else
                    {
                        voxelMap = (MyVoxelBase)MyEntities.CreateFromObjectBuilderNoinit(builder);
                        if (voxelMap is MyVoxelMap)
                        {
                            ((MyVoxelMap)voxelMap).Init(builder, storage);
                        }
                        else if (voxelMap is MyPlanet)
                        {
                            ((MyPlanet)voxelMap).Init(builder, storage);
                        }
                        else
                        {
                            Debug.Fail("Unknown voxel kind");
                        }
                        MyEntities.Add(voxelMap);
                    }
                }
                else if (isUserCreated)
                {
                    TryRemoveExistingEntity(builder.EntityId);

                    IMyStorage storage = MyGuiScreenDebugSpawnMenu.CreateAsteroidStorage(asteroidName, 0);
                    voxelMap = (MyVoxelBase)MyEntities.CreateFromObjectBuilderNoinit(builder);
                    if (voxelMap is MyVoxelMap)
                    {
                        ((MyVoxelMap)voxelMap).Init(builder, storage);
                    }
                    MyEntities.Add(voxelMap);
                }
                else
                {
                    TryRemoveExistingEntity(builder.EntityId);

                    voxelMap = (MyVoxelBase)MyEntities.CreateFromObjectBuilderNoinit(builder);
                    voxelMap.Init(builder);
                    MyEntities.Add(voxelMap);
                }
            }
            else
            {
                long voxelMapId = VRage.Serialization.MySerializer.CreateAndRead <long>(stream);
                MyEntities.TryGetEntityById <MyVoxelBase>(voxelMapId, out voxelMap);
            }
            loadingDoneHandler(voxelMap);
        }
예제 #3
0
        public bool PasteVoxelMap(MyInventory buildInventory = null)
        {
            if (m_planetMode)
            {
                if (!m_canBePlaced)
                {
                    MyHud.Notifications.Add(MyNotificationSingletons.CopyPasteAsteoridObstructed);
                    MyGuiAudio.PlaySound(MyGuiSounds.HudUnable);
                    return(false);
                }

                MyEntities.RemapObjectBuilderCollection(m_copiedVoxelMaps);

                for (int i = 0; i < m_copiedVoxelMaps.Count; ++i)
                {
                    Vector3D pos = m_pastePosition - m_copiedVoxelMapOffsets[i];
                    MyGuiScreenDebugSpawnMenu.SpawnPlanet(pos);
                }

                Deactivate();
                return(true);
            }

            if (m_copiedVoxelMaps.Count == 0)
            {
                return(false);
            }

            if ((m_copiedVoxelMaps.Count > 0) && !IsActive)
            {
                Activate();
                return(true);
            }

            if (!m_canBePlaced)
            {
                MyHud.Notifications.Add(MyNotificationSingletons.CopyPasteAsteoridObstructed);
                MyGuiAudio.PlaySound(MyGuiSounds.HudUnable);
                return(false);
            }

            MyGuiAudio.PlaySound(MyGuiSounds.HudPlaceBlock);

            MyEntities.RemapObjectBuilderCollection(m_copiedVoxelMaps);

            foreach (var voxelMap in m_previewVoxelMaps)
            {
                if (Sync.IsServer)
                {
                    voxelMap.CreatedByUser = true;
                    voxelMap.AsteroidName  = MyGuiScreenDebugSpawnMenu.GetAsteroidName();
                    EnablePhysicsRecursively(voxelMap);
                    voxelMap.Save = true;
                    MakeVisible(voxelMap);
                    m_shouldMarkForClose = false;
                    MyEntities.RaiseEntityCreated(voxelMap);
                }
                else
                {
                    m_shouldMarkForClose = true;

                    MyGuiScreenDebugSpawnMenu.SpawnAsteroid(voxelMap.PositionComp.GetPosition());
                }

                voxelMap.AfterPaste();
            }
            Deactivate();
            return(true);
        }