# use strict; # #TO DO # Virtual GRID SUPPORT SO WE CAN DO POST-PASS 1 my $max_y=200; my $max_x=200; my $rCap=100; my $threshold=65; my $empty= chr(176); my $village="v"; my $town="t"; my $city="c"; my $capital="p"; my @grid = (); for(my $i=0; $i < $max_y;$i++) { for(my $j=0;$j < $max_x; $j++) { my $randi = int(rand($rCap)); push @grid, { x => $j, y => $i, value => $randi, type => $empty, locked => 0 } } } smooth(\@grid); purge(\@grid); print "----- Placing Villages-----\n"; mapVillages(\@grid);showmap(\@grid); #functions sub showmap { my $grid = @_; print "----- MAP $max_x by $max_y\n"; my $currentCell=0; for(my $i=0; $i < $max_y;$i++) { for(my $j=0;$j < $max_x; $j++) { print $grid[$currentCell]{type}; $currentCell++; } print "\n"; } } sub smooth { my($grid) = @_; my $currentCell=0; for(my $i=0; $i < $max_y;$i++) { for(my $j=0;$j < $max_x; $j++) { my @average=(); ######### # SELF push @average, $grid[$currentCell]{value}; #print "S:$grid[$currentCell]{value}\t"; ######### ######### #find UP if($currentCell - $max_x >= 0) { push @average, $grid[$currentCell - $max_x]{value}; #print "U:$grid[$currentCell - $max_x]{value}\t"; } ######### ######### #find DOWN if($currentCell + $max_x >= 0) { push @average, $grid[$currentCell + $max_x]{value}; #print "D:$grid[$currentCell + $max_x]{value}\t"; } ######### ######### #find LEFT if($currentCell - 1 >= 0) { push @average, $grid[$currentCell - 1]{value}; #print "L:$grid[$currentCell - 1]{value}\t"; } ######### ######### #find RIGHT if($currentCell + 1 >= 0) { push @average, $grid[$currentCell + 1]{value}; #print "R:$grid[$currentCell + 1]{value}\t"; } ######### ######### #find UP-L if($currentCell - $max_x+1 >= 0) { push @average, $grid[$currentCell - $max_x+1]{value}; #print "UL:$grid[$currentCell - $max_x+1]{value}\t"; } ######### ######### #find UP-R if($currentCell - $max_x-1 >= 0) { push @average, $grid[$currentCell - $max_x-1]{value}; #print "UR:$grid[$currentCell - $max_x-1]{value}\t"; } ######### ######### #find DOWN-L if($currentCell + $max_x-1 >= 0) { push @average, $grid[$currentCell + $max_x-1]{value}; #print "DL:$grid[$currentCell + $max_x-1]{value}\t"; } ######### ######### #find DOWN-R if($currentCell + $max_x+1 >= 0) { push @average, $grid[$currentCell + $max_x+1]{value}; #print "DR:$grid[$currentCell + $max_x+1]{value}\t"; } ######### my $newAvg=int(&average(\@average)); #print "Changing $grid[$currentCell]{value} to $newAvg\n"; $grid[$currentCell]{value}=$newAvg; $currentCell++; } } } sub average { @_ == 1 or die ('Sub usage: $average = average(\@array);'); my ($array_ref) = @_; my $sum; my $count = scalar @$array_ref; foreach (@$array_ref) { $sum += $_; } return $sum / $count; } sub median { @_ == 1 or die ('Sub usage: $median = median(\@array);'); my ($array_ref) = @_; my $count = scalar @$array_ref; # Sort a COPY of the array, leaving the original untouched my @array = sort { $a <=> $b } @$array_ref; if ($count % 2) { return $array[int($count/2)]; } else { return ($array[$count/2] + $array[$count/2 - 1]) / 2; } } sub purge { my $grid = @_; my $currentCell=0; for(my $i=0; $i < $max_y;$i++) { for(my $j=0;$j < $max_x; $j++) { if ($grid[$currentCell]{value} < $threshold) { $grid[$currentCell]{value}=0} else {$grid[$currentCell]{type} =$village;} $currentCell++; } } } sub mapVillages { my($grid) = @_; my $currentCell=0; for(my $i=0; $i < $max_y;$i++) { for(my $j=0;$j < $max_x; $j++) { if($grid[$currentCell]{type} eq $village) { #################### BIG CODE HERE ### Check Neighbors if also village if so set ourselves as a town. ######### ######### #find UP if(($currentCell - $max_x >= 0) && ($grid[$currentCell - $max_x]{type} eq $village)) { $grid[$currentCell]{type} = $town; $grid[$currentCell - $max_x]{type} = $empty; } ######### ######### #find DOWN if(($currentCell + $max_x >= 0) && ($grid[$currentCell + $max_x]{type} eq $village)) { $grid[$currentCell]{type} = $town; $grid[$currentCell + $max_x]{type} = $empty; } ######### ######### #find LEFT if(($currentCell - 1 >= 0) && ($grid[$currentCell - 1]{type} eq $village)) { $grid[$currentCell]{type} = $town; $grid[$currentCell - 1]{type} = $empty; } ######### #################### END BIG CODE } $currentCell++; } } }