## Some samples of my natural cave generation routines

Warning!: This is badly written C code, long periods of exposition to this material will give neural damage.
This is the code to generate non-1st level caves, the entrance for 1st level cavel are from one of the sides of the map, not from stairs.
This is my testing code, so don't expect to see very good sintax, pointers to pointers or a good return values in funtions. Also, the map is only handled using an simple array with the ASCII values of tiles.
```int cave_level (int level) {
int i,m,l,x1,x2,y1,y2;
int stx1[4]; int sty1[4]; /*Paranoid oversize? */
int stx2[4]; int sty2[4]; /*Paranoid oversize? */
m=myrand (3)+2;
for (i=0;i<m;i++) {  /* Fist carve the stairs and some holes*/
x1= myrand (LEVELWIDTH-4)+2;
y1= myrand (LEVELHIGHT-4)+2;
make_chasm_point (x1,y1,(i<2)?'>':'.',level);
x2= myrand (LEVELWIDTH-4)+2;
y2= myrand (LEVELHIGHT-4)+2;
make_chasm_point (x2,y2,(i<2)?'<':'.',level);
stx1[i]=x1;sty1[i]=y1;  /* Store the stair coords*/
stx2[i]=x2;sty2[i]=y2;  /* to connect them later */
}
l=myrand(500)+500;
for (i=0;i<l;i++) {  /*Enlarge the "holes"*/
do {
x1= myrand (LEVELWIDTH-4)+2;
y1= myrand (LEVELHIGHT-4)+2;
if ((x1>LEVELWIDTH-6) && (myrand(5)>2)) x1--;
if ((y1>LEVELHIGHT-6) && (myrand(5)>2)) y1--;
if ((x1<4) && (myrand(5)>2)) x1++;
if ((y1<4) && (myrand(5)>2)) y1++;
} while (map[x1][y1][level]!='.');
clear_tile (x1+1,y1,level,'.');
clear_tile (x1,y1+1,level,'.');
clear_tile (x1-1,y1,level,'.');
clear_tile (x1,y1-1,level,'.');
if (myrand(6)>4) {
clear_tile (x1+1,y1+1,level,'.');
clear_tile (x1-1,y1+1,level,'.');
clear_tile (x1-1,y1-1,level,'.');
clear_tile (x1+1,y1-1,level,'.');
}
}
for (i=0;i<m;i++) {  /* Connect the stairs*/
carve_natural_corridor (stx1[i],sty1[i],stx2[i],sty2[i],level);
}
return 1;
}

void make_chasm_point (int i,int j,char cosa,int level) {
clear_tile (i,j,level,cosa); /* Can this screw the generation of stairs?*/
clear_tile (i+1,j,level,'.');
clear_tile (i,j+1,level,'.');
clear_tile (i-1,j,level,'.');
clear_tile (i,j-1,level,'.');
clear_tile (i+1,j+1,level,'.');
clear_tile (i-1,j+1,level,'.');
clear_tile (i-1,j-1,level,'.');
clear_tile (i+1,j-1,level,'.');
}

void clear_tile (int i,int j,int level, char cosa) {
if (map[i][j][level]=='#') map[i][j][level]=cosa; /* Only carve at walls*/
}

void carve_natural_corridor (int x1,int y1,int x2,int y2, int level) { /* Ugly */
int i,j,a,b,c,d;
if ((max(x1,x2)-min(x1,x2))>(max(y1,y2)-min(y1,y2))) {
if (x1<x2) {a=x1;b=y1;c=x2;d=y2;} else {a=x2;b=y2;c=x1;d=y1;};
j=b;   /*Horizontal corridors*/
for (i=a;i<c;i++) {
if (j<d) {
if (((c-i)<=(d-j))||myrand(2)) j++;
} else {
if (j>d) {
if (((c-i)<=(j-d))||myrand(2)) j--;
}
}
if ((c-i)>(myabs(j-d))) {  /* make it wider*/
clear_tile (i,j,level,'.');
myrand (2)?j++:j--;
if (j<2) j=2;if (j>LEVELHIGHT-3) j=LEVELHIGHT-3;
}
clear_tile (i,j,level,'.');
}
} else {  /*Same for vertical corridors*/
if (y1<y2) {a=y1;b=x1;c=y2;d=x2;} else {a=y2;b=x2;c=y1;d=x1;};
j=b;
for (i=a;i<c;i++) {
if (j<d) {
if (((c-i)<=(d-j))||myrand(2)) j++;
} else {
if (j>d) {
if (((c-i)<=(j-d))||myrand(2)) j--;
}
}
if ((c-i)>(myabs(j-d))) { /* make it wider*/
clear_tile (j,i,level,'.');
myrand (2)?j++:j--;
if (j<2) j=2;if (j>LEVELWIDTH-3) j=LEVELWIDTH-3;
}
clear_tile (j,i,level,'.');
}
}
}
```