<commit_before>/*
 * Copyright (c) CovertJaguar, 2011 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 railcraft.wikispaces.com.
 */
package mods.railcraft.api.electricity;

import java.util.Random;
import mods.railcraft.api.carts.CartTools;
import mods.railcraft.api.carts.ILinkageManager;
import mods.railcraft.api.tracks.RailTools;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.nbt.NBTTagCompound;

/**
 * This interface provides a simple means of using or producing Electricity
 * within a train.
 * <p>
 * The original Ic2 Battery Carts implement IEnergyTransfer. IEnergyTransfer was
 * a naive implementation of a Energy storage system for carts. I'll leave it in
 * place because of its Ic2 specific functions, but for all intents and purposes
 * this is the recommended and easier to implement interface for Electricity
 * related minecarts. In fact, the Railcraft Ic2 Energy Carts will be
 * redirecting to this interface. The Energy Loaders will continue to work
 * exclusively with IEnergyTransfer for the moment due to the high Ic2 coupling
 * of their design. An alternative loader block utilizing the IElectricMinecart
 * interface may be provided in the future, but no guarantee.
 * <p>
 * @author CovertJaguar <http://www.railcraft.info/>
 */
public interface IElectricMinecart {

    public ChargeHandler getChargeHandler();

    public static final class ChargeHandler {

        public static final int DRAW_INTERVAL = 8;
        private static final Random rand = new Random();

        public enum Type {

            /**
             * Users draw power from tracks, sources, and storage.
             */
            USER,
            /**
             * Sources provide power to users, but not storage. This interface
             * specifies no explicit way to charge Sources, that's up to the
             * implementer. Railcraft provides no Sources currently, and may
             * never do so.
             */
            SOURCE,
            /**
             * Storage provide power to users, but can't draw from tracks or
             * sources. This interface specifies no explicit way to charge
             * Storage, that's up to the implementer. Railcraft may provide a
             * trackside block in the future for charging Storage, but does not
             * currently.
             */
            STORAGE;
        }

        private final EntityMinecart minecart;
        private final Type type;
        private double capacity, charge, draw, lastTickDraw;
        private final double lossPerTick;
        private int clock = rand.nextInt();
        private int drewFromTrack;

        public ChargeHandler(EntityMinecart minecart, Type type, double capactiy) {
            this(minecart, type, capactiy, 0.0);
        }

        public ChargeHandler(EntityMinecart minecart, Type type, double capacity, double lossPerTick) {
            this.minecart = minecart;
            this.type = type;
            this.capacity = capacity;
            this.lossPerTick = lossPerTick;
        }

        public double getCharge() {
            return charge;
        }

        public double getCapacity() {
            return capacity;
        }

        public double getLosses() {
            return lossPerTick;
        }

        public double getDraw() {
            return draw;
        }

        public Type getType() {
            return type;
        }

        /**
         * Averages the charge between two ChargeHandlers.
         * <p>
         * @param other
         */
        public void balance(ChargeHandler other) {
            double total = charge + other.charge;
            double half = total / 2.0;
            charge = half;
            other.charge = half;
        }

        public void setCharge(double charge) {
            this.charge = charge;
        }

        public void addCharge(double charge) {
            this.charge += charge;
        }

        /**
         * Remove up to the requested amount of charge and returns the amount
         * removed.
         * <p>
         * @param request
         * @return charge removed
         */
        public double removeCharge(double request) {
            if (charge >= request) {
                charge -= request;
                lastTickDraw += request;
                return request;
            }
            double ret = charge;
            charge = 0.0;
            lastTickDraw += ret;
            return ret;
        }

        private void removeLosses() {
            if (lossPerTick > 0.0)
                if (charge >= lossPerTick)
                    charge -= lossPerTick;
                else
                    charge = 0.0;
        }

