Gridarta Editor
BlockedSpawnPointChecker.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.validation.checks;
21 
22 import java.awt.Point;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.HashSet;
37 import org.jetbrains.annotations.NotNull;
38 
45 public class BlockedSpawnPointChecker<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractValidator<G, A, R> implements MapValidator<G, A, R> {
46 
50  @NotNull
51  private static final int[] FREE_ARR_X = { 0, 0, 1, 1, 1, 0, -1, -1, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -3, -3, -3, -3, -2, -1, };
52 
56  @NotNull
57  private static final int[] FREE_ARR_Y = { 0, -1, -1, 0, 1, 1, 1, 0, -1, -2, -2, -2, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -3, -3, -3, -3, -2, -1, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, };
58 
59  static {
60  assert FREE_ARR_X.length == FREE_ARR_Y.length;
61  }
62 
66  @NotNull
67  private final Collection<Integer> typeNumbers = new HashSet<>();
68 
74  public BlockedSpawnPointChecker(@NotNull final ValidatorPreferences validatorPreferences, @NotNull final Integer... typeNumbers) {
75  super(validatorPreferences);
76  this.typeNumbers.addAll(Arrays.asList(typeNumbers));
77  }
78 
79  @Override
80  public void validateMap(@NotNull final MapModel<G, A, R> mapModel, @NotNull final ErrorCollector<G, A, R> errorCollector) {
81  final BlockedMatrix<G, A, R> blocked = new BlockedMatrix<>(mapModel);
82  checkSpawnPoints(mapModel, blocked, errorCollector);
83  }
84 
91  private void checkSpawnPoints(@NotNull final Iterable<MapSquare<G, A, R>> mapModel, @NotNull final BlockedMatrix<G, A, R> blocked, @NotNull final ErrorCollector<G, A, R> errorCollector) {
92  final Point pos = new Point();
93  for (final MapSquare<G, A, R> mapSquare : mapModel) {
94  for (final G gameObject : mapSquare) {
95  mapSquare.getMapLocation(pos);
96  checkSpawnPoint(gameObject, pos, blocked, errorCollector);
97  for (final G invObject : gameObject.recursive()) {
98  checkSpawnPoint(invObject, pos, blocked, errorCollector);
99  }
100  }
101  }
102  }
103 
111  private void checkSpawnPoint(@NotNull final G gameObject, @NotNull final Point pos, @NotNull final BlockedMatrix<G, A, R> blocked, @NotNull final ErrorCollector<G, A, R> errorCollector) {
112  final GameObject<G, A, R> head = gameObject.getHead();
113  if (!typeNumbers.contains(head.getTypeNo())) {
114  return;
115  }
116 
117  final int spawnRange = Math.min(head.getAttributeInt(BaseObject.LAST_HEAL), FREE_ARR_X.length);
118  for (int i = 0; i < spawnRange; i++) {
119  if (!blocked.isBlocked(pos.x + FREE_ARR_X[i], pos.y + FREE_ARR_Y[i])) {
120  return;
121  }
122  }
123 
124  errorCollector.collect(new BlockedSpawnPointError<>(gameObject));
125  }
126 
127 }
void validateMap(@NotNull final MapModel< G, A, R > mapModel, @NotNull final ErrorCollector< G, A, R > errorCollector)
Validate a map.
T getHead()
Return the head part of a multi-part object.
A MapValidator to assert that mobs or spawn points aren&#39;t on blocked squares.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
This is the base class for validators.
This package contains the framework for validating maps.
final Collection< Integer > typeNumbers
The object types to check.
BlockedSpawnPointChecker(@NotNull final ValidatorPreferences validatorPreferences, @NotNull final Integer... typeNumbers)
Creates a new instance.
void checkSpawnPoints(@NotNull final Iterable< MapSquare< G, A, R >> mapModel, @NotNull final BlockedMatrix< G, A, R > blocked, @NotNull final ErrorCollector< G, A, R > errorCollector)
Checks for blocked spawn points.
String LAST_HEAL
The name of the "last_heal" attribute.
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.
Indicates a spawn point that has can never spawn a mob because all possible destinations are blocked...
int getAttributeInt(@NotNull String attributeName, boolean queryArchetype)
Returns the requested attribute value of this GameObject as.
final ValidatorPreferences validatorPreferences
The ValidatorPreferences to use.
Determines whether a map square is blocked.
An interface for classes that collect errors.
static final int [] FREE_ARR_Y
The y offset for checking blocked squares.
void checkSpawnPoint(@NotNull final G gameObject, @NotNull final Point pos, @NotNull final BlockedMatrix< G, A, R > blocked, @NotNull final ErrorCollector< G, A, R > errorCollector)
Checks one game object.
int getTypeNo()
Returns the type number of this Archetype.
static final int [] FREE_ARR_X
The x offset for checking blocked squares.