e.creatId===t.id)&&(a.state.gs.deathOrder=a.state.gs.deathOrder.filter(e=>e.creatId!==t.id))}findDeathTime(t){const i=a.state.gs.deathOrder.filter(e=>e.creatId===t).map(e=>e.time);if(i.length!==0)return Math.min(...i)}destroy(t,i,e=void 0){t.status==="A"?(a.log.new().crd(t.code).txt(" was destroyed by ").crd(i).txt(".").go(),this.smite(t,e)):(console.warn(`Attempted to destroy creature that was not active. Targ creat ${t.code}; Killer creat ${i}`),console.trace())}gravedigger(t="X"){if(t==="X"){const i=a.state.gs.graveyard.length;return i===0?"":a.state.gs.graveyard[a.randInt(0,i)].cardCode}else{const i=a.player.toBool(t),e=a.state.gs.graveyard.filter(l=>l.owner===i),s=e.length;return s===0?"":e[a.randInt(0,s)].cardCode}}smite(t,i=void 0){t.status="D",a.state.gs.deathOrder.push({creatId:t.id,time:i||Date.now()}),a.state.gs.graveyard.push({cardCode:t.code,owner:t.owner}),a.batch.addEvents(d.creatureEvent("DIE",t)),a.effect.positionChange(t.owner),a.state.gs.turn.creaturesKilled+=1}}class Ar{startBatch(){this.deathCheck=!1,this.type==="C0"?this.type="C1":this.type==="C1"?this.type="C2":this.type==="C2"&&(this.type="N0");const t=this.type;let i;if(t==="N0")i=a.state.gs.batch.events.slice(),a.state.gs.batch.events=[];else{const s=a.state.gs.batch.events.filter(u=>u.code==="DIE"),l=a.state.gs.batch.events.filter(u=>u.code!=="DIE");t==="C1"?(i=l,a.state.gs.batch.events=s):(i=s,a.state.gs.batch.events=l)}const e=this.generateTriggers(i);a.state.gs.batch.triggerActions=this.sortTriggers(e),a.phase.whatNext()}generateTriggers(t){let i;const e=t.map(s=>{s.code==="TRS"&&(i=s);const l=a.state.gs.field.map(p=>p.triggers.map(y=>{const A=d.triggerReaction(s,y,p.owner,"C");return A.creatId=p.id,A.cardCode=p.code,A}).filter(y=>a.trigger.ddf8(y))).flat(),u=[!0,!1].map(p=>a.player.toPlayer(p).triggers.map(A=>d.triggerReaction(s,A,p,"P")).filter(A=>a.trigger.ddf8(A))).flat();return l.concat(u)}).flat();return i&&this.handleTurnStart(i),e}sortTriggers(t){const i=a.state.ddf9(),e=t.filter(u=>!u.trigger.interactive&&u.ownerPlayerA===i),s=t.filter(u=>!u.trigger.interactive&&u.ownerPlayerA!==i);return t.filter(u=>u.trigger.interactive&&u.ownerPlayerA===i).concat(s,e)}handleTurnStart(t){const i=a.state.ddf9();a.field.qPlayerCreats(i).filter(s=>s.sleeping).forEach(s=>{s.sleeping=!1});const e=Date.now();a.field.qActiveCreats().forEach(s=>{s.owner===t.playerA&&(s.activationAvailable=!0,s.mobilityAvailable=!0,s.sleeping=!1),s.armorDmg=0,s.poison>0&&s.hurtDirect(s.poison,"POIS",e),s.regen>0&&s.heal(s.regen),a.death.deathProdder(s,e)})}getNextTrigger(){const t=a.state.gs.batch.triggerActions;if(t.length!==0)return t[t.length-1]}triggerDone(){const t=a.state.gs.batch.triggerActions;t.length===0?console.error("batch.triggerDone() was called when there is no trigger actions"):(t.pop(),a.state.gs.batch.iatLane=0),a.phase.whatNext()}addEvents(...t){a.state.gs.batch.events.push(...t)}get deathCheck(){return a.state.gs.batch.deathCheck}set deathCheck(t){a.state.gs.batch.deathCheck=t}get type(){return a.state.gs.batch.type}set type(t){a.state.gs.batch.type=t}get iaTrigLane(){return a.state.gs.batch.iatLane}}class kr{chirpy=!1;chirp(t){}enterMainPhase(){a.state.setPhase("MN");const t=a.state.getActivePlayer(),i=this.getMainPhaseActions(t);a.action.ddf5(i)}passTheTurn(){a.state.gs.activePlayer=!a.state.gs.activePlayer,a.state.setPhase("WT"),a.state.gs.aiMode&&(a.state.gs.aiActive=!a.state.gs.aiActive,this.startTurn())}startTurn(){a.log.new().txt("------").go(),a.log.new().plr(a.state.ddf9()).txt(" starts turn.").go(),a.state.resetTurnData(),a.state.setPhase("ST"),a.batch.addEvents(d.playerEvent("TRS",a.state.ddf9())),this.expireMods(),this.whatNext()}async whatNext(){if(a.state.gs.winFlag!==0){this.chirp("NEXT! found win state"),a.action.actionHistory=[],a.state.gs.aiActive=!1,a.state.gs.ui.animating=!1,await a.drawing.drawAll();return}if(await a.drawing.drawAll(),a.action.actionHistory.length>0)this.chirp("NEXT! action restore"),a.action.ddf5();else if(a.batch.getNextTrigger())this.chirp("NEXT! next trigger"),a.trigger.resolve(a.batch.getNextTrigger());else if(!a.batch.deathCheck)this.chirp("NEXT! death check"),a.death.deathCheck(),this.whatNext();else if(a.state.gs.batch.events.length>0)this.chirp("NEXT! batch start"),a.batch.startBatch();else switch(this.chirp("NEXT! enter phase handler "+a.state.gs.phase),a.state.gs.field=a.state.gs.field.filter(t=>t.status==="A"),a.state.gs.deathOrder=[],a.state.gs.birthOrder=[],a.state.gs.phase){case "MN":this.enterMainPhase();break;case "ET":a.state.setPhase("PT"),a.batch.addEvents(d.playerEvent("PTN",a.state.ddf9())),a.phase.expireMods(),this.whatNext();break;case "PT":this.passTheTurn();break;case "WT":console.error("Hit unhandled waiting phase"),a.state.setPhase("MN"),this.whatNext();break;case "ST":a.state.setPhase("MN"),a.ai.cheater()?a.sleep(500).then(()=>this.whatNext()):this.whatNext();break}}expireMods(){const t=a.state.gs.phase,i=a.field.creatByTarget(d.crTarg());let e=[];t==="PT"?i.forEach(s=>{s.mods.forEach(l=>{l.expiry==="EN"?l.expiry="ET":l.expiry==="ET"&&e.push([s,l])})}):t==="ST"&&i.forEach(s=>{s.mods.forEach(l=>{l.expiry==="SN"?l.expiry="ST":l.expiry==="ST"&&e.push([s,l])})}),e.length>0&&(e.forEach(s=>s[0].expireMod(s[1])),a.effect.enforceAllNonPositionCreatureEffects())}getMainPhaseActions(t){const i=this.getHandActions(t),e=this.getFieldActions(t);let s=!a.state.gs.turn.mainBattle,l="M";return s||(s=a.field.ddf1().some(p=>p.extraBattles>0&&p.isActive&&!p.defender),l="E"),s&&i.push({actionKey:"FITE",zone:"B",param:l,hostCard:""}),s||i.push({actionKey:"ENDT",zone:"B",param:"",hostCard:""}),[i,e].flat()}getHandActions(t){const i=[...new Set(t.hand)],e=[],s=[];i.forEach(y=>{a.cards.getCard(y).isFree?e.push(y):s.push(y)});const l=e.map(y=>[y,"FREE"]);a.state.gs.turn.freeCards.forEach(y=>{a.target.cardByTarget(s,y.condition).forEach(T=>{l.push([T[0],y.id]),s.splice(s.indexOf(T[0]),1)})}),l.push(...s.map(y=>[y,""]));const u=a.state.gs.turn.regularPlays>0;let p=t.hand.map((y,A)=>[y,A]);return u||(p=p.filter(y=>!s.includes(y[0]))),p.map(y=>{const A=l.find(T=>T[0]===y[0]);return A||console.error("No cost found for card"+y[0]),d.handSelectAction("HPLY",y[0],A[1],y[1])})}getFieldActions(t){const i=a.field.qPlayerCreats(t.isPlayerA),e=i.filter(l=>a.field.canMove(l).length>0).map(l=>d.fieldSelectAction("MOVE",l.lane,l.owner)),s=i.filter(l=>l.isActive&&l.activationAvailable?a.cards.getCard(l.code).canActivate(l):!1).map(l=>{const u=d.fieldSelectAction("ACTV",l.lane,l.owner);return u.hostCard=l.code,u.param=l.id,u});return e.forEach(l=>{const u=s.findIndex(p=>p.lane===l.lane);u>-1&&(s.splice(u,1),l.param="ACTV")}),e.concat(s)}}var z=(o=>(o[o.Left=0]="Left",o[o.Both=1]="Both",o[o.Right=2]="Right",o))(z||{});class ge{action;hitTest(t,i,e){}enabled=!0;mouse=0;validButton(t){return this.mouse===1||this.mouse===t}static arrayHit(t,i,e,s){let l,u=0;const p=t.length;for(;l===void 0&&u=t&&this.originY<=i&&this.endY>=i?this.action:void 0}}class vt extends U{constructor(t,i,e,s,l=0){super({zone:"N",actionKey:"BNDL",hostCard:"",param:""},t,i,e,s,l)}childs=[];addChild(t){this.childs.push(t)}clear(){this.childs=[]}hitTest(t,i,e){if(super.hitTest(t,i,e))return ge.arrayHit(this.childs,t,i,e)}}class Tr{registry;constructor(){this.registry=[]}find(t){const i=this.registry.find(e=>e.action===t);if(i)return i;throw console.trace(),new Error("Attempted to find non-existing clicker")}entoggle(t,i){const e=this.find(t);e.enabled=i}add(t){this.registry.push(t)}get(t){return this.find(t)}remove(t){const i=this.registry.findIndex(e=>e.action===t);i>-1&&this.registry.splice(i,1)}removeAll(){this.registry=[]}enable(t){this.entoggle(t,!0)}disable(t){this.entoggle(t,!1)}hitTest(t,i,e){if(a.state.gs.ui.animating)return;let s,l;this.scaleFactor===1?(s=t,l=i):(s=Math.round(t*this.scaleFactor),l=Math.round(i*this.scaleFactor));let u,p=a.state.clickMode;return p==="M"?u=e:p==="T"?u=0:u=2,ge.arrayHit(this.registry,s,l,u)}updateScale(t){t===1?this.scaleFactor=1:this.scaleFactor=1/t}scaleFactor=1}class Cr{forgeCard(t,i){const e=this.cardPlayed(i),s=a.state.getActivePlayer();a.log.new().txt("Forged ").crd(e.code).txt(".").go(),a.field.birthCreature(e,"F",s.isPlayerA,t.lane),a.action.actionDone()}levelAndDiscard(t){const i=a.cards.getCard(t.card),e=a.state.getActivePlayer();return e.hand.splice(t.position,1),i.overload||e.discard.push(i.levelUpCard()),i}cardPlayed(t,i=!1){const e=this.levelAndDiscard(t),s=a.state.getActivePlayer();if(t.cost==="")a.state.gs.turn.regularPlays-=1;else if(t.cost!=="FREE"){const l=a.state.gs.turn.freeCards.findIndex(u=>u.id===t.cost);l>-1?a.state.gs.turn.freeCards.splice(l,1):console.error("Could not find free card rule!")}if(a.state.gs.turn.cardsPlayed+=1,!i){const l=d.playCardEvent(t.card,s.isPlayerA);a.batch.addEvents(l)}return e}spellCast(){this.cardPlayed(a.action.cardBeingPlayed),a.action.actionDone()}endTurn(){const t=a.state.getActivePlayer();t.discard.push(...t.hand),t.hand=[],t.turn++;const i=t.turn>4;a.batch.addEvents(d.playerEvent("TRE",t.isPlayerA)),i&&(t.turn=1,t.rank++,a.batch.addEvents(d.playerEvent("RKU",t.isPlayerA)),t.deck.push(...t.discard),t.discard=[],a.pack.shuffleDeck(t.isPlayerA)),a.field.qActiveCreats().forEach(e=>{e.extraBattles=0}),a.player.drawCard(t,5),a.state.setPhase("ET"),a.action.actionDone()}mobility(t){const i=a.field.jr5(t.param);i.mobilityAvailable=!1,i.move(t.lane),a.action.actionDone()}allied(t,i){const e=i??a.state.getActivePlayer(),s=d.target("TH");return s.player=a.player.toTarget(e),s.faction=t,a.target.deckByTarget(s).length>0}freeCard(t){const i=d.freeCard(t);return a.state.gs.turn.freeCards.push(i),i}}class Sr{laneIdx=[1,2,3,4,5];jr5(t){const i=this.searchById(t);if(i)return i;throw console.error("naughty creature id",t),new Error("Could not find creature by id")}creatByLane(t,i){const e=this.searchByLane(t,i);if(e)return e;throw console.error("naughty creature lane ",t,i),new Error("Could not find creature by lane")}ddf6(t){const i=this.searchByFSA(t);if(i)return i;throw console.error("naughty FSA",t),new Error("Could not find creature by FSA")}searchById(t){return a.state.gs.field.find(i=>i.id===t)}searchByLane(t,i){return a.state.gs.field.find(e=>e.lane===t&&e.owner===i&&e.status==="A")}searchByFSA(t){return this.searchByLane(t.lane,t.playerA)}creatByTarget(t){let i=a.state.gs.field;if(t){if(t.player!=="X"){const e=a.player.toBool(t.player);i=i.filter(s=>s.owner===e)}if(t.creatStatus!=="A"){const e=this.creatStatusTargetToCS(t.creatStatus);i=i.filter(s=>s.status===e)}if(!a.isUn(t.lanes)){const e=a.target.lanesToArr(t.lanes);i=i.filter(s=>e.includes(s.lane))}if(a.isUn(t.maxAttack)||(i=i.filter(e=>e.attack<=t.maxAttack)),t.notMe&&(i=i.filter(e=>e.id!==t.notMe)),t.victims){const e=a.deTil(t.victims);i=i.filter(s=>e.includes(s.id))}if(t.tribe!=="AN"&&(i=i.filter(e=>e.isTribe(t.tribe))),t.canLevel&&(i=i.filter(e=>a.cards.getCard(e.code).canLevel())),t.level!=="LA"){const e=a.target.levelTargetToLevel(t.level);i=i.filter(s=>e.includes(s.level))}t.cardCode&&(i=i.filter(e=>a.cards.getCard(e.code).cardCode()===t.cardCode)),t.canDefender&&(i=i.filter(e=>!e.negateDefender)),t.hasDefender&&(i=i.filter(e=>e.defender)),t.canArmor&&(i=i.filter(e=>!e.negateArmor)),t.hasArmor&&(i=i.filter(e=>e.armor>0)),t.canMove&&(i=i.filter(e=>!e.negateMobility)),t.hasMobility&&(i=i.filter(e=>e.mobility>0)),t.hasPoison&&(i=i.filter(e=>e.poison>0)),t.canRegen&&(i=i.filter(e=>!e.negateRegen)),t.hasRegen&&(i=i.filter(e=>e.regen>0)),t.faction!=="X"&&(i=i.filter(e=>e.faction===t.faction))}return i}cst2cs={["A"]:()=>{throw new Error("goof passed any to field state target mapper")},["V"]:()=>"A",["D"]:()=>"D",["R"]:()=>"R"};creatStatusTargetToCS(t){return this.cst2cs[t]()}emptyLanes(t){return this.laneIdx.filter(i=>!a.state.gs.field.find(s=>s.owner===t&&s.lane===i&&s.status==="A"))}oppositeLane(t){return 6-t}adjacentPositions(t){return[[2],[1,3],[2,4],[3,5],[4]][t-1]}canMove(t){return t.mobility>0&&t.mobilityAvailable&&(!t.sleeping||t.aggressive)?this.emptyLanes(t.owner).filter(i=>Math.abs(t.lane-i)<=t.mobility):[]}birthCreature(t,i,e,s=0,l=void 0){if(i==="S"){const T=this.emptyLanes(e);if(T.length===0)return;s=T[a.randInt(0,T.length)]}else if(i==="P"&&!this.emptyLanes(e).includes(s))return;const p=(typeof t=="string"?a.cards.getCard(t):t).jr3(e,s),y=a.state.gs.field;if(i==="F"||i==="R"){const T=a.field.searchByLane(s,e);T&&(a.log.new().crd(T.code).txt(" was replaced.").go(),T.status="R",a.batch.addEvents(d.replacedEvent(p,T)))}y.push(p);const A=d.enterFieldEvent(p);return A.forged=i==="F",a.state.gs.birthOrder.push({creatId:p.id,time:l||Date.now()}),a.batch.addEvents(A),p.enforceEffects("PH"),a.effect.positionChange(e),p}findBirthTime(t){const i=a.state.gs.birthOrder.filter(e=>e.creatId===t).map(e=>e.time);if(i.length!==0)return i.length>1&&console.warn("wery curious, more than one birth record for creat "+t),i[0]}qActiveCreats(){return this.creatByTarget(d.crTarg())}qPlayerCreats(t){const i=d.crTarg();return i.player=a.player.toTarget(t),this.creatByTarget(i)}ddf1(){return this.creatByTarget(d.crFriendTarg())}qEnemyCreats(){return this.creatByTarget(d.crEnemyTarg())}}class wr{creatFightState(t,i){return t?i===1||t.defender||t.sleeping&&!t.aggressive?1:i===0?2:t.extraBattles>0?(t.extraBattles-=1,2):1:0}dukeItOut(t,i,e,s){if(t>0){let l=0;if(i>0){const u=t===2&&e.breakthrough;l=s.hurtBattle(e.attack,e,u)}else t===2&&(l=e.attack);l>0&&a.player.hurtPlayerBattle(!e.owner,l,e)}}fight(t){a.log.add("FIGHT!");let i=2,e=1;t&&(a.state.gs.turn.mainBattle=!0,i=0,e=0);const s=a.state.ddf9(),l=[],u=[];a.field.laneIdx.forEach(p=>{const y=a.field.searchByLane(p,s),A=a.field.searchByLane(6-p,!s),T=this.creatFightState(y,i),D=this.creatFightState(A,e);(T===2||D===2)&&(u.push([T,D,y,A],[D,T,A,y]),T===2&&(a.state.gs.turn.friendlyFightStarters++,l.push([p,s])),D===2&&l.push([a.field.oppositeLane(p),!s]))}),this.barking&&l.length>0&&a.dogBark(),a.drawing.drawCombat(l).then(()=>{u.forEach(p=>this.dukeItOut(...p)),a.effect.playerHealthChange(),a.death.deathCheck(),a.batch.type="C0",a.action.actionDone()})}barking=!0}class Er{positionChange(t){a.field.creatByTarget().forEach(i=>{i.enforceEffects("PS",t)})}defenderChange(t){a.field.qActiveCreats().forEach(e=>{e.effects.filter(s=>s.watch==="DF").filter(s=>s.area==="C"&&e.owner===t||s.area==="A").forEach(s=>a.cards.getCard(s.logicCode).enforceEffect(s,e))})}playerHealthChange(){a.field.qActiveCreats().forEach(t=>{t.enforceEffects("PH")})}enforceAllNonPositionCreatureEffects(){a.field.qActiveCreats().forEach(i=>{i.enforceEffects("AT"),i.enforceEffects("AR"),i.enforceEffects("CH"),i.enforceEffects("DF",i.owner)})}}class Dr{}class xr{pt2pb={["X"]:()=>{throw new Error("goof passed any to player target boolean-o-fier")},["A"]:()=>!0,["B"]:()=>!1,["F"]:()=>a.state.ddf9(),["E"]:()=>!a.state.ddf9()};toPlayer(t){return typeof t=="boolean"?a.state.getPlayer(t):typeof t=="string"?a.state.getPlayer(this.pt2pb[t]()):t}toBool(t){return typeof t=="boolean"?t:typeof t=="string"?this.pt2pb[t]():t.isPlayerA}toTarget(t){return typeof t=="string"?t:(typeof t=="boolean"?t:t.isPlayerA)?"A":"B"}hurtPlayer(t,i){return i<1?0:(t.health-=i,i)}ddf3(t,i,e){const s=this.toPlayer(t),l=this.hurtPlayer(s,i);l>0&&(a.log.new().crd(e).txt(` dealt ${l} dmg to `).plr(s).txt(".").go(),a.batch.addEvents(d.damageEvent(!1,l,!1,!1,s.isPlayerA,void 0)),a.effect.playerHealthChange())}hurtPlayerBattle(t,i,e){const s=this.toPlayer(t),l=this.hurtPlayer(s,i);l>0&&(a.log.new().crd(e.code).txt(` dealt ${l} battle dmg to `).plr(s).txt(".").go(),a.batch.addEvents(d.damageEvent(!0,l,!0,!1,s.isPlayerA,e)))}healPlayer(t,i){const e=this.toPlayer(t);e.health+=i,a.log.new().plr(e).txt(` gained ${i} health.`).go(),a.batch.addEvents(d.playerHealEvent("PHL",e.isPlayerA,i)),a.effect.playerHealthChange()}drawCard(t,i){const e=this.toPlayer(t);for(let s=0;s{const s=this.actionTargGuts(e);return s.forEach(l=>{l.hostCard=e.hostCard,l.param=e.param}),s}).flat()}actionTargGuts(t){if(t.target==="TH")return this.deckByTarget(t).map(i=>d.handSelectAction(t.actionKey,i[0],"",i[1]));if(t.target==="TF"){if(t.field==="O")return a.field.creatByTarget(t).map(i=>d.fieldSelectAction(t.actionKey,i.lane,i.owner));{const i=t.player==="X"?[!0,!1]:[a.player.toBool(t.player)],e=a.isUn(t.lanes)?a.field.laneIdx:this.lanesToArr(t.lanes);if(t.field==="A")return i.map(s=>e.map(l=>d.fieldSelectAction(t.actionKey,l,s))).flat();if(t.field==="E")return i.map(s=>a.field.emptyLanes(s).filter(l=>e.includes(l)).map(l=>d.fieldSelectAction(t.actionKey,l,s))).flat()}}if(t.target==="TP")return t.player==="X"?[d.playerSelectAction(t.actionKey,!0),d.playerSelectAction(t.actionKey,!1)]:[d.playerSelectAction(t.actionKey,a.player.toBool(t.player))];if(t.target==="TB")return[{actionKey:t.actionKey,zone:"B",hostCard:t.hostCard,param:""}];if(t.target==="TR")return[{actionKey:t.actionKey,zone:"R",hostCard:t.hostCard,param:""}];if(t.target==="TD")return[];throw new Error("Target encountered that doesnt have mappings to actions: "+t.target)}cardByTarget(t,i){let e=t.map((s,l)=>[s,l]);if(i){if(a.isUn(i.deckPositionSkip)||(e=e.filter(s=>s[1]!==i.deckPositionSkip)),i.faction!=="X"&&(e=e.filter(s=>a.cards.getCard(s[0]).faction===i.faction)),i.tribe!=="AN"&&(e=e.filter(s=>{const l=a.cards.getCard(s[0]);return l.type==="C"?l.isTribe(i.tribe):!1})),i.canLevel&&(e=e.filter(s=>a.cards.getCard(s[0]).canLevel())),i.level!=="LA"){const s=a.target.levelTargetToLevel(i.level);e=e.filter(l=>{const u=parseInt(l[0].substring(3,4));return s.includes(u)})}if(i.card!=="A"){const s=i.card==="C"?"C":"S";e=e.filter(l=>a.cards.getCard(l[0]).type===s)}i.cardCode&&(e=e.filter(s=>a.cards.getCard(s[0]).cardCode()===i.cardCode))}return e}deckByTarget(t){const i=a.player.toPlayer(t.player);let e;if(t.target==="TH")e=i.hand;else if(t.target==="TD")e=i.discard;else if(t.target==="DK")e=i.deck;else throw new Error("bad target passed to deckByTarget: "+t.target);return this.cardByTarget(e,t)}buttonTarget(t,i="",e=""){return d.actionTarget("TB",t,i,e)}boardTarget(t,i){return d.actionTarget("TR",t,i,"")}ddf2(t){return t===1?"L1":t===2?"LN":"LA"}lt2l={["LA"]:()=>[1,2,3,4],["L1"]:()=>[1],["L2"]:()=>[2],["L3"]:()=>[3],["LN"]:()=>[1,2]};levelTargetToLevel(t){return this.lt2l[t]()}levelTargetTest(t,i){return this.levelTargetToLevel(t).includes(i)}levelTestCommon(t,i){return this.levelTargetTest(this.ddf2(i),t)}lanesToArr(t){return Array.isArray(t)?t:[t]}}class Pr{handleAction(t){if(t.actionKey==="DEET"){a.details.init(t.param);return}if(t.actionKey==="HPLY"){this.playCardSelect(t);return}if(t.actionKey==="FORG"){a.turn.forgeCard(t,this.cardBeingPlayed);return}if(t.actionKey==="FITE"){a.fight.fight(t.param==="M");return}if(t.actionKey==="ENDT"){a.turn.endTurn();return}if(t.actionKey==="MOVE"){this.mobilitySelect(t);return}if(t.actionKey==="MEND"){a.turn.mobility(t);return}if(t.actionKey==="HDSC"){const i=this.cardBeingPlayed;i.cost="",a.turn.cardPlayed(i,!0),this.actionDone();return}if(t.actionKey==="ACTV"){const i=a.field.jr5(t.param);a.cards.getCard(i.code).startActivation(i);return}if(t.actionKey==="NOTR"){t.param==="PM"&&a.mode.popMode(),this.actionDone(!0);return}if(t.actionKey==="CARD"){a.cards.getCard(t.hostCard).handleAction(t);return}if(t.actionKey==="CANC"||t.actionKey==="CNCL"){t.param==="PM"&&a.mode.popMode(),this.actionHistory.pop(),this.ddf5();return}if(t.actionKey==="DTUP"){a.details.updateDetails(t),a.drawing.drawAll();return}if(t.actionKey==="VWDK"){a.deck.handleShowDeck(t);return}if(t.actionKey==="SCRK"){a.deck.handleDeckScroll(t);return}if(t.actionKey==="SCRH"){t.param==="R"?a.state.gs.ui.handScrollLeft+=1:a.state.gs.ui.handScrollLeft-=1,a.drawing.drawAll();return}if(t.actionKey==="DRFT"){a.draft.resumeDraft(t.param);return}if(t.actionKey==="SCRD"){t.param==="U"?a.draft.paginating=!1:a.draft.paginating=!0,a.drawing.drawAll();return}if(t.actionKey==="UIBT"){a.menu.handleUIButton(t);return}if(t.actionKey==="MENU"){a.menu.handleMainMenuButton(t);return}if(t.actionKey==="MNBT"){a.menu.handleMenuButton(t);return}if(t.actionKey==="DSET"){this.ddf5(a.menu.generateDeckPickerActions(parseInt(t.param)));return}if(t.actionKey==="DECK"){a.menu.handleDeckPick(t);return}}ddf5(t){let i;if(t){const e=this.generateDetailActions();i=t.concat(e),this.actionHistory.push(i)}else i=this.actionHistory[this.actionHistory.length-1];a.state.gs.aiActive?a.ai.processAction(i):a.drawing.drawAll()}currPlayHandAction;get cardBeingPlayed(){if(this.currPlayHandAction)return this.currPlayHandAction;throw new Error("Attempted to read current card being played, nothing exists")}actionHistory=[];get activeActions(){const t=this.actionHistory.length;return t===0?[]:this.actionHistory[t-1]}actionDone(t=!1){this.currPlayHandAction=void 0,this.actionHistory=[],a.clickers.removeAll(),a.drawing.drawActions([]),t?a.batch.triggerDone():a.phase.whatNext()}playCardSelect(t){this.currPlayHandAction=t;const i=this.getPlayCardActions(t);this.ddf5(i)}getPlayCardActions(t){if(t.actionKey!=="HPLY")throw new Error("Unexpected action passed to getPlayCardActions: "+t.actionKey);const e=a.cards.getCard(t.card).playTargets();a.state.getRegularPlays()>0&&e.push(a.target.buttonTarget("HDSC",t.card));const s=a.target.toActions(e);return s.push(d.handSelectAction("CANC",t.card,"",t.position)),s}mobilitySelect(t){const i=this.getMoveCardActions(t);this.ddf5(i)}getMoveCardActions(t){if(t.actionKey!=="MOVE")throw new Error("Unexpected action passed to getMoveCardActions: "+t.actionKey);const i=a.field.ddf6(t);let e=[];if(t.param==="ACTV"){const s=d.actionTarget("TB","ACTV",i.code,i.id);e=a.target.toActions(s)}return e.push(d.fieldSelectAction("CANC",t.lane,t.playerA)),a.field.canMove(i).map(s=>{const l=d.fieldSelectAction("MEND",s,t.playerA);return l.param=i.id,l}).concat(e)}noneEndTrigger(){return a.target.toActions(a.target.buttonTarget("NOTR"))[0]}cancelAction(){return a.target.toActions(a.target.buttonTarget("CANC"))[0]}emptyAdjacents(t,i){const e=i||t.id,s=d.emptyPlayerActTarg("F","CARD",t.code,e);return s.lanes=t.adjacent,a.target.toActions(s)}generateDetailActions(){const t=a.mode.mode;if(a.state.gs.aiActive)return[];if(t==="G"){const i=a.state.getLocalPlayer().hand.map((s,l)=>{const u=d.handSelectAction("DEET",s,"",l);return u.param=s,u}),e=a.field.qActiveCreats().map(s=>{const l=d.fieldSelectAction("DEET",s.lane,s.owner);return l.param=s.code,l});return i.concat(e)}else if(t==="R"){const i=a.draft.currentPack.map((s,l)=>{const u=d.draftSelectAction("DEET",l,!0);return u.param=`${s}1`,u}),e=a.draft.picksAsDeck().map((s,l)=>{const u=d.draftSelectAction("DEET",l,!1);return u.param=`${s.card}1`,u});return i.concat(e)}return[]}}class Br{_state=new Ue;get gs(){return this._state}resetTurnData(){this.gs.turn=d.freshTurn(),this.gs.firstTurn&&(this.gs.firstTurn=!1,this.gs.turn.regularPlays=1)}getPlayer(t){return t?this._state.playerA:this._state.playerB}getActivePlayer(){return this.getPlayer(this._state.activePlayer)}ddf9(){return this._state.activePlayer}getLocalPlayer(){return this.getPlayer(this._state.isLocalA)}setPhase(t){this.gs.phase=t}getRegularPlays(){return this.gs.turn.regularPlays}getCardsPlayed(){return this.gs.turn.cardsPlayed}writeStorage(t,i){localStorage.setItem(t,i)}readStorage(t){return localStorage.getItem(t)}strRead(t,i){const e=this.readStorage(t);return e===null?i:e}boolRead(t,i){const e=this.readStorage(t);return e===null?i:e==="Y"}boolWrite(t,i){this.writeStorage(t,i?"Y":"N")}restoreOptions(){const t=this.gs.options;t.bark=this.boolRead("bark",!0),t.bigCards=this.boolRead("bigCards",!0),t.cheater=this.boolRead("cheater",!1),t.highHealth=this.boolRead("highHealth",!1),t.fatPacks=this.boolRead("fatPacks",!1),t.legDraft=this.boolRead("legDraft",!1),t.scale=this.strRead("scale","100")}toggleOption(t){this.gs.options[t]=!this.gs.options[t],this.boolWrite(t,this.gs.options[t])}setClickMode(t){this.gs.clickMode=t}get clickMode(){return this.gs.clickMode}}class Nr{friends;friendText;get browseStack(){return a.state.gs.ui.detailStack}n(t,i){return`${t}${i}`}allLevelFriendo(t,i){const e=Array.isArray(i)?i:[i];[1,2,3].forEach(s=>{this.friends[this.n(t,s)]=e.map(l=>this.n(l,s))})}plont(t){this.friends[this.n(t,1)]=[this.n("SDL",1)],this.friends[this.n(t,2)]=[this.n("SPL",2)],this.friends[this.n(t,3)]=[this.n("TFK",3)]}constructor(){const t=this.n("FWC",1);this.friends={[this.n("CGL",1)]:[this.n("CGF",1)],[this.n("DTD",1)]:[this.n("TOT",1)],[t]:[this.n("FWG",1)],[this.n("MPH",1)]:[t],[this.n("MPH",2)]:[t],[this.n("MPH",3)]:[t],[this.n("NGQ",4)]:[this.n("NSD",4)],[this.n("RHU",1)]:[this.n("RSC",1)],[this.n("SSE",1)]:[this.n("SXD",1)],[this.n("SSE",2)]:[this.n("SXP",2)],[this.n("SSE",3)]:[this.n("SXT",3)],[this.n("TGK",1)]:[this.n("SXD",1)],[this.n("TGK",2)]:[this.n("SPL",2)],[this.n("TGK",3)]:[this.n("TTF",3)],[this.n("TSL",1)]:[this.n("FUN",1)],[this.n("ZTU",2)]:[this.n("ZTR",2)]},this.allLevelFriendo("BLH","LWY"),this.allLevelFriendo("BTM","BOS"),this.allLevelFriendo("BWD","TFD"),this.plont("BTS"),this.allLevelFriendo("BWT","DNK"),this.allLevelFriendo("CSB","ZBS"),this.allLevelFriendo("CYS","COZ"),this.allLevelFriendo("DSK","SPN"),this.allLevelFriendo("DNK","BWT"),this.allLevelFriendo("DZD","DZA"),this.allLevelFriendo("DBQ","BRF"),this.allLevelFriendo("EPH","EPS"),this.allLevelFriendo("FST","ZST"),this.allLevelFriendo("FWK","ZMB"),this.allLevelFriendo("FGD",["FGA","FGB","FGG","FGO"]),this.allLevelFriendo("FGG","FGO"),this.allLevelFriendo("FMD","FME"),this.plont("GMR"),this.allLevelFriendo("IZL",["IFL","IFR","IZR"]),this.allLevelFriendo("IZR",["IFL","IFR","IZL"]),this.allLevelFriendo("IZK",["IFL","IFR","IZL","IZR"]),this.allLevelFriendo("NSW","SPN"),this.allLevelFriendo("NSH","NDR"),this.allLevelFriendo("NGQ","NSD"),this.allLevelFriendo("NFF","TFF"),this.allLevelFriendo("NOZ","OOZ"),this.plont("PTB"),this.allLevelFriendo("RWC","RPT"),this.plont("STS"),this.allLevelFriendo("SND","SPU"),this.allLevelFriendo("TNL","ZMB"),this.plont("WWS"),this.allLevelFriendo("YNS","XYR"),this.friendText={["BOS"]:[51,""],["BWT"]:[68,"Bron"],["BRF"]:[18,""],["CGF"]:[65,"Fiend"],["COZ"]:[31,""],["DNK"]:[8,""],["DZA"]:[59,"Dozer"],["EPS"]:[49,"Soldier"],["FWG"]:[44,""],["FWC"]:[28,"Chrysalis"],["FGA"]:[65,"Alpha"],["FGB"]:[77,"Beta"],["FGG"]:[53,"Gamma"],["FGO"]:[57,"Omega"],["FME"]:[78,"Egg"],["FUN"]:[49,""],["IZL"]:[42,"Av Flame"],["IZR"]:[44,"Av Frost"],["IFL"]:[65,"Flame"],["IFR"]:[67,"Frost"],["LWY"]:[65,"Wyrm"],["NDR"]:[3,""],["NSD"]:[40,""],["OOZ"]:[31,""],["RPT"]:[51,""],["RSC"]:[60,"Scout"],["SPL"]:[51,""],["SXP"]:[51,""],["SXD"]:[40,""],["SXT"]:[38,""],["SDL"]:[40,""],["SPN"]:[66,""],["SPU"]:[66,""],["XYR"]:[66,""],["TOT"]:[40,"Tendrils"],["TFK"]:[38,""],["TFD"]:[38,""],["TFF"]:[38,""],["TTF"]:[38,""],["ZTR"]:[32,"Returned"],["ZBS"]:[52,""],["ZST"]:[52,""],["ZMB"]:[52,""]}}get currentCard(){const t=this.browseStack;return t[t.length-1]}get friendos(){return this.friends[this.currentCard]??[]}get canGoBack(){return this.browseStack.length>1}init(t){if(a.state.gs.ui.detailStack=[t],a.mode.mode!=="C"){a.mode.setMode("C");const i=d.basicAction("CNCL");i.zone=a.mode.baseMode==="G"?"B":"N",i.param="PM",a.action.ddf5([i])}else a.drawing.drawAll()}changeLevel(t){const i=this.browseStack,e=i.length-1,s=i[e];i[e]=this.n(ut.extractCardCode(s),t)}goBack(){this.browseStack.pop()}goToFriend(t){this.browseStack.push(t)}friendTxt(t){const i=this.friendText[t];return i||[3,"I am error"]}goBackAction(){const t=d.basicAction("DTUP");return t.param="DB",t}levelAction(t){const i=d.basicAction("DTUP");return i.param=a.enTil("DL",t),i}friendAction(t){const i=d.basicAction("DTUP");return i.param=a.enTil("DF",t),i}updateDetails(t){const i=a.deTil(t.param),e=i[0];e==="DB"?this.goBack():e==="DF"?this.goToFriend(i[1]):e==="DL"?this.changeLevel(parseInt(i[1])):console.error("Unknown detail arg",t)}}class Hr{async drawAll(){const t=a.mode.mode;a.clickers.removeAll(),this.clearDetails(),t==="G"?(this.clearDeck(),await Promise.all([this.drawHand(a.state.getLocalPlayer()),this.drawPlayers(),this.drawField(),this.drawActions(a.action.activeActions)]),a.state.gs.winFlag!==0?await this.drawGameOver():await this.drawCommonButtons(!0,!0,!0)):t==="C"?await this.drawDetails(a.action.activeActions):t==="D"?await this.drawDeck(a.action.activeActions):t==="R"?await this.drawDraft(a.action.activeActions):t==="P"?await this.drawDeckPick(a.action.activeActions):t==="I"?await this.drawInfoMenu():t==="X"?await this.drawExit(a.action.activeActions):await this.drawMenu()}async drawGameOver(){await new be().drawGameOver()}async drawHand(t){await new Ne().drawCards(t)}async drawField(){await new ye().drawCards()}async drawPlayers(){await new be().drawAllPlayers()}async drawDeck(t){const i=t.find(s=>s.zone==="B");i&&await new pe().drawActions([i]);const e=new Ur;await e.drawCards(),await e.drawActions(t),await this.drawCommonButtons(!0,!0,!1)}async drawActions(t){const i=new pe;if(a.state.gs.aiActive)await i.drawActions([]);else{a.canvases.get("effects").clear();const s=new Ne,l=new ye,u=new be;await Promise.all([s.drawActions(t),l.drawActions(t),i.drawActions(t),u.drawActions(t)])}}async drawCombat(t){if(t.length===0)return;a.state.gs.ui.animating=!0,a.canvases.get("effects").clear(),await new ye().drawFight(t),a.state.gs.ui.animating=!1}async drawDraft(t){a.canvases.get("effects").clear();const e=new zr,s=a.canvases.get("game-ui");s.clear(),a.clickers.removeAll(),await e.drawPack(t,s),await e.drawList(t,s),await this.drawCommonButtons(!0,!0,!1)}async drawMenu(){const t=new Kr,i=a.canvases.get("game-ui");i.clear(),a.clickers.removeAll(),await t.drawMenu(i)}async drawInfoMenu(){const t=new Zr,i=a.canvases.get("game-ui");i.clear(),a.clickers.removeAll(),await t.drawMenu(i)}async drawDeckPick(t){const i=new Re,e=a.canvases.get("game-ui");e.clear(),a.clickers.removeAll(),await i.drawSets(t,e),await i.drawList(t,e)}clearDetails(){a.canvases.get("card").clear()}clearDeck(){a.canvases.get("decklist").clear()}async drawDetails(t){await this.drawActions(t),await this.drawCommonButtons(!0,!0,!1),a.canvases.get("card").clear();const e=new Wr,s=t.find(l=>l.actionKey==="CNCL");s?await e.drawDetails(s):console.error("Attempted to show details view with no cancel action")}async drawCommonButtons(t,i,e){let s=979;const l=723,u=new me,p=a.canvases.get("game-ui");if(e&&p.clear(),t&&(await u.makeIconButton(p,s,l,"EX"),s-=50),i){const y=a.state.clickMode;y!=="M"&&await u.makeIconButton(p,s,l,y==="I"?"TM":"DM")}}async drawExit(t){a.canvases.get("decklist").clear();const e=a.canvases.get("effects"),s=new jt;await s.drawButton(e,151,488,t[1],"Quit Game",24,!1),await s.drawButton(e,478,488,t[0],"Resume",52,!1)}setBackground(t){const i="hide";this.allBackgrounds.forEach(e=>{const s=document.getElementById(e);t===e?s.classList.remove(i):s.classList.add(i)})}changeScale(t){this.clearAll();let i=a.state.gs.options.scale;i===t&&(i="100");const e=t==="100"?1:.90625;a.clickers.updateScale(e);const s=`uiSize${i}`,l=`uiSize${t}`;this.allCanvases.forEach(y=>{a.canvases.get(y).dom.classList.replace(s,l)}),this.allBackgrounds.forEach(y=>{document.getElementById(y).classList.replace(s,l)}),document.getElementById("solforge").classList.replace(`gameSize${i}`,`gameSize${t}`),document.getElementById("log").classList.replace(`logSize${i}`,`logSize${t}`),a.state.gs.options.scale=t,a.state.writeStorage("scale",t)}clearAll(){this.allCanvases.forEach(t=>{a.canvases.get(t).clear()})}allCanvases=["game-ui","field","player","hand","effects","decklist","card"];allBackgrounds=["playmat","blueface","mainmenu"]}class Ht{paStats;pbStats;constructor(t,i){this.paStats=t,this.pbStats=i}allStats(){return[this.paStats,this.pbStats]}playerStats(t){return t?this.paStats:this.pbStats}deltaStats(t){const i=this.playerStats(t),e=this.playerStats(!t);return Ht.deltafier(i,e)}static deltafier(t,i){const e=Ht.blankStats(!0);return e.creatCount=t.creatCount-i.creatCount,e.creatValue=t.creatValue-i.creatValue,e.threatValue=t.threatValue-i.threatValue,e.playerHealth=t.playerHealth-i.playerHealth,e}static blankStats(t){return{side:t,creatCount:0,creatValue:0,threatValue:0,playerHealth:0}}}class At{guts;constructor(t){this.guts=t}static fromState(){const t=a.field.qActiveCreats(),i=[!0,!1],e=i.map(l=>a.field.laneIdx.map(u=>{const p=t.find(y=>y.owner===l&&y.lane===u);return p?d.evalVectorCreature(p):d.evalVectorEmpty(l,u)})).flat(),s=i.map(l=>{const u=a.player.toPlayer(l),p=d.evalVectorPlayer(l);return p.health=u.health,p});return e.push(...s),new At(e)}static ddp9(t,i,e,s,l=0){return t/2+l+i/10+(s+e)/3}static creatValueFormula(t){return t.health+Math.max(0,t.attack)+t.bonus+t.regen+t.armor-t.poison}clone(){return new At(structuredClone(this.guts))}boardStats(){const t=[!0,!1].map(i=>this.guts.filter(e=>e.playerA===i)).map(i=>{const e=Ht.blankStats(i[0].playerA),s=i.filter(l=>l.type==="C");return e.creatCount=s.length,e.playerHealth=i[5].health,e.creatValue=this.creatSum(s,At.creatValueFormula),e.threatValue=this.creatSum(s,l=>{if(l.defender)return 0;{const u=this.guts[this.findLaneIndex(6-l.lane,!l.playerA)];let p=0;if(u.type==="E"?p=l.attack:l.breakthrough&&(p=l.attack-u.health),p<1)return 0;if(p<6)return p;{let y=!0,A=0,T=5,D=1;const N=5;for(;y;)p>T+N?(A+=D*N,T+=N,D++):(A+=D*(p-T),y=!1);return p+(l.sleeping&&!l.aggressive?A/2:A)}}}),e});return new Ht(t[0],t[1])}weighState(t,i,e){const s=this.boardStats(),l=s.playerStats(!e).playerHealth;if(l<1&&li(e)).reduce((e,s)=>e+s,0)}findLaneIndex(t,i){return t+(i?-1:4)}findVectorIdx(t){return t.shape==="B"?this.findLaneIndex(t.lane,t.playerA):t.playerA?10:11}getBaseVector(t){return this.guts[this.findVectorIdx(t)]}replaceBaseVector(t){const i=this.findVectorIdx(t);this.guts[i]=t}applyVectors(t){t.forEach(e=>{switch(e.type){case "M":if(e.shape==="B")if(e.destroy)this.replaceBaseVector(d.evalVectorEmpty(e.playerA,e.lane));else{const l=this.getBaseVector(e);if(l.type!=="E"){if(e.moveTo){const u=d.evalVectorEmpty(e.playerA,e.lane);l.lane=e.moveTo,this.replaceBaseVector(l),this.replaceBaseVector(u)}else if(this.applyCreatVector(l,e),e.dirDmg){const u=Math.min(e.dirDmg,l.armor-l.armorDmg);l.armorDmg+=u,l.health-=e.dirDmg-u}}}else{const l=this.getBaseVector(e);l.health+=e.health,e.dirDmg&&(l.health-=e.dirDmg)}break;case "R":const s=d.evalVectorEmpty(e.playerA,e.lane);this.applyCreatVector(s,e),s.type="C",this.replaceBaseVector(s);break;case "P":break;default:console.warn("BoardEval is applying vectors, encountered unexpected vector type:",e)}}),this.guts.filter(e=>e.shape==="B"&&e.health<1).forEach(e=>this.replaceBaseVector(d.evalVectorEmpty(e.playerA,e.lane)))}battleState(t){return t.type==="E"?0:t.defender||t.sleeping&&!t.aggressive?1:2}battleVector(t,i,e,s){const l=[];if(t>0){let u=0;const p=Math.max(e.attack,0);if(i>0){const y=t===2&&e.breakthrough,A=Math.max(Math.min(p,s.armor-s.armorDmg),0);let T=p-A;if(y&&T>s.health&&(u=T-s.health,T=s.health),A>0||T>0){const D=d.ddp1(s.playerA,s.lane);D.health=-T,D.armorDmg=A,l.push(D)}}else t===2&&(u=p);u>0&&l.push(d.evalVectorPlayerLife(s.playerA,-u))}return l}applyCombat(){const t=a.field.laneIdx.map(i=>{const e=this.guts[i-1],s=this.guts[10-i],l=this.battleState(e),u=this.battleState(s);return l===2||u===2?this.battleVector(l,u,e,s).concat(this.battleVector(u,l,s,e)):[]}).flat();this.applyVectors(t)}applyTurnEnd(t,i){const e=!t,s=!i,l=this.guts.filter(u=>u.shape==="B"&&(e||u.playerA===s)&&u.poison>0).map(u=>{const p=u.regen-Math.max(0,u.poison-u.armor),y=d.ddp1(u.playerA,u.lane);return y.health=p,y}).filter(u=>u.health!==0);this.applyVectors(l)}applyCreatVector(t,i){t.health+=i.health,t.attack+=this.doubleOrNossing(t.attack,i.attack),t.armor+=i.armor,t.armorDmg+=i.armorDmg,t.poison+=this.doubleOrNossing(t.poison,i.poison),t.regen+=i.regen,t.aggressive=i.aggressive,t.breakthrough=i.breakthrough,t.defender=i.defender,t.negateDefender=i.negateDefender}doubleOrNossing(t,i){if(i>=Rt&&i