/*             Make multipoint polygon          */


/*      there must be a min and max city perimeter size based
	on city size*/


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

block virtscreen;
color virtpalette[256];


tpolypoint plainpoly[9]; /* Holds polygon points before rotation */

static int numspot;   /*number of free places for buildings*/
static int citysize;  /*1=hamlet 2=village 3=town 4=sm city 5=med city 6=lg city*/
static int castle;
static int keap;
static int market;
static int walls;
static int terrain;

static int FOREST;
static int MOUNTAINS;
static int DESERT;
static int PLAINS;
static int COASTAL;

static int MAP[41][26];
int max_x,max_y,xstart,ystart;
int num_sides,stopp;


static int total_bus;
static int total_gov;
static int total_temp;
static int total_res;
static int total_misc;
static int total_park;
static int total_all;



/***general*****/

static int scribe;
static int municipal;
static int artisan;
static int blacksmith;
static int inn;
static int inntavern;
static int tavern;
static int tailor;
static int baker;
static int shoemaker;
static int restaurant;
static int stable;
static int carpenter;
static int locksmith;
static int leatherworker;
static int woodcrafter;
static int glassworker;
static int sculptor;
static int mapmaker;
static int potter;
static int furrier;
static int jeweler;
static int mason;
static int generalstore;
static int temple;
static int weaponsmith;
static int armorer;
static int moneylender;
static int bank;
static int alchemist;
static int apothecary;
static int textileseller;
static int butcher;
static int mercantileexchange;
static int jail;
static int well;
static int statue;
static int resident;
static int barracks;
static int warehouse;
static int library;
static int bookseller;
static int park;
static int graveyard;

/*PLAINS*/
static int dairy;
static int grainery;
static int ropemaker;
static int farmersguild;

/*MOUNTAINS*/
static int miner;
static int lapidary;
static int mountaintrapper;
static int quary;
static int minersguild;

/*DESERT*/
static int exoticanimalseller;

/*FOREST*/
static int foresttrapper;
static int woodcutter;
static int forestersguild;

/*COASTAL*/
static int fisherman;
static int shipmaker;
static int baitseller;
static int sailmaker;
static int shipmastersguild;
static int fishermansguild;

/*GUILDS*/
static int thievesguild;
static int merchantsguild;
static int manatarmsguild;
static int assasinsguild;
static int wizardsguild;
static int moneylendersguild;
static int artisansguild;

char cityname[81];

FILE *fp;


void make_object (void)
/* Set up some points to make a triangle */
{
 plainpoly[0].x = random(max_x)+1+xstart;
 plainpoly[0].y = random(max_y)+1+ystart;
 plainpoly[0].sx = 0;
 plainpoly[0].sy = 0;

 plainpoly[1].x = random(max_x)+1+xstart;
 plainpoly[1].y = random(max_y)+1+ystart;
 plainpoly[1].sx = 0;
 plainpoly[1].sy = 0;

 plainpoly[2].x = random(max_x)+1+xstart;
 plainpoly[2].y = random(max_y)+1+ystart;
 plainpoly[2].sx = 0;
 plainpoly[2].sy = 0;

 plainpoly[3].x = random(max_x)+1+xstart;
 plainpoly[3].y = random(max_y)+1+ystart;
 plainpoly[3].sx = 0;
 plainpoly[3].sy = 0;

 plainpoly[4].x = random(max_x)+1+xstart;
 plainpoly[4].y = random(max_y)+1+ystart;
 plainpoly[4].sx = 0;
 plainpoly[4].sy = 0;

 plainpoly[5].x = random(max_x)+1+xstart;
 plainpoly[5].y = random(max_y)+1+ystart;
 plainpoly[5].sx = 0;
 plainpoly[5].sy = 0;

 plainpoly[6].x = random(max_x)+1+xstart;
 plainpoly[6].y = random(max_y)+1+ystart;
 plainpoly[6].sx = 0;
 plainpoly[6].sy = 0;

 plainpoly[7].x = random(max_x)+1+xstart;
 plainpoly[7].y = random(max_y)+1+ystart;
 plainpoly[7].sx = 0;
 plainpoly[7].sy = 0;
}

void fillit()
{
int x,y;
int c;
	wsetcolor(40);
	for(y=0;y<=25;y++){         /*left to right*/
		for(x=0;x<=40;x++){
			c=wgetpixel(x,y);
			if(c==60) {x=999;c=255;}
			if(c!=255) wputpixel(x,y);
			c=0;
			}
		}
	for(y=0;y<=25;y++){      /*right to left*/
		for(x=40;x>=0;x--){
			c=wgetpixel(x,y);
			if(c==60) {x=-1;c=255;}
			if(c!=255) wputpixel(x,y);
			c=0;
			}
		}

	for(x=0;x<=40;x++){        /*top to bottom*/
		for(y=0;y<=25;y++){
			c=wgetpixel(x,y);
			if(c==60) {y=999;c=255;}
			if(c!=255) wputpixel(x,y);
			c=0;
			}
		}
	for(x=0;x<=40;x++){        /*bottom to top*/
		for(y=25;y>=0;y--){
			c=wgetpixel(x,y);
			if(c==60) {y=-1;c=255;}
			if(c!=255) wputpixel(x,y);
			c=0;
			}
		}
	wsetcolor(20);
	for(x=0;x<=40;x++){
		for(y=0;y<=25;y++){
			c=wgetpixel(x,y);
			if(c==60) wputpixel(x,y);
			if(c==0)  wputpixel(x,y);
		}
	}
}

int countspot(void)
{
int x,y,c,cnt;

cnt=0;
	for(x=0;x<=40;x++){
		for(y=0;y<=25;y++){
			c=wgetpixel(x,y);
			if(c==20) cnt++;
		}
	}
return cnt;
}


void roads(int pop_size)
{                                /*road icon fills entire 16pixel block*/
int totl;
int numroadsmin,numroadsmax,howmany;
int cnt,poscheck,poscheck2,length;
int bad,startx,starty,co,direction,x,y;
int l,inter,cl,br,counter;

totl=0;

if(pop_size==1) {numroadsmin=1;numroadsmax=2;}
if(pop_size==2) {numroadsmin=1;numroadsmax=3;}
if(pop_size==3) {numroadsmin=2;numroadsmax=4;}
if(pop_size==4) {numroadsmin=3;numroadsmax=5;}
if(pop_size==5) {numroadsmin=4;numroadsmax=7;}
if(pop_size==6) {numroadsmin=6;numroadsmax=15;}
totl=numroadsmax-numroadsmin;
howmany=random(totl)+numroadsmin;

       for(cnt=1;cnt<=howmany;cnt++){
	  bad=1;
	  counter=0;
	  while(bad==1){

		poscheck=0;
		poscheck2=0;

		inter=0;
		br=0;
		while(br==0){
		  startx=(random(39)+1);
		  starty=(random(24)+1);
		  co=wgetpixel(startx,starty);
		  if(co==40) br=0;
		  else br=1;
		}
		direction=random(4)+1;
		length=(random(13)+5);
		l=length;
		counter++;
		if(counter>2000) {bad=0;cnt=howmany+999;co=0;direction=0;}
		if(co==20) {
			if(direction==1){  /*up*/
			     if(wgetpixel(startx-1,starty-1)==30) poscheck=1;
			     if(wgetpixel(startx-1,starty)==30) poscheck2=1;
			     if(wgetpixel(startx+1,y-1)==30) poscheck=1;
			     if(wgetpixel(startx+1,y)==30) poscheck2=1;


				l=(starty-length);
				if(l<0) l=0;
				for(y=starty;y>=l;y--){
						     cl=wgetpixel(startx,y);
						     if(cl==30) inter=1;
				}
			}
			if(direction==2){ /*down*/
			     if(wgetpixel(startx-1,starty)==30) poscheck=1;
			     if(wgetpixel(startx-1,starty+1)==30) poscheck2=1;

			     if(wgetpixel(startx+1,starty)==30) poscheck=1;
			     if(wgetpixel(startx+1,starty+1)==30) poscheck2=1;


				l=(starty+length);
				if(l>24) l=24;
				for(y=starty;y<=l;y++){
						     cl=wgetpixel(startx,y);
						     if(cl==30) inter=1;
				}
			}
			if(direction==3){   /*right*/
			     if(wgetpixel(startx+1,starty-1)==30) poscheck=1;
			     if(wgetpixel(startx+1,starty+1)==30) poscheck=1;
			     if(wgetpixel(startx+2,starty-1)==30) poscheck2=1;
			     if(wgetpixel(startx+2,starty+1)==30) poscheck2=1;

			     l=(startx+length);
				if(l>39) l=39;
				for(x=startx;x<=l;x++){
						     cl=wgetpixel(x,starty);
						     if(cl==30) inter=1;
				}
			}
			if(direction==4){    /*left*/
			     if(wgetpixel(startx-1,starty-1)==30) poscheck=1;
			     if(wgetpixel(startx-1,starty+1)==30) poscheck=1;
			     if(wgetpixel(startx-2,starty-1)==30) poscheck2=1;
			     if(wgetpixel(startx-2,starty+1)==30) poscheck2=1;

			     l=(startx-length);
				if(l<0) l=0;
				for(x=startx;x>=l;x--){
						     cl=wgetpixel(x,starty);
						     if(cl==30) inter=1;
				}
			}
			if((poscheck==1)&&(poscheck2==1)) poscheck=3;

			if((poscheck!=3)&&(inter==1)) bad=0;
			if(cnt==1) bad=0;
		} /*end if*/
	  } /*end while*/



		if(direction==1){    /*up*/
				length=(starty-length);
				if(length<0) length=0;
				for(y=starty;y>=length;y--){
						     wsetcolor(30);
						     co=wgetpixel(startx,y);
						     if(co==40) y=-1;
						     else wputpixel(startx,y);
				}
		}
		if(direction==2){     /*down*/
				length=(starty+length);
				if(length>24) length=24;
				for(y=starty;y<=length;y++){
						     wsetcolor(30);
						     co=wgetpixel(startx,y);
						     if(co==40) y=999;
						     else wputpixel(startx,y);
				}
		}
		if(direction==3){     /*right*/
				length=(startx+length);
				if(length>39) length=39;
				for(x=startx;x<=length;x++){
						wsetcolor(30);
						co=wgetpixel(x,starty);
						if(co==40) x=999;
						else wputpixel(x,starty);
				}
		}
		if(direction==4){    /*left*/
				length=(startx-length);
				if(length<0) length=0;
				for(x=startx;x>=length;x--){
						wsetcolor(30);
						co=wgetpixel(x,starty);
						if(co==40)  x=-1;
						else wputpixel(x,starty);
				}
		}
	  }/*end of for statement*/
}

int placecastle(void)
{
int x,y,c,r;
int stoprl,cnt,brk;

stoprl=0;
c=0;
r=0;
cnt=0;
brk=0;

	while(stoprl==0){
		c=0;
		r=0;
		x=random(39);
		y=random(24);
		if(wgetpixel(x,y)==20) c=c+1;
		if(wgetpixel(x+1,y)==20) c=c+1;
		if(wgetpixel(x+2,y)==20) c=c+1;
		if(wgetpixel(x,y+1)==20) c=c+1;
		if(wgetpixel(x+1,y+1)==20) c=c+1;
		if(wgetpixel(x+2,y+1)==20) c=c+1;
		if(wgetpixel(x+1,y-1)==30) r=1;  /*check road intersection*/
		if(wgetpixel(x+1,y+2)==30) r=1;

		if((c>=6)&&(r==1)) stoprl=1;
		cnt++;
		if(cnt>2000) brk=-1;
		if(brk<0) stoprl=1;

	}
	if(brk>=0){
	wsetcolor(50);
	wputpixel(x,y);
	wputpixel(x+1,y);
	wputpixel(x+2,y);
	wputpixel(x,y+1);
	wputpixel(x+1,y+1);
	wputpixel(x+2,y+1);
	}

return(brk);
}

int placemarket(void)
{
int x,y,c,r;
int stoprl,cnt,brk;

stoprl=0;
c=0;
r=0;
cnt=0;
brk=0;

	while(stoprl==0){
		c=0;
		r=0;
		x=random(39);
		y=random(24);
		if(wgetpixel(x,y)==20) c=c+1;
		if(wgetpixel(x+1,y)==20) c=c+1;
		if(wgetpixel(x+2,y)==20) c=c+1;
		if(wgetpixel(x,y+1)==20) c=c+1;
		if(wgetpixel(x+1,y+1)==20) c=c+1;
		if(wgetpixel(x+2,y+1)==20) c=c+1;
				       /*check road intersection*/
		if(wgetpixel(x,y-1)==30) r=1;
		if(wgetpixel(x+1,y-1)==30) r=1;
		if(wgetpixel(x+2,y-1)==30) r=1;
		if(wgetpixel(x+3,y)==30) r=1;
		if(wgetpixel(x+3,y+1)==30) r=1;
		if(wgetpixel(x+2,y+2)==30) r=1;
		if(wgetpixel(x+1,y+2)==30) r=1;
		if(wgetpixel(x,y+2)==30) r=1;
		if(wgetpixel(x-1,y)==30) r=1;
		if(wgetpixel(x-1,y+1)==30) r=1;

		if((c>=6)&&(r==1)) stoprl=1;
		cnt++;
		if(cnt>2000) brk=-1;
		if(brk<0) stoprl=1;

	}
	if(brk>=0){
	wsetcolor(70);
	wputpixel(x,y);
	wputpixel(x+1,y);
	wputpixel(x+2,y);
	wputpixel(x,y+1);
	wputpixel(x+1,y+1);
	wputpixel(x+2,y+1);
	}

return(brk);
}

int placekeep(void)
{
int x,y,c,r;
int stoprl,cnt,brk;

stoprl=0;
c=0;
r=0;
cnt=0;
brk=0;

	while(stoprl==0){
		c=0;
		r=0;
		x=random(39);
		y=random(24);
		if(wgetpixel(x,y)==20) c=c+1;
		if(wgetpixel(x+1,y)==20) c=c+1;
		if(wgetpixel(x,y+1)==20) c=c+1;
		if(wgetpixel(x+1,y+1)==20) c=c+1;
		if(wgetpixel(x,y-1)==30) r=1;  /*check road intersection*/
		if(wgetpixel(x+1,y-1)==30) r=1;
		if(wgetpixel(x,y+2)==30) r=1;
		if(wgetpixel(x+1,y+2)==30) r=1;
		if(wgetpixel(x-1,y)==30) r=1;
		if(wgetpixel(x-1,y+1)==30) r=1;
		if(wgetpixel(x+2,y)==30) r=1;
		if(wgetpixel(x+2,y+1)==30) r=1;
		if(wgetpixel(x+2,y)==30) r=1;
		if(wgetpixel(x+2,y+1)==30) r=1;

		if((c>=4)&&(r==1)) stoprl=1;
		cnt++;
		if(cnt>2000) brk=-1;
		if(brk<0) stoprl=1;

	}
	if(brk>=0){
	wsetcolor(80);
	wputpixel(x,y);
	wputpixel(x+1,y);
	wputpixel(x,y+1);
	wputpixel(x+1,y+1);
	}

return(brk);
}





/*********HAMLET***********/
void hamlet()
{
int z;


                z=(random(100)+1);
		if(z<=75){
                        z=(random(3)+1);
			if(z==1) blacksmith++;
                        if(z==2) generalstore++;
			if(z==3) butcher++;
		total_bus++;
		}

		z=(random(100)+1);
		if(z<=50){
			z=(random(12)+1);
			if(z==1) inn++;
			if(z==2) tavern++;
			if(z==3) tailor++;
			if(z==4) carpenter++;
			if(z==5) mason++;
			if(z==6) weaponsmith++;
			if(z==7) armorer++;
			if(z==8) shoemaker++;
			if(z==9) stable++;
			if(z==10) baker++;
			if(z==11) inntavern++;
			if(z==12) artisan++;
		total_bus++;
		}

		z=(random(100)+1);
		if(z<=25){
			z=(random(8)+1);
			if(z==1) restaurant++;
			if(z==2) locksmith++;
			if(z==3) leatherworker++;
			if(z==4) woodcrafter++;
			if(z==5) glassworker++;
			if(z==6) mapmaker++;
			if(z==7) furrier++;
			if(z==8) potter++;
		total_bus++;
		}

		z=(random(100)+1);
		if(z<=10){
			z=(random(9)+1);
			if(z==1) temple++;
			if(z==2) sculptor++;
			if(z==3) moneylender++;
			if(z==4) bank++;
			if(z==5) alchemist++;
			if(z==6) apothecary++;
			if(z==7) textileseller++;
			if(z==8) mercantileexchange++;
			if(z==9) bookseller++;

		  if(z==1) total_temp++;
		  if(z>1)  total_bus++;
		}

		if(FOREST==1){
			z=(random(100)+1);
			if(z<=90) {
				woodcutter++;
				total_bus++;
				}
			z=(random(100)+1);
			if(z<=75){
				foresttrapper++;
				total_bus++;
				}
		}

		if(MOUNTAINS==1){
			z=(random(100)+1);
			if(z<=90){
				miner++;
				total_bus++;
				}
			z=(random(100)+1);
			if(z<=75){
				lapidary++;
				total_bus++;
				}
			z=(random(100)+1);
			if(z<=60){
				mountaintrapper++;
				total_bus++;
				}
			quary++;
			total_bus++;
		}
		if(DESERT==1){
			z=(random(100)+1);
			if(z<=50){
				exoticanimalseller++;
				total_bus++;
				}
		}
		if(PLAINS==1){
			z=(random(100)+1);
			if(z<=90){
				dairy++;
				total_bus++;
				}
			z=(random(100)+1);
			if(z<=60){
				 grainery++;
				 total_bus++;
				 }
			z=(random(100)+1);
			if(z<=40){
				ropemaker++;
				total_bus++;
				}
		}
		if(COASTAL==1){
			z=(random(100)+1);
			if(z<=90){
				fisherman++;
				total_bus++;
				}
			z=(random(100)+1);
			if(z<=75){
				baitseller++;
				total_bus++;
				}
			z=(random(100)+1);
			if(z<=50){
				warehouse++;
				total_bus++;
				}
			z=(random(100)+1);
			if(z<=20){
				shipmaker++;
				total_bus++;
				}
			z=(random(100)+1);
			if(z<=20){
				sailmaker++;
				total_bus++;
				}
		}
	z=(random(100)+1);
	if(z<=10) {
			park++;
			total_park++;
			}
	if(z>=98) {
			graveyard++;
			total_park++;
			}

	z=(random(100)+1);
	if(z<=50){
			well++;
			total_misc++;
			}

	z=(random(100)+1);
	if(z<=25) {
			municipal++;
			total_gov++;
		   }

	total_res=(total_bus+(int)(total_bus/3));

	resident=total_res;
total_all=total_res+total_bus+total_gov+total_temp+total_misc+total_park;
}

