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.2 by root, Sat Jan 20 23:39:08 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 a image
5# - saves loading time (less data to read)
6# - saves temporary space (only overlay stuff needs to be saved)
7 4
8our $WORLD = Storable::retrieve cf::datadir . "/world.pst" 5cf::map->register (qr{^/world/world_(-?\d+)_(-?\d+)(?:([+\-]\d+))?$}, 100);
9 or die "unable to load world.pst";
10 6
11$WORLD->{version} == 1 7# universal
12 or die "world.pst version mismatch, expected version 1, got $WORLD->{version}"; 8our $TILE_W = 50;
9our $TILE_H = 50;
13 10
14cf::map->register (qr{^/world/world_(\d\d\d)_(\d\d\d)$}, 100); 11# registry
12our @AREAS;
13
14sub register($$$$$$$@) {
15 my $meta = {
16 pkg => shift,
17 x0 => shift, y0 => shift, z0 => shift,
18 x1 => shift, y1 => shift, z1 => shift,
19 @_
20 };
21
22 my $pkg = $meta->{pkg};
23
24 @AREAS = sort { $b->{pri} <=> $a->{pri} }
25 $meta,
26 grep $pkg ne $_->{pkg},
27 @AREAS;
28
29 my $isa = \@{"$pkg\::ISA"};
30
31 @$isa = (
32 __PACKAGE__,
33 grep __PACKAGE__ ne $_, @$isa
34 );
35}
36
37sub format_xyz($$;$) {
38 sprintf "/world/world_%03d_%03d%s",
39 $_[0], $_[1],
40 $_[2] ? sprintf "%+04d", $_[2] : "";
41}
42
43#############################################################################
15 44
16sub init { 45sub init {
17 my ($self) = @_; 46 my ($self) = @_;
18 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 }
19 1 66 }
67
68 bless $self, ext::map_world::empty::;
69
70done:
71 $self->SUPER::init;
20} 72}
21 73
22sub wxwy { 74sub init_worldmap {
23 $_[0]->path =~ m{/world/world_(\d\d\d)_(\d\d\d)$} 75 # only for handlers
24 ? ($1, $2)
25 : (0, 0)
26} 76}
27 77
28sub load_header_orig { 78sub load_header_orig {
29 my ($self) = @_; 79 my ($self) = @_;
30 80
31 $self->width ($WORLD->{tilew}); 81 my ($x, $y, $z) = @$self{qw(x y z)};
32 $self->height ($WORLD->{tileh});
33 82
34 my ($x, $y) = $self->wxwy; 83 $self->width ($TILE_W);
84 $self->height ($TILE_H);
35 85
36 $self->tile_path (0, sprintf "/world/world_%03d_%03d", $x, $y - 1) if $y > 0; 86 $self->name (sprintf "'The World' at %+d%+d%+d", $x, $y, $z);
37 $self->tile_path (1, sprintf "/world/world_%03d_%03d", $x + 1, $y) if $x < 999; 87 $self->msg ("worldmap dynamically created by map-world extension");
38 $self->tile_path (2, sprintf "/world/world_%03d_%03d", $x, $y + 1) if $y < 999; 88 $self->outdoor ($self->{z} >= 0);
39 $self->tile_path (3, sprintf "/world/world_%03d_%03d", $x - 1, $y) if $x > 0; 89 $self->default_region (cf::region::find "wilderness");
40 90
41 $self->msg ("worldmap dynamically created by map-world extension"); 91 # all possible worldmap tiles also exist
92 $self->tile_path (cf::TILE_NORTH, format_xyz $x, $y - 1, $z);
93 $self->tile_path (cf::TILE_SOUTH, format_xyz $x, $y + 1, $z);
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);
42 98
43 #TODO: region 99 # an overlay always wins
44 100 my $overlay = sprintf "%s/%s.map", $cf::MAPDIR, format_xyz $x, $y, $z;
45 $self->{load_path} = sprintf "%s/%s/world-overlay/world_%03d_%03d", cf::datadir, cf::mapdir, $x, $y
46 if $x >= $WORLD->{minx} && $x <= $WORLD->{maxx}
47 && $y >= $WORLD->{miny} && $y <= $WORLD->{maxy};
48 101
102 $self->{load_path} = $overlay
103 unless Coro::AIO::aio_stat $overlay;
104
105 $self->{need_create_treasure} = 1;
49 106
50 1 107 1
51} 108}
52 109
53sub 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 {
54 my ($self) = @_; 113 my ($self) = @_;
55 114
56 if ($self->{load_path}) { 115 if ($self->{z} > 0) {
57 $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"]);
58 } else { 120 } else {
59 $self->alloc; 121 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["deep_sea"]);
60
61 for my $X (0 .. $WORLD->{tilew} - 1) {
62 Coro::cede;
63 for my $Y (0 .. $WORLD->{tileh} - 1) {
64 my $ob = cf::object::new "deep_sea";
65 $self->insert ($ob, $X, $Y);
66 }
67 }
68 $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1);
69
70 $self->in_memory (cf::MAP_IN_MEMORY);
71 } 122 }
72} 123}
73 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
74sub pre_load { 144sub post_load {
75 my ($self) = @_; 145 my ($self) = @_;
76 146
77 my ($x, $y) = $self->wxwy; 147 $self->create_region_treasure
78 148 if delete $self->{need_create_treasure};
79 my $stride = ($WORLD->{maxx} - $WORLD->{minx} + 1) * $WORLD->{tilew};
80 my $top = ($y - $WORLD->{miny}) * $WORLD->{tileh} * $stride
81 + ($x - $WORLD->{minx}) * $WORLD->{tilew};
82
83 for my $Y (0 .. $WORLD->{tileh} - 1) {
84 Coro::cede;
85 my $row = substr $WORLD->{data}, $top + $Y * $stride, $WORLD->{tilew};
86 for my $X (0 .. $WORLD->{tilew} - 1) {
87 my $ob = cf::object::new $WORLD->{arch}[ord substr $row, $X];
88 $self->insert ($ob, $X, $Y);
89 }
90 }
91
92 $self->set_object_flag (cf::FLAG_NO_MAP_SAVE, 1);
93} 149}
94 150
951 151package ext::map_world::empty;
96 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