Skip to content

Commit

Permalink
Update to SteamHammer 3.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
kant2002 committed Sep 4, 2020
1 parent 870d342 commit fea65da
Show file tree
Hide file tree
Showing 128 changed files with 5,700 additions and 3,120 deletions.
24 changes: 6 additions & 18 deletions Steamhammer/Source/BOSSManager.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "Common.h"
#include "BOSSManager.h"
#include "BuildingManager.h"
#include "UnitUtil.h"

#include "The.h"

using namespace UAlbertaBot;

Expand Down Expand Up @@ -29,10 +30,10 @@ void BOSSManager::reset()
// start a new search for a new goal
void BOSSManager::startNewSearch(const std::vector<MetaPair> & goalUnits)
{
size_t numWorkers = UnitUtil::GetAllUnitCount(BWAPI::Broodwar->self()->getRace().getWorker());
size_t numDepots = UnitUtil::GetAllUnitCount(BWAPI::Broodwar->self()->getRace().getCenter())
+ UnitUtil::GetAllUnitCount(BWAPI::UnitTypes::Zerg_Lair)
+ UnitUtil::GetAllUnitCount(BWAPI::UnitTypes::Zerg_Hive);
size_t numWorkers = the.my.all.count(BWAPI::Broodwar->self()->getRace().getWorker());
size_t numDepots = the.my.all.count(BWAPI::Broodwar->self()->getRace().getCenter())
+ the.my.all.count(BWAPI::UnitTypes::Zerg_Lair)
+ the.my.all.count(BWAPI::UnitTypes::Zerg_Hive);

// TODO these are both solvable problems :-/
if (numWorkers == 0)
Expand Down Expand Up @@ -104,19 +105,6 @@ void BOSSManager::drawSearchInformation(int x, int y)
BWAPI::Broodwar->drawTextScreen(BWAPI::Position(x, y+45), "BO Size: %d", (int)_savedSearchResults.buildOrder.size());
}

void BOSSManager::drawStateInformation(int x, int y)
{
if (!Config::Debug::DrawBOSSStateInfo)
{
return;
}

BOSS::GameState currentState(BWAPI::Broodwar, BWAPI::Broodwar->self(), BuildingManager::Instance().buildingsQueued());
BWAPI::Broodwar->drawTextScreen(BWAPI::Position(x-100, y+30), "\x04%s", currentState.getBuildingData().toString().c_str());
BWAPI::Broodwar->drawTextScreen(BWAPI::Position(x+150, y), "\x04%s", currentState.toString().c_str());

}

