Skip to content

Commit

Permalink
Misc junk
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Jul 22, 2024
1 parent bf84949 commit 8666fd2
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 136 deletions.
86 changes: 55 additions & 31 deletions src/games/stendhal/common/CollisionDetection.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
***************************************************************************/
package games.stendhal.common;

import static games.stendhal.common.constants.CollisionType.COLLISION_TYPES;

import java.awt.geom.Rectangle2D;

import org.apache.log4j.Logger;

import games.stendhal.common.constants.CollisionType;
import games.stendhal.common.tiled.LayerDefinition;

Expand All @@ -23,6 +27,9 @@
* not with any of the non trespasable areas of the world.
*/
public class CollisionDetection {

private static final Logger logger = Logger.getLogger(CollisionDetection.class);

private CollisionMap map;

private int width;
Expand Down Expand Up @@ -102,7 +109,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.
Expand All @@ -111,38 +119,54 @@ public void setCollisionData(final LayerDefinition collisionLayer) {

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
final int collisionType = collisionLayer.getTileAt(x, y);

// DEBUG:
//System.out.println("\nCollsion tile: " + collisionType);

if (!testserver) {
/*
* NOTE: Right now our collision detection system is binary, so
* something is blocked or is not.
*/
if (collisionType != 0) {
map.set(x, y);
}
} else {
// DEBUG:
//System.out.println("Collision type: " + (byte) collisionType);
/*
if (collisionType > 0) {
System.out.println("\nNon-standard collision: " + (collisionType + 1) + "\n");
}
*/
/*
* NOTE: Right now our collision detection system is binary, so
* something is blocked or is not.
*/
if (collisionLayer.getTileAt(x, y) != 0) {
map.set(x, y);
}
}
}
}

