Gridarta Editor
ExitLocation.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.exitconnector;
21 
22 import java.awt.Point;
23 import java.util.Objects;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
35 public class ExitLocation {
36 
40  @NotNull
41  private final MapFile mapFile;
42 
46  private final int x;
47 
51  private final int y;
52 
56  @NotNull
57  private final String mapName;
58 
65  public ExitLocation(@NotNull final MapFile mapFile, @NotNull final Point point, @NotNull final String mapName) {
66  this.mapFile = mapFile;
67  x = point.x;
68  y = point.y;
69  this.mapName = mapName;
70  }
71 
76  @NotNull
77  public Point getMapCoordinate() {
78  return new Point(x, y);
79  }
80 
88  public void updateExitObject(@NotNull final BaseObject<?, ?, ?, ?> gameObject, final boolean updateName, @Nullable final MapFile sourceMapFile) {
89  final MapPath targetMapPath = mapFile.getMapPath();
90  final MapPath mapPath;
91  if (sourceMapFile == null) {
92  // unsaved source map => absolute reference
93  mapPath = targetMapPath;
94  } else {
95  final AbsoluteMapPath sourceMapPath = sourceMapFile.getMapPath();
96  final String sourceMapComponent = sourceMapPath.getMapComponent();
97  final String targetMapComponent = targetMapPath.getMapComponent();
98  if (Objects.equals(sourceMapComponent, targetMapComponent)) {
99  // relative reference
100  mapPath = targetMapPath.getRelativeMapPathFrom(sourceMapPath);
101  } else {
102  // absolute reference
103  mapPath = targetMapPath;
104  }
105  }
106  gameObject.setAttributeString(BaseObject.SLAYING, mapPath.toString());
107  gameObject.setAttributeInt(BaseObject.HP, x);
108  gameObject.setAttributeInt(BaseObject.SP, y);
109  if (updateName) {
110  gameObject.setAttributeString(BaseObject.NAME, mapName);
111  }
112  }
113 
118  @NotNull
119  public MapFile getMapFile() {
120  return mapFile;
121  }
122 
123  @Override
124  public boolean equals(@Nullable final Object obj) {
125  if (obj == this) {
126  return true;
127  }
128  if (obj == null || obj.getClass() != getClass()) {
129  return false;
130  }
131  final ExitLocation exitLocation = (ExitLocation) obj;
132  return x == exitLocation.x && y == exitLocation.y && mapFile.equals(exitLocation.mapFile) && mapName.equals(exitLocation.mapName);
133  }
134 
135  @Override
136  public int hashCode() {
137  return x ^ (y << 16) ^ (y >> 16) ^ mapFile.hashCode() ^ mapName.hashCode();
138  }
139 
140  @Override
141  public String toString() {
142  return mapFile + " " + x + "/" + y + " " + mapName;
143  }
144 
145 }
net.sf.gridarta.model.exitconnector.ExitLocation
Stores information about a remembered exit location.
Definition: ExitLocation.java:35
net.sf.gridarta.model.mapmodel.AbsoluteMapPath
A MapPath that is absolute, that is, it starts with a "/".
Definition: AbsoluteMapPath.java:29
net.sf.gridarta.model.exitconnector.ExitLocation.getMapCoordinate
Point getMapCoordinate()
Returns the coordinate of the remembered exit.
Definition: ExitLocation.java:77
net.sf.gridarta
Base package of all Gridarta classes.
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.mapmodel.AbsoluteMapPath.getMapComponent
String getMapComponent()
Returns the initial path component of a map path.
Definition: AbsoluteMapPath.java:86
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.model.mapmodel.MapFile.equals
boolean equals(@Nullable final Object obj)
Definition: MapFile.java:122
net.sf.gridarta.model.baseobject.BaseObject.NAME
String NAME
The attribute name of the object's name.
Definition: BaseObject.java:60
net.sf.gridarta.model.mapmodel.MapFile.getMapPath
AbsoluteMapPath getMapPath()
Returns the map path within getMapsDir().
Definition: MapFile.java:93
net.sf.gridarta.model.exitconnector.ExitLocation.getMapFile
MapFile getMapFile()
Returns the file of the map that contains the remembered exit.
Definition: ExitLocation.java:119
net
net.sf.gridarta.model.mapmodel.MapPath.getMapComponent
String getMapComponent()
Returns the initial path component of a map path.
net.sf.gridarta.model.mapmodel.MapPath
Represents a maps directory local map path.
Definition: MapPath.java:31
net.sf.gridarta.model.exitconnector.ExitLocation.equals
boolean equals(@Nullable final Object obj)
Definition: ExitLocation.java:124
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.model.mapmodel.MapFile
The location of a map file with a map directory.
Definition: MapFile.java:31
net.sf.gridarta.model.exitconnector.ExitLocation.hashCode
int hashCode()
Definition: ExitLocation.java:136
net.sf.gridarta.model.exitconnector.ExitLocation.mapFile
final MapFile mapFile
The file of the map that contains the remembered exit.
Definition: ExitLocation.java:41
net.sf.gridarta.model.exitconnector.ExitLocation.mapName
final String mapName
The map name of the remembered exit.
Definition: ExitLocation.java:57
net.sf.gridarta.model
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.model.exitconnector.ExitLocation.toString
String toString()
Definition: ExitLocation.java:141
net.sf.gridarta.model.exitconnector.ExitLocation.y
final int y
The y-coordinate of the remembered exit.
Definition: ExitLocation.java:51
net.sf.gridarta.model.mapmodel.MapPath.getRelativeMapPathFrom
RelativeMapPath getRelativeMapPathFrom(@NotNull AbsoluteMapPath mapPath)
Returns this map path relative to another map path.
net.sf.gridarta.model.exitconnector.ExitLocation.ExitLocation
ExitLocation(@NotNull final MapFile mapFile, @NotNull final Point point, @NotNull final String mapName)
Creates a new instance.
Definition: ExitLocation.java:65
net.sf.gridarta.model.exitconnector.ExitLocation.x
final int x
The x-coordinate of the remembered exit.
Definition: ExitLocation.java:46
net.sf.gridarta.model.baseobject.BaseObject.HP
String HP
The attribute name of the "hp" attribute.
Definition: BaseObject.java:90
net.sf.gridarta.model.exitconnector.ExitLocation.updateExitObject
void updateExitObject(@NotNull final BaseObject<?, ?, ?, ?> gameObject, final boolean updateName, @Nullable final MapFile sourceMapFile)
Updates exit information.
Definition: ExitLocation.java:88
net.sf.gridarta.model.mapmodel.MapFile.hashCode
int hashCode()
Definition: MapFile.java:131