/**********VILLAGE************/
void village()
{
int w,x,y,z;



        w=0;
        z=(random(5)+1);
	for(x=1;x<=z;x++){
	  w=0;
	  while(w==0){
		y=(random(16)+1);

		if((y==1)&&(blacksmith==0)){
			blacksmith++;
                        total_bus++;
                        w=1;
                        }
		if((y==2)&&(generalstore==0)){
			generalstore++;
                        total_bus++;
			w=1;
                        }
		if((y==3)&&(butcher==0)){
                        butcher++;
			total_bus++;
                        w=1;
                        }
		if((y==4)&&(inn<2)){
                        inn++;
			total_bus++;
			w=1;
			}
                if((y==5)&&(tavern<2)){
			tavern++;
			total_bus++;
                        w=1;
                        }
		if((y==6)&&(tailor==0)){
                        tailor++;
                        total_bus++;
			w=1;
                        }
		if((y==7)&&(shoemaker==0)){
                        shoemaker++;
                        total_bus++;
                        w=1;
                        }
		if((y==8)&&(stable==0)){
                        stable++;
			total_bus++;
			w=1;
			}
                if((y==9)&&(weaponsmith==0)){
			weaponsmith++;
                        total_bus++;
                        w=1;
			}
		if((y==10)&&(baker==0)){
                        baker++;
                        total_bus++;
			w=1;
                        }
		if((y==11)&&(mason==0)){
                        mason++;
                        total_bus++;
                        w=1;
			}
		if((y==12)&&(leatherworker==0)){
                        leatherworker++;
			total_bus++;
			w=1;
			}
		if((y==13)&&(potter==0)){
			potter++;
                        total_bus++;
                        w=1;
                        }
		if((y==14)&&(woodcrafter==0)){
                        woodcrafter++;
                        total_bus++;
			w=1;
			}
		if((y==15)&&(inntavern==0)){
			inntavern++;
			total_bus++;
			w=1;
			}
		if((y==16)&&(artisan<2)){
			artisan++;
			total_bus++;
			w=1;
			}

		} /*end while loop*/
	  }

	  z=(random(100)+1);
	  if(z<=40) y=3;
	  if((z>40)&&(z<=70)) y=2;
	  if(z>70) y=1;

	  for(x=1;x<=y;x++){
		w=0;
		while(w==0){
			z=(random(8)+1);

			if((z==1)&&(restaurant==0)){
				restaurant++;
				total_bus++;
				w=1;
				}
			if((z==2)&&(armorer==0)){
				armorer++;
				total_bus++;
				w=1;
				}
			if((z==3)&&(locksmith==0)){
				locksmith++;
                                total_bus++;
                                w=1;
				}
                        if((z==4)&&(glassworker==0)){
				glassworker++;
				total_bus++;
				w=1;
				}
			if((z==5)&&(moneylender==0)){
				moneylender++;
				total_bus++;
				w=1;
				}
			if((z==6)&&(textileseller==0)){
				textileseller++;
				total_bus++;
				w=1;
				}
			if((z==7)&&(carpenter==0)){
				carpenter++;
				total_bus++;
				w=1;
				}
			if((z==8)&&(artisan==0)){
				artisan++;
				total_bus++;
				w=1;
				}
		} /*end while loop*/
	  }

		z=(random(100)+1);
		if(z<=50){
			z=(random(10)+1);
			if(z==1) {
				mapmaker++;
				total_bus++;
				}
			if(z==2) {
				furrier++;
				total_bus++;
				}
			if(z==3) {
				temple++;
				total_temp++;
				}
			if(z==4){
				mercantileexchange++;
				total_gov++;
				}
			if(z==5){
                                apothecary++;
                                total_bus++;
				}
			if(z==6){
                                sculptor++;
                                total_bus++;
                                }
			if(z==7){
				bank++;
                                total_bus++;
                                }
                        if(z==8){
                                alchemist++;
                                total_bus++;
				}
			if(z==9){
				bookseller++;
                                total_bus++;
				}
			if(z==10){
				scribe++;
				total_bus++;
				}
		}

		if(FOREST==1){
			z=(random(100)+1);
			if(z<=20) y=8;
			if((z>20)&&(z<=40)) y=6;
			if((z>40)&&(z<=60)) y=4;
			if((z>60)&&(z<=80)) y=2;
			if(z>80) y=1;

			for(x=1;x<=y;x++){
				z=(random(2)+1);
				if(z<2){
					woodcutter++;
                                        total_bus++;
					}
                                if(z>1){
                                        foresttrapper=foresttrapper++;
                                        total_bus++;
					}
                        }
		}

		if(MOUNTAINS==1){

                        z=(random(100)+1);
			if(z<=30){
                                miner=miner+3;
				total_bus=total_bus+3;
				}
			if((z>30)&&(z<=60)){
                                miner=miner+2;
                                total_bus=total_bus+2;
                                }
			if(z>60){
                                miner++;
				total_bus++;
                                }
			z=(random(100)+1);
                        if(z<=50){
				quary=quary+2;
                                total_bus=total_bus+2;
                                }
                        if((z>50)&&(z<=95)){
				quary++;
				total_bus++;
				}
                        z=(random(100)+1);
                        if(z<=40){
				lapidary=lapidary+2;
				total_bus=total_bus+2;
                                }
			if((z>40)&&(z<=70)){
                                lapidary++;
				total_bus++;
                                }
			z=(random(100)+1);
                        if(z<=20){
				mountaintrapper=mountaintrapper+6;
                                total_bus=total_bus+6;
				}
			if((z>20)&&(z<=40)){
                                mountaintrapper=mountaintrapper+5;
                                total_bus=total_bus+5;
                                }
                        if((z>40)&&(z<=60)){
				mountaintrapper=mountaintrapper+4;
                                total_bus=total_bus+4;
                                }
			if((z>60)&&(z<=80)){
				mountaintrapper=mountaintrapper+2;
                                total_bus=total_bus+2;
                                }
                        if(z>80){
				mountaintrapper++;
				total_bus++;
				}
		}

		if(DESERT==1){
			z=(random(100)+1);
                        if(z<=40){
                                exoticanimalseller=exoticanimalseller+2;
                                total_bus=total_bus+2;
                                }
                        if((z>40)&&(z<=80)){
				exoticanimalseller++;
                                total_bus++;
                                }
		}

		if(PLAINS==1){
			z=(random(100)+1);
			if(z<=30){
				dairy=dairy+3;
                                total_bus=total_bus+3;
				}
                        if((z>30)&&(z<=60)){
                                dairy=dairy+2;
                                total_bus=total_bus+2;
				}
                        if(z>60){
				dairy++;
                                total_bus++;
                                }
                        z=(random(100)+1);
			if(z<=40){
                                grainery=grainery+2;
				total_bus=total_bus+2;
				}
			if((z>40)&&(z<=75)){
                                grainery++;
                                total_bus++;
                                }
			z=(random(100)+1);
                        if(z<=50){
				ropemaker++;
                                total_bus++;
				}

		}

		if(COASTAL==1){
                        z=(random(100)+1);
			if(z<=10) {
				fisherman=fisherman+8;
				total_bus=total_bus+8;
                                }
                        if((z>10)&&(z<=20)){
				fisherman=fisherman+7;
				total_bus=total_bus+7;
                                }
                        if((z>20)&&(z<=30)){
                                fisherman=fisherman+6;
				total_bus=total_bus+6;
                                }
			if((z>30)&&(z<=50)){
                                fisherman=fisherman+5;
                                total_bus=total_bus+5;
                                }
			if((z>50)&&(z<=60)){
				fisherman=fisherman+4;
                                total_bus=total_bus+4;
                                }
			if((z>60)&&(z<=70)){
                                fisherman=fisherman+3;
				total_bus=total_bus+3;
                                }
			if((z>70)&&(z<=90)){
				fisherman=fisherman+2;
				total_bus=total_bus+2;
                                }
                        if(z>90){
                                fisherman++;
				total_bus++;
                                }
		}
	z=(random(100)+1);
	if(z<=10) {
			park++;
			total_park++;
			}
	if(z>=98) {
			graveyard++;
			total_park++;
			}

	z=(random(100)+1);
	if(z<=50) {
			well++;
			total_misc++;
			}
	z=(random(100)+1);
	if(z<=30) {
			municipal++;
			total_gov++;
			}
	total_res=(total_bus+(int)(total_bus/3));

	resident=total_res;
total_all=total_res+total_bus+total_gov+total_temp+total_misc+total_park;

}


/**********TOWN*************/
void town(void)
{
int w,x,y,z,p;


	w=0;
	z=(random(5)+1);
	for(x=1;x<=z;x++){
	  w=0;
	  while(w==0){
		y=(random(15)+1);

		if((y==1)&&(blacksmith<2)){
			blacksmith++;
			total_bus++;
			w=1;
			}
		if((y==2)&&(generalstore<2)){
			generalstore++;
			total_bus++;
			w=1;
			}
		if((y==3)&&(butcher<2)){
			butcher++;
			total_bus++;
			w=1;
			}
		if((y==4)&&(inn<4)){
		      p=(random(2)+1);
		      if(p==1){
			inn++;
			total_bus++;
			}
		      if(p==2){
			inntavern++;
			total_bus++;
			}
			w=1;
		      }
		if((y==5)&&(tavern<4)){
			tavern++;
			total_bus++;
			w=1;
			}
		if((y==6)&&(tailor<2)){
			tailor++;
			total_bus++;
			w=1;
			}
		if((y==7)&&(shoemaker<2)){
			shoemaker++;
			total_bus++;
			w=1;
			}
		if((y==8)&&(stable<2)){
			stable++;
			total_bus++;
			w=1;
			}
		if((y==9)&&(weaponsmith<2)){
			weaponsmith++;
			total_bus++;
			w=1;
			}
		if((y==10)&&(baker<2)){
			baker++;
			total_bus++;
			w=1;
			}
		if((y==11)&&(mason<2)){
			mason++;
			total_bus++;
			w=1;
			}
		if((y==12)&&(leatherworker<2)){
			leatherworker++;
			total_bus++;
			w=1;
			}
		if((y==13)&&(potter<2)){
			potter++;
			total_bus++;
			w=1;
			}
		if((y==14)&&(woodcrafter<2)){
			woodcrafter++;
			total_bus++;
			w=1;
			}
		if((y==15)&&(artisan<2)){
			artisan++;
			total_bus++;
			w=1;
			}

		} /*end while loop*/
	  }

	  z=(random(100)+1);
	  if(z<=40) y=5;
	  if((z>40)&&(z<=70)) y=3;
	  if(z>70) y=2;

	  for(x=1;x<=y;x++){
		w=0;
		while(w==0){
			z=(random(8)+1);

			if((z==1)&&(restaurant<2)){
				restaurant++;
				total_bus++;
				w=1;
				}
			if((z==2)&&(armorer<2)){
				armorer++;
				total_bus++;
				w=1;
				}
			if((z==3)&&(locksmith<1)){
				locksmith++;
				total_bus++;
				w=1;
				}
			if((z==4)&&(glassworker<2)){
				glassworker++;
				total_bus++;
				w=1;
				}
			if((z==5)&&(moneylender<2)){
				moneylender++;
				total_bus++;
				w=1;
				}
			if((z==6)&&(textileseller<2)){
				textileseller++;
				total_bus++;
				w=1;
				}
			if((z==7)&&(carpenter<2)){
				carpenter++;
				total_bus++;
				w=1;
				}
			if((z==8)&&(scribe<2)){
				scribe++;
				total_bus++;
				w=1;
				}
		} /*end while loop*/
	  }

		z=(random(100)+1);
		if(z<=50){
			y=(random(2)+1);
		     for(x=1;x<=y;x++){
			z=(random(10)+1);

			if(z==1) {
				mapmaker++;
				total_bus++;
				}
			if(z==2) {
				furrier++;
				total_bus++;
				}
			if(z==3) {
				temple++;
				total_temp++;
				}
			if(z==4){
				mercantileexchange++;
				total_gov++;
				}
			if(z==5){
				apothecary++;
				total_bus++;
				}
			if(z==6){
				sculptor++;
				total_bus++;
				}
			if(z==7){
				bank++;
				total_bus++;
				}
			if(z==8){
				alchemist++;
				total_bus++;
				}
			if(z==9){
				bookseller++;
				total_bus++;
				}
			if(z==10){
				jeweler++;
				total_bus++;
				}
			}
		}

		if(FOREST==1){
			z=(random(100)+1);
			if(z<=20) y=10;
			if((z>20)&&(z<=40)) y=8;
			if((z>40)&&(z<=60)) y=6;
			if((z>60)&&(z<=80)) y=4;
			if(z>80) y=2;

			for(x=1;x<=y;x++){
				z=(random(2)+1);
				if(z<2){
					woodcutter++;
					total_bus++;
					}
				if(z>1){
					foresttrapper++;
					total_bus++;
					}
			}
		}

		if(MOUNTAINS==1){

			z=(random(100)+1);
			if(z<=30){
				miner=miner+6;
				total_bus=total_bus+6;
				}
			if((z>30)&&(z<=60)){
				miner=miner+4;
				total_bus=total_bus+4;
				}
			if(z>60){
				miner=miner+2;
				total_bus=total_bus+2;
				}
			z=(random(100)+1);
			if(z<=50){
				quary=quary+4;
				total_bus=total_bus+4;
				}
			if((z>50)&&(z<=95)){
				quary=quary+2;
				total_bus=total_bus+2;
				}
			z=(random(100)+1);
			if(z<=40){
				lapidary=lapidary+4;
				total_bus=total_bus+4;
				}
			if((z>40)&&(z<=70)){
				lapidary=lapidary+2;
				total_bus=lapidary+2;
				}
			z=(random(100)+1);
			if(z<=20){
				mountaintrapper=mountaintrapper+10;
				total_bus=total_bus+10;
				}
			if((z>20)&&(z<=40)){
				mountaintrapper=mountaintrapper+8;
				total_bus=total_bus+8;
				}
			if((z>40)&&(z<=60)){
				mountaintrapper=mountaintrapper+6;
				total_bus=total_bus+6;
				}
			if((z>60)&&(z<=80)){
				mountaintrapper=mountaintrapper+4;
				total_bus=total_bus+4;
				}
			if(z>80){
				mountaintrapper=mountaintrapper+2;
				total_bus=total_bus+2;
				}
		}

		if(DESERT==1){
			z=(random(100)+1);
			if(z<=40){
				exoticanimalseller=exoticanimalseller+4;
				total_bus=total_bus+4;
				}
			if((z>40)&&(z<=80)){
				exoticanimalseller=exoticanimalseller+2;
				total_bus=total_bus+2;
				}
		}

		if(PLAINS==1){
			z=(random(100)+1);
			if(z<=30){
				dairy=dairy+5;
				total_bus=total_bus+5;
				}
			if((z>30)&&(z<=60)){
				dairy=dairy+4;
				total_bus=total_bus+4;
				}
			if(z>60){
				dairy=dairy+2;
				total_bus=total_bus+2;
				}
			z=(random(100)+1);
			if(z<=40){
				grainery=grainery+4;
				total_bus=total_bus+4;
				}
			if((z>40)&&(z<=75)){
				grainery=grainery+2;
				total_bus=total_bus+2;
				}
			z=(random(100)+1);
			if(z<=50){
				ropemaker=ropemaker+2;
				total_bus=total_bus+2;
				}

		}

		if(COASTAL==1){
			z=(random(100)+1);
			if(z<=10) {
				fisherman=fisherman+12;
				total_bus=total_bus+12;
				}
			if((z>10)&&(z<=20)){
				fisherman=fisherman+10;
				total_bus=total_bus+10;
				}
			if((z>20)&&(z<=30)){
				fisherman=fisherman+8;
				total_bus=total_bus+8;
				}
			if((z>30)&&(z<=50)){
				fisherman=fisherman+6;
				total_bus=total_bus+6;
				}
			if((z>50)&&(z<=60)){
				fisherman=fisherman+5;
				total_bus=total_bus+5;
				}
			if((z>60)&&(z<=70)){
				fisherman=fisherman+4;
				total_bus=total_bus+4;
				}
			if((z>70)&&(z<=90)){
				fisherman=fisherman+3;
				total_bus=total_bus+3;
				}
			if(z>90){
				fisherman=fisherman+2;
				total_bus=total_bus+2;
				}
		}
	z=(random(100)+1);
	if(z<=10) {
			park=park+2;
			total_park=total_park+2;
			}
	if((z>10)&&(z<=25)){
			park++;
			total_park++;
			}
	if(z>=96)       {
			graveyard++;
			total_park=total_park+1;
			}
	z=(random(100)+1);
	if(z<=50) {
			well=well+2;
			total_misc=total_misc+2;
			}
	if((z>50)&&(z<=90)){
			well++;
			total_misc++;
			}
	z=(random(100)+1);
	if(z<=50){
			statue++;
			total_misc++;
			}
	z=(random(100)+1);
	if(z<=20){
			jail=1;
			total_gov++;
			}

	z=(random(100)+1);
	if(z<50) {
			municipal=1;
			total_gov++;
		}

	total_res=(total_bus+(int)(total_bus/3));

	resident=total_res;
total_all=total_res+total_bus+total_gov+total_temp+total_misc+total_park;
}


