Exemplo n.º 1
0
        private bool InvokeCheckConditionCallback(ref TArgument argument, BehaviorData <TAction, TTarget, TCondition> .Condition condition)
        {
            var callback = checkConditionCallbacks.GetValueOrDefault(condition.Type);

            if (callback == null)
            {
                if (onErrorCallback != null)
                {
                    var message = string.Format("InvokeCheckConditionCallback: Callback not found.\n{0} => {1}", typeof(TCondition).Name, condition.Type);

                    onErrorCallback.Invoke(message);
                }

                return(false);
            }

            var contents = ParseParameters(condition.Parameters);

            var parameter = new Parameter(contents, onErrorCallback);

            return(callback.Invoke(ref argument, parameter));
        }
Exemplo n.º 2
0
        public TBehaviorData Build(BehaviorControlAsset behaviorControlAsset)
        {
            if (behaviorControlAsset == null)
            {
                return(null);
            }

            var hasError     = false;
            var errorMessage = new StringBuilder();

            errorMessage.AppendFormat("Build Error").AppendLine();
            errorMessage.AppendLine();

            var behaviors = new List <BehaviorData <TAction, TTarget, TCondition> .Behavior>();

            for (var i = 0; i < behaviorControlAsset.Behaviors.Count; i++)
            {
                var recordError = new StringBuilder();

                var content = behaviorControlAsset.Behaviors[i];

                if (content == null)
                {
                    continue;
                }

                //------ ActionType ------

                var actionType = default(TAction);

                if (!string.IsNullOrEmpty(content.ActionType) && actionEnums.ContainsKey(content.ActionType))
                {
                    actionType = actionEnums.GetValueOrDefault(content.ActionType);
                }
                else
                {
                    var message = string.Format("Unknown action type. {0} = {1}", typeof(TAction).Name, content.ActionType);

                    recordError.AppendLine(message);
                }

                //------ TargetType ------

                var targetType = default(TTarget);

                if (!string.IsNullOrEmpty(content.TargetType) && targetEnums.ContainsKey(content.TargetType))
                {
                    targetType = targetEnums.GetValueOrDefault(content.TargetType);
                }
                else
                {
                    var message = string.Format("Unknown target type. {0} = {1}", typeof(TTarget).Name, content.TargetType);

                    recordError.AppendLine(message);
                }

                //------ Conditions ------

                var conditions = new List <BehaviorData <TAction, TTarget, TCondition> .Condition>();

                foreach (var item in content.Conditions)
                {
                    var conditionType = default(TCondition);

                    if (!string.IsNullOrEmpty(item.Type) && conditionEnums.ContainsKey(item.Type))
                    {
                        conditionType = conditionEnums.GetValueOrDefault(item.Type);
                    }
                    else
                    {
                        var message = string.Format("Unknown condition type. {0} = {1}", typeof(TCondition).Name, item.Type);

                        recordError.AppendLine(message);
                    }

                    var condition = new BehaviorData <TAction, TTarget, TCondition> .Condition
                    {
                        Connecter  = item.Connecter,
                        Type       = conditionType,
                        Parameters = item.Parameters,
                    };

                    conditions.Add(condition);
                }

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

                if (recordError.Length == 0)
                {
                    var behavior = new BehaviorData <TAction, TTarget, TCondition> .Behavior
                    {
                        ActionType = actionType,
                        TargetType = targetType,
                        Conditions = conditions.ToArray(),

                        SuccessRate      = content.SuccessRate,
                        ActionParameters = content.ActionParameters,
                        TargetParameters = content.TargetParameters,
                    };

                    behaviors.Add(behavior);
                }
                else
                {
                    errorMessage.AppendFormat("----- Record:{0} -----", i).AppendLine();
                    errorMessage.AppendLine();
                    errorMessage.AppendLine(recordError.ToString());

                    hasError = true;
                }
            }

            if (hasError)
            {
                if (onErrorCallback != null)
                {
                    onErrorCallback.Invoke(errorMessage.ToString());
                }

                return(null);
            }

            var behaviorData = new TBehaviorData()
            {
                Description = behaviorControlAsset.Description,
                Behaviors   = behaviors.ToArray(),
            };

            return(behaviorData);
        }
