ProtoBot
Loading...
Searching...
No Matches
NexusEconomy Class Reference

A NexusEconomy is a the way we are able to assign workers to a specific Nexus to farm resources at the Resource Depots that ProtoBot has created.

[Note]
I have to admit the code structure for this is honestly terrible and is not great the way to be able to assign workers. If you are reading this and wanting to create a SC BW bot, make workers a class on their own and define a state machine for their specific behaviour and create components that are able to find optimal ways to farm, scout, and build given some extra arguments. It makes the handling of workers from different states easier and makes crashing less likely to happen if you dont have them as references else where. More...

#include <NexusEconomy.h>

Public Member Functions

 NexusEconomy (BWAPI::Unit nexus, int id, EconomyManager *economyReference)
void defendWorker ()
void checkGasSteal ()
void onFrame ()
void printMineralWorkerCounts ()
bool OnUnitDestroy (BWAPI::Unit unit)
BWAPI::Unit GetClosestMineralToWorker (BWAPI::Unit worker)
void assignWorker (BWAPI::Unit unit)
void assignWorkerBulk ()
void assignAssimilator (BWAPI::Unit assimilator)
BWAPI::Unitset getWorkersToTransfer (int numberOfWorkersForTransfer)
BWAPI::Unit getWorkerToScout ()
BWAPI::Unit getWorkerToBuild (BWAPI::Position locationToBuild)

Public Attributes

EconomyManagereconomyReference
int nexusID
int lifetime = 0
int fff = 0
int spot
double dx
double dy
double slope
double intercept
int spot_to_move
int offset
int workerNums
int attackingWorkers = 0
bool needWorkers = true
BWAPI::Unit nexus
BWAPI::Unitset workers
BWAPI::Unitset minerals
BWAPI::Unitset closestMinerals
BWAPI::Unit vespeneGyser = nullptr
BWAPI::Unit assimilator = nullptr
std::unordered_map< BWAPI::Unit, int > resourceWorkerCount
std::unordered_map< BWAPI::Unit, BWAPI::Unit > assignedResource
std::unordered_map< BWAPI::Unit, int > workerOrder
std::unordered_map< BWAPI::Unit, PathworkerPaths
int optimalWorkerAmount
int maximumWorkers
int workerOverflowAmount

Detailed Description

A NexusEconomy is a the way we are able to assign workers to a specific Nexus to farm resources at the Resource Depots that ProtoBot has created.

[Note]
I have to admit the code structure for this is honestly terrible and is not great the way to be able to assign workers. If you are reading this and wanting to create a SC BW bot, make workers a class on their own and define a state machine for their specific behaviour and create components that are able to find optimal ways to farm, scout, and build given some extra arguments. It makes the handling of workers from different states easier and makes crashing less likely to happen if you dont have them as references else where.

Definition at line 23 of file NexusEconomy.h.

Constructor & Destructor Documentation

◆ NexusEconomy()

NexusEconomy::NexusEconomy ( BWAPI::Unit nexus,
int id,
EconomyManager * economyReference )

Definition at line 5 of file NexusEconomy.cpp.

5 : nexus(nexus), nexusID(id), economyReference(economyReference)
6{
7 //std::cout << "Created new Nexus Economy " << id << "\n";
8}

◆ ~NexusEconomy()

NexusEconomy::~NexusEconomy ( )

Definition at line 11 of file NexusEconomy.cpp.

12{
13
14}

Member Function Documentation

◆ assignAssimilator()

void NexusEconomy::assignAssimilator ( BWAPI::Unit assimilator)

Definition at line 423 of file NexusEconomy.cpp.

424{
425 this->assimilator = assimilator;
426 resourceWorkerCount[assimilator] = 0;
427}

◆ assignWorker()

void NexusEconomy::assignWorker ( BWAPI::Unit unit)

Definition at line 403 of file NexusEconomy.cpp.

404{
405 workers.insert(unit);
406 workerOrder[unit] = 0;
407 //std::cout << "Worker " << unit->getID() << " assigned to nexus " << nexus->getID() << "\n";
408}

◆ assignWorkerBulk()

void NexusEconomy::assignWorkerBulk ( )

Definition at line 410 of file NexusEconomy.cpp.

411{
412 for (BWAPI::Unit worker : workers)
413 {
414 workerOrder[worker] = 0;
415 }
416
417 for (BWAPI::Unit mineral : minerals)
418 {
419 resourceWorkerCount[mineral] = 0;
420 }
421}

