Esempio n. 1
0
    VariableComparator* Condition::Create(const char* typeName, const char* comparionOperator, Property* lhs, behaviac::CMethodBase* lhs_m, Property* rhs, behaviac::CMethodBase* rhs_m)
    {
        E_VariableComparisonType comparisonType = VariableComparator::ParseComparisonType(comparionOperator);

        bool bAgentPtr = false;

        //it might be par or the right value of condition/assignment
        if (Agent::IsAgentClassName(typeName))
        {
            bAgentPtr = true;
            typeName = "void*";
        }

        BEHAVIAC_UNUSED_VAR(bAgentPtr);

        VariableComparatorCreator* pCreator = ComparatorCreators()[typeName];

        if (pCreator)
        {
            VariableComparator* pComparator = (*pCreator)(comparisonType, lhs, lhs_m, rhs, rhs_m);
            return pComparator;
        }
        else
        {
            BEHAVIAC_LOGWARNING("please add Condition::Register<%s>(\"%s\") in your code\n", typeName, typeName, typeName);
            BEHAVIAC_ASSERT(0);
        }

        return 0;
    }
Esempio n. 2
0
    EBTStatus ReferencedBehaviorTask::update(Agent* pAgent, EBTStatus childStatus)
    {
        BEHAVIAC_UNUSED_VAR(childStatus);
		BEHAVIAC_ASSERT(ReferencedBehavior::DynamicCast(this->GetNode()));
		const ReferencedBehavior* pNode = (const ReferencedBehavior*)this->m_node;
		BEHAVIAC_ASSERT(pNode);

#if !defined(BEHAVIAC_RELEASE)
		pAgent->m_debug_count++;
		if (pAgent->m_debug_count > 20)
		{
			BEHAVIAC_LOGWARNING("%s might be in a recurrsive inter calling of trees\n", pAgent->GetName().c_str());
			BEHAVIAC_ASSERT(false);
		}
#endif//#if !defined(BEHAVIAC_RELEASE)

		EBTStatus result = this->m_subTree->exec(pAgent);

		bool bTransitioned = State::UpdateTransitions(pAgent, pNode, pNode->m_transitions, this->m_nextStateId);

		if (bTransitioned)
		{
			result = BT_SUCCESS;
		}

		return result;
    }
Esempio n. 3
0
    BehaviorNode* BehaviorNode::load(const char* agentType, rapidxml::xml_node<>* node, int version)
    {
        //BEHAVIAC_ASSERT(node.Tag == "node");
        BEHAVIAC_ASSERT(StringUtils::StrEqual(node->name(), "node"));

        if (rapidxml::xml_attribute<>* attr = node->first_attribute(kStrClass))
        {
            BEHAVIAC_ASSERT(StringUtils::StrEqual(attr->name(), kStrClass));
            const char* pClassName = attr->value();
            BehaviorNode* pNode = BehaviorNode::Create(pClassName);

            if (!pNode)
            {
                BEHAVIAC_LOGWARNING("invalid node class '%s'\n", pClassName);
            }

            //BEHAVIAC_ASSERT(pNode != NULL, "unsupported class {0}", pClassName);
            BEHAVIAC_ASSERT(pNode != NULL, "unsupported class %s", pClassName);

            if (pNode != NULL)
            {
                pNode->SetClassNameString(pClassName);
                const char* idStr = node->first_attribute(kStrId)->value();//node.Attribute("id");
				pNode->SetId((uint16_t)atoi(idStr));

                pNode->load_properties_pars_attachments_children(true, version, agentType, node);
            }

            return pNode;
        }

        return 0;
    }
