public void LoadActionPlanStep(string actionName, OCAction.OCActionArgs arguments) { Debug.Log("OCActionController::LoadActionPlanStep: " + actionName); TreeType treeType = _ActionNameDictionary[actionName]; Tree tree = _TreeTypeDictionary[treeType]; OCActionPlanStep actionPlanStep = OCScriptableObject.CreateInstance <OCActionPlanStep>(); actionPlanStep.Behaviour = tree; actionPlanStep.Arguments = arguments; _ActionPlanQueue.AddLast(actionPlanStep); Debug.Log("Enqueued Action Step: " + actionPlanStep.Arguments.ActionName); }
public void LoadActionPlanStep(string actionName, OCAction.OCActionArgs arguments) { //Debug.Log("OCActionController::LoadActionPlanStep: " + actionName); TreeType treeType = _ActionNameDictionary[actionName]; Tree tree = _TreeTypeDictionary[treeType]; OCActionPlanStep actionPlanStep = OCScriptableObject.CreateInstance <OCActionPlanStep>(); actionPlanStep.Behaviour = tree; actionPlanStep.Arguments = arguments; _ActionPlanQueue.AddLast(actionPlanStep); System.Console.WriteLine(OCLogSymbol.DETAILEDINFO + "Enqueued Action Step: " + actionPlanStep.Arguments.ActionName); }
public void LoadLevel() { int verticalOffset = 85; Debug.Log("About to load level folder: " + _fullMapPath + "."); Substrate.AnvilWorld mcWorld = Substrate.AnvilWorld.Create(_fullMapPath); Substrate.AnvilRegionManager mcAnvilRegionManager = mcWorld.GetRegionManager(); OpenCog.BlockSet.OCBlockSet blockSet = _map.GetBlockSet(); //_map.GetSunLightmap().SetSunHeight(20, 4, 4); int createCount = 0; System.Collections.Generic.Dictionary <int, int> unmappedBlockTypes = new System.Collections.Generic.Dictionary <int, int>(); //Debug.Log("In LoadLevel, there are " + blockSet.BlockCount + " blocks available."); foreach (Substrate.AnvilRegion mcAnvilRegion in mcAnvilRegionManager) { // Loop through x-axis of chunks in this region for (int iMCChunkX = 0; iMCChunkX < mcAnvilRegion.XDim; iMCChunkX++) { // Loop through z-axis of chunks in this region. for (int iMCChunkZ = 0; iMCChunkZ < mcAnvilRegion.ZDim; iMCChunkZ++) { // Retrieve the chunk at the current position in our 2D loop... Substrate.ChunkRef mcChunkRef = mcAnvilRegion.GetChunkRef(iMCChunkX, iMCChunkZ); if (mcChunkRef != null) { if (mcChunkRef.IsTerrainPopulated) { // Ok...now to stick the blocks in... int iMCChunkY = 0; OCChunk chunk = null; //new OCChunk(_map, new Vector3i(iMCChunkX, iMCChunkY, iMCChunkZ)); OCChunk lastChunk = null; Vector3i chunkPos = new Vector3i(mcAnvilRegion.ChunkGlobalX(iMCChunkX), iMCChunkY + verticalOffset, mcAnvilRegion.ChunkGlobalZ(iMCChunkZ)); Vector3i lastChunkPos = Vector3i.zero; chunk = _map.GetChunkInstance(chunkPos); for (int iMCChunkInternalY = 0; iMCChunkInternalY < mcChunkRef.Blocks.YDim; iMCChunkInternalY++) { if (iMCChunkInternalY / OCChunk.SIZE_Y > iMCChunkY) { lastChunk = chunk; lastChunkPos = chunkPos; chunkPos = new Vector3i(mcAnvilRegion.ChunkGlobalX(iMCChunkX), (iMCChunkInternalY + verticalOffset) / OCChunk.SIZE_Y, mcAnvilRegion.ChunkGlobalZ(iMCChunkZ)); chunk = _map.GetChunkInstance(chunkPos); } for (int iMCChunkInternalX = 0; iMCChunkInternalX < mcChunkRef.Blocks.XDim; iMCChunkInternalX++) { for (int iMCChunkInternalZ = 0; iMCChunkInternalZ < mcChunkRef.Blocks.ZDim; iMCChunkInternalZ++) { int iBlockID = mcChunkRef.Blocks.GetID(iMCChunkInternalX, iMCChunkInternalY, iMCChunkInternalZ); if (iBlockID != 0) { Vector3i blockPos = new Vector3i(iMCChunkInternalX, iMCChunkInternalY % OCChunk.SIZE_Y, iMCChunkInternalZ); int ourBlockID = -1; // switch (iBlockID) // { // case 3: // Dirt to first grass // ourBlockID = 1; // break; // case 12: // Grass to grass // ourBlockID = 1; // break; // case 13: // Gravel to stone // ourBlockID = 4; // break; // case 1: // Stone to second stone // ourBlockID = 5; // break; // case 16: // Coal ore to fungus // ourBlockID = 17; // break; // case 15: // Iron ore to pumpkin // ourBlockID = 20; // break; // case 9: // Water to water // ourBlockID = 8; // //Debug.Log ("Creating some water at [" + blockPos.x + ", " + blockPos.y + ", " + blockPos.z + "]"); // break; //// case 2: //// iBlockID = 16; //// break; //// case 4: //// iBlockID = 16; //// break; //// case 18: //// iBlockID = 16; //// break; // default: // { // //Debug.Log ("Unmapped BlockID: " + iBlockID); // // if (!unmappedBlockTypes.ContainsKey (iBlockID)) // { // unmappedBlockTypes.Add (iBlockID, 1); // } // else // { // unmappedBlockTypes[iBlockID] += 1; // } // // break; // } // } if (mcToOCBlockDictionary.ContainsKey(iBlockID)) { ourBlockID = mcToOCBlockDictionary[iBlockID]; } else { if (!unmappedBlockTypes.ContainsKey(iBlockID)) { unmappedBlockTypes.Add(iBlockID, 1); } else { unmappedBlockTypes[iBlockID] += 1; } } if (ourBlockID != -1) { OCBlock newBlock = blockSet.GetBlock(ourBlockID); //OCBlockData block = new OpenCog.Map.OCBlockData(newBlock, blockPos); OCBlockData block = (OCBlockData)OCScriptableObject.CreateInstance <OCBlockData>(); block.Init(newBlock, blockPos); chunk.SetBlock(block, blockPos); OpenCog.Map.Lighting.OCLightComputer.RecomputeLightAtPosition(_map, blockPos); if (block.block.GetName() == "Battery") { GameObject batteryPrefab = OCMap.Instance.BatteryPrefab; if (batteryPrefab == null) { UnityEngine.Debug.Log("OCBuilder::Update, batteryPrefab == null"); } else { GameObject battery = (GameObject)GameObject.Instantiate(batteryPrefab); battery.transform.position = blockPos; battery.name = "Battery"; battery.transform.parent = OCMap.Instance.BatteriesSceneObject.transform; } } if (block.block.GetName() == "Hearth") { GameObject hearthPrefab = OCMap.Instance.HearthPrefab; if (hearthPrefab == null) { UnityEngine.Debug.Log("OCBuilder::Update, hearthPrefab == null"); } else { GameObject hearth = (GameObject)GameObject.Instantiate(hearthPrefab); hearth.transform.position = blockPos; hearth.name = "Hearth"; hearth.transform.parent = OCMap.Instance.HearthsSceneObject.transform; } } createCount += 1; } } } // End for (int iMCChunkInternalZ = 0; iMCChunkInternalZ < mcChunkRef.Blocks.ZDim; iMCChunkInternalZ++) } // End for (int iMCChunkInternalY = 0; iMCChunkInternalY < mcChunkRef.Blocks.YDim; iMCChunkInternalY++) string chunkCoord = chunkPos.x + ", " + chunkPos.z; if (!chunkList.ContainsKey(chunkCoord)) { chunkList.Add(chunkCoord, chunkPos); } if (iMCChunkY < iMCChunkInternalY / OCChunk.SIZE_Y) { _map.Chunks.AddOrReplace(lastChunk, lastChunkPos); _map.UpdateChunkLimits(lastChunkPos); _map.SetDirty(lastChunkPos); iMCChunkY = iMCChunkInternalY / OCChunk.SIZE_Y; } } // End for (int iMCChunkInternalX = 0; iMCChunkInternalX < mcChunkRef.Blocks.XDim; iMCChunkInternalX++) } // End if (mcChunkRef.IsTerrainPopulated) } // End if (mcChunkRef != null) } // End for (int iMCChunkZ = 0; iMCChunkZ < mcAnvilRegion.ZDim; iMCChunkZ++) } // End for (int iMCChunkX = 0; iMCChunkX < mcAnvilRegion.XDim; iMCChunkX++) } // End foreach( Substrate.AnvilRegion mcAnvilRegion in mcAnvilRegionManager ) foreach (Vector3i chunkToLight in chunkList.Values) { OpenCog.Map.Lighting.OCChunkSunLightComputer.ComputeRays(_map, chunkToLight.x, chunkToLight.z); OpenCog.Map.Lighting.OCChunkSunLightComputer.Scatter(_map, null, chunkToLight.x, chunkToLight.z); } foreach (System.Collections.Generic.KeyValuePair <int, int> unmappedBlockData in unmappedBlockTypes) { UnityEngine.Debug.Log("Unmapped BlockID '" + unmappedBlockData.Key + "' found " + unmappedBlockData.Value + " times."); } Debug.Log("Loaded level: " + _fullMapPath + ", created " + createCount + " blocks."); _map.AddColliders(); } // End public void LoadLevel()
public void UpdateAI() { _ActionPlanList = _ActionPlanQueue.ToList(); if (_step == null && _ActionPlanQueue.Count != 0) { _step = _ActionPlanQueue.First(); _ActionPlanQueue.RemoveFirst(); Debug.LogWarning("In OCActionController.UpdateAI, starting action step: " + _step.Arguments.ActionName + ", retry: " + _step.Retry); } else if (_step == null && _ActionPlanQueue.Count == 0) { _PlanSucceeded = true; OCActionPlanStep step = OCScriptableObject.CreateInstance <OCActionPlanStep>(); step.Behaviour = _TreeTypeDictionary[_TreeType]; step.Arguments = new OCAction.OCActionArgs(_defaultSource, _defaultStartTarget, _defaultEndTarget); KeyValuePair <string, TreeType> keyValuePair = _ActionNameDictionary.First(s => s.Value == _TreeType); step.Arguments.ActionName = keyValuePair.Key; _step = step; } BehaveResult result = _step.Behaviour.Tick(); // if((_step.Behaviour.Name == _TreeTypeDictionary[TreeType.Character_IdleShow].Name) && result == BehaveResult.Success) // { // //iTween itween = _step.Arguments.Source.GetComponent<iTween>(); // iTween.Stop(_step.Arguments.Source); // } if (result != BehaveResult.Running) { OCAction.OCActionArgs args = _step.Arguments; // if((_step.Behaviour.Name != _TreeTypeDictionary[_TreeType].Name) // || ((_step.Behaviour.Name == _TreeTypeDictionary[_TreeType].Name) // && (GameObject.Find("EndPointStub").transform.position != Vector3.zero))) { // if we have a goal... if (_step.Arguments.EndTarget != null) { if (_step.Arguments.EndTarget.transform.position != Vector3.zero) { _PlanSucceeded &= result == BehaveResult.Success; } Vector3 startPosition = _step.Arguments.StartTarget.transform.position; Vector3 endPosition = _step.Arguments.EndTarget.transform.position; Vector3 sourcePosition = _step.Arguments.Source.transform.position; sourcePosition.y = sourcePosition.y - 0.5f; Vector3 startToEnd = endPosition - startPosition; Vector3 sourceToEnd = endPosition - sourcePosition; float startToEndManDist = Math.Abs(endPosition.x - startPosition.x) + Math.Abs(endPosition.y - startPosition.y) + Math.Abs(endPosition.z - startPosition.z); float sourceToEndManDist = Math.Abs(endPosition.x - sourcePosition.x) + Math.Abs(endPosition.y - sourcePosition.y) + Math.Abs(endPosition.z - sourcePosition.z); if (_step.Behaviour.Name == "Character.Move" || _step.Arguments.ActionName == "walk" || _step.Arguments.ActionName == "jump_toward") { // don't use euclideon distance //_PlanSucceeded |= sourceToEnd.sqrMagnitude < startToEnd.sqrMagnitude; // use manhattan distance //_PlanSucceeded = sourceToEndManDist <= startToEndManDist; if (VectorUtil.AreVectorsEqual(sourcePosition, endPosition)) { _PlanSucceeded = true; } else { _PlanSucceeded = false; } } if (_step.Behaviour.Name == "Character.Destroy" || _step.Arguments.ActionName == "eat") { _PlanSucceeded = (endPosition == Vector3.zero || _step.Arguments.EndTarget == null); } if (_step.Arguments.ActionName == "grab") { _PlanSucceeded = OCAction.IsEndTargetCloseForward(null, _step.Arguments); } // if(_step.Arguments.ActionName == "grab") // { // _PlanSucceeded = endPosition != startPosition && endPosition != null; // } if (_step.Arguments.ActionPlanID != null && (_PlanSucceeded || _step.Retry > OCActionPlanStep.MaxRetries)) { OCConnectorSingleton.Instance.SendActionStatus(args.ActionPlanID, args.SequenceID, args.ActionName, _PlanSucceeded); if (_step.Behaviour.Name != "Character.IdleShow" && !_step.Behaviour.Name.Contains("Behaviour")) { Debug.LogWarning("In OCActionController.UpdateAI, Result: " + (_PlanSucceeded ? "Success" : "Failure") + " for Action: " + (_step.Arguments.ActionName == null ? _step.Behaviour.Name : (_step.Arguments.ActionName + " & Sequence: " + _step.Arguments.SequenceID))); } } else if (_step.Arguments.ActionPlanID == null && (_PlanSucceeded || _step.Retry > OCActionPlanStep.MaxRetries) && OCConnectorSingleton.Instance.IsEstablished) { OCConnectorSingleton.Instance.HandleOtherAgentActionResult(_step, _PlanSucceeded); } } // if(!_PlanSucceeded) // Debug.LogWarning(" -- Step Failed: " + (_step.Arguments.ActionName == null ? _step.Behaviour.Name : _step.Arguments.ActionName)); _step.Behaviour.Reset(); // if we failed, retry last step if (_PlanSucceeded == false && OCActionPlanStep.MaxRetries > _step.Retry) { _ActionPlanQueue.AddFirst(_step); _step.Retry += 1; } else if (_PlanSucceeded == false && OCActionPlanStep.MaxRetries <= _step.Retry) { _ActionPlanQueue.Clear(); _step = null; } else if (_step.Arguments.EndTarget) { OCFadeOutGameObject fadeOut = _step.Arguments.EndTarget.GetComponent <OCFadeOutGameObject>(); if (fadeOut != null) { fadeOut.enabled = true; } } if (_ActionPlanQueue.Count == 0) { if (_LastPlanID != null) { // if(result == BehaveResult.Failure) // OCConnectorSingleton.Instance.SendActionStatus(args.ActionPlanID, args.SequenceID, args.ActionName, true); OCConnectorSingleton.Instance.SendActionPlanStatus(_LastPlanID, _PlanSucceeded); if (_step != null && _step.Arguments.EndTarget != null) { OCFadeOutGameObject fadeOut = _step.Arguments.EndTarget.GetComponent <OCFadeOutGameObject>(); if (fadeOut != null) { fadeOut.enabled = true; } } _LastPlanID = null; } _step = null; } else if (_LastPlanID != null) { _step = _ActionPlanQueue.First(); _ActionPlanQueue.RemoveFirst(); Debug.LogWarning("In OCActionController.UpdateAI, starting action step: " + _step.Arguments.ActionName + ", retry: " + _step.Retry); if (_LastPlanID != _step.Arguments.ActionPlanID) { Debug.LogError("We've changed plans without reporting back to OpenCog!"); } } else { _LastPlanID = _step.Arguments.ActionPlanID; _step = _ActionPlanQueue.First(); _ActionPlanQueue.RemoveFirst(); Debug.LogWarning("In OCActionController.UpdateAI, starting action step: " + _step.Arguments.ActionName + ", retry: " + _step.Retry); } } } }
//--------------------------------------------------------------------------- #endregion //--------------------------------------------------------------------------- #region Public Member Functions //--------------------------------------------------------------------------- public IEnumerator Start() { _TreeTypeDictionary = new Dictionary <TreeType, Tree>(); _ActionPlanQueue = new LinkedList <OCActionPlanStep>(); foreach (TreeType type in Enum.GetValues(typeof(TreeType)).Cast <TreeType>()) { _TreeTypeDictionary.Add(type, BLOCBehaviours.InstantiateTree(type, this)); } OCAction[] actions = gameObject.GetComponentsInChildren <OCAction>(true); foreach (Tree tree in _TreeTypeDictionary.Values) { if (tree == null) { continue; } int index = tree.Name.LastIndexOf(".") + 1; if (index < 0 || index > tree.Name.Count()) { index = 0; } string treeName = tree.Name.Substring(index); foreach (OCAction action in actions) { if (((action.FullName.Contains(treeName) || treeName.Contains("Behaviour")) && !(treeName == "GhostBehaviour" && action.name.Contains("Create")) && !(treeName == "GhostBehaviour" && action.name.Contains("Destroy"))) || (treeName == "TurnAndCreate" && action.FullName == "HoldRightHandCreate") || (treeName == "TurnAndCreate" && action.FullName == "HoldRightHandCreateBelow") || (treeName == "TurnAndCreate" && action.FullName == "StandTurnLeftMove") || (treeName == "TurnAndCreate" && action.FullName == "StandTurnRightMove") || (treeName == "TurnAndCreate" && action.FullName == "StandIdleShow") || (treeName == "TurnAndCreate" && action.FullName == "FallIdleShow") || (treeName == "TurnLeftOrRight" && action.FullName == "StandTurnLeftMove") || (treeName == "TurnLeftOrRight" && action.FullName == "StandTurnRightMove") || (treeName == "TurnLeftOrRight" && action.FullName == "StandIdleShow") || (treeName == "TurnLeftOrRight" && action.FullName == "FallIdleShow") || (treeName == "BothHandsTransfer" && action.FullName == "HoldBothHandsTransfer") ) { int actionTypeID = (int)Enum.Parse(typeof(BLOCBehaviours.ActionType), action.FullName); tree.SetTickForward(actionTypeID, action.ExecuteBehave); } } } OCActionPlanStep firstStep = OCScriptableObject.CreateInstance <OCActionPlanStep>(); firstStep.Behaviour = _TreeTypeDictionary[_TreeType]; firstStep.Arguments = new OCAction.OCActionArgs(_defaultSource, _defaultStartTarget, _defaultEndTarget); KeyValuePair <string, TreeType> keyValuePair = _ActionNameDictionary.First(s => s.Value == _TreeType); firstStep.Arguments.ActionName = keyValuePair.Key; _ActionPlanQueue.AddLast(firstStep); RunningActions = new List <string>(); //RunningActions.Add("StandIdleShow"); // vvvv Testing Nil's action for the SantaFe problem vvvv ///////////////////////////////////////////////////////////////////////////////// // XmlDocument doc = new XmlDocument(); // doc.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n<oc:action-plan xmlns:oc=\"http://www.opencog.org/brain\" demand=\"\" entity-id=\"OAC_AGI_Robot\" id=\"0\">\n<action name=\"step_forward\" sequence=\"1\"/>\n</oc:action-plan>"); // // XmlNodeList list = doc.GetElementsByTagName(OCEmbodimentXMLTags.ACTION_PLAN_ELEMENT); // XmlElement root = (XmlElement)list.Item(0);//MakeXMLElementRoot(doc); // // CharacterController charControl = gameObject.GetComponent<CharacterController>(); // bool tryParse = true; ///////////////////////////////////////////////////////////////////////////////// while (Application.isPlaying) { yield return(new WaitForSeconds(1.0f / 100.0f)); UpdateAI(); // if(tryParse && charControl.isGrounded) // { // OCConnectorSingleton.Instance.ParseActionPlanElement(root); // tryParse = false; // } } }