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,mp_max=_G.you.mp() local mp_regen=math.max(1,math.floor((mp_max or 20)/10)) -- Djinni uses HP as MP if you.race()=="Djinni" then mp=you.hp()-10 end -- reserve 10 HP -- Reserve MP for escape spells (Blink, Passage of Golubria, Dispersal, Disjunction) local escape_spells={"Blink","Passage of Golubria","Dispersal","Disjunction"} local reserve_mp=0 local reserve_spell=nil for _,esc in ipairs(escape_spells) do if spells.memorised(esc) then local c=spells.mana_cost(esc) if c and c>reserve_mp then reserve_mp=c;reserve_spell=esc end end end local los=you.los() local t={} local friends={} local found_bsph=false local found_servitor=false local found_star=false local found_mortar=false 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 then if m:name()=="battlesphere" then found_bsph=true elseif m:name()=="spellspark servitor" then found_servitor=true elseif string.find(m:name(),"shooting star") then found_star=true;friends[#friends+1]={x=x,y=y} elseif m:name()~="orb of destruction" then friends[#friends+1]={x=x,y=y} end end end if m and string.find(m:name() or "","mortar") then found_mortar=true end end end if #t==0 then crawl.mpr("No")return end local function get_ac(m) local pips=m:ac() if pips==0 then return 0 end -- Use exact AC from lookup table if pip count matches local known=monster_ac[m:name()] if known and known>=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 local ev=2.5+(pips-1)*5 -- Status effects that modify EV (source: monster.cc:3345-3360) if m:status("paralysed") or m:status("petrified") or m:status("petrifying") or m:status("asleep") or m:status("covered in magnetic dust") then return 0 end if m:is_caught() then return math.max(0,ev/5) end if m:status("confused") then return math.max(0,ev/2) end if m:is_constricted() then ev=ev-10 end return math.max(0,ev) 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 hp_frac={[0]=1.0,[1]=0.9,[2]=0.7,[3]=0.5,[4]=0.3,[5]=0.1,[6]=0} local function get_hp(m) local mhp=get_max_hp(m) local dl=m:damage_level() return math.max(1, mhp*(hp_frac[dl] or 0.5)) end local closest_dist=99 local enemy_at={} for _,e in ipairs(t) do local d=math.max(math.abs(e.x),math.abs(e.y)) if d0) e.is_regen=e.m:status("regenerating") e.mon_range=e.m:range() or 0 end -- Pre-compute adjacency counts for each enemy (O(n) via enemy_at hash) for _,e in ipairs(t) do e.adj_enemies=0 for dx=-1,1 do for dy=-1,1 do if not (dx==0 and dy==0) then local nb=enemy_at[(e.x+dx)..","..((e.y+dy))] if nb then e.adj_enemies=e.adj_enemies+1 end end end end end local n_poisoned=0 if spells.memorised("Ignite Poison") then for _,e in ipairs(t) do if e.m:status("poisoned") then n_poisoned=n_poisoned+1 end end end -- Fast AC/EV lookup: use cached enemy entry if available, else compute local function fast_ac(m,x,y) local e=enemy_at[x..","..y] if e then return e.ac end return get_ac(m) end local function fast_ev(m,x,y) local e=enemy_at[x..","..y] if e then return e.ev end return get_ev(m) 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 -- LRD: determine dice, radius, and ice flag for a monster target (by name) -- Returns dice,radius,is_ice or 0,0,false if not fraggable local function lrd_dice_mon(m) local mn=string.lower(m:name()) -- Metal monsters (4 dice) — check before rock due to war gargoyle/gargoyle overlap local metal={"iron golem","iron elemental","peacekeeper","war gargoyle","clockwork bee", "lightning spire","blazeheart golem","walking alembic","monarch bomb","bomblet", "phalanx beetle","spellspark servitor","platinum paragon","crawling flesh cage"} for _,p in ipairs(metal) do if string.find(mn,p) then return 4,1,false end end -- Crystal monsters (4 dice, radius 2) local crystal={"crystal guardian","crystal echidna","orange statue","obsidian bat", "obsidian statue","diamond sawblade","roxanne","glass eye","screaming refraction"} for _,p in ipairs(crystal) do if string.find(mn,p) then return 4,2,false end end -- Rock/bone monsters (3 dice) local rock={"toenail golem","saltling","pillar of salt","pile of debris","petrified flower", "earth elemental","mountainshell","rockslime","boulder","ushabti","statue","gargoyle", "rock fish","hellfire mortar"} for _,p in ipairs(rock) do if string.find(mn,p) and mn~="molten gargoyle" and mn~="war gargoyle" then return 3,1,false end end -- Ice monsters (3 dice, cold damage) local ice={"ice beast","simulacr","ice statue","block of ice","nargun", "hoarfrost cannon","pillar of rime","splinterfrost barricade"} for _,p in ipairs(ice) do if string.find(mn,p) then return 3,1,true end end -- Skeletal monsters (3 dice) local skel={"draugr","bone dragon","skeletal warrior","ancient champion","revenant", "weeping skull","laughing skull","curse skull","marrowcuda","murray","nameless revenant"} for _,p in ipairs(skel) do if string.find(mn,p) then return 3,1,false end end -- Petrified/petrifying monsters if m:status("petrified") or m:status("petrifying") then return 3,1,false end return 0,0,false end -- LRD: determine dice and radius for terrain -- Returns dice,radius or 0,0 if not fraggable local function lrd_dice_terrain(f) local fl=string.lower(f or "") -- Exclude stairs early (stone_stairs contains "stone") if string.find(fl,"stair") then return 0,0 end -- Rock/stone terrain (3 dice) if string.find(fl,"rock_wall") or string.find(fl,"stone_wall") or string.find(fl,"slimy_wall") or string.find(fl,"door") or string.find(fl,"stone_arch") or string.find(fl,"granite_statue") or string.find(fl,"orcish_idol") or string.find(fl,"petrified_tree") or string.find(fl,"spike_launcher") then return 3,1 end -- Metal terrain (4 dice) if string.find(fl,"metal") or string.find(fl,"iron") or string.find(fl,"grate") then return 4,1 end -- Crystal terrain (4 dice, radius 2) if string.find(fl,"crystal") then return 4,2 end return 0,0 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 -- Channeled spells: active for up to 3 turns after initial cast if name=="Flame Wave" or name=="Searing Ray" then return (turn-lt)<=3 end -- Persistent summons: always active once cast if name=="Iskenderun's Battlesphere" or name=="Conjure Ball Lightning" then return true end return false end -- Mark spell as cast local function mark_cast(name) last_cast_turn[name]=turn end local function cdist(x,y) return math.max(math.abs(x),math.abs(y)) end local function score_dmg(dmg,m,dist,e) -- e: optional pre-cached enemy entry from t[] local hp=e and e.hp or get_hp(m) local mhp=e and e.mhp or get_max_hp(m) local ok_th,th=pcall(function() return m:threat() end) local threat=(ok_th and th or 0)+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 -- Ranged threat: monsters already in attack range are more urgent if e then if e.mon_range>1 and dist<=e.mon_range then urgency=urgency*1.3 end else local mon_rng=m:range() or 0 if mon_rng>1 and dist<=mon_rng then urgency=urgency*1.3 end end -- Base score: effective damage weighted by threat, finish, urgency, HP ratio local score=effective*finish*urgency*threat/mhp if e then if e.is_summoned then score=score*0.2 end if e.is_safe then score=score*0.1 end if e.is_stationary then score=score*0.4 end if e.is_unique then score=score*1.3 end if e.is_caster then score=score*1.15 end if e.is_regen then score=score*1.3 end else -- Fallback: slow path for non-cached monsters local desc=m:target_desc() or "" if string.find(desc,"summoned") then score=score*0.2 end if m:is_safe() then score=score*0.1 end if m:is_stationary() then score=score*0.4 end if m:is_unique() then score=score*1.3 end local mon_sp=m:spells() if mon_sp and #mon_sp>0 then score=score*1.15 end if m:status("regenerating") then score=score*1.3 end end return score end -- Power caps: all non-200 caps listed; unlisted spells default to 200 (verified against spl-data.h 0.34.0) local pow_cap={["Foxfire"]=25,["Freeze"]=25,["Magic Dart"]=25,["Poisonous Vapours"]=25,["Shock"]=25, ["Sandblast"]=50,["Mercury Arrow"]=50,["Scorch"]=50,["Searing Ray"]=50,["Static Discharge"]=50,["Frozen Ramparts"]=50,["Stone Arrow"]=50, ["Hailstorm"]=100,["Dispel Undead"]=100,["Flame Wave"]=100,["Iskenderun's Battlesphere"]=100,["Iskenderun's Mystic Blast"]=100, ["Ignite Poison"]=100,["Olgreb's Toxic Radiance"]=100,["Sticky Flame"]=100,["Brom's Barrelling Boulder"]=100} local function get_pow(name) local p=spells.power_perc(name) if not p then return 0 end local cap=pow_cap[name] or 200 return p*cap/100 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,halfac) 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 -- halfac: electricity gets ac_type::half (saved = random2(1+ac)/2, avg=ac/4) -- ac3: BEAM_FRAG gets ac_type::triple (saved = 3*random2(1+ac), avg=3*ac/2) -- source: actor.cc apply_ac — single subtraction from damage local saved=ac/2 -- normal: avg of random2(1+ac) if ac3 then saved=3*ac/2 -- triple: 3 independent random2(1+ac) summed elseif halfac then saved=ac/4 end -- half: random2(1+ac)/2 dmg=math.max(0,dmg-saved) 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 local function spell_dmg(base,mult,ac,ev,pow,sp,elec,nohit_ov) local nh=nohit_ov~=nil and nohit_ov or sp.nohit if sp.halfres then return calc_dmg(base*0.5*mult,ac,ev,pow,sp.noac,nh,sp.ac3,elec) +calc_dmg(base*0.5,ac,ev,pow,sp.noac,nh,sp.ac3,elec) end return calc_dmg(base*mult,ac,ev,pow,sp.noac,nh,sp.ac3,elec) 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 local function get_hit_chance(m,spellname,ev) return evasion_check(m,spellname) or math.max(0,1.0-(ev or get_ev(m))/50.0) 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*(5.5+pow/8+1)/2 end -- calcdice<2,11,1,4> = 2d(5.5+pow/8) 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*(4.5+pow/14+1)/2 end -- calcdice<2,9,1,7> = 2d(4.5+pow/14) 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*(5+pow/9+1)/2 end -- 3d(5+pow/9) calcdice<3,15,1,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*(5+7*pow/40+1)/2 end -- 3d(5+7pow/40) irresistible; hd=pow/10, 3d(5+hd*7/4) if name=="Iskenderun's Battlesphere" then return 2*(7+pow/9+1)/2 end -- 2d(6+hd) where hd=1+pow/9 → 2d(7+pow/9) if name=="Iskenderun's Mystic Blast" then return 2*(3+pow/6+1)/2 end -- calcdice<2,6,1,3> = 2d(3+pow/6) + 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=="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.5+3*pow/32+1)/2 end -- 2x 4d(1.5+3pow/32) calcdice<4,6,3,8> 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 -- Maxwell's Capacitive Coupling: scored specially (auto_target, channeled 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_res_mult(r,halfres) 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 halfres then mult=0.5+mult*0.5 end return mult 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) if not f then return false end 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" or f=="mangrove" or f=="slimy_wall" 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 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) if not m and not is_solid(x+dx,y+dy) 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 -- Shock bounce: get full path with ricochets (aimed_at_spot=false) -- Returns {hit_counts, enemies} where hit_counts[key]=count (max 2), enemies[key]={m,x,y} local shock_cache={} local function shock_bounce_hits(tx,ty,pow) local ck=tx..","..ty if shock_cache[ck] then return shock_cache[ck][1],shock_cache[ck][2] end local path=spells.path("Shock",tx,ty,0,0,false) if not path then shock_cache[ck]={{},{}} return {},{} end local hit_counts={} local enemies={} for _,p in ipairs(path) do local m=monster.get_monster_at(p[1],p[2]) if m and m:attitude()==0 and not m:is_firewood() then local key=p[1]..","..p[2] if not hit_counts[key] then hit_counts[key]=0;enemies[key]={m=m,x=p[1],y=p[2]} end if hit_counts[key]<2 then hit_counts[key]=hit_counts[key]+1 end end end shock_cache[ck]={hit_counts,enemies} return hit_counts,enemies 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,rpois_immune=true -- poison, range 3, rPois>=1 blocks cast },{n="Sandblast",e=0,ac3=true,slow=true -- physical, triple AC, 1.5 turns },{n="Shock",e=2,beam=true,penetrate=true,any_target=true -- elec, bouncing bolt, 1/2 AC via halfac -- Level 2 },{n="Mercury Arrow",e=4 -- poison+30% irresist },{n="Scorch",e=3,nohit=true -- fire, applies rF- },{n="Searing Ray",e=0,penetrate=true,channeled=true -- irresistible, channeled, penetrates, can miss },{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,nohit=true -- irresistible, smite, AC applies },{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,centered=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,noac=true,nohit=true -- fire, smite all poisoned in LOS, ignores AC/EV },{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,rpois_immune=true -- poison, LOS AoE, ignores AC, rPois>=1 blocks },{n="Sticky Flame",e=3,nohit=true,noac=true -- fire, DoT ignores AC (bulk of dmg) -- Level 5 },{n="Arcjolt",e=2,aoe=2,nohit=true -- elec, arcs, 1/2 AC via halfac },{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,centered=true -- irresistible, all adjacent, player-centered },{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=1,halfres=true,auto_target=true -- phys+cold 50/50, auto-targets best cluster },{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,ignition=true -- fire, 3x3 on every enemy },{n="Lehudib's Crystal Spear",e=0 -- irresistible, short range },{n="Fulsome Fusillade",e=0,aoe=9,nohit=true -- irresistible (BEAM_MMISSILE), auto-hit, 3 explosions/turn },{n="Maxwell's Capacitive Coupling",e=2,noac=true,nohit=true,maxwells=true,auto_target=true -- elec, channeled 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,chain_lightning=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 }} -- === Tactical situation assessment === local hp_now,hp_max=you.hp() local hp_ratio=hp_now/hp_max -- Corridor detection: count solid tiles around player local corridor_walls=0 for cx=-1,1 do for cy=-1,1 do if (cx~=0 or cy~=0) and is_solid(cx,cy) then corridor_walls=corridor_walls+1 end end end local in_corridor=(corridor_walls>=5) -- Threat weighting: parse enemy HP, find min distance, detect boss local total_threat=0 local max_single_threat=0 local min_enemy_dist=99 local adj_hostiles=0 for _,e in ipairs(t) do local d=cdist(e.x,e.y) if dmax_single_threat then max_single_threat=e.mhp end end -- Boss fight: one enemy has >60% of total threat local boss_fight=(#t<=2 and max_single_threat>total_threat*0.6) -- Swarm fight: many weak enemies local swarm_fight=(#t>=4 and max_single_threat=3 then surround_boost=1.5 elseif adj_hostiles>=2 then surround_boost=1.2 end -- Low HP panic: prefer nohit burst on closest enemy local panic_mode=(hp_ratio<0.25 and adj_hostiles>0) local candidates={} local almost={} -- spells needing 1 more MP -- Battlesphere detected during initial monster scan (found_bsph) local bsph_bonus=0 local bsph_raw=0 local has_bsph=found_bsph if has_bsph and #t>0 then -- Battlesphere targets most-injured enemy: 2d(7+pow/9), irresistible, auto-hit local bsph_pow=0 if spells.memorised("Iskenderun's Battlesphere") then bsph_pow=get_pow("Iskenderun's Battlesphere") end local bsph_base=2*(7+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 dmg=calc_dmg(bsph_base,bsph_target.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)) bsph_raw=dmg end -- Pre-compute magnetised targets for Magnavolt (reuse t[] instead of full LOS scan) local mag_cache={} if spells.memorised("Magnavolt") then for _,e in ipairs(t) do if e.m:status("covered in magnetic dust") then mag_cache[#mag_cache+1]={x=e.x,y=e.y,m=e.m} end end 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) and cost<=mp+1 then -- Chain Lightning requires rElec to safely cast -- Irradiate: avoid if post-cast contamination could reach dangerous glow (>=100) if (sp.n=="Chain Lightning" and you.res_shock()<1) or (sp.n=="Irradiate" and (you.contamination() or 0)+40>100) or (sp.n=="Polar Vortex" and you.status("in a vortex")) or (sp.n=="Frozen Ramparts" and you.status("freezing walls")) or (sp.n=="Fulsome Fusillade" and you.status("raining reagents")) or (sp.n=="Iskenderun's Battlesphere" and (has_bsph or closest_dist<=3)) or (sp.n=="Spellspark Servitor" and found_servitor) or (sp.n=="Hellfire Mortar" and found_mortar) or (sp.n=="Olgreb's Toxic Radiance" and you.status("radiating poison")) then else local range=get_range(sp.n) local pow=get_pow(sp.n) local elec=(sp.e==2) -- electricity: half-AC (beam.cc:4595) local best_val=0 local best_kn=99 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 local reachable=0 -- Find closest enemy for pathing check local fx_e=t[1];local fx_d=99 for _,e in ipairs(t) do local d=cdist(e.x,e.y) if d0 and not sp.auto_target then local total_dmg=0 local best_tx,best_ty=0,0 if sp.n=="Static Discharge" then local base=get_spell_dmg(sp.n,pow) for _,e in ipairs(t) do if math.abs(e.x)<=1 and math.abs(e.y)<=1 then local val=0 -- O(9) per center: check 3x3 neighborhood via enemy_at hash for dx=-1,1 do for dy=-1,1 do local e2=enemy_at[(e.x+dx)..","..((e.y+dy))] if e2 then local r=get_res(e2.m,sp.e) local mult=get_res_mult(r) if mult>0 then local dmg=calc_dmg(base*mult,e2.ac,e2.ev,pow,sp.noac,sp.nohit,sp.ac3,elec) val=val+score_dmg(dmg,e2.m,cdist(e2.x,e2.y),e2) end 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*surround_boost 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=get_res_mult(r) if mult>0 then local dmg=spell_dmg(base,mult,e2.ac,0,pow,sp,elec) val=val+score_dmg(dmg,e2.m,cdist(e2.x,e2.y),e2) end end end end if not found then break end end best_val=val*surround_boost elseif sp.centered then if sp.n=="Iskenderun's Mystic Blast" then -- IMB: blast centered on player, hits all in radius 2, knockback bonus local val=0 for _,e in ipairs(t) do if cdist(e.x,e.y)<=range then local r=get_res(e.m,sp.e) local mult=get_res_mult(r) if mult>0 then local base=get_spell_dmg(sp.n,pow) local dmg=calc_dmg(base*mult,e.ac,e.ev,pow,sp.noac,sp.nohit,sp.ac3,elec) -- knockback collision bonus 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 kd=2+pow/50 local col_dmg=get_spell_dmg("IMB_collision",pow) local kx,ky=e.x,e.y 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,e.ac,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 dmg=dmg+calc_dmg(col_dmg,e.ac,0,pow,false,true) dmg=dmg+calc_dmg(col_dmg,fast_ac(om,nx,ny),0,pow,false,true) break end kx=nx;ky=ny end if cdist(e.x,e.y)<=1 then dmg=dmg*1.3 end val=val+score_dmg(dmg,e.m,cdist(e.x,e.y),e) end end end best_val=val*surround_boost if best_val>0 then best_tx=0;best_ty=0 end elseif sp.n=="Irradiate" then -- Irradiate: hits all adjacent enemies (radius 1, player-centered) local val=0 for _,e in ipairs(t) do if cdist(e.x,e.y)<=1 then local base=get_spell_dmg(sp.n,pow) local dmg=calc_dmg(base,e.ac,e.ev,pow,sp.noac,sp.nohit,sp.ac3,elec) dmg=sh_damage(dmg,e.m) val=val+score_dmg(dmg,e.m,cdist(e.x,e.y),e) end end best_val=val*surround_boost if best_val>0 then best_tx=0;best_ty=0 end elseif sp.n=="Flame Wave" then -- Flame Wave: expanding AoE, 3 turns. Closer enemies hit more turns. local val=0 local base=get_spell_dmg(sp.n,pow)/3 -- per-turn damage for _,e in ipairs(t) do local d=cdist(e.x,e.y) if d<=3 then local turns_hit=4-d -- dist1=3turns, dist2=2turns, dist3=1turn local r=get_res(e.m,sp.e) local mult=get_res_mult(r) if mult>0 then local dmg=calc_dmg(base*turns_hit*mult,e.ac,e.ev,pow,sp.noac,sp.nohit,sp.ac3,elec) dmg=sh_damage(dmg,e.m) val=val+score_dmg(dmg,e.m,d,e) end end end best_val=val*surround_boost if best_val>0 then best_tx=0;best_ty=0 end elseif sp.n=="Frozen Ramparts" then -- Frozen Ramparts: icy walls within range 2 of player damage adjacent monsters per turn -- Score enemies adjacent to walls that are within range of player local base=get_spell_dmg(sp.n,pow)*3 -- ~3 turns conservative local val=0 for _,e in ipairs(t) do -- Check if enemy has a wall neighbor within spell range of player (would be icy) local near_wall=false for dx=-1,1 do for dy=-1,1 do if not (dx==0 and dy==0) then local wx,wy=e.x+dx,e.y+dy if is_solid(wx,wy) and cdist(wx,wy)<=range then near_wall=true end end end end if near_wall then local r=get_res(e.m,sp.e) local mult=get_res_mult(r) if mult>0 then local dmg=calc_dmg(base*mult,e.ac,0,pow,sp.noac,sp.nohit,sp.ac3,elec) val=val+score_dmg(dmg,e.m,cdist(e.x,e.y),e) end end end best_val=val if best_val>0 then best_tx=0;best_ty=0 end else -- Other centered spells (Polar Vortex): hit all enemies within AoE range -- Wall penalty: tight spaces reduce vortex effectiveness local vortex_pen=1.0 if corridor_walls>=4 then vortex_pen=0.3 end local val=0 for _,e in ipairs(t) do if cdist(e.x,e.y)<=sp.aoe then local r=get_res(e.m,sp.e) local mult=get_res_mult(r) if mult>0 then local base=get_spell_dmg(sp.n,pow) local dmg=spell_dmg(base,mult,e.ac,0,pow,sp,elec) val=val+score_dmg(dmg,e.m,cdist(e.x,e.y),e) end end end best_val=val*vortex_pen 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 dice=0 local radius=1 local is_ice=false local from_mon=false -- Check monster first (by name-based lookup, matching source fraggable_monsters) local tm=monster.get_monster_at(x,y) if tm and tm:attitude()==0 and not tm:is_firewood() then local md,mr,mi=lrd_dice_mon(tm) if md>0 then dice=md;radius=mr;is_ice=mi;from_mon=true end end -- Then terrain if dice==0 then local f=view.feature_at(x,y) local td,tr=lrd_dice_terrain(f) if td>0 then dice=td;radius=tr end end if dice>0 and not has_friendly(x,y,radius) and math.max(math.abs(x),math.abs(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 base=dice*(dmg_per+1)/2 local dmg=calc_dmg(base,e2.ac,0,pow,false,true,true) -- Ice explosion: apply cold resistance if is_ice then local cr=e2.m:res_cold() if cr>0 then dmg=dmg/(1+cr) elseif cr<0 then dmg=dmg*1.5 end end local sv=score_dmg(dmg,e2.m,cdist(e2.x,e2.y),e2) if from_mon and e2.x==x and e2.y==y then sv=sv*1.5 end val=val+sv 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={} local aoe_seen={} local function add_center(cx,cy) local k=cx..","..cy if not aoe_seen[k] then aoe_seen[k]=true;aoe_centers[#aoe_centers+1]={x=cx,y=cy} end end for _,e in ipairs(t) do add_center(e.x,e.y) end if sp.aoe and sp.aoe>0 and sp.aoe<9 then local p_res=player_res(sp.e) if (sp.n=="Fireball" or sp.n=="Fire Storm") and p_res>=2 then add_center(0,0) end -- For any_target spells, also add empty cells near player 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) and not enemy_at[dx..","..dy] and view.cell_see_cell(0,0,dx,dy) then add_center(dx,dy) end end end end -- Add offset centers around each enemy (only for targeted AoE, not full-LOS) 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 cx,cy=e.x+dx,e.y+dy if not enemy_at[cx..","..cy] and view.cell_see_cell(0,0,cx,cy) then add_center(cx,cy) end end end end end end -- Chain Lightning: arc decay model (0.6x per arc, initial range penalty) if sp.chain_lightning then local sorted={} for _,e2 in ipairs(t) do sorted[#sorted+1]=e2 end table.sort(sorted,function(a,b) return cdist(a.x,a.y)0 then local base=get_spell_dmg(sp.n,pow) local dmg=spell_dmg(base,mult,e2.ac,e2.ev,pow,sp,elec)*arc_mult dmg=sh_damage(dmg,e2.m) val=val+score_dmg(dmg,e2.m,cdist(e2.x,e2.y),e2) end arc_mult=arc_mult*0.6 end best_val=val best_tx=sorted[1].x;best_ty=sorted[1].y -- Ignition: independent 3x3 explosion per enemy, overlapping hits elseif sp.ignition then local val=0 for _,e2 in ipairs(t) do local r=get_res(e2.m,sp.e) local mult=get_res_mult(r,sp.halfres) if mult>0 then local explosions=1 for _,e3 in ipairs(t) do if e3~=e2 and math.max(math.abs(e3.x-e2.x),math.abs(e3.y-e2.y))<=1 then explosions=explosions+1 end end local base=get_spell_dmg(sp.n,pow) local dmg=calc_dmg(base*mult,e2.ac,e2.ev,pow,sp.noac,sp.nohit,sp.ac3,elec)*explosions dmg=sh_damage(dmg,e2.m) val=val+score_dmg(dmg,e2.m,cdist(e2.x,e2.y),e2) end end best_val=val best_tx=t[1].x;best_ty=t[1].y else 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=get_res_mult(r) if sp.undead and e2.m:holiness()~="undead" then mult=0 end if sp.rpois_immune and r>=1 then mult=0 end if sp.n=="Ignite Poison" and not e2.m:status("poisoned") then mult=0 end if mult>0 then local base=get_spell_dmg(sp.n,pow) if sp.n=="Shatter" then -- 2x: fraggable (same list as LRD) + petrified/petrifying local lrd_d=lrd_dice_mon(e2.m) if lrd_d>0 then base=base*2 else -- 1/3: airborne or amorphous (jellies, slimes) local hdesc=e2.m:target_desc() or "" if string.find(hdesc,"lying") or string.find(hdesc,"evitat") then base=base/3 end end end local dmg=spell_dmg(base,mult,e2.ac,e2.ev,pow,sp,elec) dmg=sh_damage(dmg,e2.m) val=val+score_dmg(dmg,e2.m,cdist(e2.x,e2.y),e2) end end end -- Prism: discount for 2-turn delay (enemies may move away) if sp.prism then val=val*0.5 end -- Boost AoE centered on/near player when surrounded if cdist(center.x,center.y)<=1 then val=val*surround_boost 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 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 val=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=get_res_mult(r) if mult>0 then local base=get_spell_dmg(sp.n,pow) local ac=fast_ac(target.m,target.x,target.y) local ev=fast_ev(target.m,target.x,target.y) local hitC=get_hit_chance(target.m,sp.n,ev) local tdmg=spell_dmg(base,mult,ac,0,pow,sp,elec,true)*hitC tdmg=sh_damage(tdmg,target.m) val=val+score_dmg(tdmg,target.m,cdist(target.x,target.y)) end end -- Add the farthest target itself local r=get_res(farthest_e.m,sp.e) local mult=get_res_mult(r) if mult>0 then local base=get_spell_dmg(sp.n,pow) local ac=farthest_e.ac local ev=farthest_e.ev local hitC=get_hit_chance(farthest_e.m,sp.n,ev) local tdmg=spell_dmg(base,mult,ac,0,pow,sp,elec,true)*hitC tdmg=sh_damage(tdmg,farthest_e.m) val=val+score_dmg(tdmg,farthest_e.m,cdist(farthest_e.x,farthest_e.y)) end -- Lineup bonus for penetrating beam local lineup=#others if lineup>=1 then val=val*(1.0+lineup*0.3) end if val>0 then best_val=val;best_tx=farthest_e.x;best_ty=farthest_e.y end end elseif sp.n=="Permafrost Eruption" then -- Permafrost: auto-targets best cluster (game chooses), we estimate damage -- Source: picks enemy with most adjacent foes, skips dist<2 from caster -- Direct earth hit on target + cold explosion (ex_size=1) on neighbors local best_cluster=0 local best_target=nil for _,e in ipairs(t) do if cdist(e.x,e.y)>=2 then -- source: grid_distance(t,src)<2 skips adjacent 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 end -- Fallback: if all enemies too close, pick closest (game auto-aims anyway) if not best_target and #t>0 then best_target=t[1] end if best_target then local val=0 local base=get_spell_dmg(sp.n,pow) 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=get_res_mult(r) if mult>0 then local tdmg=spell_dmg(base,mult,e2.ac,e2.ev,pow,sp,elec) tdmg=sh_damage(tdmg,e2.m) val=val+score_dmg(tdmg,e2.m,cdist(e2.x,e2.y),e2) end end end if val>0 then best_val=val;best_tx=0;best_ty=0 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 reduction=math.min(e.adj_enemies,2)*0.3 local r=get_res(e.m,sp.e) local mult=get_res_mult(r) if mult>0 then local base=get_spell_dmg(sp.n,pow) local tdmg=calc_dmg(base*mult*(1-reduction),e.ac,0,pow,sp.noac,sp.nohit,sp.ac3,elec) tdmg=sh_damage(tdmg,e.m) total_dmg=total_dmg+score_dmg(tdmg,e.m,cdist(e.x,e.y),e) end end if total_dmg>0 then best_val=total_dmg;best_tx=0;best_ty=0 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=get_res_mult(r) if mult>0 then local ac=fast_ac(m2,rx,ry) local ev=fast_ev(m2,rx,ry) local dmg=calc_dmg(base*mult,ac,ev,pow,sp.noac,sp.nohit,sp.ac3,elec) 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: use pre-computed magnetised targets (mag_cache), find best new target local mag_targets=mag_cache -- 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=get_res_mult(r) if mult==0 then return 0 end local ac=fast_ac(m2,mx,my) local dmg=calc_dmg(base*mult,ac,0,pow,sp.noac,true,sp.ac3,elec) 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, scaled by target threat -- High-HP targets benefit more from stacking; weak enemies get less ramp-up local threat_scale=math.min(2.0,math.max(0.5,e.mhp/(max_single_threat+1))) val=val*(1.0+0.5*threat_scale+#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 + random_range(pow/35, pow/25+1); avg ~ 4.5 + pow/29 local constr_turns=4.5+pow/29 local constr_dmg=get_spell_dmg(sp.n,pow)*constr_turns -- Try each enemy as a bolt direction; 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,0,0,false) 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 -- Score each hit: skip already constricted (check target_desc for "onstrict") -- EV synergy now handled globally in get_ev() (-10 EV for constricted monsters) for _,h in ipairs(hits) do local hdesc=h.m:target_desc() or "" if not string.find(hdesc,"onstrict") then local ac=fast_ac(h.m,h.x,h.y) local dmg=calc_dmg(constr_dmg,ac,0,pow,false,true,false) -- Regen vs DoT: constriction damage offset by regeneration local he=enemy_at[h.x..","..h.y] if he and he.is_regen then dmg=dmg*0.6 end 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=get_res_mult(r,sp.halfres) if mult>0 then local ac=fast_ac(pm,p[1],p[2]) local ev=fast_ev(pm,p[1],p[2]) 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=get_res_mult(r,sp.halfres) if mult>0 then local dmg=calc_dmg(base_dmg*mult,e.ac,e.ev,pow,false,false,false) val=val+score_dmg(dmg,e.m,cdist(e.x,e.y),e) 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 dmg=calc_dmg(9*(base_M*scale+1)/2,e.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),e) if val>best_val then best_val=val;best_tx=e.x;best_ty=e.y end end end end end elseif sp.maxwells then -- Maxwell's Capacitive Coupling: targets closest visible enemy, kills after channeling local closest_e=nil local closest_d=99 for _,e in ipairs(t) do local d=cdist(e.x,e.y) if d0 and pm:name()~="battlesphere" and pm:name()~="orb of destruction" then path_has_friendly=true break end end end end if can_reach(e.x,e.y,range) and not ((sp.beam or sp.penetrate) and path_has_friendly) then local r=get_res(e.m,sp.e) local mult=get_res_mult(r) if sp.undead and e.m:holiness()~="undead" then mult=0 end if sp.rpois_immune and r>=1 then mult=0 end local hp=get_hp(e.m) local base=get_spell_dmg(sp.n,pow) -- Sticky Flame: heavily penalize target already burning (doesn't stack) if sp.n=="Sticky Flame" then if e.m:status("covered in liquid flames") then base=base*0.1 end -- Regen vs DoT: regeneration offsets burn damage over time if e.is_regen then base=base*0.6 end end if sp.n=="Airstrike" then local empty=count_empty(e.x,e.y) base=base+empty*2 end local ac=e.ac local ev=e.ev -- 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=spell_dmg(base,mult,ac,use_ev,pow,sp,elec) if hit_pct and not sp.nohit then dmg=dmg*hit_pct end dmg=sh_damage(dmg,e.m) -- Shock bounce: compute full ricochet path once, reuse for scoring local shock_hits,shock_enemies,shock_ekey if sp.n=="Shock" then shock_hits,shock_enemies=shock_bounce_hits(e.x,e.y,pow) shock_ekey=e.x..","..e.y local primary_hits=shock_hits[shock_ekey] or 1 dmg=dmg*primary_hits 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,e.ac,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 dmg=dmg+calc_dmg(col_dmg,e.ac,0,pow,false,true) dmg=dmg+calc_dmg(col_dmg,fast_ac(om,nx,ny),0,pow,false,true) break end kx=nx;ky=ny end if cdist(e.x,e.y)<=1 then dmg=dmg*1.3 end end -- Note: penetrate intermediate damage is scored per-monster below, not added to primary dmg if dmg>0 then local val=score_dmg(dmg,e.m,cdist(e.x,e.y),e) -- Shock bounce: add score for all enemies hit on bounce path if sp.n=="Shock" and shock_hits then local bounce_count=0 for key,info in pairs(shock_enemies) do if key~=shock_ekey then local hits=shock_hits[key] local r2=get_res(info.m,sp.e) local m2=get_res_mult(r2) if m2>0 then local b2=get_spell_dmg(sp.n,pow) local hitC=get_hit_chance(info.m,sp.n) local iac=fast_ac(info.m,info.x,info.y) local d2=spell_dmg(b2,m2,iac,0,pow,sp,elec,true)*hitC d2=sh_damage(d2,info.m)*hits val=val+score_dmg(d2,info.m,cdist(info.x,info.y)) bounce_count=bounce_count+1 end end end -- Bounce alignment bonus: boost only bounce portion if bounce_count>=1 then local primary_val=score_dmg(dmg,e.m,cdist(e.x,e.y),e) val=primary_val+(val-primary_val)*(1.0+bounce_count*0.5) end end if sp.penetrate and sp.n~="Shock" then local reachChance2=1.0 -- Use cached path when available (avoids second spells.path call) local others if found_star then others=monsters_on_line(e.x,e.y) elseif cached_path then others={} for _,p in ipairs(cached_path) do if not (p[1]==e.x and p[2]==e.y) then local pm=monster.get_monster_at(p[1],p[2]) if pm and pm:attitude()==0 and not pm:is_firewood() then others[#others+1]={m=pm,x=p[1],y=p[2]} end end end else others=monsters_on_path(sp.n,e.x,e.y) end local lineup_count=0 for _,other in ipairs(others) do local r2=get_res(other.m,sp.e) local m2=get_res_mult(r2) if m2>0 then local b2=get_spell_dmg(sp.n,pow) local hitC=get_hit_chance(other.m,sp.n) local d2=calc_dmg(b2*m2,fast_ac(other.m,other.x,other.y),0,pow,sp.noac,true,sp.ac3,elec)*hitC*reachChance2 d2=sh_damage(d2,other.m) val=val+score_dmg(d2,other.m,cdist(other.x,other.y)) reachChance2=reachChance2*(1-hitC) lineup_count=lineup_count+1 end end -- Bolt alignment bonus: prefer penetrating bolts when enemies line up if lineup_count>=1 then val=val*(1.0+lineup_count*0.3) end end -- Soft penalty for single-target spells needing many casts to kill local kn=1 if not sp.aoe and not sp.penetrate and not sp.beam then local total_dmg=dmg+(has_bsph and not sp.no_bsph and bsph_raw or 0) kn=math.ceil(hp/math.max(total_dmg,1)) if kn>4 then val=val*math.max(0.25,0.8^(kn-4)) end end if val>best_val then best_val=val;best_tx=e.x;best_ty=e.y;best_kn=kn end end end end -- Shock wall-bounce: score wall-aimed ricochets that may outperform direct shots if sp.n=="Shock" and #shock_wall_targets>0 then for _,wt in ipairs(shock_wall_targets) do local val=0 local bounce_count=0 for key,info in pairs(wt.enemies) do local hits=wt.hits[key] local r2=get_res(info.m,sp.e) local m2=get_res_mult(r2) if m2>0 then local b2=get_spell_dmg(sp.n,pow) local hitC=get_hit_chance(info.m,sp.n) local iac=fast_ac(info.m,info.x,info.y) local d2=spell_dmg(b2,m2,iac,0,pow,sp,elec,true)*hitC d2=sh_damage(d2,info.m)*hits val=val+score_dmg(d2,info.m,cdist(info.x,info.y)) bounce_count=bounce_count+1 end end if bounce_count>=1 then val=val*(1.0+bounce_count*0.5) end if val>best_val then best_val=val;best_tx=wt.x;best_ty=wt.y end end end end if best_val>0 then local penalty=1.0 if sp.channeled and sp.n=="Flame Wave" then penalty=0.33 end if sp.channeled and sp.n=="Searing Ray" then penalty=0.5 end if sp.slow then -- 1.5 turn cast: scale penalty by closest enemy distance local min_d=99 for _,e in ipairs(t) do local d=cdist(e.x,e.y) if d2 then best_val=best_val*0.5 end end -- Tactical modifiers: depth, corridor, fight type local tac=1.0 -- Depth scaling: early game penalizes expensive, late game penalizes cheap (smooth) if cost>=7 then tac=tac*(0.6+0.4*depth_fac) end if cost<=3 then tac=tac*(1.0-0.5*depth_fac) end -- Corridor: boost beam/penetrate, penalize wide AoE if in_corridor then if sp.beam or sp.penetrate then tac=tac*1.3 elseif sp.aoe and sp.aoe>=2 then tac=tac*0.7 end end -- Boss fight: boost single-target burst if boss_fight and not sp.aoe then tac=tac*1.3 end -- Swarm fight: boost AoE if swarm_fight and sp.aoe then tac=tac*1.3 end -- Mana conservation: prefer cheap spells when low MP and no critical threat if mp<=mp_max*0.3 and adj_hostiles==0 and cost>=5 then tac=tac*0.6 end -- Ignite Poison: boost when many enemies already poisoned (OTR synergy) if sp.n=="Ignite Poison" and n_poisoned>0 then local pois_frac=n_poisoned/#t if pois_frac>=0.5 then tac=tac*(1.0+pois_frac) end end -- Noise: prefer quiet spells when few enemies, none adjacent if #t<=2 and adj_hostiles==0 then local lvl=spells.level(sp.n) or 5 if lvl>=7 then tac=tac*0.7 end if lvl>=5 then tac=tac*0.85 end end -- Distant threat: penalize expensive spells when enemies far, non-threatening if adj_hostiles==0 and min_enemy_dist>=4 and not boss_fight then if cost>=6 then tac=tac*0.7 end end local eff_val=best_val*penalty*tac/math.sqrt(cost) local entry={n=sp.n,v=eff_val,x=best_tx,y=best_ty,c=cost,e=sp.e,ch=sp.channeled,manual=sp.mortar,ep=not (sp.clutch or sp.penetrate),ki=best_kn} if cost<=mp then table.insert(candidates,entry) elseif cost==mp+1 then table.insert(almost,entry) end end end end end -- 2-Turn Combo Lookahead: boost setup spells if payoff spell benefits next turn -- Poison → Ignite Poison combo local has_ignite=spells.memorised("Ignite Poison") if has_ignite then local ig_pow=get_pow("Ignite Poison") local ig_cost=get_cost("Ignite Poison") local ig_base=get_spell_dmg("Ignite Poison",ig_pow) -- Count how many enemies are NOT currently poisoned (Ignite can't hit them yet) local unpoisoned={} for _,e in ipairs(t) do local r=get_res(e.m,4) -- poison resistance if r<2 then -- not poison-immune if not e.m:status("poisoned") then unpoisoned[#unpoisoned+1]=e end end end if #unpoisoned>0 then -- For each poison-applying candidate, calculate Ignite Poison payoff next turn for _,c in ipairs(candidates) do local is_poison_setup=(c.n=="Poisonous Vapours" or c.n=="Mercury Arrow") local is_aoe_poison=(c.n=="Olgreb's Toxic Radiance") if is_poison_setup or is_aoe_poison then local combo_val=0 local targets_hit=is_aoe_poison and unpoisoned or {} -- Single-target poison: only the targeted enemy gets poisoned if is_poison_setup then for _,e in ipairs(unpoisoned) do if e.x==c.x and e.y==c.y then targets_hit={e};break end end end -- Simulate Ignite Poison damage on newly-poisoned targets for _,e in ipairs(targets_hit) do local r=get_res(e.m,3) -- fire resistance for Ignite local mult=get_res_mult(r) if mult>0 then local dmg=calc_dmg(ig_base*mult,e.ac,e.ev,ig_pow,true,true,false) combo_val=combo_val+score_dmg(dmg,e.m,cdist(e.x,e.y),e) end end -- Add discounted future value (0.7 = next turn uncertainty) -- MP check: can we afford setup + payoff next turn? (mp_regen ≈ mp_max/10) if combo_val>0 and (mp-c.c+mp_regen)>=ig_cost then c.v=c.v+combo_val*0.7/math.sqrt(ig_cost) end end end end end -- BVC → EV reduction combo: constriction helps all future non-nohit spells for _,c in ipairs(candidates) do if c.n=="Borgnjor's Vile Clutch" then -- Find best non-nohit spell for EV synergy calculation local best_followup=0 for _,c2 in ipairs(candidates) do if c2.n~=c.n and c2.v>best_followup then best_followup=c2.v end end -- Constriction gives -10 EV; rough estimate: 10/50 = 20% more hits -- Apply to best follow-up spell score if best_followup>0 then c.v=c.v+best_followup*0.2*0.7 -- 20% hit improvement, 0.7 discount end end end -- Scorch → fire spell combo: rF- makes follow-up fire spells deal ~50% more for _,c in ipairs(candidates) do if c.n=="Scorch" then local best_fire=0 for _,c2 in ipairs(candidates) do if c2.e==3 and c2.n~="Scorch" and c2.c<=(mp-c.c+mp_regen) and c2.v>best_fire then best_fire=c2.v end end if best_fire>0 then c.v=c.v+best_fire*0.5*0.7 end end end -- Generic 2-turn lookahead: boost cheap spells that leave MP for a strong follow-up -- Only adds value when casting A now + B next turn beats casting B now directly if #candidates>1 then local orig_v={} for i,c in ipairs(candidates) do orig_v[i]=c.v end -- Find best spell castable RIGHT NOW (what we'd do if no combo) local best_now=0 for i,c in ipairs(candidates) do if orig_v[i]>best_now then best_now=orig_v[i] end end for i,c in ipairs(candidates) do local next_mp=mp-c.c+mp_regen if next_mp>0 then local best_next=0 for j,c2 in ipairs(candidates) do if j~=i and c2.c<=next_mp and orig_v[j]>best_next then best_next=orig_v[j] end end -- Combo value = this spell + discounted follow-up -- Only boost if combo total exceeds what we'd get casting best spell now + filler next turn if best_next>0 then local combo_total=orig_v[i]+best_next*0.5 if combo_total>best_now then c.v=combo_total end 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 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) local active_channel=nil if recently_cast("Flame Wave") then active_channel="Flame Wave" elseif recently_cast("Searing Ray") then active_channel="Searing Ray" end if active_channel then -- If best option is the active channeled spell, just continue if b and b.n==active_channel then crawl.mpr("Cont") return end -- Find current channel value from candidates local ch_val=0 for _,c in ipairs(candidates) do if c.n==active_channel then ch_val=c.v;break end end if ch_val<=0 then ch_val=20 end -- fallback -- Check if any option is significantly better (2x) local has_better = false for _,c in ipairs(candidates) do if c.n~=active_channel and c.v > ch_val * 2 then has_better = true break end end if not has_better then crawl.mpr("Cont") return -- don't interrupt channeling end end -- Check if a significantly better spell is 1 MP away — wait instead of casting weaker -- Don't wait if current spell is practical (kills in ≤4 casts) if b and #almost>0 then table.sort(almost,function(a,ab) return a.v>ab.v end) if (not b.ki or b.ki>4) and almost[1].v > b.v*1.5 then crawl.mpr("Wait 1MP for "..almost[1].n) return end end if b then -- Check MP reserve for escape spells (override if current spell is lethal: kills ≤3) if reserve_mp>0 and (mp-b.c)3) then crawl.mpr("Reserve "..reserve_mp.."MP for "..reserve_spell) else mark_cast(b.n) crawl.mpr(b.n) if b.manual then crawl.sendkeys("z"..spells.letter(b.n)) else spells.cast(b.n,b.x,b.y,b.ep) end end else crawl.mpr("No") end end }