Last active
January 22, 2024 11:04
-
-
Save megazalrock/375da3c6e12b38b72efd to your computer and use it in GitHub Desktop.
Cookie Clicker AI for ver 2.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Cookie Clicker AI for ver 2.0 | |
* Author: Otto Kamiya a.k.a MegazalRock | |
* License: MIT | |
*/ | |
/* global Game, numberFormatters*/ | |
(function(){ | |
"use strict"; | |
var AI = function(){ | |
this.isEnable = true; | |
this.interval = 1; | |
this.isAutoBuyEnabled = true; | |
this.isAutoBuyObjectEnabled = true; | |
this.isAutoClickEnabled = true; | |
this.upgradePurchaseWaitSecDefault = 3 * 60; | |
this.upgradePurchaseWaitSec = this.upgradePurchaseWaitSecDefault; | |
this._delta = []; | |
this.delta = Infinity; | |
this.isGoldenCookie = false; | |
this.goldenCookieCount = 0; | |
this.objects = []; | |
this.startAt = new Date(); | |
console.clear(); | |
this.start(); | |
}; | |
AI.prototype.start = function(){ | |
var self = this; | |
var prev = Date.now(); | |
var n = Date.now(); | |
(function loop(){ | |
if(!self.isEnable){ | |
return false; | |
} | |
prev = self._step(prev); | |
self._delta.push(Date.now() - n); | |
if(500 < self._delta.length){ | |
self._delta.shift(); | |
} | |
n = Date.now(); | |
if(self.isEnable){ | |
setTimeout(loop, self.interval); | |
} | |
})(); | |
var isFirst = true; | |
(function loop(){ | |
self.delta = self._getDelta(); | |
if(isFirst){ | |
self.showNextCookies(); | |
isFirst = false; | |
} | |
setTimeout(loop, 1000); | |
})(); | |
}; | |
AI.prototype._getDelta = function(){ | |
return Math.round(getAvg(this._delta)); | |
}; | |
AI.prototype._getObjectNextCps = function(object){ | |
return object.cps(object) * (object.amount + 1) || object.baseCps; | |
}; | |
AI.prototype.getObjects = function(sortOnly){ | |
var self = this; | |
var objects = Game.ObjectsById.concat(); | |
objects.sort(function(a, b){ | |
return (self._getObjectNextCps(b) / b.price) - (self._getObjectNextCps(a) / a.price); | |
}); | |
if(sortOnly){ | |
return objects; | |
} | |
var i = 0, length = objects.length; | |
var _objects = []; | |
for(;i < length; i++){ | |
if(objects[i].price < Game.cookies){ | |
_objects.push(objects[i]); | |
} | |
} | |
return _objects; | |
}; | |
AI.prototype.showObjects = function(){ | |
var self = this; | |
var objects = self.getObjects(true); | |
var i = 0, length = objects.length; | |
var object; | |
for(;i < length; i++){ | |
object = objects[i]; | |
console.log(object.name + ' ' + (object.cps(object) / object.price)); | |
} | |
}; | |
AI.prototype._getUpgrades = function(canByNow){ | |
var self = this; | |
var upgrades = Game.UpgradesInStore.concat(); | |
upgrades.sort(function(a, b){ | |
return a.basePrice - b.basePrice; | |
}); | |
var i = 0, length = upgrades.length; | |
var _upgrades = []; | |
var target = canByNow ? Game.cookies : self._getNextCookies(self.upgradePurchaseWaitSec); | |
for(;i < length; i++){ | |
if(upgrades[i].basePrice < target){ | |
_upgrades.push(upgrades[i]); | |
} | |
} | |
return _upgrades; | |
}; | |
AI.prototype._buyObject = function(){ | |
var self = this; | |
var objects = self.getObjects(); | |
var object; | |
var result = 0; | |
for(var i = 0; i < objects.length; i++){ | |
object = objects[i]; | |
if(object.price <= Game.cookies){ | |
object.buy(); | |
console.log('Object ' + object.name + ' bought'); | |
result++; | |
} | |
} | |
return result; | |
}; | |
AI.prototype._buyUpgrades = function(upgrades){ | |
if(!upgrades.length){return 0;} | |
var upgrade; | |
var result = 0; | |
for(var i = 0; i < upgrades.length; i++){ | |
upgrade = upgrades[i]; | |
if(upgrade.basePrice <= Game.cookies){ | |
upgrade.buy(); | |
console.log('Upgrade ' + upgrade.name + ' bought'); | |
result++; | |
} | |
} | |
return result; | |
}; | |
AI.prototype.autoBuy = function(){ | |
var self = this; | |
var upgrades = self._getUpgrades(); | |
var result = 0; | |
result += self._buyUpgrades(self._getUpgrades(true)); | |
if(upgrades.length){ | |
self.isAutoBuyObjectEnabled = false; | |
result += self._buyUpgrades(upgrades); | |
}else{ | |
self.isAutoBuyObjectEnabled = true; | |
} | |
if(self.isAutoBuyObjectEnabled){ | |
result += self._buyObject(); | |
} | |
if (result) { | |
if(self.isGoldenCookie && self.goldenCookieCount){ | |
self.upgradePurchaseWaitSec = self.goldenCookieCount; | |
} | |
self.showNextCookies(); | |
} | |
}; | |
AI.prototype._getNextCookies = function(sec){ | |
var self = this; | |
return Game.cookiesPs * sec + Game.computedMouseCps * sec * (1000 / self.delta); | |
}; | |
AI.prototype.showNextCookies = function(sec){ | |
var self = this; | |
if(!sec){ | |
sec = self.upgradePurchaseWaitSec; | |
} | |
console.log( | |
numberFormatters[1](self._getNextCookies(sec)) + | |
' cokkies in the next ' + | |
Math.floor(sec / 60) + 'min ' + | |
sec % 60 + 'sec' | |
); | |
}; | |
AI.prototype._autoClick = function(){ | |
var self = this; | |
Game.ClickCookie(); | |
if(Game.goldenCookie.life){ | |
Game.goldenCookie.click(); | |
self.showNextCookies(); | |
console.log(Game.goldenCookie.last + ' was clicked !'); | |
if(Game.goldenCookie.last === 'frenzy'){ | |
self.upgradePurchaseWaitSec = 77 * Game.goldenCookie.getEffectDurMod(); | |
self.goldenCookieCount = 77 * Game.goldenCookie.getEffectDurMod(); | |
if(!self.isGoldenCookie){ | |
self.isGoldenCookie = true; | |
(function loop(){ | |
if(0 < self.goldenCookieCount){ | |
self.goldenCookieCount--; | |
setTimeout(loop, 1000); | |
}else{ | |
self.goldenCookieCount = 0; | |
self.upgradePurchaseWaitSec = self.upgradePurchaseWaitSecDefault; | |
self.showNextCookies(); | |
self.isGoldenCookie = false; | |
} | |
})(); | |
} | |
} | |
} | |
}; | |
AI.prototype._step = function(prev){ | |
var self = this; | |
if(self.isAutoClickEnabled){ | |
self._autoClick(); | |
} | |
if(self.isAutoBuyEnabled){ | |
self.autoBuy(); | |
} | |
var delta = Date.now() - prev; | |
if(1000 * 60 < delta){ | |
console.clear(); | |
self.timeLong(); | |
self.showNextCookies(); | |
prev = Date.now(); | |
} | |
return prev; | |
}; | |
AI.prototype.abort = function(){ | |
this.isEnable = false; | |
}; | |
AI.prototype.enableAutoBuy = function(){ | |
this.isAutoBuyEnabled = true; | |
}; | |
AI.prototype.disableAutoBuy = function(){ | |
this.isAutoBuyEnabled = false; | |
}; | |
AI.prototype.timeLong = function(){ | |
var startTime = this.startAt.getTime(); | |
var currentTime = (new Date()).getTime(); | |
var d = currentTime - startTime; | |
console.log(Math.floor(d / 1000) + ' seconds'); | |
return d; | |
}; | |
function getAvg(array){ | |
var i = 0, length = array.length; | |
var sum = 0; | |
for(; i < length; i++){ | |
sum += array[i]; | |
} | |
return sum / length; | |
} | |
window.AI = new AI(); | |
})(); | |
/* TODO: Upgradeで稼げる量とcookies per 3 minの比較? | |
Twice系Upgradeを積極的に取りに行く? | |
ClickFrenzy系の時のCPS計算修正 | |
そもそもの予想CPS計算が怪しいから、”次の”CPSを計算する仕組みを作る? -> 本家のコードが複雑すぎてむずそう? | |
wrinkler killer | |
*/ |
@cega0r
This is old code and probably will not work with the current version of Cookie Clicker.
what code plat form did you use
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how am ı gonna use thıs