From 485e6260833571cdf68918d171738c0649409cd5 Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Mon, 22 Jul 2024 08:58:25 -0700 Subject: [PATCH] Add doc strings & code cleanup --- .../stendhal/common/CollisionDetection.java | 103 ++++++++------ src/games/stendhal/common/CollisionMap.java | 126 ++++++++++++++---- .../server/core/config/ZonesXMLLoader.java | 3 - .../server/core/engine/StendhalRPZone.java | 20 ++- 4 files changed, 177 insertions(+), 75 deletions(-) diff --git a/src/games/stendhal/common/CollisionDetection.java b/src/games/stendhal/common/CollisionDetection.java index 7022aa0571e..585f95e4459 100644 --- a/src/games/stendhal/common/CollisionDetection.java +++ b/src/games/stendhal/common/CollisionDetection.java @@ -1,6 +1,6 @@ /* $Id$ */ /*************************************************************************** - * (C) Copyright 2003 - Marauroa * + * (C) Copyright 2003-2024 - Marauroa * *************************************************************************** *************************************************************************** * * @@ -12,7 +12,6 @@ ***************************************************************************/ package games.stendhal.common; - import java.awt.geom.Rectangle2D; import games.stendhal.common.tiled.LayerDefinition; @@ -22,10 +21,10 @@ * not with any of the non trespasable areas of the world. */ public class CollisionDetection { + private CollisionMap map; private int width; - private int height; /** @@ -40,8 +39,10 @@ public void clear() { /** * Initialize the collision map to desired size. * - * @param width width of the map - * @param height height of the map + * @param width + * Width of the map. + * @param height + * Height of the map. */ public void init(final int width, final int height) { if (this.width != width || this.height != height) { @@ -59,8 +60,10 @@ public void init(final int width, final int height) { /** * Set a position in the collision map to static collision. * - * @param x x coordinate - * @param y y coordinate + * @param x + * X coordinate. + * @param y + * Y coordinate. */ public void setCollide(final int x, final int y) { if ((x < 0) || (x >= width) || (y < 0) || (y >= height)) { @@ -72,7 +75,8 @@ public void setCollide(final int x, final int y) { /** * Fill the collision map from layer data. * - * @param collisionLayer static collision information + * @param collisionLayer + * Static collision information. */ public void setCollisionData(final LayerDefinition collisionLayer) { // First we build the int array. @@ -83,7 +87,7 @@ public void setCollisionData(final LayerDefinition collisionLayer) { for (int x = 0; x < width; x++) { /* * NOTE: Right now our collision detection system is binary, so - * something or is blocked or is not. + * something is blocked or is not. */ if (collisionLayer.getTileAt(x, y) != 0) { map.set(x, y); @@ -119,9 +123,10 @@ public void printaround(final int x, final int y, final int size) { /** * Check if a rectangle is at least partially outside the map. * - * @param shape area to be checked - * @return true if shape is at least partially outside the map, - * false otherwise + * @param shape + * Area to be checked. + * @return + * {@code true} if shape is at least partially outside the map, {@code false} otherwise. */ public boolean leavesZone(final Rectangle2D shape) { final double x = shape.getX(); @@ -132,12 +137,34 @@ public boolean leavesZone(final Rectangle2D shape) { return (x < 0) || (x + w > width) || (y < 0) || (y + h > height); } + /** + * Get the width of the collision map. + * + * @return + * Map width. + */ + public int getWidth() { + return width; + } + + /** + * Get the height of the collision map. + * + * @return + * Map height. + */ + public int getHeight() { + return height; + } + /** * Check if a rectangle overlaps colliding areas. * - * @param shape checked area - * @return true if the shape enters in any of the non - * trespassable areas of the map, false otherwise + * @param shape + * Checked Area + * @return + * {@codetrue} if the shape enters in any of the non-trespassable areas of the map, + * {@code false} otherwise. */ public boolean collides(final Rectangle2D shape) { final double x = shape.getX(); @@ -147,16 +174,20 @@ public boolean collides(final Rectangle2D shape) { return collides(x, y, w, h); } - /** * Check if a rectangle overlaps colliding areas. * - * @param x x-position - * @param y y-position - * @param w width - * @param h height - * @return true if the shape enters in any of the non - * trespassable areas of the map, false otherwise + * @param x + * Rectangle X position. + * @param y + * Rectangle Y position. + * @param w + * Rectangle width. + * @param h + * Rectangle height. + * @return + * {@code true} if the shape enters in any of the non-trespassable areas of the map, + * {@code false} otherwise. */ public boolean collides(final double x, final double y, final double w, final double h) { /* @@ -178,10 +209,12 @@ public boolean collides(final double x, final double y, final double w, final do /** * Check if a location is marked with collision. * - * @param x x coordinate - * @param y y coordinate - * @return true if the map position is a collision tile, - * otherwise false + * @param x + * X coordinate. + * @param y + * Y coordinate. + * @return + * {@code true} if the map position is a collision tile, otherwise {@code false}. */ public boolean collides(final int x, final int y) { if ((x < 0) || (x >= width)) { @@ -193,22 +226,4 @@ public boolean collides(final int x, final int y) { } return map.get(x, y); } - - /** - * Get the width of the collision map. - * - * @return width - */ - public int getWidth() { - return width; - } - - /** - * Get the height of the collision map. - * - * @return height - */ - public int getHeight() { - return height; - } } diff --git a/src/games/stendhal/common/CollisionMap.java b/src/games/stendhal/common/CollisionMap.java index 4f19386f9f9..f5b95d351e9 100644 --- a/src/games/stendhal/common/CollisionMap.java +++ b/src/games/stendhal/common/CollisionMap.java @@ -1,6 +1,6 @@ /* $Id$ */ /*************************************************************************** - * (C) Copyright 2003-2023 - Stendhal * + * (C) Copyright 2003-2024 - Stendhal * *************************************************************************** *************************************************************************** * * @@ -19,12 +19,24 @@ import games.stendhal.common.tiled.LayerDefinition; + +/** + * A map containing collision information. + */ public class CollisionMap { private final int width; private final int height; private final BitSet[] colls; + /** + * Creates a new empty collision map. + * + * @param width + * Node width of new map. + * @param height. + * Node height of new map. + */ public CollisionMap(final int width, final int height) { this.width = width; this.height = height; @@ -35,6 +47,12 @@ public CollisionMap(final int width, final int height) { } + /** + * Creates a new collision map using layers definition. + * + * @param layer + * Definition used to create map. + */ public CollisionMap(final LayerDefinition layer) { this(layer.getWidth(), layer.getHeight()); for (int x = 0; x < width; x++) { @@ -46,23 +64,94 @@ public CollisionMap(final LayerDefinition layer) { } } + /** + * Retrieves width of collision map. + */ public int getWidth() { return width; } + /** + * Retrieves height of collision map. + */ public int getHeight() { return height; } + /** + * Checks if node is a collision tile. + * + * @param i + * Node X coordinate. + * @param j + * Node Y coordinate. + * @return + * {@code true} if node has collision. + */ public boolean get(final int i, final int j) { return colls[i].get(j); } + /** + * Sets a collision node. + * + * @param i + * Node X coordinate. + * @param j + * Node Y coordinate. + */ public void set(final int i, final int j) { - colls[i].set(j); } + /** + * Sets collision for a rectangle area. + * + * @param shape + * Area to be set. + */ + public void set(final Rectangle2D shape) { + int y = (int) shape.getY(); + for (int x = (int) shape.getX(); x < shape.getX() + shape.getWidth(); x++) { + colls[x].set(y, (int) (y + shape.getHeight())); + } + } + + /** + * Removes collision from a node. + * + * @param i + * Node X coordinate. + * @param k + * Node Y coordinate. + */ + public void unset(final int i, final int k) { + colls[i].clear(k); + } + + /** + * Removes all collision from the map. + */ + public void clear() { + for (int i = 0; i < this.width; i++) { + colls[i].clear(); + } + } + + /** + * Checks for collision in a rectangle area. + * + * @param x + * Beginning node X coordinate. + * @param y + * Beginning node Y coordinate. + * @param width + * Width of area to be checked. + * @param height + * Height of area to be checked. + * @return + * {@code true} if collision is found in area. + */ public boolean collides(final int x, final int y, final int width, final int height) { if (x < 0 || x - 1 + width >= this.width) { return true; @@ -80,16 +169,16 @@ public boolean collides(final int x, final int y, final int width, final int hei return !result.get(y, y + height).isEmpty(); } - public void clear() { - for (int i = 0; i < this.width; i++) { - colls[i].clear(); - } - - } + /** + * Creates a new collision map. + * + * @param layer + * Layer definition. + * @return + * Resulting collision map. + */ public static CollisionMap create(final LayerDefinition layer) { - - CollisionMap collissionMap = new CollisionMap(layer.getWidth(), layer - .getHeight()); + CollisionMap collissionMap = new CollisionMap(layer.getWidth(), layer.getHeight()); for (int x = 0; x < layer.getWidth(); x++) { for (int y = 0; y < layer.getHeight(); y++) { if (layer.getTileAt(x, y) != 0) { @@ -99,19 +188,4 @@ public static CollisionMap create(final LayerDefinition layer) { } return collissionMap; } - - public void unset(final int i, final int k) { - colls[i].clear(k); - - } - - public void set(final Rectangle2D shape) { - int y = (int) shape.getY(); - for (int x = (int) shape.getX(); x < shape.getX() + shape.getWidth(); x++) { - - colls[x].set(y, (int) (y + shape.getHeight())); - } - - } - } diff --git a/src/games/stendhal/server/core/config/ZonesXMLLoader.java b/src/games/stendhal/server/core/config/ZonesXMLLoader.java index feaba63b608..c199e83f749 100644 --- a/src/games/stendhal/server/core/config/ZonesXMLLoader.java +++ b/src/games/stendhal/server/core/config/ZonesXMLLoader.java @@ -31,9 +31,6 @@ import org.w3c.dom.Element; import org.xml.sax.SAXException; -// -// - import games.stendhal.common.tiled.LayerDefinition; import games.stendhal.common.tiled.StendhalMapStructure; import games.stendhal.server.core.config.zone.AttributesXMLReader; diff --git a/src/games/stendhal/server/core/engine/StendhalRPZone.java b/src/games/stendhal/server/core/engine/StendhalRPZone.java index 0b4a755fe59..5970dfe932b 100644 --- a/src/games/stendhal/server/core/engine/StendhalRPZone.java +++ b/src/games/stendhal/server/core/engine/StendhalRPZone.java @@ -450,12 +450,28 @@ public void setAttributes(ZoneAttributes attr) { attributes = attr; } + /** + * Sets collision information for this zone. + * + * @param name + * Layer name. + * @param collisionLayer + * Layer definition. + */ public void addCollisionLayer(final String name, final LayerDefinition collisionLayer) throws IOException { addToContent(name, collisionLayer.encode()); collisionMap.setCollisionData(collisionLayer); } + /** + * Sets protection information for this zone. + * + * @param name + * Layer name. + * @param protectionLayer + * Layer definition. + */ public void addProtectionLayer(final String name, final LayerDefinition protectionLayer) throws IOException { addToContent(name, protectionLayer.encode()); @@ -1104,9 +1120,9 @@ public boolean collides(final int x, final int y) { * Checks an area for collision. * * @param shape - * Rectangle area. + * Rectangle area. * @return - * true if any collision tiles are found in the area. + * {@code true} if any collision tiles are found in the area. */ public boolean collides(final Rectangle2D shape) { return collisionMap.collides(shape);