Major Butterfly AI rework, they should be less derpy about getting stuck in holes and flying over oceans.

Various other tweaks and improvements as well, including a fix for the spazzy wing movement that 1.7 introduced.
This commit is contained in:
CovertJaguar committed 2014-11-13 02:14:17 -08:00
1 parent aea3c55192
commit 4609ebc754
16 files changed
+446 -246

No files matched your search

@@ -393,7 +393,7 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru
boolean wasDestroyed = isDestroyed();
encumbrance += caterpillar.getGenome().getMetabolism();
wasDestroyed = !wasDestroyed && isDestroyed() ? true : false;
wasDestroyed = !wasDestroyed && isDestroyed();
if(maturationTime >= (float)caterpillar.getGenome().getLifespan() / (caterpillar.getGenome().getFertility()*2)
&& caterpillar.canTakeFlight(worldObj, xCoord, yCoord, zCoord)) {
@@ -10,6 +10,7 @@
******************************************************************************/
package forestry.lepidopterology.entities;
import net.minecraft.block.Block;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.ai.RandomPositionGenerator;
import net.minecraft.util.ChunkCoordinates;
@@ -24,7 +25,7 @@ public abstract class AIButterflyBase extends EntityAIBase {
}
protected ChunkCoordinates getRandomDestination() {
if(entity.isInWater())
if (entity.isInWater())
return getRandomDestinationUpwards();
Vec3 randomTarget = RandomPositionGenerator.findRandomTargetBlockAwayFrom(entity, 16, 7,
@@ -33,29 +34,31 @@ public abstract class AIButterflyBase extends EntityAIBase {
if (randomTarget == null)
return null;
ChunkCoordinates dest = new ChunkCoordinates((int)randomTarget.xCoord, (int)randomTarget.yCoord, (int)randomTarget.zCoord);
if(validateDestination(dest))
ChunkCoordinates dest = new ChunkCoordinates((int) randomTarget.xCoord, (int) randomTarget.yCoord, (int) randomTarget.zCoord);
if (validateDestination(dest, false))
return dest;
else
return null;
}
protected ChunkCoordinates getRandomDestinationUpwards() {
ChunkCoordinates dest = new ChunkCoordinates((int)entity.posX, (int)entity.posY + entity.worldObj.rand.nextInt(10) + 2, (int)entity.posZ);
if(validateDestination(dest))
ChunkCoordinates dest = new ChunkCoordinates((int) entity.posX, (int) entity.posY + entity.getRNG().nextInt(10) + 2, (int) entity.posZ);
if (validateDestination(dest, true))
return dest;
else
return null;
}
protected boolean validateDestination(ChunkCoordinates dest) {
if(!entity.worldObj.isAirBlock(dest.posX, dest.posY, dest.posZ) || dest.posY < 1)
protected boolean validateDestination(ChunkCoordinates dest, boolean allowFluids) {
if (dest.posY < 1)
return false;
if(!entity.getButterfly().isAcceptedEnvironment(entity.worldObj, dest.posX, dest.posY, dest.posZ))
Block block = entity.worldObj.getBlock(dest.posX, dest.posY, dest.posZ);
if (!allowFluids && block.getMaterial().isLiquid())
return false;
return true;
// getBlocksMovement is a bad name, getAllowsMovement would be a better name.
if (!block.getBlocksMovement(entity.worldObj, dest.posX, dest.posY, dest.posZ))
return false;
return entity.getButterfly().isAcceptedEnvironment(entity.worldObj, dest.posX, dest.posY, dest.posZ);
}
}
@@ -11,14 +11,11 @@
package forestry.lepidopterology.entities;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ChunkCoordinates;
import forestry.lepidopterology.entities.EntityButterfly.EnumButterflyState;
public class AIButterflyFlee extends AIButterflyBase {
public class AIButterflyFlee extends AIButterflyMovement {
private EntityPlayer player;
private ChunkCoordinates flightTarget;
public AIButterflyFlee(EntityButterfly entity) {
super(entity);
@@ -48,45 +45,4 @@ public class AIButterflyFlee extends AIButterflyBase {
return true;
}
@Override
public boolean continueExecuting() {
if (entity.getState() != EnumButterflyState.FLYING)
return false;
if (flightTarget == null)
return false;
// Abort if the flight target changed on us.
if (entity.getDestination() == null || !entity.getDestination().equals(flightTarget))
return false;
// Continue if we have not yet reached the destination.
if (entity.getDestination().getDistanceSquared((int) entity.posX, (int) entity.posY, (int) entity.posZ) > 2.0f)
return true;
entity.setDestination(null);
return false;
}
@Override
public void startExecuting() {
}
@Override
public void resetTask() {
flightTarget = null;
}
@Override
public void updateTask() {
// Reset destination if we did collide.
if (entity.isInWater()) {
flightTarget = getRandomDestinationUpwards();
entity.setDestination(flightTarget);
}
if (entity.isCollided || entity.worldObj.rand.nextInt(300) == 0) {
flightTarget = getRandomDestination();
entity.setDestination(flightTarget);
}
entity.changeExhaustion(1);
}
}
@@ -10,32 +10,16 @@
******************************************************************************/
package forestry.lepidopterology.entities;
import java.util.Map;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChunkCoordinates;
import forestry.api.arboriculture.ITreeRoot;
import forestry.api.genetics.AlleleManager;
import forestry.api.genetics.IIndividual;
import forestry.api.genetics.IPollinatable;
import forestry.api.lepidopterology.IButterflyNursery;
import forestry.core.proxy.Proxies;
import forestry.core.utils.StackUtils;
import forestry.core.utils.Utils;
import forestry.lepidopterology.entities.EntityButterfly.EnumButterflyState;
public class AIButterflyInteract extends AIButterflyBase {
public abstract class AIButterflyInteract extends AIButterflyBase {
private ChunkCoordinates rest;
protected ChunkCoordinates rest;
private boolean canLayEgg = false;
private boolean canPollinate = false;
private boolean hasLayedEgg = false;
private boolean hasPollinated = false;
private boolean canInteract = false;
private boolean hasInteracted = false;
public AIButterflyInteract(EntityButterfly entity) {
super(entity);
@@ -44,75 +28,23 @@ public class AIButterflyInteract extends AIButterflyBase {
@Override
public boolean shouldExecute() {
if(entity.getState() != EnumButterflyState.RESTING)
if (entity.getState() != EnumButterflyState.RESTING)
return false;
rest = new ChunkCoordinates((int)entity.posX, ((int)Math.floor(entity.posY)) - 1, (int)entity.posZ);
if(entity.worldObj.isAirBlock(rest.posX, rest.posY, rest.posZ))
rest = new ChunkCoordinates((int) entity.posX, ((int) Math.floor(entity.posY)) - 1, (int) entity.posZ);
if (entity.worldObj.isAirBlock(rest.posX, rest.posY, rest.posZ))
return false;
canLayEgg = canLayEgg();
canPollinate = canPollinate();
canInteract = canInteract();
return canLayEgg || canPollinate;
return canInteract;
}
private boolean canLayEgg() {
if(entity.cooldownEgg > 0)
return false;
if(entity.getButterfly().getMate() == null)
return false;
TileEntity tile = entity.worldObj.getTileEntity(rest.posX, rest.posY, rest.posZ);
IButterflyNursery nursery = null;
if(tile instanceof IButterflyNursery)
nursery = (IButterflyNursery)tile;
else {
for (Map.Entry<ItemStack, IIndividual> entry : AlleleManager.ersatzSpecimen.entrySet()) {
if (!StackUtils.equals(entity.worldObj.getBlock(rest.posX, rest.posY, rest.posZ), entry.getKey()))
continue;
int meta = entity.worldObj.getBlockMetadata(rest.posX, rest.posY, rest.posZ);
if (StackUtils.equals(Blocks.leaves, entry.getKey()))
meta = meta & 3;
if (entry.getKey().getItemDamage() != meta)
continue;
// We matched, replace the leaf block with ours and set the ersatz genome
((ITreeRoot)AlleleManager.alleleRegistry.getSpeciesRoot("rootTrees")).setLeaves(entity.worldObj, entry.getValue(), null, rest.posX, rest.posY, rest.posZ);
// Now let's pollinate
nursery = (IButterflyNursery)entity.worldObj.getTileEntity(rest.posX, rest.posY, rest.posZ);
}
}
if(nursery == null)
return false;
return nursery.canNurse(entity.getButterfly());
}
private boolean canPollinate() {
if(entity.cooldownPollination > 0)
return false;
IPollinatable pollinatable = Utils.getOrCreatePollinatable(null, entity.worldObj, rest.posX, rest.posY, rest.posZ);
if(pollinatable == null)
return false;
if(!entity.getButterfly().getGenome().getFlowerProvider().isAcceptedPollinatable(entity.worldObj, pollinatable))
return false;
if(entity.getPollen() != null && !pollinatable.canMateWith(entity.getPollen()))
return false;
return true;
}
protected abstract boolean canInteract();
@Override
public boolean continueExecuting() {
return (canLayEgg && !hasLayedEgg) || (canPollinate && !hasPollinated);
return canInteract && !hasInteracted;
}
@Override
@@ -121,42 +53,13 @@ public class AIButterflyInteract extends AIButterflyBase {
@Override
public void resetTask() {
hasLayedEgg = false;
hasPollinated = false;
canLayEgg = false;
canPollinate = false;
canInteract = false;
hasInteracted = false;
rest = null;
}
@Override
public void updateTask() {
if(canLayEgg && !hasLayedEgg) {
IButterflyNursery tile = (IButterflyNursery)entity.worldObj.getTileEntity(rest.posX, rest.posY, rest.posZ);
if(tile.canNurse(entity.getButterfly())) {
tile.setCaterpillar(entity.getButterfly().spawnCaterpillar(tile));
Proxies.log.finest("A butterfly '%s' laid an egg at %s/%s/%s.", entity.getButterfly().getIdent(), rest.posX, rest.posY, rest.posZ);
if(entity.getRNG().nextFloat() < 1.0f / entity.getButterfly().getGenome().getFertility())
entity.setHealth(0);
}
hasLayedEgg = true;
entity.cooldownEgg = EntityButterfly.COOLDOWNS;
}
if(canPollinate && !hasPollinated) {
IPollinatable pollinatable = (IPollinatable)entity.worldObj.getTileEntity(rest.posX, rest.posY, rest.posZ);
if(entity.getPollen() == null) {
entity.setPollen(pollinatable.getPollen());
//Proxies.log.finest("A butterfly '%s' grabbed a pollen '%s' at %s/%s/%s.", entity.getButterfly().getIdent(), entity.getPollen().getIdent(), rest.posX, rest.posY, rest.posZ);
} else if(pollinatable.canMateWith(entity.getPollen())) {
pollinatable.mateWith(entity.getPollen());
//Proxies.log.finest("A butterfly '%s' unloaded pollen '%s' at %s/%s/%s.", entity.getButterfly().getIdent(), entity.getPollen().getIdent(), rest.posX, rest.posY, rest.posZ);
entity.setPollen(null);
}
hasPollinated = true;
entity.cooldownPollination = EntityButterfly.COOLDOWNS;
}
protected void setHasInteracted() {
hasInteracted = true;
}
}
@@ -0,0 +1,86 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.lepidopterology.entities;
import java.util.Map;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import forestry.api.arboriculture.ITreeRoot;
import forestry.api.genetics.AlleleManager;
import forestry.api.genetics.IIndividual;
import forestry.api.lepidopterology.IButterflyNursery;
import forestry.core.utils.StackUtils;
public class AIButterflyMate extends AIButterflyInteract {
public AIButterflyMate(EntityButterfly entity) {
super(entity);
}
/**
* Should lay egg?
* @return
*/
@Override
protected boolean canInteract() {
if (entity.cooldownEgg > 0)
return false;
if (entity.getButterfly().getMate() == null)
return false;
TileEntity tile = entity.worldObj.getTileEntity(rest.posX, rest.posY, rest.posZ);
IButterflyNursery nursery = null;
if (tile instanceof IButterflyNursery)
nursery = (IButterflyNursery) tile;
else
for (Map.Entry<ItemStack, IIndividual> entry : AlleleManager.ersatzSpecimen.entrySet()) {
if (!StackUtils.equals(entity.worldObj.getBlock(rest.posX, rest.posY, rest.posZ), entry.getKey()))
continue;
int meta = entity.worldObj.getBlockMetadata(rest.posX, rest.posY, rest.posZ);
if (StackUtils.equals(Blocks.leaves, entry.getKey()))
meta = meta & 3;
if (entry.getKey().getItemDamage() != meta)
continue;
// We matched, replace the leaf block with ours and set the ersatz genome
((ITreeRoot) AlleleManager.alleleRegistry.getSpeciesRoot("rootTrees")).setLeaves(entity.worldObj, entry.getValue(), null, rest.posX, rest.posY, rest.posZ);
// Now let's pollinate
nursery = (IButterflyNursery) entity.worldObj.getTileEntity(rest.posX, rest.posY, rest.posZ);
}
if (nursery == null)
return false;
return nursery.canNurse(entity.getButterfly());
}
@Override
public void updateTask() {
if (continueExecuting()) {
IButterflyNursery tile = (IButterflyNursery) entity.worldObj.getTileEntity(rest.posX, rest.posY, rest.posZ);
if (tile.canNurse(entity.getButterfly())) {
tile.setCaterpillar(entity.getButterfly().spawnCaterpillar(tile));
// Proxies.log.finest("A butterfly '%s' laid an egg at %s/%s/%s.", entity.getButterfly().getIdent(), rest.posX, rest.posY, rest.posZ);
if (entity.getRNG().nextFloat() < 1.0f / entity.getButterfly().getGenome().getFertility())
entity.setHealth(0);
}
setHasInteracted();
entity.cooldownEgg = EntityButterfly.COOLDOWNS;
}
}
}
@@ -0,0 +1,64 @@
/*
* Copyright (c) CovertJaguar, 2014 http://railcraft.info
*
* This code is the property of CovertJaguar
* and may only be used with explicit written
* permission unless otherwise specified on the
* license page at http://railcraft.info/wiki/info:license.
*/
package forestry.lepidopterology.entities;
import net.minecraft.util.ChunkCoordinates;
/**
*
* @author CovertJaguar <http://www.railcraft.info/>
*/
public abstract class AIButterflyMovement extends AIButterflyBase {
protected ChunkCoordinates flightTarget;
public AIButterflyMovement(EntityButterfly entity) {
super(entity);
}
@Override
public boolean continueExecuting() {
if (entity.getState() != EntityButterfly.EnumButterflyState.FLYING)
return false;
if (flightTarget == null)
return false;
// Abort if the flight target changed on us.
if (entity.getDestination() == null || !entity.getDestination().equals(flightTarget))
return false;
// Continue if we have not yet reached the destination.
if (entity.getDestination().getDistanceSquared((int) entity.posX, (int) entity.posY, (int) entity.posZ) > 2.0f)
return true;
entity.setDestination(null);
return false;
}
@Override
public void updateTask() {
// Reset destination if we did collide.
if (entity.isInWater())
flightTarget = getRandomDestinationUpwards();
else if (entity.isCollided)
flightTarget = entity.getRNG().nextBoolean() ? getRandomDestination() : null;
else if (entity.worldObj.rand.nextInt(300) == 0)
flightTarget = getRandomDestination();
entity.setDestination(flightTarget);
entity.changeExhaustion(1);
}
@Override
public void startExecuting() {
}
@Override
public void resetTask() {
flightTarget = null;
}
}
@@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.lepidopterology.entities;
import forestry.api.genetics.IPollinatable;
import forestry.core.proxy.Proxies;
import forestry.core.utils.Utils;
public class AIButterflyPollinate extends AIButterflyInteract {
public AIButterflyPollinate(EntityButterfly entity) {
super(entity);
}
/**
* Should pollinate?
* @return
*/
@Override
protected boolean canInteract() {
if (entity.cooldownPollination > 0)
return false;
IPollinatable pollinatable = Utils.getOrCreatePollinatable(null, entity.worldObj, rest.posX, rest.posY, rest.posZ);
if (pollinatable == null)
return false;
if (!entity.getButterfly().getGenome().getFlowerProvider().isAcceptedPollinatable(entity.worldObj, pollinatable))
return false;
return entity.getPollen() == null || pollinatable.canMateWith(entity.getPollen());
}
@Override
public void updateTask() {
if (continueExecuting()) {
IPollinatable pollinatable = (IPollinatable) entity.worldObj.getTileEntity(rest.posX, rest.posY, rest.posZ);
if (entity.getPollen() == null) {
entity.setPollen(pollinatable.getPollen());
Proxies.log.finest("A butterfly '%s' grabbed a pollen '%s' at %s/%s/%s.", entity.getButterfly().getIdent(), entity.getPollen().getIdent(), rest.posX, rest.posY, rest.posZ);
} else if (pollinatable.canMateWith(entity.getPollen())) {
pollinatable.mateWith(entity.getPollen());
Proxies.log.finest("A butterfly '%s' unloaded pollen '%s' at %s/%s/%s.", entity.getButterfly().getIdent(), entity.getPollen().getIdent(), rest.posX, rest.posY, rest.posZ);
entity.setPollen(null);
}
setHasInteracted();
entity.cooldownPollination = EntityButterfly.COOLDOWNS;
}
}
}
@@ -10,10 +10,14 @@
******************************************************************************/
package forestry.lepidopterology.entities;
import net.minecraft.util.ChunkCoordinates;
import forestry.core.utils.Utils;
import forestry.lepidopterology.entities.EntityButterfly.EnumButterflyState;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFence;
import net.minecraft.block.BlockFlower;
import net.minecraft.block.BlockWall;
import net.minecraft.block.IGrowable;
import net.minecraft.block.material.Material;
import net.minecraftforge.common.IPlantable;
public class AIButterflyRest extends AIButterflyBase {
@@ -29,15 +33,19 @@ public class AIButterflyRest extends AIButterflyBase {
&& entity.canFly())
return false;
if (!entity.worldObj.isAirBlock((int) entity.posX, ((int) Math.floor(entity.posY)), (int) entity.posZ))
int x = (int) entity.posX;
int y = ((int) Math.floor(entity.posY));
int z = (int) entity.posZ;
if (!canLand(x, y, z))
return false;
ChunkCoordinates rest = new ChunkCoordinates((int) entity.posX, ((int) Math.floor(entity.posY)) - 1, (int) entity.posZ);
if (entity.worldObj.isAirBlock(rest.posX, rest.posY, rest.posZ))
y--;
if (entity.worldObj.isAirBlock(x, y, z))
return false;
if (Utils.isLiquidBlock(entity.worldObj, rest.posX, rest.posY, rest.posZ))
if (entity.worldObj.getBlock(x, y, z).getMaterial().isLiquid())
return false;
if (!entity.getButterfly().isAcceptedEnvironment(entity.worldObj, rest.posX, rest.posY, rest.posZ))
if (!entity.getButterfly().isAcceptedEnvironment(entity.worldObj, x, y, z))
return false;
entity.setDestination(null);
@@ -64,4 +72,33 @@ public class AIButterflyRest extends AIButterflyBase {
public void updateTask() {
entity.changeExhaustion(-1);
}
private boolean canLand(int x, int y, int z) {
Block block = entity.worldObj.getBlock(x, y, z);
// getBlocksMovement is a bad name, allowsMovement would be a better name.
if (!block.getBlocksMovement(entity.worldObj, x, y, z))
return false;
if (isPlant(block))
return true;
block = entity.worldObj.getBlock(x, y - 1, z);
return isRest(block) || block.isLeaves(entity.worldObj, x, y - 1, z);
}
private boolean isRest(Block block) {
if (block instanceof BlockFence)
return true;
return block instanceof BlockWall;
}
private boolean isPlant(Block block) {
if (block instanceof BlockFlower)
return true;
else if (block instanceof IPlantable)
return true;
else if (block instanceof IGrowable)
return true;
else if (block.getMaterial() == Material.plants)
return true;
return false;
}
}
@@ -0,0 +1,70 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.lepidopterology.entities;
import forestry.lepidopterology.entities.EntityButterfly.EnumButterflyState;
public class AIButterflyRise extends AIButterflyMovement {
public AIButterflyRise(EntityButterfly entity) {
super(entity);
setMutexBits(1);
}
@Override
public boolean shouldExecute() {
if (entity.getDestination() != null)
return false;
if (!entity.isCollidedHorizontally && entity.getRNG().nextInt(64) != 0)
return false;
flightTarget = getRandomDestinationUpwards();
if (flightTarget == null) {
if (entity.getState().doesMovement)
entity.setState(EnumButterflyState.HOVER);
return false;
}
entity.setDestination(flightTarget);
entity.setState(EnumButterflyState.RISING);
return true;
}
@Override
public boolean continueExecuting() {
if (entity.getState() != EntityButterfly.EnumButterflyState.RISING)
return false;
if (flightTarget == null)
return false;
// Abort if the flight target changed on us.
if (entity.getDestination() == null || !entity.getDestination().equals(flightTarget))
return false;
// Continue if we have not yet reached the destination.
if (entity.getDestination().getDistanceSquared((int) entity.posX, (int) entity.posY, (int) entity.posZ) > 2.0f)
return true;
entity.setDestination(null);
return false;
}
@Override
public void updateTask() {
if (entity.isInWater())
flightTarget = getRandomDestinationUpwards();
else if (entity.isCollidedVertically && entity.getRNG().nextInt(62) == 0)
flightTarget = null;
entity.setDestination(flightTarget);
entity.changeExhaustion(1);
}
}
@@ -10,13 +10,9 @@
******************************************************************************/
package forestry.lepidopterology.entities;
import net.minecraft.util.ChunkCoordinates;
import forestry.lepidopterology.entities.EntityButterfly.EnumButterflyState;
public class AIButterflyWander extends AIButterflyBase {
private ChunkCoordinates flightTarget;
public class AIButterflyWander extends AIButterflyMovement {
public AIButterflyWander(EntityButterfly entity) {
super(entity);
@@ -40,47 +36,4 @@ public class AIButterflyWander extends AIButterflyBase {
entity.setState(EnumButterflyState.FLYING);
return true;
}
@Override
public boolean continueExecuting() {
if (entity.getState() != EnumButterflyState.FLYING)
return false;
if (flightTarget == null)
return false;
// Abort if the flight target changed on us.
if (entity.getDestination() == null || !entity.getDestination().equals(flightTarget))
return false;
// Continue if we have not yet reached the destination.
if (entity.getDestination().getDistanceSquared((int) entity.posX, (int) entity.posY, (int) entity.posZ) > 2.0f)
return true;
entity.setDestination(null);
return false;
}
@Override
public void startExecuting() {
}
@Override
public void resetTask() {
flightTarget = null;
}
@Override
public void updateTask() {
// Reset destination if we did collide.
if (entity.isInWater()) {
flightTarget = getRandomDestinationUpwards();
entity.setDestination(flightTarget);
}
if (entity.isCollided || entity.worldObj.rand.nextInt(300) == 0) {
flightTarget = getRandomDestination();
entity.setDestination(flightTarget);
}
entity.changeExhaustion(1);
}
}
@@ -39,12 +39,21 @@ import forestry.core.utils.StackUtils;
import forestry.lepidopterology.genetics.Butterfly;
import forestry.lepidopterology.render.ButterflyItemRenderer;
import forestry.plugins.PluginLepidopterology;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFence;
import net.minecraft.block.BlockFlower;
import net.minecraft.block.BlockWall;
import net.minecraft.block.IGrowable;
import net.minecraft.block.material.Material;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.common.IPlantable;
public class EntityButterfly extends EntityCreature implements IEntityButterfly {
public enum EnumButterflyState {
FLYING(true), GLIDING(true), RESTING(false), HOVER(false);
FLYING(true), GLIDING(true), RISING(true), RESTING(false), HOVER(false);
public static final EnumButterflyState[] VALUES = values();
@@ -110,10 +119,12 @@ public class EntityButterfly extends EntityCreature implements IEntityButterfly
private void setDefaults(IButterfly butterfly) {
contained = butterfly;
tasks.addTask(10, new AIButterflyWander(this));
tasks.addTask(9, new AIButterflyRest(this));
tasks.addTask(8, new AIButterflyInteract(this));
tasks.addTask(8, new AIButterflyFlee(this));
tasks.addTask(9, new AIButterflyMate(this));
tasks.addTask(10, new AIButterflyPollinate(this));
tasks.addTask(11, new AIButterflyRest(this));
tasks.addTask(12, new AIButterflyRise(this));
tasks.addTask(12, new AIButterflyWander(this));
}
@Override
@@ -181,6 +192,59 @@ public class EntityButterfly extends EntityCreature implements IEntityButterfly
flightTarget = destination;
}
@Override
public float getBlockPathWeight(int x, int y, int z) {
float weight = 0.0f;
if (!getButterfly().isAcceptedEnvironment(worldObj, x, y, z))
weight -= 15.0f;
if (!worldObj.getEntitiesWithinAABB(EntityButterfly.class, AxisAlignedBB.getBoundingBox(x, y, z, x + 1, y + 1, z + 1)).isEmpty())
weight -= 1.0f;
int depth = getFluidDepth(x, z);
if (depth > 0)
weight -= 0.1f * depth;
else {
Block block = worldObj.getBlock(x, y, z);
if (block instanceof BlockFlower)
weight += 2.0f;
else if (block instanceof IPlantable)
weight += 1.5f;
else if (block instanceof IGrowable)
weight += 1.0f;
else if (block.getMaterial() == Material.plants)
weight += 1.0f;
block = worldObj.getBlock(x, y - 1, z);
if (block.isLeaves(worldObj, x, y - 1, z))
weight += 2.5f;
else if (block instanceof BlockFence)
weight += 1.0f;
else if (block instanceof BlockWall)
weight += 1.0f;
}
weight += worldObj.getLightBrightness(x, y, z);
return weight;
}
private int getFluidDepth(int x, int z) {
Chunk chunk = worldObj.getChunkFromBlockCoords(x, z);
int xx = x & 15;
int zz = z & 15;
int depth = 0;
for (int y = chunk.getTopFilledSegment() + 15; y > 0; --y) {
Block block = chunk.getBlock(xx, y, zz);
if (block.getMaterial().isLiquid())
depth++;
else if (!block.isAir(worldObj, x, y, z))
break;
}
return depth;
}
/* POLLEN */
@Override
public IIndividual getPollen() {
@@ -337,8 +401,9 @@ public class EntityButterfly extends EntityCreature implements IEntityButterfly
/* LOOT */
@Override
protected void dropFewItems(boolean playerKill, int lootLevel) {
for (ItemStack stack : contained.getLootDrop(this, playerKill, lootLevel))
for (ItemStack stack : contained.getLootDrop(this, playerKill, lootLevel)) {
StackUtils.dropItemStackAsEntity(stack, worldObj, posX, posY, posZ);
}
// Drop pollen if any
if (getPollen() != null)
@@ -162,9 +162,7 @@ public class Butterfly extends IndividualLiving implements IButterfly {
private boolean canFly(World world) {
if(world.isRaining() && !getGenome().getTolerantFlyer())
return false;
if(!isActiveThisTime(world.isDaytime()))
return false;
return true;
return isActiveThisTime(world.isDaytime());
}
@Override
@@ -196,7 +196,7 @@ public class ButterflyHelper extends SpeciesRoot implements IButterflyRoot {
}
/* MUTATIONS */
private static ArrayList<IButterflyMutation> butterflyMutations = new ArrayList<IButterflyMutation>();
private static final ArrayList<IButterflyMutation> butterflyMutations = new ArrayList<IButterflyMutation>();
@Override
public void registerMutation(IMutation mutation) {
@@ -41,7 +41,7 @@ public class ButterflyItemRenderer implements IItemRenderer {
if(butterfly.isAlive()) {
long systemTime = System.currentTimeMillis();
long flapping = systemTime + butterfly.getIdent().hashCode();
float flap = (float) (flapping % 1000) / 1000; // 0 to 1
float flap = (float) (flapping % 1024) / 1024; // 0 to 1
wingYaw = getIrregularWingYaw(flapping, flap);
@@ -51,8 +51,8 @@ public class ButterflyItemRenderer implements IItemRenderer {
}
public static float getIrregularWingYaw(long flapping, float flap) {
long irregular = (long)(flapping / 1000);
float wingYaw = 1f;
long irregular = (long)(flapping / 1024);
float wingYaw;
if(irregular % 11 == 0) {
wingYaw = 0.75f;
@@ -95,8 +95,8 @@ public class ModelButterfly extends ModelBase {
}
@Override
public void setRotationAngles(float f, float f1, float f2, float f3, float f4, float f5, Entity entity) {
super.setRotationAngles(f, f1, f2, f3, f4, f5, entity);
public void setRotationAngles(float f, float f1, float swing, float f3, float f4, float f5, Entity entity) {
super.setRotationAngles(f, f1, swing, f3, f4, f5, entity);
}
}
@@ -15,6 +15,7 @@ import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;
import forestry.lepidopterology.entities.EntityButterfly;
import net.minecraft.entity.EntityLivingBase;
public class RenderButterfly extends RenderLiving {
@@ -22,25 +23,30 @@ public class RenderButterfly extends RenderLiving {
public RenderButterfly() {
super(new ModelButterfly(), 0.25f);
butterflyModel = (ModelButterfly)mainModel;
butterflyModel = (ModelButterfly) mainModel;
}
public void renderButterfly(EntityButterfly entity, double x, double y, double z, float par8, float par9) {
if(!entity.isRenderable())
public void renderButterfly(EntityButterfly entity, double x, double y, double z, float light, float partialTickTime) {
if (!entity.isRenderable())
return;
butterflyModel.setScale(entity.getScale());
super.doRender(entity, x, y, z, par8, entity.getWingFlap(par9));
super.doRender(entity, x, y, z, light, partialTickTime);
}
@Override
public void doRender(Entity entity, double x, double y, double z, float par8, float par9) {
this.renderButterfly((EntityButterfly)entity, x, y, z, par8, par9);
public void doRender(Entity entity, double x, double y, double z, float light, float partialTickTime) {
this.renderButterfly((EntityButterfly) entity, x, y, z, light, partialTickTime);
}
@Override
protected ResourceLocation getEntityTexture(Entity entity) {
return ((EntityButterfly)entity).getTexture();
return ((EntityButterfly) entity).getTexture();
}
@Override
protected float handleRotationFloat(EntityLivingBase entity, float partialTickTime) {
return ((EntityButterfly) entity).getWingFlap(partialTickTime);
}
}