From: colin@Cayman.COM (Colin "Atilla" Steele)
Newsgroups: rec.games.pbm
Subject: galaxy utility, ver 1.1
Date: 9 Sep 92 15:10:44 GMT
Distribution: rec
Organization: Cayman Systems Inc., Cambridge, MA


Well, there were a couple of problems with ships.pl, so here's a new
version, which should fix them.  In this version, ships with
fractional characteristics, like Drive 1.66, will be processed
correctly.  Also, the name of the ship is no longer used as the key,
so if there are multiple ships types with the same name, they should
be processed correctly.  There are two new switches, -d and -b.  They
are explained in the comments in the source file.  I put them in
because I was hitting the 80-column wall.  That's about it.  Thanks to
all who found bugs and let me know.  Keep the reports and/or new
utility ideas flowing!

Enjoy.

--- cut here ------------------------------------------------------------------
#!/bin/perl
#
#
###############################################################################
# SHIPS.PL Ver 1.1
# Author:  Colin A. Steele (colin@Cayman.com)
# Redistribute freely.  Go nuts ;-)
# Report bugs to colin@Cayman.com.  I may not fix it, but I'll be sympathetic.
#       
#       
# Usage:  ships.pl [-f turnfile] [-a] [-o ownername] [-d] [-b]
# use -f to specify the turnfile to process, else use stdin.
# use -a to see stats on all ships.
# use -o to specify the name of the player whose ships you want stats on.
# use -b to see basic stats.
# use -d to see derived (interesting) stats.
#       
# Stats are calculated as follows.  None of the derived
# stats take tech into account - just multiply by tech to get
# the real stat...
#       
# From "galaxy-rules":  The mass of a ship is equal to Drive +
#           Attacks*Weapons + Shield + Cargo.
#       
#           A ship moves a number of light years
#           per turn equal to 20 times its
#           drive power divided by its total mass.
#
#           "Total mass" means the mass of the ship
#           itself plus the mass of any cargo it's carrying...
#           
#                   The power of the attack is
#           equal to the Weapons number multiplied by the
#                   Weapons tech level.
#         
#                   The power of the defense is equal to the Shield
#                   number multiplied by the Shield tech level
#           and divided by the diameter of the target ship.
#
#           A ship's diameter is equal to
#           the cube root of its mass.
###############################################################################

require "getopts.pl";

#
# Check command line options - if no file is specified, use stdin.
#
&Getopts('af:o:bd');

$inGroups = 0;
undef $owner;

$FILE = $opt_f;
$OWNER = $opt_o;
$ALL = $opt_a;
$BASIC = $opt_b;
$DERIVED = $opt_d;

(!$FILE) && ($FILE = STDIN);

if ($FILE ne "STDIN") {
        open(INFILE, "<$FILE") || die "Can't open input file $FILE: $!\n";
}

#
# Here's the main part - snarf in the file, and when we hit a section
# that starts with "N      D A  W  S  C", then start processing
# the ships.  Use the ship's name and owner as the key into our
# associative arrays.  When you hit a blank line, stop processing
# ships.
#
while (<INFILE>) {

        if (/\s*(\w*) Ship Types\s*/) {
                $owner = $1;
        }
        
        if (/N\s*D\s*A\s*W\s*S\s*C\s*$/) {
                if (($OWNER && ($owner eq $OWNER)) || !$OWNER || $ALL) {
                        $inGroups = 1;
                }
        }
        
        if ($inGroups) {

                if (/^(\w*)\s*([\d.]+)\s*([\d.]+)\s*([\d.]+)\s*([\d.]+)\s*([\d.]+)\s*/) {
                        $key = $1 . $owner;
                        $shipName{$key} = $1;
                        $shipOwner{$key} = $owner;
                        $shipDrives{$key} = $2;
                        $shipAttacks{$key} = $3;
                        $shipWeapons{$key} = $4;
                        $shipShields{$key} = $5;
                        $shipCargo{$key} = $6;
                        $shipMass{$key} = $2 + ($3 * $4) + $5 + $6;
                        $shipMassRoot{$key} = $shipMass{$key} ** (1/3);
                }

                if (/^\s*$/) {
                        $inGroups = 0;
                }
        }
}

#
# Print out the basic stats
#
if ($BASIC) {
        printf "%14s      %s    %s    %s    %s    %s   %s   %s     %s     %s\n",
       "Name",
       "Dri",
       "Att",
       "Wea",
       "Shd",
       "Car";
        for (sort keys %shipName) {
                if (($OWNER && ($shipOwner{$_} eq $OWNER)) ||
                        (!$OWNER && ($shipOwner{$_} eq "Your")) ||
                        $ALL) {
                  printf "%14s   %6.2f %6.2f %6.2f %6.2f %6.2f\n",
                     $shipName{$_},
                           $shipDrives{$_},
                           $shipAttacks{$_},
                           $shipWeapons{$_},
                           $shipShields{$_},
                           $shipCargo{$_};
            }
        }
}

print "\n" . "\n";

#
# Print out the dervied stats
#
if ($DERIVED) {
        printf "%14s     %s   %s     %s     %s   %s   %s\n",
       "Name",
       "Mass",
       "Diam",
       "SS",
       "AS",
       "Move",
       "Owner";
        for (sort keys %shipDrives) {
                if (($OWNER && ($shipOwner{$_} eq $OWNER)) ||
                        (!$OWNER && ($shipOwner{$_} eq "Your")) ||
                        $ALL) {
                  printf "%14s   %6.2f %6.2f %6.2f %6.2f %6.2f   %s\n",
                    $shipName{$_},
                    $shipMass{$_},
                    $shipMassRoot{$_},
                    $shipShields{$_} / $shipMassRoot{$_},
                    $shipAttacks{$_} * $shipWeapons{$_},
                    20 * $shipDrives{$_} / $shipMass{$_},
                    $shipOwner{$_};
            }
        }
}
--


------------------------------------------------------------------------------
 Colin Steele     | Cayman Systems, 26 Landsdowne St., Cambridge, MA 02139
 colin@cayman.com | (617) 494-1916 x209 | applelink D0523 | Fax (617) 494-9270
Newsgroups: rec.games.pbm