#!/usr/bin/perl sub UsageShort() { print "Usage: varcalc [ -h | --help | -l | --list ] | variables1.var [ ... variablesN.var ] command\n" ; # print "Usage: varcalc [ -h | --help ] | variables1.var [ ... variablesN.var ] command\n" ; } sub UsageLong() { print " -l | --list: prints a description of available commands\n" ; print " The variables file contains one variable definition by line:\n" ; print " _ VAR = no_whitespace_string\n" ; print " _ VAR = \"any words without double quotes\"\n" ; print " A dash ('-') stands for the standard input\n" ; } sub CommandList() { print " varcalc available commands:\n" ; print " _ cost : calculates the point cost of the character\n" ; } if(scalar(@ARGV)==0) { UsageShort() ; exit 1 ; } if((scalar(@ARGV)>=1)&&(($ARGV[0] eq '-h')||($ARGV[0] eq '--help'))) { UsageShort() ; UsageLong() ; exit 0 ; } if((scalar(@ARGV)>=1)&&(($ARGV[0] eq '-l')||($ARGV[0] eq '--list'))) { UsageShort() ; CommandList() ; exit 0 ; } my $verb = 0 ; if($ARGV[0] eq '-v') { $verb = 1 ; shift @ARGV ; } if(scalar(@ARGV)<2) { UsageShort() ; exit 1 ; } # LoadVarFile # Loads a variables definition file into a hash # Args: # _var : reference to a variables hash # _file : the file to read # Returns: # _the original hash ref if it was defined and valid # _a ref to a new hash if the first argument was undef # _'undef' if the hash ref was invalid sub LoadVarFile($$) { my $var = shift ; my $file = shift ; my $fd ; my $base = undef ; if(defined $var) { if(ref($var) ne 'HASH') { return undef ; } } else { $var = { } ; } if($file ne '-') { open($fd,'<',$file) ; my @base = split('/', $file) ; pop @base ; if(@base) { $base = join('/',@base) . '/' ; } else { $base = '' ; } } else { $fd = *stdin ; } while(<$fd>) { if(/^#/) { next ; } if(/^\s*$/) { next ; } if( (/^\s*([^\s]+)\s*=\s*([^'"\s][^\s]*)/) || (/^\s*([^\s]+)\s*=\s*"([^"]+)"/) || (/^\s*([^\s]+)\s*=\s*'([^']+)'/) ) { my ($nam,$cnt) = ($1,$2) ; if($cnt=~/^@(?!\/)/) { $cnt = '@' . $base . substr($cnt,1) ; } $var->{$nam} = $cnt ; } } if($file ne '-') { close($fd) ; } return $var ; } ################################################################################ ### Processing arguments $cmd = pop @ARGV ; my %allvars = ( '_processed_with_' => "$0" ) ; for $var (@ARGV) { LoadVarFile(\%allvars,$var) ; } if($verb) { for my $key (keys %allvars) { print $key, ' => "', $allvars{$key}, '"' , "\n" ; } } ################################################################################ ### General defines my $ptvalue = 50 ; my @phys = ( 'Grav', 'Quan', 'Elec', 'Magn', 'Optc', 'Mech', 'Chem' ) ; my @intl = ( 'Mark', 'Logc', 'Intu', 'Conc', 'Synt', 'Mmry', 'Refx' ) ; my @morl = ( 'Lead', 'Rela', 'Thrg', 'Loyt', 'Mind' ) ; ################################################################################ ### Points costs calculus if($cmd eq 'cost') { print "\n" ; printf "Character/Species: %-20s - %-10s\n", $allvars{'Name'}, $allvars{'Race'} ; print "\n" ; my $ccst = 0 ; for my $cap (@phys) { my $ct = 1*$allvars{$cap.',F'} + 6*$allvars{$cap.',A'} + 3*$allvars{$cap.',C'} + 3*$allvars{$cap.',P'} ; if($cap eq 'Quan') { $ccst += 3*$ct ; } else { $ccst += $ct ; } } for my $cap (@intl) { my $ct += 10*$allvars{$cap.',B'} - 8*$allvars{$cap.',R'} - 3*$allvars{$cap.',S'} - 3*$allvars{$cap.',D'} ; $ccst += $ct ; } for my $cap (@morl) { my $ct += 2*$allvars{$cap.',Y'} + 2*$allvars{$cap.',O'} + 2*$allvars{$cap.',N'} + 2*$allvars{$cap.',U'} ; $ccst += $ct ; } printf "Capacities cost: %5d p\n", $ccst ; my $scst = 0 ; my $skills = $allvars{'Skills'} ; if($skills =~ m/^@/) { local *F ; if(open(F,'<',substr($skills,1))) { while() { my $ct = 0 ; chomp ; (undef,$ct) = split /\t+/ ; if($ct =~ /^\s*([-+]?\d+)\s*p\s*$/) { $scst += $1 ; } } close(F) ; } } printf "Skills cost: %5d p\n", $scst ; my $ecst = 0 ; my $equipt = $allvars{'Equipt'} ; if($equipt =~ m/^@/) { local *F ; if(open(F,'<',substr($equipt,1))) { while() { my $ct = 0 ; chomp ; (undef,$ct) = split /\t+/ ; if($ct =~ /^\s*([-+]?\d+)\s*c\s*$/) { $ecst += $1 ; } } close(F) ; } } printf "Equipment cost: %5d p or %8d c\n", ($ecst/$ptvalue), $ecst ; print "\n" ; printf "Total character cost: %5d p or %8d c\n", ($ccst+$scst+$ecst/$ptvalue), ($ccst*$ptvalue+$scst*$ptvalue+$ecst) ; my $totpts = 0 ; if($allvars{'Race'} eq 'Human') { $totpts = 7200 ; } elsif($allvars{'Race'} eq 'Tytlal') { $totpts = 7200 ; } elsif($allvars{'Race'} eq 'Tymbrimi') { $totpts = 12000 ; } printf "Remaining: %5d p or %8d c\n", ($totpts-($ccst+$scst+$ecst/$ptvalue)), ($totpts*$ptvalue-($ccst*$ptvalue+$scst*$ptvalue+$ecst)) ; print "\n" ; } ################################################################################ ### Default else { print "Unknown command: $cmd\n" ; exit 2 ; }