SHARE
TWEET

Cookie Clicker bot

a guest Sep 5th, 2013 1,209 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // by toby hinloopen - toby at kutcomputers dot nl
  2.  
  3. // Remove existing API, if present
  4.  
  5. if(window.CCAPI) {
  6.   window.CCAPI.destroy();
  7.   window.CCAPI = undefined;
  8. }
  9.  
  10. // API
  11.  
  12. var CCAPI = (function() {
  13.   var CCAPI = this;
  14.   var objects = {};
  15.   var upgrades = {};
  16.   var stepCallbacks = [];
  17.  
  18.   var stepActionPerformed = null;
  19.   var performAction = function(desc) {
  20.     if(stepActionPerformed) {
  21.       console.warn("Ignoring '"+stepActionPerformed+"': already performed '"+desc+"'");
  22.       return false;
  23.     }
  24.     stepActionPerformed = desc || true;
  25.     return true;
  26.   };
  27.  
  28.   var runApiLogic = function() {
  29.     if(!stepCallbacks.length) { return; }
  30.     stepActionPerformed = null;
  31.     for(var i=0; i<stepCallbacks.length; i++) {
  32.       stepCallbacks[i](CCAPI);
  33.     }
  34.     if(!stepActionPerformed) console.log("No action performed.");
  35.   };
  36.  
  37.   var runGameLogic = Game.Logic;
  38.   Game.Logic = function() {
  39.     runApiLogic();
  40.     runGameLogic();
  41.   }
  42.  
  43.   var Obj = this.Obj = function(id) { this.id = id; };
  44.   Obj.prototype.sell = function() { return this.sellable() && performAction("sell "+this.name()) && (Game.ObjectsById[this.id].sell() && true || true); };
  45.   Obj.prototype.buy = function() { return this.buyable() && performAction("buy "+this.name()) && (Game.ObjectsById[this.id].buy() && true || true); };
  46.   Obj.prototype.name = function() { return Game.ObjectsById[this.id].name; };
  47.   Obj.prototype.cps = function() { return Game.ObjectsById[this.id].cps(); };
  48.   Obj.prototype.amount = function() { return Game.ObjectsById[this.id].amount; };
  49.   Obj.prototype.bought = function() { return Game.ObjectsById[this.id].bought; };
  50.   Obj.prototype.price = function() { return Game.ObjectsById[this.id].price; };
  51.   Obj.prototype.sellable = function(){ return this.amount() > 0; };
  52.   Obj.prototype.buyable = function(){ return this.price() <= CCAPI.cookies(); };
  53.  
  54.   for(var i=0; i<Game.ObjectsN; i++) {
  55.     var gameObj = Game.ObjectsById[i];
  56.     objects[gameObj.id] = objects[gameObj.name] = new this.Obj(gameObj.id);
  57.   }
  58.  
  59.   var Upgrade = this.Upgrade = function(id){ this.id = id; };
  60.   Upgrade.prototype.buy = function(){ return this.buyable() && performAction("buy "+this.name()) && (Game.UpgradesById[this.id].buy() && true || true); };
  61.   Upgrade.prototype.buyable = function(){ return this.unlocked() && !this.bought() && this.price() <= CCAPI.cookies(); };
  62.   Upgrade.prototype.unlocked = function(){ return Game.UpgradesById[this.id].unlocked > 0; };
  63.   Upgrade.prototype.price = function(){ return Game.UpgradesById[this.id].basePrice; };
  64.   Upgrade.prototype.bought = function(){ return Game.UpgradesById[this.id].bought > 0; };
  65.   Upgrade.prototype.name = function(){ return Game.UpgradesById[this.id].name; };
  66.  
  67.   for(var i=0; i<Game.UpgradesN; i++) {
  68.     var gameUpgrade = Game.UpgradesById[i];
  69.     upgrades[gameUpgrade.id] = upgrades[gameUpgrade.name] = new this.Upgrade(gameUpgrade.id);
  70.   }
  71.  
  72.   this.goldenCookieLife = function() { return Game.goldenCookie.life; }
  73.   this.clickGoldenCookie = function() { return CCAPI.goldenCookieLife() > 0 && performAction("click golden cookie") && (Game.goldenCookie.click() && true || true) }
  74.   this.clickCookie = function() { return performAction("click cookie") && (Game.ClickCookie() && true || true); };
  75.   this.cps = function() { return Game.cookiesPs; }
  76.   this.cookies = function() { return Game.cookies; };
  77.   this.step = function(fn) { stepCallbacks.push(fn); };
  78.   this.obj = function(idOrName) { return objects[idOrName]; };
  79.   this.objlen = function() { return Game.ObjectsN; };
  80.   this.upgrade = function(idOrName) { return upgrades[idOrName]; };
  81.   this.upgradelen = function() { return Game.UpgradesN; };
  82.   this.destroy = function() { Game.Logic = runGameLogic; };
  83.   this.reset = function() { Game.Reset(true); Game.T = 0; };
  84.   this.multiplier = 1;
  85.  
  86.   return this;
  87. }).call({});
  88.  
  89. // BOT LOGIC
  90.  
  91. var infoContainer = document.createElement("div");
  92. infoContainer.style.background = "black";
  93. infoContainer.style.width = "300px";
  94. infoContainer.style.position = "fixed";
  95. infoContainer.style.bottom = "0px";
  96. infoContainer.style.left = "0px";
  97. infoContainer.style.padding = "3px";
  98. infoContainer.style.zIndex = "100000001";
  99. document.body.appendChild(infoContainer);
  100.  
  101. var buyingInfo = document.createElement("div");
  102. infoContainer.appendChild(buyingInfo);
  103.  
  104. var timeInfo = document.createElement("div");
  105. infoContainer.appendChild(timeInfo);
  106.  
  107. CCAPI.Obj.prototype.paybackPeriod = CCAPI.Upgrade.prototype.paybackPeriod = function() {
  108.   return this.price() / this.cps();
  109. };
  110.  
  111. CCAPI.Obj.prototype.buyableDelay = CCAPI.Upgrade.prototype.buyableDelay = function() {
  112.   return this.buyable() ? 0 : (this.price() - CCAPI.cookies()) / CCAPI.cps();
  113. };
  114.  
  115. CCAPI.reset();
  116.  
  117. var mreached = false;
  118. CCAPI.step(function(api) {
  119.   if(!mreached && api.cookies() >= 1000000) {
  120.     var secs = (Game.T / Game.fps).toFixed();
  121.     var timeStr = "Time: "+Math.floor(secs/60)+"m "+(secs%60)+"s";
  122.     alert("1M cookies reached @ "+timeStr);
  123.     mreached = true;
  124.   }
  125. });
  126.  
  127. CCAPI.step(function(api) {
  128.  
  129.   var secs = (Game.T / Game.fps).toFixed();
  130.   timeInfo.textContent = "Time: "+Math.floor(secs/60)+"m "+(secs%60)+"s";
  131.  
  132.   if(api.clickGoldenCookie())
  133.     return;
  134.  
  135.   var cheapestUpgrade = null;
  136.   for(var j=0; j<api.upgradelen(); j++) {
  137.     var upgrade = api.upgrade(j);
  138.     if(!upgrade.bought() && upgrade.unlocked() && (cheapestUpgrade == null || cheapestUpgrade.price() > upgrade.price())) {
  139.       cheapestUpgrade = upgrade;
  140.     }
  141.   }
  142.  
  143.   var targetObj = null;
  144.   for(var i=0; i<api.objlen(); i++) {
  145.     var obj = api.obj(i);
  146.     if(targetObj == null || obj.buyableDelay() + obj.paybackPeriod() < targetObj.buyableDelay() + targetObj.paybackPeriod()) {
  147.       targetObj = obj;
  148.     }
  149.   }
  150.  
  151.   targetBuyable = cheapestUpgrade && cheapestUpgrade.price()*2 < targetObj.price() ? cheapestUpgrade : targetObj;
  152.  
  153.   buyingInfo.textContent = "Buying "+targetBuyable.name()+" in "+targetBuyable.buyableDelay().toFixed();
  154.   if(targetBuyable.buy()) return;
  155.   api.clickCookie();
  156. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top