Gridarta Editor
ExitConnectorActions.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.actions;
21 
22 import java.awt.Point;
23 import java.io.IOException;
39 import org.jetbrains.annotations.NotNull;
40 import org.jetbrains.annotations.Nullable;
41 
47 public class ExitConnectorActions<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
48 
52  @NotNull
54 
58  @NotNull
60 
64  @NotNull
66 
70  @NotNull
72 
76  @NotNull
78 
82  @NotNull
84 
95  this.exitConnectorModel = exitConnectorModel;
96  this.exitMatcher = exitMatcher;
97  this.archetypeSet = archetypeSet;
98  this.mapManager = mapManager;
99  this.fileControl = fileControl;
100  this.insertionModeSet = insertionModeSet;
101  }
102 
110  public boolean doExitCopy(final boolean performAction, @NotNull final MapControl<G, A, R> mapControl, @NotNull final Point location) {
111  final MapModel<G, A, R> mapModel = mapControl.getMapModel();
112  final MapFile mapFile = mapModel.getMapFile();
113  if (mapFile == null) {
114  // unsaved maps do not have a map path ==> no location to remember
115  return false;
116  }
117 
118  if (performAction) {
119  final ExitLocation exitLocation = new ExitLocation(mapFile, location, mapModel.getMapArchObject().getMapName());
120  exitConnectorModel.setExitLocation(exitLocation);
121  }
122 
123  return true;
124  }
125 
133  public boolean doExitPaste(final boolean performAction, @NotNull final MapControl<G, A, R> mapControl, @NotNull final Point targetLocation) {
134  final ExitLocation sourceExitLocation = exitConnectorModel.getExitLocation();
135  if (sourceExitLocation == null) {
136  return false;
137  }
138 
139  final MapModel<G, A, R> targetMapModel = mapControl.getMapModel();
140  @Nullable final BaseObject<G, A, R, ?> targetExit;
141  //XXX final G selectedExit = exitMatcher.getValidExit(selectedSquareModel.getSelectedGameObject());
142  //XXX if (selectedExit != null) {
143  //XXX targetExit = selectedExit;
144  //XXX } else {
145  final BaseObject<G, A, R, ?> cursorExit = exitMatcher.getExit(targetMapModel, targetLocation);
146  if (cursorExit != null) {
147  targetExit = cursorExit;
148  } else if (exitConnectorModel.isAutoCreateExit()) {
149  targetExit = null;
150  } else {
151  return false;
152  }
153 
154  final MapFile targetMapFile = targetMapModel.getMapFile();
155 
156  if (targetExit == null) {
157  // paste into newly created exit game object
158 
159  final BaseObject<G, A, R, ?> targetArchetype;
160  try {
162  } catch (final UndefinedArchetypeException ignored) {
163  return false;
164  }
165 
166  if (performAction) {
167  if (!pasteExit(targetLocation, targetMapModel, targetArchetype, sourceExitLocation, targetMapFile)) {
168  return false;
169  }
170  }
171  } else {
172  // paste into existing exit
173 
174  if (performAction) {
175  pasteExit(targetExit, targetMapModel, sourceExitLocation, targetMapFile);
176  }
177  }
178 
179  return true;
180  }
181 
189  public boolean doExitConnect(final boolean performAction, @NotNull final MapControl<G, A, R> mapControl, @NotNull final Point targetLocation) {
190  final ExitLocation sourceExitLocation = exitConnectorModel.getExitLocation();
191  if (sourceExitLocation == null) {
192  return false;
193  }
194 
195  final MapModel<G, A, R> targetMapModel = mapControl.getMapModel();
196  final MapFile targetMapFile = targetMapModel.getMapFile();
197  if (targetMapFile == null) {
198  // target map file is unsaved ==> no location to connect
199  return false;
200  }
201 
202  @Nullable final BaseObject<G, A, R, ?> targetExit;
203  @Nullable final BaseObject<G, A, R, ?> targetArchetype;
204  //XXX final G selectedExit = exitMatcher.getValidExit(selectedSquareModel.getSelectedGameObject());
205  //XXX if (selectedExit != null) {
206  //XXX targetExit = selectedExit;
207  //XXX targetArchetype = null;
208  //XXX } else {
209  final BaseObject<G, A, R, ?> cursorExit = exitMatcher.getExit(targetMapModel, targetLocation);
210  if (cursorExit != null) {
211  targetExit = cursorExit;
212  targetArchetype = null;
213  } else if (exitConnectorModel.isAutoCreateExit()) {
214  targetExit = null;
215  try {
217  } catch (final UndefinedArchetypeException ignored) {
218  return false;
219  }
220  } else {
221  return false;
222  }
223 
224  final MapControl<G, A, R> sourceMapControl;
225  try {
226  sourceMapControl = mapManager.openMapFile(sourceExitLocation.getMapFile(), false);
227  } catch (final IOException ex) {
228  fileControl.reportLoadError(sourceExitLocation.getMapFile().getFile(), ex.getMessage());
229  return false;
230  }
231  try {
232  return doExitConnect(performAction, targetExit, targetArchetype, targetMapModel, targetLocation, targetMapFile, sourceMapControl.getMapModel(), sourceExitLocation);
233  } finally {
234  try {
235  // XXX: remove hack when MapManager automatically saves released maps
236  if (sourceMapControl.getMapModel().isModified() && sourceMapControl.getUseCounter() <= 1) {
237  try {
238  sourceMapControl.save();
239  } catch (final IOException ex) {
240  fileControl.reportSaveError(sourceMapControl, ex.getMessage());
241  }
242  }
243  } finally {
244  mapManager.release(sourceMapControl);
245  }
246  }
247  }
248 
263  private boolean doExitConnect(final boolean performAction, @Nullable final BaseObject<?, ?, ?, ?> targetExit, @Nullable final BaseObject<G, A, R, ?> targetArchetype, @NotNull final MapModel<G, A, R> targetMapModel, @NotNull final Point targetLocation, @NotNull final MapFile targetMapFile, @NotNull final MapModel<G, A, R> sourceMapModel, @NotNull final ExitLocation sourceExitLocation) {
264  final MapFile sourceMapFile = sourceMapModel.getMapFile();
265  if (sourceMapFile == null) {
266  // source map is unsaved ==> cannot connect
267  return false;
268  }
269 
270  final Point sourceLocation = sourceExitLocation.getMapCoordinate();
271 
272  @Nullable final BaseObject<G, A, R, ?> sourceExit;
273  @Nullable final BaseObject<G, A, R, ?> sourceArchetype;
274  final BaseObject<G, A, R, ?> exit = exitMatcher.getExit(sourceMapModel, sourceLocation);
275  if (exit != null) {
276  sourceExit = exit;
277  sourceArchetype = null;
278  } else if (exitConnectorModel.isAutoCreateExit()) {
279  sourceExit = null;
280  try {
282  } catch (final UndefinedArchetypeException ignored) {
283  return false;
284  }
285  } else {
286  return false;
287  }
288 
289  if (performAction) {
290  final ExitLocation targetExitLocation = new ExitLocation(targetMapFile, targetLocation, targetMapModel.getMapArchObject().getMapName());
291  if (sourceExit == null) {
292  if (!pasteExit(sourceLocation, sourceMapModel, sourceArchetype, targetExitLocation, sourceMapFile)) {
293  return false;
294  }
295  } else {
296  pasteExit(sourceExit, sourceMapModel, targetExitLocation, sourceMapFile);
297  }
298 
299  if (targetExit == null) {
300  assert targetArchetype != null;
301  if (!pasteExit(targetLocation, targetMapModel, targetArchetype, sourceExitLocation, targetMapFile)) {
302  return false;
303  }
304  } else {
305  pasteExit(targetExit, targetMapModel, sourceExitLocation, targetMapFile);
306  }
307  }
308 
309  return true;
310  }
311 
320  private void pasteExit(@NotNull final BaseObject<?, ?, ?, ?> gameObject, @NotNull final MapModel<G, A, R> mapModel, @NotNull final ExitLocation exitLocation, @Nullable final MapFile gameObjectMapFile) {
321  mapModel.beginTransaction("paste exit");
322  try {
323  exitLocation.updateExitObject(gameObject, exitConnectorModel.isPasteExitName(), gameObjectMapFile);
324  } finally {
325  mapModel.endTransaction();
326  }
327  }
328 
339  private boolean pasteExit(@NotNull final Point location, @NotNull final MapModel<G, A, R> mapModel, @NotNull final BaseObject<G, A, R, ?> archetype, @NotNull final ExitLocation exitLocation, @Nullable final MapFile mapFile) {
340  mapModel.beginTransaction("paste exit");
341  try {
342  final BaseObject<G, A, R, ?> newExit = mapModel.insertBaseObject(archetype, location, true, false, insertionModeSet.getTopmostInsertionMode());
343  if (newExit == null) {
344  return false;
345  }
346 
347  exitLocation.updateExitObject(newExit, exitConnectorModel.isPasteExitName(), mapFile);
348  } finally {
349  mapModel.endTransaction();
350  }
351 
352  return true;
353  }
354 
355 }
net.sf.gridarta.model.mapmodel.InsertionModeSet.getTopmostInsertionMode
InsertionMode getTopmostInsertionMode()
Returns the "topmost" insertion mode.
Definition: InsertionModeSet.java:98
net.sf.gridarta.model.mapmanager
Definition: AbstractMapManager.java:20
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.model.archetypeset.ArchetypeSet.getArchetype
R getArchetype(@NotNull String archetypeName)
Returns an Archetype by its name.
net.sf.gridarta.model.mapmodel.MapModel.getMapArchObject
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
net.sf.gridarta.model.mapmodel.InsertionModeSet
A set of InsertionModes.
Definition: InsertionModeSet.java:33
net.sf.gridarta.model.mapmanager.MapManager
A MapManager manages all opened maps.
Definition: MapManager.java:37
net.sf.gridarta.actions.ExitConnectorActions.ExitConnectorActions
ExitConnectorActions(@NotNull final ExitConnectorModel exitConnectorModel, @NotNull final ExitMatcher< G, A, R > exitMatcher, @NotNull final ArchetypeSet< G, A, R > archetypeSet, @NotNull final MapManager< G, A, R > mapManager, @NotNull final FileControl< G, A, R > fileControl, @NotNull final InsertionModeSet< G, A, R > insertionModeSet)
Creates a new instance.
Definition: ExitConnectorActions.java:94
net.sf.gridarta.model.exitconnector.ExitLocation
Stores information about a remembered exit location.
Definition: ExitLocation.java:35
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.actions.ExitConnectorActions.fileControl
final FileControl< G, A, R > fileControl
The FileControl to use.
Definition: ExitConnectorActions.java:77
net.sf.gridarta.model.archetype.UndefinedArchetypeException
Exception thrown if an Archetype does not exist.
Definition: UndefinedArchetypeException.java:28
net.sf.gridarta.actions.ExitConnectorActions.mapManager
final MapManager< G, A, R > mapManager
The MapManager for loading maps.
Definition: ExitConnectorActions.java:71
net.sf
net.sf.gridarta.model.exitconnector.ExitConnectorModel.setExitLocation
void setExitLocation(@Nullable ExitLocation exitLocation)
Sets the remembered exit location.
net.sf.gridarta.model.mapmanager.FileControl
Definition: FileControl.java:30
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.model.exitconnector.ExitMatcher.getExit
G getExit(@NotNull final MapModel< G, A, R > mapModel, @Nullable final Point point)
Returns an exit game object on a given map square.
Definition: ExitMatcher.java:102
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.mapmanager.FileControl.reportLoadError
void reportLoadError(@Nullable File file, @NotNull String message)
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.mapcontrol
Definition: DefaultMapControl.java:20
net.sf.gridarta.model.exitconnector.ExitMatcher
Selects valid exit game objects from maps.
Definition: ExitMatcher.java:36
net.sf.gridarta.model.mapmodel.MapModel.isModified
boolean isModified()
Return whether the map has been modified from the on-disk state.
net.sf.gridarta.actions.ExitConnectorActions.doExitCopy
boolean doExitCopy(final boolean performAction, @NotNull final MapControl< G, A, R > mapControl, @NotNull final Point location)
Executes the "exit copy" action.
Definition: ExitConnectorActions.java:110
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net.sf.gridarta.model.exitconnector.ExitLocation.getMapFile
MapFile getMapFile()
Returns the file of the map that contains the remembered exit.
Definition: ExitLocation.java:119
net
net.sf.gridarta.actions.ExitConnectorActions.doExitConnect
boolean doExitConnect(final boolean performAction, @Nullable final BaseObject<?, ?, ?, ?> targetExit, @Nullable final BaseObject< G, A, R, ?> targetArchetype, @NotNull final MapModel< G, A, R > targetMapModel, @NotNull final Point targetLocation, @NotNull final MapFile targetMapFile, @NotNull final MapModel< G, A, R > sourceMapModel, @NotNull final ExitLocation sourceExitLocation)
Executes part of the "exit connect" action.
Definition: ExitConnectorActions.java:263
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.mapmodel.MapFile.getFile
File getFile()
Returns a File for this map file.
Definition: MapFile.java:102
net.sf.gridarta.model.mapmanager.MapManager.openMapFile
MapControl< G, A, R > openMapFile(@NotNull MapFile mapFile, boolean interactive)
Loads a map file.
net.sf.gridarta.model.mapmanager.FileControl.reportSaveError
void reportSaveError(@NotNull MapControl< G, A, R > mapControl, @NotNull String message)
Reports an error while saving a map file to the user.
net.sf.gridarta.model.archetypeset.ArchetypeSet
Interface that captures similarities between different ArchetypeSet implementations.
Definition: ArchetypeSet.java:37
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.model.mapcontrol.MapControl.save
void save()
Saves the map to a file.
net.sf.gridarta.model.mapmodel.MapFile
The location of a map file with a map directory.
Definition: MapFile.java:31
net.sf.gridarta.model.exitconnector
Definition: AbstractExitConnectorModel.java:20
net.sf.gridarta.model.exitconnector.ExitConnectorModel.getExitArchetypeName
String getExitArchetypeName()
Returns the archetype name when creating exit game objects.
net.sf.gridarta.model.mapmanager.MapManager.release
void release(@NotNull MapControl< G, A, R > mapControl)
Releases a MapControl instance.
net.sf.gridarta.actions.ExitConnectorActions
Utility class implementing actions that operate on ExitConnectorModels.
Definition: ExitConnectorActions.java:47
net.sf.gridarta.actions.ExitConnectorActions.exitConnectorModel
final ExitConnectorModel exitConnectorModel
The ExitConnectorModel to use.
Definition: ExitConnectorActions.java:53
net.sf.gridarta.actions.ExitConnectorActions.archetypeSet
final ArchetypeSet< G, A, R > archetypeSet
The ArchetypeSet to use.
Definition: ExitConnectorActions.java:65
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.exitconnector.ExitConnectorModel.isAutoCreateExit
boolean isAutoCreateExit()
Returns whether exit game objects should be auto-created when needed.
net.sf.gridarta.actions.ExitConnectorActions.insertionModeSet
final InsertionModeSet< G, A, R > insertionModeSet
The InsertionModeSet to use.
Definition: ExitConnectorActions.java:83
net.sf.gridarta.model.mapcontrol.MapControl.getUseCounter
int getUseCounter()
Returns the use counter.
net.sf.gridarta.model.exitconnector.ExitConnectorModel.getExitLocation
ExitLocation getExitLocation()
Returns the remembered exit location.
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.actions.ExitConnectorActions.pasteExit
boolean pasteExit(@NotNull final Point location, @NotNull final MapModel< G, A, R > mapModel, @NotNull final BaseObject< G, A, R, ?> archetype, @NotNull final ExitLocation exitLocation, @Nullable final MapFile mapFile)
Creates a new exit game object.
Definition: ExitConnectorActions.java:339
net.sf.gridarta.model.mapmodel.MapModel.getMapFile
MapFile getMapFile()
Returns the map file.
net.sf.gridarta.model.exitconnector.ExitConnectorModel
Stores information needed by the exit connector.
Definition: ExitConnectorModel.java:29
net.sf.gridarta.actions.ExitConnectorActions.exitMatcher
final ExitMatcher< G, A, R > exitMatcher
The ExitMatcher to use.
Definition: ExitConnectorActions.java:59
net.sf.gridarta.model.archetypeset
Definition: ArchetypeSet.java:20
net.sf.gridarta.actions.ExitConnectorActions.pasteExit
void pasteExit(@NotNull final BaseObject<?, ?, ?, ?> gameObject, @NotNull final MapModel< G, A, R > mapModel, @NotNull final ExitLocation exitLocation, @Nullable final MapFile gameObjectMapFile)
Pastes exit information into an exit game object.
Definition: ExitConnectorActions.java:320
net.sf.gridarta.model.exitconnector.ExitConnectorModel.isPasteExitName
boolean isPasteExitName()
Returns whether the exit name should be updated.
net.sf.gridarta.actions.ExitConnectorActions.doExitConnect
boolean doExitConnect(final boolean performAction, @NotNull final MapControl< G, A, R > mapControl, @NotNull final Point targetLocation)
Executes the "exit connect" action.
Definition: ExitConnectorActions.java:189
net.sf.gridarta.actions.ExitConnectorActions.doExitPaste
boolean doExitPaste(final boolean performAction, @NotNull final MapControl< G, A, R > mapControl, @NotNull final Point targetLocation)
Executes the "exit paste" action.
Definition: ExitConnectorActions.java:133