ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/map-world.ext
(Generate patch)

Comparing deliantra/server/ext/map-world.ext (file contents):
Revision 1.6 by root, Wed Jan 24 03:13:21 2007 UTC vs.
Revision 1.22 by root, Wed Apr 18 14:24:09 2007 UTC

1#! perl # OPTIONAL 1#! perl # OPTIONAL
2 2
3# optional plug-in to speed up worldmap rendering by dynamically 3# optional plug-in to speed up worldmap rendering by dynamically
4# generating it out of a image 4# generating it out of an image
5# - saves loading time (less data to read) 5# - saves loading time (less data to read)
6# - saves temporary space (only overlay stuff needs to be saved) 6# - saves temporary space (only overlay stuff needs to be saved)
7# - might get reused as a generic tiled map
7 8
8our $WORLD = Storable::retrieve cf::datadir . "/world.pst" 9use Coro::Handle;
9 or die "unable to load world.pst"; 10use Coro::AIO;
10 11
11$WORLD->{version} == 1 12our $WORLD;
12 or die "world.pst version mismatch, expected version 1, got $WORLD->{version}"; 13
14sub 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
54sub 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
82sub reload() {
83 $WORLD = load_gridmap sprintf "%s/%s/%s", cf::datadir, cf::mapdir, "world";
84 warn "worldmap gridmap loaded.";
85}
86
87# this is contorted, but likely the correct way to acquire the lock :)
88cf::sync_job {
89 my $guard = cf::lock_acquire "ext::world_gridmap";
90 cf::async_ext {
91 reload;
92 undef $guard;
93 }
94};
13 95
14cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); 96cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100);
15
16sub init {
17 my ($self) = @_;
18
19 1
20}
21 97
22sub wxwy { 98sub wxwy {
23 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} 99 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$}
24 ? ($1, $2) 100 ? ($1, $2)
25 : (0, 0) 101 : (0, 0)
26} 102}
27 103
28sub load_header_orig { 104sub load_header_orig {
29 my ($self) = @_; 105 my ($self) = @_;
30 106
31 $self->width ($WORLD->{tilew}); 107 my ($x, $y) = $self->wxwy;
32 $self->height ($WORLD->{tileh});
33 108
34 my ($x, $y) = $self->wxwy; 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");
35 118
36 $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; 119 $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0;
37 $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; 120 $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999;
38 $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; 121 $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999;
39 $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; 122 $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0;
40 123
41 $self->outdoor (1);
42
43 $self->name ("'The World' at +$x+$y");
44 $self->msg ("worldmap dynamically created by map-world extension");
45
46 #TODO: region
47
48 $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d", cf::datadir, cf::mapdir, $x, $y 124 $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d.map", cf::datadir, cf::mapdir, $x, $y
49 if $x >= $WORLD->{minx} && $x <= $WORLD->{maxx} 125 if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129;
50 && $y >= $WORLD->{miny} && $y <= $WORLD->{maxy};
51
52 126
53 1 127 1
128}
129
130sub fill {
131 my ($self) = @_;
132
133 $self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt});
134 $self->default_region (cf::region::find $WORLD->{reg_plt}[0]);
54} 135}
55 136
56sub load { 137sub load {
57 my ($self) = @_; 138 my ($self) = @_;
58 139
59 if ($self->{load_path}) { 140 if ($self->{load_path}) {
60 $self->SUPER::load; 141 $self->SUPER::load;
61 } else { 142 } else {
62 $self->alloc; 143 $self->alloc;
63 144 $self->fill;
64 for my $X (0 .. $WORLD->{tilew} - 1) {
65 Coro::cede;
66 for my $Y (0 .. $WORLD->{tileh} - 1) {
67 my $ob = cf::object::new "deep_sea";
68 $self->insert ($ob, $X, $Y);
69 }
70 }
71 $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1);
72
73 $self->in_memory (cf::MAP_IN_MEMORY); 145 $self->in_memory (cf::MAP_IN_MEMORY);
74 } 146 }
75} 147}
76 148
77sub post_load { 149sub post_load {
78 my ($self) = @_; 150 my ($self) = @_;
79 151
152 my $guard = cf::lock_acquire "ext::world_gridmap";
153
80 my ($x, $y) = $self->wxwy; 154 my ($x, $y) = $self->wxwy;
81 155
82 return 156 if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) {
83 unless $x >= $WORLD->{minx} && $x <= $WORLD->{maxx} 157 my $stride = $WORLD->{grid_w} * $WORLD->{tile_w};
84 && $y >= $WORLD->{miny} && $y <= $WORLD->{maxy}; 158 my $top = ($y - 100) * $WORLD->{tile_h} * $stride
159 + ($x - 100) * $WORLD->{tile_w};
85 160
86 my $stride = ($WORLD->{maxx} - $WORLD->{minx} + 1) * $WORLD->{tilew}; 161 $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt});
87 my $top = ($y - $WORLD->{miny}) * $WORLD->{tileh} * $stride 162 $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt});
88 + ($x - $WORLD->{minx}) * $WORLD->{tilew};
89 163
90 for my $Y (0 .. $WORLD->{tileh} - 1) { 164 } else {
91 Coro::cede; 165 $self->fill;
92 my $row = substr $WORLD->{data}, $top + $Y * $stride, $WORLD->{tilew};
93 for my $X (0 .. $WORLD->{tilew} - 1) {
94 next if grep $_->flag (cf::FLAG_IS_FLOOR), $self->at ($X, $Y);
95 my $ob = cf::object::new $WORLD->{arch}[ord substr $row, $X];
96 $ob->flag (cf::FLAG_NO_MAP_SAVE, 1);
97 $self->insert ($ob, $X, $Y, undef, cf::INS_ABOVE_FLOOR_ONLY);
98 }
99 } 166 }
100} 167}
101 168
1021 1691
103 170

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines