Guest User

Untitled

a guest
Mar 1st, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. #include <stdio.h>      /* printf, scanf, puts, NULL */
  2. #include <stdlib.h>     /* srand, rand */
  3. #include <time.h>       /* time */
  4.  
  5. #define maxBlockID 5
  6. #define maxWorldLength 10
  7. #define maxWorldWidth 10
  8.  
  9. typedef enum {
  10.     PLAYER = 0,
  11.     AIR = 1,
  12.     DIRT = 2,
  13.     SAND = 3,
  14.     LAVA = 4,
  15.     DIAMOND = 5
  16. } blockID_t; //Keep synced with maxBlockID
  17.  
  18. void worldBlockPlace(blockID_t* worldBlocks[maxWorldLength][maxWorldWidth], int x, int y, int block) {
  19.     worldBlocks[y][x] = block;
  20.     //printf("BLOCK PLACE: (X,Y) %i, %i BLOCK ID: %i\n", x, y, block); //Debug
  21. }
  22.  
  23. void worldGenerate(blockID_t* worldBlocks[maxWorldLength][maxWorldWidth]) {
  24.     int i, ii;
  25.     srand (time(NULL));
  26.     for(i = 0; i < maxWorldLength; i++) { //Start the bottom to the top
  27.         for(ii = 0; ii < maxWorldWidth; ii++) { //Start placing blocks left to right
  28.            
  29.             worldBlockPlace(worldBlocks,ii,i,rand() % maxBlockID + 1); //Generates random number according to maxBlockID + 1
  30.         }
  31.     }
  32. }
  33.  
  34. void worldDisplay(blockID_t* worldBlocks[maxWorldLength][maxWorldWidth]) {
  35.     int i, ii;
  36.     for(i = 0; i < maxWorldLength; i++) {
  37.         printf("%i: ", i);
  38.         for(ii = 0; ii < maxWorldWidth; ii++) {
  39.             switch((int)worldBlocks[i][ii]) {
  40.                 case PLAYER:
  41.                     printf("\x1b[31m@\x1b[0m");
  42.                     break;
  43.                 case AIR:
  44.                     printf("\x1b[32mA\x1b[0m");
  45.                     break;
  46.                 case DIRT:
  47.                     printf("\x1b[32mD\x1b[0m");
  48.                     break;
  49.                 case SAND:
  50.                     printf("\x1b[33mS\x1b[0m");
  51.                     break;
  52.                 case LAVA:
  53.                     printf("\x1b[31mL\x1b[0m");
  54.                     break;
  55.                 case DIAMOND:
  56.                     printf("\x1b[36mX\x1b[0m");
  57.                     break;
  58.             }
  59.         }
  60.         printf("\n");
  61.     }
  62. }
  63.  
  64. void worldGravity(blockID_t* worldBlocks[maxWorldLength][maxWorldWidth]) {
  65.     int i, ii, iii;
  66.     for(i = 0; i < maxWorldLength; i++) {
  67.         for(ii = 0; ii < maxWorldWidth; ii++) {
  68.             switch((int)worldBlocks[i][ii]) {
  69.                 case SAND:
  70.                     for(iii = 1; i < maxWorldLength-i; iii++) {
  71.                         if(worldBlocks[i+iii][ii] == AIR || worldBlocks[i+iii][ii] == LAVA) {
  72.                             worldBlockPlace(worldBlocks, ii, i, AIR);
  73.                             worldBlockPlace(worldBlocks, ii, i+iii, SAND);
  74.                             printf("GRAVITY(X,Y): %i,%i(SAND) moved to %i,%i\n", ii, i, ii,i+1);
  75.                         } else {
  76.                             break; //Break when the block below isn't AIR
  77.                         }
  78.                     }
  79.                     break;
  80.                 case LAVA:
  81.                     for(iii = 1; i < maxWorldLength-i; iii++) {
  82.                         if(worldBlocks[i+iii][ii] == AIR) {
  83.                             worldBlockPlace(worldBlocks, ii, i+iii, LAVA);
  84.                             printf("GRAVITY(X,Y): %i,%i(LAVA) spread to %i,%i\n", ii, i, ii,i+1);
  85.                         } else {
  86.                             break; //Break when the block below isn't AIR
  87.                         }
  88.                     }
  89.                     break;
  90.                 case PLAYER:
  91.                     printf("To do!\n");
  92.                     break;
  93.             }
  94.         }
  95.     }
  96. }
  97.  
  98.  
  99.  
  100. int main(void) {
  101.     blockID_t* worldBlocks[maxWorldLength][maxWorldWidth];
  102.    
  103.     printf("First generation before gravity\n");
  104.     worldGenerate(worldBlocks);
  105.     worldDisplay(worldBlocks);
  106.    
  107.     printf("\nFirst generation after one pass of gravity\n");
  108.     worldGravity(worldBlocks);
  109.     worldDisplay(worldBlocks);
  110.    
  111. }
Advertisement
Add Comment
Please, Sign In to add comment