Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- struct TileMap{
- unsigned int Width,Height;
- int* Map;
- };
- struct TileMap* generateCave(
- unsigned int Width,
- unsigned int Height,
- unsigned int FP, //Fill Percent
- unsigned int SL, //Smooth level (must be 0-8)
- unsigned int HM //Human made mode(may be -1,0,1) -1 - digging, 0 - standart, 1 - building
- ){
- srand(time(0));
- struct TileMap*m;
- m=(struct TileMap*) malloc(sizeof(struct TileMap));
- m->Map=(int*) malloc(Width * Height * sizeof(int*));
- if(HM<-1)
- HM=-1;//normalize
- if(HM>1)
- HM=1;//normalize
- m->Height=Height;
- m->Width=Width;
- //random fill map
- for(unsigned int y=0;y<Height-1;y++)
- for(unsigned int x=0;x<Width-1;x++)
- if(y==0||x==0||y==Height-2||x==Width-2)
- m->Map[y*Width+x]=1;//wall arround Map
- else
- m->Map[y*Width+x]=(1+rand()%100<FP-(0.4f*FP*(HM)));
- for(unsigned int i=0;i<SL;i++)
- for(unsigned int y=1;y<Height-2;y++)
- for(unsigned int x=1;x<Width-2;x++)
- {
- int wall=0;
- for(unsigned int x1=x-1;x1<=x+1;x1++)
- for(unsigned int y1=y-1;y1<=y+1;y1++)
- if(x1!=x||y1!=y)
- wall+=(m->Map[y1*Width+x1])?(1):(0);
- if(wall>4)
- m->Map[y*Width+x]=1;
- else if(wall<4)
- m->Map[y*Width+x]=0;
- else if(HM==-1)
- m->Map[y*Width+x]=0;
- else if(HM==1)
- m->Map[y*Width+x]=1;
- }
- return m;
- }
- void visualiseTileMap(struct TileMap*m){
- for(unsigned int y=0;y<m->Height;y++){
- for(unsigned int x=0;x<m->Width;x++){
- printf("%c",(m->Map[y*m->Width+x])?('#'):(' '));
- }
- printf("\n");
- }
- }
- int main(){
- struct TileMap* map;
- map = generateCave(160,50,65,6,-1);
- visualiseTileMap(map);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement