From 4a83fb0802afb81ba7f3e4912c8879308454576c Mon Sep 17 00:00:00 2001 From: Andrei Abrudan Date: Thu, 16 Jun 2022 22:47:32 +0100 Subject: [PATCH 1/2] Fix tree farm --- .../src/user/cheat/world/AutoTreeFarm.cpp | 49 ++++++++++++++++--- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/cheat-library/src/user/cheat/world/AutoTreeFarm.cpp b/cheat-library/src/user/cheat/world/AutoTreeFarm.cpp index ab22d3c..bee71a3 100644 --- a/cheat-library/src/user/cheat/world/AutoTreeFarm.cpp +++ b/cheat-library/src/user/cheat/world/AutoTreeFarm.cpp @@ -116,11 +116,48 @@ namespace cheat::feature } }; + template > + class lra_map { + using key_value_pair = std::pair; + using list_iterator = typename std::list::iterator; + public: + void put(const KeyT& key, const ValT& val) { + auto it = elem_map.find(key); + // if element already in the map, don't modify it. + if (it != elem_map.end()) + return; + + items_list.push_front(key_value_pair(key, val)); + elem_map[key] = items_list.begin(); + + if (Size < elem_map.size()) { + { + const KeyT& last_key = items_list.back().first; + elem_map.erase(last_key); + } + items_list.pop_back(); + } + } + + ValT& get(const KeyT& key) { + auto it = elem_map.find(key); + if (it == elem_map.end()) + throw std::runtime_error("Tried to access key not present in map"); + return it->second->second; + } + + bool exists(const KeyT& key) const { + return elem_map.find(key) != elem_map.end(); + } + protected: + std::list items_list; + std::unordered_map elem_map; + }; void AutoTreeFarm::OnGameUpdate() { - static std::unordered_map s_AttackCountMap; + static lra_map s_AttackCountMap; static std::queue s_AttackQueue; static std::unordered_set s_AttackQueueSet; @@ -173,11 +210,12 @@ namespace cheat::feature if (m_AttackPerTree > 0) { - if (s_AttackCountMap.count(position) == 0) - s_AttackCountMap[position] = 0; + if (!s_AttackCountMap.exists(position)) + s_AttackCountMap.put(position, 0); - auto& attackCount = s_AttackCountMap[position]; + auto& attackCount = s_AttackCountMap.get(position); attackCount++; + if (attackCount > static_cast(m_AttackPerTree)) continue; } @@ -186,9 +224,6 @@ namespace cheat::feature app::MoleMole_NetworkManager_RequestHitTreeDropNotify(networkManager, position, position, treeType, nullptr); break; } - - if (s_AttackCountMap.size() > 1000) - s_AttackCountMap.clear(); } } From 479f50f910cbdf08372a22915b0a69ac8d5e24f1 Mon Sep 17 00:00:00 2001 From: Andrei Abrudan Date: Sat, 18 Jun 2022 02:45:34 +0100 Subject: [PATCH 2/2] Fixed a bug where multiple trees in range would cause RepeatDelay to be ignored resulting in less wood than expected --- cheat-library/src/user/cheat/world/AutoTreeFarm.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cheat-library/src/user/cheat/world/AutoTreeFarm.cpp b/cheat-library/src/user/cheat/world/AutoTreeFarm.cpp index bee71a3..c1e7874 100644 --- a/cheat-library/src/user/cheat/world/AutoTreeFarm.cpp +++ b/cheat-library/src/user/cheat/world/AutoTreeFarm.cpp @@ -158,7 +158,6 @@ namespace cheat::feature void AutoTreeFarm::OnGameUpdate() { static lra_map s_AttackCountMap; - static std::queue s_AttackQueue; static std::unordered_set s_AttackQueueSet; static uint64_t s_LastAttackTimestamp = 0; @@ -179,7 +178,7 @@ namespace cheat::feature if (s_AttackQueueSet.count(tree) > 0) continue; - if (tree->fields._lastTreeDropTimeStamp + m_RepeatDelay > timestamp) + if (s_LastAttackTimestamp + m_RepeatDelay > timestamp) continue; auto position = tree->fields._.realBounds.m_Center; @@ -220,7 +219,7 @@ namespace cheat::feature continue; } - tree->fields._lastTreeDropTimeStamp = timestamp; + s_LastAttackTimestamp = timestamp; app::MoleMole_NetworkManager_RequestHitTreeDropNotify(networkManager, position, position, treeType, nullptr); break; }