Skip to content

Commit

Permalink
new features
Browse files Browse the repository at this point in the history
- small bug fixes
- tank manager for terran
- enemy specific strats in config file
  • Loading branch information
davechurchill committed Sep 29, 2015
1 parent 34fafa9 commit 6ec25dd
Show file tree
Hide file tree
Showing 17 changed files with 368 additions and 81 deletions.
7 changes: 6 additions & 1 deletion BOSS/source/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ GameState::GameState(BWAPI::GameWrapper & game, BWAPI::PlayerInterface * self, c
continue;
}

const ActionType actionType(unit->getType());
ActionType actionType(unit->getType());

// if the unit is completed
if (unit->isCompleted())
Expand Down Expand Up @@ -155,6 +155,11 @@ GameState::GameState(BWAPI::GameWrapper & game, BWAPI::PlayerInterface * self, c
// otherwise it is a non-building unit
else
{
if (unit->getType() == BWAPI::UnitTypes::Terran_Siege_Tank_Siege_Mode)
{
actionType = ActionType(BWAPI::UnitTypes::Terran_Siege_Tank_Tank_Mode);
}

// add the unit to the state
_units.addCompletedAction(actionType, false);

Expand Down
4 changes: 2 additions & 2 deletions UAlbertaBot/Source/BOSSManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ void BOSSManager::drawSearchInformation(int x, int y)
y += 10;
BWAPI::Broodwar->drawTextScreen(BWAPI::Position(x, y), "%s", _previousStatus.c_str());

/*for (size_t i(0); i < _previousGoalUnits.size(); ++i)
for (size_t i(0); i < _previousGoalUnits.size(); ++i)
{
if (_previousGoalUnits[i].second > 0)
{
y += 10;
BWAPI::Broodwar->drawTextScreen(BWAPI::Position(x,y), "%d %s", _previousGoalUnits[i].second, _previousGoalUnits[i].first.getName().c_str());
}
}*/
}

BWAPI::Broodwar->drawTextScreen(BWAPI::Position(x, y+25), "Time (ms): %.3lf", _totalPreviousSearchTime);
BWAPI::Broodwar->drawTextScreen(BWAPI::Position(x, y+35), "Nodes: %d", _savedSearchResults.nodesExpanded);
Expand Down
2 changes: 1 addition & 1 deletion UAlbertaBot/Source/CombatCommander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void CombatCommander::initializeSquads()
BWAPI::Position ourBasePosition = BWAPI::Position(BWAPI::Broodwar->self()->getStartLocation());

// the scout defense squad will handle chasing the enemy worker scout
SquadOrder enemyScoutDefense(SquadOrderTypes::Defend, ourBasePosition, 500, "Get the scout");
SquadOrder enemyScoutDefense(SquadOrderTypes::Defend, ourBasePosition, 900, "Get the scout");
_squadData.addSquad("ScoutDefense", Squad("ScoutDefense", enemyScoutDefense, ScoutDefensePriority));

_initialized = true;
Expand Down
2 changes: 2 additions & 0 deletions UAlbertaBot/Source/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Config
std::string WriteDir = "bwapi-data/write/";
bool GasStealWithScout = false;
bool ScoutHarassEnemy = true;
bool UseEnemySpecificStrategy = false;
bool FoundEnemySpecificStrategy = false;
}

namespace Modules
Expand Down
5 changes: 2 additions & 3 deletions UAlbertaBot/Source/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ namespace Config

namespace Strategy
{
extern std::string ProtossStrategyName;
extern std::string TerranStrategyName;
extern std::string ZergStrategyName;
extern std::string StrategyName;
extern std::string ReadDir;
extern std::string WriteDir;
extern bool GasStealWithScout;
extern bool ScoutHarassEnemy;
extern bool UseEnemySpecificStrategy;
extern bool FoundEnemySpecificStrategy;
}

namespace BWAPIOptions
Expand Down
2 changes: 1 addition & 1 deletion UAlbertaBot/Source/GameCommander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void GameCommander::drawGameInformation(int x, int y)
y += 12;

