/* Set or get C-Evo tech costs */

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

int main(int argc, char **argv) {
	FILE *cevo, *cevohack = 0;
	int a,b,c;
	int offset;
	uint8_t *d;
	float cost[3] = {2.0, 2.3, 2.6};
	float num = 0;
	if(argc != 1 && argc != 4) {
		printf("Tech cost: A utility to change the tech cost 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 tech costs.\n\n"
	"To use, type in tech-costs {easy} {medium} {hard}\n"
	"{easy} is the base tech cost at easy level; the others are likewise\n"
  "The default values are 2.0 for easy, 2.3 for medium and 2.6 for hard\n"
	"If run without arguments, this program reports the tech costs inside\n"
	"the Cevo executable\n");
	exit(0);
	}
	d = (uint8_t *)&num;
	if(argc == 4) {
		cevohack = fopen("Cevo-hack.exe","wb");
		cost[0] = atof(argv[1]);
		cost[1] = atof(argv[2]);
		cost[2] = atof(argv[3]);
		printf("Changing tech cost...\n");
	}
	cevo = fopen("Cevo.exe","rb");
	if(cevo == 0) {
		printf("Could not find Cevo.exe!\n");
		exit(1);
	}
	for(a=0;a<0xc4e1c;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<3;a++) {
		if(cevohack == 0) {
			cost[a] = 0;
		}
		num = cost[a];
		for(c=0;c<4;c++) {
			b = getc(cevo);
			if(cevohack != 0) {
				/* d points to where num is */
				b = ((d[c]) & 0xff);
				putc(b,cevohack);
			} else {
				d[c] = b;
			}
		}
		cost[a] = num;
	}
	while(!feof(cevo)) {
		b = getc(cevo);
		if(cevohack != 0 && !feof(cevo)) {
			putc(b,cevohack);
		}
	}
	fclose(cevo);
	if(cevohack != 0) {
		fclose(cevohack);
		printf("Cost modified and put in cevo-hack.exe\n");
	}
	for(a=0;a<3;a++) {
		printf("Difficulty %d tech base cost %f\n",a+1,cost[a]);
	}
}
