my $tile = $tiles[$rx][$ry][$bz][type];
if (!defined $tile || $$tile != $type_data[$tile_index])
{
$changed = 1;
$$tile = $type_data[$tile_index];
}
####
use strict;
use warnings;
use Benchmark qw(cmpthese);
use constant type => 0;
cmpthese (-1, {
Original => 'org (0, 0, 0)',
Grandfathers => 'gf (0, 0, 0)',
Tillys => 'tilly (0, 0, 0)',
justers => 'juster (0, 0, 0)'
});
sub org {
my ($bz, $bx, $by) = @_;
my $changed;
my @tiles;
my @type_data;
for my $y (0 .. 15) {
for my $x (0 .. 15) {
# this calculates the tile index we are currently at,
# using the x and y coords in this block
my $tile_index = $y + ($x * 16);
# this calculates the real x and y values of this tile
my $rx = ($bx * 16) + $x;
my $ry = ($by * 16) + $y;
# insert type data into the internal map array and
# note that there was change in this block if applicable
if (!defined $tiles[$rx][$ry][$bz][type]
|| $tiles[$rx][$ry][$bz][type] != $type_data[$tile_index])
{
$changed = 1;
$tiles[$rx][$ry][$bz][type] = $type_data[$tile_index];
}
}
}
}
sub gf {
my ($bz, $bx, $by) = @_;
my $changed;
my @tiles;
my @type_data;
my $bxScaled = $bx * 16;
my $byScaled = $by * 16;
for my $x (0 .. 15) {
my $xScaled = 16 * $x;
for my $y (0 .. 15) {
# this calculates the tile index we are currently at,
# using the x and y coords in this block
my $tile_index = $y + $xScaled;
# this calculates the real x and y values of this tile
my $rx = $bxScaled + $x;
my $ry = $byScaled + $y;
# insert type data into the internal map array and
# note that there was change in this block if applicable
my $tile = $tiles[$rx][$ry][$bz][type];
if (!defined $tile || $$tile != $type_data[$tile_index])
{
$changed = 1;
$$tile = $type_data[$tile_index];
}
}
}
}
sub tilly {
my ($bz, $bx, $by) = @_;
my $changed;
my @tiles;
my @type_data;
my $bxScaled = $bx * 16;
my $byScaled = $by * 16;
for my $x (0 .. 15)
{
my $xScaled = 16 * $x;
# These calculations were factored out of the inner loop
my $rx = $bxScaled + $x;
my $tiles_for_rx = $tiles[$rx];
for my $y (0 .. 15)
{
# this calculates the tile index we are currently at,
# using the x and y coords in this block
my $tile_index = $y + $xScaled;
# this calculates the real y value of this tile
my $ry = $byScaled + $y;
# insert type data into the internal map array and
# note that there was change in this block if applicable
my $tile = $tiles_for_rx->[$ry][$bz][type];
if (!defined $tile || $$tile != $type_data[$tile_index])
{
$changed = 1;
$$tile = $type_data[$tile_index];
}
}
}
}
sub juster {
my ($bz, $bx, $by) = @_;
my $changed;
my @tiles;
my @type_data;
my $bxScaledBase = $bx * 16;
my $byScaledBase = $by * 16;
# seemed to help a bit!
my $tile_index=0;
# this didn't help much
my (@bxScaled, @byScaled);
for (0..15) {
push @byScaled, $byScaledBase+$_;
push @bxScaled, $bxScaledBase+$_;
}
for my $x (0 .. 15)
{
my $xScaled = 16 * $x;
# These calculations were factored out of the inner loop
my $rx = $bxScaled[$x];
my $tiles_for_rx = $tiles[$rx];
for my $y (0 .. 15)
{
# this calculates the real y value of this tile
my $ry = $byScaled[$y];
# insert type data into the internal map array and
# note that there was change in this block if applicable
my $tile = $tiles_for_rx->[$ry][$bz][type];
if (!defined $tile || $$tile != $type_data[$tile_index])
{
$changed = 1;
$$tile = $type_data[$tile_index];
}
# I think this works properly, but ruins the benchmark unless
# applied to gf's and tillys.
# if (!exists $tiles_for_rx->[$ry][$bz][type] ||
# $tiles_for_rx->[$ry][$bz][type] != $type_data[$tile_index])
# {
# $changed = 1;
# $tiles_for_rx->[$ry][$bz][type] = $type_data[$tile_index];
# }
++$tile_index;
}
}
}
####
(1 2 3)
(4 5 6) ===> ( 1 2 3 4 5 6 7 8 9 )
(7 8 9) how to do in three dimensions, I dunno...