# ATITD: Third Telling
# ====================
#
# Third telling of ATITD, contains specific information about regions and
# chariot stops. Also knows what the map and where the wiki are.

package ATITD::Tale3;

use strict;
use warnings;

use constant {
	# URL of the Wiki for that telling
	WIKI_URL => 'http://wiki.atitd.net/tale3',
	
	# Boundaries of the map (left, top, right, bottom)
	MAP_BOUNDARIES => [-3071,  8191,
						5119, -8191],
	
	# Name of the region and the coordinates of its chariot stop
	REGIONS => {
		'Cradle of the Sun' => [ 4292,  -632],
		'Desert of Shades'  => [-1419,  2769],
		'Fool\'s Paradise'  => [-2092,  -480],
		'Karnak'            => [ 1402,  -861],
		'Khartoum'          => [  524, -6145],
		'Kush'              => [  826, -3654],
		'Lower Egypt'       => [ 1029,  6958],
		'Qatara'            => [ -403,  6586],
		'Seven Lakes'       => [ 1419,  1794],
		'Sinai'             => [ 3205,  4357],
		'Tanis'             => [ 4286,  7624],
		'Upper Egypt'       => [ 1696,  3718],
	},
	
	# Alias used for regions
	ALIAS => {
		'CotS'           => 'Cradle of the Sun',
		'DoS'            => 'Desert of Shades',
		'FP'             => 'Fool\'s Paradise',
		'Fools Paradise' => 'Fool\'s Paradise',
		'LE'             => 'Lower Egypt',
		'SL'             => 'Seven Lakes',
		'7L'             => 'Seven Lakes',
		'UE'             => 'Upper Egypt',
	},
};

use ATITD::Map;
use ATITD::Region;
use ATITD::Wiki;

# Extensions
use ATITD::Map::Chariot;
use ATITD::Wiki::Chariot;

# Create a new third telling (don't forget to distribute the stones in the
# desert!)
sub new {
	my ($class) = @_;
	my $self = bless {
		alias => {map { lc $_ => ALIAS->{$_} } keys %{&ALIAS}},
		bounds => MAP_BOUNDARIES,
	}, $class;
	
	$self->{regions} = {map {
		lc $_ => ATITD::Region->new($self, $_, REGIONS->{$_})
	} keys %{&REGIONS}};
	
	return $self;
}

# Create a map of this telling
sub map {
	my ($self, $filename) = @_;
	
	return ATITD::Map->new($self, $filename);
}

# Create a reference to the wiki of this telling; optionally take a page to load
sub wiki {
	my ($self, $page) = @_;
	
	return ATITD::Wiki->new($self, WIKI_URL, $page);
}

# Which region name is behind this alias?
sub alias { $_[0]->{alias}->{lc $_[1]} }

# Region with this name or alias
sub region {
	defined $_[0]->{regions}->{lc $_[1]}
		? $_[0]->{regions}->{lc $_[1]} 
		: $_[0]->{regions}->{lc $_[0]->alias($_[1])}
}

# All regions in this telling
sub regions { wantarray ? values %{$_[0]->{regions}} : $_[0]->{regions} }

# Chariot in the region with this name or alias
sub chariot { 
	defined $_[0]->region($_[1]) 
		? $_[0]->region($_[1])->chariot 
		: undef
}

# All chariot stops in this telling
sub chariots { 
	my %cs = map { $_->name => $_->chariot } $_[0]->regions;
	wantarray ? values %cs : \%cs 
}

# Map boundaries
sub bounds { wantarray ? @{$_[0]->{bounds}} : $_[0]->{bounds} }

# Coordinates of the top border
sub top { $_[0]->bounds->[1] }

# Coordinates of the bottom border
sub bottom { $_[0]->bounds->[3] }

# Coordinates of the left border
sub left { $_[0]->bounds->[0] }

# Coordinates of the right border
sub right { $_[0]->bounds->[2] }

# Width of the map
sub width { $_[0]->right - $_[0]->left }

# Height of the map
sub height { $_[0]->top - $_[0]->bottom }

# Coordinates of the top left corner
sub top_left {
	wantarray
		? ($_[0]->left, $_[0]->top)
		: [$_[0]->left, $_[0]->top]
}

# Coordinates of the top bottom right
sub bottom_right {
	wantarray
		? ($_[0]->right, $_[0]->bottom)
		: [$_[0]->right, $_[0]->bottom]
}

1;
