mirror of https://github.com/FreeCol/freecol.git
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.
This commit is contained in:
parent
e601fb1dcd
commit
e80cc21a55
|
@ -0,0 +1,46 @@
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
|
||||||
|
<xs:annotation>
|
||||||
|
<xs:documentation>
|
||||||
|
|
||||||
|
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
|
||||||
|
</xs:documentation>
|
||||||
|
</xs:annotation>
|
||||||
|
|
||||||
|
<xs:include schemaLocation="data-common.xsd" />
|
||||||
|
|
||||||
|
<xs:element name="areas">
|
||||||
|
<xs:complexType>
|
||||||
|
<xs:choice minOccurs="0" maxOccurs="unbounded">
|
||||||
|
<xs:element name="area">
|
||||||
|
<xs:complexType>
|
||||||
|
<xs:choice minOccurs="0" maxOccurs="unbounded">
|
||||||
|
<xs:element name="tileReference">
|
||||||
|
<xs:complexType>
|
||||||
|
<xs:attribute name="id" type="TileId" use="required"/>
|
||||||
|
</xs:complexType>
|
||||||
|
</xs:element>
|
||||||
|
</xs:choice>
|
||||||
|
<xs:attribute name="id" type="AreaId" use="required"/>
|
||||||
|
</xs:complexType>
|
||||||
|
</xs:element>
|
||||||
|
</xs:choice>
|
||||||
|
</xs:complexType>
|
||||||
|
</xs:element>
|
||||||
|
|
||||||
|
</xs:schema>
|
|
@ -29,6 +29,12 @@
|
||||||
</xs:restriction>
|
</xs:restriction>
|
||||||
</xs:simpleType>
|
</xs:simpleType>
|
||||||
|
|
||||||
|
<xs:simpleType name="AreaId">
|
||||||
|
<xs:restriction base="xs:int">
|
||||||
|
<xs:pattern value="model\.area\.(.)+" />
|
||||||
|
</xs:restriction>
|
||||||
|
</xs:simpleType>
|
||||||
|
|
||||||
<xs:simpleType name="BuildingId">
|
<xs:simpleType name="BuildingId">
|
||||||
<xs:restriction base="xs:string">
|
<xs:restriction base="xs:string">
|
||||||
<xs:pattern value="building:([0-9])+" />
|
<xs:pattern value="building:([0-9])+" />
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<xs:annotation>
|
<xs:annotation>
|
||||||
<xs:documentation>
|
<xs:documentation>
|
||||||
|
|
||||||
Copyright (C) 2002-2021 The FreeCol Team
|
Copyright (C) 2002-2024 The FreeCol Team
|
||||||
|
|
||||||
This file is part of FreeCol.
|
This file is part of FreeCol.
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@
|
||||||
<xs:include schemaLocation="data-player.xsd" />
|
<xs:include schemaLocation="data-player.xsd" />
|
||||||
<xs:include schemaLocation="data-map.xsd" />
|
<xs:include schemaLocation="data-map.xsd" />
|
||||||
<xs:include schemaLocation="data-modelMessage.xsd" />
|
<xs:include schemaLocation="data-modelMessage.xsd" />
|
||||||
|
<xs:include schemaLocation="data-areas.xsd" />
|
||||||
|
|
||||||
<xs:element name="game">
|
<xs:element name="game">
|
||||||
<xs:annotation>
|
<xs:annotation>
|
||||||
|
@ -46,6 +47,7 @@
|
||||||
<xs:element ref="nationOptions" />
|
<xs:element ref="nationOptions" />
|
||||||
<xs:element name="newWorld" />
|
<xs:element name="newWorld" />
|
||||||
<xs:element ref="player" />
|
<xs:element ref="player" />
|
||||||
|
<xs:element ref="areas" />
|
||||||
</xs:choice>
|
</xs:choice>
|
||||||
|
|
||||||
<xs:attribute name="id" type="GameId" use="required"/>
|
<xs:attribute name="id" type="GameId" use="required"/>
|
||||||
|
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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<Tile> 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<Tile> 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 <T extends FreeColObject> 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() + "]";
|
||||||
|
}
|
||||||
|
}
|
|
@ -167,6 +167,12 @@ public class Game extends FreeColGameObject {
|
||||||
/** The map of the New World. */
|
/** The map of the New World. */
|
||||||
protected Map map = null;
|
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<String, Area> areas = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The current nation options. Mainly used to see if a player
|
* The current nation options. Mainly used to see if a player
|
||||||
* nation is available.
|
* nation is available.
|
||||||
|
@ -1495,6 +1501,51 @@ public class Game extends FreeColGameObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<Area> 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
|
// 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
|
// 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
|
// 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.
|
// 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 CIBOLA_TAG = "cibola";
|
||||||
private static final String CLIENT_USER_NAME_TAG = "clientUserName";
|
private static final String CLIENT_USER_NAME_TAG = "clientUserName";
|
||||||
private static final String CURRENT_PLAYER_TAG = "currentPlayer";
|
private static final String CURRENT_PLAYER_TAG = "currentPlayer";
|
||||||
|
@ -1674,7 +1726,15 @@ public class Game extends FreeColGameObject {
|
||||||
if (unknown != null) unknown.toXML(xw);
|
if (unknown != null) unknown.toXML(xw);
|
||||||
|
|
||||||
Map map = getMap();
|
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)) {
|
} else if (Specification.TAG.equals(tag)) {
|
||||||
setSpecification(new Specification(xr));
|
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 {
|
} else {
|
||||||
super.readChild(xr);
|
super.readChild(xr);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue