Skip to content

Commit

Permalink
fix creep spotting
Browse files Browse the repository at this point in the history
  • Loading branch information
s7jones committed Dec 18, 2018
1 parent 8d6b1ad commit d3c079d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 51 deletions.
10 changes: 5 additions & 5 deletions PeregrineBot/Source/ArmyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion PeregrineBot/Source/BaseManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion PeregrineBot/Source/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
86 changes: 50 additions & 36 deletions PeregrineBot/Source/InformationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void InformationManager::setup()
}
else
{
enemyRace = race;
m_enemyRace = race;
}
}
else
Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand Down Expand Up @@ -233,16 +233,16 @@ void InformationManager::setupScouting()

void InformationManager::update()
{
if (enemyRace == Races::Unknown)
if (m_enemyRace == Races::Unknown)
{
auto enemy = Broodwar->enemy();
if (enemy)
{
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;
}
}
}
Expand Down Expand Up @@ -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))
{
Expand All @@ -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;
}
Expand All @@ -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;
}
}
}

Expand Down Expand Up @@ -386,27 +392,27 @@ void InformationManager::spotting(BWAPI::Unit spotter)
{

// creep spotting
if (isSpottingCreepTime)
if (m_isSpottingCreepTime && (!IsBuilding)(spotter))
{
spotCreep(spotter);
}

if (isSpottingUnitsTime)
if (m_isSpottingUnitsTime)
{
spotUnits(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
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
Expand All @@ -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.
Expand Down
15 changes: 8 additions & 7 deletions PeregrineBot/Source/InformationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -54,10 +55,10 @@ class InformationManager

std::unique_ptr<ResourceUnitInfo> 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;

Expand All @@ -68,7 +69,7 @@ class InformationManager
std::set<ScoutingOptionFor4, sortByMeanTime> scoutingOptions;
std::map<std::set<BWAPI::TilePosition>, distAndTime> zerglingNetwork;
std::map<std::set<BWAPI::TilePosition>, distAndTime> overlordNetwork;
bool isEnemyBaseFromSpotting = false;
bool m_isEnemyBaseFromSpotting = false;
BWAPI::Position enemyBaseSpottingGuess = { 0, 0 };

std::set<FriendlyUnitInfo> friendlyUnits;
Expand Down Expand Up @@ -100,10 +101,10 @@ class InformationManager
double ground;
double air;
} maxBaseToBaseDistance;
std::map<BWAPI::UnitType, double> spottingTimes;
double spottingTime = 0;
bool isSpottingCreepTime = true;
bool isSpottingUnitsTime = true;
std::map<BWAPI::UnitType, double> m_spottingTimes;
double m_spottingTime = 0;
bool m_isSpottingCreepTime = true;
bool m_isSpottingUnitsTime = true;
using unitAndPotentialBases = std::pair<BWAPI::Unit, std::set<BWAPI::Position>>;
std::set<unitAndPotentialBases> spottedUnitsAndPotentialBases;
};
2 changes: 1 addition & 1 deletion PeregrineBot/Source/UtilityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit d3c079d

Please sign in to comment.