File indexing completed on 2024-04-14 03:40:32

0001 #!/usr/bin/perl
0002 #
0003 #
0004 # Copyright (C) 2004 by Yann Verley
0005 # yann.verley@free.fr
0006 #
0007 # This program is free software; you can redistribute it and/or modify
0008 # it under the terms of the GNU General Public License as published by
0009 # the Free Software Foundation; either version 2 of the License, or
0010 # (at your option) any later version.
0011 #
0012 #
0013 #
0014 # Last modification : 12 May 2004
0015 
0016 use strict;
0017 
0018 sub print_colors
0019 {
0020     my ($red, $green, $blue) = @_;
0021 
0022     print "\t\t<color>\n";
0023     print "\t\t\t<red>$red</red>\n";
0024     print "\t\t\t<green>$green</green>\n";
0025     print "\t\t\t<blue>$blue</blue>\n";
0026     print "\t\t</color>\n";
0027 }
0028 
0029 sub print_header
0030 {
0031     my ($map_file, $map_name) = @_;
0032 
0033     print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
0034     print "<map>\n";
0035 
0036     print "\t<mapFile>$map_file</mapFile>\n";
0037     print "\t<name>$map_name</name>\n";
0038 }
0039 
0040 sub print_new_division
0041 {
0042     my ($name, $capital, $flag, $ignore, $red, $green, $blue) = @_;
0043 
0044     print "\t<division>\n";
0045     
0046     print "\t\t<name>$name</name>\n";
0047     
0048     if ($ignore eq "yes")
0049     {
0050         print "\t\t<ignore>yes</ignore>\n";
0051     }
0052 
0053     if ($flag ne "")
0054     {
0055         print "\t\t<flag>$flag</flag>\n";
0056     }
0057 
0058     if ($capital ne "")
0059     {
0060         print "\t\t<capital>$capital</capital>\n";
0061     }
0062 
0063     print_colors($red, $green, $blue);
0064 
0065     print "\t</division>\n";
0066 }
0067 
0068 sub print_end_of_file
0069 {
0070     print "</map>\n";
0071 }
0072 
0073 sub usage
0074 {
0075     print "Usage : gen_map.pl division_list.txt [ignored_list.txt]\n\n";
0076 
0077     print "This script produces a kgm file to be used with KGeography.\n";
0078     print "It produces too a file, gimp_rgb.txt. This file can be used to help you\n";
0079     print "to fill the divisions in your map with an image processing program.\n\n";
0080 
0081     print "Example :\n";
0082     print "gen_map.pl spain.txt ignored.txt >spain.kgm\n\n";
0083     
0084     print "Example 1 of division_list.txt :\n";
0085     print "#division name;capital\n";
0086     print "Andalucia;Seville\n";
0087     print "Aragon;Zaragoza\n\n";
0088     
0089     print "Example 2 of division_list.txt :\n";
0090     print "#division name;capital;flag file\n";
0091     print "Germany;Berlin;germany.png\n";
0092     print "France;Paris;france.png\n\n";
0093     
0094     print "Example of ignored_list.txt :\n";
0095     print "#division name;red value;green value;blue value\n";
0096     print "Frontier;0;0;0\n";
0097     print "Water;50;50;255\n";
0098     exit;
0099 }
0100 
0101 #####################
0102 #BEGINNING OF PROGRAM
0103 #####################
0104 
0105 my @div_name_list;
0106 my @div_capital_list;
0107 my @div_flag_list;
0108 my @ign_name_list;
0109 my @ign_red_list;
0110 my @ign_green_list;
0111 my @ign_blue_list;
0112 
0113 
0114 if (($ARGV[0] eq "") or ($ARGV[0] eq "-h"))
0115 {
0116     usage();
0117 }
0118 
0119 open(DIVFILE, "<".$ARGV[0]) or die "Can't open file $ARGV[0] : $!";
0120 
0121 while (<DIVFILE>)
0122 {
0123     next if (/^#/);
0124     chomp;
0125 
0126     my @spl = split /;/;
0127 
0128     push @div_name_list, $spl[0];
0129     push @div_capital_list, $spl[1];
0130     push @div_flag_list, $spl[2];
0131 }
0132 
0133 close(DIVFILE);
0134 
0135 if (open(IGNFILE, "<".$ARGV[1]))
0136 {
0137     while (<IGNFILE>)
0138     {
0139         next if (/^#/);
0140         chomp;
0141 
0142         my @spl = split /;/;
0143 
0144         push @ign_name_list, $spl[0];
0145         push @ign_red_list, $spl[1];
0146         push @ign_green_list, $spl[2];
0147         push @ign_blue_list, $spl[3];
0148     }
0149 
0150     close(IGNFILE);
0151 }
0152 
0153 my $basename = $ARGV[0];
0154 $basename =~ s/\.[[:alnum:]]*$//;
0155 
0156 print_header($basename.".png", ucfirst($basename));
0157 
0158 for (my $i = 0; $i <= $#ign_name_list ; $i++)
0159 {
0160     print_new_division($ign_name_list[$i],
0161                "",
0162                "",
0163                "yes",
0164                $ign_red_list[$i],
0165                $ign_green_list[$i],
0166                $ign_blue_list[$i]);
0167 }
0168 
0169 my $min_red = 130;
0170 my $min_green = 130;
0171 my $min_blue = 130;
0172 my $max_red = 250;
0173 my $max_green = 250;
0174 my $max_blue = 250;
0175 
0176 my @red_list;
0177 my @green_list;
0178 my @blue_list;
0179 
0180 my $red;
0181 my $green;
0182 my $blue;
0183 
0184 my $step;
0185 
0186 if ($#div_name_list < 25)
0187 {
0188     $step = 60;
0189 }
0190 elsif ($#div_name_list < 60)
0191 {
0192     $step = 40;
0193 }
0194 elsif ($#div_name_list < 120)
0195 {
0196     $step = 30;
0197 }
0198 elsif ($#div_name_list < 210)
0199 {
0200     $step = 24;
0201 }
0202 elsif ($#div_name_list < 340)
0203 {
0204     $step = 20;
0205 }
0206 else
0207 {
0208     # > 700 divisions allowed
0209     $step = 15;
0210 }
0211 
0212 #now we build list of colors which will be used to colorize the map
0213 for ($red = $min_red ; $red <= $max_red ; $red += $step)
0214 {
0215     for ($green = $min_green ; $green <= $max_green ; $green += $step)
0216     {
0217         for ($blue = $min_blue ; $blue <= $max_blue ; $blue += $step)
0218         {
0219             push @red_list, $red;
0220             push @green_list, $green;
0221             push @blue_list, $blue;
0222         }
0223     }
0224 }
0225 
0226 open(RGB_FILE, ">gimp_rgb.txt") or die "Cannot open file : $!";
0227 
0228 for (my $i = 0 ; $i <= $#div_name_list ; $i++)
0229 {
0230     print_new_division($div_name_list[$i],
0231             $div_capital_list[$i],
0232             $div_flag_list[$i],
0233             "no",
0234             $red_list[$i],
0235             $green_list[$i],
0236             $blue_list[$i]);
0237 
0238     printf RGB_FILE "%s : %X%X%X\n", $div_name_list[$i],
0239             $red_list[$i], $green_list[$i], $blue_list[$i];
0240 }
0241 
0242 close(RGB_FILE);
0243 
0244 print_end_of_file();
0245