]> git.lizzy.rs Git - dragonblocks-bedrock.git/blob - src/mapgen.cpp
Add files via upload
[dragonblocks-bedrock.git] / src / mapgen.cpp
1 #include <string>
2 #include <iostream>
3 #include <stdlib.h>
4 #include <math.h>  
5
6 #include "mapgen.h"
7 #include "map.h"
8 #include "node.h"
9 #include "util.h"
10 #include "game.h"
11
12 using namespace std;
13 void Mapgen(){
14         Game::log("Generating Map");
15         //Air
16         for(int x = 0; x < MAPWIDTH; x++){
17                 for(int y = 0; y < MAPHEIGHT; y++){
18                         Game::map->setNode(x,y,MAPGEN_AIR);
19                         }
20                 }
21         //Seed 13 Easteregg
22         if(Game::seed == 13){
23                 for(int x = 0; x < MAPWIDTH; x++){
24                         for(int y = 0; y < MAPHEIGHT; y++){
25                                 Game::map->setNode(x,y,MAPGEN_BEDROCK);
26                         }
27                 }
28                 Game::log("LOL Seed 13", EASTEREGG);
29                 return;
30         }
31         //Bottom
32         int bottom[MAPWIDTH];
33         bottom[0] = random(MAPHEIGHT/2 - MAPHEIGHT/10, MAPHEIGHT/2);
34         for(int x = 1; x < MAPWIDTH; x++){
35                 bottom[x] = bottom[x-1] + random(-1,1);
36                 }
37         //Smooth        
38         for(int x = 1; x < MAPWIDTH-1; x++){
39                 if(bottom[x] > bottom[x+1] && bottom[x] > bottom[x-1]) bottom[x]--;
40                 else if(bottom[x] < bottom[x+1] && bottom[x] < bottom[x-1])bottom[x]++; 
41                 }
42         //Dirt with Grass
43         for(int x = 0; x < MAPWIDTH; x++){
44                 Game::map->setNode(x,bottom[x],MAPGEN_GRASS);
45                 }
46         //Dirt          
47         for(int x = 0; x < MAPWIDTH; x++){
48                 for(int y = bottom[x]+1; y < bottom[x]+5; y++){
49                         Game::map->setNode(x,y,MAPGEN_DIRT);
50                         }
51                 }
52         //Stone
53         for(int x = 0; x < MAPWIDTH; x++){
54                 for(int y = bottom[x]+5; y < MAPHEIGHT; y++){
55                         Game::map->setNode(x,y,MAPGEN_STONE);
56                         }
57                 }
58         //Mese
59         for(int x = 0; x < MAPWIDTH; x++){
60                 for(int y = bottom[x]+10; y < MAPHEIGHT; y++){
61                         if((rand() % 100) == 0) Game::map->setNode(x,y,MAPGEN_MESE);
62                         }
63                 }               
64         //Bedrock
65         for(int x = 0; x < MAPWIDTH; x++){
66                 Game::map->setNode(x,MAPHEIGHT-1,MAPGEN_BEDROCK);
67                 if(random(0,1) == 0){
68                         Game::map->setNode(x,MAPHEIGHT-2,MAPGEN_BEDROCK);
69                         if(random(0,2) == 0){
70                                 Game::map->setNode(x,MAPHEIGHT-3,MAPGEN_BEDROCK);
71                                 }
72                         }
73                 }
74         //Water
75         int flatcount = 0;
76         for(int x = 1; x < MAPWIDTH; x++){
77                 if(bottom[x] == bottom[x-1]) flatcount++;
78                 else if(flatcount > 7){
79                         int leftborder = rand() % 2;
80                         int rightborder = rand() % 2;
81                         for(int mx = x-flatcount-3; mx < x+2; mx++){
82                                 for(int y = bottom[mx];y < bottom[mx]+5;y++){
83                                         if(Game::map->getNode(mx,y) == MAPGEN_STONE) break;
84                                         if(Game::map->getNode(mx,y) == MAPGEN_DIRT || Game::map->getNode(mx,y) == MAPGEN_GRASS) Game::map->setNode(mx,y,MAPGEN_SAND);
85                                         }
86                                 }
87                         for(int mx = x-flatcount+leftborder;mx < x-1-rightborder;mx++) Game::map->setNode(mx,bottom[x-1],MAPGEN_WATER);
88                         for(int mx = x-flatcount+1+leftborder;mx < x-2-rightborder;mx++) Game::map->setNode(mx,bottom[x-1]+1,MAPGEN_WATER);
89                         for(int mx = x-flatcount+2+leftborder; mx < x-3-rightborder;mx++) Game::map->setNode(mx,bottom[x-1]+2,MAPGEN_WATER);
90                         flatcount = 0;
91                         }
92                 else flatcount = 0;
93                 }       
94         //Tree
95         int treecount = rand() % MAPWIDTH/10;
96         int treepos[treecount];
97         for(int i = 0; i < treecount; i++){
98                 while(true){
99                         bool cont = true;
100                         treepos[i] = rand() % MAPWIDTH;
101                         for(int j = 0; j < i; j++){
102                                 if(abs(treepos[j]-treepos[i]) < 3) cont = false; 
103                                 }
104                         if(!(Game::map->getNode(treepos[i],bottom[treepos[i]]) == MAPGEN_GRASS)) cont = false;  
105                         if(cont) break; 
106                         }
107                 int x = treepos[i];
108                 int y = bottom[x] - 1;  
109                 Game::map->setNode(x,y,MAPGEN_WOOD);
110                 Game::map->setNode(x,y-1,MAPGEN_WOOD);
111                 Game::map->setNode(x,y-2,MAPGEN_LEAVES);
112                 Game::map->setNode(x,y-3,MAPGEN_LEAVES);
113                 Game::map->setNode(x,y-4,MAPGEN_LEAVES);
114                 Game::map->setNode(x-1,y-2,MAPGEN_LEAVES);
115                 Game::map->setNode(x-1,y-3,MAPGEN_LEAVES);
116                 Game::map->setNode(x-1,y-4,MAPGEN_LEAVES);
117                 Game::map->setNode(x+1,y-2,MAPGEN_LEAVES);
118                 Game::map->setNode(x+1,y-3,MAPGEN_LEAVES);
119                 Game::map->setNode(x+1,y-4,MAPGEN_LEAVES);      
120                 }
121         }