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 | $size * 3 == read $fh, my $data, $size * 3 |
45 | $data |
47 | or die "$path.png, expected $size rgb triplets: $!"; |
46 | }; |
48 | |
47 | |
49 | cf::_quantise $data, [map "$_$plt{$_}", keys %plt]; |
48 | $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 ($!)"; |
49 | or cf::cleanup "$path.png, expected $size index octets ($!)"; |
62 | |
50 | |
63 | ($data, \@plt) |
51 | ($data, \@plt) |
64 | } |
52 | } |
65 | |
53 | |
66 | sub load_gridmap($) { |
54 | sub load_gridmap($) { |
67 | my ($path) = @_; |
55 | my ($path) = @_; |
68 | |
56 | |
69 | # if (! -e "/tmp/xxxx.gridmap") { |
57 | my $map = cf::cache "ext::map-world/gridmap" => |
70 | 0 < aio_load "$path/gridmap.meta", my $map |
58 | [ |
71 | 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) = @_; |
72 | |
67 | |
73 | $map = cf::from_json $map; |
68 | my $map = cf::from_json $src->[0]; |
74 | |
69 | |
75 | 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} |
76 | or cf::cleanup "$path/gridmap.meta: empty gridmap?"; |
71 | or cf::cleanup "$path/gridmap.meta: empty gridmap?"; |
77 | |
72 | |
78 | ($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; |
79 | ($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; |
80 | |
75 | |
81 | # Storable::nstore $map, "/tmp/xxxx.gridmap" if 0;#d# |
76 | Coro::Storable::freeze $map |
|
|
77 | }; |
82 | |
78 | |
83 | $map |
79 | Coro::Storable::thaw $map |
84 | # } else { |
80 | } |
85 | # Storable::retrieve "/tmp/xxxx.gridmap"; |
81 | |
86 | # } |
82 | sub reload() { |
|
|
83 | $WORLD = load_gridmap "$MAPDIR/world"; |
|
|
84 | warn "worldmap gridmap loaded."; |
87 | } |
85 | } |
88 | |
86 | |
89 | # 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 :) |
90 | cf::sync_job { |
88 | cf::sync_job { |
91 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
89 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
92 | cf::async_ext { |
90 | cf::async_ext { |
93 | $WORLD = load_gridmap sprintf "%s/%s/%s", cf::datadir, cf::mapdir, "world"; |
91 | reload; |
94 | warn "worldmap gridmap loaded."; |
|
|
95 | undef $guard; |
92 | undef $guard; |
96 | } |
93 | }; |
97 | }; |
94 | }; |
98 | |
95 | |
99 | 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); |
100 | |
97 | |
101 | sub wxwy { |
98 | sub wxwy { |
… | |
… | |
122 | $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; |
123 | $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; |
124 | $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; |
125 | $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; |
126 | |
123 | |
127 | $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d.map", cf::datadir, cf::mapdir, $x, $y |
124 | my $overlay = sprintf "%s/world-overlay/world_%03d_%03d.map", $cf::MAPDIR, $x, $y; |
128 | 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; |
129 | |
130 | |
130 | 1 |
131 | 1 |
131 | } |
132 | } |
132 | |
133 | |
133 | sub fill { |
134 | sub fill { |
… | |
… | |
144 | $self->SUPER::load; |
145 | $self->SUPER::load; |
145 | } else { |
146 | } else { |
146 | $self->alloc; |
147 | $self->alloc; |
147 | $self->fill; |
148 | $self->fill; |
148 | $self->in_memory (cf::MAP_IN_MEMORY); |
149 | $self->in_memory (cf::MAP_IN_MEMORY); |
|
|
150 | $self->activate; |
149 | } |
151 | } |
150 | } |
152 | } |
151 | |
153 | |
152 | sub post_load { |
154 | sub post_load { |
153 | my ($self) = @_; |
155 | my ($self) = @_; |
154 | |
156 | |
|
|
157 | { |
155 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
158 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
156 | |
159 | |
157 | my ($x, $y) = $self->wxwy; |
160 | my ($x, $y) = $self->wxwy; |
158 | |
161 | |
159 | if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { |
162 | if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { |
160 | my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; |
163 | my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; |
161 | my $top = ($y - 100) * $WORLD->{tile_h} * $stride |
164 | my $top = ($y - 100) * $WORLD->{tile_h} * $stride |
162 | + ($x - 100) * $WORLD->{tile_w}; |
165 | + ($x - 100) * $WORLD->{tile_w}; |
163 | |
166 | |
164 | $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt}); |
167 | $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt}); |
165 | $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt}); |
168 | $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt}); |
166 | |
169 | |
167 | } else { |
170 | } else { |
168 | $self->fill; |
171 | $self->fill; |
|
|
172 | } |
169 | } |
173 | } |
|
|
174 | |
|
|
175 | $self->create_region_treasure |
|
|
176 | if delete $self->{need_create_treasure}; |
170 | } |
177 | } |
171 | |
178 | |
172 | 1 |
179 | 1 |
173 | |
180 | |