Exemple #1
0
 public Chunk(World world, ChunkLocation loc)
 {
     World    = world;
     Location = loc;
     Blocks   = new List <Block>();
     // FillTempChunk(); // Temp
 }
Exemple #2
0
    public static ChunkLocation operator -(ChunkLocation chunkLocation1, ChunkLocation chunkLocation2)
    {
        Location location1 = chunkLocation1;
        Location location2 = chunkLocation2;

        return(ChunkLocation.asChunkLocation(location1 - location2));
    }
Exemple #3
0
    //This function is only temporary as the map will not always be a hills biome,
    //it does however show how one goes about generating terrain
    public void generateMap()
    {
        //Create a new reference to the hills bimoe which stores data on what blocks need to be generated and how
        Biome hills = new Hills();

        //Create a new terrain generator for the hills biome in this world
        TerrainGenerator terrainGenerator = new TerrainGenerator(gameManager, this, hills);

        //Iterate over a 4*4 chunk region to generate 16 chunks
        for (int i = -2; i < 2; i++)
        {
            for (int j = -2; j < 2; j++)
            {
                //Get the location of the current chunk
                ChunkLocation chunkLocation = new ChunkLocation(this, i, 0, j);

                Chunk chunk = loadChunk(terrainGenerator, chunkLocation);
                //renderChunk(chunk);
            }
        }

        MapDisplay1 display = FindObjectOfType <MapDisplay1>();

        display.drawMap(4, 4, getLoadedChunks());
    }
Exemple #4
0
    private void loadChunksInRange(ChunkLocation centre)
    {
        World world = centre.getWorld();

        for (int x = -loadedChunksRange; x < loadedChunksRange; x++)
        {
            for (int y = -loadedChunksRange; y < loadedChunksRange; y++)
            {
                ChunkLocation chunkLocation = centre + new ChunkLocation(world, x, y, 0);

                if (!world.containsChunk(chunkLocation))
                {
                    world.loadChunk(new TerrainGenerator(gameManager, world, new Hills()), chunkLocation);
                }
            }
        }

        for (int x = -renderedChunksRange; x < renderedChunksRange; x++)
        {
            for (int y = -renderedChunksRange; y < renderedChunksRange; y++)
            {
                ChunkLocation chunkLocation = centre + new ChunkLocation(world, x, y, 0);

                if (world.containsChunk(chunkLocation))
                {
                    Chunk chunk = world.getChunk(chunkLocation);
                    if (!chunk.isRendered())
                    {
                        chunk.toggleRender();
                    }
                }
            }
        }
    }
Exemple #5
0
        public Nullable <Block> GetBlock(WorldLocation loc)
        {
            ChunkLocation cl = new ChunkLocation((int)Math.Floor((Double)loc.X / 16), loc.Y, (int)Math.Floor((Double)loc.Z / 16));
            Chunk         c  = GetChunk(cl);

            return(c.GetBlock(loc));
        }
Exemple #6
0
 //intialise sets up all the necessary variables whn the chunk is instantiated
 public void initialise(ChunkLocation location)
 {
     //Set up the location in WorldObject
     base.initialise(location);
     //Set the position of the chunk in the game to it's location object
     transform.position = location.getPosition();
 }
Exemple #7
0
 /// <summary>
 /// Returns a copy of the given block location offset by the chunk location
 /// </summary>
 /// <param name="block"></param>
 /// <param name="chunk"></param>
 /// <returns></returns>
 public static BlockLocation GetBlockOffsetByChunk(BlockLocation block, ChunkLocation chunk)
 {
     TempB.Set(
         GetBlockXZOffsetByChunk(block.X, chunk.X),
         block.Y,
         GetBlockXZOffsetByChunk(block.Z, chunk.Z));
     return(TempB);
 }
Exemple #8
0
 /// <summary>
 /// Returns a blank block location (starting at 0,0) offset by a given chunk location
 /// </summary>
 /// <param name="chunkLocation"></param>
 /// <returns></returns>
 public static BlockLocation GetBlankChunkOffset(ChunkLocation chunkLocation)
 {
     TempB.Set(
         chunkLocation.X * ChunkWidth,
         0,
         chunkLocation.Z * ChunkWidth);
     return(TempB);
 }
