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-2023 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 GameObject<?, ?, ?> 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.getMapSquareOptional();
98  if (mapSquare == null) {
99  baseMapPath = new AbsoluteMapPath();
100  } else {
101  final MapModel<?, ?, ?> mapModel = mapSquare.getMapModel();
102  final MapFile mapFile = mapModel.getMapFile();
103  baseMapPath = mapFile == null ? new AbsoluteMapPath() : mapFile.getMapPath();
104  }
105  final AbsoluteMapPath canonicalMapPath = MapPathUtils.append(baseMapPath, mapPath);
106  final int mapX = getMapX(gameObject);
107  final int mapY = getMapY(gameObject);
108  return new MapLocation(canonicalMapPath, mapX, mapY);
109  }
110 
115  @NotNull
116  public MapPath getMapPath() {
117  return mapPath;
118  }
119 
124  @NotNull
125  public Point getMapCoordinate() {
126  return new Point(mapCoordinate);
127  }
128 
129  @Override
130  public boolean equals(@Nullable final Object obj) {
131  if (obj == this) {
132  return true;
133  }
134  if (obj == null || obj.getClass() != getClass()) {
135  return false;
136  }
137  final MapLocation mapLocation = (MapLocation) obj;
138  return mapLocation.mapPath.equals(mapPath) && mapLocation.mapCoordinate.equals(mapCoordinate);
139  }
140 
141  @Override
142  public int hashCode() {
143  return mapPath.hashCode() ^ mapCoordinate.hashCode();
144  }
145 
151  private static int getMapY(@NotNull final Attributes gameObject) {
152  return gameObject.getAttributeInt(BaseObject.SP);
153  }
154 
160  private static int getMapX(@NotNull final Attributes gameObject) {
161  return gameObject.getAttributeInt(BaseObject.HP);
162  }
163 
171  @NotNull
172  public static MapPath getMapPath(@NotNull final GameObject<?, ?, ?> gameObject, final boolean allowRandomMapParameters) throws NoExitPathException {
173  String path = gameObject.getAttributeString(BaseObject.SLAYING);
174  if (path.isEmpty()) {
175  final MapSquare<?, ?, ?> mapSquare = gameObject.getMapSquareOptional();
176  if (mapSquare == null) {
177  throw new NoExitPathException(gameObject);
178  }
179 
180  final MapModel<?, ?, ?> mapModel = mapSquare.getMapModel();
181  final MapFile mapFile = mapModel.getMapFile();
182  if (mapFile == null) {
183  throw new NoExitPathException(gameObject);
184  }
185 
186  return mapFile.getMapPath();
187  }
188 
189  if (allowRandomMapParameters && (path.equals("/!") || path.startsWith("/random/"))) {
190  // destination is a random map; extract the final non-random map
191  path = getRandomMapParameter(gameObject, "final_map");
192  if (path == null) {
193  throw new NoExitPathException(gameObject);
194  }
195  }
196 
197  return MapPathUtils.newMapPath(path);
198  }
199 
207  @Nullable
208  private static String getRandomMapParameter(@NotNull final BaseObject<?, ?, ?, ?> gameObject, @NotNull final String parameterName) {
209  final String msg = gameObject.getMsgText(true);
210  if (msg == null) {
211  return null;
212  }
213 
214  final String[] lines = PATTERN_END_OF_LINE.split(msg);
215  for (final String line : lines) {
216  final String[] tmp = StringUtils.PATTERN_SPACES.split(line, 2);
217  if (tmp.length == 2 && tmp[0].equals(parameterName)) {
218  return tmp[1];
219  }
220  }
221 
222  return null;
223  }
224 
225  @NotNull
226  @Override
227  public String toString() {
228  return mapCoordinate.x + "/" + mapCoordinate.y + "@" + mapPath;
229  }
230 
231  @Override
232  @SuppressWarnings("CompareToUsesNonFinalVariable")
233  public int compareTo(@NotNull final MapLocation o) {
234  final int cmp = mapPath.toString().compareTo(o.mapPath.toString());
235  if (cmp != 0) {
236  return cmp;
237  }
238  if (mapCoordinate.x < o.mapCoordinate.x) {
239  return -1;
240  }
241  if (mapCoordinate.x > o.mapCoordinate.x) {
242  return -1;
243  }
244  if (mapCoordinate.y < o.mapCoordinate.y) {
245  return -1;
246  }
247  if (mapCoordinate.y > o.mapCoordinate.y) {
248  return -1;
249  }
250  return 0;
251  }
252 
253 }
net.sf.gridarta.model.maplocation.MapLocation.getMapPath
MapPath getMapPath()
Returns the map path.
Definition: MapLocation.java:116
net.sf.gridarta.model.maplocation.MapLocation.equals
boolean equals(@Nullable final Object obj)
Definition: MapLocation.java:130
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.model.mapmodel.MapSquare.getMapSquareOptional
MapSquare< G, A, R > getMapSquareOptional()
Returns the MapSquare this game object is part of.
Definition: MapSquare.java:156
net.sf.gridarta.model.maplocation.MapLocation.compareTo
int compareTo(@NotNull final MapLocation o)
Definition: MapLocation.java:233
net.sf.gridarta.model.maplocation.MapLocation.mapCoordinate
final Point mapCoordinate
The map coordinate.
Definition: MapLocation.java:60
net.sf.gridarta.model.mapmodel.AbsoluteMapPath
A MapPath that is absolute, that is, it starts with a "/".
Definition: AbsoluteMapPath.java:29
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.mapmodel.MapSquare
A single Map Square.
Definition: MapSquare.java:45
net.sf.gridarta.model.baseobject.BaseObject.SP
String SP
The attribute name of the "sp" attribute.
Definition: BaseObject.java:96
net.sf
net.sf.gridarta.model.maplocation.MapLocation.MapLocation
MapLocation(@NotNull final GameObject<?, ?, ?> gameObject, final boolean allowRandomMapParameters)
Creates a new instance from a BaseObject instance.
Definition: MapLocation.java:80
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.model.maplocation.MapLocation.getMapPath
static MapPath getMapPath(@NotNull final GameObject<?, ?, ?> gameObject, final boolean allowRandomMapParameters)
Returns the exit map path of a BaseObject.
Definition: MapLocation.java:172
net.sf.gridarta.model.mapmodel.MapPathUtils.newMapPath
static MapPath newMapPath(@NotNull final String string)
Creates a MapPath instance from string representation.
Definition: MapPathUtils.java:43
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.mapmodel.MapPathUtils
Utility class for MapPath related functions.
Definition: MapPathUtils.java:28
net.sf.gridarta.model.mapmodel.MapFile.getMapPath
AbsoluteMapPath getMapPath()
Returns the map path within getMapsDir().
Definition: MapFile.java:93
net.sf.gridarta.model.baseobject.Attributes
A set of key/value pairs.
Definition: Attributes.java:28
net.sf.gridarta.model.maplocation.MapLocation.getMapCoordinate
Point getMapCoordinate()
Returns the map coordinate.
Definition: MapLocation.java:125
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.maplocation.MapLocation.newAbsoluteMapLocation
static MapLocation newAbsoluteMapLocation(@NotNull final GameObject<?, ?, ?> gameObject, final boolean allowRandomMapParameters)
Creates a new instance from a BaseObject instance.
Definition: MapLocation.java:94
net.sf.gridarta.model.mapmodel.MapPathUtils.append
static AbsoluteMapPath append(@NotNull final AbsoluteMapPath baseMapPath, @NotNull final MapPath mapPath)
Appends a map path to another map path.
Definition: MapPathUtils.java:85
net.sf.gridarta.model.maplocation.MapLocation.PATTERN_END_OF_LINE
static final Pattern PATTERN_END_OF_LINE
The Pattern that matches end of lines in random map parameters.
Definition: MapLocation.java:48
net.sf.gridarta.model.maplocation.NoExitPathException
Exception thrown if a game object does not specify a valid exit path.
Definition: NoExitPathException.java:29
net.sf.gridarta.model.maplocation.MapLocation.getMapY
static int getMapY(@NotNull final Attributes gameObject)
Returns the exit x coordinate of a BaseObject.
Definition: MapLocation.java:151
net.sf.gridarta.model.maplocation.MapLocation.hashCode
int hashCode()
Definition: MapLocation.java:142
net.sf.gridarta.model.mapmodel.MapPath
Represents a maps directory local map path.
Definition: MapPath.java:31
net.sf.gridarta.model.maplocation.MapLocation
Represents a location on a map consisting of a map path and a map coordinate.
Definition: MapLocation.java:42
net.sf.gridarta.model.baseobject.BaseObject.SLAYING
String SLAYING
The name of the "slaying" attribute.
Definition: BaseObject.java:120
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.utils.StringUtils
Utility class for string manipulation.
Definition: StringUtils.java:31
net.sf.gridarta.model.mapmodel.MapFile
The location of a map file with a map directory.
Definition: MapFile.java:31
net.sf.gridarta.model.mapmodel.MapSquare.getMapModel
MapModel< G, A, R > getMapModel()
Returns the MapModel this map square is part of.
Definition: MapSquare.java:99
net.sf.gridarta.model.maplocation.MapLocation.getRandomMapParameter
static String getRandomMapParameter(@NotNull final BaseObject<?, ?, ?, ?> gameObject, @NotNull final String parameterName)
Extracts a parameter value for an exit to a random map.
Definition: MapLocation.java:208
net.sf.gridarta.model
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.model.maplocation.MapLocation.mapPath
final MapPath mapPath
The map path.
Definition: MapLocation.java:54
net.sf.gridarta.utils.StringUtils.PATTERN_SPACES
static final Pattern PATTERN_SPACES
The pattern that matches a non-empty sequence of spaces.
Definition: StringUtils.java:73
net.sf.gridarta.model.maplocation.MapLocation.getMapX
static int getMapX(@NotNull final Attributes gameObject)
Returns the exit y coordinate of a BaseObject.
Definition: MapLocation.java:160
net.sf.gridarta.var.crossfire.model.gameobject.GameObject<?, ?, ?>
net.sf.gridarta.model.maplocation.MapLocation.MapLocation
MapLocation(@NotNull final MapPath mapPath, final int mapXCoordinate, final int mapYCoordinate)
Creates a new instance.
Definition: MapLocation.java:68
net.sf.gridarta.model.baseobject.BaseObject.HP
String HP
The attribute name of the "hp" attribute.
Definition: BaseObject.java:90
net.sf.gridarta.model.mapmodel.MapModel.getMapFile
MapFile getMapFile()
Returns the map file.
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.maplocation.MapLocation.toString
String toString()
Definition: MapLocation.java:227