#!/usr/exp/bin/perl

# nhk - nethack monster high scores (version 1.0)
#
# Copyright (C) 1993 by John J. Chew, III <jjchew@math.utoronto.ca>
# Heavily edited by Boudewijn Wayers <dedos4@win.tue.nl>
#
# DESCRIPTION
#
# This program lists high scores for monsters, either by the number of
# players that they have managed to kill, or by the total score of 
# their victims.  
#
# BUGS
#
# - nhk has been tested only on our local files and may not correctly
#   format unusual killers.  Please report bugs to the address above.
# - nhk stands, for historical reasons, for NetHack Killers.  It does
#   not stand for Nihon Housou Kyoku.
#
# The output looks like:
#
# 15 1218362 corpse
#  2  229752 starvation
#  2  189090 cockatrice
#  3  164639 magic bolt
#  1  134738 vampire lord
#  2   73613 black pudding
# 10   46236 rothe
#  1   34126 death ray
#  2   11243 Woodland-elf
#  1    6460 giant beetle
#  1    2529 burning book
#  1    1454 iguana

# configuration variables

$CV_DEFAULT_DIRECTORY = '/home/svin04d/dedos4/bin/nethackdir';
$CV_LOGFILE_NAME = 'logfile';
$CV_RECORD_NAME = 'record';

sub usage {
  warn "Usage: $0 [ -f file | -l | -r ] [ -n | -s ]\n\n";
  warn "  -f  use indicated file\n";
  warn "  -l  use logfile\n";
  warn "  -r  use record (high score file) [default]\n";
  warn "  -n  sort by number of kills\n";
  warn "  -s  sort by aggregate victim score [default]\n";
  exit 1;
  }

require 'getopts.pl';
&Getopts('f:lnrs') || &usage;
(defined $opt_f) && $opt_l && $opt_r && &usage;
$opt_n && $opt_s && &usage;
$opt_n || $opt_s || ($opt_s = 1);
$FILE = (defined $opt_f) ? $opt_f : 
  $CV_DEFAULT_DIRECTORY . '/' . ($opt_l ? $CV_LOGFILE_NAME : $CV_RECORD_NAME);

(defined $opt_f) || $opt_l || ($opt_r = 1);

open(FILE, "<$FILE") || die "$0: cannot open $FILE";
@file = <FILE>;
close(FILE);

$killlen = 1;
$scorelen = 1;
for $_ (@file) {
  chop;
  # date uid deathdnum deathlev maxlvl hp maxhp points plchar sex name death
  split(/ /, $_, 10); $_ = $_[9]; 
  s/, the shopkeeper//;
  s/.*,//;
  s/(killed by|choked on|poisoned by|drowned in|crushed to death by|petrified by)( an?)? //; # see topten.c
  s/hallucinogen-distorted |invisible //;
  s/ called .*//;
  s/priest(ess)?.*/priest(ess)/;
  s/.* corpse/corpse/;
  s/bolt of .*/magic bolt/;
  s/fall onto |moat by a |.* of //;
  $count{$_}++;
  $killlen = length($count{$_}) if length($count{$_}) > $killlen;
  $score{$_} += $_[7];
  $scorelen = length($score{$_}) if length($score{$_}) > $scorelen;
  }

sub mysort { 
  ($A = $a) =~ tr/A-Z/a-z/; ($B = $b) =~ tr/A-Z/a-z/;
  (($opt_n) ? $count{$b} <=> $count{$a} : $score{$b} <=> $score{$a}) 
    || $A cmp $B;
  }

for $key (sort mysort keys %count)
  {
  printf "%${killlen}d %${scorelen}d %${scorelen}d %s\n", $count{$key}, $score{$key}, $score{$key}/$count{$key}, $key;
  }

exit 0;
__END__

