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.3 by root, Sun Jan 21 13:08:36 2007 UTC vs.
Revision 1.21 by root, Tue Apr 17 20:41:15 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 $pid = open my $pipe, "-|";
36
37 defined $pid
38 or cf::cleanup "fork: $!";
39
40 unless ($pid) {
41 eval {
42 open my $fh, "convert \Q$path.png\E -depth 8 rgb:- |"
43 or die "convert: $!";
44 binmode $fh;
45
46 $size * 3 == read $fh, my $data, $size * 3
47 or die "$path.png, expected $size rgb triplets: $!";
48
49 cf::_quantise $data, [map "$_$plt{$_}", keys %plt];
50
51 binmode STDOUT;
52 syswrite STDOUT, $data;
53 };
54 warn $@ if $@;
55 cf::_exit;
56 }
57
58 $pipe = Coro::Handle::unblock $pipe;
59 binmode $pipe;
60 $size == read $pipe, my $data, $size
61 or cf::cleanup "$path.png, expected $size index octets ($!)";
62
63 ($data, \@plt)
64}
65
66sub load_gridmap($) {
67 my ($path) = @_;
68
69# if (! -e "/tmp/xxxx.gridmap") {
70 0 < aio_load "$path/gridmap.meta", my $map
71 or cf::cleanup "$path/gridmap.meta: $!\n";
72
73 $map = cf::from_json $map;
74
75 my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h}
76 or cf::cleanup "$path/gridmap.meta: empty gridmap?";
77
78 ($map->{arc_data}, $map->{arc_plt}) = load_indexed "$path/gridmap.arch", $size;
79 ($map->{reg_data}, $map->{reg_plt}) = load_indexed "$path/gridmap.regn", $size;
80
81# Storable::nstore $map, "/tmp/xxxx.gridmap" if 0;#d#
82
83 $map
84# } else {
85# Storable::retrieve "/tmp/xxxx.gridmap";
86# }
87}
88
89# this is contorted, but likely the correct way to acquire the lock :)
90cf::sync_job {
91 my $guard = cf::lock_acquire "ext::world_gridmap";
92 cf::async_ext {
93 $WORLD = load_gridmap sprintf "%s/%s/%s", cf::datadir, cf::mapdir, "world";
94 warn "worldmap gridmap loaded.";
95 undef $guard;
96 }
97};
13 98
14cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); 99cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100);
15
16sub init {
17 my ($self) = @_;
18
19 1
20}
21 100
22sub wxwy { 101sub wxwy {
23 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} 102 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$}
24 ? ($1, $2) 103 ? ($1, $2)
25 : (0, 0) 104 : (0, 0)
26} 105}
27 106
28sub load_header_orig { 107sub load_header_orig {
29 my ($self) = @_; 108 my ($self) = @_;
30 109
31 $self->width ($WORLD->{tilew}); 110 my ($x, $y) = $self->wxwy;
32 $self->height ($WORLD->{tileh});
33 111
34 my ($x, $y) = $self->wxwy; 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");
35 121
36 $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; 122 $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; 123 $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; 124 $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; 125 $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0;
40 126
41 $self->outdoor (1);
42
43 $self->msg ("worldmap dynamically created by map-world extension");
44
45 #TODO: region
46
47 $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d", cf::datadir, cf::mapdir, $x, $y 127 $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d.map", cf::datadir, cf::mapdir, $x, $y
48 if $x >= $WORLD->{minx} && $x <= $WORLD->{maxx} 128 if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129;
49 && $y >= $WORLD->{miny} && $y <= $WORLD->{maxy};
50
51 129
52 1 130 1
131}
132
133sub fill {
134 my ($self) = @_;
135
136 $self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt});
137 $self->default_region (cf::region::find $WORLD->{reg_plt}[0]);
53} 138}
54 139
55sub load { 140sub load {
56 my ($self) = @_; 141 my ($self) = @_;
57 142
58 if ($self->{load_path}) { 143 if ($self->{load_path}) {
59 $self->SUPER::load; 144 $self->SUPER::load;
60 } else { 145 } else {
61 $self->alloc; 146 $self->alloc;
62 147 $self->fill;
63 for my $X (0 .. $WORLD->{tilew} - 1) {
64 Coro::cede;
65 for my $Y (0 .. $WORLD->{tileh} - 1) {
66 my $ob = cf::object::new "deep_sea";
67 $self->insert ($ob, $X, $Y);
68 }
69 }
70 $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1);
71
72 $self->in_memory (cf::MAP_IN_MEMORY); 148 $self->in_memory (cf::MAP_IN_MEMORY);
73 } 149 }
74} 150}
75 151
76sub post_load { 152sub post_load {
77 my ($self) = @_; 153 my ($self) = @_;
78 154
155 my $guard = cf::lock_acquire "ext::world_gridmap";
156
79 my ($x, $y) = $self->wxwy; 157 my ($x, $y) = $self->wxwy;
80 158
81 my $stride = ($WORLD->{maxx} - $WORLD->{minx} + 1) * $WORLD->{tilew}; 159 if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) {
160 my $stride = $WORLD->{grid_w} * $WORLD->{tile_w};
82 my $top = ($y - $WORLD->{miny}) * $WORLD->{tileh} * $stride 161 my $top = ($y - 100) * $WORLD->{tile_h} * $stride
83 + ($x - $WORLD->{minx}) * $WORLD->{tilew}; 162 + ($x - 100) * $WORLD->{tile_w};
84 163
85 for my $Y (0 .. $WORLD->{tileh} - 1) { 164 $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt});
86 Coro::cede; 165 $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt});
87 my $row = substr $WORLD->{data}, $top + $Y * $stride, $WORLD->{tilew}; 166
88 for my $X (0 .. $WORLD->{tilew} - 1) { 167 } else {
89 next if grep $_->flag (cf::FLAG_IS_FLOOR), $self->at ($X, $Y); 168 $self->fill;
90 my $ob = cf::object::new $WORLD->{arch}[ord substr $row, $X];
91 $ob->flag (cf::FLAG_NO_MAP_SAVE, 1);
92 $self->insert ($ob, $X, $Y);
93 }
94 } 169 }
95} 170}
96 171
971 1721
98 173

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines