Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1d4fc36

Browse files
committedMar 21, 2021
fix: Fix gas saturation, #34
Only check vespene workers every 10 frames, to prevent too many vespene workers from being added and oversaturated.
1 parent e3e3e6b commit 1d4fc36

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed
 

‎src/plugins/Miner.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,17 @@ void DistrubuteMineralWorker(const sc2::Unit* unit_) {
138138

139139
} // namespace
140140

141+
Miner::Miner() {
142+
m_turns_to_skip = 7;
143+
}
144+
141145
void Miner::OnStep(Builder* builder_) {
146+
if (m_skip_until_turn > gAPI->observer().GetGameLoop())
147+
return;
148+
149+
// add 1 extra, otherwise skipping 1 turn would trigger next turn, etc
150+
m_skip_until_turn = m_turns_to_skip + (gAPI->observer().GetGameLoop() + 1);
151+
142152
SecureMineralsIncome(builder_);
143153
SecureVespeneIncome();
144154

@@ -151,7 +161,12 @@ void Miner::OnUnitCreated(const sc2::Unit* unit_, Builder*) {
151161
DistrubuteMineralWorker(unit_);
152162
}
153163

154-
void Miner::OnUnitIdle(const sc2::Unit* unit_, Builder*) {
164+
void Miner::OnUnitIdle(const sc2::Unit* unit_, Builder* builder_) {
155165
if (unit_->unit_type == gHub->GetCurrentWorkerType())
156166
DistrubuteMineralWorker(unit_);
167+
168+
if (sc2::IsTownHall()(unit_->unit_type) &&
169+
unit_->assigned_harvesters <= unit_->ideal_harvesters &&
170+
builder_->CountScheduledOrders(gHub->GetCurrentWorkerType()) < 1)
171+
builder_->ScheduleOptionalOrder(gHub->GetCurrentWorkerType(), unit_);
157172
}

‎src/plugins/Miner.h

+2
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.h

+4
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ struct Plugin {
3838

3939
virtual void OnGameEnd() {
4040
}
41+
42+
protected:
43+
uint32_t m_turns_to_skip = 0;
44+
uint32_t m_skip_until_turn = 0; // set with turns_to_skip + (CurrentGameLoop + 1)
4145
};

‎src/plugins/QuarterMaster.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,20 @@ float CalcConsumptionRate::operator()(float sum, const sc2::Unit* unit_) const {
111111

112112
} // namespace
113113

114-
QuarterMaster::QuarterMaster():
115-
Plugin(), m_skip_until_frame(0) {
114+
QuarterMaster::QuarterMaster() {
115+
m_turns_to_skip = 10 * 22; // 10 Seconds * 22 Frames Per Second (~22.4)
116116
}
117117

118118
void QuarterMaster::OnStep(Builder* builder_) {
119-
if (m_skip_until_frame > gAPI->observer().GetGameLoop())
119+
if (m_skip_until_turn > gAPI->observer().GetGameLoop())
120120
return;
121121

122122
auto units = gAPI->observer().GetUnits();
123123
std::list<Order> orders = builder_->GetOrders();
124124

125125
if (!orders.empty()
126126
&& orders.begin()->unit_type_id == gHub->GetCurrentSupplyType()) {
127-
m_skip_until_frame = gAPI->observer().GetGameLoop() + m_frames_to_skip;
127+
m_skip_until_turn = m_turns_to_skip + (gAPI->observer().GetGameLoop() + 1);
128128
return; // wait 10 seconds if already scheduled Supply
129129
}
130130

@@ -140,13 +140,13 @@ void QuarterMaster::OnStep(Builder* builder_) {
140140
gHistory.info() << "Request additional supplies: " <<
141141
expected_consumption << " >= " << expected_supply << std::endl;
142142

143-
m_skip_until_frame = gAPI->observer().GetGameLoop() + m_frames_to_skip;
143+
m_skip_until_turn = m_turns_to_skip + (gAPI->observer().GetGameLoop() + 1);
144144

145145
builder_->ScheduleObligatoryOrder(gHub->GetCurrentSupplyType(), true);
146146
}
147147

148148
void QuarterMaster::OnUnitCreated(const sc2::Unit* unit_, Builder*) {
149149
if (unit_->unit_type == gHub->GetCurrentSupplyType()) {
150-
m_skip_until_frame = 0;
150+
m_skip_until_turn = 0;
151151
}
152152
}

‎src/plugins/QuarterMaster.h

-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,4 @@ struct QuarterMaster : Plugin {
1313
void OnStep(Builder* builder_) final;
1414

1515
void OnUnitCreated(const sc2::Unit* unit_, Builder*) final;
16-
17-
private:
18-
const size_t m_frames_to_skip = 10 * 22; // 10 Seconds * 22 Frames Per Second (~22.4)
19-
size_t m_skip_until_frame;
2016
};

0 commit comments

Comments
 (0)
Please sign in to comment.