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 a 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 | |
7 | # - might get reused as a generic tiled map |
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 | |
8 | |
14 | cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); |
9 | cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); |
15 | |
10 | |
16 | sub init { |
11 | use Coro::Handle; |
17 | my ($self) = @_; |
12 | use Coro::AIO; |
18 | |
13 | |
19 | 1 |
14 | my $TILE_W = 50; |
|
|
15 | my $TILE_H = 50; |
|
|
16 | |
|
|
17 | my $GRID_W = 30; |
|
|
18 | my $GRID_H = 30; |
|
|
19 | |
|
|
20 | our $ARCH; |
|
|
21 | our $REGN; |
|
|
22 | |
|
|
23 | sub reload() { |
|
|
24 | cf::trace "loading world+100+100 table.\n"; |
|
|
25 | $ARCH = cf::decode_storable cf::unlzf cf::load_file "$DATADIR/world+100+100.arch"; |
|
|
26 | $REGN = cf::decode_storable cf::unlzf cf::load_file "$DATADIR/world+100+100.regn"; |
|
|
27 | cf::trace "loaded world+100+100 table.\n"; |
|
|
28 | } |
|
|
29 | |
|
|
30 | { |
|
|
31 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
|
|
32 | |
|
|
33 | cf::post_init { |
|
|
34 | cf::async_ext { |
|
|
35 | $Coro::current->{desc} = "worldmap loader"; |
|
|
36 | reload; |
|
|
37 | undef $guard; |
|
|
38 | }; |
|
|
39 | }; |
20 | } |
40 | } |
21 | |
41 | |
22 | sub wxwy { |
42 | sub wxwy { |
23 | $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
43 | $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
24 | ? ($1, $2) |
44 | ? ($1, $2) |
… | |
… | |
26 | } |
46 | } |
27 | |
47 | |
28 | sub load_header_orig { |
48 | sub load_header_orig { |
29 | my ($self) = @_; |
49 | my ($self) = @_; |
30 | |
50 | |
31 | $self->width ($WORLD->{tilew}); |
51 | my ($x, $y) = $self->wxwy; |
32 | $self->height ($WORLD->{tileh}); |
|
|
33 | |
52 | |
34 | my ($x, $y) = $self->wxwy; |
53 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
|
|
54 | |
|
|
55 | $self->width ($TILE_W); |
|
|
56 | $self->height ($TILE_H); |
|
|
57 | |
|
|
58 | $self->name ("'The World' at +$x+$y"); |
|
|
59 | $self->msg ("worldmap dynamically created by map-world extension"); |
|
|
60 | $self->outdoor (1); |
|
|
61 | $self->default_region (cf::region::find "wilderness"); |
35 | |
62 | |
36 | $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; |
63 | $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; |
64 | $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; |
65 | $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; |
66 | $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; |
40 | |
67 | |
41 | $self->msg ("worldmap dynamically created by map-world extension"); |
68 | my $overlay = sprintf "%s/world/world_%03d_%03d.map", $cf::MAPDIR, $x, $y; |
42 | |
69 | |
43 | #TODO: region |
70 | $self->{load_path} = $overlay |
44 | |
71 | unless Coro::AIO::aio_stat $overlay; |
45 | $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d", cf::datadir, cf::mapdir, $x, $y |
|
|
46 | if $x >= $WORLD->{minx} && $x <= $WORLD->{maxx} |
|
|
47 | && $y >= $WORLD->{miny} && $y <= $WORLD->{maxy}; |
|
|
48 | |
72 | |
|
|
73 | $self->{need_create_treasure} = 1; |
49 | |
74 | |
50 | 1 |
75 | 1 |
|
|
76 | } |
|
|
77 | |
|
|
78 | sub fill { |
|
|
79 | my ($self) = @_; |
|
|
80 | |
|
|
81 | $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, $ARCH->{plt}); |
|
|
82 | $self->default_region (cf::region::find $REGN->{plt}[0]); |
51 | } |
83 | } |
52 | |
84 | |
53 | sub load { |
85 | sub load { |
54 | my ($self) = @_; |
86 | my ($self) = @_; |
55 | |
87 | |
56 | if ($self->{load_path}) { |
88 | if ($self->{load_path}) { |
57 | $self->SUPER::load; |
89 | $self->SUPER::load; |
58 | } else { |
90 | } else { |
59 | $self->alloc; |
91 | $self->alloc; |
60 | |
92 | $self->fill; |
61 | for my $X (0 .. $WORLD->{tilew} - 1) { |
|
|
62 | Coro::cede; |
|
|
63 | for my $Y (0 .. $WORLD->{tileh} - 1) { |
|
|
64 | my $ob = cf::object::new "deep_sea"; |
|
|
65 | $self->insert ($ob, $X, $Y); |
|
|
66 | } |
|
|
67 | } |
|
|
68 | $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1); |
|
|
69 | |
|
|
70 | $self->in_memory (cf::MAP_IN_MEMORY); |
93 | $self->in_memory (cf::MAP_ACTIVE); |
|
|
94 | $self->activate; |
71 | } |
95 | } |
72 | } |
96 | } |
73 | |
97 | |
74 | sub pre_load { |
98 | sub post_load { |
75 | my ($self) = @_; |
99 | my ($self) = @_; |
76 | |
100 | |
77 | my ($x, $y) = $self->wxwy; |
101 | { |
|
|
102 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
78 | |
103 | |
79 | my $stride = ($WORLD->{maxx} - $WORLD->{minx} + 1) * $WORLD->{tilew}; |
104 | my ($x, $y) = $self->wxwy; |
80 | my $top = ($y - $WORLD->{miny}) * $WORLD->{tileh} * $stride |
|
|
81 | + ($x - $WORLD->{minx}) * $WORLD->{tilew}; |
|
|
82 | |
105 | |
83 | for my $Y (0 .. $WORLD->{tileh} - 1) { |
106 | if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { |
84 | Coro::cede; |
107 | my $stride = $GRID_W * $TILE_W; |
85 | my $row = substr $WORLD->{data}, $top + $Y * $stride, $WORLD->{tilew}; |
108 | my $top = ($y - 100) * $TILE_H * $stride |
86 | for my $X (0 .. $WORLD->{tilew} - 1) { |
109 | + ($x - 100) * $TILE_W; |
87 | my $ob = cf::object::new $WORLD->{arch}[ord substr $row, $X]; |
110 | |
88 | $self->insert ($ob, $X, $Y); |
111 | $self->add_underlay ($ARCH->{tbl}, $top, $stride, $ARCH->{plt}); |
|
|
112 | $self->set_regiondata ($REGN->{tbl}, $top, $stride, $REGN->{plt}); |
|
|
113 | |
|
|
114 | } else { |
|
|
115 | $self->fill; |
89 | } |
116 | } |
90 | } |
117 | } |
91 | |
118 | |
92 | $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1); |
119 | $self->create_region_treasure |
|
|
120 | if delete $self->{need_create_treasure}; |
93 | } |
121 | } |
94 | |
122 | |
95 | 1 |
|
|
96 | |
|
|