1 |
#! perl # OPTIONAL |
2 |
|
3 |
# optional plug-in to speed up worldmap rendering by dynamically |
4 |
# generating it out of an image |
5 |
# - saves loading time (less data to read) |
6 |
# - saves temporary space (only overlay stuff needs to be saved) |
7 |
# - might get reused as a generic tiled map |
8 |
|
9 |
use Coro::Handle; |
10 |
use Coro::AIO; |
11 |
|
12 |
our $WORLD; |
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 $data = cf::fork_call { |
36 |
open my $fh, "convert \Q$path.png\E -depth 8 rgb:- |" |
37 |
or die "convert: $!"; |
38 |
binmode $fh; |
39 |
|
40 |
$size * 3 == read $fh, my $data, $size * 3 |
41 |
or die "$path.png, expected $size rgb triplets: $!"; |
42 |
|
43 |
cf::_quantise $data, [map "$_$plt{$_}", keys %plt]; |
44 |
|
45 |
$data |
46 |
}; |
47 |
|
48 |
$size == length $data |
49 |
or cf::cleanup "$path.png, expected $size index octets ($!)"; |
50 |
|
51 |
($data, \@plt) |
52 |
} |
53 |
|
54 |
sub load_gridmap($) { |
55 |
my ($path) = @_; |
56 |
|
57 |
my $map = cf::cache "ext::map-world/gridmap" => |
58 |
[ |
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) = @_; |
67 |
|
68 |
my $map = cf::from_json $src->[0]; |
69 |
|
70 |
my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h} |
71 |
or cf::cleanup "$path/gridmap.meta: empty gridmap?"; |
72 |
|
73 |
($map->{arc_data}, $map->{arc_plt}) = load_indexed "$path/gridmap.arch", $size; |
74 |
($map->{reg_data}, $map->{reg_plt}) = load_indexed "$path/gridmap.regn", $size; |
75 |
|
76 |
Coro::Storable::freeze $map |
77 |
}; |
78 |
|
79 |
Coro::Storable::thaw $map |
80 |
} |
81 |
|
82 |
sub reload() { |
83 |
$WORLD = load_gridmap "$MAPDIR/world"; |
84 |
warn "worldmap gridmap loaded."; |
85 |
} |
86 |
|
87 |
# this is contorted, but likely the correct way to acquire the lock :) |
88 |
cf::sync_job { |
89 |
my $guard = cf::lock_acquire "ext::world_gridmap"; |
90 |
cf::async_ext { |
91 |
reload; |
92 |
undef $guard; |
93 |
}; |
94 |
}; |
95 |
|
96 |
cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); |
97 |
|
98 |
sub wxwy { |
99 |
$_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
100 |
? ($1, $2) |
101 |
: (0, 0) |
102 |
} |
103 |
|
104 |
sub load_header_orig { |
105 |
my ($self) = @_; |
106 |
|
107 |
my ($x, $y) = $self->wxwy; |
108 |
|
109 |
my $guard = cf::lock_acquire "ext::world_gridmap"; |
110 |
|
111 |
$self->width ($WORLD->{tile_w}); |
112 |
$self->height ($WORLD->{tile_h}); |
113 |
|
114 |
$self->name ("'The World' at +$x+$y"); |
115 |
$self->msg ("worldmap dynamically created by map-world extension"); |
116 |
$self->outdoor (1); |
117 |
$self->default_region (cf::region::find "wilderness"); |
118 |
|
119 |
$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 (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 |
|
124 |
$self->{load_path} = sprintf "%s/world-overlay/world_%03d_%03d.map", $cf::MAPDIR, $x, $y |
125 |
if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; |
126 |
|
127 |
$self->{need_create_treasure} = 1; |
128 |
|
129 |
1 |
130 |
} |
131 |
|
132 |
sub fill { |
133 |
my ($self) = @_; |
134 |
|
135 |
$self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt}); |
136 |
$self->default_region (cf::region::find $WORLD->{reg_plt}[0]); |
137 |
} |
138 |
|
139 |
sub load { |
140 |
my ($self) = @_; |
141 |
|
142 |
if ($self->{load_path}) { |
143 |
$self->SUPER::load; |
144 |
} else { |
145 |
$self->alloc; |
146 |
$self->fill; |
147 |
$self->in_memory (cf::MAP_IN_MEMORY); |
148 |
} |
149 |
} |
150 |
|
151 |
sub post_load { |
152 |
my ($self) = @_; |
153 |
|
154 |
my $guard = cf::lock_acquire "ext::world_gridmap"; |
155 |
|
156 |
my ($x, $y) = $self->wxwy; |
157 |
|
158 |
if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { |
159 |
my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; |
160 |
my $top = ($y - 100) * $WORLD->{tile_h} * $stride |
161 |
+ ($x - 100) * $WORLD->{tile_w}; |
162 |
|
163 |
$self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt}); |
164 |
$self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt}); |
165 |
|
166 |
} else { |
167 |
$self->fill; |
168 |
} |
169 |
|
170 |
if (delete $self->{need_create_treasure}) { |
171 |
$self->create_region_treasure; |
172 |
# TODO: temporary? hack to fix multipart objects because treausres only create the head |
173 |
$self->link_multipart_objects; |
174 |
} |
175 |
} |
176 |
|
177 |
1 |
178 |
|