Esempio n. 4
0
//! Parse xml from memory buffer with specific size
XmlNodeRef XmlParser::parseBuffer(char* buffer, int size, const char* rootNodeName)
{
    m_errorString = "";
    XmlParserImp xml;
    XmlNodeRef ref = xml.parse(buffer, size, rootNodeName, m_errorString, true);

    if (!m_errorString.empty())
    {
        BEHAVIAC_LOGWARNING("Error while parsing XML file : \n\n%s", m_errorString.c_str());
    }

    return ref;
}
Esempio n. 5
0
XmlNodeRef XmlParser::parse(behaviac::IFile* file, const char* rootNodeName, const char* suffix, bool handleError)
{
    BEHAVIAC_UNUSED_VAR(suffix);
    BEHAVIAC_UNUSED_VAR(handleError);

    m_errorString.clear();
    XmlParserImp xml;

    if (file)
    {
        int iSize = (int)file->GetSize() - (int)file->Seek(0, behaviac::CFileSystem::ESeekMoveMode_Cur);

        if (iSize != 0)
        {
            static const int32_t ReadBlockSize = 64 * 1024;
            char* buf = (char*)BEHAVIAC_MALLOC_WITHTAG(ReadBlockSize, "XML");
            XmlNodeRef ref;

            for (int32_t i = 0; i <= iSize / (ReadBlockSize); ++i)
            {
                int32_t bufSize = file->Read(buf, ReadBlockSize);
                {
                    buf[bufSize] = '\0';
                    ref = xml.parse(buf, bufSize, rootNodeName, m_errorString, i == iSize / (ReadBlockSize));
                }
            }

            BEHAVIAC_FREE(buf);

            if (handleError && !m_errorString.empty())
            {
                BEHAVIAC_LOGWARNING("Error while parsing file\n\n%s", m_errorString.c_str());
            }

            return ref;

        }
        else
        {
            return XmlNodeRef();
        }
    }
    else
    {
        BEHAVIAC_ASSERT(0, "XmlParse(behaviac::IFile*) - Invalid file\n");
        return XmlNodeRef();
    }
}
Esempio n. 6
0
XmlNodeRef XmlParser::parse(const char* fileName, const char* rootNodeName, const char* suffix)
{
    m_errorString.clear();
    behaviac::IFile* file = behaviac::CFileManager::GetInstance()->FileOpen(fileName, behaviac::CFileSystem::EOpenAccess_Read);

    if (file)
    {
        XmlNodeRef xml = this->parse(file, rootNodeName, suffix, false);
        behaviac::CFileManager::GetInstance()->FileClose(file);

        if (!m_errorString.empty())
        {
            BEHAVIAC_LOGWARNING("Error while parsing file : %s\n\n%s", fileName, m_errorString.c_str());
        }

        return xml;

    }
    else
    {
        BEHAVIAC_ASSERT(0, "Cannot open XML file : %s\n", fileName);
        return XmlNodeRef();
    }
}
Esempio n. 7
0
    behaviac::CMethodBase* Action::LoadMethod(const char* value_)
    {
        //Self.test_ns::AgentActionTest::Action2(0)
        char agentIntanceName[kNameLength];
        char agentClassName[kNameLength];
        char methodName[kNameLength];

        const char* pBeginP = ParseMethodNames(value_, agentIntanceName, agentClassName, methodName);

        //propertyName = FormatString("%s::%s", agentClassName, methodName);
        CStringID agentClassId(agentClassName);
        CStringID methodId(methodName);

        behaviac::CMethodBase* method = Agent::CreateMethod(agentClassId, methodId);

        if (!method)
        {
            BEHAVIAC_LOGWARNING("No Method %s::%s registered\n", agentClassName, methodName);
            BEHAVIAC_ASSERT(0, "No Method %s::%s registered\n", agentClassName, methodName);
        }
        else
        {
            //if (Agent::IsInstanceNameRegistered(agentIntanceName))
            {
                method->SetInstanceNameString(agentIntanceName, PT_INSTANCE);
            }

            BEHAVIAC_ASSERT(method, "No Method %s::%s registered", agentClassName, methodName);
            const char* params = pBeginP;

            BEHAVIAC_ASSERT(params[0] == '(');

            behaviac::vector<behaviac::string> tokens;

            {
                size_t len = strlen(params);

                BEHAVIAC_ASSERT(params[len - 1] == ')');

                behaviac::string text = behaviac::string(params + 1, len - 2);
                //behaviac::StringUtils::SplitIntoArray(text, ",", tokens);
                ParseForParams(text, tokens);
            }

            if (tokens.size() > 0)
            {
                XmlNodeRef xmlNode = CreateXmlNode("Method");

                for (uint32_t i = 0; i < tokens.size(); ++i)
                {
                    const behaviac::string& token = tokens[i];
					char attriName[1024];
					string_sprintf(attriName, "param%d", i + 1);
                    xmlNode->setAttr(attriName, token);
                }

                CTextNode node(xmlNode);
                method->LoadFromXML(0, node);
            }
        }

        return method;
    }
