Search in sources :

Example 1 with ILinkageManager

use of mods.railcraft.api.carts.ILinkageManager in project Railcraft by Railcraft.

the class CartBaseEnergy method injectEnergy.

@Override
public double injectEnergy(Object source, double amount, int tier, boolean ignoreTransferLimit, boolean simulate, boolean passAlong) {
    if (tier < getTier())
        return amount;
    double extra = 0;
    if (!ignoreTransferLimit) {
        extra = Math.max(amount - getTransferLimit(), 0);
        amount = Math.min(amount, getTransferLimit());
    }
    double e = getEnergy() + amount;
    int capacity = getCapacity();
    if (e > capacity) {
        extra += e - capacity;
        e = capacity;
    }
    if (!simulate)
        setEnergy(e);
    if (!passAlong)
        return extra;
    try {
        ILinkageManager lm = CartToolsAPI.getLinkageManager(worldObj);
        EntityMinecart linkedCart = lm.getLinkedCartA(this);
        if (extra > 0 && linkedCart != source && linkedCart instanceof IEnergyTransfer)
            extra = ((IEnergyTransfer) linkedCart).injectEnergy(this, extra, tier, ignoreTransferLimit, simulate, true);
        linkedCart = lm.getLinkedCartB(this);
        if (extra > 0 && linkedCart != source && linkedCart instanceof IEnergyTransfer)
            extra = ((IEnergyTransfer) linkedCart).injectEnergy(this, extra, tier, ignoreTransferLimit, simulate, true);
    } catch (Throwable t) {
        APIErrorHandler.versionMismatch(IEnergyTransfer.class);
    }
    return extra;
}
Also used : ILinkageManager(mods.railcraft.api.carts.ILinkageManager) IEnergyTransfer(mods.railcraft.api.carts.IEnergyTransfer) EntityMinecart(net.minecraft.entity.item.EntityMinecart)

Example 2 with ILinkageManager

use of mods.railcraft.api.carts.ILinkageManager 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 3 with ILinkageManager

use of mods.railcraft.api.carts.ILinkageManager in project Railcraft by Railcraft.

the class CartBaseEnergy method extractEnergy.

@Override
public double extractEnergy(Object source, double amount, int tier, boolean ignoreTransferLimit, boolean simulate, boolean passAlong) {
    if (tier < getTier())
        return 0;
    if (!ignoreTransferLimit)
        amount = Math.min(amount, getTransferLimit());
    double e = getEnergy();
    double provide = Math.min(amount, e);
    e -= provide;
    if (e < 0)
        e = 0;
    if (!simulate)
        setEnergy(e);
    if (!passAlong)
        return provide;
    ILinkageManager lm = CartToolsAPI.getLinkageManager(worldObj);
    EntityMinecart linkedCart = lm.getLinkedCartA(this);
    if (provide < amount && linkedCart != source && linkedCart instanceof IEnergyTransfer)
        provide += ((IEnergyTransfer) linkedCart).extractEnergy(this, amount - provide, tier, ignoreTransferLimit, simulate, true);
    linkedCart = lm.getLinkedCartB(this);
    if (provide < amount && linkedCart != source && linkedCart instanceof IEnergyTransfer)
        provide += ((IEnergyTransfer) linkedCart).extractEnergy(this, amount - provide, tier, ignoreTransferLimit, simulate, true);
    return provide;
}
Also used : ILinkageManager(mods.railcraft.api.carts.ILinkageManager) IEnergyTransfer(mods.railcraft.api.carts.IEnergyTransfer) EntityMinecart(net.minecraft.entity.item.EntityMinecart)

Aggregations

ILinkageManager (mods.railcraft.api.carts.ILinkageManager)3 EntityMinecart (net.minecraft.entity.item.EntityMinecart)3 IEnergyTransfer (mods.railcraft.api.carts.IEnergyTransfer)2 EntityLivingBase (net.minecraft.entity.EntityLivingBase)1 EntityIronGolem (net.minecraft.entity.monster.EntityIronGolem)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1