Debian Codegraph @

lib/collect

raw
1 #!/usr/bin/env perl
2
3 require "util.pl";
4
5 if ($#ARGV >= 0) {
6 if ($ARGV[0] =~ m/^--?[hH](elp)?$/) {
7 die ("\nUSAGE: perl collect.pl ARCHDIR\n".
8 "\nWhere ARCHDIR is the directory where you stored the ".
9 "raw archetypes.\n".
10 "This script will then create these files:\n".
11 "archetypes,bmaps.paths,faces,treasures,animations.\n"
12 )
13 }
14 }
15
16 # mkdir is nice because it is an atomic operation - if 2 programs
17 # try to do it at the same time, one will fail. Do a -e/create
18 # file check has a chance of race condition.
19 if (!mkdir("collect.lock",0)) {
20 print "Collect is already running. If you think this is an error,\n";
21 print "rmdir collect.lock\n";
22 # exit with a non zero code - in that way make errors out and
23 # won't try to build the next target - the images. If another
24 # process is running on the archetypes, that will make a new bmaps.paths
25 # requiring new images at that time.
26 exit(1);
27 }
28
29
30 # archonly is used to only build the archetypes. I find this
31 # very handy if I know I've only changed .arc files - I don't want
32 # to rebuild the other files, because now cvs tries to do diffs
33 # on them as well as commit them, even if there are no changes.
34 $archonly = 0;
35 if ($#ARGV >= 1) {
36 if ($ARGV[1] eq "ARCHONLY") { $archonly = 1; }
37 else {print "Ignoring unknown option: $ARGV[1]\n"; }
38 }
39
40
41 $root = $ARGV[0];
42 $archetypes = "archetypes";
43 $faces = "faces";
44 $treasures = "treasures";
45 $animations = "animations";
46 $paths = "bmaps.paths";
47 $faceExt = "\\.[a-zA-Z0-9][A-Z0-9][A-Z0-9]";
48 $smooths = "smooth";
49
50 ### main
51 &info("looking ...");
52 &traverse($root);
53
54 $attacktype{ 'physical' } = ( 1 << 0 );
55 $attacktype{ 'magic' } = ( 1 << 1 );
56 $attacktype{ 'fire' } = ( 1 << 2 );
57 $attacktype{ 'electricity' } = ( 1 << 3 );
58 $attacktype{ 'cold' } = ( 1 << 4 );
59 $attacktype{ 'confusion' } = ( 1 << 5 );
60 $attacktype{ 'acid' } = ( 1 << 6 );
61 $attacktype{ 'drain' } = ( 1 << 7 );
62 $attacktype{ 'weaponmagic' } = ( 1 << 8 );
63 $attacktype{ 'ghosthit' } = ( 1 << 9 );
64 $attacktype{ 'poison' } = ( 1 << 10 );
65 $attacktype{ 'slow' } = ( 1 << 11 );
66 $attacktype{ 'paralyze' } = ( 1 << 12 );
67 $attacktype{ 'turnundead' } = ( 1 << 13 );
68 $attacktype{ 'fear' } = ( 1 << 14 );
69 $attacktype{ 'cancellation' } = ( 1 << 15 );
70 $attacktype{ 'deplete' } = ( 1 << 16 );
71 $attacktype{ 'death' } = ( 1 << 17 );
72 $attacktype{ 'chaos' } = ( 1 << 18 );
73 $attacktype{ 'counterspell' } = ( 1 << 19 );
74 $attacktype{ 'godpower' } = ( 1 << 20 );
75 $attacktype{ 'holyword' } = ( 1 << 21 );
76 $attacktype{ 'blind' } = ( 1 << 22 );
77 $attacktype{ 'internal' } = ( 1 << 23 );
78 $attacktype{ 'lifestealing' } = ( 1 << 24 );
79 $attacktype{ 'disease' } = ( 1 << 25 );
80
81 &info("writing ...$archetypes");
82 open(ARCH,">".$archetypes) || &my_die("cannot open ".$archetypes);
83
84 binmode(ARCH);
85 &archsOut($root);
86 close(ARCH);
87
88
89 if (!$archonly) {
90 &info("$paths");
91 open(BMAPS,">".$paths) || &my_die("cannot open ".$paths);
92 binmode(BMAPS);
93 &pathsOut;
94 close(BMAPS);
95
96 &info("$faces");
97 open(FACES,">".$faces) || &my_die("cannot open ".$faces);
98 binmode(FACES);
99 &facesOut;
100 close(FACES);
101
102 &info("$smooths");
103 open(SMOOTHS,">".$smooths) || &my_die("cannot open ".$smooths);
104 binmode(SMOOTHS);
105 &smoothOut;
106 close(SMOOTHS);
107
108 &info("$treasures");
109 # We still support the old consolidated treasure information
110 # so copy it over.
111 open(TREASURES,">".$treasures) || &my_die("cannot open ".$treasures);
112 binmode(TREASURES);
113 print TREASURES "#
114 # Do not modify this file - any changes will get overwritten.
115 # instead, modify the .trs file in the arch directory.
116 #
117 ";
118 &treasuresOut;
119 close(TREASURES);
120
121 &info("$animations");
122 open(ANIM,">".$animations) || &my_die("cannot open ".$animations);
123 binmode(ANIM);
124 &animOut;
125 close(ANIM);
126 }
127
128
129 &stats;
130 rmdir("collect.lock");
131 exit 0;
132
133 sub traverse {
134 local($dir) = shift;
135 local($file,$name);
136 local( $tfile);
137
138 opendir(THISDIR, $dir) || my_die("couldn't open $dir");
139 local(@allfiles) = readdir(THISDIR);
140 closedir(THISDIR);
141
142 foreach $tfile (sort @allfiles) {
143 next if $tfile =~ /^\./;
144 next if $tfile =~ /~$/;
145 $file = $dir."/".$tfile;
146 $name = &basename($file,""); # DIR
147
148 if( -d $file && $name ne "dev" && $name ne "trashbin" && $name ne ".svn" ) {
149 &traverse($file);
150 } elsif ( -d $file && ( $name eq "dev" || $name eq "trashbin" ) ) {
151 # Empty directive to prevent warnings below
152 } elsif( $file =~ /.*\.arc$/) { # ARCHETYPE
153 $archsNum++;
154 push(@archs,$file);n
155 } elsif( $name =~ /(\S+)\.base($faceExt)\.png$/) { # FACE
156 $facesNum++;
157 $im_name = "$1$2";
158 &warn("duplicate face $im_name in ".$dir." and $faces{$im_name}")
159 if $faces{$im_name};
160 $faces{$im_name} = $dir."/".$im_name;
161
162 } elsif ( $file =~ /.*\.face$/) { # Face information file
163 $facesFileNum++;
164 push(@face_files, $file);
165 } elsif ( $file =~ /.*\.trs$/) { # Treasure information file
166 push(@treasure_files, $file);
167 }
168 elsif ( $file =~ /\.png$/ || $file =~ /\.xpm$/ || $file =~ /\.xcf/ || $file =~ /\.doc$/ || $file =~ /\.txt$/ || $file =~ /$faceExt$/ || $file =~ /\.blend$/ ) {
169 # we cover many files we probably shouldn't, but oh well.
170 # we just don't want complaints about all of these.
171 }
172 # ignore a couple of the more common 'junk' files that are not
173 # really junk.
174 elsif (($name ne "README") && ($name ne "ChangeLog") && ($name ne "TODO") && ($name ne ".svn") && ($name ne "artifacts") && ($name ne "attackmess") && ($name ne "formulae") && ($name ne "image_info") && ($name ne "materials") && ($name ne "messages") && ($name ne "races")) {
175 $trashNum++;
176 print "Warning: $file might be a junk file\n";
177 }
178 }
179 }
180
181 sub storeFaceInfo {
182 local($lface,@values) = @_;
183
184 if ($values[0] ne "") {
185 # blank.111 is a special case -
186 # since no foreground pixels will actually be drawn, foreground colors is
187 # not relevant. Several monsters use blank.111 as part of their
188 # animation to make them appear invisible, but have some other
189 # foreground color set.
190 # Same applies to empty also.
191 if ($fg{$lface} && $fg{$lface} ne $values[0] && $lface ne "blank.111"
192 && $lface ne "empty.111") {
193 &warn($arch." duplicate fg color ".$fg{$lface}."/".$values[0]." face ".$lface);
194 } else {
195 $fg{$lface} = $values[0];
196 }
197 }
198 if ($values[1] ne "" && $lface ne "blank.111" && $lface ne "empty.111") {
199 # blank.111 is a special case - see above explanation
200 # Its visibility is always 0.
201 if ($visibility{$lface} && $visibility{$lface} ne $values[1]) {
202 &warn($arch." duplicate visibilty ".$visibility{$lface}."/".$values[1]." face ".$lface);
203 } else {
204 $visibility{$lface} = $values[1];
205 }
206 }
207 if ($values[2] ne "" && lface ne "blank.111" && $lface ne "empty.111") {
208 if ($magicmap{$lface} && $magicmap{$lface} ne $values[2]) {
209 &warn($arch." duplicate magicmap color ".$magicmap{$lface}."/".$values[2]." face ".$lface);
210 } else {
211 $magicmap{$lface} = $values[2];
212 }
213 }
214 if ($values[3] ne "") {
215 if ($floor{$lface} && $floor{$lface} ne $values[3]) {
216 &warn($arch." duplicate floor information ".$floor{$lface}."/".$values[3]." face ".$lface);
217 } else {
218 $floor{$lface} = $values[3];
219 }
220 }
221 }
222
223
224 sub archsOut {
225 local($dir) = shift;
226
227 foreach $arch (@archs) {
228 # Assume the filename $arch begins with $dir. Assign all path name
229 # components after $dir to $pathto.
230 if($arch =~ /^\Q$dir\E\/(.*)\/[^\/]*[.]arc$/) {
231 $pathto = $1;
232 } else {
233 &warn("cannot determine editor_folder from arch '$arch'");
234 $pathto = "";
235 }
236 open(ARC,$arch) || &my_die("cannot open ".$arch);
237 line: while(<ARC>) {
238 chop;
239 ($var,@values) = split;
240 if ($var eq "#") {
241 #developper comment, switch to next line
242 $commentNum++;
243 next line;
244 }
245 if ($var eq "Object") {
246 $lface[0] = "";
247 $#lface = 0;
248 $lfg = "";
249 $lvis = "";
250 $mm = "";
251 $floor = "";
252 $moveon = 0;
253 $nopick = 0;
254 $arch = join "_", @values;
255 $print_editor_folder = 1;
256 } else {
257 $print_editor_folder = 0;
258 }
259 if ($var eq "end") {
260 if ($#lface !=0) {
261 $#lface--;
262 foreach $face (@lface) {
263 &storeFaceInfo($face, $lfg, $lvis,$mm,$floor);
264 }
265 }
266 if ($moveon && !$nopick) {
267 &warn("File $arch has an object with move_on set which can be picked up\n");
268 }
269 }
270 # Process the color/face info now
271 if ($var eq "color_fg") {
272 $lfg = $values[0];
273 next line;
274 }
275 if ($var eq "visibility") {
276 $lvis = $values[0];
277 next line;
278 }
279 if ($var eq "magicmap") {
280 $mm = $values[0];
281 next line;
282 }
283 if ($var eq "attacktype") {
284 $at = 0;
285 foreach $t ( @values ) {
286 if ( $t =~ /^\d+$/ ) {
287 $at |= $t;
288 } else {
289 if ( defined( $attacktype{ $t } ) ) {
290 $at |= $attacktype{ $t };
291 } else {
292 &warn($arch . " has invalid attacktype " . $t);
293 }
294 }
295 }
296 $_ = $var . ' ' . $at;
297 }
298 if ($var eq "is_floor") {
299 $floor = $values[0];
300 # is_floor is also needed for archs, so let it pass
301 # through
302 }
303 elsif ($var eq "no_pick") {
304 $nopick = $values[0];
305 } elsif ($var eq "move_on") {
306 $moveon = 1;
307 }
308 elsif ($var eq "face") {
309 $lface[$#lface++] = $values[0]
310 }
311 if ($var eq "face" && ! $faces{$values[0]}) {
312 &warn($arch." is missing face ".$values[0])
313 }
314 if ($var eq "smoothface") {
315 if ($smoothing{$values[0]} && ($smoothing{$values[0]} ne $values[1])) {
316 &warn($arch." duplicate smoothface for ".$values[0].": ".$smoothing{$values[0]}." and ".$values[1]);
317 } elsif ( ($values[0] eq "") || ($values[1] eq "")) {
318 &warn ($arch." incomplete smoothface entry found: ".$values[0]." ".$values[1]);
319 } else {
320 $smoothing{$values[0]}=$values[1]
321 }
322 next line; #smoothface must be excluded from archetype file
323 }
324 print ARCH $_,"\n";
325 if ($print_editor_folder) {
326 print ARCH "editor_folder $pathto\n" if $pathto ne "";
327 }
328 }
329 close(ARC);
330 }
331 }
332
333 sub pline {
334 local($face) = shift;
335 print BMAPS sprintf("%05d",$idx++)," ",$face,"\n";
336 }
337
338 sub opline {
339 local($face) = shift;
340 print BMAPS sprintf("\\%05d",$idx++),"\t",$face,"\n";
341 }
342
343 sub pheader {
344 print BMAPS "# This file is generated by $0, do not edit\n";
345 }
346
347 sub pathsOut {
348 &pheader;
349 $idx = 0;
350 &opline($root."/system/bug.111");
351 foreach $face (sort(keys %faces)) {
352 &opline($faces{$face}) if $faces{$face} !~ /bug\.111/;
353 }
354 }
355
356 sub treasuresOut {
357 foreach $treasure (@treasure_files) {
358 open(TREAS, $treasure) || &my_die("cannot open ".$treasure);
359 while(<TREAS>) {
360 if (! /^\s*$/) {
361 print TREASURES $_;
362 }
363 }
364 close(FACE);
365 }
366 }
367
368 sub facesOut {
369 foreach $face (@face_files) {
370 open(FACE, $face) || &my_die("cannot open ".$face);
371 while(<FACE>) {
372 chop;
373 local ($var, @values) = split;
374 if ($var eq "face") {
375 $lface = $values[0];
376 $lfg = "";
377 $lvis = "";
378 $mm = "";
379 $floor = "";
380 }
381 elsif ($var eq "color_fg") {
382 $lfg = $values[0];
383 }
384 elsif ($var eq "visibility") {
385 $lvis = $values[0];
386 }
387 elsif ($var eq "magicmap") {
388 $mm = $values[0];
389 }
390 elsif ($var eq "is_floor") {
391 $floor = $values[0];
392 }
393 elsif ($var eq "end") {
394 &storeFaceInfo($lface, $lfg, $lvis, $mm, $floor);
395 }
396 elsif ($var eq "animation") {
397 $animation=$values[0];
398 if ($anim{$1}) {
399 &warn("$animation is a duplicate animation name");
400 $anim{$animation}="";
401 }
402 while (<FACE>) {
403 chomp;
404 $var = $_;
405 last if ($var =~ /^mina\s*$/);
406 if ($var !~ /^facings/ ) {
407 if (! $faces{$var}) {
408 &warn($arch." is missing face ".$var);
409 }
410 else {
411 $lface[$#lface++] = $var;
412 }
413 }
414 $anim{$animation} .= "$var\n";
415 }
416 next; # don't want the mina
417 }
418 }
419 close(FACE);
420 }
421 print FACES "# This file is generated by $0, do not edit\n";
422 foreach $face (sort(keys %faces)) {
423 if ($fg{$face} ne "" || $bg{$face} ne "" || $visibility{$face} ne "" ||
424 $magicmap{$face} ne "" || $floor{$face} ne "") {
425 print FACES "face ".$face."\n";
426 print FACES "color_fg ".$fg{$face}."\n"
427 if $fg{$face} ne "";
428 print FACES "visibility ".$visibility{$face}."\n"
429 if $visibility{$face} ne "";
430 print FACES "magicmap ".$magicmap{$face}."\n"
431 if $magicmap{$face} ne "";
432 print FACES "is_floor ".$floor{$face}."\n"
433 if $floor{$face} ne "";
434 print FACES "end\n";
435 }
436 }
437 }
438
439 sub animOut {
440 foreach $anim (sort keys %anim) {
441 print ANIM "anim $anim\n$anim{$anim}mina\n";
442 $animationsNum++;
443 }
444 }
445 sub smoothOut {
446 local ($sm);
447 print SMOOTHS "##########################################################\n";
448 print SMOOTHS "# Do not touch this file. #\n";
449 print SMOOTHS "# It has been generated from the informations present #\n";
450 print SMOOTHS "# in the archetype files. #\n";
451 print SMOOTHS "# To add new entries, simply add #\n";
452 print SMOOTHS "# smoothface xxx yyy #\n";
453 print SMOOTHS "# to an archetype and collect.pl will put below an entry #\n";
454 print SMOOTHS "# xxx yyy #\n";
455 print SMOOTHS "##########################################################\n\n";
456 print SMOOTHS "\n# Data extracted from arch files\n";
457 foreach $sm (sort (keys %smoothing)) {
458 print SMOOTHS "$sm $smoothing{$sm}\n";
459 $smoothNum++;
460 }
461 }
462
463 ### print out statical information
464 sub stats {
465 &info(Archs.":\t".$archsNum);
466 &info(Images.":\t".$facesNum);
467 &info(Faces.":\t".$facesFileNum);
468 &info(Animations.":\t".$animationsNum);
469 &info(Treasures.":\t".($#treasure_files+1));
470 &info(Trash.":\t".$trashNum);
471 &info(Smooths.":\t".$smoothNum);
472 &info("Comment lines:\t".$commentNum);
473 }
474
475 # This is a simple function to clean up the collect lock
476 # and then call die.
477 sub my_die {
478 rmdir("collect.lock");
479 die(@_);
480 }