private MonsterTree graft(MonsterTree tree, out Dictionary <MonsterTreeNode, int> parent1Map, out Dictionary <MonsterTreeNode, int> parent2Map) { //Select Node and where to insert in this node from caller's tree int index = Random.Range(0, nodes.Count); MonsterTreeNode selectedNode = nodes[index]; int insertionPos; do { insertionPos = Random.Range(0, selectedNode.children.Length); }while(insertionPos == selectedNode.parent); //Select Node from param's tree int targetIndex = Random.Range(0, tree.nodes.Count); MonsterTreeNode targetNode = tree.nodes[targetIndex]; //Copy Caller's Tree and target's subtree MonsterTree res = this.clone(); parent1Map = new Dictionary <MonsterTreeNode, int>(); for (int i = 0; i < res.nodes.Count; ++i) { parent1Map.Add(res.nodes[i], i); } parent2Map = new Dictionary <MonsterTreeNode, int>(); Dictionary <MonsterTreeNode, int> tempMap = new Dictionary <MonsterTreeNode, int>(); for (int i = 0; i < tree.nodes.Count; ++i) { tempMap.Add(tree.nodes[i], i); } List <MonsterTreeNode> subtree = targetNode.GetSubTree(); List <MonsterTreeNode> newNodes = targetNode.CopySubTree(); for (int i = 0; i < subtree.Count; ++i) { if (tempMap.ContainsKey(subtree[i])) { parent2Map.Add(newNodes[i], tempMap[subtree[i]]); } } if (index < res.nodes.Count && index > 0 && insertionPos < res.nodes[index].children.Length && insertionPos > 0 && newNodes.Count > 0) { //Insert into Caller and add target's nodes to caller's list res.nodes[index].children[insertionPos] = newNodes[0]; res.nodes.AddRange(newNodes); } return(res); }
private void Randomize(int maxDepth) { root = new CubeTreeNode(-1); root.Randomize(maxDepth, maxDepth); nodes = root.GetSubTree(); }