aboutsummaryrefslogtreecommitdiff
path: root/src/Java/miscutil/enderio/conduit/GregTech/GtConduitNetwork.java
blob: 7855547a7ec9e8341097e1fefe32903d6f092290 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package miscutil.enderio.conduit.GregTech;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import mekanism.api.gas.GasStack;
import mekanism.api.gas.IGasHandler;
import miscutil.enderio.conduit.ConduitGTHandler;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import cpw.mods.fml.common.gameevent.TickEvent.ServerTickEvent;
import crazypants.enderio.conduit.ConduitNetworkTickHandler;
import crazypants.enderio.conduit.ConduitNetworkTickHandler.TickListener;
import crazypants.enderio.conduit.IConduit;
import crazypants.util.BlockCoord;

public class GtConduitNetwork extends AbstractGtTankConduitNetwork<GtConduit> {

  private final ConduitGtTank tank = new ConduitGtTank(0);

  private final Set<GtOutput> outputs = new HashSet<GtOutput>();

  private Iterator<GtOutput> outputIterator;

  private int ticksActiveUnsynced;

  private boolean lastSyncedActive = false;

  private int lastSyncedVolume = -1;

  private long timeAtLastApply;

  private final InnerTickHandler tickHandler = new InnerTickHandler();

  public GtConduitNetwork() {
    super(GtConduit.class);
  }

  @Override
  public Class<IGtConduit> getBaseConduitType() {
    return IGtConduit.class;
  }

  @Override
  public void addConduit(GtConduit con) {
    tank.setCapacity(tank.getMaxGas() + GtConduit.CONDUIT_VOLUME);
    if(con.getTank().containsValidGas()) {
      tank.addAmount(con.getTank().getStored());
    }
    for (ForgeDirection dir : con.getExternalConnections()) {
      if(con.getConnectionMode(dir).acceptsOutput()) {
        outputs.add(new GtOutput(con.getLocation().getLocation(dir), dir.getOpposite()));
      }
    }
    outputIterator = null;
    super.addConduit(con);
  }

  @Override
  public boolean setGasType(GasStack newType) {
    if(super.setGasType(newType)) {

      GasStack ft = getGasType();
      tank.setGas(ft == null ? null : ft.copy());
      return true;
    }
    return false;
  }

  @Override
  public void destroyNetwork() {
    setConduitVolumes();
    outputs.clear();
    super.destroyNetwork();
  }

  private void setConduitVolumes() {
    if(tank.containsValidGas() && !conduits.isEmpty()) {
      GasStack gasPerConduit = tank.getGas().copy();
      int numCons = conduits.size();
      int leftOvers = gasPerConduit.amount % numCons;
      gasPerConduit.amount = gasPerConduit.amount / numCons;

      for (GtConduit con : conduits) {
        GasStack f = gasPerConduit.copy();
        if(leftOvers > 0) {
          f.amount += 1;
          leftOvers--;
        }
        con.getTank().setGas(f);
        BlockCoord bc = con.getLocation();
        con.getBundle().getEntity().getWorldObj().markTileEntityChunkModified(bc.x, bc.y, bc.z, con.getBundle().getEntity());
      }

    }
  }

  @Override
  public void onUpdateEntity(IConduit conduit) {
    World world = conduit.getBundle().getEntity().getWorldObj();
    if(world == null) {
      return;
    }
    if(world.isRemote) {
      return;
    }

    long curTime = world.getTotalWorldTime();
    if(curTime > 0 && curTime != timeAtLastApply) {
      timeAtLastApply = curTime;
      ConduitNetworkTickHandler.instance.addListener(tickHandler);
    }

  }

  private void doTick() {
    if(gasType == null || outputs.isEmpty() || !tank.containsValidGas() || tank.isEmpty()) {
      updateActiveState();
      return;
    }

    if(outputIterator == null || !outputIterator.hasNext()) {
      outputIterator = outputs.iterator();
    }

    updateActiveState();

    int numVisited = 0;
    while (!tank.isEmpty() && numVisited < outputs.size()) {
      if(!outputIterator.hasNext()) {
        outputIterator = outputs.iterator();
      }
      GtOutput output = outputIterator.next();
      if(output != null) {
    	 ConduitGTHandler cont = (ConduitGTHandler) getTankContainer(output.location);
        if(cont != null) {
          GasStack offer = tank.getGas().copy();
          int filled = cont.receiveGas(output.dir, offer);
          if(filled > 0) {
            tank.addAmount(-filled);

          }
        }
      }
      numVisited++;
    }

  }