Exemple #9
0
        protected override void OnKeyDown(KeyboardKeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.IsRepeat)
            {
                return;
            }
            switch (e.Key)
            {
            case Key.F1:
                Settings.Instance.ToggleUpdateCamera();
                break;

            case Key.F2:
                Settings.Instance.ToggleWireframe();
                break;

            case Key.F3:
                Settings.Instance.ToggleAmbientOcclusion();
                break;

            case Key.F4:
                Settings.Instance.ToggleFog();
                break;

            case Key.F5:
                Settings.Instance.TogglePhysics();
                break;

            case Key.F6:
                Settings.Instance.ToggleDebugInfo();
                break;

            case Key.F11:
                WindowState = WindowState == WindowState.Fullscreen ? WindowState.Normal : WindowState.Fullscreen;
                Console.WriteLine(WindowState);
                break;

            case Key.F12:
                VSync = VSync == VSyncMode.Adaptive ? VSyncMode.Off : VSyncMode.Adaptive;
                Console.WriteLine(VSync);
                break;

            case Key.Escape:
                CursorVisible = true;
                break;

            case Key.Space:
                Console.WriteLine(ChunkLocation.FromPos(camera.Position));
                break;
            }
        }
Exemple #10
0
    public bool containsChunk(Location location)
    {
        ChunkLocation chunkLocation = ChunkLocation.asChunkLocation(location);

        foreach (Chunk chunk in loadedChunks)
        {
            if (chunk.getLocation() == chunkLocation)
            {
                return(true);
            }
        }
        return(false);
    }
Exemple #11
0
    public Chunk getChunk(Location location)
    {
        ChunkLocation chunkLocation = ChunkLocation.asChunkLocation(location);

        foreach (Chunk chunk in loadedChunks)
        {
            if (chunk.getLocation() == chunkLocation)
            {
                return(chunk);
            }
        }
        throw new NullReferenceException("No chunk is loaded at the given location");
    }
Exemple #12
0
        public void SetChunk(ChunkLocation loc, Chunk chunk)
        {
            // Check if chunk already exists
            for (int i = 0; i < Chunks.Count; i++)
            {
                if (Chunks[i].Location.Equals(loc))
                {
                    Chunks[i] = chunk;
                }
            }

            // If not, add it
            Chunks.Add(chunk);
        }
    //Generate a chunk at the given location, it populates all chunk columns in the chunk with
    //block type height maps
    public Chunk generateChunk(ChunkLocation chunkLocation, Direction direction)
    {
        //get the biome and the world the terrain is being generated for
        Biome biome = getBiome();
        World world = chunkLocation.getWorld();

        //Instantiate the GameObject in the gme that represents a chunk from file
        GameObject chunkGameObject = MonoBehaviour.Instantiate(Resources.Load("Chunk", typeof(GameObject))) as GameObject;
        //Get the chunk script attached to this GameObject
        Chunk chunk = chunkGameObject.GetComponent("Chunk") as Chunk;

        //Set up the values in the chunk script
        chunk.initialise(chunkLocation);

        //Get the x and y coordinates of the chunk
        int chunkX = chunk.getLocation().getBlockX();
        int chunkZ = chunk.getLocation().getBlockZ();

        //Iterate over all columns in the chunk
        for (int x = 0; x < Chunk.chunkSize; x++)
        {
            for (int z = 0; z < Chunk.chunkSize; z++)
            {
                //Get the coordinate of the column as the chunk's location in the world and the column's position in the
                //chunk, it's z-coord (height) can be 0
                Location columnBaseLocation = new Location(world, chunkX + x, 0, chunkZ + z);

                //Instantieate a ChunkColumn GameObject from file aswell
                GameObject columnGameObject = MonoBehaviour.Instantiate(Resources.Load("Chunk Column", typeof(GameObject))) as GameObject;
                //Set the chunk column to be a child of the chunk in the heirarchy
                columnGameObject.transform.SetParent(chunkGameObject.transform);
                //Get the ChunkColumn script attached to this GameObject
                ChunkColumn chunkColumn = columnGameObject.GetComponent("ChunkColumn") as ChunkColumn;
                //initialise the variables in the chunkcolumn
                chunkColumn.initialise(chunk, biome, columnBaseLocation, direction);

                //Fill the column with blocks based off of the biome
                gameManager.StartCoroutine("populateColumn", chunkColumn);
                //Generate caves in the column (air spaces)
                gameManager.StartCoroutine("generateCaves", chunkColumn);

                //Set the chunk column in the parent chunk to be this chunk column
                chunk.setColumn(columnBaseLocation, chunkColumn);
            }
        }

        //Return the newly generated chunk
        return(chunk);
    }