◆ checkGasSteal()

void NexusEconomy::checkGasSteal ( )

Definition at line 16 of file NexusEconomy.cpp.

17{
18 const int left = nexus->getPosition().x - 300;
19 const int top = nexus->getPosition().y - 300;
20 const int right = nexus->getPosition().x + 300;
21 const int bottom = nexus->getPosition().y + 300;
22
23 BWAPI::Unitset resourcesInRectangle = BWAPI::Broodwar->getUnitsInRectangle(BWAPI::Position(left, top), BWAPI::Position(right, bottom));
24 BWAPI::Unitset newMinerals;
25
26 //Should be better way to do this but this works for now.
27 for (const BWAPI::Unit unit : resourcesInRectangle)
28 {
29
30 if (unit->getType().isRefinery() && unit->getPlayer() != BWAPI::Broodwar->self()) {
31 //std::cout << "Gas steal detected...\n";
32 for (auto u : workers)
33 {
34 if (u->getType().isWorker() && attackingWorkers < 8) {
35 u->attack(unit);
36 attackingWorkers += 1;
37 }
38 }
39 attackingWorkers = 0;
40 }
41
42 }
43}

◆ defendWorker()

void NexusEconomy::defendWorker ( )

Definition at line 45 of file NexusEconomy.cpp.

46{
47 const int left = nexus->getPosition().x - 300;
48 const int top = nexus->getPosition().y - 300;
49 const int right = nexus->getPosition().x + 300;
50 const int bottom = nexus->getPosition().y + 300;
51
52 BWAPI::Unitset resourcesInRectangle = BWAPI::Broodwar->getUnitsInRectangle(BWAPI::Position(left, top), BWAPI::Position(right, bottom));
53 BWAPI::Unitset newMinerals;
54
55 //Should be better way to do this but this works for now.
56 for (const BWAPI::Unit unit : resourcesInRectangle)
57 {
58
59 if (unit->isAttacking() && unit->getPlayer() != BWAPI::Broodwar->self() ) {
60 for (auto u : workers)
61 {
62 if (u->getType().isWorker() && attackingWorkers < 8 && assignedResource[u]) {
63 u->attack(unit);
64 workerOrder[u] = 7;
65 attackingWorkers += 1;
66 }
67 }
68 attackingWorkers = 0;
69 }
70
71 }
72}

◆ GetClosestMineralToWorker()

BWAPI::Unit NexusEconomy::GetClosestMineralToWorker ( BWAPI::Unit worker)

Definition at line 349 of file NexusEconomy.cpp.

350{
351 //Check if we have a mineral patch that has no workers
352 BWAPI::Unit closestMineral = nullptr;
353 int closestMineralDistance = 99999;
354
355 for (BWAPI::Unit mineral : minerals)
356 {
357 int distanceFromWorker = worker->getDistance(mineral);
358
359 if (resourceWorkerCount[mineral] == 0 && distanceFromWorker < closestMineralDistance)
360 {
361 closestMineralDistance = distanceFromWorker;
362 closestMineral = mineral;
363 }
364 }
365
366 if (closestMineral == nullptr)
367 {
368 //Maximum workers is the calculated value of (# of minerals * OPTIMAL_WORKERS_PER_MINERAL) + (# of gysers * WORKERS_PER_ASSIMILATOR)
369 if (workers.size() < maximumWorkers)
370 {
371 for (BWAPI::Unit mineral : minerals)
372 {
373 const int distanceFromWorker = worker->getDistance(mineral);
374 const int workersAssignedToMineral = resourceWorkerCount[mineral];
375
376 if (resourceWorkerCount[mineral] < OPTIMAL_WORKERS_PER_MINERAL && distanceFromWorker < closestMineralDistance)
377 {
378 closestMineralDistance = distanceFromWorker;
379 closestMineral = mineral;
380 }
381 }
382 }
383 else
384 {
385 //This doesnt work if we never build and assimialtor so will need to add edge case.
386 for (BWAPI::Unit mineral : minerals)
387 {
388 const int distanceFromWorker = worker->getDistance(mineral);
389 const int workersAssignedToMineral = resourceWorkerCount[mineral];
390
391 if (resourceWorkerCount[mineral] < MAXIMUM_WORKERS_PER_MINERAL && distanceFromWorker < closestMineralDistance)
392 {
393 closestMineralDistance = distanceFromWorker;
394 closestMineral = mineral;
395 }
396 }
397 }
398 }
399
400 return closestMineral;
401}