Exemplo n.º 3
0
        public bool Execute(string behaviorName, BehaviorData <TAction, TTarget, TCondition> behaviorData, TArgument argument)
        {
            var result = false;

            if (behaviorData == null)
            {
                return(false);
            }

            var logData = new LogData(controllerName, behaviorName);

            foreach (var behavior in behaviorData.Behaviors)
            {
                // 0f - 1fの範囲内に丸める.
                var probability = Math.Min(Math.Max(behavior.SuccessRate, 0f), 1f) * 100f;

                //====== ログデータ構築 ======

                var actionNode = new LogData.Node()
                {
                    Type = behavior.ActionType.ToString(), Parameter = behavior.ActionParameters
                };

                var targetNode = new LogData.Node()
                {
                    Type = behavior.TargetType.ToString(), Parameter = behavior.TargetParameters
                };

                var conditionNodes = behavior.Conditions
                                     .Select(x =>
                {
                    return(new LogData.Node()
                    {
                        Type = x.Type.ToString(),
                        Parameter = x.Parameters,
                    });
                })
                                     .ToArray();

                var connecters = behavior.Conditions.Select(x => x.Connecter).ToArray();

                var logElement = new LogData.Element()
                {
                    Probability    = probability,
                    ActionNode     = actionNode,
                    TargetNode     = targetNode,
                    ConditionNodes = conditionNodes,
                    Connecters     = connecters,
                };

                //====== 確率判定 ======

                var percentage = RandomUtility.RandomInRange(1f, 100f);

                var isHit = percentage != 0 && percentage <= probability;

                logElement.Percentage = percentage;

                if (!isHit)
                {
                    continue;
                }

                //====== 対象検索 ======

                var selectResult = InvokeTargetSelectCallback(ref argument, behavior);

                logElement.TargetNode.Result = selectResult;

                if (!selectResult)
                {
                    continue;
                }

                //====== 条件一致判定 ======

                var connecter = ConditionConnecter.None;

                var check = true;

                for (var i = 0; i < behavior.Conditions.Length; i++)
                {
                    var condition = behavior.Conditions[i];

                    var conditionResult = InvokeCheckConditionCallback(ref argument, condition);

                    // 複数条件は接続詞が必要.
                    if (0 < i)
                    {
                        connecter = condition.Connecter;

                        // 接続詞が未定義.
                        if (connecter == ConditionConnecter.None)
                        {
                            break;
                        }
                    }

                    switch (connecter)
                    {
                    case ConditionConnecter.And:
                        check &= conditionResult;
                        break;

                    case ConditionConnecter.Or:
                        check |= conditionResult;
                        break;

                    case ConditionConnecter.None:
                        check = conditionResult;
                        break;
                    }

                    logElement.ConditionNodes[i].Result = conditionResult;

                    if (!check)
                    {
                        break;
                    }
                }

                //====== 行動実行 ======

                if (check)
                {
                    var actionResult = InvokeActionCallback(ref argument, behavior);

                    logElement.ActionNode.Result = actionResult;

                    // 行動実行したら終了.
                    if (actionResult)
                    {
                        result = true;
                    }
                }

                logData.AddElement(logElement);

                if (result)
                {
                    break;
                }
            }

            BehaviorControlLogger.Instance.Add(logData);

            return(result);
        }
Exemplo n.º 4
0
        public TBehaviorData Convert(string dataPath, ImportData importData)
        {
            var hasError     = false;
            var errorMessage = new StringBuilder();

            errorMessage.AppendFormat("Import Error").AppendLine();
            errorMessage.AppendFormat("File: {0}", dataPath).AppendLine();
            errorMessage.AppendFormat("Sheet: {0}", importData.sheetName).AppendLine();
            errorMessage.AppendLine();

            var behaviors = new List <BehaviorData <TAction, TTarget, TCondition> .Behavior>();

            for (var i = 0; i < importData.records.Length; i++)
            {
                var recordError = new StringBuilder();

                var record = importData.records[i];

                if (record.behavior == null)
                {
                    continue;
                }

                //------ ActionType ------

                var actionType = default(TAction);

                if (!string.IsNullOrEmpty(record.behavior.actionType) && actionEnums.ContainsKey(record.behavior.actionType))
                {
                    actionType = actionEnums.GetValueOrDefault(record.behavior.actionType);
                }
                else
                {
                    var message = string.Format("Unknown action type. {0} = {1}", typeof(TAction).Name, record.behavior.actionType);

                    recordError.AppendLine(message);
                }

                //------ TargetType ------

                var targetType = default(TTarget);

                if (!string.IsNullOrEmpty(record.behavior.targetType) && targetEnums.ContainsKey(record.behavior.targetType))
                {
                    targetType = targetEnums.GetValueOrDefault(record.behavior.targetType);
                }
                else
                {
                    var message = string.Format("Unknown target type. {0} = {1}", typeof(TTarget).Name, record.behavior.targetType);

                    recordError.AppendLine(message);
                }

                //------ Conditions ------

                var conditions = new List <BehaviorData <TAction, TTarget, TCondition> .Condition>();

                foreach (var item in record.behavior.conditions)
                {
                    var conditionType = default(TCondition);

                    if (!string.IsNullOrEmpty(item.type) && conditionEnums.ContainsKey(item.type))
                    {
                        conditionType = conditionEnums.GetValueOrDefault(item.type);
                    }
                    else
                    {
                        var message = string.Format("Unknown condition type. {0} = {1}", typeof(TCondition).Name, item.type);

                        recordError.AppendLine(message);
                    }

                    var connecter = ConditionConnecter.None;

                    if (conditions.Any())
                    {
                        switch (item.connecter)
                        {
                        case "&":
                            connecter = ConditionConnecter.And;
                            break;

                        case "|":
                            connecter = ConditionConnecter.Or;
                            break;
                        }

                        if (connecter == ConditionConnecter.None)
                        {
                            var message = string.Format("Unknown connecter type. [{0}]", item.connecter);

                            recordError.AppendLine(message);
                        }
                    }

                    var condition = new BehaviorData <TAction, TTarget, TCondition> .Condition
                    {
                        Connecter  = connecter,
                        Type       = conditionType,
                        Parameters = item.parameters,
                    };

                    conditions.Add(condition);
                }

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

                if (recordError.Length == 0)
                {
                    var behavior = new BehaviorData <TAction, TTarget, TCondition> .Behavior()
                    {
                        ActionType = actionType,
                        TargetType = targetType,
                        Conditions = conditions.ToArray(),

                        SuccessRate      = record.behavior.successRate,
                        ActionParameters = record.behavior.actionParameters,
                        TargetParameters = record.behavior.targetParameters,
                    };

                    behaviors.Add(behavior);
                }
                else
                {
                    errorMessage.AppendFormat("----- Record:{0} -----", i).AppendLine();
                    errorMessage.AppendLine();
                    errorMessage.AppendLine(recordError.ToString());

                    hasError = true;
                }
            }

            if (hasError)
            {
                if (onErrorCallback != null)
                {
                    onErrorCallback.Invoke(errorMessage.ToString());
                }

                return(null);
            }

            var behaviorData = new TBehaviorData()
            {
                Description = importData.description,
                Behaviors   = behaviors.ToArray(),
            };

            return(behaviorData);
        }