From e5c2079866b11cd40cb46652f762c182268359cd Mon Sep 17 00:00:00 2001 From: A Date: Tue, 11 Apr 2023 23:30:14 -0500 Subject: [PATCH] bot defends against zergling rushes now, added now, added unsetTargetUnit, droneDefenseNeeded, among others --- Source/CUnit.cpp | 7 +++++++ Source/CUnit.h | 2 ++ Source/CheckItem.cpp | 9 +++++++++ Source/Korabot.cpp | 6 +++--- Source/Macro.cpp | 1 + Source/Military.cpp | 38 +++++++++++++++++++++++++++++--------- Source/Military.h | 1 + Source/Squad.cpp | 11 +++++++++++ Source/Squad.h | 1 + 9 files changed, 64 insertions(+), 12 deletions(-) diff --git a/Source/CUnit.cpp b/Source/CUnit.cpp index 119f2a6..bd85bc9 100644 --- a/Source/CUnit.cpp +++ b/Source/CUnit.cpp @@ -21,6 +21,7 @@ void CUnit::initialize() this->isScout = false; this->lastSeen = TilePosition(0, 0); this->miningBase = -1; + this->squadIndex = -1; this->target = TilePosition(-1, -1); } @@ -53,4 +54,10 @@ void CUnit::mine(std::string action, int baseIndex) { this->action = action; this->miningBase = baseIndex; +} + +void CUnit::unsetTargetUnit() +{ + this->targetUnit = UnitTypes::None; + Macro::queue.at(Util::getQueueIndex(this->targetUnit)).inProgress = 0; } \ No newline at end of file diff --git a/Source/CUnit.h b/Source/CUnit.h index 7a71008..2efd096 100644 --- a/Source/CUnit.h +++ b/Source/CUnit.h @@ -26,9 +26,11 @@ public: int miningBase; int resourceType; int resourceVal; + int squadIndex; BWAPI::TilePosition target; BWAPI::UnitType targetUnit; TilePosition tilePosition; BWAPI::Unit unit; BWAPI::UnitType unitType; + void unsetTargetUnit(); }; diff --git a/Source/CheckItem.cpp b/Source/CheckItem.cpp index b00ad55..efbf3b7 100644 --- a/Source/CheckItem.cpp +++ b/Source/CheckItem.cpp @@ -510,6 +510,15 @@ void CheckItem::checkWorkers() Macro::freeUnit(i); }*/ } + else if (Macro::players.at(Macro::selfID).units.at(i).action == "defend") + { + //@TODO bot doesn't build stuff + int squadIndex = Macro::players.at(Macro::selfID).units.at(i).squadIndex; + if (Military::likelihoodToAttack() < 1 && squadIndex > -1) + { + Macro::squads.at(squadIndex).freeDrones(); + } + } } //std::chrono::duration elapsed = std::chrono::high_resolution_clock::now() - start; //if(elapsed.count() > 1) diff --git a/Source/Korabot.cpp b/Source/Korabot.cpp index 4eba1d4..2389052 100644 --- a/Source/Korabot.cpp +++ b/Source/Korabot.cpp @@ -105,7 +105,7 @@ void KoraBot::onStart() Broodwar << "EXCEPTION: " << e.what() << std::endl; } - Broodwar << "KoraBot 0.0" << std::endl; + Broodwar << "KoraBot 1.0" << std::endl; } } @@ -183,10 +183,10 @@ void KoraBot::onFrame() if (Broodwar->getFrameCount() % Broodwar->getLatencyFrames() != 0) return; - // in the top right spawn of jade, exposted to center is false for the natural unless it's run later + // in the top right spawn of jade, exposed to center is false for the natural unless it's run later if (Broodwar->getFrameCount() == 240 * 24) { - for (unsigned int i = 0; i < Macro::players.at(Macro::selfID).bases.size(); i++) + for (size_t i = 0; i < Macro::players.at(Macro::selfID).bases.size(); i++) { Macro::players.at(Macro::selfID).bases.at(i).exposedToCenter = Macro::players.at(Macro::selfID).bases.at(i).isExposedToCenter(); } diff --git a/Source/Macro.cpp b/Source/Macro.cpp index 0d9ed7a..597013c 100644 --- a/Source/Macro.cpp +++ b/Source/Macro.cpp @@ -112,6 +112,7 @@ void Macro::analyzeQueue() else { players.at(selfID).units.at(workerIndex).targetUnit = queue.at(index).unit; + target = BuildingPlacement::getPosition(players.at(selfID).units.at(workerIndex).targetUnit); } players.at(selfID).units.at(workerIndex).target = target; } diff --git a/Source/Military.cpp b/Source/Military.cpp index dbeda34..b7747a9 100644 --- a/Source/Military.cpp +++ b/Source/Military.cpp @@ -131,6 +131,7 @@ void Military::checkDefense() }*/ } +// returns how much attack power is at the specified CBase double Military::checkEnemiesAt(CBase b) { double score = 0; @@ -186,6 +187,13 @@ void Military::checkSquads() } } +// for defending in rushes or other low-eco situations +bool Military::droneDefenseNeeded() +{ + return (Util::countUnits(Macro::selfID, BWAPI::UnitTypes::Zerg_Zergling) < 10 + && Util::countUnits(Macro::selfID, BWAPI::UnitTypes::Zerg_Hydralisk) < 10); +} + std::pair Military::getAttackLocation() { // start here - pick the most vulnerable base (getbasedefense) and attack @@ -356,12 +364,12 @@ bool Military::inThreatRange(TilePosition tp, CUnit unit) return Util::getDistance(tp, unit.unit->getTilePosition()) < unit.unit->getType().seekRange() + 1; } +// returns how likely on a scale of 0 to 1 the enemy is to attack soon +// things to consider: +// how long it's been since the last time they attacked +// how many units the enemy might have double Military::likelihoodToAttack() { - // things to consider: - // how long it's been since the last time they attacked - // how many units the enemy might have - double score = 1; double timeModifier = 0; double attackTime = 360.0 * 24; @@ -389,6 +397,8 @@ double Military::likelihoodToAttack() return score * timeModifier; } + +// sends a squad to defend // things to consider: // how important the place being attacked is // if a bigger win can be found by attacking instead (base race) @@ -397,26 +407,36 @@ double Military::likelihoodToAttack() void Military::sendDefense(TilePosition tp, int attackerScore, int defenseScore) { int squadIndex = getClosestSquad(tp); - if (squadIndex == -1) squadIndex = 0; - int padding = 80; // give the defense some breathing room + if (squadIndex == -1) + { + squadIndex = 0; + Macro::squads.push_back(Squad()); + } + int padding = 80; // give the defense some breathing room by spotting the attackers some damage Macro::squads.at(squadIndex).action = "defend"; if (defenseScore + Macro::squads.at(squadIndex).groundDamage < attackerScore + padding) { + int index = 0; for (auto u : Macro::players.at(Macro::selfID).units) { if (defenseScore + Macro::squads.at(squadIndex).groundDamage < attackerScore + padding) { - if (Util::isAttackingUnit(u) - && !u.isInSquad() - ) + if ((Util::isAttackingUnit(u) || droneDefenseNeeded()) && !u.isInSquad()) { Macro::squads.at(squadIndex).addUnit(u); + u.squadIndex = squadIndex; + if (u.unit->getType() == UnitTypes::Zerg_Drone) + { + Macro::freeUnit(index); + u.unsetTargetUnit(); + } /*Macro::squads.at(squadIndex).units.push_back(u); Macro::squads.at(squadIndex).airDamage += u.unitType.airWeapon().damageAmount(); Macro::squads.at(squadIndex).groundDamage += u.unitType.groundWeapon().damageAmount();*/ } } + index++; } } for (auto u : Macro::squads.at(squadIndex).units) diff --git a/Source/Military.h b/Source/Military.h index 28b105e..b4880d9 100644 --- a/Source/Military.h +++ b/Source/Military.h @@ -17,6 +17,7 @@ public: static void checkDefense(); static double checkEnemiesAt(CBase base); static void checkSquads(); + static bool droneDefenseNeeded(); static std::pair getAttackLocation(); static int getAttackSquadsIndex(); static double getBaseDefense(CBase b, std::string type, int playerID); diff --git a/Source/Squad.cpp b/Source/Squad.cpp index 12a6988..b8069b2 100644 --- a/Source/Squad.cpp +++ b/Source/Squad.cpp @@ -48,6 +48,17 @@ bool Squad::containsType(UnitType ut) return false; } +void Squad::freeDrones() +{ + for (auto u : this->units) + { + if (u.unit->getType() == UnitTypes::Zerg_Drone) + { + Macro::freeUnit(Util::getUnitIndex(u.unit)); + } + } +} + void Squad::updateCenter() { int averageX = 0; diff --git a/Source/Squad.h b/Source/Squad.h index a606841..bb0f497 100644 --- a/Source/Squad.h +++ b/Source/Squad.h @@ -21,6 +21,7 @@ public: void attack(std::pair baseToAttack);; bool contains(CUnit unit); bool containsType(UnitType ut); + void freeDrones(); void updateCenter(); void updateDamage(); }; \ No newline at end of file