summaryrefslogtreecommitdiff
path: root/ihl/datanet/DataNet.java
blob: 3f88c55a0f134e54b70afd06cf79c546d7f2bd4e (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
package ihl.datanet;

import ihl.IHLMod;
import ihl.flexible_cable.NodeEntity;
import ihl.interfaces.IDataNode;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import net.minecraft.nbt.NBTTagCompound;

public class DataNet {
	
	public Map<Integer, DataGrid> grids = new HashMap();
	private int griduid=0;
	
	public DataNet()
	{
	}
	
	public int getNewUniqueGridID()
	{
		for(int i=0;i<Integer.MAX_VALUE;i++)
		{
			if(grids.get(++griduid)==null)
			{
				return griduid;
			}
		}
		return -1;
	}

	public int mergeGrids(int gridID, int gridID2) 
	{
		if(gridID==-1 && gridID2!=-1)
		{
			return gridID2;
		}
		else if(gridID!=-1 && gridID2==-1)
		{
			return gridID;
		}
		else if(gridID==-1 && gridID2==-1)
		{
			int newGridID=this.getNewUniqueGridID();
			DataGrid cgrid;
			cgrid=new DataGrid();
			grids.put(newGridID, cgrid);
			return newGridID;
		}
		else if(gridID!=gridID2)
		{
			Iterator<IDataNode> tei = grids.get(gridID2).telist.iterator();
			while(tei.hasNext())
			{
				IDataNode te = tei.next();
				te.setDataGrid(gridID);
			}
			grids.remove(gridID2);
			return gridID;
		}
		return gridID2;
	}

	
	public DataGrid getGrid(int gridID)
	{
		if(this.grids.get(gridID)==null)
		{
			DataGrid cgrid;
			cgrid=new DataGrid();
			grids.put(gridID, cgrid);
			return cgrid;
		}
		else
		{
			return this.grids.get(gridID);
		}
	}
	
	public void splitGrids(int gridID)
	{
		DataGrid grid1 = this.grids.get(gridID);
		Set<IDataNode> excludedNodes = grid1.getListOfExcludedNodes();
		if(!excludedNodes.isEmpty())
		{
			if(grid1.telist.size()==1)
			{
				IDataNode singleNode = grid1.telist.iterator().next();
				singleNode.setDataGrid(-1);
				grid1.telist.remove(singleNode);
			}
			if(excludedNodes.size()==1)
			{
				IDataNode singleNode = excludedNodes.iterator().next();
				singleNode.setDataGrid(-1);
			}
			else //Form a new grid
			{
				int newGridId = this.getNewUniqueGridID();
				Iterator<IDataNode> excludedNodesI = excludedNodes.iterator();
				while(excludedNodesI.hasNext())
				{
					excludedNodesI.next().setDataGrid(newGridId);
				}
			}
		}
	}
	
	public void removeCableEntities(NBTTagCompound cable) 
	{
		int uid = cable.getInteger("chainUID");
		Set<NodeEntity> cs = IHLMod.proxy.nodeEntityRegistry.get(uid);
		if(cs!=null)
		{
			for(NodeEntity ne:cs)
			{
				if(ne!=null)
				{
					ne.setDead();
				}
			}
		}
	}
}