//Author: ndogg

num FTL_AC			= 0x6E4C;
num FTL_CROSS_SPEED		= 0x6E09;		// location to byte which is the ftl disabling speed (KPH)
num FTL_FUEL_RES		= 0x6E10;		// Dont forget these two are hard coded into ftl.asm
num FTL_FUEL_CUT		= 0x6E15;
num REG_FUEL_RES		= 0x6E20;
num REG_FUEL_CUT		= 0x6E25;
num FTS_FUEL_RES		= 0x6E2D;		
num FTS_FUEL_CUT		= 0x6E32;
num BCUT_FUEL_RES		= 0x6E3A;
num BCUT_FUEL_CUT		= 0x6E3F;
num BOOST_CUT_P			= 0x6E03;
num BCUT_OFF			= 0x6E50;



num hFtsCut			= -1;
num hFtsRes			= -1;
num hFtlSpeed 			= -1;
num hFtlCut			= -1;
num hFtlRes			= -1;
num hEnableftl			= -1;
num hUseAC			= -1;
num hUseBcut			= -1;
num hBcutCut			= -1;
num hBcutRes			= -1;
num hBcutPressure		= -1;
num temp			= 0;
num temp2			= 0;



void open()
{

	x = 220;
	y = 130;

	// RPM Shit
	CreateBorder(x, y, 240, 295, 5, 0, "Full Throttle Launch");
	CreateStatic(x+5, y-5, 150, 20, 5, 0, "Full Throttle Launch");

	CreateStatic(80+x+20, y+20, 130, 20, 5, 0, "RPM Launch Cut");
	CreateStatic(80+x+20, y+40, 130, 20, 5, 0, "RPM Launch Resume");
	CreateStatic(80+x+20, y+60, 130, 20, 5, 0, "MPH Launch Disable");
	CreateStatic(80+x+20, y+80, 130, 20, 5, 0, "RPM Shift Cut");
	CreateStatic(80+x+20, y+100, 130, 20, 5, 0, "RPM Shift Resume");
 	CreateStatic(80+x+20, y+120, 130, 20, 5, 0, "BoostCut Cut");
	CreateStatic(80+x+20, y+140, 130, 20, 5, 0, "BoostCut Resume");
	CreateStatic(80+x+20, y+160, 130, 20, 5, 0, "BoostCut Psi");

	hFtlCut = CreateEdit(x+10, y+20, 80, 20, 5, 0, " ");
	hFtlRes  = CreateEdit(x+10, y+40, 80, 20, 5, 0, " ");
	hFtlSpeed  = CreateEdit(x+10, y+60, 80, 20, 5, 0, " ");
	hFtsCut = CreateEdit(x+10, y+80, 80, 20, 5, 0, " ");
	hFtsRes  = CreateEdit(x+10, y+100, 80, 20, 5, 0, " ");
	hBcutCut = CreateEdit(x+10, y+120, 80, 20, 5, 0, " ");
	hBcutRes  = CreateEdit(x+10, y+140, 80, 20, 5, 0, " ");
	hBcutPressure  = CreateEdit(x+10, y+160, 80, 20, 5, 0, " ");

	SetItem(hFtlCut, round(1875600 / readword(FTL_FUEL_CUT)));
	SetItem(hFtlRes, round(1875600 / readword(FTL_FUEL_RES)));
	SetItem(hFtlSpeed, round(readbyte(FTL_CROSS_SPEED) * 0.6214));
	SetItem(hFtsCut, round(1875600 / readword(FTS_FUEL_CUT)));
	SetItem(hFtsRes, round(1875600 / readword(FTS_FUEL_RES)));
	SetItem(hBcutCut, round(1875600 / readword(BCUT_FUEL_CUT)));
 	SetItem(hBcutRes, round(1875600 / readword(BCUT_FUEL_RES)));
	temp = ((((((readbyte(BOOST_CUT_P))/255) * 1000)*(readfloat(MAPSENSOR_FLOAT)))-1000)/1000)*14.5);
	SetItem(hBcutPressure,  temp);

	y = y + 200;
	x = x + 10;
	hEnableftl = CreateCheck(x, y, 150, 20, 5, 0, "FTLs/Bcut Enable");
	if(readbyte(0x3FCB) == 0x03)	
		setitem(hEnableftl, 1);
	else
		setitem(hEnableftl, 0);

  	y = y + 20;
	hUseAC = CreateCheck(x, y, 150, 20, 5, 0, "Use AC switch");
	if(readbyte(FTL_AC) == 0xFF)
		setitem(hUseAC, 0);
	else
		setitem(hUseAC, 1);
  	
	
}

