Gridarta Editor
AutoInsertionMode.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.mapmodel;
21 
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29 
35 public class AutoInsertionMode implements InsertionMode {
36 
40  private static final long serialVersionUID = 1L;
41 
46  @Nullable
48 
53  @Nullable
55 
60  @Nullable
62 
68  @Nullable
70 
81  this.floorGameObjectMatcher = floorGameObjectMatcher;
82  this.wallGameObjectMatcher = wallGameObjectMatcher;
83  this.belowFloorGameObjectMatcher = belowFloorGameObjectMatcher;
84  this.systemObjectGameObjectMatcher = systemObjectGameObjectMatcher;
85  }
86 
87  @Override
88  public <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> void insert(@NotNull final G gameObject, @NotNull final MapSquare<G, A, R> mapSquare) {
89  if (floorGameObjectMatcher != null && floorGameObjectMatcher.isMatching(gameObject)) {
90  replaceFloor(gameObject, mapSquare, mapSquare.getLast(floorGameObjectMatcher));
91  } else if (wallGameObjectMatcher != null && wallGameObjectMatcher.isMatching(gameObject) && replaceWall(gameObject, mapSquare, mapSquare.getLast(wallGameObjectMatcher))) {
92  } else if (belowFloorGameObjectMatcher != null && belowFloorGameObjectMatcher.isMatching(gameObject)) {
93  mapSquare.addFirst(gameObject);
94  } else if (systemObjectGameObjectMatcher != null && !systemObjectGameObjectMatcher.isMatching(gameObject)) {
95  insertNonSystemObject(gameObject, mapSquare, mapSquare.getFirst(systemObjectGameObjectMatcher));
96  } else {
97  mapSquare.addLast(gameObject);
98  }
99  }
100 
101  @Override
102  public String toString() {
103  return "auto";
104  }
105 
115  private <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> void replaceFloor(@NotNull final G gameObject, @NotNull final MapSquare<G, A, R> mapSquare, @Nullable final G lastFloor) {
116  // floor exists ==> replace it
117  if (lastFloor != null && !lastFloor.isMulti()) {
118  mapSquare.replace(lastFloor, gameObject);
119  return;
120  }
121 
122  // "below floor" objects exist ==> insert afterwards
123  if (belowFloorGameObjectMatcher != null) {
124  final G lastBelowFloor = mapSquare.getLastOfLeadingSpan(belowFloorGameObjectMatcher);
125  if (lastBelowFloor != null) {
126  mapSquare.insertAfter(lastBelowFloor, gameObject);
127  return;
128  }
129  }
130 
131  // fallback ==> insert bottommost
132  mapSquare.addFirst(gameObject);
133  }
134 
145  private static <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> boolean replaceWall(@NotNull final G gameObject, @NotNull final GameObjectContainer<G, A, R> mapSquare, @Nullable final G lastWall) {
146  if (lastWall != null && !lastWall.isMulti()) {
147  mapSquare.replace(lastWall, gameObject);
148  return true;
149  }
150 
151  return false;
152  }
153 
164  private static <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> void insertNonSystemObject(@NotNull final G gameObject, @NotNull final GameObjectContainer<G, A, R> mapSquare, @Nullable final G firstSystemObject) {
165  mapSquare.insertAfter(firstSystemObject, gameObject);
166  }
167 
168 }
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.mapmodel.AutoInsertionMode.replaceFloor
private< G extends GameObject< G, A, R >, A extends MapArchObject< A >, R extends Archetype< G, A, R > > void replaceFloor(@NotNull final G gameObject, @NotNull final MapSquare< G, A, R > mapSquare, @Nullable final G lastFloor)
Replace a floor game object.
Definition: AutoInsertionMode.java:115
net.sf.gridarta.model.mapmodel.MapSquare< G, A, R >
net.sf.gridarta.model.mapmodel.AutoInsertionMode.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: AutoInsertionMode.java:40
net.sf.gridarta.model.mapmodel.AutoInsertionMode.insert
public< G extends GameObject< G, A, R >, A extends MapArchObject< A >, R extends Archetype< G, A, R > > void insert(@NotNull final G gameObject, @NotNull final MapSquare< G, A, R > mapSquare)
Definition: AutoInsertionMode.java:88
net.sf
net.sf.gridarta.model.match.GameObjectMatcher
Interface for classes that match GameObjects.
Definition: GameObjectMatcher.java:30
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.match.GameObjectMatcher.isMatching
boolean isMatching(@NotNull GameObject<?, ?, ?> gameObject)
Matches a GameObject.
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
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.mapmodel.AutoInsertionMode.floorGameObjectMatcher
final GameObjectMatcher floorGameObjectMatcher
A GameObjectMatcher matching floor game objects.
Definition: AutoInsertionMode.java:47
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.mapmodel.AutoInsertionMode
Automatically guess the insertion position.
Definition: AutoInsertionMode.java:35
net.sf.gridarta.model.mapmodel.InsertionMode
Insertion modes.
Definition: InsertionMode.java:33
net.sf.gridarta.model.mapmodel.AutoInsertionMode.AutoInsertionMode
AutoInsertionMode(@Nullable final GameObjectMatcher floorGameObjectMatcher, @Nullable final GameObjectMatcher wallGameObjectMatcher, @Nullable final GameObjectMatcher belowFloorGameObjectMatcher, @Nullable final GameObjectMatcher systemObjectGameObjectMatcher)
Initializes the class.
Definition: AutoInsertionMode.java:80
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.model.mapmodel.AutoInsertionMode.systemObjectGameObjectMatcher
final GameObjectMatcher systemObjectGameObjectMatcher
A GameObjectMatcher matching system objects that should stay on top.
Definition: AutoInsertionMode.java:69
net.sf.gridarta.model.mapmodel.AutoInsertionMode.replaceWall
static< G extends GameObject< G, A, R > A extends R extends Archetype< G, A, R > boolean replaceWall(@NotNull final G gameObject, @NotNull final GameObjectContainer< G, A, R > mapSquare, @Nullable final G lastWall)
Definition: AutoInsertionMode.java:145
net.sf.gridarta.model.mapmodel.AutoInsertionMode.insertNonSystemObject
static< G extends GameObject< G, A, R > A extends R extends Archetype< G, A, R > void insertNonSystemObject(@NotNull final G gameObject, @NotNull final GameObjectContainer< G, A, R > mapSquare, @Nullable final G firstSystemObject)
Definition: AutoInsertionMode.java:164
net.sf.gridarta.model.mapmodel.AutoInsertionMode.belowFloorGameObjectMatcher
final GameObjectMatcher belowFloorGameObjectMatcher
A GameObjectMatcher matching monster game objects.
Definition: AutoInsertionMode.java:61
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.mapmodel.AutoInsertionMode.toString
String toString()
Definition: AutoInsertionMode.java:102
net.sf.gridarta.model.baseobject.GameObjectContainer
Base class for classes that contain GameObjects as children in the sense of containment.
Definition: GameObjectContainer.java:50
net.sf.gridarta.model.mapmodel.AutoInsertionMode.wallGameObjectMatcher
final GameObjectMatcher wallGameObjectMatcher
A GameObjectMatcher matching wall game objects.
Definition: AutoInsertionMode.java:54