View Javadoc
1 package org.argosfields.converter; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileReader; 6 import java.io.IOException; 7 import java.util.ArrayList; 8 import java.util.StringTokenizer; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.argosfields.battlefield.BattleField; 13 import org.argosfields.model.Tile; 14 import org.argosfields.model.Unit; 15 import org.argosfields.multi.Player; 16 import org.eclipse.jface.util.Assert; 17 18 /*** 19 * CFLevelConverter.java 20 * 21 * @author Xavier Cho 22 * @version $Revision: 1.4 $ $Date: 2004/04/17 18:21:01 $ 23 */ 24 public final class CFLevelConverter { 25 private static final int SECTION_MISSION = 0; 26 private static final int SECTION_MAP = 1; 27 private static final int SECTION_PLAYER = 2; 28 private static final int SECTION_UNIT = 3; 29 private static final int SECTION_BUILDING = 4; 30 private static final int SECTION_EVENT = 5; 31 private static final int SECTION_MESSAGES = 6; 32 33 private static Log log = LogFactory.getLog(CFLevelConverter.class); 34 35 private CFLevelConverter() { 36 } 37 38 public static BattleField convert(final File file) throws Exception { 39 BattleField field = null; 40 BufferedReader reader = null; 41 42 Assert.isNotNull(file); 43 44 if (log.isInfoEnabled()) { 45 log.info("Converting Crimson Fields level file... : "); 46 log.info(" - file : " + file.getAbsolutePath()); 47 } 48 49 try { 50 reader = new BufferedReader(new FileReader(file)); 51 52 //Map 53 String title = null; 54 int width = 0; 55 int height = 0; 56 int playerCount = 0; 57 58 //Tiles 59 int row = 0; 60 61 //Units 62 int player = 0; 63 int xpos = 0; 64 int ypos = 0; 65 String unitName = null; 66 67 String line = null; 68 int section = SECTION_MISSION; 69 70 while ((line = reader.readLine()) != null) { 71 if (line.startsWith("[mission]")) { 72 section = SECTION_MISSION; 73 } else if (line.startsWith("[map-raw]")) { 74 section = SECTION_MAP; 75 } else if (line.startsWith("[player]")) { 76 section = SECTION_PLAYER; 77 } else if (line.startsWith("[unit]")) { 78 section = SECTION_UNIT; 79 } else if (line.startsWith("[building]")) { 80 section = SECTION_BUILDING; 81 } else if (line.startsWith("[event]")) { 82 section = SECTION_EVENT; 83 } else if (line.startsWith("[messages]")) { 84 section = SECTION_MESSAGES; 85 } else if (line.trim().length() > 0) { 86 switch (section) { 87 case SECTION_MISSION : 88 if (line.startsWith("title")) { 89 title = parseStringValue(line); 90 } else if (line.startsWith("players")) { 91 playerCount = parseIntValue(line); 92 } else if (line.startsWith("mapwidth")) { 93 width = parseIntValue(line); 94 } else if (line.startsWith("mapheight")) { 95 height = parseIntValue(line); 96 } 97 98 break; 99 case SECTION_MAP : 100 if (title == null 101 || width == 0 102 || height == 0 103 || playerCount == 0) { 104 105 String msg = 106 "Unable to read [mission] section data."; 107 108 throw new InvalidFileFormatException(msg); 109 } else if (row == 0) { 110 field = new BattleField(width, height); 111 112 field.setName(title); 113 field.setPlayerCount(playerCount); 114 } 115 116 int[] data = parseIntArrayValue(line); 117 118 for (int i = 0; i < width; i++) { 119 Tile tile = new Tile(data[i]); 120 field.setTile(i, row, tile); 121 } 122 123 row++; 124 125 break; 126 case SECTION_UNIT : 127 if (line.startsWith("type")) { 128 unitName = parseStringValue(line); 129 } else if (line.startsWith("player")) { 130 player = parseIntValue(line); 131 } else if (line.startsWith("xpos")) { 132 xpos = parseIntValue(line); 133 } else if (line.startsWith("ypos")) { 134 ypos = parseIntValue(line); 135 136 if (unitName != null && player > 0) { 137 Unit unit = Unit.forName(unitName); 138 unit.setPlayer(new Player(Integer.toString(player))); 139 140 field.setUnit(xpos, ypos, unit); 141 unit = null; 142 } 143 } 144 145 break; 146 default : 147 break; 148 } 149 } 150 } 151 } finally { 152 if (reader != null) { 153 try { 154 reader.close(); 155 } catch (IOException e) { 156 if (log.isWarnEnabled()) { 157 log.warn("Unable to close input stream."); 158 } 159 } 160 } 161 } 162 163 return field; 164 } 165 166 private static String parseStringValue(final String line) { 167 String value = line; 168 169 int index = value.indexOf("="); 170 171 if (index > 0) { 172 value = value.substring(index + 1); 173 } 174 175 return value.trim(); 176 } 177 178 private static int parseIntValue(final String line) { 179 return Integer.parseInt(parseStringValue(line)); 180 } 181 182 private static int[] parseIntArrayValue(final String line) { 183 ArrayList list = new ArrayList(100); 184 185 String value = parseStringValue(line); 186 187 StringTokenizer tokenizer = new StringTokenizer(line, ","); 188 while (tokenizer.hasMoreTokens()) { 189 list.add(tokenizer.nextToken()); 190 } 191 192 list.trimToSize(); 193 194 int size = list.size(); 195 int[] values = new int[size]; 196 197 for (int i = 0; i < size; i++) { 198 values[i] = Integer.parseInt((String) list.get(i)); 199 } 200 201 return values; 202 } 203 }

This page was automatically generated by Maven