1 | #! perl # OPTIONAL |
1 | #! perl # optional |
2 | |
2 | |
3 | # optional plug-in to speed up worldmap rendering by dynamically |
3 | # manages the worldmap by directing to other handlers |
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 | |
4 | |
9 | use Coro::Handle; |
5 | cf::map->register (qr{^/world/world_(-?\d+)_(-?\d+)(?:_(-?\d+))?$}, 100); |
10 | use Coro::AIO; |
|
|
11 | |
6 | |
12 | our $WORLD; |
7 | # universal |
|
|
8 | our $TILE_W = 50; |
|
|
9 | our $TILE_H = 50; |
13 | |
10 | |
14 | sub load_indexed($$) { |
11 | # registry |
15 | my ($path, $size) = @_; |
12 | our @AREAS; |
16 | |
13 | |
17 | use bytes; |
14 | sub register($$$$$$$@) { |
|
|
15 | my $meta = { |
|
|
16 | pkg => shift, |
|
|
17 | x0 => shift, y0 => shift, z0 => shift, |
|
|
18 | x1 => shift, y1 => shift, z1 => shift, |
|
|
19 | @_ |
|
|
20 | }; |
18 | |
21 | |
19 | 0 < aio_load "$path.plt", my $plt |
22 | my $pkg = $meta->{pkg}; |
20 | or cf::cleanup "$path.plt: $!"; |
|
|
21 | |
23 | |
22 | my %plt; |
24 | @AREAS = sort { $b->{pri} <=> $a->{pri} } |
23 | my @plt; |
25 | $meta, |
|
|
26 | grep $pkg ne $_->{pkg}, |
|
|
27 | @AREAS; |
24 | |
28 | |
25 | for (split /\n/, $plt) { |
29 | my $isa = \@{"$pkg\::ISA"}; |
26 | my ($name, $rgb) = split /\s+/; |
30 | |
27 | if (/^(\S+)\s+(\S+)$/) { |
31 | @$isa = ( |
28 | my ($name, $rgb) = ($1, $2); |
32 | __PACKAGE__, |
29 | $rgb = join "", map chr, map $_ * 17, map hex, split //, $rgb; |
33 | grep __PACKAGE__ ne $_, @$isa |
30 | $plt{$rgb} = chr @plt; |
34 | ); |
31 | push @plt, $name; |
35 | } |
|
|
36 | |
|
|
37 | sub format_xyz($$;$) { |
|
|
38 | sprintf "/world/world_%03d_%03d%s", |
|
|
39 | $_[0], $_[1], |
|
|
40 | $_[2] ? sprintf "_%03d", $_[2] : ""; |
|
|
41 | } |
|
|
42 | |
|
|
43 | ############################################################################# |
|
|
44 | |
|
|
45 | sub init { |
|
|
46 | my ($self) = @_; |
|
|
47 | |
|
|
48 | my ($x, $y, $z) = @$self{qw(x y z)} = ($1, $2, $3+0); |
|
|
49 | |
|
|
50 | # find handler for this area |
|
|
51 | for (@AREAS) { |
|
|
52 | if ( $_->{x0} <= $x && $x <= $_->{x1} |
|
|
53 | && $_->{y0} <= $y && $y <= $_->{y1} |
|
|
54 | && $_->{z0} <= $z && $z <= $_->{z1} |
|
|
55 | ) { |
|
|
56 | $self->{ox} = $x - $_->{x0}; |
|
|
57 | $self->{oy} = $y - $_->{y0}; |
|
|
58 | $self->{oz} = $z - $_->{z0}; |
|
|
59 | |
|
|
60 | bless $self, $_->{pkg}; |
|
|
61 | |
|
|
62 | $self->init_worldmap; |
|
|
63 | |
|
|
64 | last; |
32 | } |
65 | } |
|
|
66 | |
33 | } |
67 | } |
34 | |
68 | |
35 | my $pid = open my $pipe, "-|"; |
69 | $self->SUPER::init; |
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 | } |
70 | } |
68 | |
71 | |
69 | sub load_gridmap($) { |
72 | sub init_worldmap { |
70 | my ($path) = @_; |
73 | # only for handlers |
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 | }; |
|
|
101 | |
|
|
102 | cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); |
|
|
103 | |
|
|
104 | sub wxwy { |
|
|
105 | $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
|
|
106 | ? ($1, $2) |
|
|
107 | : (0, 0) |
|
|
108 | } |
74 | } |
109 | |
75 | |
110 | sub load_header_orig { |
76 | sub load_header_orig { |
111 | my ($self) = @_; |
77 | my ($self) = @_; |
112 | |
78 | |
113 | my ($x, $y) = $self->wxwy; |
79 | my ($x, $y, $z) = @$self{qw(x y z)}; |
114 | |
80 | |
115 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
81 | $self->width ($TILE_W); |
|
|
82 | $self->height ($TILE_H); |
116 | |
83 | |
117 | $self->width ($WORLD->{tile_w}); |
84 | $self->name (sprintf "'The World' at %+d%+d%+d", $x, $y, $z); |
118 | $self->height ($WORLD->{tile_h}); |
85 | $self->msg ("worldmap dynamically created by map-world extension"); |
|
|
86 | $self->outdoor ($self->{z} >= 0); |
|
|
87 | $self->default_region (cf::region::find "wilderness"); |
119 | |
88 | |
120 | $self->name ("'The World' at +$x+$y"); |
89 | # all possible worldmap tiles also exist |
121 | $self->msg ("worldmap dynamically created by map-world extension"); |
90 | $self->tile_path (0, format_xyz $x, $y - 1, $z); |
122 | $self->outdoor (1); |
91 | $self->tile_path (1, format_xyz $x + 1, $y, $z); |
123 | $self->default_region (undef); |
92 | $self->tile_path (2, format_xyz $x, $y + 1, $z); |
|
|
93 | $self->tile_path (3, format_xyz $x - 1, $y, $z); |
|
|
94 | $self->tile_path (4, format_xyz $x, $y, $z + 1); |
|
|
95 | $self->tile_path (5, format_xyz $x, $y, $z - 1); |
124 | |
96 | |
125 | $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; |
97 | # an overlay always wins |
126 | $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; |
98 | my $overlay = sprintf "%s/%s.map", $cf::MAPDIR, format_xyz $x, $y, $z; |
127 | $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; |
|
|
128 | $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; |
|
|
129 | |
99 | |
130 | $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d.map", cf::datadir, cf::mapdir, $x, $y |
100 | $self->{load_path} = $overlay |
131 | if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; |
101 | unless Coro::AIO::aio_stat $overlay; |
|
|
102 | |
|
|
103 | $self->{need_create_treasure} = 1; |
132 | |
104 | |
133 | 1 |
105 | 1 |
134 | } |
106 | } |
135 | |
107 | |
|
|
108 | # fill map with "default" data - first region and first archetype from palette |
|
|
109 | # used when we have no map data from elsewhere |
136 | sub fill { |
110 | sub fill { |
137 | my ($self) = @_; |
111 | my ($self) = @_; |
138 | |
112 | |
139 | for my $X (0 .. $WORLD->{tile_w} - 1) { |
113 | if ($self->{z} > 0) { |
140 | Coro::cede; |
114 | # hmmm... air? :) |
141 | for my $Y (0 .. $WORLD->{tile_h} - 1) { |
115 | $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]); |
142 | my $ob = cf::object::new "deep_sea"; |
116 | } elsif ($self->{z} < 0) { |
143 | $ob->flag (cf::FLAG_NO_MAP_SAVE, 1); |
117 | $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]); |
144 | $self->insert ($ob, $X, $Y, undef, cf::INS_ABOVE_FLOOR_ONLY); |
118 | } else { |
145 | } |
119 | $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["deep_sea"]); |
146 | } |
120 | } |
147 | $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1); |
|
|
148 | $self->default_region (cf::region::find "panthalassia"); |
|
|
149 | } |
121 | } |
150 | |
122 | |
151 | sub load { |
123 | sub load { |
152 | my ($self) = @_; |
124 | my ($self) = @_; |
153 | |
125 | |
154 | if ($self->{load_path}) { |
126 | if ($self->{load_path}) { |
155 | $self->SUPER::load; |
127 | $self->SUPER::load; |
156 | } else { |
128 | } else { |
157 | $self->alloc; |
129 | $self->alloc; |
158 | $self->fill; |
130 | $self->fill; |
159 | $self->in_memory (cf::MAP_IN_MEMORY); |
131 | $self->in_memory (cf::MAP_ACTIVE); |
|
|
132 | $self->activate; |
160 | } |
133 | } |
161 | } |
134 | } |
162 | |
135 | |
|
|
136 | # MUST be overwritten to call at least ->fill |
163 | sub post_load { |
137 | sub post_load { |
164 | my ($self) = @_; |
138 | my ($self) = @_; |
165 | |
139 | |
166 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
140 | $self->create_region_treasure |
167 | |
141 | if delete $self->{need_create_treasure}; |
168 | my ($x, $y) = $self->wxwy; |
|
|
169 | |
|
|
170 | if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { |
|
|
171 | my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; |
|
|
172 | my $top = ($y - 100) * $WORLD->{tile_h} * $stride |
|
|
173 | + ($x - 100) * $WORLD->{tile_w}; |
|
|
174 | |
|
|
175 | my $reg; |
|
|
176 | |
|
|
177 | for my $Y (0 .. $WORLD->{tile_h} - 1) { |
|
|
178 | Coro::cede; |
|
|
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}; |
|
|
181 | for my $X (0 .. $WORLD->{tile_w} - 1) { |
|
|
182 | next if grep $_->flag (cf::FLAG_IS_FLOOR), $self->at ($X, $Y); |
|
|
183 | my $ob = cf::object::new $WORLD->{arc_plt}[ord substr $row, $X]; |
|
|
184 | $ob->flag (cf::FLAG_NO_MAP_SAVE, 1); |
|
|
185 | $self->insert ($ob, $X, $Y, undef, cf::INS_ABOVE_FLOOR_ONLY); |
|
|
186 | } |
|
|
187 | } |
|
|
188 | |
|
|
189 | $self->set_regiondata ($reg, $WORLD->{reg_plt}); |
|
|
190 | } else { |
|
|
191 | $self->fill; |
|
|
192 | } |
|
|
193 | } |
142 | } |
194 | |
143 | |
195 | 1 |
|
|
196 | |
|
|