Gridarta Editor
MapLocation.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.maplocation;
21 
22 import java.awt.Point;
23 import java.util.regex.Pattern;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
42 public class MapLocation implements Comparable<MapLocation> {
43 
47  @NotNull
48  private static final Pattern PATTERN_END_OF_LINE = Pattern.compile("[\r\n]+");
49 
53  @NotNull
54  private final MapPath mapPath;
55 
59  @NotNull
60  private final Point mapCoordinate;
61 
68  private MapLocation(@NotNull final MapPath mapPath, final int mapXCoordinate, final int mapYCoordinate) {
69  this.mapPath = mapPath;
70  mapCoordinate = new Point(mapXCoordinate, mapYCoordinate);
71  }
72 
80  public MapLocation(@NotNull final BaseObject<?, ?, ?, ?> gameObject, final boolean allowRandomMapParameters) throws NoExitPathException {
81  this(getMapPath(gameObject, allowRandomMapParameters), getMapX(gameObject), getMapY(gameObject));
82  }
83 
93  @NotNull
94  public static MapLocation newAbsoluteMapLocation(@NotNull final GameObject<?, ?, ?> gameObject, final boolean allowRandomMapParameters) throws NoExitPathException {
95  final MapPath mapPath = getMapPath(gameObject, allowRandomMapParameters);
96  final AbsoluteMapPath baseMapPath;
97  final MapSquare<?, ?, ?> mapSquare = gameObject.getMapSquare();
98  if (mapSquare == null) {
99  baseMapPath = new AbsoluteMapPath();
100  } else {
101  final MapModel<?, ?, ?> mapModel = mapSquare.getMapModel();
102  final MapFile mapFile = mapModel.getMapFile();
103  if (mapFile == null) {
104  baseMapPath = new AbsoluteMapPath();
105  } else {
106  baseMapPath = mapFile.getMapPath();
107  }
108  }
109  final AbsoluteMapPath canonicalMapPath = MapPathUtils.append(baseMapPath, mapPath);
110  final int mapX = getMapX(gameObject);
111  final int mapY = getMapY(gameObject);
112  return new MapLocation(canonicalMapPath, mapX, mapY);
113  }
114 
119  @NotNull
120  public MapPath getMapPath() {
121  return mapPath;
122  }
123 
128  @NotNull
129  public Point getMapCoordinate() {
130  return new Point(mapCoordinate);
131  }
132 
133  @Override
134  public boolean equals(@Nullable final Object obj) {
135  if (obj == this) {
136  return true;
137  }
138  if (obj == null || obj.getClass() != getClass()) {
139  return false;
140  }
141  final MapLocation mapLocation = (MapLocation) obj;
142  return mapLocation.mapPath.equals(mapPath) && mapLocation.mapCoordinate.equals(mapCoordinate);
143  }
144 
145  @Override
146  public int hashCode() {
147  return mapPath.hashCode() ^ mapCoordinate.hashCode();
148  }
149 
155  private static int getMapY(@NotNull final Attributes gameObject) {
156  return gameObject.getAttributeInt(BaseObject.SP);
157  }
158 
164  private static int getMapX(@NotNull final Attributes gameObject) {
165  return gameObject.getAttributeInt(BaseObject.HP);
166  }
167 
175  @NotNull
176  public static MapPath getMapPath(@NotNull final BaseObject<?, ?, ?, ?> gameObject, final boolean allowRandomMapParameters) throws NoExitPathException {
177  String path = gameObject.getAttributeString(BaseObject.SLAYING);
178  if (path.isEmpty()) {
179  throw new NoExitPathException(gameObject);
180  }
181 
182  if (allowRandomMapParameters && (path.equals("/!") || path.startsWith("/random/"))) {
183  // destination is a random map; extract the final non-random map
184  path = getRandomMapParameter(gameObject, "final_map");
185  if (path == null) {
186  throw new NoExitPathException(gameObject);
187  }
188  }
189 
190  return MapPathUtils.newMapPath(path);
191  }
192 
200  @Nullable
201  private static String getRandomMapParameter(@NotNull final BaseObject<?, ?, ?, ?> gameObject, @NotNull final String parameterName) {
202  final String msg = gameObject.getMsgText();
203  if (msg == null) {
204  return null;
205  }
206 
207  final String[] lines = PATTERN_END_OF_LINE.split(msg);
208  for (final String line : lines) {
209  final String[] tmp = StringUtils.PATTERN_SPACES.split(line, 2);
210  if (tmp.length == 2 && tmp[0].equals(parameterName)) {
211  return tmp[1];
212  }
213  }
214 
215  return null;
216  }
217 
218  @NotNull
219  @Override
220  public String toString() {
221  return mapCoordinate.x + "/" + mapCoordinate.y + "@" + mapPath;
222  }
223 
224  @Override
225  @SuppressWarnings("CompareToUsesNonFinalVariable")
226  public int compareTo(@NotNull final MapLocation o) {
227  final int cmp = mapPath.toString().compareTo(o.mapPath.toString());
228  if (cmp != 0) {
229  return cmp;
230  }
231  if (mapCoordinate.x < o.mapCoordinate.x) {
232  return -1;
233  }
234  if (mapCoordinate.x > o.mapCoordinate.x) {
235  return -1;
236  }
237  if (mapCoordinate.y < o.mapCoordinate.y) {
238  return -1;
239  }
240  if (mapCoordinate.y > o.mapCoordinate.y) {
241  return -1;
242  }
243  return 0;
244  }
245 
246 }
Utility class for string manipulation.
static MapPath newMapPath(@NotNull final String string)
Creates a MapPath instance from string representation.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
Exception thrown if a game object does not specify a valid exit path.
MapModel< G, A, R > getMapModel()
Returns the MapModel this map square is part of.
Definition: MapSquare.java:99
A MapPath that is absolute, that is, it starts with a "/".
AbsoluteMapPath getMapPath()
Returns the map path within getMapsDir().
Definition: MapFile.java:93
int compareTo(@NotNull final MapLocation o)
Represents a maps directory local map path.
Definition: MapPath.java:31
static final Pattern PATTERN_SPACES
The pattern that matches a non-empty sequence of spaces.
String SLAYING
The name of the "slaying" attribute.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
MapPath getMapPath()
Returns the map path.
String HP
The attribute name of the "hp" attribute.
Definition: BaseObject.java:90
GameObjects are the objects based on Archetypes found on maps.
final Point mapCoordinate
The map coordinate.
Utility class for MapPath related functions.
static int getMapY(@NotNull final Attributes gameObject)
Returns the exit x coordinate of a BaseObject.
static String getRandomMapParameter(@NotNull final BaseObject<?, ?, ?, ?> gameObject, @NotNull final String parameterName)
Extracts a parameter value for an exit to a random map.
MapLocation(@NotNull final BaseObject<?, ?, ?, ?> gameObject, final boolean allowRandomMapParameters)
Creates a new instance from a BaseObject instance.
static MapLocation newAbsoluteMapLocation(@NotNull final GameObject<?, ?, ?> gameObject, final boolean allowRandomMapParameters)
Creates a new instance from a BaseObject instance.
MapFile getMapFile()
Returns the map file.
String SP
The attribute name of the "sp" attribute.
Definition: BaseObject.java:96
Represents a location on a map consisting of a map path and a map coordinate.
static AbsoluteMapPath append(@NotNull final AbsoluteMapPath baseMapPath, @NotNull final MapPath mapPath)
Appends a map path to another map path.
static MapPath getMapPath(@NotNull final BaseObject<?, ?, ?, ?> gameObject, final boolean allowRandomMapParameters)
Returns the exit map path of a BaseObject.
Point getMapCoordinate()
Returns the map coordinate.
static final Pattern PATTERN_END_OF_LINE
The Pattern that matches end of lines in random map parameters.
MapLocation(@NotNull final MapPath mapPath, final int mapXCoordinate, final int mapYCoordinate)
Creates a new instance.
boolean equals(@Nullable final Object obj)
The location of a map file with a map directory.
Definition: MapFile.java:31
static int getMapX(@NotNull final Attributes gameObject)
Returns the exit y coordinate of a BaseObject.