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-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.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(@NotNull final boolean[][] 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 }
A ValidationError that refers to a completely blocked map square.
static boolean and9x9(@NotNull final boolean[][] matrix, final int x, final int y)
Returns the.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
This is the base class for validators.
static boolean isBlocked(@NotNull final Iterable<? extends GameObject<?, ?, ?>> square)
Return whether a square is blocked and impassable.
This package contains the framework for validating maps.
String NO_PASS
The name of the "no_pass" 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.
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
final ValidatorPreferences validatorPreferences
The ValidatorPreferences to use.
An interface for classes that collect errors.
String BLOCKSVIEW
The name of the "blocksview" attribute.
BlockedSquareChecker(@NotNull final ValidatorPreferences validatorPreferences)
Creates a new instance.
A MapValidator to assert that a square that&#39;s blocked and no_pass is not surrounded by blocked and no...
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
void validateMap(@NotNull final MapModel< G, A, R > mapModel, @NotNull final ErrorCollector< G, A, R > errorCollector)
Validate a map.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30