--- deliantra/server/ext/map-world.ext 2007/04/14 07:22:59 1.20 +++ deliantra/server/ext/map-world.ext 2011/04/22 02:03:11 1.40 @@ -1,4 +1,4 @@ -#! perl # OPTIONAL +#! perl # optional # optional plug-in to speed up worldmap rendering by dynamically # generating it out of an image @@ -6,100 +6,38 @@ # - saves temporary space (only overlay stuff needs to be saved) # - might get reused as a generic tiled map +cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); + use Coro::Handle; use Coro::AIO; -our $WORLD; - -sub load_indexed($$) { - my ($path, $size) = @_; +my $TILE_W = 50; +my $TILE_H = 50; - use bytes; +my $GRID_W = 30; +my $GRID_H = 30; - 0 < aio_load "$path.plt", my $plt - or cf::cleanup "$path.plt: $!"; - - my %plt; - my @plt; - - 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; - } - } +our $ARCH; +our $REGN; - my $pid = open my $pipe, "-|"; - - defined $pid - or cf::cleanup "fork: $!"; - - unless ($pid) { - eval { - open my $fh, "convert \Q$path.png\E -depth 8 rgb:- |" - or die "convert: $!"; - binmode $fh; - - binmode $fh; - $size * 3 == read $fh, my $data, $size * 3 - or die "$path.png, expected $size rgb triplets: $!"; - - $data =~ s{(...)}{ - $plt{$1} or die "$path.png: color not in palette: #" . unpack "H*", $1 - }sge; - - 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) +sub reload() { + cf::trace "loading world+100+100 table.\n"; + $ARCH = cf::decode_storable cf::unlzf cf::load_file "$DATADIR/world+100+100.arch"; + $REGN = cf::decode_storable cf::unlzf cf::load_file "$DATADIR/world+100+100.regn"; + cf::trace "loaded world+100+100 table.\n"; } -sub load_gridmap($) { - my ($path) = @_; - -# 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; - - my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h} - or cf::cleanup "$path/gridmap.meta: empty gridmap?"; - - ($map->{arc_data}, $map->{arc_plt}) = load_indexed "$path/gridmap.arch", $size; - ($map->{reg_data}, $map->{reg_plt}) = load_indexed "$path/gridmap.regn", $size; - -# Storable::nstore $map, "/tmp/xxxx.gridmap" if 0;#d# - - $map -# } else { -# Storable::retrieve "/tmp/xxxx.gridmap"; -# } -} - -# 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; - } -}; -cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); + cf::post_init { + cf::async_ext { + $Coro::current->{desc} = "worldmap loader"; + reload; + undef $guard; + }; + }; +} sub wxwy { $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} @@ -114,8 +52,8 @@ my $guard = cf::lock_acquire "ext::world_gridmap"; - $self->width ($WORLD->{tile_w}); - $self->height ($WORLD->{tile_h}); + $self->width ($TILE_W); + $self->height ($TILE_H); $self->name ("'The World' at +$x+$y"); $self->msg ("worldmap dynamically created by map-world extension"); @@ -127,8 +65,12 @@ $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; - $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; + my $overlay = sprintf "%s/world/world_%03d_%03d.map", $cf::MAPDIR, $x, $y; + + $self->{load_path} = $overlay + unless Coro::AIO::aio_stat $overlay; + + $self->{need_create_treasure} = 1; 1 } @@ -136,8 +78,8 @@ sub fill { my ($self) = @_; - $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]); + $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, $ARCH->{plt}); + $self->default_region (cf::region::find $REGN->{plt}[0]); } sub load { @@ -148,29 +90,33 @@ } else { $self->alloc; $self->fill; - $self->in_memory (cf::MAP_IN_MEMORY); + $self->in_memory (cf::MAP_ACTIVE); + $self->activate; } } sub post_load { my ($self) = @_; - my $guard = cf::lock_acquire "ext::world_gridmap"; + { + my $guard = cf::lock_acquire "ext::world_gridmap"; - my ($x, $y) = $self->wxwy; + 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}; + if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { + my $stride = $GRID_W * $TILE_W; + my $top = ($y - 100) * $TILE_H * $stride + + ($x - 100) * $TILE_W; - $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt}); - $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt}); + $self->add_underlay ($ARCH->{tbl}, $top, $stride, $ARCH->{plt}); + $self->set_regiondata ($REGN->{tbl}, $top, $stride, $REGN->{plt}); - } else { - $self->fill; + } else { + $self->fill; + } } -} -1 + $self->create_region_treasure + if delete $self->{need_create_treasure}; +}