ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cfmaps/cfmap2html
Revision: 1.27
Committed: Thu Jun 21 00:14:50 2007 UTC (16 years, 11 months ago) by root
Branch: MAIN
Changes since 1.26: +15 -8 lines
Log Message:
optimised amd fixed cfmap2html

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2    
3 root 1.9 # cfmap2html - convert crossfire maps to html
4 root 1.23 # Copyright (C) 2005,2007 Marc Lehmann <cfmaps@schmorp.de>
5 root 1.9 #
6     # CFMAP2HTML is free software; you can redistribute it and/or modify
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
9     # (at your option) any later version.
10     #
11     # This program is distributed in the hope that it will be useful,
12     # but WITHOUT ANY WARRANTY; without even the implied warranty of
13     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     # GNU General Public License for more details.
15     #
16     # You should have received a copy of the GNU General Public License
17 root 1.25 # along with cfmaps; if not, write to the Free Software
18 root 1.9 # Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19    
20 root 1.27 our $VERSION = '2.001';
21 root 1.10
22 root 1.27 use Crossfire 1.0;
23 root 1.1
24     my $T = 32;
25    
26     my $arch;
27    
28     sub escape_html($) {
29     local $_ = shift;
30     s/([<>&])/sprintf "&#%d;", ord $1/ge;
31     $_
32     }
33    
34 root 1.27 my @cfmap2png;
35    
36 root 1.1 for my $path (@ARGV) {
37 root 1.24 (my $base = $path) =~ s/\.map//;
38 root 1.5 # print STDERR "$path\n";
39 root 1.3
40 root 1.24 if (!-e "$base.png"
41     || !-e "$base.pst"
42     || -M "$base.pst" > -M $path
43     || -M "$base.png" > -M $path) {
44 root 1.1 # regenerate png and metainfo
45 root 1.27 push @cfmap2png, $path;
46     };
47     }
48 root 1.1
49 root 1.27 system "cfmap2png", @cfmap2png
50     if @cfmap2png;
51 root 1.1
52 root 1.27 for my $path (@ARGV) {
53     (my $base = $path) =~ s/\.map//;
54 root 1.24 if (!-e "$base.xhtml"
55     || -M "$base.xhtml" > -M "$base.pst") {
56 root 1.27
57     Crossfire::load_archetypes
58     unless %ARCH;
59    
60 root 1.24 my $meta = Storable::retrieve "$base.pst";
61    
62 root 1.25 open my $fh, ">:utf8", "$base.xhtml"
63     or die "$base.xhtml: $!";
64 root 1.24
65     select $fh;
66    
67     my $W = $meta->{width} * $T;
68     my $H = $meta->{height} * $T;
69    
70     my $W2 = $W + 600;
71    
72 root 1.25 my (@path) = split /\//, $base;
73 root 1.24
74     print "<?xml version='1.0' encoding='utf-8'?>",
75     '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
76     "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>",
77     "<head>",
78     "<title>Crossfire Map \"$path\"</title>",
79     "<link rel='stylesheet' type='text/css' media='all' href='/common.css'/>\n",
80     "<link rel='stylesheet' type='text/css' media='all' href='/overlay.css' title='Show Overlays'/>\n",
81     "<link rel='alternate stylesheet' type='text/css' media='all' href='/plain.css' title='Hide Overlays'/>\n",
82     "<style type='text/css'>\n",
83     ".map { width: ${W}px; height: ${H}px; background-image: url($path[-1].png); }\n",
84     ".enlarge { width: ${W2}px; height: 600px; }\n",
85     "</style>",
86     "</head>",
87     "<body>";
88    
89     print "<table class='nav'>",
90     "<tr class='center'><td class='title' rowspan='3'>",
91     "Crossfire Map<br/>",
92     "<span class='big'>";
93     print "<a href='/'>/</a> ";
94     for (0 .. $#path - 1) {
95     print "<a href='/", (join "/", @path[0..$_]), "/'>$path[$_]</a> / ";
96     }
97 root 1.1
98 root 1.24 my @dir = qw(none up right down left);
99     my @tile = map {
100     $meta->{info}{"tile_path_$_"}
101     ? "<a href='$meta->{info}{\"tile_path_$_\"}.xhtml'><img class='tile' src='$meta->{info}{\"tile_path_$_\"}.jpg' alt='$dir[$_]'/></a>"
102     : ""
103     } 1..4;
104     #"}"# vim misparses without this comment
105    
106     print "$path[-1]",
107     "</span>",
108     "<p class='about'><a href='/about.txt'>[more about cfmaps.schmorp.de]</a></p>",
109     "</td>",
110     "<td/><td>$tile[0]</td><td/></tr>",
111     "<tr><td>$tile[3]</td>",
112     "<td><img class='thumb' src='@path[-1].jpg' width='$meta->{width}' height='$meta->{height}' alt='map thumbnail'/></td>",
113     "<td>$tile[1]</td></tr>",
114     "<tr><td/><td>$tile[2]</td><td/></tr>",
115     "</table>";
116    
117     my $W1 = $W + 600;
118    
119     print "<p class='m'>",
120     escape_html $meta->{info}{msg},
121     "</p>";
122    
123     print "<table class='map'>";
124    
125     my %ignore = map +($_ => 1), qw(name _name x y);
126     my %is_exit = map +($_ => 1), 41, 57, 66;
127    
128     for my $y (0.. $meta->{height} - 1) {
129     print "<tr>";
130     for my $x (0.. $meta->{width} - 1) {
131     if (my $as = $meta->{map}[$x][$y]) {
132     my @class;
133    
134     push @class, "fishy" if grep $_->{invisible} || $_->{face} || exists $_->{no_pass} || exists $_->{no_pick}, @$as;
135     push @class, "exit" if grep $is_exit{$arch->{$_->{_name}}{type}} && $_->{slaying}, @$as;
136     push @class, "dialog" if grep $_->{msg} =~ /^\@match/m, @$as;
137    
138     print "<td", (@class ? " class='" . (join " ", @class) . "'" : ""), ">";
139     print "<div>";
140    
141     print join "\n", map "<span class='c'>$_</span>",
142     reverse sort { (length $a) <=> (length $b) or $b <=> $a }
143     grep $_, map $_->{connected}, @$as;
144    
145     print "<div>($x|$y)";
146    
147     sub print_archs {
148     print "<ul>";
149     for my $a (@{$_[0]}) {
150     my $o = $arch->{$a->{_name}};
151     my $type = $a->{type} || $o->{type};
152     my $aname = escape_html $a->{_name};
153     my $name = escape_html $a->{name} || $o->{name};
154    
155     print "<li><a href='/a/$a->{_name}'>$aname \"$name\"</a>\n";
156     for (sort keys %$a) {
157     next if $ignore{$_};
158     my $v = escape_html $a->{$_};
159    
160     if ($_ eq "slaying" && $is_exit{$type}) { # door, teleporter, player_changer
161     $a->{msg} =~ /^final_map\s*(\S+)\s*$/m, $v = $1
162     if $v eq "/!"; # random map
163    
164     print "slaying => <a href='$v.xhtml'>$v</a>\n";
165     } elsif ($_ eq "other_arch") {
166     print "$_ => <a href='/a/$a->{$_}'>$v</a>\n";
167     } elsif ($_ eq "inventory") {
168     print "inventory =>\n";
169     print_archs ($a->{$_});
170     } elsif ($_ eq "msg") {
171     print "<p class='m'>$v</p>";
172     } else {
173     print "$_ => $v\n";
174     }
175 root 1.5 }
176 root 1.24 print "</li>";
177 root 1.1 }
178 root 1.24 print "</ul>";
179 root 1.1 }
180 root 1.24
181     print_archs $as;
182     print "</div></div></td>";
183     } else {
184     print "<td/>";
185 root 1.1 }
186 root 1.24 }
187     print "</tr>";
188 root 1.1 }
189    
190 root 1.24 print "</table><p class='footer'>created by <a href='http://software.schmorp.de/pkg/cfmaps'>cfmap2html</a> version $VERSION</p>",
191     "<p class='enlarge'/></body></html>";
192 root 1.1
193 root 1.24 close $fh;
194 root 1.1
195 root 1.24 #system "gzip", "-7f", "$path.xhtml";
196     }
197 root 1.1 }
198