/** * Read and parse UNITSPOT.DAT * * @return double[][][] */ public static double[][][] readUnitSpot() { String file_name = FN.S_UNITSPOT_DAT; double[][][] unit_spot = new double[C.UNIT_SPOT_HEX][C.UNIT_SPOT_PLANET][]; int line_nr = 0; try (BufferedReader in = new BufferedReader(new FileReader(file_name))) { String s = in.readLine(); line_nr++; // System.out.println("s = " + s); // true if between { and } false if between } and { boolean read = false; int terrain_type = -1; // initialized to -1 on purpose int planet_type = 0; Pattern mark_begin = Pattern.compile("^\\{"); Pattern mark_end = Pattern.compile("^\\}"); Pattern comment = Pattern.compile("^//"); while (s != null) { Matcher matcher = comment.matcher(s); // if not comment if (!(matcher.find())) { // if between { and } if (read) { matcher = mark_end.matcher(s); // if found } at beginning of line if (matcher.find()) { read = false; // else read data } else { unit_spot[terrain_type][planet_type] = getCosts(s, file_name, line_nr); Util.debugPrint("Process record"); planet_type++; } // else between } and { } else { matcher = mark_begin.matcher(s); // if found { at beginning of line if (matcher.find()) { read = true; planet_type = 0; terrain_type++; // initialized to -1 // incorrect data file } else { throw new Exception(); } } } s = in.readLine(); line_nr++; } } catch (Exception e) { e.printStackTrace(System.out); System.out.println("Exception: " + e.getMessage()); System.out.println("Failed to read " + file_name); Util.logEx(null, e); Util.logFFErrorAndExit(file_name, line_nr, e); // CrashReporter.showCrashReport(e); } // printData(unit_spot); // System.exit(0); return unit_spot; }
/** * Parse a line of UNITSPOT.DAT * * @param s * @param vals * @param file_name * @param line_nr */ public static void processDoubleVals(String s, double[] vals, String file_name, int line_nr) { int start = 0; int state = 1; int index = 0; int counter = 0; boolean loop = true; try { while (loop) { switch (state) { case 1: if (s.charAt(index) == ' ') { } else if (s.charAt(index) >= '0' && s.charAt(index) <= '9') { start = index; state = 2; } else { throw new Exception(); } break; case 2: if (s.charAt(index) == '.') { state = 3; } else { throw new Exception(); } break; case 3: if (s.charAt(index) >= '0' && s.charAt(index) <= '9') { state = 4; } else { throw new Exception(); } break; case 4: if (counter == C.UNIT_SPOT_MOVE - 1) { if (s.charAt(index) == '"') { vals[counter++] = Double.parseDouble(s.substring(start, index)); loop = false; } else { throw new Exception(); } } else if (s.charAt(index) == ' ') { vals[counter++] = Double.parseDouble(s.substring(start, index)); state = 1; } else { throw new Exception(); } break; default: throw new AssertionError(); } index++; } } catch (Exception e) { Util.logEx(null, e); Util.logFFErrorAndExit(file_name, line_nr, e); // CrashReporter.showCrashReport(e); } }
/** * Read and parse TARGET.DAT. * * @return int[][] */ public static int[][] readTargetDat() { String file_name = FN.S_TARGET_DAT; int[][] target_dat = new int[C.TARGET_DAT_Y][]; int line_nr = 0; try (BufferedReader in = new BufferedReader(new FileReader(file_name))) { String s = in.readLine(); line_nr++; // System.exit(0); // true if between { and } false if between } and { boolean read = false; int index = 0; Pattern mark_begin = Pattern.compile("^\\{"); Pattern mark_end = Pattern.compile("^\\}"); Pattern comment = Pattern.compile("^//"); // Pattern reserved = Pattern.compile("Reserved"); while (s != null) { Matcher matcher = comment.matcher(s); // if not comment if (!(matcher.find())) { // if between { and } if (read) { matcher = mark_end.matcher(s); // if found } at beginning of line if (matcher.find()) { return target_dat; // else read data } else { // error (?) in Hyperion 1.4g TARGET.DAT requires this matcher = mark_begin.matcher(s); if (matcher.find()) { return target_dat; } // end error target_dat[index++] = getTarget(s); } // else between } and { } else { matcher = mark_begin.matcher(s); // if found { at beginning of line if (matcher.find()) { read = true; } else { throw new Exception(); } } } s = in.readLine(); line_nr++; } } catch (Exception e) { e.printStackTrace(System.out); System.out.println("Exception: " + e.getMessage()); System.out.println("Failed to read " + file_name); Util.logEx(null, e); Util.logFFErrorAndExit(file_name, line_nr, e); // CrashReporter.showCrashReport(e); } return target_dat; }