  private void updateActiveState() {
    boolean isActive = tank.containsValidGas() && !tank.isEmpty();
    if(lastSyncedActive != isActive) {
      ticksActiveUnsynced++;
    } else {
      ticksActiveUnsynced = 0;
    }
    if(ticksActiveUnsynced >= 10 || ticksActiveUnsynced > 0 && isActive) {
      if(!isActive) {
        setGasType(null);
      }
      for (IConduit con : conduits) {
        con.setActive(isActive);
      }
      lastSyncedActive = isActive;
      ticksActiveUnsynced = 0;
    }
  }

  public int fill(ForgeDirection from, GasStack resource, boolean doFill) {
    if(resource == null) {
      return 0;
    }
    resource.amount = Math.min(resource.amount, GtConduit.MAX_IO_PER_TICK);
    boolean gasWasValid = tank.containsValidGas();
    int res = tank.receive(resource, doFill);
    if(doFill && res > 0 && gasWasValid) {
      int vol = tank.getStored();
      setGasType(resource);
      tank.setAmount(vol);
    }
    return res;
  }

  public GasStack drain(ForgeDirection from, GasStack resource, boolean doDrain) {
    if(resource == null || tank.isEmpty() || !tank.containsValidGas() || !GtConduitNetwork.areGassCompatable(getGasType(), resource)) {
      return null;
    }
    int amount = Math.min(resource.amount, tank.getStored());
    amount = Math.min(amount, GtConduit.MAX_IO_PER_TICK);
    GasStack result = resource.copy();
    result.amount = amount;
    if(doDrain) {
      tank.addAmount(-amount);
    }
    return result;
  }

  public GasStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
    if(tank.isEmpty() || !tank.containsValidGas()) {
      return null;
    }
    int amount = Math.min(maxDrain, tank.getStored());
    GasStack result = tank.getGas().copy();
    result.amount = amount;
    if(doDrain) {
      tank.addAmount(-amount);
    }
    return result;
  }

  public boolean extractFrom(GtConduit advancedGtConduit, ForgeDirection dir, int maxExtractPerTick) {

    if(tank.isFull()) {
      return false;
    }

    IGasHandler extTank = getTankContainer(advancedGtConduit, dir);
    if(extTank != null) {
      int maxExtract = Math.min(maxExtractPerTick, tank.getAvailableSpace());

      if(gasType == null || !tank.containsValidGas()) {
        GasStack drained = extTank.drawGas(dir.getOpposite(), maxExtract);
        if(drained == null || drained.amount <= 0) {
          return false;
        }
        setGasType(drained);
        tank.setGas(drained.copy());
        return true;
      }

      GasStack couldDrain = gasType.copy();
      couldDrain.amount = maxExtract;

      //      GasStack drained = extTank.drain(dir.getOpposite(), couldDrain, true);
      //      if(drained == null || drained.amount <= 0) {
      //        return false;
      //      }
      //      tank.addAmount(drained.amount);     

      //Have to use this 'double handle' approach to work around an issue with TiC
      GasStack drained = extTank.drawGas(dir.getOpposite(), maxExtract);
      if(drained == null || drained.amount == 0) {
        return false;
      } else {
        if(drained.isGasEqual(getGasType())) {
          tank.addAmount(drained.amount);
        }
      }
      return true;
    }
    return false;
  }

  public IGasHandler getTankContainer(BlockCoord bc) {
    World w = getWorld();
    if(w == null) {
      return null;
    }
    TileEntity te = w.getTileEntity(bc.x, bc.y, bc.z);
    if(te instanceof IGasHandler) {
      return (IGasHandler) te;
    }
    return null;
  }

  public IGasHandler getTankContainer(GtConduit con, ForgeDirection dir) {
    BlockCoord bc = con.getLocation().getLocation(dir);
    return getTankContainer(bc);
  }

  World getWorld() {
    if(conduits.isEmpty()) {
      return null;
    }
    return conduits.get(0).getBundle().getWorld();
  }

  public void removeInput(GtOutput lo) {
    outputs.remove(lo);
    outputIterator = null;
  }

  public void addInput(GtOutput lo) {
    outputs.add(lo);
    outputIterator = null;
  }

  public void updateConduitVolumes() {
    if(tank.getStored() == lastSyncedVolume) {
      return;
    }
    setConduitVolumes();
    lastSyncedVolume = tank.getStored();
  }

  private class InnerTickHandler implements TickListener {

    @Override
    public void tickStart(ServerTickEvent evt) {
    }

    @Override
    public void tickEnd(ServerTickEvent evt) {
      doTick();
    }
  }

}