Gridarta Editor
AutojoinList.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.autojoin;
21 
22 import java.io.Serializable;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.IdentityHashMap;
26 import java.util.List;
27 import java.util.Map;
31 import org.apache.log4j.Category;
32 import org.apache.log4j.Logger;
33 import org.jetbrains.annotations.NotNull;
34 
39 public class AutojoinList<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Serializable {
40 
44  private static final long serialVersionUID = 1L;
45 
49  @NotNull
50  private static final Category LOG = Logger.getLogger(AutojoinList.class);
51 
55  public static final int SIZE = 16;
56 
57  // bitmask constants for {@link #archetypes} index
58 
59  public static final int NORTH = 1;
60 
61  public static final int EAST = 2;
62 
63  public static final int SOUTH = 4;
64 
65  public static final int WEST = 8;
66 
83  @NotNull
84  private final List<List<R>> archetypes;
85 
92  public AutojoinList(@NotNull final Iterable<List<R>> archetypes) throws IllegalAutojoinListException {
93  final ArrayList<List<R>> newArchetypes = new ArrayList<>();
94 
95  for (final List<R> archetypeList : archetypes) {
96  newArchetypes.add(validate(archetypeList));
97  }
98 
99  if (newArchetypes.size() > SIZE) {
100  throw new IllegalAutojoinListException("autojoin list with more than " + SIZE + " valid entries");
101  }
102  if (newArchetypes.size() < SIZE) {
103  throw new IllegalAutojoinListException("autojoin list with less than " + SIZE + " valid entries");
104  }
105 
106  newArchetypes.trimToSize();
107  this.archetypes = newArchetypes;
108  }
109 
117  @NotNull
118  private static <R extends Archetype<?, ?, R>> List<R> validate(@NotNull final Iterable<R> archetypeList) throws IllegalAutojoinListException {
119  final ArrayList<R> result = new ArrayList<>();
120  final Map<R, R> tmp = new IdentityHashMap<>();
121  for (final R archetype : archetypeList) {
122  if (archetype == null) {
123  throw new IllegalArgumentException("can't add null archetype");
124  }
125 
126  if (tmp.put(archetype, archetype) != null) {
127  throw new IllegalAutojoinListException("autojoin list contains duplicate archetype '" + archetype.getArchetypeName() + "'");
128  }
129 
130  result.add(archetype);
131  }
132  if (result.isEmpty()) {
133  throw new IllegalAutojoinListException("autojoin list is empty");
134  }
135  result.trimToSize();
136  return result;
137  }
138 
144  public int getIndex(@NotNull final R archetype) {
145  for (int i = 0; i < SIZE; i++) {
146  if (archetypes.get(i).contains(archetype)) {
147  return i;
148  }
149  }
150 
151  LOG.warn("Error in AutojoinList.get_index: index not found");
152  return 0;
153  }
154 
161  public boolean isMainIndex(@NotNull final R archetype) {
162  for (int i = 0; i < SIZE; i++) {
163  if (archetypes.get(i).get(0) == archetype) {
164  return true;
165  }
166  }
167 
168  return false;
169  }
170 
177  public int getAlternativeIndex(@NotNull final R archetype) {
178  for (int i = 0; i < SIZE; i++) {
179  final List<R> tmp = archetypes.get(i);
180  if (tmp.get(0) != archetype && tmp.contains(archetype)) {
181  return i;
182  }
183  }
184 
185  return -1;
186  }
187 
188  @NotNull
189  public R getArchetype(final int index) {
190  return archetypes.get(index).get(0);
191  }
192 
198  @NotNull
199  public Iterable<R> getArchetypes(final int index) {
200  return Collections.unmodifiableCollection(archetypes.get(index));
201  }
202 
203 }
net.sf.gridarta.model.autojoin.AutojoinList.getAlternativeIndex
int getAlternativeIndex(@NotNull final R archetype)
Returns the index of an Archetype if it is an alternative archetype for any direction.
Definition: AutojoinList.java:177
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.autojoin.AutojoinList.getArchetypes
Iterable< R > getArchetypes(final int index)
Returns all archetypes for an index.
Definition: AutojoinList.java:199
net.sf.gridarta.model.autojoin.AutojoinList.isMainIndex
boolean isMainIndex(@NotNull final R archetype)
Returns the index of an Archetype if it is a main archetype for any direction.
Definition: AutojoinList.java:161
net.sf.gridarta.model.autojoin.AutojoinList.WEST
static final int WEST
Definition: AutojoinList.java:65
net.sf
net.sf.gridarta.model.autojoin.AutojoinList.SIZE
static final int SIZE
The number of archetypes in an autojoin list.
Definition: AutojoinList.java:55
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.autojoin.AutojoinList.getIndex
int getIndex(@NotNull final R archetype)
Looks up the given node in the archetype array of this class.
Definition: AutojoinList.java:144
net.sf.gridarta.model.autojoin.AutojoinList.NORTH
static final int NORTH
Definition: AutojoinList.java:59
net.sf.gridarta.model.autojoin.AutojoinList.SOUTH
static final int SOUTH
Definition: AutojoinList.java:63
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.autojoin.AutojoinList.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: AutojoinList.java:44
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.autojoin.AutojoinList
Contains a list of (typically wall-)arches which do autojoining.
Definition: AutojoinList.java:39
net.sf.gridarta.model.autojoin.IllegalAutojoinListException
Exception thrown if an AutojoinList is invalid.
Definition: IllegalAutojoinListException.java:28
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.autojoin.AutojoinList.LOG
static final Category LOG
The Logger for printing log messages.
Definition: AutojoinList.java:50
net.sf.gridarta.model.autojoin.AutojoinList.validate
static< R extends Archetype<?, ?, R > List< R > validate(@NotNull final Iterable< R > archetypeList)
Validates a list of archetypes: no entry must be.
Definition: AutojoinList.java:118
net.sf.gridarta.model.autojoin.AutojoinList.getArchetype
R getArchetype(final int index)
Definition: AutojoinList.java:189
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.autojoin.AutojoinList.AutojoinList
AutojoinList(@NotNull final Iterable< List< R >> archetypes)
Creates a new instance.
Definition: AutojoinList.java:92
net.sf.gridarta.model.autojoin.AutojoinList.archetypes
final List< List< R > > archetypes
Maps index to list of archetypes representing autojoin-able archetypes for this direction.
Definition: AutojoinList.java:84
net.sf.gridarta.model.autojoin.AutojoinList.EAST
static final int EAST
Definition: AutojoinList.java:61