1 | #! perl # optional |
1 | #! perl # optional |
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 | cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); |
5 | cf::map->register (qr{^/world/world_(-?\d+)_(-?\d+)(?:_(-?\d+))?$}, 100); |
10 | |
6 | |
11 | use Coro::Handle; |
7 | # universal |
12 | use Coro::AIO; |
8 | our $TILE_W = 50; |
|
|
9 | our $TILE_H = 50; |
13 | |
10 | |
14 | my $TILE_W = 50; |
11 | # registry |
15 | my $TILE_H = 50; |
12 | our @AREAS; |
16 | |
13 | |
17 | my $GRID_W = 30; |
14 | sub register($$$$$$$@) { |
18 | my $GRID_H = 30; |
15 | my $meta = { |
|
|
16 | pkg => shift, |
|
|
17 | x0 => shift, y0 => shift, z0 => shift, |
|
|
18 | x1 => shift, y1 => shift, z1 => shift, |
|
|
19 | @_ |
|
|
20 | }; |
19 | |
21 | |
20 | our $ARCH; |
22 | my $pkg = $meta->{pkg}; |
21 | our $REGN; |
|
|
22 | |
23 | |
23 | sub reload() { |
24 | @AREAS = sort { $b->{pri} <=> $a->{pri} } |
24 | cf::trace "loading world+100+100 table.\n"; |
25 | $meta, |
25 | $ARCH = cf::decode_storable cf::unlzf cf::load_file "$DATADIR/world+100+100.arch"; |
26 | grep $pkg ne $_->{pkg}, |
26 | $REGN = cf::decode_storable cf::unlzf cf::load_file "$DATADIR/world+100+100.regn"; |
27 | @AREAS; |
27 | cf::trace "loaded world+100+100 table.\n"; |
28 | |
|
|
29 | my $isa = \@{"$pkg\::ISA"}; |
|
|
30 | |
|
|
31 | @$isa = ( |
|
|
32 | __PACKAGE__, |
|
|
33 | grep __PACKAGE__ ne $_, @$isa |
|
|
34 | ); |
28 | } |
35 | } |
29 | |
36 | |
30 | { |
37 | sub format_xyz($$;$) { |
31 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
38 | sprintf "/world/world_%03d_%03d%s", |
32 | |
39 | $_[0], $_[1], |
33 | cf::post_init { |
40 | $_[2] ? sprintf "_%03d", $_[2] : ""; |
34 | cf::async_ext { |
|
|
35 | $Coro::current->{desc} = "worldmap loader"; |
|
|
36 | reload; |
|
|
37 | undef $guard; |
|
|
38 | }; |
|
|
39 | }; |
|
|
40 | } |
41 | } |
41 | |
42 | |
42 | sub wxwy { |
43 | ############################################################################# |
43 | $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
44 | |
44 | ? ($1, $2) |
45 | sub init { |
45 | : (0, 0) |
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 (@AREAS) { |
|
|
52 | if ( $_->{x0} <= $x && $x <= $_->{x1} |
|
|
53 | && $_->{y0} <= $y && $y <= $_->{y1} |
|
|
54 | && $_->{z0} <= $z && $z <= $_->{z1} |
|
|
55 | ) { |
|
|
56 | $self->{ox} = $x - $_->{x0}; |
|
|
57 | $self->{oy} = $y - $_->{y0}; |
|
|
58 | $self->{oz} = $z - $_->{z0}; |
|
|
59 | |
|
|
60 | bless $self, $_->{pkg}; |
|
|
61 | |
|
|
62 | $self->init_worldmap; |
|
|
63 | |
|
|
64 | last; |
|
|
65 | } |
|
|
66 | |
|
|
67 | } |
|
|
68 | |
|
|
69 | $self->SUPER::init; |
|
|
70 | } |
|
|
71 | |
|
|
72 | sub init_worldmap { |
|
|
73 | # only for handlers |
46 | } |
74 | } |
47 | |
75 | |
48 | sub load_header_orig { |
76 | sub load_header_orig { |
49 | my ($self) = @_; |
77 | my ($self) = @_; |
50 | |
78 | |
51 | my ($x, $y) = $self->wxwy; |
79 | my ($x, $y, $z) = @$self{qw(x y z)}; |
52 | |
|
|
53 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
|
|
54 | |
80 | |
55 | $self->width ($TILE_W); |
81 | $self->width ($TILE_W); |
56 | $self->height ($TILE_H); |
82 | $self->height ($TILE_H); |
57 | |
83 | |
58 | $self->name ("'The World' at +$x+$y"); |
84 | $self->name (sprintf "'The World' at %+d%+d%+d", $x, $y, $z); |
59 | $self->msg ("worldmap dynamically created by map-world extension"); |
85 | $self->msg ("worldmap dynamically created by map-world extension"); |
60 | $self->outdoor (1); |
86 | $self->outdoor ($self->{z} >= 0); |
61 | $self->default_region (cf::region::find "wilderness"); |
87 | $self->default_region (cf::region::find "wilderness"); |
62 | |
88 | |
63 | $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; |
89 | # all possible worldmap tiles also exist |
64 | $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; |
90 | $self->tile_path (0, format_xyz $x, $y - 1, $z); |
65 | $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; |
91 | $self->tile_path (1, format_xyz $x + 1, $y, $z); |
66 | $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; |
92 | $self->tile_path (2, format_xyz $x, $y + 1, $z); |
|
|
93 | $self->tile_path (3, format_xyz $x - 1, $y, $z); |
|
|
94 | $self->tile_path (4, format_xyz $x, $y, $z + 1); |
|
|
95 | $self->tile_path (5, format_xyz $x, $y, $z - 1); |
67 | |
96 | |
|
|
97 | # an overlay always wins |
68 | my $overlay = sprintf "%s/world/world_%03d_%03d.map", $cf::MAPDIR, $x, $y; |
98 | my $overlay = sprintf "%s/%s.map", $cf::MAPDIR, format_xyz $x, $y, $z; |
69 | |
99 | |
70 | $self->{load_path} = $overlay |
100 | $self->{load_path} = $overlay |
71 | unless Coro::AIO::aio_stat $overlay; |
101 | unless Coro::AIO::aio_stat $overlay; |
72 | |
102 | |
73 | $self->{need_create_treasure} = 1; |
103 | $self->{need_create_treasure} = 1; |
74 | |
104 | |
75 | 1 |
105 | 1 |
76 | } |
106 | } |
77 | |
107 | |
|
|
108 | # fill map with "default" data - first region and first archetype from palette |
|
|
109 | # used when we have no map data from elsewhere |
78 | sub fill { |
110 | sub fill { |
79 | my ($self) = @_; |
111 | my ($self) = @_; |
80 | |
112 | |
|
|
113 | if ($self->{z} > 0) { |
|
|
114 | # hmmm... air? :) |
81 | $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, $ARCH->{plt}); |
115 | $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]); |
82 | $self->default_region (cf::region::find $REGN->{plt}[0]); |
116 | } elsif ($self->{z} < 0) { |
|
|
117 | $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]); |
|
|
118 | } else { |
|
|
119 | $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["deep_sea"]); |
|
|
120 | } |
83 | } |
121 | } |
84 | |
122 | |
85 | sub load { |
123 | sub load { |
86 | my ($self) = @_; |
124 | my ($self) = @_; |
87 | |
125 | |
… | |
… | |
93 | $self->in_memory (cf::MAP_ACTIVE); |
131 | $self->in_memory (cf::MAP_ACTIVE); |
94 | $self->activate; |
132 | $self->activate; |
95 | } |
133 | } |
96 | } |
134 | } |
97 | |
135 | |
|
|
136 | # MUST be overwritten to call at least ->fill |
98 | sub post_load { |
137 | sub post_load { |
99 | my ($self) = @_; |
138 | my ($self) = @_; |
100 | |
|
|
101 | { |
|
|
102 | my $guard = cf::lock_acquire "ext::world_gridmap"; |
|
|
103 | |
|
|
104 | my ($x, $y) = $self->wxwy; |
|
|
105 | |
|
|
106 | if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { |
|
|
107 | my $stride = $GRID_W * $TILE_W; |
|
|
108 | my $top = ($y - 100) * $TILE_H * $stride |
|
|
109 | + ($x - 100) * $TILE_W; |
|
|
110 | |
|
|
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; |
|
|
116 | } |
|
|
117 | } |
|
|
118 | |
139 | |
119 | $self->create_region_treasure |
140 | $self->create_region_treasure |
120 | if delete $self->{need_create_treasure}; |
141 | if delete $self->{need_create_treasure}; |
121 | } |
142 | } |
122 | |
143 | |