87 |
|
|
88 |
int lines = 0; |
int lines = 0; |
89 |
|
|
90 |
FileInputStream fis = new FileInputStream( templateFile ); |
try (FileInputStream fis = new FileInputStream( templateFile ); |
91 |
BufferedReader in = new BufferedReader( new InputStreamReader(fis) ); |
InputStreamReader isr = new InputStreamReader(fis); |
92 |
String line = in.readLine(); |
BufferedReader in = new BufferedReader( isr ) |
93 |
lines ++; |
) { |
|
|
|
|
if ( line.charAt(0) != '#') { |
|
|
throw new Exception( "Template: invalid start character" ); |
|
|
} |
|
|
|
|
|
line = line.replace('#', ' ').trim(); |
|
|
String dimensions[] = line.split(","); |
|
|
|
|
|
if ( dimensions.length != 3) { |
|
|
throw new Exception( "Template: invalid dimensions line" ); |
|
|
} |
|
|
|
|
|
int xsize = Integer.parseInt(dimensions[0]); |
|
|
int ysize = Integer.parseInt(dimensions[1]); |
|
|
int zsize = Integer.parseInt(dimensions[2]); |
|
|
|
|
|
Pair[][][] array = new Pair[xsize][ysize][zsize]; |
|
|
|
|
|
int x = 0; |
|
|
int y = 0; |
|
|
|
|
|
|
|
|
while ( (line = in.readLine() ) != null ) { |
|
|
lines++; |
|
|
|
|
|
line = line.trim(); |
|
|
if (line.length() == 0) |
|
|
continue; |
|
|
|
|
|
if (line.charAt(0) == '#') |
|
|
continue; |
|
94 |
|
|
95 |
if ( y >= ysize) { |
|
96 |
throw new Exception( "Template: found more levels in file than specified in header" ); |
String line = in.readLine(); |
97 |
|
lines ++; |
98 |
|
|
99 |
|
if ( line.charAt(0) != '#') { |
100 |
|
throw new Exception( "Template: invalid start character" ); |
101 |
} |
} |
102 |
|
|
103 |
String elements[] = line.split(","); |
line = line.replace('#', ' ').trim(); |
104 |
if (elements.length != zsize) { |
String dimensions[] = line.split(","); |
105 |
throw new Exception( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize ); |
|
106 |
|
if ( dimensions.length != 3) { |
107 |
|
throw new Exception( "Template: invalid dimensions line" ); |
108 |
} |
} |
109 |
|
|
110 |
for (int i=0; i<elements.length; i++) { |
int xsize = Integer.parseInt(dimensions[0]); |
111 |
int val = 0; |
int ysize = Integer.parseInt(dimensions[1]); |
112 |
int subval = 0; |
int zsize = Integer.parseInt(dimensions[2]); |
113 |
|
|
114 |
String element[] = elements[i].trim().split("\\."); |
Pair[][][] array = new Pair[xsize][ysize][zsize]; |
115 |
|
|
116 |
|
int x = 0; |
117 |
|
int y = 0; |
118 |
|
|
119 |
|
|
120 |
|
while ( (line = in.readLine() ) != null ) { |
121 |
|
lines++; |
122 |
|
|
123 |
|
line = line.trim(); |
124 |
|
if (line.length() == 0) |
125 |
|
continue; |
126 |
|
|
127 |
|
if (line.charAt(0) == '#') |
128 |
|
continue; |
129 |
|
|
130 |
try { |
if ( y >= ysize) { |
131 |
val = Integer.parseInt( element[0] ); |
throw new Exception( "Template: found more levels in file than specified in header" ); |
|
} catch (Exception e) { |
|
|
throw new Exception( "Template: invalid value on line " + lines + ": " + element[0] ); |
|
132 |
} |
} |
133 |
|
|
134 |
if (val < -1 || val>255) { |
String elements[] = line.split(","); |
135 |
throw new Exception( "Template: invalid value on line " + lines + ": " + val ); |
if (elements.length != zsize) { |
136 |
|
throw new Exception( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize ); |
137 |
} |
} |
138 |
|
|
139 |
if (element.length == 2) { |
for (int i=0; i<elements.length; i++) { |
140 |
|
int val = 0; |
141 |
|
int subval = 0; |
142 |
|
|
143 |
|
String element[] = elements[i].trim().split("\\."); |
144 |
|
|
145 |
|
|
146 |
try { |
try { |
147 |
subval = Integer.parseInt( element[1] ); |
val = Integer.parseInt( element[0] ); |
148 |
} catch (Exception e) { |
} catch (Exception e) { |
149 |
throw new Exception( "Template: invalid value on line " + lines + ": " + element[1] ); |
throw new Exception( "Template: invalid value on line " + lines + ": " + element[0] ); |
150 |
|
} |
151 |
|
|
152 |
|
if (val < -1 || val>255) { |
153 |
|
throw new Exception( "Template: invalid value on line " + lines + ": " + val ); |
154 |
} |
} |
155 |
|
|
156 |
|
if (element.length == 2) { |
157 |
|
try { |
158 |
|
subval = Integer.parseInt( element[1] ); |
159 |
|
} catch (Exception e) { |
160 |
|
throw new Exception( "Template: invalid value on line " + lines + ": " + element[1] ); |
161 |
|
} |
162 |
|
} |
163 |
|
|
164 |
|
array[x][y][i] = new Pair(val,subval); |
165 |
|
} |
166 |
|
|
167 |
|
x++; |
168 |
|
if (x >= xsize) { |
169 |
|
x = 0; |
170 |
|
y++; |
171 |
} |
} |
|
|
|
|
array[x][y][i] = new Pair(val,subval); |
|
172 |
} |
} |
173 |
|
|
174 |
x++; |
if (x != 0) { |
175 |
if (x >= xsize) { |
throw new Exception( "Template: not enough lines to complete the last level" ); |
|
x = 0; |
|
|
y++; |
|
176 |
} |
} |
177 |
|
|
178 |
|
if (y != ysize) { |
179 |
|
throw new Exception( "Template: not enough levels"); |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
return array; |
184 |
|
|
185 |
} |
} |
186 |
|
|
|
if (x != 0) { |
|
|
throw new Exception( "Template: not enough lines to complete the last level" ); |
|
|
} |
|
|
|
|
|
if (y != ysize) { |
|
|
throw new Exception( "Template: not enough levels"); |
|
|
} |
|
|
|
|
|
|
|
|
return array; |
|
|
|
|
187 |
} |
} |
188 |
|
|
189 |
void buildTemplate(Location loc, Pair[][][] template, int pass) { |
void buildTemplate(Location loc, Pair[][][] template, int pass) { |