float verbweight = 0.2;
float letterweight = 10;
float elemresistweight = 10;
float famweightweight = 5;
float powerweight = -100;
float diveweight = 1.5;
string checkletter;
int lettersinname(item g, string l)
{
  int lettercount=0;
  for pos from 0 to length(g)-1
  {
    if (char_at(g,pos)==l) lettercount+=1;  
  }
  return lettercount;
}
int checkdive(item g) // needs improving - may return stuff that just boosts a single modifier rather than reducing dive penalty
{
  string modstring = string_modifier(g,"Modifiers");
  matcher divematcher = create_matcher("(\\d+)\\*zone\\(The Sea\\)",modstring);
  if (divematcher.find()) return to_int(divematcher.group(1)); else return 0;
}
int valuation(item g)
{
  int thisvalue;
  thisvalue += g.name_length*verbweight;
  thisvalue += lettersinname(g,checkletter)*letterweight;
  thisvalue += min(numeric_modifier(g,"Cold Resistance"),min(numeric_modifier(g,"Hot Resistance"),min(numeric_modifier(g,"Spooky Resistance"),min(numeric_modifier(g,"Sleaze Resistance"),numeric_modifier(g,"Stench Resistance")))))*elemresistweight;
  thisvalue += numeric_modifier(g,"Familiar Weight")*famweightweight;
  
  if (to_slot(g)==to_slot("hat") || to_slot(g)==to_slot("pants") || to_slot(g)==to_slot("shirt")) thisvalue += get_power(g)*powerweight;
  thisvalue += checkdive(g)*diveweight;
  return 0-thisvalue;
}
void main(string letter)
{
  checkletter=letter;
  item [slot] [int] gear;
  slot sl;
  foreach i in $items[]
    if(available_amount(i) > 0 && to_slot(i) != $slot[none]) {
      sl = to_slot(i);
      if((can_equip(i) || sl == $slot[familiar]) && !(sl == $slot[weapon] && weapon_hands(i) > 1))
        gear [sl] [count(gear[sl])] = i;
    }
  foreach s in gear
  {
    sort gear[s] by valuation(value);
  }
  boolean gearup(slot s, item it) {
    if(it == $item[none]) return false;
    if(equipped_amount(it) < 1 && item_amount(it) < 1)
      retrieve_item(1, it);
    return equip(s, it);
  }
  foreach s in $slots[hat, back, weapon, off-hand, shirt, pants]
    gearup(s, gear[s][0]);
  gearup($slot[acc1], gear[$slot[acc1]][0]);
  int acctry=0;
  int accslot=1;
  foreach s in $slots[acc2,acc3]
  {
    if(available_amount(gear[$slot[acc1]][acctry]) < 1+accslot
          || boolean_modifier(gear[$slot[acc1]][acctry],"Single Equip")==true)
      {
        acctry+=1;
      }
    else {accslot+=1;}
    
    gearup(s, gear[$slot[acc1]][acctry]);
  }
  item fam = gear[$slot[familiar]][0];
  if(can_equip(fam))
    gearup($slot[familiar], fam);
  else print_html("<b>"+fam+"</b> is recommended, but it cannot be equipped on your current familiar");
}