Modding 1.4.7: Basic Mob Part 3: AI

In this tutorial I will explain the basics of AI. This is the Entity file from the end of the last tutorial.


package tutorial;

import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.EnumCreatureAttribute;
import net.minecraft.world.World;


public class EntityTutorial extends EntityMob
{
public EntityTutorial(World par1World) 
{
super(par1World);
this.texture = “/Tutorial/TutorialEntity.png”;
this.moveSpeed = 0.25F;
}


public int getAttackStrength(Entity par1Entity)
    {
    return 4;
    }
public int getMaxHealth() 
{
return 20;
}

public EnumCreatureAttribute getCreatureAttribute()
    {
        return EnumCreatureAttribute.UNDEAD;
    }
}


There are 2 ways to add AI to a mob in Minecraft. The first one is by using the method updateEntityActionState(), but I will not show you how to do that in this tutorial, because that is an old and not very efficient way of adding AI. If you want to see how this is done you will have to look at the EntityGhast file.
The new way of AI is a lot better. To get it to work you will need this method in your entity file.


protected boolean isAIEnabled()
    {
        return true;
    }


This will not really do anything on itself. To get this to work you will have to add the actual code to the constructor of the entity.
Most of the Ai code inside of the entity file will look like this.


this.tasks.addTask(0, new EntityAISwimming(this));


It’s always this (which means this file). After that you get tasks or targetTasks (will get back to that later) and than .addTask().
Inside of the addTask method you have a number and an AI file. The number is the priority of the AI. The higher the number the faster it will do it. The AI file is simply the AI it will do when that AI is chosen.
If you want your mob to be able to swim I suggest getting the Swimming ai is number 0 (which is the first number in java). This will make sure that if it is in water the first thing it will do is swim up.
Once you have added several of those AI lines the file should look something like this.


package tutorial;

import net.minecraft.entity.EnumCreatureAttribute;

import net.minecraft.entity.ai.EntityAIAttackOnCollide;
import net.minecraft.entity.ai.EntityAIBreakDoor;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;


public class EntityTutorial extends EntityMob
{
public EntityTutorial(World par1World) 
{
super(par1World);
this.texture = “/Tutorial/TutorialEntity.png”;
this.moveSpeed = 0.25F;
this.tasks.addTask(0, new EntityAISwimming(this));
                this.tasks.addTask(1, new EntityAIBreakDoor(this));
                this.tasks.addTask(2, new EntityAIAttackOnCollide(this, EntityPlayer.class, this.moveSpeed, false));
                this.tasks.addTask(3, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
                this.tasks.addTask(4, new EntityAIWander(this, this.moveSpeed));
}

public int getAttackStrength(Entity par1Entity) { return 4; }

protected boolean isAIEnabled()
    {
        return true;
    }

public int getMaxHealth() 
{
return 20;
}

public EnumCreatureAttribute getCreatureAttribute()
    {
        return EnumCreatureAttribute.UNDEAD;
    }
}


Some of the methods will need some more parameters than just this. For example EntityAIWander needs the movement speed of the entity file.
With the AI code inside of the file right now it will only wander around and hurt the player if he walks into him. To actually make it go after the player you will need the targetTasks. AI code with target tasks looks like this.


this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, false));


This code will simply attack any entity that hurts this entity. To make it go after something like a player you will need this.


this.targetTasks.addTask(2, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 16.0F, 0, true));


The first parameter is always this. The second is the entity it’s going to chase and the 16F is the distance.

The whole file should now look something like this.


package tutorial;
import net.minecraft.entity.EnumCreatureAttribute;
import net.minecraft.entity.ai.EntityAIAttackOnCollide;
import net.minecraft.entity.ai.EntityAIBreakDoor;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;


public class EntityTutorial extends EntityMob
{
public EntityTutorial(World par1World) 
{
super(par1World);
this.texture = “/Tutorial/TutorialEntity.png”;
this.moveSpeed = 0.25F;
this.tasks.addTask(0, new EntityAISwimming(this));
        this.tasks.addTask(1, new EntityAIBreakDoor(this));
        this.tasks.addTask(2, new EntityAIAttackOnCollide(this, EntityPlayer.class, this.moveSpeed, false));
        this.tasks.addTask(3, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
        this.tasks.addTask(4, new EntityAIWander(this, this.moveSpeed));
        this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, false));
        this.targetTasks.addTask(2, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 16.0F, 0, true));
}

public int getAttackStrength(Entity par1Entity) { return 4; }

protected boolean isAIEnabled()
    {
        return true;
    }

public int getMaxHealth() 
{
return 20;
}

public EnumCreatureAttribute getCreatureAttribute()
    {
        return EnumCreatureAttribute.UNDEAD;
    }
}


There will be more about the entity file in the next tutorial. The source code will also not be avaliable until the last tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">