Skip to content

Commit

Permalink
Finish the mine gather algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
ziap committed Mar 7, 2021
1 parent dc9442e commit a68f51f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 69 deletions.
6 changes: 3 additions & 3 deletions Release/2qb-SSCAIT.log
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
2qb-SSCAIT.cpp
Creating library Release\2qb-SSCAIT.lib and object Release\2qb-SSCAIT.exp
Generating code
672 of 2844 functions (23.6%) were compiled, the rest were copied from previous compilation.
242 functions were new in current compilation
168 functions had inline decision re-evaluated but remain unchanged
1 of 2872 functions (<0.1%) were compiled, the rest were copied from previous compilation.
0 functions were new in current compilation
0 functions had inline decision re-evaluated but remain unchanged
Finished generating code
2qb-SSCAIT.vcxproj -> C:\Users\Zap\BWAPI\Release\2qb-SSCAIT.dll
Binary file modified Release/2qb-SSCAIT.obj
Binary file not shown.
Binary file modified Release/2qb-SSCAIT.tlog/2qb-SSCAIT.write.1u.tlog
Binary file not shown.
Binary file modified Release/vc142.pdb
Binary file not shown.
20 changes: 10 additions & 10 deletions Source/2qb-SSCAIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ MineGather gatherer;
using namespace BWAPI;
using namespace Filter;

WorkerContainer workers;
MineContainer mines;
ResourceDepotContainer resourcedepots;

void ExampleAIModule::onStart()
{
// Hello World!
Broodwar->sendText("Hello world from team 2QB!");
// Broodwar->sendText("Hello world from team 2QB!");

// Speed up the game
Broodwar->setLocalSpeed(0);
// Broodwar->setLocalSpeed(0);

// Print the map name.
// BWAPI returns std::string when retrieving a string, don't forget to add .c_str() when printing!
Expand Down Expand Up @@ -58,6 +56,10 @@ void ExampleAIModule::onStart()
// If you wish to deal with multiple enemies then you must use enemies().
if (Broodwar->enemy()) // First make sure there is an enemy
Broodwar << "The matchup is " << Broodwar->self()->getRace() << " vs " << Broodwar->enemy()->getRace() << std::endl;

for (auto& u : Broodwar->getAllUnits()) {
onUnitCreate(u);
}
}

}
Expand Down Expand Up @@ -88,13 +90,13 @@ void ExampleAIModule::onFrame()
if (Broodwar->getFrameCount() % Broodwar->getLatencyFrames() != 0)
return;

gatherer.AssignMine(workers.unit_set, mines.unit_set);
workers.OnFrame();
resourcedepots.OnFrame();

if (Broodwar->getFrameCount() >= 8000) Broodwar->leaveGame();
// Don't put mines.OnFrame() here

gatherer.OnFrame();

}

void ExampleAIModule::onSendText(std::string text)
Expand Down Expand Up @@ -170,17 +172,15 @@ void ExampleAIModule::onUnitCreate(BWAPI::Unit unit)
}
}
else {
workers.Insert(unit);
resourcedepots.Insert(unit);
mines.Insert(unit);
gatherer.Insert(unit);
}
}

void ExampleAIModule::onUnitDestroy(BWAPI::Unit unit)
{
workers.Remove(unit);
resourcedepots.Remove(unit);
mines.Remove(unit);
gatherer.Remove(unit);
}

