// calculate the recursive prerequisites for an Action, storing them in allPre // assumes that prerequisites have already been calculated void ActionTypeData::CalculateRecursivePrerequisites(PrerequisiteSet & allPre, const ActionTypeData & action) { PrerequisiteSet pre = action.getPrerequisites(); if (action.gasPrice() > 0) { if (action.raceID == Races::Protoss) { pre.add(ActionType(Races::Protoss, GetActionID(BWAPI::UnitTypes::Protoss_Assimilator))); } else if (action.raceID == Races::Terran) { pre.add(ActionType(Races::Terran, GetActionID(BWAPI::UnitTypes::Terran_Refinery))); } if (action.raceID == Races::Zerg) { pre.add(ActionType(Races::Zerg, GetActionID(BWAPI::UnitTypes::Zerg_Extractor))); } } for (size_t a(0); a < pre.size(); ++a) { const ActionType & actionType = pre.getActionType(a); if (!allPre.contains(actionType)) { allPre.add(actionType); CalculateRecursivePrerequisites(allPre, GetActionTypeData(actionType.getRace(), actionType.ID())); } } }
void Tools::CalculatePrerequisitesRequiredToBuild(const GameState & state, const PrerequisiteSet & needed, PrerequisiteSet & added) { // if anything needed gas and we don't have a refinery, we need to add one PrerequisiteSet allNeeded(needed); const ActionType & refinery = ActionTypes::GetRefinery(state.getRace()); if (!needed.contains(refinery) && (state.getUnitData().getNumCompleted(refinery) == 0) && !added.contains(refinery)) { for (size_t n(0); n<needed.size(); ++n) { if (needed.getActionType(n).gasPrice() > 0) { allNeeded.add(refinery); break; } } } for (size_t n(0); n<allNeeded.size(); ++n) { const ActionType & neededType = allNeeded.getActionType(n); // if we already have the needed type completed we can skip it if (added.contains(neededType) || state.getUnitData().getNumCompleted(neededType) > 0) { } // if we have the needed type in progress we can add that time else if (state.getUnitData().getNumInProgress(neededType) > 0) { //added.add(neededType); } // otherwise we need to recurse on the needed type to build its prerequisites else { added.add(neededType); CalculatePrerequisitesRequiredToBuild(state, neededType.getPrerequisites(), added); } } }