char* Workspace::ReadFileToBuffer(const char* file) { IFile* fp = CFileManager::GetInstance()->FileOpen(file, CFileSystem::EOpenAccess_Read); if (!fp) { return 0; } //fp->Seek(0, CFileSystem::ESeekMoveMode_End); uint32_t fileSize = (uint32_t)fp->GetSize(); BEHAVIAC_ASSERT(ms_fileBufferTop < kFileBufferDepth - 1); uint32_t offset = ms_fileBufferOffset[ms_fileBufferTop++]; uint32_t offsetNew = offset + fileSize + 1; ms_fileBufferOffset[ms_fileBufferTop] = offsetNew; if (ms_fileBuffer == 0 || offsetNew > ms_fileBufferLength) { //to allocate extra 10k ms_fileBufferLength = offsetNew + 10 * 1024; if (ms_fileBufferLength < 50 * 1024) { ms_fileBufferLength = 50 * 1024; } ms_fileBuffer = (char*)BEHAVIAC_REALLOC(ms_fileBuffer, ms_fileBufferLength); } BEHAVIAC_ASSERT(ms_fileBuffer); BEHAVIAC_ASSERT(offsetNew < ms_fileBufferLength); char* pBuffer = ms_fileBuffer + offset; fp->Read(pBuffer, sizeof(char) * fileSize); pBuffer[fileSize] = 0; CFileManager::GetInstance()->FileClose(fp); return pBuffer; }
EBTStatus ActionTask::update(Agent* pAgent, EBTStatus childStatus) { BEHAVIAC_UNUSED_VAR(pAgent); BEHAVIAC_UNUSED_VAR(childStatus); BEHAVIAC_ASSERT(Action::DynamicCast(this->GetNode())); Action* pActionNode = (Action*)(this->GetNode()); EBTStatus result = pActionNode->Execute(pAgent, childStatus); return result; }
Mutex::Mutex() : _impl(0) { // uint32_t s = sizeof(MutexImpl); // printf("size of MutexImpl %d\n", s); // Be sure that the shadow is large enough BEHAVIAC_ASSERT(sizeof(m_Shadow) >= sizeof(MutexImpl)); // Use the shadow as memory space for the platform specific implementation _impl = (MutexImpl*)m_Shadow; pthread_mutex_init(&_impl->_mutex, 0); }
int CFunctionHandler::EndFunction(IScriptObject* pObj) { if (!pObj) { BEHAVIAC_ASSERT(!"CFunctionHandler::EndFunction - Invalid object reference"); lua_pushnil(m_pLS); return 0; } lua_xgetref(m_pLS, pObj->GetRef()); return 1; }
int ConnectorInterface::GetBufferIndex(bool bReserve) { #if BEHAVIAC_COMPILER_MSVC BEHAVIAC_ASSERT(t_packetBufferIndex != TLS_OUT_OF_INDEXES); uint32_t bufferIndex = (uint32_t)(uint64_t)TlsGetValue(t_packetBufferIndex); #else BEHAVIAC_ASSERT(t_packetBufferIndex != (unsigned int) - 1); uint32_t bufferIndex = t_packetBufferIndex; #endif //WHEN bReserve is false, it is unsafe to allocate memory as other threads might be allocating //you can avoid the following assert to malloc a block of memory in your thread at the very beginning BEHAVIAC_ASSERT(bufferIndex > 0 || bReserve); //bufferIndex initially is 0 if (bufferIndex <= 0 && bReserve) { bufferIndex = ReserveThreadPacketBuffer(); } return (int)bufferIndex; }
void CScriptSystem::CheckStackOnEndCall() { m_CurrentDeep = (m_CurrentDeep == 0) ? 0 : --m_CurrentDeep; int aLocal = lua_gettop(m_pLS); if (m_BeginStackTop != aLocal && m_CurrentDeep == 0) { BEHAVIAC_ASSERT(false && "Last invoked LUA script has corrupted the LUA stack (don't ignore all)"); // Set new top (even if corrupted) m_BeginStackTop = aLocal; } }
bool TaskTask::onenter(Agent* pAgent) { this->m_activeChildIndex = CompositeTask::InvalidChildIndex; BEHAVIAC_ASSERT(this->m_activeChildIndex == CompositeTask::InvalidChildIndex); Task* pMethodNode = (Task*)(this->GetNode()); #if BEHAVIAC_USE_HTN _planner->Init(pAgent, pMethodNode); #endif //BEHAVIAC_USE_HTN BEHAVIAC_UNUSED_VAR(pMethodNode); return super::onenter(pAgent); }
void PlannerTaskReference::onexit(Agent* pAgent, EBTStatus status) { BEHAVIAC_UNUSED_VAR(pAgent); BEHAVIAC_UNUSED_VAR(status); BEHAVIAC_ASSERT(ReferencedBehavior::DynamicCast(this->m_node) != 0); ReferencedBehavior* pNode = (ReferencedBehavior*)this->m_node; BEHAVIAC_UNUSED_VAR(pNode); BEHAVIAC_ASSERT(pNode != NULL); this->m_subTree = 0; #if !BEHAVIAC_RELEASE pAgent->LogReturnTree(pNode->GetReferencedTree()); #endif BEHAVIAC_ASSERT(this->currentState != NULL); this->currentState->Pop(); this->currentState = 0; }
Workspace* Workspace::GetInstance() { if (ms_instance == NULL) { //if new an Workspace class ,then the staict variable will be set value Workspace* _workspace = BEHAVIAC_NEW Workspace(); BEHAVIAC_UNUSED_VAR(_workspace); BEHAVIAC_ASSERT(ms_instance != NULL); } return ms_instance; }
void ReferencedBehavior::Attach(BehaviorNode* pAttachment, bool bIsPrecondition, bool bIsEffector, bool bIsTransition) { if (bIsTransition) { BEHAVIAC_ASSERT(!bIsEffector && !bIsPrecondition); if (this->m_transitions == 0) { this->m_transitions = BEHAVIAC_NEW behaviac::vector<Transition*>(); } BEHAVIAC_ASSERT(Transition::DynamicCast(pAttachment) != 0); Transition* pTransition = (Transition*)pAttachment; this->m_transitions->push_back(pTransition); return; } BEHAVIAC_ASSERT(bIsTransition == false); super::Attach(pAttachment, bIsPrecondition, bIsEffector, bIsTransition); }
void SingeChildTask::copyto(BehaviorTask* target) const { super::copyto(target); BEHAVIAC_ASSERT(SingeChildTask::DynamicCast(target)); SingeChildTask* ttask = (SingeChildTask*)target; if (this->m_root) { //referencebehavior/query, etc. if (!ttask->m_root) { const BehaviorNode* pNode = this->m_root->GetNode(); BEHAVIAC_ASSERT(BehaviorTree::DynamicCast(pNode)); ttask->m_root = pNode->CreateAndInitTask(); } BEHAVIAC_ASSERT(ttask->m_root); this->m_root->copyto(ttask->m_root); } }
void SingeChildTask::load(ISerializableNode* node) { super::load(node); if (this->m_status != BT_INVALID) { CSerializationID rootId("root"); ISerializableNode* rootNode = node->findChild(rootId); BEHAVIAC_ASSERT(rootNode); this->m_root->load(rootNode); } }
void Context::CleanupInstances() { //don't delete it as it is DestroyInstance's resposibity //for (Context::NamedAgents_t::iterator it = m_namedAgents.begin(); it != m_namedAgents.end(); ++it) //{ // Agent* pAgent = it->second; // BEHAVIAC_DELETE(pAgent); //} BEHAVIAC_ASSERT(m_namedAgents.size() == 0, "you need to call DestroyInstance or UnbindInstance"); m_namedAgents.clear(); }
bool CFileSystem::copyFile(const char* lpExistingFileName, const char* lpNewFileName, bool bFailIfExists) { BEHAVIAC_UNUSED_VAR(lpExistingFileName); BEHAVIAC_UNUSED_VAR(lpNewFileName); BEHAVIAC_UNUSED_VAR(bFailIfExists); BEHAVIAC_ASSERT(0); return false; }
EBTStatus IfElseTask::update(Agent* pAgent, EBTStatus childStatus) { BEHAVIAC_ASSERT(childStatus != BT_INVALID); BEHAVIAC_ASSERT(this->m_children.size() == 3); EBTStatus conditionResult = BT_INVALID; if (childStatus == BT_SUCCESS || childStatus == BT_FAILURE) { // if the condition returned running then ended with childStatus conditionResult = childStatus; } if (this->m_activeChildIndex == CompositeTask::InvalidChildIndex) { BehaviorTask* pCondition = this->m_children[0]; if (conditionResult == BT_INVALID) { // condition has not been checked conditionResult = pCondition->exec(pAgent); } if (conditionResult == BT_SUCCESS) { // if this->m_activeChildIndex = 1; } else if (conditionResult == BT_FAILURE) { // else this->m_activeChildIndex = 2; } } else { return childStatus; } if (this->m_activeChildIndex != CompositeTask::InvalidChildIndex) { BehaviorTask* pBehavior = this->m_children[this->m_activeChildIndex]; EBTStatus s = pBehavior->exec(pAgent); return s; } return BT_RUNNING; }
size_t Read(Handle& h, const void* buffer, size_t bytesMax) { size_t bytesRead = 0; if (bytesMax == 0 || !h) { return bytesRead; } fd_set readfds; FD_ZERO(&readfds); FD_SET(::AsWinSocket(h), &readfds); struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 100000;//0.1s int rv = ::select(1, &readfds, 0, 0, &tv); if (rv == -1) { BEHAVIAC_ASSERT(0); } else if (rv == 0) { //timeout } else { int res = ::recv(::AsWinSocket(h), (char*)buffer, (int)bytesMax, 0); if (res == SOCKET_ERROR) { int err = WSAGetLastError(); if (err == WSAECONNRESET || err == WSAECONNABORTED) { Close(h); } } else { bytesRead = res; gs_packetsReceived++; } return bytesRead; } return 0; }
void BehaviorNode::load_custom(int version, const char* agentType, BsonDeserizer& d) { d.OpenDocument(); BsonDeserizer::BsonTypes type = d.ReadType(); BEHAVIAC_UNUSED_VAR(type); //BEHAVIAC_ASSERT(type == BsonDeserizer.BsonTypes.BT_NodeElement); BEHAVIAC_ASSERT(type == BsonDeserizer::BT_NodeElement); d.OpenDocument(); BehaviorNode* pChildNode = this->load(agentType, d, version); this->m_customCondition = pChildNode; d.CloseDocument(false); d.CloseDocument(false); type = d.ReadType(); //BEHAVIAC_ASSERT(type == BsonDeserizer.BsonTypes.BT_None); BEHAVIAC_ASSERT(type == BsonDeserizer::BT_None); }
void CompositeTask::copyto(BehaviorTask* target) const { super::copyto(target); BEHAVIAC_ASSERT(CompositeTask::DynamicCast(target)); CompositeTask* ttask = (CompositeTask*)target; ttask->m_activeChildIndex = this->m_activeChildIndex; BEHAVIAC_ASSERT(this->m_children.size() > 0); BEHAVIAC_ASSERT(this->m_children.size() == ttask->m_children.size()); BehaviorTasks_t::size_type count = this->m_children.size(); for (BehaviorTasks_t::size_type i = 0; i < count; ++i) { BehaviorTask* childTask = this->m_children[i]; BehaviorTask* childTTask = ttask->m_children[i]; childTask->copyto(childTTask); } }
bool BsonDeserizer::OpenDocument() { const char* head = this->m_pPtr; uint32_t size = this->ReadInt32(); //uint16_t size = this->ReadUInt16(); const char* end = head + size - 1; if (*end == 0) { #if USE_DOCUMENET BEHAVIAC_ASSERT(this->m_documentStackTop >= 0 && this->m_documentStackTop < kDocumentStackMax - 1); this->m_documentStack[this->m_documentStackTop++] = this->m_document; this->m_document = head; #endif return true; } BEHAVIAC_ASSERT(0); return false; }
bool BehaviorNode::IsValid(Agent* pAgent, BehaviorTask* pTask) const { BEHAVIAC_UNUSED_VAR(pAgent); BEHAVIAC_UNUSED_VAR(pTask); #if !defined(BEHAVIAC_RELEASE) BEHAVIAC_ASSERT(!this->m_agentType.empty()); CStringID btAgentClass(this->m_agentType.c_str()); return pAgent->IsAKindOf(btAgentClass); #else return true; #endif//#if !defined(BEHAVIAC_RELEASE) }
int DecoratorWeight::GetWeight(behaviac::Agent* pAgent) const { if (this->m_weight_var) { BEHAVIAC_ASSERT(this->m_weight_var); TProperty<int>* pP = (TProperty<int>*)this->m_weight_var; uint64_t count = pP->GetValue(pAgent); return (count == ((uint64_t) - 1) ? -1 : (int)count); } return 0; }
bool ReferencedBehaviorTask::onevent(Agent* pAgent, const char* eventName) { if (this->m_status == BT_RUNNING && this->m_node->HasEvents()) { BEHAVIAC_ASSERT(this->m_subTree); if (!this->m_subTree->onevent(pAgent, eventName)) { return false; } } return true; }
EBTStatus PlannerTaskTask::update(Agent* pAgent, EBTStatus childStatus) { BEHAVIAC_UNUSED_VAR(childStatus); //BEHAVIAC_ASSERT(Task::Da); // Task* pNode = (Task*) this->m_node;// as Task; BEHAVIAC_ASSERT(this->m_children.size() == 1); BehaviorTask* c = this->m_children[0]; EBTStatus s = c->exec(pAgent); return s; }
int DecoratorCount::GetCount(Agent* pAgent) const { if (this->m_count_var) { BEHAVIAC_ASSERT(this->m_count_var); TProperty<int>* pP = (TProperty<int>*)this->m_count_var; uint64_t count = pP->GetValue(pAgent); return (count == ((uint64_t) - 1) ? -1 : (int)count); } return 0; }
void CScriptSystem::CheckStackOnBeginCall() { int aLocal = lua_gettop(m_pLS); if (m_CurrentDeep == 0) { BEHAVIAC_ASSERT(m_BeginStackTop == aLocal && "About to invoke a LUA script on a corrupted LUA stack (don't ignore all)"); // Set new top (even if corrupted) m_BeginStackTop = aLocal; } m_CurrentDeep++; }
bool CFunctionHandler::GetParam(int nIdx, float& f) const { int nRealIdx = nIdx + 1; BEHAVIAC_ASSERT(nIdx <= GetParamCount() && "CFunctionHandler::GetParam - (CODE/ERROR) Index out of range"); if (!lua_isnumber(m_pLS, nRealIdx)) { return false; } f = (float)lua_tonumber(m_pLS, nRealIdx); return true; }
EBTStatus TaskTask::update(Agent* pAgent, EBTStatus childStatus) { EBTStatus status = childStatus; if (childStatus == BT_RUNNING) { BEHAVIAC_ASSERT(Task::DynamicCast(this->GetNode()) != 0, "node is not an Method"); Task* pTaskNode = (Task*)(this->GetNode()); if (pTaskNode->IsHTN()) { #if BEHAVIAC_USE_HTN status = _planner->Update(); #endif //BEHAVIAC_USE_HTN } else { BEHAVIAC_ASSERT(this->m_children.size() == 1); BehaviorTask* c = this->m_children[0]; status = c->exec(pAgent); } } else { BEHAVIAC_ASSERT(true); } return status; }
bool CFunctionHandler::GetParam(int nIdx, CDynamicType*& ud) const { BEHAVIAC_ASSERT(nIdx <= GetParamCount() && "CFunctionHandler::GetParam - (CODE/ERROR) Index out of range"); USER_DATA_CHUNK* udc = (USER_DATA_CHUNK*)lua_touserdata(m_pLS, nIdx + 1); if (!udc) { return false; } ud = udc->userData; return true; }
bool IfElseTask::onenter(Agent* pAgent) { BEHAVIAC_UNUSED_VAR(pAgent); //reset it as it will be checked for the condition execution at the first time this->m_activeChildIndex = CompositeTask::InvalidChildIndex; if (this->m_children.size() == 3) { return true; } BEHAVIAC_ASSERT(false, "IfElseTask has to have three children: condition, if, else"); return false; }
bool Mutex::TryLock() { #if !BEHAVIAC_COMPILER_GCC_LINUX int rval = pthread_mutex_trylock(&_impl->_mutex); // valid returns are 0 (locked) and [EBUSY] BEHAVIAC_ASSERT(rval == 0 || rval == EBUSY, "critical_section::trylock: pthread_mutex_trylock failed"); bool gotlock = (rval == 0); return gotlock; #else return false; #endif }