--- deliantra/server/ext/map-world.ext 2007/01/20 23:39:08 1.2 +++ deliantra/server/ext/map-world.ext 2011/05/08 21:51:26 1.46 @@ -1,96 +1,158 @@ -#! perl # OPTIONAL +#! perl # mandatory -# optional plug-in to speed up worldmap rendering by dynamically -# generating it out of a image -# - saves loading time (less data to read) -# - saves temporary space (only overlay stuff needs to be saved) +# manages the worldmap by directing to other handlers -our $WORLD = Storable::retrieve cf::datadir . "/world.pst" - or die "unable to load world.pst"; +cf::map->register (qr{^/world/world_(-?\d+)_(-?\d+)(?:([+\-]\d+))?$}, 100); -$WORLD->{version} == 1 - or die "world.pst version mismatch, expected version 1, got $WORLD->{version}"; +# universal +our $TILE_W = 50; +our $TILE_H = 50; -cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); +# registry +our @AREAS; + +sub register($$$$$$$@) { + my $meta = { + pkg => shift, + x0 => shift, y0 => shift, z0 => shift, + x1 => shift, y1 => shift, z1 => shift, + @_ + }; + + my $pkg = $meta->{pkg}; + + @AREAS = sort { $b->{pri} <=> $a->{pri} } + $meta, + grep $pkg ne $_->{pkg}, + @AREAS; + + my $isa = \@{"$pkg\::ISA"}; + + @$isa = ( + __PACKAGE__, + grep __PACKAGE__ ne $_, @$isa + ); +} + +sub format_xyz($$;$) { + sprintf "/world/world_%03d_%03d%s", + $_[0], $_[1], + $_[2] ? sprintf "%+04d", $_[2] : ""; +} + +############################################################################# sub init { my ($self) = @_; - 1 + 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); + + goto done; + } + } + + bless $self, ext::map_world::empty::; + +done: + $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) = @_; - $self->width ($WORLD->{tilew}); - $self->height ($WORLD->{tileh}); - - my ($x, $y) = $self->wxwy; + my ($x, $y, $z) = @$self{qw(x y z)}; - $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->width ($TILE_W); + $self->height ($TILE_H); + $self->name (sprintf "'The World' at %+d%+d%+d", $x, $y, $z); $self->msg ("worldmap dynamically created by map-world extension"); + $self->outdoor ($self->{z} >= 0); + $self->default_region (cf::region::find "wilderness"); - #TODO: region - - $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d", cf::datadir, cf::mapdir, $x, $y - if $x >= $WORLD->{minx} && $x <= $WORLD->{maxx} - && $y >= $WORLD->{miny} && $y <= $WORLD->{maxy}; + # all possible worldmap tiles also exist + $self->tile_path (cf::TILE_NORTH, format_xyz $x, $y - 1, $z); + $self->tile_path (cf::TILE_SOUTH, format_xyz $x, $y + 1, $z); + $self->tile_path (cf::TILE_EAST , format_xyz $x + 1, $y, $z); + $self->tile_path (cf::TILE_WEST , format_xyz $x - 1, $y, $z); + $self->tile_path (cf::TILE_UP , format_xyz $x, $y, $z + 1); + $self->tile_path (cf::TILE_DOWN , format_xyz $x, $y, $z - 1); + # 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; + + $self->{need_create_treasure} = 1; 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; - - for my $X (0 .. $WORLD->{tilew} - 1) { - Coro::cede; - for my $Y (0 .. $WORLD->{tileh} - 1) { - my $ob = cf::object::new "deep_sea"; - $self->insert ($ob, $X, $Y); - } - } - $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1); - - $self->in_memory (cf::MAP_IN_MEMORY); + $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["deep_sea"]); } } -sub pre_load { +#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 ($x, $y) = $self->wxwy; + $self->create_region_treasure + if delete $self->{need_create_treasure}; +} - my $stride = ($WORLD->{maxx} - $WORLD->{minx} + 1) * $WORLD->{tilew}; - my $top = ($y - $WORLD->{miny}) * $WORLD->{tileh} * $stride - + ($x - $WORLD->{minx}) * $WORLD->{tilew}; - - for my $Y (0 .. $WORLD->{tileh} - 1) { - Coro::cede; - my $row = substr $WORLD->{data}, $top + $Y * $stride, $WORLD->{tilew}; - for my $X (0 .. $WORLD->{tilew} - 1) { - my $ob = cf::object::new $WORLD->{arch}[ord substr $row, $X]; - $self->insert ($ob, $X, $Y); - } - } +package ext::map_world::empty; - $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1); -} +our @ISA = ext::map_world::; -1 +sub post_load_original { + $_[0]->fill; +}