/**********SMALL CITY*********/
void smallcity()
{
int w,x,y,z,p;



	w=0;
	z=(random(10)+1);     /*SELECTS AMOUNT*/
	for(x=1;x<=z;x++){
	  w=0;
	  while(w==0){
		y=(random(16)+1);

		if((y==1)&&(blacksmith<4)){
			blacksmith++;
			total_bus++;
			w=1;
			}
		if((y==2)&&(generalstore<5)){
			generalstore++;
			total_bus++;
			w=1;
			}
		if((y==3)&&(butcher<4)){
			butcher++;
			total_bus++;
			w=1;
			}
		if((y==4)&&(inn<8)){
			p=(random(2)+1);
			if(p==1){
			   inn++;
			   total_bus++;
			}
			if(p==2){
			   inntavern++;
			   total_bus++;
			}
			w=1;
		     }
		if((y==5)&&(tavern<8)){
			tavern++;
			total_bus++;
			w=1;
			}
		if((y==6)&&(tailor<5)){
			tailor++;
			total_bus++;
			w=1;
			}
		if((y==7)&&(shoemaker<5)){
			shoemaker++;
			total_bus++;
			w=1;
			}
		if((y==8)&&(stable<5)){
			stable++;
			total_bus++;
			w=1;
			}
		if((y==9)&&(weaponsmith<5)){
			weaponsmith++;
			total_bus++;
			w=1;
			}
		if((y==10)&&(baker<5)){
			baker++;
			total_bus++;
			w=1;
			}
		if((y==11)&&(mason<5)){
			mason++;
			total_bus++;
			w=1;
			}
		if((y==12)&&(leatherworker<5)){
			leatherworker++;
			total_bus++;
			w=1;
			}
		if((y==13)&&(potter<5)){
			potter++;
			total_bus++;
			w=1;
			}
		if((y==14)&&(woodcrafter<5)){
			woodcrafter++;
			total_bus++;
			w=1;
			}
		if((y==15)&&(jeweler<3)){
			jeweler++;
			total_bus++;
			w=1;
			}
		if((y==16)&&(artisan<3)){
			artisan++;
			total_bus++;
			w=1;
			}
		} /*end while loop*/
	  }

	  z=(random(100)+1);
	  if(z<=40) y=10;
	  if((z>40)&&(z<=70)) y=7;
	  if(z>70) y=5;

	  for(x=1;x<=y;x++){
		w=0;
		while(w==0){
			z=(random(8)+1);

			if((z==1)&&(restaurant<8)){
				restaurant++;
				total_bus++;
				w=1;
				}
			if((z==2)&&(armorer<5)){
				armorer++;
				total_bus++;
				w=1;
				}
			if((z==3)&&(locksmith<3)){
				locksmith++;
				total_bus++;
				w=1;
				}
			if((z==4)&&(glassworker<5)){
				glassworker++;
				total_bus++;
				w=1;
				}
			if((z==5)&&(moneylender<4)){
				moneylender++;
				total_bus++;
				w=1;
				}
			if((z==6)&&(textileseller<5)){
				textileseller++;
				total_bus++;
				w=1;
				}
			if((z==7)&&(carpenter<6)){
				carpenter++;
				total_bus++;
				w=1;
				}
			if((z==8)&&(scribe<3)){
				scribe++;
				total_bus++;
				w=1;
				}
		} /*end while loop*/
	  }

		z=(random(100)+1);
		if(z<=75){
			y=(random(8)+1);
		     for(x=1;x<=y;x++){
			z=(random(9)+1);

			if(z==1) {
				mapmaker++;
				total_bus++;
				}
			if(z==2) {
				furrier++;
				total_bus++;
				}
			if(z==3) {
				temple++;
				total_temp++;
				}
			if(z==4){
				mercantileexchange++;
				total_gov++;
				}
			if(z==5){
				apothecary++;
				total_bus++;
				}
			if(z==6){
				sculptor++;
				total_bus++;
				}
			if(z==7){
				bank++;
				total_gov++;
				}
			if(z==8){
				alchemist++;
				total_bus++;
				}
			if(z==9){
				bookseller++;
				total_bus++;
				}
			}
		}

		if(FOREST==1){
			z=(random(100)+1);
			if(z<=20) y=20;
			if((z>20)&&(z<=40)) y=20;
			if((z>40)&&(z<=80)) y=15;
			if((z>80)&&(z<=90)) y=10;
			if(z>90) y=6;

			for(x=1;x<=y;x++){
				z=(random(2)+1);
				if(z<2){
					woodcutter++;
					total_bus++;
					}
				if(z>1){
					foresttrapper++;
					total_bus++;
					}
			}
			z=(random(100)+1);
			if(z<=20){
				forestersguild++;
				total_misc++;
				}
		}

		if(MOUNTAINS==1)
		     {
			z=(random(100)+1);
			if(z<=30){
				miner=miner+12;
				total_bus=total_bus+12;
				}
			if((z>30)&&(z<=80)){
				miner=miner+8;
				total_bus=total_bus+8;
				}
			if(z>80){
				miner=miner+4;
				total_bus=total_bus+4;
				}
			z=(random(100)+1);
			if(z<=70){
				quary=quary+6;
				total_bus=total_bus+6;
				}
			if((z>50)&&(z<=80)){
				quary=quary+4;
				total_bus=total_bus+4;
				}
			if(z>80){
				quary=quary+2;
				total_bus=total_bus+2;
				}
			z=(random(100)+1);
			if(z<=40){
				lapidary=lapidary+8;
				total_bus=total_bus+8;
				}
			if((z>40)&&(z<=70)){
				lapidary=lapidary+4;
				total_bus=total_bus+4;
				}
			if(z>70){
				lapidary=lapidary+2;
				total_bus=total_bus+2;
				}
			z=(random(100)+1);
			if(z<=20){
				mountaintrapper=mountaintrapper+15;
				total_bus=total_bus+15;
				}
			if((z>20)&&(z<=40)){
				mountaintrapper=mountaintrapper+10;
				total_bus=total_bus+10;
				}
			if((z>40)&&(z<=60)){
				mountaintrapper=mountaintrapper+8;
				total_bus=total_bus+8;
				}
			if((z>60)&&(z<=80)){
				mountaintrapper=mountaintrapper+6;
				total_bus=total_bus+6;
				}
			if(z>80){
				mountaintrapper=mountaintrapper+4;
				total_bus=total_bus+4;
				}
			z=(random(100)+1);
			if(z<=25){
				minersguild++;
				total_misc++;
				}

		}

		if(DESERT==1){
			z=(random(100)+1);
			if(z<=40){
				exoticanimalseller=exoticanimalseller+8;
				total_bus=total_bus+8;
				}
			if((z>40)&&(z<=80)){
				exoticanimalseller=exoticanimalseller+4;
				total_bus=total_bus+4;
				}
			if(z>80){
				exoticanimalseller=exoticanimalseller+2;
				total_bus=total_bus+2;
				}

		}

		if(PLAINS==1){
			z=(random(100)+1);
			if(z<=30){
				dairy=dairy+10;
				total_bus=total_bus+10;
				}
			if((z>30)&&(z<=60)){
				dairy=dairy+8;
				total_bus=total_bus+8;
				}
			if((z>60)&&(z<=90)){
				dairy=dairy+4;
				total_bus=total_bus+4;
				}
			if(z>90){
				dairy=dairy+2;
				total_bus=total_bus+2;
				}
			z=(random(100)+1);
			if(z<=40){
				grainery=grainery+8;
				total_bus=total_bus+8;
				}
			if((z>40)&&(z<=75)){
				grainery=grainery+4;
				total_bus=total_bus+4;
				}
			if(z>75){
				grainery=grainery+2;
				total_bus=total_bus+2;
				}

			z=(random(100)+1);
			if(z<=50){
				ropemaker=ropemaker+4;
				total_bus=total_bus+4;
				}
			z=(random(100)+1);
			if(z<=20){
				farmersguild++;
				total_misc++;
				}

		}

		if(COASTAL==1){
			z=(random(100)+1);
			if(z<=10) {
				fisherman=fisherman+25;
				total_bus=total_bus+25;
				}
			if((z>10)&&(z<=20)){
				fisherman=fisherman+20;
				total_bus=total_bus+20;
				}
			if((z>20)&&(z<=30)){
				fisherman=fisherman+15;
				total_bus=total_bus+15;
				}
			if((z>30)&&(z<=50)){
				fisherman=fisherman+10;
				total_bus=total_bus+10;
				}
			if((z>50)&&(z<=65)){
				fisherman=fisherman+8;
				total_bus=total_bus+8;
				}
			if((z>65)&&(z<=75)){
				fisherman=fisherman+6;
				total_bus=total_bus+6;
				}
			if((z>75)&&(z<=95)){
				fisherman=fisherman+4;
				total_bus=total_bus+4;
				}
			if(z>95){
				fisherman=fisherman+2;
				total_bus=total_bus+2;
				}
			z=(random(10)+1);
			for(x=1;x<=z;x++){
			     y=(random(4)+1);
			     if(y==1){
				shipmaker++;
				total_bus++;
				}
			     if(y==2){
				baitseller++;
				total_bus++;
				}
			     if(y==3){
				sailmaker++;
				total_bus++;
				}
			     if(y==4){
				if((sailmaker>0)||(shipmaker>0)){
					if(shipmastersguild<=0){
					shipmastersguild++;
					total_misc++;
					}
				}
			     }
			}
			     if(fisherman>0){
					if(fishermansguild<=0){
					fishermansguild++;
					total_misc++;
					}
			     }

		}
	z=(random(100)+1);
	if(z<=10) {
			park=park+4;
			total_park=total_park+4;
			}
	if((z>10)&&(z<=50)){
			park++;
			total_park++;
			}
	if(z>=95)       {
			graveyard++;
			total_park=total_park+1;
			}
	z=(random(100)+1);
	if(z<=50) {
			well=well+3;
			total_misc=total_misc+3;
			}
	if((z>50)&&(z<=90)){
			well=well+2;
			total_misc=total_misc+2;
			}
	if(z>90){
			well++;
			total_misc++;
			}


	z=(random(100)+1);
	if(z<=50){
			statue=statue+2;
			total_misc=total_misc+2;
			}
	if((z>50)&&(z<=80)){
			statue++;
			total_misc++;
			}

	z=(random(100)+1);
	if(z<=10){
		 thievesguild++;
		 total_misc++;
		 }
	z=(random(100)+1);
	if(z<<=20){
		merchantsguild++;
		total_misc++;
		}
	z=(random(100)+1);
	if(z<=3){
		assasinsguild++;
		total_misc++;
		}
	z=(random(100)+1);
	if((z<20)&&(moneylender>0)){
		moneylendersguild++;
		total_misc++;
		}
	if((alchemist>0)||(apothecary>0)){
		z=(random(100)+1);
		if(z<=20){
			wizardsguild++;
			total_misc++;
			}
	}
	z=(random(100)+1);
	if(z<20){
		manatarmsguild++;
		total_misc++;
		}

	z=(random(100)+1);
	if(z<20){
		artisansguild++;
		total_misc++;
		}

	z=(random(100)+1);
	if(z<=40){
		jail=1;
		total_gov++;
		}
	z=(random(100)+1);
	if(z<=20){
		library++;
		total_gov++;
		}
	z=(random(100)+1);
	if(z<=20){
		barracks++;
		total_gov++;
		}
	z=(random(100)+1);
	if(z<=90){
		municipal++;
		total_gov++;
		}
	z=(random(5)+1);
	warehouse=warehouse+z;
	total_bus++;

	total_res=(total_bus+(int)(total_bus/3));

	resident=total_res;
total_all=total_res+total_bus+total_gov+total_temp+total_misc+total_park;

}