BWAPI::Broodwar->drawTextScreen(x, y, "\x04Strategy:");
BWAPI::Broodwar->drawTextScreen(x+50, y, "\x03%s", Config::Strategy::StrategyName.c_str());
BWAPI::Broodwar->drawTextScreen(x+50, y, "\x03%s %s", Config::Strategy::StrategyName.c_str(), Config::Strategy::FoundEnemySpecificStrategy ? "(enemy specific)" : "");
BWAPI::Broodwar->setTextSize();
y += 12;

Expand Down
47 changes: 30 additions & 17 deletions UAlbertaBot/Source/ParseUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ using namespace UAlbertaBot;
void ParseUtils::ParseConfigFile(const std::string & filename)
{
rapidjson::Document doc;
BWAPI::Race race = BWAPI::Broodwar->self()->getRace();
const char * ourRace = race.getName().c_str();

std::string config = FileUtils::ReadFile(filename);

Expand Down Expand Up @@ -134,29 +136,38 @@ void ParseUtils::ParseConfigFile(const std::string & filename)
{
const rapidjson::Value & strategy = doc["Strategy"];

// read in the various strategic elements
JSONTools::ReadBool("ScoutGasSteal", strategy, Config::Strategy::GasStealWithScout);
JSONTools::ReadBool("ScoutHarassEnemy", strategy, Config::Strategy::ScoutHarassEnemy);
JSONTools::ReadString("ProtossStrategyName", strategy, Config::Strategy::ProtossStrategyName);
JSONTools::ReadString("TerranStrategyName", strategy, Config::Strategy::TerranStrategyName);
JSONTools::ReadString("ZergStrategyName", strategy, Config::Strategy::ZergStrategyName);

BWAPI::Race race = BWAPI::Broodwar->self()->getRace();
JSONTools::ReadString("ReadDirectory", strategy, Config::Strategy::ReadDir);
JSONTools::ReadString("WriteDirectory", strategy, Config::Strategy::WriteDir);

if (race == BWAPI::Races::Protoss)
{
Config::Strategy::StrategyName = Config::Strategy::ProtossStrategyName;
}
else if (race == BWAPI::Races::Terran)
// if we have set a strategy for the current race, use it
if (strategy.HasMember(race.c_str()) && strategy[race.c_str()].IsString())
{
Config::Strategy::StrategyName = Config::Strategy::TerranStrategyName;
Config::Strategy::StrategyName = strategy[race.c_str()].GetString();
}
else if (race == BWAPI::Races::Zerg)

// check if we are using an enemy specific strategy
JSONTools::ReadBool("UseEnemySpecificStrategy", strategy, Config::Strategy::UseEnemySpecificStrategy);
if (Config::Strategy::UseEnemySpecificStrategy && strategy.HasMember("EnemySpecificStrategy") && strategy["EnemySpecificStrategy"].IsObject())
{
Config::Strategy::StrategyName = Config::Strategy::ZergStrategyName;
}
const std::string enemyName = BWAPI::Broodwar->enemy()->getName();
const rapidjson::Value & specific = strategy["EnemySpecificStrategy"];

JSONTools::ReadString("ReadDirectory", strategy, Config::Strategy::ReadDir);
JSONTools::ReadString("WriteDirectory", strategy, Config::Strategy::WriteDir);
// check to see if our current enemy name is listed anywhere in the specific strategies
if (specific.HasMember(enemyName.c_str()) && specific[enemyName.c_str()].IsObject())
{
const rapidjson::Value & enemyStrategies = specific[enemyName.c_str()];

// if that enemy has a strategy listed for our current race, use it
if (enemyStrategies.HasMember(ourRace) && enemyStrategies[ourRace].IsString())
{
Config::Strategy::StrategyName = enemyStrategies[ourRace].GetString();
Config::Strategy::FoundEnemySpecificStrategy = true;
}
}
}

// Parse all the Strategies
if (strategy.HasMember("Strategies") && strategy["Strategies"].IsObject())
Expand All @@ -167,6 +178,7 @@ void ParseUtils::ParseConfigFile(const std::string & filename)
const std::string & name = itr->name.GetString();
const rapidjson::Value & val = itr->value;


BWAPI::Race strategyRace;
if (val.HasMember("Race") && val["Race"].IsString())
{
Expand Down Expand Up @@ -202,7 +214,8 @@ void ParseUtils::ParseConfigFile(const std::string & filename)
}
}

