// http://pastebin.com/fDzye8tD

/*

Cookie Clicker version 1.0393 automation script.

Features: golden cookie display, autoclicker and autobuyer.

+changes title bar to (!) if there is a golden cookie.

+press C to toggle autoclicker.

+press A to toggle autobuy that chooses the best.

+maximizes Cps for the Cookie price among buildings and upgrades.

+minimal computation - only makes plans once for each purchase.

+what will be bought is displayed.

+when it will be bought is displayed.

+polls once a second and updates the estimated time.

+never buys bingo center, elder pledge, elder covenant, revoke covenant.

?if another purchase has become available it will make another plan.

?TODO add another key to switch max buildings between Centennial (default), Mathematician and Base 10 achievements.

*/

/*

CC2BA togglable (no page reload) bookmarklet

javascript:

(

function()

{

var uri="http://pastebin.com/raw.php?i=fDzye8tD";

var script = document.getElementById("BuildAuoro");

if(script) script.parentNode.removeChild( script );

script=document.createElement("script");

script.setAttribute("id","BuildAuoro");

script.setAttribute("src",uri);

document.getElementById("wrapper").appendChild(script);

}()

);

*/

var BuildAuoro =

{

goldenScannerID : 0 ,

autoclick : false ,

autoclickTID : 0 ,

autobuy : false ,

next :

{

effiency : 0 ,

item : - 1 ,

price : 0 ,

upgradeCount : 0 ,

timeout : 0 ,

timeoutID : 0 ,

autobuy : function ( )

{ // make sure our estimate was correct

if ( BuildAuoro. next . price <= Game. cookies )

{

BuildAuoro. next . item . buy ( ) ;

BuildAuoro. findBestPurchase ( ) ;

}

else

{

BuildAuoro. next . wait ( ) ;

}

} ,

wait : function ( )

{

// if a new upgrade is available, let us revist all the options

if ( BuildAuoro. upgradeCount != Game. UpgradesInStore . length )

{

BuildAuoro. findBestPurchase ( ) ;

return ;

}

var cookieDeficit = Math . max ( 0 , BuildAuoro. next . price - Game. cookies )

var t = cookieDeficit / Game. cookiesPs ;

if ( Game. cookiesPs == 0 )

t = Infinity ;

BuildAuoro. next . timeout = Math . min ( 1000 , Math . round ( ( 2 / 60 + t ) * 1000 ) ) ; //in ms with lag; Time = C / CpS

BuildAuoro. next . timeoutID = setTimeout ( BuildAuoro. next . autobuy , BuildAuoro. next . timeout ) ;

BuildAuoro. tickMsg ( "Saving for " + BuildAuoro. next . item . name + "... " + BuildAuoro. timeFormat ( t ) , BuildAuoro. next . timeout ) ;

} ,

} ,

toggleActive : function ( event )

{

if ( event. keyCode == 65 ) // A

{

BuildAuoro. autobuy = ! BuildAuoro. autobuy ;

var msg = "Autobuy is " ;

if ( BuildAuoro. autobuy )

{

msg += "on" ;

setTimeout ( function ( ) { BuildAuoro. findBestPurchase ( ) ; } , 107 ) ;

}

else

{

msg += "off" ;

clearTimeout ( BuildAuoro. next . timeoutID ) ;

}

BuildAuoro. tickMsg ( msg )

}

else if ( event. keyCode == 67 ) // C

{

BuildAuoro. autoclick = ! BuildAuoro. autoclick ;

if ( BuildAuoro. autoclick )

{

BuildAuoro. autoclickTID = setInterval ( Game. ClickCookie , 400 ) ;

}

else

{

clearInterval ( BuildAuoro. autoclickTID ) ;

}

}

} ,

findBestPurchase : function ( )

{

// reset the next upgrade data

BuildAuoro. next . effiency = 0 ;

BuildAuoro. next . itemID = - 1 ;

BuildAuoro. next . timeout = 0 ;

var baseCpS = Game. cookiesPs , scanItem , scanItemID , scanItemCpS , scanItemEff ;

// scan list of currently available upgrades for highest eff

BuildAuoro. upgradeCount = Game. UpgradesInStore . length ;

for ( var i = 0 ; i < Game. UpgradesInStore . length ; i ++ )

{

scanItem = Game. UpgradesInStore [ i ] ;

scanItemID = scanItem. id ;

// auto buy forbidden from: bingo center, elder pledge, elder covenant, revoke

if ( scanItemID != 64 && scanItemID != 74 && scanItemID != 84 && scanItemID != 85 )

{

// simulate buying by toggle() ?

Game. UpgradesById [ scanItemID ] . toggle ( ) ;

// update Game to reflect the purchase ?

Game. CalculateGains ( ) ;

// find the upgraded building cps

scanItemCpS = Game. cookiesPs ;

// undo the purchase

Game. UpgradesById [ scanItemID ] . toggle ( ) ;

// update Game to reflect undoing purchase ?

Game. CalculateGains ( ) ;

scanItemEff = ( scanItemCpS - baseCpS ) / scanItem. basePrice ;

//scanItemEff = 1.15 * scanItem.basePrice / baseCpS + scanItem.basePrice / (scanItemCpS - baseCpS);

if ( scanItemEff > BuildAuoro. next . effiency )

{

BuildAuoro. next . effiency = scanItemEff ;

BuildAuoro. next . item = scanItem ;

BuildAuoro. next . price = scanItem. basePrice ;

}

}

}

// scan list of currently available upgrades for highest eff

var bMax = [ 200 , 200 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 ] ; // gets us cent build achievs

//var bMax = [128,128,128,64,32,16,8,4,2,1]; // gets us all Mathematician achievs

//for(var i = Game.ObjectsById.length-1; i >= 0; i--)

for ( var i = 0 ; i < Game. ObjectsById . length ; i ++ )

{

scanItem = Game. ObjectsById [ i ] ;

// simulate buying with amount increment

scanItem. amount += 1 ;

Game. CalculateGains ( ) ;

// find the upgraded building cps

scanItemCpS = Game. cookiesPs ;

// undo the simulation

scanItem. amount -= 1 ;

Game. CalculateGains ( ) ;

scanItemEff = ( scanItemCpS - baseCpS ) / scanItem. price ;

//scanItemEff = 1.15 * scanItem.basePrice / baseCpS + scanItem.basePrice / (scanItemCpS - baseCpS);

if ( scanItem. amount < bMax [ i ] && scanItemEff > BuildAuoro. next . effiency )

//if(scanItemEff > BuildAuoro.next.effiency)

{

BuildAuoro. next . effiency = scanItemEff ;

BuildAuoro. next . item = scanItem ;

BuildAuoro. next . price = scanItem. price ;

}

}

BuildAuoro. next . wait ( ) ;

} ,

timeFormat : function ( t )

{

var res = "" ;

t = Math . round ( t ) ;

var d = Math . floor ( t / 86400 ) ;

if ( d > 0 )

{

t -= d * 86400 ;

res += d + "d " ;

}

var h = Math . floor ( t / 3600 ) ;

if ( h > 0 )

{

t -= h * 3600 ;

res += h + "h " ;

}

var m = Math . floor ( t / 60 ) ;

if ( m > 0 )

{

t -= m * 60 ;

res += m + "m " ;

}

var s = t ;

res += s + "s " ;

// res += "= " +(21600*d + 3600*h + 60*m + s)+ "s ";

return res ;

} ,

tickMsg : function ( str , t )

{

if ( t === undefined )

t = 90 ;

Game. Ticker = str ;

Game. TickerAge = t ;

} ,

} ;

//var gctMin=364.4*1000, gctMax=445.3*1000;

BuildAuoro. goldenScannerID = setInterval

(

function gcr ( )

{

if ( Game. goldenCookie . life > 0 ) document. title = '(!)' ;

// if(Game.UpgradesById[53].bought) // unlocked with 27 GC clicks: serendipity

// {

// gctMin=96.3*1000, gctMax=122.9*1000;

// }

// else if(Game.UpgradesById[52].bought) // unlocked with 7 GC clicks: lukcy day

// {

// gctMin=187.0*1000, gctMax=233.4*1000;

// }

// if (Game.goldenCookie.time >= gctMin && Game.goldenCookie.time <= gctMax ) document.title = '???,???,???';

}

, 17

) ;

// listen for keypress

document. addEventListener ( 'keydown' , BuildAuoro. toggleActive ) ;

BuildAuoro. tickMsg ( "BuildAuoro loaded! Press A for autobuy, C for autoclick." ) ;

//Game.Ticker = "BuildAuoro loaded!";