bb::cascades::VisualNode* NSRLastDocItemFactory::createItem (bb::cascades::ListView* list, const QString& type) { Q_UNUSED (type); NSRLastDocsListView *listView = static_cast<NSRLastDocsListView *> (list); NSRLastDocItem *item = new NSRLastDocItem (); NSRTranslator *translator = item->getTranslator (); ActionSet *actionSet = ActionSet::create (); ActionItem *shareAction = ActionItem::create().title (trUtf8 ("Share")); ActionItem *hideAction = ActionItem::create().title (trUtf8 ("Clear Recent", "Clear recent files")); DeleteActionItem *removeAction = DeleteActionItem::create (); shareAction->setImageSource (QUrl ("asset:///share.png")); hideAction->setImageSource (QUrl ("asset:///list-remove.png")); #if BBNDK_VERSION_AT_LEAST(10,2,0) shareAction->accessibility()->setName (trUtf8 ("Share file with others")); hideAction->accessibility()->setName (trUtf8 ("Remove file from the recent list only")); translator->addTranslatable ((UIObject *) shareAction->accessibility (), NSRTranslator::NSR_TRANSLATOR_TYPE_A11Y, QString ("NSRLastDocItemFactory"), QString ("Share file with others")); translator->addTranslatable ((UIObject *) hideAction->accessibility (), NSRTranslator::NSR_TRANSLATOR_TYPE_A11Y, QString ("NSRLastDocItemFactory"), QString ("Remove file from the recent list only")); #endif bool ok = connect (shareAction, SIGNAL (triggered ()), listView, SLOT (onShareActionTriggered ())); Q_UNUSED (ok); Q_ASSERT (ok); ok = connect (removeAction, SIGNAL (triggered ()), listView, SLOT (onRemoveActionTriggered ())); Q_ASSERT (ok); ok = connect (hideAction, SIGNAL (triggered ()), listView, SLOT (onHideActionTriggered ())); Q_ASSERT (ok); actionSet->add (shareAction); actionSet->add (hideAction); actionSet->add (removeAction); item->addActionSet (actionSet); translator->addTranslatable ((UIObject *) shareAction, NSRTranslator::NSR_TRANSLATOR_TYPE_ACTION, QString ("NSRLastDocItemFactory"), QString ("Share")); translator->addTranslatable ((UIObject *) hideAction, NSRTranslator::NSR_TRANSLATOR_TYPE_ACTION, QString ("NSRLastDocItemFactory"), QString ("Clear Recent")); return item; }
// builds the relevant action set for this search ActionSet calculateRelevantActions() { // the initial mask is blank ActionSet all; // let's say that we will always allow workers and supply producers all.add(DATA.getWorker()); all.add(DATA.getSupplyProvider()); // loop through each nonzero element of the goal for (Action a(0); a<DATA.size(); ++a) { // if we want some of this action if (params.goal.get(a) > 0) { //printf("GOAL HAS: %s\n", DATA[a].getName().c_str()); // add itself to the mask all.add(a); // if this action costs gas if (DATA[a].gasPrice() > 0) { // extractors are now relevant all.add(DATA.getRefinery()); } // also add all recursive prerequisites of this action calculateRecursivePrerequisites(a, all); } } return all; }
ActionSet getAllActions() const { ActionSet temp; for (int i=0; i<size(); ++i) { temp.add(i); } return temp; }
ActionSet* Bb10Ui::generateActionSetForBuffer(BufferId bufId) { //const Network* net = Client::network(m_networkInfo.networkId); ActionSet* actions = ActionSet::create(); BufferInfo::Type itemType = Client::networkModel()->bufferType(bufId); switch (itemType) { case BufferInfo::StatusBuffer: { ActionItem* listChannels = ActionItem::create().title("List Channels"); connect(listChannels, SIGNAL(triggered()), this, SLOT(listChannels())); actions->add(listChannels); break; } case BufferInfo::ChannelBuffer: { ActionItem* join = ActionItem::create().title("Join"); connect(join, SIGNAL(triggered()), this, SLOT(joinChannel())); actions->add(join); ActionItem* part = ActionItem::create().title("Part"); connect(part, SIGNAL(triggered()), this, SLOT(partChannel())); actions->add(part); ActionItem* del = ActionItem::create().title("Delete"); connect(del, SIGNAL(triggered()), this, SLOT(deleteBuffer())); actions->add(del); break; } case BufferInfo::QueryBuffer: { ActionItem* del = ActionItem::create().title("Delete"); connect(del, SIGNAL(triggered()), this, SLOT(deleteBuffer())); actions->add(del); break; } default: delete actions; return 0; } return actions->count() ? actions : 0; }
void GameState::getAllLegalActions(ActionSet & actions) const { const std::vector<ActionType> & allActions = ActionTypes::GetAllActionTypes(getRace()); for (ActionID i(0); i<allActions.size(); ++i) { const ActionType & action = allActions[i]; if (isLegal(action)) { actions.add(action); } } }
void DFBB_BuildOrderStackSearch::generateLegalActions(const GameState & state, ActionSet & legalActions) { legalActions.clear(); DFBB_BuildOrderSearchGoal & goal = _params.goal; const ActionType & worker = ActionTypes::GetWorker(state.getRace()); // add all legal relevant actions that are in the goal for (size_t a(0); a < _params.relevantActions.size(); ++a) { const ActionType & actionType = _params.relevantActions[a]; if (state.isLegal(actionType)) { // if there's none of this action in the goal it's not legal if (!goal.getGoal(actionType) && !goal.getGoalMax(actionType)) { continue; } // if we alread have more than the goal it's not legal if (goal.getGoal(actionType) && (state.getUnitData().getNumTotal(actionType) >= goal.getGoal(actionType))) { continue; } // if we already have more than the goal max it's not legal if (goal.getGoalMax(actionType) && (state.getUnitData().getNumTotal(actionType) >= goal.getGoalMax(actionType))) { continue; } legalActions.add(_params.relevantActions[a]); } } // don't make anything until we have 8 workers if (state.getUnitData().getNumTotal(worker) < 8) { legalActions.clear(); legalActions.add(worker); return; } // if we enabled the supply bounding flag if (_params.useSupplyBounding) { UnitCountType supplySurplus = state.getUnitData().getMaxSupply() + state.getUnitData().getSupplyInProgress() - state.getUnitData().getCurrentSupply(); UnitCountType threshold = (UnitCountType)(ActionTypes::GetSupplyProvider(state.getRace()).supplyProvided() * _params.supplyBoundingThreshold); if (supplySurplus >= threshold) { legalActions.remove(ActionTypes::GetSupplyProvider(state.getRace())); } } // if we enabled the always make workers flag, and workers are legal if (_params.useAlwaysMakeWorkers && legalActions.contains(worker)) { bool actionLegalBeforeWorker = false; ActionSet legalEqualWorker; FrameCountType workerReady = state.whenCanPerform(worker); for (size_t a(0); a < legalActions.size(); ++a) { const ActionType & actionType = legalActions[a]; const FrameCountType whenCanPerformAction = state.whenCanPerform(actionType); if (whenCanPerformAction < workerReady) { actionLegalBeforeWorker = true; break; } if ((whenCanPerformAction == workerReady) && (actionType.mineralPrice() == worker.mineralPrice())) { legalEqualWorker.add(actionType); } } if (actionLegalBeforeWorker) { legalActions.remove(worker); } else { legalActions = legalEqualWorker; } } }
// returns an ActionSet of prerequisites for a given action ActionSet calculatePrerequisites(StarcraftAction & action) { ActionSet pre; if (DEBUG_StarcraftData) { printf("DEBUG: Hello\n"); printf("DEBUG: %d \t%s \t%s\n", getAction(action), action.getName().c_str(), actions[getAction(action)].getName().c_str()); } // if it's a UnitType if (action.getType() == StarcraftAction::UnitType) { std::map<BWAPI::UnitType, int> requiredUnits = action.getUnitType().requiredUnits(); BWAPI::UnitType actionType = action.getUnitType(); // if it's a protoss building that isn't a Nexus or Assimilator, we need a pylon (indirectly) if (actionType.getRace() == BWAPI::Races::Protoss && actionType.isBuilding() && !actionType.isResourceDepot() && !(actionType == BWAPI::UnitTypes::Protoss_Pylon) && !(actionType == BWAPI::UnitTypes::Protoss_Assimilator)) { pre.add(getAction(BWAPI::UnitTypes::Protoss_Pylon)); } // for each of the required UnitTypes for (std::map<BWAPI::UnitType, int>::iterator unitIt = requiredUnits.begin(); unitIt != requiredUnits.end(); unitIt++) { if (DEBUG_StarcraftData) printf("\tPRE: %s\n", unitIt->first.getName().c_str()); BWAPI::UnitType type = unitIt->first; // add the action to the ActionSet if it is not a larva if (type != BWAPI::UnitTypes::Zerg_Larva) { //printf("\t\tAdding %s\n", type.getName().c_str()); pre.add(getAction(type)); } } // if there is a TechType required if (action.getUnitType().requiredTech() != BWAPI::TechTypes::None) { if (DEBUG_StarcraftData) printf("\tPRE: %s\n", action.getUnitType().requiredTech().getName().c_str()); // add it to the ActionSet pre.add(getAction(action.getUnitType().requiredTech())); } } // if it's a TechType if (action.getType() == StarcraftAction::TechType) { if (action.getTechType().whatResearches() != BWAPI::UnitTypes::None) { if (DEBUG_StarcraftData) printf("\tPRE: %s\n", action.getTechType().whatResearches().getName().c_str()); // add what researches it pre.add(getAction(action.getTechType().whatResearches())); } } // if it's an UpgradeType if (action.getType() == StarcraftAction::UpgradeType) { if (action.getUpgradeType().whatUpgrades() != BWAPI::UnitTypes::None) { if (DEBUG_StarcraftData) printf("\tPRE: %s\n", action.getUpgradeType().whatUpgrades().getName().c_str()); // add what upgrades it pre.add(getAction(action.getUpgradeType().whatUpgrades())); } } //printf("Finish Prerequisites\n"); return pre; }
// recursive function which does all search logic void DFBB(StarcraftState & s, int depth) { // increase the node expansion count nodesExpanded++; // if we have constraints and they are not met if (params.useConstraints && !s.meetsConstraints(params.ssc)) { // this state is not legal return; } // the time at which the last thing in the queue will finish int finishTime = s.getLastFinishTime(); /*int lookupVal = TT.lookup(s.hashAllUnits(1), s.hashAllUnits(2)); if (lookupVal != -1 && lookupVal < finishTime) { ttcuts++; //return; } TT.save(s.hashAllUnits(1), s.hashAllUnits(2), finishTime);*/ // if we already have completed the units for the goal, don't worry about last finish time if (s.meetsGoalCompleted(params.goal)) { finishTime = s.getCurrentFrame(); } // if we have met the goal, we're good if (s.meetsGoal(params.goal)) { // if it's better than the current best solution, set the new best if (finishTime < upperBound)// || ((finishTime == upperBound) && (s.getWorkerCount() > winnerWorkerCount))) { // set the winning info upperBound = finishTime; winner = s; winnerFound = true; winnerWorkerCount = s.getWorkerCount(); results = SearchResults(true, upperBound, nodesExpanded, searchTimer.getElapsedTimeInMilliSec(), s.getBuildOrder()); results.upperBound = s.calculateUpperBoundHeuristic(params.goal); results.lowerBound = s.eval(params.goal); results.avgBranch = numChildren / (double)numGenerations; results.minerals = s.getFinishTimeMinerals(); results.gas = s.getFinishTimeGas(); results.saveState = SearchSaveState(getBuildOrder(s), upperBound); //graphVizOutput(s, true); results.printResults(true); s.printData(); return; } } // if we are using search timeout and we are over the limit // (nodesExpanded % 1000 == 0) only checks the time every 1000 expansions, since it is slow if (params.searchTimeLimit && (nodesExpanded % 200 == 0) && (searchTimer.getElapsedTimeInMilliSec() > params.searchTimeLimit)) { results.saveState = SearchSaveState(getBuildOrder(s), upperBound); //results.saveState.print(); // throw an exception to unroll the recursion throw 1; } // get the legal action set ActionSet legalActions = s.getLegalActions(params.goal); // only use relevant actions legalActions = legalActions & relevantActions; // if we enabled the supply bounding flag if (params.useSupplyBounding) { // if we are more than 2 supply providers in the lead if ((s.getMaxSupply() - s.getCurrentSupply()) >= params.supplyBoundingThreshold*DATA[DATA.getSupplyProvider()].supplyProvided()) { // make supply providers illegal legalActions.subtract(DATA.getSupplyProvider()); } } // if we enabled the always make workers flag, and workers are legal if (params.useAlwaysMakeWorkers && !params.goal[DATA.getWorker()] && legalActions[DATA.getWorker()]) { ActionSet tempLegal(legalActions); ActionSet legalBeforeWorker; // compute when the next worker will be trainable int workerReady = s.resourcesReady(DATA.getWorker()); // for each other legal action while (!tempLegal.isEmpty()) { Action nextAction = tempLegal.popAction(); // if the action will be ready before the next worker if (s.resourcesReady(nextAction) <= workerReady) { // it's legal legalBeforeWorker.add(nextAction); } } // update the legal actions legalActions = legalBeforeWorker; } // if we enabled the use worker cutoff flag and we're above the cutoff if (params.useWorkerCutoff && s.getCurrentFrame() > (params.workerCutoff * upperBound)) { // workers are no longer legal legalActions.subtract(DATA.getWorker()); // if we have enough supply for the remaining goal if (s.hasEnoughSupplyForGoal(params.goal)) { // make supply providers illegal legalActions.subtract(DATA.getSupplyProvider()); } } // if we have children, update the counter if (!legalActions.isEmpty()) { numGenerations += 1; numChildren += legalActions.numActions(); } // load the save state if we are using it if (params.useSaveState && !finishedLoadingSaveState) { // if we are under the saved depth, load accordingly if (depth < params.saveState.getDepth()) { // pop actions until the NEXT action is the one we want to start on while (!legalActions.isEmpty() && legalActions.nextAction() != params.saveState[depth]) { legalActions.popAction(); } } // if we are over the depth, we are finished loading else { finishedLoadingSaveState = true; } } // children of this state in the search std::vector<StarcraftState> childStates; // while there are still legal actions to perform while (!legalActions.isEmpty()) { // get the next action Action nextAction = legalActions.popAction(); // when this action would finish int actionFinishTime = s.resourcesReady(nextAction) + DATA[nextAction].buildTime(); // heuristic value of the goal state int heuristicTime = s.getCurrentFrame() + s.eval(params.goal, params.useLandmarkLowerBoundHeuristic); // the h value for this node int h = (actionFinishTime > heuristicTime) ? actionFinishTime : heuristicTime; // primary cut-off, very quick heuristic if (h <= upperBound) { bool stillLegal = true; StarcraftState child(s); // set the repetitions if we are using repetitions, otherwise set to 1 int repeat = params.useRepetitions ? params.getRepetitions(nextAction) : 1; // if we are using increasing repetitions if (params.useIncreasingRepetitions) { // if we don't have the threshold amount of units, use a repetition value of 1 repeat = child.getNumUnits(nextAction) >= params.getRepetitionThreshold(nextAction) ? repeat : 1; } // make sure we don't repeat to more than we need for this unit type if (params.goal.get(nextAction)) { repeat = (std::min)(repeat, params.goal.get(nextAction) - child.getNumUnits(nextAction)); } else if (params.goal.getMax(nextAction)) { repeat = (std::min)(repeat, params.goal.getMax(nextAction) - child.getNumUnits(nextAction)); } // limit repetitions to how many we can make based on current used supply if (DATA[nextAction].supplyRequired() > 0) { int haveSupplyFor = (s.getMaxSupply() + s.getSupplyInProgress() - s.getCurrentSupply()) / DATA[nextAction].supplyRequired(); repeat = (std::min)(repeat, haveSupplyFor); } // if we're not finished loading the state, repeat value is 1 if (params.useSaveState && !finishedLoadingSaveState) { repeat = 1; } // for each repetition of this action for (int r = 0; r < repeat; ++r) { // if the action is still legal if (child.isLegal(nextAction, params.goal)) { int readyTime = child.resourcesReady(nextAction); child.doAction(nextAction, readyTime); } // if it's not legal, break the chain else { stillLegal = false; break; } } // if all actions in a row are legal, recurse on the child if (stillLegal) { child.setParent(&s); child.setActionPerformedK((UnitCountType)repeat); //DFBB(child, depth+1); childStates.push_back(child); } } } //std::sort(childStates.begin(), childStates.end(), StarcraftStateCompare<StarcraftStateType>(params)); //std::random_shuffle(childStates.begin(), childStates.end()); for (size_t i(0); i<childStates.size(); ++i) { DFBB(childStates[i], depth+1); } }