//handles choosing which cells to turn to rooms private void ChooseLargeRooms() { foreach (GameObject sr in startingRoomsList) { sr.SetActive(false); if (alternativeMinCheck) { if (sr.transform.localScale.x > minHeight || sr.transform.localScale.y > minHeight) { sr.SetActive(true); DTNode tmpRoom = new DTNode(sr.transform.position.x, sr.transform.position.y, sr.gameObject); roomList.Add(tmpRoom); } } else { if (sr.transform.localScale.x > minHeight && sr.transform.localScale.y > minHeight) { sr.SetActive(true); DTNode tmpRoom = new DTNode(sr.transform.position.x, sr.transform.position.y, sr.gameObject); roomList.Add(tmpRoom); } } Destroy(sr.GetComponent <DTRoomRectangle>()); } }
void InitDecisions() { for (int i = 0; i < decisions_.trees_.Count; ++i) { DTNode.Init(decisions_.trees_[i].rootNode_); } }
// return root node of this sub tree public static DTNode LoadSubTree(XmlNode i_xmlNode) { XmlAttribute nameAttr = i_xmlNode.Attributes[DTNode.xmlNodeNameAtrName_]; string nodeName = nameAttr != null ? nameAttr.Value : ""; DTNode node = new DTNode(nodeName); for (int i = 0; i < i_xmlNode.ChildNodes.Count; ++i) { var curNode = i_xmlNode.ChildNodes[i]; if (curNode.Name.Equals(DTNode.xmlNodeName_)) { node.subNodes_.Add(LoadSubTree(curNode)); } else if (curNode.Name.Equals(BasicCondition.xmlNodeName)) { BasicCondition condition = BasicCondition.Deserialize(curNode as XmlElement); if (condition != null) { node.conditions_.Add(condition); } } else if (curNode.Name.Equals(BasicResult.xmlNodeName)) { BasicResult result = BasicResult.Deserialize(curNode as XmlElement); if (result != null) { node.results_.Add(result); } } } return(node); }
private void DrawRoom(DTNode r) { int iMin = (int)(r.getNodePosition().x - r.getParentRoom().transform.localScale.x / 2 + 0.5f); int iMax = (int)(r.getNodePosition().x + r.getParentRoom().transform.localScale.x / 2 + 0.5f); int jMin = (int)(r.getNodePosition().y - r.getParentRoom().transform.localScale.y / 2 + 0.5f); int jMax = (int)(r.getNodePosition().y + r.getParentRoom().transform.localScale.y / 2 + 0.5f); for (int i = iMin; i < iMax; i++) { for (int j = jMin; j < jMax; j++) { if (i == iMin || i == iMax - 1 || j == jMin || j == jMax - 1) { if (CheckForTag(new Vector2(i, j), 0.1f, "Floor")) { continue; } else { InstantiateFromArray(wallTiles, i, j); } } else { InstantiateFromArray(floorTiles, i, j); } } } }
// public Dictionary <string, DTNode> genealogy; public string Decide(GSR s) { string result = "None"; int next_node = 0; DTNode current_node = nodes[0]; int counter = 0; while (next_node != -1) { // Debug.Log("Current node: " + current_node.Name + " Counter " + counter.ToString()); current_node.Decide(s, out next_node, out result); // Debug.Log("Next node: " + next_node.ToString()); if (next_node == -1) { return(result); } else { current_node = nodes[next_node]; } counter += 1; } return(result); }
public void createTree() { //Depth = 0 //Root Node root = (DTNode)DTNode.CreateInstance("DTNode"); root.setUpDTNode("Safe", 1); //Depth = 1 //Yes it is safe DTNode people = (DTNode)DTNode.CreateInstance("DTNode"); people.setUpDTNode("People", 0.4f); //No it isn't safe DTNode faction = (DTNode)DTNode.CreateInstance("DTNode"); faction.setUpDTNode("Faction", 0.6f); root.add(people, faction); //Depth = 2 //It is safe -> Yes there are People DTLeaf camp = (DTLeaf)DTLeaf.CreateInstance("DTLeaf"); camp.setUpDTNode("Campers", 0.5f); camp.setPrefab(LandscapeGen.prefabs.friendlyCamps); //It is safe -> No there aren't People DTLeaf emptyHouse = (DTLeaf)DTLeaf.CreateInstance("DTLeaf"); emptyHouse.setUpDTNode("Empty Houses", 0.5f); emptyHouse.setPrefab(LandscapeGen.prefabs.emptyHouses); people.add(camp, emptyHouse); //Depth = 2 //It isn't safe -> Feral Faction DTLeaf feralGroups = (DTLeaf)DTLeaf.CreateInstance("DTLeaf"); feralGroups.setUpDTNode("Feral Groups", 0.4f); feralGroups.setPrefab(LandscapeGen.prefabs.feralCrowds); //It isn't safe -> Bandit Faction DTLeaf banditGroup = (DTLeaf)DTLeaf.CreateInstance("DTLeaf"); banditGroup.setUpDTNode("Bandit Faction", 0.4f); banditGroup.setPrefab(LandscapeGen.prefabs.hostileCamps); //It isn't safe -> Feral & Bandit Fight DTLeaf banditFeralFight = (DTLeaf)DTLeaf.CreateInstance("DTLeaf"); banditFeralFight.setUpDTNode("Bandit & Feral Fight", 0.2f); banditFeralFight.setPrefab(LandscapeGen.prefabs.fightCrowds); faction.add(feralGroups, banditGroup, banditFeralFight); }
public DTEdge(DTNode nodeA, DTNode nodeB) { this.nodeA = nodeA; this.nodeB = nodeB; theLine = new GameObject().AddComponent <LineRenderer>(); theLine.name = "Edge"; theLine.tag = "Line"; }
// not real deep copy, only copy DtVariables for editor public static DTNode DeepCopy(DTNode i_node) { DTNode retNode = new DTNode(i_node.nodeName_); for (int i = 0; i < i_node.conditions_.Count; ++i) { Type conditionType = i_node.conditions_[i].GetType(); BasicCondition tempCondition = Activator.CreateInstance(conditionType) as BasicCondition; FieldInfo[] conditionVariables = conditionType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); for (int vi = 0; vi < conditionVariables.Length; ++vi) { FieldInfo var = conditionVariables[vi]; if (Attribute.IsDefined(var, typeof(DtVariable))) { if (var.FieldType == typeof(string) || var.FieldType.IsArray || !var.FieldType.IsClass) { var.SetValue(tempCondition, var.GetValue(i_node.conditions_[i])); } else { System.Object value = Activator.CreateInstance(var.FieldType, var.GetValue(i_node.conditions_[i])); var.SetValue(tempCondition, value); } } } retNode.conditions_.Add(tempCondition); } for (int i = 0; i < i_node.results_.Count; ++i) { Type resultType = i_node.results_[i].GetType(); BasicResult tempResult = Activator.CreateInstance(resultType) as BasicResult; FieldInfo[] resultVariables = resultType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); for (int vi = 0; vi < resultVariables.Length; ++vi) { FieldInfo var = resultVariables[vi]; if (Attribute.IsDefined(var, typeof(DtVariable))) { if (var.FieldType == typeof(string) || var.FieldType.IsArray || !var.FieldType.IsClass) { var.SetValue(tempResult, var.GetValue(i_node.results_[i])); } else { System.Object value = Activator.CreateInstance(var.FieldType, var.GetValue(i_node.results_[i])); var.SetValue(tempResult, value); } } } retNode.results_.Add(tempResult); } for (int i = 0; i < i_node.subNodes_.Count; ++i) { retNode.subNodes_.Add(DeepCopy(i_node.subNodes_[i])); } return(retNode); }
private void CreateMinSpanningTree() { int randomIndex = pseudoRandom.Next(0, allNodes.Count); DTNode theNode = allNodes[randomIndex]; connectionNodes.Add(theNode); ConnectNextNode(); }
public bool containsNode(DTNode _aNode) { if (nodeA == _aNode || nodeB == _aNode) { return(true); } return(false); }
private void ConnectNextNode() { DTNode oldNode = null; DTNode closestNode = null; float closestDistance = 0; foreach (DTNode connectedNode in connectionNodes) { List <DTNode> nearbyNodes = (List <DTNode>)vertexTable[connectedNode]; foreach (DTNode nearbyNode in nearbyNodes) { if (!connectionNodes.Contains(nearbyNode)) { float tempDst = Vector2.Distance(nearbyNode.getParentRoom().transform.position, connectedNode.getParentRoom().transform.position); if (closestNode != null) { if (tempDst < closestDistance) { closestDistance = tempDst; closestNode = nearbyNode; oldNode = connectedNode; } } else { closestNode = nearbyNode; closestDistance = tempDst; oldNode = connectedNode; } } } } connectionNodes.Add(closestNode); foreach (DTEdge e in allEdges) { if (e.containsNode(oldNode) && e.containsNode(closestNode)) { e.setDrawColor(new Color(50, 255, 0, 255)); connectionEdges.Add(e); break; } } if (connectionNodes.Count == allNodes.Count) { return; } else { ConnectNextNode(); } }
public override DTAction MakeDecision() { DTNode node = testFunction() ? trueTree : falseTree; if (node != null) { return(node.MakeDecision()); } return(null); }
private void CreateTree() { DecisionTree my_dt = ScriptableObject.CreateInstance <DecisionTree>(); Dictionary <Node, DTNode> mapping = new Dictionary <Node, DTNode>(); Dictionary <Node, int> gen_mapping = new Dictionary <Node, int>(); List <DTNode> translated_nodes = new List <DTNode>(); int counter_node = 0; foreach (Node n in nodes) { DTNode current_node = new DTNode(); current_node.Name = n.Name; if (n.decision_type) { current_node.state_value_to_test = n.IndexToConsider; current_node.test_value = n.TestValue; current_node.decision_type = true; } else { current_node.actions = n.prob_actions; current_node.Normalize(); } mapping.Add(n, current_node); gen_mapping.Add(n, counter_node); counter_node += 1; } foreach (Node n in nodes) { if (n.decision_type) { mapping[n].true_node = gen_mapping[n.my_true_node]; mapping[n].false_node = gen_mapping[n.my_false_node]; } } foreach (DTNode d in mapping.Values) { translated_nodes.Add(d); } my_dt.nodes = translated_nodes; AssetDatabase.CreateAsset(my_dt, "Assets/ProbabilisticDecisionTree/DTFromCustom.asset"); AssetDatabase.SaveAssets(); EditorUtility.FocusProjectWindow(); Selection.activeObject = my_dt; }
//Find if this triangle contains a vert) public bool ContainsNode(DTNode node) { foreach (DTEdge e in edgeList) { if (e.getNodeA() == node || e.getNodeB() == node) { return(true); } } return(false); }
public static void Init(DTNode i_node) { for (int i = 0; i < i_node.conditions_.Count; ++i) { i_node.conditions_[i].Initialize(); } for (int i = 0; i < i_node.results_.Count; ++i) { i_node.results_[i].Initialize(); } for (int i = 0; i < i_node.subNodes_.Count; ++i) { Init(i_node.subNodes_[i]); } }
void LoadDecisions(XmlDocument xmldoc) { XmlNode rootNode = xmldoc.SelectSingleNode("DecisionTreeTable"); foreach (XmlNode layerNode in rootNode.ChildNodes) { XmlAttribute activeAttr = layerNode.Attributes["active"]; bool tempActive = activeAttr != null?bool.Parse(activeAttr.Value) : false; XmlAttribute updateIntervalAttr = layerNode.Attributes["UpdateInterval"]; uint tempUpdateInterval = updateIntervalAttr != null?uint.Parse(updateIntervalAttr.Value) : 1; for (int i = 0; i < layerNode.ChildNodes.Count; ++i) // normally, a layer only has one childNode { decisions_.trees_.Add(new DecisionTree(DTNode.LoadSubTree(layerNode.ChildNodes[i]), tempUpdateInterval, tempActive)); } } }
private void DrawConnection(DTEdge c) { DTNode room1 = c.getNodeA(); DTNode room2 = c.getNodeB(); Vector2 pos = new Vector2((float)Math.Round(room1.getNodePosition().x), (float)Math.Round(room1.getNodePosition().y)); float xDir = 0f, yDir = 0f; if (room1.getNodePosition().x <= room2.getNodePosition().x) { xDir = 1f; } else { xDir = -1f; } while (pos.x != (float)Math.Round(room2.getNodePosition().x)) { ConnectionFloor(pos.x, pos.y); ConnectionFloor(pos.x, pos.y + 1f); pos = new Vector2(pos.x + xDir, pos.y); } if (room1.getNodePosition().y <= room2.getNodePosition().y) { yDir = 1f; } else { yDir = -1f; } while (pos.y != (float)Math.Round(room2.getNodePosition().y)) { ConnectionFloor(pos.x, pos.y); ConnectionFloor(pos.x + 1f, pos.y); pos = new Vector2(pos.x, pos.y + yDir); } }
// every level, only one child will be select to enter or to execute (from the first to the last) private void DecideTree(DTNode node) { DTNode tempNode = node; while (true) { #if UNITY_EDITOR if (searchMarker_) { nodeNameLists_.Add(tempNode.nodeName_); } #endif for (int i = 0; i < tempNode.results_.Count; ++i) { OverrideFinalDecision(tempNode.results_[i]); } bool enterNextLevel = false; for (int i = 0; i < tempNode.subNodes_.Count; ++i) { bool passAllConditions = true; for (int j = 0; j < tempNode.subNodes_[i].conditions_.Count; j++) { BasicCondition tempCond = tempNode.subNodes_[i].conditions_[j]; if ((tempCond.IsConditionMet() && tempCond.nor) || (!tempCond.IsConditionMet() && !tempCond.nor)) { passAllConditions = false; break; } } if (passAllConditions) { tempNode = tempNode.subNodes_[i]; enterNextLevel = true; break; } } if (!enterNextLevel) { return; } } }
//Handles set up of triangulation public void SetupTriangulation(List <DTNode> roomList) { //puts all verticies into the toDo list foreach (DTNode n in roomList) { nodesToAddList.Add(n); } //creates three artificial verticies for the omega triangle DTNode nodeA = new DTNode(0, 250, null); DTNode nodeB = new DTNode(-250, -200, null); DTNode nodeC = new DTNode(250, -200, null); //creates the omega triangle bigTriangle = new DTTriangle(new DTEdge(nodeA, nodeB), new DTEdge(nodeA, nodeC), new DTEdge(nodeB, nodeC)); //adds the omega triangle to the triangle list triangleList.Add(bigTriangle); }
private void DrawNodes(int nodeIndex, Rect pos, int height) { if (nodeIndex == -1) { return; } DTNode dt = tree.nodes[nodeIndex]; Rect leftChildRect = new Rect(pos.x - (150.0f / (float)height), pos.y + 100, pos.width, pos.height); Rect rightChildRect = new Rect(pos.x + (150.0f / (float)height), pos.y + 100, pos.width, pos.height); if (dt.leftChildIndex != -1) { if (tree.nodes[dt.leftChildIndex].IsLeaf()) { if (GUI.Button(leftChildRect, "")) { Selection.activeGameObject = scene.bodies[tree.nodes[dt.leftChildIndex].bodyIndex].gameObject; } } GUI.Box(leftChildRect, tree.nodes[dt.leftChildIndex].IsLeaf() ? "L" : "N"); } if (dt.rightChildIndex != -1) { if (tree.nodes[dt.rightChildIndex].IsLeaf()) { if (GUI.Button(rightChildRect, "")) { Selection.activeGameObject = scene.bodies[tree.nodes[dt.rightChildIndex].bodyIndex].gameObject; } } GUI.Box(rightChildRect, tree.nodes[dt.rightChildIndex].IsLeaf() ? "R" : "N"); } if (!dt.IsLeaf()) { DrawNodes(dt.leftChildIndex, leftChildRect, height + 1); DrawNodes(dt.rightChildIndex, rightChildRect, height + 1); } }
public static XmlElement SaveSubTree(DTNode i_node, XmlDocument i_ownerDoc) { XmlElement elem = i_ownerDoc.CreateElement(DTNode.xmlNodeName_); elem.SetAttribute(DTNode.xmlNodeNameAtrName_, i_node.nodeName_); for (int i = 0; i < i_node.conditions_.Count; ++i) { XmlElement conditionElem = i_ownerDoc.CreateElement(BasicCondition.xmlNodeName); BasicCondition.Serialize(i_node.conditions_[i], ref conditionElem); elem.AppendChild(conditionElem); } for (int i = 0; i < i_node.results_.Count; ++i) { XmlElement resultElem = i_ownerDoc.CreateElement(BasicResult.xmlNodeName); BasicResult.Serialize(i_node.results_[i], ref resultElem); elem.AppendChild(resultElem); } for (int i = 0; i < i_node.subNodes_.Count; ++i) { elem.AppendChild(SaveSubTree(i_node.subNodes_[i], i_ownerDoc)); } return(elem); }
//Adds a verticies to the triangulation private void AddVertexToTriangulation() { //Find a Random verticie from the todo list int choice = pseudoRandom.Next(0, nodesToAddList.Count); //set next node to selected verticies nextNode = nodesToAddList[choice]; //remove selected verticies from todo list nodesToAddList.Remove(nextNode); //stores triangles created during the loop to be appended to main list after loop List <DTTriangle> tempTriList = new List <DTTriangle>(); //All edges are clean at this point. Remove any that may be left over from previous loop edgesToCheck.Clear(); float count = -1; foreach (DTTriangle aTri in triangleList) { List <DTEdge> triEdges = aTri.GetEdges(); count++; //Find which triangle the current vertex being add is located within if (LineIntersection.PointInTraingle(nextNode.getNodePosition(), triEdges[0].getNodeA().getNodePosition(), triEdges[0].getNodeB().getNodePosition(), triEdges[1].getNodeB().getNodePosition())) { //cache the triangle we are in so we can delete it after loop inTriangle = aTri; //create three new triangles from each edge of the triangle vertex is in to the new vertex foreach (DTEdge aEdge in aTri.GetEdges()) { DTTriangle nTri1 = new DTTriangle(new DTEdge(nextNode, aEdge.getNodeA()), new DTEdge(nextNode, aEdge.getNodeB()), new DTEdge(aEdge.getNodeB(), aEdge.getNodeA())); //cache created triangles so we can add to list after loop tempTriList.Add(nTri1); //mark the edges of the old triangle as dirty edgesToCheck.Add(new DTEdge(aEdge.getNodeA(), aEdge.getNodeB())); } break; } } //add the three new triangles to the triangle list foreach (DTTriangle aTri in tempTriList) { triangleList.Add(aTri); } //delete the old triangle that the vertex was inside of if (inTriangle != null) { triangleList.Remove(inTriangle); inTriangle.StopDraw(); inTriangle = null; } CheckEdges(edgesToCheck); }
private void CheckEdges(List <DTEdge> edgesList) { //the current dirty edge if (edgesList.Count == 0) { if (nodesToAddList.Count > 0) { AddVertexToTriangulation(); } return; } //get the next edge in the dirty list DTEdge currentEdge = edgesList[0]; DTTriangle[] connectedTris = new DTTriangle[2]; int index = 0; foreach (DTTriangle aTri in triangleList) { if (aTri.ContainsEdge(currentEdge)) { connectedTris[index] = aTri; index++; } } //in first case (omega triangle) this will = 1 so dont flip if (index == 2) { //stores the two verticies from both triangles that arnt on the shared edge DTNode[] uniqueNodes = new DTNode[2]; int index1 = 0; //loop through the connected triangles and there edges. Checking for a vertex that isnt in the edge for (int i = 0; i < connectedTris.Length; i++) { foreach (DTEdge aEdge in connectedTris[i].GetEdges()) { if (!currentEdge.containsNode(aEdge.getNodeA())) { uniqueNodes[index1] = aEdge.getNodeA(); index1++; break; } if (!currentEdge.containsNode(aEdge.getNodeB())) { uniqueNodes[index1] = aEdge.getNodeB(); index1++; break; } } } //find the angles of the two unique verticies float angle0 = CalculateVertexAngle(uniqueNodes[0].getNodePosition(), currentEdge.getNodeA().getNodePosition(), currentEdge.getNodeB().getNodePosition()); float angle1 = CalculateVertexAngle(uniqueNodes[1].getNodePosition(), currentEdge.getNodeA().getNodePosition(), currentEdge.getNodeB().getNodePosition()); //Check if the target Edge needs flipping if (angle0 + angle1 > 180) { //create the new edge after flipped DTEdge flippedEdge = new DTEdge(uniqueNodes[0], uniqueNodes[1]); //store the edges of both triangles in the Quad DTEdge[] firstTriEdges = new DTEdge[3]; DTEdge[] secondTriEdges = new DTEdge[3]; DTNode sharedNode0; DTNode sharedNode1; //set the shared nodes on the shared edge sharedNode0 = currentEdge.getNodeA(); sharedNode1 = currentEdge.getNodeB(); //construct a new triangle to update old triangle after flip firstTriEdges[0] = new DTEdge(uniqueNodes[0], sharedNode0); firstTriEdges[1] = new DTEdge(sharedNode0, uniqueNodes[1]); firstTriEdges[2] = flippedEdge; //construct a new triangle to update the other old triangle after flip secondTriEdges[0] = new DTEdge(uniqueNodes[1], sharedNode1); secondTriEdges[1] = new DTEdge(sharedNode1, uniqueNodes[0]); secondTriEdges[2] = flippedEdge; //update the edges of the triangles involved in the flip connectedTris[0].SetEdges(firstTriEdges[0], firstTriEdges[1], firstTriEdges[2]); connectedTris[1].SetEdges(secondTriEdges[0], secondTriEdges[1], secondTriEdges[2]); //Adds all edges to be potentially dirty. This is bad and should only add the edges that *could* be dirty foreach (DTEdge eEdge in connectedTris[0].GetEdges()) { edgesList.Add(eEdge); } foreach (DTEdge eEdge in connectedTris[1].GetEdges()) { edgesList.Add(eEdge); } //also add new edge to dirty list edgesList.Add(flippedEdge); } } //remove the current edge from the dirty list edgesList.Remove(currentEdge); CheckEdges(edgesList); }
public DTCondiccao(Func <bool> condition, DTNode tChild, DTNode fChild) { this.condition = condition; trueChild = tChild; falseChild = fChild; }
public DTBinaryDecision(Func <bool> testFunction, DTNode trueTree, DTNode falseTree) { this.testFunction = testFunction; this.trueTree = trueTree; this.falseTree = falseTree; }
public DecisionTree(DTNode i_rootNode, uint i_updateInterval, bool i_active) { rootNode_ = i_rootNode; updateInterval_ = i_updateInterval; active_ = i_active; }