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 a 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 | |
8 | |
8 | our $WORLD = Storable::retrieve cf::datadir . "/world.pst" |
9 | use Coro::Handle; |
9 | or die "unable to load world.pst"; |
10 | use Coro::AIO; |
10 | |
11 | |
11 | $WORLD->{version} == 1 |
12 | our $WORLD; |
12 | or die "world.pst version mismatch, expected version 1, got $WORLD->{version}"; |
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 | binmode $fh; |
|
|
47 | $size * 3 == read $fh, my $data, $size * 3 |
|
|
48 | or die "$path.png, expected $size rgb triplets: $!"; |
|
|
49 | |
|
|
50 | $data =~ s{(...)}{ |
|
|
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 ($!)"; |
|
|
65 | |
|
|
66 | ($data, \@plt) |
|
|
67 | } |
|
|
68 | |
|
|
69 | sub load_gridmap($) { |
|
|
70 | my ($path) = @_; |
|
|
71 | |
|
|
72 | 0 < aio_load "$path/gridmap.meta", my $map |
|
|
73 | or cf::cleanup "$path/gridmap.meta: $!\n"; |
|
|
74 | |
|
|
75 | $map = cf::from_json $map; |
|
|
76 | |
|
|
77 | my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h} |
|
|
78 | or cf::cleanup "$path/gridmap.meta: empty gridmap?"; |
|
|
79 | |
|
|
80 | ($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; |
|
|
82 | |
|
|
83 | $map |
|
|
84 | } |
|
|
85 | |
|
|
86 | # this is contorted, but likely the correct way to acquire the lock :) |
|
|
87 | cf::sync_job { |
|
|
88 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
|
|
89 | cf::async_ext { |
|
|
90 | $WORLD = load_gridmap sprintf "%s/%s/%s", cf::datadir, cf::mapdir, "world"; |
|
|
91 | warn "worldmap gridmap loaded."; |
|
|
92 | undef $guard; |
|
|
93 | } |
|
|
94 | }; |
13 | |
95 | |
14 | 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); |
15 | |
|
|
16 | sub init { |
|
|
17 | my ($self) = @_; |
|
|
18 | |
|
|
19 | 1 |
|
|
20 | } |
|
|
21 | |
97 | |
22 | sub wxwy { |
98 | sub wxwy { |
23 | $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
99 | $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
24 | ? ($1, $2) |
100 | ? ($1, $2) |
25 | : (0, 0) |
101 | : (0, 0) |
26 | } |
102 | } |
27 | |
103 | |
28 | sub load_header_orig { |
104 | sub load_header_orig { |
29 | my ($self) = @_; |
105 | my ($self) = @_; |
30 | |
106 | |
31 | $self->width ($WORLD->{tilew}); |
|
|
32 | $self->height ($WORLD->{tileh}); |
|
|
33 | |
|
|
34 | my ($x, $y) = $self->wxwy; |
107 | my ($x, $y) = $self->wxwy; |
35 | |
108 | |
36 | $self->outdoor (1); |
109 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
|
|
110 | |
|
|
111 | $self->width ($WORLD->{tile_w}); |
|
|
112 | $self->height ($WORLD->{tile_h}); |
37 | |
113 | |
38 | $self->name ("'The World' at +$x+$y"); |
114 | $self->name ("'The World' at +$x+$y"); |
39 | $self->msg ("worldmap dynamically created by map-world extension"); |
115 | $self->msg ("worldmap dynamically created by map-world extension"); |
|
|
116 | $self->outdoor (1); |
|
|
117 | $self->default_region (undef); |
40 | |
118 | |
41 | # $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; |
42 | # $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; |
43 | # $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; |
44 | # $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; |
45 | $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 100; |
123 | $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 100; |
46 | $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 129; |
124 | $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 129; |
47 | $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 129; |
125 | $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 129; |
48 | $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 100; |
126 | $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 100; |
49 | |
127 | |
50 | #TODO: region |
|
|
51 | |
|
|
52 | $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d", cf::datadir, cf::mapdir, $x, $y |
128 | $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d", cf::datadir, cf::mapdir, $x, $y |
53 | if $x >= $WORLD->{minx} && $x <= $WORLD->{maxx} |
129 | if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; |
54 | && $y >= $WORLD->{miny} && $y <= $WORLD->{maxy}; |
|
|
55 | |
130 | |
56 | 1 |
131 | 1 |
57 | } |
132 | } |
58 | |
133 | |
59 | sub load { |
134 | sub load { |
… | |
… | |
62 | if ($self->{load_path}) { |
137 | if ($self->{load_path}) { |
63 | $self->SUPER::load; |
138 | $self->SUPER::load; |
64 | } else { |
139 | } else { |
65 | $self->alloc; |
140 | $self->alloc; |
66 | |
141 | |
67 | for my $X (0 .. $WORLD->{tilew} - 1) { |
142 | for my $X (0 .. $WORLD->{tile_w} - 1) { |
68 | Coro::cede; |
143 | Coro::cede; |
69 | for my $Y (0 .. $WORLD->{tileh} - 1) { |
144 | for my $Y (0 .. $WORLD->{tile_h} - 1) { |
70 | my $ob = cf::object::new "deep_sea"; |
145 | my $ob = cf::object::new "deep_sea"; |
71 | $self->insert ($ob, $X, $Y); |
146 | $self->insert ($ob, $X, $Y); |
72 | } |
147 | } |
73 | } |
148 | } |
74 | $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1); |
149 | $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1); |
… | |
… | |
78 | } |
153 | } |
79 | |
154 | |
80 | sub post_load { |
155 | sub post_load { |
81 | my ($self) = @_; |
156 | my ($self) = @_; |
82 | |
157 | |
|
|
158 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
|
|
159 | |
83 | my ($x, $y) = $self->wxwy; |
160 | my ($x, $y) = $self->wxwy; |
84 | |
161 | |
85 | return |
162 | return |
86 | unless $x >= $WORLD->{minx} && $x <= $WORLD->{maxx} |
163 | unless $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; |
87 | && $y >= $WORLD->{miny} && $y <= $WORLD->{maxy}; |
|
|
88 | |
164 | |
89 | my $stride = ($WORLD->{maxx} - $WORLD->{minx} + 1) * $WORLD->{tilew}; |
165 | my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; |
90 | my $top = ($y - $WORLD->{miny}) * $WORLD->{tileh} * $stride |
166 | my $top = ($y - 100) * $WORLD->{tile_h} * $stride |
91 | + ($x - $WORLD->{minx}) * $WORLD->{tilew}; |
167 | + ($x - 100) * $WORLD->{tile_w}; |
92 | |
168 | |
|
|
169 | my $reg; |
|
|
170 | |
93 | for my $Y (0 .. $WORLD->{tileh} - 1) { |
171 | for my $Y (0 .. $WORLD->{tile_h} - 1) { |
94 | Coro::cede; |
172 | Coro::cede; |
95 | my $row = substr $WORLD->{data}, $top + $Y * $stride, $WORLD->{tilew}; |
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}; |
96 | for my $X (0 .. $WORLD->{tilew} - 1) { |
175 | for my $X (0 .. $WORLD->{tile_w} - 1) { |
97 | next if grep $_->flag (cf::FLAG_IS_FLOOR), $self->at ($X, $Y); |
176 | next if grep $_->flag (cf::FLAG_IS_FLOOR), $self->at ($X, $Y); |
98 | my $ob = cf::object::new $WORLD->{arch}[ord substr $row, $X]; |
177 | my $ob = cf::object::new $WORLD->{arc_plt}[ord substr $row, $X]; |
99 | $ob->flag (cf::FLAG_NO_MAP_SAVE, 1); |
178 | $ob->flag (cf::FLAG_NO_MAP_SAVE, 1); |
100 | $self->insert ($ob, $X, $Y, undef, cf::INS_ABOVE_FLOOR_ONLY); |
179 | $self->insert ($ob, $X, $Y, undef, cf::INS_ABOVE_FLOOR_ONLY); |
101 | } |
180 | } |
102 | } |
181 | } |
|
|
182 | |
|
|
183 | $self->set_regiondata ($reg, $WORLD->{reg_plt}); |
103 | } |
184 | } |
104 | |
185 | |
105 | 1 |
186 | 1 |
106 | |
187 | |