◆ getWorkersToTransfer()

BWAPI::Unitset NexusEconomy::getWorkersToTransfer ( int numberOfWorkersForTransfer)

Definition at line 430 of file NexusEconomy.cpp.

431{
432 BWAPI::Unitset unitsToReturn;
433
434 //Get idle units if possible.
435 for (BWAPI::Unit unit : workers)
436 {
437 if (unit->isIdle())
438 {
439 if (assignedResource.find(unit) != assignedResource.end())
440 {
441 unitsToReturn.insert(unit);
442 }
443 else
444 {
445 unitsToReturn.insert(unit);
446 }
447 }
448
449 if (unitsToReturn.size() == numberOfWorkersForTransfer) break;
450 }
451
452 if (unitsToReturn.size() == numberOfWorkersForTransfer)
453 {
454 for (BWAPI::Unit unit : unitsToReturn)
455 {
456 unit->stop();
457 //workerOrder[unit] = 5;
458 if (unit->isCarryingMinerals())
459 {
460 //unit->returnCargo();
461 }
462 workers.erase(unit);
463 resourceWorkerCount[assignedResource[unit]] -= 1;
464 assignedResource.erase(unit);
465 }
466
467 return unitsToReturn;
468 }
469
470 //Choose random unit if we did not find unit.
471 for (BWAPI::Unit unit : workers)
472 {
473 if (!unit->isCarryingMinerals() && !unitsToReturn.contains(unit))
474 {
475 //BWAPI::Unit assignedMineral = assignedResource[unit];
476 //resourceWorkerCount[assignedMineral] -= 1;
477 //assignedResource.erase(unit);
478 //workers.erase(unit);
479 unitsToReturn.insert(unit);
480 }
481
482 if (unitsToReturn.size() == numberOfWorkersForTransfer) break;
483 }
484
485 if (unitsToReturn.size() == numberOfWorkersForTransfer)
486 {
487 for (BWAPI::Unit unit : unitsToReturn)
488 {
489 unit->stop();
490 //workerOrder[unit] = 5;
491 workers.erase(unit);
492 if (unit->isCarryingMinerals())
493 {
494 unit->returnCargo();
495 }
496 resourceWorkerCount[assignedResource[unit]] -= 1;
497 assignedResource.erase(unit);
498 //BWAPI::Unit assignedMineral = assignedResource[unit];
499 }
500
501 /*
502 for (BWAPI::Unit unit : workers)
503 {
504 std::cout << "worker " << unit->getID() << "\n";
505 }
506 */
507 return unitsToReturn;
508 }
509
510 //If not enough units we found, get units until we meet the number of workers needs
511 for (BWAPI::Unit unit : workers)
512 {
513 BWAPI::Unit assignedMineral = assignedResource[unit];
514
515 if (assignedMineral != vespeneGyser && !unitsToReturn.contains(unit))
516 {
517 //resourceWorkerCount[assignedMineral] -= 1;
518 //assignedResource.erase(unit);
519 //workers.erase(unit);
520 unitsToReturn.insert(unit);
521 }
522
523 if (unitsToReturn.size() == numberOfWorkersForTransfer) break;
524 }
525
526 //std::cout << "Got " << unitsToReturn.size() << " Workers\n";
527
528 for (BWAPI::Unit unit : unitsToReturn)
529 {
530 unit->stop();
531 //workerOrder[unit] = 5;
532 workers.erase(unit);
533 if (unit->isCarryingMinerals())
534 {
535 //unit->returnCargo();
536 }
537 resourceWorkerCount[assignedResource[unit]] -= 1;
538 assignedResource.erase(unit);
539 //BWAPI::Unit assignedMineral = assignedResource[unit];
540 }
541
542 /*
543 for (BWAPI::Unit unit : workers)
544 {
545 std::cout << "worker " << unit->getID() << "\n";
546 }
547 */
548
549 return unitsToReturn;
550}

◆ getWorkerToBuild()

BWAPI::Unit NexusEconomy::getWorkerToBuild ( BWAPI::Position locationToBuild)

Definition at line 642 of file NexusEconomy.cpp.

