541224c8886a7dfec05a0df674b270d2c49786c1
[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   [1861, 1862], # forest underground gras
15   [1865, 1866], # forest underground stones top
16   [1868, 1872], # forest underground stones left
17   [1869, 1873, 1874], # forest underground stones fill
18   [1871, 1875], # forest underground stones right
19   [1877, 1879], # forest underground stones bottom
20   [2058, 2060, 2061, 2065, 2066, 2070], # ice tiles
21   [2179, 2180, 2181, 2187, 2188, 2189, 2195, 2196, 2197], # snow bg
22   [2171, 2172, 2175, 2176] # those other snow thingies
23 ];
24 our $TileMap = {};
25
26 sub init
27 {
28   for (@$TileGroups)
29   {
30     my $group = $_;
31
32     for (@$group)
33     {
34       my $id = $_;
35
36       $TileMap->{$id} = $group;
37     }
38   }
39 } # init
40
41 sub get_random
42 {
43   my $id = shift;
44   my $group;
45   my $group_size;
46
47   if (!$TileMap->{$id})
48   {
49     return ($id);
50   }
51
52   $group = $TileMap->{$id};
53   $group_size = 0 + @$group;
54
55   return ($group->[int (rand ($group_size))]);
56 }
57
58 if (-t STDIN)
59 {
60         print STDERR "Usage: ./tile_randomizer.px <in_file >out_file\n";
61         exit (1);
62 }
63
64 init ();
65   
66 while (<>)
67 {
68   my $line = $_;
69   chomp ($line);
70
71   if ($line =~ m/[^\s\d]/)
72   {
73     print "$line\n";
74     next;
75   }
76
77   $line =~ s/\b([1-9][0-9]{0,3})\b/get_random ($1)/ge;
78
79   print "$line\n";
80 }
81
82 exit (0);