Esempio n. 8
0
void CScriptSystem::Warning(const char* message)
{
    BEHAVIAC_LOGWARNING(message);
}
Esempio n. 9
0
    bool Workspace::Load(const char* relativePath, bool bForce)
    {
        bool bOk = this->TryInit();
		if (!bOk)
		{
			//not init correctly
			return false;
		}

        //BEHAVIAC_ASSERT(behaviac::StringUtils::FindExtension(relativePath) == 0, "no extention to specify");
        BEHAVIAC_ASSERT(IsValidPath(relativePath));

        BehaviorTree* pBT = 0;
        BehaviorTrees_t::iterator it = m_behaviortrees.find(relativePath);

        if (it != m_behaviortrees.end())
        {
            if (!bForce)
            {
                return true;
            }

            pBT = it->second;
        }

        behaviac::string fullPath = StringUtils::CombineDir(this->GetFilePath(), relativePath);

        Workspace::EFileFormat f = this->GetFileFormat();

        switch (f)
        {
            case EFF_default:
            {
                // try to load the behavior in xml
                behaviac::string path = fullPath + ".xml";

                if (behaviac::CFileManager::GetInstance()->FileExists(path.c_str()))
                {
                    f = EFF_xml;
                    fullPath = path;
                }
                else
                {
                    // try to load the behavior in bson
                    path = fullPath + ".bson.bytes";

                    if (behaviac::CFileManager::GetInstance()->FileExists(path.c_str()))
                    {
                        f = EFF_bson;
                        fullPath = path;
                    }
                    // try to load the behavior in cpp
                    else
                    {
                        f = EFF_cpp;
                    }
                }
            }
            break;

            case EFF_xml:
                fullPath += ".xml";
                break;

            case EFF_bson:
                fullPath += ".bson.bytes";
                break;

            case EFF_cpp:
                break;

            default:
                BEHAVIAC_ASSERT(0);
                break;
        }

        bool bLoadResult = false;
        bool bNewly = false;

        if (!pBT)
        {
            //in case of circular referencebehavior
            bNewly = true;
            pBT = BEHAVIAC_NEW BehaviorTree();
            m_behaviortrees[relativePath] = pBT;
        }

        BEHAVIAC_ASSERT(pBT);

        bool bCleared = false;

        if (f == EFF_xml || f == EFF_bson)
        {
            char* pBuffer = ReadFileToBuffer(fullPath.c_str());

            if (pBuffer)
            {
                //if forced to reload
                if (!bNewly)
                {
                    bCleared = true;
                    pBT->Clear();
                }

                if (f == EFF_xml)
                {
                    bLoadResult = pBT->load_xml(pBuffer);
                }
                else
                {
                    bLoadResult = pBT->load_bson(pBuffer);
                }

                PopFileFromBuffer(pBuffer);
            }
            else
            {
                BEHAVIAC_LOGERROR("'%s' doesn't exist!, Please check the file name or override Workspace and its GetFilePath()\n", fullPath.c_str());
                BEHAVIAC_ASSERT(false);
            }
        }
        else if (f == EFF_cpp)
        {
            if (!bNewly)
            {
                bCleared = true;
                pBT->Clear();
            }

            if (m_behaviortreeCreators && m_behaviortreeCreators->find(relativePath) != m_behaviortreeCreators->end())
            {
                BehaviorTreeCreator_t btCreator = (*m_behaviortreeCreators)[relativePath];
                bLoadResult = (*btCreator)(pBT);
            }
            else
            {
                BEHAVIAC_ASSERT(0);
                BEHAVIAC_LOGWARNING("The behaviac_generated/behaviors/generated_behaviors.h should be included by one of your apps.");
            }
        }
        else
        {
            BEHAVIAC_ASSERT(0);
        }

        if (bLoadResult)
        {
            BEHAVIAC_ASSERT(pBT->GetName() == relativePath);

            if (!bNewly)
            {
                BEHAVIAC_ASSERT(m_behaviortrees[pBT->GetName()] == pBT);
            }
        }
        else
        {
            if (bNewly)
            {
                //if it is forced to reload
                m_behaviortrees.erase(relativePath);

                BEHAVIAC_DELETE(pBT);
            }
            else if (bCleared)
            {
                //it has been cleared but failed to load, to remove it
                m_behaviortrees.erase(relativePath);
            }

            BEHAVIAC_LOGWARNING("'%s' is not loaded!\n", fullPath.c_str());
        }

        return bLoadResult;
    }
