Search in sources :

Example 16 with EntityMinecart

use of net.minecraft.entity.item.EntityMinecart in project Railcraft by Railcraft.

the class LinkageHandler method onMinecartUpdate.

//    /**
//     * Determines whether a cart is leading another.
//     *
//     * @param leader EntityMinecart
//     * @param follower EntityMinecart
//     * @return true if leader is leading follower
//     */
//    private boolean isCartLeading(EntityMinecart leader, EntityMinecart follower) {
//        return true; // magic goes here
//    }
//    /**
//     * Adjust the current cart's position based on the linked cart its following
//     * so that it follows the same path at a set distance.
//     *
//     * @param current EntityMinecart
//     * @param linked EntityMinecart
//     */
//    private void adjustCartFromHistory(EntityMinecart current, EntityMinecart linked) {
//        // If we are leading, we don't want to adjust anything
//        if (isCartLeading(current, linked))
//            return;
//
//        CircularVec3Queue leaderHistory = history.get(linked);
//
//        // Optimal distance is how far apart the carts should be
//        double optimalDist = getOptimalDistance(current, linked);
//        optimalDist *= optimalDist;
//
//        double currentDistance = linked.getDistanceSqToEntity(current);
//
//        // Search the history for the point closest to the optimal distance.
//        // There may be some issues with it choosing the wrong side of the cart.
//        // Probably needs some kind of logic to compare the distance from the
//        // new position to the current position and determine if its a valid position.
//        Vec3 closestPoint = null;
//        Vec3 linkedVec = new Vec3(linked.posX, linked.posY, linked.posZ);
//        double distance = Math.abs(optimalDist - currentDistance);
//        for (Vec3 pos : leaderHistory) {
//            double historyDistance = linkedVec.squareDistanceTo(pos);
//            double diff = Math.abs(optimalDist - historyDistance);
//            if (diff < distance) {
//                closestPoint = pos;
//                distance = diff;
//            }
//        }
//
//        // If we found a point closer to our desired distance, move us there
//        if (closestPoint != null)
//            current.setPosition(closestPoint.xCoord, closestPoint.yCoord, closestPoint.zCoord);
//    }
//    /**
//     * Saved the position history of the cart every tick in a Circular Buffer.
//     *
//     * @param cart EntityMinecart
//     */
//    private void savePosition(EntityMinecart cart) {
//        CircularVec3Queue myHistory = history.get(cart);
//        if (myHistory == null) {
//            myHistory = new CircularVec3Queue(TICK_HISTORY);
//            history.put(cart, myHistory);
//        }
//        myHistory.add(cart.posX, cart.posY, cart.posZ);
//    }
/**
     * This is our entry point, its triggered once per tick per cart.
     *
     * @param event MinecartUpdateEvent
     */
@SubscribeEvent
public void onMinecartUpdate(MinecartUpdateEvent event) {
    EntityMinecart cart = event.getMinecart();
    // Physics done here
    adjustCart(cart);
//        savePosition(cart);
}
Also used : EntityMinecart(net.minecraft.entity.item.EntityMinecart) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 17 with EntityMinecart

use of net.minecraft.entity.item.EntityMinecart in project Railcraft by Railcraft.

the class LinkageManager method breakLink.

@Nullable
private EntityMinecart breakLink(EntityMinecart cart, LinkType linkType) {
    Train.deleteTrain(cart);
    UUID link = getLink(cart, linkType);
    removeLinkTags(cart, linkType);
    EntityMinecart other = CartTools.getCartFromUUID(cart.worldObj, link);
    if (other != null) {
        breakLink(other, cart);
    }
    if (cart instanceof ILinkableCart)
        ((ILinkableCart) cart).onLinkBroken(other);
    printDebug("Carts {0}({1}) and {2}({3}) unlinked ({4}).", getLinkageId(cart), cart, link, other != null ? other : "null", linkType.name());
    return other;
}
Also used : ILinkableCart(mods.railcraft.api.carts.ILinkableCart) UUID(java.util.UUID) EntityMinecart(net.minecraft.entity.item.EntityMinecart) Nullable(javax.annotation.Nullable)

