Jump to content
  • Home
  • Files
  • Docs
  • Merch
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.11.2] Trying to filter entity attack targets
1.13 Update Notes for Mod Creators
Sign in to follow this  
Followers 1
Triphion

[1.11.2] Trying to filter entity attack targets

By Triphion, February 10 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

Triphion    0

Triphion

Triphion    0

  • Stone Miner
  • Triphion
  • Members
  • 0
  • 95 posts
Posted February 10 (edited)

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 February 10 by Triphion
  • Quote

Share this post


Link to post
Share on other sites

Meldexun    6

Meldexun

Meldexun    6

  • Creeper Killer
  • Meldexun
  • Members
  • 6
  • 184 posts
Posted February 10

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.

  • Quote

Share this post


Link to post
Share on other sites

Triphion    0

Triphion

Triphion    0

  • Stone Miner
  • Triphion
  • Members
  • 0
  • 95 posts
Posted February 11 (edited)
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 February 11 by Triphion
  • Quote

Share this post


Link to post
Share on other sites

Meldexun    6

Meldexun

Meldexun    6

  • Creeper Killer
  • Meldexun
  • Members
  • 6
  • 184 posts
Posted February 11
1 hour ago, Triphion said:

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

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

  • Quote

Share this post


Link to post
Share on other sites

Triphion    0

Triphion

Triphion    0

  • Stone Miner
  • Triphion
  • Members
  • 0
  • 95 posts
Posted February 11
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

  • Quote

Share this post


Link to post
Share on other sites

Triphion    0

Triphion

Triphion    0

  • Stone Miner
  • Triphion
  • Members
  • 0
  • 95 posts
Posted February 11 (edited)
        	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 February 11 by Triphion
  • Quote

Share this post


Link to post
Share on other sites

Meldexun    6

Meldexun

Meldexun    6

  • Creeper Killer
  • Meldexun
  • Members
  • 6
  • 184 posts
Posted February 11
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.

 

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • diesieben07
      [1.14.4] Modifying the containers & screens of vanilla entities

      By diesieben07 · Posted 2 minutes ago

      You are telling the slot to use index 2 of the horse inventory. The default horse inventory only has 2 slots, indices 0 and 1.
    • DragonITA
      [1.14.4] Add a Tab to the Player Inventory Gui in Survival Modus

      By DragonITA · Posted 5 minutes ago

      Ok, i know the Aether Mod, and this Mod Add a new Tab into the Gui of the Player and change the Player Inventory Gui, how to make this?
    • JetCobblestone
      [1.14] layout of a modpack

      By JetCobblestone · Posted 7 minutes ago

      Right. Can you please explain to me the things he does wrong in an easy to understand manner? If I shouldn't use Harry Talks, who should I use then? He's seems to have one of the only 1.14 tutorials. Thanks!
    • AdieCraft
      Working Flashing Christmas Tree

      By AdieCraft · Posted 7 minutes ago

      Hello,    Check out the latest video on AdieCraft   A working Redstone Christmas Tree, with all redstone included in the tree itself.   Check out the video and make sure to subscribe for more!   Flashing Redstone Christmas Tree
    • plugsmustard
      on/off button for custom furnace

      By plugsmustard · Posted 23 minutes ago

      addButton(new Button(relX + 18, relY + 15, 5, 20, "PURGE", button -> PacketHandler.sendToServer(new PacketButtonClicked(tileEntity.getPos()))));  ????   if that's not right, im gonna snap
  • Topics

    • Choco
      5
      [1.14.4] Modifying the containers & screens of vanilla entities

      By Choco
      Started 3 hours ago

    • DragonITA
      0
      [1.14.4] Add a Tab to the Player Inventory Gui in Survival Modus

      By DragonITA
      Started 5 minutes ago

    • JetCobblestone
      2
      [1.14] layout of a modpack

      By JetCobblestone
      Started 4 hours ago

    • AdieCraft
      0
      Working Flashing Christmas Tree

      By AdieCraft
      Started 7 minutes ago

    • plugsmustard
      29
      on/off button for custom furnace

      By plugsmustard
      Started Yesterday at 03:11 PM

  • Who's Online (See full list)

    • diesieben07
    • nenikitov
    • lopiy
    • deerangle
    • Hendoor64
    • Simon_kungen
    • Choonster
    • plugsmustard
    • Gnaux
    • DragonITA
    • JetCobblestone
    • AdieCraft
    • Temps Pi
    • Choco
    • vaartis
    • Draco18s
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.11.2] Trying to filter entity attack targets
  • Theme
  • Contact Us
  • Discord

Copyright © 2019 ForgeDevelopment LLC · Ads by Curse Powered by Invision Community