chrisrico

Candy box autoplay userscript

May 6th, 2013
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name            Candy box autoplay
  3. // @version         0.1
  4. // @license         Public Domain
  5. // @description     automatically fights the dragon in castle's keep
  6. // @include         http*://candies.aniwey.net/*
  7. // @require         https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js
  8. // ==/UserScript==
  9.  
  10. var w = unsafeWindow;
  11. var $ = w.$;
  12.  
  13. var common = {
  14.     makeInvulnerabilityPotions: function(min, max) {
  15.         var num = this.startMakingPotions(w.potions.list.invulnerability, min, max);
  16.         if (typeof num == "boolean") { return num; }
  17.         if (w.candies.nbrOwned < 2000 * num) {
  18.             console.log('not enough candies to make ' + num + ' invulnerability potions');
  19.             return false;
  20.         }
  21.        
  22.         w.tabs.select(3);
  23.         $("#cauldron_candies_quantity").val(2000 * num);
  24.         w.cauldron.putInTheCauldron();
  25.         w.cauldron.setWeAreMixing(true);
  26.         this.delayedCauldronAction(60, this.finishMakingPotions.bind(this));
  27.         return false;
  28.     },
  29.    
  30.     makeSeeds: function(min, max) {
  31.         var num = this.startMakingPotions(w.potions.list.seed, min, max);
  32.         if (typeof num == "boolean") { return num; }
  33.         if (w.candies.nbrOwned < 650 * num) {
  34.             console.log('not enough candies to make ' + num + ' seeds');
  35.             return false;
  36.         }
  37.        
  38.         w.tabs.select(3);
  39.         w.cauldron.setWeAreBoiling(true);
  40.         var that = this;
  41.         this.delayedCauldronAction(32, function() {
  42.             $("#cauldron_candies_quantity").val(650 * num);
  43.             w.cauldron.putInTheCauldron();
  44.             that.finishMakingPotions();
  45.         });
  46.         return false;
  47.     },
  48.    
  49.     makeMajorHealthPotions: function(min, max) {
  50.         var num = this.startMakingPotions(w.potions.list.majorHealth, min, max);
  51.         if (typeof num == "boolean") { return num; }
  52.         if (w.candies.nbrOwned < 100 * num || w.lollipops.nbrOwned < 100 * num) {
  53.             console.log('not enough candies or lollipops to make ' + num + ' major health');
  54.             return false;
  55.         }
  56.        
  57.         w.tabs.select(3);
  58.         $("#cauldron_lollipops_quantity").val(100 * num);
  59.         w.cauldron.putInTheCauldron();
  60.         w.cauldron.setWeAreMixing(true);
  61.         $("#cauldron_candies_quantity").val(100 * num);
  62.         w.cauldron.putInTheCauldron();
  63.         this.delayedCauldronAction(20, this.finishMakingPotions.bind(this));
  64.         return false;
  65.     },
  66.    
  67.     startMakingPotions: function(potion, min, max) {
  68.         var c = w.cauldron, num = potion.nbrOwned >= min ? 0 : max - potion.nbrOwned;
  69.         if (c.candiesInTheCauldron > 0 || c.lollipopsInTheCauldron > 0 || c.weAreBoiling || c.weAreMixing) {
  70.             return false;
  71.         } else if (num <= 0) {
  72.             return true;
  73.         }
  74.        
  75.         console.log('should make ' + num + ' ' + potion.buttonText);
  76.        
  77.         return num;
  78.     },
  79.    
  80.     delayedCauldronAction: function(actionTimer, func) {
  81.         window.setTimeout(func, (actionTimer - w.cauldron.actionTimer) * 1000);
  82.     },
  83.    
  84.     finishMakingPotions: function() {
  85.         w.cauldron.stopActions();
  86.         w.cauldron.putIntoBottles();
  87.     },
  88.    
  89.     buyTeleportScrolls: function(min, max) {
  90.         var item = w.potions.list.teleportScroll;
  91.         if (item.nbrOwned >= min) {
  92.             return true;
  93.         }
  94.         w.tabs.select(0);
  95.         while(item.nbrOwned < max) {
  96.             if (w.candies.nbrOwned < 400) {
  97.                 return false;
  98.             }
  99.             w.potions.buyScroll(400);
  100.         }
  101.         return true;
  102.     }
  103. };
  104.  
  105. var castleKeep = {
  106.    
  107.     prepare: function() {
  108.         return common.buyTeleportScrolls(10, 50)
  109.         && common.makeSeeds(10, 50)
  110.         && common.makeMajorHealthPotions(10, 50)
  111.         && common.makeInvulnerabilityPotions(10, 50);
  112.     },
  113.    
  114.     play: function() {
  115.         if (!w.quest.weAreQuestingRightNow) { return; }
  116.        
  117.         var me = w.quest.things[w.quest.getCharacterIndex()];
  118.         if (w.castleKeep.roomNumber == 6) {
  119.             this.fightDragon(me);
  120.         } else {
  121.             if (w.quest.potionUseCountdown == 0 && me.hp < me.max_hp / 2) {
  122.                 w.potions.heal(100);
  123.             }
  124.         }
  125.         window.setTimeout(this.play.bind(this), w.quest.speed);
  126.     },
  127.    
  128.     fightDragon: function(me) {
  129.         // the dragon is dead
  130.         if (w.quest.things[17].type == "none") { return; }
  131.         // the character is dead
  132.         if (me.hp == 0) { return; }
  133.        
  134.         var index = w.quest.getCharacterIndex(),
  135.             atDragon = w.quest.things[index + 1].type == "mob";
  136.        
  137.         if (atDragon) {
  138.             if (w.quest.potionUseCountdown == 0 && w.quest.invulnerabilityCountdown == 0) {
  139.                 w.potions.invulnerability();
  140.             } else if (w.quest.potionUseCountdown > 0 && w.quest.scrollUseCountdown == 0 && me.hp < me.max_hp / 2) {
  141.                 w.potions.teleportScroll();
  142.             }
  143.         } else if (index == 1 && w.quest.potionUseCountdown > 0)  {
  144.             w.potions.seed();
  145.         }
  146.     }
  147. };
  148.  
  149. var autoplay = {
  150.    
  151.     weArePlaying: false,
  152.     lastSave: 0,
  153.     players: {
  154.         5: castleKeep
  155.     },
  156.    
  157.     onload: function(value) {
  158.         $('#quest_form').append('<input type="checkbox" id="autoplayToggle">Autoplay</input>');
  159.         document.getElementById("autoplayToggle").addEventListener("click", autoplayToggleListener, false);
  160.     },
  161.    
  162.     setWeArePlaying: function(value) {
  163.         console.log('setWeArePlaying() value: ' + value);
  164.         $('#autoplayToggle').attr('checked', value);
  165.         this.weArePlaying = value;
  166.         this.start();
  167.     },
  168.    
  169.     start: function() {
  170.         if (!this.weArePlaying) { return; }
  171.        
  172.         if (w.quest.weAreQuestingRightNow) {
  173.             // if we're still on another quest, try later
  174.             this.startLater(5000);
  175.             return;
  176.         }
  177.        
  178.         var player = this.players[this.getLandIndex()];
  179.         if (player === undefined) {
  180.             // we have no player for this quest
  181.             this.setWeArePlaying(false);
  182.             return;
  183.         }
  184.        
  185.         if (!player.prepare() || w.quest.tiredTime > 0) {
  186.             // if the player is still preparing items
  187.             // or the character is tired
  188.             this.startLater(Math.max(5, w.quest.tiredTime) * 1000);
  189.             return;
  190.         }
  191.        
  192.         this.silentSave();
  193.         w.tabs.select(2);
  194.         w.quest.begin(true);
  195.         player.play();
  196.        
  197.         this.startLater(5000);
  198.     },
  199.    
  200.     startLater: function(when) {
  201.         window.setTimeout(this.start.bind(this), when);
  202.     },
  203.    
  204.     getLandIndex: function() {
  205.         return w.land.getLandIndexFromName($("#quest_destination").val());
  206.     },
  207.    
  208.     silentSave: function() {
  209.         $.ajax({
  210.             type: "POST",
  211.             url: "scripts/save.php",
  212.             data: {
  213.                 code : ((w.code === undefined || w.code == null || w.code.length == "") ? 0 : w.code),
  214.                 swordName : w.sword.name,
  215.                 swordSpecialSword : w.sword.specialSword ? 1 : 0,
  216.                 swordSpecialPower : w.sword.specialPower,
  217.                 candiesNbrOwned : w.candies.nbrOwned,
  218.                 candiesNbrThrown : w.candies.nbrThrown,
  219.                 candiesNbrEaten : w.candies.nbrEaten,
  220.                 candiesNbrTotal : w.candies.nbrTotal,
  221.                 candiesCandiesPerSecond : w.candies.candiesPerSecond,
  222.                 candiesConverterActivated : w.candiesConverter.activated ? 1 : 0,
  223.                 cauldronBookPage : w.cauldron.bookPage,
  224.                 cauldronCandies : w.cauldron.candiesInTheCauldron,
  225.                 cauldronLollipops : w.cauldron.lollipopsInTheCauldron,
  226.                 chocolateBarsNbrOwned : w.chocolateBars.nbrOwned,
  227.                 farmLollipopsPlanted : w.farm.lollipopsPlanted,
  228.                 farmCurrentFlagIndex : w.farm.currentFlagIndex,
  229.                 farmPlantingButtonsStep : w.farm.plantingButtonsStep,
  230.                 forgeStep : w.forge.step,
  231.                 shopLollipopsButtonsShown : w.shop.buy10LollipopsButtonShown ? 1 : 0,
  232.                 shopShown : w.shop.shown ? 1 : 0,
  233.                 shopTicklingStep : w.shop.ticklingStep,
  234.                 shopClickingOnLollipopStep : w.shop.clickingOnLollipopStep,
  235.                 hutStep : w.hut.step,
  236.                 hutSpeech : w.hut.speech,
  237.                 inventoryMagicianHatLetter : w.inventory.magicianHatLetter,
  238.                 lollipopsNbrOwned : w.lollipops.nbrOwned,
  239.                 lollipopsNbrInStock : w.lollipops.nbrInStock,
  240.                 lollipopsNbrBought : w.lollipops.nbrBought,
  241.                 mainNbrOfSecondsSinceLastMinInterval : w.main.nbrOfSecondsSinceLastMinInterval,
  242.                 mainNbrOfSecondsSinceLastHourInterval : w.main.nbrOfSecondsSinceLastHourInterval,
  243.                 mainNbrOfSecondsSinceLastDayInterval : w.main.nbrOfSecondsSinceLastDayInterval,
  244.                 mountGoblinBasicChestProbability : w.mountGoblin.basicChestProbability,
  245.                 peacefulForestBasicChestProbability : w.peacefulForest.basicChestProbability,
  246.                 peacefulForestPoniesEncountered : w.peacefulForest.poniesEncountered,
  247.                 objectsHaveObjectKey : w.objects.list.key.have ? 1 : 0,
  248.                 objectsHaveObjectHutMap : w.objects.list.hutMap.have ? 1 : 0,
  249.                 objectsHaveObjectWellMap : w.objects.list.wellMap.have ? 1 : 0,
  250.                 objectsHaveObjectSwampMap : w.objects.list.swampMap.have ? 1 : 0,
  251.                 objectsHaveObjectBoots : w.objects.list.boots.have ? 1 : 0,
  252.                 objectsHaveObjectMagicianHat : w.objects.list.magicianHat.have ? 1 : 0,
  253.                 objectsHaveObjectPinkRing : w.objects.list.pinkRing.have ? 1 : 0,
  254.                 objectsHaveObjectForgeMap : w.objects.list.forgeMap.have ? 1 : 0,
  255.                 objectsHaveObjectCandiesConverter : w.objects.list.candiesConverter.have ? 1 : 0,
  256.                 objectsHaveObjectPlateArmour : w.objects.list.plateArmour.have ? 1 : 0,
  257.                 objectsHaveObjectCauldron : w.objects.list.cauldron.have ? 1 : 0,
  258.                 objectsHaveObjectMagicalHorn : w.objects.list.magicalHorn.have ? 1 : 0,
  259.                 objectsHaveObjectHornOfPlenty : w.objects.list.hornOfPlenty.have ? 1 : 0,
  260.                 objectsHaveObjectOldAmulet : w.objects.list.oldAmulet.have ? 1 : 0,
  261.                 potionsShownHealth : w.potions.list.health.shown ? 1 : 0,
  262.                 potionsShownEscape : w.potions.list.escape.shown ? 1 : 0,
  263.                 potionsShownBerserk : w.potions.list.berserk.shown ? 1 : 0,
  264.                 potionsShownFireScroll : w.potions.list.fireScroll.shown ? 1 : 0,
  265.                 potionsShownAcidRainScroll : w.potions.list.acidRainScroll.shown ? 1 : 0,
  266.                 potionsShownTeleportScroll : w.potions.list.teleportScroll.shown ? 1 : 0,
  267.                 potionsShownEarthquakeScroll : w.potions.list.earthquakeScroll.shown ? 1 : 0,
  268.                 potionsShownImpInvocationScroll : w.potions.list.impInvocationScroll.shown ? 1 : 0,
  269.                 potionsShownMajorHealth : w.potions.list.majorHealth.shown ? 1 : 0,
  270.                 potionsShownInvulnerability : w.potions.list.invulnerability.shown ? 1 : 0,
  271.                 potionsShownTurtle : w.potions.list.turtle.shown ? 1 : 0,
  272.                 potionsShownJelly : w.potions.list.jelly.shown ? 1 : 0,
  273.                 potionsShownSeed : w.potions.list.seed.shown ? 1 : 0,
  274.                 potionsShownCloning : w.potions.list.cloning.shown ? 1 : 0,
  275.                 potionsShownSuperman : w.potions.list.superman.shown ? 1 : 0,
  276.                 potionsShownGmooh : w.potions.list.gmooh.shown ? 1 : 0,
  277.                 potionsNbrOwnedHealth : w.potions.list.health.nbrOwned,
  278.                 potionsNbrOwnedEscape : w.potions.list.escape.nbrOwned,
  279.                 potionsNbrOwnedBerserk : w.potions.list.berserk.nbrOwned,
  280.                 potionsNbrOwnedFireScroll : w.potions.list.fireScroll.nbrOwned,
  281.                 potionsNbrOwnedAcidRainScroll : w.potions.list.acidRainScroll.nbrOwned,
  282.                 potionsNbrOwnedTeleportScroll : w.potions.list.teleportScroll.nbrOwned,
  283.                 potionsNbrOwnedEarthquakeScroll : w.potions.list.earthquakeScroll.nbrOwned,
  284.                 potionsNbrOwnedImpInvocationScroll : w.potions.list.impInvocationScroll.nbrOwned,
  285.                 potionsNbrOwnedMajorHealth : w.potions.list.majorHealth.nbrOwned,
  286.                 potionsNbrOwnedInvulnerability : w.potions.list.invulnerability.nbrOwned,
  287.                 potionsNbrOwnedTurtle : w.potions.list.turtle.nbrOwned,
  288.                 potionsNbrOwnedJelly : w.potions.list.jelly.nbrOwned,
  289.                 potionsNbrOwnedSeed : w.potions.list.seed.nbrOwned,
  290.                 potionsNbrOwnedCloning : w.potions.list.cloning.nbrOwned,
  291.                 potionsNbrOwnedSuperman : w.potions.list.superman.nbrOwned,
  292.                 potionsNbrOwnedGmooh : w.potions.list.gmooh.nbrOwned,
  293.                 questMaxLandOrder : w.quest.maxLandOrder,
  294.                 questTiredTime : w.quest.tiredTime,
  295.                 spellsFasterCandiesFibo1 : w.spells.fasterCandiesFiboPrev,
  296.                 spellsFasterCandiesFibo2 : w.spells.fasterCandiesFiboCurr,
  297.                 swampStep : w.swamp.step,
  298.                 tabsAnimation : w.tabs.animation,
  299.                 wishingWellSpeech : w.wishingWell.speech,
  300.                 wishingWellStep : w.wishingWell.step,
  301.                 yourselfCanSurpass : w.yourself.canSurpass ? 1 : 0,
  302.                 developperComputerWon : w.developperComputer.won ? 1 : 0
  303.             },
  304.             success: function(msg) {
  305.                 if(msg != "Erreur") {
  306.                     this.lastSave = new Date().getTime();
  307.                     console.log('Last autosaved at ' + this.lastSave);
  308.                     if(msg.substring(0,5) != "<br /"){
  309.                         w.code = msg.substring(0,5);
  310.                     }
  311.                 }
  312.             }
  313.         });
  314.     }
  315. };
  316.  
  317. autoplay.onload();
  318. function autoplayToggleListener() {
  319.     autoplay.setWeArePlaying(document.getElementById("autoplayToggle").checked);
  320. }
  321.  
  322. w.autoplay = autoplay;
Advertisement
Add Comment
Please, Sign In to add comment