Exemple #1
0
        public IChunk Deserialize(Stream stream, PlanetIndex3 position)
        {
            Chunk chunk = new Chunk(position.ChunkIndex, position.Planet);

            using (BinaryReader br = new BinaryReader(stream))
            {
                List <IBlockDefinition>     types = new List <IBlockDefinition>();
                Dictionary <ushort, ushort> map   = new Dictionary <ushort, ushort>();

                bool longIndex = br.ReadByte() > 0;

                int typecount = longIndex ? br.ReadUInt16() : br.ReadByte();

                // Im Falle eines Luftchunks
                if (typecount == 0)
                {
                    return(chunk);
                }

                for (int i = 0; i < typecount; i++)
                {
                    string             typeName    = br.ReadString();
                    IBlockDefinition[] definitions = DefinitionManager.GetBlockDefinitions().ToArray();
                    var blockDefinition            = definitions.FirstOrDefault(d => d.GetType().FullName == typeName);
                    types.Add(blockDefinition);

                    map.Add((ushort)types.Count, (ushort)(Array.IndexOf(definitions, blockDefinition) + 1));
                }

                for (int i = 0; i < chunk.Blocks.Length; i++)
                {
                    ushort typeIndex = longIndex ? br.ReadUInt16() : br.ReadByte();
                    chunk.MetaData[i] = 0;
                    if (typeIndex > 0)
                    {
                        chunk.Blocks[i] = map[typeIndex];

                        var definition = DefinitionManager.GetBlockDefinitionByIndex(map[typeIndex]);
                        if (definition.HasMetaData)
                        {
                            chunk.MetaData[i] = br.ReadInt32();
                        }
                    }
                }
            }

            return(chunk);
        }
Exemple #2
0
        public void AllBlocksDebug()
        {
            var blockDefinitions = DefinitionManager.GetBlockDefinitions();

            foreach (var blockDefinition in blockDefinitions)
            {
                var slot = Player.Inventory.Where(s => s.Definition == blockDefinition && s.Amount < blockDefinition.StackLimit).FirstOrDefault();

                // Wenn noch kein Slot da ist oder der vorhandene voll, dann neuen Slot
                if (slot == null || slot.Amount >= blockDefinition.StackLimit)
                {
                    slot = new InventorySlot()
                    {
                        Definition = blockDefinition,
                        Amount     = 0
                    };
                    Player.Inventory.Add(slot);
                }
                slot.Amount++;
            }
        }
        private IChunk loadChunk(int planetId, Index3 index)
        {
            IUniverse universe = GetUniverse(0);
            IPlanet   planet   = GetPlanet(planetId);

            // Load from disk
            IChunk first = chunkPersistence.Load(universe.Id, planetId, index);

            if (first != null)
            {
                return(first);
            }

            IChunk[] result = mapGenerator.GenerateChunk(DefinitionManager.GetBlockDefinitions(), planet, new Index2(index.X, index.Y));
            if (result != null && result.Length > index.Z && index.Z >= 0)
            {
                result[index.Z].ChangeCounter = 0;
                return(result[index.Z]);
            }

            return(null);
        }