User:Aaxelb/Monster resistances

From NetHackWiki
Jump to navigation Jump to search

Here's the little script I wrote to generate the body of the monster resistances table (as well as the UnNetHack version). It uses Adeon's pinobot code. This page is as much for my own reference as for anyone else, and this was my first time playing with a purely functional language, let alone with Haskell, so please be gentle.

To run this script, put it in a file (say, resistancesTable.hs) in the pinobot/src directory, then:

ghc --make resistancesTable
./resistancesTable vanilla > vanillaResistances.txt
./resistancesTable unnethack > unnethackResistances.txt

And here's the script:

import qualified NetHack.Data.Monster as MD
import qualified NetHack.Imported.Vanilla as Vanilla
import qualified NetHack.Imported.UnNetHack as UnNetHack
import qualified Data.Text as T
import System.Environment
import Data.Maybe ( fromJust )
import Data.List.Utils ( join, contains )

data Variant = Vanilla
             | UnNetHack

wikitableBody :: Variant -> String
wikitableBody variant = join "\n|-\n" [wikitableMonsterLine variant monName | monName <- (allMonsters variant)]

monsterFetcher :: Variant -> T.Text -> Maybe MD.Monster
monsterFetcher Vanilla = Vanilla.monster
monsterFetcher UnNetHack = UnNetHack.monster

allMonsters :: Variant -> [T.Text]
allMonsters Vanilla = Vanilla.allMonsterNames
allMonsters UnNetHack = UnNetHack.allMonsterNames

wikitableMonsterLine :: Variant -> T.Text -> String
wikitableMonsterLine variant monName = wikitableMonsterLine' (fromJust $ monsterFetcher variant monName)

wikitableMonsterLine' :: MD.Monster -> String
wikitableMonsterLine' mon = "| " ++ join " || "
    [
      "{{monsymlink|" ++ T.unpack (MD.moName mon) ++ "}}",
      hasResistance mon MD.ReFire,
      hasResistance mon MD.ReCold,
      hasResistance mon MD.RePoison,
      hasResistance mon MD.ReSleep,
      hasResistance mon MD.ReElectricity,
      hasResistance mon MD.ReDisintegrate,
      hasResistance mon MD.ReAcid,
      hasResistance mon MD.RePetrification,
      hasResistance mon MD.ReDrain,
      hasResistance mon MD.ReMagic ]
  where
    hasResistance mon res = formatCell res (contains [res] (MD.moResistances mon))

    resistanceName MD.ReFire = "Fire resistance"
    resistanceName MD.ReCold = "Cold resistance"
    resistanceName MD.ReSleep = "Sleep resistance"
    resistanceName MD.ReDisintegrate = "Disintegration resistance"
    resistanceName MD.ReElectricity = "Shock resistance"
    resistanceName MD.RePoison = "Poison resistance"
    resistanceName MD.ReAcid = "Acid resistance"
    resistanceName MD.RePetrification = "Petrification resistance"
    resistanceName MD.ReDrain = "Drain resistance"
    resistanceName MD.ReMagic = "Magic resistance"

    formatCell res hasRes = "title='" ++ resistanceName res ++ "' " ++ yesify hasRes
    yesify True = "{{yes}}"
    yesify False = "{{no}}"

variantFetcher :: String -> Variant
variantFetcher "vanilla" = Vanilla
variantFetcher "unnethack" = UnNetHack

main = do
    (variant:_) <- getArgs
    putStrLn $ wikitableBody (variantFetcher variant)