1 | #! perl # OPTIONAL |
1 | #! perl # mandatory |
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 a image |
|
|
5 | # - saves loading time (less data to read) |
|
|
6 | # - saves temporary space (only overlay stuff needs to be saved) |
|
|
7 | |
4 | |
8 | our $WORLD = Storable::retrieve cf::datadir . "/world.pst" |
5 | cf::map->register (qr{^/world/world_(-?\d+)_(-?\d+)(?:_(-?\d+))?$}, 100); |
9 | or die "unable to load world.pst"; |
|
|
10 | |
6 | |
11 | $WORLD->{version} == 1 |
7 | # universal |
12 | or die "world.pst version mismatch, expected version 1, got $WORLD->{version}"; |
8 | our $TILE_W = 50; |
|
|
9 | our $TILE_H = 50; |
13 | |
10 | |
14 | cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); |
11 | # registry |
|
|
12 | our @AREAS; |
|
|
13 | |
|
|
14 | sub register($$$$$$$@) { |
|
|
15 | my $meta = { |
|
|
16 | pkg => shift, |
|
|
17 | x0 => shift, y0 => shift, z0 => shift, |
|
|
18 | x1 => shift, y1 => shift, z1 => shift, |
|
|
19 | @_ |
|
|
20 | }; |
|
|
21 | |
|
|
22 | my $pkg = $meta->{pkg}; |
|
|
23 | |
|
|
24 | @AREAS = sort { $b->{pri} <=> $a->{pri} } |
|
|
25 | $meta, |
|
|
26 | grep $pkg ne $_->{pkg}, |
|
|
27 | @AREAS; |
|
|
28 | |
|
|
29 | my $isa = \@{"$pkg\::ISA"}; |
|
|
30 | |
|
|
31 | @$isa = ( |
|
|
32 | __PACKAGE__, |
|
|
33 | grep __PACKAGE__ ne $_, @$isa |
|
|
34 | ); |
|
|
35 | } |
|
|
36 | |
|
|
37 | sub format_xyz($$;$) { |
|
|
38 | sprintf "/world/world_%03d_%03d%s", |
|
|
39 | $_[0], $_[1], |
|
|
40 | $_[2] ? sprintf "_%03d", $_[2] : ""; |
|
|
41 | } |
|
|
42 | |
|
|
43 | ############################################################################# |
15 | |
44 | |
16 | sub init { |
45 | sub init { |
17 | my ($self) = @_; |
46 | my ($self) = @_; |
18 | |
47 | |
|
|
48 | my ($x, $y, $z) = @$self{qw(x y z)} = ($1, $2, $3+0); |
|
|
49 | |
|
|
50 | # find handler for this area |
|
|
51 | for my $meta (@AREAS) { |
|
|
52 | if ( $meta->{x0} <= $x && $x <= $meta->{x1} |
|
|
53 | && $meta->{y0} <= $y && $y <= $meta->{y1} |
|
|
54 | && $meta->{z0} <= $z && $z <= $meta->{z1} |
|
|
55 | ) { |
|
|
56 | $self->{ox} = $x - $meta->{x0}; |
|
|
57 | $self->{oy} = $y - $meta->{y0}; |
|
|
58 | $self->{oz} = $z - $meta->{z0}; |
|
|
59 | |
|
|
60 | bless $self, $meta->{pkg}; |
|
|
61 | |
|
|
62 | $self->init_worldmap ($meta); |
|
|
63 | |
|
|
64 | last; |
|
|
65 | } |
|
|
66 | |
19 | 1 |
67 | } |
|
|
68 | |
|
|
69 | $self->SUPER::init; |
20 | } |
70 | } |
21 | |
71 | |
22 | sub wxwy { |
72 | sub init_worldmap { |
23 | $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} |
73 | # only for handlers |
24 | ? ($1, $2) |
|
|
25 | : (0, 0) |
|
|
26 | } |
74 | } |
27 | |
75 | |
28 | sub load_header_orig { |
76 | sub load_header_orig { |
29 | my ($self) = @_; |
77 | my ($self) = @_; |
30 | |
78 | |
31 | $self->width ($WORLD->{tilew}); |
79 | my ($x, $y, $z) = @$self{qw(x y z)}; |
32 | $self->height ($WORLD->{tileh}); |
|
|
33 | |
80 | |
34 | my ($x, $y) = $self->wxwy; |
81 | $self->width ($TILE_W); |
|
|
82 | $self->height ($TILE_H); |
35 | |
83 | |
36 | $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; |
84 | $self->name (sprintf "'The World' at %+d%+d%+d", $x, $y, $z); |
37 | $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; |
85 | $self->msg ("worldmap dynamically created by map-world extension"); |
38 | $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; |
86 | $self->outdoor ($self->{z} >= 0); |
39 | $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; |
87 | $self->default_region (cf::region::find "wilderness"); |
40 | |
88 | |
41 | $self->outdoor (1); |
89 | # all possible worldmap tiles also exist |
|
|
90 | $self->tile_path (0, format_xyz $x, $y - 1, $z); |
|
|
91 | $self->tile_path (1, format_xyz $x + 1, $y, $z); |
|
|
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); |
42 | |
96 | |
43 | $self->msg ("worldmap dynamically created by map-world extension"); |
97 | # an overlay always wins |
|
|
98 | my $overlay = sprintf "%s/%s.map", $cf::MAPDIR, format_xyz $x, $y, $z; |
44 | |
99 | |
45 | #TODO: region |
100 | $self->{load_path} = $overlay |
46 | |
101 | unless Coro::AIO::aio_stat $overlay; |
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 | |
102 | |
|
|
103 | $self->{need_create_treasure} = 1; |
51 | |
104 | |
52 | 1 |
105 | 1 |
53 | } |
106 | } |
54 | |
107 | |
55 | sub load { |
108 | # fill map with "default" data - first region and first archetype from palette |
|
|
109 | # used when we have no map data from elsewhere |
|
|
110 | sub fill { |
56 | my ($self) = @_; |
111 | my ($self) = @_; |
57 | |
112 | |
58 | if ($self->{load_path}) { |
113 | if ($self->{z} > 0) { |
59 | $self->SUPER::load; |
114 | # hmmm... air? :) |
|
|
115 | $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]); |
|
|
116 | } elsif ($self->{z} < 0) { |
|
|
117 | $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]); |
60 | } else { |
118 | } else { |
61 | $self->alloc; |
119 | $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["deep_sea"]); |
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 | } |
120 | } |
74 | } |
121 | } |
75 | |
122 | |
|
|
123 | #sub load { |
|
|
124 | # my ($self) = @_; |
|
|
125 | # |
|
|
126 | # if ($self->{load_path}) { |
|
|
127 | # warn "load $self\n";#d# |
|
|
128 | # $self->SUPER::load; |
|
|
129 | # } else { |
|
|
130 | # warn "fill $self\n";#d# |
|
|
131 | # my $guard = cf::lock_acquire "map_data:$self->{path}"; |
|
|
132 | # |
|
|
133 | # $self->alloc; |
|
|
134 | # $self->fill; |
|
|
135 | # $self->in_memory (cf::MAP_ACTIVE); |
|
|
136 | # $self->activate; |
|
|
137 | # $self->post_load; |
|
|
138 | # } |
|
|
139 | #} |
|
|
140 | |
|
|
141 | # MUST be overwritten to call at least ->fill |
76 | sub post_load { |
142 | sub post_load { |
77 | my ($self) = @_; |
143 | my ($self) = @_; |
78 | |
144 | |
79 | my ($x, $y) = $self->wxwy; |
145 | $self->create_region_treasure |
80 | |
146 | if delete $self->{need_create_treasure}; |
81 | my $stride = ($WORLD->{maxx} - $WORLD->{minx} + 1) * $WORLD->{tilew}; |
|
|
82 | my $top = ($y - $WORLD->{miny}) * $WORLD->{tileh} * $stride |
|
|
83 | + ($x - $WORLD->{minx}) * $WORLD->{tilew}; |
|
|
84 | |
|
|
85 | for my $Y (0 .. $WORLD->{tileh} - 1) { |
|
|
86 | Coro::cede; |
|
|
87 | my $row = substr $WORLD->{data}, $top + $Y * $stride, $WORLD->{tilew}; |
|
|
88 | for my $X (0 .. $WORLD->{tilew} - 1) { |
|
|
89 | next if grep $_->flag (cf::FLAG_IS_FLOOR), $self->at ($X, $Y); |
|
|
90 | my $ob = cf::object::new $WORLD->{arch}[ord substr $row, $X]; |
|
|
91 | $ob->flag (cf::FLAG_NO_MAP_SAVE, 1); |
|
|
92 | $self->insert ($ob, $X, $Y, undef, cf::INS_ABOVE_FLOOR_ONLY); |
|
|
93 | } |
|
|
94 | } |
|
|
95 | } |
147 | } |
96 | |
148 | |
97 | 1 |
|
|
98 | |
|
|