
// This file is not normally executed
// and is only used to add new bricks to the pack.

function rfb_sizeToName(%size) {
	%l = getWord(%size,0);
	%w = getWord(%size,1);
	%h = getWord(%size,2);
	%lw = %l @ "x" @ %w;
	if(%h==1) {
		if(%l%16==0 && %w%16==0)
			return %lw @ " Base";
		else
			return %lw @ "f";
	} else if(%h==2) {
		return %lw @ "h";
	} else if(%h==3) {
		return %lw;
	} else if(%h%3==0) {
		return %lw @ "x" @ %h/3;
	} else {
		return %lw @ "x" @ %h @ "f";
	}
}
function rfb_nameToFilename(%name) {
	%name = strReplace(%name, "h", "x2f");
	%name = strReplace(%name, " Base", "f");
	return %name;
}

function rfb_generateBlb(%size){
	%name = rfb_sizeToName(%size);
	%filename = rfb_nameToFilename(%name);
	%fn = "Add-Ons/Brick_RUltimateFiller/bricks/" @ %filename @ ".blb";
	if(!isFile(%fn)){
		%file = new FileObject();
		%s = %file.openForWrite(%fn);
		if(!%s){ echo("ERROR: Could not open file " @ %fn); return; }
		
		%file.writeLine(%size);
		%file.writeLine("BRICK");
		
		%file.close();
		%file.delete();
	}
}

function rfb_generateDatablock(%cat, %subcat, %size) {
	%name = rfb_sizeToName(%size);
	%filename = rfb_nameToFilename(%name);
	%cmd = "datablock fxDtsBrickData(brickRf" @ %filename @ "data){\n" @
		"\tbrickFile = \"Add-Ons/Brick_RUltimateFiller/bricks/" @ %filename @ ".blb\";\n" @
		"\tcategory = \"" @ %cat @ "\";\n" @
		"\tsubCategory = \"" @ %subcat @ "\";\n" @
		"\tuiName = \"" @ %name @ "\";\n" @
		"\ticonName = \"Add-Ons/Brick_RUltimateFiller/icons/" @ %filename @ "\";\n" @
		"\tisRedFill = true;\n" @
	"};\n";
	eval(%cmd);
	echo(%cmd);
}

function rfb_generateSize(%cat, %subcat, %size) {
	rfb_generateBlb(%size);
	rfb_generateDatablock(%cat, %subcat, %size);
}

function rfb_generateList(%cat, %subcat, %depth, %height, %widthList) {
	if(%widthList$="ALL"){
		%widthListNew = "";
		//%listAll = "1 2 3 4 6 8 10 12 16 5 7 9 11 13 14 15 24 32 48 64";
		%listAll = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 24 32 48 64";
		for(%i=0; %i<getWordCount(%listAll); %i++) {
			%width = getWord(%listAll, %i);
			if(%width>=%depth){
				%widthListNew = %widthListNew SPC %width;
			}
		}
		%widthList = trim(%widthListNew);
	}
	
	for(%i=0; %i<getWordCount(%widthList); %i++) {
		%width = getWord(%widthList, %i);
		
		%size = %depth SPC %width SPC %height;
		rfb_generateSize(%cat, %subcat, %size);
	}
}

function rfb_generateAll() {
	// bricks
	rfb_generateList("Bricks",  "1x",  1, 3, "5 7 9 11 13 14 15 24 32 48 64");
	rfb_generateList("Bricks",  "2x",  2, 3, "5 7 9 11 12 13 14 15 16 24 32 48 64");
	rfb_generateList("Bricks",  "3x",  3, 3, "ALL");
	rfb_generateList("Bricks",  "4x",  4, 3, "5 7 9 11 13 14 15 24 32 48 64");
	rfb_generateList("Bricks",  "5x",  5, 3, "ALL");
	rfb_generateList("Bricks",  "6x",  6, 3, "ALL");
	rfb_generateList("Bricks",  "8x",  8, 3, "9 10 11 12 13 14 15 24 32 48 64");
	rfb_generateList("Bricks", "10x", 10, 3, "11 12 13 14 15 16 24 32 48 64");
	rfb_generateList("Bricks", "12x", 12, 3, "12 13 14 15 16 32 36 48 64");
	// plates
	rfb_generateList("Plates",  "1x",  1, 1, "5 7 9 11 13 14 15 24 32 48 64");
	rfb_generateList("Plates",  "2x",  2, 1, "5 7 9 11 12 13 14 15 16 24 32 48 64");
	rfb_generateList("Plates",  "3x",  3, 1, "ALL");
	rfb_generateList("Plates",  "4x",  4, 1, "5 7 9 11 13 14 15 24 32 48 64");
	rfb_generateList("Plates",  "5x",  5, 1, "ALL");
	rfb_generateList("Plates",  "6x",  6, 1, "7 9 11 13 14 15 32 48 64");
	rfb_generateList("Plates",  "8x",  8, 1, "9 10 11 12 13 14 15 24 32 48 64");
	rfb_generateList("Plates", "10x", 10, 1, "10 11 12 13 14 15 16 20 24 32 48 64");
	rfb_generateList("Plates", "12x", 12, 1, "12 13 14 15 16 24 32 36 48 64");
	// tall bricks
	rfb_generateList("Bricks", "2x Height", 1,  6, "ALL");
	rfb_generateList("Bricks", "2x Height", 2,  6, "ALL");
	rfb_generateList("Bricks", "3x Height", 1,  9, "ALL");
	rfb_generateList("Bricks", "3x Height", 2,  9, "3 5 7 8 9 10 11 12 13 14 15 16 24 32 48 64");
	rfb_generateList("Bricks", "4x Height", 1, 12, "ALL");
	rfb_generateList("Bricks", "4x Height", 2, 12, "ALL");
	rfb_generateList("Bricks", "5x Height", 1, 15, "5 7 8 9 10 11 13 14 15 16 24 32 48 64");
	rfb_generateList("Bricks", "5x Height", 2, 15, "5 7 8 9 10 11 13 14 15 16 24 32 48 64");
	rfb_generateList("Bricks", "6x Height", 1, 18, "ALL");
	rfb_generateList("Bricks", "6x Height", 2, 18, "ALL");
	// baseplates
	rfb_generateList("Baseplates", "Plain", 16, 1, "48 64");
	rfb_generateList("Baseplates", "Plain", 32, 1, "48 64");
	rfb_generateList("Baseplates", "Plain", 48, 1, "64");
	rfb_generateList("Baseplates", "Brick", 16, 3, "16 32 48 64");
	rfb_generateList("Baseplates", "Brick", 32, 3, "32 48 64");
	rfb_generateList("Baseplates", "Brick", 48, 3, "48 64");
	rfb_generateList("Baseplates", "Brick", 64, 3, "64");
	// half bricks
	rfb_generateList("Bricks", "1x Half",  1, 2, "ALL");
	rfb_generateList("Bricks", "2x Half",  2, 2, "ALL");
	rfb_generateList("Bricks", "3x Half",  3, 2, "ALL");
	rfb_generateList("Bricks", "4x Half",  4, 2, "ALL");
	rfb_generateList("Bricks", "5x Half",  5, 2, "ALL");
	rfb_generateList("Bricks", "6x Half",  6, 2, "ALL");
	rfb_generateList("Bricks", "8x Half",  8, 2, "ALL");
}
rfb_generateAll();
