#include "CombatCommander.h" #include "UnitUtil.h" #include "BaseLocationManager.h" #include "Global.h" #include "WorkerManager.h" #include "UnitData.h" #include "MapTools.h" #include "InformationManager.h" #include "StrategyManager.h" #include "Squad.h" #include "SquadData.h" using namespace UAlbertaBot; const size_t IdlePriority = 0; const size_t AttackPriority = 1; const size_t BaseDefensePriority = 2; const size_t ScoutDefensePriority = 3; const size_t DropPriority = 4; CombatCommander::CombatCommander() { } void CombatCommander::initializeSquads() { SquadOrder idleOrder(SquadOrderTypes::Idle, BWAPI::Position(BWAPI::Broodwar->self()->getStartLocation()), 100, "Chill Out"); m_squadData.addSquad("Idle", Squad("Idle", idleOrder, IdlePriority)); // the main attack squad that will pressure the enemy's closest base location SquadOrder mainAttackOrder(SquadOrderTypes::Attack, getMainAttackLocation(), 800, "Attack Enemy Base"); m_squadData.addSquad("MainAttack", Squad("MainAttack", mainAttackOrder, AttackPriority)); BWAPI::Position ourBasePosition = BWAPI::Position(BWAPI::Broodwar->self()->getStartLocation()); // the scout defense squad will handle chasing the enemy worker scout SquadOrder enemyScoutDefense(SquadOrderTypes::Defend, ourBasePosition, 900, "Get the scout"); m_squadData.addSquad("ScoutDefense", Squad("ScoutDefense", enemyScoutDefense, ScoutDefensePriority)); // add a drop squad if we are using a drop strategy if (Config::Strategy::StrategyName == "Protoss_Drop") { SquadOrder zealotDrop(SquadOrderTypes::Drop, ourBasePosition, 900, "Wait for transport"); m_squadData.addSquad("Drop", Squad("Drop", zealotDrop, DropPriority)); } m_initialized = true; } bool CombatCommander::isSquadUpdateFrame() { return BWAPI::Broodwar->getFrameCount() % 10 == 0; } void CombatCommander::update(const BWAPI::Unitset & combatUnits) { PROFILE_FUNCTION(); if (!Config::Modules::UsingCombatCommander) { return; } if (!m_initialized) { initializeSquads(); } m_combatUnits = combatUnits; if (isSquadUpdateFrame()) { updateIdleSquad(); updateDropSquads(); updateScoutDefenseSquad(); updateDefenseSquads(); updateAttackSquads(); } m_squadData.update(); } void CombatCommander::updateIdleSquad() { Squad & idleSquad = m_squadData.getSquad("Idle"); for (auto & unit : m_combatUnits) { // if it hasn't been assigned to a squad yet, put it in the low priority idle squad if (m_squadData.canAssignUnitToSquad(unit, idleSquad)) { idleSquad.addUnit(unit); } } } void CombatCommander::updateAttackSquads() { Squad & mainAttackSquad = m_squadData.getSquad("MainAttack"); for (auto & unit : m_combatUnits) { if (unit->getType() == BWAPI::UnitTypes::Zerg_Scourge && UnitUtil::GetAllUnitCount(BWAPI::UnitTypes::Zerg_Hydralisk) < 30) { continue; } // get every unit of a lower priority and put it into the attack squad if (!unit->getType().isWorker() && (unit->getType() != BWAPI::UnitTypes::Zerg_Overlord) && m_squadData.canAssignUnitToSquad(unit, mainAttackSquad)) { m_squadData.assignUnitToSquad(unit, mainAttackSquad); } } SquadOrder mainAttackOrder(SquadOrderTypes::Attack, getMainAttackLocation(), 800, "Attack Enemy Base"); mainAttackSquad.setSquadOrder(mainAttackOrder); } void CombatCommander::updateDropSquads() { if (Config::Strategy::StrategyName != "Protoss_Drop") { return; } Squad & dropSquad = m_squadData.getSquad("Drop"); // figure out how many units the drop squad needs bool dropSquadHasTransport = false; int transportSpotsRemaining = 8; auto & dropUnits = dropSquad.getUnits(); for (auto & unit : dropUnits) { if (unit->isFlying() && unit->getType().spaceProvided() > 0) { dropSquadHasTransport = true; } else { transportSpotsRemaining -= unit->getType().spaceRequired(); } } // if there are still units to be added to the drop squad, do it if (transportSpotsRemaining > 0 || !dropSquadHasTransport) { // take our first amount of combat units that fill up a transport and add them to the drop squad for (auto & unit : m_combatUnits) { // if this is a transport unit and we don't have one in the squad yet, add it if (!dropSquadHasTransport && (unit->getType().spaceProvided() > 0 && unit->isFlying())) { m_squadData.assignUnitToSquad(unit, dropSquad); dropSquadHasTransport = true; continue; } if (unit->getType().spaceRequired() > transportSpotsRemaining) { continue; } // get every unit of a lower priority and put it into the attack squad if (!unit->getType().isWorker() && m_squadData.canAssignUnitToSquad(unit, dropSquad)) { m_squadData.assignUnitToSquad(unit, dropSquad); transportSpotsRemaining -= unit->getType().spaceRequired(); } } } // otherwise the drop squad is full, so execute the order else { SquadOrder dropOrder(SquadOrderTypes::Drop, getMainAttackLocation(), 800, "Attack Enemy Base"); dropSquad.setSquadOrder(dropOrder); } } void CombatCommander::updateScoutDefenseSquad() { if (m_combatUnits.empty()) { return; } // if the current squad has units in it then we can ignore this Squad & scoutDefenseSquad = m_squadData.getSquad("ScoutDefense"); // get the region that our base is located in auto myBase = Global::Bases().getPlayerStartingBaseLocation(BWAPI::Broodwar->self()); // get all of the enemy units in this region BWAPI::Unitset enemyUnitsInRegion; for (auto & unit : BWAPI::Broodwar->enemy()->getUnits()) { if (myBase->containsPosition(unit->getPosition())) { enemyUnitsInRegion.insert(unit); } } // if there's an enemy worker in our region then assign someone to chase him bool assignScoutDefender = enemyUnitsInRegion.size() == 1 && (*enemyUnitsInRegion.begin())->getType().isWorker(); // if our current squad is empty and we should assign a worker, do it if (scoutDefenseSquad.isEmpty() && assignScoutDefender) { // the enemy worker that is attacking us BWAPI::Unit enemyWorker = *enemyUnitsInRegion.begin(); // get our worker unit that is mining that is closest to it BWAPI::Unit workerDefender = findClosestWorkerToTarget(m_combatUnits, enemyWorker); if (enemyWorker && workerDefender) { // grab it from the worker manager and put it in the squad if (m_squadData.canAssignUnitToSquad(workerDefender, scoutDefenseSquad)) { Global::Workers().setCombatWorker(workerDefender); m_squadData.assignUnitToSquad(workerDefender, scoutDefenseSquad); } } } // if our squad is not empty and we shouldn't have a worker chasing then take him out of the squad else if (!scoutDefenseSquad.isEmpty() && !assignScoutDefender) { for (auto & unit : scoutDefenseSquad.getUnits()) { unit->stop(); if (unit->getType().isWorker()) { Global::Workers().finishedWithWorker(unit); } } scoutDefenseSquad.clear(); } } void CombatCommander::updateDefenseSquads() { if (m_combatUnits.empty()) { return; } // for each of our occupied regions const BaseLocation * enemyBaseLocation = Global::Bases().getPlayerStartingBaseLocation(BWAPI::Broodwar->enemy()); for (const BaseLocation * myBaseLocation : Global::Bases().getOccupiedBaseLocations(BWAPI::Broodwar->self())) { // don't defend inside the enemy region, this will end badly when we are stealing gas or cannon rushing if (myBaseLocation == enemyBaseLocation) { continue; } BWAPI::Position basePosition = myBaseLocation->getPosition(); // start off assuming all enemy units in region are just workers int numDefendersPerEnemyUnit = 2; // all of the enemy units in this region std::vector enemyUnitsInRegion; for (auto & unit : BWAPI::Broodwar->enemy()->getUnits()) { // if it's an overlord, don't worry about it for defense, we don't care what they see if (unit->getType() == BWAPI::UnitTypes::Zerg_Overlord) { continue; } if (myBaseLocation->containsPosition(unit->getPosition())) { enemyUnitsInRegion.push_back(unit); } } // we can ignore the first enemy worker in our region since we assume it is a scout for (auto unit : enemyUnitsInRegion) { if (unit->getType().isWorker()) { enemyUnitsInRegion.erase(std::remove(enemyUnitsInRegion.begin(), enemyUnitsInRegion.end(), unit), enemyUnitsInRegion.end()); break; } } // calculate how many units are flying / ground units int numEnemyFlyingInRegion = 0; int numEnemyGroundInRegion = 0; for (auto & unit : enemyUnitsInRegion) { if (unit->isFlying()) { numEnemyFlyingInRegion++; } else { numEnemyGroundInRegion++; } } std::stringstream squadName; squadName << "Base Defense " << basePosition.x << " " << basePosition.y; // if there's nothing in this region to worry about if (enemyUnitsInRegion.empty()) { // if a defense squad for this region exists, remove it if (m_squadData.squadExists(squadName.str())) { m_squadData.getSquad(squadName.str()).clear(); } // and return, nothing to defend here continue; } else { // if we don't have a squad assigned to this region already, create one if (!m_squadData.squadExists(squadName.str())) { SquadOrder defendRegion(SquadOrderTypes::Defend, basePosition, 32 * 25, "Defend Region!"); m_squadData.addSquad(squadName.str(), Squad(squadName.str(), defendRegion, BaseDefensePriority)); } } // assign units to the squad if (m_squadData.squadExists(squadName.str())) { Squad & defenseSquad = m_squadData.getSquad(squadName.str()); // figure out how many units we need on defense int flyingDefendersNeeded = numDefendersPerEnemyUnit * numEnemyFlyingInRegion; int groundDefensersNeeded = numDefendersPerEnemyUnit * numEnemyGroundInRegion; updateDefenseSquadUnits(defenseSquad, flyingDefendersNeeded, groundDefensersNeeded); } else { UAB_ASSERT(false, "Squad should have existed: %s", squadName.str().c_str()); } } // for each of our defense squads, if there aren't any enemy units near the position, remove the squad std::set uselessDefenseSquads; for (const auto & kv : m_squadData.getSquads()) { const Squad & squad = kv.second; const SquadOrder & order = squad.getSquadOrder(); if (order.getType() != SquadOrderTypes::Defend) { continue; } bool enemyUnitInRange = false; for (auto & unit : BWAPI::Broodwar->enemy()->getUnits()) { if (unit->getDistance(order.getPosition()) < order.getRadius()) { enemyUnitInRange = true; break; } } if (!enemyUnitInRange) { m_squadData.getSquad(squad.getName()).clear(); } } } void CombatCommander::updateDefenseSquadUnits(Squad & defenseSquad, const size_t & flyingDefendersNeeded, const size_t & groundDefendersNeeded) { const BWAPI::Unitset & squadUnits = defenseSquad.getUnits(); size_t flyingDefendersInSquad = std::count_if(squadUnits.begin(), squadUnits.end(), UnitUtil::CanAttackAir); size_t groundDefendersInSquad = std::count_if(squadUnits.begin(), squadUnits.end(), UnitUtil::CanAttackGround); // if there's nothing left to defend, clear the squad if (flyingDefendersNeeded == 0 && groundDefendersNeeded == 0) { defenseSquad.clear(); return; } // add flying defenders if we still need them size_t flyingDefendersAdded = 0; while (flyingDefendersNeeded > flyingDefendersInSquad + flyingDefendersAdded) { BWAPI::Unit defenderToAdd = findClosestDefender(defenseSquad, defenseSquad.getSquadOrder().getPosition(), true); // if we find a valid flying defender, add it to the squad if (defenderToAdd) { m_squadData.assignUnitToSquad(defenderToAdd, defenseSquad); ++flyingDefendersAdded; } // otherwise we'll never find another one so break out of this loop else { break; } } // add ground defenders if we still need them size_t groundDefendersAdded = 0; while (groundDefendersNeeded > groundDefendersInSquad + groundDefendersAdded) { BWAPI::Unit defenderToAdd = findClosestDefender(defenseSquad, defenseSquad.getSquadOrder().getPosition(), false); // if we find a valid ground defender add it if (defenderToAdd) { m_squadData.assignUnitToSquad(defenderToAdd, defenseSquad); ++groundDefendersAdded; } // otherwise we'll never find another one so break out of this loop else { break; } } } BWAPI::Unit CombatCommander::findClosestDefender(const Squad & defenseSquad, BWAPI::Position pos, bool flyingDefender) { BWAPI::Unit closestDefender = nullptr; double minDistance = std::numeric_limits::max(); int zerglingsInOurBase = numZerglingsInOurBase(); bool zerglingRush = zerglingsInOurBase > 0 && BWAPI::Broodwar->getFrameCount() < 5000; for (auto & unit : m_combatUnits) { if ((flyingDefender && !UnitUtil::CanAttackAir(unit)) || (!flyingDefender && !UnitUtil::CanAttackGround(unit))) { continue; } if (!m_squadData.canAssignUnitToSquad(unit, defenseSquad)) { continue; } // add workers to the defense squad if we are being rushed very quickly if (!Config::Micro::WorkersDefendRush || (unit->getType().isWorker() && !zerglingRush && !beingBuildingRushed())) { continue; } double dist = unit->getDistance(pos); if (!closestDefender || (dist < minDistance)) { closestDefender = unit; minDistance = dist; } } return closestDefender; } BWAPI::Position CombatCommander::getDefendLocation() { return BWAPI::Position(BWAPI::Broodwar->self()->getStartLocation()); } void CombatCommander::drawSquadInformation(int x, int y) { m_squadData.drawSquadInformation(x, y); } BWAPI::Position CombatCommander::getMainAttackLocation() { const BaseLocation * enemyBaseLocation = Global::Bases().getPlayerStartingBaseLocation(BWAPI::Broodwar->enemy()); // First choice: Attack an enemy region if we can see units inside it if (enemyBaseLocation) { BWAPI::Position enemyBasePosition = enemyBaseLocation->getPosition(); // get all known enemy units in the area BWAPI::Unitset enemyUnitsInArea; Global::Map().getUnits(enemyUnitsInArea, enemyBasePosition, 800, false, true); bool onlyOverlords = true; for (auto & unit : enemyUnitsInArea) { if (unit->getType() != BWAPI::UnitTypes::Zerg_Overlord) { onlyOverlords = false; } } if (!BWAPI::Broodwar->isExplored(BWAPI::TilePosition(enemyBasePosition)) || !enemyUnitsInArea.empty()) { if (!onlyOverlords) { return enemyBaseLocation->getPosition(); } } } // Second choice: Attack known enemy buildings for (const auto & kv : Global::Info().getUnitInfo(BWAPI::Broodwar->enemy())) { const UnitInfo & ui = kv.second; if (ui.type.isBuilding() && ui.lastPosition != BWAPI::Positions::None) { return ui.lastPosition; } } // Third choice: Attack visible enemy units that aren't overlords for (auto & unit : BWAPI::Broodwar->enemy()->getUnits()) { if (unit->getType() == BWAPI::UnitTypes::Zerg_Overlord) { continue; } if (UnitUtil::IsValidUnit(unit) && unit->isVisible()) { return unit->getPosition(); } } // Fourth choice: We can't see anything so explore the map attacking along the way return BWAPI::Position(Global::Map().getLeastRecentlySeenTile()); } BWAPI::Unit CombatCommander::findClosestWorkerToTarget(BWAPI::Unitset & unitsToAssign, BWAPI::Unit target) { UAB_ASSERT(target != nullptr, "target was null"); if (!target) { return nullptr; } BWAPI::Unit closestMineralWorker = nullptr; double closestDist = 100000; // for each of our workers for (auto & unit : unitsToAssign) { if (!unit->getType().isWorker()) { continue; } // if it is a move worker if (Global::Workers().isFree(unit)) { double dist = unit->getDistance(target); if (!closestMineralWorker || dist < closestDist) { closestMineralWorker = unit; dist = closestDist; } } } return closestMineralWorker; } // when do we want to defend with our workers? // this function can only be called if we have no fighters to defend with int CombatCommander::defendWithWorkers() { // our home nexus position BWAPI::Position homePosition(BWAPI::Broodwar->self()->getStartLocation()); // enemy units near our workers int enemyUnitsNearWorkers = 0; // defense radius of nexus int defenseRadius = 300; // fill the set with the types of units we're concerned about for (auto & unit : BWAPI::Broodwar->enemy()->getUnits()) { // if it's a zergling or a worker we want to defend if (unit->getType() == BWAPI::UnitTypes::Zerg_Zergling) { if (unit->getDistance(homePosition) < defenseRadius) { enemyUnitsNearWorkers++; } } } // if there are enemy units near our workers, we want to defend return enemyUnitsNearWorkers; } int CombatCommander::numZerglingsInOurBase() { int concernRadius = 600; int zerglings = 0; BWAPI::Position ourBasePosition = BWAPI::Position(BWAPI::Broodwar->self()->getStartLocation()); // check to see if the enemy has zerglings as the only attackers in our base for (auto & unit : BWAPI::Broodwar->enemy()->getUnits()) { if (unit->getType() != BWAPI::UnitTypes::Zerg_Zergling) { continue; } if (unit->getDistance(ourBasePosition) < concernRadius) { zerglings++; } } return zerglings; } bool CombatCommander::beingBuildingRushed() { int concernRadius = 1200; BWAPI::Position ourBasePosition = BWAPI::Position(BWAPI::Broodwar->self()->getStartLocation()); // check to see if the enemy has zerglings as the only attackers in our base for (auto & unit : BWAPI::Broodwar->enemy()->getUnits()) { if (unit->getType().isBuilding()) { return true; } } return false; }