From d3c079da73241f2007a1dba5350313c8cce99285 Mon Sep 17 00:00:00 2001 From: Sam Jones Date: Tue, 18 Dec 2018 22:39:10 +0100 Subject: [PATCH] fix creep spotting --- PeregrineBot/Source/ArmyManager.cpp | 10 +-- PeregrineBot/Source/BaseManager.cpp | 2 +- PeregrineBot/Source/FileManager.cpp | 2 +- PeregrineBot/Source/InformationManager.cpp | 86 +++++++++++++--------- PeregrineBot/Source/InformationManager.h | 15 ++-- PeregrineBot/Source/UtilityManager.cpp | 2 +- 6 files changed, 66 insertions(+), 51 deletions(-) diff --git a/PeregrineBot/Source/ArmyManager.cpp b/PeregrineBot/Source/ArmyManager.cpp index 1e7b2d0..2104894 100644 --- a/PeregrineBot/Source/ArmyManager.cpp +++ b/PeregrineBot/Source/ArmyManager.cpp @@ -13,12 +13,12 @@ using namespace Filter; void ArmyManager::ZerglingAttack(BWAPI::Unit u) { const auto& enemyMain = InformationManager::Instance().m_enemyMain; - const auto& enemyRace = InformationManager::Instance().enemyRace; + const auto& enemyRace = InformationManager::Instance().m_enemyRace; const auto& unscoutedPositions = InformationManager::Instance().unscoutedPositions; - bool isEnemyBaseFromSpotting = InformationManager::Instance().isEnemyBaseFromSpotting; - bool isEnemyBaseDeduced = InformationManager::Instance().isEnemyBaseDeduced; - bool isEnemyBaseReached = InformationManager::Instance().isEnemyBaseReached; - bool isEnemyBaseDestroyed = InformationManager::Instance().isEnemyBaseDestroyed; + const bool isEnemyBaseFromSpotting = InformationManager::Instance().m_isEnemyBaseFromSpotting; + const bool isEnemyBaseDeduced = InformationManager::Instance().m_isEnemyBaseDeduced; + const bool isEnemyBaseReached = InformationManager::Instance().isEnemyBaseReached; + const bool isEnemyBaseDestroyed = InformationManager::Instance().isEnemyBaseDestroyed; const auto& enemyBaseSpottingGuess = InformationManager::Instance().enemyBaseSpottingGuess; const auto& enemyBuildings = InformationManager::Instance().m_enemyBuildings; const auto& enemyArmy = InformationManager::Instance().m_enemyArmy; diff --git a/PeregrineBot/Source/BaseManager.cpp b/PeregrineBot/Source/BaseManager.cpp index 4820fc6..80a1a5e 100644 --- a/PeregrineBot/Source/BaseManager.cpp +++ b/PeregrineBot/Source/BaseManager.cpp @@ -189,7 +189,7 @@ void BaseManager::DoAllWorkerTasks(BWAPI::Unit u) // If the call fails, then print the last error message DebugMessenger::Instance() << Broodwar->getLastError() << std::endl; DebugMessenger::Instance() << "Worker couldn't gather mineral or from refinery" << std::endl; - auto closestKnownMineral = InformationManager::Instance().getClosestMineral(u); + const auto closestKnownMineral = InformationManager::Instance().getClosestMineral(u); if (closestKnownMineral) { OrderManager::Instance().Move(u, closestKnownMineral->getPosition()); diff --git a/PeregrineBot/Source/FileManager.cpp b/PeregrineBot/Source/FileManager.cpp index 57fd068..9eb4a7e 100644 --- a/PeregrineBot/Source/FileManager.cpp +++ b/PeregrineBot/Source/FileManager.cpp @@ -109,7 +109,7 @@ void FileManager::writeStatisticsToFile(std::string botVersion, bool isWinner) input.close(); - auto enemyRace = InformationManager::Instance().enemyRace; + const auto enemyRace = InformationManager::Instance().m_enemyRace; if (enemyRace == Races::Zerg) score[0].matches++; if (enemyRace == Races::Terran) score[1].matches++; diff --git a/PeregrineBot/Source/InformationManager.cpp b/PeregrineBot/Source/InformationManager.cpp index ec1f7e0..dbe8c50 100644 --- a/PeregrineBot/Source/InformationManager.cpp +++ b/PeregrineBot/Source/InformationManager.cpp @@ -20,7 +20,7 @@ void InformationManager::setup() } else { - enemyRace = race; + m_enemyRace = race; } } else @@ -73,8 +73,8 @@ void InformationManager::setup() // maybe make 128 * 1.5 a const "smudge factor" variable auto addToSpottingMap = [this](UnitType ut, double smudgeFactor) { auto maxDist = ut.isFlyer() ? maxBaseToBaseDistance.air : maxBaseToBaseDistance.ground; - spottingTimes.insert({ ut, - (maxDist + smudgeFactor) / ut.topSpeed() }); + m_spottingTimes.insert({ ut, + (maxDist + smudgeFactor) / ut.topSpeed() }); }; addToSpottingMap(UnitTypes::Zerg_Overlord, 128 * 1.5); @@ -83,14 +83,14 @@ void InformationManager::setup() addToSpottingMap(ut, 128 * 0.5); } - using pair_type = decltype(spottingTimes)::value_type; + using pair_type = decltype(m_spottingTimes)::value_type; auto ptr = std::max_element( - std::begin(spottingTimes), std::end(spottingTimes), + std::begin(m_spottingTimes), std::end(m_spottingTimes), [](const pair_type& p1, const pair_type& p2) { return p1.second < p2.second; }); - spottingTime = ptr->second; + m_spottingTime = ptr->second; } void InformationManager::setupScouting() @@ -233,7 +233,7 @@ void InformationManager::setupScouting() void InformationManager::update() { - if (enemyRace == Races::Unknown) + if (m_enemyRace == Races::Unknown) { auto enemy = Broodwar->enemy(); if (enemy) @@ -241,8 +241,8 @@ void InformationManager::update() auto race = enemy->getRace(); if ((race == Races::Terran) || (race == Races::Zerg) || (race == Races::Protoss)) { - enemyRace = race; - DebugMessenger::Instance() << "Enemy is " << enemyRace.c_str() << std::endl; + m_enemyRace = race; + DebugMessenger::Instance() << "Enemy is " << m_enemyRace.c_str() << std::endl; } } } @@ -282,8 +282,8 @@ void InformationManager::updateScouting() IsEnemy && IsVisible && Exists && IsResourceDepot && !IsLifted); if (unitsOnBaseTile.size() > 0) { - m_enemyMain = { *unitsOnBaseTile.begin() }; - isEnemyBaseDeduced = true; + m_enemyMain = { *unitsOnBaseTile.begin() }; + m_isEnemyBaseDeduced = true; DebugMessenger::Instance() << "Found enemy base at: " << Broodwar->getFrameCount() << "F" << std::endl; if ((m_enemyMain.x() == 0) && (m_enemyMain.y() == 0)) { @@ -301,9 +301,9 @@ void InformationManager::updateScouting() // add logic here for "not" finding base even after scouting everything // probably only applicable to Terran weird lifting stuff - if (!(isEnemyBaseDeduced || m_enemyMain.m_unit) && unscoutedPositions.size() == 1) + if (!(m_isEnemyBaseDeduced || m_enemyMain.m_unit) && unscoutedPositions.size() == 1) { - isEnemyBaseDeduced = true; + m_isEnemyBaseDeduced = true; BWAPI::Position base = (*unscoutedPositions.begin()); DebugMessenger::Instance() << "Enemy base deduced to be at: " << base.x << ", " << base.y << "P" << std::endl; } @@ -317,17 +317,23 @@ void InformationManager::updateScouting() if (singleUnitWithPotentialBases.second.size() == 1) { - isEnemyBaseFromSpotting = true; - enemyBaseSpottingGuess = *singleUnitWithPotentialBases.second.begin(); + m_isEnemyBaseFromSpotting = true; + enemyBaseSpottingGuess = *singleUnitWithPotentialBases.second.begin(); Broodwar << "Spotted guess by removal and determined base at: " << enemyBaseSpottingGuess << "P" << std::endl; return; } } - if (m_enemyMain.m_unit || isEnemyBaseFromSpotting) + if (m_enemyMain.m_unit || m_isEnemyBaseFromSpotting) { - if (isSpottingUnitsTime) isSpottingUnitsTime = false; - if (isSpottingCreepTime) isSpottingCreepTime = false; + if (m_isSpottingUnitsTime) + { + m_isSpottingUnitsTime = false; + } + if (m_isSpottingCreepTime) + { + m_isSpottingCreepTime = false; + } } } @@ -386,12 +392,12 @@ void InformationManager::spotting(BWAPI::Unit spotter) { // creep spotting - if (isSpottingCreepTime) + if (m_isSpottingCreepTime && (!IsBuilding)(spotter)) { spotCreep(spotter); } - if (isSpottingUnitsTime) + if (m_isSpottingUnitsTime) { spotUnits(spotter); } @@ -399,14 +405,14 @@ void InformationManager::spotting(BWAPI::Unit spotter) void InformationManager::spotUnits(BWAPI::Unit spotter) { - if (Broodwar->getFrameCount() > spottingTime) + if (Broodwar->getFrameCount() > m_spottingTime) { - isSpottingUnitsTime = false; + m_isSpottingUnitsTime = false; DebugMessenger::Instance() << "Past spotting time" << std::endl; } else { - if (!isEnemyBaseFromSpotting) + if (!m_isEnemyBaseFromSpotting) { const auto largestZergSightRange = UnitTypes::Zerg_Hive.sightRange(); // ADDING 32 incase there is funkiness with getUnitsInRange @@ -421,8 +427,8 @@ void InformationManager::spotUnits(BWAPI::Unit spotter) if (it != spottedUnitsAndPotentialBases.end()) { auto ut = target->getType(); - auto it = spottingTimes.find(ut); - if (it == spottingTimes.end()) + auto it = m_spottingTimes.find(ut); + if (it == m_spottingTimes.end()) { continue; } @@ -444,8 +450,8 @@ void InformationManager::spotUnits(BWAPI::Unit spotter) spottedUnitsAndPotentialBases.insert({ target, potentialStartsFromSpotting }); if (potentialStartsFromSpotting.size() == 1) { - isEnemyBaseFromSpotting = true; - enemyBaseSpottingGuess = *potentialStartsFromSpotting.begin(); + m_isEnemyBaseFromSpotting = true; + enemyBaseSpottingGuess = *potentialStartsFromSpotting.begin(); Broodwar << spotter->getType() << " spotted " << ut << " and determined base at: " << enemyBaseSpottingGuess << "P" << std::endl; return; } @@ -457,7 +463,7 @@ void InformationManager::spotUnits(BWAPI::Unit spotter) void InformationManager::spotCreep(BWAPI::Unit spotter) { - if (!(enemyRace == Races::Zerg) || (enemyRace == Races::Unknown)) + if (!((m_enemyRace == Races::Zerg) || (m_enemyRace == Races::Unknown))) { return; } @@ -486,23 +492,31 @@ void InformationManager::spotCreep(BWAPI::Unit spotter) for (auto i = -radiusTPSpotter; i < radiusTPSpotter + 1; i++) { auto x = tp.x + i; - if (x < 0 || x >= Broodwar->mapWidth()) continue; + if (x < 0 || x >= Broodwar->mapWidth()) + { + continue; + } + for (auto j = -radiusTPSpotter; j < radiusTPSpotter + 1; j++) { auto y = tp.y + j; - if (y < 0 || y >= Broodwar->mapHeight()) continue; + if (y < 0 || y >= Broodwar->mapHeight()) + { + continue; + } + TilePosition tpRelative = { x, y }; bool hasCreep = Broodwar->hasCreep(tpRelative); if (!hasCreep) { - return; + continue; } auto regionRelative = BWTA::getRegion(tpRelative); if (regionRelative == nullptr) { - return; + continue; } for (auto otherStart : m_otherStarts) @@ -515,9 +529,9 @@ void InformationManager::spotCreep(BWAPI::Unit spotter) if (regionOtherStart == regionRelative) { - isEnemyBaseFromSpotting = true; - isSpottingCreepTime = false; - enemyBaseSpottingGuess = getBasePos(otherStart); + m_isEnemyBaseFromSpotting = true; + m_isSpottingCreepTime = false; + enemyBaseSpottingGuess = getBasePos(otherStart); Broodwar << "Spotted creep and determined base at: " << enemyBaseSpottingGuess << "P" << std::endl; return; } @@ -533,7 +547,7 @@ void InformationManager::overlordScoutingAfterBaseFound(BWAPI::Unit overlord) return; } - if (enemyRace != Races::Terran) + if (m_enemyRace != Races::Terran) { // Overlord scouting perimeter of all regions // Might be more useful to have this as a text hovering over overlord. diff --git a/PeregrineBot/Source/InformationManager.h b/PeregrineBot/Source/InformationManager.h index 2f37155..21256a8 100644 --- a/PeregrineBot/Source/InformationManager.h +++ b/PeregrineBot/Source/InformationManager.h @@ -43,6 +43,7 @@ class InformationManager static InformationManager instance; return instance; } + void onUnitShow(BWAPI::Unit unit); void onUnitDestroy(BWAPI::Unit unit); void onUnitMorph(BWAPI::Unit unit); @@ -54,10 +55,10 @@ class InformationManager std::unique_ptr getClosestMineral(BWAPI::Unit u); - bool isEnemyBaseDeduced = false; + bool m_isEnemyBaseDeduced = false; bool isEnemyBaseReached = false; bool isEnemyBaseDestroyed = false; - BWAPI::Race enemyRace = BWAPI::Races::Unknown; + BWAPI::Race m_enemyRace = BWAPI::Races::Unknown; bool isEnemyRaceRandom = false; bool isIslandsOnMap = false; @@ -68,7 +69,7 @@ class InformationManager std::set scoutingOptions; std::map, distAndTime> zerglingNetwork; std::map, distAndTime> overlordNetwork; - bool isEnemyBaseFromSpotting = false; + bool m_isEnemyBaseFromSpotting = false; BWAPI::Position enemyBaseSpottingGuess = { 0, 0 }; std::set friendlyUnits; @@ -100,10 +101,10 @@ class InformationManager double ground; double air; } maxBaseToBaseDistance; - std::map spottingTimes; - double spottingTime = 0; - bool isSpottingCreepTime = true; - bool isSpottingUnitsTime = true; + std::map m_spottingTimes; + double m_spottingTime = 0; + bool m_isSpottingCreepTime = true; + bool m_isSpottingUnitsTime = true; using unitAndPotentialBases = std::pair>; std::set spottedUnitsAndPotentialBases; }; diff --git a/PeregrineBot/Source/UtilityManager.cpp b/PeregrineBot/Source/UtilityManager.cpp index 08c1c74..18b5e0a 100644 --- a/PeregrineBot/Source/UtilityManager.cpp +++ b/PeregrineBot/Source/UtilityManager.cpp @@ -38,7 +38,7 @@ bool UtilityManager::getBestActionForZergling(BWAPI::Unit zergling) void UtilityManager::constructOptions() { - auto enemyRace = InformationManager::Instance().enemyRace; + const auto enemyRace = InformationManager::Instance().m_enemyRace; if (enemyRace != Races::Unknown) {