示例#1
0
        protected override void GenerateMethod(Node node, StringWriter stream, string indent)
        {
            base.GenerateMethod(node, stream, indent);

            State state = node as State;

            if (state == null)
            {
                return;
            }

            stream.WriteLine("{0}\t\tvirtual EBTStatus update_impl(Agent* pAgent, EBTStatus childStatus)", indent);
            stream.WriteLine("{0}\t\t{{", indent);
            stream.WriteLine("{0}\t\t\tBEHAVIAC_UNUSED_VAR(pAgent);", indent);
            stream.WriteLine("{0}\t\t\tBEHAVIAC_UNUSED_VAR(childStatus);", indent);

            if (state.Method != null)
            {
                string method = MethodCppExporter.GenerateCode(node, state.Method, stream, indent + "\t\t\t", string.Empty, string.Empty, "method");
                stream.WriteLine("{0}\t\t\t{1};", indent, method);

                MethodCppExporter.PostGenerateCode(state.Method, stream, indent + "\t\t\t", string.Empty, string.Empty, "method");
            }

            stream.WriteLine("{0}\t\t\treturn BT_RUNNING;", indent);
            stream.WriteLine("{0}\t\t}}", indent);
        }
示例#2
0
        protected virtual void GenerateMethod(Node node, StreamWriter stream, string indent)
        {
            stream.WriteLine("{0}\tprotected:", indent);

            // enter
            if (node.EnterAction != null && node.EnterAction.Name != "null_method")
            {
                stream.WriteLine("{0}\t\tvirtual bool enteraction_impl(Agent* pAgent)", indent);
                stream.WriteLine("{0}\t\t{{", indent);
                stream.WriteLine("{0}\t\t\tBEHAVIAC_UNUSED_VAR(pAgent);", indent);

                string retStr = MethodCppExporter.GenerateCode(node.EnterAction, stream, indent + "\t\t\t", node.EnterAction.NativeReturnType, string.Empty, "EnterAction");
                stream.WriteLine("{0}\t\t\t{1};", indent, retStr);

                MethodCppExporter.PostGenerateCode(node.EnterAction, stream, indent + "\t\t\t", node.EnterAction.NativeReturnType, string.Empty, "EnterAction");

                stream.WriteLine("{0}\t\t\treturn true;", indent);
                stream.WriteLine("{0}\t\t}}", indent);
            }

            // exit
            if (node.ExitAction != null && node.ExitAction.Name != "null_method")
            {
                stream.WriteLine("{0}\t\tvirtual bool exitaction_impl(Agent* pAgent)", indent);
                stream.WriteLine("{0}\t\t{{", indent);
                stream.WriteLine("{0}\t\t\tBEHAVIAC_UNUSED_VAR(pAgent);", indent);

                string retStr = MethodCppExporter.GenerateCode(node.ExitAction, stream, indent + "\t\t\t", node.ExitAction.NativeReturnType, string.Empty, "ExitAction");
                stream.WriteLine("{0}\t\t\t{1};", indent, retStr);

                MethodCppExporter.PostGenerateCode(node.ExitAction, stream, indent + "\t\t\t", node.ExitAction.NativeReturnType, string.Empty, "ExitAction");

                stream.WriteLine("{0}\t\t\treturn true;", indent);
                stream.WriteLine("{0}\t\t}}", indent);
            }
        }
        protected override void GenerateMethod(Attachment attachment, StringWriter stream, string indent)
        {
            base.GenerateMethod(attachment, stream, indent);

            AttachAction attach = attachment as AttachAction;

            if (attach == null)
            {
                return;
            }

            stream.WriteLine("{0}\t\tvirtual EBTStatus update_impl(Agent* pAgent, EBTStatus childStatus)", indent);
            stream.WriteLine("{0}\t\t{{", indent);
            stream.WriteLine("{0}\t\t\tEBTStatus result = BT_SUCCESS;", indent);

            if (attach.IsAction())
            {
                string method = MethodCppExporter.GenerateCode(attachment, attach.Opl.Method, stream, indent + "\t\t\t", string.Empty, string.Empty, "opl");

                stream.WriteLine("{0}\t\t\t{1};", indent, method);
                MethodCppExporter.PostGenerateCode(attach.Opl.Method, stream, indent + "\t\t\t", string.Empty, string.Empty, "opl");
            }
            else if (attach.IsAssign())
            {
                if (attach.Opl != null && !attach.Opl.IsMethod && attach.Opl.Var != null && attach.Opr2 != null)
                {
                    PropertyDef prop = attach.Opl.Var.Property;

                    if (prop != null)
                    {
                        RightValueCppExporter.GenerateCode(attachment, attach.Opr2, stream, indent + "\t\t\t", attach.Opr2.NativeType, "opr2", "opr2");

                        string property = PropertyCppExporter.GetProperty(attachment, prop, attach.Opl.Var.ArrayIndexElement, stream, indent + "\t\t\t", "opl", "attach");
                        string propName = prop.BasicName.Replace("[]", "");

                        if (prop.IsArrayElement && attach.Opl.Var.ArrayIndexElement != null)
                        {
                            ParameterCppExporter.GenerateCode(attachment, attach.Opl.Var.ArrayIndexElement, stream, indent + "\t\t\t", "int", "opl_index", "attach_opl");
                            property = string.Format("({0})[opl_index]", property);
                        }

                        if (!prop.IsArrayElement && (prop.IsPar || prop.IsCustomized))
                        {
                            string propBasicName = prop.BasicName.Replace("[]", "");
                            uint   id            = Behaviac.Design.CRC32.CalcCRC(propBasicName);
                            string agentName     = PropertyCppExporter.GetGenerateAgentName(prop, "opl", "attach");

                            stream.WriteLine("{0}\t\t\t{1}->SetVariable(\"{2}\", {3}u, opr2);", indent, agentName, propBasicName, id);
                        }
                        else
                        {
                            stream.WriteLine("{0}\t\t\t{1} = opr2;", indent, property);
                        }

                        if (attach.Opr2.IsMethod)
                        {
                            RightValueCppExporter.PostGenerateCode(attach.Opr2, stream, indent + "\t\t\t", attach.Opr2.NativeType, "opr2", string.Empty);
                        }
                    }
                }
            }
            else if (attach.IsCompare())
            {
                ConditionCppExporter.GenerateOperand(attachment, stream, indent + "\t\t\t", attach.Opl, "opl", "");
                ConditionCppExporter.GenerateOperand(attachment, stream, indent + "\t\t\t", attach.Opr2, "opr2", "");

                switch (attach.Operator)
                {
                case OperatorTypes.Equal:
                    stream.WriteLine("{0}\t\t\tbool op = PrivateDetails::Equal(opl, opr2);", indent);
                    break;

                case OperatorTypes.NotEqual:
                    stream.WriteLine("{0}\t\t\tbool op = !PrivateDetails::Equal(opl, opr2);", indent);
                    break;

                case OperatorTypes.Greater:
                    stream.WriteLine("{0}\t\t\tbool op = PrivateDetails::Greater(opl, opr2);", indent);
                    break;

                case OperatorTypes.GreaterEqual:
                    stream.WriteLine("{0}\t\t\tbool op = PrivateDetails::GreaterEqual(opl, opr2);", indent);
                    break;

                case OperatorTypes.Less:
                    stream.WriteLine("{0}\t\t\tbool op = PrivateDetails::Less(opl, opr2);", indent);
                    break;

                case OperatorTypes.LessEqual:
                    stream.WriteLine("{0}\t\t\tbool op = PrivateDetails::LessEqual(opl, opr2);", indent);
                    break;

                default:
                    stream.WriteLine("{0}\t\t\tbool op = false;", indent);
                    break;
                }

                stream.WriteLine("{0}\t\t\tif (!op)", indent);
                stream.WriteLine("{0}\t\t\t\tresult = BT_FAILURE;", indent);
            }
            else if (attach.IsCompute())
            {
                if (attach.Opl != null && !attach.Opl.IsMethod && attach.Opl.Var != null && attach.Opr1 != null && attach.Opr2 != null)
                {
                    PropertyDef prop = attach.Opl.Var.Property;

                    if (prop != null)
                    {
                        string typeName = DataCppExporter.GetGeneratedNativeType(attach.Opr1.ValueType);

                        RightValueCppExporter.GenerateCode(attachment, attach.Opr1, stream, indent + "\t\t\t", typeName, "opr1", "opr1");
                        RightValueCppExporter.GenerateCode(attachment, attach.Opr2, stream, indent + "\t\t\t", typeName, "opr2", "opr2");

                        string oprStr = string.Empty;

                        switch (attach.Operator)
                        {
                        case OperatorTypes.Add:
                            oprStr = "opr1 + opr2";
                            break;

                        case OperatorTypes.Sub:
                            oprStr = "opr1 - opr2";
                            break;

                        case OperatorTypes.Mul:
                            oprStr = "opr1 * opr2";
                            break;

                        case OperatorTypes.Div:
                            oprStr = "opr1 / opr2";
                            break;

                        default:
                            Debug.Check(false, "The operator is wrong!");
                            break;
                        }

                        oprStr = string.Format("({0})({1})", typeName, oprStr);

                        string property = PropertyCppExporter.GetProperty(attachment, prop, attach.Opl.Var.ArrayIndexElement, stream, indent + "\t\t\t", "opl", "attach");
                        string propName = prop.BasicName.Replace("[]", "");

                        if (prop.IsArrayElement && attach.Opl.Var.ArrayIndexElement != null)
                        {
                            ParameterCppExporter.GenerateCode(attachment, attach.Opl.Var.ArrayIndexElement, stream, indent + "\t\t\t", "int", "opl_index", "attach_opl");
                            property = string.Format("({0})[opl_index]", property);
                        }

                        string propBasicName = prop.BasicName.Replace("[]", "");

                        if (!prop.IsArrayElement && (prop.IsPar || prop.IsCustomized))
                        {
                            uint   id        = Behaviac.Design.CRC32.CalcCRC(propBasicName);
                            string agentName = PropertyCppExporter.GetGenerateAgentName(prop, "opl", "attach");

                            stream.WriteLine("{0}\t\t\t{1}->SetVariable(\"{2}\", {3}u, {4});", indent, agentName, propBasicName, id, oprStr);
                        }
                        else
                        {
                            stream.WriteLine("{0}\t\t\t{1} = {2};", indent, property, oprStr);
                        }

                        if (attach.Opr1.IsMethod)
                        {
                            RightValueCppExporter.PostGenerateCode(attach.Opr1, stream, indent + "\t\t\t", typeName, "opr1", string.Empty);
                        }

                        if (attach.Opr2.IsMethod)
                        {
                            RightValueCppExporter.PostGenerateCode(attach.Opr2, stream, indent + "\t\t\t", typeName, "opr2", string.Empty);
                        }
                    }
                }
            }

            stream.WriteLine("{0}\t\t\treturn result;", indent);
            stream.WriteLine("{0}\t\t}}", indent);
        }
        protected override void GenerateMethod(Node node, StreamWriter stream, string indent)
        {
            base.GenerateMethod(node, stream, indent);

            Action action = node as Action;

            if (action == null)
            {
                return;
            }

            stream.WriteLine("{0}\t\tvirtual EBTStatus update_impl(Agent* pAgent, EBTStatus childStatus)", indent);
            stream.WriteLine("{0}\t\t{{", indent);
            stream.WriteLine("{0}\t\t\tBEHAVIAC_UNUSED_VAR(pAgent);", indent);
            stream.WriteLine("{0}\t\t\tBEHAVIAC_UNUSED_VAR(childStatus);", indent);

            string resultStatus = "BT_SUCCESS";

            if (action.Method != null)
            {
                string method = MethodCppExporter.GenerateCode(action.Method, stream, indent + "\t\t\t", string.Empty, string.Empty, "method");

                if ("behaviac::EBTStatus" == action.Method.NativeReturnType)
                {
                    stream.WriteLine("{0}\t\t\t{1} result = {2};", indent, action.Method.NativeReturnType, method);
                    resultStatus = "result";

                    MethodCppExporter.PostGenerateCode(action.Method, stream, indent + "\t\t\t", string.Empty, string.Empty, "method");
                }
                else
                {
                    if (("void" == action.Method.NativeReturnType) || (EBTStatus.BT_INVALID != action.ResultOption) || action.ResultFunctor == null)
                    {
                        stream.WriteLine("{0}\t\t\t{1};", indent, method);
                    }
                    else
                    {
                        stream.WriteLine("{0}\t\t\t{1} result = {2};", indent, action.Method.NativeReturnType, method);
                    }

                    MethodCppExporter.PostGenerateCode(action.Method, stream, indent + "\t\t\t", string.Empty, string.Empty, "method");

                    if (EBTStatus.BT_INVALID != action.ResultOption)
                    {
                        resultStatus = action.ResultOption.ToString();
                    }
                    else if (Plugin.IsMatchedStatusMethod(action.Method, action.ResultFunctor))
                    {
                        if ("void" == action.Method.NativeReturnType)
                        {
                            resultStatus = MethodCppExporter.GenerateCode(action.ResultFunctor, stream, indent + "\t\t\t", string.Empty, string.Empty, "functor");
                        }
                        else
                        {
                            string agentName = "pAgent";
                            if (action.ResultFunctor.Owner != VariableDef.kSelf)
                            {
                                agentName = "pAgent_functor";

                                stream.WriteLine("{0}Agent* {1} = Agent::GetInstance(pAgent, \"{2}\");", indent, agentName, action.ResultFunctor.Owner);
                                stream.WriteLine("{0}BEHAVIAC_ASSERT({1});", indent, agentName);
                            }

                            resultStatus = string.Format("(({0}*){1})->{2}(result)", action.ResultFunctor.ClassName, agentName, action.ResultFunctor.Name);
                        }
                    }
                }
            }

            stream.WriteLine("{0}\t\t\treturn {1};", indent, resultStatus);
            stream.WriteLine("{0}\t\t}}", indent);
        }