Gridarta Editor
ShopSquareChecker.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;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
34 
40 public class ShopSquareChecker<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractShopSquareChecker<G, A, R> {
41 
45  @NotNull
47 
51  @NotNull
53 
57  @Nullable
59 
69  public ShopSquareChecker(@NotNull final ValidatorPreferences validatorPreferences, @NotNull final GameObjectMatcher shopMatcher, @NotNull final GameObjectMatcher noSpellsMatcher, @Nullable final GameObjectMatcher blockedMatcher) {
70  super(validatorPreferences);
71  this.shopMatcher = shopMatcher;
72  this.noSpellsMatcher = noSpellsMatcher;
73  this.blockedMatcher = blockedMatcher;
74  }
75 
76  @Override
77  public void validateMap(@NotNull final MapModel<G, A, R> mapModel, @NotNull final ErrorCollector<G, A, R> errorCollector) {
78  final boolean[][] shopSquares = findMatchingSquares(mapModel, shopMatcher);
79  final boolean[][] noSpellsSquares = findMatchingSquares(mapModel, noSpellsMatcher);
80  final boolean[][] blockedSquares = blockedMatcher == null ? null : findMatchingSquares(mapModel, blockedMatcher);
81  final Point point = new Point();
82  for (point.x = 0; point.x < shopSquares.length && point.x < noSpellsSquares.length; point.x++) {
83  for (point.y = 0; point.y < shopSquares[point.x].length && point.y < noSpellsSquares[point.x].length; point.y++) {
84  if (shopSquares[point.x][point.y]) {
85  if (!noSpellsSquares[point.x][point.y]) {
86  errorCollector.collect(new ShopSquareError<>(mapModel.getMapSquare(point)));
87  } else if (blockedSquares != null && hasAdjacentNonBlockedSpellsAllowedSquare(noSpellsSquares, blockedSquares, point)) {
88  errorCollector.collect(new ShopSquare2Error<>(mapModel.getMapSquare(point)));
89  }
90  }
91  }
92  }
93  }
94 
103  private static boolean hasAdjacentNonBlockedSpellsAllowedSquare(final boolean @NotNull [] @NotNull [] noSpellsSquares, final boolean @NotNull [] @NotNull [] blockedSquares, @NotNull final Point point) {
104  if (blockedSquares[point.x][point.y]) {
105  return false;
106  }
107 
108  for (int dx = -1; dx <= +1; dx++) {
109  for (int dy = -1; dy <= +1; dy++) {
110  final int x = point.x + dx;
111  final int y = point.y + dy;
112  //noinspection ProhibitedExceptionCaught
113  try {
114  if (!noSpellsSquares[x][y] && !blockedSquares[x][y]) {
115  return true;
116  }
117  } catch (final ArrayIndexOutOfBoundsException ignored) {
118  // ignore: expected for points at the border of the map
119  }
120  }
121  }
122  return false;
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
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.model.validation.checks.ShopSquareChecker.blockedMatcher
final GameObjectMatcher blockedMatcher
The GameObjectMatcher for finding blocked squares.
Definition: ShopSquareChecker.java:58
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.validation.errors.ShopSquareError
A SquareValidationError for shop tiles that allow spells or prayers.
Definition: ShopSquareError.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
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.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.validation.checks.ShopSquareChecker
A net.sf.gridarta.model.validation.MapValidator to detect shop squares which allow magic or prayers.
Definition: ShopSquareChecker.java:40
net.sf.gridarta.model.validation.ErrorCollector
An interface for classes that collect errors.
Definition: ErrorCollector.java:33
net.sf.gridarta.model.validation.checks.ShopSquareChecker.noSpellsMatcher
final GameObjectMatcher noSpellsMatcher
The GameObjectMatcher for finding no-spell squares.
Definition: ShopSquareChecker.java:52
net.sf.gridarta.model.validation
This package contains the framework for validating maps.
Definition: AbstractValidator.java:20
net.sf.gridarta.model.validation.checks.ShopSquareChecker.ShopSquareChecker
ShopSquareChecker(@NotNull final ValidatorPreferences validatorPreferences, @NotNull final GameObjectMatcher shopMatcher, @NotNull final GameObjectMatcher noSpellsMatcher, @Nullable final GameObjectMatcher blockedMatcher)
Creates a new instance.
Definition: ShopSquareChecker.java:69
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.validation.checks.ShopSquareChecker.validateMap
void validateMap(@NotNull final MapModel< G, A, R > mapModel, @NotNull final ErrorCollector< G, A, R > errorCollector)
Definition: ShopSquareChecker.java:77
net.sf.gridarta.model.validation.errors
Definition: AttributeRangeError.java:20
net.sf.gridarta.model.validation.errors.ShopSquare2Error
A SquareValidationError for shop tiles that have an adjacent map square that is not blocked and does ...
Definition: ShopSquare2Error.java:34
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.validation.checks.AbstractShopSquareChecker
A MapValidator to detect shop squares which allow magic or prayers, or which have adjacent squares th...
Definition: AbstractShopSquareChecker.java:40
net.sf.gridarta.model.validation.checks.AbstractShopSquareChecker< G, A, R >::findMatchingSquares
boolean[][] findMatchingSquares( @NotNull final MapModel< G, A, R > mapModel, @NotNull final GameObjectMatcher matcher)
Returns a.
Definition: AbstractShopSquareChecker.java:57
net.sf.gridarta.model.validation.checks.ShopSquareChecker.hasAdjacentNonBlockedSpellsAllowedSquare
static boolean hasAdjacentNonBlockedSpellsAllowedSquare(final boolean @NotNull[] @NotNull[] noSpellsSquares, final boolean @NotNull[] @NotNull[] blockedSquares, @NotNull final Point point)
Returns whether a given square is not blocked and has a non-blocked adjacent square that allows spell...
Definition: ShopSquareChecker.java:103
net.sf.gridarta.model.validation.checks.ShopSquareChecker.shopMatcher
final GameObjectMatcher shopMatcher
The GameObjectMatcher for finding shop squares.
Definition: ShopSquareChecker.java:46