package Data::StarCraft::Tileset;

use strict;
use warnings;
use Data::Dumper;

sub open {
	my ($class, $filename) = @_;
	my $self = bless {}, $class;
	open my $groupfile, '<', "$filename.cv5" or return;
	$self->readgroups($groupfile);
	# vf4 = minitile type
	# vr4 = minitile sprites
	# vx4 = minitile map?
	return $self;
}

sub _read {
	my $self = shift;
	my ($fh, $size, $seek) = @_;
	seek $fh, $seek, 0 if $seek;
	read($fh, my $in, $size) eq $size or return undef;
	return $in;
}

sub readgroups {
	my $self = shift;
	my ($fh) = @_;
	my $id = 0;
	my @tilemap;
	while (defined (my $line = $self->_read($fh, 52))) {
		my @data = unpack "vCC v24", $line;
		my %row = (
			build => $data[1] & 0xF0,
			height => $data[2] & 0xF,
			walk => undef,
		);
		$self->{group}->[$id] = \%row;
		push @tilemap, $_ for @data[11..26];
		$id++;
	}
	$self->{tile} = \@tilemap;
	return;
	# index (ground_height unknown4 buildable unknown3) u6 u7 u8 u9 u10 u11 u12 u13
	# 1 (ground_height unknown4 buildable unknown3) 35 0 doodad_group 0 58 6 6 0
}

1;

