ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/map-world.ext
Revision: 1.29
Committed: Mon Sep 10 17:24:36 2007 UTC (16 years, 8 months ago) by root
Branch: MAIN
Changes since 1.28: +1 -1 lines
Log Message:
- rename world to world-precomposed (mostly), which is 100% optional
  for the server operation
- rename world-overlay to world (mostly), as to not lie about map paths
- disable the skip_block thingy again, its harmful
- better bdb support

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