Beispiel #1
0
        protected override void GenerateMethod(Node node, StreamWriter stream, string indent)
        {
            base.GenerateMethod(node, stream, indent);

            Compute compute = node as Compute;

            Debug.Check(compute != null);

            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);
            stream.WriteLine("{0}\t\t\tEBTStatus result = BT_SUCCESS;", indent);

            if (compute.Opl != null && compute.Opr1 != null && compute.Opr2 != null)
            {
                RightValueCppExporter.GenerateCode(compute.Opr1, stream, indent + "\t\t\t", compute.Opr1.NativeType, "opr1", "opr1");
                RightValueCppExporter.GenerateCode(compute.Opr2, stream, indent + "\t\t\t", compute.Opr2.NativeType, "opr2", "opr2");

                string oprStr = string.Empty;
                switch (compute.Operator)
                {
                case ComputeOperator.Add:
                    oprStr = "opr1 + opr2";
                    break;

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

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

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

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

                string basicType = DataCppExporter.GetBasicGeneratedNativeType(compute.Opl.NativeType);
                stream.WriteLine("{0}\t\t\t{1} opr = ({1})({2});", indent, basicType, oprStr);

                if (compute.Opl.IsPar)
                {
                    ParInfo par = compute.Opl.Value as ParInfo;
                    if (par != null)
                    {
                        uint id = Behaviac.Design.CRC32.CalcCRC(par.Name);
                        stream.WriteLine("{0}\t\t\tBEHAVIAC_ASSERT(behaviac::MakeVariableId(\"{1}\") == {2}u);", indent, par.Name, id);
                        stream.WriteLine("{0}\t\t\tpAgent->SetVariable(\"{1}\", opr, {2}u);", indent, par.Name, id);
                    }
                }
                else
                {
                    string opl = VariableCppExporter.GenerateCode(compute.Opl, stream, indent + "\t\t\t", string.Empty, string.Empty, "opl");
                    stream.WriteLine("{0}\t\t\t{1} = opr;", indent, opl);

                    VariableCppExporter.PostGenerateCode(compute.Opl, stream, indent + "\t\t\t", compute.Opl.NativeType, "opl", string.Empty, null, "", "opr");
                }

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

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

            stream.WriteLine("{0}\t\t\treturn result;", indent);
            stream.WriteLine("{0}\t\t}}", indent);
        }
Beispiel #2
0
        private void ExportHead(StreamWriter file, string exportFilename)
        {
            string wsfolder = Path.GetDirectoryName(Workspace.Current.FileName);

            exportFilename = Behaviac.Design.FileManagers.FileManager.MakeRelative(wsfolder, exportFilename);
            exportFilename = exportFilename.Replace("\\", "/");

            // write comments
            file.WriteLine("// ---------------------------------------------------------------------");
            file.WriteLine("/* This file is auto-generated, so please don't modify it by yourself!");
            file.WriteLine("Usage: include it in a certain cpp accordingly(RELATIVE_PATH is the path where it is generated):\n");
            file.WriteLine("      #include \"RELATIVE_PATH/generated_behaviors.cpp\"\n*/\n");
            file.WriteLine("// Export file: {0}", exportFilename);
            file.WriteLine("// ---------------------------------------------------------------------\r\n");

            // write included behaviac files
            file.WriteLine("// You should set the include path of the behaviac lib in your project\r\n// for using the following header files :");
            file.WriteLine("#include \"behaviac/behaviortree/behaviortree.h\"\r\n");

            file.WriteLine("#include \"behaviac/behaviortree/nodes/actions/action.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/actions/assignment.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/actions/compute.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/actions/noop.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/actions/wait.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/actions/waitforsignal.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/actions/waitframes.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/composites/compositestochastic.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/composites/ifelse.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/composites/parallel.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/composites/query.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/composites/referencebehavior.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/composites/selector.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/composites/selectorloop.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/composites/selectorprobability.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/composites/selectorstochastic.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/composites/sequence.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/composites/sequencestochastic.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/composites/withprecondition.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/conditions/and.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/conditions/conditionbase.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/conditions/condition.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/conditions/false.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/conditions/or.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/conditions/true.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/decorators/decoratoralwaysfailure.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/decorators/decoratoralwaysrunning.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/decorators/decoratoralwayssuccess.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/decorators/decoratorcount.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/decorators/decoratorcountlimit.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/decorators/decoratorfailureuntil.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/decorators/decoratorframes.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/decorators/decoratorlog.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/decorators/decoratorloop.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/decorators/decoratorloopuntil.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/decorators/decoratornot.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/decorators/decoratorsuccessuntil.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/decorators/decoratortime.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/nodes/decorators/decoratorweight.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/attachments/event.h\"");
            file.WriteLine("#include \"behaviac/behaviortree/attachments/predicate.h\"\r\n");

            // write included files for the game agents
            if (this.IncludedFilenames != null)
            {
                file.WriteLine("// You should set the agent header files of your game\r\n// when exporting cpp files in the behaviac editor:");
                foreach (string filename in this.IncludedFilenames)
                {
                    file.WriteLine("#include \"{0}\"", filename);
                }
                file.WriteLine("");
            }

            // write the namespaces for the game agents
            file.WriteLine("using namespace behaviac;\r\n");

            // write property and method handlers
            file.WriteLine("// Agent property and method handlers\r\n");

            foreach (AgentType agenType in Plugin.AgentTypes)
            {
                List <string> namespaces = new List <string>();
                string        ns         = agenType.Namespace;

                if (!string.IsNullOrEmpty(ns))
                {
                    foreach (PropertyDef property in agenType.GetProperties())
                    {
                        if (property.ClassName == agenType.AgentTypeName)
                        {
                            namespaces = GetNamespaces(ns);
                            break;
                        }
                    }
                }

                if (namespaces.Count == 0 && !string.IsNullOrEmpty(ns))
                {
                    foreach (MethodDef method in agenType.GetMethods())
                    {
                        if (method.ClassName == agenType.AgentTypeName)
                        {
                            namespaces = GetNamespaces(ns);
                            break;
                        }
                    }
                }

                string indent = WriteNamespacesHead(file, namespaces);

                foreach (PropertyDef property in agenType.GetProperties())
                {
                    if (property.ClassName == agenType.AgentTypeName)
                    {
                        string propName   = property.Name.Replace("::", "_");
                        string nativeType = DataCppExporter.GetBasicGeneratedNativeType(property.NativeType);

                        file.WriteLine("{0}struct PROPERTY_TYPE_{1} {{ }};", indent, propName);
                        file.WriteLine("{0}template<> BEHAVIAC_FORCEINLINE {1}& {2}::_Get_Property_<PROPERTY_TYPE_{3}>()", indent, nativeType, agenType.BasicClassName, propName);
                        file.WriteLine("{0}{{", indent);
                        if (property.IsStatic)
                        {
                            file.WriteLine("{0}\tunsigned char* pc = (unsigned char*)(&{1});", indent, property.Name);
                        }
                        else
                        {
                            file.WriteLine("{0}\tunsigned char* pc = (unsigned char*)this;", indent);
                            file.WriteLine("{0}\tpc += (int)offsetof({1}, {2});", indent, property.ClassName, property.Name);
                        }
                        file.WriteLine("{0}\treturn *(reinterpret_cast<{1}*>(pc));", indent, nativeType);
                        file.WriteLine("{0}}}\r\n", indent);
                    }
                }

                foreach (MethodDef method in agenType.GetMethods())
                {
                    if (method.ClassName == agenType.AgentTypeName)
                    {
                        string paramStrDef = string.Empty;
                        string paramStr    = string.Empty;
                        for (int i = 0; i < method.Params.Count; ++i)
                        {
                            string basicNativeType = DataCppExporter.GetBasicGeneratedNativeType(method.Params[i].NativeType, false);
                            string refStr          = string.Empty;
                            if (basicNativeType != "char*" && basicNativeType != "char *")
                            {
                                if (basicNativeType.EndsWith("*"))
                                {
                                    refStr = "&";

                                    basicNativeType = basicNativeType.Remove(basicNativeType.Length - 1);
                                }
                                basicNativeType = basicNativeType.Trim();
                            }

                            if (i > 0)
                            {
                                paramStrDef += ", ";
                                paramStr    += ", ";
                            }

                            string refConstStr = "&";
                            if (basicNativeType.EndsWith("&"))
                            {
                                refConstStr = string.Empty;
                            }

                            paramStrDef += string.Format("{0}{1} p{2}", basicNativeType, refConstStr, i);
                            paramStr    += string.Format("{0}p{1}", refStr, i);
                        }

                        string methodName       = method.Name.Replace("::", "_");
                        string nativeReturnType = DataCppExporter.GetGeneratedNativeType(method.NativeReturnType);
                        file.WriteLine("{0}struct METHOD_TYPE_{1} {{ }};", indent, methodName);
                        file.WriteLine("{0}template<> BEHAVIAC_FORCEINLINE {1} {2}::_Execute_Method_<METHOD_TYPE_{3}>({4})", indent, nativeReturnType, agenType.BasicClassName, methodName, paramStrDef);
                        file.WriteLine("{0}{{", indent);

                        string ret = (method.NativeReturnType == "void") ? string.Empty : "return ";
                        file.WriteLine("{0}\t{1}this->{2}({3});", indent, ret, method.Name, paramStr);

                        file.WriteLine("{0}}}\r\n", indent);
                    }
                }

                WriteNamespacesTail(file, namespaces);
            }

            // create namespace
            file.WriteLine("namespace behaviac\r\n{");
        }