Coord tripler and grid tester

main
Daniel Asher Resnick 4 months ago
parent 6acfb81512
commit 5c2c1c5afc
  1. 47
      coord_tripler.pl
  2. 68
      grid-tester.pl
  3. 3
      test-regions

@ -0,0 +1,47 @@
use v5.36;
use rlib '.';
use Carp;
use Data::Dumper;
use feature "signatures";
no warnings "experimental::signatures";
my $coords_regex = qr/^\s*(-?\d+)\s*,\s*(-?\d+)\s*$/;
my %regions_to_tiles;
my %tiles_to_regions;
my $input_file = shift;
open (my $input_fh, $input_file);
my $output_file = shift // "$input_file.out";
open (my $output_fh, ">$output_file");
while (my $line = <$input_fh>)
{
my @fields = split '; ', $line;
print $output_fh (shift @fields) . "; "; #region name
print $output_fh (shift @fields) . "; "; #background colour
my @expanded_tile_specs = ();
foreach my $coords (@fields)
{
do { carp "Skipping bad spec: $coords"; next; } unless $coords =~ $coords_regex;
my $nw_base = $1 * 3;
my $sw_base = $2 * 3;
my @new = ("$nw_base,$sw_base");
push @new, ($nw_base+1) . "," . ($sw_base);
push @new, ($nw_base-1) . "," . ($sw_base);
push @new, ($nw_base) . "," . ($sw_base+1);
push @new, ($nw_base) . "," . ($sw_base-1);
push @new, ($nw_base+1) . "," . ($sw_base-1);
push @new, ($nw_base-1) . "," . ($sw_base+1);
push @expanded_tile_specs, (join "; ", @new);
}
say $output_fh (join "; ", @expanded_tile_specs);
}
close $input_fh;
close $output_fh;

@ -0,0 +1,68 @@
use v5.36;
use rlib '.';
use HexGrid;
use HexGrid::Pin;
use Carp;
use Data::Dumper;
use feature "signatures";
no warnings "experimental::signatures";
my $coords_regex = qr/^\s*(-?\d+)\s*,\s*(-?\d+)\s*$/;
my $grid = HexGrid->new(defaults => {
style => { 'stroke-width' => 1, stroke => 'black' },
show_coords => 1});
my $test_file = shift;
open (my $test_fh, $test_file);
while (my $line = <$test_fh>)
{
my @fields = split '; ', $line;
my $region = $grid->make_region(shift @fields);
$region->{defaults}{colour} = shift @fields;
foreach my $coords (@fields)
{
do { carp "Skipping bad spec: $coords"; next; } unless $coords =~ $coords_regex;
$region->make_tile_at($1,$2);
}
}
# say $grid->render;
say wrap_in_html($grid);
close $test_fh;
sub wrap_in_html($grid)
{
my $html_builder = "<!DOCTYPE html>";
$html_builder .= "\n<html>\n<body>";
$html_builder .= "\n" . <<EOS;
<script>
function clickPin(pinId, containerId) {
let popup = document.getElementById(pinId + '-popup');
popup.style.visibility = popup.style.visibility == 'visible' ? 'hidden' : 'visible';
}
</script>
EOS
if(1)
{
$html_builder .= <<EOS;
<script>
function toggleCoords(show) {
for (var elem of document.getElementsByClassName('coords')) {
elem.style.visibility = show ? 'visible' : 'hidden';
}
}
</script>
<label for="show-coords-checkbox">Show coordinates</label>
<input type="checkbox" checked id="show-coords-checkbox" onclick="toggleCoords(event.srcElement.checked)" />
EOS
}
$html_builder .= "\n" . $grid->render;
$html_builder .= "\n</body>\n</html>";
return $html_builder;
}

@ -0,0 +1,3 @@
Minev; #2F7621; 0,1; 0,2; 1,1; 2,0; 0,3; 1,2; 2,1; 1,3; 2,2; 3,1; 1,4; 2,3; 3,2; 2,4; 3,3; 4,2; 3,4; 4,3; 5,2; 4,4; 5,3; 5,4; 6,3; 5,5; 6,5
Buslish; #E7F79C; 0,5; 1,5; 0,6; 2,5; 1,6; 0,7; -1,8; 2,5; 3,5; 2,6; 1,7; 0,8; 4,5; 3,6; 1,8; 0,9; 4,6; 3,7; 2,8; 1,9; 4,7; 3,8; 2,9; 3,9
Midhills; #E7CE5A; 5,6; 4,8; 5,7; 6,6
Loading…
Cancel
Save