/*********MEDIUM CITY*********/
void mediumcity()
{
int w,x,y,z,p;


	w=0;
	z=(random(20)+1);     /*SELECTS AMOUNT*/
	for(x=1;x<=z;x++){
	  w=0;
	  while(w==0){
		y=(random(16)+1);

		if((y==1)&&(blacksmith<6)){
			blacksmith++;
			total_bus++;
			w=1;
			}
		if((y==2)&&(generalstore<8)){
			generalstore++;
			total_bus++;
			w=1;
			}
		if((y==3)&&(butcher<6)){
			butcher++;
			total_bus++;
			w=1;
			}
		if((y==4)&&(inn<12)){
			p=(random(2)+1);
			if(p==1){
			   inn++;
			   total_bus++;
			}
			if(p==2){
			   inntavern++;
			   total_bus++;
			}
			w=1;
		      }
		if((y==5)&&(tavern<12)){
			tavern++;
			total_bus++;
			w=1;
			}
		if((y==6)&&(tailor<6)){
			tailor++;
			total_bus++;
			w=1;
			}
		if((y==7)&&(shoemaker<6)){
			shoemaker++;
			total_bus++;
			w=1;
			}
		if((y==8)&&(stable<8)){
			stable++;
			total_bus++;
			w=1;
			}
		if((y==9)&&(weaponsmith<6)){
			weaponsmith++;
			total_bus++;
			w=1;
			}
		if((y==10)&&(baker<8)){
			baker++;
			total_bus++;
			w=1;
			}
		if((y==11)&&(mason<6)){
			mason++;
			total_bus++;
			w=1;
			}
		if((y==12)&&(leatherworker<6)){
			leatherworker++;
			total_bus++;
			w=1;
			}
		if((y==13)&&(potter<6)){
			potter++;
			total_bus++;
			w=1;
			}
		if((y==14)&&(woodcrafter<8)){
			woodcrafter++;
			total_bus++;
			w=1;
			}
		if((y==15)&&(jeweler<4)){
			jeweler++;
			total_bus++;
			w=1;
			}
		if((y==16)&&(artisan<4)){
			artisan++;
			total_bus++;
			w=1;
			}
		} /*end while loop*/
	  }

	  z=(random(100)+1);
	  if(z<=40) y=20;
	  if((z>40)&&(z<=70)) y=14;
	  if(z>70) y=10;

	  for(x=1;x<=y;x++){
		w=0;
		while(w==0){
			z=(random(7)+1);

			if((z==1)&&(restaurant<16)){
				restaurant++;
				total_bus++;
				w=1;
				}
			if((z==2)&&(armorer<8)){
				armorer++;
				total_bus++;
				w=1;
				}
			if((z==3)&&(locksmith<5)){
				locksmith++;
				total_bus++;
				w=1;
				}
			if((z==4)&&(glassworker<8)){
				glassworker++;
				total_bus++;
				w=1;
				}
			if((z==5)&&(moneylender<8)){
				moneylender++;
				total_bus++;
				w=1;
				}
			if((z==6)&&(textileseller<10)){
				textileseller++;
				total_bus++;
				w=1;
				}
			if((z==7)&&(carpenter<12)){
				carpenter++;
				total_bus++;
				w=1;
				}
		} /*end while loop*/
	  }

		z=(random(100)+1);
		if(z<=75){
			y=(random(10)+1);
		     for(x=1;x<=y;x++){
			z=(random(10)+1);

			if(z==1) {
				mapmaker++;
				total_bus++;
				}
			if(z==2) {
				furrier++;
				total_bus++;
				}
			if(z==3) {
				temple++;
				total_temp++;
				}
			if(z==4){
				mercantileexchange++;
				total_gov++;
				}
			if(z==5){
				apothecary++;
				total_bus++;
				}
			if((z==6)&&(sculptor<=4)){
				sculptor++;
				total_bus++;
				}
			if((z==7)&&(bank<5)){
				bank++;
				total_gov++;
				}
			if(z==8){
				alchemist++;
				total_bus++;
				}
			if(z==9){
				bookseller++;
				total_bus++;
				}
			if((z==10)&&(scribe<3)){
				scribe++;
				total_bus++;
				}
			}
		}

		if(FOREST==1){
			z=(random(100)+1);
			if(z<=20) y=20;
			if((z>20)&&(z<=40)) y=40;
			if((z>40)&&(z<=80)) y=30;
			if((z>80)&&(z<=90)) y=20;
			if(z>90) y=10;

			for(x=1;x<=y;x++){
				z=(random(2)+1);
				if(z<2){
					woodcutter++;
					total_bus++;
					}
				if(z>1){
					foresttrapper++;
					total_bus++;
					}
			}
			z=(random(100)+1);
			if(z<=40){
				forestersguild++;
				total_misc++;
				}
		}

		if(MOUNTAINS==1)
		   {
			z=(random(100)+1);
			if(z<=30){
				miner=miner+24;
				total_bus=total_bus+24;
				}
			if((z>30)&&(z<=80)){
				miner=miner+16;
				total_bus=total_bus+16;
				}
			if(z>80){
				miner=miner+8;
				total_bus=total_bus+8;
				}
			z=(random(100)+1);
			if(z<=70){
				quary=quary+6;
				total_bus=total_bus+6;
				}
			if((z>50)&&(z<=80)){
				quary=quary+4;
				total_bus=total_bus+4;
				}
			if(z>80){
				quary=quary+2;
				total_bus=total_bus+2;
				}
			z=(random(100)+1);
			if(z<=40){
				lapidary=lapidary+12;
				total_bus=total_bus+12;
				}
			if((z>40)&&(z<=70)){
				lapidary=lapidary+8;
				total_bus=total_bus+8;
				}
			if(z>70){
				lapidary=lapidary+4;
				total_bus=total_bus+4;
				}
			z=(random(100)+1);
			if(z<=20){
				mountaintrapper=mountaintrapper+30;
				total_bus=total_bus+30;
				}
			if((z>20)&&(z<=40)){
				mountaintrapper=mountaintrapper+20;
				total_bus=total_bus+20;
				}
			if((z>40)&&(z<=60)){
				mountaintrapper=mountaintrapper+15;
				total_bus=total_bus+15;
				}
			if((z>60)&&(z<=80)){
				mountaintrapper=mountaintrapper+10;
				total_bus=total_bus+10;
				}
			if(z>80){
				mountaintrapper=mountaintrapper+8;
				total_bus=total_bus+8;
				}
			z=(random(100)+1);
			if(z<=40){
				minersguild++;
				total_misc++;
				}

		}

		if(DESERT==1){
			z=(random(100)+1);
			if(z<=40){
				exoticanimalseller=exoticanimalseller+12;
				total_bus=total_bus+12;
				}
			if((z>40)&&(z<=80)){
				exoticanimalseller=exoticanimalseller+8;
				total_bus=total_bus+8;
				}
			if(z>80){
				exoticanimalseller=exoticanimalseller+4;
				total_bus=total_bus+4;
				}

		}

		if(PLAINS==1){
			z=(random(100)+1);
			if(z<=30){
				dairy=dairy+20;
				total_bus=total_bus+20;
				}
			if((z>30)&&(z<=60)){
				dairy=dairy+15;
				total_bus=total_bus+15;
				}
			if((z>60)&&(z<=90)){
				dairy=dairy+10;
				total_bus=total_bus+10;
				}
			if(z>90){
				dairy=dairy+5;
				total_bus=total_bus+5;
				}
			z=(random(100)+1);
			if(z<=40){
				grainery=grainery+15;
				total_bus=total_bus+15;
				}
			if((z>40)&&(z<=75)){
				grainery=grainery+8;
				total_bus=total_bus+8;
				}
			if(z>75){
				grainery=grainery+4;
				total_bus=total_bus+4;
				}

			z=(random(100)+1);
			if(z<=50){
				ropemaker=ropemaker+6;
				total_bus=total_bus+6;
				}
			z=(random(100)+1);
			if(z<=40){
				farmersguild++;
				total_misc++;
				}

		}

		if(COASTAL==1){
			z=(random(100)+1);
			if(z<=10) {
				fisherman=fisherman+35;
				total_bus=total_bus+35;
				}
			if((z>10)&&(z<=20)){
				fisherman=fisherman+30;
				total_bus=total_bus+30;
				}
			if((z>20)&&(z<=30)){
				fisherman=fisherman+25;
				total_bus=total_bus+25;
				}
			if((z>30)&&(z<=50)){
				fisherman=fisherman+20;
				total_bus=total_bus+20;
				}
			if((z>50)&&(z<=65)){
				fisherman=fisherman+15;
				total_bus=total_bus+15;
				}
			if((z>65)&&(z<=75)){
				fisherman=fisherman+10;
				total_bus=total_bus+10;
				}
			if((z>75)&&(z<=95)){
				fisherman=fisherman+8;
				total_bus=total_bus+8;
				}
			if(z>95){
				fisherman=fisherman+4;
				total_bus=total_bus+4;
				}
			z=(random(15)+1);
			for(x=1;x<=z;x++){
			     y=(random(4)+1);
			     if(y==1){
				shipmaker++;
				total_bus++;
				}
			     if(y==2){
				baitseller++;
				total_bus++;
				}
			     if(y==3){
				sailmaker++;
				total_bus++;
				}
			     if(y==4){
				if((sailmaker>0)||(shipmaker>0)){
					if(shipmastersguild<=0){
					shipmastersguild++;
					total_misc++;
					}
				}
			     }
			}
			     if(fisherman>0){
					if(fishermansguild<=0){
					fishermansguild++;
					total_misc++;
					}
			     }

		}
	z=(random(100)+1);
	if(z<=10) {
			park=park+8;
			total_park=total_park+8;
			}
	if((z>10)&&(z<=50)){
			park=park+4;
			total_park=total_park+4;
			}
	if(z>=95)       {
			graveyard++;
			total_park=total_park+1;
			}
	z=(random(100)+1);
	if(z<=50) {
			well=well+6;
			total_misc=total_misc+6;
			}
	if((z>50)&&(z<=90)){
			well=well+4;
			total_misc=total_misc+4;
			}
	if(z>90){
			well=well+2;
			total_misc=total_misc+2;
			}

	z=(random(100)+1);
	if(z<=50){
			statue=statue+3;
			total_misc=total_misc+3;
			}
	if((z>50)&&(z<=80)){
			statue=statue+2;
			total_misc=total_misc+2;
			}
	if(z>80) {
			statue=0;
			}

	z=(random(100)+1);
	if(z<=15){
		 thievesguild++;
		 total_misc++;
		 }
	z=(random(100)+1);
	if(z<<=30){
		merchantsguild++;
		total_misc++;
		}
	z=(random(100)+1);
	if(z<=5){
		assasinsguild++;
		total_misc++;
		}
	z=(random(100)+1);
	if((z<30)&&(moneylender>0)){
		moneylendersguild++;
		total_misc++;
		}
	if((alchemist>0)||(apothecary>0)){
		z=(random(100)+1);
		if(z<=25){
			wizardsguild++;
			total_misc++;
			}
	}
	z=(random(100)+1);
	if(z<25){
		manatarmsguild++;
		total_misc++;
		}
	z=(random(100)+1);
	if(z<25){
		artisansguild++;
		total_misc++;
		}
	z=(random(100)+1);
	if(z<=60){
		jail=1;
		total_gov++;
		}
	z=(random(100)+1);
	if(z<=40){
		library++;
		total_gov++;
		}
	z=(random(100)+1);
	if(z<=50){
		barracks++;
		total_gov++;
		}
	if(z>50){
		barracks=barracks+2;
		total_gov=total_gov+2;
		}
	z=(random(3)+1);
		municipal=municipal+z;
		total_gov=total_gov+z;

	z=(random(20)+1);
	warehouse=warehouse+z;
	total_bus=total_bus+z;
	total_res=(total_bus+(int)(total_bus/3));

	resident=total_res;
total_all=total_res+total_bus+total_gov+total_temp+total_misc+total_park;

}

