Gridarta Editor
FloodFill.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.floodfill;
21 
22 import java.awt.Point;
23 import java.util.List;
31 import net.sf.gridarta.utils.Size2D;
32 import org.jetbrains.annotations.NotNull;
33 
57 public class FloodFill<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
58 
62  private static final byte NOT_LOOKED_AT = 0;
63 
64  private static final byte BORDER = 1;
65 
66  private static final byte WAS_EMPTY = 2;
67 
68  private static final byte BLOCKED = 3;
69 
77  public void floodFill(@NotNull final MapModel<G, A, R> mapModel, @NotNull final Point start, @NotNull final List<? extends BaseObject<G, A, R, ?>> archList, @NotNull final InsertionModeSet<G, A, R> insertionModeSet) {
78  final MapArchObject<A> mapArchObject = mapModel.getMapArchObject();
79  final Size2D mapSize = mapArchObject.getMapSize();
80  final byte[][] area = new byte[mapSize.getWidth()][mapSize.getHeight()];
81  area[start.x][start.y] = BORDER;
82  mapModel.beginTransaction("Flood Fill"); // TODO: I18N/L10N
83  try {
84  int border = 1;
85  while (border > 0) {
86  final Point p = new Point();
87  for (p.x = 0; p.x < mapSize.getWidth(); p.x++) {
88  for (p.y = 0; p.y < mapSize.getHeight(); p.y++) {
89  if (area[p.x][p.y] == BORDER) {
90  border--;
91  if (mapArchObject.isPointValid(p) && mapModel.getMapSquare(p).isEmpty()) {
92  area[p.x][p.y] = WAS_EMPTY;
93  final BaseObject<G, A, R, ?> gameObject = archList.get(RandomUtils.RND.nextInt(archList.size()));
94  mapModel.insertBaseObject(gameObject, p, false, false, insertionModeSet.getTopmostInsertionMode());
95  //noinspection ProhibitedExceptionCaught
96  try {
97  if (area[p.x - 1][p.y] == NOT_LOOKED_AT) {
98  area[p.x - 1][p.y] = BORDER;
99  border++;
100  }
101  } catch (final ArrayIndexOutOfBoundsException ignored) {
102  // ignore
103  }
104  //noinspection ProhibitedExceptionCaught
105  try {
106  if (area[p.x + 1][p.y] == NOT_LOOKED_AT) {
107  area[p.x + 1][p.y] = BORDER;
108  border++;
109  }
110  } catch (final ArrayIndexOutOfBoundsException ignored) {
111  // ignore
112  }
113  //noinspection ProhibitedExceptionCaught
114  try {
115  if (area[p.x][p.y - 1] == NOT_LOOKED_AT) {
116  area[p.x][p.y - 1] = BORDER;
117  border++;
118  }
119  } catch (final ArrayIndexOutOfBoundsException ignored) {
120  // ignore
121  }
122  //noinspection ProhibitedExceptionCaught
123  try {
124  if (area[p.x][p.y + 1] == NOT_LOOKED_AT) {
125  area[p.x][p.y + 1] = BORDER;
126  border++;
127  }
128  } catch (final ArrayIndexOutOfBoundsException ignored) {
129  // ignore
130  }
131  } else {
132  area[p.x][p.y] = BLOCKED;
133  }
134  }
135  }
136  }
137  }
138  } finally {
139  mapModel.endTransaction();
140  }
141  }
142 
143 }
net.sf.gridarta.utils.RandomUtils
Random number utilities.
Definition: RandomUtils.java:28
net.sf.gridarta.utils.Size2D.getWidth
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
net.sf.gridarta.model.floodfill.FloodFill.BORDER
static final byte BORDER
Definition: FloodFill.java:64
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.model.mapmodel.InsertionModeSet
A set of InsertionModes.
Definition: InsertionModeSet.java:33
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.maparchobject.MapArchObject.isPointValid
boolean isPointValid(@Nullable Point pos)
Checks whether the given coordinate is within map bounds.
net.sf
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
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.maparchobject.MapArchObject.getMapSize
Size2D getMapSize()
Returns the map size.
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.floodfill.FloodFill.WAS_EMPTY
static final byte WAS_EMPTY
Definition: FloodFill.java:66
net.sf.gridarta.utils.RandomUtils.RND
static final Random RND
Global random number generator.
Definition: RandomUtils.java:33
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
area
This document describes some hints and requirements for general development on the CrossfireEditor If you plan to make changes to the editor code or setup please read the following and keep it in derived from a basic editor application called Gridder by Pasi Ker�nen so please communicate with best through the cf devel mailing before considering any fundamental changes About code DO NOT USE TABS No matter what Java development platform you are please configure insert indent Tabs are displayed totally different in every editor and there are millions of different editors out there The insertion of tabs in the source code is messing up the syntax formatting in a way that is UNREPAIRABLE Apart from please keep code indentation accurate This is not just good it helps to keep code readable and in that way dramatically decreases the chance for overlooked bugs Everyone is welcomed to correct indentation errors wherever they are spotted Before you start to do this please double check that your editor is really configured to insert spaces Line feeds may be checked in either in windows or in unix linux style All reasonable text and java editors can deal with both linefeed formats Converting line feeds is but in this case please make sure that only linefeed characters are changed and nothing else is affected Due to the platform independent nature of the editor has the potential to run on almost any given operating system the build process differs greatly between systems as well as java environments In the several people have attempted to add build scripts along with structural changes to optimize the setup on one particular system environment which has led to conflict Please do *not *attempt to change the structure or any directories for the mere purpose of improving a build process or performance in a java environment Build scripts may be placed in the root it would be especially fine if it is just one or two files but the latter is not required Please excuse me for placing such restriction I and many users of the editor greatly appreciate build scripts We just had some real troubles over this issue in the past and I don t want to have them repeated the editor has relatively high performance requirements I ve spent a lot of extra work to keep everything as fast and memory efficient as possible when you add new data fields or calculations in the archetype area
Definition: Developer_README.txt:57
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.floodfill.FloodFill.BLOCKED
static final byte BLOCKED
Definition: FloodFill.java:68
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.floodfill.FloodFill.NOT_LOOKED_AT
static final byte NOT_LOOKED_AT
Constants for lookArea.
Definition: FloodFill.java:62
net.sf.gridarta.model.floodfill.FloodFill
Implements flood-filling.
Definition: FloodFill.java:57
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
net.sf.gridarta.model.floodfill.FloodFill.floodFill
void floodFill(@NotNull final MapModel< G, A, R > mapModel, @NotNull final Point start, @NotNull final List<? extends BaseObject< G, A, R, ?>> archList, @NotNull final InsertionModeSet< G, A, R > insertionModeSet)
Flood-fill the map.
Definition: FloodFill.java:77