Skip to content

Commit

Permalink
Fixed bugs
Browse files Browse the repository at this point in the history
Note to anyone ever reading this, Broodwar->isAlly(Broodwar->self()) returns true, this took me 4 hours to find and solve.
  • Loading branch information
Cmccrave committed Jan 21, 2019
1 parent 22fc823 commit 5eae7c9
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 111 deletions.
3 changes: 1 addition & 2 deletions Source/McRave/BuildingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ namespace McRave::Buildings {
// If we are fast expanding
auto isWallPiece = building == UnitTypes::Protoss_Forge || building == UnitTypes::Protoss_Gateway || building == UnitTypes::Protoss_Pylon || building == UnitTypes::Terran_Barracks || building == UnitTypes::Terran_Supply_Depot || building == UnitTypes::Zerg_Evolution_Chamber || building == UnitTypes::Zerg_Hatchery || building == UnitTypes::Zerg_Creep_Colony;
if (BWEB::Map::getNaturalChoke() && isWallPiece && !Strategy::enemyBust() && (BuildOrder::isWallNat() || BuildOrder::isWallMain())) {
Broodwar << "yeah" << endl;
here = findWallLocation(building, BWEB::Map::getMainPosition());
if (here.isValid() && isBuildable(building, here))
return here;
Expand Down Expand Up @@ -480,7 +479,7 @@ namespace McRave::Buildings {
if (building.getType() == UnitTypes::Terran_Comsat_Station) {
if (building.hasTarget() && building.getTarget().unit()->exists() && !Command::overlapsAllyDetection(building.getTarget().getPosition())) {
building.unit()->useTech(TechTypes::Scanner_Sweep, building.getTarget().getPosition());
Command::addCommand(building.unit(), building.getTarget().getPosition(), TechTypes::Scanner_Sweep);
Command::addAction(building.unit(), building.getTarget().getPosition(), TechTypes::Scanner_Sweep);
}
}

Expand Down
10 changes: 5 additions & 5 deletions Source/McRave/CombatManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ namespace McRave::Combat {
unit.setLocalState(LocalState::Retreat);
}
else if (unit.getGlobalState() == GlobalState::Attack) {
unit.setLocalState(LocalState::Retreat);
unit.setLocalState(LocalState::Attack);
}
else {
unit.setLocalState(LocalState::Attack);
unit.setLocalState(LocalState::Retreat);
}
}
else if (unit.getGlobalState() == GlobalState::Attack) {
unit.setLocalState(LocalState::Retreat);
unit.setLocalState(LocalState::Attack);
}
else {
unit.setLocalState(LocalState::Attack);
unit.setLocalState(LocalState::Retreat);
}
}

Expand Down Expand Up @@ -139,7 +139,7 @@ namespace McRave::Combat {
}
else {
for (auto &start : Broodwar->getStartLocations()) {
if (start.isValid() && !Broodwar->isExplored(start) && !Command::overlapsCommands(unit.unit(), unit.getType(), Position(start), 32)) {
if (start.isValid() && !Broodwar->isExplored(start) && !Command::overlapsActions(unit.unit(), unit.getType(), Position(start), 32)) {
unit.setDestination(Position(start));
}
}
Expand Down
44 changes: 24 additions & 20 deletions Source/McRave/CommandManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ namespace McRave::Command {
void updateCommands()
{
// Clear cache
myCommands.clear();
enemyCommands.clear();
myActions.clear();
enemyActions.clear();

// Store bullets as enemy units if they can affect us too
for (auto &b : Broodwar->getBullets()) {
if (b && b->exists()) {
if (b->getType() == BulletTypes::Psionic_Storm)
addCommand(nullptr, b->getPosition(), TechTypes::Psionic_Storm, true);
addAction(nullptr, b->getPosition(), TechTypes::Psionic_Storm, true);

if (b->getType() == BulletTypes::EMP_Missile)
addCommand(nullptr, b->getTargetPosition(), TechTypes::EMP_Shockwave, true);
addAction(nullptr, b->getTargetPosition(), TechTypes::EMP_Shockwave, true);
}
}

Expand All @@ -32,25 +32,25 @@ namespace McRave::Command {
continue;

if (unit.getType().isDetector())
addCommand(unit.unit(), unit.getPosition(), unit.getType(), true);
addAction(unit.unit(), unit.getPosition(), unit.getType(), true);

if (unit.unit()->exists()) {
Order enemyOrder = unit.unit()->getOrder();
Position enemyTarget = unit.unit()->getOrderTargetPosition();

if (enemyOrder == Orders::CastEMPShockwave)
addCommand(unit.unit(), enemyTarget, TechTypes::EMP_Shockwave, true);
addAction(unit.unit(), enemyTarget, TechTypes::EMP_Shockwave, true);
if (enemyOrder == Orders::CastPsionicStorm)
addCommand(unit.unit(), enemyTarget, TechTypes::Psionic_Storm, true);
addAction(unit.unit(), enemyTarget, TechTypes::Psionic_Storm, true);
}

if (unit.getType() == UnitTypes::Terran_Vulture_Spider_Mine)
addCommand(unit.unit(), unit.getPosition(), TechTypes::Spider_Mines, true);
addAction(unit.unit(), unit.getPosition(), TechTypes::Spider_Mines, true);
}

// Store nuke dots
for (auto &dot : Broodwar->getNukeDots())
addCommand(nullptr, dot, TechTypes::Nuclear_Strike, true);
addAction(nullptr, dot, TechTypes::Nuclear_Strike, true);
}
}

Expand Down Expand Up @@ -394,6 +394,10 @@ namespace McRave::Command {
else
unit.command(UnitCommandTypes::Move, BWEB::Map::getNaturalPosition());
}
else {
unit.command(UnitCommandTypes::Move, BWEB::Map::getMainPosition());
return true;
}
return false;
}

Expand Down Expand Up @@ -429,7 +433,7 @@ namespace McRave::Command {

const auto shouldHunt = [&]() {
if (unit.getRole() == Role::Combat) {
if (unit.getType().isWorker() || unit.isLightAir())
if (unit.isLightAir())
return true;
}
if (unit.getRole() == Role::Transport || unit.getRole() == Role::Scout)
Expand Down Expand Up @@ -512,15 +516,15 @@ namespace McRave::Command {
return false;
}

bool overlapsCommands(Unit unit, TechType tech, Position here, int radius)
bool overlapsActions(Unit unit, TechType tech, Position here, int radius)
{
auto checkTopLeft = here + Position(-radius / 2, -radius / 2);
auto checkTopRight = here + Position(radius / 2, -radius / 2);
auto checkBotLeft = here + Position(-radius / 2, radius / 2);
auto checkBotRight = here + Position(radius / 2, radius / 2);

// TechType checks use a rectangular check
for (auto &command : myCommands) {
for (auto &command : myActions) {
auto topLeft = command.pos - Position(radius, radius);
auto botRight = command.pos + Position(radius, radius);

Expand All @@ -534,10 +538,10 @@ namespace McRave::Command {
return false;
}

bool overlapsCommands(Unit unit, UnitType type, Position here, int radius)
bool overlapsActions(Unit unit, UnitType type, Position here, int radius)
{
// UnitType checks use a radial check
for (auto &command : myCommands) {
for (auto &command : myActions) {
if (command.unit != unit && command.type == type && command.pos.getDistance(here) <= radius * 2)
return true;
}
Expand All @@ -547,7 +551,7 @@ namespace McRave::Command {
bool overlapsAllyDetection(Position here)
{
// Detection checks use a radial check
for (auto &command : myCommands) {
for (auto &command : myActions) {
if (command.type == UnitTypes::Spell_Scanner_Sweep) {
double range = 420.0;
if (command.pos.getDistance(here) < range)
Expand All @@ -565,7 +569,7 @@ namespace McRave::Command {
bool overlapsEnemyDetection(Position here)
{
// Detection checks use a radial check
for (auto &command : enemyCommands) {
for (auto &command : enemyActions) {
if (command.type == UnitTypes::Spell_Scanner_Sweep) {
double range = 420.0;
double ff = 32;
Expand Down Expand Up @@ -606,7 +610,7 @@ namespace McRave::Command {
};

// Check that we're not in danger of Storm, DWEB, EMP
for (auto &command : myCommands) {
for (auto &command : myActions) {
if (command.tech == TechTypes::Psionic_Storm
|| command.tech == TechTypes::Disruption_Web
|| command.tech == TechTypes::EMP_Shockwave
Expand All @@ -627,7 +631,7 @@ namespace McRave::Command {
}
}

for (auto &command : enemyCommands) {
for (auto &command : enemyActions) {
if (command.tech == TechTypes::Psionic_Storm
|| command.tech == TechTypes::Disruption_Web
|| command.tech == TechTypes::EMP_Shockwave
Expand Down Expand Up @@ -821,6 +825,6 @@ namespace McRave::Command {
Visuals::endPerfTest("Commands");
}

vector <CommandType>& getMyCommands() { return myCommands; }
vector <CommandType>& getEnemyCommands() { return enemyCommands; }
vector <Action>& getMyActions() { return myActions; }
vector <Action>& getEnemyActions() { return enemyActions; }
}
23 changes: 12 additions & 11 deletions Source/McRave/CommandManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,34 @@

namespace McRave::Command {

struct CommandType
struct Action
{
BWAPI::UnitType type = BWAPI::UnitTypes::None;
BWAPI::TechType tech = BWAPI::TechTypes::None;
BWAPI::Position pos = BWAPI::Positions::None;
BWAPI::Unit unit = nullptr;
int frame = 0;

CommandType(BWAPI::Unit u, BWAPI::Position p, BWAPI::TechType t) { unit = u, pos = p, tech = t, frame = BWAPI::Broodwar->getFrameCount(); }
CommandType(BWAPI::Unit u, BWAPI::Position p, BWAPI::UnitType t) { unit = u, pos = p, type = t, frame = BWAPI::Broodwar->getFrameCount(); }
Action(BWAPI::Unit u, BWAPI::Position p, BWAPI::TechType t) { unit = u, pos = p, tech = t, frame = BWAPI::Broodwar->getFrameCount(); }
Action(BWAPI::Unit u, BWAPI::Position p, BWAPI::UnitType t) { unit = u, pos = p, type = t, frame = BWAPI::Broodwar->getFrameCount(); }

friend bool operator< (const CommandType &left, const CommandType &right) {
friend bool operator< (const Action &left, const Action &right) {
return left.frame < right.frame;
}
};

BWAPI::Position findViablePosition(UnitInfo&, std::function<double(BWAPI::WalkPosition)>);
void onFrame();
bool overlapsCommands(BWAPI::Unit, BWAPI::TechType, BWAPI::Position, int);
bool overlapsCommands(BWAPI::Unit, BWAPI::UnitType, BWAPI::Position, int);
bool overlapsActions(BWAPI::Unit, BWAPI::TechType, BWAPI::Position, int);
bool overlapsActions(BWAPI::Unit, BWAPI::UnitType, BWAPI::Position, int);
bool overlapsAllyDetection(BWAPI::Position);
bool overlapsEnemyDetection(BWAPI::Position);
bool isInDanger(UnitInfo&, BWAPI::Position here = BWAPI::Positions::Invalid);

bool misc(UnitInfo&);
bool move(UnitInfo&);
bool approach(UnitInfo&);
bool surround(UnitInfo&);
bool defend(UnitInfo&);
bool kite(UnitInfo&);
bool attack(UnitInfo&);
Expand All @@ -39,14 +40,14 @@ namespace McRave::Command {
bool retreat(UnitInfo&);

namespace {
inline std::vector <CommandType> myCommands;
inline std::vector <CommandType> enemyCommands;
inline std::vector <Action> myActions;
inline std::vector <Action> enemyActions;
}

// Adds a UnitType or TechType command at a Position
template<class T>
void addCommand(BWAPI::Unit unit, BWAPI::Position here, T type, bool enemy = false) {
auto &commands = enemy ? enemyCommands : myCommands;
commands.push_back(CommandType(unit, here, type));
void addAction(BWAPI::Unit unit, BWAPI::Position here, T type, bool enemy = false) {
auto &commands = enemy ? enemyActions : myActions;
commands.push_back(Action(unit, here, type));
}
}
8 changes: 4 additions & 4 deletions Source/McRave/GridManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ namespace McRave::Grids
resetGrid[x][y] = 0;
}
resetVector.clear();
Visuals::endPerfTest("Grid:Reset");
Visuals::endPerfTest("Grid Reset");
}

void updateAlly()
Expand Down Expand Up @@ -187,7 +187,7 @@ namespace McRave::Grids
else if (!unit.unit()->isLoaded())
addToGrids(unit);
}
Visuals::endPerfTest("Grid:Self");
Visuals::endPerfTest("Grid Self");
}

void updateEnemy()
Expand All @@ -208,7 +208,7 @@ namespace McRave::Grids
addToGrids(unit);
}
}
Visuals::endPerfTest("Grid:Enemy");
Visuals::endPerfTest("Grid Enemy");
}

void updateNeutral()
Expand All @@ -234,7 +234,7 @@ namespace McRave::Grids
}

if (u->getType() == UnitTypes::Spell_Disruption_Web)
Command::addCommand(u, u->getPosition(), TechTypes::Disruption_Web);
Command::addAction(u, u->getPosition(), TechTypes::Disruption_Web);
}
}

Expand Down
6 changes: 2 additions & 4 deletions Source/McRave/McRave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
// Check Muta micro to ensure we are overshooting movement to not decel near targets
// Unit Interface get distance is edge to point, lots of mistakes!
// Re-add mine targets
// Re-add defending at chokes

// *** TOTEST ***
// All units are stored in PlayerInfo objects
// If an enemy floats a CC to an expansion, we don't consider it "taken"
/// Added a customOnUnitLand in EventManager.h which should take care of this
// Test lifting buildings
Expand All @@ -39,7 +39,6 @@
// Walkable grid cached, only check collision at corners + center when looking for walkable positions for a unit
// Use Player pointer instead of BWAPI::Player pointer in UnitInfo, gives advantage of knowing upgrades/tech that are available always
// Use player filters to grab unit set in getClosestUnit template
// PlayerInfo stores all units for each player - big restructure

using namespace BWAPI;
using namespace std;
Expand Down Expand Up @@ -113,7 +112,7 @@ void McRaveModule::onPlayerLeft(Player player)

void McRaveModule::onNukeDetect(Position target)
{
Command::addCommand(nullptr, target, TechTypes::Nuclear_Strike);
Command::addAction(nullptr, target, TechTypes::Nuclear_Strike);
}

void McRaveModule::onUnitDiscover(Unit unit)
Expand Down Expand Up @@ -159,6 +158,5 @@ void McRaveModule::onSaveGame(string gameName)

void McRaveModule::onUnitComplete(Unit unit)
{
Broodwar << "McRave" << endl;
Events::onUnitComplete(unit);
}
8 changes: 4 additions & 4 deletions Source/McRave/PlayerInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ namespace McRave
}

// Set current allied status
if (thisPlayer->isEnemy(BWAPI::Broodwar->self()))
if (thisPlayer->getID() == BWAPI::Broodwar->self()->getID())
pState = PlayerState::Self;
else if (thisPlayer->isEnemy(BWAPI::Broodwar->self()))
pState = PlayerState::Enemy;
else if (thisPlayer->isAlly(BWAPI::Broodwar->self()))
pState = PlayerState::Ally;
else if (thisPlayer == BWAPI::Broodwar->self())
pState = PlayerState::Self;
else
else
pState = PlayerState::None;

currentRace = alive ? thisPlayer->getRace() : BWAPI::Races::None;
Expand Down
Loading

0 comments on commit 5eae7c9

Please sign in to comment.