From e80cc21a555f4ee409a24c577fd409bdb65133e1 Mon Sep 17 00:00:00 2001 From: Stian Grenborgen Date: Sat, 13 Jan 2024 18:08:05 +0100 Subject: [PATCH] Allows areas to be defined for a game. An area is a collection of tiles that can easily be referenced and changed in the map editor. Note that areas are defined on the Game instead of a specific map. The reason for this is that we should be able to have areas crossing different maps if we later allow multiple maps for a game. --- schema/data/data-areas.xsd | 46 ++++ schema/data/data-common.xsd | 6 + schema/data/data-game.xsd | 6 +- src/net/sf/freecol/common/model/Area.java | 284 ++++++++++++++++++++++ src/net/sf/freecol/common/model/Game.java | 71 +++++- 5 files changed, 410 insertions(+), 3 deletions(-) create mode 100644 schema/data/data-areas.xsd create mode 100644 src/net/sf/freecol/common/model/Area.java diff --git a/schema/data/data-areas.xsd b/schema/data/data-areas.xsd new file mode 100644 index 000000000..1dc74f9ba --- /dev/null +++ b/schema/data/data-areas.xsd @@ -0,0 +1,46 @@ + + + + + + Copyright (C) 2002-2024 The FreeCol Team + + This file is part of FreeCol. + + FreeCol is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + FreeCol is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with FreeCol. If not, see http://www.gnu.org/licenses + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/schema/data/data-common.xsd b/schema/data/data-common.xsd index 99a2dc75d..f4d7dd25f 100644 --- a/schema/data/data-common.xsd +++ b/schema/data/data-common.xsd @@ -28,6 +28,12 @@ + + + + + + diff --git a/schema/data/data-game.xsd b/schema/data/data-game.xsd index 2d86576d8..e1c461f8b 100644 --- a/schema/data/data-game.xsd +++ b/schema/data/data-game.xsd @@ -3,7 +3,7 @@ - Copyright (C) 2002-2021 The FreeCol Team + Copyright (C) 2002-2024 The FreeCol Team This file is part of FreeCol. @@ -29,6 +29,7 @@ + @@ -45,7 +46,8 @@ - + + diff --git a/src/net/sf/freecol/common/model/Area.java b/src/net/sf/freecol/common/model/Area.java new file mode 100644 index 000000000..444d1baff --- /dev/null +++ b/src/net/sf/freecol/common/model/Area.java @@ -0,0 +1,284 @@ +/** + * Copyright (C) 2002-2022 The FreeCol Team + * + * This file is part of FreeCol. + * + * FreeCol is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * FreeCol is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FreeCol. If not, see . + */ + +package net.sf.freecol.common.model; + +import java.awt.Color; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import javax.xml.stream.XMLStreamException; + +import net.sf.freecol.common.i18n.Messages; +import net.sf.freecol.common.io.FreeColXMLReader; +import net.sf.freecol.common.io.FreeColXMLWriter; + + +/** + * An area of the map. Areas can be used to define starting locations and other + * behavior where a list of tiles is needed. + * + * Note that areas can overlap. + * + * @see Region + */ +public class Area extends FreeColGameObject { + + public static final String TAG = "area"; + + public static final String PREFIX_PLAYER_STARTING_POSITION = "model.area.starting."; + + private String nameKey = null; + private String name = null; + + private List tiles = new ArrayList<>(); + + + /** + * Creates a new {@code Region} instance. + * + * @param game The enclosing {@code Game}. + */ + public Area(Game game) { + super(game); + } + + /** + * Creates a new {@code Region} instance. + * + * @param game The enclosing {@code Game}. + * @param id The object identifier. + */ + public Area(Game game, String id) { + super(game, id); + } + + /** + * Creates a new {@code Region} instance. + * + * @param game The enclosing {@code Game}. + * @param id The object identifier. + * @param nameKey A key for getting a translatable name of this area. + */ + public Area(Game game, String id, String nameKey) { + super(game, id); + + this.nameKey = nameKey; + } + + public Area(Game game, Area copyFrom) { + super(game, copyFrom.getId()); + this.name = copyFrom.name; + this.nameKey = copyFrom.nameKey; + for (Tile copyFromTile : copyFrom.tiles) { + tiles.add(game.getMap().getTile(copyFromTile.getX(), copyFromTile.getY())); + } + } + + + /** + * Gets a list of tiles within this {@code Area}. + */ + public List getTiles() { + return Collections.unmodifiableList(tiles); + } + + /** + * Adds a new tile to the area. + * @param tile The tile to be added. + */ + public void addTile(Tile tile) { + tiles.add(tile); + } + + /** + * Checks if this area contains the given tile. + * @param tile The tile to checked, + */ + public boolean containsTile(Tile tile) { + return tiles.contains(tile); + } + + /** + * Removes a tile from the area. + * @param tile The tile to be removed. + */ + public void removeTile(Tile tile) { + tiles.remove(tile); + } + + /** + * Gets the name of this area. + */ + public String getName() { + return name; + } + + /** + * Sets the name of this area. + * @param name The name. + */ + public void setName(String name) { + if (nameKey != null) { + throw new IllegalArgumentException("Cannot change the name of areas with a fixed nameKey."); + } + this.name = name; + } + + /** + * Gets a i18n key for naming this area. + * @return The key to be used with {@link Messages#message(String)}. + */ + public String getNameKey() { + return nameKey; + } + + /** + * Checks if this area has no tiles. + * @return {@code true} if there are no tiles attached to this area. + */ + public boolean isEmpty() { + return tiles.isEmpty(); + } + + /** + * Returns the color to be used for displaying the area in the map editor. + * @return The color that can be used by the map editor. + */ + public Color getColor() { + if (getId().startsWith(PREFIX_PLAYER_STARTING_POSITION)) { + final String nationId = getId().substring(PREFIX_PLAYER_STARTING_POSITION.length()); + final Nation nation = getGame().getSpecification().getAlreadyInitializedType(nationId, Nation.class); + if (nation != null) { + return nation.getColor(); + } + } + final int hashCode = getId().hashCode(); + final int r = hashCode % 256; + final int g = (hashCode / 256) % 256; + final int b = (hashCode / (256 * 256)) % 256; + return new Color(r, g, b); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean copyIn(T other) { + Area o = copyInCast(other, Area.class); + if (o == null || !super.copyIn(o)) return false; + final Game game = getGame(); + this.tiles = game.updateRef(tiles); + return true; + } + + + // Serialization + + private static final String TILE_REFERENCE_TAG = "tileReference"; + private static final String NAME_KEY_TAG = "nameKey"; + private static final String NAME_TAG = "name"; + + + /** + * {@inheritDoc} + */ + @Override + protected void writeAttributes(FreeColXMLWriter xw) throws XMLStreamException { + super.writeAttributes(xw); + + if (nameKey != null) { + xw.writeAttribute(NAME_KEY_TAG, nameKey); + } + + if (name != null) { + xw.writeAttribute(NAME_TAG, name); + } + } + + /** + * {@inheritDoc} + */ + @Override + protected void writeChildren(FreeColXMLWriter xw) throws XMLStreamException { + super.writeChildren(xw); + + for (Tile tile : tiles) { + xw.writeStartElement(TILE_REFERENCE_TAG); + xw.writeAttribute(ID_ATTRIBUTE_TAG, tile.getId()); + xw.writeEndElement(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void readAttributes(FreeColXMLReader xr) throws XMLStreamException { + super.readAttributes(xr); + + nameKey = xr.getAttribute(NAME_KEY_TAG, (String)null); + name = xr.getAttribute(NAME_TAG, (String)null); + } + + /** + * {@inheritDoc} + */ + @Override + public void readChildren(FreeColXMLReader xr) throws XMLStreamException { + // Clear containers. + this.tiles = new ArrayList<>(); + + super.readChildren(xr); + } + + /** + * {@inheritDoc} + */ + @Override + public void readChild(FreeColXMLReader xr) throws XMLStreamException { + final String tag = xr.getLocalName(); + + if (TILE_REFERENCE_TAG.equals(tag)) { + tiles.add(xr.makeFreeColObject(getGame(), ID_ATTRIBUTE_TAG, Tile.class, true)); + xr.closeTag(TILE_REFERENCE_TAG); + } else { + super.readChild(xr); + } + } + + /** + * {@inheritDoc} + */ + public String getXMLTagName() { + return TAG; + } + + + // Override Object + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return "[" + getId() + "]"; + } +} diff --git a/src/net/sf/freecol/common/model/Game.java b/src/net/sf/freecol/common/model/Game.java index 645917a95..265ce67fd 100644 --- a/src/net/sf/freecol/common/model/Game.java +++ b/src/net/sf/freecol/common/model/Game.java @@ -166,6 +166,12 @@ public class Game extends FreeColGameObject { /** The map of the New World. */ protected Map map = null; + + /** + * Areas are collections of tiles that can be identified using the + * area's ID. Areas may overlap. + */ + private java.util.Map areas = new HashMap<>(); /** * The current nation options. Mainly used to see if a player @@ -1494,6 +1500,51 @@ public class Game extends FreeColGameObject { throw new XMLStreamException(ex); } } + + /** + * Generates empty areas that should be made available in the map editor. + */ + public void generateDefaultAreas() { + for (Nation nation : getSpecification().getNations()) { + if (nation.isUnknownEnemy()) { + continue; + } + if (nation.getType().isREF()) { + continue; + } + final String nationAreaId = Area.PREFIX_PLAYER_STARTING_POSITION + nation.getId(); + if (!areas.containsKey(nationAreaId)) { + addArea(new Area(this, nationAreaId, nation.getNameKey())); + } + } + } + + /** + * Gets the starting area for the given nation. + * + * @param nation The nation to get the area for. + * @return The {@code Area}, if it has been defined on the map. It not, + * then just {@code null}. + */ + public Area getNationStartingArea(Nation nation) { + final String nationAreaId = Area.PREFIX_PLAYER_STARTING_POSITION + nation.getId(); + return areas.get(nationAreaId); + } + + /** + * Gets a list of all areas in this game. + */ + public List getAreas() { + return new ArrayList<>(areas.values()); + } + + /** + * Adds a new {@code Area} to the game. + * @param area The {@code Area} to be added. + */ + public void addArea(Area area) { + areas.put(area.getId(), area); + } // Override FreeColGameObject @@ -1597,6 +1648,7 @@ public class Game extends FreeColGameObject { // must be written first if the intent is to use that spec in the // game when it is read again. Similarly we try to fail fast // if required to read those fields if a spec has not shown up. + private static final String AREAS_TAG = "areas"; private static final String CIBOLA_TAG = "cibola"; private static final String CLIENT_USER_NAME_TAG = "clientUserName"; private static final String CURRENT_PLAYER_TAG = "currentPlayer"; @@ -1674,7 +1726,15 @@ public class Game extends FreeColGameObject { if (unknown != null) unknown.toXML(xw); Map map = getMap(); - if (map != null) map.toXML(xw); + if (map != null) { + map.toXML(xw); + } + + xw.writeStartElement(AREAS_TAG); + for (Area a : areas.values()) { + a.toXML(xw); + } + xw.writeEndElement(); } /** @@ -1783,6 +1843,15 @@ public class Game extends FreeColGameObject { } else if (Specification.TAG.equals(tag)) { setSpecification(new Specification(xr)); + } else if (AREAS_TAG.equals(tag)) { + try { + while (xr.moreTags()) { + final Area area = xr.readFreeColObject(game, Area.class); + areas.put(area.getId(), area); + } + } catch (XMLStreamException xse) { + logger.log(Level.SEVERE, "nextTag failed at " + tag, xse); + } } else { super.readChild(xr); }