Source code: jdstar/GDS2JDS.java
1 /*
2 * Copyright (C) 1999-2001 Max Gilead <gilead@linart.pl>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 package jdstar;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.BufferedInputStream;
23 import java.io.FileOutputStream;
24 import java.io.BufferedOutputStream;
25 import java.io.PrintStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import huf.io.FileFilter;
29 import huf.io.Tokenizer;
30
31 /**
32 * This is a converter that reads GDStar (http://www.tbp.mb.ca/~ademko/gnu/)
33 * level file and writes JDStar level file.
34 */
35 public class GDS2JDS
36 {
37 private GDS2JDS(String infile)
38 {
39 System.out.println("--------------------------------------------");
40 System.out.println("GDS2JDS - GDStar to JDStar level converter");
41 System.out.println("--------------------------------------------");
42 System.out.println();
43
44 convertFile(new File(infile));
45
46 System.out.println("Done.");
47 System.out.println();
48 }
49 private void convertFile(File fin)
50 {
51 System.out.println("Converting level "+ fin.getName());
52 int dotIdx = fin.getName().lastIndexOf('.');
53 String foutname = fin.getName().substring(0, dotIdx > 0 ? dotIdx : fin.getName().length() - 1)
54 +".ds";
55 BufferedInputStream bin = null;
56 try
57 {
58 bin = new BufferedInputStream(new FileInputStream(fin));
59 }
60 catch(FileNotFoundException fnfe)
61 {
62 System.out.println("Opening source file failed");
63 return; // skip it
64 }
65 BufferedOutputStream bout = null;
66 try
67 {
68 bout = new BufferedOutputStream(new FileOutputStream(new File(foutname)));
69 }
70 catch(FileNotFoundException fnfe)
71 {
72 System.out.println("Opening target file failed");
73 try
74 {
75 bin.close();
76 }
77 catch(IOException ioe)
78 {} // bummer!
79 return; // skip it
80 }
81 PrintStream out = new PrintStream(bout);
82
83 try
84 {
85 Tokenizer tok = new Tokenizer(bin);
86
87 String id = tok.getNextToken();
88 String v1 = tok.getNextToken();
89 String v2 = tok.getNextToken();
90 if (!"DSTARSTAGE".equals(id) || !"0".equals(v2))
91 {
92 out.println("INVALID SOURCE FILE IDENTIFIER");
93 bin.close();
94 bout.flush();
95 bout.close();
96 return;
97 }
98 int w = 0;
99 int h = 0;
100 int[][] board = null;
101
102 if ("1".equals(v1))
103 // version 1
104 {
105 System.out.println("DSTARSTAGE 1 0");
106 w = new Integer(tok.getNextToken()).intValue();
107 h = new Integer(tok.getNextToken()).intValue();
108 System.out.println(w +" "+ h);
109
110 board = new int[h][w];
111 for (int y = h - 1; y >= 0; y--)
112 for (int x = w - 1; x >= 0; x--)
113 board[y][x] = Board.EMPTY;
114
115 int x = 0;
116 int y = 0;
117 String what = null;
118 while((x = new Integer(tok.getNextToken()).intValue()) != -1)
119 {
120 y = new Integer(tok.getNextToken()).intValue();
121 what = tok.getNextToken();
122 System.out.println(x +" "+ y +" "+ what);
123 if ("wall".equals(what))
124 board[y][x] = Board.WALL;
125 else if ("food".equals(what))
126 board[y][x] = Board.ITEM;
127 else if ("player".equals(what))
128 board[y][x] = Board.PLAYER;
129 else if ("buddy".equals(what))
130 board[y][x] = Board.BUDDY;
131 else
132 System.out.println("^--------------- ERROR");
133 }
134 }
135 else
136 {
137 out.println("INVALID SOURCE FILE IDENTIFIER");
138 bin.close();
139 bout.flush();
140 bout.close();
141 return;
142 }
143
144 out.println(w +" "+ h);
145 out.println("NONE");
146 for (int y = 0; y < h; y++)
147 {
148 for (int x = 0; x < w; x++)
149 switch(board[y][x])
150 {
151 case Board.EMPTY: out.print(Board.EMPTY_CHAR); break;
152 case Board.WALL: out.print(Board.WALL_CHAR); break;
153 case Board.ITEM: out.print(Board.ITEM_CHAR); break;
154 case Board.PLAYER: out.print(Board.PLAYER_CHAR); break;
155 case Board.BUDDY: out.print(Board.BUDDY_CHAR); break;
156 }
157 out.println();
158 }
159 }
160 catch(IOException ioe)
161 {
162 // skip it, close files...
163 out.println("CONVERSION FAILED: DISK ERROR");
164 ioe.printStackTrace(out);
165 }
166 catch(NumberFormatException nfe)
167 {
168 // skip it, close files...
169 out.println("CONVERSION FAILED: INVALD NUMBER");
170 nfe.printStackTrace(out);
171 }
172 finally
173 {
174 try
175 {
176 bin.close();
177 }
178 catch(IOException ioe)
179 {}
180 try
181 {
182 bout.flush();
183 bout.close();
184 }
185 catch(IOException ioe)
186 {}
187 }
188 }
189
190 private static void usage()
191 {
192 System.out.println("--------------------------------------------");
193 System.out.println("GDS2JDS - GDStar to JDStar level converter");
194 System.out.println("--------------------------------------------");
195 System.out.println();
196 System.out.println("Usage: java jdstar.GDS2JDS <filename>");
197 System.out.println();
198 System.exit(1);
199 }
200
201 public static void main(String[] args)
202 {
203 if (args.length != 1)
204 usage();
205 new GDS2JDS(args[0]);
206 }
207 }
208