From fc726bcd3feb9d4094bc84505586b54c8ffb7495 Mon Sep 17 00:00:00 2001 From: Michael Holtan Date: Tue, 30 Mar 2021 11:18:13 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20Fix=20gas=20saturation,=C2=A0#34?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/Dispatcher.cpp | 7 +++++-- src/Dispatcher.h | 2 ++ src/plugins/Miner.cpp | 10 +++++++++- src/plugins/Miner.h | 2 ++ src/plugins/Plugin.cpp | 18 ++++++++++++++++++ src/plugins/Plugin.h | 9 +++++++++ 6 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/plugins/Plugin.cpp diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index 91cd6df..740ad04 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -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); @@ -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_) { diff --git a/src/Dispatcher.h b/src/Dispatcher.h index ed92901..ed2d304 100644 --- a/src/Dispatcher.h +++ b/src/Dispatcher.h @@ -42,4 +42,6 @@ struct Dispatcher: sc2::Agent { std::shared_ptr m_builder; std::vector> m_plugins; + + uint32_t m_steps; }; diff --git a/src/plugins/Miner.cpp b/src/plugins/Miner.cpp index 3c32549..f97c6e8 100644 --- a/src/plugins/Miner.cpp +++ b/src/plugins/Miner.cpp @@ -138,6 +138,9 @@ void DistrubuteMineralWorker(const sc2::Unit* unit_) { } // namespace +Miner::Miner(): Plugin(7) { +} + void Miner::OnStep(Builder* builder_) { SecureMineralsIncome(builder_); SecureVespeneIncome(); @@ -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_); } diff --git a/src/plugins/Miner.h b/src/plugins/Miner.h index cbf02a3..cf947c5 100644 --- a/src/plugins/Miner.h +++ b/src/plugins/Miner.h @@ -8,6 +8,8 @@ #include "Plugin.h" struct Miner : Plugin { + Miner(); + void OnStep(Builder* builder_) final; void OnUnitCreated(const sc2::Unit* unit_, Builder*) final; diff --git a/src/plugins/Plugin.cpp b/src/plugins/Plugin.cpp new file mode 100644 index 0000000..5a35811 --- /dev/null +++ b/src/plugins/Plugin.cpp @@ -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; +} diff --git a/src/plugins/Plugin.h b/src/plugins/Plugin.h index c8fd809..9e73b6a 100644 --- a/src/plugins/Plugin.h +++ b/src/plugins/Plugin.h @@ -9,6 +9,10 @@ #include struct Plugin { + Plugin(); + + explicit Plugin(uint32_t gameloop_frequency); + virtual ~Plugin() { } @@ -38,4 +42,9 @@ struct Plugin { virtual void OnGameEnd() { } + + virtual bool CheckFrequency(uint32_t count_) const; + + private: + const uint32_t m_frequency; };