ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/map-world.ext
Revision: 1.21
Committed: Tue Apr 17 20:41:15 2007 UTC (17 years, 1 month ago) by root
Branch: MAIN
Changes since 1.20: +1 -4 lines
Log Message:
considerably speed up world gridmap loading through some xs magic

File Contents

# Content
1 #! perl # OPTIONAL
2
3 # optional plug-in to speed up worldmap rendering by dynamically
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
9 use Coro::Handle;
10 use Coro::AIO;
11
12 our $WORLD;
13
14 sub load_indexed($$) {
15 my ($path, $size) = @_;
16
17 use bytes;
18
19 0 < aio_load "$path.plt", my $plt
20 or cf::cleanup "$path.plt: $!";
21
22 my %plt;
23 my @plt;
24
25 for (split /\n/, $plt) {
26 my ($name, $rgb) = split /\s+/;
27 if (/^(\S+)\s+(\S+)$/) {
28 my ($name, $rgb) = ($1, $2);
29 $rgb = join "", map chr, map $_ * 17, map hex, split //, $rgb;
30 $plt{$rgb} = chr @plt;
31 push @plt, $name;
32 }
33 }
34
35 my $pid = open my $pipe, "-|";
36
37 defined $pid
38 or cf::cleanup "fork: $!";
39
40 unless ($pid) {
41 eval {
42 open my $fh, "convert \Q$path.png\E -depth 8 rgb:- |"
43 or die "convert: $!";
44 binmode $fh;
45
46 $size * 3 == read $fh, my $data, $size * 3
47 or die "$path.png, expected $size rgb triplets: $!";
48
49 cf::_quantise $data, [map "$_$plt{$_}", keys %plt];
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 ($!)";
62
63 ($data, \@plt)
64 }
65
66 sub load_gridmap($) {
67 my ($path) = @_;
68
69 # if (! -e "/tmp/xxxx.gridmap") {
70 0 < aio_load "$path/gridmap.meta", my $map
71 or cf::cleanup "$path/gridmap.meta: $!\n";
72
73 $map = cf::from_json $map;
74
75 my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h}
76 or cf::cleanup "$path/gridmap.meta: empty gridmap?";
77
78 ($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;
80
81 # Storable::nstore $map, "/tmp/xxxx.gridmap" if 0;#d#
82
83 $map
84 # } else {
85 # Storable::retrieve "/tmp/xxxx.gridmap";
86 # }
87 }
88
89 # this is contorted, but likely the correct way to acquire the lock :)
90 cf::sync_job {
91 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
99 cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100);
100
101 sub wxwy {
102 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$}
103 ? ($1, $2)
104 : (0, 0)
105 }
106
107 sub load_header_orig {
108 my ($self) = @_;
109
110 my ($x, $y) = $self->wxwy;
111
112 my $guard = cf::lock_acquire "ext::world_gridmap";
113
114 $self->width ($WORLD->{tile_w});
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");
119 $self->outdoor (1);
120 $self->default_region (cf::region::find "wilderness");
121
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;
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;
126
127 $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d.map", cf::datadir, cf::mapdir, $x, $y
128 if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129;
129
130 1
131 }
132
133 sub fill {
134 my ($self) = @_;
135
136 $self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt});
137 $self->default_region (cf::region::find $WORLD->{reg_plt}[0]);
138 }
139
140 sub load {
141 my ($self) = @_;
142
143 if ($self->{load_path}) {
144 $self->SUPER::load;
145 } else {
146 $self->alloc;
147 $self->fill;
148 $self->in_memory (cf::MAP_IN_MEMORY);
149 }
150 }
151
152 sub post_load {
153 my ($self) = @_;
154
155 my $guard = cf::lock_acquire "ext::world_gridmap";
156
157 my ($x, $y) = $self->wxwy;
158
159 if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) {
160 my $stride = $WORLD->{grid_w} * $WORLD->{tile_w};
161 my $top = ($y - 100) * $WORLD->{tile_h} * $stride
162 + ($x - 100) * $WORLD->{tile_w};
163
164 $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt});
165 $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt});
166
167 } else {
168 $self->fill;
169 }
170 }
171
172 1
173