#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>




FILE *filpnt;
extern int MAP[81][51];



void placeovertile(int terrain, int tilenumber, char *name, int pop)
{

int gridnumber;   /*grid 1-4*/
int ok; /*is grid ok?*/
int xx,yy; /*x y coordinates for placement*/
int rep; /*repeat random scan*/
int temploc; /*temporary read location*/
int templ;
int xloc,yloc;
int br,l;
char *city;
		/*pick grid number and load grid file. if it is a
		coastal city, find water on map randomly. if after
		4000 tries with no luck, find new grid and repeat.
		random scan for terrain type and if true, mark x-y
		coordinates.  Open city map file and check if space
		is occupied, if not, place type of city in map file.
		save map file.  If it is occupied, repeat.  Continue
		4000 times until find new grid.
		*/
  rep=0;
  br=0;
  ok=0;
  for(;;){
	br=0;
	ok=0;
	xloc=0;
	yloc=0;
	xx=0;
	yy=0;
	gridnumber=(random(4)+1);

	if(gridnumber==1){
		filpnt=fopen("temp\\map1.dat","rb");
		for(yy=1;yy<=50;yy++){
		  for(xx=1;xx<=80;xx++){
		    fread(&MAP[xx][yy],sizeof(int),1,filpnt);
		    }
		}
		fclose(filpnt);
	}
	if(gridnumber==2){
		filpnt=fopen("temp\\map2.dat","rb");
		for(yy=1;yy<=50;yy++){
		  for(xx=1;xx<=80;xx++){
		    fread(&MAP[xx][yy],sizeof(int),1,filpnt);
		    }
		}
		fclose(filpnt);
	}
	if(gridnumber==3){
		filpnt=fopen("temp\\map3.dat","rb");
		for(yy=1;yy<=50;yy++){
		  for(xx=1;xx<=80;xx++){
		    fread(&MAP[xx][yy],sizeof(int),1,filpnt);
		    }
		}
		fclose(filpnt);
	}
	if(gridnumber==4){
		filpnt=fopen("temp\\map4.dat","rb");
		for(yy=1;yy<=50;yy++){
		  for(xx=1;xx<=80;xx++){
		    fread(&MAP[xx][yy],sizeof(int),1,filpnt);
		    }
		}
		fclose(filpnt);
	} /* end grid ifs*/
			br=0;
			xx=(random(79)+1);
			yy=(random(48)+1);
			temploc=MAP[xx][yy];
	/*check if terrain=0 and temploc=0 scan around water*/
			if((terrain==0)&&(temploc==0)){
			   if((xx-1)>1){
			      templ=MAP[xx-1][yy];
			      if(templ>0){
				xloc=xx-1;
				yloc=yy;
				br=1;
				}
			   }
			   if(br==0){
			      if((xx+1)<79){
				templ=MAP[xx+1][yy];
				if(templ>0){
				  xloc=xx+1;
				  yloc=yy;
				  br=1;
				  }
			      }
			   }
			   if(br==0){
			      if((yy-1)>1){
				templ=MAP[xx][yy-1];
				if(templ>0){
				  yloc=yy-1;
				  xloc=xx;
				  br=1;
				}
			      }
			   }
			   if(br==0){
			      if((yy+1)<49){
				 templ=MAP[xx][yy+1];
				 if(templ>0){
				   yloc=yy+1;
				   xloc=xx;
				   br=1;
				 }
			      }
			   }
			}/*end if terrain=0*/
			if(terrain!=0){
			  if(terrain==MAP[xx][yy]){
			     xloc=xx;
			     yloc=yy;
			     br=1;
			     }
			  if(terrain==8){
			   if(MAP[xx][yy]>0){
			     xloc=xx;
			     yloc=yy;
			     br=1;
			     }
			   }
			}
	if(gridnumber==1){
		filpnt=fopen("temp\\cmap1.dat","rb");
		for(yy=1;yy<=50;yy++){
		  for(xx=1;xx<=80;xx++){
		    fread(&MAP[xx][yy],sizeof(int),1,filpnt);
		    }
		}
		fclose(filpnt);
	}
	if(gridnumber==2){
		filpnt=fopen("temp\\cmap2.dat","rb");
		for(yy=1;yy<=50;yy++){
		  for(xx=1;xx<=80;xx++){
		    fread(&MAP[xx][yy],sizeof(int),1,filpnt);
		    }
		}
		fclose(filpnt);
	}
	if(gridnumber==3){
		filpnt=fopen("temp\\cmap3.dat","rb");
		for(yy=1;yy<=50;yy++){
		  for(xx=1;xx<=80;xx++){
		    fread(&MAP[xx][yy],sizeof(int),1,filpnt);
		    }
		}
		fclose(filpnt);
	}
	if(gridnumber==4){
		filpnt=fopen("temp\\cmap4.dat","rb");
		for(yy=1;yy<=50;yy++){
		  for(xx=1;xx<=80;xx++){
		    fread(&MAP[xx][yy],sizeof(int),1,filpnt);
		    }
		}
		fclose(filpnt);
	} /* end grid ifs*/

	if(br==1){
	  if(MAP[xloc][yloc]==0) {
	     MAP[xloc][yloc]=tilenumber;
	     ok=1;
	  }
	}
       if(ok==1){
	/*SAVE CMAP*/
	if(gridnumber==1){
		filpnt=fopen("temp\\cmap1.dat","wb");
		for(yy=1;yy<=50;yy++){
		  for(xx=1;xx<=80;xx++){
		    fwrite(&MAP[xx][yy],sizeof(int),1,filpnt);
		    }
		}
		fclose(filpnt);
	}
	if(gridnumber==2){
		filpnt=fopen("temp\\cmap2.dat","wb");
		for(yy=1;yy<=50;yy++){
		  for(xx=1;xx<=80;xx++){
		    fwrite(&MAP[xx][yy],sizeof(int),1,filpnt);
		    }
		}
		fclose(filpnt);
	}
	if(gridnumber==3){
		filpnt=fopen("temp\\cmap3.dat","wb");
		for(yy=1;yy<=50;yy++){
		  for(xx=1;xx<=80;xx++){
		    fwrite(&MAP[xx][yy],sizeof(int),1,filpnt);
		    }
		}
		fclose(filpnt);
	}
	if(gridnumber==4){
		filpnt=fopen("temp\\cmap4.dat","wb");
		for(yy=1;yy<=50;yy++){
		  for(xx=1;xx<=80;xx++){
		    fwrite(&MAP[xx][yy],sizeof(int),1,filpnt);
		    }
		}
		fclose(filpnt);
	} /* end grid ifs*/
       } /*end OK ifs*/
    rep++;
    if(rep>4000) ok=2;
    if(ok>0) break;

    }/*end for(;;) ok>0*/

    if(ok==1)
	{


	  if(terrain<8){
	       if(pop==1) strcpy(city,"Hamlet");
	       if(pop==2) strcpy(city,"Village");
	       if(pop==3) strcpy(city,"Town");
	       if(pop==4) strcpy(city,"Small City");
	       if(pop==5) strcpy(city,"Medium City");
	       if(pop==6) strcpy(city,"Large City");
	       }
	    filpnt=fopen("temp\\mlegend.tmp","a");
	    if(gridnumber==2) xloc=xloc+80;
	    if(gridnumber==3) yloc=yloc+48;
	    if(gridnumber==4) {xloc=xloc+80;yloc=yloc+48;}


	    if(terrain==8){
		fprintf(filpnt,
		"%s -(Dungeon) characters level 1-%d, Location-%d,%d\n",
		name,pop,xloc,yloc);
		}
	    if(terrain==0){
		fprintf(filpnt,"%s -(Coastal) %s, Location-%d,%d\n",
		name,city,xloc,yloc);
		}
	    if(terrain==1){
		fprintf(filpnt,"%s -(Desert) %s, Location-%d,%d\n",
		name,city,xloc,yloc);
		}
	    if(terrain==2){
		fprintf(filpnt,"%s -(Plains) %s, Location-%d,%d\n",
		name,city,xloc,yloc);
		}
	    if(terrain==3){
		fprintf(filpnt,"%s -(Lt. Forest) %s, Location-%d,%d\n",
		name,city,xloc,yloc);
		}
	    if(terrain==4){
		fprintf(filpnt,"%s -(Forest) %s, Location-%d,%d\n",
		name,city,xloc,yloc);
		}
	    if(terrain==5){
		fprintf(filpnt,"%s -(Hills) %s, Location-%d,%d\n",
		name,city,xloc,yloc);
		}
	    if(terrain==6){
		fprintf(filpnt,"%s -(Mountains) %s, Location-%d,%d\n",
		name,city,xloc,yloc);
		}
	    if(terrain==7){
		fprintf(filpnt,"%s -(Ice) %s, Location-%d,%d\n",
		name,city,xloc,yloc);
		}

	    fclose(filpnt);
	}/*end if OK*/

return;
}