        /**
         * ********************************************************************
         * The following functions must be called from your EntityMinecart
         * subclass
         * ********************************************************************
         */
        /**
         * Must be called once per tick while on tracks by the owning object.
         * Server side only.
         * <p>
         * <blockquote><pre>
         * {@code
         * public void onEntityUpdate()
         *  {
         *     super.onEntityUpdate();
         *     if (!world.isRemote)
         *        chargeHandler.tick();
         *  }
         * }
         * </pre></blockquote>
         */
        public void tick() {
            clock++;
            removeLosses();

            draw = (draw * 49.0 + lastTickDraw) / 50.0;
            lastTickDraw = 0.0;

            if (drewFromTrack > 0)
                drewFromTrack--;
            else if (type == Type.USER && charge < capacity && clock % DRAW_INTERVAL == 0) {
                ILinkageManager lm = CartTools.getLinkageManager(minecart.worldObj);<commit_msg>Simple fix to attempt to prevent Battery Cart draw down by Electric Locomotives on powered tracks<commit_after>/*
 * Copyright (c) CovertJaguar, 2011 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 railcraft.wikispaces.com.
 */
package mods.railcraft.api.electricity;

import java.util.Random;
import mods.railcraft.api.carts.CartTools;
import mods.railcraft.api.carts.ILinkageManager;
import mods.railcraft.api.tracks.RailTools;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.nbt.NBTTagCompound;

/**
 * This interface provides a simple means of using or producing Electricity
 * within a train.
 * <p>
 * The original Ic2 Battery Carts implement IEnergyTransfer. IEnergyTransfer was
 * a naive implementation of a Energy storage system for carts. I'll leave it in
 * place because of its Ic2 specific functions, but for all intents and purposes
 * this is the recommended and easier to implement interface for Electricity
 * related minecarts. In fact, the Railcraft Ic2 Energy Carts will be
 * redirecting to this interface. The Energy Loaders will continue to work
 * exclusively with IEnergyTransfer for the moment due to the high Ic2 coupling
 * of their design. An alternative loader block utilizing the IElectricMinecart
 * interface may be provided in the future, but no guarantee.
 * <p>
 * @author CovertJaguar <http://www.railcraft.info/>
 */
public interface IElectricMinecart {

    public ChargeHandler getChargeHandler();

    public static final class ChargeHandler {

        public static final int DRAW_INTERVAL = 8;
        private static final Random rand = new Random();

        public enum Type {

            /**
             * Users draw power from tracks, sources, and storage.
             */
            USER,
            /**
             * Sources provide power to users, but not storage. This interface
             * specifies no explicit way to charge Sources, that's up to the
             * implementer. Railcraft provides no Sources currently, and may
             * never do so.
             */
            SOURCE,
            /**
             * Storage provide power to users, but can't draw from tracks or
             * sources. This interface specifies no explicit way to charge
             * Storage, that's up to the implementer. Railcraft may provide a
             * trackside block in the future for charging Storage, but does not
             * currently.
             */
            STORAGE;
        }

        private final EntityMinecart minecart;
        private final Type type;
        private double capacity, charge, draw, lastTickDraw;
        private final double lossPerTick;
        private int clock = rand.nextInt();
        private int drewFromTrack;

        public ChargeHandler(EntityMinecart minecart, Type type, double capactiy) {
            this(minecart, type, capactiy, 0.0);
        }

        public ChargeHandler(EntityMinecart minecart, Type type, double capacity, double lossPerTick) {
            this.minecart = minecart;
            this.type = type;
            this.capacity = capacity;
            this.lossPerTick = lossPerTick;
        }

        public double getCharge() {
            return charge;
        }

        public double getCapacity() {
            return capacity;
        }

        public double getLosses() {
            return lossPerTick;
        }

        public double getDraw() {
            return draw;
        }

        public Type getType() {
            return type;
        }

        /**
         * Averages the charge between two ChargeHandlers.
         * <p>
         * @param other
         */
        public void balance(ChargeHandler other) {
            double total = charge + other.charge;
            double half = total / 2.0;
            charge = half;
            other.charge = half;
        }

        public void setCharge(double charge) {
            this.charge = charge;
        }

        public void addCharge(double charge) {
            this.charge += charge;
        }

        /**
         * Remove up to the requested amount of charge and returns the amount
         * removed.
         * <p>
         * @param request
         * @return charge removed
         */
        public double removeCharge(double request) {
            if (charge >= request) {
                charge -= request;
                lastTickDraw += request;
                return request;
            }
            double ret = charge;
            charge = 0.0;
            lastTickDraw += ret;
            return ret;
        }

        private void removeLosses() {
            if (lossPerTick > 0.0)
                if (charge >= lossPerTick)
                    charge -= lossPerTick;
                else
                    charge = 0.0;
        }

        /**
         * ********************************************************************
         * The following functions must be called from your EntityMinecart
         * subclass
         * ********************************************************************
         */
        /**
         * Must be called once per tick while on tracks by the owning object.
         * Server side only.
         * <p>
         * <blockquote><pre>
         * {@code
         * public void onEntityUpdate()
         *  {
         *     super.onEntityUpdate();
         *     if (!world.isRemote)
         *        chargeHandler.tick();
         *  }
         * }
         * </pre></blockquote>
         */
        public void tick() {
            clock++;
            removeLosses();

            draw = (draw * 49.0 + lastTickDraw) / 50.0;
            lastTickDraw = 0.0;

            if (drewFromTrack > 0)
                drewFromTrack--;
            else if (type == Type.USER && charge < (capacity / 2.0) && clock % DRAW_INTERVAL == 0) {
                ILinkageManager lm = CartTools.getLinkageManager(minecart.worldObj);
{'fraction_non_alphanumeric': 0.09557109557109557, 'fraction_numerical': 0.004995004995004995, 'mean_word_length': 1.7872389791183294, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6197923', 'n_tokens_mistral': 3072, 'n_tokens_neox': 2890, 'n_words': 1039}