//---------------------------------------------------------------------------

        #region Private Member Data

        //---------------------------------------------------------------------------



        //---------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------

        #region Accessors and Mutators

        //---------------------------------------------------------------------------



        //---------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------

        #region Public Member Functions

        //---------------------------------------------------------------------------

        public void TransferBlock(Vector3i?origin, Vector3i?destination)
        {
            if (origin.HasValue && destination.HasValue)
            {
                OCMap map = OCMap.Instance;                //(OCMap)GameObject.FindSceneObjectsOfType(typeof(OCMap)).FirstOrDefault();

                OCGoalController[] goalControllers = (OCGoalController[])GameObject.FindObjectsOfType(typeof(OCGoalController));

                foreach (Transform battery in map.BatteriesSceneObject.transform)
                {
                    if (VectorUtil.AreVectorsEqual(battery.position, new Vector3((float)origin.Value.x, (float)origin.Value.y, (float)origin.Value.z)))
                    {
                        battery.position = new Vector3((float)destination.Value.x, (float)destination.Value.y, (float)destination.Value.z);
                    }
                }

                OCBlockData block = map.GetBlock(origin.Value);

                map.SetBlockAndRecompute(block, destination.Value);
                map.SetBlockAndRecompute(OCBlockData.CreateInstance <OCBlockData>().Init(null, origin.Value), origin.Value);

                foreach (OCGoalController goalController in goalControllers)
                {
                    if (goalController.GoalBlockType == block.block)
                    {
                        goalController.FindGoalBlockPositionInChunks(map.GetChunks());
                    }
                }
            }
        }
Ejemplo n.º 2
0
        //---------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------

        #region Public Member Functions

        //---------------------------------------------------------------------------



        public void SetBlockAndRecompute(OCBlockData block, Vector3i pos)
        {
            //MethodInfo info = this.GetType().GetMember("SetBlockAndRecompute")[0] as MethodInfo;

// TODO [BLOCKED]: uncomment when aspect stuff is in place?
//		object[] attributes = info.GetCustomAttributes(typeof(OCLogAspect), true);
//		OCLogAspect asp = null;
//
//		if(attributes != null)
//			asp = attributes[0] as OCLogAspect;
//
//		if(asp == null)
//			Debug.Log("No OCLog Aspect...");
//
//		asp.OnEntry(null);

            OCBlockData oldBlock = GetBlock(pos);

            SetBlock(block, pos);

            // Convert the global coordinate of the block to the chunk coordinates.
            Vector3i chunkPos = OCChunk.ToChunkPosition(pos);

            UpdateChunkLimits(chunkPos);

            // Convert the global coordinate of the block to the coordinate within the chunk.
            Vector3i localPos = OCChunk.ToLocalPosition(pos);

            SetDirty(chunkPos);

            // If on the lower boundary of a chunk...set the neighbouring chunk to dirty too.
            if (localPos.x == 0)
            {
                SetDirty(chunkPos - Vector3i.right);
            }
            if (localPos.y == 0)
            {
                SetDirty(chunkPos - Vector3i.up);
            }
            if (localPos.z == 0)
            {
                SetDirty(chunkPos - Vector3i.forward);
            }

            // If on the upper boundary of a chunk...set the neighbouring chunk to dirty too.
            if (localPos.x == OCChunk.SIZE_X - 1)
            {
                SetDirty(chunkPos + Vector3i.right);
            }
            if (localPos.y == OCChunk.SIZE_Y - 1)
            {
                SetDirty(chunkPos + Vector3i.up);
            }
            if (localPos.z == OCChunk.SIZE_Z - 1)
            {
                SetDirty(chunkPos + Vector3i.forward);
            }

            OpenCog.Map.Lighting.OCSunLightComputer.RecomputeLightAtPosition(this, pos);
            OpenCog.Map.Lighting.OCLightComputer.RecomputeLightAtPosition(this, pos);

            UpdateMeshColliderAfterBlockChange();
            // TODO [BLOCKED]: uncomment when aspect stuff is in place?
            //		asp.OnExit(null);

            OpenCog.Embodiment.OCPerceptionCollector perceptionCollector = OpenCog.Embodiment.OCPerceptionCollector.Instance;

            List <GameObject> batteries = GameObject.FindGameObjectsWithTag("OCBattery").ToList();
            GameObject        battery   = batteries.Where(b => VectorUtil.AreVectorsEqual(b.transform.position, new Vector3((float)pos.x, (float)pos.y, (float)pos.z))).FirstOrDefault();

            List <GameObject> hearths = GameObject.FindGameObjectsWithTag("OCHearth").ToList();
            GameObject        hearth  = hearths.Where(h => VectorUtil.AreVectorsEqual(h.transform.position, new Vector3((float)pos.x, (float)pos.y, (float)pos.z))).FirstOrDefault();

            if (block.IsEmpty() && !oldBlock.IsEmpty())
            {
                UnityEngine.Debug.Log(OCLogSymbol.RUNNING + "OCMap.SetBlockAndRecompute(): Null block type sent; destroying block.");

                if (perceptionCollector != null && oldBlock.block.GetName() != "Battery")
                {
                    perceptionCollector.NotifyBlockRemoved(pos);
                }

                // I'm going to take a gamble here...since NotifyBatteryRemoved only does its work when it finds a battery at this location...it should be ok...

                if (perceptionCollector != null && oldBlock.block.GetName() == "Battery")
                {
                    perceptionCollector.NotifyBatteryRemoved(pos);
                }

                if (battery != default(GameObject) && battery != null)
                {
                    GameObject.DestroyImmediate(battery);
                }
                if (hearth != default(GameObject) && hearth != null)
                {
                    GameObject.DestroyImmediate(hearth);
                }
            }
            else
            {
                UnityEngine.Debug.Log(OCLogSymbol.RUNNING + "OCMap.SetBlockAndRecompute(): Block type sent; creating block.");

                // Moved notify down...to AFTER the point where it is actually created...
            }

            if (block.block != null && block.block.GetName() == "Battery" && (battery == default(GameObject) || battery == null))
            {
                GameObject batteryPrefab = OCMap.Instance.BatteryPrefab;
                if (batteryPrefab == null)
                {
                    UnityEngine.Debug.LogWarning(OCLogSymbol.WARN + "OCBuilder.Update(), batteryPrefab == null");
                }
                else
                {
                    GameObject newBattery = (GameObject)GameObject.Instantiate(batteryPrefab);
                    newBattery.transform.position = pos;
                    newBattery.name             = "Battery";
                    newBattery.transform.parent = OCMap.Instance.BatteriesSceneObject.transform;

                    if (perceptionCollector != null)
                    {
                        perceptionCollector.NotifyBatteryAdded(pos);
                    }
                }
            }

            if (block.block != null && block.block.GetName() == "Hearth" && (hearth == default(GameObject) || hearth == null))
            {
                GameObject hearthPrefab = OCMap.Instance.HearthPrefab;
                if (hearthPrefab == null)
                {
                    UnityEngine.Debug.LogWarning(OCLogSymbol.WARN + "OCBuilder::Update, hearthPrefab == null");
                }
                else
                {
                    GameObject newHearth = (GameObject)GameObject.Instantiate(hearthPrefab);
                    newHearth.transform.position = pos;
                    newHearth.name             = "Hearth";
                    newHearth.transform.parent = OCMap.Instance.HearthsSceneObject.transform;

                    if (perceptionCollector != null)
                    {
                        perceptionCollector.NotifyBlockAdded(pos);
                    }
                }
            }
        }
Ejemplo n.º 3
0
            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);
                        }
                    }
                }
            }