-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathc4
More file actions
executable file
·3627 lines (3182 loc) · 103 KB
/
c4
File metadata and controls
executable file
·3627 lines (3182 loc) · 103 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
#
# c4: Chip's Challenge Combined Converter
#
# Use "perldoc c4" to read the documentation.
#
# Copyright (C) 2003-2006 Brian Raiter. This program is licensed under
# an MIT-style license. Please see the documentation for details.
#
use strict;
#
# First, some global functions used across packages.
#
package main;
# All the names of all the tiles.
#
my @tilenames;
my %tilenames;
foreach my $names
([ "empty", "floor" ],
[ "wall" ],
[ "ic chip", "computer chip" ],
[ "water" ],
[ "fire" ],
[ "hidden wall", "invisible wall permanent", "inv wall permanent" ],
[ "wall north", "partition north", "blocked north" ],
[ "wall west", "partition west", "blocked west" ],
[ "wall south", "partition south", "blocked south" ],
[ "wall east", "partition east", "blocked east" ],
[ "block", "moveable block", "movable block" ],
[ "dirt" ],
[ "ice" ],
[ "force floor south", "force south", "slide south",
"slide floor south" ],
[ "block north", "cloning block north" ],
[ "block west", "cloning block west" ],
[ "block south", "cloning block south" ],
[ "block east", "cloning block east" ],
[ "force floor north", "force north", "slide north",
"slide floor north" ],
[ "force floor east", "force east", "slide east", "slide floor east" ],
[ "force floor west", "force west", "slide west", "slide floor west" ],
[ "exit" ],
[ "blue door", "door blue" ],
[ "red door", "door red" ],
[ "green door", "door green" ],
[ "yellow door", "door yellow" ],
[ "ice wall southeast", "ice wall se", "ice se",
"ice corner southeast", "ice corner se" ],
[ "ice wall southwest", "ice wall sw", "ice sw",
"ice corner southwest", "ice corner sw" ],
[ "ice wall northwest", "ice wall nw", "ice nw",
"ice corner northwest", "ice corner nw" ],
[ "ice wall northeast", "ice wall ne", "ice ne",
"ice corner northeast", "ice corner ne" ],
[ "blue block floor", "blue block fake", "blue wall fake" ],
[ "blue block wall", "blue block real", "blue wall real" ],
[ "(combination)" ],
[ "thief", "spy" ],
[ "socket" ],
[ "green button", "button green", "toggle button", "button toggle" ],
[ "red button", "button red", "clone button", "button clone" ],
[ "toggle closed", "toggle wall closed", "closed toggle wall",
"toggle door closed", "closed toggle door" ],
[ "toggle open", "toggle wall open", "open toggle wall",
"toggle door open", "open toggle door" ],
[ "brown button", "button brown", "trap button", "button trap" ],
[ "blue button", "button blue", "tank button", "button tank" ],
[ "teleport" ],
[ "bomb" ],
[ "trap", "beartrap", "bear trap" ],
[ "invisible wall", "invisible wall temporary", "inv wall temporary" ],
[ "gravel" ],
[ "popup wall", "pass once" ],
[ "hint button" ],
[ "wall southeast", "partition southeast", "blocked southeast",
"wall se", "partition se", "blocked se" ],
[ "clone machine", "cloner", "cloning machine" ],
[ "force floor any", "force any", "slide any", "slide floor any",
"force floor random", "force random",
"slide random", "slide floor random",
"random slide floor" ],
[ "(chip drowned)" ],
[ "(chip burned)" ],
[ "(chip bombed)" ],
[ "(unused 1)" ],
[ "(unused 2)" ],
[ "(unused 3)" ],
[ "(exiting)" ],
[ "(exit 1)" ],
[ "(exit 2)" ],
[ "(chip swimming north)", "(chip swimming n)" ],
[ "(chip swimming west)", "(chip swimming w)" ],
[ "(chip swimming south)", "(chip swimming s)" ],
[ "(chip swimming east)", "(chip swimming e)" ],
[ "bug north", "bee north" ],
[ "bug west", "bee west" ],
[ "bug south", "bee south" ],
[ "bug east", "bee east" ],
[ "fireball north", "flame north" ],
[ "fireball west", "flame west" ],
[ "fireball south", "flame south" ],
[ "fireball east", "flame east" ],
[ "ball north" ],
[ "ball west" ],
[ "ball south" ],
[ "ball east" ],
[ "tank north" ],
[ "tank west" ],
[ "tank south" ],
[ "tank east" ],
[ "glider north", "ghost north" ],
[ "glider west", "ghost west" ],
[ "glider south", "ghost south" ],
[ "glider east", "ghost east" ],
[ "teeth north", "frog north" ],
[ "teeth west", "frog west" ],
[ "teeth south", "frog south" ],
[ "teeth east", "frog east" ],
[ "walker north", "dumbbell north" ],
[ "walker west", "dumbbell west" ],
[ "walker south", "dumbbell south" ],
[ "walker east", "dumbbell east" ],
[ "blob north" ],
[ "blob west" ],
[ "blob south" ],
[ "blob east" ],
[ "paramecium north", "centipede north" ],
[ "paramecium west", "centipede west" ],
[ "paramecium south", "centipede south" ],
[ "paramecium east", "centipede east" ],
[ "blue key", "key blue" ],
[ "red key", "key red" ],
[ "green key", "key green" ],
[ "yellow key", "key yellow" ],
[ "water boots", "boots water", "water shield", "flippers" ],
[ "fire boots", "boots fire", "fire shield" ],
[ "ice boots", "boots ice", "spike shoes", "spiked shoes",
"ice skates", "skates" ],
[ "force boots", "boots force", "slide boots", "boots slide",
"magnet", "suction boots" ],
[ "chip north" ],
[ "chip west" ],
[ "chip south" ],
[ "chip east" ])
{
push @tilenames, $names->[0];
@tilenames{@$names} = ($#tilenames) x @$names;
}
# The original 150 passwords.
#
my @origpasswords = @{
[qw(BDHP JXMJ ECBQ YMCJ TQKB WNLP FXQO NHAG
KCRE VUWS CNPE WVHI OCKS BTDY COZQ SKKK
AJMG HMJL MRHR KGFP UGRW WZIN HUVE UNIZ
PQGV YVYJ IGGZ UJDD QGOL BQZP RYMS PEFS
BQSN NQFI VDTM NXIS VQNK BIFA ICXY YWFH
GKWD LMFU UJDP TXHL OVPZ HDQJ LXPP JYSF
PPXI QBDH IGGJ PPHT CGNX ZMGC SJES FCJE
UBXU YBLT BLDM ZYVI RMOW TIGW GOHX IJPQ
UPUN ZIKZ GGJA RTDI NLLY GCCG LAJM EKFT
QCCR MKNH MJDV NMRH FHIC GRMO JINU EVUG
SCWF LLIO OVPJ UVEO LEBX FLHH YJYS WZYV
VCZO OLLM JPQG DTMI REKF EWCS BIFQ WVHY
IOCS TKWD XUVU QJXR RPIR VDDU PTAC KWNL
YNEG NXYB ECRE LIOC KZQR XBAO KRQJ NJLA
PTAS JWNL EGRW HXMF FPZT OSCW PHTY FLXP
BPYS SJUM YKZE TASX MYRT QRLD JMWZ FTLA
HEAN XHIZ FIRD ZYFA TIGG XPPH LYWO LUZL
HPPX LUJT VLHH SJUK MCJE UCRY OKOR GVXQ
YBLI JHEN COZA RGSK DIGW GNLP)]
};
# Return true if the given tile is one of the creatures, one of the
# blocks, or Chip.
#
sub iscreature($) { $_[0] >= 0x40 && $_[0] < 0x64 }
sub isblock($) { $_[0] == 0x0A || ($_[0] >= 0x0E && $_[0] < 0x12) }
sub ischip($) { $_[0] >= 0x6C && $_[0] < 0x70 }
my $filename = undef;
my $filepos = undef;
my $filelevel = undef;
sub err(@)
{
if (defined $filename) {
if (defined $filelevel) {
print STDERR "$filename: level $filelevel: ";
} elsif (defined $filepos) {
print STDERR "$filename, byte $filepos: ";
} elsif ($.) {
print STDERR "$filename:$.: ";
} else {
print STDERR "$filename: ";
}
} else {
if (defined $filelevel) {
print STDERR "$filename: level $filelevel: ";
} elsif (defined $filepos) {
print STDERR "byte $filepos: ";
} elsif ($.) {
print STDERR "line $.: ";
}
}
print STDERR @_, "\n";
return;
}
# Given a pack template, return the size of the packed data in bytes.
# The template is assumed to only contain the types a, C, v, and V.
#
sub packlen($)
{
my $template = shift;
my $size = 0;
while (length $template) {
my $char = substr $template, 0, 1, "";
my $n = $char eq "V" ? 4 : $char eq "v" ? 2 : 1;
$n *= $1 if $template =~ s/\A(\d+)//;
$size += $n;
}
return $size;
}
# Read a sequence of bytes from a binary file, according to a pack
# template. The unpacked values are returned.
#
sub fileread($$;\$@)
{
my $input = shift;
my $template = shift;
my $levelsize = shift;
my ($buf, $len);
$len = ::packlen $template;
return ::err "invalid template given to fileread" unless $len > 0;
my $ret = sysread $input, $buf, $len;
return ::err $! unless defined $ret;
return ::err "unexpected EOF" unless $ret;
$filepos ||= 0;
$filepos += $ret;
if (ref $levelsize) {
return ::err "invalid metadata in data file",
" (expecting $len bytes; found only $$levelsize)"
unless $len <= $$levelsize;
$$levelsize -= $len;
}
my (@fields) = (unpack $template, $buf);
foreach my $field (@fields) {
last unless @_;
my $min = shift;
my $max = shift;
return ::err "invalid data in data file"
if defined $min && $field < $min or defined $max && $field > $max;
}
return wantarray ? @fields : $fields[-1];
}
# Translate escape sequences in the given string.
#
sub unescape($)
{
local $_ = shift;
s/\\([0-7][0-7][0-7])/chr oct$1/eg;
s/\\([\\\"])/$1/g;
return $_;
}
sub escape($)
{
local $_ = shift;
s/([\\\"])/\\$1/g;
s/([^\020-\176])/sprintf"\\%03o",ord$1/eg;
return $_;
}
# Take a standard creature list from a dat file and augment it as
# necessary for a Lynx-based file format. This involves adding entries
# for Chip, blocks, immobile creatures, and creatures on clone
# machines.
#
sub makelynxcrlist($$)
{
my $map = shift;
my $datcreatures = shift;
my @crlist;
my @listed;
if (defined $datcreatures) {
foreach my $n (0 .. $#$datcreatures) {
$listed[$datcreatures->[$n][0]][$datcreatures->[$n][1]] = $n;
}
}
my $chip = undef;
foreach my $y (0 .. 31) {
foreach my $x (0 .. 31) {
my $obj = $map->[$y][$x][0];
next unless ::iscreature $obj || ::isblock $obj || ::ischip $obj;
my ($seq, $ff, $mobile) = (0, 0, 1);
if (::ischip $obj) {
return "multiple Chips present" if defined $chip;
$chip = @crlist;
} elsif (::isblock $obj) {
$mobile = -1 if $map->[$y][$x][1] == $tilenames{"cloner"};
} else {
if ($map->[$y][$x][1] == $tilenames{"cloner"}) {
$mobile = -1;
} else {
$mobile = defined $listed[$y][$x] ? 1 : 0;
}
$seq = $listed[$y][$x] + 1 if defined $listed[$y][$x];
}
push @crlist, [ $seq, $y, $x, $mobile ];
}
}
return "Chip absent" unless defined $chip;
return "over 128 creatures" if @crlist > 128;
($crlist[$chip], $crlist[0]) = ($crlist[0], $crlist[$chip]);
my @sortlist;
foreach my $n (0 .. $#crlist) { push @sortlist, $n if $crlist[$n][0] }
@sortlist = sort { $crlist[$a][0] <=> $crlist[$b][0] } @sortlist;
my @lynxcreatures;
foreach my $n (0 .. $#crlist) {
my $creature = $crlist[$n];
$creature = $crlist[shift @sortlist] if $creature->[0];
push @lynxcreatures, [ $creature->[1],
$creature->[2],
$creature->[3] ];
}
return \@lynxcreatures;
}
# Translate a creature list from a lynx-based file format to one
# appropriate for a dat-based file format.
#
sub makedatcrlist($$)
{
my $map = shift;
my $lynxcreatures = shift;
my @crlist;
return undef unless defined $lynxcreatures;
foreach my $creature (@$lynxcreatures) {
next if $creature->[2] != 1;
next if ::ischip $map->[$creature->[0]][$creature->[1]][0];
next if ::isblock $map->[$creature->[0]][$creature->[1]][0];
push @crlist, [ $creature->[0], $creature->[1] ];
}
return \@crlist;
}
#
# The textual source file format
#
package txtfile;
# The list of default tile symbols.
#
my %tilesymbols = %{{
" " => $tilenames{"empty"},
"#" => $tilenames{"wall"},
"\$" => $tilenames{"ic chip"},
"," => $tilenames{"water"},
"&" => $tilenames{"fire"},
"~" => $tilenames{"wall north"},
"|" => $tilenames{"wall west"},
"_" => $tilenames{"wall south"},
" |" => $tilenames{"wall east"},
"[]" => $tilenames{"block"},
"[" => $tilenames{"block"},
";" => $tilenames{"dirt"},
"=" => $tilenames{"ice"},
"v" => $tilenames{"force south"},
"^" => $tilenames{"force north"},
">" => $tilenames{"force east"},
"<" => $tilenames{"force west"},
"E" => $tilenames{"exit"},
"H" => $tilenames{"socket"},
"6" => $tilenames{"bomb"},
":" => $tilenames{"gravel"},
"?" => $tilenames{"hint button"},
"_|" => $tilenames{"wall southeast"},
"<>" => $tilenames{"force any"},
"@" => $tilenames{"chip south"},
"^]" => [ $tilenames{"cloning block north"}, $tilenames{"clone machine"} ],
"<]" => [ $tilenames{"cloning block west"}, $tilenames{"clone machine"} ],
"v]" => [ $tilenames{"cloning block south"}, $tilenames{"clone machine"} ],
">]" => [ $tilenames{"cloning block east"}, $tilenames{"clone machine"} ]
}};
#
#
#
# Error message display.
#
sub err(@) { warn "line $.: ", @_, "\n"; return; }
# The list of incomplete tile names recognized. Each incomplete name
# has a list of characters that complete them.
#
my %partialnames = %{{
"key" => { "blue key" => "b", "red key" => "r",
"green key" => "g", "yellow key" => "y" },
"door" => { "blue door" => "b", "red door" => "r",
"green door" => "g", "yellow door" => "y" },
"bug" => { "bug north" => "n", "bug west" => "w",
"bug south" => "s", "bug east" => "e" },
"bee" => { "bee north" => "n", "bee west" => "w",
"bee south" => "s", "bee east" => "e" },
"fireball" => { "fireball north" => "n", "fireball west" => "w",
"fireball south" => "s", "fireball east" => "e" },
"flame" => { "flame north" => "n", "flame west" => "w",
"flame south" => "s", "flame east" => "e" },
"ball" => { "ball north" => "n", "ball west" => "w",
"ball south" => "s", "ball east" => "e" },
"tank" => { "tank north" => "n", "tank west" => "w",
"tank south" => "s", "tank east" => "e" },
"glider" => { "glider north" => "n", "glider west" => "w",
"glider south" => "s", "glider east" => "e" },
"ghost" => { "ghost north" => "n", "ghost west" => "w",
"ghost south" => "s", "ghost east" => "e" },
"teeth" => { "teeth north" => "n", "teeth west" => "w",
"teeth south" => "s", "teeth east" => "e" },
"frog" => { "frog north" => "n", "frog west" => "w",
"frog south" => "s", "frog east" => "e" },
"walker" => { "walker north" => "n", "walker west" => "w",
"walker south" => "s", "walker east" => "e" },
"dumbbell" => { "dumbbell north" => "n", "dumbbell west" => "w",
"dumbbell south" => "s", "dumbbell east" => "e" },
"blob" => { "blob north" => "n", "blob west" => "w",
"blob south" => "s", "blob east" => "e" },
"paramecium"=> { "paramecium north" => "n", "paramecium west" => "w",
"paramecium south" => "s", "paramecium east" => "e" },
"centipede" => { "centipede north" => "n", "centipede west" => "w",
"centipede south" => "s", "centipede east" => "e" },
"chip" => { "chip north" => "n", "chip west" => "w",
"chip south" => "s", "chip east" => "e" },
"(swimming chip)"
=> { "(swimming chip north)" => "n",
"(swimming chip west)" => "w",
"(swimming chip south)" => "s",
"(swimming chip east)" => "e" }
}};
# The list of tile definitions that are defined throughout the set. A
# number of definitions are made by default at startup.
#
my %globaltiles = %tilesymbols;
# The list of tile definitions for a given level.
#
my %localtiles;
# Add a list of tile definitions to a hash.
#
sub addtiledefs(\%@)
{
my $tiledefs = shift;
while (my $def = shift) { $tiledefs->{$def->[0]} = $def->[1] }
}
# Given a string, return the tile with that name. If the name is not
# recognized, undef is returned and a error message is displayed.
#
sub lookuptilename($)
{
my $name = shift;
my $value = undef;
return $tilenames{$name} if exists $tilenames{$name};
if ($name =~ /^0x([0-9A-Fa-f][0-9A-Fa-f])$/) {
$value = hex $1;
return $value if $value >= 0 && $value <= 255;
}
my $n = length $name;
foreach my $key (keys %tilenames) {
if ($name eq substr $key, 0, $n) {
return ::err "ambiguous object id \"$name\""
if defined $value && $value != $tilenames{$key};
$value = $tilenames{$key};
}
}
return ::err "unknown object id \"$name\"" unless defined $value;
return $value;
}
# Given two characters, return the tile or pair of tiles which the
# characters represent. The characters can stand for a pair of tiles
# directly, or each character can independently represent one tile. In
# either case, a pair of tiles is returned as an array ref. A single
# tile is returned directly. If one or both characters are
# unrecognized, undef is returned and an error message is displayed.
#
sub lookuptile($);
sub lookuptile($)
{
my $symbol = shift;
$symbol =~ s/\A(.) \Z/$1/;
return $localtiles{$symbol} if exists $localtiles{$symbol};
return $globaltiles{$symbol} if exists $globaltiles{$symbol};
if (length($symbol) == 2) {
my $top = lookuptile substr $symbol, 0, 1;
if (defined $top && ref $top && $top->[1] < 0) {
return $top;
} elsif (defined $top && !ref $top) {
my $bot = lookuptile substr $symbol, 1, 1;
if (defined $bot && !ref $bot) {
return [ $top, $bot ];
}
}
}
return ::err "unrecognized map tile \"$symbol\"";
}
# Return the number of chips present on the map.
#
sub getchipcount($)
{
my $map = shift;
my $count = 0;
foreach my $y (0 .. 31) {
foreach my $x (0 .. 31) {
++$count if $map->[$y][$x][0] == 0x02;
++$count if $map->[$y][$x][1] == 0x02;
}
}
return $count;
}
# Given a completed map, return the default list of traps connections
# as an array ref. (The default list follows the original Lynx rules
# of connecting buttons to the first subsequent trap in reading
# order.)
#
sub buildtraplist($)
{
my $map = shift;
my $firsttrap = undef;
my @traps;
my @buttons;
foreach my $y (0 .. 31) {
foreach my $x (0 .. 31) {
if ($map->[$y][$x][0] == 0x27 || $map->[$y][$x][1] == 0x27) {
push @buttons, [ $y, $x ];
} elsif ($map->[$y][$x][0] == 0x2B || $map->[$y][$x][1] == 0x2B) {
push @traps, map { { from => $_, to => [ $y, $x ] } } @buttons;
undef @buttons;
$firsttrap = [ $y, $x ] unless defined $firsttrap;
}
}
}
push @traps, map { { from => $_, to => $firsttrap } } @buttons
if @buttons && defined $firsttrap;
return \@traps;
}
# Given a completed map, return the default list of clone machine
# connections as an array ref. (This function looks a lot like the
# prior one.)
#
sub buildclonerlist($)
{
my $map = shift;
my $firstcm = undef;
my @cms;
my @buttons;
foreach my $y (0 .. 31) {
foreach my $x (0 .. 31) {
if ($map->[$y][$x][0] == 0x24 || $map->[$y][$x][1] == 0x24) {
push @buttons, [ $y, $x ];
} elsif ($map->[$y][$x][0] == 0x31 || $map->[$y][$x][1] == 0x31) {
push @cms, map { { from => $_, to => [ $y, $x ] } } @buttons;
undef @buttons;
$firstcm = [ $y, $x ] unless defined $firstcm;
}
}
}
push @cms, map { { from => $_, to => $firstcm } } @buttons
if @buttons && defined $firstcm;
return \@cms;
}
# Given a completed map, return the default ordering of creatures as
# an array ref. (The default ordering is to first list the creatures
# in reading order, including Chip. Then, the first creature on the
# list swaps positions with Chip, who is then removed from the list.)
#
sub buildcreaturelist($$)
{
my $map = shift;
my $ruleset = shift;
my $chippos = undef;
my @crlist;
foreach my $y (0 .. 31) {
foreach my $x (0 .. 31) {
my $tile = $map->[$y][$x][0];
if (::iscreature $tile) {
push @crlist, [ $y, $x ];
} elsif (::isblock $tile) {
push @crlist, [ $y, $x, 0 ];
} elsif (::ischip $tile) {
$chippos = @crlist;
push @crlist, [ $y, $x, 0 ];
}
}
}
if ($ruleset eq "lynx") {
($crlist[0], $crlist[$chippos]) = ($crlist[$chippos], $crlist[0])
if $chippos;
foreach my $item (@crlist) { $#$item = 1 }
} else {
if (defined $chippos && $chippos > 1) {
my $cr = shift @crlist;
$crlist[$chippos - 1] = $cr;
}
for (my $n = $#crlist ; $n >= 0 ; --$n) {
splice @crlist, $n, 1 if $#{$crlist[$n]} > 1;
}
}
return \@crlist;
}
# Compare two arrays of lines of text. Wherever the same pair of
# characters appears in same place in both arrays, the occurrence in
# the first array is replaced with spaces.
#
sub subtracttext(\@\@)
{
my $array = shift;
my $remove = shift;
for (my $n = 0 ; $n < @$array && $n < @$remove ; ++$n) {
my $m = 0;
while ($m < length $array->[$n] && $m < length $remove->[$n]) {
my $a = substr $array->[$n], $m, 2;
my $b = substr $remove->[$n], $m, 2;
$a .= " " if length $a == 1;
$b .= " " if length $b == 1;
substr($array->[$n], $m, 2) = " " if $a eq $b;
$m += 2;
}
}
}
# Interpret a textual description of a section of the map. The
# interpreted map data is added to the map array passed as the first
# argument. The second and third arguments set the origin of the map
# section. The remaining arguments are the lines from the text file
# describing the map section. The return value is 1 if the
# interpretation is successful. If any part of the map sections cannot
# be understood, undef is returned and an error message is displayed.
#
sub parsemap($$$@)
{
my $map = shift;
my $y0 = shift;
my $x0 = shift;
return ::err "map extends below the 32nd row" if $y0 + @_ > 32;
for (my $y = $y0 ; @_ ; ++$y) {
my $row = shift;
return ::err "map extends beyond the 32nd column"
if $x0 + length($row) / 2 > 32;
for (my $x = $x0 ; length $row ; ++$x) {
my $cell = lookuptile substr $row, 0, 2;
return ::err "unrecognized tile at ($x $y)" unless defined $cell;
return unless defined $cell;
if (ref $cell) {
if ($cell->[1] < 0) {
$map->[$y][$x] = [ $cell, 0x00 ];
} else {
$map->[$y][$x] = $cell;
}
} else {
$map->[$y][$x] = [ $cell, 0x00 ];
}
substr($row, 0, 2) = "";
}
}
return 1;
}
# Interpret a textual overlay section. The first argument is the
# level's hash ref. The second and third arguments set the origin of
# the overlay section. The remaining arguments are the lines from the
# text file describing the overlay. The return value is 1 if the
# interpretation is successful. If any part of the overlay section
# cannot be understood, undef is returned and an error message is
# displayed.
#
sub parsecon($$$@)
{
my %symbols;
my $data = shift;
my $y0 = shift;
my $x0 = shift;
return ::err "overlay extends below the 32nd row" if $y0 + @_ > 32;
for (my $y = $y0 ; @_ ; ++$y) {
my $row = shift;
return ::err "overlay extends beyond the 32nd column"
if $x0 + length($row) / 2 > 32;
for (my $x = $x0 ; length $row ; ++$x) {
$_ = substr $row, 0, 1, "";
push @{$symbols{$_}}, [ $y, $x ] unless $_ eq " " || $_ eq "";
$_ = substr $row, 0, 1, "";
push @{$symbols{$_}}, [ $y, $x ] unless $_ eq " " || $_ eq "";
}
}
foreach my $symbol (sort keys %symbols) {
my $list = $symbols{$symbol};
if (@$list == 1) {
my ($y, $x) = ($list->[0][0], $list->[0][1]);
my $cell = $data->{map}[$y][$x];
return ::err "no creature under \"$symbol\" at ($x $y)"
unless defined $cell &&
(::iscreature $cell->[0] || ::iscreature $cell->[1]);
push @{$data->{creatures}}, [ $y, $x ];
} else {
my $linktype = undef;
my $to = undef;
my (@from, $type);
foreach my $pos (@$list) {
my ($y, $x) = ($pos->[0], $pos->[1]);
my $cell = $data->{map}[$y][$x];
my $obj = $cell->[1] || $cell->[0];
if ($obj == $tilenames{"red button"}) {
$type = "cloners";
push @from, [ $y, $x ];
} elsif ($obj == $tilenames{"brown button"}) {
$type = "traps";
push @from, [ $y, $x ];
} elsif ($obj == $tilenames{"clone machine"}) {
$type = "cloners";
return ::err "clone machine under \"$symbol\" at ($x $y) ",
"wired to non-button at ($to->[1] $to->[0])"
if defined $to;
$to = [ $y, $x ];
} elsif ($obj == $tilenames{"beartrap"}) {
$type = "traps";
return ::err "beartrap under \"$symbol\" at ($x $y) ",
"wired to non-button at ($to->[1] $to->[0])"
if defined $to;
$to = [ $y, $x ];
} else {
return ::err "no button/trap/clone machine ",
"under \"$symbol\" at ($x $y)";
}
$linktype ||= $type;
return ::err "inconsistent connection ",
"under \"$symbol\" at ($x $y)"
unless $linktype eq $type;
}
push @{$data->{$linktype}},
map { { from => $_, to => $to } } @from;
}
}
return 1;
}
# Interpret a tile definition. Given a line of text supplying the tile
# definition, the function returns an array ref. Each element in the
# array is a pair: the first element gives the character(s), and the
# second element supplies the tile(s). If the definition is ambiguous
# or invalid, undef is returned and an error message is displayed.
#
sub parsetiledef($)
{
my $def = shift;
$def =~ s/^(\S\S?)\t//
or return ::err "syntax error in tile defintion \"$def\"";
my $symbol = $1;
$def = lc $def;
$def =~ s/^\s+//;
$def =~ s/\s+$//;
if ($def =~ /^([^\+]*[^\+\s])\s*\+\s*([^\+\s][^\+]*)$/) {
my ($def1, $def2) = ($1, $2);
my ($tile1, $tile2);
$tile1 = lookuptilename $def1;
return unless defined $tile1;
if (lc $def2 eq "pos") {
return ::err "ordered tile definition \"$symbol\" ",
"must be a single character"
unless length($symbol) == 1;
$tile2 = -1;
} else {
$tile2 = lookuptilename $def2;
return unless defined $tile2;
}
return [ [ $symbol, [ $tile1, $tile2 ] ] ];
}
my @defs;
if (exists $partialnames{$def}) {
return ::err "incomplete tile definition \"$symbol\" ",
"must be a single character"
unless length($symbol) == 1;
foreach my $comp (keys %{$partialnames{$def}}) {
push @defs, [ $symbol . $partialnames{$def}{$comp},
$tilenames{$comp} ];
}
return \@defs;
}
my $tile = lookuptilename $def;
return [ [ $symbol, $tile ] ] if defined $tile;
return;
}
# Given a handle to a text file, read the introductory lines that
# precede the first level definition, if any, and return a hash ref
# for storing the level set. If an error occurs, undef is returned and
# an error message is displayed.
#
sub parseheader($)
{
my $input = shift;
my $data = { ruleset => "lynx" };
my $slurpingdefs = undef;
local $_;
while (<$input>) {
chomp;
if (defined $slurpingdefs) {
if (/^\s*[Ee][Nn][Dd]\s*$/) {
undef $slurpingdefs;
} else {
my $def = parsetiledef $_;
return unless $def;
addtiledefs %globaltiles, @$def;
}
next;
} elsif (/^\s*[Tt][Ii][Ll][Ee][Ss]\s*$/) {
$slurpingdefs = 1;
next;
}
last if /^%%%$/;
next if /^\s*$/ || /^%/;
/^\s*(\S+)\s+(\S(?:.*\S)?)\s*$/ or return ::err "syntax error";
my ($name, $value) = ($1, $2);
$name = lc $name;
if ($name eq "ruleset") {
$value = lc $value;
return ::err "invalid ruleset \"$value\""
unless $value =~ /^(lynx|ms)$/;
$data->{ruleset} = $value;
} elsif ($name eq "maxlevel") {
return ::err "invalid maximum level \"$value\""
unless $value =~ /\A\d+\Z/ && $value < 65536;
$data->{maxlevel} = $value;
} else {
return ::err "invalid statement \"$name\"";
}
}
return ::err "unclosed definition section" if $slurpingdefs;
return $data;
}
# Given a handle to a text file, positioned at the start of a level
# description, parse the lines describing the level and return a hash
# ref containing the level data. If the end of the file is encountered
# before a level description is found, false is returned. If any
# errors are encountered, undef is returned and an error message is
# displayed.
#
sub parselevel($$$)
{
my $input = shift;
my $ruleset = shift;
my $number = shift;
my %data = (number => $number, leveltime => 0);
my $seenanything = undef;
my $slurpingdefs = undef;
my $slurpingmap = undef;
my @maptext;
local $_;
$data{passwd} = $origpasswords[$number - 1]
if $number >= 1 && $number <= 150;
for my $y (0 .. 31) {
for my $x (0 .. 31) { $data{map}[$y][$x] = [ 0, 0 ] }
}
undef %localtiles;
while (<$input>) {
chomp;
if (defined $slurpingdefs) {
if (/^\s*[Ee][Nn][Dd]\s*$/) {
undef $slurpingdefs;
} else {
my $def = parsetiledef $_;
return unless $def;
addtiledefs %localtiles, @$def;
}
next;
} elsif (defined $slurpingmap) {
if (/^\s*([AEae])[Nn][Dd]\s*$/) {
my $overlay = lc($1) eq "a";
if ($slurpingmap->[2] >= 0) {
my @overlaytext = splice @maptext, $slurpingmap->[2];
return ::err "overlay section is taller than map section"
if @overlaytext > @maptext;
subtracttext @overlaytext, @maptext;
return unless parsecon \%data,
$slurpingmap->[0],
$slurpingmap->[1],
@overlaytext;
} else {
$slurpingmap->[2] = @maptext;
return unless parsemap $data{map},
$slurpingmap->[0],
$slurpingmap->[1],
@maptext;
}
unless ($overlay) {
undef $slurpingmap;
undef @maptext;
}
} else {
1 while s{^([^\t]*)\t}{$1 . (" " x (8 - length($1) % 8))}e;
push @maptext, $_;
}
next;
} elsif (/^\s*[Tt][Ii][Ll][Ee][Ss]\s*$/) {
$slurpingdefs = 1;
next;
} elsif (/^\s*[Mm][Aa][Pp]\s*(?:(\d+)\s+(\d+)\s*)?$/) {
$slurpingmap = [ $2 || 0, $1 || 0, -1 ];
next;
} elsif (/^\s*[Mm][Aa][Pp]/) {
return ::err "invalid syntax following \"map\"";
} elsif (/^\s*[Tt][Rr][Aa][Pp][Ss]\s*$/) {
$data{traps} ||= [ ];
next;
} elsif (/^\s*[Cc][Ll][Oo][Nn][Ee][Rr][Ss]\s*$/) {
$data{cloners} ||= [ ];
next;
} elsif (/^\s*[Cc][Rr][Ee][Aa][Tt][Uu][Rr][Ee][Ss]\s*$/) {
$data{creatures} ||= [ ];
next;
}
last if /^%%%$/;
next if /^\s*$/ || /^%/;
$seenanything = 1;
/^\s*(\S+)\s+(\S(?:.*\S)?)\s*$/ or return ::err "syntax error";
my ($name, $value) = ($1, $2);
$name = lc $name;
if ($name eq "level") {
return ::err "invalid level number \"$value\""
unless $value =~ /\A\d+\Z/ && $value < 65536;
$data{number} = $value;
} elsif ($name eq "time") {
return ::err "invalid level time \"$value\""
unless $value =~ /\A\d+\Z/ && $value < 65536;
$data{leveltime} = $value;
} elsif ($name eq "chips") {
return ::err "invalid chip count \"$value\""
unless $value =~ /\A\d+\Z/ && $value < 65536;
$data{chips} = $value;
} elsif ($name eq "title" || $name eq "name") {
$value = ::unescape $value if $value =~ s/\A\"(.*)\"\Z/$1/;
$data{title} .= " " if defined $data{title};
$data{title} .= $value;
} elsif ($name eq "password" || $name eq "passwd") {
return ::err "invalid password \"$value\""
unless $value =~ /\A[A-Z][A-Z][A-Z][A-Z]\Z/;
$data{passwd} = $value;
} elsif ($name eq "hint") {
$value = ::unescape $value if $value =~ s/\A\"(.*)\"\Z/$1/;
$data{hint} .= " " if defined $data{hint};
$data{hint} .= $value;