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.21 by root, Tue Apr 17 20:41:15 2007 UTC vs.
Revision 1.39 by root, Tue May 4 21:45:42 2010 UTC

1#! perl # OPTIONAL 1#! perl # optional
2 2
3# optional plug-in to speed up worldmap rendering by dynamically 3# optional plug-in to speed up worldmap rendering by dynamically
4# generating it out of an image 4# generating it out of an image
5# - saves loading time (less data to read) 5# - saves loading time (less data to read)
6# - saves temporary space (only overlay stuff needs to be saved) 6# - saves temporary space (only overlay stuff needs to be saved)
7# - might get reused as a generic tiled map 7# - might get reused as a generic tiled map
8
9cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100);
8 10
9use Coro::Handle; 11use Coro::Handle;
10use Coro::AIO; 12use Coro::AIO;
11 13
12our $WORLD; 14our $WORLD;
30 $plt{$rgb} = chr @plt; 32 $plt{$rgb} = chr @plt;
31 push @plt, $name; 33 push @plt, $name;
32 } 34 }
33 } 35 }
34 36
35 my $pid = open my $pipe, "-|"; 37 my $data = cf::fork_call {
38 open my $fh, "convert \Q$path.png\E -depth 8 rgb:- |"
39 or die "convert: $!";
40 binmode $fh;
36 41
37 defined $pid 42 $size * 3 == read $fh, my $data, $size * 3
38 or cf::cleanup "fork: $!"; 43 or die "$path.png, expected $size rgb triplets: $!";
39 44
40 unless ($pid) { 45 cf::_quantise $data, [map "$_$plt{$_}", keys %plt];
41 eval {
42 open my $fh, "convert \Q$path.png\E -depth 8 rgb:- |"
43 or die "convert: $!";
44 binmode $fh;
45 46
46 $size * 3 == read $fh, my $data, $size * 3 47 $data
47 or die "$path.png, expected $size rgb triplets: $!"; 48 };
48 49
49 cf::_quantise $data, [map "$_$plt{$_}", keys %plt]; 50 $size == length $data
50
51 binmode STDOUT;
52 syswrite STDOUT, $data;
53 };
54 warn $@ if $@;
55 cf::_exit;
56 }
57
58 $pipe = Coro::Handle::unblock $pipe;
59 binmode $pipe;
60 $size == read $pipe, my $data, $size
61 or cf::cleanup "$path.png, expected $size index octets ($!)"; 51 or cf::cleanup "$path.png, expected $size index octets ($!)";
62 52
63 ($data, \@plt) 53 ($data, \@plt)
64} 54}
65 55
66sub load_gridmap($) { 56sub load_gridmap($) {
67 my ($path) = @_; 57 my ($path) = @_;
68 58
69# if (! -e "/tmp/xxxx.gridmap") { 59 my $map = cf::cache "ext::map-world/gridmap" =>
70 0 < aio_load "$path/gridmap.meta", my $map 60 [
71 or cf::cleanup "$path/gridmap.meta: $!\n"; 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) = @_;
72 69
73 $map = cf::from_json $map; 70 my $map = cf::decode_json $src->[0];
74 71
75 my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h} 72 my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h}
76 or cf::cleanup "$path/gridmap.meta: empty gridmap?"; 73 or cf::cleanup "$path/gridmap.meta: empty gridmap?";
77 74
78 ($map->{arc_data}, $map->{arc_plt}) = load_indexed "$path/gridmap.arch", $size; 75 ($map->{arc_data}, $map->{arc_plt}) = load_indexed "$path/gridmap.arch", $size;
79 ($map->{reg_data}, $map->{reg_plt}) = load_indexed "$path/gridmap.regn", $size; 76 ($map->{reg_data}, $map->{reg_plt}) = load_indexed "$path/gridmap.regn", $size;
80 77
81# Storable::nstore $map, "/tmp/xxxx.gridmap" if 0;#d# 78 Coro::Storable::freeze $map
79 };
82 80
83 $map 81 Coro::Storable::thaw $map
84# } else {
85# Storable::retrieve "/tmp/xxxx.gridmap";
86# }
87} 82}
88 83
89# this is contorted, but likely the correct way to acquire the lock :) 84sub reload() {
90cf::sync_job { 85 $WORLD = load_gridmap "$MAPDIR/world";
86 cf::trace "worldmap gridmap loaded.\n";
87}
88
89{
91 my $guard = cf::lock_acquire "ext::world_gridmap"; 90 my $guard = cf::lock_acquire "ext::world_gridmap";
92 cf::async_ext {
93 $WORLD = load_gridmap sprintf "%s/%s/%s", cf::datadir, cf::mapdir, "world";
94 warn "worldmap gridmap loaded.";
95 undef $guard;
96 }
97};
98 91
99cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); 92 cf::post_init {
93 cf::async_ext {
94 $Coro::current->{desc} = "worldmap loader";
95 reload;
96 undef $guard;
97 };
98 };
99}
100 100
101sub wxwy { 101sub wxwy {
102 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} 102 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$}
103 ? ($1, $2) 103 ? ($1, $2)
104 : (0, 0) 104 : (0, 0)
122 $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; 122 $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0;
123 $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; 123 $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999;
124 $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; 124 $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999;
125 $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; 125 $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0;
126 126
127 $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d.map", cf::datadir, cf::mapdir, $x, $y 127 my $overlay = sprintf "%s/world/world_%03d_%03d.map", $cf::MAPDIR, $x, $y;
128 if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; 128
129 $self->{load_path} = $overlay
130 unless Coro::AIO::aio_stat $overlay;
131
132 $self->{need_create_treasure} = 1;
129 133
130 1 134 1
131} 135}
132 136
133sub fill { 137sub fill {
134 my ($self) = @_; 138 my ($self) = @_;
135 139
136 $self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt}); 140 $self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt});#d#
137 $self->default_region (cf::region::find $WORLD->{reg_plt}[0]); 141 $self->default_region (cf::region::find $WORLD->{reg_plt}[0]);
138} 142}
139 143
140sub load { 144sub load {
141 my ($self) = @_; 145 my ($self) = @_;
143 if ($self->{load_path}) { 147 if ($self->{load_path}) {
144 $self->SUPER::load; 148 $self->SUPER::load;
145 } else { 149 } else {
146 $self->alloc; 150 $self->alloc;
147 $self->fill; 151 $self->fill;
148 $self->in_memory (cf::MAP_IN_MEMORY); 152 $self->in_memory (cf::MAP_ACTIVE);
153 $self->activate;
149 } 154 }
150} 155}
151 156
152sub post_load { 157sub post_load {
153 my ($self) = @_; 158 my ($self) = @_;
154 159
160 {
155 my $guard = cf::lock_acquire "ext::world_gridmap"; 161 my $guard = cf::lock_acquire "ext::world_gridmap";
156 162
157 my ($x, $y) = $self->wxwy; 163 my ($x, $y) = $self->wxwy;
158 164
159 if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { 165 if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) {
160 my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; 166 my $stride = $WORLD->{grid_w} * $WORLD->{tile_w};
161 my $top = ($y - 100) * $WORLD->{tile_h} * $stride 167 my $top = ($y - 100) * $WORLD->{tile_h} * $stride
162 + ($x - 100) * $WORLD->{tile_w}; 168 + ($x - 100) * $WORLD->{tile_w};
163 169
164 $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt}); 170 $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt});
165 $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt}); 171 $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt});
166 172
167 } else { 173 } else {
168 $self->fill; 174 $self->fill;
175 }
169 } 176 }
177
178 $self->create_region_treasure
179 if delete $self->{need_create_treasure};
170} 180}
171 181
1721
173

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines