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.40 by root, Fri Apr 22 02:03:11 2011 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
9cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); 5cf::map->register (qr{^/world/world_(-?\d+)_(-?\d+)(?:([+\-]\d+))?$}, 100);
10 6
11use Coro::Handle; 7# universal
12use Coro::AIO; 8our $TILE_W = 50;
9our $TILE_H = 50;
13 10
14my $TILE_W = 50; 11# registry
15my $TILE_H = 50; 12our @AREAS;
16 13
17my $GRID_W = 30; 14sub register($$$$$$$@) {
18my $GRID_H = 30; 15 my $meta = {
16 pkg => shift,
17 x0 => shift, y0 => shift, z0 => shift,
18 x1 => shift, y1 => shift, z1 => shift,
19 @_
20 };
19 21
20our $ARCH; 22 my $pkg = $meta->{pkg};
21our $REGN;
22 23
23sub reload() { 24 @AREAS = sort { $b->{pri} <=> $a->{pri} }
24 cf::trace "loading world+100+100 table.\n"; 25 $meta,
25 $ARCH = cf::decode_storable cf::unlzf cf::load_file "$DATADIR/world+100+100.arch"; 26 grep $pkg ne $_->{pkg},
26 $REGN = cf::decode_storable cf::unlzf cf::load_file "$DATADIR/world+100+100.regn"; 27 @AREAS;
27 cf::trace "loaded world+100+100 table.\n"; 28
29 my $isa = \@{"$pkg\::ISA"};
30
31 @$isa = (
32 __PACKAGE__,
33 grep __PACKAGE__ ne $_, @$isa
34 );
28} 35}
29 36
30{ 37sub format_xyz($$;$) {
31 my $guard = cf::lock_acquire "ext::world_gridmap"; 38 sprintf "/world/world_%03d_%03d%s",
32 39 $_[0], $_[1],
33 cf::post_init { 40 $_[2] ? sprintf "%+04d", $_[2] : "";
34 cf::async_ext {
35 $Coro::current->{desc} = "worldmap loader";
36 reload;
37 undef $guard;
38 };
39 };
40} 41}
41 42
42sub wxwy { 43#############################################################################
43 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} 44
44 ? ($1, $2) 45sub init {
45 : (0, 0) 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;
65 }
66 }
67
68 bless $self, ext::map_world::empty::;
69
70done:
71 $self->SUPER::init;
72}
73
74sub init_worldmap {
75 # only for handlers
46} 76}
47 77
48sub load_header_orig { 78sub load_header_orig {
49 my ($self) = @_; 79 my ($self) = @_;
50 80
51 my ($x, $y) = $self->wxwy; 81 my ($x, $y, $z) = @$self{qw(x y z)};
52
53 my $guard = cf::lock_acquire "ext::world_gridmap";
54 82
55 $self->width ($TILE_W); 83 $self->width ($TILE_W);
56 $self->height ($TILE_H); 84 $self->height ($TILE_H);
57 85
58 $self->name ("'The World' at +$x+$y"); 86 $self->name (sprintf "'The World' at %+d%+d%+d", $x, $y, $z);
59 $self->msg ("worldmap dynamically created by map-world extension"); 87 $self->msg ("worldmap dynamically created by map-world extension");
60 $self->outdoor (1); 88 $self->outdoor ($self->{z} >= 0);
61 $self->default_region (cf::region::find "wilderness"); 89 $self->default_region (cf::region::find "wilderness");
62 90
63 $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; 91 # all possible worldmap tiles also exist
64 $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; 92 $self->tile_path (cf::TILE_NORTH, format_xyz $x, $y - 1, $z);
65 $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; 93 $self->tile_path (cf::TILE_SOUTH, format_xyz $x, $y + 1, $z);
66 $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; 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);
67 98
99 # an overlay always wins
68 my $overlay = sprintf "%s/world/world_%03d_%03d.map", $cf::MAPDIR, $x, $y; 100 my $overlay = sprintf "%s/%s.map", $cf::MAPDIR, format_xyz $x, $y, $z;
69 101
70 $self->{load_path} = $overlay 102 $self->{load_path} = $overlay
71 unless Coro::AIO::aio_stat $overlay; 103 unless Coro::AIO::aio_stat $overlay;
72 104
73 $self->{need_create_treasure} = 1; 105 $self->{need_create_treasure} = 1;
74 106
75 1 107 1
76} 108}
77 109
110# fill map with "default" data - first region and first archetype from palette
111# used when we have no map data from elsewhere
78sub fill { 112sub fill {
79 my ($self) = @_; 113 my ($self) = @_;
80 114
115 if ($self->{z} > 0) {
116 # hmmm... air? :)
81 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, $ARCH->{plt}); 117 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]);
82 $self->default_region (cf::region::find $REGN->{plt}[0]); 118 } elsif ($self->{z} < 0) {
83} 119 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]);
84
85sub load {
86 my ($self) = @_;
87
88 if ($self->{load_path}) {
89 $self->SUPER::load;
90 } else { 120 } else {
91 $self->alloc; 121 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["deep_sea"]);
92 $self->fill;
93 $self->in_memory (cf::MAP_ACTIVE);
94 $self->activate;
95 } 122 }
96} 123}
97 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
98sub post_load { 144sub post_load {
99 my ($self) = @_; 145 my ($self) = @_;
100
101 {
102 my $guard = cf::lock_acquire "ext::world_gridmap";
103
104 my ($x, $y) = $self->wxwy;
105
106 if ($x >= 100 && $x <= 129 && $y >= 100 && $y <= 129) {
107 my $stride = $GRID_W * $TILE_W;
108 my $top = ($y - 100) * $TILE_H * $stride
109 + ($x - 100) * $TILE_W;
110
111 $self->add_underlay ($ARCH->{tbl}, $top, $stride, $ARCH->{plt});
112 $self->set_regiondata ($REGN->{tbl}, $top, $stride, $REGN->{plt});
113
114 } else {
115 $self->fill;
116 }
117 }
118 146
119 $self->create_region_treasure 147 $self->create_region_treasure
120 if delete $self->{need_create_treasure}; 148 if delete $self->{need_create_treasure};
121} 149}
122 150
151package ext::map_world::empty;
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