Author Topic: Deleting bricks of a certain color  (Read 1761 times)

I decided to make a script that could delete all of the bricks of a specified color from a build.
My method was to edit the business lines of the save file and rewrite to a new file, which I would copy/paste back in.
Code: [Select]
//Client_ColorDestroy

function ColorDestroyList(%file, %colorN)
{
new scriptObject(LineSO) {}
%file = expandFilename(%file);
if(isFile(%file))
{
%fileObject = new FileObject();
if(%fileObject.OpenForRead(%file))
{
for(%l=0;%l<256000;%l++)
{
%line = %fileObject.readline();
%brickVal = getWord(%line,8);
if(%brickVal != %colorN)
{
LineSO.value[%SoL] = %line;
%SoL++;
}
else
{
return;
}
}
%SoL = $SOL
%file.close();
%file.delete();
deletecolorso();
colordestroywrite();
}
else
{
echo("ColorDelete() - Error opening the .bls file "@%file);
}
%fileObject.delete();
}
else
{
echo("ColorDelete() - Error locating the .bls file "@%file);
}
}

function ColorDestroyWrite(%file)
{
%obj.save(%file);
}

function DeleteColorSO(%this)
{
LineSO.delete();
}

Since I'm making a topic and know this isn't the best way to do this, you might as well tell me a better option if you know one.

So
1) Tell me what I did wrong with this code
2) Tell me how else I could go about doing this

Someone was trying to do this once.

You can loop through the main brick group and find every brick with the color ID you're looking for and delete them. Much simpler, but regardless of what way you go about doing it you have to know every single color ID and also possibly make functions for every single one.

Okay, tons of things.

1. Nowhere in this code does it actually attempt to do anything to any bricks. It's just a mess trying to read a file.
2. new scriptObject(LineSO){} - I have no idea what this is supposed to do. LineSO is never referenced anywhere. There's no semicolon after the object declaration so it's going to cause a syntax error.
3. Why the forget do you have two functions outside of the actual function that have no purpose other than deleting script objects?
4. I just realized that the file is called Client_ColorDestroy. How is this a client mod? What.

I don't even know why you want it to read a file. I have this in some random "Handy Function" package I built.

Code: [Select]
function deleteAll(%brickType)
{
// Did we supply a valid datablock?
if(isObject(%brickType))
{
%killList = "";

// Loop through all brickgroups.
for(%a = 0; %a < MainBrickGroup.getCount(); %a++)
{
%bg = MainBrickGroup.getObject(%a);

// Loop through every brick in this brickgroup.
for(%b = 0; %b < %bg.getCount(); %b++)
{
%brick = %bg.getObject(%b);

// Test if the bricktype of the selected brick
if(%brick.getDatablock().getID() == %brickType.getID())
{
%killList = setField(%str, getFieldCount(%killList), %brick.getID());
}
}
}

for(%a = 0; %a < getFieldCount(%killList); %a++)
{
%brick = getField(%killList, %a);
%brick.killBrick();
}

echo("Destroyed" SPC getFieldCount(%killList) SPC "Brick(s).");
}
}

Just change it to delete if the color ID matches the ID entered in the parameters.



Someone was trying to do this once.
How could he fail? This is not a complex issue.

Not really complex at all, just annoying.

Not really complex at all, just annoying.
not really.

Code: [Select]
function descriminateBricks(%findColor, %replaceColor)
{
%killList = "";

// Loop through all brickgroups.
for(%a = 0; %a < MainBrickGroup.getCount(); %a++)
{
%bg = MainBrickGroup.getObject(%a);

// Loop through every brick in this brickgroup.
for(%b = 0; %b < %bg.getCount(); %b++)
{
%brick = %bg.getObject(%b);

// Test if the bricktype of the selected brick
if(%brick.getColorID() == %findColor)
{
%killList = setField(%str, getFieldCount(%killList), %brick.getID());
}
}
}

for(%a = 0; %a < getFieldCount(%killList); %a++)
{
%brick = getField(%killList, %a);

if(isObject(%brick))
{
if(%replaceColor $= "")
{
%brick.killBrick();
}
else
{
%brick.setColor(%replaceColor);
}
}
}

echo("Acted upon" SPC getFieldCount(%killList) SPC "Brick(s).");
}