643{
644
645 BWAPI::Unitset closestWorkers;
646 if (closestMinerals.size() == 0)
647 {
648 int largeDist = 999;
649 int compare = 0;
650 BWAPI::Unit appended;
651 for (int ii = 0; ii < 3; ii++)
652 {
653 largeDist = 999;
654 for (auto u : minerals)
655 {
656 //compare = nexus->getPosition().getApproxDistance(u->getPosition());
657 compare = std::abs(nexus->getPosition().x-u->getPosition().x);
658 if (compare < largeDist && !closestMinerals.contains(u))
659 {
660 appended = u;
661 largeDist = compare;
662 }
663 }
664
665 closestMinerals.insert(appended);
666 }
667 }
668
669
670 for (const BWAPI::Unit unit : workers)
671 {
672 if (closestMinerals.contains(assignedResource[unit]))
673 {
674 closestWorkers.insert(unit);
675 }
676 }
677 //economyReference->commanderReference->timerManager.startTimer(TimerManager::Testing);
678
679 if (closestWorkers.size() == 0) return nullptr;
680
681 BWAPI::Unit unitToReturn = nullptr;
682 int minDistance = INT_MAX;
683
684 //Get closest idle units if possible.
685 for (const BWAPI::Unit unit : closestWorkers)
686 {
687 if (economyReference->workerIsConstructing(unit)) continue;
688
689 //const int distance = locationToBuild.getApproxDistance(unit->getPosition());
690 int distance = 0;
691 theMap.GetPath(BWAPI::Position(BWAPI::Broodwar->self()->getStartLocation()), BWAPI::Position(locationToBuild), &distance);
692
693
694 if (unit->isIdle() && distance < minDistance)
695 {
696 minDistance = distance;
697
698 //Only take workers that are mining mineral patches. Workers assigned to gysers are never deassgined, unless destoryed.
699 if (assignedResource.find(unit) != assignedResource.end() && assignedResource[unit] != vespeneGyser)
700 {
701 unitToReturn = unit;
702 //std::cout << unit->getID() << " is going to build!" << "\n";
703 }
704 }
705 }
706
707 //economyReference->commanderReference->timerManager.stopTimer(TimerManager::Testing);
708 //std::cout << "time to build: " << economyReference->commanderReference->timerManager.getTotalElapsedTest() << " From m1\n";
709
710 //If idle unit is avalible return, otherwise search for the second best option
711 if (unitToReturn != nullptr)
712 {
713 if (assignedResource.find(unitToReturn) != assignedResource.end())
714 {
715 BWAPI::Unit assignedMineral = assignedResource[unitToReturn];
716 //resourceWorkerCount[assignedMineral] -= 1;
717 //assignedResource.erase(unitToReturn);
718 }
719
720 workerOrder[unitToReturn] = 2;
721 return unitToReturn;
722 }
723
724 minDistance = INT_MAX;
725 for (const BWAPI::Unit unit : closestWorkers)
726 {
727 if (economyReference->workerIsConstructing(unit)) continue;
728
729 //const int distance = locationToBuild.getApproxDistance(unit->getPosition());
730 int distance = 0;
731 theMap.GetPath(BWAPI::Position(BWAPI::Broodwar->self()->getStartLocation()), BWAPI::Position(locationToBuild), &distance);
732
733 if (distance < minDistance)
734 {
735 minDistance = distance;
736 unitToReturn = unit;
737 }
738 }
739
740
741 //economyReference->commanderReference->timerManager.stopTimer(TimerManager::Testing);
742 //std::cout << "time to build: " << economyReference->commanderReference->timerManager.getTotalElapsedTest() << " from m2\n";
743
744 if (unitToReturn != nullptr)
745 {
746 //std::cout << "Unit Carrying Minerals " << unitToReturn->getID() << " Avalible!\n";
747 workerOrder[unitToReturn] = 2;
748 return unitToReturn;
749 }
750
751
752 minDistance = INT_MAX;
753 for (const BWAPI::Unit unit : closestWorkers)
754 {
755 if (economyReference->workerIsConstructing(unit)) continue;
756
757 //const int distance = locationToBuild.getApproxDistance(unit->getPosition());
758 int distance = 0;
759 theMap.GetPath(BWAPI::Position(BWAPI::Broodwar->self()->getStartLocation()), BWAPI::Position(locationToBuild), &distance);
760
761 if (distance < minDistance && assignedResource.find(unitToReturn) != assignedResource.end() && assignedResource[unit] != vespeneGyser)
762 {
763 unitToReturn = unit;
764 //std::cout << unit->getID() << " is going to build!" << "\n";
765 }
766 }
767
768 if (assignedResource.find(unitToReturn) != assignedResource.end())
769 {
770 BWAPI::Unit assignedMineral = assignedResource[unitToReturn];
771 //resourceWorkerCount[assignedMineral] -= 1;
772 //assignedResource.erase(unitToReturn);
773 }
774 workerOrder[unitToReturn] = 2;
775
776 //economyReference->commanderReference->timerManager.stopTimer(TimerManager::Testing);
777 //std::cout << "time to build: " << economyReference->commanderReference->timerManager.getTotalElapsedTest() << " from m3\n";
778
779
780
781 return unitToReturn;
782}

