Skip to content

Commit

Permalink
Update to SteamHammer 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
kant2002 committed Sep 4, 2020
1 parent fea65da commit 78be93a
Show file tree
Hide file tree
Showing 26 changed files with 551 additions and 303 deletions.
3 changes: 2 additions & 1 deletion Steamhammer/Source/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class Base
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); };
int getDistance(const BWAPI::TilePosition & pos) const { return 32 * getTileDistance(pos); };
int getDistance(const BWAPI::Position & pos) const { return 32 * getTileDistance(pos); };

void setOwner(BWAPI::Unit depot, BWAPI::Player player);
void setInferredEnemyBase();
Expand Down
3 changes: 2 additions & 1 deletion Steamhammer/Source/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Config
{
bool ConfigFileFound = false;
bool ConfigFileParsed = false;
std::string ConfigFileLocation = "bwapi-data/AI/Steamhammer_3.0.2.json";
std::string ConfigFileLocation = "bwapi-data/AI/Steamhammer_3.1.json";
}

namespace IO
Expand Down Expand Up @@ -96,6 +96,7 @@ namespace Config
bool DrawMapDistances = false;
bool DrawTerrainHeights = false;
bool DrawBaseInfo = false;
bool DrawExpoScores = false;
bool DrawStrategyBossInfo = false;
bool DrawUnitTargets = false;
bool DrawUnitOrders = false;
Expand Down
1 change: 1 addition & 0 deletions Steamhammer/Source/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ namespace Config
extern bool DrawMapDistances;
extern bool DrawTerrainHeights;
extern bool DrawBaseInfo;
extern bool DrawExpoScores;
extern bool DrawStrategyBossInfo;
extern bool DrawSquadInfo;
extern bool DrawClusters;
Expand Down
1 change: 1 addition & 0 deletions Steamhammer/Source/GameCommander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ void GameCommander::drawDebugInterface()
drawUnitCounts(345, 30);
Bases::Instance().drawBaseInfo();
Bases::Instance().drawBaseOwnership(575, 30);
the.map.drawExpoScores();
InformationManager::Instance().drawResourceAmounts();
BuildingManager::Instance().drawBuildingInformation(200, 50);
the.placer.drawReservedTiles();
Expand Down
105 changes: 78 additions & 27 deletions Steamhammer/Source/MacroAct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

using namespace UAlbertaBot;

MacroLocation MacroAct::getMacroLocationFromString(const std::string & s)
// Map unit type names to unit types.
static std::map<std::string, BWAPI::UnitType> _unitTypesByName;

