Пример #1
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);
                        }
                    }
                }
            }