◆ getWorkerToScout()

BWAPI::Unit NexusEconomy::getWorkerToScout ( )

Definition at line 553 of file NexusEconomy.cpp.

554{
555 //std::cout << "Request for Worker scouts!\n";
556 if (workers.size() == 0) return nullptr;
557
558 BWAPI::Unit unitToReturn = nullptr;
559
560 //Get idle units if possible.
561 for (BWAPI::Unit unit : workers)
562 {
563 if (unit->isIdle() && unit->isCompleted())
564 {
565 if (assignedResource.find(unit) != assignedResource.end())
566 {
567 BWAPI::Unit assignedMineral = assignedResource[unit];
568 //resourceWorkerCount[assignedMineral] -= 1;
569 //assignedResource.erase(unit);
570 unitToReturn = unit;
571
572 }
573 else
574 {
575 unitToReturn = unit;
576 }
577 //workers.erase(unit);
578 break;
579 }
580 }
581
582 if (unitToReturn != nullptr)
583 {
584 workerOrder[unitToReturn] = 3;
585 workers.erase(unitToReturn);
586 resourceWorkerCount[assignedResource[unitToReturn]] -= 1;
587 assignedResource.erase(unitToReturn);
588 //std::cout << "Requesting Worker scouts!\n";
589 return unitToReturn;
590 }
591
592
593 //Choose random unit if we did not find unit.
594
595 for (BWAPI::Unit unit : workers)
596 {
597 if (!unit->isCarryingMinerals() && unit->isGatheringMinerals() && !unit->isMoving() && unit->isCompleted())
598 {
599 unitToReturn = unit;
600 break;
601 }
602 //workers.erase(unit);
603 }
604
605 if (unitToReturn != nullptr)
606 {
607 workers.erase(unitToReturn);
608 resourceWorkerCount[assignedResource[unitToReturn]] -= 1;
609 assignedResource.erase(unitToReturn);
610 //std::cout << "Reuqesting Worker scouts!\n";
611 return unitToReturn;
612 }
613
614 //If not unit is avalible that meets prior conditions choose unit randomly for now.
615 const int random = rand() % workers.size();
616 //std::cout << "Random Index Choosen: " << random << "\n";
617
618 int index = 0;
619 for (BWAPI::Unit unit : workers)
620 {
621 if (/*index == random && */ !unit->isMoving() && unit->isCompleted())
622 {
623 BWAPI::Unit assignedMineral = assignedResource[unit];
624 //resourceWorkerCount[assignedMineral] -= 1;
625 unitToReturn = unit;
626 //assignedResource.erase(unit);
627 //workers.erase(unit);
628 break;
629 }
630
631 index++;
632 }
633
634 workers.erase(unitToReturn);
635 resourceWorkerCount[assignedResource[unitToReturn]] -= 1;
636 assignedResource.erase(unitToReturn);
637 //std::cout << "Reuqesting Worker scouts!\n";
638 return unitToReturn;
639}

◆ onFrame()

void NexusEconomy::onFrame ( )

Definition at line 76 of file NexusEconomy.cpp.

