/*
Area generator for Randian.

Created by Liko(Eddie Pierce) 3/17/2019
*/


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


void main()
{
	char area_name[100], builders_name[100], answer;
	int ZON_NUMBER, WIDTH, HEIGHT, VNUM_START;
	int vnum, n, e, s, w, line_pos;

	printf("What is the AREA NAME of the new area to be generated? ");
	scanf("%99s", area_name); //Area Name

	printf("What is the world number of %s? ", area_name);
	scanf("%d", &ZON_NUMBER); //Zone Number

	printf("Who is building %s? ", area_name);
	scanf("%99s", builders_name); //Who is building the area.  Note:  If you use a space it will not work.  For multiple builders just use a comma and as one world.  Example: Liko,Liko2

	printf("%s starting VNUM? ", area_name);
	scanf("%d", &VNUM_START); //Starting Vnum of the Area

	printf("What is the WIDTH of the area? ");
	scanf("%d", &WIDTH); //Width of the area

	printf("What is the HEIGHT of the area? ");
	scanf("%d", &HEIGHT); //Height of the area

	printf("\n\n\n"); //Spacing
	printf("Area Name: %s\n", area_name);
	printf("World Number: %d\n", ZON_NUMBER);
	printf("Builders: %s\n", builders_name);
	printf("Width: %d\n", WIDTH);
	printf("Height: %d\n", HEIGHT);
        printf("Are These variables correct? (Y/N)\n ");
	scanf(" %c", &answer); //Making Sure all the variables you inputed are correct and what you want.
	if(answer== 'Y' || answer == 'y') {
		int VNUM_END = WIDTH*HEIGHT+(VNUM_START-1);
		chdir("wld");//Go to the wld directory
		char wld_buf[0x100];
		snprintf(wld_buf, sizeof(wld_buf), "%d.wld", ZON_NUMBER);
		FILE *fp = fopen(wld_buf, "w"); //create a number file in the wld folder
		printf("################################\n");
		printf("# Generateing area %3d by %3d   #\n", WIDTH, HEIGHT);
		printf("# Generating %5d rooms          #\n", VNUM_END);
		printf("################################\n");

		for(vnum = VNUM_START; vnum <=VNUM_END; vnum++)
		{
			n=vnum-WIDTH;
			s=vnum+WIDTH;
			e=vnum+1;
			w=vnum-1;

			/*Where is it on line 0.*/
			line_pos=(vnum-VNUM_START+1)%(WIDTH);
			if(line_pos==0) {
				line_pos=WIDTH;
			}

			/*North Border*/
			if ( (vnum >= VNUM_START) && (vnum < VNUM_START+WIDTH) )
			{
				n=(VNUM_END-WIDTH)+(line_pos);
			}
			/*South Border*/
			if ( (vnum > VNUM_END-WIDTH) && (vnum <= VNUM_END) ) {
				s=(VNUM_START)+(line_pos-1);
			}
			/*East Border*/
			if ( (vnum-VNUM_START+1)%(WIDTH)==0 ) {
				e=vnum-WIDTH+1;
			}
			/*West Border*/
			if ( (vnum-VNUM_START+1)%(WIDTH)==1 ) {
				w=vnum+WIDTH-1;
			}



			fprintf(fp, "#%d\n", vnum); /*Rooms Vnum*/
			fprintf(fp, "%s %d~\n", area_name, vnum); /*Rooms Name and Vnum*/
			fprintf(fp, "Blank Room Desc.\n~\n"); /*Rooms Desc*/
			fprintf(fp, "0 0 0 0 0 1\n"); /*Zone Number, Room Flag, Room Flag, Room Flag, Room Flag, Room Sector*/
			fprintf(fp, "D0\n~\n~\n0 0 %d\n", n); /*North Exit*/
			fprintf(fp, "D1\n~\n~\n0 0 %d\n", e); /* East Exit */
			fprintf(fp, "D2\n~\n~\n0 0 %d\n", s); /* South Exit*/
			fprintf(fp, "D3\n~\n~\n0 0 %d\n", w); /* West Exit*/
			fprintf(fp, "S\n");
		}
		fprintf(fp, "$\n");
		fclose(fp); //close wld file
		chdir(".."); //move back to the world folder

                chdir("zon"); //change to the zon folder
                char zon_buf[0x100];
                snprintf(zon_buf, sizeof(zon_buf), "%d.zon", ZON_NUMBER);
                FILE *fp2 = fopen(zon_buf, "w"); //make a new .zon file in the zon
		fprintf(fp2, "#%d\n%s~\n%s~\n%d %d 30 2 g 0 0 0 -1 -1\nS\n$\n",ZON_NUMBER, builders_name, area_name, VNUM_START, VNUM_END);
 		fclose(fp2); //close the zon file
		chdir(".."); //move back to the world folder

		chdir("mob"); //move to mob folder
		char mob_buf[0x100];
		snprintf(mob_buf, sizeof(mob_buf), "%d.mob", ZON_NUMBER);
		FILE *fp3 = fopen(mob_buf, "w"); //make the new .mob file in the mob
        	fprintf(fp3, "$\n");
        	fclose(fp3); //close mob file
		chdir(".."); //move back to the world folder

		chdir("obj"); //move to the obj folder
		char obj_buf[0x100];
		snprintf(obj_buf, sizeof(obj_buf), "%d.obj", ZON_NUMBER);
		FILE *fp4 = fopen(obj_buf, "w"); //make the new .obj file in obj
		fprintf(fp4, "$\n");
		fclose(fp4); //close .obj file
		chdir(".."); //move back to world folder

		chdir("shp"); //move to the shp folder
		char shp_buf[0x100];
		snprintf(shp_buf, sizeof(shp_buf), "%d.shp", ZON_NUMBER);
		FILE *fp5 = fopen(shp_buf, "w"); //make the new .shp file in shp
		fprintf(fp5, "$~\n");
		fclose(fp5); //close the .shp file
		chdir("..");//move back to world folder

		chdir("qst"); //move to the qst file
		char qst_buf[0x100];
		snprintf(qst_buf, sizeof(qst_buf), "%d.qst", ZON_NUMBER);
		FILE *fp6 = fopen(qst_buf, "w"); //makes the new .qst file
		fprintf(fp6, "$~\n");
		fclose(fp6); //close the .qst file
		chdir("..");//move back to the world folder

		chdir("trg"); //move to the trg folder
		char trg_buf[0x100];
		snprintf(trg_buf, sizeof(trg_buf), "%d.trg", ZON_NUMBER);
		FILE *fp7 = fopen(trg_buf, "w"); //makes the new .trg file
		fprintf(fp7, "$~\n");
		fclose(fp7); //close the .trg file
		chdir(".."); //and finally back to the world folder

	} else {
		return;
	}
}