Jump to content

[Solved]Making Vanilla Entities Aggro vs Mod entities (AI/targeting)


shadowmage4513

Recommended Posts

Hi all,

 

I was wondering if anyone is aware of a method for setting vanilla entities to be aggro vs. another entity?

 

For example my mod introduces new NPCs.  I would like zombies/skeletons/etc to attack these entities as they would a player.

 

I've tried a couple solutions with out (much) luck.

 

First --

Using entity join world event to add targeting tasks (more for the zombie than others) -- I can get the zombie to 'look at' my NPCs, but their speed is not being set properly for the task (and moveSpeed field is protected/not accessible from the event).  I could manually set the speed for the mob, but would still need to be able to access the moveSpeed field. (or manually map class -> speed for a lookup table)  Also, this method will only work for 'new AI enabled' mobs as far as I can tell (zombie, skeleton).

 

//@ForgeSubscribe
//public void onEntitySpawn(EntityJoinWorldEvent evt)
//  {
//  if(evt.entity instanceof EntityMob)
//    {
//    EntityMob zomb = (EntityMob)evt.entity;
//    Config.logDebug("setting entity attack tasks for: "+zomb);
//    zomb.tasks.addTask(3, new EntityAIAttackOnCollide(zomb, NpcBase.class, zomb.getAIMoveSpeed(), true));  //<--yes I know getAIMoveSpeed() is incorrect......
//    zomb.targetTasks.addTask(2, new EntityAINearestAttackableTarget(zomb, NpcBase.class, 16.0F, 0, true));
//    }
//  }

 

Second--

Tried having my NPCs 'broadcast' attack/revenge targets to nearby EntityMob entities.  Works good for skeletons, but not on zombie pig-men (didn't really check past that).

 

protected void broadcastAggro()  
  {
  List<EntityMob> mobs = worldObj.getEntitiesWithinAABB(EntityMob.class, AxisAlignedBB.getAABBPool().getAABB(posX-16, posY-8, posZ-16, posX+16, posY+8, posZ+16));
  for(EntityMob mob : mobs)
    {
    if(mob.getAttackTarget()==null)
      {
      Config.logDebug("setting mob attack target");
      mob.setRevengeTarget(this);
      }
    }
  }

 

 

So, I've got a  couple of 'hacks' that 'work' for specific mobs, but are not something I really want to implement, and I'm really looking for something a bit more abstract that will work for _all_ vanilla mobs (and hopefully at least some mod-entities/mobs).

 

Is anyone aware of a 'proper' method to handle this?

 

Thanks in advance,

Link to comment
Share on other sites

I guess you can just take a look at EntityWolf. In this base class of minecraft you can probably find a method that makes the wolf attack sheep. I think it can't be too hard.

 

That's the wrong way around though. The method there is in the wolf, not the sheep; he wants to make the vanilla thing (wolf as example) attack his mod mobs

I don't know how to do it but I am curious, it's something I want too :P

(Off-topic; will the NPCs be scriptable at all? (like Citizens for bukkit))

width=463 height=200

http://s13.postimg.org/z9mlly2av/siglogo.png[/img]

My mods (Links coming soon)

Cities | Roads | Remula | SilvaniaMod | MoreStats

Link to comment
Share on other sites

Thanks for the reply,

 

Yes, I'm looking for a method to set vanilla entities to be hostile towards mine. 

 

I'm thinking I could probably use the two methods I pointed out above, but they would be specialized per-vanilla mob, and not really what I'm looking for (e.g. I would have to set zombie AI one way, skeleton another, witches yet another, etc...).  I was hoping someone had come across and solved this one before (or at least that I had missed something obvious in the MC source).

 

 

Off topic:

The NPCs are more like assorted RTS units -- peons/workers, melee units, archers, siege-engine drivers.  Not custom-scriptable or storyline oriented -- more production/resource/combat oriented.

Link to comment
Share on other sites

That's the wrong way around though. The method there is in the wolf, not the sheep; he wants to make the vanilla thing (wolf as example) attack his mod mobs

I don't know how to do it but I am curious, it's something I want too

 

Well you say, the wrong way around. But all that i say is that the method found in EntityWolf can be used to make it so that whatever entity you want to attack your own mob or a vanilla mob. I'm not sure how to do this without altering base classes because i am not really going to do something similar but i think the method is not that complicated or is it?

Link to comment
Share on other sites

I tried to do the same thing with my turrets. Works quite well, except of the entities move faster than usual when targeted, so you have to ajust the speed (so do I :P).

Everytime the projectile hits a target, the target will 'target' the shooting entity (the turret).

Have some code:

https://github.com/SanAndreasP/TurretModv3/blob/master/sanandreasp/mods/TurretMod3/entity/projectile/TurretProjectile.java#L381

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

Thanks SanAndreasP,

 

I think the move-speed thing can be solved 'fairly' easily (more reflection to pull the default moveSpeed out of the mob).

 

Your solution might be the way to go.  Trying to insert AI tasks will be problematic with some mobs, but it looks like most mobs use the revenge-target/attack target methods to at least some effect. 

 

I was really hoping to find a method to set during mob spawning/creation, and not have to have my npcs 'broadcast' for aggro at all, but its looking like it won't really be feasible with the current state of things.

 

 

One final question  -- I've mostly stayed away from reflection for various reasons (specifically reflecting on obfuscated code) -- I see that you are pulling the private field by field name --  If I go this route, all I should really have to worry about is renaming the field in my code if the name changes in an MC update ?  (e.g. how stable is it generally between forge/MCP versions for the same MC version?)

Link to comment
Share on other sites

Thanks SanAndreasP,

 

I think the move-speed thing can be solved 'fairly' easily (more reflection to pull the default moveSpeed out of the mob).

 

Your solution might be the way to go.  Trying to insert AI tasks will be problematic with some mobs, but it looks like most mobs use the revenge-target/attack target methods to at least some effect. 

 

I was really hoping to find a method to set during mob spawning/creation, and not have to have my npcs 'broadcast' for aggro at all, but its looking like it won't really be feasible with the current state of things.

 

 

One final question  -- I've mostly stayed away from reflection for various reasons (specifically reflecting on obfuscated code) -- I see that you are pulling the private field by field name --  If I go this route, all I should really have to worry about is renaming the field in my code if the name changes in an MC update ?  (e.g. how stable is it generally between forge/MCP versions for the same MC version?)

 

You see the 2 string parameters. The first one is the name given by MCP, so you can test it. The second one is the SRG name of that field. It's like an index given by MCP before the real naming occurs. (path of deobfuscation goes like >> Notch (obfuscated) names -> SRG (index) names -> deobfuscated (community-driven) names <<

The Notch names and the deobf-names are changing through MC updates, where SRG names are unlikely to change (except the fields / methods have changed or been removed or something like that).

Forge comes with "runtime-deobfuscation", meaning it converts the Notch names to SRG names and thus mods don't need to update unless a part in MC changed which the mod uses.

To get those SRG names, open the '/forge/mcp/conf/fields.csv' and look up the name. The SRG name is the very first (starts with 'field_'). For methods it's in the 'method.csv' in the same directory.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

Awesome, thanks for the crash course in obfuscation.  Your explanation straightens out a couple things I had wondered about (SRG naming and consistency).

 

I'll let you know if I run across any other solutions to this problem, but will probably be using similar to what you have proposed.

 

 

Thanks again :)

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.