ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/map-world.ext
Revision: 1.30
Committed: Tue Sep 11 13:27:54 2007 UTC (16 years, 8 months ago) by root
Branch: MAIN
CVS Tags: rel-2_2
Changes since 1.29: +2 -2 lines
Log Message:
progress

File Contents

# User Rev Content
1 root 1.27 #! perl # optional
2 root 1.1
3     # optional plug-in to speed up worldmap rendering by dynamically
4 root 1.8 # generating it out of an image
5 root 1.1 # - saves loading time (less data to read)
6     # - saves temporary space (only overlay stuff needs to be saved)
7 root 1.8 # - might get reused as a generic tiled map
8 root 1.1
9 root 1.30 cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100);
10    
11 root 1.8 use Coro::Handle;
12     use Coro::AIO;
13 root 1.1
14 root 1.8 our $WORLD;
15 root 1.1
16 root 1.8 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 root 1.22 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 root 1.8
42 root 1.22 $size * 3 == read $fh, my $data, $size * 3
43     or die "$path.png, expected $size rgb triplets: $!";
44 root 1.8
45 root 1.22 cf::_quantise $data, [map "$_$plt{$_}", keys %plt];
46 root 1.8
47 root 1.22 $data
48     };
49 root 1.8
50 root 1.22 $size == length $data
51 root 1.8 or cf::cleanup "$path.png, expected $size index octets ($!)";
52    
53     ($data, \@plt)
54     }
55    
56     sub load_gridmap($) {
57     my ($path) = @_;
58    
59 root 1.22 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::from_json $src->[0];
71 root 1.8
72 root 1.22 my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h}
73     or cf::cleanup "$path/gridmap.meta: empty gridmap?";
74 root 1.8
75 root 1.22 ($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 root 1.1
78 root 1.22 Coro::Storable::freeze $map
79     };
80 root 1.1
81 root 1.22 Coro::Storable::thaw $map
82     }
83 root 1.11
84 root 1.22 sub reload() {
85 root 1.23 $WORLD = load_gridmap "$MAPDIR/world";
86 root 1.22 warn "worldmap gridmap loaded.";
87 root 1.1 }
88    
89 root 1.8 # this is contorted, but likely the correct way to acquire the lock :)
90     cf::sync_job {
91     my $guard = cf::lock_acquire "ext::world_gridmap";
92     cf::async_ext {
93 root 1.22 reload;
94 root 1.8 undef $guard;
95 root 1.23 };
96 root 1.8 };
97    
98 root 1.1 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 root 1.8 my ($x, $y) = $self->wxwy;
108 root 1.1
109 root 1.8 my $guard = cf::lock_acquire "ext::world_gridmap";
110 root 1.1
111 root 1.8 $self->width ($WORLD->{tile_w});
112     $self->height ($WORLD->{tile_h});
113 root 1.3
114 root 1.6 $self->name ("'The World' at +$x+$y");
115 root 1.1 $self->msg ("worldmap dynamically created by map-world extension");
116 root 1.8 $self->outdoor (1);
117 root 1.20 $self->default_region (cf::region::find "wilderness");
118 root 1.1
119 root 1.12 $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 root 1.7
124 root 1.29 my $overlay = sprintf "%s/world/world_%03d_%03d.map", $cf::MAPDIR, $x, $y;
125 root 1.28
126     $self->{load_path} = $overlay
127     unless Coro::AIO::aio_stat $overlay;
128 root 1.1
129 root 1.24 $self->{need_create_treasure} = 1;
130    
131 root 1.1 1
132     }
133    
134 root 1.13 sub fill {
135     my ($self) = @_;
136    
137 root 1.15 $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 root 1.13 }
140    
141 root 1.1 sub load {
142     my ($self) = @_;
143    
144     if ($self->{load_path}) {
145     $self->SUPER::load;
146     } else {
147 root 1.2 $self->alloc;
148 root 1.13 $self->fill;
149 root 1.2 $self->in_memory (cf::MAP_IN_MEMORY);
150 root 1.26 $self->activate;
151 root 1.1 }
152     }
153    
154 root 1.3 sub post_load {
155 root 1.1 my ($self) = @_;
156    
157 root 1.26 {
158     my $guard = cf::lock_acquire "ext::world_gridmap";
159 root 1.8
160 root 1.26 my ($x, $y) = $self->wxwy;
161 root 1.1
162 root 1.26 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 root 1.1 }
174 root 1.24
175 root 1.26 $self->create_region_treasure
176     if delete $self->{need_create_treasure};
177 root 1.1 }
178    
179     1
180