Advertisement
AuoroP

BuildAuoro

Nov 22nd, 2013
2,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // http://pastebin.com/fDzye8tD
  2. /*
  3. Cookie Clicker version 1.0393 automation script.
  4.  
  5. Features: golden cookie display, autoclicker and autobuyer.
  6. +changes title bar to (!) if there is a golden cookie.
  7. +press C to toggle autoclicker.
  8. +press A to toggle autobuy that chooses the best.
  9. +maximizes Cps for the Cookie price among buildings and upgrades.
  10. +minimal computation - only makes plans once for each purchase.
  11. +what will be bought is displayed.
  12. +when it will be bought is displayed.
  13. +polls once a second and updates the estimated time.
  14. +never buys bingo center, elder pledge, elder covenant, revoke covenant.
  15. ?if another purchase has become available it will make another plan.
  16. ?TODO add another key to switch max buildings between Centennial (default), Mathematician and Base 10 achievements.
  17.  
  18. */
  19.  
  20. /*
  21.  
  22. CC2BA togglable (no page reload) bookmarklet
  23. javascript:
  24. (
  25.   function()
  26.   {
  27.     var uri="http://pastebin.com/raw.php?i=fDzye8tD";
  28.     var script = document.getElementById("BuildAuoro");
  29.     if(script) script.parentNode.removeChild( script );
  30.     script=document.createElement("script");
  31.     script.setAttribute("id","BuildAuoro");
  32.     script.setAttribute("src",uri);
  33.     document.getElementById("wrapper").appendChild(script);
  34.   }()
  35. );
  36. */
  37.  
  38.  
  39.  
  40. var BuildAuoro =
  41. {
  42.   goldenScannerID: 0,
  43.   autoclick: false,
  44.   autoclickTID: 0,
  45.   autobuy: false,
  46.   next:
  47.   {
  48.     effiency: 0,
  49.     item: -1,
  50.     price: 0,
  51.     upgradeCount: 0,
  52.     timeout: 0,
  53.     timeoutID: 0,
  54.  
  55.     autobuy: function()
  56.     { // make sure our estimate was correct
  57.       if(BuildAuoro.next.price <= Game.cookies)
  58.       {
  59.         BuildAuoro.next.item.buy();
  60.         BuildAuoro.findBestPurchase();
  61.       }
  62.       else
  63.       {
  64.         BuildAuoro.next.wait();
  65.       }
  66.     },
  67.  
  68.     wait: function()
  69.     {
  70.       // if a new upgrade is available, let us revist all the options
  71.       if(BuildAuoro.upgradeCount != Game.UpgradesInStore.length )
  72.       {
  73.         BuildAuoro.findBestPurchase();
  74.         return;
  75.       }
  76.       var cookieDeficit = Math.max(0, BuildAuoro.next.price - Game.cookies )
  77.       var t = cookieDeficit / Game.cookiesPs;
  78.       if(Game.cookiesPs == 0)
  79.         t = Infinity;
  80.       BuildAuoro.next.timeout = Math.min( 1000, Math.round( (2/60 + t) * 1000) ); //in ms with lag; Time = C / CpS
  81.       BuildAuoro.next.timeoutID = setTimeout(BuildAuoro.next.autobuy,  BuildAuoro.next.timeout);
  82.  
  83.       BuildAuoro.tickMsg("Saving for "+  BuildAuoro.next.item.name  +"... "+  BuildAuoro.timeFormat(t), BuildAuoro.next.timeout);
  84.     },
  85.   },
  86.  
  87.   toggleActive: function(event)
  88.   {
  89.     if(event.keyCode == 65) // A
  90.     {
  91.       BuildAuoro.autobuy = !BuildAuoro.autobuy;
  92.  
  93.       var msg = "Autobuy is ";
  94.  
  95.       if(BuildAuoro.autobuy)
  96.       {
  97.         msg += "on";
  98.         setTimeout( function(){  BuildAuoro.findBestPurchase(); }, 107);
  99.       }
  100.       else
  101.       {
  102.         msg += "off";
  103.         clearTimeout( BuildAuoro.next.timeoutID );
  104.       }
  105.       BuildAuoro.tickMsg(msg)
  106.  
  107.     }
  108.     else if(event.keyCode == 67) // C
  109.     {
  110.       BuildAuoro.autoclick = !BuildAuoro.autoclick;
  111.  
  112.       if(BuildAuoro.autoclick)
  113.       {
  114.         BuildAuoro.autoclickTID = setInterval(Game.ClickCookie, 400);
  115.       }
  116.       else
  117.       {
  118.         clearInterval( BuildAuoro.autoclickTID );
  119.       }
  120.     }
  121.   },
  122.  
  123.   findBestPurchase: function()
  124.   {
  125.     // reset the next upgrade data
  126.     BuildAuoro.next.effiency = 0;
  127.     BuildAuoro.next.itemID = -1;
  128.     BuildAuoro.next.timeout = 0;
  129.  
  130.  
  131.     var baseCpS = Game.cookiesPs, scanItem, scanItemID, scanItemCpS, scanItemEff;
  132.     // scan list of currently available upgrades for highest eff
  133.     BuildAuoro.upgradeCount = Game.UpgradesInStore.length;
  134.     for(var i = 0; i < Game.UpgradesInStore.length; i++)
  135.     {
  136.       scanItem = Game.UpgradesInStore[i];
  137.       scanItemID = scanItem.id;
  138.  
  139.       // auto buy forbidden from: bingo center, elder pledge, elder covenant, revoke
  140.       if(scanItemID != 64 && scanItemID != 74 && scanItemID != 84 && scanItemID != 85)
  141.       {
  142.         // simulate buying by toggle() ?
  143.         Game.UpgradesById[scanItemID].toggle();
  144.         // update Game to reflect the purchase ?
  145.         Game.CalculateGains();
  146.         // find the upgraded building cps
  147.         scanItemCpS = Game.cookiesPs;
  148.         // undo the purchase
  149.         Game.UpgradesById[scanItemID].toggle();
  150.         // update Game to reflect undoing purchase ?
  151.         Game.CalculateGains();
  152.         scanItemEff = (scanItemCpS - baseCpS) / scanItem.basePrice;
  153.         //scanItemEff = 1.15 * scanItem.basePrice / baseCpS + scanItem.basePrice / (scanItemCpS - baseCpS);
  154.         if( scanItemEff > BuildAuoro.next.effiency)
  155.         {
  156.           BuildAuoro.next.effiency = scanItemEff;
  157.           BuildAuoro.next.item = scanItem;
  158.           BuildAuoro.next.price = scanItem.basePrice;
  159.         }
  160.       }
  161.  
  162.     }
  163.  
  164.     // scan list of currently available upgrades for highest eff
  165.     var bMax = [200,200,100,100,100,100,100,100,100,100]; // gets us cent build achievs
  166.     //var bMax = [128,128,128,64,32,16,8,4,2,1]; // gets us all Mathematician achievs
  167.  
  168.     //for(var i = Game.ObjectsById.length-1; i >= 0; i--)
  169.     for(var i = 0; i < Game.ObjectsById.length; i++)
  170.     {
  171.       scanItem = Game.ObjectsById[i];
  172.       // simulate buying with amount increment
  173.       scanItem.amount += 1;
  174.       Game.CalculateGains();
  175.       // find the upgraded building cps
  176.       scanItemCpS = Game.cookiesPs;
  177.       // undo the simulation
  178.       scanItem.amount -= 1;
  179.       Game.CalculateGains();
  180.  
  181.       scanItemEff = (scanItemCpS - baseCpS) / scanItem.price;
  182.       //scanItemEff =  1.15 * scanItem.basePrice / baseCpS + scanItem.basePrice / (scanItemCpS - baseCpS);
  183.       if(scanItem.amount < bMax[i] && scanItemEff > BuildAuoro.next.effiency)
  184.       //if(scanItemEff > BuildAuoro.next.effiency)
  185.       {
  186.         BuildAuoro.next.effiency = scanItemEff;
  187.         BuildAuoro.next.item = scanItem;
  188.         BuildAuoro.next.price = scanItem.price;
  189.       }
  190.     }
  191.  
  192.     BuildAuoro.next.wait();
  193.   },
  194.  
  195.  
  196.   timeFormat: function(t)
  197.   {
  198.     var res = "";
  199.     t = Math.round( t );
  200.     var d = Math.floor( t / 86400 );
  201.     if(d > 0)
  202.     {
  203.       t -= d*86400;
  204.       res+= d + "d ";
  205.     }
  206.  
  207.     var h = Math.floor( t / 3600 );
  208.     if(h > 0)
  209.     {
  210.       t -= h*3600;
  211.       res+= h + "h ";
  212.     }
  213.  
  214.     var m = Math.floor( t / 60 );
  215.     if(m > 0)
  216.     {
  217.       t -= m*60;
  218.       res+= m + "m ";
  219.     }
  220.  
  221.     var s = t;
  222.     res+= s + "s ";
  223.  
  224.  
  225. //    res +=  "= " +(21600*d + 3600*h + 60*m + s)+ "s ";
  226.     return res;
  227.   },
  228.  
  229.   tickMsg: function(str,t)
  230.   {
  231.     if(t === undefined)
  232.       t = 90;
  233.     Game.Ticker = str;
  234.     Game.TickerAge = t;
  235.   },
  236.  
  237. };
  238.  
  239. //var gctMin=364.4*1000, gctMax=445.3*1000;
  240. BuildAuoro.goldenScannerID = setInterval
  241. (
  242.   function gcr()
  243.   {
  244.     if (Game.goldenCookie.life > 0) document.title = '(!)';
  245.  
  246. //    if(Game.UpgradesById[53].bought) // unlocked with 27 GC clicks: serendipity
  247. //    {
  248. //      gctMin=96.3*1000, gctMax=122.9*1000;
  249. //    }
  250. //    else if(Game.UpgradesById[52].bought) // unlocked with 7 GC clicks: lukcy day
  251. //    {
  252. //      gctMin=187.0*1000, gctMax=233.4*1000;
  253. //    }
  254. //    if (Game.goldenCookie.time >= gctMin && Game.goldenCookie.time <= gctMax ) document.title = '???,???,???';
  255.   }
  256. ,17
  257. );
  258.  
  259.  
  260.  
  261. // listen for keypress
  262. document.addEventListener('keydown', BuildAuoro.toggleActive );
  263.  
  264. BuildAuoro.tickMsg("BuildAuoro loaded! Press A for autobuy, C for autoclick.");
  265. //Game.Ticker = "BuildAuoro loaded!";
  266. //Game.TickerAge = 90;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement