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-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.mapmodel;
21 
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29 
35 public class AutoInsertionMode<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements InsertionMode<G, A, R> {
36 
40  private static final long serialVersionUID = 1L;
41 
46  @Nullable
48 
53  @Nullable
55 
60  @Nullable
62 
68  @Nullable
70 
80  public AutoInsertionMode(@Nullable final GameObjectMatcher floorGameObjectMatcher, @Nullable final GameObjectMatcher wallGameObjectMatcher, @Nullable final GameObjectMatcher belowFloorGameObjectMatcher, @Nullable final GameObjectMatcher systemObjectGameObjectMatcher) {
81  this.floorGameObjectMatcher = floorGameObjectMatcher;
82  this.wallGameObjectMatcher = wallGameObjectMatcher;
83  this.belowFloorGameObjectMatcher = belowFloorGameObjectMatcher;
84  this.systemObjectGameObjectMatcher = systemObjectGameObjectMatcher;
85  }
86 
87  @Override
88  public 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 
112  private void replaceFloor(@NotNull final G gameObject, @NotNull final MapSquare<G, A, R> mapSquare, @Nullable final G lastFloor) {
113  // floor exists ==> replace it
114  if (lastFloor != null && !lastFloor.isMulti()) {
115  mapSquare.replace(lastFloor, gameObject);
116  return;
117  }
118 
119  // "below floor" objects exist ==> insert afterwards
120  if (belowFloorGameObjectMatcher != null) {
121  final G lastBelowFloor = mapSquare.getLastOfLeadingSpan(belowFloorGameObjectMatcher);
122  if (lastBelowFloor != null) {
123  mapSquare.insertAfter(lastBelowFloor, gameObject);
124  return;
125  }
126  }
127 
128  // fallback ==> insert bottommost
129  mapSquare.addFirst(gameObject);
130  }
131 
139  private boolean replaceWall(@NotNull final G gameObject, @NotNull final GameObjectContainer<G, A, R> mapSquare, @Nullable final G lastWall) {
140  if (lastWall != null && !lastWall.isMulti()) {
141  mapSquare.replace(lastWall, gameObject);
142  return true;
143  }
144 
145  return false;
146  }
147 
155  private void insertNonSystemObject(@NotNull final G gameObject, @NotNull final GameObjectContainer<G, A, R> mapSquare, @Nullable final G firstSystemObject) {
156  mapSquare.insertAfter(firstSystemObject, gameObject);
157  }
158 
159 }
void insert(@NotNull final G gameObject, @NotNull final MapSquare< G, A, R > mapSquare)
static final long serialVersionUID
The serial version UID.
Interface for classes that match GameObjects.
This package contains classes related to matching GameObjects, so called GameObjectMatchers.
final GameObjectMatcher belowFloorGameObjectMatcher
A GameObjectMatcher matching monster game objects.
Base class for classes that contain GameObjects as children in the sense of containment.
final GameObjectMatcher systemObjectGameObjectMatcher
A GameObjectMatcher matching system objects that should stay on top.
void insertNonSystemObject(@NotNull final G gameObject, @NotNull final GameObjectContainer< G, A, R > mapSquare, @Nullable final G firstSystemObject)
Inserts a non-system game object.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
GameObjects are the objects based on Archetypes found on maps.
final GameObjectMatcher floorGameObjectMatcher
A GameObjectMatcher matching floor game objects.
final GameObjectMatcher wallGameObjectMatcher
A GameObjectMatcher matching wall game objects.
void replaceFloor(@NotNull final G gameObject, @NotNull final MapSquare< G, A, R > mapSquare, @Nullable final G lastFloor)
Replace a floor game object.
AutoInsertionMode(@Nullable final GameObjectMatcher floorGameObjectMatcher, @Nullable final GameObjectMatcher wallGameObjectMatcher, @Nullable final GameObjectMatcher belowFloorGameObjectMatcher, @Nullable final GameObjectMatcher systemObjectGameObjectMatcher)
Initializes the class.
Automatically guess the insertion position.
boolean replaceWall(@NotNull final G gameObject, @NotNull final GameObjectContainer< G, A, R > mapSquare, @Nullable final G lastWall)
Replaces a wall game object.
boolean isMatching(@NotNull GameObject<?, ?, ?> gameObject)
Matches an GameObject.