Gridarta Editor
ExitMatcher.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;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31 
36 public class ExitMatcher<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements GameObjectMatcher {
37 
41  private static final long serialVersionUID = 1L;
42 
46  @NotNull
48 
53  public ExitMatcher(@NotNull final GameObjectMatcher exitMatcher) {
54  this.exitMatcher = exitMatcher;
55  }
56 
64  @Nullable
65  public G getValidExit(@NotNull final MapModel<G, A, R> mapModel, @NotNull final Point point) {
66  if (!mapModel.getMapArchObject().isPointValid(point)) {
67  return null;
68  }
69  for (final GameObject<G, A, R> part : mapModel.getMapSquare(point)) {
70  final G head = part.getHead();
71  if (isValidExit(head)) {
72  return head;
73  }
74  }
75  return null;
76  }
77 
85  @Nullable
86  public GameObject<G, A, R> getValidExit(@Nullable final G exit) {
87  if (exit == null) {
88  return null;
89  }
90  final GameObject<G, A, R> head = exit.getHead();
91  return isValidExit(head) ? head : null;
92  }
93 
101  @Nullable
102  public G getExit(@NotNull final MapModel<G, A, R> mapModel, @Nullable final Point point) {
103  if (point == null || !mapModel.getMapArchObject().isPointValid(point)) {
104  return null;
105  }
106  for (final GameObject<G, A, R> part : mapModel.getMapSquare(point)) {
107  final G head = part.getHead();
108  if (isExit(head)) {
109  return head;
110  }
111  }
112  return null;
113  }
114 
121  @Nullable
122  public G getExit(@Nullable final G exit) {
123  if (exit == null) {
124  return null;
125  }
126  final G head = exit.getHead();
127  return isExit(head) ? head : null;
128  }
129 
135  private boolean isValidExit(@NotNull final GameObject<G, A, R> gameObject) {
136  return isExit(gameObject) && (gameObject.hasAttribute(BaseObject.SLAYING) || gameObject.hasAttribute(BaseObject.HP) || gameObject.hasAttribute(BaseObject.SP));
137  }
138 
144  private boolean isExit(@NotNull final GameObject<?, ?, ?> gameObject) {
145  return exitMatcher.isMatching(gameObject);
146  }
147 
148  @Override
149  public boolean isMatching(@NotNull final GameObject<?, ?, ?> gameObject) {
150  return isExit(gameObject);
151  }
152 
153 }
net.sf.gridarta.model.exitconnector.ExitMatcher.isExit
boolean isExit(@NotNull final GameObject<?, ?, ?> gameObject)
Returns whether a GameObject is a valid exit.
Definition: ExitMatcher.java:144
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.model.exitconnector.ExitMatcher.exitMatcher
final GameObjectMatcher exitMatcher
The matcher for selecting exit objects.
Definition: ExitMatcher.java:47
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
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.model.exitconnector.ExitMatcher.getExit
G getExit(@NotNull final MapModel< G, A, R > mapModel, @Nullable final Point point)
Returns an exit game object on a given map square.
Definition: ExitMatcher.java:102
net.sf.gridarta.model.match.GameObjectMatcher
Interface for classes that match GameObjects.
Definition: GameObjectMatcher.java:30
net.sf.gridarta.model.exitconnector.ExitMatcher.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: ExitMatcher.java:41
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.exitconnector.ExitMatcher
Selects valid exit game objects from maps.
Definition: ExitMatcher.java:36
net.sf.gridarta.model.exitconnector.ExitMatcher.isMatching
boolean isMatching(@NotNull final GameObject<?, ?, ?> gameObject)
Matches a GameObject.
Definition: ExitMatcher.java:149
net.sf.gridarta.model.match.GameObjectMatcher.isMatching
boolean isMatching(@NotNull GameObject<?, ?, ?> gameObject)
Matches a GameObject.
net.sf.gridarta.model.exitconnector.ExitMatcher.ExitMatcher
ExitMatcher(@NotNull final GameObjectMatcher exitMatcher)
Creates a new instance.
Definition: ExitMatcher.java:53
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.exitconnector.ExitMatcher.getValidExit
GameObject< G, A, R > getValidExit(@Nullable final G exit)
Returns whether the given game object is an exit game object having exit information.
Definition: ExitMatcher.java:86
net.sf.gridarta.model.match
Classes related to matching {GameObjects}, so called { net.sf.gridarta.model.match....
Definition: AndGameObjectMatcher.java:20
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
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.exitconnector.ExitMatcher.getValidExit
G getValidExit(@NotNull final MapModel< G, A, R > mapModel, @NotNull final Point point)
Returns an exit game object on a given map square having exit information.
Definition: ExitMatcher.java:65
net.sf.gridarta.model.baseobject.BaseObject.getHead
T getHead()
Return the head part of a multi-part object.
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.var.crossfire.model.gameobject.GameObject<?, ?, ?>
net.sf.gridarta.model.gameobject.GameObject.getMapSquare
MapSquare< G, A, R > getMapSquare()
Returns the MapSquare this game object is part of.
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
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.ExitMatcher.getExit
G getExit(@Nullable final G exit)
Returns whether the given game object is an exit game object.
Definition: ExitMatcher.java:122
net.sf.gridarta.model.exitconnector.ExitMatcher.isValidExit
boolean isValidExit(@NotNull final GameObject< G, A, R > gameObject)
Returns whether a GameObject is a valid exit.
Definition: ExitMatcher.java:135