private void PlaceBuildings() { Vector3 position = new Vector3(); List <TreeNode> planList = new List <TreeNode>(); foreach (WorldState goal in goals) { goalIndex++; planner.SetGoalWorldState(goal); plan = planner.RunAStar((WorldState)blackBoard.GetFact(clan, "currentWorldState")[0].GetFactValue()); Debug.Log("STARTPLAN!!!!!!!!: " + plan.Count); foreach (AStarNode node in plan) { Debug.Log("-----l node.getName() " + node.GetName()); //Debug.Log("-----" + node.getName()); } //Debug.Log ("Plan count: " + plan.Count); //Add the wireframe for each house!!! for (int j = plan.Count - 1; j > -1; j--) { //Debug.Log ("PlanStep " + plan[j].getName() ); if (plan[j].GetName().Substring(0, 1) == ("B")) { //Debug.Log ("***********BUILD**********"); position = new Vector3(Random.Range(gameObject.transform.position.x - 10.0f, gameObject.transform.position.x + 10.0f), 0.5f, Random.Range(gameObject.transform.position.z - 10.0f, gameObject.transform.position.z + 10.0f)); List <WorkingMemoryValue> buildings = BlackBoard.Instance.GetFact(clan, "Buildings"); if (buildings.Count > 0) { //TODO borde kontrollera fler saker än bara andra hus //Debug.Log ("måste kolla positionen på husen"); position = CheckPosition(position, buildings); } GameObject house = GameObject.CreatePrimitive(PrimitiveType.Cube); Destroy(house.GetComponent <BoxCollider>()); house.tag = "wireframe"; house.AddComponent("wireframe"); house.transform.position = position; house.transform.parent = buildingGroup.transform; string actionName = plan[j].GetName(); string color = ""; string type = actionName.Substring(5, actionName.Length - 6); Dictionary <string, WorkingMemoryValue> houseInfo = new Dictionary <string, WorkingMemoryValue>(); houseInfo.Add("Type", new WorkingMemoryValue(type)); houseInfo.Add("Position", new WorkingMemoryValue(house.transform.position)); houseInfo.Add("Length", new WorkingMemoryValue(1.0f)); //houseInfo.Add ("Width", new WorkingMemoryValue(1.0f)); houseInfo.Add("Health", new WorkingMemoryValue(300.0f)); houseInfo.Add("Floors", new WorkingMemoryValue(1)); BlackBoard.Instance.SetFact(clan, "Buildings", new WorkingMemoryValue(houseInfo)); //Debug.Log ("actionName: " + actionName); int firstUpper = -1; for (int i = 1; i < actionName.Length; i++) { if (char.IsUpper(actionName[i])) { if (firstUpper == -1) { firstUpper = i; } else { color = actionName.Substring(firstUpper, i - firstUpper); break; } } } //color = char.ToUpper(color[0]) + color.Substring(1); //Debug.Log ("color: " + color); //Debug.Log (color); house.name = color + " wireframe"; wireframe aa = (wireframe)house.GetComponent("wireframe"); aa.lineColor = BlackBoard.Instance.GetColorForObject(color); } if (j == plan.Count - 1) { planList.Add(new TreeNode(position, plan[j].GetName(), null, goalIndex)); //Debug.Log ("***************** " + plan[j].getName() + " with position: " + position); } else { for (int k = 0; k < planList.Count; k++) { if (planList[k].GetActionName() == plan[j].GetParent().GetName()) { if (plan[j].GetName().Substring(0, 1) == ("B")) { //Debug.Log ("***************** " + plan[j].getName() + " with position: " + position); planList.Add(new TreeNode(position, plan[j].GetName(), planList[k], goalIndex)); } else { planList.Add(new TreeNode(planList[k].GetPosition(), plan[j].GetName(), planList[k], goalIndex)); } break; } } } //blackBoard.AddToTaskTree(plan[j], position, goalIndex); } blackBoard.AddToTaskTree(clan, planList); } }
private void PlaceBuildings() { Vector3 position = new Vector3(); foreach (WorldState goal in goals) //goes thru all the goals { goalIndex++; planner.SetGoalWorldState(goal); plan = planner.RunAStar((WorldState)BlackBoard.Instance.GetFact(clan, "currentWorldState")[0].GetFactValue()); Debug.Log("***************STARTPLAN!!!!!!!!: " + plan.Count); foreach (AStarNode node in plan) { Debug.Log("-----l " + node.GetName()); } Debug.Log("***************"); ConvertNodes(plan[plan.Count - 1], null); for (int j = 0; j < planList.Count; j++) { if (planList[j].GetActionName().Substring(0, 1) == ("B")) //its a build action so need new position for house { position = new Vector3(Random.Range(gameObject.transform.position.x - 10.0f, gameObject.transform.position.x + 10.0f), 0.5f, Random.Range(gameObject.transform.position.z - 10.0f, gameObject.transform.position.z + 10.0f)); //new random position of building List <WorkingMemoryValue> buildings = BlackBoard.Instance.GetFact(clan, "Buildings"); //get all the buildings of the clan position = CheckPosition(position, buildings); //check if position is available if not a new will be given string actionName = planList[j].GetActionName(); string colorAndType = actionName.Substring(5, actionName.Length - 11); GameObject house = (GameObject)Instantiate(Resources.Load(("Prefabs/Building"), typeof(GameObject))); //Add a wireframe for each house Destroy(house.GetComponent <BoxCollider>()); house.tag = "wireframe"; house.gameObject.AddComponent("wireframe"); house.transform.parent = buildingGroup.transform; house.transform.position = position; //add information about the house to the blackboard Dictionary <string, WorkingMemoryValue> houseInfo = new Dictionary <string, WorkingMemoryValue>(); houseInfo.Add("Type", new WorkingMemoryValue(colorAndType)); houseInfo.Add("Position", new WorkingMemoryValue(position)); houseInfo.Add("Length", new WorkingMemoryValue(1.0f)); houseInfo.Add("Width", new WorkingMemoryValue(1.0f)); houseInfo.Add("Health", new WorkingMemoryValue(300.0f)); houseInfo.Add("Floors", new WorkingMemoryValue(1)); BlackBoard.Instance.SetFact(clan, "Buildings", new WorkingMemoryValue(houseInfo)); string color = ""; //get the color of the building int firstUpper = -1; for (int i = 1; i < actionName.Length; i++) { if (char.IsUpper(actionName[i])) { if (firstUpper == -1) { firstUpper = i; } else { color = actionName.Substring(firstUpper, i - firstUpper); break; } } } house.name = color + " wireframe"; wireframe aa = (wireframe)house.GetComponent("wireframe"); aa.lineColor = BlackBoard.Instance.GetColorForObject(color); planList[j].SetPosition(position); } else //its a get action so need position of the parent node (position of the building) { planList[j].SetPosition(planList[j].GetParent().GetPosition()); } } BlackBoard.Instance.AddToTaskTree(clan, planList); //set up the new plan in the tasktree planList.Clear(); } }