Gridarta Editor
EnvironmentChecker.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.util.Arrays;
23 import java.util.IdentityHashMap;
24 import java.util.Map;
35 import org.jetbrains.annotations.NotNull;
36 
43 public class EnvironmentChecker<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractValidator<G, A, R> implements GameObjectValidator<G, A, R> {
44 
48  @NotNull
49  private final Map<ArchetypeType, Void> noMap = new IdentityHashMap<>();
50 
55  @NotNull
56  private final Map<ArchetypeType, int @NotNull []> inv = new IdentityHashMap<>();
57 
63  super(validatorPreferences);
64  }
65 
70  public void addNoMap(@NotNull final ArchetypeType archetypeType) {
71  noMap.put(archetypeType, null);
72  }
73 
80  public void addInv(@NotNull final ArchetypeType archetypeType, final int @NotNull [] types) {
81  if (inv.containsKey(archetypeType)) {
82  throw new IllegalArgumentException("archetype type has already been set: " + archetypeType);
83  }
84 
85  final int[] tmp = types.clone();
86  Arrays.sort(tmp);
87  inv.put(archetypeType, tmp);
88  }
89 
90  @Override
91  public void validateGameObject(@NotNull final G gameObject, @NotNull final ErrorCollector<G, A, R> errorCollector) {
92  final G envGameObject = gameObject.getContainerGameObject();
93  if (envGameObject == null) {
94  for (final ArchetypeType archetypeType : noMap.keySet()) {
95  if (archetypeType.matches(gameObject)) {
96  errorCollector.collect(new EnvironmentMapError<>(gameObject, archetypeType.getTypeName()));
97  break;
98  }
99  }
100  } else {
101  for (final Map.Entry<ArchetypeType, int[]> entry : inv.entrySet()) {
102  final ArchetypeType archetypeType = entry.getKey();
103  if (archetypeType.matches(gameObject)) {
104  final int[] types = entry.getValue();
105  if (types != null && Arrays.binarySearch(types, envGameObject.getTypeNo()) < 0) {
106  final String typeDescription = archetypeType.getTypeName();
107  final String envTypeDescription = Integer.toString(envGameObject.getTypeNo()); // XXX: use envArchetypeType.getTypeName()
108  errorCollector.collect(new EnvironmentInvError<>(gameObject, typeDescription, envTypeDescription));
109  }
110  }
111  }
112  }
113  }
114 
115 }
net.sf.gridarta.model.archetypetype
Defines types of GameObjects with corresponding attributes.
Definition: AbstractArchetypeAttributeInvSpell.java:20
net.sf.gridarta.model.validation.checks.EnvironmentChecker.addNoMap
void addNoMap(@NotNull final ArchetypeType archetypeType)
Marks an ArchetypeType to not be allowed directly on maps.
Definition: EnvironmentChecker.java:70
net.sf.gridarta.model.archetypetype.ArchetypeType.matches
boolean matches(@NotNull final BaseObject<?, ?, ?, ?> baseObject)
Checks whether a BaseObject matches all type attributes.
Definition: ArchetypeType.java:216
net.sf.gridarta.model.validation.checks.EnvironmentChecker.inv
final Map< ArchetypeType, int @NotNull[]> inv
Maps ArchetypeTypes to allowed environment types.
Definition: EnvironmentChecker.java:56
net.sf.gridarta.model.validation.errors.EnvironmentMapError
Validation error that's used when the EnvironmentChecker detected a possible error on the map.
Definition: EnvironmentMapError.java:33
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.validation.checks.EnvironmentChecker.addInv
void addInv(@NotNull final ArchetypeType archetypeType, final int @NotNull[] types)
Sets the allowed environment game object types for an {}.
Definition: EnvironmentChecker.java:80
net.sf.gridarta.model.archetypetype.ArchetypeType.getTypeName
String getTypeName()
Returns the type name (artificial).
Definition: ArchetypeType.java:160
net.sf
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.AbstractValidator.validatorPreferences
final ValidatorPreferences validatorPreferences
The ValidatorPreferences to use.
Definition: AbstractValidator.java:55
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.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.validation.checks.EnvironmentChecker.validateGameObject
void validateGameObject(@NotNull final G gameObject, @NotNull final ErrorCollector< G, A, R > errorCollector)
Validates a game object.
Definition: EnvironmentChecker.java:91
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.validation.checks.EnvironmentChecker.noMap
final Map< ArchetypeType, Void > noMap
The ArchetypeTypes that are not allowed on maps.
Definition: EnvironmentChecker.java:49
net.sf.gridarta.model.validation.checks.EnvironmentChecker
A GameObjectValidator that checks for valid environment.
Definition: EnvironmentChecker.java:43
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.validation.errors.EnvironmentInvError
Validation error that's used when the EnvironmentChecker detected a possible error on the map.
Definition: EnvironmentInvError.java:33
net.sf.gridarta.model.validation.GameObjectValidator
Interface for GameObject Validators.
Definition: GameObjectValidator.java:32
net.sf.gridarta.model.validation.errors
Definition: AttributeRangeError.java:20
net.sf.gridarta.model.archetypetype.ArchetypeType
Contains the data of one Gridarta Object-Type.
Definition: ArchetypeType.java:35
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.validation.checks.EnvironmentChecker.EnvironmentChecker
EnvironmentChecker(@NotNull final ValidatorPreferences validatorPreferences)
Creates a new instance.
Definition: EnvironmentChecker.java:62