Skip to content

Commit

Permalink
feat: Attack first worker-scout, #16
Browse files Browse the repository at this point in the history
Preemptively attack early scout-worker
  • Loading branch information
ImpulseCloud committed Mar 18, 2021
1 parent 850f8b5 commit d5753b5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/objects/Worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ void Worker::Repair(const sc2::Unit& target_) {
gAPI->action().ToggleAutocast(Tag(), sc2::ABILITY_ID::EFFECT_REPAIR_SCV);
m_job = Job::REPAIRING;
}

void Worker::Attack(const sc2::Unit& target_) {
gAPI->action().Cast(ToUnit(), sc2::ABILITY_ID::SMART, target_);
m_job = Job::ATTACKING;
}
3 changes: 3 additions & 0 deletions src/objects/Worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum Job {
BUILDING = 2,
BUILDING_REFINERY = 3,
REPAIRING = 4,
ATTACKING = 5,
};

struct Worker: GameObject {
Expand All @@ -24,6 +25,8 @@ struct Worker: GameObject {

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

void Attack(const sc2::Unit& target_);

void Repair(const sc2::Unit& target_);

private:
Expand Down
19 changes: 19 additions & 0 deletions src/strategies/Strategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "Historican.h"
#include "Strategy.h"
#include "Hub.h"
#include "core/API.h"
#include "core/Helpers.h"

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

m_units.push_back(unit_);
}

void Strategy::OnUnitEnterVision(const sc2::Unit* unit_, Builder*) {
if (IsCombatUnit()(*unit_))
return;

if (m_attackFirstScout) {
AssignWorkerAttack(*unit_);
m_attackFirstScout = false; // only attack first scout, first time seen
}
}

void Strategy::AssignWorkerAttack(const sc2::Unit& target_) {
Worker* worker = gHub->GetClosestFreeWorker(target_.pos);
if (!worker)
return;

worker->Attack(target_);
}
5 changes: 5 additions & 0 deletions src/strategies/Strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ struct Strategy : Plugin {

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

void OnUnitEnterVision(const sc2::Unit* unit_, Builder*) override;

void AssignWorkerAttack(const sc2::Unit& target_);

protected:
bool m_attackFirstScout = true;
float m_attack_limit;
sc2::Units m_units;
};

0 comments on commit d5753b5

Please sign in to comment.