Jump to content

[1.7.2] Custom Entity only attack when he sees you.


FLUFFY2

Recommended Posts

Hello there,

I have a problem with my custom mob AI.

I try to make it so it can only attack you if he sees you.

So if its too dark he can only see you if you are close enough.

If its light he can only see you if he saw you.

I tryed the following but its not worked:

 

if(par1World.getLightBrightness((int)this.posX, (int)this.posY, (int)this.posZ) >= 4 &&
getEntitySenses().canSee(player))
		this.targetTasks.addTask(2, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 0, true));

if(par1World.getLightBrightness((int)this.posX, (int)this.posY, (int)this.posZ) < 4 && getEntitySenses().canSee(player) && getEntityAttribute(SharedMonsterAttributes.followRange).getAttributeValue() >= 5.0D)
		this.targetTasks.addTask(2, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 0, true));

 

Thanks!

Link to comment
Share on other sites

Okay, you're not using AI tasks properly.  You actually should NOT be adding and removing AI tasks.  Rather, each AI task has a method shouldExecute() which you use to detect the condition you want go run.  You should be able to detect the light conditions, proximity of player, or whatever you want for your case there.  So the AI task should always be on the list, but shouldExecute() should only return true in condition you care about.

 

Also, like GoToLink mentions, you're continuing to add tasks without ever removing them.  I believe that the number ("2" or whatever) in the addTask() method is NOT actually an index, but rather a priority indicator, and is not unique.  In other words, adding another task with same priority will not overwrite the previous task.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

jabelar,

correct me if I'm wrong, but that is only going to work if only one player is in range.  If two players are in range and one isn't seeable, it will ignore both. 

 

He is not going to get there simply.

Long time Bukkit & Forge Programmer

Happy to try and help

Link to comment
Share on other sites

jabelar,

correct me if I'm wrong, but that is only going to work if only one player is in range.  If two players are in range and one isn't seeable, it will ignore both. 

 

He is not going to get there simply.

 

The AI is run on the server side per entity instance, so you would have ability to check for any player or any other condition.  I wasn't suggesting any particular method, just explaining that the shouldExecute() method is how you control the execution, not addding/removing the task from the list itself.

 

So in the shouldExecute() he could cycle through all the players the server knows are in that world, see if any of them are visible (based on lighting as well in his case) and if so then set the attack target accordingly.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

If two were in range, one was closer but not seeable, that could lead to it getting attacked if I understand how it works right.  I could be wrong though.

Long time Bukkit & Forge Programmer

Happy to try and help

Link to comment
Share on other sites

If two were in range, one was closer but not seeable, that could lead to it getting attacked if I understand how it works right.  I could be wrong though.

 

No, I'm suggesting that you loop through all players (that the server knows is on that world), check first that they are within the maximum distance (based on the amount of light) and then check if they can be seen.  If BOTH conditions are true, set that player as the attack target.

 

Ideally, you want to make it more fair because the above algorithm will choose the player that joined earlier if more than one are in range and in sight, so a better implementation would be to add all the possible targets to a list or array and then randomly choose from that list.

 

Anyway, I'm saying find a player that meets both conditions (within range of the light, and also actually in direct line of sight).

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.

Announcements



×
×
  • Create New...

Important Information

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