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 an 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 | # - might get reused as a generic tiled map |
|
|
8 | |
|
|
9 | cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); |
8 | |
10 | |
9 | use Coro::Handle; |
11 | use Coro::Handle; |
10 | use Coro::AIO; |
12 | use Coro::AIO; |
11 | |
13 | |
12 | our $WORLD; |
14 | our $WORLD; |
… | |
… | |
78 | |
80 | |
79 | Coro::Storable::thaw $map |
81 | Coro::Storable::thaw $map |
80 | } |
82 | } |
81 | |
83 | |
82 | sub reload() { |
84 | sub reload() { |
83 | $WORLD = load_gridmap sprintf "%s/%s/%s", cf::datadir, cf::mapdir, "world"; |
85 | $WORLD = load_gridmap "$MAPDIR/world"; |
84 | warn "worldmap gridmap loaded."; |
86 | warn "worldmap gridmap loaded."; |
85 | } |
87 | } |
86 | |
88 | |
87 | # this is contorted, but likely the correct way to acquire the lock :) |
89 | # this is contorted, but likely the correct way to acquire the lock :) |
88 | cf::sync_job { |
90 | cf::sync_job { |
89 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
91 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
90 | cf::async_ext { |
92 | cf::async_ext { |
|
|
93 | $Coro::current->{desc} = "worldmap loader"; |
91 | reload; |
94 | reload; |
92 | undef $guard; |
95 | undef $guard; |
93 | } |
96 | }; |
94 | }; |
97 | }; |
95 | |
|
|
96 | cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); |
|
|
97 | |
98 | |
98 | sub wxwy { |
99 | sub wxwy { |
99 | $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
100 | $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
100 | ? ($1, $2) |
101 | ? ($1, $2) |
101 | : (0, 0) |
102 | : (0, 0) |
… | |
… | |
119 | $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; |
120 | $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; |
121 | $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; |
122 | $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; |
123 | |
124 | |
124 | $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d.map", cf::datadir, cf::mapdir, $x, $y |
125 | my $overlay = sprintf "%s/world/world_%03d_%03d.map", $cf::MAPDIR, $x, $y; |
125 | if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; |
126 | |
|
|
127 | $self->{load_path} = $overlay |
|
|
128 | unless Coro::AIO::aio_stat $overlay; |
|
|
129 | |
|
|
130 | $self->{need_create_treasure} = 1; |
126 | |
131 | |
127 | 1 |
132 | 1 |
128 | } |
133 | } |
129 | |
134 | |
130 | sub fill { |
135 | sub fill { |
… | |
… | |
141 | $self->SUPER::load; |
146 | $self->SUPER::load; |
142 | } else { |
147 | } else { |
143 | $self->alloc; |
148 | $self->alloc; |
144 | $self->fill; |
149 | $self->fill; |
145 | $self->in_memory (cf::MAP_IN_MEMORY); |
150 | $self->in_memory (cf::MAP_IN_MEMORY); |
|
|
151 | $self->activate; |
146 | } |
152 | } |
147 | } |
153 | } |
148 | |
154 | |
149 | sub post_load { |
155 | sub post_load { |
150 | my ($self) = @_; |
156 | my ($self) = @_; |
151 | |
157 | |
|
|
158 | { |
152 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
159 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
153 | |
160 | |
154 | my ($x, $y) = $self->wxwy; |
161 | my ($x, $y) = $self->wxwy; |
155 | |
162 | |
156 | if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { |
163 | if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { |
157 | my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; |
164 | my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; |
158 | my $top = ($y - 100) * $WORLD->{tile_h} * $stride |
165 | my $top = ($y - 100) * $WORLD->{tile_h} * $stride |
159 | + ($x - 100) * $WORLD->{tile_w}; |
166 | + ($x - 100) * $WORLD->{tile_w}; |
160 | |
167 | |
161 | $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt}); |
168 | $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt}); |
162 | $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt}); |
169 | $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt}); |
163 | |
170 | |
164 | } else { |
171 | } else { |
165 | $self->fill; |
172 | $self->fill; |
|
|
173 | } |
166 | } |
174 | } |
|
|
175 | |
|
|
176 | $self->create_region_treasure |
|
|
177 | if delete $self->{need_create_treasure}; |
167 | } |
178 | } |
168 | |
179 | |
169 | 1 |
180 | 1 |
170 | |
181 | |