donut2.stl: Randomize tiles.
[supertux-levels.git] / tile_randomizer.px
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 our $TileGroups =
7 [
8   [1001, 1002], # forest gras
9   [1005, 1006], # forest stones top
10   [1008, 1012], # forest stones left
11   [1009, 1013, 1014], # forest stones fill
12   [1011, 1015], # forest stones right
13   [1017, 1018], # forest stones bottom
14   [2058, 2060, 2061, 2065, 2066, 2070], # ice tiles
15   [2179, 2180, 2181, 2187, 2188, 2189, 2195, 2196, 2197], # snow bg
16   [2171, 2172, 2175, 2176] # those other snow thingies
17 ];
18 our $TileMap = {};
19
20 sub init
21 {
22   for (@$TileGroups)
23   {
24     my $group = $_;
25
26     for (@$group)
27     {
28       my $id = $_;
29
30       $TileMap->{$id} = $group;
31     }
32   }
33 } # init
34
35 sub get_random
36 {
37   my $id = shift;
38   my $group;
39   my $group_size;
40
41   if (!$TileMap->{$id})
42   {
43     return ($id);
44   }
45
46   $group = $TileMap->{$id};
47   $group_size = 0 + @$group;
48
49   return ($group->[int (rand ($group_size))]);
50 }
51
52 if (-t STDIN)
53 {
54         print STDERR "Usage: ./tile_randomizer.px <in_file >out_file\n";
55         exit (1);
56 }
57
58 init ();
59   
60 while (<>)
61 {
62   my $line = $_;
63   chomp ($line);
64
65   if ($line =~ m/[^\s\d]/)
66   {
67     print "$line\n";
68     next;
69   }
70
71   $line =~ s/\b([1-9][0-9]{0,3})\b/get_random ($1)/ge;
72
73   print "$line\n";
74 }
75
76 exit (0);