Skip to content

Commit fc726bc

Browse files
committed
fix: Fix gas saturation, #34
Only check vespene workers every 7 steps, to prevent too many vespene workers from being added and oversaturated. Also, create functionality for other Plugin-derived classes to run every X steps.
1 parent e5ab540 commit fc726bc

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

src/Dispatcher.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ namespace {
2727
Historican gHistory("dispatcher");
2828
} // namespace
2929

30-
Dispatcher::Dispatcher(const std::string& opponent_id_): m_builder(new Builder()) {
30+
Dispatcher::Dispatcher(const std::string& opponent_id_): m_builder(new Builder()),
31+
m_steps(0) {
3132
gAPI.reset(new API::Interface(Actions(), Control(), Debug(), Observation(), Query()));
3233
m_plugins.reserve(10);
3334

@@ -91,9 +92,11 @@ void Dispatcher::OnStep() {
9192
gHub->OnStep();
9293

9394
for (const auto& i : m_plugins)
94-
i->OnStep(m_builder.get());
95+
if (i->CheckFrequency(m_steps))
96+
i->OnStep(m_builder.get());
9597

9698
m_builder->OnStep();
99+
m_steps++;
97100
}
98101

99102
void Dispatcher::OnUnitCreated(const sc2::Unit* unit_) {

src/Dispatcher.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ struct Dispatcher: sc2::Agent {
4242
std::shared_ptr<Builder> m_builder;
4343

4444
std::vector<std::shared_ptr<Plugin>> m_plugins;
45+
46+
uint32_t m_steps;
4547
};

src/plugins/Miner.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ void DistrubuteMineralWorker(const sc2::Unit* unit_) {
138138

139139
} // namespace
140140

141+
Miner::Miner(): Plugin(7) {
142+
}
143+
141144
void Miner::OnStep(Builder* builder_) {
142145
SecureMineralsIncome(builder_);
143146
SecureVespeneIncome();
@@ -151,7 +154,12 @@ void Miner::OnUnitCreated(const sc2::Unit* unit_, Builder*) {
151154
DistrubuteMineralWorker(unit_);
152155
}
153156

154-
void Miner::OnUnitIdle(const sc2::Unit* unit_, Builder*) {
157+
void Miner::OnUnitIdle(const sc2::Unit* unit_, Builder* builder_) {
155158
if (unit_->unit_type == gHub->GetCurrentWorkerType())
156159
DistrubuteMineralWorker(unit_);
160+
161+
if (sc2::IsTownHall()(unit_->unit_type) &&
162+
unit_->assigned_harvesters <= unit_->ideal_harvesters &&
163+
builder_->CountScheduledOrders(gHub->GetCurrentWorkerType()) < 1)
164+
builder_->ScheduleOptionalOrder(gHub->GetCurrentWorkerType(), unit_);
157165
}

src/plugins/Miner.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "Plugin.h"
99

1010
struct Miner : Plugin {
11+
Miner();
12+
1113
void OnStep(Builder* builder_) final;
1214

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

src/plugins/Plugin.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2017-2021 Alexander Kurbatov
4+
5+
#include "Plugin.h"
6+
7+
Plugin::Plugin(): m_frequency(1) {
8+
}
9+
10+
Plugin::Plugin(uint32_t frequency): m_frequency(frequency) {
11+
}
12+
13+
bool Plugin::CheckFrequency(uint32_t count_) const {
14+
if (count_ % m_frequency == 0)
15+
return true;
16+
17+
return false;
18+
}

src/plugins/Plugin.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <sc2api/sc2_unit.h>
1010

1111
struct Plugin {
12+
Plugin();
13+
14+
explicit Plugin(uint32_t gameloop_frequency);
15+
1216
virtual ~Plugin() {
1317
}
1418

@@ -38,4 +42,9 @@ struct Plugin {
3842

3943
virtual void OnGameEnd() {
4044
}
45+
46+
virtual bool CheckFrequency(uint32_t count_) const;
47+
48+
private:
49+
const uint32_t m_frequency;
4150
};

0 commit comments

Comments
 (0)