77{
78
79
80 if (lifetime < 500) lifetime += 1;
81 //moveTimer += 1;
82
83 if (BWAPI::Broodwar->getFrameCount() % 150 == 0)
84 {
85 checkGasSteal();
86 }
87
88 //make this solution better.
89 //if (lifetime < 500) int y = addMissedResources();
90
91 //BWAPI::Unitset WorkersToBeKicked;
92
93 /*
94 ===========================
95 Debug
96 ===========================
97 */
98 /*const int left = nexus->getPosition().x - 300;
99 const int top = nexus->getPosition().y - 300;
100 const int right = nexus->getPosition().x + 300;
101 const int bottom = nexus->getPosition().y + 300;
102 BWAPI::Broodwar->drawBoxMap(BWAPI::Position(left, top), BWAPI::Position(right, bottom), BWAPI::Color(255, 0, 0));*/
103
104 //Problem start here
105
106
107 /*for (BWAPI::Unit mineral : minerals)
108 {
109 BWAPI::Broodwar->drawLineMap(nexus->getPosition(), mineral->getPosition(), BWAPI::Color(0, 0, 255));
110 BWAPI::Broodwar->drawTextMap(mineral->getPosition(), std::to_string(resourceWorkerCount[mineral]).c_str());
111 }*/
112
113 //if (assimilator != nullptr) BWAPI::Broodwar->drawLineMap(nexus->getPosition(), assimilator->getPosition(), BWAPI::Color(255, 255, 0));
114
115 //if (vespeneGyser != nullptr && assimilator == nullptr) BWAPI::Broodwar->drawLineMap(nexus->getPosition(), vespeneGyser->getPosition(), BWAPI::Color(144, 238, 144));
116
117 //Problem end here
118
119 /*if (BWAPI::Broodwar->getFrameCount() % 500 == 0 && BWAPI::Broodwar->getFrameCount() != 0)
120 {
121 std::cout << "Frame: " << BWAPI::Broodwar->getFrameCount() << " Minerals Gathered: " << BWAPI::Broodwar->self()->gatheredMinerals() << "\n";
122 }
123 else if(BWAPI::Broodwar->getFrameCount() == 0)
124 {
125 std::cout << "Frame: " << BWAPI::Broodwar->getFrameCount() << " Minerals Gathered: " << 0 << "\n";
126 }*/
127
128 /*if (BWAPI::Broodwar->getFrameCount() % 48 == 0)
129 {
130 std::cout << "===== MineralAssignments =====\n";
131 printMineralWorkerCounts();
132 }*/
133
134 /*
135 ===========================
136 Main loop
137 ===========================
138 */
139
140 //std::string temp = "Nexus ID " + std::to_string(nexus->getID()) + "\n" + "Worker Size : " + std::to_string(workers.size()) + "\nMinerals : " + std::to_string(minerals.size());
141 //BWAPI::Broodwar->drawTextMap(BWAPI::Position(nexus->getPosition().x, nexus->getPosition().y), temp.c_str());
142
143 for (BWAPI::Unit worker : workers)
144 {
145 //if (worker->isIdle()) workerOrder[worker] = 0;
146 //BWAPI::Broodwar->drawTextMap(worker->getPosition(), std::to_string(worker->getID()).c_str());
147
148 if (workerOrder[worker] == 7 && BWAPI::Broodwar->getFrameCount() % 150 == 0)
149 {
150 if (worker->getPosition().getApproxDistance(nexus->getPosition()) > 300)
151 {
152 worker->gather(assignedResource[worker]);
153 workerOrder[worker] = 1;
154 }
155 }
156
157 if (minerals.size() == 0) break;
158
159 //If a worker is constructing skip over them until they are done.
160 if (economyReference->workerIsConstructing(worker) || worker->isConstructing() || worker->isMoving() || workerOrder[worker] == 5)
161 {
162 //BWAPI::Broodwar->drawEllipseMap(worker->getPosition(), 3, 3, BWAPI::Color(0, 0, 255), true);
163 continue;
164 }
165
166 if (worker->isUnderAttack() && !worker->isAttacking())
167 {
168 defendWorker();
169 }
170
171 //If a worker is not carrying minerals and hasnt been assigned to one, assign them to farm.
172 if (!worker->isCarryingMinerals())
173 {
174 if (assimilator != nullptr
175 && resourceWorkerCount[assimilator] + 1 <= WORKERS_PER_ASSIMILATOR
176 && (workers.size() >= minerals.size() || minerals.size() == 0))
177 {
178 if (assignedResource[worker]) {
179 resourceWorkerCount[assignedResource[worker]] -= 1;
180 }
181
182 worker->gather(assimilator);
183 assignedResource[worker] = assimilator;
184 resourceWorkerCount[assimilator] += 1;
185 workerOrder[worker] = 1;
186
187 }
188 else if (assignedResource.find(worker) == assignedResource.end() || workerOrder[worker] == 0)
189 {
190 //std::cout << "reached this point\n";
191 BWAPI::Unit closestMineral = GetClosestMineralToWorker(worker);
192 if (closestMineral) {
193 resourceWorkerCount[closestMineral] += 1;
194 assignedResource[worker] = closestMineral;
195
196 worker->gather(closestMineral);
197 workerOrder[worker] = 1;
198 //std::cout << "worker " << worker->getID() << " assigned to mineral " << closestMineral->getID() << "\n";
199
200
201 }
202
203 }
204
205 }
206
207 if (worker->getOrder() == BWAPI::Orders::MoveToMinerals ||
208 worker->getOrder() == BWAPI::Orders::WaitForMinerals || worker->isIdle())
209 {
210 if (worker->getOrderTarget() != assignedResource[worker])
211 {
212 worker->rightClick(assignedResource[worker]);
213 }
214
215 //continue;
216 }
217
218 if ((worker->isCarryingMinerals() || worker->isCarryingGas()) && worker->isIdle())
219 {
220 //BWAPI::Unit assignedMineral = assignedResource[worker];
221 //assignedResource.erase(worker);
222 //resourceWorkerCount[assignedMineral] -= 1;
223 worker->move(nexus->getPosition());
224 worker->rightClick(nexus);
225 workerOrder[worker] = 1;
226 }
227 }
228
229 /*
230 if (moveTimer >= 5)
231 {
232 moveTimer = 0;
233 }*/
234}

