ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/map-world.ext
Revision: 1.27
Committed: Mon Jun 11 21:38:13 2007 UTC (16 years, 11 months ago) by root
Branch: MAIN
Changes since 1.26: +1 -1 lines
Log Message:
port micropather to c++...

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.8 use Coro::Handle;
10     use Coro::AIO;
11 root 1.1
12 root 1.8 our $WORLD;
13 root 1.1
14 root 1.8 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 root 1.22 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 root 1.8
40 root 1.22 $size * 3 == read $fh, my $data, $size * 3
41     or die "$path.png, expected $size rgb triplets: $!";
42 root 1.8
43 root 1.22 cf::_quantise $data, [map "$_$plt{$_}", keys %plt];
44 root 1.8
45 root 1.22 $data
46     };
47 root 1.8
48 root 1.22 $size == length $data
49 root 1.8 or cf::cleanup "$path.png, expected $size index octets ($!)";
50    
51     ($data, \@plt)
52     }
53    
54     sub load_gridmap($) {
55     my ($path) = @_;
56    
57 root 1.22 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 root 1.8
70 root 1.22 my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h}
71     or cf::cleanup "$path/gridmap.meta: empty gridmap?";
72 root 1.8
73 root 1.22 ($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 root 1.1
76 root 1.22 Coro::Storable::freeze $map
77     };
78 root 1.1
79 root 1.22 Coro::Storable::thaw $map
80     }
81 root 1.11
82 root 1.22 sub reload() {
83 root 1.23 $WORLD = load_gridmap "$MAPDIR/world";
84 root 1.22 warn "worldmap gridmap loaded.";
85 root 1.1 }
86    
87 root 1.8 # 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 root 1.22 reload;
92 root 1.8 undef $guard;
93 root 1.23 };
94 root 1.8 };
95    
96     cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100);
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.23 $self->{load_path} = sprintf "%s/world-overlay/world_%03d_%03d.map", $cf::MAPDIR, $x, $y
125 root 1.8 if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129;
126 root 1.1
127 root 1.24 $self->{need_create_treasure} = 1;
128    
129 root 1.1 1
130     }
131    
132 root 1.13 sub fill {
133     my ($self) = @_;
134    
135 root 1.15 $self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt});
136     $self->default_region (cf::region::find $WORLD->{reg_plt}[0]);
137 root 1.13 }
138    
139 root 1.1 sub load {
140     my ($self) = @_;
141    
142     if ($self->{load_path}) {
143     $self->SUPER::load;
144     } else {
145 root 1.2 $self->alloc;
146 root 1.13 $self->fill;
147 root 1.2 $self->in_memory (cf::MAP_IN_MEMORY);
148 root 1.26 $self->activate;
149 root 1.1 }
150     }
151    
152 root 1.3 sub post_load {
153 root 1.1 my ($self) = @_;
154    
155 root 1.26 {
156     my $guard = cf::lock_acquire "ext::world_gridmap";
157 root 1.8
158 root 1.26 my ($x, $y) = $self->wxwy;
159 root 1.1
160 root 1.26 if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) {
161     my $stride = $WORLD->{grid_w} * $WORLD->{tile_w};
162     my $top = ($y - 100) * $WORLD->{tile_h} * $stride
163     + ($x - 100) * $WORLD->{tile_w};
164    
165     $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt});
166     $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt});
167    
168     } else {
169     $self->fill;
170     }
171 root 1.1 }
172 root 1.24
173 root 1.26 $self->create_region_treasure
174     if delete $self->{need_create_treasure};
175 root 1.1 }
176    
177     1
178