Gridarta Editor
ReplaceUtils.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.gui.dialog.replace;
21 
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.List;
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
39 
44 public class ReplaceUtils {
45 
49  private ReplaceUtils() {
50  }
51 
62  public static <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> int replace(@NotNull final MatchCriteria<G, A, R> matchCriteria, final boolean entireMap, final boolean deleteOnly, final int replaceDensity, @NotNull final MapView<G, A, R> mapView, @NotNull final InsertionModeSet<G, A, R> insertionModeSet, @Nullable final List<? extends BaseObject<G, A, R, ?>> replaceList) {
63  final int replaceListSize = replaceList == null ? 0 : replaceList.size();
64  final MapControl<G, A, R> mapControl = mapView.getMapControl();
65  final MapModel<G, A, R> mapModel = mapControl.getMapModel();
66  mapModel.beginTransaction("Replace"); // TODO: I18N/L10N
67  try {
68  int replaceCount = 0;
69  final Collection<G> objectsToReplace = new ArrayList<>();
70  for (final MapSquare<G, A, R> square : entireMap ? mapModel : mapView.getSelectedSquares()) {
71  // Operate on a copy of the nodes to prevent ConcurrentModificationException
72 
73  // find objects to replace
74  objectsToReplace.clear();
75  for (final G node : square) {
76  if (node.isHead() && matchCriteria.matches(node)) {
77  if (replaceDensity > RandomUtils.RND.nextInt(100)) {
78  objectsToReplace.add(node);
79  }
80  }
81  }
82 
83  // actually replace the objects
84  for (final G objectToReplace : objectsToReplace) {
85  final Iterator<G> it = square.iterator();
86  G prevArch = null;
87  G node = null;
88  while (it.hasNext()) {
89  node = it.next();
90 
91  if (node == objectToReplace) {
92  break;
93  }
94 
95  prevArch = node;
96  }
97  assert node != null;
98 
99  // first, delete the old arch
100  node.remove();
101 
102  if (replaceListSize > 0 && !deleteOnly) {
103  final BaseObject<G, A, R, ?> randomArch = replaceList.get(replaceListSize == 1 ? 0 : RandomUtils.RND.nextInt(replaceList.size()));
104  final G newGameObject;
105  // insert replacement object
106  //noinspection SimplifiableIfStatement
107  if (randomArch.isMulti()) {
108  newGameObject = mapModel.insertBaseObject(randomArch, square.getMapLocation(), false, false, insertionModeSet.getTopmostInsertionMode());
109  } else {
110  newGameObject = mapModel.insertArchToMap(randomArch, prevArch, square.getMapLocation(), false);
111  }
112  if (newGameObject != null) {
113  newGameObject.propagateElevation(node);
114  }
115  }
116  replaceCount++;
117  }
118  }
119  return replaceCount;
120  } finally {
121  mapModel.endTransaction();
122  }
123  }
124 
125 }
net.sf.gridarta.model.mapmodel.MapModel.insertArchToMap
G insertArchToMap(@NotNull BaseObject< G, A, R, ?> templateBaseObject, @Nullable G nextGameObject, @NotNull Point pos, boolean join)
Insert a game object to the map at a specified position.
net.sf.gridarta.utils.RandomUtils
Random number utilities.
Definition: RandomUtils.java:28
net.sf.gridarta.gui.dialog.replace.ReplaceUtils.replace
static< G extends GameObject< G, A, R > A extends R extends Archetype< G, A, R > int replace(@NotNull final MatchCriteria< G, A, R > matchCriteria, final boolean entireMap, final boolean deleteOnly, final int replaceDensity, @NotNull final MapView< G, A, R > mapView, @NotNull final InsertionModeSet< G, A, R > insertionModeSet, @Nullable final List<? extends BaseObject< G, A, R, ?>> replaceList)
Definition: ReplaceUtils.java:62
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.model.baseobject.BaseObject.isMulti
boolean isMulti()
Returns whether this Archetype is a multi-part object.
net.sf.gridarta.model.mapmodel.MapModel.insertBaseObject
G insertBaseObject(@NotNull BaseObject< G, A, R, ?> baseObject, @NotNull Point pos, boolean allowMany, boolean join, @NotNull InsertionMode insertionMode)
Inserts a BaseObject to a map.
net.sf.gridarta.model.select.MatchCriteria
Criteria for game object matchers.
Definition: MatchCriteria.java:31
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.mapmodel.MapModel.endTransaction
void endTransaction()
End a transaction.
net.sf
net.sf.gridarta.model.mapmodel.MapModel.beginTransaction
void beginTransaction(@NotNull String name)
Starts a new transaction.
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.model.baseobject.BaseObject.getMapLocation
Point getMapLocation()
Returns the coordinate of this GameObject on its map.
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.select
Definition: ArchetypeNameMatchCriteria.java:20
net.sf.gridarta.model.mapcontrol
Definition: DefaultMapControl.java:20
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
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.gui.map.mapview
Definition: AbstractMapView.java:20
net.sf.gridarta.gui.map.mapview.MapView
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
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.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.gui.map
Base classes for rendering maps.
Definition: AbstractPerMapDialogManager.java:20
net.sf.gridarta.gui.dialog.replace.ReplaceUtils
Utility class to replace objects in a map.
Definition: ReplaceUtils.java:44
net.sf.gridarta.model.mapcontrol.MapControl
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
net.sf.gridarta.model.mapcontrol.MapControl.getMapModel
MapModel< G, A, R > getMapModel()
Returns the map model.
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.gui.dialog.replace.ReplaceUtils.ReplaceUtils
ReplaceUtils()
Private constructor to prevent instantiation.
Definition: ReplaceUtils.java:49
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
it
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 please make sure they are as efficient as possible and worth both the time and space they consume Now don t be afraid too much No development would be possible without adding calculations and data at all Just bear in mind unlike for many other open source performance does make a difference for the CrossfireEditor The for as many systems as possible In case you are unexperienced with java and note that the graphics look different on every and with every font They also have different sizes proportions and behave different A seemingly trivial and effectless change can wreck havoc for the same GUI run on another system please don t be totally afraid of it
Definition: Developer_README.txt:76