Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix gas saturation, #34 #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace {
Historican gHistory("dispatcher");
} // namespace

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

Expand Down Expand Up @@ -91,9 +92,11 @@ void Dispatcher::OnStep() {
gHub->OnStep();

for (const auto& i : m_plugins)
i->OnStep(m_builder.get());
if (i->CheckFrequency(m_steps))
i->OnStep(m_builder.get());

m_builder->OnStep();
m_steps++;
}

void Dispatcher::OnUnitCreated(const sc2::Unit* unit_) {
Expand Down
2 changes: 2 additions & 0 deletions src/Dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ struct Dispatcher: sc2::Agent {
std::shared_ptr<Builder> m_builder;

std::vector<std::shared_ptr<Plugin>> m_plugins;

uint32_t m_steps;
};
10 changes: 9 additions & 1 deletion src/plugins/Miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ void DistrubuteMineralWorker(const sc2::Unit* unit_) {

} // namespace

Miner::Miner(): Plugin(7) {
}

void Miner::OnStep(Builder* builder_) {
SecureMineralsIncome(builder_);
SecureVespeneIncome();
Expand All @@ -151,7 +154,12 @@ void Miner::OnUnitCreated(const sc2::Unit* unit_, Builder*) {
DistrubuteMineralWorker(unit_);
}

void Miner::OnUnitIdle(const sc2::Unit* unit_, Builder*) {
void Miner::OnUnitIdle(const sc2::Unit* unit_, Builder* builder_) {
if (unit_->unit_type == gHub->GetCurrentWorkerType())
DistrubuteMineralWorker(unit_);

if (sc2::IsTownHall()(unit_->unit_type) &&
unit_->assigned_harvesters <= unit_->ideal_harvesters &&
builder_->CountScheduledOrders(gHub->GetCurrentWorkerType()) < 1)
builder_->ScheduleOptionalOrder(gHub->GetCurrentWorkerType(), unit_);
}
2 changes: 2 additions & 0 deletions src/plugins/Miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "Plugin.h"

struct Miner : Plugin {
Miner();

void OnStep(Builder* builder_) final;

void OnUnitCreated(const sc2::Unit* unit_, Builder*) final;
Expand Down
18 changes: 18 additions & 0 deletions src/plugins/Plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// The MIT License (MIT)
//
// Copyright (c) 2017-2021 Alexander Kurbatov

#include "Plugin.h"

Plugin::Plugin(): m_frequency(1) {
}

Plugin::Plugin(uint32_t frequency): m_frequency(frequency) {
}

bool Plugin::CheckFrequency(uint32_t count_) const {
if (count_ % m_frequency == 0)
return true;

return false;
}
9 changes: 9 additions & 0 deletions src/plugins/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include <sc2api/sc2_unit.h>

struct Plugin {
Plugin();

explicit Plugin(uint32_t gameloop_frequency);

virtual ~Plugin() {
}

Expand Down Expand Up @@ -38,4 +42,9 @@ struct Plugin {

virtual void OnGameEnd() {
}

virtual bool CheckFrequency(uint32_t count_) const;

private:
const uint32_t m_frequency;
};