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.31 by root, Wed Sep 19 21:56:30 2007 UTC vs.
Revision 1.40 by root, Fri Apr 22 02:03:11 2011 UTC

9cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); 9cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100);
10 10
11use Coro::Handle; 11use Coro::Handle;
12use Coro::AIO; 12use Coro::AIO;
13 13
14our $WORLD; 14my $TILE_W = 50;
15my $TILE_H = 50;
15 16
16sub load_indexed($$) { 17my $GRID_W = 30;
17 my ($path, $size) = @_; 18my $GRID_H = 30;
18 19
19 use bytes; 20our $ARCH;
21our $REGN;
20 22
21 0 < aio_load "$path.plt", my $plt 23sub reload() {
22 or cf::cleanup "$path.plt: $!"; 24 cf::trace "loading world+100+100 table.\n";
23 25 $ARCH = cf::decode_storable cf::unlzf cf::load_file "$DATADIR/world+100+100.arch";
24 my %plt; 26 $REGN = cf::decode_storable cf::unlzf cf::load_file "$DATADIR/world+100+100.regn";
25 my @plt; 27 cf::trace "loaded world+100+100 table.\n";
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 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
42 $size * 3 == read $fh, my $data, $size * 3
43 or die "$path.png, expected $size rgb triplets: $!";
44
45 cf::_quantise $data, [map "$_$plt{$_}", keys %plt];
46
47 $data
48 };
49
50 $size == length $data
51 or cf::cleanup "$path.png, expected $size index octets ($!)";
52
53 ($data, \@plt)
54} 28}
55 29
56sub load_gridmap($) { 30{
57 my ($path) = @_; 31 my $guard = cf::lock_acquire "ext::world_gridmap";
58 32
59 my $map = cf::cache "ext::map-world/gridmap" => 33 cf::post_init {
60 [ 34 cf::async_ext {
61 "$path/gridmap.meta", 35 $Coro::current->{desc} = "worldmap loader";
62 "$path/gridmap.arch.plt", 36 reload;
63 "$path/gridmap.arch.png", 37 undef $guard;
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
72 my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h}
73 or cf::cleanup "$path/gridmap.meta: empty gridmap?";
74
75 ($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
78 Coro::Storable::freeze $map
79 }; 38 };
80 39 };
81 Coro::Storable::thaw $map
82} 40}
83
84sub reload() {
85 $WORLD = load_gridmap "$MAPDIR/world";
86 warn "worldmap gridmap loaded.";
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 $Coro::current->{desc} = "worldmap loader";
94 reload;
95 undef $guard;
96 };
97};
98 41
99sub wxwy { 42sub wxwy {
100 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} 43 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$}
101 ? ($1, $2) 44 ? ($1, $2)
102 : (0, 0) 45 : (0, 0)
107 50
108 my ($x, $y) = $self->wxwy; 51 my ($x, $y) = $self->wxwy;
109 52
110 my $guard = cf::lock_acquire "ext::world_gridmap"; 53 my $guard = cf::lock_acquire "ext::world_gridmap";
111 54
112 $self->width ($WORLD->{tile_w}); 55 $self->width ($TILE_W);
113 $self->height ($WORLD->{tile_h}); 56 $self->height ($TILE_H);
114 57
115 $self->name ("'The World' at +$x+$y"); 58 $self->name ("'The World' at +$x+$y");
116 $self->msg ("worldmap dynamically created by map-world extension"); 59 $self->msg ("worldmap dynamically created by map-world extension");
117 $self->outdoor (1); 60 $self->outdoor (1);
118 $self->default_region (cf::region::find "wilderness"); 61 $self->default_region (cf::region::find "wilderness");
133} 76}
134 77
135sub fill { 78sub fill {
136 my ($self) = @_; 79 my ($self) = @_;
137 80
138 $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});
139 $self->default_region (cf::region::find $WORLD->{reg_plt}[0]); 82 $self->default_region (cf::region::find $REGN->{plt}[0]);
140} 83}
141 84
142sub load { 85sub load {
143 my ($self) = @_; 86 my ($self) = @_;
144 87
145 if ($self->{load_path}) { 88 if ($self->{load_path}) {
146 $self->SUPER::load; 89 $self->SUPER::load;
147 } else { 90 } else {
148 $self->alloc; 91 $self->alloc;
149 $self->fill; 92 $self->fill;
150 $self->in_memory (cf::MAP_IN_MEMORY); 93 $self->in_memory (cf::MAP_ACTIVE);
151 $self->activate; 94 $self->activate;
152 } 95 }
153} 96}
154 97
155sub post_load { 98sub post_load {
159 my $guard = cf::lock_acquire "ext::world_gridmap"; 102 my $guard = cf::lock_acquire "ext::world_gridmap";
160 103
161 my ($x, $y) = $self->wxwy; 104 my ($x, $y) = $self->wxwy;
162 105
163 if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) { 106 if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) {
164 my $stride = $WORLD->{grid_w} * $WORLD->{tile_w}; 107 my $stride = $GRID_W * $TILE_W;
165 my $top = ($y - 100) * $WORLD->{tile_h} * $stride 108 my $top = ($y - 100) * $TILE_H * $stride
166 + ($x - 100) * $WORLD->{tile_w}; 109 + ($x - 100) * $TILE_W;
167 110
168 $self->add_underlay ($WORLD->{arc_data}, $top, $stride, $WORLD->{arc_plt}); 111 $self->add_underlay ($ARCH->{tbl}, $top, $stride, $ARCH->{plt});
169 $self->set_regiondata ($WORLD->{reg_data}, $top, $stride, $WORLD->{reg_plt}); 112 $self->set_regiondata ($REGN->{tbl}, $top, $stride, $REGN->{plt});
170 113
171 } else { 114 } else {
172 $self->fill; 115 $self->fill;
173 } 116 }
174 } 117 }
175 118
176 $self->create_region_treasure 119 $self->create_region_treasure
177 if delete $self->{need_create_treasure}; 120 if delete $self->{need_create_treasure};
178} 121}
179 122
1801
181

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines