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 | if (! -e "/tmp/xxxx.gridmap") { |
|
|
73 | 0 < aio_load "$path/gridmap.meta", my $map |
|
|
74 | or cf::cleanup "$path/gridmap.meta: $!\n"; |
|
|
75 | |
|
|
76 | $map = cf::from_json $map; |
|
|
77 | |
|
|
78 | my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h} |
|
|
79 | or cf::cleanup "$path/gridmap.meta: empty gridmap?"; |
|
|
80 | |
|
|
81 | ($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; |
|
|
83 | |
|
|
84 | Storable::nstore $map, "/tmp/xxxx.gridmap";#d# |
|
|
85 | |
|
|
86 | $map |
|
|
87 | } else { |
|
|
88 | Storable::retrieve "/tmp/xxxx.gridmap"; |
|
|
89 | } |
|
|
90 | } |
|
|
91 | |
|
|
92 | # this is contorted, but likely the correct way to acquire the lock :) |
|
|
93 | cf::sync_job { |
|
|
94 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
|
|
95 | cf::async_ext { |
|
|
96 | $WORLD = load_gridmap sprintf "%s/%s/%s", cf::datadir, cf::mapdir, "world"; |
|
|
97 | warn "worldmap gridmap loaded."; |
|
|
98 | undef $guard; |
|
|
99 | } |
|
|
100 | }; |
13 | |
101 | |
14 | cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); |
102 | 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 | |
103 | |
22 | sub wxwy { |
104 | sub wxwy { |
23 | $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
105 | $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
24 | ? ($1, $2) |
106 | ? ($1, $2) |
25 | : (0, 0) |
107 | : (0, 0) |
26 | } |
108 | } |
27 | |
109 | |
28 | sub load_header_orig { |
110 | sub load_header_orig { |
29 | my ($self) = @_; |
111 | my ($self) = @_; |
30 | |
112 | |
31 | $self->width ($WORLD->{tilew}); |
113 | my ($x, $y) = $self->wxwy; |
32 | $self->height ($WORLD->{tileh}); |
|
|
33 | |
114 | |
34 | my ($x, $y) = $self->wxwy; |
115 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
|
|
116 | |
|
|
117 | $self->width ($WORLD->{tile_w}); |
|
|
118 | $self->height ($WORLD->{tile_h}); |
|
|
119 | |
|
|
120 | $self->name ("'The World' at +$x+$y"); |
|
|
121 | $self->msg ("worldmap dynamically created by map-world extension"); |
|
|
122 | $self->outdoor (1); |
|
|
123 | $self->default_region (undef); |
35 | |
124 | |
36 | $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; |
125 | $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; |
37 | $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; |
126 | $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; |
38 | $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; |
127 | $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; |
39 | $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; |
128 | $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; |
40 | |
129 | |
41 | $self->msg ("worldmap dynamically created by map-world extension"); |
|
|
42 | |
|
|
43 | #TODO: region |
|
|
44 | |
|
|
45 | $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d", cf::datadir, cf::mapdir, $x, $y |
130 | $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d.map", cf::datadir, cf::mapdir, $x, $y |
46 | if $x >= $WORLD->{minx} && $x <= $WORLD->{maxx} |
131 | if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; |
47 | && $y >= $WORLD->{miny} && $y <= $WORLD->{maxy}; |
|
|
48 | |
|
|
49 | |
132 | |
50 | 1 |
133 | 1 |
|
|
134 | } |
|
|
135 | |
|
|
136 | sub fill { |
|
|
137 | my ($self) = @_; |
|
|
138 | |
|
|
139 | for my $X (0 .. $WORLD->{tile_w} - 1) { |
|
|
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"); |
51 | } |
149 | } |
52 | |
150 | |
53 | sub load { |
151 | sub load { |
54 | my ($self) = @_; |
152 | my ($self) = @_; |
55 | |
153 | |
56 | if ($self->{load_path}) { |
154 | if ($self->{load_path}) { |
57 | $self->SUPER::load; |
155 | $self->SUPER::load; |
58 | } else { |
156 | } else { |
59 | for my $X (0 .. $WORLD->{tilew} - 1) { |
157 | $self->alloc; |
60 | Coro::cede; |
158 | $self->fill; |
61 | for my $Y (0 .. $WORLD->{tileh} - 1) { |
159 | $self->in_memory (cf::MAP_IN_MEMORY); |
62 | my $ob = cf::object::new "deep_sea"; |
|
|
63 | $self->insert ($ob, $X, $Y); |
|
|
64 | } |
|
|
65 | } |
|
|
66 | $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1); |
|
|
67 | } |
160 | } |
68 | } |
161 | } |
69 | |
162 | |
70 | sub pre_load { |
163 | sub post_load { |
71 | my ($self) = @_; |
164 | my ($self) = @_; |
|
|
165 | |
|
|
166 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
72 | |
167 | |
73 | my ($x, $y) = $self->wxwy; |
168 | my ($x, $y) = $self->wxwy; |
74 | |
169 | |
75 | my $stride = ($WORLD->{maxx} - $WORLD->{minx} + 1) * $WORLD->{tilew}; |
170 | if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { |
|
|
171 | my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; |
76 | my $top = ($y - $WORLD->{miny}) * $WORLD->{tileh} * $stride |
172 | my $top = ($y - 100) * $WORLD->{tile_h} * $stride |
77 | + ($x - $WORLD->{minx}) * $WORLD->{tilew}; |
173 | + ($x - 100) * $WORLD->{tile_w}; |
78 | |
174 | |
|
|
175 | my $reg; |
|
|
176 | |
79 | for my $Y (0 .. $WORLD->{tileh} - 1) { |
177 | for my $Y (0 .. $WORLD->{tile_h} - 1) { |
80 | Coro::cede; |
178 | Coro::cede; |
81 | my $row = substr $WORLD->{data}, $top + $Y * $stride, $WORLD->{tilew}; |
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}; |
82 | for my $X (0 .. $WORLD->{tilew} - 1) { |
181 | for my $X (0 .. $WORLD->{tile_w} - 1) { |
|
|
182 | next if grep $_->flag (cf::FLAG_IS_FLOOR), $self->at ($X, $Y); |
83 | my $ob = cf::object::new $WORLD->{arch}[ord substr $row, $X]; |
183 | my $ob = cf::object::new $WORLD->{arc_plt}[ord substr $row, $X]; |
84 | $self->insert ($ob, $X, $Y); |
184 | $ob->flag (cf::FLAG_NO_MAP_SAVE, 1); |
|
|
185 | $self->insert ($ob, $X, $Y, undef, cf::INS_ABOVE_FLOOR_ONLY); |
|
|
186 | } |
85 | } |
187 | } |
|
|
188 | |
|
|
189 | $self->set_regiondata ($reg, $WORLD->{reg_plt}); |
|
|
190 | } else { |
|
|
191 | $self->fill; |
86 | } |
192 | } |
87 | |
|
|
88 | $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1); |
|
|
89 | } |
193 | } |
90 | |
194 | |
91 | 1 |
195 | 1 |
92 | |
196 | |