Exemple #14
0
    private void unloadChunksOutOfRange(ChunkLocation centre)
    {
        World world = centre.getWorld();

        foreach (Chunk chunk in world.getLoadedChunks())
        {
            Location chunksLocation = chunk.getLocation();
            Debug.Log(centre.distance(chunksLocation));
            if (centre.distance(chunksLocation) > (loadedChunksRange + 0.5) * Chunk.chunkSize)
            {
                Debug.Log("=> Boop");
                world.destroyChunk(chunk);
            }
        }
    }
    //Generate a chunk at the given location, it populates all chunk columns in the chunk with
    //block type height maps
    public Chunk regenerateChunk(Chunk chunk, ChunkLocation chunkLocation, Direction direction)
    {
        //get the biome and the world the terrain is being generated for
        Biome biome = getBiome();
        World world = chunkLocation.getWorld();
        //Get the GameObject of the chunk
        GameObject chunkGameObject = chunk.gameObject;

        chunkGameObject.SetActive(true);

        //Set up the values in the chunk script
        chunk.initialise(chunkLocation);

        //Get the x and y coordinates of the chunk
        int chunkX = chunk.getLocation().getBlockX();
        int chunkZ = chunk.getLocation().getBlockZ();

        int i = 0;

        //Iterate over all columns in the chunk
        for (int x = 0; x < Chunk.chunkSize; x++)
        {
            for (int z = 0; z < Chunk.chunkSize; z++)
            {
                //Get the coordinate of the column as the chunk's location in the world and the column's position in the
                //chunk, it's z-coord (height) can be 0
                Location columnBaseLocation = new Location(world, chunkX + x, 0, chunkZ + z);

                //Debug.Log(i);
                ChunkColumn chunkColumn = chunk.getColumns()[i];
                i++;
                chunkColumn.gameObject.SetActive(true);

                chunkColumn.initialise(chunk, biome, columnBaseLocation, direction);

                //Fill the column with blocks based off of the biome
                gameManager.StartCoroutine("populateColumn", chunkColumn);
                //Generate caves in the column (air spaces)
                gameManager.StartCoroutine("generateCaves", chunkColumn);

                //Set the chunk column in the parent chunk to be this chunk column
                chunk.setColumn(columnBaseLocation, chunkColumn);
            }
        }

        //Return the newly generated chunk
        return(chunk);
    }
Exemple #16
0
        public Chunk GetChunk(ChunkLocation loc)
        {
            // Check if chunk exists
            foreach (Chunk c in Chunks)
            {
                if (c.Location.Equals(loc))
                {
                    return(c);
                }
            }

            Chunk chunk = new Chunk(this, loc);

            Chunks.Add(chunk);
            return(chunk);
        }
Exemple #17
0
    public void onPlayerMove(PlayerMoveEvent ev)
    {
        Location location = ev.getLocation();
        World    world    = location.getWorld();

        Vector3 movement = ev.getMovement();

        Direction moveDirection   = DirectionMethods.getDominantDirection(movement);
        Location  ordinalMovement = moveDirection.ordinal(world) * Chunk.chunkSize;

        ChunkLocation playerChunkLocation = ChunkLocation.asChunkLocation(location + ordinalMovement);

        loadChunksInRange(playerChunkLocation);

        unloadChunksOutOfRange(playerChunkLocation);
    }
