--- deliantra/server/ext/map-world.ext 2011/04/22 02:03:11 1.40 +++ deliantra/server/ext/map-world.ext 2011/05/04 16:12:15 1.45 @@ -1,71 +1,100 @@ -#! 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 -cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); +cf::map->register (qr{^/world/world_(-?\d+)_(-?\d+)(?:_(-?\d+))?$}, 100); -use Coro::Handle; -use Coro::AIO; +# universal +our $TILE_W = 50; +our $TILE_H = 50; -my $TILE_W = 50; -my $TILE_H = 50; +# registry +our @AREAS; -my $GRID_W = 30; -my $GRID_H = 30; +sub register($$$$$$$@) { + my $meta = { + pkg => shift, + x0 => shift, y0 => shift, z0 => shift, + x1 => shift, y1 => shift, z1 => shift, + @_ + }; + + my $pkg = $meta->{pkg}; -our $ARCH; -our $REGN; + @AREAS = sort { $b->{pri} <=> $a->{pri} } + $meta, + grep $pkg ne $_->{pkg}, + @AREAS; -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"; + my $isa = \@{"$pkg\::ISA"}; + + @$isa = ( + __PACKAGE__, + grep __PACKAGE__ ne $_, @$isa + ); } -{ - my $guard = cf::lock_acquire "ext::world_gridmap"; +sub format_xyz($$;$) { + sprintf "/world/world_%03d_%03d%s", + $_[0], $_[1], + $_[2] ? sprintf "_%03d", $_[2] : ""; +} - cf::post_init { - cf::async_ext { - $Coro::current->{desc} = "worldmap loader"; - reload; - undef $guard; - }; - }; +############################################################################# + +sub init { + my ($self) = @_; + + my ($x, $y, $z) = @$self{qw(x y z)} = ($1, $2, $3+0); + + # 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}; + + bless $self, $meta->{pkg}; + + $self->init_worldmap ($meta); + + last; + } + } + + $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 $guard = cf::lock_acquire "ext::world_gridmap"; + my ($x, $y, $z) = @$self{qw(x y z)}; $self->width ($TILE_W); $self->height ($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); - my $overlay = sprintf "%s/world/world_%03d_%03d.map", $cf::MAPDIR, $x, $y; + # an overlay always wins + my $overlay = sprintf "%s/%s.map", $cf::MAPDIR, format_xyz $x, $y, $z; $self->{load_path} = $overlay unless Coro::AIO::aio_stat $overlay; @@ -75,47 +104,43 @@ 1 } +# 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) = @_; - $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 { - 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_ACTIVE); - $self->activate; + $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 = $GRID_W * $TILE_W; - my $top = ($y - 100) * $TILE_H * $stride - + ($x - 100) * $TILE_W; - - $self->add_underlay ($ARCH->{tbl}, $top, $stride, $ARCH->{plt}); - $self->set_regiondata ($REGN->{tbl}, $top, $stride, $REGN->{plt}); - - } else { - $self->fill; - } - } - $self->create_region_treasure if delete $self->{need_create_treasure}; }