MacroLocation MacroAct::getMacroLocationFromString(const std::string & s) const
{
if (s == "main")
{
Expand Down Expand Up @@ -65,6 +68,30 @@ MacroLocation MacroAct::getMacroLocationFromString(const std::string & s)
return MacroLocation::Anywhere;
}

void MacroAct::initializeUnitTypesByName()
{
if (_unitTypesByName.size() == 0) // if not already initialized
{
for (BWAPI::UnitType unitType : BWAPI::UnitTypes::allUnitTypes())
{
std::string typeName = TrimRaceName(unitType.getName());
std::replace(typeName.begin(), typeName.end(), '_', ' ');
std::transform(typeName.begin(), typeName.end(), typeName.begin(), ::tolower);
_unitTypesByName[typeName] = unitType;
}
}
}

BWAPI::UnitType MacroAct::getUnitTypeFromString(const std::string & s) const
{
auto it = _unitTypesByName.find(s);
if (it != _unitTypesByName.end())
{
return (*it).second;
}
return BWAPI::UnitTypes::Unknown;
}

MacroAct::MacroAct ()
: _type(MacroActs::Default)
, _macroLocation(MacroLocation::Anywhere)
Expand All @@ -77,9 +104,24 @@ MacroAct::MacroAct(const std::string & name)
: _type(MacroActs::Default)
, _macroLocation(MacroLocation::Anywhere)
{
initializeUnitTypesByName();

// Normalize the input string.
std::string inputName(name);
std::replace(inputName.begin(), inputName.end(), '_', ' ');
std::transform(inputName.begin(), inputName.end(), inputName.begin(), ::tolower);
if (inputName.substr(0, 7) == "terran ")
{
inputName = inputName.substr(7, std::string::npos);
}
else if (inputName.substr(0, 8) == "protoss ")
{
inputName = inputName.substr(8, std::string::npos);
}
else if (inputName.substr(0, 5) == "zerg ")
{
inputName = inputName.substr(5, std::string::npos);
}

// You can specify a location, like "hatchery @ expo" or "go post worker @ enemy natural".
MacroLocation specifiedMacroLocation(MacroLocation::Anywhere); // the default
Expand All @@ -98,20 +140,38 @@ MacroAct::MacroAct(const std::string & name)
for (MacroCommandType t : MacroCommand::allCommandTypes())
{
std::string commandName = MacroCommand::getName(t);
if (MacroCommand::hasArgument(t))
if (MacroCommand::hasNumericArgument(t))
{
// There's an argument. Match the command name and parse out the argument.
std::regex commandWithArgRegex(commandName + " (\\d+)");
std::smatch m;
if (std::regex_match(inputName, m, commandWithArgRegex)) {
if (std::regex_match(inputName, m, commandWithArgRegex))
{
int amount = GetIntFromString(m[1].str());
if (amount >= 0) {
if (amount >= 0)
{
*this = MacroAct(t, amount);
_macroLocation = specifiedMacroLocation;
return;
}
}
}
else if (MacroCommand::hasUnitArgument(t))
{
// There's an argument. Match the command name and parse out the argument.
std::regex commandWithArgRegex(commandName + " ([A-Za-z_ ]+)");
std::smatch m;
if (std::regex_match(inputName, m, commandWithArgRegex))
{
BWAPI::UnitType unitType = getUnitTypeFromString(m[1].str());
if (unitType != BWAPI::UnitTypes::Unknown && unitType != BWAPI::UnitTypes::None)
{
*this = MacroAct(t, unitType);
_macroLocation = specifiedMacroLocation;
return;
}
}
}
else
{
// No argument. Just compare for equality.
Expand All @@ -125,28 +185,12 @@ MacroAct::MacroAct(const std::string & name)
}
}

for (BWAPI::UnitType unitType : BWAPI::UnitTypes::allUnitTypes())
BWAPI::UnitType unitType = getUnitTypeFromString(inputName);
if (unitType != BWAPI::UnitTypes::Unknown && unitType != BWAPI::UnitTypes::None)
{
// Check whether the names match exactly.
std::string typeName = unitType.getName();
std::replace(typeName.begin(), typeName.end(), '_', ' ');
std::transform(typeName.begin(), typeName.end(), typeName.begin(), ::tolower);
if (typeName == inputName)
{
*this = MacroAct(unitType);
_macroLocation = specifiedMacroLocation;
return;
}

// Check whether the names match without the race prefix.
std::string raceName = unitType.getRace().getName();
std::transform(raceName.begin(), raceName.end(), raceName.begin(), ::tolower);
if ((typeName.length() > raceName.length()) && (typeName.compare(raceName.length() + 1, typeName.length(), inputName) == 0))
{
*this = MacroAct(unitType);
_macroLocation = specifiedMacroLocation;
return;
}
*this = MacroAct(unitType);
_macroLocation = specifiedMacroLocation;
return;
}

for (BWAPI::TechType techType : BWAPI::TechTypes::allTechTypes())
Expand All @@ -164,7 +208,7 @@ MacroAct::MacroAct(const std::string & name)

for (BWAPI::UpgradeType upgradeType : BWAPI::UpgradeTypes::allUpgradeTypes())
{
std::string typeName = upgradeType.getName();
std::string typeName = TrimRaceName(upgradeType.getName());
std::replace(typeName.begin(), typeName.end(), '_', ' ');
std::transform(typeName.begin(), typeName.end(), typeName.begin(), ::tolower);
if (typeName == inputName)
Expand Down Expand Up @@ -220,6 +264,13 @@ MacroAct::MacroAct(MacroCommandType t, int amount)
{
}

MacroAct::MacroAct(MacroCommandType t, BWAPI::UnitType type)
: _macroCommandType(t, type)
, _type(MacroActs::Command)
, _macroLocation(MacroLocation::Anywhere)
{
}

size_t MacroAct::type() const
{
return _type;
Expand Down Expand Up @@ -541,7 +592,7 @@ bool MacroAct::hasEventualProducer() const
// any condition that makes it unable to produce ever.
if (unit->getType() == producerType &&
unit->isPowered() && // replacing a pylon is a separate queue item
!unit->isLifted() && // lifting/landing a building will be a separate queue item when implemented
!unit->isLifted() && // lifting/landing a building is a separate queue item
(!producerType.isAddon() || unit->getAddon() == nullptr))
{
return true;
Expand Down
5 changes: 4 additions & 1 deletion Steamhammer/Source/MacroAct.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class MacroAct

MacroLocation _macroLocation;

MacroLocation getMacroLocationFromString(const std::string & s);
void initializeUnitTypesByName();
MacroLocation getMacroLocationFromString(const std::string & s) const;
BWAPI::UnitType getUnitTypeFromString(const std::string & s) const;

public:

Expand All @@ -48,6 +50,7 @@ class MacroAct
MacroAct(BWAPI::UpgradeType t);
MacroAct(MacroCommandType t);
MacroAct(MacroCommandType t, int amount);
MacroAct(MacroCommandType t, BWAPI::UnitType type);

bool isUnit() const;
bool isWorker() const;
Expand Down
Loading

0 comments on commit 78be93a

Please sign in to comment.