1#!/usr/bin/perl -w
2
3# Programmed by:	Jalal A. Elhusseini
4# Date:			DEC 5, 2003
5
6# Perl script to Convert a '.DEF' level definition file for the NJAM game
7# to a '.COOP' file used by NJAM.
8
9$ARGC = $#ARGV + 1; # Get number of arguments.
10
11$WORLD_HEADER = "";
12$LEVEL_END = "[EndEndEnd]";
13
14$Stop_Script      = 0;
15
16$Row = "";
17
18$Row_START = 0;
19$Row_END   = 23;
20$Col_START = 0;
21$Col_END   = 27;
22
23$WORLD_COUNT = 0;
24$WORLDS_MAX = 20;
25
26$BLOCK        = 0x0;
27$EMPTY        = 0x1;
28$GHOST_HOME   = 0x2;
29$DOOR         = 0x3;
30$POWER        = 0x4;
31$COOKIE       = 0x5;
32$FREEZE       = 0x6;
33$TRAP         = 0x7;
34$WARP         = 0x8;
35$INVISIBILITY = 0x9;
36
37$SYMBOLS      = "BEGDPCFTWI";
38
39%Elements = ();		#clear hash 1st
40%Elements = (	B	=>	0,
41		E	=>	1,
42		G	=>	2,
43		D	=>	3,
44		P	=>	4,
45		C	=>	5,
46		F	=>	6,
47		T	=>	7,
48		W	=>	8,
49		I	=>	9, );
50
51@Dead_world0 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
52@Dead_world1 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0);
53@Dead_world2 = (0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0);
54
55sub write_dead
56{
57 for ( $LOOP4 = $Row_START ; $LOOP4 <= $Row_END ; $LOOP4++ )
58 { printf D_OUT "%s", chr($_[$LOOP4]); }
59}
60
61sub gen_dead_worlds
62{
63 print "I'm in the DEAD zone\n";
64 for ( $LOOP2 = $WORLD_COUNT + 1 ; $LOOP2 <= $WORLDS_MAX  ; $LOOP2++ )
65 {
66  write_dead(@Dead_world0);
67  write_dead(@Dead_world1);
68  for ( $LOOP3 = 2 ; $LOOP3 <= 25 ; $LOOP3++ ) { write_dead(@Dead_world0); }
69  write_dead(@Dead_world2);
70  write_dead(@Dead_world0);
71 }
72}
73
74# ARGUMENT ERROR Message
75if ($ARGC < 2 )
76{
77	print "\n\n\n";
78	print "Usage: genCOOP INPUT_FILE (OUTPUT_FILE)\n";
79	print "Do NOT include the file extension on the command line (.DEF).\n";
80	print "Do include the .COOP or .DUEL part of the input file (filname.COOP or filename.DUEL).\n";
81	print "Default extention for Output file is .COOP or .DUEL (based on input file).\n";
82	print "If OUTPUT_FILE not entered then OUTPUT_FILE = INPUT_FILE'\n";
83	print "File names will be capitalized.\n";
84	print "\n\n\n";
85}
86
87# Open Input and output files
88if ($ARGC >= 1)
89{
90	$INPUT_FILE_TEMP = $ARGV[0];
91	$_ = $INPUT_FILE_TEMP; tr/a-z/A-Z/; $INPUT_FILE_TEMP = $_;
92	$INPUT_FILE = "$INPUT_FILE_TEMP".".DEF";
93	open (D_IN, $INPUT_FILE) || die "Sorry, I can't open $INPUT_FILE.\n";
94	print "Input File = $INPUT_FILE.\n";
95
96	if ( $ARGC == 1 ) { $OUTPUT_FILE = "$INPUT_FILE_TEMP" }
97	else
98	{ $OUTPUT_FILE = $ARGV[1];
99	  $_ = $OUTPUT_FILE; tr/a-z/A-Z/; $OUTPUT_FILE = $_;
100	}
101	open (D_OUT,">$OUTPUT_FILE") || die "Sorry, I can't create $OUTPUT_FILE.\n";
102	binmode D_OUT;
103	print "Output File = $OUTPUT_FILE.\n";
104
105# Start a loop based on a valid World header in the Input file [world name]
106	while ( $Stop_Script == 0 )
107	{
108	 $WORLD_HEADER = <D_IN>;
109	 chomp ($WORLD_HEADER);
110	 if ($WORLD_HEADER eq $LEVEL_END)
111	 {
112	  $Stop_Script = 1;
113	  print "End of file detected.\n";
114	  if ($WORLD_COUNT < $WORLDS_MAX) { gen_dead_worlds(); }
115	 }
116	 elsif ( index($WORLD_HEADER , "[") > -1 )	#valid header name?
117	 {
118	  $world = $WORLD_HEADER;
119	  print "New World = $world.\n";
120	  $WORLD_COUNT++;
121
122# Read a World from Input file into a temp 2D array
123          for ( $LOOP0 = $Row_START ; $LOOP0 <= $Row_END  ; $LOOP0++ )
124          {
125	   $Row = <D_IN>; chomp ($Row);
126	   @TEMP = split(/,/,$Row);
127	   $World[$LOOP0] = [@TEMP];
128#	   print "@TEMP\n";
129	  }
130
131# Check for undefined symbols in the world
132          for ( $LOOP0 = $Row_START ; $LOOP0 <= $Row_END  ; $LOOP0++ )
133          {
134	   for ( $LOOP1 = $Col_START ; $LOOP1 <= $Col_END  ; $LOOP1++ )
135	   {
136	    $test = index $SYMBOLS, $World[$LOOP0][$LOOP1];
137	    if  ( $test < 0 ) { $World[$LOOP0][$LOOP1] = "B"; }
138	   }
139	  }
140
141# Make sure the world boundary is a BLOCK type
142	  for ( $LOOP1 = $Col_START ; $LOOP1 <= $Col_END  ; $LOOP1++ )
143	  {
144	   $World[$Row_START][$LOOP1] = "B";	# Top Row
145	   $World[$Row_END][$LOOP1]   = "B";	# Bottom Row
146	  }
147          for ( $LOOP0 = ($Row_START+1) ; $LOOP0 <= ($Row_END-1)  ; $LOOP0++ )
148          {
149	   $World[$LOOP0][$Col_START] = "B";	# 1st Col
150	   $World[$LOOP0][$Col_END]   = "B";	# Last Col
151	  }
152
153# Convert the temp array from text to numbers
154          for ( $LOOP0 = $Row_START ; $LOOP0 <= $Row_END  ; $LOOP0++ )
155          {
156	   for ( $LOOP1 = $Col_START ; $LOOP1 <= $Col_END  ; $LOOP1++ )
157	   {
158	    $Old = $World[$LOOP0][$LOOP1];
159	    $World[$LOOP0][$LOOP1] = $Elements{$Old};
160	    $New = $World[$LOOP0][$LOOP1]; print "$New ";
161	   }
162	   print "\n";
163	  }
164	  print "----------------------------------------------------\n";
165	  print "Done with World: $world, Number: $WORLD_COUNT\n";
166	  print "----------------------------------------------------\n\n";
167
168# Write the World to the Output file in a .COOP compatible write foramt
169          for ( $LOOP0 = $Col_START ; $LOOP0 <= $Col_END  ; $LOOP0++ )
170          {
171	   for ( $LOOP1 = $Row_START ; $LOOP1 <= $Row_END  ; $LOOP1++ )
172	   { printf D_OUT "%s", chr($World[$LOOP1][$LOOP0]); }
173          }
174
175	 }	#valid header name?
176	 if ($WORLD_COUNT == $WORLDS_MAX)
177	 {
178	  $Stop_Script = 1;
179	  print "Only $WORLDS_MAX Worlds in a .COOP file are allowed.\n"
180	 }
181	}	#while ( $Stop_Script == 0)
182
183	close (D_IN);
184	close (D_OUT);
185}
186