Skip to content

Commit d5753b5

Browse files
committed
feat: Attack first worker-scout, #16
Preemptively attack early scout-worker
1 parent 850f8b5 commit d5753b5

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/objects/Worker.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ void Worker::Repair(const sc2::Unit& target_) {
3737
gAPI->action().ToggleAutocast(Tag(), sc2::ABILITY_ID::EFFECT_REPAIR_SCV);
3838
m_job = Job::REPAIRING;
3939
}
40+
41+
void Worker::Attack(const sc2::Unit& target_) {
42+
gAPI->action().Cast(ToUnit(), sc2::ABILITY_ID::SMART, target_);
43+
m_job = Job::ATTACKING;
44+
}

src/objects/Worker.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ enum Job {
1313
BUILDING = 2,
1414
BUILDING_REFINERY = 3,
1515
REPAIRING = 4,
16+
ATTACKING = 5,
1617
};
1718

1819
struct Worker: GameObject {
@@ -24,6 +25,8 @@ struct Worker: GameObject {
2425

2526
void GatherVespene(const sc2::Unit& target_);
2627

28+
void Attack(const sc2::Unit& target_);
29+
2730
void Repair(const sc2::Unit& target_);
2831

2932
private:

src/strategies/Strategy.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "Historican.h"
66
#include "Strategy.h"
7+
#include "Hub.h"
78
#include "core/API.h"
89
#include "core/Helpers.h"
910

@@ -47,3 +48,21 @@ void Strategy::OnUnitCreated(const sc2::Unit* unit_, Builder*) {
4748

4849
m_units.push_back(unit_);
4950
}
51+
52+
void Strategy::OnUnitEnterVision(const sc2::Unit* unit_, Builder*) {
53+
if (IsCombatUnit()(*unit_))
54+
return;
55+
56+
if (m_attackFirstScout) {
57+
AssignWorkerAttack(*unit_);
58+
m_attackFirstScout = false; // only attack first scout, first time seen
59+
}
60+
}
61+
62+
void Strategy::AssignWorkerAttack(const sc2::Unit& target_) {
63+
Worker* worker = gHub->GetClosestFreeWorker(target_.pos);
64+
if (!worker)
65+
return;
66+
67+
worker->Attack(target_);
68+
}

src/strategies/Strategy.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ struct Strategy : Plugin {
1313

1414
void OnUnitCreated(const sc2::Unit* unit_, Builder*) override;
1515

16+
void OnUnitEnterVision(const sc2::Unit* unit_, Builder*) override;
17+
18+
void AssignWorkerAttack(const sc2::Unit& target_);
19+
1620
protected:
21+
bool m_attackFirstScout = true;
1722
float m_attack_limit;
1823
sc2::Units m_units;
1924
};

0 commit comments

Comments
 (0)