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) |
… | |
… | |
30 | $plt{$rgb} = chr @plt; |
30 | $plt{$rgb} = chr @plt; |
31 | push @plt, $name; |
31 | push @plt, $name; |
32 | } |
32 | } |
33 | } |
33 | } |
34 | |
34 | |
35 | my $pid = open my $pipe, "-|"; |
35 | my $data = cf::fork_call { |
|
|
36 | open my $fh, "convert \Q$path.png\E -depth 8 rgb:- |" |
|
|
37 | or die "convert: $!"; |
|
|
38 | binmode $fh; |
36 | |
39 | |
37 | defined $pid |
40 | $size * 3 == read $fh, my $data, $size * 3 |
38 | or cf::cleanup "fork: $!"; |
41 | or die "$path.png, expected $size rgb triplets: $!"; |
39 | |
42 | |
40 | unless ($pid) { |
43 | 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 | |
44 | |
46 | binmode $fh; |
45 | $data |
47 | $size * 3 == read $fh, my $data, $size * 3 |
46 | }; |
48 | or die "$path.png, expected $size rgb triplets: $!"; |
|
|
49 | |
47 | |
50 | $data =~ s{(...)}{ |
48 | $size == length $data |
51 | $plt{$1} or die "$path.png: color not in palette: #" . unpack "H*", $1 |
|
|
52 | }sge; |
|
|
53 | |
|
|
54 | binmode STDOUT; |
|
|
55 | syswrite STDOUT, $data; |
|
|
56 | }; |
|
|
57 | warn $@ if $@; |
|
|
58 | cf::_exit; |
|
|
59 | } |
|
|
60 | |
|
|
61 | $pipe = Coro::Handle::unblock $pipe; |
|
|
62 | binmode $pipe; |
|
|
63 | $size == read $pipe, my $data, $size |
|
|
64 | or cf::cleanup "$path.png, expected $size index octets ($!)"; |
49 | or cf::cleanup "$path.png, expected $size index octets ($!)"; |
65 | |
50 | |
66 | ($data, \@plt) |
51 | ($data, \@plt) |
67 | } |
52 | } |
68 | |
53 | |
69 | sub load_gridmap($) { |
54 | sub load_gridmap($) { |
70 | my ($path) = @_; |
55 | my ($path) = @_; |
71 | |
56 | |
72 | if (! -e "/tmp/xxxx.gridmap") { |
57 | my $map = cf::cache "ext::map-world/gridmap" => |
73 | 0 < aio_load "$path/gridmap.meta", my $map |
58 | [ |
74 | or cf::cleanup "$path/gridmap.meta: $!\n"; |
59 | "$path/gridmap.meta", |
|
|
60 | "$path/gridmap.arch.plt", |
|
|
61 | "$path/gridmap.arch.png", |
|
|
62 | "$path/gridmap.regn.plt", |
|
|
63 | "$path/gridmap.regn.png", |
|
|
64 | ], |
|
|
65 | 1 => sub { |
|
|
66 | my ($src) = @_; |
75 | |
67 | |
76 | $map = cf::from_json $map; |
68 | my $map = cf::from_json $src->[0]; |
77 | |
69 | |
78 | my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h} |
70 | my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h} |
79 | or cf::cleanup "$path/gridmap.meta: empty gridmap?"; |
71 | or cf::cleanup "$path/gridmap.meta: empty gridmap?"; |
80 | |
72 | |
81 | ($map->{arc_data}, $map->{arc_plt}) = load_indexed "$path/gridmap.arch", $size; |
73 | ($map->{arc_data}, $map->{arc_plt}) = load_indexed "$path/gridmap.arch", $size; |
82 | ($map->{reg_data}, $map->{reg_plt}) = load_indexed "$path/gridmap.regn", $size; |
74 | ($map->{reg_data}, $map->{reg_plt}) = load_indexed "$path/gridmap.regn", $size; |
83 | |
75 | |
84 | Storable::nstore $map, "/tmp/xxxx.gridmap";#d# |
76 | Coro::Storable::freeze $map |
|
|
77 | }; |
85 | |
78 | |
86 | $map |
79 | Coro::Storable::thaw $map |
87 | } else { |
80 | } |
88 | Storable::retrieve "/tmp/xxxx.gridmap"; |
81 | |
89 | } |
82 | sub reload() { |
|
|
83 | $WORLD = load_gridmap "$MAPDIR/world"; |
|
|
84 | warn "worldmap gridmap loaded."; |
90 | } |
85 | } |
91 | |
86 | |
92 | # this is contorted, but likely the correct way to acquire the lock :) |
87 | # this is contorted, but likely the correct way to acquire the lock :) |
93 | cf::sync_job { |
88 | cf::sync_job { |
94 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
89 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
95 | cf::async_ext { |
90 | cf::async_ext { |
96 | $WORLD = load_gridmap sprintf "%s/%s/%s", cf::datadir, cf::mapdir, "world"; |
91 | reload; |
97 | warn "worldmap gridmap loaded."; |
|
|
98 | undef $guard; |
92 | undef $guard; |
99 | } |
93 | }; |
100 | }; |
94 | }; |
101 | |
95 | |
102 | cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); |
96 | cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); |
103 | |
97 | |
104 | sub wxwy { |
98 | sub wxwy { |
… | |
… | |
118 | $self->height ($WORLD->{tile_h}); |
112 | $self->height ($WORLD->{tile_h}); |
119 | |
113 | |
120 | $self->name ("'The World' at +$x+$y"); |
114 | $self->name ("'The World' at +$x+$y"); |
121 | $self->msg ("worldmap dynamically created by map-world extension"); |
115 | $self->msg ("worldmap dynamically created by map-world extension"); |
122 | $self->outdoor (1); |
116 | $self->outdoor (1); |
123 | $self->default_region (undef); |
117 | $self->default_region (cf::region::find "wilderness"); |
124 | |
118 | |
125 | $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; |
119 | $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; |
126 | $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; |
120 | $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; |
127 | $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; |
121 | $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; |
128 | $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; |
122 | $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; |
129 | |
123 | |
130 | $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d", cf::datadir, cf::mapdir, $x, $y |
124 | my $overlay = sprintf "%s/world-overlay/world_%03d_%03d.map", $cf::MAPDIR, $x, $y; |
131 | if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; |
125 | |
|
|
126 | $self->{load_path} = $overlay |
|
|
127 | unless Coro::AIO::aio_stat $overlay; |
|
|
128 | |
|
|
129 | $self->{need_create_treasure} = 1; |
132 | |
130 | |
133 | 1 |
131 | 1 |
134 | } |
132 | } |
135 | |
133 | |
136 | sub fill { |
134 | sub fill { |
137 | my ($self) = @_; |
135 | my ($self) = @_; |
138 | |
136 | |
139 | for my $X (0 .. $WORLD->{tile_w} - 1) { |
137 | $self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt}); |
140 | Coro::cede; |
|
|
141 | for my $Y (0 .. $WORLD->{tile_h} - 1) { |
|
|
142 | my $ob = cf::object::new "deep_sea"; |
|
|
143 | $ob->flag (cf::FLAG_NO_MAP_SAVE, 1); |
|
|
144 | $self->insert ($ob, $X, $Y, undef, cf::INS_ABOVE_FLOOR_ONLY); |
|
|
145 | } |
|
|
146 | } |
|
|
147 | $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1); |
|
|
148 | $self->default_region (cf::region::find "panthalassia"); |
138 | $self->default_region (cf::region::find $WORLD->{reg_plt}[0]); |
149 | } |
139 | } |
150 | |
140 | |
151 | sub load { |
141 | sub load { |
152 | my ($self) = @_; |
142 | my ($self) = @_; |
153 | |
143 | |
… | |
… | |
155 | $self->SUPER::load; |
145 | $self->SUPER::load; |
156 | } else { |
146 | } else { |
157 | $self->alloc; |
147 | $self->alloc; |
158 | $self->fill; |
148 | $self->fill; |
159 | $self->in_memory (cf::MAP_IN_MEMORY); |
149 | $self->in_memory (cf::MAP_IN_MEMORY); |
|
|
150 | $self->activate; |
160 | } |
151 | } |
161 | } |
152 | } |
162 | |
153 | |
163 | sub post_load { |
154 | sub post_load { |
164 | my ($self) = @_; |
155 | my ($self) = @_; |
165 | |
156 | |
|
|
157 | { |
166 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
158 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
167 | |
159 | |
168 | my ($x, $y) = $self->wxwy; |
160 | my ($x, $y) = $self->wxwy; |
169 | |
161 | |
170 | if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { |
162 | if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { |
171 | my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; |
163 | my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; |
172 | my $top = ($y - 100) * $WORLD->{tile_h} * $stride |
164 | my $top = ($y - 100) * $WORLD->{tile_h} * $stride |
173 | + ($x - 100) * $WORLD->{tile_w}; |
165 | + ($x - 100) * $WORLD->{tile_w}; |
174 | |
166 | |
175 | my $reg; |
167 | $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt}); |
|
|
168 | $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt}); |
176 | |
169 | |
177 | for my $Y (0 .. $WORLD->{tile_h} - 1) { |
170 | } else { |
178 | Coro::cede; |
171 | $self->fill; |
179 | my $row = substr $WORLD->{arc_data}, $top + $Y * $stride, $WORLD->{tile_w}; |
|
|
180 | $reg .= substr $WORLD->{reg_data}, $top + $Y * $stride, $WORLD->{tile_w}; |
|
|
181 | for my $X (0 .. $WORLD->{tile_w} - 1) { |
|
|
182 | next if grep $_->flag (cf::FLAG_IS_FLOOR), $self->at ($X, $Y); |
|
|
183 | my $ob = cf::object::new $WORLD->{arc_plt}[ord substr $row, $X]; |
|
|
184 | $ob->flag (cf::FLAG_NO_MAP_SAVE, 1); |
|
|
185 | $self->insert ($ob, $X, $Y, undef, cf::INS_ABOVE_FLOOR_ONLY); |
|
|
186 | } |
|
|
187 | } |
172 | } |
|
|
173 | } |
188 | |
174 | |
189 | $self->set_regiondata ($reg, $WORLD->{reg_plt}); |
175 | $self->create_region_treasure |
190 | } else { |
176 | if delete $self->{need_create_treasure}; |
191 | $self->fill; |
|
|
192 | } |
|
|
193 | } |
177 | } |
194 | |
178 | |
195 | 1 |
179 | 1 |
196 | |
180 | |