void ExampleAIModule::onUnitMorph(BWAPI::Unit unit)
Expand Down
90 changes: 64 additions & 26 deletions Source/mine-gather.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,71 @@ class MineGather {
auto found = distribution.find(unit);
if (found == distribution.end()) return;
worker_count[found->second]--;
distribution.erase(found);
AssignMine(unit);
}
void AssignMine(std::unordered_set<Unit> wk, std::unordered_set<Unit> mn) {
for (Unit unit : wk) {
if (distribution.find(unit) != distribution.end()) continue;
int min_worker = -1;
for (Unit mine : mn) {
int count = worker_count[mine];
if (min_worker == -1 || count < min_worker) min_worker = count;
}
Unit choose = unit;
int min_dist = -1;
for (Unit mine : mn) {
if (worker_count[mine] != min_worker) continue;

int dist = unit->getDistance(mine);
if (min_dist == -1 || dist < min_dist) {
min_dist = dist;
choose = mine;
}
}
if (choose == unit) continue;
worker_count[choose]++;
distribution[unit] = choose;
}
void Remove(Unit unit) {
auto found = distribution.find(unit);
if (found != distribution.end()) distribution.erase(found);
auto found1 = worker_count.find(unit);
if (found1 != worker_count.end()) worker_count.erase(found1);
}
void Gather(Unit unit) {
unit->gather(distribution[unit]);
void Insert(Unit unit) {
if (unit->getType().isWorker() && unit->getPlayer() == Broodwar->self()) {
AssignMine(unit);
}
else if (unit->getType().isMineralField() || unit->getType().isRefinery()) {
worker_count.insert({ unit, 0 });
}
}
void AssignMine(Unit unit) {
auto found = distribution.find(unit);
if (found != distribution.end()) distribution.erase(found);
int min_worker = INT_MAX;
int min_dist = INT_MAX;
Unit choose = unit;
for (auto i : worker_count) min_worker = std::min(min_worker, i.second);
for (auto i : worker_count) {
if (i.second > min_worker) continue;

int dist = unit->getDistance(i.first);
if (min_dist > dist) {
min_dist = dist;
choose = i.first;
}
}
worker_count[choose]++;
if (choose != unit) distribution.insert({ unit, choose });
}
void Gather(Unit u) {
auto found = distribution.find(u);
if (found != distribution.end()) u->gather(found->second);
else AssignMine(u);
}
void OnFrame() {

for (auto i : distribution) {
Unit u = i.first;
if (u->isIdle())
{
// Order workers carrying a resource to return them to the center,
// otherwise find a mineral patch to harvest.
if (u->isCarryingGas() || u->isCarryingMinerals())
{
u->returnCargo();
DoneGathering(u);
}
else if (!u->getPowerUp()) // The worker cannot harvest anything if it
{ // is carrying a powerup such as a flag
Gather(u);
// Harvest from the nearest mineral patch or gas refinery
//if (!u->gather(distribution[u]))
//{
// // If the call fails, then print the last error message
// Broodwar << "No fuck you, " << Broodwar->getLastError() << std::endl;
//}

} // closure: has no powerup
} // closure: if idle
}
}
};
30 changes: 0 additions & 30 deletions Source/unit-container.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,6 @@ class UnitContainer {
virtual void PerformTask(Unit u) = 0;
};

class MineContainer : public UnitContainer {
public:
bool CheckType(Unit unit) override { return (unit->getType().isMineralField() || unit->getType().isRefinery()); }
void PerformTask(Unit u) override {}; // No task to perform
};

class WorkerContainer : public UnitContainer {
public:
bool CheckType(Unit unit) override { return (unit->getType().isWorker() && unit->getPlayer() == Broodwar->self()); }
private:
void PerformTask(Unit u) override {
// if our worker is idle
if (u->isIdle())
{
// Order workers carrying a resource to return them to the center,
// otherwise find a mineral patch to harvest.
if (u->isCarryingGas() || u->isCarryingMinerals())
{
u->returnCargo();
//gatherer.DoneGathering(u);
}
else if (!u->getPowerUp()) // The worker cannot harvest anything if it
{ // is carrying a powerup such as a flag
//u->gather(u->getClosestUnit(IsMineralField || IsRefinery));
gatherer.Gather(u);
} // closure: has no powerup
} // closure: if idle
}
};

class ResourceDepotContainer : public UnitContainer {
public:
bool CheckType(Unit unit) { return (unit->getType().isResourceDepot() && unit->getPlayer() == Broodwar->self()); }
Expand Down

0 comments on commit a68f51f

Please sign in to comment.