ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/map-world.ext
Revision: 1.36
Committed: Sun Dec 28 06:59:26 2008 UTC (15 years, 5 months ago) by root
Branch: MAIN
CVS Tags: rel-2_82, rel-2_81, rel-2_80, rel-2_76, rel-2_77, rel-2_74, rel-2_75, rel-2_79, rel-2_90, rel-2_92, rel-2_93, rel-2_78
Changes since 1.35: +1 -1 lines
Log Message:
smell, remove gcfclient support, material fixes, cfpod parse fixes, refactoring

File Contents

# Content
1 #! perl # optional
2
3 # optional plug-in to speed up worldmap rendering by dynamically
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
9 cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100);
10
11 use Coro::Handle;
12 use Coro::AIO;
13
14 our $WORLD;
15
16 sub load_indexed($$) {
17 my ($path, $size) = @_;
18
19 use bytes;
20
21 0 < aio_load "$path.plt", my $plt
22 or cf::cleanup "$path.plt: $!";
23
24 my %plt;
25 my @plt;
26
27 for (split /\n/, $plt) {
28 my ($name, $rgb) = split /\s+/;
29 if (/^(\S+)\s+(\S+)$/) {
30 my ($name, $rgb) = ($1, $2);
31 $rgb = join "", map chr, map $_ * 17, map hex, split //, $rgb;
32 $plt{$rgb} = chr @plt;
33 push @plt, $name;
34 }
35 }
36
37 my $data = cf::fork_call {
38 open my $fh, "convert \Q$path.png\E -depth 8 rgb:- |"
39 or die "convert: $!";
40 binmode $fh;
41
42 $size * 3 == read $fh, my $data, $size * 3
43 or die "$path.png, expected $size rgb triplets: $!";
44
45 cf::_quantise $data, [map "$_$plt{$_}", keys %plt];
46
47 $data
48 };
49
50 $size == length $data
51 or cf::cleanup "$path.png, expected $size index octets ($!)";
52
53 ($data, \@plt)
54 }
55
56 sub load_gridmap($) {
57 my ($path) = @_;
58
59 my $map = cf::cache "ext::map-world/gridmap" =>
60 [
61 "$path/gridmap.meta",
62 "$path/gridmap.arch.plt",
63 "$path/gridmap.arch.png",
64 "$path/gridmap.regn.plt",
65 "$path/gridmap.regn.png",
66 ],
67 1 => sub {
68 my ($src) = @_;
69
70 my $map = cf::decode_json $src->[0];
71
72 my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h}
73 or cf::cleanup "$path/gridmap.meta: empty gridmap?";
74
75 ($map->{arc_data}, $map->{arc_plt}) = load_indexed "$path/gridmap.arch", $size;
76 ($map->{reg_data}, $map->{reg_plt}) = load_indexed "$path/gridmap.regn", $size;
77
78 Coro::Storable::freeze $map
79 };
80
81 Coro::Storable::thaw $map
82 }
83
84 sub reload() {
85 $WORLD = load_gridmap "$MAPDIR/world";
86 warn "worldmap gridmap loaded.";
87 }
88
89 {
90 my $guard = cf::lock_acquire "ext::world_gridmap";
91
92 cf::post_init {
93 cf::async_ext {
94 $Coro::current->{desc} = "worldmap loader";
95 reload;
96 undef $guard;
97 };
98 };
99 }
100
101 sub wxwy {
102 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$}
103 ? ($1, $2)
104 : (0, 0)
105 }
106
107 sub load_header_orig {
108 my ($self) = @_;
109
110 my ($x, $y) = $self->wxwy;
111
112 my $guard = cf::lock_acquire "ext::world_gridmap";
113
114 $self->width ($WORLD->{tile_w});
115 $self->height ($WORLD->{tile_h});
116
117 $self->name ("'The World' at +$x+$y");
118 $self->msg ("worldmap dynamically created by map-world extension");
119 $self->outdoor (1);
120 $self->default_region (cf::region::find "wilderness");
121
122 $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0;
123 $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999;
124 $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999;
125 $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0;
126
127 my $overlay = sprintf "%s/world/world_%03d_%03d.map", $cf::MAPDIR, $x, $y;
128
129 $self->{load_path} = $overlay
130 unless Coro::AIO::aio_stat $overlay;
131
132 $self->{need_create_treasure} = 1;
133
134 1
135 }
136
137 sub fill {
138 my ($self) = @_;
139
140 $self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt});#d#
141 $self->default_region (cf::region::find $WORLD->{reg_plt}[0]);
142 }
143
144 sub load {
145 my ($self) = @_;
146
147 if ($self->{load_path}) {
148 $self->SUPER::load;
149 } else {
150 $self->alloc;
151 $self->fill;
152 $self->in_memory (cf::MAP_ACTIVE);
153 $self->activate;
154 }
155 }
156
157 sub post_load {
158 my ($self) = @_;
159
160 {
161 my $guard = cf::lock_acquire "ext::world_gridmap";
162
163 my ($x, $y) = $self->wxwy;
164
165 if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) {
166 my $stride = $WORLD->{grid_w} * $WORLD->{tile_w};
167 my $top = ($y - 100) * $WORLD->{tile_h} * $stride
168 + ($x - 100) * $WORLD->{tile_w};
169
170 $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt});
171 $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt});
172
173 } else {
174 $self->fill;
175 }
176 }
177
178 $self->create_region_treasure
179 if delete $self->{need_create_treasure};
180 }
181
182 1
183