Advertisement
shameless_inc

Cookie Clicker automation script v2

Apr 12th, 2015
4,246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>shameless_inc's Cookie Miner v2<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  2. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>A Cookie Clicker automation script<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  3.  
  4. //Paste this JavaScript code into the Cookie Clicker's JavaScript console.
  5. //You can access the console by opening the web browser'.s dev tools with F12
  6. //This script will click the cookie as fast as possible.
  7. //If a golden cookie spawns, it will be automatically clicked within a second.
  8. //Upgrades will be automatically bought when you have enough cookies.
  9. //There's a known bug that causes the grandmapocalypse to result in a loop
  10. //opening the prompt so many times that you will not be able to close it.
  11.  
  12. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>RELOADING THE PAGE WILL STOP THE SCRIPT.<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  13.  
  14. //This script will intelligently select which objects to buy to get CPS efficiently
  15. //by evaluating how much Time (t) you will need to be able to afford the item.
  16. //Also, it will consider how much CPS you will actually get from the object and how much it costs (c).
  17. //The formula used to evaluate the items is CPS/(c*t).
  18. //The script will wait until you have enough cookies and buy it as soon you can afford it.
  19.  
  20. //The script will every second make a report in the console.
  21. //The report consists of the values Cookies (you have in your bank), CPS (without clicks),
  22. //the delta from the last second (how many cookies you gained since the last report, think of this as CPS including mouse clicks),
  23. //which item is going to be bought next and an estimation on how long you will have to wait until that.
  24. //The values are not perfectly precise and may be inconsistent at times, but they are mostly right
  25. //and give you an idea how much cookies you will get and what this script is doing. The time estimation is based on the
  26. //delta value and might be bogus at times (e.g. when something was bought in the last second) but it should eventually correct itself.
  27.  
  28. //If you have any feedback, please feel free to contact me on Reddit:
  29. //https://www.reddit.com/r/CookieClicker/comments/32bcwg/i_made_a_v2_of_my_cookie_clicker_automation/
  30. //Turning off some effects can make it run faster in the later game.
  31. //I hope you like my work! :)
  32.  
  33. var runClickLoop = false;
  34. var runGoldenCookieLoop = false;
  35. var runUpgradeloop = false;
  36.  
  37. function clickLoop () {
  38.     setTimeout(function () {
  39.         Game.ClickCookie();
  40.         if(runClickLoop) {
  41.             clickLoop();
  42.         }
  43.     }, 1);
  44. }
  45. function goldenCookieLoop () {
  46.     setTimeout(function () {
  47.         Game.goldenCookie.click();
  48.         if(runGoldenCookieLoop) {
  49.             goldenCookieLoop();
  50.         }
  51.     }, 1000);
  52. }
  53.  
  54. function upgradeLoop () {
  55.     setTimeout(function () {
  56.         for(var i = 0; i < Game.UpgradesInStore.length; i++){
  57.             Game.UpgradesInStore[i].buy()
  58.         }
  59.         if(runUpgradeloop) {
  60.             upgradeLoop();
  61.         }
  62.     }, 5);
  63. }
  64.  
  65. function runAllLoops(){
  66.     runClickLoop = true;
  67.     runGoldenCookieLoop = true;
  68.     runUpgradeloop = true;
  69.    
  70.     clickLoop();
  71.     goldenCookieLoop();
  72.     upgradeLoop();
  73. }
  74. function stopAllLoops(){
  75.     runClickLoop = false;
  76.     runGoldenCookieLoop = false;
  77.     runUpgradeloop = false;
  78. }
  79.  
  80. function showCookieDisplay (bool){
  81.     if(bool)
  82.         l('cookies').style.display = 'block';
  83.     else
  84.         l('cookies').style.display = 'none'
  85. }
  86.  
  87. //Handles data reporting for other services and provides ticks via notifications
  88. function ReportService () {
  89.     var self = this;
  90.    
  91.     self.timing = 50;
  92.    
  93.     self.count = Game.cookiesd;
  94.     self.cps = Game.cps;
  95.     self.delta = 0;
  96.     self.deltaBySecond = 0;
  97.     self.index = 0;
  98.     self.lastCount = 0;
  99.     self.lastDelta = 0;
  100.     self.lastCountBySecond = 0;
  101.    
  102.     self.subscribers = Array();
  103.     self.reports = Array();
  104.    
  105.     self.tick = function() {
  106.         setTimeout(function(){
  107.             self.refreshData();
  108.             self.notifySubscribers();
  109.             self.consoleReport();
  110.             self.tick();
  111.         }, self.timing);
  112.     }
  113.    
  114.     self.subscribe = function(fn) {
  115.         self.subscribers.push(fn);
  116.     }
  117.    
  118.     self.addReport = function(fn) {
  119.         self.reports.push(fn);
  120.     }
  121.    
  122.     self.refreshData = function() {
  123.         self.lastCount = self.count;
  124.         self.count = Game.cookiesd;
  125.         self.cps = Game.cookiesPs*(1-Game.cpsSucked);
  126.         self.lastDelta = self.delta;
  127.         self.delta = self.count - self.lastCount;
  128.         self.index++;
  129.     }
  130.    
  131.     self.notifySubscribers = function() {
  132.         for(var i = 0; i < self.subscribers.length; i++){
  133.             self.subscribers[i]();
  134.         }
  135.     }
  136.    
  137.     self.getReports = function() {
  138.         var report = '';
  139.         for(var i = 0; i < self.reports.length; i++){
  140.             var curReport = self.reports[i]();
  141.             if(curReport != ''){
  142.                 report = report + curReport;
  143.                 if(i < self.reports.length){
  144.                     report = report + '\n';
  145.                 }
  146.             }
  147.         }
  148.        
  149.         return report;
  150.     }
  151.    
  152.     self.consoleReport = function() {
  153.         //Report every second
  154.         if(self.index * self.timing % 1000 === 0){
  155.             self.deltaBySecond = self.count - self.lastCountBySecond;
  156.             self.lastCountBySecond = self.count;
  157.             var report = self.getReports();
  158.             if(report != ''){
  159.                 report = '\n' + report;
  160.             }
  161.             console.log('Cookies:\t' + Beautify(self.count) + '\nCPS:\t\t' + Beautify(self.cps) + '\nDelta:\t\t' + Beautify(self.deltaBySecond) + report);
  162.         }
  163.     }
  164.    
  165.     self.tick();
  166. }
  167.  
  168. //Handles buying objects
  169. function ObjectService(reportService){
  170.     var self = this;
  171.    
  172.     //Next object being bought
  173.     self.target;
  174.    
  175.     //Find best object to buy next
  176.     self.findBestObject = function(){
  177.         //TODO: don't operate on first delta values
  178.         var bestValue = 0;
  179.         var bestIndex = -1;
  180.         for(var i = 0; i < Game.ObjectsById.length; i++){
  181.             var obj = Game.ObjectsById[i];
  182.             var value = 0;
  183.             if(obj.locked === 0){
  184.                 value = obj.cps() / (obj.getPrice() * (obj.getPrice() / reportService.delta));
  185.             }
  186.             if(value > bestValue){
  187.                 bestValue = value;
  188.                 bestIndex = i;
  189.             }
  190.         }
  191.         //Set target to most efficient buy
  192.         self.target = Game.ObjectsById[bestIndex];
  193.     }
  194.    
  195.     self.buyTarget = function(){
  196.         self.target.buy();
  197.         self.target = undefined;
  198.     }
  199.    
  200.     self.onTick = function(){
  201.         if(self.target != undefined){
  202.             if(reportService.count >= self.target.getPrice()){
  203.                 self.buyTarget();
  204.             }
  205.         }
  206.         else{
  207.             self.findBestObject();
  208.         }
  209.     }
  210.    
  211.     self.report = function(){
  212.         if(self.target != undefined){
  213.             var estimation = Math.round(((self.target.getPrice() - reportService.count) / reportService.deltaBySecond));
  214.             return 'Next buy:\t' + self.target.name + '\t|\tEstimated waiting time:\t' + estimation + ' seconds';
  215.         }
  216.         else
  217.             return '';
  218.     }
  219.    
  220.     reportService.subscribe(self.onTick);
  221.     reportService.addReport(self.report);
  222. }
  223.  
  224. var Report = new ReportService();
  225. var ObjectService = new ObjectService(Report);
  226. runAllLoops();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement