Gridarta Editor
MultiPositionData.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.gameobject;
21 
22 import java.awt.Dimension;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.LineNumberReader;
28 import java.io.Reader;
29 import java.net.URL;
33 import net.sf.gridarta.utils.IOUtils;
35 import org.apache.log4j.Category;
36 import org.apache.log4j.Logger;
37 import org.jetbrains.annotations.NotNull;
38 
44 public class MultiPositionData {
45 
49  @NotNull
50  private static final Category LOG = Logger.getLogger(MultiPositionData.class);
51 
55  public static final int Y_DIM = 16;
56 
60  @NotNull
62 
66  @NotNull
68 
73  public MultiPositionData(@NotNull final IsoMapSquareInfo isoMapSquareInfo) {
74  this.isoMapSquareInfo = isoMapSquareInfo;
75  }
76 
83  public void load(@NotNull final ErrorView errorView, @NotNull final URL url) {
84  final ErrorViewCollector errorViewCollector = new ErrorViewCollector(errorView, url);
85  // read data file line by line, parsing numbers into the array
86  try {
87  try (InputStream inputStream = url.openStream()) {
88  try (Reader reader = new InputStreamReader(inputStream, IOUtils.MAP_ENCODING)) {
89  try (BufferedReader bufferedReader = new BufferedReader(reader)) {
90  try (LineNumberReader lnr = new LineNumberReader(bufferedReader)) {
91  int yp = 0; // y-index in the data array
92 
93  // read the whole file line by line
94  boolean hasErrors = false;
95  while (true) {
96  final String inputLine = lnr.readLine();
97  if (inputLine == null) {
98  break;
99  }
100  final String line = inputLine.trim();
101  if (line.isEmpty() || line.startsWith("#")) {
102  continue;
103  }
104  if (yp >= Y_DIM) {
105  errorViewCollector.addWarning(ErrorViewCategory.ARCHDEF_ENTRY_INVALID, lnr.getLineNumber(), "too many entries");
106  break;
107  }
108  final String[] numbers = StringUtils.PATTERN_SPACE.split(line);
109  if (numbers.length != 2) {
110  errorViewCollector.addError(ErrorViewCategory.ARCHDEF_ENTRY_INVALID, lnr.getLineNumber(), "syntax error");
111  hasErrors = true;
112  continue;
113  }
114  final Dimension d = new Dimension();
115  try {
116  // parse and store it as integer
117  d.width = Integer.parseInt(numbers[0]);
118  } catch (final NumberFormatException ignored) {
119  errorViewCollector.addError(ErrorViewCategory.ARCHDEF_ENTRY_INVALID, lnr.getLineNumber(), "invalid number: " + numbers[0]);
120  hasErrors = true;
121  continue;
122  }
123  try {
124  // parse and store it as integer
125  d.height = Integer.parseInt(numbers[1]);
126  } catch (final NumberFormatException ignored) {
127  errorViewCollector.addError(ErrorViewCategory.ARCHDEF_ENTRY_INVALID, lnr.getLineNumber(), "invalid number: " + numbers[1]);
128  hasErrors = true;
129  continue;
130  }
131  data[yp++] = new MultiPositionEntry(isoMapSquareInfo, d);
132  }
133 
134  // report if there haven't been enough rows in the file
135  if (yp < Y_DIM && !hasErrors) {
136  errorViewCollector.addWarning(ErrorViewCategory.ARCHDEF_ENTRY_INVALID, "missing " + (Y_DIM - yp) + " entries");
137  }
138 
139  // confirm load process
140  if (LOG.isInfoEnabled()) {
141  LOG.info("Loaded multi-part position data from '" + url + "'");
142  }
143  }
144  }
145  }
146  }
147  } catch (final IOException ex) {
148  errorViewCollector.addWarning(ErrorViewCategory.ARCHDEF_FILE_INVALID, ex.getMessage());
149  }
150  }
151 
161  public int getXOffset(final int shapeID, final int positionID) {
162  final MultiPositionEntry entry = data[shapeID];
163  return entry == null ? 0 : entry.getXOffset(positionID);
164  }
165 
175  public int getYOffset(final int shapeID, final int positionID) {
176  final MultiPositionEntry entry = data[shapeID];
177  return entry == null ? 0 : entry.getYOffset(positionID);
178  }
179 
185  public int getWidth(final int shapeID) {
186  final MultiPositionEntry entry = data[shapeID];
187  return entry == null ? 0 : entry.getWidth();
188  }
189 
195  public int getHeight(final int shapeID) {
196  final MultiPositionEntry entry = data[shapeID];
197  return entry == null ? 0 : entry.getHeight();
198  }
199 
200 }
MultiPositionData(@NotNull final IsoMapSquareInfo isoMapSquareInfo)
Creates a new instance.
Utility class for string manipulation.
int getWidth()
Returns the total width of a multi-square image.
int getYOffset(final int shapeID, final int positionID)
Calculate the y-offset from the topmost pixel of the big face image and the default y-position (The d...
Convenience class for adding messages to a ErrorView instance using a fixed category name...
static final String MAP_ENCODING
Encoding to use for maps and other data.
Definition: IOUtils.java:51
void addWarning(@NotNull final ErrorViewCategory category)
Adds a warning message.
static final int Y_DIM
Number of rows in the array.
static final Category LOG
The Logger for printing log messages.
static final Pattern PATTERN_SPACE
The pattern that matches a single space.
Defines possible error categories for ErrorView instances.
Interface for classes displaying error messages.
Definition: ErrorView.java:28
Base package of all Gridarta classes.
int getWidth(final int shapeID)
Returns the total width for a multi-square image.
Utility-class for Gridarta&#39;s I/O.
Definition: IOUtils.java:40
Encapsulated information about a multi-square image.
The MultiPositionData class stores an array of numbers which is required in order to calculate displa...
void load(@NotNull final ErrorView errorView, @NotNull final URL url)
Load the array-data from file.
final MultiPositionEntry [] data
Array with position data.
int getHeight()
Returns the total height of a multi-square image.
final IsoMapSquareInfo isoMapSquareInfo
The IsoMapSquareInfo to use.
int getXOffset(final int positionID)
Returns the x offset from the left-most pixel of the multi-square image and the default x position...
Provides information about isometric map squares.
int getYOffset(final int positionID)
Returns the y offset from the left-most pixel of the multi-square image and the default y position...
void addError(@NotNull final ErrorViewCategory category)
Adds an error message.
int getXOffset(final int shapeID, final int positionID)
Calculate the x-offset from the leftmost pixel of the big face image and the default x-position (The ...
int getHeight(final int shapeID)
Returns the total height for a multi-square image.