minecraft

minecraft
git clone git://git.evenfri.pw/minecraft.git
Log | Files | Refs | README

world.c (934B)


      1 #include "minecraft.h"
      2 
      3 World_t World;
      4 
      5 Chunk_t* world_generate_chunk(vec3 position) {
      6   Chunk_t *ret = calloc(1, sizeof(*ret));
      7   if (!ret) { perror("calloc"); exit(1); }
      8 
      9   int block_id = BLOCK_AIR;
     10   const int surface = CHUNK_HEIGHT / 2;
     11 
     12   for (int x = 0; x < CHUNK_SIZE; x++) {
     13     for (int z = 0; z < CHUNK_SIZE; z++) {
     14       for (int y = 0; y < CHUNK_HEIGHT/2; y++) {
     15         if (y == surface - 1) block_id = BLOCK_GRASS;
     16         else if (y >= surface - 3) block_id = BLOCK_DIRT;
     17         else if (y < surface) block_id = BLOCK_STONE;
     18 
     19         ret->blocks[x][y][z] = block_id;
     20       }
     21     }
     22   }
     23 
     24   ret->position = position;
     25   return ret; 
     26 }
     27 
     28 void world_generate() {
     29   int half_world = WORLD_SIZE / 2;
     30   for (int x = 0; x < WORLD_SIZE; x++) {
     31     for (int z = 0; z < WORLD_SIZE; z++) {
     32       Chunk_t *chunk = 
     33         world_generate_chunk((vec3){x - half_world, 0, z - half_world});
     34       World.chunks[x][z] = chunk;
     35     }
     36   }
     37 }