void boss_sm::ReduceDistance::onUpdate(float dt) { __super::onUpdate(dt); if(!getController()->getTarget()){ boss_sm::PatrolState* next_state = new boss_sm::PatrolState(getController()); getController()->scheduleTransitionInNextFrame(next_state); } else if(getAI()->getHealth() <= getAI()->getMaxHealth()/2 && !getController()->getAngerMode() && getController()->getEnemyDistance() < getAI()->getMeleeRange()) { boss_sm::AttackAngerMode* next_state = new boss_sm::AttackAngerMode(getController()); getController()->scheduleTransitionInNextFrame(next_state); } else if(getController()->getEnemyDistance() < getAI()->getMeleeRange()) { boss_sm::AttackMelee* next_state = new boss_sm::AttackMelee(getController()); getController()->scheduleTransitionInNextFrame(next_state); } else if(getController()->getEnemyRunningAway() && getController()->getEnemyDistance() >= 10.f && getController()->getEnemyDistance() <= getAI()->getShootingRange()) { boss_sm::AttackFireball* next_state = new boss_sm::AttackFireball(getController()); getController()->scheduleTransitionInNextFrame(next_state); } else if (g_game->getTimeMs() - m_stateStartTime > 300) { boss_sm::ReduceDistance* next_state = new boss_sm::ReduceDistance(getController()); getController()->scheduleTransitionInNextFrame(next_state); } }
void Experiment2FSMActorController::onCreate() { getAI()->setSpeed(0.f); getAI()->lookAt(mkVec3::ZERO); scheduleTransitionInNextFrame(new experiment2_sm::GotoMine(this)); updateStateTransition(); }
SampleHTNActorController::SampleHTNActorController( ActorAI* ai ) : IActorController(ai) { m_planner = new HTN::Planner(); m_planner->init(getAI()->getHtnMethodsPath(),getAI()->getHtnOperatorsPath(),getAI()->getHtnGoalsPath()); //actions//////////////////////////////////////////////////////////////////////// m_actions["opAttackMelee"] = &SampleHTNActorController::actionAttackMelee; }
void BTActorController::onUpdate( float dt ) { updateWorldState(dt); BT::Status s = m_root->tick(m_bb); if(m_parser->isFileModified(getAI()->getBtTreePath()) && m_root->isTerminated()){ m_parser->parseXmlTree(getAI()->getBtTreePath(), getAI(), m_root); m_parser->parseAliases(m_bb); } }
void boss_sm::RevealAttacker::onEnter(State* prev_state) { __super::onEnter(prev_state); m_stateStartTime = g_game->getTimeMs(); ////////////////////////////////////////////////////////////////////////// getController()->setGotHit(false); getAI()->setSpeed(0.3f); size_t steps = 40; getAI()->startSmoothChangeDir(getController()->getAttackDir(), steps, 1500); }
//----------actions---------- bool SampleHTNActorController::actionAttackMelee(float duration){ if(!m_target) return false; getAI()->setSpeed(0.f); getAI()->runAnimation("Attack3",duration); getAI()->hitMelee(); return true; }
void BossFSMActorController::rotateToEnemy() { if(m_target) { size_t steps = 40; mkVec3 new_direction = m_target->getSimPos() - getAI()->getSimPos(); new_direction.normalise(); getAI()->startSmoothChangeDir(new_direction, steps, 500.f); getAI()->setSpeed(0.f); } }
void boss_sm::ExploreSpot::onEnter(State* prev_state) { __super::onEnter(prev_state); m_stateStartTime = g_game->getTimeMs(); ////////////////////////////////////////////////////////////////////////// if(getController()->getEnemyLastPos() != mkVec3::ZERO){ getController()->setEnemySeen(false); size_t steps = 40; mkVec3 new_direction = getController()->getEnemyLastPos() - getAI()->getSimPos(); new_direction.normalise(); getAI()->startSmoothChangeDir(new_direction, steps, 250); getAI()->setSpeed(1.f); } }
void boss_sm::AttackFireball::onEnter(State* prev_state) { __super::onEnter(prev_state); m_stateStartTime = g_game->getTimeMs(); ////////////////////////////////////////////////////////////////////////// Character* target = getController()->getTarget(); if(target) { getController()->rotateToEnemy(); getAI()->runAnimation("Attack1",600.f); getAI()->setSpeed(0.f); getController()->setEnemyRunningAway(false); } }
void boss_sm::AttackMelee::onEnter(State* prev_state) { __super::onEnter(prev_state); m_stateStartTime = g_game->getTimeMs(); ////////////////////////////////////////////////////////////////////////// getAI()->setSpeed(0.f); Character* target = getController()->getTarget(); if(target) { getController()->rotateToEnemy(); getAI()->runAnimation("Attack3",1100.f); getController()->setAngerMode(true); } }
void SampleHTNActorController::onCreate(){ getAI()->setDirection(mkVec3::UNIT_Z); m_planner->setStateBool("IsEnemyVisible",false); Ogre::LogManager::getSingleton().logMessage("HTN controller created!"); }
void experiment2_sm::GotoMine::onEnter(State* prev_state) { __super::onEnter(prev_state); m_stateStartTime = g_game->getTimeMs(); size_t steps = 40; mkVec3 new_direction = getController()->getMinePosition() - getAI()->getSimPos(); new_direction.normalise(); getAI()->startSmoothChangeDir(new_direction, steps, 500.f); getAI()->setSpeed(0.5f); RayCastResult ray_result = getAI()->raycast(new_direction, 0.1f, 0.5f); if (ray_result.hit && ray_result.collision_type == RayCastResult::Environment) getAI()->jump(); }
void boss_sm::PatrolState::onEnter(State* prev_state) { __super::onEnter(prev_state); m_stateStartTime = g_game->getTimeMs(); ////////////////////////////////////////////////////////////////////////// mkVec3 new_direction = getRandomHorizontalDir(); RayCastResult ray_result = getAI()->raycast(new_direction, 1.0f, 5.f); while(ray_result.hit && ray_result.collision_type == RayCastResult::Environment){ new_direction = getRandomHorizontalDir(); ray_result = getAI()->raycast(new_direction, 1.0f, 5.f); } size_t steps = 40; getAI()->startSmoothChangeDir(new_direction, steps, 1500); getAI()->setSpeed(0.5f); }
void BTActorController::updateWorldState(float dt) { if(m_resetAngerMode){ m_bb->setStateBool("IsActorAM",false); m_resetAngerMode = false; } if(m_attackDir != mkVec3::ZERO){ m_bb->setStateBool("IsEnemyAttack",true); m_bb->setStateVec3("AttackDir",m_attackDir); m_attackDir = mkVec3::ZERO; } m_bb->setStateFloat("ActorHealth", getAI()->getHealth()); m_target = getAI()->findClosestEnemyInSight(); if(m_target){ float angleDiff = (float)(getAI()->getCharToEnemyAngle(m_target->getSimPos()).valueDegrees()); m_bb->setStateFloat("EnemyDgrDiff", angleDiff); m_bb->setStateBool("IsEnemySeen",true); m_bb->setStateVec3("LastEnemySpot",m_target->getSimPos()); m_enemySpot = m_target->getSimPos(); m_bb->setStateVec3("EnemyPos", m_target->getSimPos()); m_bb->setStateBool("IsEnemyVisible", true); float enemyDistance = (float)(m_target->getSimPos() - getAI()->getSimPos()).length(); m_bb->setStateFloat("EnemyDistance", enemyDistance); m_prevDistSum += enemyDistance - m_prevEnemyDist; m_prevEnemyDist = enemyDistance; m_prevEnemyDistTime = m_prevEnemyDistTime - dt; if(m_prevEnemyDistTime <= 0.f){ if(m_prevDistSum > 1.f) m_bb->setStateBool("IsEnemyRunningAway",true); else m_bb->setStateBool("IsEnemyRunningAway",false); m_prevDistSum = 0.f; m_prevEnemyDistTime = 0.5f; } } else { m_bb->setStateVec3("EnemyPos", mkVec3::ZERO); m_bb->setStateBool("IsEnemyVisible", false); m_bb->setStateFloat("EnemyDistance", 10000.f); m_bb->setStateFloat("EnemyDgrDiff", 0.f); } }
void boss_sm::AttackFireball::onLeave(State* next_state) { Character* target = getController()->getTarget(); if(target) { getAI()->hitFireball(target->getSimPos()); } }
void boss_sm::ReduceDistance::onEnter(State* prev_state) { __super::onEnter(prev_state); m_stateStartTime = g_game->getTimeMs(); ////////////////////////////////////////////////////////////////////////// Character* target = getController()->getTarget(); if(target) { RayCastResult ray_result = getAI()->raycast(target->getSimPos(), 0.1f, 1.f); if (ray_result.hit && ray_result.collision_type == RayCastResult::Environment) getAI()->jump(); mkVec3 destDir = (target->getSimPos()-getAI()->getSimPos()).normalisedCopy(); size_t steps = 40; getAI()->startSmoothChangeDir(destDir, steps, 300); getAI()->setSpeed(1.f); } }
void SGSHero::oneAIMove(SGSTerrain* terrain) { HERO_AI hero_ai = getAI(); SGSStrategy* strategy = SGSStrategy::createStrategy(hero_ai, terrain); bool ret = strategy->oneMove(this); if (ret) { setActive(false); } }
void experiment2_sm::BuyLantern::onEnter(State* prev_state) { __super::onEnter(prev_state); m_stateStartTime = g_game->getTimeMs(); getAI()->setSpeed(0.f); getController()->setLantern(true); float gold = getController()->getNpcGold() - 600.f; getController()->setNpcGold(gold); }
void Piece::playPiece(){ auto par = dynamic_cast<HorseshoeMain*>(this->getParent()); if(par->isConnected(position)){ auto ai = par->getAI(); bool p = UserDefault::getInstance()->getBoolForKey("Player"); auto audio = CocosDenshion::SimpleAudioEngine::getInstance(); if(UserDefault::getInstance()->getBoolForKey("Sound",true)) audio->playEffect("sfx/Move.wav"); Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); int x = par->empty; par->empty = position; position = x; Vec2 np; switch(position){ case 1: np = Vec2(origin.x + visibleSize.width/2 - 180, origin.y + 2*visibleSize.height/5 + 180); break; case 2: np = Vec2(origin.x + visibleSize.width/2 + 180, origin.y + 2*visibleSize.height/5 + 180); break; case 3: np = Vec2(origin.x + visibleSize.width/2, origin.y + 2*visibleSize.height/5); break; case 4: np = Vec2(origin.x + visibleSize.width/2 - 180, origin.y + 2*visibleSize.height/5 - 180); break; case 5: np = Vec2(origin.x + visibleSize.width/2 + 180, origin.y + 2*visibleSize.height/5 - 180); break; } auto move = MoveTo::create(0.4f, np); auto scaleup = ScaleTo::create(0.2f,1.4f); auto scaledown = ScaleTo::create(0.2f,1); auto scale = Sequence::createWithTwoActions(scaleup,scaledown); auto act = Spawn::createWithTwoActions(scale,move); this->runAction(act); CCLOG("%d to %d", par->empty, position); turn=!turn; if(par->checkEnd(turn)){ ended = true; par->setBGColor(Color3B(57,120,63)); if(UserDefault::getInstance()->getBoolForKey("Sound",true)){ if(ai&&turn!=p) audio->playEffect("sfx/Win.wav"); else audio->playEffect("sfx/Lose.wav"); } } else if(!turn) par->setBGColor(Color3B(156,48,47)); else par->setBGColor(Color3B(56,92,120)); if(ai&&turn!=p) par->playAI(); } }
void SampleHTNActorController::onUpdate(float dt){ updateWorldState(dt); std::vector<HTN::pTask> plan = m_planner->getPlan(getAI()->getHtnMethodsPath(),getAI()->getHtnOperatorsPath(),getAI()->getHtnGoalsPath()); HTN::pOperator newTask; HTN::PlanResult result = m_planner->resolvePlan(plan, dt, newTask); switch (result) { case HTN::PLAN_EMPTYPLAN: getAI()->setSpeed(0.f); break; case HTN::PLAN_INTERRUPTED: getAI()->stopSmoothChangeDir(); getAI()->stopAnimation(); case HTN::PLAN_NEW: executeTask(newTask); break; case HTN::PLAN_RUNNING: break; } }
const ConnectFour::Player& ConnectFour::Player::getOpposite() const { switch(m_type) { case PLAYER_TYPE_AI: return getHuman(); case PLAYER_TYPE_HUMAN: return getAI(); case PLAYER_TYPE_UNKNOWN: default: return getInvalid(); } }
void experiment2_sm::GotoMine::onUpdate(float dt) { __super::onUpdate(dt); if((getAI()->getSimPos()-getController()->getMinePosition()).length() <= SPOT_RADIUS){ experiment2_sm::DigGold* next_state = new experiment2_sm::DigGold(getController()); getController()->scheduleTransitionInNextFrame(next_state); } else if (g_game->getTimeMs() - m_stateStartTime > 500) { experiment2_sm::GotoMine* next_state = new experiment2_sm::GotoMine(getController()); getController()->scheduleTransitionInNextFrame(next_state); } }
// All SimObjects allow the script writer to create member variables on the fly using // the object Id, so we need this function to get the Id of the AI object. const char * AIPlugin::onFetchObject(int argc, const char * * argv ) { if( argc > 1 ) { AIObj * ai = getAI( argv[1] ); if( ai ) return intToStr( ai->getId() ); else console->printf( "Could not find drone %s. Usage:", argv[1] ); } console->printf( "%s(<ai name>);", argv[0] ); console->printf( "Get an object id handle (for creating member variables)." ); return neg1Txt; }
void BossFSMActorController::updateWorldState() { m_target = getAI()->findClosestEnemyInSight(); if(m_target) { m_enemyLastPos = m_target->getSimPos(); m_enemySeen = true; m_angleDiff = (float)(getAI()->getCharToEnemyAngle(m_target->getSimPos()).valueDegrees()); m_enemyDistance = (float)(m_target->getSimPos() - getAI()->getSimPos()).length(); m_prevDistSum += m_enemyDistance - m_prevEnemyDist; m_prevEnemyDist = m_enemyDistance; if(g_game->getTimeMs()-m_prevEnemyDistTime > 500.f){ if(m_prevDistSum > 1.f) m_enemyRunningAway = true; else m_enemyRunningAway = false; m_prevDistSum = 0.f; m_prevEnemyDistTime = g_game->getTimeMs(); } } }
rett * createAny(std::string dllname, std::string methodName) { char temp[50]; rett * ret=NULL; rett*(*getAI)(); void(*getName)(char*); #ifdef _WIN32 HINSTANCE dll = LoadLibraryA(dllname.c_str()); if (dll) { getName = (void(*)(char*))GetProcAddress(dll,"GetAiName"); getAI = (rett*(*)())GetProcAddress(dll,methodName.c_str()); } #else void *dll = dlopen(dllname.c_str(), RTLD_LOCAL | RTLD_LAZY); if (dll) { getName = (void(*)(char*))dlsym(dll,"GetAiName"); getAI = (rett*(*)())dlsym(dll,methodName.c_str()); } else tlog0 << "Error: " << dlerror() <<"\n"; #endif if (!dll) { tlog1 << "Cannot open dynamic library ("<<dllname<<"). Throwing..."<<std::endl; throw std::runtime_error("Cannot open dynamic library"); } else if(!getName || !getAI) { tlog1 << dllname << " does not export method " << methodName << std::endl; #ifdef _WIN32 FreeLibrary(dll); #else dlclose(dll); #endif throw std::runtime_error("Cannot find method " + methodName); } getName(temp); tlog0 << "Loaded " << temp << std::endl; ret = getAI(); if(!ret) tlog1 << "Cannot get AI!\n"; return ret; }
void experiment2_sm::DigGold::onEnter(State* prev_state) { __super::onEnter(prev_state); m_stateStartTime = g_game->getTimeMs(); getAI()->setSpeed(0.f); float mult = 1.f; mult += getController()->hasPickaxe() == true ? 1.f : 0.f; mult += getController()->hasHelmet() == true ? 1.f : 0.f; mult += getController()->hasLantern() == true ? 1.f : 0.f; float gold = getController()->getNpcGold(); gold += 10.f * mult; getController()->setNpcGold(gold); std::stringstream ss; ss << getController()->getNpcGold(); Ogre::LogManager::getSingleton().logMessage(ss.str()); if(!getController()->hasPickaxe()){ getAI()->runAnimation("Attack1",1000); } else { getAI()->runAnimation("Attack3",1000); } }
// If the given AI has a target that it is currently going after, return its // index, else -1. const char * AIPlugin::onGetTarget(int argc, const char * * argv ) { if( argc > 1 ) { AIObj * ai = getAI( argv[1] ); if( ai ) return intToStr( ai->curTargetId() ); else console->printf(" Could not find drone %s.", argv[1] ); } console->printf("AI::GetTarget(<ai name>);"); console->printf("If the given AI is currently attacking one of its targets, the rep" ); console->printf("id of that target is returned, else -1 is returned." ); return falseTxt; }
void BTActorController::onCreate() { getAI()->lookAt(mkVec3::ZERO); m_bb->setStateFloat("rngFbMax", getAI()->getShootingRange()); m_bb->setStateFloat("rngMelee", getAI()->getMeleeRange()); m_bb->setStateFloat("HealthAMLimit", getAI()->getMaxHealth()/2); m_bb->setStateFloat("dmgConeSize", getAI()->getMeleeConeSize()); //Ogre::LogManager::getSingleton().logMessage(getAI()->getBtTreePath()); m_parser->parseXmlTree(getAI()->getBtTreePath(), getAI(), m_root); m_parser->parseAliases(m_bb); Ogre::LogManager::getSingleton().logMessage("BT controller created!"); }
const char * AIPlugin::onGetId(int argc, const char * * argv ) { if( argc > 1 ) { AIObj * ai = getAI( argv[1] ); if( ai ) return intToStr( ai->repId ); else console->printf(" Could not find drone %s.", argv[1] ); } console->printf("AI::getId(<ai name>);"); console->printf("Returns the rep Id of the given AI. The rep Id can in many" ); console->printf("cases be used like a client Id (in those console functions that" ); console->printf("take a client Id)." ); return falseTxt; }
void boss_sm::ExploreSpot::onUpdate(float dt) { __super::onUpdate(dt); if(getController()->getTarget()) { boss_sm::ReduceDistance* next_state = new boss_sm::ReduceDistance(getController()); getController()->scheduleTransitionInNextFrame(next_state); } else if(getController()->getGotHit()) { boss_sm::RevealAttacker* next_state = new boss_sm::RevealAttacker(getController()); getController()->scheduleTransitionInNextFrame(next_state); } else if(getController()->getEnemyLastPos() != mkVec3::ZERO){ if((getAI()->getSimPos() - getController()->getEnemyLastPos()).length() < 3.f){ boss_sm::PatrolState* next_state = new boss_sm::PatrolState(getController()); getController()->scheduleTransitionInNextFrame(next_state); } } }