Jump to content

[1.11.2] Trying to filter entity attack targets


Triphion

Recommended Posts

What i'm trying to achieve is a filtering system where my loyal undeads will select its next target based on a list that will get updated in updateAITasks. I think i have almost got it, but i'm still running into a few problems, such as the entity selecting a few targets that is not supposed to be targeted. To make sure this does what it's supposed to, i made a targetList which creates an aabb around the entity and then checks if there are any allies in that aabb, if there are, it adds the allies in the aabb. Or atleast is supposed to, but my problem is that i don't know how to reference all the friendly ones and then add them all at the same time. 

 

My entity class = https://github.com/triphion/Ancient-Mod/blob/master/src/main/java/ancient/entity/EntityUndead

 

Worth noting is the way i'm adding and filtering. So first of i start with checking if the current target is null and if the targetList has any loyal undeads or players in it. If it has, then it's supposed to add them all. Then i remove them from the targetList and then add all of the remaining entities in the enemyList. Then every time this method gets called, so as to not overexert anything, i remove all the entities in the enemyList and then the process continue. 

Edited by Triphion
Link to comment
Share on other sites

16 hours ago, Meldexun said:

You could make a for loop and check for every entity in the targetList if it is an ally or a player and then add it to your friendlyList.

    	if (this.getAttackTarget() == null && this.isAlly()) 
    	{
        	for (Entity entity : this.targetList) 
        	{
        		if (entity instanceof EntityUndead && ((EntityUndead) entity).isAlly() && !friendlyList.contains(entity)) 
        		{
        			friendlyList.add(entity);
        		}
        		
        		if (entity instanceof EntityPlayerMP && !friendlyList.contains(entity)) 
        		{
        			friendlyList.add(entity);
        		}
        	}
    	}

Did it and it works, now however i need to check for all entities in the enemy list for the one that is closest to the entity. Any suggestions? 

 

Edit: I realized that the game crashes when there's too many loyal undeads. Not sure where in the code it gets overexerted. 

Edited by Triphion
Link to comment
Share on other sites

1 hour ago, Meldexun said:

Probably because you set the size of your friendlyList and enemyList to 32

Tried a much higher limit aswell as no limit. But it still crashes. Any other suggestion on why it crashes? It gets called too often in updateAITasks and i should use some other update method? 

 

Undead class: https://github.com/triphion/Ancient-Mod/blob/master/src/main/java/ancient/entity/EntityUndead

Link to comment
Share on other sites

        	for (Entity entity : this.friendlyList) 	
        	{
        		if (entity.isDead) 
        		{
        			this.friendlyList.remove(entity);
        		}
        	}

Noticed aswell that if an undead dies, it crashes, i tried making this method to fix it, but to no avail. 

 

Edit: fixed it. Removed the method in its entirety and it somehow solved the problem ¯\_(ツ)_/¯

 

But i still need help regarding selecting target that is closest to the entity. 

    @Override
  public void setAttackTarget(EntityLivingBase entitylivingbaseIn) 
  {
  	if (this.canTarget) 
  	{
  		this.enemyList.removeAll(this.friendlyList);
  		Entity enemy = (Entity) this.enemyList.get(0);
  		double d0 = this.getDistanceSqToEntity((Entity) enemy);
  		EntityLivingBase entityToAttack = (EntityLivingBase) enemy;
  		
  		if (d0 < 30.0D && d0 > 0) 
  		{
  			entitylivingbaseIn = (EntityLivingBase) enemy;
      		super.setAttackTarget(entitylivingbaseIn);
  		}
  		
  		else
  		{
  			super.setAttackTarget(null);
  		}
  	 }
  }

I've started with this, but it's nowhere near done. So i need a few pointers to get the target i want. 

 

Edit: I fixed it! Thanks for all your help guys! ^^

    @Override
  public void setAttackTarget(EntityLivingBase entitylivingbaseIn) 
  {
  	if (this.canTarget) 
  	{
        for (Entity entity : this.enemyList)
        {
            double d0 = this.getDistanceSqToEntity(entity);

            if (d0 < 40.0D)
            {
            	super.setAttackTarget((EntityLivingBase) entity);
            }
        }
  	}
  	
  	else 
  	{
		super.setAttackTarget(null);
  	}
  }
@Override
	protected void updateAITasks() 
	{
		this.targetList = this.world.getEntitiesWithinAABBExcludingEntity(this, new AxisAlignedBB((double)this.posX - 17.5D, (double)this.posY - 17.5D, (double)this.posZ - 17.5D, (double)this.posX + 17.5D, (double)this.posY + 17.5D, (double)this.posZ + 17.5D));
		
    	if (this.getAttackTarget() == null && this.isAlly()) 
    	{
        	
    		for (Entity entity : this.targetList) 
        	{
        		if (entity instanceof EntityUndead && ((EntityUndead) entity).isAlly() && !this.friendlyList.contains(entity)) 
        		{
        			this.friendlyList.add(entity);
        		}
        		
        		if (entity instanceof EntityPlayerMP && !this.friendlyList.contains(entity)) 
        		{
        			this.friendlyList.add(entity);
        		}
        	}
    	}
    	
    	else if (!(this.getAttackTarget() == null)) 
    	{
    		this.canTarget = false;
    		
    		if (this.friendlyList.contains(this.getAttackTarget())) 
    		{
    			this.setAttackTarget(null);
    		}
    	}
    		
    	this.targetList.removeAll(this.friendlyList);
    	this.enemyList.clear();
    	this.enemyList.addAll(this.targetList);
    		
    	if (!this.enemyList.isEmpty()) 
    	{
    		this.canTarget = true;
    	}
    	else if (this.enemyList.isEmpty())
    	{
    		this.canTarget = false;
    	}
    }

 

Edited by Triphion
Link to comment
Share on other sites

1 hour ago, Triphion said:

But it still crashes.

Can you post your crash report?

1 hour ago, Triphion said:

But i still need help regarding selecting target that is closest to the entity. 

You could compare the distance of the first entity in the list with all the other entities in the list. And when a distance is smaller you could go on with comparing that distance.

 

And if you just need the lists of entities for setting the attack target then you could also just search for the nearest netity that is not an ally.

 

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.