// tell the search to keep going for however long we have this frame
void BOSSManager::update(double timeLimit)
{
Expand Down
99 changes: 79 additions & 20 deletions Steamhammer/Source/Base.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#include "Common.h"
#include "Base.h"

#include "InformationManager.h"
#include "WorkerManager.h"

using namespace UAlbertaBot;

// For setting base.id on initialization.
// The first base gets base id 1.
static int BaseID = 1;

// Is the base one of the map's starting bases?
// The only purpose of this method is to initialize the startingBase flag.
// NOTE This depends on tilePosition, so the startingBase flag must be declared after tilePosition.
Expand All @@ -29,47 +25,47 @@ bool Base::findIsStartingBase() const
// Create a base given its position and a set of resources that may belong to it.
// The caller is responsible for eliminating resources which are too small to be worth it.
Base::Base(BWAPI::TilePosition pos, const BWAPI::Unitset availableResources)
: id(BaseID)
: id(-1) // invalid value, will be reset after bases are sorted
, tilePosition(pos)
, distances(pos)
, reserved(false)
, startingBase(findIsStartingBase())
, natural(nullptr)
, workerDanger(false)
, failedPlacements(0)
, resourceDepot(nullptr)
, owner(BWAPI::Broodwar->neutral())
{
++BaseID;

GridDistances resourceDistances(pos, BaseResourceRange, false);

for (BWAPI::Unit resource : availableResources)
{
if (resource->getInitialTilePosition().isValid() && resourceDistances.getStaticUnitDistance(resource) >= 0)
{
if (resource->getType().isMineralField())
if (resource->getInitialType().isMineralField())
{
minerals.insert(resource);
}
else if (resource->getType() == BWAPI::UnitTypes::Resource_Vespene_Geyser)
else if (resource->getInitialType() == BWAPI::UnitTypes::Resource_Vespene_Geyser)
{
geysers.insert(resource);
initialGeysers.insert(resource);
}
}
}
geysers = initialGeysers;

// Fill in the set of blockers, destructible neutral units that are very close to the base
// and may interfere with its operation.
// This does not include the minerals to mine!
for (BWAPI::Unit unit : BWAPI::Broodwar->getStaticNeutralUnits())
{
// NOTE Khaydarin crystals are not destructible, and I don't know any way
// to find that out other than to check the name explicitly. Is there a way?
if (!unit->getType().canMove() &&
// to find that out other than to check the name explicitly. Is there a way?
if (!unit->getInitialType().canMove() &&
!unit->isInvincible() &&
unit->isTargetable() &&
!unit->isFlying() &&
unit->getType().getName().find("Khaydarin") == std::string::npos)
unit->getInitialType().getName().find("Khaydarin") == std::string::npos)
{
int dist = resourceDistances.getStaticUnitDistance(unit);
if (dist >= 0 && dist <= 9)
Expand All @@ -80,10 +76,39 @@ Base::Base(BWAPI::TilePosition pos, const BWAPI::Unitset availableResources)
}
}

// This is to be called exactly once at startup time.
// With the initial -1 value set in the constructor, the checks here prevent multiple calls.
void Base::setID(int baseID)
{
UAB_ASSERT(id == -1, "BUG! base ID reset");
UAB_ASSERT(baseID >= 1, "BUG! bad base ID");
id = baseID;
}

// The closest non-starting base to this base is its natural.
// This is only called (by Bases) if this is a starting base. Other bases have no natural.
// NOTE Some maps have mains with two naturals. This may give unhelpful results for them.
void Base::initializeNatural(const std::vector<Base *> & bases)
{
int minDist = INT_MAX;
for (Base * base : bases)
{
if (!base->isAStartingBase())
{
int dist = base->getTileDistance(tilePosition); // -1 if not connected by ground
if (dist > 0 && dist < minDist)
{
minDist = dist;
natural = base;
}
}
}
}

// The base is on an island, unconnected by ground to any starting base.
bool Base::isIsland() const
{
for (BWAPI::TilePosition tile : BWAPI::Broodwar->getStartLocations())
for (const BWAPI::TilePosition & tile : BWAPI::Broodwar->getStartLocations())
{
if (tile != getTilePosition() && getTileDistance(tile) > 0)
{
Expand All @@ -96,8 +121,8 @@ bool Base::isIsland() const

// Recalculate the base's set of geysers, including refineries (completed or not).
// This only works for visible geysers, so it should be called only for bases we own.
// Called to work around a bug related to BWAPI 4.1.2.
void Base::findGeysers()
// Called to work around BWAPI behavior (maybe not strictly a bug).
void Base::updateGeysers()
{
geysers.clear();

Expand All @@ -112,10 +137,16 @@ void Base::findGeysers()
}
}

// Return the center of the resource depot location (whether one is there or not).
// Return a tile near the center of the resource depot location. No tile is at the exact center.
const BWAPI::TilePosition Base::getCenterTile() const
{
return tilePosition + BWAPI::TilePosition(1, 1);
}

// Return the center of the resource depot location=.
const BWAPI::Position Base::getCenter() const
{
return BWAPI::Position(tilePosition) + BWAPI::Position(64, 48);
return BWAPI::Position(tilePosition) + BWAPI::Position(64, 48);
}

// The depot may be null. (That's why player is a separate argument, not depot->getPlayer().)
Expand All @@ -138,6 +169,34 @@ void Base::setInferredEnemyBase()
}
}

// The remaining minerals at the base, as of last report.
// For bases we own, the result is up to date.
int Base::getLastKnownMinerals() const
{
int total = 0;

for (BWAPI::Unit min : minerals)
{
total += InformationManager::Instance().getResourceAmount(min);
}

return total;
}

// The remaining gas at the base, as of last report.
// For bases we own, the result is up to date.
int Base::getLastKnownGas() const
{
int total = 0;

for (BWAPI::Unit gas : initialGeysers)
{
total += InformationManager::Instance().getResourceAmount(gas);
}

return total;
}

int Base::getInitialMinerals() const
{
int total = 0;
Expand All @@ -151,7 +210,7 @@ int Base::getInitialMinerals() const
int Base::getInitialGas() const
{
int total = 0;
for (const BWAPI::Unit gas : geysers)
for (const BWAPI::Unit gas : initialGeysers)
{
total += gas->getInitialResources();
}
Expand Down
28 changes: 20 additions & 8 deletions Steamhammer/Source/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace UAlbertaBot
{

class Base
{
private:
Expand All @@ -15,11 +14,13 @@ class Base
int id; // ID number for drawing base info

BWAPI::TilePosition tilePosition; // upper left corner of the resource depot spot
BWAPI::Unitset minerals; // the associated mineral patches
BWAPI::Unitset geysers; // the base's associated geysers
BWAPI::Unitset minerals; // the base's mineral patches (some may be mined out)
BWAPI::Unitset initialGeysers; // initial units of the geysers (taking one changes the unit)
BWAPI::Unitset geysers; // the base's current associated geysers
BWAPI::Unitset blockers; // destructible neutral units that may be in the way
GridDistances distances; // ground distances from tilePosition
bool startingBase; // one of the map's starting bases?
Base * natural; // if a starting base, the base's natural if any; else null

bool reserved; // if this is a planned expansion
bool workerDanger; // for our own bases only; false for others
Expand All @@ -35,20 +36,26 @@ class Base
BWAPI::Player owner; // self, enemy, neutral

Base(BWAPI::TilePosition pos, const BWAPI::Unitset availableResources);
void setID(int baseID); // called exactly once at startup

void initializeNatural(const std::vector<Base *> & bases);

int getID() const { return id; };
BWAPI::Unit getDepot() const { return resourceDepot; };
BWAPI::Player getOwner() const { return owner; };
bool isAStartingBase() const { return startingBase; };
bool isIsland() const;
Base * getNatural() const { return natural; };

void findGeysers();
void updateGeysers();

const BWAPI::TilePosition & getTilePosition() const { return tilePosition; };
const BWAPI::Position getPosition() const { return BWAPI::Position(tilePosition); };
const BWAPI::Position getCenter() const;
const BWAPI::TilePosition getCenterTile() const;
const BWAPI::Position getCenter() const;

// Ground distances.
const GridDistances & getDistances() const { return distances; };
int getTileDistance(const BWAPI::Position & pos) const { return distances.at(pos); };
int getTileDistance(const BWAPI::TilePosition & pos) const { return distances.at(pos); };
int getDistance(const BWAPI::Position & pos) const { return 32 * getTileDistance(pos); };
Expand All @@ -58,12 +65,17 @@ class Base
void placementFailed() { ++failedPlacements; };
int getFailedPlacements() const { return failedPlacements; };

// The mineral patch units and geyser units.
// The mineral patch units and geyser units. Blockers prevent the base from being used efficiently.
const BWAPI::Unitset & getMinerals() const { return minerals; };
const BWAPI::Unitset & getGeysers() const { return geysers; };
const BWAPI::Unitset & getInitialGeysers() const { return initialGeysers; };
const BWAPI::Unitset & getGeysers() const { return geysers; };
const BWAPI::Unitset & getBlockers() const { return blockers; }

// The sum of resources available.
// The latest reported total of resources available.
int getLastKnownMinerals() const;
int getLastKnownGas() const;

// The total initial resources available.
int getInitialMinerals() const;
int getInitialGas() const;

Expand Down
Loading

0 comments on commit fea65da

Please sign in to comment.