void forest1(void)
{
int w,x,y,z;

			z=(random(100)+1);
			if(z<=20) y=20;
			if((z>20)&&(z<=40)) y=40;
			if((z>40)&&(z<=80)) y=35;
			if((z>80)&&(z<=90)) y=30;
			if(z>90) y=20;

			for(x=1;x<=y;x++){
				z=(random(2)+1);
				if(z<2){
					woodcutter++;
					total_bus++;
					}
				if(z>1){
					foresttrapper++;
					total_bus++;
					}
			}
			z=(random(100)+1);
			if(z<=80){
				forestersguild++;
				total_misc++;
				}
return;
}

void mountains1(void)
{
int w,x,y,z;
			z=(random(100)+1);
			if(z<=30){
				miner=miner+40;
				total_bus=total_bus+40;
				}
			if((z>30)&&(z<=80)){
				miner=miner+30;
				total_bus=total_bus+30;
				}
			if(z>80){
				miner=miner+16;
				total_bus=total_bus+16;
				}
			z=(random(100)+1);
			if(z<=70){
				quary=quary+8;
				total_bus=total_bus+8;
				}
			if((z>50)&&(z<=80)){
				quary=quary+6;
				total_bus=total_bus+6;
				}
			if(z>80){
				quary=quary+4;
				total_bus=total_bus+4;
				}
			z=(random(100)+1);
			if(z<=40){
				lapidary=lapidary+15;
				total_bus=total_bus+15;
				}
			if((z>40)&&(z<=70)){
				lapidary=lapidary+10;
				total_bus=total_bus+10;
				}
			if(z>70){
				lapidary=lapidary+8;
				total_bus=total_bus+8;
				}
			z=(random(100)+1);
			if(z<=20){
				mountaintrapper=mountaintrapper+40;
				total_bus=total_bus+40;
				}
			if((z>20)&&(z<=40)){
				mountaintrapper=mountaintrapper+30;
				total_bus=total_bus+30;
				}
			if((z>40)&&(z<=60)){
				mountaintrapper=mountaintrapper+20;
				total_bus=total_bus+20;
				}
			if((z>60)&&(z<=80)){
				mountaintrapper=mountaintrapper+15;
				total_bus=total_bus+15;
				}
			if(z>80){
				mountaintrapper=mountaintrapper+10;
				total_bus=total_bus+10;
				}
			z=(random(100)+1);
			if(z<=80){
				minersguild++;
				total_misc++;
				}

}

void desert1(void)
{
int w,x,y,z;


			z=(random(100)+1);
			if(z<=40){
				exoticanimalseller=exoticanimalseller+16;
				total_bus=total_bus+16;
				}
			if((z>40)&&(z<=80)){
				exoticanimalseller=exoticanimalseller+12;
				total_bus=total_bus+12;
				}
			if(z>80){
				exoticanimalseller=exoticanimalseller+8;
				total_bus=total_bus+8;
				}
return;
}

void plains1(void)
{
int w,x,y,z;

			z=(random(100)+1);
			if(z<=30){
				dairy=dairy+30;
				total_bus=total_bus+30;
				}
			if((z>30)&&(z<=60)){
				dairy=dairy+20;
				total_bus=total_bus+20;
				}
			if((z>60)&&(z<=90)){
				dairy=dairy+15;
				total_bus=total_bus+15;
				}
			if(z>90){
				dairy=dairy+10;
				total_bus=total_bus+10;
				}
			z=(random(100)+1);
			if(z<=40){
				grainery=grainery+20;
				total_bus=total_bus+20;
				}
			if((z>40)&&(z<=75)){
				grainery=grainery+15;
				total_bus=total_bus+15;
				}
			if(z>75){
				grainery=grainery+8;
				total_bus=total_bus+8;
				}

			z=(random(100)+1);
			if(z<=50){
				ropemaker=ropemaker+8;
				total_bus=total_bus+8;
				}
			z=(random(100)+1);
			if(z<=80){
				farmersguild++;
				total_misc++;
				}
return;
}

