2013-08-27 00:49:32 +02:00
|
|
|
package mekanism.api.transmitters;
|
2013-08-16 17:56:42 +02:00
|
|
|
|
2013-08-22 17:36:31 +02:00
|
|
|
import java.util.ArrayList;
|
2013-08-25 18:17:56 +02:00
|
|
|
import java.util.Arrays;
|
2013-08-22 17:36:31 +02:00
|
|
|
import java.util.Collection;
|
2013-11-11 23:23:14 +01:00
|
|
|
import java.util.Collections;
|
2013-08-17 19:32:05 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
2013-08-22 17:36:31 +02:00
|
|
|
import java.util.List;
|
2013-08-17 19:32:05 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.NoSuchElementException;
|
|
|
|
import java.util.Set;
|
2013-08-25 18:17:56 +02:00
|
|
|
|
2013-08-27 00:49:32 +02:00
|
|
|
import mekanism.api.Object3D;
|
2013-08-17 19:32:05 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2013-08-22 17:36:31 +02:00
|
|
|
import net.minecraft.world.World;
|
2013-08-17 19:32:05 +02:00
|
|
|
import net.minecraftforge.common.ForgeDirection;
|
|
|
|
|
2013-08-19 03:32:47 +02:00
|
|
|
public abstract class DynamicNetwork<A, N> implements ITransmitterNetwork<A, N>
|
2013-08-16 17:56:42 +02:00
|
|
|
{
|
2013-08-19 03:32:47 +02:00
|
|
|
public HashSet<ITransmitter<N>> transmitters = new HashSet<ITransmitter<N>>();
|
2013-08-17 19:32:05 +02:00
|
|
|
|
2013-11-13 02:19:06 +01:00
|
|
|
public HashSet<A> possibleAcceptors = new HashSet<A>();
|
|
|
|
public HashMap<A, ForgeDirection> acceptorDirections = new HashMap<A, ForgeDirection>();
|
2013-08-17 19:32:05 +02:00
|
|
|
|
2013-08-16 17:56:42 +02:00
|
|
|
protected int ticksSinceCreate = 0;
|
|
|
|
|
|
|
|
protected boolean fixed = false;
|
2013-08-17 19:32:05 +02:00
|
|
|
|
2013-08-22 17:36:31 +02:00
|
|
|
protected abstract ITransmitterNetwork<A, N> create(ITransmitter<N>... varTransmitters);
|
|
|
|
|
|
|
|
protected abstract ITransmitterNetwork<A, N> create(Collection<ITransmitter<N>> collection);
|
|
|
|
|
|
|
|
protected abstract ITransmitterNetwork<A, N> create(Set<N> networks);
|
|
|
|
|
2013-08-19 03:32:47 +02:00
|
|
|
public void addAllTransmitters(Set<ITransmitter<N>> newTransmitters)
|
2013-08-17 19:32:05 +02:00
|
|
|
{
|
|
|
|
transmitters.addAll(newTransmitters);
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
@Override
|
2013-08-19 03:32:47 +02:00
|
|
|
public void removeTransmitter(ITransmitter<N> transmitter)
|
2013-08-17 19:32:05 +02:00
|
|
|
{
|
|
|
|
transmitters.remove(transmitter);
|
|
|
|
|
|
|
|
if(transmitters.size() == 0)
|
|
|
|
{
|
|
|
|
deregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
@Override
|
2013-08-17 19:32:05 +02:00
|
|
|
public void register()
|
|
|
|
{
|
|
|
|
try {
|
2013-08-19 03:32:47 +02:00
|
|
|
ITransmitter<N> aTransmitter = transmitters.iterator().next();
|
2013-08-17 19:32:05 +02:00
|
|
|
|
|
|
|
if(aTransmitter instanceof TileEntity && !((TileEntity)aTransmitter).worldObj.isRemote)
|
|
|
|
{
|
|
|
|
TransmitterNetworkRegistry.getInstance().registerNetwork(this);
|
|
|
|
}
|
|
|
|
} catch(NoSuchElementException e) {}
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
@Override
|
2013-08-17 19:32:05 +02:00
|
|
|
public void deregister()
|
|
|
|
{
|
|
|
|
transmitters.clear();
|
|
|
|
TransmitterNetworkRegistry.getInstance().removeNetwork(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSize()
|
|
|
|
{
|
|
|
|
return transmitters.size();
|
|
|
|
}
|
2013-08-18 22:51:47 +02:00
|
|
|
|
2013-08-23 20:23:08 +02:00
|
|
|
@Override
|
|
|
|
public int getAcceptorSize()
|
|
|
|
{
|
|
|
|
return possibleAcceptors.size();
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
@Override
|
|
|
|
public void tick()
|
|
|
|
{
|
|
|
|
if(!fixed)
|
|
|
|
{
|
|
|
|
++ticksSinceCreate;
|
|
|
|
|
|
|
|
if(ticksSinceCreate > 1200)
|
|
|
|
{
|
|
|
|
ticksSinceCreate = 0;
|
|
|
|
fixMessedUpNetwork(transmitters.iterator().next());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-22 17:36:31 +02:00
|
|
|
|
|
|
|
@Override
|
2013-10-20 06:04:11 +02:00
|
|
|
public synchronized void fixMessedUpNetwork(ITransmitter<N> transmitter)
|
2013-08-22 17:36:31 +02:00
|
|
|
{
|
|
|
|
if(transmitter instanceof TileEntity)
|
|
|
|
{
|
2013-08-22 20:07:28 +02:00
|
|
|
NetworkFinder finder = new NetworkFinder(((TileEntity)transmitter).getWorldObj(), getTransmissionType(), Object3D.get((TileEntity)transmitter));
|
2013-08-22 17:36:31 +02:00
|
|
|
List<Object3D> partNetwork = finder.exploreNetwork();
|
|
|
|
Set<ITransmitter<N>> newTransporters = new HashSet<ITransmitter<N>>();
|
|
|
|
|
|
|
|
for(Object3D node : partNetwork)
|
|
|
|
{
|
|
|
|
TileEntity nodeTile = node.getTileEntity(((TileEntity)transmitter).worldObj);
|
|
|
|
|
2013-08-25 07:34:45 +02:00
|
|
|
if(TransmissionType.checkTransmissionType(nodeTile, getTransmissionType(), (TileEntity) transmitter))
|
2013-08-22 17:36:31 +02:00
|
|
|
{
|
2013-08-27 01:04:00 +02:00
|
|
|
((ITransmitter<N>)nodeTile).removeFromTransmitterNetwork();
|
2013-08-22 17:36:31 +02:00
|
|
|
newTransporters.add((ITransmitter<N>)nodeTile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ITransmitterNetwork<A, N> newNetwork = create(newTransporters);
|
|
|
|
newNetwork.refresh();
|
|
|
|
newNetwork.setFixed(true);
|
|
|
|
deregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-25 18:17:56 +02:00
|
|
|
@Override
|
2013-10-20 06:04:11 +02:00
|
|
|
public synchronized void split(ITransmitter<N> splitPoint)
|
2013-08-25 18:17:56 +02:00
|
|
|
{
|
|
|
|
if(splitPoint instanceof TileEntity)
|
|
|
|
{
|
|
|
|
removeTransmitter(splitPoint);
|
|
|
|
|
|
|
|
TileEntity[] connectedBlocks = new TileEntity[6];
|
|
|
|
boolean[] dealtWith = {false, false, false, false, false, false};
|
|
|
|
|
|
|
|
for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
|
|
|
|
{
|
|
|
|
TileEntity sideTile = Object3D.get((TileEntity)splitPoint).getFromSide(direction).getTileEntity(((TileEntity)splitPoint).worldObj);
|
|
|
|
|
|
|
|
if(sideTile != null)
|
|
|
|
{
|
|
|
|
connectedBlocks[Arrays.asList(ForgeDirection.values()).indexOf(direction)] = sideTile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int countOne = 0; countOne < connectedBlocks.length; countOne++)
|
|
|
|
{
|
|
|
|
TileEntity connectedBlockA = connectedBlocks[countOne];
|
|
|
|
|
|
|
|
if(TransmissionType.checkTransmissionType(connectedBlockA, getTransmissionType()) && !dealtWith[countOne])
|
|
|
|
{
|
|
|
|
NetworkFinder finder = new NetworkFinder(((TileEntity)splitPoint).worldObj, getTransmissionType(), Object3D.get(connectedBlockA), Object3D.get((TileEntity)splitPoint));
|
|
|
|
List<Object3D> partNetwork = finder.exploreNetwork();
|
|
|
|
|
|
|
|
for(int countTwo = countOne + 1; countTwo < connectedBlocks.length; countTwo++)
|
|
|
|
{
|
|
|
|
TileEntity connectedBlockB = connectedBlocks[countTwo];
|
|
|
|
|
|
|
|
if(TransmissionType.checkTransmissionType(connectedBlockB, getTransmissionType()) && !dealtWith[countTwo])
|
|
|
|
{
|
|
|
|
if(partNetwork.contains(Object3D.get(connectedBlockB)))
|
|
|
|
{
|
|
|
|
dealtWith[countTwo] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Set<ITransmitter<N>> newNetCables = new HashSet<ITransmitter<N>>();
|
|
|
|
|
|
|
|
for(Object3D node : finder.iterated)
|
|
|
|
{
|
|
|
|
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
|
|
|
|
|
|
|
|
if(TransmissionType.checkTransmissionType(nodeTile, getTransmissionType()))
|
|
|
|
{
|
|
|
|
if(nodeTile != splitPoint)
|
|
|
|
{
|
|
|
|
newNetCables.add((ITransmitter<N>)nodeTile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ITransmitterNetwork<A, N> newNetwork = create(newNetCables);
|
|
|
|
newNetwork.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-22 17:36:31 +02:00
|
|
|
@Override
|
|
|
|
public void setFixed(boolean value)
|
|
|
|
{
|
|
|
|
fixed = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class NetworkFinder
|
|
|
|
{
|
2013-08-22 20:07:28 +02:00
|
|
|
public TransmissionType transmissionType;
|
|
|
|
|
2013-08-22 17:36:31 +02:00
|
|
|
public World worldObj;
|
|
|
|
public Object3D start;
|
|
|
|
|
|
|
|
public List<Object3D> iterated = new ArrayList<Object3D>();
|
|
|
|
public List<Object3D> toIgnore = new ArrayList<Object3D>();
|
|
|
|
|
2013-08-22 20:07:28 +02:00
|
|
|
public NetworkFinder(World world, TransmissionType type, Object3D location, Object3D... ignore)
|
2013-08-22 17:36:31 +02:00
|
|
|
{
|
|
|
|
worldObj = world;
|
|
|
|
start = location;
|
|
|
|
|
2013-08-22 20:07:28 +02:00
|
|
|
transmissionType = type;
|
2013-08-22 17:36:31 +02:00
|
|
|
|
|
|
|
if(ignore != null)
|
|
|
|
{
|
2013-10-20 06:04:11 +02:00
|
|
|
for(int i = 0; i < ignore.length; i++)
|
2013-08-25 07:34:45 +02:00
|
|
|
{
|
|
|
|
this.toIgnore.add(ignore[i]);
|
|
|
|
}
|
2013-08-22 17:36:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loopAll(Object3D location)
|
|
|
|
{
|
2013-08-25 03:39:00 +02:00
|
|
|
if(TransmissionType.checkTransmissionType(location.getTileEntity(worldObj), transmissionType))
|
2013-08-22 17:36:31 +02:00
|
|
|
{
|
|
|
|
iterated.add(location);
|
|
|
|
}
|
2013-10-20 06:04:11 +02:00
|
|
|
else {
|
2013-08-25 07:34:45 +02:00
|
|
|
toIgnore.add(location);
|
|
|
|
}
|
2013-08-22 17:36:31 +02:00
|
|
|
|
|
|
|
for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
|
|
|
|
{
|
|
|
|
Object3D obj = location.getFromSide(direction);
|
|
|
|
|
|
|
|
if(!iterated.contains(obj) && !toIgnore.contains(obj))
|
|
|
|
{
|
|
|
|
TileEntity tileEntity = obj.getTileEntity(worldObj);
|
|
|
|
|
2013-08-25 07:34:45 +02:00
|
|
|
if(TransmissionType.checkTransmissionType(tileEntity, transmissionType, location.getTileEntity(worldObj)))
|
2013-08-22 17:36:31 +02:00
|
|
|
{
|
|
|
|
loopAll(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Object3D> exploreNetwork()
|
|
|
|
{
|
|
|
|
loopAll(start);
|
|
|
|
|
|
|
|
return iterated;
|
|
|
|
}
|
|
|
|
}
|
2013-08-16 17:56:42 +02:00
|
|
|
}
|