/**
* Fill the collision map from layer data.
*
* @param collisionLayer
* Static collision information.
* @param gid
* Tileset GID offset.
*/
public void setCollisionData(final LayerDefinition collisionLayer, final Integer gid) {
// First we build the int array.
collisionLayer.build();
init(collisionLayer.getWidth(), collisionLayer.getHeight());

if (collisionType > 0) {
// DEBUG:
/*
if (collisionType > 1) {
System.out.println("\nNon-standard collision: " + collisionType + "\n");
}
*/
if (gid == null) {
logger.debug("collision tileset not found, no collision data available");
return;
}

map.set(x, y, (byte) collisionType);
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
final int tileId = collisionLayer.getTileAt(x, y);

byte collType = (byte) (tileId - gid + 1);
if (collType < 0x00) {
logger.debug("non-standard collision type: " + collType);

// no collision
collType = 0x00;
} else if (collType > COLLISION_TYPES) {
logger.debug("non-standard collision type: " + collType);

// default to normal collision
collType = 0x01;
}

if (collType > 0x00) {
map.set(x, y, collType);
}
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/games/stendhal/common/CollisionMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,6 @@ public void set(final Rectangle2D shape) {
set(shape, CollisionType.NORMAL);
}

/**
* Sets all collision info for map at once.
*
* @param collisionInfo
* Map collision information.
*/
public void set(final byte[][] collisionInfo) {
nodes = collisionInfo;
}

/**
* Removes collision from a node.
*
Expand Down
2 changes: 1 addition & 1 deletion src/games/stendhal/common/constants/CollisionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public enum CollisionType {

private final byte byteValue;

public static final int COLLISION_TYPES = 2;
public static final int COLLISION_TYPES = 3;


private CollisionType(final byte bv) {
Expand Down
75 changes: 12 additions & 63 deletions src/games/stendhal/common/tiled/CollisionLayerDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
***************************************************************************/
package games.stendhal.common.tiled;

import static games.stendhal.common.constants.CollisionType.COLLISION_TYPES;


/**
* The class that stores the definition of a layer with formatted
* collision information.
* The class that stores the definition of a layer set up to
* retrieve collision tileset GID.
*/
public CollisionLayerDefinition extends LayerDefinition {
public class CollisionLayerDefinition extends LayerDefinition {

public CollisionLayerDefinition(final int layerWidth, final int layerHeight,
final int gid) {
Expand All @@ -29,77 +27,28 @@ public CollisionLayerDefinition(final int layerWidth, final int layerHeight,
* Copy constructor.
*/
public CollisionLayerDefinition(final LayerDefinition other) {
super(other.width, other.height);

this.map = other.map;
this.width = other.width;
this.height = other.height;
this.name = other.name;
this.data = other.data;
this.raw = other.raw;
this.collisionInfo = other.collisionInfo;
}

/**
* Initializes collision info if <code>null</code> & sets
* all collision to <code>0x00</code> (no collision).
*/
private void resetCollisionInfo() {
if (collisionInfo == null) {
collisionInfo = new Byte[width][height];
}

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
collisionInfo[x][y] = 0x00;
}
}
}

/**
* Formats collision info using the tileset GID.
*
* @param offset
* Tileset's first GID.
*/
public void setCollisionInfo(final int offset) {
resetCollisionInfo();

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
final Integer tileId = getTileAt(x, y);
if (tileId != null) {
byte collisionType = (byte) (tileId - offset + 1);

// failsafe
if (collisionType < 0x00) {
// no collision
collisionType = 0x00;
} else if (collisionType > COLLISION_TYPES) {
// normal collision
collisionType = 0x01;
}

if (collisionType > 0x00) {
collisionInfo[x][y] = collisionType;
}
}
}
}
setName(other.getName());
this.data = other.expose();
this.raw = other.exposeRaw();
}

/**
* Retrieves the first GID of the collision tileset.
*/
final Integer getTilesetGid() {
Integer gid = null;

for (final TileSetDefinition def: this.map) {
public Integer getTilesetGid() {
for (final TileSetDefinition def: this.map.getTilesets()) {
final String filename = def.getSource();
if (filename != null && (filename.equals("collision.png")
|| filename.endsWith("collision.png"))) {
gid = def.getFirstGid();
return def.getFirstGid();
}
}

return gid;
return null;
}
}
24 changes: 4 additions & 20 deletions src/games/stendhal/common/tiled/LayerDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public class LayerDefinition implements Serializable {
protected StendhalMapStructure map = null;

/** Width of the layer that SHOULD be the same that the width of the map. */
private int width;
protected int width;

/** Height of the layer that SHOULD be the same that the height of the map. */
private int height;
protected int height;

/**
* Name of the layer that MUST be one of the available:
Expand All @@ -61,15 +61,13 @@ public class LayerDefinition implements Serializable {
private String name;

/** The data encoded as int in a array of size width*height .*/
private int[] data = null;
protected int[] data = null;

/**
* The same data in a raw byte array, so we save reencoding it again for
* serialization.
*/
private byte[] raw;

protected Byte[][] collisionInfo = null;
protected byte[] raw;

private final boolean testserver = System.getProperty("stendhal.testserver") != null;

Expand Down Expand Up @@ -267,20 +265,6 @@ public String getName() {
return name;
}

/**
* Checks if this layer has collision information.
*/
public boolean hasCollisionInfo() {
return collisionInfo != null;
}

/**
* Retrievies collision information for this layer.
*/
public Byte[][] getCollisionInfo() {
return collisionInfo;
}

@Override
public void readObject(final InputSerializer in) throws IOException {
name = in.readString();
Expand Down
19 changes: 17 additions & 2 deletions src/games/stendhal/server/core/config/ZonesXMLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
//
//

import games.stendhal.common.tiled.CollisionLayerDefinition;
import games.stendhal.common.tiled.LayerDefinition;
import games.stendhal.common.tiled.StendhalMapStructure;
import games.stendhal.server.core.config.zone.AttributesXMLReader;
Expand Down Expand Up @@ -80,6 +81,9 @@ public final class ZonesXMLLoader {
*/
private final URI uri;

private static final boolean testserver = System.getProperty("stendhal.testserver") != null;


/**
* Create an XML based loader of zones.
* @param uri the zone group file
Expand Down Expand Up @@ -229,12 +233,22 @@ protected StendhalRPZone load(final ZoneDesc desc, final StendhalMapStructure zo
// alternative floor layer for clients that support parallax backgrounds
loadOptionalLayer(zone, zonedata, "0_floor_parallax");

zone.addCollisionLayer(name + ".collision",
zonedata.getLayer("collision"));
if (testserver) {
final CollisionLayerDefinition collisionLayer =
new CollisionLayerDefinition(zonedata.getLayer("collision"));

zone.addCollisionLayer(name + ".collision", collisionLayer,
collisionLayer.getTilesetGid());
} else {
zone.addCollisionLayer(name + ".collision",
zonedata.getLayer("collision"));
}

zone.addProtectionLayer(name + ".protection",
zonedata.getLayer("protection"));

// DEBUG:
/*
if (zone.getName().equals("0_semos_city")) {
System.out.println("\nChecking Semos City collision ...");
final LayerDefinition collisionLayer = zonedata.getLayer("collision");
Expand All @@ -254,6 +268,7 @@ protected StendhalRPZone load(final ZoneDesc desc, final StendhalMapStructure zo
System.out.println();
}
*/

if (desc.isInterior()) {
zone.setPosition();
Expand Down
7 changes: 5 additions & 2 deletions src/games/stendhal/server/core/config/zone/TMXLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private LayerDefinition readLayer(final Node t) throws Exception {

// DEBUG:
final boolean semos_collsion = currentFilename.endsWith("semos/city.tmx")
&& layer.getName().equals("collision");
&& "collision".equals(layer.getName());
/*
if (currentFilename.endsWith("semos/city.tmx")) {
final String layerName = layer.getName();
Expand Down Expand Up @@ -270,6 +270,9 @@ private void buildMap(final Document doc) throws Exception {

// Load the tilesets, properties, layers and objectgroups
for (Node sibs = mapNode.getFirstChild(); sibs != null; sibs = sibs.getNextSibling()) {
final LayerDefinition currentLayer = readLayer(sibs);
//final boolean collisionFlag = ;

if ("tileset".equals(sibs.getNodeName())) {
stendhalMap.addTileset(unmarshalTileset(sibs));
} else if ("layer".equals(sibs.getNodeName())) {
Expand All @@ -278,7 +281,7 @@ private void buildMap(final Document doc) throws Exception {
// ignore invisible locked layers (marked for non-use)
logger.info("Ignoring layer \"" + attributes.getNamedItem("name").getNodeValue() + "\"");
} else {
stendhalMap.addLayer(readLayer(sibs));
stendhalMap.addLayer(currentLayer);
}
}
}
Expand Down
Loading

0 comments on commit 8666fd2

Please sign in to comment.