1 |
#! perl # OPTIONAL |
2 |
|
3 |
# optional plug-in to speed up worldmap rendering by dynamically |
4 |
# generating it out of a image |
5 |
# - saves loading time (less data to read) |
6 |
# - saves temporary space (only overlay stuff needs to be saved) |
7 |
|
8 |
our $WORLD = Storable::retrieve cf::datadir . "/world.pst" |
9 |
or die "unable to load world.pst"; |
10 |
|
11 |
$WORLD->{version} == 1 |
12 |
or die "world.pst version mismatch, expected version 1, got $WORLD->{version}"; |
13 |
|
14 |
cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); |
15 |
|
16 |
sub init { |
17 |
my ($self) = @_; |
18 |
|
19 |
1 |
20 |
} |
21 |
|
22 |
sub wxwy { |
23 |
$_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
24 |
? ($1, $2) |
25 |
: (0, 0) |
26 |
} |
27 |
|
28 |
sub load_header_orig { |
29 |
my ($self) = @_; |
30 |
|
31 |
$self->width ($WORLD->{tilew}); |
32 |
$self->height ($WORLD->{tileh}); |
33 |
|
34 |
my ($x, $y) = $self->wxwy; |
35 |
|
36 |
$self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; |
37 |
$self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; |
38 |
$self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; |
39 |
$self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; |
40 |
|
41 |
$self->outdoor (1); |
42 |
|
43 |
$self->msg ("worldmap dynamically created by map-world extension"); |
44 |
|
45 |
#TODO: region |
46 |
|
47 |
$self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d", cf::datadir, cf::mapdir, $x, $y |
48 |
if $x >= $WORLD->{minx} && $x <= $WORLD->{maxx} |
49 |
&& $y >= $WORLD->{miny} && $y <= $WORLD->{maxy}; |
50 |
|
51 |
|
52 |
1 |
53 |
} |
54 |
|
55 |
sub load { |
56 |
my ($self) = @_; |
57 |
|
58 |
if ($self->{load_path}) { |
59 |
$self->SUPER::load; |
60 |
} else { |
61 |
$self->alloc; |
62 |
|
63 |
for my $X (0 .. $WORLD->{tilew} - 1) { |
64 |
Coro::cede; |
65 |
for my $Y (0 .. $WORLD->{tileh} - 1) { |
66 |
my $ob = cf::object::new "deep_sea"; |
67 |
$self->insert ($ob, $X, $Y); |
68 |
} |
69 |
} |
70 |
$self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1); |
71 |
|
72 |
$self->in_memory (cf::MAP_IN_MEMORY); |
73 |
} |
74 |
} |
75 |
|
76 |
sub post_load { |
77 |
my ($self) = @_; |
78 |
|
79 |
my ($x, $y) = $self->wxwy; |
80 |
|
81 |
return |
82 |
unless $x >= $WORLD->{minx} && $x <= $WORLD->{maxx} |
83 |
&& $y >= $WORLD->{miny} && $y <= $WORLD->{maxy}; |
84 |
|
85 |
my $stride = ($WORLD->{maxx} - $WORLD->{minx} + 1) * $WORLD->{tilew}; |
86 |
my $top = ($y - $WORLD->{miny}) * $WORLD->{tileh} * $stride |
87 |
+ ($x - $WORLD->{minx}) * $WORLD->{tilew}; |
88 |
|
89 |
for my $Y (0 .. $WORLD->{tileh} - 1) { |
90 |
Coro::cede; |
91 |
my $row = substr $WORLD->{data}, $top + $Y * $stride, $WORLD->{tilew}; |
92 |
for my $X (0 .. $WORLD->{tilew} - 1) { |
93 |
next if grep $_->flag (cf::FLAG_IS_FLOOR), $self->at ($X, $Y); |
94 |
my $ob = cf::object::new $WORLD->{arch}[ord substr $row, $X]; |
95 |
$ob->flag (cf::FLAG_NO_MAP_SAVE, 1); |
96 |
$self->insert ($ob, $X, $Y, undef, cf::INS_ABOVE_FLOOR_ONLY); |
97 |
} |
98 |
} |
99 |
} |
100 |
|
101 |
1 |
102 |
|