
function rom_reload() {
	exec($Con::File);
	exec($LuaLogic::Path @ "bricks/gen/newcode/ROM 16x16.cs");
}

package lualogic_rom_update {
	function fxDtsBrick::onPlant(%brick) {
		parent::onPlant(%brick);
		lualogic_rom_createbrick(%brick);
	}
	function fxDtsBrick::onLoadPlant(%brick) {
		parent::onLoadPlant(%brick);
		lualogic_rom_createbrick(%brick);
	}
	function fxDTSBrick::onDeath(%brick) {
		parent::onDeath(%brick);
		if(%brick.lualogic_rom_affects) {
			lualogic_rom_deletebrick(%brick);
		}
	}
	function fxDTSBrick::onRemove(%brick) {
		parent::onRemove(%brick);
		if(%brick.lualogic_rom_affects) {
			lualogic_rom_deletebrick(%brick);
		}
	}
	function fxDtsBrick::setRaycasting(%brick, %val) {
		parent::setRaycasting(%brick, %val);
		if(%brick.lualogic_rom_affects) {
			lualogic_rom_updatercbrick(%brick);
		}
	}
};
schedule(0, 0, activatePackage, lualogic_rom_update);

// lualogic_rom_affects: whether this brick is on top of a rom brick
// lualogic_rom_is: whether this brick is a rom brick

function lualogic_rom_createbrick(%brick) {
	%brick.lualogic_rom_delete = 0;
	
	//if rom: set is, set affects on any up bricks, compute data
	if(%brick.getdatablock().isLogicRom) {
		%brick.lualogic_rom_is = 1;
		
		%nub = 0;
		for(%i=0; %i<%nub; %i++) {
			%upbrick = %brick.getUpBrick(%i);
			%upbrick.lualogic_rom_affects = 1;
		}
		
		lualogic_rom_updatedata(%brick);
	}
	
	// update any rom down bricks; if there are any, set affects
	%ndb = %brick.getNumDownBricks();
	for(%i=0; %i<%ndb; %i++) {
		%downbrick = %brick.getDownBrick(%i);
		if(%downbrick.lualogic_rom_is) {
			%brick.lualogic_rom_affects = 1;
			lualogic_rom_updatedata(%downbrick);
		}
	}
}
function lualogic_rom_deletebrick(%brick) {
	// update any rom down bricks
	%brick.lualogic_rom_delete = 1;
	%ndb = %brick.getNumDownBricks();
	for(%i=0; %i<%ndb; %i++) {
		%downbrick = %brick.getDownBrick(%i);
		if(%downbrick.lualogic_rom_is) {
			lualogic_rom_updatedata(%downbrick);
		}
	}
}
function lualogic_rom_updatercbrick(%brick) {
	// update any rom down bricks
	%ndb = %brick.getNumDownBricks();
	for(%i=0; %i<%ndb; %i++) {
		%downbrick = %brick.getDownBrick(%i);
		if(%downbrick.lualogic_rom_is) {
			lualogic_rom_updatedata(%downbrick);
		}
	}
}

function lualogic_rom_rotate(%pos, %rot) {
	%x = getWord(%pos, 0);
	%y = getWord(%pos, 1);
	%z = getWord(%pos, 2);
	
	%rot = %rot % 4;
	if       (%rot==0) {
		return %x SPC %y SPC %z;
	} else if(%rot==1) {
		return %y SPC -%x SPC %z;
	} else if(%rot==2) {
		return -%x SPC -%y SPC %z;
	} else if(%rot==3) {
		return -%y SPC %x SPC %z;
	}
}

function lualogic_rom_updatedata(%brick) {
	if(!%brick.lualogic_rom_updatedata_scheduled) {
		schedule(1, 0, lualogic_rom_updatedata_final, %brick);
		%brick.lualogic_rom_updatedata_scheduled = 1;
	}
}

function lualogic_rom_updatedata_final(%brick) {
	if(!isObject(%brick) || %brick.lualogic_rom_delete) return;
	
	%brick.lualogic_rom_updatedata_scheduled = 0;
	
	%data = %brick.getDatablock();
	
	%rx = %data.logicRomX;
	%ry = %data.logicRomY;
	
	%brickpos = %brick.getPosition();
	%rot = (%brick.angleId + 1)%4;
	
	%data_str = "";
	
	for(%x=0; %x<%rx; %x++) {
		%line_str = "";
		for(%y=0; %y<%ry; %y++) {
			%pos = (%x*0.5 - %rx/4 + 0.25) SPC (%y*0.5 - %ry/4 + 0.25) SPC 0;
			%pos = lualogic_rom_rotate(%pos, %rot);
			%pos = vectorAdd(%brickpos, %pos);
			%ray = containerRaycast(
				%pos,
				vectorAdd(%pos, "0 0 0.2"),
				$TypeMasks::FxBrickObjectType,
				%brick
			);
			%hit = getWord(%ray, 0);
			if(isObject(%hit) && !%hit.lualogic_rom_delete && %hit.isRaycasting()) {
				%line_str = %line_str @ "1";
			} else {
				%line_str = %line_str @ "0";
			}
		}
		%data_str = %data_str @ %line_str;
	}
	
	lualogic_sendinput(%brick, 1, %data_str);
}
