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