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

Method getWorkerPreferredActions

source/game/Player.cpp:834–888  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

832}
833
834std::vector<CreatureActionType> Player::getWorkerPreferredActions(Creature& worker) const
835{
836 std::vector<CreatureActionType> ret;
837 // We want to have more or less 40% workers digging, 40% claiming ground tiles and 20% claiming wall tiles
838 // Concerning carrying stuff, most workers should try unless more than 20% are already carrying.
839 uint32_t nbWorkersDigging = getNbWorkersDoing(CreatureActionType::searchTileToDig);
840 uint32_t nbWorkersClaimingGround = getNbWorkersDoing(CreatureActionType::searchGroundTileToClaim);
841 uint32_t nbWorkersClaimingWall = getNbWorkersDoing(CreatureActionType::searchWallTileToClaim);
842 uint32_t nbWorkersCarrying = getNbWorkersDoing(CreatureActionType::searchEntityToCarry);
843 // For the total number of workers, we consider only those doing something in the wanted list (and not
844 // the ones fighting or having nothing to do) + the one we are considering
845 uint32_t nbWorkersTotal = nbWorkersDigging + nbWorkersClaimingGround
846 + nbWorkersClaimingWall + nbWorkersCarrying + 1;
847
848 double percent;
849 bool isCarryAdded = false;
850 percent = static_cast<double>(nbWorkersCarrying) / static_cast<double>(nbWorkersTotal);
851 if(percent <= 0.2)
852 {
853 isCarryAdded = true;
854 ret.push_back(CreatureActionType::searchEntityToCarry);
855 }
856
857 bool isClaimWallAdded = false;
858 percent = static_cast<double>(nbWorkersDigging + nbWorkersClaimingGround) / static_cast<double>(nbWorkersTotal);
859 if(percent > 0.8)
860 {
861 isClaimWallAdded = true;
862 ret.push_back(CreatureActionType::searchWallTileToClaim);
863 }
864
865 bool digTileFirst = false;
866 if(nbWorkersDigging < nbWorkersClaimingGround)
867 digTileFirst = true;
868 else if(nbWorkersDigging == nbWorkersClaimingGround)
869 digTileFirst = (Random::Uint(0,1) == 0);
870
871 if(digTileFirst)
872 {
873 ret.push_back(CreatureActionType::searchTileToDig);
874 ret.push_back(CreatureActionType::searchGroundTileToClaim);
875 }
876 else
877 {
878 ret.push_back(CreatureActionType::searchGroundTileToClaim);
879 ret.push_back(CreatureActionType::searchTileToDig);
880 }
881
882 if(!isClaimWallAdded)
883 ret.push_back(CreatureActionType::searchWallTileToClaim);
884 if(!isCarryAdded)
885 ret.push_back(CreatureActionType::searchEntityToCarry);
886
887 return ret;
888}

Callers 1

handleIdleActionMethod · 0.80

Calls 1

UintFunction · 0.85

Tested by

no test coverage detected