/* Make C-Evo epic: 7x tech cost and 2.5 size increase for foodboxes.  
 * This allows there to be more wars and considerably slows down development.
 * combined with a 230% map size, you can have a C-Evo game last weeks! */

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

int main(int argc, char **argv) {
	FILE *cevo = 0, *cevohack = 0, *cevoepic = 0, *cevoquick = 0;
	int a,b,c,z;
	int32_t offset;
	uint8_t *dq, *de;
	float techcost_epic[3] = {14.0, 16.1, 18.2};
	int32_t foodbox_epic[3] = {74,100,126};
	float techcost_quick[3] = {1.0, 1.15, 1.3};
	int32_t foodbox_quick[3] = {14,20,26};
	int32_t numq = 30;
	int32_t nume = 30;
	int32_t width[6] = {20,32,42,52,61,100};
	int32_t height[6] = {20,50,68,78,96,96};
	float fnumq = 0, fnume = 0;
	if(argc != 1) {
		printf("Make cevo-hack, cevo-epic, and cevo-quick\n");
	exit(0);
	}
	dq = (uint8_t *)&numq;
	de = (uint8_t *)&nume;
	cevoepic = fopen("cevo-epic.exe","wb");
	cevoquick = fopen("cevo-quick.exe","wb");
	cevohack = fopen("cevo-hack.exe","wb");
	if(cevoepic == 0 || cevoquick == 0 || cevohack == 0) {
		printf("Could not open cevo-(quick|epic|hack) for writing!\n");
	}
	cevo = fopen("Cevo.exe","rb");
	if(cevo == 0) {
		printf("Could not find Cevo.exe!\n");
		exit(1);
	}
	/* Increase max year */
	for(offset=0;offset<0x7a748;offset++) {
		b = getc(cevo);	
		if(feof(cevo)) {
			printf("This is too small!\n");
			exit(2);
		}
		if(cevohack != 0) {
			putc(b,cevohack);
			putc(b,cevoepic);
			putc(b,cevoquick);
		}
	}
	b = getc(cevo);
	b = 71;
	putc(b,cevohack);
	putc(b,cevoepic);
	putc(b,cevoquick);
	offset++;
	/* Forward to foodbox size */
	for(;offset<0xc4e10;offset++) {
		b = getc(cevo);	
		if(feof(cevo)) {
			printf("This is too small!\n");
			exit(2);
		}
		if(cevohack != 0) {
			putc(b,cevohack);
			putc(b,cevoepic);
			putc(b,cevoquick);
		}
	}
	/* Foodbox */
	for(a=0;a<3;a++) {
		numq = foodbox_quick[a];
		nume = foodbox_epic[a];
		for(c=0;c<4;c++) {
			b = getc(cevo);
			offset++;
			if(cevohack != 0) {
				putc(b,cevohack);
				/* de points to where nume is */
				b = ((de[c]) & 0xff);
				putc(b,cevoepic);
				/* dq points to where numq is */
				b = ((dq[c]) & 0xff);
				putc(b,cevoquick);
			} 
		}
	}
	/* Tech cost */
	dq = (uint8_t *)&fnumq;
	de = (uint8_t *)&fnume;
	for(a=0;a<3;a++) {
		fnumq = techcost_quick[a];
		fnume = techcost_epic[a];
		for(c=0;c<4;c++) {
			b = getc(cevo);
			offset++;
			if(cevohack != 0) {
				putc(b,cevohack);
				/* de points to where nume is */
				b = ((de[c]) & 0xff);
				putc(b,cevoepic);
				/* dq points to where numq is */
				b = ((dq[c]) & 0xff);
				putc(b,cevoquick);
			} 
		}
	}
	/* Forward to map sizes */
	for(;offset<0xc70e8;offset++) {
		b = getc(cevo);	
		if(feof(cevo)) {
			printf("This is too small!\n");
			exit(2);
		}
		if(cevohack != 0) {
			putc(b,cevohack);
			putc(b,cevoepic);
			putc(b,cevoquick);
		}
	}
	/* Change map sizes */
	for(a=0;a<6;a++) {
		b = getc(cevo);
		offset++;
		b = width[a];
		putc(b,cevohack);
		putc(b,cevoepic);
		putc(b,cevoquick);
		for(z=0;z<3;z++) {
			b = getc(cevo);
			offset++;
			putc(b,cevohack);
			putc(b,cevoepic);
			putc(b,cevoquick);
		}
	}
	for(a=0;a<6;a++) {
		b = getc(cevo);
		offset++;
		b = height[a];
		putc(b,cevohack);
		putc(b,cevoepic);
		putc(b,cevoquick);
		for(z=0;z<3;z++) {
			b = getc(cevo);
			offset++;
			putc(b,cevohack);
			putc(b,cevoepic);
			putc(b,cevoquick);
		}
	}
	while(!feof(cevo)) {
		b = getc(cevo);	
		if(feof(cevo)) {
			break;
		}
		if(cevohack != 0) {
			putc(b,cevohack);
			putc(b,cevoepic);
			putc(b,cevoquick);
		}
	}
	fclose(cevo);
	if(cevohack != 0) {
		fclose(cevohack);
		printf("cevo-hack, cevo-quick, and cevo-epic.exe made\n");
	}
}
