Hooks
static bool BeforeCvGameQueries_GetLeastAdvancedUnitClassNobodyHas(bool bUseRandom, UnitClassTypes& result);static void AfterCvGameQueries_GetLeastAdvancedUnitClassNobodyHas(bool bUseRandom, UnitClassTypes& result);static UnitClassTypes ReplaceCvGameQueries_GetLeastAdvancedUnitClassNobodyHas(UnitClassTypes (*original)(bool), bool bUseRandom);CivRegisterBefore<CivHooks::CvGameQueries_GetLeastAdvancedUnitClassNobodyHas>(host, &BeforeCvGameQueries_GetLeastAdvancedUnitClassNobodyHas);CivRegisterAfter<CivHooks::CvGameQueries_GetLeastAdvancedUnitClassNobodyHas>(host, &AfterCvGameQueries_GetLeastAdvancedUnitClassNobodyHas);CivRegisterReplace<CivHooks::CvGameQueries_GetLeastAdvancedUnitClassNobodyHas>(host, &ReplaceCvGameQueries_GetLeastAdvancedUnitClassNobodyHas);
Code
22UnitClassTypes CvGameQueries::GetLeastAdvancedUnitClassNobodyHas(bool bUseRandom /* = false */)23{24 CvWeightedVector<int, SAFE_ESTIMATE_NUM_UNITS, true> UnitClassesVector;25 int iWeight;2627 bool bValid;2829 for (int i = 0; i < GC.getNumUnitClassInfos(); i++)30 {31 const UnitClassTypes eUnitClass = static_cast<UnitClassTypes>(i);32 CvUnitClassInfo* pkUnitClassInfo = GC.getUnitClassInfo(eUnitClass);33 if(pkUnitClassInfo)34 {35 const UnitTypes eDefaultUnit = (UnitTypes) pkUnitClassInfo->getDefaultUnitIndex();3637 // If the UnitClass doesn't have a Default Unit available to everyone, skip it38 if (eDefaultUnit == NO_UNIT)39 {40 continue;41 }4243 CvUnitEntry* pkUnitInfo = GC.getUnitInfo(eDefaultUnit);44 if(pkUnitInfo == NULL)45 {46 continue;47 }4849 // Only look at Land Units50 if (pkUnitInfo->GetDomainType() != DOMAIN_LAND)51 {52 continue;53 }54 // Only look at Military Units55 if (pkUnitInfo->GetCombat() <= 0)56 {57 continue;58 }5960 const TechTypes ePrereqTech = (TechTypes) pkUnitInfo->GetPrereqAndTech();6162 // Unit has to require a Tech, otherwise it's too easy!63 if (ePrereqTech == NO_TECH)64 {65 continue;66 }6768 CvTechEntry* pkTechInfo = GC.getTechInfo(ePrereqTech);69 if(pkTechInfo == NULL)70 {71 continue;72 }7374 bValid = true;75 bool bAllPlayersHaveTech = true;7677 for (int iMajorLoop = 0; iMajorLoop < MAX_MAJOR_CIVS; iMajorLoop++)78 {79 if (GET_PLAYER((PlayerTypes) iMajorLoop).isAlive())80 {81 // If a player already has one of this Unit, throw it out - we want something more advanced82 if (GET_PLAYER((PlayerTypes) iMajorLoop).getUnitClassCount(eUnitClass) > 0)83 {84 bValid = false;85 break;86 }8788 // Even if no one has this Unit lying around, if all players already have the Tech which unlocks it, throw it out - too easy89 if (bAllPlayersHaveTech && !GET_TEAM((TeamTypes) GET_PLAYER((PlayerTypes) iMajorLoop).getTeam()).GetTeamTechs()->HasTech(ePrereqTech))90 {91 bAllPlayersHaveTech = false;92 }93 }94 }95 if (!bValid || bAllPlayersHaveTech)96 {97 continue;98 }99100 iWeight = pkUnitInfo->GetPower();101102 // Add a Tech factor, since we want something in the near future, not too far off103 iWeight += (pkTechInfo->GetResearchCost() / 8);104105 // Add a random bit so that the same Unit isn't ALWAYS picked106 if (bUseRandom)107 {108 iWeight += GC.getGame().getJonRandNum(iWeight / 10, "Minor Civ Quest: Gift Unit - Adding random weight to Unit Chosen");109 }110111 UnitClassesVector.push_back(i, iWeight);112 }113 }114115 UnitClassesVector.SortItems();116117 //int iValue;118119 bValid = true;120121 for (int i = UnitClassesVector.size() - 1; i >= 0; i--)122 {123 const UnitClassTypes eUnitClass = (UnitClassTypes) UnitClassesVector.GetElement(i);124125 // First Unit we find in this sorted Vector is our guy126 if (bValid)127 {128 return eUnitClass;129 }130 }131132 return NO_UNITCLASS;133}