| 1 |
root |
1.1 |
#! perl # OPTIONAL |
| 2 |
|
|
|
| 3 |
|
|
# optional plug-in to speed up worldmap rendering by dynamically |
| 4 |
root |
1.8 |
# generating it out of an image |
| 5 |
root |
1.1 |
# - saves loading time (less data to read) |
| 6 |
|
|
# - saves temporary space (only overlay stuff needs to be saved) |
| 7 |
root |
1.8 |
# - might get reused as a generic tiled map |
| 8 |
root |
1.1 |
|
| 9 |
root |
1.8 |
use Coro::Handle; |
| 10 |
|
|
use Coro::AIO; |
| 11 |
root |
1.1 |
|
| 12 |
root |
1.8 |
our $WORLD; |
| 13 |
root |
1.1 |
|
| 14 |
root |
1.8 |
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 $pid = open my $pipe, "-|"; |
| 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 |
|
|
} |
| 68 |
|
|
|
| 69 |
|
|
sub load_gridmap($) { |
| 70 |
|
|
my ($path) = @_; |
| 71 |
|
|
|
| 72 |
root |
1.16 |
if (1 || ! -e "/tmp/xxxx.gridmap") { |
| 73 |
root |
1.11 |
0 < aio_load "$path/gridmap.meta", my $map |
| 74 |
|
|
or cf::cleanup "$path/gridmap.meta: $!\n"; |
| 75 |
root |
1.8 |
|
| 76 |
root |
1.11 |
$map = cf::from_json $map; |
| 77 |
root |
1.8 |
|
| 78 |
root |
1.11 |
my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h} |
| 79 |
|
|
or cf::cleanup "$path/gridmap.meta: empty gridmap?"; |
| 80 |
root |
1.1 |
|
| 81 |
root |
1.11 |
($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 |
root |
1.1 |
|
| 84 |
root |
1.11 |
Storable::nstore $map, "/tmp/xxxx.gridmap";#d# |
| 85 |
|
|
|
| 86 |
|
|
$map |
| 87 |
|
|
} else { |
| 88 |
|
|
Storable::retrieve "/tmp/xxxx.gridmap"; |
| 89 |
|
|
} |
| 90 |
root |
1.1 |
} |
| 91 |
|
|
|
| 92 |
root |
1.8 |
# 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 |
root |
1.1 |
sub wxwy { |
| 105 |
|
|
$_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
| 106 |
|
|
? ($1, $2) |
| 107 |
|
|
: (0, 0) |
| 108 |
|
|
} |
| 109 |
|
|
|
| 110 |
|
|
sub load_header_orig { |
| 111 |
|
|
my ($self) = @_; |
| 112 |
|
|
|
| 113 |
root |
1.8 |
my ($x, $y) = $self->wxwy; |
| 114 |
root |
1.1 |
|
| 115 |
root |
1.8 |
my $guard = cf::lock_acquire "ext::world_gridmap"; |
| 116 |
root |
1.1 |
|
| 117 |
root |
1.8 |
$self->width ($WORLD->{tile_w}); |
| 118 |
|
|
$self->height ($WORLD->{tile_h}); |
| 119 |
root |
1.3 |
|
| 120 |
root |
1.6 |
$self->name ("'The World' at +$x+$y"); |
| 121 |
root |
1.1 |
$self->msg ("worldmap dynamically created by map-world extension"); |
| 122 |
root |
1.8 |
$self->outdoor (1); |
| 123 |
|
|
$self->default_region (undef); |
| 124 |
root |
1.1 |
|
| 125 |
root |
1.12 |
$self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; |
| 126 |
|
|
$self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; |
| 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 |
root |
1.7 |
|
| 130 |
root |
1.14 |
$self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d.map", cf::datadir, cf::mapdir, $x, $y |
| 131 |
root |
1.8 |
if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; |
| 132 |
root |
1.1 |
|
| 133 |
|
|
1 |
| 134 |
|
|
} |
| 135 |
|
|
|
| 136 |
root |
1.13 |
sub fill { |
| 137 |
|
|
my ($self) = @_; |
| 138 |
|
|
|
| 139 |
root |
1.15 |
$self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt}); |
| 140 |
|
|
$self->default_region (cf::region::find $WORLD->{reg_plt}[0]); |
| 141 |
root |
1.13 |
} |
| 142 |
|
|
|
| 143 |
root |
1.1 |
sub load { |
| 144 |
|
|
my ($self) = @_; |
| 145 |
|
|
|
| 146 |
|
|
if ($self->{load_path}) { |
| 147 |
|
|
$self->SUPER::load; |
| 148 |
|
|
} else { |
| 149 |
root |
1.2 |
$self->alloc; |
| 150 |
root |
1.13 |
$self->fill; |
| 151 |
root |
1.2 |
$self->in_memory (cf::MAP_IN_MEMORY); |
| 152 |
root |
1.1 |
} |
| 153 |
|
|
} |
| 154 |
|
|
|
| 155 |
root |
1.3 |
sub post_load { |
| 156 |
root |
1.1 |
my ($self) = @_; |
| 157 |
|
|
|
| 158 |
root |
1.8 |
my $guard = cf::lock_acquire "ext::world_gridmap"; |
| 159 |
|
|
|
| 160 |
root |
1.1 |
my ($x, $y) = $self->wxwy; |
| 161 |
|
|
|
| 162 |
root |
1.13 |
if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { |
| 163 |
|
|
my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; |
| 164 |
|
|
my $top = ($y - 100) * $WORLD->{tile_h} * $stride |
| 165 |
|
|
+ ($x - 100) * $WORLD->{tile_w}; |
| 166 |
root |
1.8 |
|
| 167 |
root |
1.15 |
$self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt}); |
| 168 |
|
|
$self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt}); |
| 169 |
root |
1.1 |
|
| 170 |
root |
1.13 |
} else { |
| 171 |
|
|
$self->fill; |
| 172 |
root |
1.1 |
} |
| 173 |
|
|
} |
| 174 |
|
|
|
| 175 |
|
|
1 |
| 176 |
|
|
|