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 | |
|
|
9 | cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); |
8 | |
10 | |
9 | use Coro::Handle; |
11 | use Coro::Handle; |
10 | use Coro::AIO; |
12 | use Coro::AIO; |
11 | |
13 | |
12 | our $WORLD; |
14 | our $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 | binmode $fh; |
47 | $data |
47 | $size * 3 == read $fh, my $data, $size * 3 |
48 | }; |
48 | or die "$path.png, expected $size rgb triplets: $!"; |
|
|
49 | |
49 | |
50 | $data =~ s{(...)}{ |
50 | $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 ($!)"; |
51 | or cf::cleanup "$path.png, expected $size index octets ($!)"; |
65 | |
52 | |
66 | ($data, \@plt) |
53 | ($data, \@plt) |
67 | } |
54 | } |
68 | |
55 | |
69 | sub load_gridmap($) { |
56 | sub load_gridmap($) { |
70 | my ($path) = @_; |
57 | my ($path) = @_; |
71 | |
58 | |
72 | 0 < aio_load "$path/gridmap.meta", my $map |
59 | my $map = cf::cache "ext::map-world/gridmap" => |
73 | or cf::cleanup "$path/gridmap.meta: $!\n"; |
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) = @_; |
74 | |
69 | |
75 | $map = cf::from_json $map; |
70 | my $map = cf::decode_json $src->[0]; |
76 | |
71 | |
77 | 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} |
78 | or cf::cleanup "$path/gridmap.meta: empty gridmap?"; |
73 | or cf::cleanup "$path/gridmap.meta: empty gridmap?"; |
79 | |
74 | |
80 | ($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; |
81 | ($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; |
82 | |
77 | |
83 | $map |
78 | Coro::Storable::freeze $map |
|
|
79 | }; |
|
|
80 | |
|
|
81 | Coro::Storable::thaw $map |
|
|
82 | } |
|
|
83 | |
|
|
84 | sub reload() { |
|
|
85 | $WORLD = load_gridmap "$MAPDIR/world"; |
|
|
86 | warn "worldmap gridmap loaded."; |
84 | } |
87 | } |
85 | |
88 | |
86 | # this is contorted, but likely the correct way to acquire the lock :) |
89 | # this is contorted, but likely the correct way to acquire the lock :) |
87 | cf::sync_job { |
90 | cf::sync_job { |
88 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
91 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
89 | cf::async_ext { |
92 | cf::async_ext { |
90 | $WORLD = load_gridmap sprintf "%s/%s/%s", cf::datadir, cf::mapdir, "world"; |
93 | $Coro::current->{desc} = "worldmap loader"; |
91 | warn "worldmap gridmap loaded."; |
94 | reload; |
92 | undef $guard; |
95 | undef $guard; |
93 | } |
96 | }; |
94 | }; |
97 | }; |
95 | |
|
|
96 | cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); |
|
|
97 | |
98 | |
98 | sub wxwy { |
99 | sub wxwy { |
99 | $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
100 | $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
100 | ? ($1, $2) |
101 | ? ($1, $2) |
101 | : (0, 0) |
102 | : (0, 0) |
… | |
… | |
112 | $self->height ($WORLD->{tile_h}); |
113 | $self->height ($WORLD->{tile_h}); |
113 | |
114 | |
114 | $self->name ("'The World' at +$x+$y"); |
115 | $self->name ("'The World' at +$x+$y"); |
115 | $self->msg ("worldmap dynamically created by map-world extension"); |
116 | $self->msg ("worldmap dynamically created by map-world extension"); |
116 | $self->outdoor (1); |
117 | $self->outdoor (1); |
117 | $self->default_region (undef); |
118 | $self->default_region (cf::region::find "wilderness"); |
118 | |
119 | |
119 | # $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; |
120 | $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; |
120 | # $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; |
121 | $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; |
121 | # $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; |
122 | $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; |
122 | # $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; |
123 | $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; |
123 | $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 100; |
|
|
124 | $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 129; |
|
|
125 | $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 129; |
|
|
126 | $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 100; |
|
|
127 | |
124 | |
128 | $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d", cf::datadir, cf::mapdir, $x, $y |
125 | my $overlay = sprintf "%s/world/world_%03d_%03d.map", $cf::MAPDIR, $x, $y; |
129 | if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; |
126 | |
|
|
127 | $self->{load_path} = $overlay |
|
|
128 | unless Coro::AIO::aio_stat $overlay; |
|
|
129 | |
|
|
130 | $self->{need_create_treasure} = 1; |
130 | |
131 | |
131 | 1 |
132 | 1 |
|
|
133 | } |
|
|
134 | |
|
|
135 | sub fill { |
|
|
136 | my ($self) = @_; |
|
|
137 | |
|
|
138 | $self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt}); |
|
|
139 | $self->default_region (cf::region::find $WORLD->{reg_plt}[0]); |
132 | } |
140 | } |
133 | |
141 | |
134 | sub load { |
142 | sub load { |
135 | my ($self) = @_; |
143 | my ($self) = @_; |
136 | |
144 | |
137 | if ($self->{load_path}) { |
145 | if ($self->{load_path}) { |
138 | $self->SUPER::load; |
146 | $self->SUPER::load; |
139 | } else { |
147 | } else { |
140 | $self->alloc; |
148 | $self->alloc; |
141 | |
149 | $self->fill; |
142 | for my $X (0 .. $WORLD->{tile_w} - 1) { |
|
|
143 | Coro::cede; |
|
|
144 | for my $Y (0 .. $WORLD->{tile_h} - 1) { |
|
|
145 | my $ob = cf::object::new "deep_sea"; |
|
|
146 | $self->insert ($ob, $X, $Y); |
|
|
147 | } |
|
|
148 | } |
|
|
149 | $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1); |
|
|
150 | |
|
|
151 | $self->in_memory (cf::MAP_IN_MEMORY); |
150 | $self->in_memory (cf::MAP_IN_MEMORY); |
|
|
151 | $self->activate; |
152 | } |
152 | } |
153 | } |
153 | } |
154 | |
154 | |
155 | sub post_load { |
155 | sub post_load { |
156 | my ($self) = @_; |
156 | my ($self) = @_; |
157 | |
157 | |
|
|
158 | { |
158 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
159 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
159 | |
160 | |
160 | my ($x, $y) = $self->wxwy; |
161 | my ($x, $y) = $self->wxwy; |
161 | |
162 | |
162 | return |
|
|
163 | unless $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; |
163 | if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { |
|
|
164 | my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; |
|
|
165 | my $top = ($y - 100) * $WORLD->{tile_h} * $stride |
|
|
166 | + ($x - 100) * $WORLD->{tile_w}; |
164 | |
167 | |
165 | my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; |
168 | $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt}); |
166 | my $top = ($y - 100) * $WORLD->{tile_h} * $stride |
169 | $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt}); |
167 | + ($x - 100) * $WORLD->{tile_w}; |
|
|
168 | |
170 | |
169 | my $reg; |
171 | } else { |
170 | |
172 | $self->fill; |
171 | for my $Y (0 .. $WORLD->{tile_h} - 1) { |
|
|
172 | Coro::cede; |
|
|
173 | my $row = substr $WORLD->{arc_data}, $top + $Y * $stride, $WORLD->{tile_w}; |
|
|
174 | $reg .= substr $WORLD->{reg_data}, $top + $Y * $stride, $WORLD->{tile_w}; |
|
|
175 | for my $X (0 .. $WORLD->{tile_w} - 1) { |
|
|
176 | next if grep $_->flag (cf::FLAG_IS_FLOOR), $self->at ($X, $Y); |
|
|
177 | my $ob = cf::object::new $WORLD->{arc_plt}[ord substr $row, $X]; |
|
|
178 | $ob->flag (cf::FLAG_NO_MAP_SAVE, 1); |
|
|
179 | $self->insert ($ob, $X, $Y, undef, cf::INS_ABOVE_FLOOR_ONLY); |
|
|
180 | } |
173 | } |
181 | } |
174 | } |
182 | |
175 | |
183 | $self->set_regiondata ($reg, $WORLD->{reg_plt}); |
176 | $self->create_region_treasure |
|
|
177 | if delete $self->{need_create_treasure}; |
184 | } |
178 | } |
185 | |
179 | |
186 | 1 |
180 | 1 |
187 | |
181 | |