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-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.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  private static final int @NotNull [] 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, };
51 
55  private static final int @NotNull [] 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, };
56 
57  static {
58  assert FREE_ARR_X.length == FREE_ARR_Y.length;
59  }
60 
64  @NotNull
65  private final Collection<Integer> typeNumbers = new HashSet<>();
66 
72  public BlockedSpawnPointChecker(@NotNull final ValidatorPreferences validatorPreferences, @NotNull final Integer... typeNumbers) {
73  super(validatorPreferences);
74  this.typeNumbers.addAll(Arrays.asList(typeNumbers));
75  }
76 
77  @Override
78  public void validateMap(@NotNull final MapModel<G, A, R> mapModel, @NotNull final ErrorCollector<G, A, R> errorCollector) {
79  final BlockedMatrix<G, A, R> blocked = new BlockedMatrix<>(mapModel);
80  checkSpawnPoints(mapModel, blocked, errorCollector);
81  }
82 
89  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) {
90  final Point pos = new Point();
91  for (final MapSquare<G, A, R> mapSquare : mapModel) {
92  for (final G gameObject : mapSquare) {
93  mapSquare.getMapLocation(pos);
94  checkSpawnPoint(gameObject, pos, blocked, errorCollector);
95  for (final G invObject : gameObject.recursive()) {
96  checkSpawnPoint(invObject, pos, blocked, errorCollector);
97  }
98  }
99  }
100  }
101 
109  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) {
110  final GameObject<G, A, R> head = gameObject.getHead();
111  if (!typeNumbers.contains(head.getTypeNo())) {
112  return;
113  }
114 
115  final int spawnRange = Math.min(head.getAttributeInt(BaseObject.LAST_HEAL), FREE_ARR_X.length);
116  for (int i = 0; i < spawnRange; i++) {
117  if (!blocked.isBlocked(pos.x + FREE_ARR_X[i], pos.y + FREE_ARR_Y[i])) {
118  return;
119  }
120  }
121 
122  errorCollector.collect(new BlockedSpawnPointError<>(gameObject));
123  }
124 
125 }
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.validation.checks.BlockedSpawnPointChecker.FREE_ARR_Y
static final int[] FREE_ARR_Y
The y offset for checking blocked squares.
Definition: BlockedSpawnPointChecker.java:55
net.sf.gridarta.model.mapmodel.MapSquare
A single Map Square.
Definition: MapSquare.java:45
net.sf.gridarta.model.baseobject.BaseObject.getAttributeInt
int getAttributeInt(@NotNull String attributeName, boolean queryArchetype)
Returns the requested attribute value of this GameObject as.
net.sf
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.model.validation.checks.BlockedSpawnPointChecker.FREE_ARR_X
static final int[] FREE_ARR_X
The x offset for checking blocked squares.
Definition: BlockedSpawnPointChecker.java:50
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.validation.errors.BlockedSpawnPointError
Indicates a spawn point that has can never spawn a mob because all possible destinations are blocked.
Definition: BlockedSpawnPointError.java:33
net.sf.gridarta.model.validation.AbstractValidator.validatorPreferences
final ValidatorPreferences validatorPreferences
The ValidatorPreferences to use.
Definition: AbstractValidator.java:55
net.sf.gridarta.model.validation.MapValidator
Interface for Map Validators.
Definition: MapValidator.java:33
net.sf.gridarta.model.validation.ValidatorPreferences
Configuration parameters for Validators.
Definition: ValidatorPreferences.java:28
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net.sf.gridarta.model.validation.checks.BlockedSpawnPointChecker.checkSpawnPoint
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.
Definition: BlockedSpawnPointChecker.java:109
net
net.sf.gridarta.model.validation.checks.BlockedMatrix
Determines whether a map square is blocked.
Definition: BlockedMatrix.java:35
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.validation.checks.BlockedSpawnPointChecker.checkSpawnPoints
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.
Definition: BlockedSpawnPointChecker.java:89
net.sf.gridarta.model.validation.checks.BlockedSpawnPointChecker.validateMap
void validateMap(@NotNull final MapModel< G, A, R > mapModel, @NotNull final ErrorCollector< G, A, R > errorCollector)
Validate a map.
Definition: BlockedSpawnPointChecker.java:78
net.sf.gridarta.model.validation.ErrorCollector
An interface for classes that collect errors.
Definition: ErrorCollector.java:33
net.sf.gridarta.model.validation.AbstractValidator
This is the base class for validators.
Definition: AbstractValidator.java:37
net.sf.gridarta.model.validation
This package contains the framework for validating maps.
Definition: AbstractValidator.java:20
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.model.baseobject.BaseObject.LAST_HEAL
String LAST_HEAL
The name of the "last_heal" attribute.
Definition: BaseObject.java:126
net.sf.gridarta.model.baseobject.BaseObject.getHead
T getHead()
Return the head part of a multi-part object.
net.sf.gridarta.model.validation.checks.BlockedSpawnPointChecker
A MapValidator to assert that mobs or spawn points aren't on blocked squares.
Definition: BlockedSpawnPointChecker.java:45
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.validation.errors
Definition: AttributeRangeError.java:20
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.baseobject.BaseObject.getTypeNo
int getTypeNo()
Returns the type number of this Archetype.
net.sf.gridarta.model.validation.checks.BlockedSpawnPointChecker.typeNumbers
final Collection< Integer > typeNumbers
The object types to check.
Definition: BlockedSpawnPointChecker.java:65
net.sf.gridarta.model.validation.checks.BlockedSpawnPointChecker.BlockedSpawnPointChecker
BlockedSpawnPointChecker(@NotNull final ValidatorPreferences validatorPreferences, @NotNull final Integer... typeNumbers)
Creates a new instance.
Definition: BlockedSpawnPointChecker.java:72