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.21 by root, Tue Apr 17 20:41:15 2007 UTC vs.
Revision 1.40 by root, Fri Apr 22 02:03:11 2011 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 an 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# - might get reused as a generic tiled map
8 8
9cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100);
10
9use Coro::Handle; 11use Coro::Handle;
10use Coro::AIO; 12use Coro::AIO;
11 13
12our $WORLD; 14my $TILE_W = 50;
15my $TILE_H = 50;
13 16
14sub load_indexed($$) { 17my $GRID_W = 30;
15 my ($path, $size) = @_; 18my $GRID_H = 30;
16 19
17 use bytes; 20our $ARCH;
21our $REGN;
18 22
19 0 < aio_load "$path.plt", my $plt 23sub reload() {
20 or cf::cleanup "$path.plt: $!"; 24 cf::trace "loading world+100+100 table.\n";
21 25 $ARCH = cf::decode_storable cf::unlzf cf::load_file "$DATADIR/world+100+100.arch";
22 my %plt; 26 $REGN = cf::decode_storable cf::unlzf cf::load_file "$DATADIR/world+100+100.regn";
23 my @plt; 27 cf::trace "loaded world+100+100 table.\n";
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} 28}
65 29
66sub load_gridmap($) { 30{
67 my ($path) = @_; 31 my $guard = cf::lock_acquire "ext::world_gridmap";
68 32
69# if (! -e "/tmp/xxxx.gridmap") { 33 cf::post_init {
70 0 < aio_load "$path/gridmap.meta", my $map 34 cf::async_ext {
71 or cf::cleanup "$path/gridmap.meta: $!\n"; 35 $Coro::current->{desc} = "worldmap loader";
72 36 reload;
73 $map = cf::from_json $map; 37 undef $guard;
74 38 };
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# } 39 };
87} 40}
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};
98
99cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100);
100 41
101sub wxwy { 42sub wxwy {
102 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} 43 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$}
103 ? ($1, $2) 44 ? ($1, $2)
104 : (0, 0) 45 : (0, 0)
109 50
110 my ($x, $y) = $self->wxwy; 51 my ($x, $y) = $self->wxwy;
111 52
112 my $guard = cf::lock_acquire "ext::world_gridmap"; 53 my $guard = cf::lock_acquire "ext::world_gridmap";
113 54
114 $self->width ($WORLD->{tile_w}); 55 $self->width ($TILE_W);
115 $self->height ($WORLD->{tile_h}); 56 $self->height ($TILE_H);
116 57
117 $self->name ("'The World' at +$x+$y"); 58 $self->name ("'The World' at +$x+$y");
118 $self->msg ("worldmap dynamically created by map-world extension"); 59 $self->msg ("worldmap dynamically created by map-world extension");
119 $self->outdoor (1); 60 $self->outdoor (1);
120 $self->default_region (cf::region::find "wilderness"); 61 $self->default_region (cf::region::find "wilderness");
122 $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; 63 $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0;
123 $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; 64 $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999;
124 $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; 65 $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999;
125 $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; 66 $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0;
126 67
127 $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d.map", cf::datadir, cf::mapdir, $x, $y 68 my $overlay = sprintf "%s/world/world_%03d_%03d.map", $cf::MAPDIR, $x, $y;
128 if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; 69
70 $self->{load_path} = $overlay
71 unless Coro::AIO::aio_stat $overlay;
72
73 $self->{need_create_treasure} = 1;
129 74
130 1 75 1
131} 76}
132 77
133sub fill { 78sub fill {
134 my ($self) = @_; 79 my ($self) = @_;
135 80
136 $self->add_underlay ("\x00" x ($WORLD->{tile_w} * $WORLD->{tile_h}), 0, $WORLD->{tile_w}, $WORLD->{arc_plt}); 81 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, $ARCH->{plt});
137 $self->default_region (cf::region::find $WORLD->{reg_plt}[0]); 82 $self->default_region (cf::region::find $REGN->{plt}[0]);
138} 83}
139 84
140sub load { 85sub load {
141 my ($self) = @_; 86 my ($self) = @_;
142 87
143 if ($self->{load_path}) { 88 if ($self->{load_path}) {
144 $self->SUPER::load; 89 $self->SUPER::load;
145 } else { 90 } else {
146 $self->alloc; 91 $self->alloc;
147 $self->fill; 92 $self->fill;
148 $self->in_memory (cf::MAP_IN_MEMORY); 93 $self->in_memory (cf::MAP_ACTIVE);
94 $self->activate;
149 } 95 }
150} 96}
151 97
152sub post_load { 98sub post_load {
153 my ($self) = @_; 99 my ($self) = @_;
154 100
101 {
155 my $guard = cf::lock_acquire "ext::world_gridmap"; 102 my $guard = cf::lock_acquire "ext::world_gridmap";
156 103
157 my ($x, $y) = $self->wxwy; 104 my ($x, $y) = $self->wxwy;
158 105
159 if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { 106 if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) {
160 my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; 107 my $stride = $GRID_W * $TILE_W;
161 my $top = ($y - 100) * $WORLD->{tile_h} * $stride 108 my $top = ($y - 100) * $TILE_H * $stride
162 + ($x - 100) * $WORLD->{tile_w}; 109 + ($x - 100) * $TILE_W;
163 110
164 $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt}); 111 $self->add_underlay ($ARCH->{tbl}, $top, $stride, $ARCH->{plt});
165 $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt}); 112 $self->set_regiondata ($REGN->{tbl}, $top, $stride, $REGN->{plt});
166 113
167 } else { 114 } else {
168 $self->fill; 115 $self->fill;
116 }
169 } 117 }
118
119 $self->create_region_treasure
120 if delete $self->{need_create_treasure};
170} 121}
171 122
1721
173

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines