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.10 by root, Sat Feb 10 01:52:27 2007 UTC vs.
Revision 1.46 by root, Sun May 8 21:51:26 2011 UTC

1#! perl # OPTIONAL 1#! perl # mandatory
2 2
3# optional plug-in to speed up worldmap rendering by dynamically 3# manages the worldmap by directing to other handlers
4# generating it out of an image
5# - saves loading time (less data to read)
6# - saves temporary space (only overlay stuff needs to be saved)
7# - might get reused as a generic tiled map
8 4
9use Coro::Handle; 5cf::map->register (qr{^/world/world_(-?\d+)_(-?\d+)(?:([+\-]\d+))?$}, 100);
10use Coro::AIO;
11 6
12our $WORLD; 7# universal
8our $TILE_W = 50;
9our $TILE_H = 50;
13 10
14sub load_indexed($$) { 11# registry
15 my ($path, $size) = @_; 12our @AREAS;
16 13
17 use bytes; 14sub register($$$$$$$@) {
15 my $meta = {
16 pkg => shift,
17 x0 => shift, y0 => shift, z0 => shift,
18 x1 => shift, y1 => shift, z1 => shift,
19 @_
20 };
18 21
19 0 < aio_load "$path.plt", my $plt 22 my $pkg = $meta->{pkg};
20 or cf::cleanup "$path.plt: $!";
21 23
22 my %plt; 24 @AREAS = sort { $b->{pri} <=> $a->{pri} }
23 my @plt; 25 $meta,
26 grep $pkg ne $_->{pkg},
27 @AREAS;
24 28
25 for (split /\n/, $plt) { 29 my $isa = \@{"$pkg\::ISA"};
26 my ($name, $rgb) = split /\s+/; 30
27 if (/^(\S+)\s+(\S+)$/) { 31 @$isa = (
28 my ($name, $rgb) = ($1, $2); 32 __PACKAGE__,
29 $rgb = join "", map chr, map $_ * 17, map hex, split //, $rgb; 33 grep __PACKAGE__ ne $_, @$isa
30 $plt{$rgb} = chr @plt; 34 );
31 push @plt, $name; 35}
36
37sub format_xyz($$;$) {
38 sprintf "/world/world_%03d_%03d%s",
39 $_[0], $_[1],
40 $_[2] ? sprintf "%+04d", $_[2] : "";
41}
42
43#############################################################################
44
45sub init {
46 my ($self) = @_;
47
48 my ($x, $y, $z) = @$self{qw(x y z)} = ($1, $2, $3+0);
49
50 # find handler for this area
51 for my $meta (@AREAS) {
52 if ( $meta->{x0} <= $x && $x <= $meta->{x1}
53 && $meta->{y0} <= $y && $y <= $meta->{y1}
54 && $meta->{z0} <= $z && $z <= $meta->{z1}
55 ) {
56 $self->{ox} = $x - $meta->{x0};
57 $self->{oy} = $y - $meta->{y0};
58 $self->{oz} = $z - $meta->{z0};
59
60 bless $self, $meta->{pkg};
61
62 $self->init_worldmap ($meta);
63
64 goto done;
32 } 65 }
33 } 66 }
34 67
35 my $pid = open my $pipe, "-|"; 68 bless $self, ext::map_world::empty::;
36 69
37 defined $pid 70done:
38 or cf::cleanup "fork: $!"; 71 $self->SUPER::init;
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 binmode $fh;
47 $size * 3 == read $fh, my $data, $size * 3
48 or die "$path.png, expected $size rgb triplets: $!";
49
50 $data =~ s{(...)}{
51 $plt{$1} or die "$path.png: color not in palette: #" . unpack "H*", $1
52 }sge;
53
54 binmode STDOUT;
55 syswrite STDOUT, $data;
56 };
57 warn $@ if $@;
58 cf::_exit;
59 }
60
61 $pipe = Coro::Handle::unblock $pipe;
62 binmode $pipe;
63 $size == read $pipe, my $data, $size
64 or cf::cleanup "$path.png, expected $size index octets ($!)";
65
66 ($data, \@plt)
67} 72}
68 73
69sub load_gridmap($) { 74sub init_worldmap {
70 my ($path) = @_; 75 # only for handlers
71
72 0 < aio_load "$path/gridmap.meta", my $map
73 or cf::cleanup "$path/gridmap.meta: $!\n";
74
75 $map = cf::from_json $map;
76
77 my $size = $map->{tile_w} * $map->{tile_h} * $map->{grid_w} * $map->{grid_h}
78 or cf::cleanup "$path/gridmap.meta: empty gridmap?";
79
80 ($map->{arc_data}, $map->{arc_plt}) = load_indexed "$path/gridmap.arch", $size;
81 ($map->{reg_data}, $map->{reg_plt}) = load_indexed "$path/gridmap.regn", $size;
82
83 $map
84}
85
86# this is contorted, but likely the correct way to acquire the lock :)
87cf::sync_job {
88 my $guard = cf::lock_acquire "ext::world_gridmap";
89 cf::async_ext {
90 $WORLD = load_gridmap sprintf "%s/%s/%s", cf::datadir, cf::mapdir, "world";
91 warn "worldmap gridmap loaded.";
92 undef $guard;
93 }
94};
95
96cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100);
97
98sub wxwy {
99 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$}
100 ? ($1, $2)
101 : (0, 0)
102} 76}
103 77
104sub load_header_orig { 78sub load_header_orig {
105 my ($self) = @_; 79 my ($self) = @_;
106 80
107 my ($x, $y) = $self->wxwy; 81 my ($x, $y, $z) = @$self{qw(x y z)};
108 82
109 my $guard = cf::lock_acquire "ext::world_gridmap"; 83 $self->width ($TILE_W);
84 $self->height ($TILE_H);
110 85
111 $self->width ($WORLD->{tile_w}); 86 $self->name (sprintf "'The World' at %+d%+d%+d", $x, $y, $z);
112 $self->height ($WORLD->{tile_h}); 87 $self->msg ("worldmap dynamically created by map-world extension");
88 $self->outdoor ($self->{z} >= 0);
89 $self->default_region (cf::region::find "wilderness");
113 90
114 $self->name ("'The World' at +$x+$y"); 91 # all possible worldmap tiles also exist
115 $self->msg ("worldmap dynamically created by map-world extension"); 92 $self->tile_path (cf::TILE_NORTH, format_xyz $x, $y - 1, $z);
116 $self->outdoor (1); 93 $self->tile_path (cf::TILE_SOUTH, format_xyz $x, $y + 1, $z);
117 $self->default_region (undef); 94 $self->tile_path (cf::TILE_EAST , format_xyz $x + 1, $y, $z);
95 $self->tile_path (cf::TILE_WEST , format_xyz $x - 1, $y, $z);
96 $self->tile_path (cf::TILE_UP , format_xyz $x, $y, $z + 1);
97 $self->tile_path (cf::TILE_DOWN , format_xyz $x, $y, $z - 1);
118 98
119# $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; 99 # an overlay always wins
120# $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; 100 my $overlay = sprintf "%s/%s.map", $cf::MAPDIR, format_xyz $x, $y, $z;
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 $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 100;
124 $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 129;
125 $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 129;
126 $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 100;
127 101
128 $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d", cf::datadir, cf::mapdir, $x, $y 102 $self->{load_path} = $overlay
129 if $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129; 103 unless Coro::AIO::aio_stat $overlay;
104
105 $self->{need_create_treasure} = 1;
130 106
131 1 107 1
132} 108}
133 109
134sub load { 110# fill map with "default" data - first region and first archetype from palette
111# used when we have no map data from elsewhere
112sub fill {
135 my ($self) = @_; 113 my ($self) = @_;
136 114
137 if ($self->{load_path}) { 115 if ($self->{z} > 0) {
138 $self->SUPER::load; 116 # hmmm... air? :)
117 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]);
118 } elsif ($self->{z} < 0) {
119 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]);
139 } else { 120 } else {
140 $self->alloc; 121 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["deep_sea"]);
141
142 for my $X (0 .. $WORLD->{tile_w} - 1) {
143 Coro::cede;
144 for my $Y (0 .. $WORLD->{tile_h} - 1) {
145 my $ob = cf::object::new "deep_sea";
146 $self->insert ($ob, $X, $Y);
147 }
148 }
149 $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1);
150 $self->default_region (cf::region::find "ocean");
151
152 $self->in_memory (cf::MAP_IN_MEMORY);
153 } 122 }
154} 123}
155 124
125#sub load {
126# my ($self) = @_;
127#
128# if ($self->{load_path}) {
129# warn "load $self\n";#d#
130# $self->SUPER::load;
131# } else {
132# warn "fill $self\n";#d#
133# my $guard = cf::lock_acquire "map_data:$self->{path}";
134#
135# $self->alloc;
136# $self->fill;
137# $self->state (cf::MAP_ACTIVE);
138# $self->activate;
139# $self->post_load;
140# }
141#}
142
143# MUST be overwritten to call at least ->fill
156sub post_load { 144sub post_load {
157 my ($self) = @_; 145 my ($self) = @_;
158 146
159 my $guard = cf::lock_acquire "ext::world_gridmap"; 147 $self->create_region_treasure
160 148 if delete $self->{need_create_treasure};
161 my ($x, $y) = $self->wxwy;
162
163 return
164 unless $x >= 100 && $x <= 129 && $y >= 100 && $y <= 129;
165
166 my $stride = $WORLD->{grid_w} * $WORLD->{tile_w};
167 my $top = ($y - 100) * $WORLD->{tile_h} * $stride
168 + ($x - 100) * $WORLD->{tile_w};
169
170 my $reg;
171
172 for my $Y (0 .. $WORLD->{tile_h} - 1) {
173 Coro::cede;
174 my $row = substr $WORLD->{arc_data}, $top + $Y * $stride, $WORLD->{tile_w};
175 $reg .= substr $WORLD->{reg_data}, $top + $Y * $stride, $WORLD->{tile_w};
176 for my $X (0 .. $WORLD->{tile_w} - 1) {
177 next if grep $_->flag (cf::FLAG_IS_FLOOR), $self->at ($X, $Y);
178 my $ob = cf::object::new $WORLD->{arc_plt}[ord substr $row, $X];
179 $ob->flag (cf::FLAG_NO_MAP_SAVE, 1);
180 $self->insert ($ob, $X, $Y, undef, cf::INS_ABOVE_FLOOR_ONLY);
181 }
182 }
183
184 $self->set_regiondata ($reg, $WORLD->{reg_plt});
185} 149}
186 150
1871 151package ext::map_world::empty;
188 152
153our @ISA = ext::map_world::;
154
155sub post_load_original {
156 $_[0]->fill;
157}
158

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines