const std::string GUI::getTextureFileName(const ActionType & type) const { std::string filename = "units/" + type.getName() + ".png"; if (type.isTech()) { filename = "command_icons/" + type.getName() + ".png"; } else if (type.isUpgrade()) { filename = "command_icons/" + type.getName() + ".png"; } for (size_t i(0); i<filename.size(); ++i) { if (filename[i] == ' ') { filename[i] = '_'; } } return filename; }
void BuildingData::queueAction(const ActionType & action) { for (size_t i=0; i<_buildings.size(); ++i) { if (_buildings[i].canBuildNow(action)) { _buildings[i].queueActionType(action); return; } } // this method should always work since we have fast forwarded to the correct point in time BOSS_ASSERT(false, "Didn't find a building to queue this type of unit in: %s", action.getName().c_str()); }
bool GameState::whyIsNotLegal(const ActionType & action) const { const size_t numRefineries = _units.getNumTotal(ActionTypes::GetRefinery(getRace())); const size_t numDepots = _units.getNumTotal(ActionTypes::GetResourceDepot(getRace())); const size_t refineriesInProgress = _units.getNumInProgress(ActionTypes::GetRefinery(getRace())); // we can never build a larva static const ActionType & Zerg_Larva = ActionTypes::GetActionType("Zerg_Larva"); if (action == Zerg_Larva) { std::cout << "WhyNotLegal: " << action.getName() << " - Cannot build a Larva" << std::endl; return false; } // check if the tech requirements are met if (!_units.hasPrerequisites(action.getPrerequisites())) { std::cout << "WhyNotLegal: " << action.getName() << " - Tech requirements not met" << std::endl; return false; } // if it's a unit and we are out of supply and aren't making an overlord, it's not legal if (!action.isMorphed() && ((_units.getCurrentSupply() + action.supplyRequired()) > (_units.getMaxSupply() + _units.getSupplyInProgress()))) { std::cout << "WhyNotLegal: " << action.getName() << " - Not enough supply to construct" << std::endl; return false; } // specific rule for never leaving 0 workers on minerals if (action.isRefinery() && (getNumMineralWorkers() <= 4)) { std::cout << "WhyNotLegal: " << action.getName() << " - Cannot leave 0 workers on minerals" << std::endl; return false; } // if it's a new building and no drones are available, it's not legal if (!action.isMorphed() && action.isBuilding() && (getNumMineralWorkers() <= 1) && (getNumBuildingWorkers() == 0)) { std::cout << "WhyNotLegal: " << action.getName() << " - No building worker available" << std::endl; return false; } // we can't build a building with our last worker if (!action.isMorphed() && action.isBuilding() && (getNumMineralWorkers() <= 1 + 3*refineriesInProgress) && (getNumBuildingWorkers() == 0)) { std::cout << "WhyNotLegal: " << action.getName() << " - Can't build with last worker" << std::endl; return false; } // if we have no gas income we can't make a gas unit if (!canAffordGas(action) && !_units.hasGasIncome()) { std::cout << "WhyNotLegal: " << action.getName() << " - No gas income for gas unit" << std::endl; return false; } // if we have no mineral income we'll never have a minerla unit if (!canAffordMinerals(action) && !_units.hasMineralIncome()) { std::cout << "WhyNotLegal: " << action.getName() << " - No mineral income" << std::endl; return false; } // don't build more refineries than resource depots if (action.isRefinery() && (numRefineries >= numDepots)) { std::cout << "WhyNotLegal: " << action.getName() << " - Can't have more refineries than depots" << std::endl; return false; } // we don't need to go over the maximum supply limit with supply providers if (action.isSupplyProvider() && (_units.getMaxSupply() + _units.getSupplyInProgress() >= 400)) { std::cout << "WhyNotLegal: " << action.getName() << " - Can't go over max supply bound with providers" << std::endl; return false; } if (action.isTech() && getUnitData().getNumTotal(action) > 0) { std::cout << "WhyNotLegal: " << action.getName() << " - Can't produce additional copy of tech" << std::endl; return false; } return true; }
const FrameCountType GameState::whenBuildingPrereqReady(const ActionType & action) const { FrameCountType buildingAvailableTime(0); const ActionType & builder = action.whatBuildsActionType(); BOSS_ASSERT(builder.isBuilding(), "The thing that builds this is not a building"); bool buildingIsConstructed = _units.getBuildingData().canBuildEventually(action);//getNumCompleted(builder) > 0; bool buildingInProgress = _units.getNumInProgress(builder) > 0; FrameCountType constructedBuildingFreeTime = std::numeric_limits<int>::max()-10; FrameCountType buildingInProgressFinishTime = std::numeric_limits<int>::max()-10; BOSS_ASSERT(buildingIsConstructed || (!action.requiresAddon() && buildingInProgress), "We will never be able to build action: %s", action.getName().c_str()); if (buildingIsConstructed) { constructedBuildingFreeTime = _currentFrame + _units.getBuildingData().getTimeUntilCanBuild(action); } if (!action.requiresAddon() && buildingInProgress) { buildingInProgressFinishTime = _units.getFinishTime(builder); } // this will give us when the building will be free to build this action buildingAvailableTime = std::min(constructedBuildingFreeTime, buildingInProgressFinishTime); // get all prerequisites currently in progress but do not have any completed PrerequisiteSet prereqInProgress = _units.getPrerequistesInProgress(action); // remove the specific builder from this list since we calculated that earlier prereqInProgress.remove(builder); //// if we actually have some prerequisites in progress other than the building if (!prereqInProgress.isEmpty()) { // get the max time the earliest of each type will be finished in FrameCountType C = _units.getFinishTime(prereqInProgress); // take the maximum of this value and when the building was available buildingAvailableTime = (C > buildingAvailableTime) ? C : buildingAvailableTime; } return buildingAvailableTime; }
// do an action, action must be legal for this not to break std::vector<ActionType> GameState::doAction(const ActionType & action) { BOSS_ASSERT(action.getRace() == _race, "Race of action does not match race of the state"); _actionsPerformed.push_back(ActionPerformed()); _actionsPerformed[_actionsPerformed.size()-1].actionType = action; BOSS_ASSERT(isLegal(action), "Trying to perform an illegal action: %s %s", action.getName().c_str(), getActionsPerformedString().c_str()); // set the actionPerformed _actionPerformed = action; _actionPerformedK = 1; FrameCountType workerReadyTime = whenWorkerReady(action); FrameCountType ffTime = whenCanPerform(action); BOSS_ASSERT(ffTime >= 0 && ffTime < 1000000, "FFTime is very strange: %d", ffTime); auto actionsFinished=fastForward(ffTime); _actionsPerformed[_actionsPerformed.size()-1].actionQueuedFrame = _currentFrame; _actionsPerformed[_actionsPerformed.size()-1].gasWhenQueued = _gas; _actionsPerformed[_actionsPerformed.size()-1].mineralsWhenQueued = _minerals; // how much time has elapsed since the last action was queued? FrameCountType elapsed(_currentFrame - _lastActionFrame); _lastActionFrame = _currentFrame; BOSS_ASSERT(canAffordMinerals(action), "Minerals less than price: %ld < %d, ffTime=%d %s", _minerals, action.mineralPrice(), (int)elapsed, action.getName().c_str()); BOSS_ASSERT(canAffordGas(action), "Gas less than price: %ld < %d, ffTime=%d %s", _gas, action.gasPrice(), (int)elapsed, action.getName().c_str()); // modify our resources _minerals -= action.mineralPrice(); _gas -= action.gasPrice(); // do race specific things here if (getRace() == Races::Protoss) { _units.addActionInProgress(action, _currentFrame + action.buildTime()); } else if (getRace() == Races::Terran) { if (action.isBuilding() && !action.isAddon()) { if (getNumMineralWorkers() == 0) { std::cout << toString() << std::endl; } BOSS_ASSERT(getNumMineralWorkers() > 0, "Don't have any mineral workers to assign"); _units.setBuildingWorker(); } _units.addActionInProgress(action, _currentFrame + action.buildTime()); } else if (getRace() == Races::Zerg) { // zerg must subtract a larva if the action was unit creation if (action.isUnit() && !action.isBuilding()) { if (action.isMorphed()) { _units.morphUnit(action.whatBuildsActionType(), action, _currentFrame + action.buildTime()); } else { BOSS_ASSERT(getHatcheryData().numLarva() > 0, "We should have a larva to use"); _units.getHatcheryData().useLarva(); _units.addActionInProgress(action, _currentFrame + action.buildTime()); } } else if (action.isBuilding()) { _units.morphUnit(action.whatBuildsActionType(), action, _currentFrame + action.buildTime()); } else { // if it's not a unit or a building it's a tech so we queue it normally _units.addActionInProgress(action, _currentFrame + action.buildTime()); } } return actionsFinished; }