Esempio n. 10
0
	bool Workspace::Load(const char* relativePath, bool bForce)
	{
		BEHAVIAC_ASSERT(behaviac::StringUtils::FindExtension(relativePath) == 0, "no extention to specify");
		BEHAVIAC_ASSERT(IsValidPath(relativePath));

		BehaviorTree* pBT = 0;
		BehaviorTrees_t::iterator it = ms_behaviortrees.find(relativePath);
		if (it != ms_behaviortrees.end())
		{
			if (!bForce) {
				return true;
			}
			pBT = it->second;
		}

		behaviac::string fullPath = ms_workspace_export_path;
		fullPath += relativePath;

		Workspace::EFileFormat f = Workspace::GetFileFormat();
		switch (f)
		{
			case EFF_default:
				{
					// try to load the behavior in xml
					behaviac::string path = fullPath + ".xml";
					if (CFileManager::GetInstance()->FileExists(path.c_str()))
					{
						f = EFF_xml;
						fullPath = path;
					}
					else
					{
						// try to load the behavior in bson
						path = fullPath + ".bson.bytes";
						if (CFileManager::GetInstance()->FileExists(path.c_str()))
						{
							f = EFF_bson;
							fullPath = path;
						}
						// try to load the behavior in cpp
						else
						{
							f = EFF_cpp;
						}
					}
				}
				break;

			case EFF_xml:
				fullPath += ".xml";
				break;

			case EFF_bson:
				fullPath += ".bson.bytes";
				break;

			case EFF_cpp:
				break;

			default:
				BEHAVIAC_ASSERT(0);
				break;
		}

		bool bLoadResult = false;
		bool bNewly = false;
		if (!pBT) {
			//in case of circular referencebehavior
			bNewly = true;
			pBT = BEHAVIAC_NEW BehaviorTree();
			ms_behaviortrees[relativePath] = pBT;
		}

		BEHAVIAC_ASSERT(pBT);

		bool bCleared = false;

		if (f == EFF_xml || f == EFF_bson)
		{
			char* pBuffer = ReadFileToBuffer(fullPath.c_str());
			if (pBuffer) {
				//if forced to reload
				if (!bNewly) {
					bCleared = true;
					pBT->Clear();
				}

				if (f == EFF_xml) {
					bLoadResult = pBT->load_xml(pBuffer);
				}
				else {
					bLoadResult = pBT->load_bson(pBuffer);
				}

				PopFileFromBuffer(pBuffer);
			}
			else {
				BEHAVIAC_LOGWARNING("Workspace::Load:FileNotOpen %s", fullPath.c_str());
			}
		}
		else if (f == EFF_cpp)
		{
			if (!bNewly) {
				bCleared = true;
				pBT->Clear();
			}

			if (ms_behaviortreeCreators && ms_behaviortreeCreators->find(relativePath) != ms_behaviortreeCreators->end())
			{
				BehaviorTreeCreator_t btCreator = (*ms_behaviortreeCreators)[relativePath];
				bLoadResult = (*btCreator)(pBT);
			}
			else {
				BEHAVIAC_ASSERT(0);
				BEHAVIAC_LOGWARNING("The generated_behaviors.cpp file should be included by the app.");
			}
		}
		else
		{
			BEHAVIAC_ASSERT(0);
		}

		if (bLoadResult) {
			BEHAVIAC_ASSERT(pBT->GetName() == relativePath);
			if (!bNewly) {
				BEHAVIAC_ASSERT(ms_behaviortrees[pBT->GetName()] == pBT);
			}
		}
		else {
			if (bNewly) {
				//if it is forced to reload
				ms_behaviortrees.erase(relativePath);

				BEHAVIAC_DELETE(pBT);
			}
			else if (bCleared) {
				//it has been cleared but failed to load, to remove it
				ms_behaviortrees.erase(relativePath);
			}

			BEHAVIAC_LOGWARNING("BehaviorTree %s not loaded!\n", fullPath.c_str());
		}

		return bLoadResult;
	}