"And you're done."

I mean the whole having to know all the color IDs for whatever colorset you're using.

Except all you have to know is how to count to know what the IDs are so nevermind. :o

not really.

Code: [Select]
function descriminateBricks(%findColor, %replaceColor)
{
%killList = "";

// Loop through all brickgroups.
for(%a = 0; %a < MainBrickGroup.getCount(); %a++)
{
%bg = MainBrickGroup.getObject(%a);

// Loop through every brick in this brickgroup.
for(%b = 0; %b < %bg.getCount(); %b++)
{
%brick = %bg.getObject(%b);

// Test if the bricktype of the selected brick
if(%brick.getColorID() == %findColor)
{
%killList = setField(%str, getFieldCount(%killList), %brick.getID());
}
}
}

for(%a = 0; %a < getFieldCount(%killList); %a++)
{
%brick = getField(%killList, %a);

if(isObject(%brick))
{
if(%replaceColor $= "")
{
%brick.killBrick();
}
else
{
%brick.setColor(%replaceColor);
}
}
}

echo("Acted upon" SPC getFieldCount(%killList) SPC "Brick(s).");
}

"And you're done."
What is the purpose of %replacecolor in that code?

What is the purpose of %replacecolor in that code?
If replacecolor is set to an ID, it apparently sets the brick to that color.

I mean the whole having to know all the color IDs for whatever colorset you're using.

Except all you have to know is how to count to know what the IDs are so nevermind. :o

Code: [Select]
function serverCmdforgetThisColor(%client)
{
if(%client.isAdmin)
{
%player = %client.player;

if(isObject(%player))
{
%colorID = %player.getMountedImage(0).projectile.colorID;

if(%color !$= "")
{
descriminateBricks(%colorID);
%client.chatMessage("\c2That's the power of Oxyclean, baby.");
}
else
{
%client.chatMessage("\c5Take out your paint can and scroll to the color you want to annihilate.");
}
}
else
{
%client.chatMessage("\c5You must spawn first.");
}
}
else
{
%client.chatMessage("\c5You must be an admin to use this command.");
}
}

And you're done.

If replacecolor is set to an ID, it apparently sets the brick to that color.
That field is optional.

Thanks for quoting that entire code-block, by the way.

The script is not working correctly
Looks like you made a tiny mistake Iban, line 11 should be if(%colorID !$= "")

It also seems that the script only destroys a single brick each time you use the command.

The script is not working correctly
Looks like you made a tiny mistake Iban, line 11 should be if(%colorID !$= "")

It also seems that the script only destroys a single brick each time you use the command.
Not sure why. I wrote it off-handedly and did not test it but you should be able to figure it out since I did the rest.

not sure what you guys are atempting to write here.. but this should do what Treynolds416 asked for:
Code: [Select]
function serverCmdDeleteColor(%client, %colorID)
  {
   if (!%client.isAdmin)
     return;

   %toDelete = new SimSet();
   for (%a=0; %a < MainBrickgroup.getCount(); %a++)
     {
      %group = MainBrickGroup.getObject(%a);
      for (%b=0; %b < %group.getCount(); %b++)
        {
         %brick = %group.getObject(%b);
         if (%brick.colorID == %colorID)
           {
            %toDelete.add(%brick);
           }
        }
     }
   MessageClient(%client, '', "Deleting " @ %toDelete.getCount() @ " bricks");
   while (%toDelete.getCount() > 0)
     {
      %brick = %toDelete.getObject(0);
      %brick.delete();
     }
  }


then say /deletecolor 0
to delete all red bricks from the server.

I'd use Iban's last script.

I'd say Red-Guy's is much cleaner and simpler. You can use Iban's script to find the paint color and call Red_Guy's function.

I'd say Red-Guy's is much cleaner and simpler. You can use Iban's script to find the paint color and call Red_Guy's function.
The only thing he does differently is use a simset instead of a field list to keep track of the hit objects, which in retrospect is a better idea. He also uses an object property instead of a get function which is generally frowned upon.

The end result is exactly the same. I just used comments and spaced everything out nicely. I do not loving understand for the life of me why people insist on indenting with 3 spaces like god damn stuff chucking apes.