void GuardSearch::Execute( BaseType::EntityType *entity ) { CMonster *pSelf = dynamic_cast<CMonster*>( entity->GetOwner() ); ShapeListType player_list; ShapeListType monster_list; CServerRegion *pRegion = static_cast<CServerRegion*>( pSelf->GetFather() ); CMoveShape *pTarget = NULL; if( CanAttackCurrent( pSelf, entity ) ) { return; } // find the around players pRegion->FindAroundObject( pSelf, TYPE_PLAYER, player_list ); // find the around monsters including the guards. pRegion->FindAroundObject( pSelf, TYPE_MONSTER, monster_list ); // filter the result Filter filter( static_cast<CMonster*>( pSelf ) ); filter_shape( player_list, filter ); filter_shape( monster_list, filter ); // retrieve the ai reference DWORD ai_ref = pSelf->GetAIReferrence( 0 ); if( ai_ref >= 0 ) { pTarget = SearchByAIRef1( pSelf, player_list, ai_ref ); } if( pTarget == NULL && ( ai_ref = pSelf->GetAIReferrence( 1 ) ) > 0 ) { pTarget = SearchByAIRef2( pSelf, player_list, ai_ref ); } if( pTarget == NULL && ( ai_ref = pSelf->GetAIReferrence( 2 ) ) > 0 ) { pTarget = SearchByAIRef3( pSelf, monster_list, ai_ref ); } if( pTarget == NULL && ( ai_ref = pSelf->GetAIReferrence( 3 ) ) > 0 ) { pTarget = SearchByAIRef4( pSelf, monster_list, ai_ref ); } if( pTarget != NULL ) { // search successfully entity->SetTarget( pTarget->GetExID(), pTarget->GetType() ); } else if( entity->HasTarget() ) { AI_EVENT_SENDER( entity ).ReturnPeace(); } }
bool MonsterAI::RandomRun() { CMonster *monster = static_cast<CMonster*>(GetOwner()); if (monster == NULL) { LogError(AI_MODULE, "MosterAI has no owner object..."); } assert(monster); if(random(10000) < monster->GetMoveRandomValue()) { long dir = 0; long curX = monster->GetTileX(); long curY = monster->GetTileY(); long dis = monster->Distance(curX, curY, m_BornPos.x, m_BornPos.y); if(dis > monster->GetPeaceMoveRange()) { dir = GetLineDir(curX, curY, m_BornPos.x, m_BornPos.y); long gdir = (8 - dir) % 8; if(gdir >= 2 || gdir <= 6) { dir = (dir + random(3) - 1 + 8) % 8; } } else { dir = random(8); } long maxRunTimes = monster->GetMaxRunTimes(); long minRunTimes = monster->GetMinRunTimes(); AI_EVENT_SENDER(this).MoveByStep(dir, random(maxRunTimes - minRunTimes) + minRunTimes); // drive the ai Resume(0); } else { Stand(monster->GetStopFrame()); } return true; }