MCPcopy Create free account
hub / github.com/OpenDungeons/OpenDungeons / handleDefense

Method handleDefense

source/ai/KeeperAI.cpp:646–699  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

644}
645
646void KeeperAI::handleDefense()
647{
648 if(mCooldownDefense > 0)
649 {
650 --mCooldownDefense;
651 return;
652 }
653 mCooldownDefense = Random::Int(mCooldownDefenseMin, mCooldownDefenseMax);
654
655 Seat* seat = mPlayer.getSeat();
656 // We drop creatures nearby owned or allied attacked creatures
657 std::vector<Creature*> creatures = mGameMap.getCreaturesByAlliedSeat(seat);
658 for(Creature* creature : creatures)
659 {
660 // We check if a creature is fighting near a claimed tile. If yes, we drop a creature nearby
661 if(!creature->isActionInList(CreatureActionType::fight))
662 continue;
663
664 Tile* tile = creature->getPositionTile();
665 if(tile == nullptr)
666 continue;
667
668 // We look for a healthy creature not already fighting
669 Creature* creatureToDrop = nullptr;
670 for(Creature* creature2 : mGameMap.getCreaturesBySeat(seat))
671 {
672 if(creature2->getDefinition()->isWorker())
673 continue;
674 if(creature2->getHP() < (creature2->getMaxHp() * 0.5))
675 continue;
676 if(creature2->isActionInList(CreatureActionType::fight))
677 continue;
678 if((creatureToDrop != nullptr) && (creatureToDrop->getLevel() >= creature2->getLevel()))
679 continue;
680
681 creatureToDrop = creature2;
682 }
683 if(creatureToDrop == nullptr)
684 continue;
685
686 if(!creatureToDrop->tryPickup(seat))
687 continue;
688
689 for(Tile* neigh : tile->getAllNeighbors())
690 {
691 if(creatureToDrop->tryDrop(seat, neigh))
692 {
693 mPlayer.pickUpEntity(creatureToDrop);
694 mPlayer.dropHand(neigh);
695 return;
696 }
697 }
698 }
699}
700
701bool KeeperAI::handleWorkers()
702{

Callers

nothing calls this directly

Calls 15

IntFunction · 0.85
isActionInListMethod · 0.80
getPositionTileMethod · 0.80
getCreaturesBySeatMethod · 0.80
isWorkerMethod · 0.80
getDefinitionMethod · 0.80
getMaxHpMethod · 0.80
getLevelMethod · 0.80
pickUpEntityMethod · 0.80
dropHandMethod · 0.80
getSeatMethod · 0.45

Tested by

no test coverage detected