From 3b086b0ebe1dea8f88fe31c61ee99be9da57fa5d Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Tue, 2 Jan 2024 14:32:49 -0600 Subject: [PATCH] Check for bad edge direction and abort path --- HexGrid/Path.pm | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/HexGrid/Path.pm b/HexGrid/Path.pm index 3c3e9c1..957c54f 100644 --- a/HexGrid/Path.pm +++ b/HexGrid/Path.pm @@ -1,3 +1,7 @@ +=head1 HexGrid::Path + +=cut + package HexGrid::Path; use v5.30; @@ -34,14 +38,12 @@ sub get_edge_direction($tile1, $tile2) return $HexGrid::DIR{ne} if $nw_diff == 0 && $sw_diff == -1; return $HexGrid::DIR{n} if $nw_diff == 1 && $sw_diff == -1; - #TODO: render and splinter should check this returns successfully; carp("Tiles are not adjacent: " . $tile1->nw . "," . $tile1->sw . "—" . $tile2->nw . "," . $tile2->sw); return undef; } - # Instance sub clone_settings($this) @@ -90,7 +92,13 @@ sub splinter($this, $grid) # Don't set source on first tile if($i >= 1) { - $splinter->{starts_from} = get_edge_direction($this->{tiles}[$i], $this->{tiles}[$i-1]); + my $starting_edge = get_edge_direction($this->{tiles}[$i], $this->{tiles}[$i-1]); + unless($starting_edge) + { + carp("Path " . $this->{id} . " has non-adjacent edges, aborting."); + return; + } + $splinter->{starts_from} = $starting_edge; } push @{$splinter->tiles}, $this->{tiles}[$i]; push @splinters, $splinter; @@ -108,8 +116,14 @@ sub splinter($this, $grid) { # In a splinter but tile not present, set previous tile sink to this missing tile $in_splinter = 0; - $splinters[$#splinters]{ends_to} = - get_edge_direction($this->{tiles}[$i-1], $this->{tiles}[$i]); + my $ending_edge = get_edge_direction($this->{tiles}[$i-1], $this->{tiles}[$i]); + unless($ending_edge) + { + carp("Path " . $this->{id} . " has non-adjacent edges, aborting."); + return; + } + $splinters[$#splinters]{ends_to} = $ending_edge; + } } } @@ -178,6 +192,11 @@ sub render($this, $grid, $svg) my $previous_tile = $current_tile; $current_tile = shift @tiles; my $next_edge = get_edge_direction($previous_tile, $current_tile); + unless($next_edge) + { + carp("Path " . $this->{id} . " has non-adjacent edges, aborting."); + return; + } ($x, $y) = $grid->coords_of_edge($previous_tile->nw, $previous_tile->sw, $next_edge); if($this->starts_from) { @@ -199,6 +218,11 @@ sub render($this, $grid, $svg) $next_tile = shift @tiles; $previous_edge = -$next_edge; $next_edge = get_edge_direction($current_tile, $next_tile); + unless($next_edge) + { + carp("Path " . $this->{id} . " has non-adjacent edges, aborting."); + return; + } my ($qx,$qy) = $grid->coords_of_centre($current_tile->nw, $current_tile->sw); ($x,$y) = $grid->coords_of_edge($current_tile->nw, $current_tile->sw, $next_edge); @@ -228,4 +252,8 @@ sub render($this, $grid, $svg) return $g; } +=back + +=cut + 1;