void coast1(void)
{
int w,x,y,z;
			z=(random(100)+1);
			if(z<=10) {
				fisherman=fisherman+40;
				total_bus=total_bus+40;
				}
			if((z>10)&&(z<=20)){
				fisherman=fisherman+35;
				total_bus=total_bus+35;
				}
			if((z>20)&&(z<=30)){
				fisherman=fisherman+30;
				total_bus=total_bus+30;
				}
			if((z>30)&&(z<=50)){
				fisherman=fisherman+25;
				total_bus=total_bus+25;
				}
			if((z>50)&&(z<=65)){
				fisherman=fisherman+20;
				total_bus=total_bus+20;
				}
			if((z>65)&&(z<=75)){
				fisherman=fisherman+15;
				total_bus=total_bus+15;
				}
			if((z>75)&&(z<=95)){
				fisherman=fisherman+10;
				total_bus=total_bus+10;
				}
			if(z>95){
				fisherman=fisherman+8;
				total_bus=total_bus+8;
				}
			z=(random(20)+1);
			for(x=1;x<=z;x++){
			     y=(random(4)+1);
			     if(y==1){
				shipmaker++;
				total_bus++;
				}
			     if(y==2){
				baitseller++;
				total_bus++;
				}
			     if(y==3){
				sailmaker++;
				total_bus++;
				}
			     if(y==4){
				if((sailmaker>0)||(shipmaker>0)){
					if(shipmastersguild<=0){
					shipmastersguild++;
					total_misc++;
					}
				}
			     }
			}
			     if(fisherman>0){
					if(fishermansguild<=0){
					fishermansguild++;
					total_misc++;
					}
			     }


}
/********LARGE CITY**********/
void largecity(void)
{
int w,x,y,z,p;


	w=0;
	z=(random(40)+1);     /*SELECTS AMOUNT*/
	for(x=1;x<=z;x++){
	  w=0;
	  while(w==0){
		y=(random(16)+1);

		if((y==1)&&(blacksmith<12)){
			blacksmith++;
			total_bus++;
			w=1;
			}
		if((y==2)&&(generalstore<16)){
			generalstore++;
			total_bus++;
			w=1;
			}
		if((y==3)&&(butcher<10)){
			butcher++;
			total_bus++;
			w=1;
			}
		if((y==4)&&(inn<20)){
			p=(random(2)+1);
			if(p==1){
			     inn++;
			     total_bus++;
			}
			if(p==2){
			     inntavern++;
			     total_bus++;
			}
			w=1;
		     }
		if((y==5)&&(tavern<20)){
			tavern++;
			total_bus++;
			w=1;
			}
		if((y==6)&&(tailor<12)){
			tailor++;
			total_bus++;
			w=1;
			}
		if((y==7)&&(shoemaker<12)){
			shoemaker++;
			total_bus++;
			w=1;
			}
		if((y==8)&&(stable<20)){
			stable++;
			total_bus++;
			w=1;
			}
		if((y==9)&&(weaponsmith<10)){
			weaponsmith++;
			total_bus++;
			w=1;
			}
		if((y==10)&&(baker<16)){
			baker++;
			total_bus++;
			w=1;
			}
		if((y==11)&&(mason<12)){
			mason++;
			total_bus++;
			w=1;
			}
		if((y==12)&&(leatherworker<12)){
			leatherworker++;
			total_bus++;
			w=1;
			}
		if((y==13)&&(potter<12)){
			potter++;
			total_bus++;
			w=1;
			}
		if((y==14)&&(woodcrafter<16)){
			woodcrafter++;
			total_bus++;
			w=1;
			}
		if((y==15)&&(jeweler<6)){
			jeweler++;
			total_bus++;
			w=1;
			}
		if((y==16)&&(artisan<6)){
			artisan++;
			total_bus++;
			w=1;
			}
		} /*end while loop*/
	  }

	  z=(random(100)+1);
	  if(z<=40) y=40;
	  if((z>40)&&(z<=70)) y=30;
	  if(z>70) y=20;

	  for(x=1;x<=y;x++){
		w=0;
		while(w==0){
			z=(random(8)+1);

			if((z==1)&&(restaurant<25)){
				restaurant++;
				total_bus++;
				w=1;
				}
			if((z==2)&&(armorer<10)){
				armorer++;
				total_bus++;
				w=1;
				}
			if((z==3)&&(locksmith<5)){
				locksmith++;
				total_bus++;
				w=1;
				}
			if((z==4)&&(glassworker<12)){
				glassworker++;
				total_bus++;
				w=1;
				}
			if((z==5)&&(moneylender<10)){
				moneylender++;
				total_bus++;
				w=1;
				}
			if((z==6)&&(textileseller<15)){
				textileseller++;
				total_bus++;
				w=1;
				}
			if((z==7)&&(carpenter<20)){
				carpenter++;
				total_bus++;
				w=1;
				}

		}  /*end while loop*/
	  }

		z=(random(100)+1);
		if(z<=100){
			y=(random(30)+1);
		     for(x=1;x<=y;x++){
			z=(random(10)+1);

			if(z==1) {
				mapmaker++;
				total_bus++;
				}
			if(z==2) {
				furrier++;
				total_bus++;
				}
			if(z==3) {
				temple++;
				total_temp++;
				}
			if(z==4){
				mercantileexchange++;
				total_gov++;
				}
			if(z==5){
				apothecary++;
				total_bus++;
				}
			if((z==6)&&(sculptor<=6)){
				sculptor++;
				total_bus++;
				}
			if((z==7)&&(bank<7)){
				bank++;
				total_gov++;
				}
			if(z==8){
				alchemist++;
				total_bus++;
				}
			if(z==9){
				bookseller++;
				total_bus++;
				}
			if((z==10)&&(scribe<=4)){
				scribe++;
				total_bus++;
				}

			}
		}
 if(FOREST==1) forest1();
 if(MOUNTAINS==1) mountains1();
 if(DESERT==1) desert1();
 if(PLAINS==1) plains1();
 if(COASTAL==1) coast1();

	z=(random(100)+1);
	if(z<=10) {
			park=park+15;
			total_park=total_park+15;
			}
	if((z>10)&&(z<=50)){
			park=park+10;
			total_park=total_park+10;
			}
	if((z>50)&&(z<=80)){
			park=park+5;
			total_park=total_park+5;
			}
	if((z>80)&&(z<95)){
			park=park+3;
			total_park=total_park+3;
			}
	if(z>=95)       {
			graveyard++;
			total_park=total_park+1;
			}

	z=(random(100)+1);
	if(z<=50) {
			well=well+5;
			total_misc=total_misc+5;
			}
	if((z>50)&&(z<=90)){
			well=well+3;
			total_misc=total_misc+3;
			}
	if(z>90){
			well=well+2;
			total_misc=total_misc+2;
			}

	z=(random(100)+1);
	if(z<=50){
			statue=statue+5;
			total_misc=total_misc+5;
			}
	if((z>50)&&(z<=80)){
			statue=statue+3;
			total_misc=total_misc+3;
			}
	if((z>80)&&(z<95)){
			statue++;
			total_misc=total_misc++;
			}

	z=(random(100)+1);
	if(z<=20){
		 thievesguild++;
		 total_misc++;
		 }
	z=(random(100)+1);
	if(z<<=50){
		merchantsguild++;
		total_misc++;
		}
	z=(random(100)+1);
	if(z<=7){
		assasinsguild++;
		total_misc++;
		}
	z=(random(100)+1);
	if((z<40)&&(moneylender>0)){
		moneylendersguild++;
		total_misc++;
		}
	if((alchemist>0)||(apothecary>0)){
		z=(random(100)+1);
		if(z<=30){
			wizardsguild++;
			total_misc++;
			}
	}
	z=(random(100)+1);
	if(z<40){
		manatarmsguild++;
		total_misc++;
		}
	z=(random(100)+1);
	if(z<50){
		artisansguild++;
		total_misc++;
		}
	z=(random(3)+1);
	municipal=z;
	total_gov=total_gov+z;


	z=(random(100)+1);
	if(z<=80){
		jail=1;
		total_gov++;
		}
	z=(random(100)+1);
	if(z<=50){
		library++;
		total_gov++;
		}
	z=(random(100)+1);
	if(z<=60){
		barracks++;
		total_gov++;
		}
	if((z>60)&&(z<=80)){
		barracks=barracks+3;
		total_gov=total_gov+3;
		}
	z=(random(30)+1);
	warehouse=warehouse+z;
	total_bus=total_bus+z;
	total_res=(total_bus+(int)(total_bus/3));

	resident=total_res;
total_all=total_res+total_bus+total_gov+total_temp+total_misc+total_park;
}



/***********SAVE DATA***************/
void savecitydata(void)
{
  int x,y;

	fp=fopen("temp\\city.tmp","w");

	fprintf(fp,"%d\n",citysize);
	fprintf(fp,"%d\n",terrain);
	fprintf(fp,"%d\n",castle);
	fprintf(fp,"%d\n",keap);
	fprintf(fp,"%d\n",market);
	fprintf(fp,"%d\n",walls);

	fprintf(fp,"%d\n",total_bus);
	fprintf(fp,"%d\n",total_gov);
	fprintf(fp,"%d\n",total_temp);
	fprintf(fp,"%d\n",total_res);
	fprintf(fp,"%d\n",total_misc);
	fprintf(fp,"%d\n",total_park);
	fprintf(fp,"%d\n",total_all);

	fprintf(fp,"%d\n",scribe);
	fprintf(fp,"%d\n",municipal);
	fprintf(fp,"%d\n",artisan);
	fprintf(fp,"%d\n",blacksmith);
	fprintf(fp,"%d\n",inn);
	fprintf(fp,"%d\n",inntavern);
	fprintf(fp,"%d\n",tavern);
	fprintf(fp,"%d\n",tailor);
	fprintf(fp,"%d\n",baker);
	fprintf(fp,"%d\n",shoemaker);
	fprintf(fp,"%d\n",restaurant);
	fprintf(fp,"%d\n",stable);
	fprintf(fp,"%d\n",carpenter);
	fprintf(fp,"%d\n",locksmith);
	fprintf(fp,"%d\n",leatherworker);
	fprintf(fp,"%d\n",woodcrafter);
	fprintf(fp,"%d\n",glassworker);
	fprintf(fp,"%d\n",sculptor);
	fprintf(fp,"%d\n",mapmaker);
	fprintf(fp,"%d\n",potter);
	fprintf(fp,"%d\n",furrier);
	fprintf(fp,"%d\n",jeweler);
	fprintf(fp,"%d\n",mason);
	fprintf(fp,"%d\n",generalstore);
	fprintf(fp,"%d\n",temple);
	fprintf(fp,"%d\n",weaponsmith);
	fprintf(fp,"%d\n",armorer);
	fprintf(fp,"%d\n",moneylender);
	fprintf(fp,"%d\n",bank);
	fprintf(fp,"%d\n",alchemist);
	fprintf(fp,"%d\n",apothecary);
	fprintf(fp,"%d\n",textileseller);
	fprintf(fp,"%d\n",butcher);
	fprintf(fp,"%d\n",mercantileexchange);
	fprintf(fp,"%d\n",jail);
	fprintf(fp,"%d\n",well);
	fprintf(fp,"%d\n",statue);
	fprintf(fp,"%d\n",resident);
	fprintf(fp,"%d\n",barracks);
	fprintf(fp,"%d\n",warehouse);
	fprintf(fp,"%d\n",library);
	fprintf(fp,"%d\n",bookseller);
	fprintf(fp,"%d\n",park);
	fprintf(fp,"%d\n",graveyard);

	fprintf(fp,"%d\n",dairy);
	fprintf(fp,"%d\n",grainery);
	fprintf(fp,"%d\n",ropemaker);
	fprintf(fp,"%d\n",farmersguild);

	fprintf(fp,"%d\n",miner);
	fprintf(fp,"%d\n",lapidary);
	fprintf(fp,"%d\n",mountaintrapper);
	fprintf(fp,"%d\n",quary);
	fprintf(fp,"%d\n",minersguild);

	fprintf(fp,"%d\n",exoticanimalseller);

	fprintf(fp,"%d\n",foresttrapper);
	fprintf(fp,"%d\n",woodcutter);
	fprintf(fp,"%d\n",forestersguild);

	fprintf(fp,"%d\n",fisherman);
	fprintf(fp,"%d\n",shipmaker);
	fprintf(fp,"%d\n",baitseller);
	fprintf(fp,"%d\n",sailmaker);
	fprintf(fp,"%d\n",shipmastersguild);
	fprintf(fp,"%d\n",fishermansguild);

	fprintf(fp,"%d\n",thievesguild);
	fprintf(fp,"%d\n",merchantsguild);
	fprintf(fp,"%d\n",manatarmsguild);
	fprintf(fp,"%d\n",assasinsguild);
	fprintf(fp,"%d\n",wizardsguild);
	fprintf(fp,"%d\n",moneylendersguild);
	fprintf(fp,"%d\n",artisansguild);

	fclose(fp);
			    /*write array*/
	fp=fopen("temp\\citym.tmp","wb");
	  for(x=0;x<=40;x++){
	    for(y=0;y<=25;y++){
	       fwrite(&MAP[x][y],sizeof(int),1,fp);
	    }
	  }
	fclose(fp);
}