void DisableCRC()					
{

	writebyte(FTL_CROSS_SPEED, 14 / 0.6214);
	writeword(FTL_FUEL_CUT, 1875600 / 3500);
	writeword(FTL_FUEL_RES, 1875600 / 3400);
	writeword(FTS_FUEL_CUT, 1875600 / 6000);
	writeword(FTS_FUEL_RES, 1875600 / 5900);
	writeword(REG_FUEL_CUT, 1875600 / 8000);
	writeword(REG_FUEL_RES, 1875600 / 7900);
	writeword(BCUT_FUEL_CUT, 1875600 / 6000);
	writeword(BCUT_FUEL_RES, 1875600 / 5900);
	writebyte(BOOST_CUT_P, 0xCE);
}

num save()
{
	if(getitem(hEnableftl))
	{	writebyte(0x3FCB, 0x03);
		writebyte(0x3FCC, 0x00);
		writebyte(0x3FCD, 0x6E);			
		writeword(0x3FCE, 0x0000);
	}
	else
	{	
		writebyte(0x3FCB, 0xD3);
		writebyte(0x3FCC, 0x12);
		writebyte(0x3FCD, 0x42);			
		writeword(0x3FCE, 0x14D3);
	}

	if(getitem(hUseAC))
	{
		writebyte(FTL_AC, 0x00);
		// assemble ftl code w/ AC
		assemble("ftl.asm", 0x6E00);

	}
	else
	{
		writebyte(FTL_AC, 0xFF);			
		// assemble ftl code w/o ac
		assemble("ftl.asm", 0x6E00);
		writebyte(0x6E0C,0x00);
		writebyte(0x6E0D,0x00);
		writebyte(0x6E0E,0x00);
	}


	writeword(FTL_FUEL_CUT, 1875600 / GetItem(hFtlCut));
	writeword(FTL_FUEL_RES, 1875600 / GetItem(hFtlRes));
	writebyte(FTL_CROSS_SPEED, GetItem(hFtlSpeed) / 0.6214);
	writeword(FTS_FUEL_CUT, 1875600 / GetItem(hFtsCut));
	writeword(FTS_FUEL_RES, 1875600 / GetItem(hFtsRes));
	writeword(REG_FUEL_CUT, 1875600 / GetItem(hRev1));
	writeword(REG_FUEL_RES, 1875600 / GetItem(hRev2));
	writeword(BCUT_FUEL_CUT, 1875600 / GetItem(hBcutCut));
	writeword(BCUT_FUEL_RES, 1875600 / GetItem(hBcutRes));

	if(((((GetItem(hBcutPressure))/14.5)*1000)+1000) > ((readfloat(MAPSENSOR_FLOAT))*1000))
		writebyte(BOOST_CUT_P, 0xFF);
	else
	{
		if((GetItem(hBcutPressure)) < 1)
			writebyte(BOOST_CUT_P, 0x92);
		else
		{
		temp2 = (((((((GetItem(hBcutPressure))/14.5)*1000)+1000)/1000)/(readfloat(MAPSENSOR_FLOAT)))*255);
		writebyte(BOOST_CUT_P, temp2);
		}
	}
	
}