chiark / gitweb /
quacks-ingredients: wip Base vs Witches vs Both
[reprap-play.git] / quacks-ingredients-counts
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use Data::Dumper;
6 use POSIX;
7
8 our $which = shift @ARGV;
9
10 sub xdata ($) {
11     my ($cb) = @_;
12     return unless $which eq 'Base';
13     foreach my $count (qw(1 2 3)) {
14         foreach my $nspots (qw(1 2 3 4)) {
15             $_ = $cb->($count,$nspots)."\t".$_;
16         }
17     }
18 }
19
20 $_=<DATA>; chomp or die;
21 xdata sub {
22     my ($xcount,$xnspots) = @_;
23     "${xcount}x". (qw(X One Two Three Four)[$xnspots]);
24 };
25 our @names = split /\t/, $_;
26
27 our %count;
28
29 foreach my $nspots (qw(1 2 3 4)) {
30     $_=<DATA>; chomp or die;
31     xdata sub {
32         my ($xcount,$xnspots) = @_;
33         $xnspots == $nspots and "$xcount+0";
34     };
35     my @l = split /\t/, $_;
36     foreach my $i (0..$#names) {
37         $_ = $l[$i] || '0+0';
38         $_ ||= 0;
39         m/\+/;
40         
41         $count{$names[$i]}{$nspots} =
42             $which eq 'All'    ? $` + $' :
43             $which eq 'Base'    ? $`      :
44             $which eq 'Witches' ?      $' :
45             die "$which ?";
46     }
47 }
48
49 $_ = Dumper(\%count);
50 s{^}{// }mg;
51 #print STDERR;
52
53 our $name;
54 our $total_count;
55 our $total_real_count;
56 our $max_nrows=0;
57 our $max_rowsz=0;
58
59 sub wrtoplevel () {
60     print "module ${which}_$name(){ ////toplevel\n";
61     my $cs = $count{$name};
62     my $total = 0; $total += $_ foreach values %$cs;
63     my $rowsz = ceil(sqrt($total));
64     my $nrows = ceil($total / $rowsz);
65     $total_count += $total;
66     $total_real_count += $total if $name =~ m/^[A-Z][a-z]+$/;
67     $max_nrows = $nrows if $nrows > $max_nrows;
68     $max_rowsz = $rowsz if $rowsz > $max_rowsz;
69     my $ix = 0;
70     printf "// %s  %-10s  total=%2d  rowsz=$rowsz  nrows=$nrows\n",
71         $which, "$name", $total;
72     foreach my $nspots (sort keys %$cs) {
73         my $c = $cs->{$nspots};
74         print <<END;
75   union(){
76     Frame(\$phase, token_pitch * [ $rowsz + 1.00, $nrows + 0.50 ]);
77     \$nspots = $nspots;
78 END
79         while ($c--) {
80             my $xy = sprintf "[ %5.1f, %5.1f ]",
81                 int($ix / $nrows) - 0.5 * ($rowsz-1),
82                 $ix % $nrows - 0.5 * ($nrows-1);
83             print "    translate(token_pitch * $xy) Token_L();\n";
84             $ix++;
85         }
86         print <<END;
87   };
88 END
89     }
90     print "}\n";
91 }
92
93 foreach $name (sort keys %count) {
94     wrtoplevel();
95 }
96
97 print <<END;
98 // $which  total_count=$total_count   total_real_count=$total_real_count
99 // $which  max_rowsz=$max_rowsz       max_nrows=$max_nrows
100 END
101
102 STDOUT->error and die $!;
103
104 __DATA__
105 White   Green   Blue    Red     Yellow  Purple  Black   Orange
106 20+6    15+10   14+8    12+6    13+6    15+8    18+8    20+12
107 8+3     10+5    10+5    8+5     6+5
108 4+2
109         13+5    10+5    10+5    10+5