◆ OnUnitDestroy()

bool NexusEconomy::OnUnitDestroy ( BWAPI::Unit unit)

Definition at line 247 of file NexusEconomy.cpp.

248{
249 if (unit->getType().isWorker() && workers.find(unit) != workers.end())
250 {
251 BWAPI::Unit resource = assignedResource[unit];
252
253 //Check if worker is assigned to mineral or gas, MAKE THIS BETTER
254 if (resource == vespeneGyser)
255 {
256 resourceWorkerCount[assimilator] -= 1;
257
258 }
259 else
260 {
261 resourceWorkerCount[resource] -= 1;
262 }
263
264 assignedResource.erase(unit);
265 workers.erase(unit);
266 return true;
267 }
268 else if (unit->getType() == BWAPI::UnitTypes::Resource_Mineral_Field && minerals.find(unit) != minerals.end())
269 {
270 //std::cout << "Mineral has been depleted at Nexus " << nexusID << "\n";
271
272 resourceWorkerCount[unit] = 0;
273 minerals.erase(unit);
274
275 BWAPI::Unitset workersToRelocate;
276 for (BWAPI::Unit worker : workers)
277 {
278 if (workersToRelocate.size() == OPTIMAL_WORKERS_PER_MINERAL) break;
279
280 if (assignedResource.find(worker) == assignedResource.end())
281 {
282 workersToRelocate.insert(worker);
283 }
284 else if (assignedResource.find(worker) != assignedResource.end() && assignedResource[worker] != vespeneGyser)
285 {
286 BWAPI::Unit mineral = assignedResource[worker];
287 resourceWorkerCount[mineral] -= 1;
288 workersToRelocate.insert(worker);
289 }
290 }
291
292 //std::cout << "Worker size before: " << workers.size() << "\n";
293 for (BWAPI::Unit worker : workersToRelocate)
294 {
295 workers.erase(worker);
296 }
297 //std::cout << "Worker size after: " << workers.size() << "\n";
298
299 economyReference->resourcesDepletedTranfer(workersToRelocate, *this);
300
301 return true;
302 }
303 else if (unit->getType() == BWAPI::UnitTypes::Protoss_Assimilator && assimilator != nullptr && unit == assimilator)
304 {
305 //std::cout << "Assimilator destroyed...\n";
306 assimilator = nullptr;
307 resourceWorkerCount[assimilator] = 0;
308 for (auto u : workers)
309 {
310 if (assignedResource[u] == vespeneGyser)
311 {
312 assignedResource.erase(u);
313 }
314 }
315 return true;
316 }
317 else if (unit->getType() == BWAPI::UnitTypes::Resource_Vespene_Geyser && vespeneGyser != nullptr && unit == vespeneGyser)
318 {
319 //::cout << "Gyser Depleted...\n";
320 //assimilator = nullptr;
321 resourceWorkerCount[assimilator] = 0;
322 BWAPI::Unitset workersToRelocate;
323 for (BWAPI::Unit worker : workers)
324 {
325 if (workersToRelocate.size() == 3) break;
326
327 if (assignedResource[worker] == vespeneGyser)
328 {
329 workersToRelocate.insert(worker);
330 }
331
332 }
333
334 //std::cout << "Worker size before: " << workers.size() << "\n";
335 for (BWAPI::Unit worker : workersToRelocate)
336 {
337 workers.erase(worker);
338 }
339 //std::cout << "Worker size after: " << workers.size() << "\n";
340
341 economyReference->resourcesDepletedTranfer(workersToRelocate, *this);
342 return true;
343 }
344
345 return false;
346}

