Author Topic: Clear all bricks  (Read 3484 times)

How do you delete all bricks on a server with a script? This is what I'm currently using:
Code: [Select]
for (%i = 0; %i < MainBrickGroup.getCount(); %i++)
{
%group = MainBrickGroup.getObject(%i);
for (%b = 0; %b < %group.getCount(); %b++)
{
%group.getObject(%b).delete();
}
}

Only 49-51% of the bricks are deleted every time I execute this, and it causes some bricks to not be cleared properly. What's the correct thing to do?

try calling .chaindeleteall() on the subgroups, or doing schedule(0, delete)

Whenever you iteratively delete something from a group, you need to start at the end of the group, and decrement it each tick

for(%b = %group.getCount(); %b >= 0; %b--)
{
   %group.getObject(%b).delete();
}


When you do it incrementally, it'll delete object 0 just fine, then go to delete object 1, but what was object 1 is now object 0, which will stay there and never get deleted because %b has already iterated past it. It'll keep doing that until it gets halfway through, then there's no objects left to delete, because everytime you delete one, the ones ahead of it shift down one
« Last Edit: July 09, 2014, 03:30:07 PM by Headcrab Zombie »

oh yeah, that makes sense, because when you delete object 1 of a group for example, it reorganizes the group and what you think would be the second object would be moved to index 1 and you get inaccuracies, so only half the group gets cleared. cool!

you should seriously use chaindeleteall though, it's what the default code does

edit: seems headcrab covered the first thing
« Last Edit: July 09, 2014, 03:40:30 PM by Valcle »

so basically instead of doing

Code: [Select]
for(%b = 0; %b < %group.getCount(); %b++)
    %group.getObject(%b).delete();

you should do

Code: [Select]
while(%group.getCount())
    %group.getObject(0).delete();

or do what Valcle suggested.

-snip-
Woah, cool! Never knew that. But if doing it using -- is "iterate", then what's it called when you use ++?

Woah, cool! Never knew that. But if doing it using -- is "iterate", then what's it called when you use ++?
No, -- is decrement, ++ is increment
iteration is something else
« Last Edit: July 09, 2014, 08:11:42 PM by Headcrab Zombie »

...or you could just store the size of the count that way you're not calling a function every single iteration of the loop.

The reason it's not working is because %i is rising by one while ::getCount() is decreasing by one each time. So say you're clearing 10 bricks:


%b < count
----------
00 < 10
01 < 09
02 < 08
03 < 07
04 < 06
05 < 05


It doesn't keep going because %b is no longer less than the count of the group, yet only half the bricks are deleted.

Code: [Select]
%mainBrickCount = MainBrickGroup.getCount();
for (%i = 0; %i < %mainBrickCount; %i++)
{
%group = MainBrickGroup.getObject(%i);
        %groupCount = %group.getCount();
for (%b = 0; %b < %groupCount; %b++)
{
%group.getObject(0).delete();
}
}

Also, you'll want to delete object 0 for the same reason, after half the bricks are deleted %b will be larger than the count of the group, meaning it'll be calling ::delete() on a nonexistant object. 0 will always be an object in the group as long as the group is not empty. You also save the interpreter from having to look up the value of %b every iteration, which is a slight performance improvement. In practice, it would probably be negligible, but some people like practicing efficient programming even when the improvement is minor.
« Last Edit: July 09, 2014, 09:56:01 PM by $trinick »

Doing the chainDeleteAll on a dedicated server will result in a lot of lag.

Make an AIConnection, set its isAdmin to 1, and do serverCmdClearAllBricks with it. Do a loop check that will do a function when getBrickCount() returns 0. It is important to not delete the AIConnection because of a bug with creating and deleting AIConnections.
Code: [Select]
function PhailM_Init()
{
PhailM_LoadStdPrefs();
PhailM_LoadOres();
messageAll('MsgAdminForce',"Waiting for bricks to clear...");
PhailM_AIConnection.isAdmin = 1;
serverCmdClearAllBricks(PhailM_AIConnection);
schedule(3000,MissionCleanup,"PhailM_BrickCountWait");
}

function PhailM_BrickCountWait()
{
PhailM_StackClear();
talk(getbrickcount() SPC "bricks / "@$Pref::Server::BrickLimit@" bricks");
if(getBrickCount() $= "")
{
error("brickcount is null");
return;
}
if(getBrickCount() > 0)
schedule(3000,MissionCleanup,"PhailM_BrickCountWait");
else
schedule(3000,0,"PhailM_Init2");
}
Be careful: http://forum.blockland.us/index.php?topic=229152.0
« Last Edit: July 20, 2014, 04:38:38 PM by Axo-Tak »

Well first of all, servercmdclearallbricks is just going to call chaindeleteall. Second wtf is that code?

Well first of all, servercmdclearallbricks is just going to call chaindeleteall. Second wtf is that code?
Go on a speedkart server and when the round restarts and loads a new map, it will lag for a few minutes.

Go on UnlimitedMining and the server lags for a while after the round restarts.

Go on a speedkart server and when the round restarts and loads a new map, it will lag for a few minutes.

Go on UnlimitedMining and the server lags for a while after the round restarts.
Your point is?