ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/map-world.ext
Revision: 1.45
Committed: Wed May 4 16:12:15 2011 UTC (13 years ago) by root
Branch: MAIN
Changes since 1.44: +0 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #! perl # mandatory
2
3 # manages the worldmap by directing to other handlers
4
5 cf::map->register (qr{^/world/world_(-?\d+)_(-?\d+)(?:_(-?\d+))?$}, 100);
6
7 # universal
8 our $TILE_W = 50;
9 our $TILE_H = 50;
10
11 # registry
12 our @AREAS;
13
14 sub 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
37 sub format_xyz($$;$) {
38 sprintf "/world/world_%03d_%03d%s",
39 $_[0], $_[1],
40 $_[2] ? sprintf "_%03d", $_[2] : "";
41 }
42
43 #############################################################################
44
45 sub 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 last;
65 }
66 }
67
68 $self->SUPER::init;
69 }
70
71 sub init_worldmap {
72 # only for handlers
73 }
74
75 sub load_header_orig {
76 my ($self) = @_;
77
78 my ($x, $y, $z) = @$self{qw(x y z)};
79
80 $self->width ($TILE_W);
81 $self->height ($TILE_H);
82
83 $self->name (sprintf "'The World' at %+d%+d%+d", $x, $y, $z);
84 $self->msg ("worldmap dynamically created by map-world extension");
85 $self->outdoor ($self->{z} >= 0);
86 $self->default_region (cf::region::find "wilderness");
87
88 # all possible worldmap tiles also exist
89 $self->tile_path (0, format_xyz $x, $y - 1, $z);
90 $self->tile_path (1, format_xyz $x + 1, $y, $z);
91 $self->tile_path (2, format_xyz $x, $y + 1, $z);
92 $self->tile_path (3, format_xyz $x - 1, $y, $z);
93 $self->tile_path (4, format_xyz $x, $y, $z + 1);
94 $self->tile_path (5, format_xyz $x, $y, $z - 1);
95
96 # an overlay always wins
97 my $overlay = sprintf "%s/%s.map", $cf::MAPDIR, format_xyz $x, $y, $z;
98
99 $self->{load_path} = $overlay
100 unless Coro::AIO::aio_stat $overlay;
101
102 $self->{need_create_treasure} = 1;
103
104 1
105 }
106
107 # fill map with "default" data - first region and first archetype from palette
108 # used when we have no map data from elsewhere
109 sub fill {
110 my ($self) = @_;
111
112 if ($self->{z} > 0) {
113 # hmmm... air? :)
114 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]);
115 } elsif ($self->{z} < 0) {
116 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["blocked"]);
117 } else {
118 $self->add_underlay ("\x00" x ($TILE_W * $TILE_H), 0, $TILE_W, ["deep_sea"]);
119 }
120 }
121
122 #sub load {
123 # my ($self) = @_;
124 #
125 # if ($self->{load_path}) {
126 # warn "load $self\n";#d#
127 # $self->SUPER::load;
128 # } else {
129 # warn "fill $self\n";#d#
130 # my $guard = cf::lock_acquire "map_data:$self->{path}";
131 #
132 # $self->alloc;
133 # $self->fill;
134 # $self->state (cf::MAP_ACTIVE);
135 # $self->activate;
136 # $self->post_load;
137 # }
138 #}
139
140 # MUST be overwritten to call at least ->fill
141 sub post_load {
142 my ($self) = @_;
143
144 $self->create_region_treasure
145 if delete $self->{need_create_treasure};
146 }
147