/* Get C-Evo map sizes */

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

int isprime(int a) {
	int b;
	for(b=2;b * b <= a;b++) {
		if(a % b == 0) {
			return 0;
		}
	}
	return 1;
}

int main(int argc, char **argv) {
	FILE *cevo, *cevohack = 0;
	int a,b;
	int offset;
	int width[6];
	int height[6];
	int size[6] = {35,50,70,100,150,230};
	int newwidth;
	int newheight;
	int tochange;
	cevo = fopen("Cevo.exe","rb");
	if(argc != 1 && argc != 4) {
		printf("Map sizes: A utility to change the maps sizes of\n"
		"C-Evo 1.1.1.  This program needs to be run in the same\n"
		"directory as C-Evo 1.1.1's Cevo.exe, and creates a file\n"
		"named Cevo-hack.exe with different map sizes.\n\n"
	"To use, type in map-sizes {map size} {new width} {new height}\n"
	"{map size} is a number from 0 to 5, 0 is 35%%, 1 is 50%%, 2 is 70%%,\n"
	"3 is 100%%, 4 is 150%%, and 5 is 230%%\n\n"
	"{width} and {height} are the desires map size.  Width * height + 1\n"	
	"must be prime.  Some map sizes that work are:\n\n"
	"20x20 20x21 21x22 24x24 24x25 26x26 27x28 33x34 36x36 38x39 40x40\n"
        "41x42 50x51 54x54 54x55 56x56 57x58 59x60 62x63 66x66 66x67 69x70\n"
        "71x72 74x74 75x76 77x78 78x79 80x81 84x84 89x90 90x90 90x91 94x94\n\n"
	"If run without arguments, this program reports the map sizes inside\n"
	"the Cevo executable\n");
	exit(0);
	}
	if(argc == 4) {
		cevohack = fopen("Cevo-hack.exe","wb");
		tochange = atoi(argv[1]);
		newwidth = atoi(argv[2]);
		newheight = atoi(argv[3]);
		if(newwidth < 20 || newwidth > 100) {
			printf("width out of range\n");
			exit(3);
		}
		if(newheight < 20 || newheight > 100) {
			printf("height out of range\n");
			exit(3);
		}
		if(newwidth * newheight > 9600) {
			printf("Map too big for random map maker\n");
			exit(3);
		}
		if(tochange < 0 || tochange > 5) {
			printf("Map size to change out of range!\n");
			exit(3);
		}
		if(isprime(newwidth * newheight + 1) != 1) {
			printf("width * height + 1 must be prime!\n");
			exit(4);
		}
		printf("Changing size %d to %dx%d\n",tochange,newwidth,
			newheight);
	}
	if(cevo == 0) {
		printf("Could not find Cevo.exe!\n");
		exit(1);
	}
	for(a=0;a<0xc70e8;a++) {
		b = getc(cevo);	
		if(feof(cevo)) {
			printf("This is too small!\n");
			exit(2);
		}
		if(cevohack != 0) {
			putc(b,cevohack);
		}
	}
	for(a=0;a<6;a++) {
		b = getc(cevo);
		width[a] = b;	
		if(cevohack != 0) {
			if(a == tochange) {
				putc(newwidth,cevohack);
			} else {
				putc(b,cevohack);
			}
		}
		/* These are zeroes */
		b = getc(cevo);
		if(cevohack != 0) {
			putc(b,cevohack);
		}
		b = getc(cevo);
		if(cevohack != 0) {
			putc(b,cevohack);
		}
		b = getc(cevo);
		if(cevohack != 0) {
			putc(b,cevohack);
		}
	}
	for(a=0;a<6;a++) {
		b = getc(cevo);
		height[a] = b;	
		if(cevohack != 0) {
			if(a == tochange) {
				putc(newheight,cevohack);
			} else {
				putc(b,cevohack);
			}
		}
		/* These are zeroes */
		b = getc(cevo);
		if(cevohack != 0) {
			putc(b,cevohack);
		}
		b = getc(cevo);
		if(cevohack != 0) {
			putc(b,cevohack);
		}
		b = getc(cevo);
		if(cevohack != 0) {
			putc(b,cevohack);
		}
	}
	for(a=0;a<6;a++) {
		char *valid;
		if(isprime(width[a] * height[a] + 1) == 1) {
			valid = "valid map size";
		} else {
			valid = "Warning: Bad map size";
		}		
		if(cevohack == 0) {
		  printf("%d%% %dx%d %s\n",size[a],width[a],height[a],valid);
		}
	}	
	while(!feof(cevo)) {
		b = getc(cevo);
		if(cevohack != 0 && !feof(cevo)) {
			putc(b,cevohack);
		}
	}
	fclose(cevo);
	if(cevohack != 0) {
		fclose(cevohack);
	}
}