Example 18 with EntityMinecart

use of net.minecraft.entity.item.EntityMinecart in project Railcraft by Railcraft.

the class MinecartHooks method onMinecartInteract.

@SubscribeEvent
public void onMinecartInteract(MinecartInteractEvent event) {
    EntityMinecart cart = event.getMinecart();
    EntityPlayer player = event.getPlayer();
    if (!CartToolsAPI.doesCartHaveOwner(cart))
        CartToolsAPI.setCartOwner(cart, player);
    if (!(cart instanceof EntityTunnelBore) && player.getDistanceSqToEntity(cart) > MAX_INTERACT_DIST_SQ) {
        event.setCanceled(true);
        return;
    }
    if (cart.isDead) {
        event.setCanceled(true);
        return;
    }
    if (cart.canBeRidden()) {
        //TODO: this will interfere with carts that multiple players can ride, re-evaluate
        if (cart.isBeingRidden() && player.getRidingEntity() != cart) {
            event.setCanceled(true);
            return;
        }
        if (player.getRidingEntity() != null && player.getRidingEntity() != cart) {
            event.setCanceled(true);
            return;
        }
        if (player.getRidingEntity() != cart && player.isOnLadder()) {
            event.setCanceled(true);
            return;
        }
    }
    if (!player.canEntityBeSeen(cart)) {
        event.setCanceled(true);
    }
}
Also used : EntityPlayer(net.minecraft.entity.player.EntityPlayer) EntityMinecart(net.minecraft.entity.item.EntityMinecart) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 19 with EntityMinecart

use of net.minecraft.entity.item.EntityMinecart in project Railcraft by Railcraft.

the class MinecartHooks method onEntityCollision.

@Override
public void onEntityCollision(EntityMinecart cart, Entity other) {
    if (Game.isClient(cart.worldObj) || cart.isPassenger(other) || !other.isEntityAlive() || !cart.isEntityAlive())
        return;
    ILinkageManager lm = LinkageManager.instance();
    EntityMinecart link = lm.getLinkedCartA(cart);
    if (link != null && (link == other || link.isPassenger(other)))
        return;
    link = lm.getLinkedCartB(cart);
    if (link != null && (link == other || link.isPassenger(other)))
        return;
    boolean isLiving = other instanceof EntityLivingBase;
    boolean isPlayer = other instanceof EntityPlayer;
    //TODO: needs more thought in regards to passenger handling
    if (isLiving && !isPlayer && cart.canBeRidden() && !(other instanceof EntityIronGolem) && cart.motionX * cart.motionX + cart.motionZ * cart.motionZ > 0.001D && !cart.isBeingRidden() && !other.isRiding()) {
        int mountPrevention = cart.getEntityData().getInteger("MountPrevention");
        if (mountPrevention <= 0)
            other.startRiding(cart);
    }
    if (isLiving && WorldPlugin.isBlockAt(cart.worldObj, cart.getPosition(), RailcraftBlocks.TRACK_ELEVATOR.block()))
        return;
    //        System.out.println(cart.getClass().getSimpleName() + ": " + cart.entityId + " collided with " + other.getClass().getSimpleName() + ": " + other.entityId);
    Vec2D cartPos = new Vec2D(cart.posX, cart.posZ);
    Vec2D otherPos = new Vec2D(other.posX, other.posZ);
    Vec2D unit = Vec2D.subtract(otherPos, cartPos);
    unit.normalize();
    double distance = cart.getDistanceToEntity(other);
    double depth = distance - OPTIMAL_DISTANCE;
    double forceX = 0;
    double forceZ = 0;
    if (depth < 0) {
        double spring = isPlayer ? COEF_SPRING_PLAYER : COEF_SPRING;
        double penaltyX = spring * depth * unit.getX();
        double penaltyZ = spring * depth * unit.getY();
        forceX += penaltyX;
        forceZ += penaltyZ;
        if (!isPlayer) {
            double impulseX = unit.getX();
            double impulseZ = unit.getY();
            impulseX *= -(1.0 + COEF_RESTITUTION);
            impulseZ *= -(1.0 + COEF_RESTITUTION);
            Vec2D cartVel = new Vec2D(cart.motionX, cart.motionZ);
            Vec2D otherVel = new Vec2D(other.motionX, other.motionZ);
            double dot = Vec2D.subtract(otherVel, cartVel).dotProduct(unit);
            impulseX *= dot;
            impulseZ *= dot;
            impulseX *= 0.5;
            impulseZ *= 0.5;
            forceX -= impulseX;
            forceZ -= impulseZ;
        }
    }
    if (other instanceof EntityMinecart) {
        EntityMinecart otherCart = (EntityMinecart) other;
        if (!cart.isPoweredCart() || otherCart.isPoweredCart())
            if (!TrackToolsAPI.isCartLockedDown(cart))
                cart.addVelocity(forceX, 0, forceZ);
        if (!otherCart.isPoweredCart() || cart.isPoweredCart())
            if (!TrackToolsAPI.isCartLockedDown(otherCart))
                other.addVelocity(-forceX, 0, -forceZ);
    } else {
        //            if(isPlayer) {
        //                forceX += Math.abs(cart.motionX - other.motionX) / 2;
        //                forceZ += Math.abs(cart.motionZ - other.motionZ) / 2;
        //            }
        //            System.out.printf("forceX=%f, forceZ=%f%n", forceX, forceZ);
        Vec2D cartVel = new Vec2D(cart.motionX + forceX, cart.motionZ + forceZ);
        Vec2D otherVel = new Vec2D(other.motionX - forceX, other.motionZ - forceZ);
        double dot = Vec2D.subtract(otherVel, cartVel).dotProduct(unit);
        double dampX = COEF_DAMPING * dot * unit.getX();
        double dampZ = COEF_DAMPING * dot * unit.getY();
        forceX += dampX;
        forceZ += dampZ;
        //            System.out.printf("dampX=%f, dampZ=%f%n", dampX, dampZ);
        if (!isPlayer)
            other.addVelocity(-forceX, 0.0D, -forceZ);
        if (!TrackToolsAPI.isCartLockedDown(cart))
            cart.addVelocity(forceX, 0, forceZ);
    }
}
Also used : EntityIronGolem(net.minecraft.entity.monster.EntityIronGolem) ILinkageManager(mods.railcraft.api.carts.ILinkageManager) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EntityMinecart(net.minecraft.entity.item.EntityMinecart)

