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.35 by root, Tue Sep 23 05:01:41 2008 UTC vs.
Revision 1.46 by root, Sun May 8 21:51:26 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
14our $WORLD; 11# registry
12our @AREAS;
15 13
16sub load_indexed($$) { 14sub register($$$$$$$@) {
17 my ($path, $size) = @_; 15 my $meta = {
16 pkg => shift,
17 x0 => shift, y0 => shift, z0 => shift,
18 x1 => shift, y1 => shift, z1 => shift,
19 @_
20 };
18 21
19 use bytes; 22 my $pkg = $meta->{pkg};
20 23
21 0 < aio_load "$path.plt", my $plt 24 @AREAS = sort { $b->{pri} <=> $a->{pri} }
22 or cf::cleanup "$path.plt: $!"; 25 $meta,
26 grep $pkg ne $_->{pkg},
27 @AREAS;
23 28
24 my %plt; 29 my $isa = \@{"$pkg\::ISA"};
25 my @plt;
26 30
27 for (split /\n/, $plt) { 31 @$isa = (
28 my ($name, $rgb) = split /\s+/; 32 __PACKAGE__,
29 if (/^(\S+)\s+(\S+)$/) { 33 grep __PACKAGE__ ne $_, @$isa
30 my ($name, $rgb) = ($1, $2); 34 );
31 $rgb = join "", map chr, map $_ * 17, map hex, split //, $rgb; 35}
32 $plt{$rgb} = chr @plt; 36
33 push @plt, $name; 37sub format_xyz($$;$) {
38 sprintf "/world/world_%03d_%03d%s",
39 $_[0], $_[1],
40 $_[2] ? sprintf "%+04d", $_[2] : "";
41}
42
43#############################################################################
44
45sub init {
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 goto done;
34 } 65 }
35 } 66 }
36 67
37 my $data = cf::fork_call { 68 bless $self, ext::map_world::empty::;
38 open my $fh, "convert \Q$path.png\E -depth 8 rgb:- |"
39 or die "convert: $!";
40 binmode $fh;
41 69
42 $size * 3 == read $fh, my $data, $size * 3 70done:
43 or die "$path.png, expected $size rgb triplets: $!"; 71 $self->SUPER::init;
44
45 cf::_quantise $data, [map "$_$plt{$_}", keys %plt];
46
47 $data
48 };
49
50 $size == length $data
51 or cf::cleanup "$path.png, expected $size index octets ($!)";
52
53 ($data, \@plt)
54} 72}
55 73
56sub load_gridmap($) { 74sub init_worldmap {
57 my ($path) = @_; 75 # only for handlers
58
59 my $map = cf::cache "ext::map-world/gridmap" =>
60 [
61 "$path/gridmap.meta",
62 "$path/gridmap.arch.plt",
63 "$path/gridmap.arch.png",
64 "$path/gridmap.regn.plt",
65 "$path/gridmap.regn.png",
66 ],
67 1 => sub {
68 my ($src) = @_;
69
70 my $map = cf::decode_json $src->[0];
71
72 my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h}
73 or cf::cleanup "$path/gridmap.meta: empty gridmap?";
74
75 ($map->{arc_data}, $map->{arc_plt}) = load_indexed "$path/gridmap.arch", $size;
76 ($map->{reg_data}, $map->{reg_plt}) = load_indexed "$path/gridmap.regn", $size;
77
78 Coro::Storable::freeze $map
79 };
80
81 Coro::Storable::thaw $map
82}
83
84sub reload() {
85 $WORLD = load_gridmap "$MAPDIR/world";
86 warn "worldmap gridmap loaded.";
87}
88
89{
90 my $guard = cf::lock_acquire "ext::world_gridmap";
91
92 cf::post_init {
93 cf::async_ext {
94 $Coro::current->{desc} = "worldmap loader";
95 reload;
96 undef $guard;
97 };
98 };
99}
100
101sub wxwy {
102 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$}
103 ? ($1, $2)
104 : (0, 0)
105} 76}
106 77
107sub load_header_orig { 78sub load_header_orig {
108 my ($self) = @_; 79 my ($self) = @_;
109 80
110 my ($x, $y) = $self->wxwy; 81 my ($x, $y, $z) = @$self{qw(x y z)};
111 82
112 my $guard = cf::lock_acquire "ext::world_gridmap"; 83 $self->width ($TILE_W);
84 $self->height ($TILE_H);
113 85
114 $self->width ($WORLD->{tile_w}); 86 $self->name (sprintf "'The World' at %+d%+d%+d", $x, $y, $z);
115 $self->height ($WORLD->{tile_h});
116
117 $self->name ("'The World' at +$x+$y");
118 $self->msg ("worldmap dynamically created by map-world extension"); 87 $self->msg ("worldmap dynamically created by map-world extension");
119 $self->outdoor (1); 88 $self->outdoor ($self->{z} >= 0);
120 $self->default_region (cf::region::find "wilderness"); 89 $self->default_region (cf::region::find "wilderness");
121 90
122 $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; 91 # all possible worldmap tiles also exist
123 $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; 92 $self->tile_path (cf::TILE_NORTH, format_xyz $x, $y - 1, $z);
124 $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; 93 $self->tile_path (cf::TILE_SOUTH, format_xyz $x, $y + 1, $z);
125 $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; 94 $self->tile_path (cf::TILE_EAST , format_xyz $x + 1, $y, $z);
95 $self->tile_path (cf::TILE_WEST , format_xyz $x - 1, $y, $z);
96 $self->tile_path (cf::TILE_UP , format_xyz $x, $y, $z + 1);
97 $self->tile_path (cf::TILE_DOWN , format_xyz $x, $y, $z - 1);
126 98
99 # an overlay always wins
127 my $overlay = sprintf "%s/world/world_%03d_%03d.map", $cf::MAPDIR, $x, $y; 100 my $overlay = sprintf "%s/%s.map", $cf::MAPDIR, format_xyz $x, $y, $z;
128 101
129 $self->{load_path} = $overlay 102 $self->{load_path} = $overlay
130 unless Coro::AIO::aio_stat $overlay; 103 unless Coro::AIO::aio_stat $overlay;
131 104
132 $self->{need_create_treasure} = 1; 105 $self->{need_create_treasure} = 1;
133 106
134 1 107 1
135} 108}
136 109
110# fill map with "default" data - first region and first archetype from palette
111# used when we have no map data from elsewhere
137sub fill { 112sub fill {
138 my ($self) = @_; 113 my ($self) = @_;
139 114
140 $self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt}); 115 if ($self->{z} > 0) {
141 $self->default_region (cf::region::find $WORLD->{reg_plt}[0]); 116 # hmmm... air? :)
142} 117 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]);
143 118 } elsif ($self->{z} < 0) {
144sub load { 119 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]);
145 my ($self) = @_;
146
147 if ($self->{load_path}) {
148 $self->SUPER::load;
149 } else { 120 } else {
150 $self->alloc; 121 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["deep_sea"]);
151 $self->fill;
152 $self->in_memory (cf::MAP_ACTIVE);
153 $self->activate;
154 } 122 }
155} 123}
156 124
125#sub load {
126# my ($self) = @_;
127#
128# if ($self->{load_path}) {
129# warn "load $self\n";#d#
130# $self->SUPER::load;
131# } else {
132# warn "fill $self\n";#d#
133# my $guard = cf::lock_acquire "map_data:$self->{path}";
134#
135# $self->alloc;
136# $self->fill;
137# $self->state (cf::MAP_ACTIVE);
138# $self->activate;
139# $self->post_load;
140# }
141#}
142
143# MUST be overwritten to call at least ->fill
157sub post_load { 144sub post_load {
158 my ($self) = @_; 145 my ($self) = @_;
159
160 {
161 my $guard = cf::lock_acquire "ext::world_gridmap";
162
163 my ($x, $y) = $self->wxwy;
164
165 if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) {
166 my $stride = $WORLD->{grid_w} * $WORLD->{tile_w};
167 my $top = ($y - 100) * $WORLD->{tile_h} * $stride
168 + ($x - 100) * $WORLD->{tile_w};
169
170 $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt});
171 $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt});
172
173 } else {
174 $self->fill;
175 }
176 }
177 146
178 $self->create_region_treasure 147 $self->create_region_treasure
179 if delete $self->{need_create_treasure}; 148 if delete $self->{need_create_treasure};
180} 149}
181 150
1821 151package ext::map_world::empty;
183 152
153our @ISA = ext::map_world::;
154
155sub post_load_original {
156 $_[0]->fill;
157}
158

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines