#!perl
#
# civIV map parser
###################################################
# $extra_width = 5;
$off{top} = 2;
$off{left} = 4;
$/ = "Begin";
open(NEWMAP,">newmap.Civ4WarlordsWBSave") || die "$! tyring to create newmap\n";
select NEWMAP;
while(<>){
if ( /^(\S+)/ ){
$type = $1;
if ( $type eq "Plot" ){
if ( /x=(\d+)\s*,y=(\d+)/ ){
$x_coord = $1;
$y_coord = $2;
}
if ($x_coord < $off{left} ){
next;
} else {
$new_x_coord = $x_coord - $off{left}; # move all plots left 1
$_ =~s/x=$x_coord/x=$new_x_coord/;
}
next if ( $y_coord >= $newMapHeight ); # skip top plots
}
if ( $type eq "Map" ){
if ( /grid width=(\d+)/ ){
$map_width = $1;
if ( $extra_width ) {
$new_map_width = $map_width + $extra_width;
} elsif ( exists $off{left} ) {
$new_map_width = $map_width - $off{left};
} else {
$new_map_width = $map_width;
}
$_ =~s/grid width=$map_width/grid width=$new_map_width/;
}
if ( /grid height=(\d+)/ ){
$map_height = $1;
$newMapHeight = $map_height - $off{top};
# $new_map_height = $map_height -1; # remove original bottom row
$_ =~s/grid height=$map_height/grid height=$newMapHeight/;
}
if ( /num plots written=(\d+)/ ){
$new_num_plots = $new_map_width * $newMapHeight;
$_ =~s/num plots written=\d+/num plots written=$new_num_plots/;
}
}
}
print;
}