1 | package Crossfire; |
|
|
2 | =head1 NAME |
1 | =head1 NAME |
3 | |
2 | |
4 | Crossfire - Crossfire maphandling |
3 | Crossfire - Crossfire maphandling |
5 | |
4 | |
6 | =cut |
5 | =cut |
7 | |
6 | |
|
|
7 | package Crossfire; |
|
|
8 | |
8 | our $VERSION = '1.21'; |
9 | our $VERSION = '0.1'; |
9 | |
10 | |
10 | use strict; |
11 | use strict; |
11 | |
12 | |
12 | use Storable; |
13 | use Storable; |
13 | use List::Util qw(max); |
|
|
14 | |
14 | |
15 | #use Gtk2; |
|
|
16 | |
|
|
17 | #init Gtk2::Gdk; |
|
|
18 | |
|
|
19 | my $LIB = $ENV{CROSSFIRE_LIBDIR} |
15 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
20 | or die "\$CROSSFIRE_LIBDIR must be set\n"; |
16 | or die "\$CROSSFIRE_LIBDIR must be set\n"; |
21 | |
|
|
22 | my $VARDIR = "$ENV{HOME}/.gcfedit"; |
|
|
23 | mkdir $VARDIR; |
|
|
24 | |
17 | |
25 | sub T (){ 32 } |
18 | sub T (){ 32 } |
26 | |
19 | |
|
|
20 | our $ARCH; |
|
|
21 | our $TILE; |
|
|
22 | |
27 | sub read_pak($) { |
23 | sub read_pak($;$) { |
28 | my ($path) = @_; |
24 | my ($path, $cache) = @_; |
29 | |
25 | |
30 | eval { |
26 | eval { |
31 | -M "$VARDIR/crossfire.pak.pst" < -M $path |
27 | defined $cache |
32 | && Storable::retrieve "$VARDIR/crossfire.pak.pst" |
28 | && -M $cache < -M $path |
|
|
29 | && Storable::retrieve $cache |
33 | } or do { |
30 | } or do { |
34 | my %pak; |
31 | my %pak; |
35 | |
32 | |
36 | open my $fh, "<:raw", $path |
33 | open my $fh, "<:raw", $path |
37 | or die "$_[0]: $!"; |
34 | or die "$_[0]: $!"; |
… | |
… | |
39 | my ($type, $id, $len, $path) = split; |
36 | my ($type, $id, $len, $path) = split; |
40 | $path =~ s/.*\///; |
37 | $path =~ s/.*\///; |
41 | read $fh, $pak{$path}, $len; |
38 | read $fh, $pak{$path}, $len; |
42 | } |
39 | } |
43 | |
40 | |
44 | Storable::nstore \%pak, "$VARDIR/crossfire.pak.pst"; |
41 | Storable::nstore \%pak, $cache |
|
|
42 | if defined $cache; |
45 | |
43 | |
46 | \%pak |
44 | \%pak |
47 | } |
45 | } |
48 | } |
46 | } |
49 | |
47 | |
50 | sub read_arch($;$) { |
48 | sub read_arch($;$) { |
51 | my ($path) = @_; |
49 | my ($path, $cache) = @_; |
52 | |
50 | |
|
|
51 | eval { |
|
|
52 | defined $cache |
|
|
53 | && -M $cache < -M $path |
|
|
54 | && Storable::retrieve $cache |
|
|
55 | } or do { |
53 | my %arc; |
56 | my %arc; |
54 | my ($more, $prev); |
57 | my ($more, $prev); |
55 | |
58 | |
56 | open my $fh, "<:raw", $path |
59 | open my $fh, "<:raw", $path |
57 | or die "$path: $!"; |
60 | or die "$path: $!"; |
58 | |
61 | |
59 | my $parse_block; $parse_block = sub { |
62 | my $parse_block; $parse_block = sub { |
60 | my %arc = @_; |
63 | my %arc = @_; |
|
|
64 | |
|
|
65 | while (<$fh>) { |
|
|
66 | s/\s+$//; |
|
|
67 | if (/^end$/i) { |
|
|
68 | last; |
|
|
69 | } elsif (/^arch (\S+)$/) { |
|
|
70 | push @{ $arc{inventory} }, $parse_block->(_name => $1); |
|
|
71 | } elsif (/^lore$/) { |
|
|
72 | while (<$fh>) { |
|
|
73 | last if /^endlore\s*$/i; |
|
|
74 | $arc{lore} .= $_; |
|
|
75 | } |
|
|
76 | } elsif (/^msg$/) { |
|
|
77 | while (<$fh>) { |
|
|
78 | last if /^endmsg\s*$/i; |
|
|
79 | $arc{msg} .= $_; |
|
|
80 | } |
|
|
81 | } elsif (/^(\S+)\s*(.*)$/) { |
|
|
82 | $arc{lc $1} = $2; |
|
|
83 | } elsif (/^\s*($|#)/) { |
|
|
84 | # |
|
|
85 | } else { |
|
|
86 | warn "$path: unparsable line '$_' in arch $arc{_name}"; |
|
|
87 | } |
|
|
88 | } |
|
|
89 | |
|
|
90 | \%arc |
|
|
91 | }; |
61 | |
92 | |
62 | while (<$fh>) { |
93 | while (<$fh>) { |
63 | s/\s+$//; |
94 | s/\s+$//; |
64 | if (/^end$/i) { |
95 | if (/^more$/i) { |
65 | last; |
96 | $more = $prev; |
66 | } elsif (/^arch (\S+)$/) { |
97 | } elsif (/^object (\S+)$/i) { |
|
|
98 | my $name = $1; |
67 | push @{ $arc{inventory} }, $parse_block->(_name => $1); |
99 | my $arc = $parse_block->(_name => $name); |
68 | } elsif (/^lore$/) { |
100 | |
69 | while (<$fh>) { |
101 | if ($more) { |
70 | last if /^endlore\s*$/i; |
102 | $more->{more} = $arc; |
|
|
103 | } else { |
71 | $arc{lore} .= $_; |
104 | $arc{$name} = $arc; |
72 | } |
105 | } |
73 | } elsif (/^msg$/) { |
106 | $prev = $arc; |
74 | while (<$fh>) { |
107 | $more = undef; |
75 | last if /^endmsg\s*$/i; |
|
|
76 | $arc{msg} .= $_; |
|
|
77 | } |
|
|
78 | } elsif (/^(\S+)\s*(.*)$/) { |
108 | } elsif (/^arch (\S+)$/i) { |
79 | $arc{lc $1} = $2; |
109 | push @{ $arc{arch} }, $parse_block->(_name => $1); |
80 | } elsif (/^\s*($|#)/) { |
110 | } elsif (/^\s*($|#)/) { |
81 | # |
111 | # |
82 | } else { |
112 | } else { |
83 | warn "$path: unparsable line '$_' in arch $arc{_name}"; |
113 | warn "$path: unparseable top-level line '$_'"; |
84 | } |
114 | } |
85 | } |
115 | } |
86 | |
116 | |
|
|
117 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
|
|
118 | |
|
|
119 | Storable::nstore \%arc, $cache |
|
|
120 | if defined $cache; |
|
|
121 | |
87 | \%arc |
122 | \%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 | } |
123 | } |
113 | |
|
|
114 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
|
|
115 | |
|
|
116 | \%arc |
|
|
117 | } |
124 | } |
118 | |
125 | |
119 | sub cfmap_meta($;$) { |
126 | sub arch2map($;$) { |
120 | my ($self, $mapa, $mapname) = @_; |
127 | my ($mapa) = @_; |
121 | |
|
|
122 | my $arch = $self->{arch}; |
|
|
123 | |
128 | |
124 | my %meta; |
129 | my %meta; |
125 | |
130 | |
126 | my ($mapx, $mapy); |
131 | my ($mapx, $mapy); |
127 | |
132 | |
… | |
… | |
147 | } |
152 | } |
148 | |
153 | |
149 | $meta{width} = $mapx; |
154 | $meta{width} = $mapx; |
150 | $meta{height} = $mapy; |
155 | $meta{height} = $mapy; |
151 | |
156 | |
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 |
157 | \%meta |
205 | } |
158 | } |
206 | |
159 | |
207 | sub new { |
160 | sub init($) { |
208 | my $class = shift; |
161 | 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 | |
162 | |
215 | sub read { |
163 | $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
216 | my ($self, $file) = @_; |
164 | $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 | } |
165 | } |
222 | |
166 | |
223 | =head1 AUTHOR |
167 | =head1 AUTHOR |
224 | |
168 | |
225 | Marc Lehmann <schmorp@schmorp.de> |
169 | Marc Lehmann <schmorp@schmorp.de> |
… | |
… | |
227 | |
171 | |
228 | Robin Redeker <elmex@ta-sa.org> |
172 | Robin Redeker <elmex@ta-sa.org> |
229 | http://www.ta-sa.org/ |
173 | http://www.ta-sa.org/ |
230 | |
174 | |
231 | =cut |
175 | =cut |
232 | 1; |
176 | |
|
|
177 | 1 |