// ======================================================================
 
//      Hello and welcome.
//  This is CookiAscender, the script that will get you the
//  Endless Cycle achievement.
 
//  There is only one option and here it is.
        var ascenderStopAt = 1000
//  It controlls at what number of total ascentions the script stops.
 
//  To control the script use the console commands:
//      ascenderStart(), ascenderStop() and ascenderToggle().
//  What they do is quite obvious.
 
//  Check out the steam guide for more info and feedback.
//  https://steamcommunity.com/sharedfiles/filedetails/?id=2603832571
 
// ======================================================================
 
const ascenderDefaultTimeout = 200
var ascenderInProgress = false;
var ascenderReincarnationPressed = false;
var ascenderStopper = false;
 
setInterval(function() {
 
    if ((!ascenderStopper) && (Game.resets >= ascenderStopAt)) { 
        ascenderStopper = true
        console.log(ascenderStopAt + ' Ascentions reached!')
        Game.Notify(ascenderStopAt + ' Ascentions reached!','CookiAscender stopped.',[25,12])
    }
 
    if (ascenderStopper) return    // stopper
 
    if ((Game.ascendMeterLevel > 0) && (!ascenderInProgress)) { // if at least 1 level of ascention is reached - ascend.
        Game.Ascend(1); // 1 means skip the confirmation dialogue window
        ascenderInProgress = true;
        setTimeout(function() {
        Game.AscendTimer=Game.AscendDuration;
        },ascenderDefaultTimeout);
    }
 
    if (ascenderInProgress && Game.AscendTimer == 0) {  // if the animation timer ran out - reincarnate
        if (!ascenderReincarnationPressed) {
            Game.Reincarnate(1);
            ascenderReincarnationPressed = true;
       
            setTimeout(function() {
                ascenderInProgress = false;
                ascenderReincarnationPressed = false;
                console.log('Ascention #' + Game.resets + ' started. ' + (ascenderStopAt - Game.resets) + ' to go.')
            }, ascenderDefaultTimeout*2); // timeout is here to smooth the process
        }
       
        
    }
 
    if (!ascenderInProgress) {  // if not ascending - play the game
        for (var i in Game.UpgradesInStore)
        {
            var me=Game.UpgradesInStore[i];
            if (!me.isVaulted() && me.pool!='toggle' && me.pool!='tech') me.buy(1);    // buy every upgrade
        }
        let eks = -1;
        for (let i = 0; i < Game.ObjectsById.length; i++) {
            if (Game.ObjectsById[i].price < Game.cookies){
                eks = i;
            }
        }
        try {
            Game.ObjectsById[eks].buy(1000);    // buy the most expencive building
            Game.ObjectsById[0].buy(1000);      // also buy cursors
        }
        catch {
            // this is some weird fuckery that does not want to get fixed by resonable delays
            // before initialization. if you know how to fix it - contact me.
        }
    }
},ascenderDefaultTimeout/10)
 
function ascenderStart() { ascenderStopper = false }
function ascenderStop() { ascenderStopper = true }
function ascenderToggle() {
    if (ascenderStopper) { ascenderStopper = false }
    else ascenderStopper = true
}
 
if (ascenderStopper) console.log('\n\n\nCookiAscender is ready to start, '+ (ascenderStopAt - Game.resets) +' ascentions to go! \n\nType \"ascenderStart()\" whenever ready!\n');