1 | #! perl # OPTIONAL |
1 | #! perl # mandatory |
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 "%+04d", $_[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 my $meta (@AREAS) { |
|
|
52 | if ( $meta->{x0} <= $x && $x <= $meta->{x1} |
|
|
53 | && $meta->{y0} <= $y && $y <= $meta->{y1} |
|
|
54 | && $meta->{z0} <= $z && $z <= $meta->{z1} |
|
|
55 | ) { |
|
|
56 | $self->{ox} = $x - $meta->{x0}; |
|
|
57 | $self->{oy} = $y - $meta->{y0}; |
|
|
58 | $self->{oz} = $z - $meta->{z0}; |
|
|
59 | |
|
|
60 | bless $self, $meta->{pkg}; |
|
|
61 | |
|
|
62 | $self->init_worldmap ($meta); |
|
|
63 | |
|
|
64 | goto done; |
32 | } |
65 | } |
33 | } |
66 | } |
34 | |
67 | |
35 | my $data = cf::fork_call { |
68 | bless $self, ext::map_world::empty::; |
36 | open my $fh, "convert \Q$path.png\E -depth 8 rgb:- |" |
|
|
37 | or die "convert: $!"; |
|
|
38 | binmode $fh; |
|
|
39 | |
69 | |
40 | $size * 3 == read $fh, my $data, $size * 3 |
70 | done: |
41 | or die "$path.png, expected $size rgb triplets: $!"; |
71 | $self->SUPER::init; |
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 | } |
72 | } |
53 | |
73 | |
54 | sub load_gridmap($) { |
74 | sub init_worldmap { |
55 | my ($path) = @_; |
75 | # only for handlers |
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 | } |
76 | } |
103 | |
77 | |
104 | sub load_header_orig { |
78 | sub load_header_orig { |
105 | my ($self) = @_; |
79 | my ($self) = @_; |
106 | |
80 | |
107 | my ($x, $y) = $self->wxwy; |
81 | my ($x, $y, $z) = @$self{qw(x y z)}; |
108 | |
82 | |
109 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
83 | $self->width ($TILE_W); |
|
|
84 | $self->height ($TILE_H); |
110 | |
85 | |
111 | $self->width ($WORLD->{tile_w}); |
86 | $self->name (sprintf "'The World' at %+d%+d%+d", $x, $y, $z); |
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"); |
87 | $self->msg ("worldmap dynamically created by map-world extension"); |
116 | $self->outdoor (1); |
88 | $self->outdoor ($self->{z} >= 0); |
117 | $self->default_region (cf::region::find "wilderness"); |
89 | $self->default_region (cf::region::find "wilderness"); |
118 | |
90 | |
119 | $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; |
91 | # all possible worldmap tiles also exist |
120 | $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; |
92 | $self->tile_path (cf::TILE_NORTH, format_xyz $x, $y - 1, $z); |
121 | $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; |
93 | $self->tile_path (cf::TILE_SOUTH, format_xyz $x, $y + 1, $z); |
122 | $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; |
94 | $self->tile_path (cf::TILE_EAST , format_xyz $x + 1, $y, $z); |
|
|
95 | $self->tile_path (cf::TILE_WEST , format_xyz $x - 1, $y, $z); |
|
|
96 | $self->tile_path (cf::TILE_UP , format_xyz $x, $y, $z + 1); |
|
|
97 | $self->tile_path (cf::TILE_DOWN , format_xyz $x, $y, $z - 1); |
123 | |
98 | |
124 | $self->{load_path} = sprintf "%s/world-overlay/world_%03d_%03d.map", $cf::MAPDIR, $x, $y |
99 | # an overlay always wins |
125 | if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; |
100 | my $overlay = sprintf "%s/%s.map", $cf::MAPDIR, format_xyz $x, $y, $z; |
|
|
101 | |
|
|
102 | $self->{load_path} = $overlay |
|
|
103 | unless Coro::AIO::aio_stat $overlay; |
126 | |
104 | |
127 | $self->{need_create_treasure} = 1; |
105 | $self->{need_create_treasure} = 1; |
128 | |
106 | |
129 | 1 |
107 | 1 |
130 | } |
108 | } |
131 | |
109 | |
|
|
110 | # fill map with "default" data - first region and first archetype from palette |
|
|
111 | # used when we have no map data from elsewhere |
132 | sub fill { |
112 | sub fill { |
133 | my ($self) = @_; |
113 | my ($self) = @_; |
134 | |
114 | |
135 | $self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt}); |
115 | if ($self->{z} > 0) { |
136 | $self->default_region (cf::region::find $WORLD->{reg_plt}[0]); |
116 | # hmmm... air? :) |
137 | } |
117 | $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]); |
138 | |
118 | } elsif ($self->{z} < 0) { |
139 | sub load { |
119 | $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]); |
140 | my ($self) = @_; |
|
|
141 | |
|
|
142 | if ($self->{load_path}) { |
|
|
143 | $self->SUPER::load; |
|
|
144 | } else { |
120 | } else { |
145 | $self->alloc; |
121 | $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["deep_sea"]); |
146 | $self->fill; |
|
|
147 | $self->in_memory (cf::MAP_IN_MEMORY); |
|
|
148 | } |
122 | } |
149 | } |
123 | } |
150 | |
124 | |
|
|
125 | #sub load { |
|
|
126 | # my ($self) = @_; |
|
|
127 | # |
|
|
128 | # if ($self->{load_path}) { |
|
|
129 | # warn "load $self\n";#d# |
|
|
130 | # $self->SUPER::load; |
|
|
131 | # } else { |
|
|
132 | # warn "fill $self\n";#d# |
|
|
133 | # my $guard = cf::lock_acquire "map_data:$self->{path}"; |
|
|
134 | # |
|
|
135 | # $self->alloc; |
|
|
136 | # $self->fill; |
|
|
137 | # $self->state (cf::MAP_ACTIVE); |
|
|
138 | # $self->activate; |
|
|
139 | # $self->post_load; |
|
|
140 | # } |
|
|
141 | #} |
|
|
142 | |
|
|
143 | # MUST be overwritten to call at least ->fill |
151 | sub post_load { |
144 | sub post_load { |
152 | my ($self) = @_; |
145 | my ($self) = @_; |
153 | |
146 | |
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; |
147 | $self->create_region_treasure |
172 | # TODO: temporary? hack to fix multipart objects because treausres only create the head |
148 | if delete $self->{need_create_treasure}; |
173 | $self->link_multipart_objects; |
|
|
174 | } |
|
|
175 | } |
149 | } |
176 | |
150 | |
177 | 1 |
151 | package ext::map_world::empty; |
178 | |
152 | |
|
|
153 | our @ISA = ext::map_world::; |
|
|
154 | |
|
|
155 | sub post_load_original { |
|
|
156 | $_[0]->fill; |
|
|
157 | } |
|
|
158 | |