- Familiars: Now attacking potentially dangerous monsters around it and mobs that have targetted its owner once.

This commit is contained in:
HellFirePvP committed 2015-10-31 16:37:06 +01:00
1 parent 7a71dc0d95
commit 3bf5309206
8 files changed
+168 -15

No files matched your search

@@ -1,10 +1,14 @@
package makeo.gadomancy.client.renderers.item.familiar;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import makeo.gadomancy.common.familiar.FamiliarHandlerServer;
import makeo.gadomancy.common.network.packets.PacketFamiliar;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import thaumcraft.common.Thaumcraft;
/**
* This class is part of the Gadomancy Mod
@@ -46,8 +50,12 @@ public class FamiliarHandlerClient extends FamiliarHandlerServer {
}
@SideOnly(Side.CLIENT)
public void processPacket(PacketFamiliar packet) {
if(packet instanceof PacketFamiliar.PacketFamiliarBolt) {
PacketFamiliar.PacketFamiliarBolt pkt = (PacketFamiliar.PacketFamiliarBolt) packet;
EntityPlayer p = Minecraft.getMinecraft().thePlayer;
Thaumcraft.proxy.nodeBolt(Minecraft.getMinecraft().theWorld, (float) p.posX, (float) (p.posY + p.eyeHeight), (float) p.posZ, pkt.targetX, pkt.targetY, pkt.targetZ);
//TODO zap given coordinates from current familiar render coordinates
}
}
@@ -6,6 +6,7 @@ import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import makeo.gadomancy.common.containers.ContainerInfusionClaw;
import makeo.gadomancy.common.events.EventHandlerEntity;
import makeo.gadomancy.common.events.EventHandlerGolem;
import makeo.gadomancy.common.events.EventHandlerNetwork;
import makeo.gadomancy.common.events.EventHandlerWorld;
@@ -66,6 +67,7 @@ public class CommonProxy implements IGuiHandler {
MinecraftForge.EVENT_BUS.register(EVENT_HANDLER_GOLEM);
FMLCommonHandler.instance().bus().register(new EventHandlerNetwork());
MinecraftForge.EVENT_BUS.register(new EventHandlerWorld());
MinecraftForge.EVENT_BUS.register(new EventHandlerEntity());
RegisteredRecipes.init();
@@ -0,0 +1,34 @@
package makeo.gadomancy.common.events;
import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import makeo.gadomancy.common.familiar.FamiliarAIController;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
/**
* This class is part of the Gadomancy Mod
* Gadomancy is Open Source and distributed under the
* GNU LESSER GENERAL PUBLIC LICENSE
* for more read the LICENSE file
* <p/>
* Created by HellFirePvP @ 31.10.2015 15:28
*/
public class EventHandlerEntity {
@SubscribeEvent(priority = EventPriority.LOWEST)
public void on(EntityEvent.EntityConstructing e) {
if (e.entity instanceof EntityPlayer) {
//e.entity.registerExtendedProperties(Gadomancy.MODID, new ExtendedPlayerProperties((EntityPlayer) e.entity));
}
}
@SubscribeEvent
public void on(LivingSetAttackTargetEvent targetEvent) {
if(targetEvent.target instanceof EntityPlayer) {
FamiliarAIController.notifyTargetEvent(targetEvent.entityLiving, (EntityPlayer) targetEvent.target);
}
}
}
@@ -52,12 +52,6 @@ import java.util.Map;
* Created by makeo @ 05.07.2015 13:20
*/
public class EventHandlerWorld {
@SubscribeEvent(priority = EventPriority.LOWEST)
public void on(EntityEvent.EntityConstructing e) {
if (e.entity instanceof EntityPlayer) {
//e.entity.registerExtendedProperties(Gadomancy.MODID, new ExtendedPlayerProperties((EntityPlayer) e.entity));
}
}
@SubscribeEvent(priority = EventPriority.NORMAL)
public void on(WorldEvent.Load e) {
@@ -1,12 +1,14 @@
package makeo.gadomancy.common.familiar;
import makeo.gadomancy.common.familiar.ai.FamiliarAIProcess;
import makeo.gadomancy.common.registration.RegisteredFamiliarAI;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
@@ -23,8 +25,11 @@ public class FamiliarAIController {
private static final Random RAND = new Random();
private static Map<EntityPlayer, LinkedList<EntityLivingBase>> targetMap = new HashMap<EntityPlayer, LinkedList<EntityLivingBase>>();
private List<FamiliarAIProcess> availableTasks = new ArrayList<FamiliarAIProcess>();
private Map<FamiliarAIProcess, Integer> cooldownProcesses = new HashMap<FamiliarAIProcess, Integer>();
private LinkedList<FamiliarAIProcess> requestedLoop = new LinkedList<FamiliarAIProcess>();
private FamiliarAIProcess runningTask;
private int ticksInTask;
private EntityPlayer owningPlayer;
@@ -33,6 +38,11 @@ public class FamiliarAIController {
this.owningPlayer = owningPlayer;
}
public void registerDefaultTasks() {
this.availableTasks.add(RegisteredFamiliarAI.familiarAIIdle);
this.availableTasks.add(RegisteredFamiliarAI.familiarAIZapAttackingMonsters);
}
public void sheduleTick() {
reduceRunningCooldowns();
if(runningTask == null) {
@@ -42,6 +52,7 @@ public class FamiliarAIController {
ticksInTask++;
if(ticksInTask >= runningTask.getDuration()) {
if(runningTask.getCooldownDuration() > 0) cooldownProcesses.put(runningTask, runningTask.getCooldownDuration());
if(runningTask.tryLoop() && !requestedLoop.contains(runningTask)) requestedLoop.addLast(runningTask);
runningTask = null;
ticksInTask = 0;
}
@@ -49,6 +60,17 @@ public class FamiliarAIController {
}
private void selectNewTask() {
if(!requestedLoop.isEmpty()) {
Iterator<FamiliarAIProcess> it = requestedLoop.iterator();
while(it.hasNext()) {
FamiliarAIProcess process = it.next();
if(!cooldownProcesses.containsKey(process)) {
runningTask = process;
it.remove();
return;
}
}
}
int size = availableTasks.size();
int randIndex = RAND.nextInt(size);
for (int i = 0; i < size; i++) {
@@ -77,4 +99,41 @@ public class FamiliarAIController {
public EntityPlayer getOwningPlayer() {
return owningPlayer;
}
public static boolean hasLastTargetter(EntityPlayer player) {
return targetMap.containsKey(player) && targetMap.get(player).size() >= 1;
}
public static LinkedList<EntityLivingBase> getLastTargetters(EntityPlayer player) {
if(!targetMap.containsKey(player)) return null;
if(targetMap.get(player).size() < 1) return null;
return targetMap.get(player);
}
public static void cleanTargetterList(EntityPlayer player) {
if(!targetMap.containsKey(player)) return;
Iterator<EntityLivingBase> it = targetMap.get(player).iterator();
while(it.hasNext()) {
EntityLivingBase living = it.next();
if(living.isDead) it.remove();
}
}
public static void notifyTargetEvent(EntityLivingBase targetter, EntityPlayer targetted) {
LinkedList<EntityLivingBase> targetters;
boolean needsUpdate = false;
if(targetMap.containsKey(targetted)) {
targetters = targetMap.get(targetted);
} else {
targetters = new LinkedList<EntityLivingBase>();
needsUpdate = true;
}
if(!targetters.contains(targetter)) {
targetters.addLast(targetter);
}
if(needsUpdate) {
targetMap.put(targetted, targetters);
}
}
}
@@ -1,6 +1,8 @@
package makeo.gadomancy.common.familiar;
import baubles.api.BaublesApi;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
@@ -27,7 +29,11 @@ public class FamiliarHandlerServer implements FamiliarHandler {
@Override
public void notifyEquip(World world, ItemStack familiarStack, EntityPlayer player) {
if(!world.isRemote) familiarAI.put(player, new FamiliarAIController(player));
if(!world.isRemote) {
FamiliarAIController controller = new FamiliarAIController(player);
controller.registerDefaultTasks();
familiarAI.put(player, controller);
}
}
@Override
@@ -37,7 +43,18 @@ public class FamiliarHandlerServer implements FamiliarHandler {
@Override
public void equippedTick(World world, ItemStack familiarStack, EntityPlayer player) {
if(!world.isRemote) familiarAI.get(player).sheduleTick();
if(!world.isRemote) {
IInventory baublesInv = BaublesApi.getBaubles(player);
if(baublesInv.getStackInSlot(0) == null) {
notifyUnequip(world, familiarStack, player);
return;
}
if(familiarAI.get(player) == null) {
notifyEquip(world, familiarStack, player);
}
familiarAI.get(player).sheduleTick();
}
}
}
@@ -19,7 +19,7 @@ public abstract class PacketFamiliar {
public static class PacketFamiliarBolt extends PacketFamiliar implements IMessage, IMessageHandler<PacketFamiliarBolt, IMessage> {
private float targetX, targetY, targetZ;
public float targetX, targetY, targetZ;
public PacketFamiliarBolt() {}
@@ -1,15 +1,21 @@
package makeo.gadomancy.common.registration;
import cpw.mods.fml.common.network.NetworkRegistry;
import makeo.gadomancy.common.familiar.FamiliarAIController;
import makeo.gadomancy.common.familiar.ai.FamiliarAIProcess;
import makeo.gadomancy.common.network.PacketHandler;
import makeo.gadomancy.common.network.packets.PacketFamiliar;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
@@ -37,22 +43,55 @@ public class RegisteredFamiliarAI {
}
};
public static FamiliarAIProcess familiarAIZapMonsters = new FamiliarAIProcess(1) {
public static FamiliarAIProcess familiarAIZapAttackingMonsters = new FamiliarAIProcess(1) {
@Override
public boolean canRun(World world, double x, double y, double z, EntityPlayer parent) {
return !world.getEntitiesWithinAABB(EntityMob.class, AxisAlignedBB.getBoundingBox(parent.posX - 0.5, parent.posY - 0.5, parent.posZ - 0.5, parent.posX + 0.5, parent.posY + 0.5, parent.posZ + 0.5).expand(5, 5, 5)).isEmpty();
return getPotentialTargets(world, parent).size() > 0;
}
@Override
public void tick(int ticksSoFar, World world, EntityPlayer parent) {
List mobs = world.getEntitiesWithinAABB(EntityMob.class, AxisAlignedBB.getBoundingBox(parent.posX - 0.5, parent.posY - 0.5, parent.posZ - 0.5, parent.posX + 0.5, parent.posY + 0.5, parent.posZ + 0.5).expand(5, 5, 5));
Object random = mobs.get(world.rand.nextInt(mobs.size()));
EntityMob mob = (EntityMob) random;
List<EntityLivingBase> lastTargetters = getPotentialTargets(world, parent);
if(lastTargetters.size() < 1) {
FamiliarAIController.cleanTargetterList(parent);
return;
}
EntityLivingBase mob = lastTargetters.get(world.rand.nextInt(lastTargetters.size()));
if(mob.isDead) {
FamiliarAIController.cleanTargetterList(parent);
return;
}
mob.attackEntityFrom(DamageSource.magic, 6);
PacketFamiliar.PacketFamiliarBolt bolt = new PacketFamiliar.PacketFamiliarBolt((float) mob.posX, (float) mob.posY, (float) mob.posZ);
PacketHandler.INSTANCE.sendToAllAround(bolt, new NetworkRegistry.TargetPoint(mob.worldObj.provider.dimensionId, mob.posX, mob.posY, mob.posZ, 32));
FamiliarAIController.cleanTargetterList(parent);
}
private List<EntityLivingBase> getPotentialTargets(World world, EntityPlayer player) {
List<EntityLivingBase> validTargets = getCloseEnoughTargetters(world, player);
List mobs = world.getEntitiesWithinAABB(IMob.class, AxisAlignedBB.getBoundingBox(player.posX - 0.5, player.posY - 0.5, player.posZ - 0.5, player.posX + 0.5, player.posY + 0.5, player.posZ + 0.5).expand(5, 5, 5));
for(Object mobObj : mobs) {
if(!(mobObj instanceof EntityLivingBase)) continue;
EntityLivingBase mob = (EntityLivingBase) mobObj;
if(mob.isDead) continue;
if(!validTargets.contains(mob)) validTargets.add(mob);
}
return validTargets;
}
private List<EntityLivingBase> getCloseEnoughTargetters(World world, EntityPlayer parent) {
LinkedList<EntityLivingBase> lastTargetters = FamiliarAIController.getLastTargetters(parent);
if(lastTargetters == null || lastTargetters.isEmpty()) return new ArrayList<EntityLivingBase>();
List<EntityLivingBase> closeEnoughLastTargetters = new ArrayList<EntityLivingBase>();
List livingAround = world.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(parent.posX - 0.5, parent.posY - 0.5, parent.posZ - 0.5, parent.posX + 0.5, parent.posY + 0.5, parent.posZ + 0.5).expand(8, 8, 8));
for(Object living : livingAround) {
if(!(living instanceof EntityLivingBase)) continue;
if(lastTargetters.contains(living)) closeEnoughLastTargetters.add((EntityLivingBase) living);
}
return closeEnoughLastTargetters;
}
@Override