Author Topic: Detecting bricks nearby issue  (Read 776 times)

Block City uses a relatively simple brick detection system to determine if a baseplate is next one that is trying to be planted so that lots are not mis-aligned. This current alignment system works but with an issue I am unable to determine. All baseplates can properly detect if other baseplates are on their sides except for the 64x64 Baseplate when it attempts to see if loaded baseplates are next to it. The 64x64 baseplate has no problem detecting if other baseplates are next to it if they were just planted and only occurs with loaded save baseplates. No other baseplate has this issue as well.

Any help would be appreciated.
Code: [Select]
function fxDTSBrick::getSurroundingBaseplateCount(%brick)
{
%hitCount = 0;

%sizex = %brick.getDatablock().bricksizex;
%sizey = %brick.getDatablock().bricksizey;

%startPos = posFromTransform(%brick.getTransform());

%lengthy = ((%sizey / 2) / 2) + 0.1;
%lengthx = ((%sizex / 2) / 2) + 0.1;

//Y+
%endPos = vectorAdd(%startPos,"0" SPC %lengthy SPC "0");
%object = firstWord(containerRaycast(%startPos,%endPos,$TypeMasks::FxBrickAlwaysObjectType,%brick));

if(isObject(%object))
{
%hitCount++;
}
//Y-
%endPos = vectorSub(%startPos,"0" SPC %lengthy SPC "0");
%object = firstWord(containerRaycast(%startPos,%endPos,$TypeMasks::FxBrickAlwaysObjectType,%brick));

if(isObject(%object))
{
%hitCount++;
}
//X+
%endPos = vectorAdd(%startPos,%lengthx SPC "0 0");
%object = firstWord(containerRaycast(%startPos,%endPos,$TypeMasks::FxBrickAlwaysObjectType,%brick));

if(isObject(%object))
{
%hitCount++;
}
//X-
%endPos = vectorSub(%startPos,%lengthx SPC "0 0");
%object = firstWord(containerRaycast(%startPos,%endPos,$TypeMasks::FxBrickAlwaysObjectType,%brick));

if(isObject(%object))
{
%hitCount++;
}

return %hitCount;
}


Badspot's Script_TerrainBuildRules uses this to determine if a 64x baseplate is lined up with another 64x baseplate. It may help.

Code: [Select]
      // Also, it better be lined up with an existing baseplate
      // Search for nearby plates and check their relative position
      %pos = %plantedBrick.getPosition();
      %box = "32.1 32.1 0.1";
      %mask = $TypeMasks::FxBrickAlwaysObjectType;

      %plantX = getWord(%plantedBrick.getPosition(), 0);
      %plantY = getWord(%plantedBrick.getPosition(), 1);

      initContainerBoxSearch(%pos, %box, %mask);
      while (%searchObj = containerSearchNext())
      {
         if(%searchObj.getDataBlock().getID() != brick64x64fData.getID())
            continue;
         
         if(%searchObj.getDistanceFromGround() != 0)
            continue;
         
         if(!%searchObj.isPlanted())
            continue;
         
         if(%searchObj.isDead())
            continue;
         
         if(%searchObj == %plantedBrick)
            continue;
         
         //To be lined up, the x or y position must be the same
         %searchX = getWord(%searchObj.getPosition(), 0);
         %searchY = getWord(%searchObj.getPosition(), 1);
         
         // Note: We compare with subtraction like this because direct
         // comparison with "==" on floating point numbers is not reliable
         if(mAbs(%searchX - %plantX) < 0.05 || mAbs(%searchY - %plantY) < 0.05 )
            return;
      }
     
      //If we got this far it must not have been lined up with any existing plates
      CommandToClient(%client, 'MessageBoxOK', "Invalid Placement", "You must line up your baseplate with the existing grid.");
      %plantedBrick.killBrick();

That baseplate alignment system that I first posted is modeled after Badspot's TerrainBuildRules.

Everything evaluates the same from a newly-planted brick to a loaded save brick until it tries to find the object with a container raycast. I don't understand why a loaded brick cannot be detected yet a newly-planted brick has no trouble at all.

It might just be a rounding error or something. Why are you cutting it so close anyway? ((%sizey / 2) / 2) + 0.1
Try using %sizey or (%sizey / 2) - 0.1 and then making sure its position is good.