--- deliantra/server/ext/map-world.ext 2007/04/17 20:41:15 1.21 +++ deliantra/server/ext/map-world.ext 2011/05/04 07:41:13 1.44 @@ -1,173 +1,148 @@ -#! perl # OPTIONAL +#! perl # mandatory -# optional plug-in to speed up worldmap rendering by dynamically -# generating it out of an image -# - saves loading time (less data to read) -# - saves temporary space (only overlay stuff needs to be saved) -# - might get reused as a generic tiled map +# manages the worldmap by directing to other handlers -use Coro::Handle; -use Coro::AIO; +cf::map->register (qr{^/world/world_(-?\d+)_(-?\d+)(?:_(-?\d+))?$}, 100); -our $WORLD; +# universal +our $TILE_W = 50; +our $TILE_H = 50; -sub load_indexed($$) { - my ($path, $size) = @_; +# registry +our @AREAS; - use bytes; +sub register($$$$$$$@) { + my $meta = { + pkg => shift, + x0 => shift, y0 => shift, z0 => shift, + x1 => shift, y1 => shift, z1 => shift, + @_ + }; - 0 < aio_load "$path.plt", my $plt - or cf::cleanup "$path.plt: $!"; + my $pkg = $meta->{pkg}; - my %plt; - my @plt; + @AREAS = sort { $b->{pri} <=> $a->{pri} } + $meta, + grep $pkg ne $_->{pkg}, + @AREAS; - for (split /\n/, $plt) { - my ($name, $rgb) = split /\s+/; - if (/^(\S+)\s+(\S+)$/) { - my ($name, $rgb) = ($1, $2); - $rgb = join "", map chr, map $_ * 17, map hex, split //, $rgb; - $plt{$rgb} = chr @plt; - push @plt, $name; - } - } - - my $pid = open my $pipe, "-|"; - - defined $pid - or cf::cleanup "fork: $!"; + my $isa = \@{"$pkg\::ISA"}; - unless ($pid) { - eval { - open my $fh, "convert \Q$path.png\E -depth 8 rgb:- |" - or die "convert: $!"; - binmode $fh; - - $size * 3 == read $fh, my $data, $size * 3 - or die "$path.png, expected $size rgb triplets: $!"; - - cf::_quantise $data, [map "$_$plt{$_}", keys %plt]; - - binmode STDOUT; - syswrite STDOUT, $data; - }; - warn $@ if $@; - cf::_exit; - } - - $pipe = Coro::Handle::unblock $pipe; - binmode $pipe; - $size == read $pipe, my $data, $size - or cf::cleanup "$path.png, expected $size index octets ($!)"; - - ($data, \@plt) + @$isa = ( + __PACKAGE__, + grep __PACKAGE__ ne $_, @$isa + ); } -sub load_gridmap($) { - my ($path) = @_; +sub format_xyz($$;$) { + sprintf "/world/world_%03d_%03d%s", + $_[0], $_[1], + $_[2] ? sprintf "_%03d", $_[2] : ""; +} -# if (! -e "/tmp/xxxx.gridmap") { - 0 < aio_load "$path/gridmap.meta", my $map - or cf::cleanup "$path/gridmap.meta: $!\n"; +############################################################################# - $map = cf::from_json $map; +sub init { + my ($self) = @_; - my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h} - or cf::cleanup "$path/gridmap.meta: empty gridmap?"; + my ($x, $y, $z) = @$self{qw(x y z)} = ($1, $2, $3+0); - ($map->{arc_data}, $map->{arc_plt}) = load_indexed "$path/gridmap.arch", $size; - ($map->{reg_data}, $map->{reg_plt}) = load_indexed "$path/gridmap.regn", $size; + # find handler for this area + for my $meta (@AREAS) { + if ( $meta->{x0} <= $x && $x <= $meta->{x1} + && $meta->{y0} <= $y && $y <= $meta->{y1} + && $meta->{z0} <= $z && $z <= $meta->{z1} + ) { + $self->{ox} = $x - $meta->{x0}; + $self->{oy} = $y - $meta->{y0}; + $self->{oz} = $z - $meta->{z0}; -# Storable::nstore $map, "/tmp/xxxx.gridmap" if 0;#d# + bless $self, $meta->{pkg}; - $map -# } else { -# Storable::retrieve "/tmp/xxxx.gridmap"; -# } -} + $self->init_worldmap ($meta); -# this is contorted, but likely the correct way to acquire the lock :) -cf::sync_job { - my $guard = cf::lock_acquire "ext::world_gridmap"; - cf::async_ext { - $WORLD = load_gridmap sprintf "%s/%s/%s", cf::datadir, cf::mapdir, "world"; - warn "worldmap gridmap loaded."; - undef $guard; + last; + } + } -}; -cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); + $self->SUPER::init; +} -sub wxwy { - $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} - ? ($1, $2) - : (0, 0) +sub init_worldmap { + # only for handlers } sub load_header_orig { my ($self) = @_; - my ($x, $y) = $self->wxwy; + my ($x, $y, $z) = @$self{qw(x y z)}; - my $guard = cf::lock_acquire "ext::world_gridmap"; + $self->width ($TILE_W); + $self->height ($TILE_H); - $self->width ($WORLD->{tile_w}); - $self->height ($WORLD->{tile_h}); - - $self->name ("'The World' at +$x+$y"); + $self->name (sprintf "'The World' at %+d%+d%+d", $x, $y, $z); $self->msg ("worldmap dynamically created by map-world extension"); - $self->outdoor (1); + $self->outdoor ($self->{z} >= 0); $self->default_region (cf::region::find "wilderness"); - $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; - $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; - $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; - $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; + # all possible worldmap tiles also exist + $self->tile_path (0, format_xyz $x, $y - 1, $z); + $self->tile_path (1, format_xyz $x + 1, $y, $z); + $self->tile_path (2, format_xyz $x, $y + 1, $z); + $self->tile_path (3, format_xyz $x - 1, $y, $z); + $self->tile_path (4, format_xyz $x, $y, $z + 1); + $self->tile_path (5, format_xyz $x, $y, $z - 1); - $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d.map", cf::datadir, cf::mapdir, $x, $y - if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; + # an overlay always wins + my $overlay = sprintf "%s/%s.map", $cf::MAPDIR, format_xyz $x, $y, $z; - 1 -} + $self->{load_path} = $overlay + unless Coro::AIO::aio_stat $overlay; -sub fill { - my ($self) = @_; + $self->{need_create_treasure} = 1; - $self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt}); - $self->default_region (cf::region::find $WORLD->{reg_plt}[0]); + 1 } -sub load { +# fill map with "default" data - first region and first archetype from palette +# used when we have no map data from elsewhere +sub fill { my ($self) = @_; - if ($self->{load_path}) { - $self->SUPER::load; + if ($self->{z} > 0) { + # hmmm... air? :) + $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]); + } elsif ($self->{z} < 0) { + $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]); } else { - $self->alloc; - $self->fill; - $self->in_memory (cf::MAP_IN_MEMORY); + $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["deep_sea"]); } } +#sub load { +# my ($self) = @_; +# +# if ($self->{load_path}) { +# warn "load $self\n";#d# +# $self->SUPER::load; +# } else { +# warn "fill $self\n";#d# +# my $guard = cf::lock_acquire "map_data:$self->{path}"; +# +# $self->alloc; +# $self->fill; +# $self->state (cf::MAP_ACTIVE); +# $self->activate; +# $self->post_load; +# } +#} + +# MUST be overwritten to call at least ->fill sub post_load { my ($self) = @_; - my $guard = cf::lock_acquire "ext::world_gridmap"; - - my ($x, $y) = $self->wxwy; - - if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { - my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; - my $top = ($y - 100) * $WORLD->{tile_h} * $stride - + ($x - 100) * $WORLD->{tile_w}; - - $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt}); - $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt}); - - } else { - $self->fill; - } + $self->create_region_treasure + if delete $self->{need_create_treasure}; } -1 -