ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/map-world.ext
(Generate patch)

Comparing deliantra/server/ext/map-world.ext (file contents):
Revision 1.40 by root, Fri Apr 22 02:03:11 2011 UTC vs.
Revision 1.45 by root, Wed May 4 16:12:15 2011 UTC

1#! perl # optional 1#! perl # mandatory
2 2
3# optional plug-in to speed up worldmap rendering by dynamically 3# manages the worldmap by directing to other handlers
4# generating it out of an image
5# - saves loading time (less data to read)
6# - saves temporary space (only overlay stuff needs to be saved)
7# - might get reused as a generic tiled map
8 4
9cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); 5cf::map->register (qr{^/world/world_(-?\d+)_(-?\d+)(?:_(-?\d+))?$}, 100);
10 6
11use Coro::Handle; 7# universal
12use Coro::AIO; 8our $TILE_W = 50;
9our $TILE_H = 50;
13 10
14my $TILE_W = 50; 11# registry
15my $TILE_H = 50; 12our @AREAS;
16 13
17my $GRID_W = 30; 14sub register($$$$$$$@) {
18my $GRID_H = 30; 15 my $meta = {
16 pkg => shift,
17 x0 => shift, y0 => shift, z0 => shift,
18 x1 => shift, y1 => shift, z1 => shift,
19 @_
20 };
19 21
20our $ARCH; 22 my $pkg = $meta->{pkg};
21our $REGN;
22 23
23sub reload() { 24 @AREAS = sort { $b->{pri} <=> $a->{pri} }
24 cf::trace "loading world+100+100 table.\n"; 25 $meta,
25 $ARCH = cf::decode_storable cf::unlzf cf::load_file "$DATADIR/world+100+100.arch"; 26 grep $pkg ne $_->{pkg},
26 $REGN = cf::decode_storable cf::unlzf cf::load_file "$DATADIR/world+100+100.regn"; 27 @AREAS;
27 cf::trace "loaded world+100+100 table.\n"; 28
29 my $isa = \@{"$pkg\::ISA"};
30
31 @$isa = (
32 __PACKAGE__,
33 grep __PACKAGE__ ne $_, @$isa
34 );
28} 35}
29 36
30{ 37sub format_xyz($$;$) {
31 my $guard = cf::lock_acquire "ext::world_gridmap"; 38 sprintf "/world/world_%03d_%03d%s",
32 39 $_[0], $_[1],
33 cf::post_init { 40 $_[2] ? sprintf "_%03d", $_[2] : "";
34 cf::async_ext {
35 $Coro::current->{desc} = "worldmap loader";
36 reload;
37 undef $guard;
38 };
39 };
40} 41}
41 42
42sub wxwy { 43#############################################################################
43 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} 44
44 ? ($1, $2) 45sub init {
45 : (0, 0) 46 my ($self) = @_;
47
48 my ($x, $y, $z) = @$self{qw(x y z)} = ($1, $2, $3+0);
49
50 # find handler for this area
51 for my $meta (@AREAS) {
52 if ( $meta->{x0} <= $x && $x <= $meta->{x1}
53 && $meta->{y0} <= $y && $y <= $meta->{y1}
54 && $meta->{z0} <= $z && $z <= $meta->{z1}
55 ) {
56 $self->{ox} = $x - $meta->{x0};
57 $self->{oy} = $y - $meta->{y0};
58 $self->{oz} = $z - $meta->{z0};
59
60 bless $self, $meta->{pkg};
61
62 $self->init_worldmap ($meta);
63
64 last;
65 }
66 }
67
68 $self->SUPER::init;
69}
70
71sub init_worldmap {
72 # only for handlers
46} 73}
47 74
48sub load_header_orig { 75sub load_header_orig {
49 my ($self) = @_; 76 my ($self) = @_;
50 77
51 my ($x, $y) = $self->wxwy; 78 my ($x, $y, $z) = @$self{qw(x y z)};
52
53 my $guard = cf::lock_acquire "ext::world_gridmap";
54 79
55 $self->width ($TILE_W); 80 $self->width ($TILE_W);
56 $self->height ($TILE_H); 81 $self->height ($TILE_H);
57 82
58 $self->name ("'The World' at +$x+$y"); 83 $self->name (sprintf "'The World' at %+d%+d%+d", $x, $y, $z);
59 $self->msg ("worldmap dynamically created by map-world extension"); 84 $self->msg ("worldmap dynamically created by map-world extension");
60 $self->outdoor (1); 85 $self->outdoor ($self->{z} >= 0);
61 $self->default_region (cf::region::find "wilderness"); 86 $self->default_region (cf::region::find "wilderness");
62 87
63 $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; 88 # all possible worldmap tiles also exist
64 $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; 89 $self->tile_path (0, format_xyz $x, $y - 1, $z);
65 $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; 90 $self->tile_path (1, format_xyz $x + 1, $y, $z);
66 $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; 91 $self->tile_path (2, format_xyz $x, $y + 1, $z);
92 $self->tile_path (3, format_xyz $x - 1, $y, $z);
93 $self->tile_path (4, format_xyz $x, $y, $z + 1);
94 $self->tile_path (5, format_xyz $x, $y, $z - 1);
67 95
96 # an overlay always wins
68 my $overlay = sprintf "%s/world/world_%03d_%03d.map", $cf::MAPDIR, $x, $y; 97 my $overlay = sprintf "%s/%s.map", $cf::MAPDIR, format_xyz $x, $y, $z;
69 98
70 $self->{load_path} = $overlay 99 $self->{load_path} = $overlay
71 unless Coro::AIO::aio_stat $overlay; 100 unless Coro::AIO::aio_stat $overlay;
72 101
73 $self->{need_create_treasure} = 1; 102 $self->{need_create_treasure} = 1;
74 103
75 1 104 1
76} 105}
77 106
107# fill map with "default" data - first region and first archetype from palette
108# used when we have no map data from elsewhere
78sub fill { 109sub fill {
79 my ($self) = @_; 110 my ($self) = @_;
80 111
112 if ($self->{z} > 0) {
113 # hmmm... air? :)
81 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, $ARCH->{plt}); 114 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]);
82 $self->default_region (cf::region::find $REGN->{plt}[0]); 115 } elsif ($self->{z} < 0) {
83} 116 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]);
84
85sub load {
86 my ($self) = @_;
87
88 if ($self->{load_path}) {
89 $self->SUPER::load;
90 } else { 117 } else {
91 $self->alloc; 118 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["deep_sea"]);
92 $self->fill;
93 $self->in_memory (cf::MAP_ACTIVE);
94 $self->activate;
95 } 119 }
96} 120}
97 121
122#sub load {
123# my ($self) = @_;
124#
125# if ($self->{load_path}) {
126# warn "load $self\n";#d#
127# $self->SUPER::load;
128# } else {
129# warn "fill $self\n";#d#
130# my $guard = cf::lock_acquire "map_data:$self->{path}";
131#
132# $self->alloc;
133# $self->fill;
134# $self->state (cf::MAP_ACTIVE);
135# $self->activate;
136# $self->post_load;
137# }
138#}
139
140# MUST be overwritten to call at least ->fill
98sub post_load { 141sub post_load {
99 my ($self) = @_; 142 my ($self) = @_;
100
101 {
102 my $guard = cf::lock_acquire "ext::world_gridmap";
103
104 my ($x, $y) = $self->wxwy;
105
106 if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) {
107 my $stride = $GRID_W * $TILE_W;
108 my $top = ($y - 100) * $TILE_H * $stride
109 + ($x - 100) * $TILE_W;
110
111 $self->add_underlay ($ARCH->{tbl}, $top, $stride, $ARCH->{plt});
112 $self->set_regiondata ($REGN->{tbl}, $top, $stride, $REGN->{plt});
113
114 } else {
115 $self->fill;
116 }
117 }
118 143
119 $self->create_region_treasure 144 $self->create_region_treasure
120 if delete $self->{need_create_treasure}; 145 if delete $self->{need_create_treasure};
121} 146}
122 147

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines