
function rfb_addAlias(%db, %name) {
	// Save so we know to delete any other bricks with this UI name
	$rfb_uiNames[%name] = %db;
	// overwrite in uiNameTable if inferior bricks exist,
	// and insert as an "alias" if not; this ensures load compatibility.
	$uiNameTable[%name] = %db;
}

function rfb_addCompatibleUiNames(%db){
	rfb_addAlias(%db, %db.uiName);
	%l = %db.brickSizeX;
	%w = %db.brickSizeY;
	%h = %db.brickSizeZ;
	%lw = %l @ "x" @ %w;
	if(%h==1) {
		rfb_addAlias(%db, %lw @ "f"); // actual name, Brick_1x14Plate, Brick_1x15Plate, Brick_8x, Brick_9Pack, Brick_10x, Brick_BlackDragonIV, Brick_BlackDragonIV_Filler, Brick_DemiansBB
		rfb_addAlias(%db, %lw @ " Plate"); // Brick_32x64, Brick_Other_Bricks
		if(%l%16==0 && %w%16==0)
			rfb_addAlias(%db, %lw @ " Base"); // actual name for baseplates, Brick_BlackDragonIV
	} else if(%h==2) {
		rfb_addAlias(%db, %lw @ "h"); // actual name, Brick_BlackDragonIV, Brick_BlackDragonIV_Filler, Brick_Halfbrick
		rfb_addAlias(%db, %lw @ "x2f"); // Brick_RedFill old versions
	} else if(%h==3) {
		rfb_addAlias(%db, %lw); // actual name, Brick_6xWidth, Brick_8x, Brick_9Pack, Brick_DemiansBB
		rfb_addAlias(%db, %lw @ "x1"); // Brick_DemiansBB2, Brick_DemiansBB3, Brick_Dr
		rfb_addAlias(%db, %lw @ " Brick"); // Brick_32x64, Brick_Other_Bricks
		if(%l%16==0 && %w%16==0)
			rfb_addAlias(%db, %lw @ "x1 Base"); // Brick_32x_Base_variations (not actually needed)
	} else if(%h%3==0) {
		rfb_addAlias(%db, %lw @ "x" @ %h/3); // actual name, Brick_Pit10xHeight, Brick_Sgt_Gary, Brick_Jirue, Brick_4xheight, Brick_BlackDragonIV, Brick_BlackDragonIV_Filler, Brick_DemiansBB, Brick_jbskier13
	} else {
		rfb_addAlias(%lw @ "x" @ %h @ "f"); // actual name
	}
}

// Delete the datablocks of inferior brick packs, and map all compatible UI names
// to RedFill bricks.
function rfb_initCompatibility() {
	echo("Brick_RUltimateFiller: Deleting compatible brick datablocks.");
	
	// Record all UINames any RedFill brick has or can replace.
	for(%i=0; %i<getDatablockGroupSize(); %i++) {
		%db = getDatablock(%i);
		if(%db.getClassName()$="fxDtsBrickData") {
			if(%db.isRedFill)
				rfb_addCompatibleUiNames(%db);
		}
	}
	
	// Find non-RedFill brick datablocks with compatible UINames.
	// %betas[]
	%betaCount = 0;
	for(%i=0; %i<getDatablockGroupSize(); %i++) {
		%db = getDatablock(%i);
		if(%db.getClassName()$="fxDtsBrickData") {
			if($rfb_uiNames[%db.uiName] && !%db.isRedFill) {
				%betas[%betaCount] = %db.getId();
				%betaCount++;
				echo("  Deleting inferior brick: " @ %db.brickFile @ "(\"" @ %db.uiName @ "\")");
			}
		}
	}
	
	// Delete temp list of compatible UI names
	deleteVariables("$rfb_uiNames*");
	
	// Delete the inferior brick datablocks
	for(%i=0; %i<%betaCount; %i++) {
		%db = %betas[%i];
		%db.uiName = "";
		%db.brickFile = "";
		%db.iconName = "";
		%db.collisionShapeName = "";
		DatablockGroup.remove(%db);
	}
	
	echo("  Deleted " @ %betaCount @ " brick datablocks.");
	if(%betaCount>0)
		warn("Brick_RUltimateFiller: Consider disabling the inferior brick packs listed above.");
}

package rfb_compatibility {
	function verifyBrickUiNames() {
		rfb_initCompatibility();
		parent::verifyBrickUiNames();
	}
};
activatePackage(rfb_compatibility);
