--- deliantra/server/ext/map-world.ext 2007/02/11 01:38:47 1.11 +++ deliantra/server/ext/map-world.ext 2007/09/10 17:24:36 1.29 @@ -1,4 +1,4 @@ -#! perl # OPTIONAL +#! perl # optional # optional plug-in to speed up worldmap rendering by dynamically # generating it out of an image @@ -32,35 +32,20 @@ } } - my $pid = open my $pipe, "-|"; + my $data = cf::fork_call { + open my $fh, "convert \Q$path.png\E -depth 8 rgb:- |" + or die "convert: $!"; + binmode $fh; - defined $pid - or cf::cleanup "fork: $!"; + $size * 3 == read $fh, my $data, $size * 3 + or die "$path.png, expected $size rgb triplets: $!"; - 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; + cf::_quantise $data, [map "$_$plt{$_}", keys %plt]; - binmode STDOUT; - syswrite STDOUT, $data; - }; - warn $@ if $@; - cf::_exit; - } + $data + }; - $pipe = Coro::Handle::unblock $pipe; - binmode $pipe; - $size == read $pipe, my $data, $size + $size == length $data or cf::cleanup "$path.png, expected $size index octets ($!)"; ($data, \@plt) @@ -69,34 +54,43 @@ 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"; + my $map = cf::cache "ext::map-world/gridmap" => + [ + "$path/gridmap.meta", + "$path/gridmap.arch.plt", + "$path/gridmap.arch.png", + "$path/gridmap.regn.plt", + "$path/gridmap.regn.png", + ], + 1 => sub { + my ($src) = @_; + + my $map = cf::from_json $src->[0]; - $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?"; - 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; - ($map->{arc_data}, $map->{arc_plt}) = load_indexed "$path/gridmap.arch", $size; - ($map->{reg_data}, $map->{reg_plt}) = load_indexed "$path/gridmap.regn", $size; + Coro::Storable::freeze $map + }; - Storable::nstore $map, "/tmp/xxxx.gridmap";#d# + Coro::Storable::thaw $map +} - $map - } else { - Storable::retrieve "/tmp/xxxx.gridmap"; - } +sub reload() { + $WORLD = load_gridmap "$MAPDIR/world"; + warn "worldmap gridmap loaded."; } # 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."; + reload; undef $guard; - } + }; }; cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); @@ -120,23 +114,30 @@ $self->name ("'The World' at +$x+$y"); $self->msg ("worldmap dynamically created by map-world extension"); $self->outdoor (1); - $self->default_region (undef); + $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; -# $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; - $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 100; - $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 129; - $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 129; - $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 100; + my $overlay = sprintf "%s/world/world_%03d_%03d.map", $cf::MAPDIR, $x, $y; - $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d", cf::datadir, cf::mapdir, $x, $y - if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; + $self->{load_path} = $overlay + unless Coro::AIO::aio_stat $overlay; + + $self->{need_create_treasure} = 1; 1 } +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]); +} + sub load { my ($self) = @_; @@ -144,50 +145,35 @@ $self->SUPER::load; } else { $self->alloc; - - for my $X (0 .. $WORLD->{tile_w} - 1) { - Coro::cede; - for my $Y (0 .. $WORLD->{tile_h} - 1) { - my $ob = cf::object::new "deep_sea"; - $self->insert ($ob, $X, $Y); - } - } - $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1); - $self->default_region (cf::region::find "ocean"); - + $self->fill; $self->in_memory (cf::MAP_IN_MEMORY); + $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}; - return - unless $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; + $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt}); + $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt}); - my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; - my $top = ($y - 100) * $WORLD->{tile_h} * $stride - + ($x - 100) * $WORLD->{tile_w}; - - my $reg; - - for my $Y (0 .. $WORLD->{tile_h} - 1) { - Coro::cede; - my $row = substr $WORLD->{arc_data}, $top + $Y * $stride, $WORLD->{tile_w}; - $reg .= substr $WORLD->{reg_data}, $top + $Y * $stride, $WORLD->{tile_w}; - for my $X (0 .. $WORLD->{tile_w} - 1) { - next if grep $_->flag (cf::FLAG_IS_FLOOR), $self->at ($X, $Y); - my $ob = cf::object::new $WORLD->{arc_plt}[ord substr $row, $X]; - $ob->flag (cf::FLAG_NO_MAP_SAVE, 1); - $self->insert ($ob, $X, $Y, undef, cf::INS_ABOVE_FLOOR_ONLY); + } else { + $self->fill; } } - $self->set_regiondata ($reg, $WORLD->{reg_plt}); + $self->create_region_treasure + if delete $self->{need_create_treasure}; } 1