StrategyManager::Instance().addOpeningBuildOrder(name, buildOrder);
Strategy s = {name, strategyRace, buildOrder};
StrategyManager::Instance().addStrategy(name, s);
}
}
}
Expand Down
37 changes: 6 additions & 31 deletions UAlbertaBot/Source/RangedManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ RangedManager::RangedManager()
{
}

void testCopyIf()
{
std::set<BWAPI::Unit> vUnits;
BWAPI::Unitset setUnits;

const BWAPI::Unitset & allUnits = BWAPI::Broodwar->getAllUnits();

std::copy_if(allUnits.begin(), allUnits.end(), std::inserter(setUnits, setUnits.end()), UnitUtil::IsCombatUnit);
}

void RangedManager::executeMicro(const BWAPI::Unitset & targets)
{
const BWAPI::Unitset & rangedUnits = getUnits();
Expand Down Expand Up @@ -110,34 +100,14 @@ BWAPI::Unit RangedManager::getTarget(BWAPI::Unit rangedUnit, const BWAPI::Unitse
double closestDist = std::numeric_limits<double>::infinity();
BWAPI::Unit closestTarget = nullptr;

// 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 : targets)
{
// if our unit can't attack the target then skip it
if (!UnitUtil::CanAttack(rangedUnit, target))
{
continue;
}

double distance = rangedUnit->getDistance(target);
double LTD = UnitUtil::CalculateLTD(target, rangedUnit);
int priority = getAttackPriority(rangedUnit, target);
bool targetIsThreat = LTD > 0;
BWAPI::Broodwar->drawTextMap(target->getPosition(), "%d", priority);

//// calculate the highest threat in range of our attack
//if (targetIsThreat && targetsInRange.contains(target))
//{
// if (!bestTargetThreatInRange || (LTD > bestTargetThreatInRangeLTD))
// {
// bestTargetThreatInRangeLTD = LTD;
// bestTargetThreatInRange = target;
// }
//}

// secondary: attack highest priority non-threat target
// this way we don't chase after threats, we only attack them if they're a danger

if (!closestTarget || (priority > highPriority) || (priority == highPriority && distance < closestDist))
{
closestDist = distance;
Expand Down Expand Up @@ -172,6 +142,11 @@ int RangedManager::getAttackPriority(BWAPI::Unit rangedUnit, BWAPI::Unit target)
return 0;
}

if (rangedUnit->isFlying() && target->getType() == BWAPI::UnitTypes::Protoss_Carrier)
{
return 101;
}

// if the target is building something near our base something is fishy
BWAPI::Position ourBasePosition = BWAPI::Position(BWAPI::Broodwar->self()->getStartLocation());
if (target->getType().isWorker() && (target->isConstructing() || target->isRepairing()) && target->getDistance(ourBasePosition) < 1200)
Expand Down
9 changes: 8 additions & 1 deletion UAlbertaBot/Source/Squad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void Squad::update()
{
_meleeManager.execute(_order);
_rangedManager.execute(_order);
_tankManager.execute(_order);
_transportManager.execute(_order);

_detectorManager.setUnitClosestToEnemy(unitClosestToEnemy());
Expand Down Expand Up @@ -139,14 +140,19 @@ void Squad::addUnitsToMicroManagers()
BWAPI::Unitset rangedUnits;
BWAPI::Unitset detectorUnits;
BWAPI::Unitset transportUnits;
BWAPI::Unitset tankUnits;

// add _units to micro managers
for (auto & unit : _units)
{
if(unit->isCompleted() && unit->getHitPoints() > 0 && unit->exists())
{
// select dector _units
if (unit->getType().isDetector() && !unit->getType().isBuilding())
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);
}
Expand All @@ -172,6 +178,7 @@ void Squad::addUnitsToMicroManagers()
_rangedManager.setUnits(rangedUnits);
_detectorManager.setUnits(detectorUnits);
_transportManager.setUnits(detectorUnits);
_tankManager.setUnits(tankUnits);
}

// calculates whether or not to regroup
Expand Down
2 changes: 2 additions & 0 deletions UAlbertaBot/Source/Squad.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "DistanceMap.hpp"
#include "StrategyManager.h"
#include "CombatSimulation.h"
#include "TankManager.h"

namespace UAlbertaBot
{
Expand All @@ -27,6 +28,7 @@ class Squad
RangedManager _rangedManager;
DetectorManager _detectorManager;
TransportManager _transportManager;
TankManager _tankManager;

std::map<BWAPI::Unit, bool> _nearEnemy;

Expand Down
24 changes: 18 additions & 6 deletions UAlbertaBot/Source/StrategyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const BuildOrder & StrategyManager::getOpeningBookBuildOrder() const
// look for the build order in the build order map
if (buildOrderIt != std::end(_openingBuildOrders))
{
return (*buildOrderIt).second;
return (*buildOrderIt).second.buildOrder;
}
else
{
Expand Down Expand Up @@ -110,9 +110,9 @@ const bool StrategyManager::shouldExpandNow() const
return false;
}

void StrategyManager::addOpeningBuildOrder(const std::string & name, BuildOrder & buildOrder)
void StrategyManager::addStrategy(const std::string & name, Strategy & strategy)
{
_openingBuildOrders[name] = buildOrder;
_openingBuildOrders[name] = strategy;
}

const MetaPairVector StrategyManager::getBuildOrderGoal()
Expand Down Expand Up @@ -161,7 +161,7 @@ const MetaPairVector StrategyManager::getProtossBuildOrderGoal() const
goal.push_back(MetaPair(BWAPI::UnitTypes::Protoss_Dragoon, numDragoons + 4));
}
}
else if (Config::Strategy::StrategyName == "Protoss_DragoontRush")
else if (Config::Strategy::StrategyName == "Protoss_DragoonRush")
{
goal.push_back(MetaPair(BWAPI::UnitTypes::Protoss_Dragoon, numDragoons + 6));
}
Expand Down Expand Up @@ -210,7 +210,6 @@ const MetaPairVector StrategyManager::getProtossBuildOrderGoal() const
return goal;
}


const MetaPairVector StrategyManager::getTerranBuildOrderGoal() const
{
// the goal to return
Expand All @@ -222,16 +221,29 @@ const MetaPairVector StrategyManager::getTerranBuildOrderGoal() const
int numMedics = UnitUtil::GetAllUnitCount(BWAPI::UnitTypes::Terran_Medic);
int numWraith = UnitUtil::GetAllUnitCount(BWAPI::UnitTypes::Terran_Wraith);
int numVultures = UnitUtil::GetAllUnitCount(BWAPI::UnitTypes::Terran_Vulture);
int numGoliath = UnitUtil::GetAllUnitCount(BWAPI::UnitTypes::Terran_Goliath);
int numTanks = UnitUtil::GetAllUnitCount(BWAPI::UnitTypes::Terran_Siege_Tank_Tank_Mode)
+ UnitUtil::GetAllUnitCount(BWAPI::UnitTypes::Terran_Siege_Tank_Siege_Mode);

if (Config::Strategy::StrategyName == "Terran_MarineRush")
{
goal.push_back(std::pair<MetaType, int>(BWAPI::UnitTypes::Terran_Marine, numMarines + 13));
goal.push_back(std::pair<MetaType, int>(BWAPI::UnitTypes::Terran_Marine, numMarines + 8));
}
else if (Config::Strategy::StrategyName == "Terran_VultureRush")
{
goal.push_back(std::pair<MetaType, int>(BWAPI::UnitTypes::Terran_Vulture, numVultures + 8));
goal.push_back(std::pair<MetaType, int>(BWAPI::TechTypes::Spider_Mines, 1));
}
else if (Config::Strategy::StrategyName == "Terran_TankPush")
{
goal.push_back(std::pair<MetaType, int>(BWAPI::UnitTypes::Terran_Siege_Tank_Tank_Mode, 6));
goal.push_back(std::pair<MetaType, int>(BWAPI::UnitTypes::Terran_Goliath, numGoliath + 6));
goal.push_back(std::pair<MetaType, int>(BWAPI::TechTypes::Tank_Siege_Mode, 1));
}
else
{
BWAPI::Broodwar->printf("Warning: No build order goal for Terran Strategy: %s", Config::Strategy::StrategyName.c_str());
}

if (shouldExpandNow())
{
Expand Down
Loading

0 comments on commit 6ec25dd

Please sign in to comment.