@@ -19,7 +19,11 @@ public boolean canReceive(@NotNull Direction dir) {
19
19
@ Override
20
20
public void tick () {
21
21
super .tick ();
22
+ //reset counters
22
23
ampsUsing = 0 ;
24
+ averageAmpLoad .increment (worldObj ,0 );
25
+ averageEnergyTransfer .increment (worldObj ,0 );
26
+ //try to pull max allowed current from any connected side
23
27
for (Direction dir : Direction .values ()) {
24
28
TileEntity tile = dir .getTileEntity (worldObj ,this );
25
29
if (tile instanceof TileEntityElectricConductor ) {
@@ -35,21 +39,28 @@ public long receiveEnergy(@NotNull Direction dir, long amperage) {
35
39
return 0 ;
36
40
}
37
41
long remainingCapacity = getCapacityRemaining ();
42
+ long willUseAmps = 0 ;
38
43
TileEntity tile = dir .getTileEntity (worldObj ,this );
39
44
if (tile instanceof TileEntityElectricConductor ) {
40
45
TileEntityElectricConductor wire = (TileEntityElectricConductor ) tile ;
41
46
47
+ //for every known path
42
48
for (NetworkPath path : energyNet .getPathData (wire .getPosition ())) {
43
49
long pathLoss = 0 ;
50
+ //ignore itself or non-electric components in the path
44
51
if (path .target == this || !(path .target instanceof IElectric )){
45
52
continue ;
46
53
}
47
54
IElectric dest = (IElectric ) path .target ;
48
55
49
- if (dest .canProvide (path .targetDirection )) {
56
+ //receive/provide check
57
+ if (dest .canProvide (path .targetDirection .getOpposite ())) {
50
58
if (canReceive (dir )) {
59
+ //get max voltage from destination
60
+ //limit amps to maximum available from dest
51
61
long voltage = dest .getMaxOutputVoltage ();
52
62
amperage = Math .min (amperage , (dest .getMaxOutputAmperage () - dest .getAmpsCurrentlyUsed ()));
63
+ //calculate path loss
53
64
for (NetworkComponentTile component : path .path ) {
54
65
if (component instanceof TileEntityElectricConductor ){
55
66
pathLoss += ((TileEntityElectricConductor ) component ).getProperties ().getMaterial ().getLossPerBlock ();
@@ -59,8 +70,10 @@ public long receiveEnergy(@NotNull Direction dir, long amperage) {
59
70
//avoid paths where all energy is lost
60
71
continue ;
61
72
}
73
+ //voltage drop
62
74
long pathVoltage = voltage - pathLoss ;
63
75
boolean pathBroken = false ;
76
+ //handle wires with insufficient voltage rating
64
77
for (NetworkComponentTile pathTile : path .path ) {
65
78
if (pathTile instanceof TileEntityElectricConductor ){
66
79
TileEntityElectricConductor pathWire = (TileEntityElectricConductor ) pathTile ;
@@ -75,29 +88,34 @@ public long receiveEnergy(@NotNull Direction dir, long amperage) {
75
88
if (pathBroken ) continue ;
76
89
77
90
if (pathVoltage > 0 ){
91
+ //handle device over-voltage
78
92
if (pathVoltage > getMaxInputVoltage ()){
79
93
//TODO: do something bad here later :tf:
80
94
return Math .max (amperage , getMaxInputAmperage () - ampsUsing ); //short circuit amperage
81
95
}
82
96
if (remainingCapacity >= pathVoltage ){
83
- long willUseAmps = Math .min (remainingCapacity / pathVoltage , Math .min (amperage , getMaxInputAmperage () - ampsUsing ));
97
+ //calculate real current draw
98
+ willUseAmps = Math .min (remainingCapacity / pathVoltage , Math .min (amperage , getMaxInputAmperage () - ampsUsing ));
84
99
if (willUseAmps > 0 ){
85
- for (NetworkComponentTile pathTile : path .path ) {
86
- if (pathTile instanceof TileEntityElectricConductor ) {
87
- TileEntityElectricConductor pathWire = (TileEntityElectricConductor ) pathTile ;
88
- long voltageTraveled = voltage ;
89
- voltageTraveled -= pathWire .getProperties ().getMaterial ().getLossPerBlock ();
90
- if (voltageTraveled <= 0 ) break ;
91
- pathWire .incrementAmperage (willUseAmps );
92
- }
93
- }
94
100
long willUseEnergy = pathVoltage * willUseAmps ;
95
101
if (dest .getEnergy () >= willUseEnergy ){
96
- addAmpUsage (willUseAmps );
97
- dest .addAmpUsage (willUseAmps );
102
+
103
+ //set current in wires
104
+ for (NetworkComponentTile pathTile : path .path ) {
105
+ if (pathTile instanceof TileEntityElectricConductor ) {
106
+ TileEntityElectricConductor pathWire = (TileEntityElectricConductor ) pathTile ;
107
+ long voltageTraveled = voltage ;
108
+ voltageTraveled -= pathWire .getProperties ().getMaterial ().getLossPerBlock ();
109
+ if (voltageTraveled <= 0 ) break ;
110
+ pathWire .incrementAmperage (willUseAmps );
111
+ }
112
+ }
113
+
114
+ //finish energy transfer
115
+ addAmpsToUse (willUseAmps );
116
+ //dest.addAmpsToUse(willUseAmps);
98
117
internalAddEnergy (willUseEnergy );
99
118
dest .internalRemoveEnergy (willUseEnergy );
100
- return willUseAmps ;
101
119
}
102
120
}
103
121
}
@@ -106,6 +124,6 @@ public long receiveEnergy(@NotNull Direction dir, long amperage) {
106
124
}
107
125
}
108
126
}
109
- return 0 ;
127
+ return willUseAmps ; //return amps used
110
128
}
111
129
}
0 commit comments