Path: blob/main/assets/javascript/cookieclicker/minigameGrimoire.js
15361 views
var M={};1M.parent=Game.Objects['Wizard tower'];2M.parent.minigame=M;3M.launch=function()4{5var M=this;6M.name=M.parent.minigameName;7M.init=function(div)8{9//populate div with html and initialize values1011M.spells={12'conjure baked goods':{13name:'Conjure Baked Goods',14desc:'Summon half an hour worth of your CpS, capped at 15% of your cookies owned.',15failDesc:'Trigger a 15-minute clot and lose 15 minutes of CpS.',16icon:[21,11],17costMin:2,18costPercent:0.4,19win:function()20{21var val=Math.max(7,Math.min(Game.cookies*0.15,Game.cookiesPs*60*30));22Game.Earn(val);23Game.Notify('Conjure baked goods!','You magic <b>'+Beautify(val)+' cookie'+(val==1?'':'s')+'</b> out of thin air.',[21,11],6);24Game.Popup('<div style="font-size:80%;">+'+Beautify(val)+' cookie'+(val==1?'':'s')+'!</div>',Game.mouseX,Game.mouseY);25},26fail:function()27{28var buff=Game.gainBuff('clot',60*15,0.5);29var val=Math.min(Game.cookies*0.15,Game.cookiesPs*60*15)+13;30val=Math.min(Game.cookies,val);31Game.Spend(val);32Game.Notify(buff.name,buff.desc,buff.icon,6);33Game.Popup('<div style="font-size:80%;">Backfire!<br>Summoning failed! Lost '+Beautify(val)+' cookie'+(val==1?'':'s')+'!</div>',Game.mouseX,Game.mouseY);34},35},36'hand of fate':{37name:'Force the Hand of Fate',38desc:'Summon a random golden cookie. Each existing golden cookie makes this spell +15% more likely to backfire.',39failDesc:'Summon an unlucky wrath cookie.',40icon:[22,11],41costMin:10,42costPercent:0.6,43failFunc:function(fail)44{45return fail+0.15*Game.shimmerTypes['golden'].n;46},47win:function()48{49var newShimmer=new Game.shimmer('golden',{noWrath:true});50var choices=[];51choices.push('frenzy','multiply cookies');52if (!Game.hasBuff('Dragonflight')) choices.push('click frenzy');53if (Math.random()<0.1) choices.push('chain cookie','cookie storm','blab');54if (Game.BuildingsOwned>=10 && Math.random()<0.25) choices.push('building special');55//if (Math.random()<0.2) choices.push('clot','cursed finger','ruin cookies');56if (Math.random()<0.15) choices=['cookie storm drop'];57if (Math.random()<0.0001) choices.push('free sugar lump');58newShimmer.force=choose(choices);59if (newShimmer.force=='cookie storm drop')60{61newShimmer.sizeMult=Math.random()*0.75+0.25;62}63Game.Popup('<div style="font-size:80%;">Promising fate!</div>',Game.mouseX,Game.mouseY);64},65fail:function()66{67var newShimmer=new Game.shimmer('golden',{wrath:true});68var choices=[];69choices.push('clot','ruin cookies');70if (Math.random()<0.1) choices.push('cursed finger','blood frenzy');71if (Math.random()<0.003) choices.push('free sugar lump');72if (Math.random()<0.1) choices=['blab'];73newShimmer.force=choose(choices);74Game.Popup('<div style="font-size:80%;">Backfire!<br>Sinister fate!</div>',Game.mouseX,Game.mouseY);75},76},77'stretch time':{78name:'Stretch Time',79desc:'All active buffs gain 10% more time (up to 5 more minutes).',80failDesc:'All active buffs are shortened by 20% (up to 10 minutes shorter).',81icon:[23,11],82costMin:8,83costPercent:0.2,84win:function()85{86var changed=0;87for (var i in Game.buffs)88{89var me=Game.buffs[i];90var gain=Math.min(Game.fps*60*5,me.maxTime*0.1);91me.maxTime+=gain;92me.time+=gain;93changed++;94}95if (changed==0){Game.Popup('<div style="font-size:80%;">No buffs to alter!</div>',Game.mouseX,Game.mouseY);return -1;}96Game.Popup('<div style="font-size:80%;">Zap! Buffs lengthened.</div>',Game.mouseX,Game.mouseY);97},98fail:function()99{100var changed=0;101for (var i in Game.buffs)102{103var me=Game.buffs[i];104var loss=Math.min(Game.fps*60*10,me.time*0.2);105me.time-=loss;106me.time=Math.max(me.time,0);107changed++;108}109if (changed==0){Game.Popup('<div style="font-size:80%;">No buffs to alter!</div>',Game.mouseX,Game.mouseY);return -1;}110Game.Popup('<div style="font-size:80%;">Backfire!<br>Fizz! Buffs shortened.</div>',Game.mouseX,Game.mouseY);111},112},113'spontaneous edifice':{114name:'Spontaneous Edifice',115desc:'The spell picks a random building you could afford if you had twice your current cookies, and gives it to you for free. The building selected must be under 400, and cannot be your most-built one (unless it is your only one).',116failDesc:'Lose a random building.',117icon:[24,11],118costMin:20,119costPercent:0.75,120win:function()121{122var buildings=[];123var max=0;124var n=0;125for (var i in Game.Objects)126{127if (Game.Objects[i].amount>max) max=Game.Objects[i].amount;128if (Game.Objects[i].amount>0) n++;129}130for (var i in Game.Objects)131{if ((Game.Objects[i].amount<max || n==1) && Game.Objects[i].getPrice()<=Game.cookies*2 && Game.Objects[i].amount<400) buildings.push(Game.Objects[i]);}132if (buildings.length==0){Game.Popup('<div style="font-size:80%;">No buildings to improve!</div>',Game.mouseX,Game.mouseY);return -1;}133var building=choose(buildings);134building.buyFree(1);135Game.Popup('<div style="font-size:80%;">A new '+building.single+'<br>bursts out of the ground.</div>',Game.mouseX,Game.mouseY);136},137fail:function()138{139if (Game.BuildingsOwned==0){Game.Popup('<div style="font-size:80%;">Backfired, but no buildings to destroy!</div>',Game.mouseX,Game.mouseY);return -1;}140var buildings=[];141for (var i in Game.Objects)142{if (Game.Objects[i].amount>0) buildings.push(Game.Objects[i]);}143var building=choose(buildings);144building.sacrifice(1);145Game.Popup('<div style="font-size:80%;">Backfire!<br>One of your '+building.plural+'<br>disappears in a puff of smoke.</div>',Game.mouseX,Game.mouseY);146},147},148'haggler\'s charm':{149name:'Haggler\'s Charm',150desc:'Upgrades are 2% cheaper for 1 minute.',151failDesc:'Upgrades are 2% more expensive for an hour.<q>What\'s that spell? Loadsamoney!</q>',152icon:[25,11],153costMin:10,154costPercent:0.1,155win:function()156{157Game.killBuff('Haggler\'s misery');158var buff=Game.gainBuff('haggler luck',60,2);159Game.Popup('<div style="font-size:80%;">Upgrades are cheaper!</div>',Game.mouseX,Game.mouseY);160},161fail:function()162{163Game.killBuff('Haggler\'s luck');164var buff=Game.gainBuff('haggler misery',60*60,2);165Game.Popup('<div style="font-size:80%;">Backfire!<br>Upgrades are pricier!</div>',Game.mouseX,Game.mouseY);166},167},168'summon crafty pixies':{169name:'Summon Crafty Pixies',170desc:'Buildings are 2% cheaper for 1 minute.',171failDesc:'Buildings are 2% more expensive for an hour.',172icon:[26,11],173costMin:10,174costPercent:0.2,175win:function()176{177Game.killBuff('Nasty goblins');178var buff=Game.gainBuff('pixie luck',60,2);179Game.Popup('<div style="font-size:80%;">Crafty pixies!<br>Buildings are cheaper!</div>',Game.mouseX,Game.mouseY);180},181fail:function()182{183Game.killBuff('Crafty pixies');184var buff=Game.gainBuff('pixie misery',60*60,2);185Game.Popup('<div style="font-size:80%;">Backfire!<br>Nasty goblins!<br>Buildings are pricier!</div>',Game.mouseX,Game.mouseY);186},187},188'gambler\'s fever dream':{189name:'Gambler\'s Fever Dream',190desc:'Cast a random spell at half the magic cost, with twice the chance of backfiring.',191icon:[27,11],192costMin:3,193costPercent:0.05,194win:function()195{196var spells=[];197var selfCost=M.getSpellCost(M.spells['gambler\'s fever dream']);198for (var i in M.spells)199{if (i!='gambler\'s fever dream' && (M.magic-selfCost)>=M.getSpellCost(M.spells[i])*0.5) spells.push(M.spells[i]);}200if (spells.length==0){Game.Popup('<div style="font-size:80%;">No eligible spells!</div>',Game.mouseX,Game.mouseY);return -1;}201var spell=choose(spells);202var cost=M.getSpellCost(spell)*0.5;203setTimeout(function(){204var out=M.castSpell(spell,{cost:cost,failChanceMax:0.5,passthrough:true});205if (!out)206{207M.magic+=selfCost;208setTimeout(function(){209Game.Popup('<div style="font-size:80%;">That\'s too bad!<br>Magic refunded.</div>',Game.mouseX,Game.mouseY);210},1500);211}212},1000);213Game.Popup('<div style="font-size:80%;">Casting '+spell.name+'<br>for '+Beautify(cost)+' magic...</div>',Game.mouseX,Game.mouseY);214},215},216'resurrect abomination':{217name:'Resurrect Abomination',218desc:'Instantly summon a wrinkler if conditions are fulfilled.',219failDesc:'Pop one of your wrinklers.',220icon:[28,11],221costMin:20,222costPercent:0.1,223win:function()224{225var out=Game.SpawnWrinkler();226if (!out){Game.Popup('<div style="font-size:80%;">Unable to spawn a wrinkler!</div>',Game.mouseX,Game.mouseY);return -1;}227Game.Popup('<div style="font-size:80%;">Rise, my precious!</div>',Game.mouseX,Game.mouseY);228},229fail:function()230{231var out=Game.PopRandomWrinkler();232if (!out){Game.Popup('<div style="font-size:80%;">Backfire!<br>But no wrinkler was harmed.</div>',Game.mouseX,Game.mouseY);return -1;}233Game.Popup('<div style="font-size:80%;">Backfire!<br>So long, ugly...</div>',Game.mouseX,Game.mouseY);234},235},236'diminish ineptitude':{237name:'Diminish Ineptitude',238desc:'Spells backfire 10 times less for the next 5 minutes.',239failDesc:'Spells backfire 5 times more for the next 10 minutes.',240icon:[29,11],241costMin:5,242costPercent:0.2,243win:function()244{245Game.killBuff('Magic inept');246var buff=Game.gainBuff('magic adept',5*60,10);247Game.Popup('<div style="font-size:80%;">Ineptitude diminished!</div>',Game.mouseX,Game.mouseY);248},249fail:function()250{251Game.killBuff('Magic adept');252var buff=Game.gainBuff('magic inept',10*60,5);253Game.Popup('<div style="font-size:80%;">Backfire!<br>Ineptitude magnified!</div>',Game.mouseX,Game.mouseY);254},255},256};257M.spellsById=[];var n=0;258for (var i in M.spells){M.spells[i].id=n;M.spellsById[n]=M.spells[i];n++;}259260261M.computeMagicM=function()262{263var towers=Math.max(M.parent.amount,1);264var lvl=Math.max(M.parent.level,1);265M.magicM=Math.floor(4+Math.pow(towers,0.6)+Math.log((towers+(lvl-1)*10)/15+1)*15);266//old formula :267/*268M.magicM=8+Math.min(M.parent.amount,M.parent.level*5)+Math.ceil(M.parent.amount/10);269if (M.magicM>200)270{271//diminishing returns starting at 200, being 5% as fast by 400272var x=M.magicM;273var top=x-200;274top/=200;275var top2=top;276top*=(1-top/2);277if (top2>=1) top=0.5;278top=top*0.95+top2*0.05;279top*=200;280x=top+200;281M.magicM=x;282}283*/284M.magic=Math.min(M.magicM,M.magic);285}286287M.getFailChance=function(spell)288{289var failChance=0.15;290if (Game.hasBuff('Magic adept')) failChance*=0.1;291if (Game.hasBuff('Magic inept')) failChance*=5;292if (spell.failFunc) failChance=spell.failFunc(failChance);293return failChance;294}295296M.castSpell=function(spell,obj)297{298var obj=obj||{};299var out=0;300var cost=0;301var fail=false;302if (typeof obj.cost!=='undefined') cost=obj.cost; else cost=M.getSpellCost(spell);303if (M.magic<cost) return false;304var failChance=M.getFailChance(spell);305if (typeof obj.failChanceSet!=='undefined') failChance=obj.failChanceSet;306if (typeof obj.failChanceAdd!=='undefined') failChance+=obj.failChanceAdd;307if (typeof obj.failChanceMult!=='undefined') failChance*=obj.failChanceMult;308if (typeof obj.failChanceMax!=='undefined') failChance=Math.max(failChance,obj.failChanceMax);309Math.seedrandom(Game.seed+'/'+M.spellsCastTotal);310if (!spell.fail || Math.random()<(1-failChance)) {out=spell.win();} else {fail=true;out=spell.fail();}311Math.seedrandom();312if (out!=-1)313{314if (!spell.passthrough && !obj.passthrough)315{316M.spellsCast++;317M.spellsCastTotal++;318if (M.spellsCastTotal>=9) Game.Win('Bibbidi-bobbidi-boo');319if (M.spellsCastTotal>=99) Game.Win('I\'m the wiz');320if (M.spellsCastTotal>=999) Game.Win('A wizard is you');321}322323M.magic-=cost;324M.magic=Math.max(0,M.magic);325326var rect=l('grimoireSpell'+spell.id).getBoundingClientRect();327Game.SparkleAt((rect.left+rect.right)/2,(rect.top+rect.bottom)/2-24);328329if (fail) PlaySound('snd/spellFail.mp3',0.75); else PlaySound('snd/spell.mp3',0.75);330return true;331}332PlaySound('snd/spellFail.mp3',0.75);333return false;334}335336M.getSpellCost=function(spell)337{338var out=spell.costMin;339if (spell.costPercent) out+=M.magicM*spell.costPercent;340return Math.floor(out);341}342M.getSpellCostBreakdown=function(spell)343{344var str='';345if (spell.costPercent) str+=Beautify(spell.costMin)+' magic +'+Beautify(Math.ceil(spell.costPercent*100))+'% of max magic';346else str+=Beautify(spell.costMin)+' magic';347return str;348}349350M.spellTooltip=function(id)351{352return function(){353var me=M.spellsById[id];354me.icon=me.icon||[28,12];355var cost=Beautify(M.getSpellCost(me));356var costBreakdown=M.getSpellCostBreakdown(me);357if (cost!=costBreakdown) costBreakdown=' <small>('+costBreakdown+')</small>'; else costBreakdown='';358var backfire=M.getFailChance(me);359var str='<div style="padding:8px 4px;min-width:350px;">'+360'<div class="icon" style="float:left;margin-left:-8px;margin-top:-8px;background-position:'+(-me.icon[0]*48)+'px '+(-me.icon[1]*48)+'px;"></div>'+361'<div class="name">'+me.name+'</div>'+362'<div>Magic cost : <b style="color:#'+(cost<=M.magic?'6f6':'f66')+';">'+cost+'</b>'+costBreakdown+'</div>'+363(me.fail?('<div><small>Chance to backfire : <b style="color:#f66">'+Math.ceil(100*backfire)+'%</b></small></div>'):'')+364'<div class="line"></div><div class="description"><b>Effect :</b> '+me.desc+(me.failDesc?('<div style="height:8px;"></div><b>Backfire :</b> '+me.failDesc):'')+'</div></div>';365return str;366};367}368369var str='';370str+='<style>'+371'#grimoireBG{background:url(img/shadedBorders.png),url(img/grimoireBG.png);background-size:100% 100%,auto;position:absolute;left:0px;right:0px;top:0px;bottom:16px;}'+372'#grimoireContent{position:relative;box-sizing:border-box;padding:4px 24px;}'+373'#grimoireBar{max-width:95%;margin:4px auto;height:16px;}'+374'#grimoireBarFull{transform:scale(1,2);transform-origin:50% 0;height:50%;}'+375'#grimoireBarText{transform:scale(1,0.8);width:100%;position:absolute;left:0px;top:0px;text-align:center;color:#fff;text-shadow:-1px 1px #000,0px 0px 4px #000,0px 0px 6px #000;margin-top:2px;}'+376'#grimoireSpells{text-align:center;width:100%;padding:8px;box-sizing:border-box;}'+377'.grimoireIcon{pointer-events:none;margin:2px 6px 0px 6px;width:48px;height:48px;opacity:0.8;position:relative;}'+378'.grimoirePrice{pointer-events:none;}'+379'.grimoireSpell{box-shadow:4px 4px 4px #000;cursor:pointer;position:relative;color:#f33;opacity:0.8;text-shadow:0px 0px 4px #000,0px 0px 6px #000;font-weight:bold;font-size:12px;display:inline-block;width:60px;height:74px;background:url(img/spellBG.png);}'+380'.grimoireSpell.ready{color:rgba(255,255,255,0.8);opacity:1;}'+381'.grimoireSpell.ready:hover{color:#fff;}'+382'.grimoireSpell:hover{box-shadow:6px 6px 6px 2px #000;z-index:1000000001;top:-1px;}'+383'.grimoireSpell:active{top:1px;}'+384'.grimoireSpell.ready .grimoireIcon{opacity:1;}'+385'.grimoireSpell:hover{background-position:0px -74px;} .grimoireSpell:active{background-position:0px 74px;}'+386'.grimoireSpell:nth-child(4n+1){background-position:-60px 0px;} .grimoireSpell:nth-child(4n+1):hover{background-position:-60px -74px;} .grimoireSpell:nth-child(4n+1):active{background-position:-60px 74px;}'+387'.grimoireSpell:nth-child(4n+2){background-position:-120px 0px;} .grimoireSpell:nth-child(4n+2):hover{background-position:-120px -74px;} .grimoireSpell:nth-child(4n+2):active{background-position:-120px 74px;}'+388'.grimoireSpell:nth-child(4n+3){background-position:-180px 0px;} .grimoireSpell:nth-child(4n+3):hover{background-position:-180px -74px;} .grimoireSpell:nth-child(4n+3):active{background-position:-180px 74px;}'+389390'.grimoireSpell:hover .grimoireIcon{top:-1px;}'+391'.grimoireSpell.ready:hover .grimoireIcon{animation-name:bounce;animation-iteration-count:infinite;animation-duration:0.8s;}'+392393'#grimoireLumpRefill{cursor:pointer;width:48px;height:48px;position:absolute;left:-40px;top:-17px;transform:scale(0.5);z-index:1000;transition:transform 0.05s;}'+394'#grimoireLumpRefill:hover{transform:scale(1);}'+395'#grimoireLumpRefill:active{transform:scale(0.4);}'+396397'#grimoireInfo{text-align:center;font-size:11px;margin-top:12px;color:rgba(255,255,255,0.75);text-shadow:-1px 1px 0px #000;}'+398'</style>';399str+='<div id="grimoireBG"></div>';400str+='<div id="grimoireContent">';401str+='<div id="grimoireSpells">';//did you know adding class="shadowFilter" to this cancels the "z-index:1000000001" that displays the selected spell above the tooltip? stacking orders are silly https://philipwalton.com/articles/what-no-one-told-you-about-z-index/402for (var i in M.spells)403{404var me=M.spells[i];405var icon=me.icon||[28,12];406str+='<div class="grimoireSpell titleFont" id="grimoireSpell'+me.id+'" '+Game.getDynamicTooltip('Game.ObjectsById['+M.parent.id+'].minigame.spellTooltip('+me.id+')','this')+'><div class="usesIcon shadowFilter grimoireIcon" style="background-position:'+(-icon[0]*48)+'px '+(-icon[1]*48)+'px;"></div><div class="grimoirePrice" id="grimoirePrice'+me.id+'">-</div></div>';407}408str+='</div>';409var icon=[29,14];410str+='<div id="grimoireBar" class="smallFramed meterContainer"><div '+Game.getTooltip('<div style="padding:8px;width:300px;font-size:11px;text-align:center;">Click to refill <b>100 units</b> of your magic meter<br>for <span class="price lump">1 sugar lump</span>.</div>')+' id="grimoireLumpRefill" class="usesIcon shadowFilter" style="background-position:'+(-icon[0]*48)+'px '+(-icon[1]*48)+'px;"></div><div id="grimoireBarFull" class="meter filling"></div><div id="grimoireBarText" class="titleFont"></div><div '+Game.getTooltip('<div style="padding:8px;width:300px;font-size:11px;text-align:center;">This is your magic meter. Each spell costs magic to use.<div class="line"></div>Your maximum amount of magic varies depending on your amount of <b>Wizard towers</b>, and their level.<div class="line"></div>Magic refills over time. The lower your magic meter, the slower it refills.</div>')+' style="position:absolute;left:0px;top:0px;right:0px;bottom:0px;"></div></div>';411str+='<div id="grimoireInfo"></div>';412str+='</div>';413div.innerHTML=str;414M.magicBarL=l('grimoireBar');415M.magicBarFullL=l('grimoireBarFull');416M.magicBarTextL=l('grimoireBarText');417M.lumpRefill=l('grimoireLumpRefill');418M.infoL=l('grimoireInfo');419for (var i in M.spells)420{421var me=M.spells[i];422AddEvent(l('grimoireSpell'+me.id),'click',function(spell){return function(){PlaySound('snd/tick.mp3');M.castSpell(spell);}}(me));423}424425AddEvent(M.lumpRefill,'click',function(){426if (Game.lumps>=1 && M.magic<M.magicM)427{428M.magic+=100;429M.magic=Math.min(M.magic,M.magicM);430Game.lumps-=1;431PlaySound('snd/pop'+Math.floor(Math.random()*3+1)+'.mp3',0.75);432}433});434435M.computeMagicM();436M.magic=M.magicM;437M.spellsCast=0;438M.spellsCastTotal=0;439440//M.parent.switchMinigame(1);441}442M.save=function()443{444//output cannot use ",", ";" or "|"445var str=''+446parseFloat(M.magic)+' '+447parseInt(Math.floor(M.spellsCast))+' '+448parseInt(Math.floor(M.spellsCastTotal))449;450return str;451}452M.load=function(str)453{454//interpret str; called after .init455//note : not actually called in the Game's load; see "minigameSave" in main.js456if (!str) return false;457var i=0;458var spl=str.split(' ');459M.computeMagicM();460M.magic=parseFloat(spl[i++]||M.magicM);461M.spellsCast=parseInt(spl[i++]||0);462M.spellsCastTotal=parseInt(spl[i++]||0);463}464M.reset=function()465{466M.computeMagicM();467M.magic=M.magicM;468M.spellsCast=0;469}470M.logic=function()471{472//run each frame473if (Game.T%5==0) {M.computeMagicM();}474M.magicPS=Math.max(0.002,Math.pow(M.magic/Math.max(M.magicM,100),0.5))*0.002;475M.magic+=M.magicPS;476M.magic=Math.min(M.magic,M.magicM);477if (Game.T%5==0)478{479for (var i in M.spells)480{481var me=M.spells[i];482var cost=M.getSpellCost(me);483l('grimoirePrice'+me.id).innerHTML=Beautify(cost);484if (M.magic<cost) l('grimoireSpell'+me.id).className='grimoireSpell titleFont';485else l('grimoireSpell'+me.id).className='grimoireSpell titleFont ready';486}487}488}489M.draw=function()490{491//run each draw frame492M.magicBarTextL.innerHTML=Math.min(Math.floor(M.magicM),Beautify(M.magic))+'/'+Beautify(Math.floor(M.magicM))+(M.magic<M.magicM?(' (+'+Beautify((M.magicPS||0)*Game.fps,2)+'/s)'):'');493M.magicBarFullL.style.width=((M.magic/M.magicM)*100)+'%';494M.magicBarL.style.width=(M.magicM*3)+'px';495M.infoL.innerHTML='Spells cast : '+Beautify(M.spellsCast)+' (total : '+Beautify(M.spellsCastTotal)+')';496}497M.init(l('rowSpecial'+M.parent.id));498}499var M=0;500501502