show_more = false macros += M 1 ===bonsai_smart_cast { local monster_ac={["adder"]=1,["jelly"]=0,["Tiamat"]=30,["Sonja"]=2,["jumping spider"]=6,["spatial vortex"]=0,["formicid"]=3,["queen bee"]=10,["acid dragon"]=5,["rime drake"]=3,["dwarf"]=2,["centaur"]=3,["orb of destruction"]=0,["deep elf zephyrmancer"]=0,["hobgoblin"]=2,["Amaemon"]=3,["deep troll shaman"]=6,["shadow imp"]=3,["human"]=3,["eldritch tentacle segment"]=13,["twister"]=0,["bat"]=1,["glowing shapeshifter"]=0,["Bai Suzhen"]=14,["golden dragon"]=15,["draconian annihilator"]=-1,["ophan"]=10,["white draconian"]=9,["death knight"]=2,["radroach"]=13,["Rupert"]=0,["sky beast"]=3,["ballistomycete spore"]=0,["black mamba"]=4,["the Serpent of Hell"]=30,["Gloorx Vloq"]=10,["Roxanne"]=20,["withered plant"]=0,["white imp"]=4,["Grinder"]=3,["thermic dynamo"]=4,["deep elf sorcerer"]=0,["spectral thing"]=8,["deep elf blademaster"]=0,["bunyip"]=6,["flayed ghost"]=0,["kraken"]=20,["bone dragon"]=20,["bombardier beetle"]=4,["tentacle segment"]=5,["shard shrike"]=2,["yaktaur"]=4,["alligator"]=4,["the Royal Jelly"]=8,["emperor scorpion"]=18,["snake"]=0,["curse skull"]=35,["tentacle"]=5,["Gastronok"]=2,["small abomination"]=0,["minotaur"]=6,["acid blob"]=1,["deep elf master archer"]=0,["orange demon"]=3,["Executioner"]=10,["naga"]=6,["frilled lizard"]=0,["oni"]=1,["Louise"]=0,["Murray"]=30,["water elemental"]=4,["bound soul"]=8,["culicivora"]=2,["mummy priest"]=8,["green draconian"]=9,["tentacled starspawn"]=5,["faun"]=2,["fire crab"]=9,["yaktaur captain"]=5,["pandemonium lord"]=1,["servant of whispers"]=1,["komodo dragon"]=7,["occultist"]=0,["goliath frog"]=3,["hydra"]=0,["hellwing"]=16,["Mara"]=10,["orc"]=0,["dream sheep"]=2,["wolf"]=4,["gargoyle"]=18,["Urug"]=2,["Norris"]=1,["ribbon worm"]=1,["Sojobo"]=2,["boggart"]=0,["skyshark"]=6,["Maggie"]=0,["reaper"]=15,["Lom Lobon"]=10,["large simulacrum"]=10,["small zombie"]=0,["satyr"]=2,["fire vortex"]=0,["ancient champion"]=15,["mutant beast"]=8,["demigod"]=2,["snapping turtle"]=16,["hell hound"]=6,["spark wasp"]=9,["deep troll earth mage"]=12,["draconian"]=10,["deep elf death mage"]=0,["manticore"]=5,["sun demon"]=10,["giant cockroach"]=3,["starspawn tentacle"]=8,["spriggan air mage"]=1,["ice dragon"]=10,["halazid warlock"]=8,["laughing skull"]=4,["will-o-the-wisp"]=4,["very ugly thing"]=6,["Duvessa"]=2,["Ijyb"]=2,["hell lord"]=0,["quicksilver dragon"]=10,["entropy weaver"]=7,["Natasha"]=2,["deep elf high priest"]=3,["ghost moth"]=8,["balrug"]=5,["hellion"]=5,["fire elemental"]=4,["dire elephant"]=13,["silent spectre"]=5,["chaos spawn"]=4,["hell rat"]=7,["walking divine tome"]=10,["fungus"]=0,["Sigmund"]=0,["training dummy"]=0,["demonspawn warmonger"]=3,["foxfire"]=0,["molten gargoyle"]=14,["water moccasin"]=2,["starcursed mass"]=10,["Josephine"]=0,["ogre mage"]=1,["merfolk siren"]=4,["draconian stormcaller"]=0,["polar bear"]=7,["demonspawn corrupter"]=3,["Psyche"]=0,["harpy"]=2,["inugami"]=5,["angel"]=10,["quasit"]=5,["Parghit"]=1,["moth"]=0,["eleionoma"]=2,["spectral weapon"]=5,["Zenata"]=10,["sixfirhy"]=2,["royal mummy"]=10,["orc knight"]=2,["blazeheart golem"]=9,["merfolk avatar"]=4,["vault guard"]=1,["halfling"]=2,["orb of fire"]=20,["iron elemental"]=20,["ancient lich"]=20,["curse toe"]=25,["demonspawn blood saint"]=6,["tengu conjurer"]=2,["boulder"]=10,["orc sorcerer"]=5,["Vv"]=27,["purple draconian"]=9,["guardian mummy"]=6,["draconian monk"]=-3,["eidolon"]=12,["sphinx"]=5,["ynoxinul"]=3,["fire bat"]=1,["ironbound frostheart"]=0,["death drake"]=6,["shadow"]=3,["stone giant"]=12,["wight"]=4,["bloated husk"]=5,["ironbound preserver"]=0,["wyvern"]=5,["broodmother"]=2,["frost giant"]=9,["water nymph"]=2,["hog"]=2,["Mnoleg"]=11,["Nessos"]=4,["mummy"]=3,["armataur"]=15,["Agnes"]=0,["sea snake"]=2,["iron imp"]=6,["golem"]=0,["smoke demon"]=5,["daeva"]=10,["iron golem"]=25,["Grum"]=2,["Mennas"]=15,["ghoul"]=4,["formless jellyfish"]=0,["animated tree"]=0,["pillar of salt"]=1,["lava snake"]=2,["shadow demon"]=7,["lightning spire"]=13,["merfolk javelineer"]=0,["merfolk impaler"]=0,["deep elf demonologist"]=0,["sickly merfolk siren"]=4,["sacred lotus"]=24,["tyrant leech"]=5,["phantasmal warrior"]=12,["djinni"]=5,["naga mage"]=6,["Polyphemus"]=10,["quokka"]=2,["ball lightning"]=0,["deathcap"]=5,["snaplasher vine segment"]=6,["Terence"]=0,["fire dragon"]=10,["hornet"]=6,["wendigo"]=4,["lich"]=10,["briar patch"]=10,["kobold demonologist"]=2,["naga ritualist"]=6,["fire giant"]=8,["Ignacio"]=10,["bennu"]=6,["Crazy Yiuf"]=2,["ufetubus"]=2,["malarious merfolk avatar"]=4,["giant"]=0,["grey draconian"]=16,["hell hog"]=2,["living spell"]=0,["shambling mangrove"]=13,["sleepcap"]=5,["bullfrog"]=0,["death yak"]=9,["Dissolution"]=10,["the Lernaean hydra"]=0,["ballistomycete"]=1,["walking frostbound tome"]=10,["elemental wellspring"]=8,["Jory"]=10,["deep elf annihilator"]=0,["iron giant"]=18,["spriggan"]=1,["pale draconian"]=9,["Joseph"]=0,["ancient zyme"]=6,["Chuck"]=14,["nargun"]=25,["ironbound convoker"]=0,["kobold blastminer"]=4,["peacekeeper"]=20,["dragon"]=0,["deep elf knight"]=0,["small simulacrum"]=10,["shock serpent"]=2,["protean progenitor"]=7,["Margery"]=0,["animated armour"]=8,["Lodul"]=3,["snaplasher vine"]=4,["mana viper"]=3,["goblin"]=0,["imperial myrmidon"]=1,["tengu reaver"]=2,["deep elf elementalist"]=0,["worldbinder"]=12,["orc warrior"]=0,["Frederick"]=0,["Jessica"]=0,["elephant slug"]=2,["moon troll"]=20,["Pikel"]=4,["starspawn tentacle segment"]=8,["Azrael"]=10,["draconian scorcher"]=-1,["Asmodeus"]=30,["seraph"]=10,["soul eater"]=18,["great orb of eyes"]=10,["crystal guardian"]=20,["Khufu"]=10,["salamander"]=5,["golden eye"]=0,["orange crystal statue"]=12,["caustic shrike"]=8,["two-headed ogre"]=3,["rust devil"]=10,["Maurice"]=1,["Nellie"]=13,["naga warrior"]=6,["plant"]=0,["floating eye"]=0,["orc wizard"]=1,["revenant"]=8,["ice beast"]=5,["death cob"]=10,["holy swine"]=2,["fenstrider witch"]=3,["thrashing horror"]=5,["meliai"]=2,["drowned soul"]=0,["demonspawn"]=3,["glowing orange brain"]=2,["war gargoyle"]=25,["pearl dragon"]=10,["spriggan berserker"]=2,["spriggan druid"]=1,["Prince Ribbit"]=0,["vault sentinel"]=1,["Cloud Mage"]=0,["vine stalker"]=2,["Kirke"]=0,["green death"]=5,["centaur warrior"]=4,["basilisk"]=3,["dancing weapon"]=10,["tainted leviathan"]=15,["draconian knight"]=9,["orc warlord"]=3,["raiju"]=4,["diamond obelisk"]=12,["warg"]=9,["electric golem"]=5,["Nergalle"]=9,["fulminant prism"]=3,["scrub nettle"]=8,["martyred shade"]=0,["ugly thing"]=4,["demonspawn black sun"]=9,["Vashnia"]=6,["giant lizard"]=0,["ice devil"]=12,["orc priest"]=1,["Saint Roka"]=3,["storm dragon"]=13,["eldritch tentacle"]=13,["crystal echidna"]=10,["burial acolyte"]=0,["Robin"]=1,["tormentor"]=12,["Jorgrun"]=2,["ironbound thunderhulk"]=1,["glass eye"]=2,["Fannar"]=4,["toenail golem"]=8,["block of ice"]=15,["Hellbinder"]=0,["red devil"]=7,["ancestor"]=5,["giant frog"]=0,["Killer Klown"]=10,["necromancer"]=0,["meteoran"]=2,["torpor snail"]=8,["electric eel"]=1,["juggernaut"]=20,["hell beast"]=5,["apocalypse crab"]=11,["red draconian"]=9,["tengu warrior"]=2,["rat"]=1,["guardian serpent"]=6,["large zombie"]=8,["Arachne"]=3,["creeping inferno"]=0,["spectator"]=0,["slime creature"]=1,["felid"]=2,["merfolk aquamancer"]=0,["demonic plant"]=0,["doom hound"]=6,["earth elemental"]=14,["merfolk"]=4,["iron dragon"]=20,["Blork the orc"]=0,["elephant"]=8,["cacodemon"]=11,["orc high priest"]=1,["hell knight"]=0,["lemure"]=4,["gnoll sergeant"]=2,["shadow dragon"]=15,["Snorg"]=0,["weeping skull"]=7,["Xtahua"]=18,["phantom"]=3,["Ice Fiend"]=15,["antique champion"]=20,["Aizul"]=8,["spellforged servitor"]=10,["crimson imp"]=3,["Ilsuiw"]=5,["hexer"]=5,["river rat"]=5,["yellow draconian"]=9,["titan"]=10,["wolf spider"]=3,["small skeleton"]=0,["Orb Guardian"]=13,["naga sharpshooter"]=6,["vault warden"]=1,["ball python"]=0,["steam dragon"]=5,["salamander tyrant"]=5,["swamp worm"]=3,["Frances"]=0,["battlemage"]=5,["death scarab"]=7,["black bear"]=2,["quicksilver ooze"]=3,["arcanist"]=0,["freezing wraith"]=12,["yak"]=4,["Jeremiah"]=2,["deep elf pyromancer"]=0,["jackal"]=2,["spriggan rider"]=1,["insubstantial wisp"]=0,["necrophage"]=2,["tentacled monstrosity"]=5,["efreet"]=10,["saltling"]=15,["nameless horror"]=8,["lost soul"]=0,["octopode"]=1,["Nikola"]=1,["Edmund"]=0,["ragged hierophant"]=0,["strange machine"]=12,["unseen horror"]=5,["spriggan defender"]=3,["dread lich"]=20,["hound"]=2,["nagaraja"]=6,["Brimstone Fiend"]=15,["jiangshi"]=10,["large skeleton"]=0,["elemental"]=0,["alligator snapping turtle"]=19,["vampire mage"]=10,["drake"]=0,["catoblepas"]=10,["tarantella"]=3,["lindwurm"]=8,["putrid mouth"]=5,["Josephina"]=10,["skeleton"]=0,["swamp dragon"]=7,["vampire mosquito"]=2,["Mlioglotl"]=10,["Donald"]=3,["boulder beetle"]=20,["vampire"]=10,["troll"]=3,["bush"]=15,["ice statue"]=12,["kobold"]=2,["gnoll bouda"]=2,["Geryon"]=15,["eye of devastation"]=12,["cerulean imp"]=3,["cyclops"]=5,["toadstool"]=1,["ghost"]=0,["vampire knight"]=10,["simulacrum"]=10,["kobold brigand"]=3,["Menkaure"]=3,["walking earthen tome"]=20,["shapeshifter"]=0,["bear"]=0,["Ereshkigal"]=10,["scorpion"]=5,["blizzard demon"]=10,["rakshasa"]=6,["orb spider"]=3,["spatial maelstrom"]=0,["Pargi"]=1,["Cerebov"]=30,["black draconian"]=9,["Hell Sentinel"]=25,["Harold"]=0,["neqoxec"]=4,["draconian shifter"]=-1,["gnoll"]=2,["statue"]=12,["rockslime"]=27,["wandering mushroom"]=5,["Antaeus"]=28,["quicksilver elemental"]=1,["battlesphere"]=0,["Head Instructor"]=0,["Grunn"]=6,["salamander mystic"]=5,["the Enchantress"]=1,["Dispater"]=35,["apis"]=9,["hellephant"]=13,["azure jelly"]=5,["Erolcha"]=3,["Asterion"]=4,["zombie"]=0,["cherub"]=10,["lorocyproca"]=10,["orc apostle"]=2,["steelbarb worm"]=11,["obsidian statue"]=12,["deep dwarf"]=2,["blazeheart core"]=0,["cactus giant"]=1,["walking crystal tome"]=15,["shadow wraith"]=7,["iguana"]=5,["pharaoh ant"]=4,["brain worm"]=1,["redback"]=2,["Dowan"]=0,["anaconda"]=4,["wraith"]=10,["killer bee"]=2,["merged slime creature"]=0,["butterfly"]=0,["endoplasm"]=1,["moth of wrath"]=0,["wind drake"]=3,["crab"]=0,["aspiring flesh"]=2,["searing wretch"]=4,["jorogumo"]=4,["crocodile"]=4,["starflower"]=16,["barachi"]=0,["ettin"]=9,["ghost crab"]=9,["profane servitor"]=10,["shining eye"]=3,["dryad"]=6,["thorn hunter"]=9,["demonic crawler"]=10,["bog body"]=1,["stoker"]=5,["wretched star"]=10,["deep troll"]=6,["Eustachio"]=0,["elf"]=1,["vampire bat"]=1,["cane toad"]=6,["tengu"]=2,["oklob plant"]=10,["skeletal warrior"]=15,["swamp drake"]=3,["lurking horror"]=0,["player ghost"]=1,["iron troll"]=20,["ushabti"]=9,["Erica"]=0,["howler monkey"]=1,["blink frog"]=0,["dart slug"]=1,["spider"]=0,["sun moth"]=6,["ogre"]=1,["knight"]=5,["player illusion"]=1,["Tzitzimitl"]=12,["air elemental"]=2,["walking tome"]=0,["large abomination"]=0,["deep elf archer"]=0,["Boris"]=12,["oklob sapling"]=10} function bonsai_smart_cast() local mp=_G.you.mp() -- Djinni uses HP as MP local ok_race,race=pcall(function() return you.race() end) if ok_race and race=="Djinni" then mp=you.hp()-10 end -- reserve 10 HP local los=you.los() local t={} local friends={} for x=-los,los do for y=-los,los do local m=monster.get_monster_at(x,y) if m and not m:is_firewood() then if m:attitude()==0 then t[#t+1]={m=m,x=x,y=y} elseif m:attitude()>0 and m:name()~="battlesphere" and m:name()~="orb of destruction" then friends[#friends+1]={x=x,y=y} end end end end if #t==0 then crawl.mpr("No")return end local function has_friendly(cx,cy,radius) for _,f in ipairs(friends) do if math.max(math.abs(f.x-cx),math.abs(f.y-cy))<=radius then return true end end return false end -- Track last cast turn for lingering spells last_cast_turn = last_cast_turn or {} local turn=you.turns() -- Check if spell was cast recently local function recently_cast(name) local lt=last_cast_turn[name] if not lt then return false end local turns_left=0 if name=="Flame Wave" then turns_left=3 elseif name=="Searing Ray" then turns_left=3 elseif name=="Iskenderun's Battlesphere" then turns_left=999 elseif name=="Conjure Ball Lightning" then turns_left=999 else return false end return (turn-lt)=0 and math.ceil(known/5.0)==pips then return known end return 2.5+(pips-1)*5 end local function get_ev(m) local pips=m:ev() if pips==0 then return 0 end return 2.5+(pips-1)*5 end local function get_max_hp(m) local hp_str=tostring(m:max_hp()) hp_str=hp_str:gsub("about ",""):gsub("~","") return tonumber(hp_str) or 20 end local function get_hp(m) local mhp=get_max_hp(m) local dl=m:damage_level() -- damage_level: 0=full, 1=lightly, 2=moderately, 3=heavily, 4=severely, 5=almost dead, 6=dead return math.max(1, mhp*(6-dl)/6) end local function cdist(x,y) return math.max(math.abs(x),math.abs(y)) end local function score_dmg(dmg,m,dist) local hp=get_hp(m) local mhp=get_max_hp(m) local threat=m:threat()+1 -- Overkill waste: only 20% credit for damage beyond kill threshold local effective=dmg if dmg>hp then effective=hp+(dmg-hp)*0.2 end -- Finish-off bonus: killing removes threat entirely local finish=1.0 if dmg>=hp then finish=1.5 end -- Distance urgency: adjacent monsters are most dangerous dist=dist or 4 local urgency=1.0 if dist<=1 then urgency=2.0 elseif dist<=2 then urgency=1.5 elseif dist<=3 then urgency=1.2 end -- Base score: effective damage weighted by threat, finish, urgency, HP ratio local score=effective*finish*urgency*threat/mhp -- Deprioritize summoned monsters (they disappear soon) local desc=m:target_desc() or "" if string.find(desc,"summoned") then score=score*0.2 end -- Deprioritize safe monsters if m:is_safe() then score=score*0.1 end return score end local function get_pow(name) local p=spells.power_perc(name) if not p then return 0 end return p end local function player_res(e_type) if e_type==1 then return you.res_cold()end if e_type==2 then return you.res_shock()end if e_type==3 then return you.res_fire()end if e_type==4 then return you.res_poison()end return 0 end local function calc_dmg(base_dmg,ac,ev,pow,noac,nohit,ac3) local dmg=base_dmg -- pow is power_perc (0-100), already factored into get_spell_dmg formulas if noac then -- Skip AC reduction elseif ac>0 then if ac3 then -- AC applied 3 times (Sandblast) local dmg1=dmg local dmg2=dmg local dmg3=dmg if dmg1<=ac then dmg1=dmg1*dmg1/(2*(ac+1)) elseif dmg1>ac then dmg1=dmg1-ac/2 end if dmg2<=ac then dmg2=dmg2*dmg2/(2*(ac+1)) elseif dmg2>ac then dmg2=dmg2-ac/2 end if dmg3<=ac then dmg3=dmg3*dmg3/(2*(ac+1)) elseif dmg3>ac then dmg3=dmg3-ac/2 end dmg=dmg1+dmg2+dmg3 else if dmg<=ac then dmg=dmg*dmg/(2*(ac+1)) else dmg=dmg-ac/2 end end end if nohit then -- Skip EV reduction (always hits) elseif ev>0 then dmg=dmg*math.max(0,1.0-ev/50.0) end return dmg end -- Shield block: ~50% damage reduction for shielded monsters local function sh_damage(dmg,m) local desc=m:target_desc() or "" if string.find(desc,"shield") then return dmg*0.5 end return dmg end -- Evasion check using game API (more accurate than pip estimation) local function evasion_check(m,spellname) if not spellname then return 1.0 end local ok,desc=pcall(function() return m:target_spell(spellname) end) if ok and desc then local hit=string.match(desc,"(%d+)%% to hit") if hit then return tonumber(hit)/100.0 end end return nil -- fallback to pip-based end -- Calculate average damage using wiki formulas (Nd(X) avg = N*(X+1)/2) local function get_spell_dmg(name,pow) -- Level 1 if name=="Foxfire" then return 2*(4+pow/5+1)/2 end -- 2x 1d(4+pow/5) if name=="Freeze" then return (3+3*pow/10+1)/2 end -- 1d(3+3pow/10), ignores AC if name=="Magic Dart" then return (3+pow/5+1)/2 end -- 1d(3+pow/5) if name=="Sandblast" then return 2*(4+pow/3+1)/2 end -- 2d(4+pow/3), triple AC if name=="Poisonous Vapours" then return (1+pow/8+1)/2 end -- 1d(1+pow/8) if name=="Shock" then return (3+pow/4+1)/2 end -- 1d(3+pow/4) -- Level 2 if name=="Mercury Arrow" then return 2*(11+pow/4+1)/2 end -- 2d(11+pow/4) poison+30% irresist if name=="Scorch" then return 2*(5+pow/12+1)/2 end -- 2d(5+pow/12) if name=="Searing Ray" then return 2*(9+pow/7+1)/2 end -- 2d(9+pow/7) irresistible if name=="Static Discharge" then return 3+(2+pow/12)/2 end -- 3+random2(3+pow/12) -- Level 3 if name=="Frozen Ramparts" then return (1+0.3*pow+1)/2 end -- 1d(1+0.3pow) per turn if name=="Hailstorm" then return 3*(15+pow/3+1)/2 end -- 3d(15+pow/3) 50cold+50phys if name=="Stone Arrow" then return 3*(7+pow/8+1)/2 end -- 3d(7+pow/8) -- Level 4 if name=="Airstrike" then return 2*((pow+13)/14+1)/2 end -- 2d((pow+13)/14), +2/space added separately if name=="Brom's Barrelling Boulder" then return 2*(4+pow/10+1)/2 end -- 2d(4+pow/10) if name=="Dispel Undead" then return 3*(6.66+pow/4+1)/2 end -- 3d(6.66+pow/4), ignores AC, undead only if name=="Flame Wave" then return 3*2*(4.5+pow/6+1)/2 end -- 2d(4.5+pow/6) x 3 turns if name=="Fulminant Prism" then return 3*(6+7*pow/40+1)/2 end -- 3d(6+7pow/40) irresistible if name=="Iskenderun's Battlesphere" then return 2*(7+2*pow/9+1)/2 end -- 2d(7+2pow/9) hd=1+rawpow/9 if name=="Iskenderun's Mystic Blast" then return 2*(6+pow/3+1)/2 end -- 2d(6+pow/3) + collision 2d(1+pow/10) if name=="IMB_collision" then return 2*(1+pow/10+1)/2 end -- 2d(1+pow/10) wall/monster impact if name=="Ignite Poison" then return 4*(12+pow*6/100+1)/2 end -- 2*pois_str d(12+pow*6/100), assume pois=2 if name=="Olgreb's Toxic Radiance" then return 3*(1+pow/20+1)/2 end -- 1d(1+pow/20)/turn x ~3 turns if name=="Sticky Flame" then return 2*(4+pow/9+1)/2 + (3+3*pow/40)*8 end -- 2d(4+pow/9) impact + 2d7/turn DoT -- Level 5 if name=="Arcjolt" then return (10+pow/2+1)/2 end -- 1d(10+pow/2) if name=="Borgnjor's Vile Clutch" then return 2*(4+pow/20+1)/2 end -- 2d(4+pow/20) per turn if name=="Fulminant Prism" then return 3*(5+7*pow/20+1)/2 end -- 3d(5+7*hd/4), hd=pow/5 (power_perc scaled) if name=="Freezing Cloud" then return 14 end -- cloud: 6+r2a(16,2) avg ~14/turn, not pow-scaled if name=="Fireball" then return 3*(3.33+pow/6+1)/2 end -- 3d(3.33+pow/6) if name=="Irradiate" then return 3*(11.66+pow/6+1)/2 end -- 3d(11.66+pow/6) if name=="Lee's Rapid Deconstruction" then return 3*(4+pow/5+1)/2 end -- 3d(4+pow/5) triple AC; needs wall/fraggable target -- Level 6 if name=="Bombard" then return 9*((13+2*pow/3)/9+1)/2 end -- 9d((13+2pow/3)/9) if name=="Permafrost Eruption" then return 2*4*(1.75+pow/10+1)/2 end -- 2x 4d(1.75+pow/10) if name=="Plasma Beam" then return 2*(10+11*pow/20+1)/2 end -- 2x 1d(10+11pow/20) elec+fire if name=="Starburst" then return 6*(3+pow/9+1)/2 end -- 6d(3+pow/9) per bolt (8 bolts, single target hit once) -- Level 7 if name=="Hellfire Mortar" then local hd=1+pow/10;local zp=hd*12;return 4*(4+zp*2/21+1)/2 end -- 4d(4+hd*12*2/21) per shot if name=="Magnavolt" then return 4*(9+pow/10+1)/2 end -- 4d(9+pow/10) if name=="Orb of Destruction" then return 9*(5+pow/12+1)/2 end -- 9d(5+pow/12) if name=="Ozocubu's Refrigeration" then return 4*(7.5+pow/9+1)/2 end -- 4d(7.5+pow/9) -- Level 8 if name=="Ignition" then return 3*(3.33+pow/9+1)/2 end -- 3d(3.33+pow/9) per enemy if name=="Lehudib's Crystal Spear" then return 10*(2.3+pow/10+1)/2 end -- 10d(2.3+pow/10) if name=="Fulsome Fusillade" then return 3*(5+pow/8+1)/2 end -- 3d(5+pow/8) per explosion if name=="Maxwell's Capacitive Coupling" then return 999 end -- guaranteed kill -- Level 9 if name=="Fire Storm" then return 8*((5+pow)/8+1)/2 end -- 8d((5+pow)/8) 50% bypasses rF if name=="Chain Lightning" then return 3*(2*pow/3+1)/2 end -- 3d(2pow/3) if name=="Polar Vortex" then return 6*12*(pow/45+1)/2 end -- 12d(rpow/15) per turn x6 turns; rpow≈pow/3 avg (open space) if name=="Shatter" then return 3*(5+pow/3+1)/2 end -- 3d(5+pow/3) base; x2 for nonliving return 10 end local function get_res(m,e_type) if e_type==1 then return m:res_cold()end if e_type==2 then return m:res_shock()end if e_type==3 then return m:res_fire()end if e_type==4 then return m:res_poison()end return 0 end local function get_range(name) local r=spells.range(name) if not r then return 0 end return r end local function can_reach(tx,ty,range) local dist=math.max(math.abs(tx),math.abs(ty)) if dist>range then return false end return view.cell_see_cell(0,0,tx,ty) end local function is_solid(x,y) local f=view.feature_at(x,y) return f=="rock wall" or f=="stone wall" or f=="permarock wall" or f=="unnaturally hard rock wall" or f=="metal wall" or f=="crystal wall" or f=="closed door" or f=="runed door" or f=="sealed door" or f=="tree" end local function get_cost(name) local c=spells.mana_cost(name) if not c then return 99 end -- Flame Wave costs 1 MP to continue, full cost to start if name=="Flame Wave" and recently_cast("Flame Wave") then return 1 end return c end local function near_wall(x,y) for dx=-1,1 do for dy=-1,1 do if dx~=0 or dy~=0 then local f=view.feature_at(x+dx,y+dy) if f and (f=="#" or f=="=" or f=="|" or f=="x") then return true end end end end return false end local function count_empty(x,y) local count=0 for dx=-1,1 do for dy=-1,1 do if dx~=0 or dy~=0 then local m=monster.get_monster_at(x+dx,y+dy) local f=view.feature_at(x+dx,y+dy) if not m and f and f~="#" and f~="=" and f~="|" and f~="x" then count=count+1 end end end end return count end local function beam_blocked_by_friendly(spell_name,tx,ty) local path=spells.path(spell_name,tx,ty) if not path then return false end for _,p in ipairs(path) do local m=monster.get_monster_at(p[1],p[2]) if m and m:attitude()>0 and m:name()~="battlesphere" and m:name()~="orb of destruction" then return true end end return false end local function monsters_on_path(spell_name,tx,ty) local result={} local path=spells.path(spell_name,tx,ty) if not path then return result end for _,p in ipairs(path) do if not (p[1]==tx and p[2]==ty) then local m=monster.get_monster_at(p[1],p[2]) if m and m:attitude()==0 and not m:is_firewood() then result[#result+1]={m=m,x=p[1],y=p[2]} end end end return result end local function monsters_on_line(tx,ty) local result={} local dx=tx>0 and 1 or (tx<0 and -1 or 0) local dy=ty>0 and 1 or (ty<0 and -1 or 0) local steps=math.max(math.abs(tx),math.abs(ty)) if steps==0 then return result end local x,y=0,0 for i=1,steps-1 do x=x+dx y=y+dy local m=monster.get_monster_at(x,y) if m and m:attitude()==0 and not m:is_firewood() then result[#result+1]={m=m,x=x,y=y} end end return result end local s={{ -- Level 1 n="Foxfire",e=3,summon=true,nohit=true -- fire, 2 foxfires, never miss },{n="Freeze",e=1,noac=true,nohit=true -- cold, range 1, ignores AC },{n="Magic Dart",e=0,nohit=true -- irresistible, never misses },{n="Poisonous Vapours",e=4 -- poison, range 3 },{n="Sandblast",e=0,ac3=true,slow=true -- physical, triple AC, 1.5 turns },{n="Shock",e=2,beam=true,halfres=true,penetrate=true,any_target=true -- elec, bouncing bolt, 1/2 AC -- Level 2 },{n="Mercury Arrow",e=4 -- poison+30% irresist },{n="Scorch",e=3,nohit=true -- fire, applies rF- },{n="Searing Ray",e=0,nohit=true,penetrate=true,channeled=true -- irresistible, channeled, penetrates },{n="Static Discharge",e=2,aoe=1,noac=true,nohit=true -- elec, arcs adj, ignores AC -- Level 3 },{n="Frozen Ramparts",e=1,aoe=2,centered=true,nohit=true -- cold, centered, adj walls },{n="Hailstorm",e=1,aoe=3,halfres=true,noadjacent=true -- cold+phys 50/50, donut AoE },{n="Stone Arrow",e=0,beam=true -- physical, bolt -- Level 4 },{n="Airstrike",e=0,noac=true,nohit=true -- irresistible, smite, ignores AC },{n="Brom's Barrelling Boulder",e=0,beam=true -- physical, line, knockback },{n="Dispel Undead",e=0,noac=true,nohit=true,undead=true,no_bsph=true -- ignores AC, undead only },{n="Flame Wave",e=3,aoe=3,nohit=true,channeled=true -- fire, expanding AoE centered, 3 turns },{n="Fulminant Prism",e=0,aoe=2,nohit=true,no_bsph=true,prism=true -- irresistible, smite-placed, delayed 2 turns },{n="Ignite Poison",e=3,aoe=9 -- fire, all poisoned in LOS },{n="Iskenderun's Battlesphere",e=0,summon=true,no_bsph=true -- irresistible, construct },{n="Iskenderun's Mystic Blast",e=0,aoe=2,nohit=true,centered=true -- irresistible, 2-tile AoE },{n="Olgreb's Toxic Radiance",e=4,aoe=9,noac=true,nohit=true -- poison, LOS AoE, ignores AC },{n="Sticky Flame",e=3,nohit=true,noac=true -- fire, DoT ignores AC (bulk of dmg) -- Level 5 },{n="Arcjolt",e=2,aoe=2,halfres=true,nohit=true -- elec, arcs, 1/2 AC },{n="Borgnjor's Vile Clutch",e=0,clutch=true,nohit=true,no_bsph=true -- necro+earth, bolt constricts all on path },{n="Fireball",e=3,aoe=2,nohit=true,any_target=true -- fire, 3x3 explosion },{n="Freezing Cloud",e=1,fcloud=true,nohit=true,no_bsph=true -- cold, smite cloud r2, zone control },{n="Irradiate",e=0,aoe=1,nohit=true -- irresistible, all adjacent },{n="Lee's Rapid Deconstruction",e=0,aoe=1,ac3=true,nohit=true -- physical, triple AC, needs wall/fraggable -- Level 6 },{n="Bombard",e=0,beam=true -- physical, bolt },{n="Conjure Ball Lightning",e=2,summon=true -- elec, 3 autonomous balls },{n="Permafrost Eruption",e=1,aoe=3,halfres=true,centered=true -- phys+cold 50/50 },{n="Plasma Beam",e=2,penetrate=true,halfres=true,auto_target=true -- elec+fire, 2 penetrating bolts, auto-targets farthest },{n="Starburst",e=3,starburst=true -- fire, 8 penetrating bolts from player -- Level 7 },{n="Hellfire Mortar",e=3,mortar=true,halfres=true,no_bsph=true -- fire+earth, bolt path creates lava, summons mortar },{n="Magnavolt",e=2,nohit=true,magnavolt=true -- elec, smite, magnetise chain },{n="Orb of Destruction",e=0,nohit=true,ood=true -- irresistible, homing, distance scaling },{n="Ozocubu's Refrigeration",e=1,aoe=9,nohit=true,auto_target=true -- cold, full LOS, damage reduced by adj enemies },{n="Spellspark Servitor",e=0,summon=true,no_bsph=true -- varies, construct -- Level 8 },{n="Ignition",e=3,aoe=9,nohit=true -- fire, 3x3 on every enemy },{n="Lehudib's Crystal Spear",e=0 -- irresistible, short range },{n="Fulsome Fusillade",e=3,aoe=9 -- variable, 3 explosions/turn },{n="Maxwell's Capacitive Coupling",e=2,aoe=9,noac=true,nohit=true -- elec, guaranteed kill -- Level 9 },{n="Fire Storm",e=3,aoe=3,nohit=true,halfres=true -- fire 50% irresist, smite },{n="Chain Lightning",e=2,aoe=9,halfres=true,nohit=true -- elec, arcs all in LOS },{n="Polar Vortex",e=1,aoe=5,centered=true,halfres=true,nohit=true -- cold 50% irresist, 6-turn sustained, r5 },{n="Shatter",e=0,aoe=9,nohit=true -- physical, full LOS AoE, ignores EV }} local candidates={} -- Detect active battlesphere and pre-calculate its bonus damage local bsph_bonus=0 local has_bsph=false for x=-los,los do for y=-los,los do local m2=monster.get_monster_at(x,y) if m2 and m2:name()=="battlesphere" and m2:attitude()>0 then has_bsph=true break end end if has_bsph then break end end if has_bsph and #t>0 then -- Battlesphere targets most-injured enemy: 2d(7+2*pow_perc/9), irresistible, auto-hit, ignores nothing local bsph_pow=0 if spells.memorised("Iskenderun's Battlesphere") then bsph_pow=get_pow("Iskenderun's Battlesphere") end local bsph_base=2*(7+2*bsph_pow/9+1)/2 -- Find most-injured enemy (most hp missing) local most_missing=0 local bsph_target=nil for _,e in ipairs(t) do local missing=get_max_hp(e.m)-get_hp(e.m) if missing>most_missing then most_missing=missing;bsph_target=e end end if not bsph_target then bsph_target=t[1] end -- Battlesphere damage: irresistible (e=0), auto-hit, vs AC local ac=get_ac(bsph_target.m) local dmg=calc_dmg(bsph_base,ac,0,0,false,true,false) dmg=sh_damage(dmg,bsph_target.m) bsph_bonus=score_dmg(dmg,bsph_target.m,cdist(bsph_target.x,bsph_target.y)) end for _,sp in ipairs(s) do local mem=spells.memorised(sp.n) local cost=get_cost(sp.n) local fail=spells.fail(sp.n) local ok_sev,sev=pcall(function() return spells.fail_severity(sp.n) end) if mem and fail<20 and (not ok_sev or sev<=2) then -- Chain Lightning requires rElec to safely cast -- Irradiate: avoid if high contamination (>1000) if (sp.n=="Chain Lightning" and you.res_shock()<1) or (sp.n=="Irradiate" and you.contaminated()>1000) or (sp.n=="Polar Vortex" and string.find(you.status() or "","ortex")) or (sp.n=="Fulsome Fusillade" and string.find(you.status() or "","usillade")) or (sp.n=="Hellfire Mortar" and string.find(you.status() or "","ellfire")) then else local range=get_range(sp.n) local pow=get_pow(sp.n) local best_val=0 local best_tx,best_ty=0,0 if sp.summon then local dmg=get_spell_dmg(sp.n,pow) -- Ball Lightning danger: check enemies nearby if sp.n=="Conjure Ball Lightning" then local adj_count=0 local near_count=0 for _,e in ipairs(t) do local dist=math.max(math.abs(e.x),math.abs(e.y)) if dist==1 then adj_count=adj_count+1 elseif dist<=2 then near_count=near_count+1 end end -- Very dangerous if adjacent enemies if adj_count>0 then dmg=dmg*0.1 -- Dangerous if enemies within 2 tiles elseif near_count>0 then dmg=dmg*0.3 -- Also needs rElec elseif you.res_shock()<1 then dmg=dmg*0.2 end end if sp.n=="Foxfire" then local clear=0 for dx=-1,1 do for dy=-1,1 do if dx~=0 or dy~=0 then local f=monster.get_monster_at(dx,dy)if not f then clear=clear+1 end end end end if clear<2 then dmg=dmg*0.1 end end if sp.centered and sp.n=="Polar Vortex" then local walls=0 for dx=-1,1 do for dy=-1,1 do if dx~=0 or dy~=0 then local f=view.feature_at(dx,dy) if f and (f=="#" or f=="=" or f=="|") then walls=walls+1 end end end end if walls>=4 then dmg=dmg*0.3 end end local kills_needed=math.ceil(20/dmg) local val=100/kills_needed best_val=val elseif sp.aoe and sp.aoe>0 then local total_dmg=0 local best_tx,best_ty=0,0 if sp.n=="Static Discharge" then for _,e in ipairs(t) do if math.abs(e.x)<=1 and math.abs(e.y)<=1 then local val=0 for _,e2 in ipairs(t) do local dist=math.max(math.abs(e2.x-e.x),math.abs(e2.y-e.y)) if dist<=1 then local r=get_res(e2.m,sp.e) local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0 elseif r>=3 then mult=0.0 end if mult>0 then local base=get_spell_dmg(sp.n,pow) local ac=get_ac(e2.m) local ev=get_ev(e2.m) local dmg=calc_dmg(base*mult,ac,ev,pow,sp.noac,sp.nohit,sp.ac3) val=val+score_dmg(dmg,e2.m,cdist(e2.x,e2.y)) end end end if val>total_dmg then total_dmg=val;best_tx=e.x;best_ty=e.y end end end best_val=total_dmg elseif sp.n=="Arcjolt" then -- Chain-arc simulation: start from player, expand outward local hit={} hit["0,0"]=true local base=get_spell_dmg(sp.n,pow) local val=0 for ring=1,los do local found=false for _,e2 in ipairs(t) do local key=e2.x..","..e2.y if not hit[key] then -- Check if adjacent to any already-hit cell local adj=false for dx=-1,1 do for dy=-1,1 do if hit[(e2.x+dx)..","..((e2.y+dy))] then adj=true end end end if adj then hit[key]=true found=true local r=get_res(e2.m,sp.e) local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0 elseif r>=3 then mult=0.0 end if mult>0 then local ac=get_ac(e2.m) local dmg if sp.halfres then local dmg1=calc_dmg(base*0.5*mult,ac,0,pow,sp.noac,sp.nohit,sp.ac3) local dmg2=calc_dmg(base*0.5,ac,0,pow,sp.noac,sp.nohit,sp.ac3) dmg=dmg1+dmg2 else dmg=calc_dmg(base*mult,ac,0,pow,sp.noac,sp.nohit,sp.ac3) end val=val+score_dmg(dmg,e2.m,cdist(e2.x,e2.y)) end end end end if not found then break end end best_val=val elseif sp.centered then -- Special handling for Permafrost Eruption: target largest cluster if sp.n=="Permafrost Eruption" then local best_cluster=0 local best_target=nil for _,e in ipairs(t) do local cluster_size=0 for _,e2 in ipairs(t) do if math.abs(e2.x-e.x)<=1 and math.abs(e2.y-e.y)<=1 then cluster_size=cluster_size+1 end end if cluster_size>best_cluster then best_cluster=cluster_size best_target=e end end if best_target then local dmg=0 for _,e2 in ipairs(t) do if math.abs(e2.x-best_target.x)<=1 and math.abs(e2.y-best_target.y)<=1 then local r=get_res(e2.m,sp.e) local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0 elseif r>=3 then mult=0.0 end if mult>0 then local base=get_spell_dmg(sp.n,pow) local ac=get_ac(e2.m) local ev=get_ev(e2.m) local tdmg=calc_dmg(base*mult,ac,ev,pow,sp.noac,sp.nohit,sp.ac3) dmg=dmg+score_dmg(tdmg,e2.m,cdist(e2.x,e2.y)) end end end if dmg>0 then best_val=dmg;best_tx=best_target.x;best_ty=best_target.y end end else -- Other centered spells (Starburst, Frozen Ramparts, Polar Vortex) local total_dmg=0 local val=0 for _,e in ipairs(t) do local others=monsters_on_line(e.x,e.y) others[#others+1]={m=e.m,x=e.x,y=e.y} for _,target in ipairs(others) do local r=get_res(target.m,sp.e) local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0 elseif r>=3 then mult=0.0 end if mult>0 then local base=get_spell_dmg(sp.n,pow) local ac=get_ac(target.m) local ev=get_ev(target.m) local dmg=calc_dmg(base*mult,ac,ev,pow,sp.noac,sp.nohit,sp.ac3) val=val+score_dmg(dmg,target.m,cdist(target.x,target.y)) end end end best_val=val if best_val>0 then best_tx=0;best_ty=0 end end elseif sp.n=="Lee's Rapid Deconstruction" then -- LRD: scan all cells in range for walls or fraggable monsters local best_dmg=0 for x=-range,range do for y=-range,range do if you.see_cell_solid_see(x,y) then local f=view.feature_at(x,y) local dice=0 local radius=1 local from_mon=false -- Check monster first local tm=monster.get_monster_at(x,y) if tm and tm:attitude()==0 and not tm:is_firewood() and tm:holiness()=="nonliving" then dice=3;from_mon=true end -- Then terrain if dice==0 then local fl=string.lower(f or "") if string.find(fl,"rock") or string.find(fl,"stone") or string.find(fl,"door") or string.find(fl,"slimy") or string.find(fl,"statue") then dice=3 elseif string.find(fl,"metal") or string.find(fl,"iron") then dice=4 elseif string.find(fl,"crystal") then dice=4;radius=2 end end if dice>0 and not has_friendly(x,y,radius) then local val=0 local dmg_per=4+pow/5 for _,e2 in ipairs(t) do local dist=math.max(math.abs(e2.x-x),math.abs(e2.y-y)) if dist<=radius and (e2.x~=0 or e2.y~=0) then local ac=get_ac(e2.m) local base=dice*(dmg_per+1)/2 local dmg=calc_dmg(base,ac,0,pow,false,true,true) val=val+score_dmg(dmg,e2.m,cdist(e2.x,e2.y)) end end if val>best_dmg then best_dmg=val;best_tx=x;best_ty=y end end end end end best_val=best_dmg else local aoe_centers={} for _,e in ipairs(t) do table.insert(aoe_centers,{x=e.x,y=e.y}) end if sp.aoe and sp.aoe>0 then local p_res=player_res(sp.e) -- Fireball/Fire Storm: can cast on self if rF++ if (sp.n=="Fireball" or sp.n=="Fire Storm") and p_res>=2 then table.insert(aoe_centers,{x=0,y=0}) end -- For any_target spells (Fireball), also add empty cells as potential targets if sp.any_target then for dx=-sp.aoe,sp.aoe do for dy=-sp.aoe,sp.aoe do if not (dx==0 and dy==0) then local empty=true for _,e2 in ipairs(t) do if e2.x==dx and e2.y==dy then empty=false break end end if empty and view.cell_see_cell(0,0,dx,dy) then local exists=false for _,c in ipairs(aoe_centers) do if c.x==dx and c.y==dy then exists=true break end end if not exists then table.insert(aoe_centers,{x=dx,y=dy}) end end end end end end for _,e in ipairs(t) do for dx=-sp.aoe,sp.aoe do for dy=-sp.aoe,sp.aoe do if not (dx==0 and dy==0) then local empty=true for _,e2 in ipairs(t) do if e2.x==e.x+dx and e2.y==e.y+dy then empty=false break end end if empty and view.cell_see_cell(0,0,e.x+dx,e.y+dy) then local exists=false for _,c in ipairs(aoe_centers) do if c.x==e.x+dx and c.y==e.y+dy then exists=true break end end if not exists then table.insert(aoe_centers,{x=e.x+dx,y=e.y+dy}) end end end end end end end for _,center in ipairs(aoe_centers) do -- Prism: must target empty non-solid non-self tile local prism_ok=true if sp.prism then if (center.x==0 and center.y==0) or is_solid(center.x,center.y) or monster.get_monster_at(center.x,center.y) then prism_ok=false end end if prism_ok and not has_friendly(center.x,center.y,sp.aoe) then local val=0 for _,e2 in ipairs(t) do local dist=math.max(math.abs(e2.x-center.x),math.abs(e2.y-center.y)) local adj_dist=math.max(math.abs(e2.x),math.abs(e2.y)) if dist<=sp.aoe and (not sp.noadjacent or adj_dist>1) then local r=get_res(e2.m,sp.e) local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0 elseif r>=3 then mult=0.0 end if sp.undead and e2.m:holiness()~="undead" then mult=0 end if sp.n=="Ignite Poison" then local ok,st=pcall(function() return e2.m:status("poisoned") end) if not (ok and st) then mult=0 end end if mult>0 then local base=get_spell_dmg(sp.n,pow) if sp.n=="Shatter" then if e2.m:holiness()=="nonliving" or e2.m:flags("petrified") or e2.m:flags("petrifying") then base=base*2 elseif e2.m:flags("airborne") then base=base/3 end end local ac=get_ac(e2.m) local ev=get_ev(e2.m) local dmg if sp.halfres then local dmg1=calc_dmg(base*0.5*mult,ac,ev,pow,sp.noac,sp.nohit,sp.ac3) local dmg2=calc_dmg(base*0.5,ac,ev,pow,sp.noac,sp.nohit,sp.ac3) dmg=dmg1+dmg2 else dmg=calc_dmg(base*mult,ac,ev,pow,sp.noac,sp.nohit,sp.ac3) end dmg=sh_damage(dmg,e2.m) val=val+score_dmg(dmg,e2.m,cdist(e2.x,e2.y)) end end end -- Prism: discount for 2-turn delay (enemies may move away) if sp.prism then val=val*0.5 end if val>total_dmg then total_dmg=val;best_tx=center.x;best_ty=center.y end end end best_val=total_dmg end elseif sp.auto_target then -- Auto-target spells: Plasma Beam (farthest), Permafrost (largest cluster), Ozocubu (all in LOS) if sp.n=="Plasma Beam" then -- Plasma Beam: targets farthest enemy, hits all on line local farthest_dist=0 local farthest_e=nil for _,e in ipairs(t) do local dist=math.max(math.abs(e.x),math.abs(e.y)) if dist>farthest_dist then farthest_dist=dist;farthest_e=e end end if farthest_e then local dmg=0 local others=monsters_on_line(farthest_e.x,farthest_e.y) for _,target in ipairs(others) do local r=get_res(target.m,sp.e) local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0 elseif r>=3 then mult=0.0 end if mult>0 then local base=get_spell_dmg(sp.n,pow) local ac=get_ac(target.m) local ev=get_ev(target.m) local tdmg=calc_dmg(base*mult,ac,ev,pow,sp.noac,sp.nohit,sp.ac3) dmg=dmg+tdmg end end -- Add the farthest target itself local r=get_res(farthest_e.m,sp.e) local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0 elseif r>=3 then mult=0.0 end if mult>0 then local base=get_spell_dmg(sp.n,pow) local ac=get_ac(farthest_e.m) local ev=get_ev(farthest_e.m) local tdmg=calc_dmg(base*mult,ac,ev,pow,sp.noac,sp.nohit,sp.ac3) dmg=dmg+tdmg end local hp=get_hp(farthest_e.m) if dmg>0 then local kills_needed=math.ceil(hp/dmg) if kills_needed<=4 then local val=100/kills_needed best_val=val;best_tx=farthest_e.x;best_ty=farthest_e.y end end end elseif sp.n=="Permafrost Eruption" then -- Permafrost Eruption: target largest cluster of adjacent enemies local best_cluster=0 local best_target=nil for _,e in ipairs(t) do local cluster_size=0 for _,e2 in ipairs(t) do if math.abs(e2.x-e.x)<=1 and math.abs(e2.y-e.y)<=1 then cluster_size=cluster_size+1 end end if cluster_size>best_cluster then best_cluster=cluster_size best_target=e end end if best_target then local dmg=0 for _,e2 in ipairs(t) do if math.abs(e2.x-best_target.x)<=1 and math.abs(e2.y-best_target.y)<=1 then local r=get_res(e2.m,sp.e) local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0 elseif r>=3 then mult=0.0 end if mult>0 then local base=get_spell_dmg(sp.n,pow) local ac=get_ac(e2.m) local ev=get_ev(e2.m) local tdmg=calc_dmg(base*mult,ac,ev,pow,sp.noac,sp.nohit,sp.ac3) dmg=dmg+tdmg end end end if dmg>0 then local kills_needed=math.ceil(20/dmg) -- assume avg 20hp if kills_needed<=4 then local val=100/kills_needed best_val=val;best_tx=best_target.x;best_ty=best_target.y end end end elseif sp.n=="Ozocubu's Refrigeration" then -- Ozocubu's Refrigeration: hits all in LOS, damage reduced by adjacent enemies (max 2) local total_dmg=0 for _,e in ipairs(t) do local adj_count=0 for _,e2 in ipairs(t) do if e2~=e and math.abs(e2.x-e.x)<=1 and math.abs(e2.y-e.y)<=1 then adj_count=adj_count+1 end end local reduction=math.min(adj_count,2)*0.3 local r=get_res(e.m,sp.e) local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0 elseif r>=3 then mult=0.0 end if mult>0 then local base=get_spell_dmg(sp.n,pow) local ac=get_ac(e.m) local ev=get_ev(e.m) local tdmg=calc_dmg(base*mult*(1-reduction),ac,ev,pow,sp.noac,sp.nohit,sp.ac3) total_dmg=total_dmg+tdmg end end if total_dmg>0 then local kills_needed=math.ceil(20*#t/total_dmg) if kills_needed<=4 then local val=100/kills_needed best_val=val;best_tx=0;best_ty=0 end end end elseif sp.starburst then -- Starburst: 8 rays from player in cardinal+diagonal directions local base=get_spell_dmg(sp.n,pow) local val=0 local dirs={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}} for _,d in ipairs(dirs) do for R=1,range do local rx,ry=d[1]*R,d[2]*R if is_solid(rx,ry) then break end local m2=monster.get_monster_at(rx,ry) if m2 and m2:attitude()==0 and not m2:is_firewood() then local r=get_res(m2,sp.e) local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0 elseif r>=3 then mult=0 end if mult>0 then local ac=get_ac(m2) local ev=get_ev(m2) local dmg=calc_dmg(base*mult,ac,ev,pow,sp.noac,sp.nohit,sp.ac3) dmg=sh_damage(dmg,m2) val=val+score_dmg(dmg,m2,cdist(rx,ry)) end end end end if val>0 then best_val=val;best_tx=0;best_ty=0 end elseif sp.magnavolt then -- Magnavolt: collect magnetised targets + clouds, then find best new target local mag_targets={} for x=-los,los do for y=-los,los do local m2=monster.get_monster_at(x,y) if m2 and m2:attitude()==0 and not m2:is_firewood() then local ok,st=pcall(function() return m2:status("covered in magnetic dust") end) if ok and st then mag_targets[#mag_targets+1]={x=x,y=y,m=m2} end end local ok2,cl=pcall(function() return view.cloud_at(x,y) end) if ok2 and cl=="magnetised fragments" then -- Check not already added as monster target local found=false for _,mt in ipairs(mag_targets) do if mt.x==x and mt.y==y then found=true break end end if not found then mag_targets[#mag_targets+1]={x=x,y=y,cloud=true} end end end end -- Calculate damage for a bolt aimed at (tx,ty): hits target + all enemies on path local base=get_spell_dmg(sp.n,pow) local function bolt_dmg_to_mon(m2,mx,my) local r=get_res(m2,sp.e) local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0 elseif r>=3 then mult=0 end if mult==0 then return 0 end local ac=get_ac(m2) local dmg=calc_dmg(base*mult,ac,0,pow,sp.noac,true,sp.ac3) dmg=sh_damage(dmg,m2) return score_dmg(dmg,m2,cdist(mx,my)) end local function mag_bolt_val(tx,ty) -- Bolt to (tx,ty) penetrates through all enemies on the path local val=0 local path=spells.path(sp.n,tx,ty) if path then for _,p in ipairs(path) do local pm=monster.get_monster_at(p[1],p[2]) if pm and pm:attitude()==0 and not pm:is_firewood() then val=val+bolt_dmg_to_mon(pm,p[1],p[2]) end end end -- Add the endpoint target itself (may not be in path) local tm=monster.get_monster_at(tx,ty) if tm and tm:attitude()==0 and not tm:is_firewood() then -- Check not already counted via path local counted=false if path then for _,p in ipairs(path) do if p[1]==tx and p[2]==ty then counted=true break end end end if not counted then val=val+bolt_dmg_to_mon(tm,tx,ty) end end return val end -- Existing magnetised damage (bolts to all current targets, each penetrating) local existing_val=0 for _,mt in ipairs(mag_targets) do existing_val=existing_val+mag_bolt_val(mt.x,mt.y) end -- Try each non-magnetised enemy as new target for _,e in ipairs(t) do if can_reach(e.x,e.y,range) then local already_mag=false for _,mt in ipairs(mag_targets) do if mt.x==e.x and mt.y==e.y then already_mag=true break end end -- New target gets bolt (penetrating) + all existing magnetised get bolts too local new_bolt=mag_bolt_val(e.x,e.y) local val=existing_val+new_bolt -- Ramp-up bonus: magnetising targets builds future value -- Even vs 1 target, the noise draws enemies who arrive pre-magnetised -- 1.5x base bonus (investing in future stacking) + 0.3 per existing mag target val=val*(1.5+#mag_targets*0.3) if val>best_val then best_val=val;best_tx=e.x;best_ty=e.y end end end -- If all enemies already magnetised, pick highest-damage one as target if best_val==0 and #mag_targets>0 then for _,mt in ipairs(mag_targets) do if not mt.cloud and can_reach(mt.x,mt.y,range) then local val=existing_val if val>best_val then best_val=val;best_tx=mt.x;best_ty=mt.y end end end end elseif sp.clutch then -- Borgnjor's Vile Clutch: bolt constricts all enemies on path -- Score = sum of constriction DoT value for each non-constricted enemy hit -- Prioritize paths that constrict the most dangerous, closest enemies -- Duration ~4+pow/35 to pow/25+1 turns; use conservative avg ~4 turns local constr_turns=4+math.floor(pow/80) local constr_dmg=get_spell_dmg(sp.n,pow)*constr_turns -- Try each enemy as a bolt endpoint; score all enemies on the path for _,e in ipairs(t) do if can_reach(e.x,e.y,range) and not beam_blocked_by_friendly(sp.n,e.x,e.y) then local val=0 local path=spells.path(sp.n,e.x,e.y) local hits={} if path then for _,p in ipairs(path) do local pm=monster.get_monster_at(p[1],p[2]) if pm and pm:attitude()==0 and not pm:is_firewood() then hits[#hits+1]={m=pm,x=p[1],y=p[2]} end end end -- Add endpoint if not already in path local has_end=false if path then for _,p in ipairs(path) do if p[1]==e.x and p[2]==e.y then has_end=true break end end end if not has_end then hits[#hits+1]={m=e.m,x=e.x,y=e.y} end -- Score each hit: skip already constricted (check target_desc for "onstrict") for _,h in ipairs(hits) do local hdesc=h.m:target_desc() or "" if not string.find(hdesc,"onstrict") then local ac=get_ac(h.m) local dmg=calc_dmg(constr_dmg,ac,0,pow,false,true,false) val=val+score_dmg(dmg,h.m,cdist(h.x,h.y)) end end if val>best_val then best_val=val;best_tx=e.x;best_ty=e.y end end end elseif sp.fcloud then -- Freezing Cloud: smite-targeted cloud, radius 2 -- Score by enemies covered + corridor blocking bonus -- Safe to self-cast at rC+3 local cloud_turns=3 local cloud_dmg=get_spell_dmg(sp.n,pow)*cloud_turns local cloud_r=2 local player_immune=(you.res_cold()>=3) -- Gather candidate cloud centers: on enemies, between player and enemies, and on player local centers={} for _,e in ipairs(t) do centers[#centers+1]={x=e.x,y=e.y} -- Also try midpoint between player and enemy (corridor blocking) local mx=math.floor(e.x/2+0.5) local my=math.floor(e.y/2+0.5) if (mx~=0 or my~=0) and not is_solid(mx,my) and cdist(mx,my)<=range then centers[#centers+1]={x=mx,y=my} end end if player_immune then centers[#centers+1]={x=0,y=0} end -- Score each center for _,c in ipairs(centers) do if cdist(c.x,c.y)<=range and not is_solid(c.x,c.y) and (player_immune or cdist(c.x,c.y)>cloud_r) and not has_friendly(c.x,c.y,cloud_r) then local val=0 -- Count enemies in cloud radius, skip those already in freezing cloud for _,e2 in ipairs(t) do local d=math.max(math.abs(e2.x-c.x),math.abs(e2.y-c.y)) local cl=view.cloud_at(e2.x,e2.y) local in_fcloud=cl and string.find(cl,"freezing") if d<=cloud_r and not in_fcloud then local r=get_res(e2.m,sp.e) if r<3 then -- rC+3 immune local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0.25 end local dmg=cloud_dmg*mult val=val+score_dmg(dmg,e2.m,cdist(e2.x,e2.y)) end end end -- Corridor bonus: count solid tiles adjacent to cloud center -- More walls = narrower passage = better zone control local walls=0 for dx=-1,1 do for dy=-1,1 do if (dx~=0 or dy~=0) and is_solid(c.x+dx,c.y+dy) then walls=walls+1 end end end if walls>=2 then val=val*(1+walls*0.15) end if val>best_val then best_val=val;best_tx=c.x;best_ty=c.y end end end elseif sp.mortar then -- Hellfire Mortar: bolt path creates lava (blocks non-flying), summons mortar -- Score = enemies on path damage + lava blocking bonus for nearby non-flying enemies -- Mortar fires ~path_len*1.5 turns; use conservative 3 shots estimate local shots=3 local base_dmg=get_spell_dmg(sp.n,pow)*shots for _,e in ipairs(t) do if can_reach(e.x,e.y,range) and not beam_blocked_by_friendly(sp.n,e.x,e.y) then local val=0 local path=spells.path(sp.n,e.x,e.y) local path_tiles={} if path then for _,p in ipairs(path) do path_tiles[#path_tiles+1]={x=p[1],y=p[2]} -- Score enemies on the path (mortar will fire at them) local pm=monster.get_monster_at(p[1],p[2]) if pm and pm:attitude()==0 and not pm:is_firewood() then local r=get_res(pm,sp.e) local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0.25 elseif r>=3 then mult=0 end if sp.halfres then mult=0.5+mult*0.5 end if mult>0 then local ac=get_ac(pm) local ev=get_ev(pm) local dmg=calc_dmg(base_dmg*mult,ac,ev,pow,false,false,false) val=val+score_dmg(dmg,pm,cdist(p[1],p[2])) end end end end -- Also score endpoint enemy local has_end=false if path then for _,p in ipairs(path) do if p[1]==e.x and p[2]==e.y then has_end=true break end end end if not has_end then local r=get_res(e.m,sp.e) local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0.25 elseif r>=3 then mult=0 end if sp.halfres then mult=0.5+mult*0.5 end if mult>0 then local ac=get_ac(e.m) local ev=get_ev(e.m) local dmg=calc_dmg(base_dmg*mult,ac,ev,pow,false,false,false) val=val+score_dmg(dmg,e.m,cdist(e.x,e.y)) end end -- Lava blocking bonus: count nearby non-path enemies that would be blocked -- Lava blocks ground movement, so non-flying enemies near path get bonus if #path_tiles>=2 then local lava_bonus=0 for _,e2 in ipairs(t) do -- Check if enemy is adjacent to lava path (would be blocked) for _,pt in ipairs(path_tiles) do local d=math.max(math.abs(e2.x-pt.x),math.abs(e2.y-pt.y)) if d<=1 then -- Non-flying enemies blocked by lava get bonus local hdesc=e2.m:target_desc() or "" local flying=string.find(hdesc,"lying") or string.find(hdesc,"evitat") if not flying then lava_bonus=lava_bonus+0.3 end break end end end val=val*(1+lava_bonus) end -- Corridor bonus: lava in narrow passages is more valuable local walls=0 for _,pt in ipairs(path_tiles) do for dx=-1,1 do for dy=-1,1 do if (dx~=0 or dy~=0) and is_solid(pt.x+dx,pt.y+dy) then walls=walls+1 end end end end if #path_tiles>0 then walls=walls/#path_tiles end if walls>=3 then val=val*1.5 elseif walls>=2 then val=val*1.2 end if val>best_val then best_val=val;best_tx=e.x;best_ty=e.y end end end elseif sp.ood then -- Orb of Destruction: distance scaling, obstacle check, adjacent orb check -- Don't fire if there's already an OoD adjacent to player (collision risk) local adj_ood=false for dx=-1,1 do for dy=-1,1 do local m2=monster.get_monster_at(dx,dy) if m2 and m2:name()=="orb of destruction" and m2:attitude()>0 then adj_ood=true end end end if not adj_ood then local base_M=5+pow/12 for _,e in ipairs(t) do if can_reach(e.x,e.y,range) then local dist=math.max(math.abs(e.x),math.abs(e.y)) -- Enemy likely steps toward player before orb arrives if dist>3 then dist=dist-1 end local scale=1.0 if dist<4 then scale=dist*0.3 end local ac=get_ac(e.m) local dmg=calc_dmg(9*(base_M*scale+1)/2,ac,0,pow,false,true,false) dmg=sh_damage(dmg,e.m) -- Check path for obstacles (walls, firewood, other monsters blocking) local blocked=false local path=spells.path(sp.n,e.x,e.y) if path then for _,p in ipairs(path) do if not (p[1]==e.x and p[2]==e.y) then if is_solid(p[1],p[2]) then blocked=true break end local pm=monster.get_monster_at(p[1],p[2]) if pm and pm:is_firewood() then blocked=true break end if pm and pm:attitude()>0 and pm:name()~="battlesphere" and pm:name()~="orb of destruction" then blocked=true break end end end end if not blocked and dmg>0 then local val=score_dmg(dmg,e.m,cdist(e.x,e.y)) if val>best_val then best_val=val;best_tx=e.x;best_ty=e.y end end end end end else for _,e in ipairs(t) do if can_reach(e.x,e.y,range) and not ((sp.beam or sp.penetrate) and beam_blocked_by_friendly(sp.n,e.x,e.y)) then local r=get_res(e.m,sp.e) local mult=1.0 if r==-1 then mult=1.5 elseif r==1 then mult=0.5 elseif r==2 then mult=0 elseif r>=3 then mult=0.0 end if sp.undead and e.m:holiness()~="undead" then mult=0 end local hp=get_hp(e.m) local base=get_spell_dmg(sp.n,pow) if sp.n=="Airstrike" then local empty=count_empty(e.x,e.y) base=base+empty*2 end local ac=get_ac(e.m) local ev=get_ev(e.m) -- Use game API evasion if available local hit_pct=evasion_check(e.m,sp.n) local use_ev=ev if hit_pct and not sp.nohit then use_ev=0 end -- will apply hit_pct separately local dmg if sp.halfres then local dmg1=calc_dmg(base*0.5*mult,ac,use_ev,pow,sp.noac,sp.nohit,sp.ac3) local dmg2=calc_dmg(base*0.5,ac,use_ev,pow,sp.noac,sp.nohit,sp.ac3) dmg=dmg1+dmg2 else dmg=calc_dmg(base*mult,ac,use_ev,pow,sp.noac,sp.nohit,sp.ac3) end if hit_pct and not sp.nohit then dmg=dmg*hit_pct end dmg=sh_damage(dmg,e.m) if sp.beam and sp.n=="Shock" and near_wall(e.x,e.y) then dmg=dmg*2 end if sp.n=="Iskenderun's Mystic Blast" then -- knockback: push enemy up to 2+pow/50 tiles away from player -- collision with wall or another monster deals 2d(1+pow/10) extra local kd=2+pow/50 local dx=e.x>0 and 1 or (e.x<0 and -1 or 0) local dy=e.y>0 and 1 or (e.y<0 and -1 or 0) local kx,ky=e.x,e.y local col_dmg=get_spell_dmg("IMB_collision",pow) for _=1,math.floor(kd) do local nx,ny=kx+dx,ky+dy if is_solid(nx,ny) then dmg=dmg+calc_dmg(col_dmg,get_ac(e.m),0,pow,false,true) break end local om=monster.get_monster_at(nx,ny) if om and om:attitude()==0 and not om:is_firewood() then local cd=calc_dmg(col_dmg,get_ac(e.m),0,pow,false,true) dmg=dmg+cd local cd2=calc_dmg(col_dmg,get_ac(om),0,pow,false,true) dmg=dmg+cd2 break end kx=nx;ky=ny end end if sp.penetrate then -- Bolt reach probability: each enemy on path reduces chance of reaching the next local reachChance=1.0 local others=monsters_on_path(sp.n,e.x,e.y) for _,other in ipairs(others) do local r2=get_res(other.m,sp.e) local mult2=1.0 if r2==-1 then mult2=1.5 elseif r2==1 then mult2=0.5 elseif r2==2 then mult2=0 elseif r2>=3 then mult2=0.0 end if mult2>0 then local base2=get_spell_dmg(sp.n,pow) local ac2=get_ac(other.m) local ev2=get_ev(other.m) local hitChance2=evasion_check(other.m,sp.n) or math.max(0,1.0-ev2/50.0) local dmg2=calc_dmg(base2*mult2,ac2,0,pow,sp.noac,true,sp.ac3)*hitChance2*reachChance dmg2=sh_damage(dmg2,other.m) dmg=dmg+dmg2 reachChance=reachChance*(1-hitChance2) end end end if dmg>0 then local val=score_dmg(dmg,e.m,cdist(e.x,e.y)) if sp.penetrate then local reachChance2=1.0 local others=monsters_on_path(sp.n,e.x,e.y) for _,other in ipairs(others) do local r2=get_res(other.m,sp.e) local m2=1.0 if r2==-1 then m2=1.5 elseif r2==1 then m2=0.5 elseif r2==2 then m2=0 elseif r2>=3 then m2=0.0 end if m2>0 then local b2=get_spell_dmg(sp.n,pow) local hitC=evasion_check(other.m,sp.n) or math.max(0,1.0-get_ev(other.m)/50.0) local d2=calc_dmg(b2*m2,get_ac(other.m),0,pow,sp.noac,true,sp.ac3)*hitC*reachChance2 d2=sh_damage(d2,other.m) val=val+score_dmg(d2,other.m,cdist(other.x,other.y)) reachChance2=reachChance2*(1-hitC) end end end if val>best_val then best_val=val;best_tx=e.x;best_ty=e.y end end end end end if best_val>0 and cost<=mp then local penalty=1.0 if sp.channeled and sp.n=="Flame Wave" then penalty=0.33 end if sp.slow then penalty=penalty*0.67 end -- 1.5 turn cast -- Add battlesphere bonus damage for triggering spells if has_bsph and not sp.no_bsph then best_val=best_val+bsph_bonus end local eff_val=best_val*penalty/math.sqrt(cost) table.insert(candidates,{n=sp.n,v=eff_val,x=best_tx,y=best_ty,c=cost,ch=sp.channeled,manual=sp.mortar}) end end end end table.sort(candidates,function(a,b) return a.v>b.v end) local b=candidates[1] -- If top candidate is channeled but not significantly better than non-channeled, prefer non-channeled if b and b.ch and #candidates>1 and b.n=="Flame Wave" then local best_non_ch=nil for _,c in ipairs(candidates) do if not c.ch then best_non_ch=c;break end end if best_non_ch and b.v < best_non_ch.v * 1.2 then b=best_non_ch end end -- If already channeling, don't override unless significantly better (2x) if recently_cast("Flame Wave") or recently_cast("Searing Ray") then -- If best option is the channeled spell itself, just continue if b and (b.n == "Flame Wave" or b.n == "Searing Ray") then crawl.mpr("Cont") return end -- Check if any option is significantly better (2x) local has_better = false for _,c in ipairs(candidates) do if c.v > 20 * 2 then -- assume current channel does 20 dmg/turn has_better = true break end end if not has_better then crawl.mpr("Cont") return -- don't interrupt channeling end end if not b then for _,c in ipairs(candidates) do local cost_diff=mp-c.c if cost_diff>=0 or cost_diff>=-1 then b=c break end end end if b then mark_cast(b.n) crawl.mpr(b.n) if b.manual then spells.cast(b.n) else spells.cast(b.n,b.x,b.y,true) end else crawl.mpr("No") end end }