#!python
""" twink.py: search PernAngband's race info for interesting races.
    Intended for posessors looking for an interesting victim.

    Run this as a script to just print the names of the monsters;
    import it from Python to do other things with the results.
"""
from r_parse import *

# The location of the Angband race info file on your system.
# Right now I only support pern, but eventually you can place as many
# r_info files as you want here.
files = {"pern":"d:/angband/pern/lib/edit/r_info.txt"}

# The qualities you demand, grouped in lists.
demands = (
          (1, "F", ('DROP_CORPSE',),                                  "body",),
          (1, "S", ('1_IN_4','1_IN_3', '1_IN_2', '1_IN_1'),     "spell fail",),
          (1, "S", ('TPORT','TELE_AWAY','TELE_LEVEL', 'TELE_TO'),   "escape",),
          (1, "S", ('BA_', 'BO_', 'BR_', 'CAUSE_3', 'CAUSE_4',
                    'ARROW_3', 'S_', 'BLAST','SMASH','ROCKET','HAND_DOOM'),
                                                      "attack spell ability",),
          (1, "FS",('HASTE','BLINK', 'TELE_TO', 'HEAL', 'SLOW','CONF', 'S_',
               'IM_', 'REFLECTING'),                      "tactical ability",),
         (-1, "F",('RAND_','NEVER_BLOW','NEVER_MOVE'),  "control difficulty",),
    ),  {
        "weapons":(GE,1),
        "speed":(GE,110),
        "rings":(GE,2),
        }

################### test code ##########################
data = {}
for variant, file in files.items():
    data[variant] = RaceFile(file, parsers[variant])

# test only the pern parser...
results = data["pern"]

def showChanges(original,new,reason):
    magnitude = len(original) - len(new)
    print "Removed", magnitude, "monsters because of", reason
    if 0 < magnitude < 10:
        print( original - new )

def main():
    global results
    print "Starting with", str(len(results)), "monsters..."
    text_demands = demands[0]
    field_demands = demands[1]
    for criteria in text_demands:
        numberNeeded, attributes, desired, reason = criteria
        new = results.queryFlags(attributes,desired,numberNeeded)
        showChanges(results, new, reason)
        results = new
    for name,test in field_demands.items():
        new = results.queryValues(name,test)
        showChanges(results,new, "undesired "+name+".")
        results = new
    print "There are", len(results), "monsters in Pern which fit your needs."

    uniques = results.queryFlags("F",('UNIQUE',),1)
    print len(uniques), "of them are unique:"
    print uniques
    print "and", len(results-uniques), "of them are not unique:"
    print results-uniques

if __name__ == '__main__':
    main()
