Gridarta Editor
BlockedSquareChecker.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.ArrayList;
24 import java.util.Collection;
36 import net.sf.gridarta.utils.Size2D;
37 import org.jetbrains.annotations.NotNull;
38 
45 public class BlockedSquareChecker<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 
52  super(validatorPreferences);
53  }
54 
55  @Override
56  public void validateMap(@NotNull final MapModel<G, A, R> mapModel, @NotNull final ErrorCollector<G, A, R> errorCollector) {
57  for (final MapSquare<G, A, R> completelyBlockedSquare : findCompletelyBlockedSquares(mapModel)) {
58  errorCollector.collect(new BlockedSquareError<>(completelyBlockedSquare));
59  }
60  }
61 
67  public static <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> Iterable<MapSquare<G, A, R>> findCompletelyBlockedSquares(@NotNull final MapModel<G, A, R> mapModel) {
68  final Size2D mapSize = mapModel.getMapArchObject().getMapSize();
69  final int mapWidth = mapSize.getWidth();
70  final int mapHeight = mapSize.getHeight();
71  // The blockedMatrix is 1 row larger than the map on each border to ease two things:
72  // First, it's not necessary to do a bounds check then.
73  // Second, it will cope with the future requirement to perform this check with respect of tiled maps.
74  final boolean[][] blockedMatrix = new boolean[mapWidth + 2][mapHeight + 2];
75  final Point pos = new Point();
76  for (int x = 0; x < mapWidth; x++) {
77  pos.x = x;
78  for (int y = 0; y < mapHeight; y++) {
79  pos.y = y;
80  if (isBlocked(mapModel.getMapSquare(pos))) {
81  blockedMatrix[x + 1][y + 1] = true;
82  }
83  }
84  }
85  final Collection<MapSquare<G, A, R>> completelyBlockedSquares = new ArrayList<>();
86  for (int x = 0; x < mapWidth; x++) {
87  pos.x = x;
88  for (int y = 0; y < mapHeight; y++) {
89  pos.y = y;
90  if (and9x9(blockedMatrix, x + 1, y + 1)) {
91  completelyBlockedSquares.add(mapModel.getMapSquare(pos));
92  }
93  }
94  }
95  return completelyBlockedSquares;
96  }
97 
109  private static boolean and9x9(final boolean @NotNull [] @NotNull [] matrix, final int x, final int y) {
110  return matrix[x - 1][y - 1] && matrix[x - 1][y] && matrix[x - 1][y + 1] && matrix[x][y - 1] && matrix[x][y] && matrix[x][y + 1] && matrix[x + 1][y - 1] && matrix[x + 1][y] && matrix[x + 1][y + 1];
111  }
112 
120  private static boolean isBlocked(@NotNull final Iterable<? extends GameObject<?, ?, ?>> square) {
121  boolean noPass = false;
122  boolean blocksView = false;
123  for (final BaseObject<?, ?, ?, ?> gameObject : square) {
124  if (!noPass) {
125  noPass = gameObject.getAttributeInt(BaseObject.NO_PASS) == 1;
126  }
127  if (!blocksView) {
128  blocksView = gameObject.getAttributeInt(BaseObject.BLOCKSVIEW) == 1;
129  }
130  if (noPass && blocksView) {
131  return true;
132  }
133  }
134  return false;
135  }
136 
137 }
net.sf.gridarta.utils.Size2D.getWidth
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
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.mapmodel.MapSquare
A single Map Square.
Definition: MapSquare.java:45
net.sf.gridarta.model.validation.errors.BlockedSquareError
A ValidationError that refers to a completely blocked map square.
Definition: BlockedSquareError.java:34
net.sf.gridarta.model.validation.checks.BlockedSquareChecker.BlockedSquareChecker
BlockedSquareChecker(@NotNull final ValidatorPreferences validatorPreferences)
Creates a new instance.
Definition: BlockedSquareChecker.java:51
net.sf
net.sf.gridarta.model.baseobject.BaseObject.BLOCKSVIEW
String BLOCKSVIEW
The name of the "blocksview" attribute.
Definition: BaseObject.java:132
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.model.validation.checks.BlockedSquareChecker.isBlocked
static boolean isBlocked(@NotNull final Iterable<? extends GameObject<?, ?, ?>> square)
Return whether a square is blocked and impassable.
Definition: BlockedSquareChecker.java:120
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.checks.BlockedSquareChecker.findCompletelyBlockedSquares
static< G extends GameObject< G, A, R > A extends R extends Archetype< G, A, R > Iterable< MapSquare< G, A, R > > findCompletelyBlockedSquares(@NotNull final MapModel< G, A, R > mapModel)
Definition: BlockedSquareChecker.java:67
net.sf.gridarta.model.validation.checks.BlockedSquareChecker.validateMap
void validateMap(@NotNull final MapModel< G, A, R > mapModel, @NotNull final ErrorCollector< G, A, R > errorCollector)
Validate a map.
Definition: BlockedSquareChecker.java:56
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.validation.checks.BlockedSquareChecker
A MapValidator to assert that a square that's blocked and no_pass is not surrounded by blocked and no...
Definition: BlockedSquareChecker.java:45
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.utils.Size2D.getHeight
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
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.baseobject.BaseObject.NO_PASS
String NO_PASS
The name of the "no_pass" attribute.
Definition: BaseObject.java:102
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
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.validation.checks.BlockedSquareChecker.and9x9
static boolean and9x9(final boolean @NotNull[] @NotNull[] matrix, final int x, final int y)
Returns the.
Definition: BlockedSquareChecker.java:109
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.var.crossfire.model.gameobject.GameObject<?, ?, ?>
net.sf.gridarta.model.validation.errors
Definition: AttributeRangeError.java:20
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20