void resetcitydata()
{
total_bus=0;
total_gov=0;
total_temp=0;
total_res=0;
total_misc=0;
total_park=0;
total_all=0;



/***general*****/

scribe=0;
municipal=0;
artisan=0;
blacksmith=0;
inn=0;
inntavern=0;
tavern=0;
tailor=0;
baker=0;
shoemaker=0;
restaurant=0;
stable=0;
carpenter=0;
locksmith=0;
leatherworker=0;
woodcrafter=0;
glassworker=0;
sculptor=0;
mapmaker=0;
potter=0;
furrier=0;
jeweler=0;
mason=0;
generalstore=0;
temple=0;
weaponsmith=0;
armorer=0;
moneylender=0;
bank=0;
alchemist=0;
apothecary=0;
textileseller=0;
butcher=0;
mercantileexchange=0;
jail=0;
well=0;
statue=0;
resident=0;
barracks=0;
warehouse=0;
library=0;
bookseller=0;
park=0;
graveyard=0;

/*PLAINS*/
dairy=0;
grainery=0;
ropemaker=0;
farmersguild=0;

/*MOUNTAINS*/
miner=0;
lapidary=0;
mountaintrapper=0;
quary=0;
minersguild=0;

/*DESERT*/
exoticanimalseller=0;

/*FOREST*/
foresttrapper=0;
woodcutter=0;
forestersguild=0;

/*COASTAL*/
fisherman=0;
shipmaker=0;
baitseller=0;
sailmaker=0;
shipmastersguild=0;
fishermansguild=0;

/*GUILDS*/
thievesguild=0;
merchantsguild=0;
manatarmsguild=0;
assasinsguild=0;
wizardsguild=0;
moneylendersguild=0;
artisansguild=0;
}


void makewalls(void)         /*create walls around city*/
{
int x,y,c;
	wsetcolor(60);
	for(y=1;y<=25;y++){         /*left to right*/
		for(x=1;x<=40;x++){
			c=wgetpixel(x,y);
			if(c==40){
				c=wgetpixel((x+1),y);
				if((c!=40)&&(c!=60)){
				   if((x+1)<=40) {
						wputpixel(x,y);
						}
				}
			}
			c=0;
		}
	}
	for(y=1;y<=25;y++){      /*right to left*/
		for(x=40;x>=1;x--){
			c=wgetpixel(x,y);
			if(c==40){
				c=wgetpixel(x-1,y);
				if((c!=40)&&(c!=60)) {
					if((x-1)>=0) wputpixel(x,y);
				}
			c=0;
			}
		}
	}

	for(x=1;x<=40;x++){        /*top to bottom*/
		for(y=1;y<=25;y++){
			c=wgetpixel(x,y);
			if(c==40){
				c=wgetpixel(x,y+1);
				if((c!=40)&&(c!=60)) {
					if((y+1)<=25) wputpixel(x,y);
				}
			c=0;
			}
		}
	}

	for(x=1;x<=40;x++){        /*bottom to top*/
		for(y=25;y>=1;y--){
			c=wgetpixel(x,y);
			if(c==40){
				c=wgetpixel(x,y-1);
				if((c!=40)&&(c!=60)) {
					if((y-1)>=0) wputpixel(x,y);
				}
			c=0;
			}
		}
	}

}

void placebuildingtypes(void)
{
/*cityground=color 20*/
int x,y,c;
int b_type; /*building type (random)*/


int stp;  /*stop while loop*/
int tb;   /*total business*/
int tg;   /*total government*/
int tt;   /*total temples*/
int tr;   /*total residential*/
int tm;   /*total misc*/
int tp;   /*total parks*/
int ta;   /*total all*/

tb=total_bus;
tg=total_gov;
tt=total_temp;
tr=total_res;
tm=total_misc;
tp=total_park;
ta=total_all;
stp=0;

while(ta>0){
   x=(random(40)+1);
   y=(random(25)+1);


      c=wgetpixel(x,y);
	 if(c==20){
	 /*pick type*/
	    stp=0;
	    while(stp==0){
	       b_type=(random(7)+1);
	       switch(b_type){
		 case 1: {    /*business*/
			   if(tb>0){
				wsetcolor(13);
				wputpixel(x,y);
				tb=tb-1;
				ta=ta-1;
				stp=1;
			     }
			     break;
			   }

		 case 2:  {  /*government*/
			    if(tg>0){
				wsetcolor(24);
				wputpixel(x,y);
				tg=tg-1;
				ta=ta-1;
				stp=1;
			    }
			    break;
			   }

		 case 3:   {  /*temple*/
			     if(tt>0){
				wsetcolor(35);
				wputpixel(x,y);
				tt=tt-1;
				ta=ta-1;
				stp=1;
			     }
			     break;
			    }

		 case 4:    {  /*resident*/
			      if(tr>0){
				wsetcolor(46);
				wputpixel(x,y);
				tr=tr-1;
				ta=ta-1;
				stp=1;
			      }
			      break;
			     }

		 case 5:    {  /*misc*/
			       if(tm>0){
				 wsetcolor(57);
				 wputpixel(x,y);
				 tm=tm-1;
				 ta=ta-1;
				 stp=1;
			       }
			       break;
			     }

		 case 6:     {  /*park*/
				if(tp>0){
				  wsetcolor(68);
				  wputpixel(x,y);
				  tp=tp-1;
				  ta=ta-1;
				  stp=1;
				}
				break;
			      }
		 case 7:      {stp=1;
			       break;
			       }
		 default: break;
		}
	       } /*end while loop*/
	    } /*end if c==20*/
  } /*while ta>0*/
  /*fill roads*/
  wsetcolor(30);  /*road color*/
  for(x=1;x<=40;x++){
     for(y=1;y<=25;y++){
	c=wgetpixel(x,y);
	if(c==20) wputpixel(x,y);
     }
  }
  /*create map data in MAP array*/
  for(x=0;x<=40;x++){
     for(y=0;y<=25;y++){
	MAP[x][y]=0;
     }
  }
  /*place colors into MAP array*/
  for(x=0;x<=40;x++){
     for(y=0;y<=25;y++){
	c=wgetpixel(x,y);
	if(c!=40) MAP[x][y]=c;
     }
  }
}


void createcity(void)
{
int stop_it,stop_poly;
int kastle;  /*returns if no good (-1) for castle*/
int mrket; /*returns if no good (-1) for market*/
int keeep; /*returns if no good (-1) for keep*/
stop_it=0;
stop_poly=0;


if(citysize==1) {max_x=7;max_y=7;xstart=14;ystart=8;}
if(citysize==2) {max_x=8;max_y=8;xstart=12;ystart=7;}
if(citysize==3) {max_x=12;max_y=12;xstart=7;ystart=5;}
if(citysize==4) {max_x=22;max_y=19;xstart=3;ystart=3;}
if(citysize==5) {max_x=35;max_y=22;xstart=2;ystart=1;}
if(citysize==6) {max_x=39;max_y=23;xstart=1;ystart=1;}

 while(stop_it==0){                   /*create city data*/
	resetcitydata();
	if(citysize==1) hamlet();
	if(citysize==2) village();
	if(citysize==3) town();
	if(citysize==4) smallcity();
	if(citysize==5) mediumcity();
	if(citysize==6) largecity();
  stop_poly=0;
  while(stop_poly==0){
   wcls(0);
   make_object();
	while(stopp==0){
		num_sides = random(8)+1;
		if(num_sides>4) stopp=1;
	}
   wsetcolor(60);
   whollowpoly(plainpoly, num_sides, 0, 0, CLOSED_POLY);
   fillit();
      numspot=countspot();
   if(citysize==1){     /*hamlet*/
			if(numspot<=20) stop_poly=1;
     }
   if(citysize==2){     /*village*/
			if((numspot>20)&&(numspot<=64)) stop_poly=1;
     }
   if(citysize==3){     /*town*/
			if((numspot>64)&&(numspot<=150)) stop_poly=1;
     }
   if(citysize==4){     /*small city*/
			if((numspot>150)&&(numspot<=300)) stop_poly=1;
     }
   if(citysize==5){     /*medium city*/
			if((numspot>300)&&(numspot<=400)) stop_poly=1;
     }
   if(citysize==6){     /*large city*/
			if(numspot>400) stop_poly=1;
     }
   } /*end while stop poly*/
 roads(citysize);
 if(market==1) mrket=placemarket();
 if(castle==1) kastle=placecastle();
 if(keap==1)   keeep=placekeep();
 if(walls==1) makewalls();
   numspot=countspot();
   if(citysize==1){     /*hamlet*/
			if(numspot<=20) stop_it=1;
     }
   if(citysize==2){     /*village*/
			if((numspot>20)&&(numspot<=64)) stop_it=1;
     }
   if(citysize==3){     /*town*/
			if((numspot>64)&&(numspot<=150)) stop_it=1;
     }
   if(citysize==4){     /*small city*/
			if((numspot>150)&&(numspot<=300)) stop_it=1;
     }
   if(citysize==5){     /*medium city*/
			if((numspot>300)&&(numspot<=400)) stop_it=1;
     }
   if(citysize==6){     /*large city*/
			if(numspot>400) stop_it=1;
     }
     if(kastle<0) stop_it=0;
     if(mrket<0) stop_it=0;
     if(keeep<0) stop_it=0;
  numspot=countspot();
     if(total_all>numspot) stop_it=0; /*if more buildings than spots*/

  }   /*end while stop_it==0 */

	placebuildingtypes();

	savecitydata();


}




void main(void)
{
/*color 20=city ground*/
/*color 40=outside city*/
/*color 30=roads*/
/*color 50=castle*/
/*color 70=market*/
/*color 80=keep*/
/*color 60=walls*/
/*color 13=business*/
/*color 24=government*/
/*color 35=temple*/
/*color 46=residential*/
/*color 57=misc*/
/*color 68=park*/
FOREST=0;
MOUNTAINS=0;
DESERT=0;
PLAINS=0;
COASTAL=0;

randomize();
stopp=0;
vga256();


	wreadpalette(0,255,virtpalette);
	wcls(0);
	wtexttransparent(0);
	wtextcolor(15);

	wouttextxy(110,100,NULL,"Making City...");

resetcitydata();
	fp=fopen("temp\\cdata.tmp","r");   /*read city data params*/
	fscanf(fp,"%d",&citysize);
	fscanf(fp,"%d",&terrain);
	fscanf(fp,"%d",&castle);
	fscanf(fp,"%d",&keap);
	fscanf(fp,"%d",&market);
	fscanf(fp,"%d",&walls);
	fclose(fp);

if(terrain==1) FOREST=1;
if(terrain==2) MOUNTAINS=1;
if(terrain==3) DESERT=1;
if(terrain==4) PLAINS=1;
if(terrain==5) COASTAL=1;


	wtextcolor(0);
	virtscreen=wnewblock(0,0,319,199);
	wsetscreen(virtscreen);
	wsetpalette(0,255,virtpalette);
createcity();   /*make map*/
	wcls(0);
	wnormscreen();

wfreeblock(virtscreen);

execv("citysav",NULL);
exit(0);
}

