ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
(Generate patch)

Comparing deliantra/Deliantra/Deliantra.pm (file contents):
Revision 1.3 by elmex, Sat Feb 4 23:56:14 2006 UTC vs.
Revision 1.4 by root, Sun Feb 5 00:18:26 2006 UTC

1package Crossfire;
2=head1 NAME 1=head1 NAME
3 2
4Crossfire - Crossfire maphandling 3Crossfire - Crossfire maphandling
5 4
6=cut 5=cut
6
7package Crossfire;
7 8
8our $VERSION = '0.1'; 9our $VERSION = '0.1';
9 10
10use strict; 11use strict;
11 12
12use Storable; 13use Storable;
13use List::Util qw(max); 14use List::Util qw(max);
14 15
15#use Gtk2;
16
17#init Gtk2::Gdk;
18
19my $LIB = $ENV{CROSSFIRE_LIBDIR} 16our $LIB = $ENV{CROSSFIRE_LIBDIR}
20 or die "\$CROSSFIRE_LIBDIR must be set\n"; 17 or die "\$CROSSFIRE_LIBDIR must be set\n";
21
22my $VARDIR = "$ENV{HOME}/.gcfedit";
23mkdir $VARDIR;
24 18
25sub T (){ 32 } 19sub T (){ 32 }
26 20
21our $ARCH;
22our $TILE;
23
27sub read_pak($) { 24sub read_pak($;$) {
28 my ($path) = @_; 25 my ($path, $cache) = @_;
29 26
30 eval { 27 eval {
31 -M "$VARDIR/crossfire.pak.pst" < -M $path 28 defined $cache
32 && Storable::retrieve "$VARDIR/crossfire.pak.pst" 29 && -M $cache < -M $path
30 && Storable::retrieve $cache
33 } or do { 31 } or do {
34 my %pak; 32 my %pak;
35 33
36 open my $fh, "<:raw", $path 34 open my $fh, "<:raw", $path
37 or die "$_[0]: $!"; 35 or die "$_[0]: $!";
39 my ($type, $id, $len, $path) = split; 37 my ($type, $id, $len, $path) = split;
40 $path =~ s/.*\///; 38 $path =~ s/.*\///;
41 read $fh, $pak{$path}, $len; 39 read $fh, $pak{$path}, $len;
42 } 40 }
43 41
44 Storable::nstore \%pak, "$VARDIR/crossfire.pak.pst"; 42 Storable::nstore \%pak, $cache
43 if defined $cache;
45 44
46 \%pak 45 \%pak
47 } 46 }
48} 47}
49 48
50sub read_arch($;$) { 49sub read_arch($;$) {
51 my ($path) = @_; 50 my ($path, $cache) = @_;
52 51
52 eval {
53 defined $cache
54 && -M $cache < -M $path
55 && Storable::retrieve $cache
56 } or do {
53 my %arc; 57 my %arc;
54 my ($more, $prev); 58 my ($more, $prev);
55 59
56 open my $fh, "<:raw", $path 60 open my $fh, "<:raw", $path
57 or die "$path: $!"; 61 or die "$path: $!";
58 62
59 my $parse_block; $parse_block = sub { 63 my $parse_block; $parse_block = sub {
60 my %arc = @_; 64 my %arc = @_;
65
66 while (<$fh>) {
67 s/\s+$//;
68 if (/^end$/i) {
69 last;
70 } elsif (/^arch (\S+)$/) {
71 push @{ $arc{inventory} }, $parse_block->(_name => $1);
72 } elsif (/^lore$/) {
73 while (<$fh>) {
74 last if /^endlore\s*$/i;
75 $arc{lore} .= $_;
76 }
77 } elsif (/^msg$/) {
78 while (<$fh>) {
79 last if /^endmsg\s*$/i;
80 $arc{msg} .= $_;
81 }
82 } elsif (/^(\S+)\s*(.*)$/) {
83 $arc{lc $1} = $2;
84 } elsif (/^\s*($|#)/) {
85 #
86 } else {
87 warn "$path: unparsable line '$_' in arch $arc{_name}";
88 }
89 }
90
91 \%arc
92 };
61 93
62 while (<$fh>) { 94 while (<$fh>) {
63 s/\s+$//; 95 s/\s+$//;
64 if (/^end$/i) { 96 if (/^more$/i) {
65 last; 97 $more = $prev;
66 } elsif (/^arch (\S+)$/) { 98 } elsif (/^object (\S+)$/i) {
99 my $name = $1;
67 push @{ $arc{inventory} }, $parse_block->(_name => $1); 100 my $arc = $parse_block->(_name => $name);
68 } elsif (/^lore$/) { 101
69 while (<$fh>) { 102 if ($more) {
70 last if /^endlore\s*$/i; 103 $more->{more} = $arc;
104 } else {
71 $arc{lore} .= $_; 105 $arc{$name} = $arc;
72 } 106 }
73 } elsif (/^msg$/) { 107 $prev = $arc;
74 while (<$fh>) { 108 $more = undef;
75 last if /^endmsg\s*$/i;
76 $arc{msg} .= $_;
77 }
78 } elsif (/^(\S+)\s*(.*)$/) { 109 } elsif (/^arch (\S+)$/i) {
79 $arc{lc $1} = $2; 110 push @{ $arc{arch} }, $parse_block->(_name => $1);
80 } elsif (/^\s*($|#)/) { 111 } elsif (/^\s*($|#)/) {
81 # 112 #
82 } else { 113 } else {
83 warn "$path: unparsable line '$_' in arch $arc{_name}"; 114 warn "$path: unparseable top-level line '$_'";
84 } 115 }
85 } 116 }
86 117
118 undef $parse_block; # work around bug in perl not freeing $fh etc.
119
120 Storable::nstore \%arc, $cache
121 if defined $cache;
122
87 \%arc 123 \%arc
88 };
89
90 while (<$fh>) {
91 s/\s+$//;
92 if (/^more$/i) {
93 $more = $prev;
94 } elsif (/^object (\S+)$/i) {
95 my $name = $1;
96 my $arc = $parse_block->(_name => $name);
97
98 if ($more) {
99 $more->{more} = $arc;
100 } else {
101 $arc{$name} = $arc;
102 }
103 $prev = $arc;
104 $more = undef;
105 } elsif (/^arch (\S+)$/i) {
106 push @{ $arc{arch} }, $parse_block->(_name => $1);
107 } elsif (/^\s*($|#)/) {
108 #
109 } else {
110 warn "$path: unparseable top-level line '$_'";
111 }
112 } 124 }
113
114 undef $parse_block; # work around bug in perl not freeing $fh etc.
115
116 \%arc
117} 125}
118 126
119sub cfmap_meta($;$) { 127sub arch2map($;$) {
120 my ($self, $mapa, $mapname) = @_; 128 my ($mapa) = @_;
121
122 my $arch = $self->{arch};
123 129
124 my %meta; 130 my %meta;
125 131
126 my ($mapx, $mapy); 132 my ($mapx, $mapy);
127 133
147 } 153 }
148 154
149 $meta{width} = $mapx; 155 $meta{width} = $mapx;
150 $meta{height} = $mapy; 156 $meta{height} = $mapy;
151 157
152 my %draw_info;
153 my %map_info;
154
155 # first pass, gather face stacking order, border and corner info
156 for my $x (0 .. $mapx - 1) {
157 my $col = $map->[$x];
158 for my $y (0 .. $mapy - 1) {
159 my $as = $col->[$y] || [];
160
161 for my $layer (0 .. $#$as) {
162 my $a = $as->[$layer];
163
164 my $o = $arch->{$a->{_name}}
165 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
166
167 #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
168 my $level = $layer * 256;
169
170 $level -= 100 * 256 if $o->{_name} eq "blocked";
171
172 while ($o) {
173 my $face = $a->{face} || $o->{face};
174
175 my $mx = $x + $o->{x};
176 my $my = $y + $o->{y};
177
178 last if 0 > $mx || $mx >= $mapx
179 || 0 > $my || $my >= $mapy;
180
181 push @{ $map_info{$level}{$mx, $my} }, $a;
182
183 $o = $o->{more};
184 $level = ($layer + 1000) * 2; # put "big things" on top, no matter what
185 }
186 }
187 }
188 }
189
190 # third pass, gather meta info
191 for my $level (sort { $a <=> $b } keys %map_info) {
192 my $info = $map_info{$level};
193
194 while (my ($xy, $as) = each %$info) {
195 my ($x, $y) = split $;, $xy;
196
197 next if $x < 0 || $x >= $mapx
198 || $y < 0 || $y >= $mapy;
199
200 push @{ $meta{map}[$x][$y] }, $_ for @$as;
201 }
202 }
203
204 \%meta 158 \%meta
205} 159}
206 160
207sub new { 161sub init($) {
208 my $class = shift; 162 my ($cachedir) = @_;
209 my $self = bless { }, $class;
210 $self->{arch} = read_arch "$LIB/archetypes";
211 $self->{tile} = read_pak "$LIB/crossfire.0";
212 $self
213}
214 163
215sub read { 164 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
216 my ($self, $file) = @_; 165 $TILE = read_pak "$LIB/crossfire.0", "$cachedir/crossfire.0.pst";
217 my $mapa = read_arch $file;
218 my $map = $self->cfmap_meta ($mapa, $file);
219 print "READ: ".join(',', %{$map->{info}})."\n";
220 return $map;
221} 166}
222 167
223=head1 AUTHOR 168=head1 AUTHOR
224 169
225 Marc Lehmann <schmorp@schmorp.de> 170 Marc Lehmann <schmorp@schmorp.de>
227 172
228 Robin Redeker <elmex@ta-sa.org> 173 Robin Redeker <elmex@ta-sa.org>
229 http://www.ta-sa.org/ 174 http://www.ta-sa.org/
230 175
231=cut 176=cut
2321; 177
1781

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines