Skip to content

Commit 18a41b5

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

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

src/Dispatcher.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ void Dispatcher::OnStep() {
9090

9191
gHub->OnStep();
9292

93-
for (const auto& i : m_plugins)
94-
i->OnStep(m_builder.get());
93+
for (const auto& i : m_plugins) {
94+
if (i->GetNextRunGameLoop() <= gAPI->observer().GetGameLoop()) {
95+
i->OnStep(m_builder.get());
96+
i->UpdateNextRunGameLoop();
97+
}
98+
}
9599

96100
m_builder->OnStep();
97101
}

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2017-2020 Alexander Kurbatov
4+
5+
#include "Plugin.h"
6+
#include "core/API.h"
7+
8+
Plugin::Plugin(): m_gameloop_frequency(1), m_next_run_gameloop(0) {
9+
}
10+
11+
Plugin::Plugin(uint32_t gameloop_frequency): m_gameloop_frequency(gameloop_frequency),
12+
m_next_run_gameloop(0) {
13+
}
14+
15+
uint32_t Plugin::GetNextRunGameLoop() const {
16+
return m_next_run_gameloop;
17+
}
18+
19+
void Plugin::UpdateNextRunGameLoop() {
20+
m_next_run_gameloop = m_gameloop_frequency + gAPI->observer().GetGameLoop();
21+
}

src/plugins/Plugin.h

Lines changed: 12 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,12 @@ struct Plugin {
3842

3943
virtual void OnGameEnd() {
4044
}
45+
46+
virtual uint32_t GetNextRunGameLoop() const;
47+
48+
virtual void UpdateNextRunGameLoop();
49+
50+
private:
51+
const uint32_t m_gameloop_frequency;
52+
uint32_t m_next_run_gameloop;
4153
};

0 commit comments

Comments
 (0)