Author Topic: Regrouping Bricks  (Read 501 times)

For my Trench Digging mod, I am trying to make it regroup bricks when you place them together. Like if you place four 1x1's next to eachother in a box, it will put a 2x2 in their place. EDIT: Nvm, got it working.

Code: [Select]
function fxDtsBrick::checkBricks(%this)
{
%pos1 = %this.getPosition();
%DBName = %this.getDatablock().getName();
%Data = %this.getDatablock();
%rot=round(getWord(%this.rotation,3));
if(%rot==90 || %rot == 270)
{
%sizeX= %Data.brickSizeY;
%sizeY = %Data.brickSizeX;
}
else
{
%sizeX = %Data.brickSizeX;
%sizeY = %Data.brickSizeY;
}
%sizeZ = %Data.brickSizeZ;
%box = (%sizeX*0.5+0.6) SPC (%sizeY*0.5+0.6) SPC %sizeZ;
if(%DBName $= "brick1x1Data")
{
%size = 0.5;
%stringSize = "0.5";
%oppSize = -0.5;
%div = 4;
InitContainerBoxSearch(%pos1,%box,$TypeMasks::fxBrickObjectType);
}
else if(%DBName $= "brick2x2Data")
{
%size = 1;
%stringSize = "1";
%oppSize = "-1";
%div = 2;
InitContainerBoxSearch(%pos1,%box,$TypeMasks::fxBrickObjectType);
}
else if(%DBName $= "brick4x4Data")
{
%size = 2;
%stringSize = "2";
%oppSize = "-2";
%div = 1;
InitContainerBoxSearch(%pos1,%box,$TypeMasks::fxBrickObjectType);
}
else
{
return;
}
echo("The datablock is "@%DBName);

%puzzleComplete = 0;
%puzzlePos = "0 0 0";
echo("Started up the container search...");
while(%next = containerSearchNext())
{
if(%next.getDatablock().getName() $= %DBName && %next.isPlanted)
{
%pos2 = %next.getPosition();
%Sub = vectorSub(%pos1,%pos2);
echo("Found a "@%DBName@" brick and the sub is "@ %Sub);
switch$(%Sub)
{
case %stringSize SPC "0 0":
%puzzleComplete++;
%smallBrick[1,0] = %next;
%puzzleDir[1,-1] += 1;
%puzzleDir[1,1] += 1;
case %oppSize SPC "0 0":
%puzzleComplete++;
%smallBrick[-1,0] = %next;
%puzzleDir[-1,1] += 1;
%puzzleDir[-1,-1] += 1;
case "0" SPC %stringSize SPC "0":
%puzzleComplete++;
%smallBrick[0,1] = %next;
%puzzleDir[1,1] += 1;
%puzzleDir[-1,1] += 1;
case "0" SPC %oppSize SPC "0":
%puzzleComplete++;
%smallBrick[0,-1] = %next;
%puzzleDir[1,-1] += 1;
%puzzleDir[-1,-1] += 1;
case %stringSize SPC %stringSize SPC "0":
%puzzleComplete++;
%smallBrick[1,1] = %next;
%puzzleDir[1,1] += 1;
case %stringSize SPC %oppSize SPC "0":
%puzzleComplete++;
%smallBrick[1,-1] = %next;
%puzzleDir[1,-1] += 1;
case %oppSize SPC %stringSize SPC "0":
%puzzleComplete++;
%smallBrick[-1,1] = %next;
%puzzleDir[-1,1] += 1;
case %oppSize SPC %oppSize SPC "0":
%puzzleComplete++;
%smallBrick[-1,-1] = %next;
%puzzleDir[-1,-1] += 1;
}
}
else
{
echo("Found a "@%next.getDatablock().getName()@" brick...");
}
}
if(%puzzleComplete >= 3)
{
echo("Found "@%puzzleComplete@" bricks surrounding...");
for(%a = -1;%a <= 1;%a++)
{
for(%b = -1;%b <= 1;%b++)
{
%cur = %puzzleDir[%a,%b];
if(%highest $= "" || %cur > %highest)
{
%highest = %cur;
%highA = %a;
%highB = %b;
}
}
}
if(%highest < 3)
{
echo("Aren't enough bricks to make the desired grouping...");
return;
}
echo("Got the highest direction...");
%pos = vectorAdd(%this.getPosition(),%highA/%div*-1 SPC %highB/%div*-1 SPC "0");
%client = %this.getGroup().client;
%BG = %this.getGroup();
%colorId = %this.colorId;
%this.delete();
%smallBrick[%highA,0].delete();
%smallBrick[0,%highB].delete();
%smallBrick[%highA,%highB].delete();
echo("Deleted the bricks...");
schedule(0,0,finishPuzzleCompletion,%pos,%client,%BG,%colorId,%DBName);
}
else
{
echo("Didn't find enough surrounding bricks!");
}
}
function finishPuzzleCompletion(%pos,%client,%BG,%colorId,%DBName)
{
if(%DBName $= "brick1x1Data")
{
%DB = brick2x2Data;
}
else if(%DBName $= "brick2x2Data")
{
%DB = brick4x4Data;
}
else if(%DBName $= "brick4x4Data")
{
%DB = brick8x8Data;
}
%newBrick = new fxDtsBrick()
{
position = %pos;
datablock = %DB;
colorId = %colorId;
client = %client;
colorFxId = 0;
shapeFxId = 0;
};
%newBrick.isPlanted=1;
%newBrick.setTrusted(1);
%error = %newBrick.plant();
echo("Called .plant() and it returned: "@ %error);
if(%error && %error != 2)
{
%newBrick.delete();
return;
}
%BG.add(%newBrick);
echo("All was successful! Checking for more groupage...");
%newBrick.schedule(0,checkBricks);
}

    New code works.[/list]
    « Last Edit: October 29, 2009, 12:13:46 AM by lilboarder32 »

    It all works now, only post if you see an error in my code that I'm not catching.