本文整理汇总了C++中bwapi::Unitset::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ Unitset::insert方法的具体用法?C++ Unitset::insert怎么用?C++ Unitset::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bwapi::Unitset
的用法示例。
在下文中一共展示了Unitset::insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getMineralPatchesNearDepot
BWAPI::Unitset WorkerData::getMineralPatchesNearDepot(BWAPI::Unit depot)
{
// if there are minerals near the depot, add them to the set
BWAPI::Unitset mineralsNearDepot;
int radius = 300;
for (auto & unit : BWAPI::Broodwar->getAllUnits())
{
if ((unit->getType() == BWAPI::UnitTypes::Resource_Mineral_Field) && unit->getDistance(depot) < radius)
{
mineralsNearDepot.insert(unit);
}
}
// if we didn't find any, use the whole map
if (mineralsNearDepot.empty())
{
for (auto & unit : BWAPI::Broodwar->getAllUnits())
{
if ((unit->getType() == BWAPI::UnitTypes::Resource_Mineral_Field))
{
mineralsNearDepot.insert(unit);
}
}
}
return mineralsNearDepot;
}
示例2: immediateThreat
bool ScoutManager::immediateThreat()
{
BWAPI::Unitset enemyAttackingWorkers;
for (auto & unit : BWAPI::Broodwar->enemy()->getUnits())
{
if (unit->getType().isWorker() && unit->isAttacking())
{
enemyAttackingWorkers.insert(unit);
}
}
if (_workerScout->isUnderAttack())
{
return true;
}
for (auto & unit : BWAPI::Broodwar->enemy()->getUnits())
{
double dist = unit->getDistance(_workerScout);
double range = unit->getType().groundWeapon().maxRange();
if (unit->getType().canAttack() && !unit->getType().isWorker() && (dist <= range + 32))
{
return true;
}
}
return false;
}
示例3: computeResourceBox
void BuildingPlacer::computeResourceBox()
{
BWAPI::Position start(BWAPI::Broodwar->self()->getStartLocation());
BWAPI::Unitset unitsAroundNexus;
for (auto & unit : BWAPI::Broodwar->getAllUnits())
{
// if the units are less than 400 away add them if they are resources
if (unit->getDistance(start) < 300 && unit->getType().isMineralField())
{
unitsAroundNexus.insert(unit);
}
}
for (auto & unit : unitsAroundNexus)
{
int x = unit->getPosition().x;
int y = unit->getPosition().y;
int left = x - unit->getType().dimensionLeft();
int right = x + unit->getType().dimensionRight() + 1;
int top = y - unit->getType().dimensionUp();
int bottom = y + unit->getType().dimensionDown() + 1;
_boxTop = top < _boxTop ? top : _boxTop;
_boxBottom = bottom > _boxBottom ? bottom : _boxBottom;
_boxLeft = left < _boxLeft ? left : _boxLeft;
_boxRight = right > _boxRight ? right : _boxRight;
}
//BWAPI::Broodwar->printf("%d %d %d %d", boxTop, boxBottom, boxLeft, boxRight);
}
示例4: assignUnit
void GameCommander::assignUnit(BWAPI::Unit unit, BWAPI::Unitset & set)
{
if (_scoutUnits.contains(unit)) { _scoutUnits.erase(unit); }
else if (_combatUnits.contains(unit)) { _combatUnits.erase(unit); }
set.insert(unit);
}
示例5: executeMicro
void MedicManager::executeMicro(const BWAPI::Unitset & targets)
{
const BWAPI::Unitset & medics = getUnits();
// create a set of all medic targets
BWAPI::Unitset medicTargets;
for (auto & unit : BWAPI::Broodwar->self()->getUnits())
{
if (unit->getHitPoints() < unit->getInitialHitPoints() && !unit->getType().isMechanical() && !unit->getType().isBuilding())
{
medicTargets.insert(unit);
}
}
BWAPI::Unitset availableMedics(medics);
// for each target, send the closest medic to heal it
for (auto & target : medicTargets)
{
// only one medic can heal a target at a time
if (target->isBeingHealed())
{
continue;
}
double closestMedicDist = std::numeric_limits<double>::infinity();
BWAPI::Unit closestMedic = nullptr;
for (auto & medic : availableMedics)
{
double dist = medic->getDistance(target);
if (!closestMedic || (dist < closestMedicDist))
{
closestMedic = medic;
closestMedicDist = dist;
}
}
// if we found a medic, send it to heal the target
if (closestMedic)
{
closestMedic->useTech(BWAPI::TechTypes::Healing, target);
availableMedics.erase(closestMedic);
}
// otherwise we didn't find a medic which means they're all in use so break
else
{
break;
}
}
// the remaining medics should head to the squad order position
for (auto & medic : availableMedics)
{
Micro::SmartAttackMove(medic, order.getPosition());
}
}
示例6: getTarget
// get a target for the zealot to attack
BWAPI::Unit TankManager::getTarget(BWAPI::Unit tank, const BWAPI::Unitset & targets)
{
int bestPriorityDistance = 1000000;
int bestPriority = 0;
double bestLTD = 0;
BWAPI::Unit bestTargetThreatInRange = nullptr;
double bestTargetThreatInRangeLTD = 0;
int highPriority = 0;
double closestDist = std::numeric_limits<double>::infinity();
BWAPI::Unit closestTarget = nullptr;
int siegeTankRange = BWAPI::UnitTypes::Terran_Siege_Tank_Siege_Mode.groundWeapon().maxRange() - 32;
BWAPI::Unitset targetsInSiegeRange;
for (auto & target : targets)
{
if (target->getDistance(tank) < siegeTankRange && UnitUtil::CanAttack(tank, target))
{
targetsInSiegeRange.insert(target);
}
}
const BWAPI::Unitset & newTargets = targetsInSiegeRange.empty() ? targets : targetsInSiegeRange;
// check first for units that are in range of our attack that can cause damage
// choose the highest priority one from them at the lowest health
for (const auto & target : newTargets)
{
if (!UnitUtil::CanAttack(tank, target))
{
continue;
}
double distance = tank->getDistance(target);
double LTD = UnitUtil::CalculateLTD(target, tank);
int priority = getAttackPriority(tank, target);
bool targetIsThreat = LTD > 0;
BWAPI::Broodwar->drawTextMap(target->getPosition(), "%d", priority);
if (!closestTarget || (priority > highPriority) || (priority == highPriority && distance < closestDist))
{
closestDist = distance;
highPriority = priority;
closestTarget = target;
}
}
if (bestTargetThreatInRange)
{
return bestTargetThreatInRange;
}
return closestTarget;
}
示例7: addUnitsToMicroManagers
void Squad::addUnitsToMicroManagers()
{
BWAPI::Unitset meleeUnits;
BWAPI::Unitset rangedUnits;
BWAPI::Unitset detectorUnits;
BWAPI::Unitset transportUnits;
BWAPI::Unitset tankUnits;
BWAPI::Unitset medicUnits;
BWAPI::Unitset scienceVessels;
// add _units to micro managers
for (auto & unit : _units)
{
if(unit->isCompleted() && unit->getHitPoints() > 0 && unit->exists())
{
// select dector _units
if (unit->getType() == BWAPI::UnitTypes::Terran_Medic)
{
medicUnits.insert(unit);
}
else if (unit->getType() == BWAPI::UnitTypes::Terran_Science_Vessel)
{
scienceVessels.insert(unit);
}
else if (unit->getType() == BWAPI::UnitTypes::Terran_Siege_Tank_Siege_Mode || unit->getType() == BWAPI::UnitTypes::Terran_Siege_Tank_Tank_Mode)
{
tankUnits.insert(unit);
}
else if (unit->getType().isDetector() && !unit->getType().isBuilding())
{
detectorUnits.insert(unit);
}
// select transport _units
else if (unit->getType() == BWAPI::UnitTypes::Protoss_Shuttle || unit->getType() == BWAPI::UnitTypes::Terran_Dropship)
{
transportUnits.insert(unit);
}
// select ranged _units
else if ((unit->getType().groundWeapon().maxRange() > 32) || (unit->getType() == BWAPI::UnitTypes::Protoss_Reaver) || (unit->getType() == BWAPI::UnitTypes::Zerg_Scourge))
{
rangedUnits.insert(unit);
}
// select melee _units
else if (unit->getType().groundWeapon().maxRange() <= 32)
{
meleeUnits.insert(unit);
}
}
}
_meleeManager.setUnits(meleeUnits);
_rangedManager.setUnits(rangedUnits);
_detectorManager.setUnits(detectorUnits);
_transportManager.setUnits(transportUnits);
_tankManager.setUnits(tankUnits);
_medicManager.setUnits(medicUnits);
_sciencevesselManager.setUnits(scienceVessels);
}
示例8: assignTargetsOld
void InterceptorManager::assignTargetsOld(const BWAPI::Unitset & targets)
{
const BWAPI::Unitset & rangedUnits = getUnits();
// figure out targets
BWAPI::Unitset rangedUnitTargets;
for (auto & target : targets)
{
// conditions for targeting
if (!(target->getType() == BWAPI::UnitTypes::Zerg_Larva) &&
!(target->getType() == BWAPI::UnitTypes::Zerg_Egg) &&
!(target->getType() == BWAPI::UnitTypes::Buildings) &&
(target->isTargetable()) &&
target->isVisible() &&
target->getType() != BWAPI::UnitTypes::Resource_Vespene_Geyser)
{
rangedUnitTargets.insert(target);
}
}
auto attacker2target = assignEnemy(rangedUnits, rangedUnitTargets);
BWAPI::Position shome = BWAPI::Position(BWTA::getStartLocation(BWAPI::Broodwar->self())->getPosition());
for (auto & rangedUnit : rangedUnits)
{
// train sub units such as scarabs or interceptors
//trainSubUnits(rangedUnit);
// if the order is to attack or defend
if (order.getType() == SquadOrderTypes::Attack || order.getType() == SquadOrderTypes::Defend)
{
// if there are targets
if (!rangedUnitTargets.empty())
{
// find the best target for this zealot
auto targetIdx = attacker2target.find(rangedUnit);
BWAPI::Unit target = targetIdx == attacker2target.end() ? getTarget(rangedUnit, rangedUnitTargets) : targetIdx->first;
if (target && Config::Debug::DrawUnitTargetInfo)
{
BWAPI::Broodwar->drawLineMap(rangedUnit->getPosition(), rangedUnit->getTargetPosition(), BWAPI::Colors::Purple);
}
Micro::SmartAttackUnit(rangedUnit, target);
}
// if there are no targets
else
{
// if we're not near the order position
if (rangedUnit->getDistance(order.getPosition()) > 100)
{
// move to it
Micro::SmartAttackMove(rangedUnit, order.getPosition());
}
}
}
}
}
示例9: setAllUnits
void Squad::setAllUnits()
{
// clean up the _units vector just in case one of them died
BWAPI::Unitset goodUnits;
for (auto & unit : _units)
{
if( unit->isCompleted() &&
unit->getHitPoints() > 0 &&
unit->exists() &&
unit->getPosition().isValid() &&
unit->getType() != BWAPI::UnitTypes::Unknown)
{
goodUnits.insert(unit);
}
}
_units = goodUnits;
}
示例10: verifySquadUniqueMembership
void SquadData::verifySquadUniqueMembership()
{
BWAPI::Unitset assigned;
for (const auto & kv : _squads)
{
for (auto & unit : kv.second.getUnits())
{
if (assigned.contains(unit))
{
BWAPI::Broodwar->printf("Unit is in at least two squads: %s", unit->getType().getName().c_str());
}
assigned.insert(unit);
}
}
}
示例11: assignTargetsNew
// still has bug in it somewhere, use Old version
void MeleeManager::assignTargetsNew(const BWAPI::Unitset & targets)
{
const BWAPI::Unitset & meleeUnits = getUnits();
// figure out targets
BWAPI::Unitset meleeUnitTargets;
for (auto & target : targets)
{
// conditions for targeting
if (!(target->getType().isFlyer()) &&
!(target->isLifted()) &&
!(target->getType() == BWAPI::UnitTypes::Zerg_Larva) &&
!(target->getType() == BWAPI::UnitTypes::Zerg_Egg) &&
target->isVisible())
{
meleeUnitTargets.insert(target);
}
}
BWAPI::Unitset meleeUnitsToAssign(meleeUnits);
std::map<BWAPI::Unit, int> attackersAssigned;
for (auto & unit : meleeUnitTargets)
{
attackersAssigned[unit] = 0;
}
int smallThreshold = BWAPI::Broodwar->self()->getRace() == BWAPI::Races::Zerg ? 3 : 1;
int bigThreshold = BWAPI::Broodwar->self()->getRace() == BWAPI::Races::Zerg ? 12 : 3;
// keep assigning targets while we have attackers and targets remaining
while (!meleeUnitsToAssign.empty() && !meleeUnitTargets.empty())
{
auto attackerAssignment = findClosestUnitPair(meleeUnitsToAssign, meleeUnitTargets);
BWAPI::Unit & attacker = attackerAssignment.first;
BWAPI::Unit & target = attackerAssignment.second;
UAB_ASSERT_WARNING(attacker, "We should have chosen an attacker!");
if (!attacker)
{
break;
}
if (!target)
{
Micro::SmartMove(attacker, order.getPosition());
continue;
}
Micro::SmartAttackUnit(attacker, target);
// update the number of units assigned to attack the target we found
int & assigned = attackersAssigned[attackerAssignment.second];
assigned++;
// if it's a small / fast unit and there's more than 2 things attacking it already, don't assign more
if ((target->getType().isWorker() || target->getType() == BWAPI::UnitTypes::Zerg_Zergling) && (assigned >= smallThreshold))
{
meleeUnitTargets.erase(target);
}
// if it's a building and there's more than 10 things assigned to it already, don't assign more
else if (assigned > bigThreshold)
{
meleeUnitTargets.erase(target);
}
meleeUnitsToAssign.erase(attacker);
}
// if there's no targets left, attack move to the order destination
if (meleeUnitTargets.empty())
{
for (auto & unit : meleeUnitsToAssign)
{
if (unit->getDistance(order.getPosition()) > 100)
{
// move to it
Micro::SmartMove(unit, order.getPosition());
BWAPI::Broodwar->drawLineMap(unit->getPosition(), order.getPosition(), BWAPI::Colors::Yellow);
}
}
}
}
示例12: addUnitsToMicroManagers
void Squad::addUnitsToMicroManagers()
{
BWAPI::Unitset meleeUnits;
BWAPI::Unitset rangedUnits;
BWAPI::Unitset detectorUnits;
BWAPI::Unitset transportUnits;
BWAPI::Unitset tankUnits;
BWAPI::Unitset medicUnits;
BWAPI::Unitset lurkerUnits;
BWAPI::Unitset zerglingUnits;
// add _units to micro managers
for (auto & unit : _units)
{
if(unit->isCompleted() && unit->getHitPoints() > 0 && unit->exists())
{
// Special Unit Types
// Medics
if (unit->getType() == BWAPI::UnitTypes::Terran_Medic)
{
medicUnits.insert(unit);
}
// Tanks
else if (unit->getType() == BWAPI::UnitTypes::Terran_Siege_Tank_Siege_Mode || unit->getType() == BWAPI::UnitTypes::Terran_Siege_Tank_Tank_Mode)
{
tankUnits.insert(unit);
}
// Lurkers
else if (unit->getType() == BWAPI::UnitTypes::Zerg_Lurker) {
lurkerUnits.insert(unit);
}
/*else if (unit->getType() == BWAPI::UnitTypes::Zerg_Zergling) {
zerglingUnits.insert(unit);
}*/
// Detectors
else if (unit->getType().isDetector() && !unit->getType().isBuilding())
{
detectorUnits.insert(unit);
}
// Transporters
else if (unit->getType() == BWAPI::UnitTypes::Protoss_Shuttle || unit->getType() == BWAPI::UnitTypes::Terran_Dropship)
{
transportUnits.insert(unit);
}
// Ranged Units
else if ((unit->getType().groundWeapon().maxRange() > 32) || (unit->getType() == BWAPI::UnitTypes::Protoss_Reaver) || (unit->getType() == BWAPI::UnitTypes::Zerg_Scourge))
{
rangedUnits.insert(unit);
}
// Melee Units
else if (unit->getType().groundWeapon().maxRange() <= 32)
{
meleeUnits.insert(unit);
}
}
}
_meleeManager.setUnits(meleeUnits);
_rangedManager.setUnits(rangedUnits);
_detectorManager.setUnits(detectorUnits);
_transportManager.setUnits(transportUnits);
_tankManager.setUnits(tankUnits);
_medicManager.setUnits(medicUnits);
_lurkerManager.setUnits(lurkerUnits);
_zerglingManager.setUnits(zerglingUnits);
}
示例13: assignTargetsOld
void RangedManager::assignTargetsOld(const BWAPI::Unitset & targets)
{
const BWAPI::Unitset & rangedUnits = getUnits();
// figure out targets
BWAPI::Unitset rangedUnitTargets;
for (auto & target : targets)
{
// conditions for targeting
if (!(target->getType() == BWAPI::UnitTypes::Zerg_Larva) &&
!(target->getType() == BWAPI::UnitTypes::Zerg_Egg) &&
!(target->getType() == BWAPI::UnitTypes::Buildings) &&
(target->isTargetable()) &&
target->isVisible() &&
target->getType() != BWAPI::UnitTypes::Resource_Vespene_Geyser)
{
rangedUnitTargets.insert(target);
}
}
auto attacker2target = assignEnemy(rangedUnits, rangedUnitTargets);
BWAPI::Position shome = BWAPI::Position(BWTA::getStartLocation(BWAPI::Broodwar->self())->getPosition());
BWAPI::Unitset _rangedUnits = rangedUnits;
auto _rangedUnitTargets = rangedUnitTargets;
int maxChoices = rangedUnits.size() / 3 + 1, i = 0;
std::map<BWAPI::Unit, std::pair<BWAPI::Unit, BWAPI::Unit> > dodgeUnits;
BWAPI::Unitset threatens;
for (BWAPI::Unit threatenClosest = NULL, unitClosetEnemy = NULL;;)
{
if (i++ > maxChoices)
break;
double dist = BWAPI::Broodwar->self()->getUpgradeLevel(BWAPI::UpgradeTypes::Singularity_Charge)?96:96;
double enerange = BWAPI::Broodwar->self()->getUpgradeLevel(BWAPI::UpgradeTypes::Singularity_Charge) ? 80 : 80;
for (auto & rangedUnit : _rangedUnits)
{
for (auto target : _rangedUnitTargets)
{
if (target->getType().isWorker() || (target->getType().isBuilding()))
continue;
if (target->getType().groundWeapon().maxRange() <= enerange)
{
if (rangedUnit->getDistance(target) < dist)
{
threatenClosest = target;
unitClosetEnemy = rangedUnit;
dist = rangedUnit->getDistance(target);
}
}
}
}
if (threatenClosest != NULL)
{
threatens.insert(threatenClosest);
BWAPI::Broodwar->drawCircleMap(threatenClosest->getPosition(), 2, BWAPI::Colors::Red, true);
auto vec1 = unitClosetEnemy->getPosition() - threatenClosest->getPosition();
dodgeUnits[unitClosetEnemy] = std::make_pair(unitClosetEnemy, threatenClosest);
auto __rangedUnits = rangedUnits;
for (auto rangedUnit:__rangedUnits)
{
if (rangedUnit->getDistance(unitClosetEnemy) > unitClosetEnemy->getDistance(threatenClosest)+64)
continue;
auto vec2 = rangedUnit->getPosition() - threatenClosest->getPosition();
int judge = (vec1.x*vec2.x + vec1.y*vec2.y)*(vec1.x*vec2.x + vec1.y*vec2.y);
if (judge * 6 > (vec1.x*vec1.x + vec1.y*vec1.y)*(vec2.x*vec2.x + vec2.y*vec2.y) * 5)
{
dodgeUnits[rangedUnit] = std::make_pair(unitClosetEnemy, threatenClosest);
_rangedUnits.erase(rangedUnit);
}
}
}
else
break;
}
BWAPI::Unitset withoutBuildingsAndWorkers = rangedUnitTargets;
auto _withoutBuildingsAndWorkers = withoutBuildingsAndWorkers;
for (auto target : _withoutBuildingsAndWorkers)
if (target->getType().isBuilding() || target->getType().isWorker())
withoutBuildingsAndWorkers.erase(target);
for (auto & rangedUnit : rangedUnits)
{
// train sub units such as scarabs or interceptors
//trainSubUnits(rangedUnit);
// if the order is to attack or defend
int chokeZealot = 0;
for (const auto & zealot : BWAPI::Broodwar->self()->getUnits())
{
// trivial case: unit which exists matches the type
if (zealot->getType() != BWAPI::UnitTypes::Protoss_Zealot)
continue;
if (zealot->isCompleted() == false)
continue;
auto ckpt = BWTA::getNearestChokepoint(zealot->getPosition());
if (zealot->getDistance(rangedUnit->getPosition()) < 300)
{
if (ckpt && ckpt->getWidth()<256 && ckpt->getCenter().getDistance(zealot->getPosition()) < 128)
chokeZealot += 3;
else
chokeZealot += 1;
//.........这里部分代码省略.........
示例14: getProducer
BWAPI::Unit ProductionManager::getProducer(MetaType t, BWAPI::Position closestTo)
{
// get the type of unit that builds this
BWAPI::UnitType producerType = t.whatBuilds();
// make a set of all candidate producers
BWAPI::Unitset candidateProducers;
for (auto & unit : BWAPI::Broodwar->self()->getUnits())
{
UAB_ASSERT(unit != nullptr, "Unit was null");
// reasons a unit can not train the desired type
if (unit->getType() != producerType) { continue; }
if (!unit->isCompleted()) { continue; }
if (unit->isTraining()) { continue; }
if (unit->isLifted()) { continue; }
if (!unit->isPowered()) { continue; }
// if the type is an addon, some special cases
if (t.getUnitType().isAddon())
{
// if the unit already has an addon, it can't make one
if (unit->getAddon() != nullptr)
{
continue;
}
// if we just told this unit to build an addon, then it will not be building another one
// this deals with the frame-delay of telling a unit to build an addon and it actually starting to build
if (unit->getLastCommand().getType() == BWAPI::UnitCommandTypes::Build_Addon
&& (BWAPI::Broodwar->getFrameCount() - unit->getLastCommandFrame() < 10))
{
continue;
}
bool isBlocked = false;
// if the unit doesn't have space to build an addon, it can't make one
BWAPI::TilePosition addonPosition(unit->getTilePosition().x + unit->getType().tileWidth(), unit->getTilePosition().y + unit->getType().tileHeight() - t.getUnitType().tileHeight());
BWAPI::Broodwar->drawBoxMap(addonPosition.x*32, addonPosition.y*32, addonPosition.x*32 + 64, addonPosition.y*32 + 64, BWAPI::Colors::Red);
for (int i=0; i<unit->getType().tileWidth() + t.getUnitType().tileWidth(); ++i)
{
for (int j=0; j<unit->getType().tileHeight(); ++j)
{
BWAPI::TilePosition tilePos(unit->getTilePosition().x + i, unit->getTilePosition().y + j);
// if the map won't let you build here, we can't build it
if (!BWAPI::Broodwar->isBuildable(tilePos))
{
isBlocked = true;
BWAPI::Broodwar->drawBoxMap(tilePos.x*32, tilePos.y*32, tilePos.x*32 + 32, tilePos.y*32 + 32, BWAPI::Colors::Red);
}
// if there are any units on the addon tile, we can't build it
BWAPI::Unitset uot = BWAPI::Broodwar->getUnitsOnTile(tilePos.x, tilePos.y);
if (uot.size() > 0 && !(uot.size() == 1 && *(uot.begin()) == unit))
{
isBlocked = true;;
BWAPI::Broodwar->drawBoxMap(tilePos.x*32, tilePos.y*32, tilePos.x*32 + 32, tilePos.y*32 + 32, BWAPI::Colors::Red);
}
}
}
if (isBlocked)
{
continue;
}
}
// if the type requires an addon and the producer doesn't have one
typedef std::pair<BWAPI::UnitType, int> ReqPair;
for (const ReqPair & pair : t.getUnitType().requiredUnits())
{
BWAPI::UnitType requiredType = pair.first;
if (requiredType.isAddon())
{
if (!unit->getAddon() || (unit->getAddon()->getType() != requiredType))
{
continue;
}
}
}
// if we haven't cut it, add it to the set of candidates
candidateProducers.insert(unit);
}
return getClosestUnitToPosition(candidateProducers, closestTo);
}
示例15: assignTargetsOld
void MeleeManager::assignTargetsOld(const BWAPI::Unitset & targets)
{
const BWAPI::Unitset & meleeUnits = getUnits();
// figure out targets
BWAPI::Unitset meleeUnitTargets;
for (auto & target : targets)
{
// conditions for targeting
if (!(target->getType().isFlyer()) &&
!(target->isLifted()) &&
!(target->getType() == BWAPI::UnitTypes::Zerg_Larva) &&
!(target->getType() == BWAPI::UnitTypes::Zerg_Egg) &&
!(target->getType() == BWAPI::UnitTypes::Buildings)&&
(target->isTargetable())&&
target->isVisible())
{
meleeUnitTargets.insert(target);
}
}
std::unordered_map<BWAPI::Unit, BWAPI::Unit> attacker2target = assignEnemy(meleeUnits,meleeUnitTargets);
// for each meleeUnit
for (auto & meleeUnit : meleeUnits)
{
// if the order is to attack or defend
if (order.getType() == SquadOrderTypes::Attack || order.getType() == SquadOrderTypes::Defend)
{
// run away if we meet the retreat critereon
if (meleeUnitShouldRetreat(meleeUnit, targets) && meleeUnit->isUnderAttack())
{
BWAPI::Position fleeTo(pullPosition);
fleeTo.x = (fleeTo.x - meleeUnit->getPosition().x)*2 + meleeUnit->getPosition().x;
fleeTo.y = (fleeTo.y - meleeUnit->getPosition().y)*2 + meleeUnit->getPosition().y;
Micro::SmartMove(meleeUnit, fleeTo);
}
// if there are targets
else if (!meleeUnitTargets.empty())
{
BWAPI::Unit target;
if (attacker2target.find(meleeUnit) != attacker2target.end())
{
target = attacker2target[meleeUnit];
}
else target = getTarget(meleeUnit, meleeUnitTargets);
Micro::SmartAttackUnit(meleeUnit, target);
}
// if there are no targets
else
{
// if we're not near the order position
if (meleeUnit->getDistance(order.getPosition()) > 100)
{
// move to it
Micro::SmartMove(meleeUnit, order.getPosition());
}
}
}
if (Config::Debug::DrawUnitTargetInfo)
{
BWAPI::Broodwar->drawLineMap(meleeUnit->getPosition().x, meleeUnit->getPosition().y,
meleeUnit->getTargetPosition().x, meleeUnit->getTargetPosition().y, Config::Debug::ColorLineTarget);
}
}
}