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

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];
static int TMAP[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;


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

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

	fscanf(fp,"%d",&citysize);
	fscanf(fp,"%d",&terrain);
	fscanf(fp,"%d",&castle);
	fscanf(fp,"%d",&keap);
	fscanf(fp,"%d",&market);
	fscanf(fp,"%d",&walls);

	fscanf(fp,"%d",&total_bus);
	fscanf(fp,"%d",&total_gov);
	fscanf(fp,"%d",&total_temp);
	fscanf(fp,"%d",&total_res);
	fscanf(fp,"%d",&total_misc);
	fscanf(fp,"%d",&total_park);
	fscanf(fp,"%d",&total_all);

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

	fscanf(fp,"%d",&dairy);
	fscanf(fp,"%d",&grainery);
	fscanf(fp,"%d",&ropemaker);
	fscanf(fp,"%d",&farmersguild);

	fscanf(fp,"%d",&miner);
	fscanf(fp,"%d",&lapidary);
	fscanf(fp,"%d",&mountaintrapper);
	fscanf(fp,"%d",&quary);
	fscanf(fp,"%d",&minersguild);

	fscanf(fp,"%d",&exoticanimalseller);

	fscanf(fp,"%d",&foresttrapper);
	fscanf(fp,"%d",&woodcutter);
	fscanf(fp,"%d",&forestersguild);

	fscanf(fp,"%d",&fisherman);
	fscanf(fp,"%d",&shipmaker);
	fscanf(fp,"%d",&baitseller);
	fscanf(fp,"%d",&sailmaker);
	fscanf(fp,"%d",&shipmastersguild);
	fscanf(fp,"%d",&fishermansguild);

	fscanf(fp,"%d",&thievesguild);
	fscanf(fp,"%d",&merchantsguild);
	fscanf(fp,"%d",&manatarmsguild);
	fscanf(fp,"%d",&assasinsguild);
	fscanf(fp,"%d",&wizardsguild);
	fscanf(fp,"%d",&moneylendersguild);
	fscanf(fp,"%d",&artisansguild);

	fclose(fp);
			    /*write array*/

}


void main(void)
{
int x,y;

readcitydata();
	fp=fopen("temp\\city.tst","w");
	fprintf(fp,"citysize=%d\n",citysize);
	fprintf(fp,"terrain=%d\n",terrain);
	fprintf(fp,"castle=%d\n",castle);
	fprintf(fp,"keep=%d\n",keap);
	fprintf(fp,"market=%d\n",market);
	fprintf(fp,"walls=%d\n",walls);

	fprintf(fp,"totalbus=%d\n",total_bus);
	fprintf(fp,"totalgov=%d\n",total_gov);
	fprintf(fp,"totaltemple=%d\n",total_temp);
	fprintf(fp,"totalres=%d\n",total_res);
	fprintf(fp,"totalmisc=%d\n",total_misc);
	fprintf(fp,"totalpark=%d\n",total_park);
	fprintf(fp,"totalall=%d\n",total_all);

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

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

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

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

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

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

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


	fclose(fp);


			    /*write array*/
	fp=fopen("temp\\citym.tmp","rb");
	  for(x=0;x<=40;x++){
	    for(y=0;y<=25;y++){
	       fread(&TMAP[x][y],sizeof(int),1,fp);
	    }
	  }
	fclose(fp);
	  for(x=0;x<=40;x++){
	    for(y=0;y<=25;y++){
		if(TMAP[x][y]==1){
				sound(440);
				delay(200);
				nosound();
		}
	    }
	  }

}
