Files
CoCoSmash/generate-scoring.pl

102 lines
1.8 KiB
Perl
Executable File

#!/usr/bin/perl -w
#
# This script determines the scoring associated with certain targets.
# See the %scoring table.
use strict;
my $numLevels = 6; # must be consistent with MAX_LEVEL_NO in the C code
# The product of on of these numbers with the maximum level (6)
# must not exceed 9999.
#
my %scoring = (
ROCK => 10,
PULSAR => 50,
SPINNER => 80,
SAUCER => 400,
);
my $mode = shift; # "h" or "c"
my $numKeys = keys %scoring;
my $tableDeclaration = "const unsigned char scoringTable[$numKeys][$numLevels][4]";
my $title = "This file generated by $0. Do not edit.";
my @sortedKeys = sort keys %scoring;
if ($mode eq "c")
{
print <<__EOF__;
/* $title */
#include "scoring.h"
$tableDeclaration =
{
__EOF__
for (@sortedKeys)
{
printf(" { /* SCORE_%s */\n", $_);
for (my $lvl = 1; $lvl <= $numLevels; ++$lvl)
{
my $product = $scoring{$_} * $lvl;
printf(" { %u, %u, %u, %u }, /* level %u */\n",
int($product / 1000),
int($product / 100) % 10,
int($product / 10) % 10,
$product % 10,
$lvl);
}
printf(" },\n");
}
print <<__EOF__;
};
__EOF__
}
elsif ($mode eq "h")
{
print <<__EOF__;
/* $title */
#ifndef _H_scoring
#define _H_scoring
/* Indexes in scoringTable[].
*/
enum
{
__EOF__
my $last;
for (@sortedKeys)
{
printf(" SCORE_%s,\n", $_);
$last = $_;
}
printf(" SCORE_MAX_INDEX = SCORE_%s\n", $last);
print <<__EOF__;
};
/* scoringTable[SCORE_*][levelNo - 1] gives a 4-byte big endian decimal array.
The 2nd index must be 0..5 for actual game levels 1..6.
*/
extern $tableDeclaration;
#endif /* _H_scoring */
__EOF__
}
else
{
die "unsupported mode '$mode'";
}