Example 20 with EntityMinecart

use of net.minecraft.entity.item.EntityMinecart in project Railcraft by Railcraft.

the class Train method resetTrain.

protected void resetTrain() {
    LinkageManager lm = LinkageManager.instance();
    for (UUID id : carts) {
        EntityMinecart cart = CartTools.getCartFromUUID(world, id);
        if (cart != null) {
            removeTrainTag(cart);
        }
    }
    carts.clear();
    lockingTracks.clear();
}
Also used : EntityMinecart(net.minecraft.entity.item.EntityMinecart)

Aggregations

EntityMinecart (net.minecraft.entity.item.EntityMinecart)45 ItemStack (net.minecraft.item.ItemStack)11 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)11 EntityPlayer (net.minecraft.entity.player.EntityPlayer)10 ILinkageManager (mods.railcraft.api.carts.ILinkageManager)6 World (net.minecraft.world.World)6 Nullable (javax.annotation.Nullable)5 Entity (net.minecraft.entity.Entity)5 List (java.util.List)4 EntityLivingBase (net.minecraft.entity.EntityLivingBase)4 ArrayList (java.util.ArrayList)3 IEnergyTransfer (mods.railcraft.api.carts.IEnergyTransfer)2 ILinkableCart (mods.railcraft.api.carts.ILinkableCart)2 ItemGoggles (mods.railcraft.common.items.ItemGoggles)2 EntityItem (net.minecraft.entity.item.EntityItem)2 Item (net.minecraft.item.Item)2 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)2 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)2 ShaderWrapper_Direct (blusunrize.immersiveengineering.api.shader.CapabilityShader.ShaderWrapper_Direct)1 HashMultiset (com.google.common.collect.HashMultiset)1