void ReferencedBehavior::load(int version, const char* agentType, const properties_t& properties)
    {
        super::load(version, agentType, properties);

        for (propertie_const_iterator_t it = properties.begin(); it != properties.end(); ++it)
        {
            const property_t& p = (*it);

            if (strcmp(p.name, "ReferenceFilename") == 0)
            {
                this->m_referencedBehaviorPath = p.value;

				BehaviorTree* behaviorTree = Workspace::GetInstance()->LoadBehaviorTree(this->m_referencedBehaviorPath.c_str());
                BEHAVIAC_ASSERT(behaviorTree);

				if (behaviorTree)
				{
					this->m_bHasEvents |= behaviorTree->HasEvents();
				}
            }
            else if (strcmp(p.name, "Task") == 0)
            {
                BEHAVIAC_ASSERT(!StringUtils::IsNullOrEmpty(p.value));
                CMethodBase* m = Action::LoadMethod(p.value);
                //BEHAVIAC_ASSERT(m is CTaskMethod);

                this->m_taskMethod = (CTaskMethod*)m;
            }
            else
            {
                //BEHAVIAC_ASSERT(0, "unrecognised property %s", p.name);
            }
        }
    }
Exemple #2
0
    bool QueryTask::ReQuery(Agent* pAgent)
    {
        const Query* pQueryNode = Query::DynamicCast(this->GetNode());

        if (pQueryNode)
        {
            const Query::Descriptors_t& qd = pQueryNode->GetDescriptors();

            if (qd.size() > 0)
            {
                const Workspace::BehaviorTrees_t& bs = Workspace::GetInstance()->GetBehaviorTrees();

                BehaviorTree* btFound = 0;
                float similarityMax = -1.0f;

                for (Workspace::BehaviorTrees_t::const_iterator it = bs.begin();
                     it != bs.end(); ++it)
                {
                    BehaviorTree* bt = it->second;

                    const behaviac::string& domains = bt->GetDomains();

                    if (pQueryNode->m_domain.empty() || domains.find(pQueryNode->m_domain) != behaviac::string::npos)
                    {
                        const BehaviorTree::Descriptors_t& bd = bt->GetDescriptors();

                        float similarity = pQueryNode->ComputeSimilarity(qd, bd);

                        if (similarity > similarityMax)
                        {
                            similarityMax = similarity;
                            btFound = bt;
                        }
                    }
                }

                if (btFound)
                {
                    //BehaviorTreeTask* pAgentCurrentBT = pAgent->btgetcurrent();
                    //if (pAgentCurrentBT && pAgentCurrentBT->GetName() != btFound->GetName())
                    {
                        const char* pReferencedTree = btFound->GetName().c_str();
                        pAgent->btreferencetree(pReferencedTree);

                        return true;
                    }
                }
            }
        }

        return false;
    }
Exemple #3
0
    Task* ReferencedBehavior::RootTaskNode()
    {
        if (this->m_taskNode == 0)
        {
            BehaviorTree* bt = Workspace::GetInstance()->LoadBehaviorTree(this->m_referencedBehaviorPath.c_str());

            if (bt != 0 && bt->GetChildrenCount() == 1)
            {
                BehaviorNode* root = (BehaviorNode*)bt->GetChild(0);
                this->m_taskNode = (Task*)root;
            }
        }

        return this->m_taskNode;
    }
Exemple #4
0
    void BehaviorTreeTask::Save(ISerializableNode* node) const
    {
        CSerializationID  btId("BehaviorTree");
        ISerializableNode* btNodeRoot = node->newChild(btId);

        BEHAVIAC_ASSERT(BehaviorTree::DynamicCast(this->GetNode()));
        BehaviorTree* bt = (BehaviorTree*)this->GetNode();

        CSerializationID  sourceId("source");
        btNodeRoot->setAttr(sourceId, bt->GetName());

        CSerializationID  nodeId("node");
        ISerializableNode* btNode = btNodeRoot->newChild(nodeId);

        this->save(btNode);
    }
Exemple #5
0
	EBTStatus BehaviorTreeTask::update_current(Agent* pAgent, EBTStatus childStatus)
	{
		BEHAVIAC_ASSERT(this->m_node != 0);
		BEHAVIAC_ASSERT(BehaviorTree::DynamicCast(this->m_node) != 0);
		BehaviorTree* tree = (BehaviorTree*)this->m_node;

		EBTStatus status = BT_RUNNING;

		if (tree->IsFSM())
		{
			status = this->update(pAgent, childStatus);
		}
		else
		{
			status = super::update_current(pAgent, childStatus);
		}

		return status;
	}
Exemple #6
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;
    }
Exemple #7
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;
	}