Exemple #18
0
        private static void TestNbtRegionFileRead()
        {
            int chunkX = 0;
            int chunkZ = 0;

            var files = Directory.GetFiles($@"..\..\..\..\MCJarServer\1.15.2\world\region");

            foreach (var file in files)
            {
                //using var stream = File.OpenRead(
                //   $@"..\..\..\..\MCJarServer\1.15.2\world\region\r.{chunkX}.{chunkZ}.mca");
                using var stream = File.OpenRead(file);

                int regionX = chunkX / 32;
                int regionZ = chunkZ / 32;

                var reader = new NetBinaryReader(stream, NetBinaryOptions.JavaDefault);

                var locations       = new ChunkLocation[1024];
                var locationsStatus = reader.Read(MemoryMarshal.AsBytes(locations.AsSpan()));

                var locationIndices = new int[1024];
                for (int i = 0; i < locationIndices.Length; i++)
                {
                    locationIndices[i] = i;
                }

                Array.Sort(locations, locationIndices);

                var timestamps       = new int[1024];
                var timestampsStatus = reader.Read(timestamps);

                // Find the first valid chunk location.
                int firstValidLocation = 0;
                for (int i = 0; i < locations.Length; i++)
                {
                    if (locations[i].SectorCount != 0)
                    {
                        firstValidLocation = i;
                        break;
                    }
                }
                int chunkCount = 1024 - firstValidLocation;

                var chunkList = new (int Index, NbtDocument Chunk)[chunkCount];
        public void Deserialize(Stream input)
        {
            this.Version = input.ReadValueU32();

            // Detect little endian mode.
            if ((this.Version & 0xFFFF0000) != 0 && (this.Version & 0x0000FFFF) == 0)
            {
                this.LittleEndian = false;
            }
            else
            {
                this.LittleEndian = true;
            }

            this.Unknown1 = input.ReadValueS32(this.LittleEndian);
            int unknown2Count = input.ReadValueS32(this.LittleEndian);
            int textureCount  = input.ReadValueS32(this.LittleEndian);
            int chunkCount    = input.ReadValueS32(this.LittleEndian);

            if (unknown2Count != 0)
            {
                throw new Exception();
            }

            this.ChunkKeys    = new List <ResourceKey>(this.ReadResourceKeys(input, chunkCount));
            this.Unknown2Keys = new List <ResourceKey>(this.ReadResourceKeys(input, unknown2Count));
            this.TextureKeys  = new List <ResourceKey>(this.ReadResourceKeys(input, textureCount));

            ChunkLocation[] chunks = new ChunkLocation[chunkCount];
            for (int i = 0; i < chunkCount; i++)
            {
                chunks[i].Size   = input.ReadValueU32(this.LittleEndian);
                chunks[i].Offset = input.ReadValueU32(this.LittleEndian);
            }

            this.ChunkData = new List <Stream>();
            for (int i = 0; i < chunkCount; i++)
            {
                input.Seek(chunks[i].Offset, SeekOrigin.Begin);
                byte[] data = new byte[chunks[i].Size];
                input.Read(data, 0, data.Length);
                this.ChunkData.Add(new MemoryStream(data));
            }
        }
Exemple #20
0
    public Chunk loadChunk(TerrainGenerator terrainGenerator, ChunkLocation chunkLocation)
    {
        Chunk chunk = null;

        if (unusedChunks.Count > 0)
        {
            chunk = unusedChunks[0];
            unusedChunks.RemoveAt(0);
            chunk = terrainGenerator.regenerateChunk(chunk, chunkLocation, Direction.NORTH);
        }
        else
        {
            //Populate all columns in the chunk using the data in the terrain generator
            chunk = terrainGenerator.generateChunk(chunkLocation, Direction.NORTH);
            //set the chunk to be nested under the world in the heirarchy
            chunk.transform.SetParent(transform);
        }
        //Add this chunk to the list of loaded chunks
        loadedChunks.Add(chunk);

        return(chunk);
    }
Exemple #21
0
        public void Load()
        {
            String worldFolder = WorldManager.WorldPath + "/" + Name;
            String worldFile   = worldFolder + "/world.dat";
            String playerFile  = worldFolder + "/player.dat";

            Logger.Info("Loading world file ['" + Name + "']");
            FileStream fs;

            // World File
            fs = new FileStream(worldFile, FileMode.Open, FileAccess.Read);

            int           chunkCount = fs.ReadInt();
            int           cDataLength;
            Chunk         c;
            ChunkLocation cLoc;

            byte[] cData;
            for (int i = 0; i < chunkCount; i++)
            {
                cLoc        = new ChunkLocation(fs.ReadInt(), 0, fs.ReadInt());
                cDataLength = fs.ReadInt();
                cData       = new byte[cDataLength];
                fs.Read(cData, 0, cData.Length);
                c = new Chunk(this, cLoc);
                c.SetChunkData(cData);
                Chunks.Add(c);
            }

            fs.Close();
            // Player File
            fs = new FileStream(playerFile, FileMode.Open, FileAccess.Read);
            fs.Close();

            Logger.Info("World load complete ['" + Name + "']");
        }
Exemple #22
0
        public void Generate()
        {
            Logger.Info("Generating new world; this may take a minute...");

            // TODO: Real world generation

            ChunkLocation chunkLoc;
            Chunk         c;

            for (int x = SpawnLocation.X - 10; x <= SpawnLocation.X + 10; x++)
            {
                for (int z = SpawnLocation.Z - 10; z <= SpawnLocation.Z + 10; z++)
                {
                    chunkLoc = new ChunkLocation(x, 0, z);
                    c        = new Chunk(this, chunkLoc);
                    c.FillTempChunk();
                    Chunks.Add(c);
                }
            }
            c = null;

            Logger.Info("World generation complete!");
            Save();
        }
Exemple #23
0
 static GridLatch()
 {
     TempB = new BlockLocation(0, 0, 0);
     TempC = new ChunkLocation(0, 0);
 }
Exemple #24
0
 public static Vector3 WTMGetWorldBlock(ChunkLocation chunk, BlockLocation location)
 {
     return(WTMGetChunk(chunk) + WTMGetBlock(location) - ChunkScale);
 }
Exemple #25
0
 /// <summary>
 /// Gets the transformation matrix world coordinates of a chunk based on its location
 /// </summary>
 /// <param name="location"></param>
 /// <returns></returns>
 public static Vector3 WTMGetChunk(ChunkLocation location)
 {
     return(WTMGetChunk(location.X, location.Z));
 }
 public BlockLocation GetWorldLocation(ChunkLocation chunk)
 {
     return(new BlockLocation((chunk.X * GridLatch.ChunkWidth) + X, Y, (chunk.Z * GridLatch.ChunkWidth) + Z));
 }
 public Chunk GetChunkAt(ChunkLocation location)
 {
     Chunks.TryGetValue(location, out Chunk chunk);
     return(chunk);
 }
Exemple #28
0
 public void GenerateChunk(ChunkLocation loc)
 {
     // TODO
 }