From c9600caa61326d3c933b150e6617d05aae5e4954 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 20 Apr 2018 08:48:31 +0200 Subject: [PATCH] Use game object to buy buildings Signed-off-by: Knut Ahlers --- autocookieclicker.user.js | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/autocookieclicker.user.js b/autocookieclicker.user.js index c9b46b4..e661781 100644 --- a/autocookieclicker.user.js +++ b/autocookieclicker.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Automate CookieClicker // @namespace https://luzifer.io/ -// @version 0.16.2 +// @version 0.17.0 // @description Automate everything! // @author Knut Ahlers // @source https://github.com/Luzifer/automate-cookie-clicker @@ -53,12 +53,19 @@ function executeAutoActions() { } // Get the top enabled purchase to be made - let availableProducts = $('.product.unlocked.enabled').filter(productFilter); - if (availableProducts.length > 0 && Game.buyMode === 1) { // buyMode 1 = buy, -1 = sell - let product = $(availableProducts[availableProducts.length - 1]); + let availableProducts = Game.ObjectsById.filter(obj => obj.price < Game.cookies && obj.amount < purchaseSteps); + while (availableProducts.length > 0 && Game.buyMode === 1) { // buyMode 1 = buy, -1 = sell + let product = availableProducts[availableProducts.length - 1]; - product.click(); - note('Purchased ' + product.find('.title:first').text() + ' for you.'); + let buyAmount = 0; + for (buyAmount = purchaseSteps - product.amount; buyAmount > 0; buyAmount--) { + if (product.getSumPrice(buyAmount) <= Game.cookies) { + break; + } + } + + product.buy(buyAmount); + availableProducts = Game.ObjectsById.filter(obj => obj.price < Game.cookies && obj.amount < purchaseSteps); } manageDragon(); @@ -78,12 +85,6 @@ function controlAutoClicker() { } } -function getMaxBuy() { - let topPurchaseCount = Game.ObjectsById[Game.ObjectsN - 1].amount; - - return Math.max(Math.ceil((topPurchaseCount + 1) / purchaseSteps), 1) * purchaseSteps; -} - function hasActiveClickBuff() { let hasBuff = false; for (let key in Game.buffs) { @@ -123,11 +124,6 @@ function note(msg, quick = true) { Game.Notify('Auto-CookieClicker', msg, [12, 0], quick, true); } -function productFilter() { - let owned = Game.ObjectsById[parseInt($(this).attr('id').replace(/^product/, ''))].amount; - return owned < getMaxBuy(); -} - function upgradeFilter() { let onClickHandler = $(this).attr('onclick'); if (onClickHandler == null) {