1 | #!/opt/bin/perl |
1 | #!/opt/bin/perl |
2 | |
2 | |
3 | # cfmap2idx - inverted index for crossfire maps |
3 | # cfmap2idx - inverted index for crossfire maps |
4 | # Copyright (C) 2005 Marc Lehmann <gvpe@schmorp.de> |
4 | # Copyright (C) 2005,2007 Marc Lehmann <cfmaps@schmorp.de> |
5 | # |
5 | # |
6 | # CFMAPIDX is free software; you can redistribute it and/or modify |
6 | # CFMAPIDX is free software; you can redistribute it and/or modify |
7 | # it under the terms of the GNU General Public License as published by |
7 | # it under the terms of the GNU General Public License as published by |
8 | # the Free Software Foundation; either version 2 of the License, or |
8 | # the Free Software Foundation; either version 2 of the License, or |
9 | # (at your option) any later version. |
9 | # (at your option) any later version. |
… | |
… | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | # GNU General Public License for more details. |
14 | # GNU General Public License for more details. |
15 | # |
15 | # |
16 | # You should have received a copy of the GNU General Public License |
16 | # You should have received a copy of the GNU General Public License |
17 | # along with gvpe; if not, write to the Free Software |
17 | # along with cfmaps; if not, write to the Free Software |
18 | # Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
18 | # Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | |
19 | |
20 | our $VERSION = '0.0'; |
20 | our $VERSION = '0.912'; |
21 | |
21 | |
22 | use Storable; |
22 | use Crossfire; |
23 | use DB_File; |
23 | use DB_File; |
24 | |
24 | |
25 | sub escape_html($) { |
25 | sub escape_html($) { |
26 | local $_ = shift; |
26 | local $_ = shift; |
27 | s/([<>&])/sprintf "&#%d;", ord $1/ge; |
27 | s/([<>&])/sprintf "&#%d;", ord $1/ge; |
28 | $_ |
28 | $_ |
|
|
29 | } |
|
|
30 | |
|
|
31 | sub search { |
|
|
32 | my (@kw) = @_; |
|
|
33 | |
|
|
34 | tie %idx, DB_File, ".index.dat", O_RDONLY, 0, $DB_HASH |
|
|
35 | or die ".index.dat: $!"; |
|
|
36 | |
|
|
37 | my $cnt; |
|
|
38 | my %res; |
|
|
39 | my @found; |
|
|
40 | |
|
|
41 | for (map lc, @kw) { |
|
|
42 | if (exists $idx{$_}) { |
|
|
43 | $cnt++; |
|
|
44 | $res{$_}++ for unpack "n*", $idx{$_}; |
|
|
45 | push @found, $_; |
|
|
46 | #warn "$_ found\n";#d# |
|
|
47 | } else { |
|
|
48 | #warn "$_ not found\n";#d# |
|
|
49 | } |
|
|
50 | } |
|
|
51 | |
|
|
52 | my @paths; |
|
|
53 | |
|
|
54 | while (my ($k, $v) = each %res) { |
|
|
55 | next unless $v == $cnt; |
|
|
56 | push @paths, $idx{"D" . pack "n", $k}; |
|
|
57 | } |
|
|
58 | |
|
|
59 | binmode STDOUT, ":utf8"; |
|
|
60 | |
|
|
61 | print "<?xml version='1.0' encoding='utf-8'?>", |
|
|
62 | '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">', |
|
|
63 | "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>", |
|
|
64 | "<head>", |
|
|
65 | "<title>Crossfire Keyword Search</title>", |
|
|
66 | "<link rel='stylesheet' type='text/css' media='all' href='/common.css'/>\n", |
|
|
67 | "</head>", |
|
|
68 | "<body>"; |
|
|
69 | |
|
|
70 | print "<h1>Search results for '", (join "' & '", map escape_html $_, @found), "'</h1>", |
|
|
71 | "<p class='searchcount'>", (scalar @paths), " results found, up to 200 results shown.</p>", |
|
|
72 | "<p class='searchresult'><ul>"; |
|
|
73 | |
|
|
74 | pop @paths while @paths > 200; |
|
|
75 | |
|
|
76 | for (sort @paths) { |
|
|
77 | print "<li><a href='$_.xhtml'>$_</a></li>"; |
|
|
78 | } |
|
|
79 | |
|
|
80 | print "</ul></p><p class='footer'>created by <a href='http://software.schmorp.de/pkg/cfmaps'>cfmapidx</a> version $VERSION</p>", |
|
|
81 | "</body></html>"; |
29 | } |
82 | } |
30 | |
83 | |
31 | if ($ARGV[0] eq "-a") { |
84 | if ($ARGV[0] eq "-a") { |
32 | shift; |
85 | shift; |
33 | |
86 | |
… | |
… | |
35 | or die ".index.dat~: $!"; |
88 | or die ".index.dat~: $!"; |
36 | |
89 | |
37 | my %idx2; |
90 | my %idx2; |
38 | |
91 | |
39 | for my $path (@ARGV) { |
92 | for my $path (@ARGV) { |
|
|
93 | (my $base = $path) =~ s/\.map$//; |
40 | my $docnum = pack "n", ++$idx{Vdocnum}; |
94 | my $docnum = pack "n", ++$idx{Vdocnum}; |
41 | $idx{"D$docnum"} = $path; |
95 | $idx{"D$docnum"} = $base; |
42 | my $meta = Storable::retrieve "$path.pst"; |
96 | my $meta = read_arch "$base.map"; |
43 | |
97 | |
44 | for my $x (0 .. $meta->{width} - 1) { |
98 | for my $x (0 .. $meta->{width} - 1) { |
45 | for my $y (0 .. $meta->{height} - 1) { |
99 | for my $y (0 .. $meta->{height} - 1) { |
46 | for my $a (@{ $meta->{map}[$x][$y] }) { |
100 | for my $a (@{ $meta->{map}[$x][$y] }) { |
47 | for my $v (values %$a) { |
101 | for my $v (values %$a) { |
48 | $v = lc $v; |
102 | $v = lc $v; |
49 | $v =~ y/a-zA-Z0-9_\-./ /c; |
103 | $v =~ y/a-zA-Z0-9_\-.\// /c; |
50 | for (split /\s+/, $v) { |
104 | for (split /\s+/, $v) { |
51 | $idx2{$_} .= $docnum; |
105 | $idx2{$_} .= $docnum; |
52 | |
106 | |
53 | if (/_\-\./) { |
107 | if (/[_\-\.\/]/) { |
54 | $idx2{$_} .= $docnum for (split /[_\-\.]/) |
108 | $idx2{$_} .= $docnum for (split /[_\-\.\/]/) |
55 | } |
109 | } |
56 | } |
110 | } |
57 | } |
111 | } |
58 | } |
112 | } |
59 | } |
113 | } |
… | |
… | |
65 | } |
119 | } |
66 | |
120 | |
67 | } elsif ($ARGV[0] eq "-s") { |
121 | } elsif ($ARGV[0] eq "-s") { |
68 | shift; |
122 | shift; |
69 | |
123 | |
70 | tie %idx, DB_File, ".index.dat", O_RDONLY, 0, $DB_HASH |
124 | search @ARGV; |
71 | or die ".index.dat: $!"; |
125 | } else { |
|
|
126 | # assume CGI |
|
|
127 | chdir "/var/www/cfmaps.schmorp.de" or exit 69; |
72 | |
128 | |
73 | my $cnt; |
129 | print <<EOF; |
74 | my %res; |
130 | Content-Type: application/xhtml+xml |
75 | |
131 | |
76 | for (map lc, @ARGV) { |
132 | EOF |
77 | if (exists $idx{$_}) { |
133 | $ENV{QUERY_STRING} =~ s/^k=//; |
78 | $cnt++; |
134 | search split /\s+|\++|(?:%20)+/, $ENV{QUERY_STRING}; |
79 | $res{$_}++ for unpack "n*", $_; |
|
|
80 | } else { |
|
|
81 | warn "$_ not found\n";#d# |
|
|
82 | } |
|
|
83 | } |
|
|
84 | |
|
|
85 | while (my ($k, $v) = each %res) { |
|
|
86 | next unless $v == $cnt; |
|
|
87 | my $path = $idx{"D" . pack "n", $k}; |
|
|
88 | warn "found in doc $path\n"; |
|
|
89 | } |
|
|
90 | |
|
|
91 | # print STDERR "$path\n"; |
|
|
92 | die; |
|
|
93 | |
|
|
94 | open my $fh, ">:utf8", "$path.xhtml" |
|
|
95 | or die "$path.xhtml: $!"; |
|
|
96 | |
|
|
97 | select $fh; |
|
|
98 | |
|
|
99 | my $W = $meta->{width} * $T; |
|
|
100 | my $H = $meta->{height} * $T; |
|
|
101 | |
|
|
102 | my $W2 = $W + 600; |
|
|
103 | |
|
|
104 | my (@path) = split /\//, $path; |
|
|
105 | |
|
|
106 | print "<?xml version='1.0' encoding='utf-8'?>", |
|
|
107 | "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>", |
|
|
108 | "<head>", |
|
|
109 | "<title>Crossfire Map \"$path\"</title>", |
|
|
110 | "<link rel='stylesheet' type='text/css' media='all' href='/common.css'/>\n", |
|
|
111 | "<link rel='stylesheet' type='text/css' media='all' href='/overlay.css' title='Show Overlays'/>\n", |
|
|
112 | "<link rel='alternate stylesheet' type='text/css' media='all' href='/plain.css' title='Hide Overlays'/>\n", |
|
|
113 | "<style type='text/css'>\n", |
|
|
114 | ".map { width: ${W}px; height: ${H}px; background-image: url($path[-1].png); }\n", |
|
|
115 | ".enlarge { width: ${W2}px; height: 600px; }\n", |
|
|
116 | "</style>", |
|
|
117 | "</head>", |
|
|
118 | "<body>"; |
|
|
119 | |
|
|
120 | print "<table class='nav'>", |
|
|
121 | "<tr class='center'><td class='title' rowspan='3'>", |
|
|
122 | "Crossfire Map<br/>", |
|
|
123 | "<span class='big'>"; |
|
|
124 | print "<a href='/'>/</a> "; |
|
|
125 | for (0 .. $#path - 1) { |
|
|
126 | print "<a href='/", (join "/", @path[0..$_]), "/'>$path[$_]</a> / "; |
|
|
127 | } |
|
|
128 | |
|
|
129 | my @dir = qw(none up right down left); |
|
|
130 | my @tile = map { |
|
|
131 | $meta->{info}{"tile_path_$_"} |
|
|
132 | ? "<a href='$meta->{info}{\"tile_path_$_\"}.xhtml'><img class='tile' src='$meta->{info}{\"tile_path_$_\"}.jpg' alt='$dir[$_]'/></a>" |
|
|
133 | : "" |
|
|
134 | } 1..4; |
|
|
135 | #"}"# vim misparses without this comment |
|
|
136 | |
|
|
137 | print "$path[-1]", |
|
|
138 | "</span>", |
|
|
139 | "<p class='about'><a href='/about.txt'>[more about cfmaps.schmorp.de]</a></p>", |
|
|
140 | "</td>", |
|
|
141 | "<td/><td>$tile[0]</td><td/></tr>", |
|
|
142 | "<tr><td>$tile[3]</td>", |
|
|
143 | "<td><img class='thumb' src='@path[-1].jpg' width='$meta->{width}' height='$meta->{height}' alt='map thumbnail'/></td>", |
|
|
144 | "<td>$tile[1]</td></tr>", |
|
|
145 | "<tr><td/><td>$tile[2]</td><td/></tr>", |
|
|
146 | "</table>"; |
|
|
147 | |
|
|
148 | my $W1 = $W + 600; |
|
|
149 | |
|
|
150 | print "<p class='m'>", |
|
|
151 | escape_html $meta->{info}{msg}, |
|
|
152 | "</p>"; |
|
|
153 | |
|
|
154 | print "<table class='map'>"; |
|
|
155 | |
|
|
156 | my %ignore = map +($_ => 1), qw(name _name x y); |
|
|
157 | my %is_exit = map +($_ => 1), 41, 57, 66; |
|
|
158 | |
|
|
159 | for my $y (0.. $meta->{height} - 1) { |
|
|
160 | print "<tr>"; |
|
|
161 | for my $x (0.. $meta->{width} - 1) { |
|
|
162 | if (my $as = $meta->{map}[$x][$y]) { |
|
|
163 | my @class; |
|
|
164 | |
|
|
165 | push @class, "fishy" if grep $_->{invisible} || $_->{face} || exists $_->{no_pass}, @$as; |
|
|
166 | push @class, "exit" if grep $is_exit{$arch->{$_->{_name}}{type}} && $_->{slaying}, @$as; |
|
|
167 | push @class, "dialog" if grep $_->{msg} =~ /^\@match/m, @$as; |
|
|
168 | |
|
|
169 | print "<td", (@class ? " class='" . (join " ", @class) . "'" : ""), ">"; |
|
|
170 | print "<div>"; |
|
|
171 | |
|
|
172 | print join "\n", map "<span class='c'>$_</span>", |
|
|
173 | reverse sort { (length $a) <=> (length $b) or $b <=> $a } |
|
|
174 | grep $_, map $_->{connected}, @$as; |
|
|
175 | |
|
|
176 | print "<div>($x|$y)"; |
|
|
177 | |
|
|
178 | sub print_archs { |
|
|
179 | print "<ul>"; |
|
|
180 | for my $a (@{$_[0]}) { |
|
|
181 | my $o = $arch->{$a->{_name}}; |
|
|
182 | my $type = $a->{type} || $o->{type}; |
|
|
183 | my $aname = escape_html $a->{_name}; |
|
|
184 | my $name = escape_html $a->{name} || $o->{name}; |
|
|
185 | |
|
|
186 | print "<li><a href='/arc.xhtml#", (lc $a->{_name}), "'>$aname \"$name\"</a>\n"; |
|
|
187 | for (sort keys %$a) { |
|
|
188 | next if $ignore{$_}; |
|
|
189 | my $v = escape_html $a->{$_}; |
|
|
190 | |
|
|
191 | if ($_ eq "slaying" && $is_exit{$type}) { # door, teleporter, player_changer |
|
|
192 | $a->{msg} =~ /^final_map\s*(\S+)\s*$/m, $v = $1 |
|
|
193 | if $v eq "/!"; # random map |
|
|
194 | |
|
|
195 | print "slaying => <a href='$v.xhtml'>$v</a>\n"; |
|
|
196 | } elsif ($_ eq "other_arch") { |
|
|
197 | print "$_ => <a href='/arc.xhtml#", (lc $a->{$_}), "'>$v</a>\n"; |
|
|
198 | } elsif ($_ eq "inventory") { |
|
|
199 | print "inventory =>\n"; |
|
|
200 | print_archs ($a->{$_}); |
|
|
201 | } elsif ($_ eq "msg") { |
|
|
202 | print "<p class='m'>$v</p>"; |
|
|
203 | } else { |
|
|
204 | print "$_ => $v\n"; |
|
|
205 | } |
|
|
206 | } |
|
|
207 | print "</li>"; |
|
|
208 | } |
|
|
209 | print "</ul>"; |
|
|
210 | } |
|
|
211 | |
|
|
212 | print_archs $as; |
|
|
213 | print "</div></div></td>"; |
|
|
214 | } else { |
|
|
215 | print "<td/>"; |
|
|
216 | } |
|
|
217 | } |
|
|
218 | print "</tr>"; |
|
|
219 | } |
|
|
220 | |
|
|
221 | print "</table><p class='footer'>created by <a href='http://software.schmorp.de/#crossfire'>cfmap2html</a> version $VERSION</p>", |
|
|
222 | "<p class='enlarge'/></body></html>"; |
|
|
223 | |
|
|
224 | close $fh; |
|
|
225 | } |
135 | } |
226 | |
136 | |