aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/graphs/paths/PowerNodePath.java
blob: d09cf059e93c863bddf69cd7507bd653938c5eee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package gregtech.api.graphs.paths;

import net.minecraft.server.MinecraftServer;

import gregtech.api.enums.TickTime;
import gregtech.api.metatileentity.BaseMetaPipeEntity;
import gregtech.api.metatileentity.MetaPipeEntity;
import gregtech.api.metatileentity.implementations.GT_MetaPipeEntity_Cable;
import gregtech.api.util.AveragePerTickCounter;

// path for cables
// all calculations like amp and voltage happens here
public class PowerNodePath extends NodePath {

    long mMaxAmps;
    long mAmps = 0;
    long mLoss;
    long mVoltage = 0;
    long mMaxVoltage;
    int mTick = 0;
    boolean mCountUp = true;

    private AveragePerTickCounter avgAmperageCounter = new AveragePerTickCounter(TickTime.SECOND);
    private AveragePerTickCounter avgVoltageCounter = new AveragePerTickCounter(TickTime.SECOND);

    public PowerNodePath(MetaPipeEntity[] aCables) {
        super(aCables);
    }

    public long getLoss() {
        return mLoss;
    }

    public void applyVoltage(long aVoltage, boolean aCountUp) {

        avgVoltageCounter.addValue(Math.max(aVoltage - mLoss, 0));

        int tNewTime = MinecraftServer.getServer()
            .getTickCounter();
        if (mTick != tNewTime) {
            reset(tNewTime - mTick);
            mTick = tNewTime;
            this.mVoltage = aVoltage;
            this.mCountUp = aCountUp;
        } else if (this.mCountUp != aCountUp && (aVoltage - mLoss) > this.mVoltage || aVoltage > this.mVoltage) {
            this.mCountUp = aCountUp;
            this.mVoltage = aVoltage;
        }
        if (aVoltage > mMaxVoltage) {
            lock.addTileEntity(null);
            for (MetaPipeEntity tCable : mPipes) {
                if (((GT_MetaPipeEntity_Cable) tCable).mVoltage < this.mVoltage) {
                    BaseMetaPipeEntity tBaseCable = (BaseMetaPipeEntity) tCable.getBaseMetaTileEntity();
                    if (tBaseCable != null) {
                        tBaseCable.setToFire();
                    }
                }
            }
        }
    }

    private void reset(int aTimePassed) {
        if (aTimePassed < 0 || aTimePassed > 100) {
            mAmps = 0;
            return;
        }
        mAmps = Math.max(0, mAmps - (mMaxAmps * aTimePassed));
    }

    public void addAmps(long aAmps) {

        avgAmperageCounter.addValue(aAmps);

        this.mAmps += aAmps;
        if (this.mAmps > mMaxAmps * 40) {
            lock.addTileEntity(null);
            for (MetaPipeEntity tCable : mPipes) {
                if (((GT_MetaPipeEntity_Cable) tCable).mAmperage * 40 < this.mAmps) {
                    BaseMetaPipeEntity tBaseCable = (BaseMetaPipeEntity) tCable.getBaseMetaTileEntity();
                    if (tBaseCable != null) {
                        tBaseCable.setToFire();
                    }
                }
            }
        }
    }

    public long getAmperage() {
        return avgAmperageCounter.getLast();
    }

    public double getAvgAmperage() {
        return avgAmperageCounter.getAverage();
    }

    public long getVoltage() {
        return avgVoltageCounter.getLast();
    }

    public double getAvgVoltage() {
        return avgVoltageCounter.getAverage();
    }

    @Override
    protected void processPipes() {
        super.processPipes();
        mMaxAmps = Integer.MAX_VALUE;
        mMaxVoltage = Integer.MAX_VALUE;
        for (MetaPipeEntity tCable : mPipes) {
            if (tCable instanceof GT_MetaPipeEntity_Cable) {
                mMaxAmps = Math.min(((GT_MetaPipeEntity_Cable) tCable).mAmperage, mMaxAmps);
                mLoss += ((GT_MetaPipeEntity_Cable) tCable).mCableLossPerMeter;
                mMaxVoltage = Math.min(((GT_MetaPipeEntity_Cable) tCable).mVoltage, mMaxVoltage);
            }
        }
    }
}