Jump to content

Finding closest Block and tileEntity


DarkGuardsman

Recommended Posts

For a mod i'm working on i have a function that scans from a start location out so many blocks. This works fine for getting the first instance of a block in from the corner of the scan area; However, it doesn't work for getting the closest block to a location. How would i program it so it will find the closest block too the location. Which the location is going to be an entity which moves.

 

 

Also another question how would i find the closest instanceof a tileEntity

Here is the code i'm using to find it but it doesn't seem to be working

 /**
  * used to find the closest controller
  * @param par1 - x
  * @param par3 - y
  * @param par5 - z
  * @param par7 - range in blocks
  * @return the nearest controller
  */
	 public TileEntityComp getClosestControler(double par1, double par3, double par5, double par7)
    {
	 double var9 = -1.0D;
	    TileEntityComp var11 = null;
	    List itemList = worldObj.getEntitiesWithinAABB(TileEntityComp.class, this.boundingBox.expand(par7, 4.0D, par7));
	    for (int var12 = 0; var12 < itemList.size(); ++var12)
	    {
	    	TileEntityComp var13 = (TileEntityComp)itemList.get(var12);
	        double var14 = var13.getDistanceFrom(par1, par3, par5);

	        if ((par7 < 0.0D || var14 < par7 * par7) && (var9 == -1.0D || var14 < var9))
	        {
	            var9 = var14;
	            var11 = var13;
	        }
	    }

	    return var11;
    }
 /**
  * used to find the closest controller with an empty slot mainly used to find and add a robot to the controller
  * @param par1 - x
  * @param par3 - y
  * @param par5 - z
  * @param par7 - range in blocks
  * @return the nearest controller block with an empty control slot
  */
 public TileEntityComp getEmptyControler(double par1, double par3, double par5, double par7)
    {
	 double var9 = -1.0D;
	    TileEntityComp var11 = null;
	    List itemList = worldObj.getEntitiesWithinAABB(TileEntityComp.class, this.boundingBox.expand(par7, 4.0D, par7));
	    for (int var12 = 0; var12 < itemList.size(); ++var12)
	    {
	    	TileEntityComp var13 = (TileEntityComp)itemList.get(var12);
	        double var14 = var13.getDistanceFrom(par1, par3, par5);

	        if ((par7 < 0.0D || var14 < par7 * par7) && (var9 == -1.0D || var14 < var9))
	        {
	        	for(int c = 0; c < 4; c++)
	        	{
	        	if(var13.BotList[c]==null)
	        	{
	        		 var11 = var13;
	        	}
	        	}
	            var9 = var14;		           
	        }
	    }

	    return var11;
    }

Link to comment
Share on other sites

Time to do a search of one at a time.  ;)

already doing one by one search just from the corner of a search box that was expanded from the target location. Was hoping there might be a hidden easier way to get the closest block to a location. My method works just causes a ton of lag if searching anything over 20x20x20 area.

Link to comment
Share on other sites

Spread it over multiple ticks?

hmm i have it setup to wait 2 ticks every y level it scans which has reduced the lag some. I'm trying to reduce it further so i can scan areas like 40x40x256 without lagging the player at all. though i might break the areas up into 10x10x10 then scan and move to the next area.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.