◆ printMineralWorkerCounts()

void NexusEconomy::printMineralWorkerCounts ( )

Definition at line 236 of file NexusEconomy.cpp.

237{
238 for (BWAPI::Unit mineral : minerals)
239 {
240 std::cout << "Mineral " << mineral->getID() << " Count: " << resourceWorkerCount[mineral] << "\n";
241 }
242}

Member Data Documentation

◆ assignedResource

std::unordered_map<BWAPI::Unit, BWAPI::Unit> NexusEconomy::assignedResource

Definition at line 50 of file NexusEconomy.h.

◆ assimilator

BWAPI::Unit NexusEconomy::assimilator = nullptr

Definition at line 48 of file NexusEconomy.h.

◆ attackingWorkers

int NexusEconomy::attackingWorkers = 0

Definition at line 39 of file NexusEconomy.h.

◆ closestMinerals

BWAPI::Unitset NexusEconomy::closestMinerals

Definition at line 45 of file NexusEconomy.h.

◆ dx

double NexusEconomy::dx

Definition at line 31 of file NexusEconomy.h.

◆ dy

double NexusEconomy::dy

Definition at line 32 of file NexusEconomy.h.

◆ economyReference

EconomyManager* NexusEconomy::economyReference

Definition at line 26 of file NexusEconomy.h.

◆ fff

int NexusEconomy::fff = 0

Definition at line 29 of file NexusEconomy.h.

◆ intercept

double NexusEconomy::intercept

Definition at line 34 of file NexusEconomy.h.

◆ lifetime

int NexusEconomy::lifetime = 0

Definition at line 28 of file NexusEconomy.h.

◆ maximumWorkers

int NexusEconomy::maximumWorkers

Definition at line 56 of file NexusEconomy.h.

◆ minerals

BWAPI::Unitset NexusEconomy::minerals

Definition at line 44 of file NexusEconomy.h.

◆ needWorkers

bool NexusEconomy::needWorkers = true

Definition at line 40 of file NexusEconomy.h.

◆ nexus

BWAPI::Unit NexusEconomy::nexus

Definition at line 42 of file NexusEconomy.h.

◆ nexusID

int NexusEconomy::nexusID

Definition at line 27 of file NexusEconomy.h.

◆ offset

int NexusEconomy::offset

Definition at line 36 of file NexusEconomy.h.

◆ optimalWorkerAmount

int NexusEconomy::optimalWorkerAmount

Definition at line 55 of file NexusEconomy.h.

◆ resourceWorkerCount

std::unordered_map<BWAPI::Unit, int> NexusEconomy::resourceWorkerCount

Definition at line 49 of file NexusEconomy.h.

◆ slope

double NexusEconomy::slope

Definition at line 33 of file NexusEconomy.h.

◆ spot

int NexusEconomy::spot

Definition at line 30 of file NexusEconomy.h.

◆ spot_to_move

int NexusEconomy::spot_to_move

Definition at line 35 of file NexusEconomy.h.

◆ vespeneGyser

BWAPI::Unit NexusEconomy::vespeneGyser = nullptr

Definition at line 46 of file NexusEconomy.h.

◆ workerNums

int NexusEconomy::workerNums

Definition at line 38 of file NexusEconomy.h.

◆ workerOrder

std::unordered_map<BWAPI::Unit, int> NexusEconomy::workerOrder

Definition at line 51 of file NexusEconomy.h.

◆ workerOverflowAmount

int NexusEconomy::workerOverflowAmount

Definition at line 59 of file NexusEconomy.h.

◆ workerPaths

std::unordered_map<BWAPI::Unit, Path> NexusEconomy::workerPaths

Definition at line 52 of file NexusEconomy.h.

◆ workers

BWAPI::Unitset NexusEconomy::workers

Definition at line 43 of file NexusEconomy.h.


The documentation for this class was generated from the following files: