private int GetLocalNodeType(int from, int to, int x, int y, int z) { if (from < 0) { from = 0; } int n = 0; for (int i = from; i <= to && i < chances.Count; i++) { n += chances[i]; } int rnd = GMARandom.Range3D(0, n, x, y, z); int s = 0; for (int i = from; i <= to && i < chances.Count; i++) { if (s + chances[i] > rnd) { return(i); } s += chances[i]; } return(-1); }
public GlobalNodeInfo GenerateGNI(int x, int y, int difficulty) { GlobalNodeInfo info = new GlobalNodeInfo(); info.info = new NetworkNodeInfo(); info.info.globalAddress = new Vector2Int(x, y); info.info.localAddress = new Vector2Int(x, y); info.info.nodeDifficulty = difficulty; info.info.nodeRole = 7; info.info.nodeType = Node.GetType(x, y); width = 2 + GMARandom.Range3D(difficulty, (1 + difficulty) * 2, x, y, 1); height = 2 + GMARandom.Range3D(difficulty, (1 + difficulty) * 2, x, y, 2); this.difficulty = difficulty; Debug.Log("Init " + x + " " + y + " " + difficulty + " " + width + " " + height); net = new int[width, height]; links = new Link[width, height]; roles = new int[width, height]; haveEntryPoint = false; shiftx = GMARandom.Range2D(-1000000, 100000, x + y, (x + 1) * (y + 1)); shifty = GMARandom.Range2D(-1000000, 100000, x - y, x + y); //shiftx = -863620; //shifty = 52045; Debug.Log(x + " " + y + " " + shiftx + " " + shifty); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int nodeType = Node.GetType(i + shiftx, j + shifty); net[i, j] = nodeType; links[i, j] = GetLink(nodeType, new Vector2(i, j)); links[i, j].nodeType = nodeType; roles[i, j] = -1; } } for (int i = 0; i < width; i++) { net[i, 0] = Node.RemoveLink(net[i, 0], 2); links[i, 0].RemoveLink(2); //if ((net[i, 0] == 1 || net[i, 0] == 8 || net[i, 0] == 2) && !haveEntryPoint) //TODO entryPoint //{ // entryPoint.x = i; // entryPoint.y = 0; // haveEntryPoint = true; //} net[i, height - 1] = Node.RemoveLink(net[i, height - 1], 0); links[i, height - 1].RemoveLink(0); } for (int j = 0; j < height; j++) { net[0, j] = Node.RemoveLink(net[0, j], 3); links[0, j].RemoveLink(3); //if ((net[0, j] == 1 || net[0, j] == 2) && !haveEntryPoint) //{ // entryPoint.x = 0; // entryPoint.y = j; // haveEntryPoint = true; //} net[width - 1, j] = Node.RemoveLink(net[width - 1, j], 1); links[width - 1, j].RemoveLink(1); } FormNodesGroups(); ConnectNearestPoints(); NodesRedefinition(); GetEndPoints(); root = MakeTree(); FormEndPointsGroups2(); return(info); }
private void FormEndPointsGroups2() { List <List <Node> > groups = new List <List <Node> >(); List <KeyValuePair <int, int> > handled = new List <KeyValuePair <int, int> >(); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { Node node = new Node(GetNodeType(i, j), i, j); if (node.x == entryPoint.x && node.y == entryPoint.y) { continue; } var links = GetLinks(node); if (links.Count != 1) { continue; } if (handled.Contains(new KeyValuePair <int, int>(i, j))) { //Debug.Log("Skip " + i + " " + j); continue; } handled.Add(new KeyValuePair <int, int>(i, j)); //Debug.Log("Handle " + i + " " + j); List <Node> group = new List <Node>(); group.Add(node); Node prev = node; node = links[0]; while (node != null) { //Debug.Log("Add " + node.x + " " + node.y); group.Add(node); //if (handled.Contains(new KeyValuePair<int, int>(node.x, node.y))) break; handled.Add(new KeyValuePair <int, int>(node.x, node.y)); links = GetLinks(node); if (links.Count != 2) { break; } if (node.x == entryPoint.x && node.y == entryPoint.y) { break; } //Debug.Log("Node " + node.x + " " + node.y + " has 2 links " + links[0].x + " " + links[0].y + " | " + links[1].x + " " + links[1].y); if (links[0].x == prev.x && links[0].y == prev.y) { //Debug.Log("Select 1"); prev = node; node = links[1]; } else { //Debug.Log("Select 0"); prev = node; node = links[0]; } } groups.Add(group); } } Debug.Log("Found " + groups.Count + " groups"); //for(int i = 0; i < groups.Count; i++) //{ // string str = ""; // for (int j = 0; j < groups[i].Count; j++) str += "(" + groups[i][j].x + "," + groups[i][j].y + "), "; // Debug.Log(str); //} for (int i = 0; i < groups.Count; i++) { string str = ""; for (int j = 0; j < groups[i].Count; j++) { str += "(" + groups[i][j].x + "," + groups[i][j].y + "), "; } Debug.Log(str); var group = groups[i]; int role = -1; int cnt = group.Count - 1; for (int j = 0; j <= cnt; j++) { var node = group[j]; if (cnt == 1) { role = GetLocalNodeType(4, 5, globalAddress.x + globalAddress.y, i, j);//DS or PC Debug.Log("Set " + node.x + " " + node.y + " to " + role + " | " + j); } else if (cnt == 2) { if (j == 0) { role = GetLocalNodeType(2, 5, globalAddress.x + globalAddress.y, i, j); // GMARandom.Range3D(2, 5, coord.x + coord.y, i, j); } else { role = GetLocalNodeType(1, 5, globalAddress.x + globalAddress.y, i, j); // GMARandom.Range3D(1, 5, coord.x + coord.y, i, j); } Debug.Log("Set " + node.x + " " + node.y + " to " + role + " | " + j); } else if (cnt == 3) { if (j == cnt - 1) { role = 1; //SM } else { role = GetLocalNodeType(2, 5, globalAddress.x + globalAddress.y, i, j); // GMARandom.Range3D(2, 5, coord.x + coord.y, i, j); } Debug.Log("Set " + node.x + " " + node.y + " to " + role + " | " + j); } else if (cnt > 3) { if (j == cnt - 1) { role = 1; //SM } else if (j == 0) { role = GetLocalNodeType(2, 5, globalAddress.x + globalAddress.y, i, j); // GMARandom.Range3D(2, 5, coord.x + coord.y, i, j); } else { if (j % 2 == 0) { role = GetLocalNodeType(1, 5, globalAddress.x + globalAddress.y, i, j); // GMARandom.Range3D(1, 5, coord.x + coord.y, i, j); } else { role = GetLocalNodeType(2, 5, globalAddress.x + globalAddress.y, i, j); // GMARandom.Range3D(2, 5, coord.x + coord.y, i, j); } } Debug.Log("Set " + node.x + " " + node.y + " to " + role + " | " + j); } roles[node.x, node.y] = role;//TODO } if (group.Count > 1) { roles[group[cnt].x, group[cnt].y] = 0; //SW } } roles[(int)entryPoint.x, (int)entryPoint.y] = 7;//R for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (net[i, j] == 0) { continue; } if (roles[i, j] >= 0) { continue; } roles[i, j] = GMARandom.Range2D(0, 2, globalAddress.x + i, globalAddress.y + j);//SW or SM } } if (posibleAlarmNodes.Count > 0) { int n = GMARandom.Range2D(1, Mathf.Min(3, posibleAlarmNodes.Count + 1), globalAddress.x, globalAddress.y); for (int i = 0; i < n; i++) { int ind = GMARandom.Range3D(0, posibleAlarmNodes.Count, globalAddress.x, globalAddress.y, i); Vector2Int c = posibleAlarmNodes[ind]; if (roles[c.x, c.y] != 7) { roles[c.x, c.y] = 6; //AM } posibleAlarmNodes.RemoveAt(ind); } } }
public override void Init(int x, int y, int difficulty) { globalAddress = new Vector2Int(x, y); GlobalNodeInfo info = null; if (GameController.instance._globalNodesOverrides.ContainsKey(globalAddress)) { info = GameController.instance._globalNodesOverrides[globalAddress]; } if (info != null) { Init(info); return; } else { nodes = null; } width = 2 + GMARandom.Range3D(difficulty, (1 + difficulty) * 2, x, y, 1); height = 2 + GMARandom.Range3D(difficulty, (1 + difficulty) * 2, x, y, 2); this.difficulty = difficulty; //width = 2; //height = 1; Debug.Log("Init " + x + " " + y + " " + difficulty + " " + width + " " + height); net = new int[width, height]; links = new Link[width, height]; roles = new int[width, height]; haveEntryPoint = false; shiftx = GMARandom.Range2D(-1000000, 100000, x + y, (x + 1) * (y + 1)); shifty = GMARandom.Range2D(-1000000, 100000, x - y, x + y); //shiftx = -863620; //shifty = 52045; Debug.Log(x + " " + y + " " + shiftx + " " + shifty); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int nodeType = Node.GetType(i + shiftx, j + shifty); net[i, j] = nodeType; links[i, j] = GetLink(nodeType, new Vector2(i, j)); links[i, j].nodeType = nodeType; roles[i, j] = -1; } } for (int i = 0; i < width; i++) { net[i, 0] = Node.RemoveLink(net[i, 0], 2); links[i, 0].RemoveLink(2); //if ((net[i, 0] == 1 || net[i, 0] == 8 || net[i, 0] == 2) && !haveEntryPoint) //TODO entryPoint //{ // entryPoint.x = i; // entryPoint.y = 0; // haveEntryPoint = true; //} net[i, height - 1] = Node.RemoveLink(net[i, height - 1], 0); links[i, height - 1].RemoveLink(0); } for (int j = 0; j < height; j++) { net[0, j] = Node.RemoveLink(net[0, j], 3); links[0, j].RemoveLink(3); //if ((net[0, j] == 1 || net[0, j] == 2) && !haveEntryPoint) //{ // entryPoint.x = 0; // entryPoint.y = j; // haveEntryPoint = true; //} net[width - 1, j] = Node.RemoveLink(net[width - 1, j], 1); links[width - 1, j].RemoveLink(1); } FormNodesGroups(); ConnectNearestPoints(); NodesRedefinition(); GetEndPoints(); root = MakeTree(); FormEndPointsGroups2(); }