bot defends against zergling rushes now, added now, added unsetTargetUnit, droneDefenseNeeded, among others

This commit is contained in:
A 2023-04-11 23:30:14 -05:00
parent 71f26b01ee
commit e5c2079866
9 changed files with 64 additions and 12 deletions

View File

@ -21,6 +21,7 @@ void CUnit::initialize()
this->isScout = false; this->isScout = false;
this->lastSeen = TilePosition(0, 0); this->lastSeen = TilePosition(0, 0);
this->miningBase = -1; this->miningBase = -1;
this->squadIndex = -1;
this->target = TilePosition(-1, -1); this->target = TilePosition(-1, -1);
} }
@ -53,4 +54,10 @@ void CUnit::mine(std::string action, int baseIndex)
{ {
this->action = action; this->action = action;
this->miningBase = baseIndex; this->miningBase = baseIndex;
}
void CUnit::unsetTargetUnit()
{
this->targetUnit = UnitTypes::None;
Macro::queue.at(Util::getQueueIndex(this->targetUnit)).inProgress = 0;
} }

View File

@ -26,9 +26,11 @@ public:
int miningBase; int miningBase;
int resourceType; int resourceType;
int resourceVal; int resourceVal;
int squadIndex;
BWAPI::TilePosition target; BWAPI::TilePosition target;
BWAPI::UnitType targetUnit; BWAPI::UnitType targetUnit;
TilePosition tilePosition; TilePosition tilePosition;
BWAPI::Unit unit; BWAPI::Unit unit;
BWAPI::UnitType unitType; BWAPI::UnitType unitType;
void unsetTargetUnit();
}; };

View File

@ -510,6 +510,15 @@ void CheckItem::checkWorkers()
Macro::freeUnit(i); 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<double, std::milli> elapsed = std::chrono::high_resolution_clock::now() - start; //std::chrono::duration<double, std::milli> elapsed = std::chrono::high_resolution_clock::now() - start;
//if(elapsed.count() > 1) //if(elapsed.count() > 1)

View File

@ -105,7 +105,7 @@ void KoraBot::onStart()
Broodwar << "EXCEPTION: " << e.what() << std::endl; 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) if (Broodwar->getFrameCount() % Broodwar->getLatencyFrames() != 0)
return; 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) 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(); Macro::players.at(Macro::selfID).bases.at(i).exposedToCenter = Macro::players.at(Macro::selfID).bases.at(i).isExposedToCenter();
} }

View File

@ -112,6 +112,7 @@ void Macro::analyzeQueue()
else else
{ {
players.at(selfID).units.at(workerIndex).targetUnit = queue.at(index).unit; 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; players.at(selfID).units.at(workerIndex).target = target;
} }

View File

@ -131,6 +131,7 @@ void Military::checkDefense()
}*/ }*/
} }
// returns how much attack power is at the specified CBase
double Military::checkEnemiesAt(CBase b) double Military::checkEnemiesAt(CBase b)
{ {
double score = 0; 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<TilePosition, int> Military::getAttackLocation() std::pair<TilePosition, int> Military::getAttackLocation()
{ {
// start here - pick the most vulnerable base (getbasedefense) and attack // 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; 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() 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 score = 1;
double timeModifier = 0; double timeModifier = 0;
double attackTime = 360.0 * 24; double attackTime = 360.0 * 24;
@ -389,6 +397,8 @@ double Military::likelihoodToAttack()
return score * timeModifier; return score * timeModifier;
} }
// sends a squad to defend
// things to consider: // things to consider:
// how important the place being attacked is // how important the place being attacked is
// if a bigger win can be found by attacking instead (base race) // 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) void Military::sendDefense(TilePosition tp, int attackerScore, int defenseScore)
{ {
int squadIndex = getClosestSquad(tp); int squadIndex = getClosestSquad(tp);
if (squadIndex == -1) squadIndex = 0; if (squadIndex == -1)
int padding = 80; // give the defense some breathing room {
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"; Macro::squads.at(squadIndex).action = "defend";
if (defenseScore + Macro::squads.at(squadIndex).groundDamage < attackerScore + padding) if (defenseScore + Macro::squads.at(squadIndex).groundDamage < attackerScore + padding)
{ {
int index = 0;
for (auto u : Macro::players.at(Macro::selfID).units) for (auto u : Macro::players.at(Macro::selfID).units)
{ {
if (defenseScore + Macro::squads.at(squadIndex).groundDamage < attackerScore + padding) if (defenseScore + Macro::squads.at(squadIndex).groundDamage < attackerScore + padding)
{ {
if (Util::isAttackingUnit(u) if ((Util::isAttackingUnit(u) || droneDefenseNeeded()) && !u.isInSquad())
&& !u.isInSquad()
)
{ {
Macro::squads.at(squadIndex).addUnit(u); 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).units.push_back(u);
Macro::squads.at(squadIndex).airDamage += u.unitType.airWeapon().damageAmount(); Macro::squads.at(squadIndex).airDamage += u.unitType.airWeapon().damageAmount();
Macro::squads.at(squadIndex).groundDamage += u.unitType.groundWeapon().damageAmount();*/ Macro::squads.at(squadIndex).groundDamage += u.unitType.groundWeapon().damageAmount();*/
} }
} }
index++;
} }
} }
for (auto u : Macro::squads.at(squadIndex).units) for (auto u : Macro::squads.at(squadIndex).units)

View File

@ -17,6 +17,7 @@ public:
static void checkDefense(); static void checkDefense();
static double checkEnemiesAt(CBase base); static double checkEnemiesAt(CBase base);
static void checkSquads(); static void checkSquads();
static bool droneDefenseNeeded();
static std::pair<TilePosition, int> getAttackLocation(); static std::pair<TilePosition, int> getAttackLocation();
static int getAttackSquadsIndex(); static int getAttackSquadsIndex();
static double getBaseDefense(CBase b, std::string type, int playerID); static double getBaseDefense(CBase b, std::string type, int playerID);

View File

@ -48,6 +48,17 @@ bool Squad::containsType(UnitType ut)
return false; 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() void Squad::updateCenter()
{ {
int averageX = 0; int averageX = 0;

View File

@ -21,6 +21,7 @@ public:
void attack(std::pair<TilePosition, int> baseToAttack);; void attack(std::pair<TilePosition, int> baseToAttack);;
bool contains(CUnit unit); bool contains(CUnit unit);
bool containsType(UnitType ut); bool containsType(UnitType ut);
void freeDrones();
void updateCenter(); void updateCenter();
void updateDamage(); void updateDamage();
}; };